use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class ConsoleObject method createConsole.
/**
* Creates a new {@code console} object.
*
* @param realm
* the realm instance
* @return the console object
* @throws IOException
* if there was any I/O error
* @throws URISyntaxException
* the URL is not a valid URI
* @throws MalformedNameException
* if any imported module request cannot be normalized
* @throws ResolutionException
* if any export binding cannot be resolved
*/
public static ScriptObject createConsole(Realm realm) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
ExecutionContext cx = realm.defaultContext();
ModuleRecord module = NativeCode.loadModule(realm, "console.jsm");
ScriptObject console = NativeCode.getModuleExport(module, "default", ScriptObject.class);
Callable inspectFn = Properties.createFunction(cx, new InspectFunction(), InspectFunction.class);
CreateMethodProperty(cx, console, "_inspect", inspectFn);
return console;
}
use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class MozShellFunctions method evaluate.
/**
* shell-function: {@code evaluate(code, [options])}
*
* @param cx
* the execution context
* @param caller
* the caller context
* @param code
* the source code to evaluate
* @param options
* additional options object
* @return the eval result value
*/
@Function(name = "evaluate", arity = 2)
public Object evaluate(ExecutionContext cx, ExecutionContext caller, Object code, Object options) {
if (!(Type.isString(code) && (Type.isUndefined(options) || Type.isObject(options)))) {
throw Errors.newError(cx, "invalid arguments");
}
String sourceCode = Type.stringValue(code).toString();
String sourceName = "@evaluate";
int sourceLine = 1;
boolean noScriptRval = false;
boolean catchTermination = false;
Realm realm = cx.getRealm();
if (Type.isObject(options)) {
ScriptObject opts = Type.objectValue(options);
Object fileName = Get(cx, opts, "fileName");
if (!Type.isUndefined(fileName)) {
sourceName = Type.isNull(fileName) ? "" : ToFlatString(cx, fileName);
}
Object lineNumber = Get(cx, opts, "lineNumber");
if (!Type.isUndefined(lineNumber)) {
sourceLine = ToInt32(cx, lineNumber);
}
Object g = Get(cx, opts, "global");
if (!Type.isUndefined(g)) {
ScriptObject obj = ToObject(cx, g);
if (!(obj instanceof GlobalObject)) {
throw Errors.newError(cx, "invalid global argument");
}
realm = ((GlobalObject) obj).getRealm();
}
noScriptRval = ToBoolean(Get(cx, opts, "noScriptRval"));
catchTermination = ToBoolean(Get(cx, opts, "catchTermination"));
}
Source source = new Source(cx.getRealm().sourceInfo(caller), sourceName, sourceLine);
try {
Script script = realm.getScriptLoader().script(source, sourceCode);
Object result = script.evaluate(realm);
return (!noScriptRval ? result : UNDEFINED);
} catch (ParserException | CompilationException e) {
// Create a script exception from the requested code realm, not from the caller's realm.
throw e.toScriptException(realm.defaultContext());
} catch (ScriptException | StackOverflowError e) {
throw e;
} catch (Error | Exception e) {
if (catchTermination) {
return "terminated";
}
throw Errors.newError(cx, Objects.toString(e.getMessage(), ""));
}
}
use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class WrapperProxy method CreateWrapProxy.
/**
* (extension): CreateWrapProxy
*
* @param cx
* the execution context
* @param target
* the proxy target object
* @param proto
* the proxy protoype object
* @return the new wrapper proxy
*/
public static WrapperProxy CreateWrapProxy(ExecutionContext cx, Object target, Object proto) {
if (!Type.isObject(target)) {
throw newTypeError(cx, Messages.Key.NotObjectType);
}
if (!Type.isObjectOrNull(proto)) {
throw newTypeError(cx, Messages.Key.NotObjectOrNull);
}
ScriptObject proxyTarget = Type.objectValue(target);
ScriptObject prototype = Type.objectValueOrNull(proto);
WrapperProxy proxy;
if (IsCallable(proxyTarget)) {
proxy = new CallableWrapperProxy((Callable) proxyTarget, prototype);
} else {
proxy = new WrapperProxy(proxyTarget, prototype);
}
return proxy;
}
use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class WrapperProxy method hasProperty.
@Override
public boolean hasProperty(ExecutionContext cx, long propertyKey) {
/* modified 9.1.7 [[HasProperty]](P) */
boolean hasOwn = HasOwnProperty(cx, proxyTarget, propertyKey);
if (hasOwn) {
return true;
}
// modified
ScriptObject parent = getPrototype();
if (parent != null) {
return parent.hasProperty(cx, propertyKey);
}
return false;
}
use of com.github.anba.es6draft.runtime.types.ScriptObject in project es6draft by anba.
the class WrapperProxy method set.
@Override
public boolean set(ExecutionContext cx, long propertyKey, Object value, Object receiver) {
/* modified 9.1.9 [[Set] (P, V, Receiver) */
Property ownDesc = proxyTarget.getOwnProperty(cx, propertyKey);
if (ownDesc == null) {
// modified
ScriptObject parent = getPrototype();
if (parent != null) {
return parent.set(cx, propertyKey, value, receiver);
} else {
ownDesc = new Property(UNDEFINED, true, true, true);
}
}
if (ownDesc.isDataDescriptor()) {
if (!ownDesc.isWritable()) {
return false;
}
if (!Type.isObject(receiver)) {
return false;
}
ScriptObject _receiver = Type.objectValue(receiver);
Property existingDescriptor = _receiver.getOwnProperty(cx, propertyKey);
if (existingDescriptor != null) {
PropertyDescriptor valueDesc = new PropertyDescriptor(value);
return _receiver.defineOwnProperty(cx, propertyKey, valueDesc);
} else {
return CreateDataProperty(cx, _receiver, propertyKey, value);
}
}
assert ownDesc.isAccessorDescriptor();
Callable setter = ownDesc.getSetter();
if (setter == null) {
return false;
}
setter.call(cx, receiver, value);
return true;
}
Aggregations