diff options
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp')
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp b/Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp new file mode 100644 index 0000000000..3b46804760 --- /dev/null +++ b/Userland/Libraries/LibJS/Runtime/ShadowRealm.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibJS/Runtime/ShadowRealm.h> +#include <LibJS/Runtime/WrappedFunction.h> + +namespace JS { + +// 3.1.3 GetWrappedValue ( callerRealm, value ), https://tc39.es/proposal-shadowrealm/#sec-getwrappedvalue +ThrowCompletionOr<Value> get_wrapped_value(GlobalObject& global_object, Realm& caller_realm, Value value) +{ + auto& vm = global_object.vm(); + + // 1. Assert: callerRealm is a Realm Record. + + // 2. If Type(value) is Object, then + if (value.is_object()) { + // a. If IsCallable(value) is false, throw a TypeError exception. + if (!value.is_function()) + return vm.throw_completion<TypeError>(global_object, ErrorType::ShadowRealmWrappedValueNonFunctionObject, value); + + // b. Return ! WrappedFunctionCreate(callerRealm, value). + return { WrappedFunction::create(global_object, caller_realm, value.as_function()) }; + } + + // 3. Return value. + return value; +} + +} |