diff options
author | Linus Groh <mail@linusgroh.de> | 2022-08-30 12:00:04 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-08-30 12:00:04 +0100 |
commit | 6cedb1b9d9794addae1879f5c8c39319fa6a2a10 (patch) | |
tree | 21d8faa05a6b25334209694bb255805cd15741d5 /Userland/Libraries/LibJS/Contrib | |
parent | 69415f060828881affae25ac323d6c0df4f7a787 (diff) | |
download | serenity-6cedb1b9d9794addae1879f5c8c39319fa6a2a10.zip |
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
Diffstat (limited to 'Userland/Libraries/LibJS/Contrib')
-rw-r--r-- | Userland/Libraries/LibJS/Contrib/Test262/$262Object.cpp | 30 |
1 files 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<SyntaxError>(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<SyntaxError>(error.to_string()); + } + + // 5. Let status be ScriptEvaluation(s). + auto status = vm.interpreter().run(script_or_error.value()); + + // 6. Return Completion(status). + return status; } } |