summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2023-03-17 10:21:51 -0400
committerLinus Groh <mail@linusgroh.de>2023-03-17 16:39:08 +0000
commiteb5aae24f404571e750f6537cb97de95ababb686 (patch)
treee07d2cc2eab259b67d93b6cb1a3157f7f2d73fa6 /Userland/Libraries/LibJS
parent782cdaeccffbb7e88a55770d7441493e4a82cb6d (diff)
downloadserenity-eb5aae24f404571e750f6537cb97de95ababb686.zip
LibJS: Move creation of fallible VM objects to its creation factory
No change of behavior in this patch, but this will allow this factory to propagate any errors from the creation of these objects.
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/VM.cpp25
-rw-r--r--Userland/Libraries/LibJS/Runtime/VM.h22
2 files changed, 31 insertions, 16 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp
index 47ed699bf8..64c90a6d0a 100644
--- a/Userland/Libraries/LibJS/Runtime/VM.cpp
+++ b/Userland/Libraries/LibJS/Runtime/VM.cpp
@@ -36,7 +36,20 @@ namespace JS {
NonnullRefPtr<VM> VM::create(OwnPtr<CustomData> custom_data)
{
- return adopt_ref(*new VM(move(custom_data)));
+ ErrorMessages error_messages {};
+ error_messages[to_underlying(ErrorMessage::OutOfMemory)] = String::from_utf8(ErrorType::OutOfMemory.message()).release_value_but_fixme_should_propagate_errors();
+
+ auto vm = adopt_ref(*new VM(move(custom_data), move(error_messages)));
+
+ WellKnownSymbols well_known_symbols {
+#define __JS_ENUMERATE(SymbolName, snake_name) \
+ Symbol::create(*vm, "Symbol." #SymbolName##_string.release_value_but_fixme_should_propagate_errors(), false),
+ JS_ENUMERATE_WELL_KNOWN_SYMBOLS
+#undef __JS_ENUMERATE
+ };
+
+ vm->set_well_known_symbols(move(well_known_symbols));
+ return vm;
}
template<u32... code_points>
@@ -47,8 +60,9 @@ static constexpr auto make_single_ascii_character_strings(IndexSequence<code_poi
static constexpr auto single_ascii_character_strings = make_single_ascii_character_strings(MakeIndexSequence<128>());
-VM::VM(OwnPtr<CustomData> custom_data)
+VM::VM(OwnPtr<CustomData> custom_data, ErrorMessages error_messages)
: m_heap(*this)
+ , m_error_messages(move(error_messages))
, m_custom_data(move(custom_data))
{
m_empty_string = m_heap.allocate_without_realm<PrimitiveString>(String {});
@@ -150,13 +164,6 @@ VM::VM(OwnPtr<CustomData> custom_data)
// NOTE: Since LibJS has no way of knowing whether the current environment is a browser we always
// call HostEnsureCanAddPrivateElement when needed.
};
-
-#define __JS_ENUMERATE(SymbolName, snake_name) \
- m_well_known_symbol_##snake_name = Symbol::create(*this, "Symbol." #SymbolName##_string.release_value_but_fixme_should_propagate_errors(), false);
- JS_ENUMERATE_WELL_KNOWN_SYMBOLS
-#undef __JS_ENUMERATE
-
- m_error_messages[to_underlying(ErrorMessage::OutOfMemory)] = String::from_utf8(ErrorType::OutOfMemory.message()).release_value_but_fixme_should_propagate_errors();
}
String const& VM::error_message(ErrorMessage type) const
diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h
index 287b53dfad..e306ef2a51 100644
--- a/Userland/Libraries/LibJS/Runtime/VM.h
+++ b/Userland/Libraries/LibJS/Runtime/VM.h
@@ -68,7 +68,7 @@ public:
#define __JS_ENUMERATE(SymbolName, snake_name) \
Symbol* well_known_symbol_##snake_name() const \
{ \
- return m_well_known_symbol_##snake_name; \
+ return m_well_known_symbols.snake_name; \
}
JS_ENUMERATE_WELL_KNOWN_SYMBOLS
#undef __JS_ENUMERATE
@@ -269,7 +269,16 @@ public:
Function<ThrowCompletionOr<void>(Object&)> host_ensure_can_add_private_element;
private:
- explicit VM(OwnPtr<CustomData>);
+ using ErrorMessages = AK::Array<String, to_underlying(ErrorMessage::__Count)>;
+
+ struct WellKnownSymbols {
+#define __JS_ENUMERATE(SymbolName, snake_name) \
+ GCPtr<Symbol> snake_name;
+ JS_ENUMERATE_WELL_KNOWN_SYMBOLS
+#undef __JS_ENUMERATE
+ };
+
+ VM(OwnPtr<CustomData>, ErrorMessages);
ThrowCompletionOr<void> property_binding_initialization(BindingPattern const& binding, Value value, Environment* environment);
ThrowCompletionOr<void> iterator_binding_initialization(BindingPattern const& binding, Iterator& iterator_record, Environment* environment);
@@ -280,6 +289,8 @@ private:
ThrowCompletionOr<void> import_module_dynamically(ScriptOrModule referencing_script_or_module, ModuleRequest module_request, PromiseCapability const& promise_capability);
void finish_dynamic_import(ScriptOrModule referencing_script_or_module, ModuleRequest module_request, PromiseCapability const& promise_capability, Promise* inner_promise);
+ void set_well_known_symbols(WellKnownSymbols well_known_symbols) { m_well_known_symbols = move(well_known_symbols); }
+
HashMap<String, GCPtr<PrimitiveString>> m_string_cache;
HashMap<DeprecatedString, GCPtr<PrimitiveString>> m_deprecated_string_cache;
@@ -301,7 +312,7 @@ private:
GCPtr<PrimitiveString> m_empty_string;
GCPtr<PrimitiveString> m_single_ascii_character_strings[128] {};
- AK::Array<String, to_underlying(ErrorMessage::__Count)> m_error_messages;
+ ErrorMessages m_error_messages;
struct StoredModule {
ScriptOrModule referencing_script_or_module;
@@ -315,10 +326,7 @@ private:
Vector<StoredModule> m_loaded_modules;
-#define __JS_ENUMERATE(SymbolName, snake_name) \
- GCPtr<Symbol> m_well_known_symbol_##snake_name;
- JS_ENUMERATE_WELL_KNOWN_SYMBOLS
-#undef __JS_ENUMERATE
+ WellKnownSymbols m_well_known_symbols;
u32 m_execution_generation { 0 };