1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:47:34 +00:00

LibJS: Convert ShadowRealmPrototype functions to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-29 01:08:49 +03:00
parent 658056233e
commit 040e29c7b9
2 changed files with 14 additions and 16 deletions

View file

@ -22,27 +22,25 @@ void ShadowRealmPrototype::initialize(GlobalObject& global_object)
Object::initialize(global_object);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.evaluate, evaluate, 1, attr);
define_old_native_function(vm.names.importValue, import_value, 2, attr);
define_native_function(vm.names.evaluate, evaluate, 1, attr);
define_native_function(vm.names.importValue, import_value, 2, attr);
// 3.4.3 ShadowRealm.prototype [ @@toStringTag ], https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, vm.names.ShadowRealm.as_string()), Attribute::Configurable);
}
// 3.4.1 ShadowRealm.prototype.evaluate ( sourceText ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype.evaluate
JS_DEFINE_OLD_NATIVE_FUNCTION(ShadowRealmPrototype::evaluate)
JS_DEFINE_NATIVE_FUNCTION(ShadowRealmPrototype::evaluate)
{
auto source_text = vm.argument(0);
// 1. Let O be this value.
// 2. Perform ? ValidateShadowRealmObject(O).
auto* object = TRY_OR_DISCARD(typed_this_object(global_object));
auto* object = TRY(typed_this_object(global_object));
// 3. If Type(sourceText) is not String, throw a TypeError exception.
if (!source_text.is_string()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAString, source_text);
return {};
}
if (!source_text.is_string())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAString, source_text);
// 4. Let callerRealm be the current Realm Record.
auto* caller_realm = vm.current_realm();
@ -51,24 +49,24 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(ShadowRealmPrototype::evaluate)
auto& eval_realm = object->shadow_realm();
// 6. Return ? PerformShadowRealmEval(sourceText, callerRealm, evalRealm).
return TRY_OR_DISCARD(perform_shadow_realm_eval(global_object, source_text.as_string().string(), *caller_realm, eval_realm));
return perform_shadow_realm_eval(global_object, source_text.as_string().string(), *caller_realm, eval_realm);
}
// 3.4.2 ShadowRealm.prototype.importValue ( specifier, exportName ), https://tc39.es/proposal-shadowrealm/#sec-shadowrealm.prototype.importvalue
JS_DEFINE_OLD_NATIVE_FUNCTION(ShadowRealmPrototype::import_value)
JS_DEFINE_NATIVE_FUNCTION(ShadowRealmPrototype::import_value)
{
auto specifier = vm.argument(0);
auto export_name = vm.argument(1);
// 1. Let O be this value.
// 2. Perform ? ValidateShadowRealmObject(O).
auto* object = TRY_OR_DISCARD(typed_this_object(global_object));
auto* object = TRY(typed_this_object(global_object));
// 3. Let specifierString be ? ToString(specifier).
auto specifier_string = TRY_OR_DISCARD(specifier.to_string(global_object));
auto specifier_string = TRY(specifier.to_string(global_object));
// 4. Let exportNameString be ? ToString(exportName).
auto export_name_string = TRY_OR_DISCARD(export_name.to_string(global_object));
auto export_name_string = TRY(export_name.to_string(global_object));
// 5. Let callerRealm be the current Realm Record.
auto* caller_realm = vm.current_realm();
@ -80,7 +78,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(ShadowRealmPrototype::import_value)
auto& eval_context = object->execution_context();
// 8. Return ? ShadowRealmImportValue(specifierString, exportNameString, callerRealm, evalRealm, evalContext).
return TRY_OR_DISCARD(shadow_realm_import_value(global_object, move(specifier_string), move(export_name_string), *caller_realm, eval_realm, eval_context));
return shadow_realm_import_value(global_object, move(specifier_string), move(export_name_string), *caller_realm, eval_realm, eval_context);
}
}

View file

@ -20,8 +20,8 @@ public:
virtual ~ShadowRealmPrototype() override = default;
private:
JS_DECLARE_OLD_NATIVE_FUNCTION(evaluate);
JS_DECLARE_OLD_NATIVE_FUNCTION(import_value);
JS_DECLARE_NATIVE_FUNCTION(evaluate);
JS_DECLARE_NATIVE_FUNCTION(import_value);
};
}