Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: src/wasm/wasm-js.cc

Issue 2626263004: [wasm] Implement WebAssembly.Module.customSections. (Closed)
Patch Set: Fix semantics Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 the V8 project authors. All rights reserved. 1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/api-natives.h" 5 #include "src/api-natives.h"
6 #include "src/api.h" 6 #include "src/api.h"
7 #include "src/asmjs/asm-js.h" 7 #include "src/asmjs/asm-js.h"
8 #include "src/asmjs/asm-typer.h" 8 #include "src/asmjs/asm-typer.h"
9 #include "src/asmjs/asm-wasm-builder.h" 9 #include "src/asmjs/asm-wasm-builder.h"
10 #include "src/assert-scope.h" 10 #include "src/assert-scope.h"
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 i::wasm::WasmModule::Instantiate(i_isolate, thrower, i_module_obj, ffi); 224 i::wasm::WasmModule::Instantiate(i_isolate, thrower, i_module_obj, ffi);
225 if (instance.is_null()) { 225 if (instance.is_null()) {
226 if (!thrower->error()) 226 if (!thrower->error())
227 thrower->RuntimeError("Could not instantiate module"); 227 thrower->RuntimeError("Could not instantiate module");
228 return nothing; 228 return nothing;
229 } 229 }
230 DCHECK(!i_isolate->has_pending_exception()); 230 DCHECK(!i_isolate->has_pending_exception());
231 return Utils::ToLocal(instance.ToHandleChecked()); 231 return Utils::ToLocal(instance.ToHandleChecked());
232 } 232 }
233 233
234 void WebAssemblyModuleImports(const v8::FunctionCallbackInfo<v8::Value>& args) { 234 namespace {
235 HandleScope scope(args.GetIsolate()); 235 i::MaybeHandle<i::WasmModuleObject> GetFirstArgumentAsModule(
236 const v8::FunctionCallbackInfo<v8::Value>& args, ErrorThrower& thrower) {
236 v8::Isolate* isolate = args.GetIsolate(); 237 v8::Isolate* isolate = args.GetIsolate();
237 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 238 i::MaybeHandle<i::WasmModuleObject> nothing;
238
239 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()");
240
241 if (args.Length() < 1) { 239 if (args.Length() < 1) {
242 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); 240 thrower.TypeError("Argument 0 must be a WebAssembly.Module");
243 return; 241 return nothing;
244 } 242 }
245 243
246 Local<Context> context = isolate->GetCurrentContext(); 244 Local<Context> context = isolate->GetCurrentContext();
247 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); 245 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
248 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), 246 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]),
249 i::Handle<i::Symbol>(i_context->wasm_module_sym()), 247 i::Handle<i::Symbol>(i_context->wasm_module_sym()),
250 "Argument 0 must be a WebAssembly.Module")) { 248 "Argument 0 must be a WebAssembly.Module")) {
251 return; 249 return nothing;
252 } 250 }
253 251
254 Local<Object> module_obj = Local<Object>::Cast(args[0]); 252 Local<Object> module_obj = Local<Object>::Cast(args[0]);
255 i::Handle<i::WasmModuleObject> i_module_obj = 253 return i::Handle<i::WasmModuleObject>::cast(
256 i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); 254 v8::Utils::OpenHandle(*module_obj));
255 }
256 } // namespace
257 257
258 i::Handle<i::JSArray> imports = i::wasm::GetImports(i_isolate, i_module_obj); 258 void WebAssemblyModuleImports(const v8::FunctionCallbackInfo<v8::Value>& args) {
259 HandleScope scope(args.GetIsolate());
260 v8::Isolate* isolate = args.GetIsolate();
261 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
262 ErrorThrower thrower(i_isolate, "WebAssembly.Module.imports()");
259 263
260 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue(); 264 auto maybe_module = GetFirstArgumentAsModule(args, thrower);
261 return_value.Set(Utils::ToLocal(imports)); 265
266 if (!maybe_module.is_null()) {
ahaas 2017/01/24 11:35:13 You do not set the return value when maybe_module.
titzer 2017/01/24 12:38:39 In these cases the ErrorThrower has an error and w
ahaas 2017/01/24 12:42:21 In that case, could we add a DCHECK that there rea
267 auto imports =
268 i::wasm::GetImports(i_isolate, maybe_module.ToHandleChecked());
269 args.GetReturnValue().Set(Utils::ToLocal(imports));
270 }
262 } 271 }
263 272
264 void WebAssemblyModuleExports(const v8::FunctionCallbackInfo<v8::Value>& args) { 273 void WebAssemblyModuleExports(const v8::FunctionCallbackInfo<v8::Value>& args) {
265 HandleScope scope(args.GetIsolate()); 274 HandleScope scope(args.GetIsolate());
266 v8::Isolate* isolate = args.GetIsolate(); 275 v8::Isolate* isolate = args.GetIsolate();
267 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 276 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
268 277
269 ErrorThrower thrower(i_isolate, "WebAssembly.Module.exports()"); 278 ErrorThrower thrower(i_isolate, "WebAssembly.Module.exports()");
270 279
271 if (args.Length() < 1) { 280 auto maybe_module = GetFirstArgumentAsModule(args, thrower);
272 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); 281
282 if (!maybe_module.is_null()) {
ahaas 2017/01/24 11:35:13 Same here.
283 auto exports =
284 i::wasm::GetExports(i_isolate, maybe_module.ToHandleChecked());
285 args.GetReturnValue().Set(Utils::ToLocal(exports));
286 }
287 }
288
289 void WebAssemblyModuleCustomSections(
290 const v8::FunctionCallbackInfo<v8::Value>& args) {
291 HandleScope scope(args.GetIsolate());
292 v8::Isolate* isolate = args.GetIsolate();
293 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
294
295 ErrorThrower thrower(i_isolate, "WebAssembly.Module.customSections()");
296
297 auto maybe_module = GetFirstArgumentAsModule(args, thrower);
298
299 if (args.Length() < 2) {
300 thrower.TypeError("Argument 1 must be a string");
273 return; 301 return;
274 } 302 }
275 303
276 Local<Context> context = isolate->GetCurrentContext(); 304 i::Handle<i::Object> name = Utils::OpenHandle(*args[1]);
277 i::Handle<i::Context> i_context = Utils::OpenHandle(*context); 305 if (!name->IsString()) {
278 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]), 306 thrower.TypeError("Argument 1 must be a string");
279 i::Handle<i::Symbol>(i_context->wasm_module_sym()),
280 "Argument 0 must be a WebAssembly.Module")) {
281 return; 307 return;
282 } 308 }
283 309
284 Local<Object> module_obj = Local<Object>::Cast(args[0]); 310 if (!maybe_module.is_null()) {
ahaas 2017/01/24 11:35:13 same here.
285 i::Handle<i::WasmModuleObject> i_module_obj = 311 auto custom_sections =
286 i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj)); 312 i::wasm::GetCustomSections(i_isolate, maybe_module.ToHandleChecked(),
287 313 i::Handle<i::String>::cast(name));
288 i::Handle<i::JSArray> exports = i::wasm::GetExports(i_isolate, i_module_obj); 314 args.GetReturnValue().Set(Utils::ToLocal(custom_sections));
289 315 }
290 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
291 return_value.Set(Utils::ToLocal(exports));
292 } 316 }
293 317
294 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { 318 void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) {
295 HandleScope scope(args.GetIsolate()); 319 HandleScope scope(args.GetIsolate());
296 v8::Isolate* isolate = args.GetIsolate(); 320 v8::Isolate* isolate = args.GetIsolate();
297 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 321 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
298 322
299 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()"); 323 ErrorThrower thrower(i_isolate, "WebAssembly.Instance()");
300 324
301 if (args.Length() < 1) { 325 auto maybe_module = GetFirstArgumentAsModule(args, thrower);
302 thrower.TypeError("Argument 0 must be a WebAssembly.Module"); 326
303 return; 327 if (!maybe_module.is_null()) {
ahaas 2017/01/24 11:35:13 same here.
328 MaybeLocal<Value> instance = InstantiateModuleImpl(
329 i_isolate, maybe_module.ToHandleChecked(), args, &thrower);
330
331 if (instance.IsEmpty()) {
332 DCHECK(thrower.error());
333 return;
334 }
335 args.GetReturnValue().Set(instance.ToLocalChecked());
304 } 336 }
305
306 Local<Context> context = isolate->GetCurrentContext();
307 i::Handle<i::Context> i_context = Utils::OpenHandle(*context);
308 if (!BrandCheck(isolate, Utils::OpenHandle(*args[0]),
309 i::Handle<i::Symbol>(i_context->wasm_module_sym()),
310 "Argument 0 must be a WebAssembly.Module")) {
311 return;
312 }
313
314 Local<Object> module_obj = Local<Object>::Cast(args[0]);
315 i::Handle<i::WasmModuleObject> i_module_obj =
316 i::Handle<i::WasmModuleObject>::cast(v8::Utils::OpenHandle(*module_obj));
317
318 MaybeLocal<Value> instance =
319 InstantiateModuleImpl(i_isolate, i_module_obj, args, &thrower);
320 if (instance.IsEmpty()) {
321 DCHECK(thrower.error());
322 return;
323 }
324
325 v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
326 return_value.Set(instance.ToLocalChecked());
327 } 337 }
328 338
329 void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) { 339 void WebAssemblyInstantiate(const v8::FunctionCallbackInfo<v8::Value>& args) {
330 v8::Isolate* isolate = args.GetIsolate(); 340 v8::Isolate* isolate = args.GetIsolate();
331 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); 341 i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
332 342
333 HandleScope scope(isolate); 343 HandleScope scope(isolate);
334 ErrorThrower thrower(i_isolate, "WebAssembly.instantiate()"); 344 ErrorThrower thrower(i_isolate, "WebAssembly.instantiate()");
335 345
336 Local<Context> context = isolate->GetCurrentContext(); 346 Local<Context> context = isolate->GetCurrentContext();
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 Handle<JSObject> module_proto = 867 Handle<JSObject> module_proto =
858 factory->NewJSObject(module_constructor, TENURED); 868 factory->NewJSObject(module_constructor, TENURED);
859 i::Handle<i::Map> module_map = isolate->factory()->NewMap( 869 i::Handle<i::Map> module_map = isolate->factory()->NewMap(
860 i::JS_API_OBJECT_TYPE, i::JSObject::kHeaderSize + 870 i::JS_API_OBJECT_TYPE, i::JSObject::kHeaderSize +
861 WasmModuleObject::kFieldCount * i::kPointerSize); 871 WasmModuleObject::kFieldCount * i::kPointerSize);
862 JSFunction::SetInitialMap(module_constructor, module_map, module_proto); 872 JSFunction::SetInitialMap(module_constructor, module_map, module_proto);
863 InstallFunc(isolate, module_constructor, "imports", WebAssemblyModuleImports, 873 InstallFunc(isolate, module_constructor, "imports", WebAssemblyModuleImports,
864 1); 874 1);
865 InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports, 875 InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports,
866 1); 876 1);
877 InstallFunc(isolate, module_constructor, "customSections",
878 WebAssemblyModuleCustomSections, 2);
867 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(), 879 JSObject::AddProperty(module_proto, isolate->factory()->constructor_string(),
868 module_constructor, DONT_ENUM); 880 module_constructor, DONT_ENUM);
869 JSObject::AddProperty(module_proto, factory->to_string_tag_symbol(), 881 JSObject::AddProperty(module_proto, factory->to_string_tag_symbol(),
870 v8_str(isolate, "WebAssembly.Module"), ro_attributes); 882 v8_str(isolate, "WebAssembly.Module"), ro_attributes);
871 883
872 // Setup Instance 884 // Setup Instance
873 Handle<JSFunction> instance_constructor = 885 Handle<JSFunction> instance_constructor =
874 InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance, 1); 886 InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance, 1);
875 context->set_wasm_instance_constructor(*instance_constructor); 887 context->set_wasm_instance_constructor(*instance_constructor);
876 Handle<JSObject> instance_proto = 888 Handle<JSObject> instance_proto =
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
951 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate); 963 i::Handle<i::Symbol> symbol(isolate->context()->wasm_memory_sym(), isolate);
952 return HasBrand(value, symbol); 964 return HasBrand(value, symbol);
953 } 965 }
954 966
955 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) { 967 bool WasmJs::IsWasmTableObject(Isolate* isolate, Handle<Object> value) {
956 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate); 968 i::Handle<i::Symbol> symbol(isolate->context()->wasm_table_sym(), isolate);
957 return HasBrand(value, symbol); 969 return HasBrand(value, symbol);
958 } 970 }
959 } // namespace internal 971 } // namespace internal
960 } // namespace v8 972 } // namespace v8
OLDNEW
« no previous file with comments | « src/wasm/module-decoder.cc ('k') | src/wasm/wasm-module.h » ('j') | src/wasm/wasm-module.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698