1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:38:10 +00:00

LibJS: Make native function/property callbacks take VM, not Interpreter

More work on decoupling the general runtime from Interpreter. The goal
is becoming clearer. Interpreter should be one possible way to execute
code inside a VM. In the future we might have other ways :^)
This commit is contained in:
Andreas Kling 2020-09-27 18:36:49 +02:00
parent 1ff9d33131
commit 340a115dfe
64 changed files with 1160 additions and 1114 deletions

View file

@ -500,13 +500,13 @@ void generate_implementation(const IDL::Interface& interface)
// Implementation: impl_from()
if (!interface.attributes.is_empty() || !interface.functions.is_empty()) {
out() << "static " << interface.fully_qualified_name << "* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)";
out() << "static " << interface.fully_qualified_name << "* impl_from(JS::VM& vm, JS::GlobalObject& global_object)";
out() << "{";
out() << " auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);";
out() << " auto* this_object = vm.this_value(global_object).to_object(global_object);";
out() << " if (!this_object)";
out() << " return {};";
out() << " if (!this_object->inherits(\"" << wrapper_class << "\")) {";
out() << " interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, \"" << interface.fully_qualified_name << "\");";
out() << " vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, \"" << interface.fully_qualified_name << "\");";
out() << " return nullptr;";
out() << " }";
out() << " return &static_cast<" << wrapper_class << "*>(this_object)->impl();";
@ -521,27 +521,27 @@ void generate_implementation(const IDL::Interface& interface)
out() << " return {};";
};
if (parameter.type.name == "DOMString") {
out() << " auto " << cpp_name << " = " << js_name << js_suffix << ".to_string(interpreter);";
out() << " if (interpreter.exception())";
out() << " auto " << cpp_name << " = " << js_name << js_suffix << ".to_string(global_object);";
out() << " if (vm.exception())";
generate_return();
} else if (parameter.type.name == "EventListener") {
out() << " if (!" << js_name << js_suffix << ".is_function()) {";
out() << " interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, \"Function\");";
out() << " vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, \"Function\");";
generate_return();
out() << " }";
out() << " auto " << cpp_name << " = adopt(*new EventListener(JS::make_handle(&" << js_name << js_suffix << ".as_function())));";
} else if (is_wrappable_type(parameter.type)) {
out() << " auto " << cpp_name << "_object = " << js_name << js_suffix << ".to_object(interpreter, global_object);";
out() << " if (interpreter.exception())";
out() << " auto " << cpp_name << "_object = " << js_name << js_suffix << ".to_object(global_object);";
out() << " if (vm.exception())";
generate_return();
out() << " if (!" << cpp_name << "_object->inherits(\"" << parameter.type.name << "Wrapper\")) {";
out() << " interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, \"" << parameter.type.name << "\");";
out() << " vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, \"" << parameter.type.name << "\");";
generate_return();
out() << " }";
out() << " auto& " << cpp_name << " = static_cast<" << parameter.type.name << "Wrapper*>(" << cpp_name << "_object)->impl();";
} else if (parameter.type.name == "double") {
out() << " auto " << cpp_name << " = " << js_name << js_suffix << ".to_double(interpreter);";
out() << " if (interpreter.exception())";
out() << " auto " << cpp_name << " = " << js_name << js_suffix << ".to_double(global_object);";
out() << " if (vm.exception())";
generate_return();
} else if (parameter.type.name == "boolean") {
out() << " auto " << cpp_name << " = " << js_name << js_suffix << ".to_boolean();";
@ -556,7 +556,7 @@ void generate_implementation(const IDL::Interface& interface)
size_t argument_index = 0;
for (auto& parameter : parameters) {
parameter_names.append(snake_name(parameter.name));
out() << " auto arg" << argument_index << " = interpreter.argument(" << argument_index << ");";
out() << " auto arg" << argument_index << " = vm.argument(" << argument_index << ");";
generate_to_cpp(parameter, "arg", argument_index, snake_name(parameter.name), return_void);
++argument_index;
}
@ -580,7 +580,7 @@ void generate_implementation(const IDL::Interface& interface)
}
if (return_type.name == "DOMString") {
out() << " return JS::js_string(interpreter, retval);";
out() << " return JS::js_string(vm, retval);";
} else if (return_type.name == "ArrayFromVector") {
// FIXME: Remove this fake type hack once it's no longer needed.
// Basically once we have NodeList we can throw this out.
@ -604,7 +604,7 @@ void generate_implementation(const IDL::Interface& interface)
for (auto& attribute : interface.attributes) {
out() << "JS_DEFINE_NATIVE_GETTER(" << wrapper_class << "::" << attribute.getter_callback_name << ")";
out() << "{";
out() << " auto* impl = impl_from(interpreter, global_object);";
out() << " auto* impl = impl_from(vm, global_object);";
out() << " if (!impl)";
out() << " return {};";
@ -629,7 +629,7 @@ void generate_implementation(const IDL::Interface& interface)
if (!attribute.readonly) {
out() << "JS_DEFINE_NATIVE_SETTER(" << wrapper_class << "::" << attribute.setter_callback_name << ")";
out() << "{";
out() << " auto* impl = impl_from(interpreter, global_object);";
out() << " auto* impl = impl_from(vm, global_object);";
out() << " if (!impl)";
out() << " return;";
@ -652,15 +652,15 @@ void generate_implementation(const IDL::Interface& interface)
for (auto& function : interface.functions) {
out() << "JS_DEFINE_NATIVE_FUNCTION(" << wrapper_class << "::" << snake_name(function.name) << ")";
out() << "{";
out() << " auto* impl = impl_from(interpreter, global_object);";
out() << " auto* impl = impl_from(vm, global_object);";
out() << " if (!impl)";
out() << " return {};";
if (function.length() > 0) {
out() << " if (interpreter.argument_count() < " << function.length() << ") {";
out() << " if (vm.argument_count() < " << function.length() << ") {";
if (function.length() == 1)
out() << " interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, \"" << function.name << "\");";
out() << " vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, \"" << function.name << "\");";
else
out() << " interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountMany, \"" << function.name << "\", \"" << function.length() << "\");";
out() << " vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountMany, \"" << function.name << "\", \"" << function.length() << "\");";
out() << " return {};";
out() << " }";
}