Search in sources :

Example 1 with Constructor

use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.

the class FunctionCodeGenerator method generateLegacyFunctionConstruct.

/**
     * Generate bytecode for:
     * 
     * <pre>
     * oldCaller = function.getLegacyCaller()
     * oldArguments = function.getLegacyArguments()
     * function.setLegacyCaller(callerContext.getCurrentFunction())
     * try {
     *   thisArgument = OrdinaryCreateFromConstructor(callerContext, newTarget, %ObjectPrototype%)
     *   calleeContext = newFunctionExecutionContext(function, newTarget, thisArgument)
     *   result = OrdinaryCallEvaluateBody(function, argumentsList)
     *   return returnResultOrThis(result)
     * } finally {
     *   function.restoreLegacyProperties(oldCaller, oldArguments)
     * }
     * </pre>
     * 
     * @param node
     *            the function node
     * @param mv
     *            the instruction visitor
     */
private void generateLegacyFunctionConstruct(FunctionNode node, InstructionVisitor mv) {
    final boolean hasArguments = codegen.isEnabled(CompatibilityOption.FunctionArguments);
    final boolean hasCaller = codegen.isEnabled(CompatibilityOption.FunctionCaller);
    Variable<LegacyConstructorFunction> function = mv.getParameter(FUNCTION, LegacyConstructorFunction.class);
    Variable<ExecutionContext> callerContext = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    Variable<Constructor> newTarget = mv.getParameter(NEW_TARGET, Constructor.class);
    Variable<Object[]> arguments = mv.getParameter(ARGUMENTS, Object[].class);
    Variable<ScriptObject> thisArg = mv.newVariable("thisArgument", ScriptObject.class);
    Variable<ExecutionContext> calleeContext = mv.newVariable("calleeContext", ExecutionContext.class);
    Variable<FunctionObject> oldCaller = mv.newVariable("oldCaller", FunctionObject.class);
    Variable<LegacyConstructorFunction.Arguments> oldArguments = mv.newVariable("oldArguments", LegacyConstructorFunction.Arguments.class);
    Variable<Throwable> throwable = mv.newVariable("throwable", Throwable.class);
    // (1) Retrieve 'caller' and 'arguments' and store in local variables
    if (hasCaller) {
        mv.load(function);
        mv.invoke(Methods.LegacyConstructorFunction_getLegacyCaller);
    } else {
        mv.anull();
    }
    mv.store(oldCaller);
    if (hasArguments) {
        mv.load(function);
        mv.invoke(Methods.LegacyConstructorFunction_getLegacyArguments);
    } else {
        mv.anull();
    }
    mv.store(oldArguments);
    // (2) Update 'caller' and 'arguments' properties
    if (hasCaller) {
        setLegacyCaller(function, callerContext, mv);
    }
    if (hasArguments) {
        setLegacyArguments(function, arguments, mv);
    }
    TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
    TryCatchLabel handlerFinally = new TryCatchLabel();
    mv.mark(startFinally);
    {
        // (3) Create this-argument
        ordinaryCreateFromConstructor(callerContext, newTarget, thisArg, mv);
        // (4) Create a new ExecutionContext
        prepareCallAndBindThis(node, calleeContext, function, newTarget, thisArg, mv);
        // (5) Call OrdinaryCallEvaluateBody
        ordinaryCallEvaluateBody(node, calleeContext, function, arguments, mv);
        // (6) Restore 'caller' and 'arguments'
        restoreLegacyProperties(function, oldCaller, oldArguments, mv);
        // (7) Return result value
        returnResultOrThis(thisArg, false, mv);
    }
    mv.mark(endFinally);
    // Exception: Restore 'caller' and 'arguments' and then rethrow exception
    mv.finallyHandler(handlerFinally);
    mv.store(throwable);
    restoreLegacyProperties(function, oldCaller, oldArguments, mv);
    mv.load(throwable);
    mv.athrow();
    mv.tryFinally(startFinally, endFinally, handlerFinally);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Constructor(com.github.anba.es6draft.runtime.types.Constructor) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) LegacyConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)

Example 2 with Constructor

use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.

the class NodeModuleLoader method initialize.

/**
     * Initializes this module loader.
     * 
     * @param realm
     *            the realm instance
     * @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 void initialize(Realm realm) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
    ModuleRecord module = NativeCode.loadModule(realm, "module.jsm");
    Constructor moduleConstructor = NativeCode.getModuleExport(module, "default", Constructor.class);
    setModuleConstructor(moduleConstructor);
}
Also used : ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) Constructor(com.github.anba.es6draft.runtime.types.Constructor)

Example 3 with Constructor

use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.

the class TypedArrayConstructorPrototype method construct.

/**
     * 22.2.1.1 %TypedArray% ( )
     */
