Search in sources :

Example 91 with ScriptObject

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

the class TypedArrayConstructor method construct.

/**
     * 22.2.4.1 TypedArray ( )<br>
     * 22.2.4.2 TypedArray ( length )<br>
     * 22.2.4.3 TypedArray ( typedArray )<br>
     * 22.2.4.4 TypedArray ( object )<br>
     * 22.2.4.5 TypedArray ( buffer [ , byteOffset [ , length ] ] )<br>
     */
@Override
public TypedArrayObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    if (args.length == 0) {
        return constructWithNoArguments(calleeContext, newTarget);
    }
    Object arg0 = args[0];
    if (!Type.isObject(arg0)) {
        return constructWithLength(calleeContext, newTarget, arg0);
    }
    if (arg0 instanceof TypedArrayObject) {
        return constructWithTypedArray(calleeContext, newTarget, (TypedArrayObject) arg0);
    }
    if (arg0 instanceof ArrayBuffer) {
        Object byteOffset = argument(args, 1, 0);
        Object length = argument(args, 2);
        return constructWithArrayBuffer(calleeContext, newTarget, (ArrayBuffer) arg0, byteOffset, length);
    }
    return constructWithObject(calleeContext, newTarget, (ScriptObject) arg0);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject)

Example 92 with ScriptObject

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

the class TypedArrayConstructor method TypedArrayCreate.

/**
     * TypedArrayCreate( constructor, argumentList )
     * 
     * @param cx
     *            the execution context
     * @param constructor
     *            the constructor function
     * @param length
     *            the new typed array length
     * @return the new typed array object
     */
public static TypedArrayObject TypedArrayCreate(ExecutionContext cx, Constructor constructor, long length) {
    /* step 1 */
    ScriptObject newObject = constructor.construct(cx, length);
    /* step 2 */
    TypedArrayObject newTypedArray = ValidateTypedArray(cx, newObject);
    /* step 3 */
    if (newTypedArray.getArrayLength() < length) {
        throw newTypeError(cx, Messages.Key.InvalidTypedArrayLength);
    }
    /* step 4 */
    return newTypedArray;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject)

Example 93 with ScriptObject

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

the class TypedArrayConstructor method constructWithTypedArray.

/**
     * 22.2.4.3 TypedArray ( typedArray )
     * 
     * @param cx
     *            the execution context
     * @param newTarget
     *            the NewTarget constructor object
     * @param typedArray
     *            the source typed array object
     * @return the typed array object
     */
private TypedArrayObject constructWithTypedArray(ExecutionContext cx, Constructor newTarget, TypedArrayObject typedArray) {
    /* step 1 (implicit) */
    /* step 2 (not applicable) */
    /* steps 3-4 (TypedArray allocation deferred) */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, prototypeForType(elementType));
    /* step 5 */
    TypedArrayObject srcArray = typedArray;
    /* step 6 */
    ArrayBuffer srcData = srcArray.getBuffer();
    /* step 7 */
    if (IsDetachedBuffer(srcData)) {
        throw newTypeError(cx, Messages.Key.BufferDetached);
    }
    /* steps 8-9 */
    ElementType elementType = this.elementType;
    /* step 10 */
    long elementLength = srcArray.getArrayLength();
    /* steps 11-12 */
    ElementType srcType = srcArray.getElementType();
    /* step 13 */
    int srcElementSize = srcType.size();
    /* step 14 */
    long srcByteOffset = srcArray.getByteOffset();
    /* step 15 */
    int elementSize = elementType.size();
    /* step 16 */
    long byteLength = elementSize * elementLength;
    /* steps 17-18 */
    ArrayBufferObject data;
    if (elementType == srcType) {
        /* step 17 */
        data = CloneArrayBuffer(cx, srcData, srcByteOffset);
    } else {
        /* step 18 */
        /* step 18.a */
        // FIXME: spec bug - SharedArrayBuffers not handled correctly!
        Constructor bufferConstructor = SpeciesConstructor(cx, srcData, Intrinsics.ArrayBuffer);
        /* step 18.b */
        data = AllocateArrayBuffer(cx, bufferConstructor, byteLength);
        /* step 18.c */
        if (IsDetachedBuffer(srcData)) {
            throw newTypeError(cx, Messages.Key.BufferDetached);
        }
        /* step 18.d */
        long srcByteIndex = srcByteOffset;
        /* step 18.e */
        long targetByteIndex = 0;
        /* steps 18.f-g */
        for (long count = elementLength; count > 0; --count) {
            double value = GetValueFromBuffer(srcData, srcByteIndex, srcType);
            SetValueInBuffer(data, targetByteIndex, elementType, value);
            srcByteIndex += srcElementSize;
            targetByteIndex += elementSize;
        }
    }
    /* steps 4, 19-23 */
    return new TypedArrayObject(cx.getRealm(), elementType, data, byteLength, 0, elementLength, proto);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Constructor(com.github.anba.es6draft.runtime.types.Constructor) BuiltinConstructor(com.github.anba.es6draft.runtime.types.builtins.BuiltinConstructor) SpeciesConstructor(com.github.anba.es6draft.runtime.AbstractOperations.SpeciesConstructor) ArrayBufferConstructor(com.github.anba.es6draft.runtime.objects.binary.ArrayBufferConstructor)

