| Index: src/wasm/wasm-js.cc
|
| diff --git a/src/wasm/wasm-js.cc b/src/wasm/wasm-js.cc
|
| index 6dc14952a01e943b8bd5a9fc012c8d648c59dbae..c6f9da17a07a8c46322eaee70ff08649b0c051d8 100644
|
| --- a/src/wasm/wasm-js.cc
|
| +++ b/src/wasm/wasm-js.cc
|
| @@ -35,8 +35,8 @@ struct RawBuffer {
|
| size_t size() { return static_cast<size_t>(end - start); }
|
| };
|
|
|
| -RawBuffer GetRawBufferSource(
|
| - v8::Local<v8::Value> source, ErrorThrower* thrower) {
|
| +RawBuffer GetRawBufferSource(v8::Local<v8::Value> source,
|
| + ErrorThrower* thrower) {
|
| const byte* start = nullptr;
|
| const byte* end = nullptr;
|
|
|
| @@ -66,12 +66,34 @@ RawBuffer GetRawBufferSource(
|
| thrower->Error("ArrayBuffer argument is empty");
|
| }
|
| } else {
|
| - thrower->Error("Argument 0 must be an ArrayBuffer or Uint8Array");
|
| + thrower->Error("Source must be an ArrayBuffer or Uint8Array");
|
| }
|
|
|
| return {start, end};
|
| }
|
|
|
| +i::Handle<i::JSArrayBuffer> GetMemoryArgument(
|
| + const v8::FunctionCallbackInfo<v8::Value>& args, int index) {
|
| + i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null();
|
| + if (args.Length() > index && args[index]->IsArrayBuffer()) {
|
| + Local<Object> obj = Local<Object>::Cast(args[index]);
|
| + i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj);
|
| + memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj));
|
| + }
|
| + return memory;
|
| +}
|
| +
|
| +i::Handle<i::JSObject> GetForeignArgument(
|
| + const v8::FunctionCallbackInfo<v8::Value>& args, int index) {
|
| + i::Handle<i::JSObject> foreign;
|
| + if (args.Length() > index && args[index]->IsObject()) {
|
| + Local<Object> local_foreign = Local<Object>::Cast(args[index]);
|
| + i::Handle<i::Object> foreign_obj = v8::Utils::OpenHandle(*local_foreign);
|
| + foreign = i::Handle<i::JSObject>(i::JSObject::cast(*foreign_obj));
|
| + }
|
| + return foreign;
|
| +}
|
| +
|
| void VerifyModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
| HandleScope scope(args.GetIsolate());
|
| i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
|
| @@ -124,48 +146,31 @@ void VerifyFunction(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
| if (result.val) delete result.val;
|
| }
|
|
|
| -v8::internal::wasm::ZoneBuffer* TranslateAsmModule(
|
| - i::ParseInfo* info, ErrorThrower* thrower,
|
| - i::Handle<i::FixedArray>* foreign_args) {
|
| +static bool ParseAsmModule(i::ParseInfo* info, ErrorThrower* thrower) {
|
| info->set_global();
|
| info->set_lazy(false);
|
| info->set_allow_lazy_parsing(false);
|
| info->set_toplevel(true);
|
|
|
| if (!i::Compiler::ParseAndAnalyze(info)) {
|
| - return nullptr;
|
| + return false;
|
| }
|
|
|
| if (info->scope()->declarations()->length() == 0) {
|
| thrower->Error("Asm.js validation failed: no declarations in scope");
|
| - return nullptr;
|
| + return false;
|
| }
|
|
|
| info->set_literal(
|
| info->scope()->declarations()->at(0)->AsFunctionDeclaration()->fun());
|
| -
|
| - v8::internal::AsmTyper typer(info->isolate(), info->zone(), *(info->script()),
|
| - info->literal());
|
| - if (i::FLAG_enable_simd_asmjs) {
|
| - typer.set_allow_simd(true);
|
| - }
|
| - if (!typer.Validate()) {
|
| - thrower->Error("Asm.js validation failed: %s", typer.error_message());
|
| - return nullptr;
|
| - }
|
| -
|
| - v8::internal::wasm::AsmWasmBuilder builder(info->isolate(), info->zone(),
|
| - info->literal(), &typer);
|
| -
|
| - return builder.Run(foreign_args);
|
| + return true;
|
| }
|
|
|
| i::MaybeHandle<i::JSObject> InstantiateModuleCommon(
|
| - const v8::FunctionCallbackInfo<v8::Value>& args, const byte* start,
|
| - const byte* end, ErrorThrower* thrower,
|
| + i::Isolate* isolate, i::Handle<i::JSArrayBuffer> memory,
|
| + i::Handle<i::JSObject> foreign, const byte* start, const byte* end,
|
| + ErrorThrower* thrower,
|
| internal::wasm::ModuleOrigin origin = i::wasm::kWasmOrigin) {
|
| - i::Isolate* isolate = reinterpret_cast<i::Isolate*>(args.GetIsolate());
|
| -
|
| // Decode but avoid a redundant pass over function bodies for verification.
|
| // Verification will happen during compilation.
|
| i::Zone zone(isolate->allocator());
|
| @@ -179,23 +184,7 @@ i::MaybeHandle<i::JSObject> InstantiateModuleCommon(
|
| thrower->Failed("", result);
|
| } else {
|
| // Success. Instantiate the module and return the object.
|
| - i::Handle<i::JSReceiver> ffi = i::Handle<i::JSObject>::null();
|
| - if (args.Length() > 1 && args[1]->IsObject()) {
|
| - Local<Object> obj = Local<Object>::Cast(args[1]);
|
| - ffi = i::Handle<i::JSReceiver>::cast(v8::Utils::OpenHandle(*obj));
|
| - }
|
| -
|
| - i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null();
|
| - if (args.Length() > 2 && args[2]->IsArrayBuffer()) {
|
| - Local<Object> obj = Local<Object>::Cast(args[2]);
|
| - i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj);
|
| - memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj));
|
| - }
|
| -
|
| - object = result.val->Instantiate(isolate, ffi, memory);
|
| - if (!object.is_null()) {
|
| - args.GetReturnValue().Set(v8::Utils::ToLocal(object.ToHandleChecked()));
|
| - }
|
| + object = result.val->Instantiate(isolate, foreign, memory);
|
| }
|
|
|
| if (result.val) delete result.val;
|
| @@ -218,59 +207,27 @@ void InstantiateModuleFromAsm(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
| i::Handle<i::Script> script = factory->NewScript(Utils::OpenHandle(*source));
|
| i::ParseInfo info(&zone, script);
|
|
|
| - i::Handle<i::Object> foreign;
|
| - if (args.Length() > 1 && args[1]->IsObject()) {
|
| - Local<Object> local_foreign = Local<Object>::Cast(args[1]);
|
| - foreign = v8::Utils::OpenHandle(*local_foreign);
|
| - }
|
| -
|
| - i::Handle<i::FixedArray> foreign_args;
|
| - auto module = TranslateAsmModule(&info, &thrower, &foreign_args);
|
| - if (module == nullptr) {
|
| + if (!ParseAsmModule(&info, &thrower)) {
|
| return;
|
| }
|
|
|
| - i::MaybeHandle<i::Object> maybe_module_object =
|
| - InstantiateModuleCommon(args, module->begin(), module->end(), &thrower,
|
| - internal::wasm::kAsmJsOrigin);
|
| - if (maybe_module_object.is_null()) {
|
| + i::Handle<i::FixedArray> wasm_data;
|
| + if (!i::WasmJs::ConvertAsmToWasm(&info, false, &wasm_data)) {
|
| + thrower.Error("Asm.js failed to validate");
|
| return;
|
| }
|
|
|
| - i::Handle<i::Name> name =
|
| - factory->NewStringFromStaticChars("__foreign_init__");
|
| -
|
| - i::Handle<i::Object> module_object = maybe_module_object.ToHandleChecked();
|
| - i::MaybeHandle<i::Object> maybe_init =
|
| - i::Object::GetProperty(module_object, name);
|
| - DCHECK(!maybe_init.is_null());
|
| + i::Handle<i::JSObject> foreign = GetForeignArgument(args, 1);
|
| + i::Handle<i::JSArrayBuffer> memory = GetMemoryArgument(args, 2);
|
|
|
| - i::Handle<i::Object> init = maybe_init.ToHandleChecked();
|
| - i::Handle<i::Object> undefined = isolate->factory()->undefined_value();
|
| - i::Handle<i::Object>* foreign_args_array =
|
| - new i::Handle<i::Object>[foreign_args->length()];
|
| - for (int j = 0; j < foreign_args->length(); j++) {
|
| - if (!foreign.is_null()) {
|
| - i::MaybeHandle<i::Name> name = i::Object::ToName(
|
| - isolate, i::Handle<i::Object>(foreign_args->get(j), isolate));
|
| - if (!name.is_null()) {
|
| - i::MaybeHandle<i::Object> val =
|
| - i::Object::GetProperty(foreign, name.ToHandleChecked());
|
| - if (!val.is_null()) {
|
| - foreign_args_array[j] = val.ToHandleChecked();
|
| - continue;
|
| - }
|
| - }
|
| - }
|
| - foreign_args_array[j] = undefined;
|
| + i::Handle<i::Object> result;
|
| + if (!i::WasmJs::InstantiateAsmWasm(isolate, wasm_data, memory, foreign,
|
| + &result)) {
|
| + return;
|
| }
|
| - i::MaybeHandle<i::Object> retval = i::Execution::Call(
|
| - isolate, init, undefined, foreign_args->length(), foreign_args_array);
|
| - delete[] foreign_args_array;
|
|
|
| - if (retval.is_null()) {
|
| - thrower.Error(
|
| - "WASM.instantiateModuleFromAsm(): foreign init function failed");
|
| + if (!result.is_null()) {
|
| + args.GetReturnValue().Set(v8::Utils::ToLocal(result));
|
| }
|
| }
|
|
|
| @@ -286,9 +243,16 @@ void InstantiateModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
| RawBuffer buffer = GetRawBufferSource(args[0], &thrower);
|
| if (buffer.start == nullptr) return;
|
|
|
| - InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower);
|
| -}
|
| + i::Handle<i::JSObject> foreign = GetForeignArgument(args, 1);
|
| + i::Handle<i::JSArrayBuffer> memory = GetMemoryArgument(args, 2);
|
|
|
| + i::MaybeHandle<i::Object> result = InstantiateModuleCommon(
|
| + isolate, memory, foreign, buffer.start, buffer.end, &thrower);
|
| +
|
| + if (!result.is_null()) {
|
| + args.GetReturnValue().Set(v8::Utils::ToLocal(result.ToHandleChecked()));
|
| + }
|
| +}
|
|
|
| static i::MaybeHandle<i::JSObject> CreateModuleObject(
|
| v8::Isolate* isolate, const v8::Local<v8::Value> source,
|
| @@ -356,8 +320,8 @@ void WebAssemblyModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
| void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
| HandleScope scope(args.GetIsolate());
|
| v8::Isolate* isolate = args.GetIsolate();
|
| - ErrorThrower thrower(reinterpret_cast<i::Isolate*>(isolate),
|
| - "WebAssembly.Instance()");
|
| + i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
|
| + ErrorThrower thrower(i_isolate, "WebAssembly.Instance()");
|
|
|
| if (args.Length() < 1) {
|
| thrower.Error("Argument 0 must be a WebAssembly.Module");
|
| @@ -374,7 +338,15 @@ void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
| GetRawBufferSource(Utils::ToLocal(source.ToHandleChecked()), &thrower);
|
| if (buffer.start == nullptr) return;
|
|
|
| - InstantiateModuleCommon(args, buffer.start, buffer.end, &thrower);
|
| + i::Handle<i::JSObject> foreign = GetForeignArgument(args, 1);
|
| + i::Handle<i::JSArrayBuffer> memory = GetMemoryArgument(args, 2);
|
| +
|
| + i::MaybeHandle<i::Object> result = InstantiateModuleCommon(
|
| + i_isolate, memory, foreign, buffer.start, buffer.end, &thrower);
|
| +
|
| + if (!result.is_null()) {
|
| + args.GetReturnValue().Set(v8::Utils::ToLocal(result.ToHandleChecked()));
|
| + }
|
| }
|
| } // namespace
|
|
|
| @@ -405,12 +377,19 @@ static Handle<JSFunction> InstallFunc(Isolate* isolate, Handle<JSObject> object,
|
| }
|
|
|
| void WasmJs::Install(Isolate* isolate, Handle<JSGlobalObject> global) {
|
| + if (!FLAG_expose_wasm && !FLAG_validate_asm) {
|
| + return;
|
| + }
|
| Factory* factory = isolate->factory();
|
|
|
| // Setup wasm function map.
|
| Handle<Context> context(global->native_context(), isolate);
|
| InstallWasmFunctionMap(isolate, context);
|
|
|
| + if (!FLAG_expose_wasm) {
|
| + return;
|
| + }
|
| +
|
| // Bind the experimental WASM object.
|
| // TODO(rossberg, titzer): remove once it's no longer needed.
|
| {
|
| @@ -492,5 +471,102 @@ void WasmJs::InstallWasmFunctionMap(Isolate* isolate, Handle<Context> context) {
|
| }
|
| }
|
|
|
| +bool WasmJs::ConvertAsmToWasm(ParseInfo* info, bool fixed_signature,
|
| + Handle<FixedArray>* wasm_data) {
|
| + ErrorThrower thrower(info->isolate(), "Asm.js -> WebAssembly conversion");
|
| + AsmTyper typer(info->isolate(), info->zone(), *(info->script()),
|
| + info->literal());
|
| + typer.set_fixed_signature(fixed_signature);
|
| + if (i::FLAG_enable_simd_asmjs) {
|
| + typer.set_allow_simd(true);
|
| + }
|
| + if (!typer.Validate()) {
|
| + DCHECK(!info->isolate()->has_pending_exception());
|
| + PrintF("Validation of asm.js module failed: %s", typer.error_message());
|
| + return false;
|
| + }
|
| + v8::internal::wasm::AsmWasmBuilder builder(info->isolate(), info->zone(),
|
| + info->literal(), &typer);
|
| + i::Handle<i::FixedArray> foreign_globals;
|
| + auto module = builder.Run(&foreign_globals);
|
| + size_t byte_length = module->end() - module->begin();
|
| + Handle<JSArrayBuffer> buffer = info->isolate()->factory()->NewJSArrayBuffer();
|
| + JSArrayBuffer::SetupAllocatingData(buffer, info->isolate(), byte_length,
|
| + false, SharedFlag::kNotShared);
|
| + uint8_t* module_bytes = reinterpret_cast<uint8_t*>(buffer->backing_store());
|
| + memcpy(module_bytes, module->begin(), byte_length);
|
| + *wasm_data = info->isolate()->factory()->NewFixedArray(2);
|
| + (*wasm_data)->set(0, *buffer);
|
| + (*wasm_data)->set(1, *foreign_globals);
|
| + return true;
|
| +}
|
| +
|
| +bool WasmJs::InstantiateAsmWasm(i::Isolate* isolate,
|
| + Handle<FixedArray> wasm_data,
|
| + Handle<JSArrayBuffer> memory,
|
| + Handle<JSObject> foreign,
|
| + Handle<Object>* result) {
|
| + i::Handle<i::JSArrayBuffer> module_bytes(
|
| + i::JSArrayBuffer::cast(wasm_data->get(0)));
|
| + i::Handle<i::FixedArray> foreign_globals(
|
| + i::FixedArray::cast(wasm_data->get(1)));
|
| +
|
| + ErrorThrower thrower(isolate, "Asm.js -> WebAssembly instantiation");
|
| + i::Factory* factory = isolate->factory();
|
| +
|
| + const byte* module_start =
|
| + reinterpret_cast<const byte*>(module_bytes->backing_store());
|
| + size_t module_length =
|
| + static_cast<size_t>(module_bytes->byte_length()->Number());
|
| + const byte* module_end = module_start + module_length;
|
| + i::MaybeHandle<i::JSObject> maybe_module_object = InstantiateModuleCommon(
|
| + isolate, memory, foreign, module_start, module_end, &thrower,
|
| + internal::wasm::kAsmJsOrigin);
|
| + if (maybe_module_object.is_null()) {
|
| + return false;
|
| + }
|
| +
|
| + i::Handle<i::Name> name =
|
| + factory->NewStringFromStaticChars("__foreign_init__");
|
| +
|
| + i::Handle<i::Object> module_object = maybe_module_object.ToHandleChecked();
|
| + i::MaybeHandle<i::Object> maybe_init =
|
| + i::Object::GetProperty(module_object, name);
|
| + DCHECK(!maybe_init.is_null());
|
| +
|
| + i::Handle<i::Object> init = maybe_init.ToHandleChecked();
|
| + i::Handle<i::Object> undefined(isolate->heap()->undefined_value(), isolate);
|
| + i::Handle<i::Object>* foreign_args_array =
|
| + new i::Handle<i::Object>[foreign_globals->length()];
|
| + for (int j = 0; j < foreign_globals->length(); j++) {
|
| + if (!foreign.is_null()) {
|
| + i::MaybeHandle<i::Name> name = i::Object::ToName(
|
| + isolate, i::Handle<i::Object>(foreign_globals->get(j), isolate));
|
| + if (!name.is_null()) {
|
| + i::MaybeHandle<i::Object> val =
|
| + i::Object::GetProperty(foreign, name.ToHandleChecked());
|
| + if (!val.is_null()) {
|
| + foreign_args_array[j] = val.ToHandleChecked();
|
| + continue;
|
| + }
|
| + }
|
| + }
|
| + foreign_args_array[j] = undefined;
|
| + }
|
| + i::MaybeHandle<i::Object> retval = i::Execution::Call(
|
| + isolate, init, undefined, foreign_globals->length(), foreign_args_array);
|
| + delete[] foreign_args_array;
|
| +
|
| + if (retval.is_null()) {
|
| + thrower.Error(
|
| + "WASM.instantiateModuleFromAsm(): foreign init function failed");
|
| + return false;
|
| + } else {
|
| + *result = maybe_module_object.ToHandleChecked();
|
| + }
|
| +
|
| + return true;
|
| +}
|
| +
|
| } // namespace internal
|
| } // namespace v8
|
|
|