static EncodedJSValue JSC_HOST_CALL constructArrayBuffer(ExecState* exec)
{
    JSArrayBufferConstructor* constructor =
        jsCast<JSArrayBufferConstructor*>(exec->callee());
    
    unsigned length;
    if (exec->argumentCount()) {
        length = exec->uncheckedArgument(0).toUInt32(exec);
        if (exec->hadException())
            return JSValue::encode(jsUndefined());
    } else {
        // Although the documentation doesn't say so, it is in fact correct to say
        // "new ArrayBuffer()". The result is the same as allocating an array buffer
        // with a zero length.
        length = 0;
    }
    
    RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(length, 1);
    if (!buffer)
        return throwVMError(exec, createOutOfMemoryError(constructor->globalObject()));
    
    JSArrayBuffer* result = JSArrayBuffer::create(
        exec->vm(), constructor->globalObject()->arrayBufferStructure(), buffer);
    
    return JSValue::encode(result);
}
EncodedJSValue JSC_HOST_CALL JSArrayBufferConstructor::constructJSArrayBuffer(ExecState* exec)
{
    JSArrayBufferConstructor* jsConstructor = jsCast<JSArrayBufferConstructor*>(exec->callee());

    int length = 0;
    if (exec->argumentCount() > 0)
        length = exec->argument(0).toInt32(exec); // NaN/+inf/-inf returns 0, this is intended by WebIDL
    RefPtr<ArrayBuffer> buffer;
    if (length >= 0)
        buffer = ArrayBuffer::create(static_cast<unsigned>(length), 1);
    if (!buffer.get())
        return throwVMError(exec, createRangeError(exec, "ArrayBuffer size is not a small enough positive integer."));
    return JSValue::encode(asObject(toJS(exec, jsConstructor->globalObject(), buffer.get())));
}
Ejemplo n.º 3
0
static EncodedJSValue JSC_HOST_CALL constructArrayBuffer(ExecState* exec)
{
    VM& vm = exec->vm();
    auto scope = DECLARE_THROW_SCOPE(vm);

    JSArrayBufferConstructor* constructor =
        jsCast<JSArrayBufferConstructor*>(exec->jsCallee());
    
    unsigned length;
    if (exec->argumentCount()) {
        length = exec->uncheckedArgument(0).toIndex(exec, "length");
        RETURN_IF_EXCEPTION(scope, encodedJSValue());
    } else {
        // Although the documentation doesn't say so, it is in fact correct to say
        // "new ArrayBuffer()". The result is the same as allocating an array buffer
        // with a zero length.
        length = 0;
    }
    
    auto buffer = ArrayBuffer::tryCreate(length, 1);
    if (!buffer)
        return JSValue::encode(throwOutOfMemoryError(exec, scope));
    
    if (constructor->sharingMode() == ArrayBufferSharingMode::Shared)
        buffer->makeShared();
    
    ASSERT(constructor->sharingMode() == buffer->sharingMode());
    
    Structure* arrayBufferStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), constructor->globalObject()->arrayBufferStructure(constructor->sharingMode()));
    RETURN_IF_EXCEPTION(scope, encodedJSValue());
    JSArrayBuffer* result = JSArrayBuffer::create(vm, arrayBufferStructure, WTFMove(buffer));
    
    return JSValue::encode(result);
}