Example 94 with ScriptObject

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

the class PluralRulesConstructor method InitializePluralRules.

/**
     * InitializePluralRules (pluralRules, locales, options)
     * 
     * @param cx
     *            the execution context
     * @param pluralRules
     *            the pluralRules object
     * @param locales
     *            the locales array
     * @param opts
     *            the options object
     */
public static void InitializePluralRules(ExecutionContext cx, PluralRulesObject pluralRules, Object locales, Object opts) {
    /* steps 1-2 (not applicable) */
    /* step 3 */
    Set<String> requestedLocales = CanonicalizeLocaleList(cx, locales);
    /* steps 4-5 */
    ScriptObject options;
    if (Type.isUndefined(opts)) {
        options = ObjectCreate(cx, Intrinsics.ObjectPrototype);
    } else {
        options = ToObject(cx, opts);
    }
    /* step 6 */
    OptionsRecord opt = new OptionsRecord(OptionsRecord.MatcherType.forName("lookup"));
    /* step 7 */
    String s = GetStringOption(cx, options, "type", set("cardinal", "ordinal"), "cardinal");
    /* step 8 */
    pluralRules.setType(s);
    /* step 9 */
    PluralRulesLocaleData localeData = new PluralRulesLocaleData();
    /* step 10 */
    ResolvedLocale r = ResolveLocale(cx.getRealm(), getAvailableLocalesLazy(cx), requestedLocales, opt, relevantExtensionKeys, localeData);
    /* step 11 */
    pluralRules.setLocale(r.getLocale());
    /* steps 12-16 (not applicable) */
    /* step 17 */
    pluralRules.setBoundResolve(null);
/* step 18 (not applicable) */
/* step 19 (return) */
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OptionsRecord(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.OptionsRecord) ResolvedLocale(com.github.anba.es6draft.runtime.objects.intl.IntlAbstractOperations.ResolvedLocale)

Example 95 with ScriptObject

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

the class GeneratorFunctionConstructor method CreateDynamicConstructorGenerator.

private static OrdinaryConstructorGenerator CreateDynamicConstructorGenerator(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.Generator;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.Generator, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.generator(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* step 12 */
    boolean strict = function.isStrict();
    /* steps 21-22 */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
    /* step 23 */
    OrdinaryConstructorGenerator f = OrdinaryConstructorGenerator.FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
    /* steps 24-25 */
    LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
    /* step 26 */
    FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
    /* step 27 */
    OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.GeneratorPrototype);
    MakeConstructor(f, true, prototype);
    /* step 28 (not applicable) */
    /* step 29 */
    SetFunctionName(f, "anonymous");
    /* step 30 */
    return f;
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) OrdinaryConstructorGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorGenerator) FunctionConstructor.functionSource(com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource) Source(com.github.anba.es6draft.runtime.internal.Source) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Aggregations

ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)129 Callable (com.github.anba.es6draft.runtime.types.Callable)43 Property (com.github.anba.es6draft.runtime.types.Property)32 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)26 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)21 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)16 Constructor (com.github.anba.es6draft.runtime.types.Constructor)11 Realm (com.github.anba.es6draft.runtime.Realm)9 PropertyDescriptor (com.github.anba.es6draft.runtime.types.PropertyDescriptor)9 ParserException (com.github.anba.es6draft.parser.ParserException)8 CompilationException (com.github.anba.es6draft.compiler.CompilationException)7 Source (com.github.anba.es6draft.runtime.internal.Source)7 ArrayList (java.util.ArrayList)7 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)6 ImmutablePrototypeObject (com.github.anba.es6draft.runtime.types.builtins.ImmutablePrototypeObject)6 OrdinaryCreateFromConstructor (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject.OrdinaryCreateFromConstructor)6 Test (org.junit.Test)6 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)5 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)4 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)4