Search in sources :

Example 1 with ToFlatString

use of com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString in project es6draft by anba.

the class ShellFunctions method evalScript.

/**
     * shell-function: {@code evalScript(sourceString, [options])}
     * 
     * @param cx
     *            the execution context
     * @param sourceString
     *            the source string
     * @param options
     *            the options object (optional)
     * @return the evaluation result
     */
@Function(name = "evalScript", arity = 1)
public Object evalScript(ExecutionContext cx, String sourceString, Object options) {
    String name = "";
    int line = 1;
    Realm realm = cx.getRealm();
    if (Type.isObject(options)) {
        ScriptObject opts = Type.objectValue(options);
        Object fileName = Get(cx, opts, "fileName");
        if (!Type.isUndefined(fileName)) {
            name = ToFlatString(cx, fileName);
        }
        Object lineNumber = Get(cx, opts, "lineNumber");
        if (!Type.isUndefined(lineNumber)) {
            line = ToInt32(cx, lineNumber);
        }
        Object g = Get(cx, opts, "global");
        if (!Type.isUndefined(g)) {
            if (!(g instanceof GlobalObject)) {
                throw Errors.newError(cx, "invalid global argument");
            }
            realm = ((GlobalObject) g).getRealm();
        }
        Object r = Get(cx, opts, "realm");
        if (!Type.isUndefined(r)) {
            if (!(r instanceof RealmObject)) {
                throw Errors.newError(cx, "invalid realm argument");
            }
            realm = ((RealmObject) r).getRealm();
        }
    }
    Source source = new Source(name, line);
    Script script = realm.getScriptLoader().script(source, sourceString);
    return script.evaluate(realm);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) Script(com.github.anba.es6draft.Script) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) WeakMapObject(com.github.anba.es6draft.runtime.objects.collection.WeakMapObject) ErrorObject(com.github.anba.es6draft.runtime.objects.ErrorObject) GlobalObject(com.github.anba.es6draft.runtime.objects.GlobalObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ArrayBufferObject(com.github.anba.es6draft.runtime.objects.binary.ArrayBufferObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) RealmObject(com.github.anba.es6draft.runtime.objects.reflect.RealmObject) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) Realm(com.github.anba.es6draft.runtime.Realm) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) Source(com.github.anba.es6draft.runtime.internal.Source) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 2 with ToFlatString

use of com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString in project es6draft by anba.

the class ShellFunctions method disassemble.

/**
     * shell-function: {@code disassemble([function])}
     * 
     * @param cx
     *            the execution context
     * @param caller
     *            the caller context
     * @param args
     *            the arguments
     * @throws IOException
     *             if there was any I/O error
     * @throws MalformedNameException
     *             if the module name cannot be normalized
     */
@Function(name = "disassemble", arity = 1)
public void disassemble(ExecutionContext cx, ExecutionContext caller, Object... args) throws IOException, MalformedNameException {
    DebugInfo debugInfo = null;
    if (args.length == 0) {
        FunctionObject currentFunction = caller.getCurrentFunction();
        Executable currentExec = caller.getCurrentExecutable();
        if (currentFunction != null && currentFunction.getExecutable() == currentExec) {
            debugInfo = currentFunction.getCode().debugInfo();
        } else if (currentExec != null && currentExec.getSourceObject() != null) {
            debugInfo = currentExec.getSourceObject().debugInfo();
        }
    } else if (args[0] instanceof FunctionObject) {
        debugInfo = ((FunctionObject) args[0]).getCode().debugInfo();
    } else {
        String sourceCode = ToFlatString(cx, args[0]);
        boolean isModule = false;
        if (args.length > 1 && Type.isObject(args[1])) {
            isModule = ToBoolean(Get(cx, Type.objectValue(args[1]), "module"));
        }
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        if (isModule) {
            ModuleLoader moduleLoader = cx.getRealm().getModuleLoader();
            SourceIdentifier identifier = moduleLoader.normalizeName("disassemble", null);
            ModuleSource src = new StringModuleSource(identifier, sourceCode);
            SourceTextModuleRecord module = ParseModule(scriptLoader, identifier, src);
            debugInfo = module.getScriptCode().getSourceObject().debugInfo();
        } else {
            Source source = new Source("<disassemble>", 1);
            Script script = scriptLoader.compile(scriptLoader.parseScript(source, sourceCode), "#disassemble");
            debugInfo = script.getSourceObject().debugInfo();
        }
    }
    if (debugInfo != null) {
        PrintWriter writer = cx.getRuntimeContext().getConsole().writer();
        for (DebugInfo.Method method : debugInfo.getMethods()) {
            writer.println(method.disassemble());
        }
    }
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) SharedFunctions.relativePathToScript(com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript) SharedFunctions.loadScript(com.github.anba.es6draft.repl.global.SharedFunctions.loadScript) Script(com.github.anba.es6draft.Script) ModuleLoader(com.github.anba.es6draft.runtime.modules.ModuleLoader) SourceIdentifier(com.github.anba.es6draft.runtime.modules.SourceIdentifier) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) Source(com.github.anba.es6draft.runtime.internal.Source) Executable(com.github.anba.es6draft.Executable) DebugInfo(com.github.anba.es6draft.runtime.internal.DebugInfo) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader) StringModuleSource(com.github.anba.es6draft.runtime.modules.loader.StringModuleSource) ModuleSource(com.github.anba.es6draft.runtime.modules.ModuleSource) PrintWriter(java.io.PrintWriter) Function(com.github.anba.es6draft.runtime.internal.Properties.Function)

