From 6cedb1b9d9794addae1879f5c8c39319fa6a2a10 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Tue, 30 Aug 2022 12:00:04 +0100 Subject: [PATCH] LibJS: Implement $262.evalScript() according to the given algorithm test262's INTERPRETING.md specifies the exact steps for this function, so let's shuffle some things around and add "spec" comments. Most importantly this now returns the result of the evaluated script, which at least one test relies on: https://github.com/tc39/test262/blob/main/test/built-ins/Proxy/revocable/tco-fn-realm.js --- .../LibJS/Contrib/Test262/$262Object.cpp | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibJS/Contrib/Test262/$262Object.cpp b/Userland/Libraries/LibJS/Contrib/Test262/$262Object.cpp index 65edc88d48..e7c588a30a 100644 --- a/Userland/Libraries/LibJS/Contrib/Test262/$262Object.cpp +++ b/Userland/Libraries/LibJS/Contrib/Test262/$262Object.cpp @@ -80,12 +80,30 @@ JS_DEFINE_NATIVE_FUNCTION($262Object::detach_array_buffer) JS_DEFINE_NATIVE_FUNCTION($262Object::eval_script) { - auto source = TRY(vm.argument(0).to_string(vm)); - auto script_or_error = Script::parse(source, *vm.current_realm()); - if (script_or_error.is_error()) - return vm.throw_completion(script_or_error.error()[0].to_string()); - TRY(vm.interpreter().run(script_or_error.value())); - return js_undefined(); + auto source_text = TRY(vm.argument(0).to_string(vm)); + + // 1. Let hostDefined be any host-defined values for the provided sourceText (obtained in an implementation dependent manner) + + // 2. Let realm be the current Realm Record. + auto& realm = *vm.current_realm(); + + // 3. Let s be ParseScript(sourceText, realm, hostDefined). + auto script_or_error = Script::parse(source_text, realm); + + // 4. If s is a List of errors, then + if (script_or_error.is_error()) { + // a. Let error be the first element of s. + auto& error = script_or_error.error()[0]; + + // b. Return Completion { [[Type]]: throw, [[Value]]: error, [[Target]]: empty }. + return vm.throw_completion(error.to_string()); + } + + // 5. Let status be ScriptEvaluation(s). + auto status = vm.interpreter().run(script_or_error.value()); + + // 6. Return Completion(status). + return status; } }