@@ -2318,28 +2318,49 @@ void WasmJs::InstallConditionalFeatures(Isolate* isolate,
23182318 Handle<JSGlobalObject> global = handle (context->global_object (), isolate);
23192319 MaybeHandle<Object> maybe_webassembly =
23202320 JSObject::GetProperty (isolate, global, " WebAssembly" );
2321- Handle<JSObject> webassembly =
2322- Handle<JSObject>::cast (maybe_webassembly.ToHandleChecked ());
2321+ Handle<Object> webassembly_obj;
2322+ if (!maybe_webassembly.ToHandle (&webassembly_obj)) {
2323+ // There is not {WebAssembly} object. We just return without adding the
2324+ // {Exception} constructor.
2325+ return ;
2326+ }
2327+ if (!webassembly_obj->IsJSObject ()) {
2328+ // The {WebAssembly} object is invalid. As we cannot add the {Exception}
2329+ // constructor, we just return.
2330+ return ;
2331+ }
2332+ Handle<JSObject> webassembly = Handle<JSObject>::cast (webassembly_obj);
23232333 // Setup Exception
23242334 Handle<String> exception_name = v8_str (isolate, " Exception" );
2325- if (!JSObject::HasProperty (webassembly, exception_name).FromMaybe (true )) {
2326- Handle<JSFunction> exception_constructor =
2327- CreateFunc (isolate, exception_name, WebAssemblyException, true ,
2328- SideEffectType::kHasSideEffect );
2329- exception_constructor->shared ().set_length (1 );
2330- JSObject::AddProperty (isolate, webassembly, exception_name,
2331- exception_constructor, DONT_ENUM);
2332- // Install the constructor on the context.
2333- context->set_wasm_exception_constructor (*exception_constructor);
2334- SetDummyInstanceTemplate (isolate, exception_constructor);
2335- JSFunction::EnsureHasInitialMap (exception_constructor);
2336- Handle<JSObject> exception_proto (
2337- JSObject::cast (exception_constructor->instance_prototype ()), isolate);
2338- Handle<Map> exception_map = isolate->factory ()->NewMap (
2339- i::WASM_EXCEPTION_OBJECT_TYPE, WasmExceptionObject::kHeaderSize );
2340- JSFunction::SetInitialMap (isolate, exception_constructor, exception_map,
2341- exception_proto);
2335+
2336+ if (JSObject::HasOwnProperty (webassembly, exception_name).FromMaybe (true )) {
2337+ // The {Exception} constructor already exists, there is nothing more to
2338+ // do.
2339+ return ;
2340+ }
2341+
2342+ bool has_prototype = true ;
2343+ Handle<JSFunction> exception_constructor =
2344+ CreateFunc (isolate, exception_name, WebAssemblyException, has_prototype,
2345+ SideEffectType::kHasNoSideEffect );
2346+ exception_constructor->shared ().set_length (1 );
2347+ auto result = Object::SetProperty (
2348+ isolate, webassembly, exception_name, exception_constructor,
2349+ StoreOrigin::kNamed , Just (ShouldThrow::kDontThrow ));
2350+ if (result.is_null ()) {
2351+ // Setting the {Exception} constructor failed. We just bail out.
2352+ return ;
23422353 }
2354+ // Install the constructor on the context.
2355+ context->set_wasm_exception_constructor (*exception_constructor);
2356+ SetDummyInstanceTemplate (isolate, exception_constructor);
2357+ JSFunction::EnsureHasInitialMap (exception_constructor);
2358+ Handle<JSObject> exception_proto (
2359+ JSObject::cast (exception_constructor->instance_prototype ()), isolate);
2360+ Handle<Map> exception_map = isolate->factory ()->NewMap (
2361+ i::WASM_EXCEPTION_OBJECT_TYPE, WasmExceptionObject::kHeaderSize );
2362+ JSFunction::SetInitialMap (isolate, exception_constructor, exception_map,
2363+ exception_proto);
23432364 }
23442365}
23452366#undef ASSIGN
0 commit comments