LibJS: Update HostEnsureCanCompileStrings arguments to latest spec

This commit is contained in:
Shannon Booth 2024-11-03 20:18:56 +13:00 committed by Andrew Kaster
commit c1998f96c2
Notes: github-actions[bot] 2024-11-05 00:16:51 +00:00
6 changed files with 21 additions and 18 deletions

View file

@ -511,13 +511,14 @@ ThrowCompletionOr<Value> perform_eval(VM& vm, Value x, CallerMode strict_caller,
// 2. If Type(x) is not String, return x.
if (!x.is_string())
return x;
auto& code_string = x.as_string();
// 3. Let evalRealm be the current Realm Record.
auto& eval_realm = *vm.running_execution_context().realm;
// 4. NOTE: In the case of a direct eval, evalRealm is the realm of both the caller of eval and of the eval function itself.
// 5. Perform ? HostEnsureCanCompileStrings(evalRealm).
TRY(vm.host_ensure_can_compile_strings(eval_realm));
// 5. Perform ? HostEnsureCanCompileStrings(evalRealm, « », x, direct).
TRY(vm.host_ensure_can_compile_strings(eval_realm, {}, code_string.utf8_string_view(), direct));
// 6. Let inFunction be false.
bool in_function = false;
@ -571,8 +572,6 @@ ThrowCompletionOr<Value> perform_eval(VM& vm, Value x, CallerMode strict_caller,
// f. If inMethod is false, and body Contains SuperProperty, throw a SyntaxError exception.
// g. If inDerivedConstructor is false, and body Contains SuperCall, throw a SyntaxError exception.
// h. If inClassFieldInitializer is true, and ContainsArguments of body is true, throw a SyntaxError exception.
auto& code_string = x.as_string();
Parser::EvalInitialState initial_state {
.in_eval_function_context = in_function,
.allow_super_property_lookup = in_method,

View file

@ -17,6 +17,7 @@
#include <LibJS/Runtime/Iterator.h>
#include <LibJS/Runtime/KeyedCollections.h>
#include <LibJS/Runtime/PrivateEnvironment.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
@ -67,10 +68,7 @@ enum class CallerMode {
Strict,
NonStrict
};
enum class EvalMode {
Direct,
Indirect
};
ThrowCompletionOr<Value> perform_eval(VM&, Value, CallerMode, EvalMode);
ThrowCompletionOr<void> eval_declaration_instantiation(VM& vm, Program const& program, Environment* variable_environment, Environment* lexical_environment, PrivateEnvironment* private_environment, bool strict);

View file

@ -142,8 +142,8 @@ ThrowCompletionOr<NonnullGCPtr<ECMAScriptFunctionObject>> FunctionConstructor::c
// 10. Let currentRealm be the current Realm Record.
auto& realm = *vm.current_realm();
// FIXME: 11. Perform ? HostEnsureCanCompileStrings(currentRealm, parameterStrings, bodyString, false).
TRY(vm.host_ensure_can_compile_strings(current_realm));
// 11. Perform ? HostEnsureCanCompileStrings(currentRealm, parameterStrings, bodyString, false).
TRY(vm.host_ensure_can_compile_strings(realm, parameter_strings, body_string, EvalMode::Indirect));
// 12. Let P be the empty String.
String parameters_string;

View file

@ -95,9 +95,8 @@ ThrowCompletionOr<void> copy_name_and_length(VM& vm, FunctionObject& function, F
// 3.1.3 PerformShadowRealmEval ( sourceText: a String, callerRealm: a Realm Record, evalRealm: a Realm Record, ), https://tc39.es/proposal-shadowrealm/#sec-performshadowrealmeval
ThrowCompletionOr<Value> perform_shadow_realm_eval(VM& vm, StringView source_text, Realm& caller_realm, Realm& eval_realm)
{
// FIXME: Needs to be updated to latest ECMA-262. See: https://github.com/tc39/proposal-shadowrealm/issues/367
// 1. Perform ? HostEnsureCanCompileStrings(callerRealm, evalRealm).
TRY(vm.host_ensure_can_compile_strings(eval_realm));
// 1. Perform ? HostEnsureCanCompileStrings(evalRealm, « », sourceText, false).
TRY(vm.host_ensure_can_compile_strings(eval_realm, {}, source_text, EvalMode::Indirect));
// 2. Perform the following substeps in an implementation-defined order, possibly interleaving parsing and error detection:

View file

@ -120,10 +120,12 @@ VM::VM(OwnPtr<CustomData> custom_data, ErrorMessages error_messages)
return Vector<ByteString> { "type" };
};
// 19.2.1.2 HostEnsureCanCompileStrings ( callerRealm, calleeRealm ), https://tc39.es/ecma262/#sec-hostensurecancompilestrings
host_ensure_can_compile_strings = [](Realm&) -> ThrowCompletionOr<void> {
// The host-defined abstract operation HostEnsureCanCompileStrings takes argument calleeRealm (a Realm Record)
// and returns either a normal completion containing unused or a throw completion.
// 19.2.1.2 HostEnsureCanCompileStrings ( calleeRealm, parameterStrings, bodyString, direct ), https://tc39.es/ecma262/#sec-hostensurecancompilestrings
host_ensure_can_compile_strings = [](Realm&, ReadonlySpan<String>, StringView, EvalMode) -> ThrowCompletionOr<void> {
// The host-defined abstract operation HostEnsureCanCompileStrings takes arguments calleeRealm (a Realm Record),
// parameterStrings (a List of Strings), bodyString (a String), and direct (a Boolean) and returns either a normal
// completion containing unused or a throw completion.
//
// It allows host environments to block certain ECMAScript functions which allow developers to compile strings into ECMAScript code.
// An implementation of HostEnsureCanCompileStrings must conform to the following requirements:
// - If the returned Completion Record is a normal completion, it must be a normal completion containing unused.

View file

@ -37,6 +37,11 @@ enum class HandledByHost {
Unhandled,
};
enum class EvalMode {
Direct,
Indirect
};
class VM : public RefCounted<VM> {
public:
struct CustomData {
@ -276,7 +281,7 @@ public:
Function<void(FinalizationRegistry&)> host_enqueue_finalization_registry_cleanup_job;
Function<void(NonnullGCPtr<HeapFunction<ThrowCompletionOr<Value>()>>, Realm*)> host_enqueue_promise_job;
Function<JS::NonnullGCPtr<JobCallback>(FunctionObject&)> host_make_job_callback;
Function<ThrowCompletionOr<void>(Realm&)> host_ensure_can_compile_strings;
Function<ThrowCompletionOr<void>(Realm&, ReadonlySpan<String>, StringView, EvalMode)> host_ensure_can_compile_strings;
Function<ThrowCompletionOr<void>(Object&)> host_ensure_can_add_private_element;
Function<ThrowCompletionOr<HandledByHost>(ArrayBuffer&, size_t)> host_resize_array_buffer;
Function<void(StringView)> host_unrecognized_date_string;