Java Examples for jdk.nashorn.internal.runtime.Undefined
The following java examples will help you to understand the usage of jdk.nashorn.internal.runtime.Undefined. These source code samples are taken from different open source projects.
Example 1
| Project: elasticsearch-lang-javascript-master File: NashornUnwrapper.java View source code |
/**
* Convert an object from a script wrapper value to a serializable value valid outside
* of the Nashorn script processor context.
*
* This includes converting Array objects to Lists of valid objects.
*
* @param value the value to convert from script wrapper object to external object value
* @return unwrapped and converted value
*/
public static Object unwrapValue(Object value) {
if (value == null) {
return null;
}
if (value instanceof NativeArray) {
NativeArray nativeArray = (NativeArray) value;
ArrayData data = nativeArray.getArray();
int length = (int) data.length();
ArrayList<Object> list = new ArrayList<Object>(length);
for (int i = 0; i < length; i++) {
list.add(unwrapValue(data.getObject(i)));
}
return list;
} else if (value instanceof Map) {
Map map = (Map) value;
Map<Object, Object> result = new LinkedHashMap<Object, Object>();
for (Object key : map.keySet()) {
result.put(key, unwrapValue(map.get(key)));
}
return result;
} else if (value instanceof ScriptObject) {
ScriptObject object = (ScriptObject) value;
Map<Object, Object> result = new LinkedHashMap<Object, Object>();
for (Object key : object.getOwnKeys(true)) {
result.put(key, unwrapValue(object.get(key)));
}
return result;
} else if (value instanceof Undefined) {
return null;
} else if (value instanceof ConsString) {
return value.toString();
} else {
return value;
}
}Example 2
| Project: openjdk-master File: CodeGenerator.java View source code |
// literal values
private void loadLiteral(final LiteralNode<?> node, final TypeBounds resultBounds) {
final Object value = node.getValue();
if (value == null) {
method.loadNull();
} else if (value instanceof Undefined) {
method.loadUndefined(resultBounds.within(Type.OBJECT));
} else if (value instanceof String) {
final String string = (String) value;
if (string.length() > MethodEmitter.LARGE_STRING_THRESHOLD / 3) {
// 3 == max bytes per encoded char
loadConstant(string);
} else {
method.load(string);
}
} else if (value instanceof RegexToken) {
loadRegex((RegexToken) value);
} else if (value instanceof Boolean) {
method.load((Boolean) value);
} else if (value instanceof Integer) {
if (!resultBounds.canBeNarrowerThan(Type.OBJECT)) {
method.load((Integer) value);
method.convert(Type.OBJECT);
} else if (!resultBounds.canBeNarrowerThan(Type.NUMBER)) {
method.load(((Integer) value).doubleValue());
} else {
method.load((Integer) value);
}
} else if (value instanceof Double) {
if (!resultBounds.canBeNarrowerThan(Type.OBJECT)) {
method.load((Double) value);
method.convert(Type.OBJECT);
} else {
method.load((Double) value);
}
} else if (node instanceof ArrayLiteralNode) {
final ArrayLiteralNode arrayLiteral = (ArrayLiteralNode) node;
final ArrayType atype = arrayLiteral.getArrayType();
loadArray(arrayLiteral, atype);
globalAllocateArray(atype);
} else {
throw new UnsupportedOperationException("Unknown literal for " + node.getClass() + " " + value.getClass() + " " + value);
}
}Example 3
| Project: crate-master File: JavaScriptUserDefinedFunction.java View source code |
private Object evaluateScriptWithBindings(Bindings bindings, Input<Object>[] values) {
Object[] args = new Object[values.length];
for (int i = 0; i < values.length; i++) {
args[i] = processBytesRefInputIfNeeded(values[i].value());
}
Object result;
try {
result = ((ScriptObjectMirror) bindings.get(info.ident().name())).call(this, args);
} catch (NullPointerException e) {
throw new io.crate.exceptions.ScriptException("The name of the function signature doesn't match the function name in the function definition.", JavaScriptLanguage.NAME);
} catch (ECMAException e) {
throw new io.crate.exceptions.ScriptException(e.getMessage(), e, JavaScriptLanguage.NAME);
}
if (result instanceof ScriptObjectMirror) {
return info.returnType().value(convertScriptResult((ScriptObjectMirror) result));
} else if (result instanceof Undefined) {
return null;
} else {
return info.returnType().value(result);
}
}Example 4
| Project: baasbox-master File: NashornMapper.java View source code |
public ScriptResult convertResult(Object result) throws ScriptEvalException {
if (result == null) {
return ScriptResult.NULL;
} else if (result instanceof Boolean) {
return (Boolean) result ? ScriptResult.TRUE : ScriptResult.FALSE;
} else if (result instanceof String) {
return new ScriptResult((String) result);
} else if (result instanceof ScriptObjectMirror) {
JsonNode node = convertDeepJson(result);
if (node != null) {
return new ScriptResult(node);
} else {
return ScriptResult.NULL;
}
} else if (result instanceof Number) {
return new ScriptResult((Number) result);
} else if (result instanceof Undefined) {
return ScriptResult.NULL;
} else {
BaasBoxLogger.warn("Mirror: %s, of type: %s", result, result.getClass());
return ScriptResult.NULL;
}
}Example 5
| Project: platypus-master File: Scripts.java View source code |
public String toJson(Object aObj) {
assert writeJsonFunc != null : SCRIPT_NOT_INITIALIZED;
if (aObj instanceof Undefined) {
//nashorn JSON parser could not work with undefined.
aObj = null;
}
if (aObj instanceof JSObject || aObj instanceof CharSequence || aObj instanceof Number || aObj instanceof Boolean || aObj instanceof ScriptObject || aObj == null) {
return JSType.toString(writeJsonFunc.call(null, new Object[] { aObj }));
} else {
throw new IllegalArgumentException("Java object couldn't be converted to JSON!");
}
}Example 6
| Project: automately-core-master File: NativeJSContext.java View source code |
/**
* This method is used privately to initialize the correct javax.script.ScriptContext and it's bindings.
* This also calls getCustomObjects from the NativeJSContextFactory so you can implement your own Custom Modules/Objects.
*/
private void initializeGlobals() {
// Load this as default first.
// This is the default context
ctx = engine.getContext();
if (ctx.getBindings(javax.script.ScriptContext.GLOBAL_SCOPE) == null) {
Bindings globalContextBindings = new SimpleBindings();
globalContextBindings.put("__jenginename", engine.getFactory().getEngineName());
globalContextBindings.put("__jengineversion", engine.getFactory().getEngineVersion());
// This is the script config that can be used within your code
if (scriptConfig != null) {
// We must store this as a string.
globalContextBindings.put("__ctxConfig", scriptConfig.encode());
}
ctx.setBindings(globalContextBindings, javax.script.ScriptContext.GLOBAL_SCOPE);
}
// These are the bindings of the global properites
Bindings globalBindings = ctx.getBindings(javax.script.ScriptContext.ENGINE_SCOPE);
nativeModules.clear();
// NOTE: Custom objects exists so we can add customization to the ScriptContext
Map<String, Object> globalVariables = new HashMap<>();
globalVariables.putAll(factory.getCustomObjects(this));
if (!globalVariables.containsKey("httpserver")) {
globalVariables.put("httpserver", HttpServerObject.class);
}
if (!globalVariables.containsKey("httpclient")) {
globalVariables.put("httpclient", HttpClientObject.class);
}
if (!globalVariables.containsKey("smtpclient")) {
globalVariables.put("smtpclient", SmtpClientObject.class);
}
if (!globalVariables.containsKey("tcpsocket")) {
globalVariables.put("tcpsocket", TcpSocketObject.class);
}
if (!globalVariables.containsKey("messagebus")) {
globalVariables.put("messagebus", MessageBusObject.class);
}
if (!globalVariables.containsKey("mb")) {
globalVariables.put("mb", MessageBusObject.class);
}
if (!globalVariables.containsKey("databus")) {
globalVariables.put("databus", DataBusObject.class);
}
if (!globalVariables.containsKey("db")) {
globalVariables.put("db", DataBusObject.class);
}
if (!globalVariables.containsKey("filesystem")) {
globalVariables.put("filesystem", FileSystemObject.class);
}
if (!globalVariables.containsKey("fs")) {
globalVariables.put("fs", FileSystemObject.class);
}
if (!globalVariables.containsKey("container") && ContainerService.initialized()) {
globalVariables.put("container", ContainerManagerObject.class);
}
if (!globalVariables.containsKey("load")) {
globalVariables.put("load", Undefined.getUndefined());
}
if (!globalVariables.containsKey("loadWithNewGlobal")) {
globalVariables.put("loadWithNewGlobal", Undefined.getUndefined());
}
Map<String, Object> newMap = new HashMap<>();
// Let's automatically globalVariables over the new modules
for (String name : globalVariables.keySet()) {
Object obj = globalVariables.get(name);
if (obj instanceof Class && ScriptObject.class.isAssignableFrom(((Class) obj))) {
// This will store the class without converting it so we can access static methods
nativeModules.put(name, toJSObject((Class) obj));
continue;
} else if (obj instanceof Class) {
Object converted = evaluate("var converted = arguments[0].static;\n\nreturn converted;", obj);
newMap.put(name, converted);
continue;
}
newMap.put(name, obj);
}
globalVariables.clear();
globalVariables.putAll(newMap);
globalVariables.putAll(getGlobalObjects());
// Lite jobs should not have access to this.
if (!globalVariables.containsKey("Automately")) {
globalVariables.put("Automately", constructJSObject(AutomatelyObject.class));
}
for (String name : globalVariables.keySet()) {
Object obj = globalVariables.get(name);
if (obj instanceof Class) {
// This will store the class without converting it so we can access static methods
Object converted = toJSObject((Class) obj);
if (converted != null) {
globalVariables.put(name, converted);
continue;
}
}
// Store it without converting it
globalVariables.put(name, obj);
}
// must be a JSObject so we don't cause weird JavaScript errors with Java Methods
if (!(globalVariables.get("Automately") instanceof JSObject)) {
throw new RuntimeException("The Automately object must be a JSObject.");
}
if (user.admin) {
globalVariables.put("_admin_cluster", cluster);
globalVariables.put("_admin_cluster_data", cluster.data());
globalVariables.put("_admin_cluster_config", cluster.config());
globalVariables.put("_admin_user_data", UserData.class);
globalVariables.put("_admin_job_util", JobServer.class);
}
Logger logger = cluster.logger();
for (String name : globalVariables.keySet()) {
Object obj = globalVariables.get(name);
if (obj instanceof Class) {
logger.info("Attempting to convert the class " + name + " to a js object.");
// This will store the class without converting it so we can access static methods
Object converted = evaluate("var converted = arguments[0].static;\n\nreturn converted;", obj);
globalBindings.put(name, converted);
continue;
}
globalBindings.put(name, obj);
}
}Example 7
| Project: rakam-master File: CustomEventMapperHttpService.java View source code |
private NewField getValue(Object value) {
if (value instanceof Undefined) {
return null;
}
if (value instanceof String) {
return new NewField(value, FieldType.STRING);
}
if (value instanceof Double || value instanceof Integer) {
return new NewField(value, FieldType.DOUBLE);
}
if (value instanceof NativeDate) {
return new NewField(NativeDate.getTime(value), FieldType.TIMESTAMP);
}
if (value instanceof NativeNumber) {
return new NewField(((NativeNumber) value).doubleValue(), FieldType.DOUBLE);
}
if (value instanceof Boolean) {
return new NewField(value, FieldType.BOOLEAN);
}
if (value instanceof ScriptObjectMirror) {
ScriptObjectMirror mirror = (ScriptObjectMirror) value;
if (mirror.isEmpty()) {
return null;
}
if (mirror.isArray()) {
Iterator<Object> iterator = mirror.values().iterator();
NewField next = getValue(iterator.next());
while (next == null && iterator.hasNext()) {
next = getValue(iterator.next());
}
if (next == null) {
return null;
}
FieldType fieldType = next.fieldType;
GenericArray<Object> objects = new GenericData.Array(mirror.size(), generateAvroSchema(fieldType.convertToArrayType()));
objects.add(next.value);
while (iterator.hasNext()) {
next = getValue(iterator.next());
if (next.fieldType != fieldType) {
throw new IllegalStateException("Array values must have the same type.");
}
objects.add(next.value);
}
} else {
HashMap<Object, Object> map = new HashMap<>(mirror.size());
FieldType type = null;
for (Map.Entry<String, Object> entry : mirror.entrySet()) {
NewField inlineValue = getValue(entry.getValue());
if (inlineValue == null) {
continue;
}
if (type != null && inlineValue.fieldType != type) {
throw new IllegalStateException("Object values must have the same type.");
}
type = inlineValue.fieldType;
map.put(entry.getKey(), inlineValue.value);
}
return new NewField(map, type.convertToMapValueType());
}
}
throw new IllegalStateException();
}Example 8
| Project: elasticsearch-lang-javascript-nashorn-master File: NashornUnwrapper.java View source code |
/**
* Convert an object from a script wrapper value to a serializable value valid outside
* of the Nashorn script processor context.
*
* This includes converting Array objects to Lists of valid objects.
*
* @param value the value to convert from script wrapper object to external object value
* @return unwrapped and converted value
*/
public static Object unwrapValue(Object value) {
if (value == null) {
return null;
}
if (value instanceof NativeArray) {
NativeArray nativeArray = (NativeArray) value;
ArrayData data = nativeArray.getArray();
int length = (int) data.length();
ArrayList<Object> list = new ArrayList<Object>(length);
for (int i = 0; i < length; i++) {
list.add(unwrapValue(data.getObject(i)));
}
return list;
} else if (value instanceof Map) {
Map map = (Map) value;
Map<Object, Object> result = new LinkedHashMap<Object, Object>();
for (Object key : map.keySet()) {
result.put(key, unwrapValue(map.get(key)));
}
return result;
} else if (value instanceof ScriptObject) {
ScriptObject object = (ScriptObject) value;
Map<Object, Object> result = new LinkedHashMap<Object, Object>();
for (Object key : object.getOwnKeys(true)) {
result.put(key, unwrapValue(object.get(key)));
}
return result;
} else if (value instanceof Undefined) {
return null;
} else if (value instanceof ConsString) {
return value.toString();
} else {
return value;
}
}Example 9
| Project: typescript-generator-master File: SymbolTable.java View source code |
private static boolean isUndefined(Object variable) {
// jdk.nashorn.internal.runtime.Undefined (Java 8)
return variable != null && variable.getClass().getSimpleName().equals("Undefined");
}