@Override
public ScriptObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    /* step 3 */
    if (newTarget == this) {
        throw newTypeError(calleeContext, Messages.Key.TypedArrayCreate);
    }
    /* step 4 */
    ScriptObject super_ = getPrototypeOf(calleeContext);
    /* step 5 */
    if (!IsConstructor(super_)) {
        throw newTypeError(calleeContext, Messages.Key.NotConstructor);
    }
    /* steps 6-7 */
    return ((Constructor) super_).construct(calleeContext, newTarget, args);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Constructor(com.github.anba.es6draft.runtime.types.Constructor) BuiltinConstructor(com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor)

Example 4 with Constructor

use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.

the class ArrayBufferConstructor method CloneArrayBuffer.

/**
     * 24.1.1.4 CloneArrayBuffer (srcBuffer, srcByteOffset)
     * 
     * @param cx
     *            the execution context
     * @param srcBuffer
     *            the source buffer
     * @param srcByteOffset
     *            the source offset
     * @param cloneConstructor
     *            the intrinsic constructor function
     * @return the new array buffer object
     */
public static ArrayBufferObject CloneArrayBuffer(ExecutionContext cx, ArrayBuffer srcBuffer, long srcByteOffset, Intrinsics cloneConstructor) {
    /* step 1 (implicit) */
    /* steps 2-3 */
    Constructor bufferConstructor;
    if (cloneConstructor == null) {
        /* steps 2.a-b */
        bufferConstructor = SpeciesConstructor(cx, srcBuffer, Intrinsics.ArrayBuffer);
        /* step 2.c */
        if (IsDetachedBuffer(srcBuffer)) {
            throw newTypeError(cx, Messages.Key.BufferDetached);
        }
    } else {
        /* step 3 */
        assert IsConstructor(cx.getIntrinsic(cloneConstructor));
        bufferConstructor = (Constructor) cx.getIntrinsic(cloneConstructor);
    }
    /* step 4 */
    ByteBuffer srcBlock = srcBuffer.getData();
    /* step 5 */
    long srcLength = srcBuffer.getByteLength();
    /* step 6 */
    assert srcByteOffset <= srcLength;
    /* step 7 */
    long cloneLength = srcLength - srcByteOffset;
    /* steps 8-9 */
    ArrayBufferObject targetBuffer = AllocateArrayBuffer(cx, bufferConstructor, cloneLength);
    /* step 10 */
    if (IsDetachedBuffer(srcBuffer)) {
        throw newTypeError(cx, Messages.Key.BufferDetached);
    }
    /* step 11 */
    ByteBuffer targetBlock = targetBuffer.getData();
    /* step 12 */
    CopyDataBlockBytes(targetBlock, 0, srcBlock, srcByteOffset, cloneLength);
    /* step 13 */
    return targetBuffer;
}
Also used : BuiltinConstructor(com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor) SpeciesConstructor(com.github.anba.es6draft.runtime.AbstractOperations.SpeciesConstructor) Constructor(com.github.anba.es6draft.runtime.types.Constructor) IsConstructor(com.github.anba.es6draft.runtime.AbstractOperations.IsConstructor) ByteBuffer(java.nio.ByteBuffer)

Example 5 with Constructor

use of com.github.anba.es6draft.runtime.types.Constructor in project es6draft by anba.

the class PropertiesTest method createCustomClassWithSubclass.

@Test
public void createCustomClassWithSubclass() {
    Constructor customClass = Properties.createClass(cx, "CustomClassWithSubclass", CustomClassWithSubclass.ConstructorProperties.class, CustomClassWithSubclass.PrototypeProperties.class);
    ScriptObject object = customClass.construct(cx);
    assertNotNull(object);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Constructor(com.github.anba.es6draft.runtime.types.Constructor) OrdinaryCreateFromConstructor(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject.OrdinaryCreateFromConstructor) Test(org.junit.Test)

Aggregations

Constructor (com.github.anba.es6draft.runtime.types.Constructor)33 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)21 OrdinaryCreateFromConstructor (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject.OrdinaryCreateFromConstructor)11 Test (org.junit.Test)11 BuiltinConstructor (com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor)9 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)6 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4 Intrinsics (com.github.anba.es6draft.runtime.types.Intrinsics)4 SpeciesConstructor (com.github.anba.es6draft.runtime.AbstractOperations.SpeciesConstructor)3 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)3 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)2 IsConstructor (com.github.anba.es6draft.runtime.AbstractOperations.IsConstructor)2 CheckConstructor (com.github.anba.es6draft.runtime.language.CallOperations.CheckConstructor)2 ModuleRecord (com.github.anba.es6draft.runtime.modules.ModuleRecord)2 ArrayBufferConstructor (com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 MakeClassConstructor (com.github.anba.es6draft.runtime.types.builtins.FunctionObject.MakeClassConstructor)2 MakeConstructor (com.github.anba.es6draft.runtime.types.builtins.FunctionObject.MakeConstructor)2 LegacyConstructorFunction (com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)2 MethodHandle (java.lang.invoke.MethodHandle)2