Example 3 with ToFlatString

use of com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString in project es6draft by anba.

the class SymbolConstructor method call.

/**
     * 19.4.1.1 Symbol ( [ description ] )
     */
@Override
public Symbol call(ExecutionContext callerContext, Object thisValue, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object description = argument(args, 0);
    /* step 1 (not applicable) */
    /* steps 2-4 */
    String descString = Type.isUndefined(description) ? null : ToFlatString(calleeContext, description);
    /* step 5 */
    return new Symbol(descString);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Symbol(com.github.anba.es6draft.runtime.types.Symbol) BuiltinSymbol(com.github.anba.es6draft.runtime.types.BuiltinSymbol) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)

Example 4 with ToFlatString

use of com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString in project es6draft by anba.

the class FunctionConstructor method functionSourceText.

/**
     * 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
     * 
     * @param cx
     *            the execution context
     * @param args
     *            the function arguments
     * @return the function source text as a tuple {@code <parameters, body>}
     */
public static String[] functionSourceText(ExecutionContext cx, Object... args) {
    /* steps 4-10 */
    int argCount = args.length;
    String p, bodyText;
    if (argCount == 0) {
        p = "";
        bodyText = "";
    } else if (argCount == 1) {
        p = "";
        bodyText = ToFlatString(cx, args[0]);
    } else {
        StringBuilder sb = new StringBuilder();
        Object firstArg = args[0];
        sb.append(ToFlatString(cx, firstArg));
        int k = 2;
        for (; k < argCount; ++k) {
            Object nextArg = args[k - 1];
            String nextArgString = ToFlatString(cx, nextArg);
            sb.append(',').append(nextArgString);
        }
        p = sb.toString();
        bodyText = ToFlatString(cx, args[k - 1]);
    }
    return new String[] { p, bodyText };
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) CompiledObject(com.github.anba.es6draft.compiler.CompiledObject) ToFlatString(com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)

Aggregations

ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)4 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)3 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)3 Script (com.github.anba.es6draft.Script)2 SharedFunctions.loadScript (com.github.anba.es6draft.repl.global.SharedFunctions.loadScript)2 SharedFunctions.relativePathToScript (com.github.anba.es6draft.repl.global.SharedFunctions.relativePathToScript)2 Function (com.github.anba.es6draft.runtime.internal.Properties.Function)2 Source (com.github.anba.es6draft.runtime.internal.Source)2 ModuleSource (com.github.anba.es6draft.runtime.modules.ModuleSource)2 StringModuleSource (com.github.anba.es6draft.runtime.modules.loader.StringModuleSource)2 Executable (com.github.anba.es6draft.Executable)1 CompiledObject (com.github.anba.es6draft.compiler.CompiledObject)1 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)1 Realm (com.github.anba.es6draft.runtime.Realm)1 DebugInfo (com.github.anba.es6draft.runtime.internal.DebugInfo)1 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)1 ModuleLoader (com.github.anba.es6draft.runtime.modules.ModuleLoader)1 SourceIdentifier (com.github.anba.es6draft.runtime.modules.SourceIdentifier)1 SourceTextModuleRecord (com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord)1 ErrorObject (com.github.anba.es6draft.runtime.objects.ErrorObject)1