Search in sources :

Example 16 with Script

use of com.github.anba.es6draft.Script in project es6draft by anba.

the class Properties method createConstructor.

private static OrdinaryObject createConstructor(ExecutionContext cx, String className, OrdinaryObject proto, Converter converter, ObjectLayout layout) {
    Entry<Function, MethodHandle> constructorEntry = findConstructor(layout);
    if (constructorEntry != null) {
        // User supplied method, perform manual ClassDefinitionEvaluation for constructors
        Function function = constructorEntry.getKey();
        MethodHandle unreflect = constructorEntry.getValue();
        MethodHandle callMethod = getConstructorStaticMethodHandle(cx, converter, unreflect, MethodKind.Call);
        MethodHandle constructMethod = getConstructorStaticMethodHandle(cx, converter, unreflect, MethodKind.Construct);
        NativeConstructor constructor = new NativeConstructor(cx.getRealm(), className, function.arity(), callMethod, constructMethod);
        constructor.defineOwnProperty(cx, "prototype", new PropertyDescriptor(proto, false, false, false));
        proto.defineOwnProperty(cx, "constructor", new PropertyDescriptor(constructor, true, false, true));
        return constructor;
    }
    // Create default constructor
    String sourceText = String.format("(class %s { })", sanitizeName(className));
    ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
    Script script = scriptLoader.script(new Source("<Constructor>", 1), sourceText);
    Object constructor = script.evaluate(cx);
    assert constructor instanceof OrdinaryConstructorFunction : constructor.getClass();
    return (OrdinaryConstructorFunction) constructor;
}
Also used : NativeFunction(com.github.anba.es6draft.runtime.types.builtins.NativeFunction) OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) BuiltinFunction(com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction) NativeTailCallFunction(com.github.anba.es6draft.runtime.types.builtins.NativeTailCallFunction) NativeConstructor(com.github.anba.es6draft.runtime.types.builtins.NativeConstructor) Script(com.github.anba.es6draft.Script) OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) PropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor) AccessorPropertyDescriptor(com.github.anba.es6draft.runtime.types.PropertyDescriptor.AccessorPropertyDescriptor) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) MethodHandle(java.lang.invoke.MethodHandle)

Example 17 with Script

use of com.github.anba.es6draft.Script in project es6draft by anba.

the class TestGlobals method newGlobal.

public final GLOBAL newGlobal(Console console, TEST test) throws MalformedNameException, ResolutionException, IOException, URISyntaxException {
    RuntimeContext context = createContext(console, test);
    World world = new World(context);
    Realm realm = world.newInitializedRealm();
    // Evaluate additional initialization scripts and modules
    TestModuleLoader<?> moduleLoader = (TestModuleLoader<?>) world.getModuleLoader();
    for (ModuleRecord module : modules.allModules) {
        moduleLoader.defineFromTemplate(module, realm);
    }
    for (ModuleRecord module : modules.mainModules) {
        ModuleRecord testModule = moduleLoader.get(module.getSourceCodeId(), realm);
        testModule.instantiate();
        testModule.evaluate();
    }
    for (Script script : scripts) {
        script.evaluate(realm);
    }
    @SuppressWarnings("unchecked") GLOBAL global = (GLOBAL) realm.getGlobalObject();
    return global;
}
Also used : Script(com.github.anba.es6draft.Script) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) World(com.github.anba.es6draft.runtime.World) Realm(com.github.anba.es6draft.runtime.Realm)

Example 18 with Script

use of com.github.anba.es6draft.Script in project es6draft by anba.

the class TestGlobals method compileScripts.

private List<Script> compileScripts() throws IOException {
    List<?> scriptNames = configuration.getList("scripts", emptyList());
    if (scriptNames.isEmpty()) {
        return Collections.emptyList();
    }
    Path basedir = getBaseDirectory();
    RuntimeContext context = createContext();
    ScriptLoader scriptLoader = new ScriptLoader(context);
    ArrayList<Script> scripts = new ArrayList<>();
    for (String scriptName : nonEmpty(scriptNames)) {
        Source source = new Source(Resources.resourcePath(scriptName, basedir), scriptName, 1);
        Script script = scriptLoader.script(source, Resources.resource(scriptName, basedir));
        scripts.add(script);
    }
    return scripts;
}
Also used : Path(java.nio.file.Path) Script(com.github.anba.es6draft.Script) ArrayList(java.util.ArrayList) RuntimeContext(com.github.anba.es6draft.runtime.internal.RuntimeContext) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) Source(com.github.anba.es6draft.runtime.internal.Source)

Aggregations

Script (com.github.anba.es6draft.Script)18 Realm (com.github.anba.es6draft.runtime.Realm)10 Source (com.github.anba.es6draft.runtime.internal.Source)8 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)5 SharedFunctions.loadScript (com.github.anba.es6draft.repl.global.SharedFunctions.loadScript)4 SharedFunctions.relativePathToScript (com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript)4 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)4 CompilationException (com.github.anba.es6draft.compiler.CompilationException)3 ParserException (com.github.anba.es6draft.parser.ParserException)3 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)3 GlobalObject (com.github.anba.es6draft.runtime.objects.GlobalObject)3 BuiltinFunction (com.github.anba.es6draft.runtime.types.builtins.BuiltinFunction)3 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)3 IOException (java.io.IOException)3 ToSource (com.github.anba.es6draft.repl.SourceBuilder.ToSource)2 ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)2 World (com.github.anba.es6draft.runtime.World)2 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)2 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)2 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)2