1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:57:35 +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

@ -61,19 +61,19 @@ Value StringConstructor::call()
return js_string(heap(), "");
if (vm().argument(0).is_symbol())
return js_string(heap(), vm().argument(0).as_symbol().to_string());
auto* string = vm().argument(0).to_primitive_string(interpreter());
auto* string = vm().argument(0).to_primitive_string(global_object());
if (vm().exception())
return {};
return string;
}
Value StringConstructor::construct(Interpreter& interpreter, Function&)
Value StringConstructor::construct(Interpreter&, Function&)
{
PrimitiveString* primitive_string = nullptr;
if (!interpreter.argument_count())
primitive_string = js_string(interpreter, "");
if (!vm().argument_count())
primitive_string = js_string(vm(), "");
else
primitive_string = interpreter.argument(0).to_primitive_string(interpreter);
primitive_string = vm().argument(0).to_primitive_string(global_object());
if (!primitive_string)
return {};
return StringObject::create(global_object(), *primitive_string);
@ -81,55 +81,55 @@ Value StringConstructor::construct(Interpreter& interpreter, Function&)
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::raw)
{
auto* template_object = interpreter.argument(0).to_object(interpreter, global_object);
if (interpreter.exception())
auto* template_object = vm.argument(0).to_object(global_object);
if (vm.exception())
return {};
auto raw = template_object->get("raw");
if (interpreter.exception())
if (vm.exception())
return {};
if (raw.is_empty() || raw.is_undefined() || raw.is_null()) {
interpreter.vm().throw_exception<TypeError>(global_object, ErrorType::StringRawCannotConvert, raw.is_null() ? "null" : "undefined");
vm.throw_exception<TypeError>(global_object, ErrorType::StringRawCannotConvert, raw.is_null() ? "null" : "undefined");
return {};
}
if (!raw.is_array())
return js_string(interpreter, "");
return js_string(vm, "");
auto* array = static_cast<Array*>(raw.to_object(interpreter, global_object));
auto* array = static_cast<Array*>(raw.to_object(global_object));
auto& raw_array_elements = array->indexed_properties();
StringBuilder builder;
for (size_t i = 0; i < raw_array_elements.array_like_size(); ++i) {
auto result = raw_array_elements.get(array, i);
if (interpreter.exception())
if (vm.exception())
return {};
if (!result.has_value())
continue;
builder.append(result.value().value.to_string(interpreter));
if (interpreter.exception())
builder.append(result.value().value.to_string(global_object));
if (vm.exception())
return {};
if (i + 1 < interpreter.argument_count() && i < raw_array_elements.array_like_size() - 1) {
builder.append(interpreter.argument(i + 1).to_string(interpreter));
if (interpreter.exception())
if (i + 1 < vm.argument_count() && i < raw_array_elements.array_like_size() - 1) {
builder.append(vm.argument(i + 1).to_string(global_object));
if (vm.exception())
return {};
}
}
return js_string(interpreter, builder.build());
return js_string(vm, builder.build());
}
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
{
StringBuilder builder;
for (size_t i = 0; i < interpreter.argument_count(); ++i) {
auto char_code = interpreter.argument(i).to_i32(interpreter);
if (interpreter.exception())
for (size_t i = 0; i < vm.argument_count(); ++i) {
auto char_code = vm.argument(i).to_i32(global_object);
if (vm.exception())
return {};
auto truncated = char_code & 0xffff;
// FIXME: We need an Utf16View :^)
builder.append(Utf32View((u32*)&truncated, 1));
}
return js_string(interpreter, builder.build());
return js_string(vm, builder.build());
}
}