[wasm] Move heap-object related functionality out of wasm-module.h.

These functions clearly belong closer to respective classes
in wasm-objects.h.

R=clemensh@chromium.org

Bug: 
Change-Id: I46117b15181d33b237f4cdf318ce66ddb18d96bd
Reviewed-on: https://chromium-review.googlesource.com/684594
Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
Commit-Queue: Ben Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48183}
This commit is contained in:
Ben L. Titzer 2017-09-26 18:07:50 +02:00 committed by Commit Bot
parent 21b24620dd
commit 552150b228
8 changed files with 72 additions and 88 deletions

View File

@ -1669,7 +1669,7 @@ Address WasmCompiledFrame::GetCallerStackPointer() const {
}
WasmInstanceObject* WasmCompiledFrame::wasm_instance() const {
WasmInstanceObject* obj = wasm::GetOwningWasmInstance(LookupCode());
WasmInstanceObject* obj = WasmInstanceObject::GetOwningInstance(LookupCode());
// This is a live stack frame; it must have a live instance.
DCHECK_NOT_NULL(obj);
return obj;
@ -1751,7 +1751,7 @@ Code* WasmInterpreterEntryFrame::unchecked_code() const {
}
WasmInstanceObject* WasmInterpreterEntryFrame::wasm_instance() const {
WasmInstanceObject* ret = wasm::GetOwningWasmInstance(LookupCode());
WasmInstanceObject* ret = WasmInstanceObject::GetOwningInstance(LookupCode());
// This is a live stack frame, there must be a live wasm instance available.
DCHECK_NOT_NULL(ret);
return ret;

View File

@ -722,9 +722,7 @@ Handle<Object> WasmStackFrame::Null() const {
bool WasmStackFrame::HasScript() const { return true; }
Handle<Script> WasmStackFrame::GetScript() const {
return handle(
WasmInstanceObject::cast(*wasm_instance_)->compiled_module()->script(),
isolate_);
return handle(wasm_instance_->compiled_module()->script(), isolate_);
}
AsmJsWasmStackFrame::AsmJsWasmStackFrame() {}
@ -748,15 +746,13 @@ Handle<Object> AsmJsWasmStackFrame::GetFunction() const {
}
Handle<Object> AsmJsWasmStackFrame::GetFileName() {
Handle<Script> script =
wasm::GetScript(Handle<JSObject>::cast(wasm_instance_));
Handle<Script> script(wasm_instance_->compiled_module()->script(), isolate_);
DCHECK(script->IsUserJavaScript());
return handle(script->name(), isolate_);
}
Handle<Object> AsmJsWasmStackFrame::GetScriptNameOrSourceUrl() {
Handle<Script> script =
wasm::GetScript(Handle<JSObject>::cast(wasm_instance_));
Handle<Script> script(wasm_instance_->compiled_module()->script(), isolate_);
DCHECK_EQ(Script::TYPE_NORMAL, script->type());
return ScriptNameOrSourceUrl(script, isolate_);
}
@ -764,8 +760,8 @@ Handle<Object> AsmJsWasmStackFrame::GetScriptNameOrSourceUrl() {
int AsmJsWasmStackFrame::GetPosition() const {
DCHECK_LE(0, offset_);
int byte_offset = code_->SourcePosition(offset_);
Handle<WasmCompiledModule> compiled_module(
WasmInstanceObject::cast(*wasm_instance_)->compiled_module(), isolate_);
Handle<WasmCompiledModule> compiled_module(wasm_instance_->compiled_module(),
isolate_);
DCHECK_LE(0, byte_offset);
return WasmCompiledModule::GetSourcePosition(
compiled_module, wasm_func_index_, static_cast<uint32_t>(byte_offset),
@ -774,16 +770,14 @@ int AsmJsWasmStackFrame::GetPosition() const {
int AsmJsWasmStackFrame::GetLineNumber() {
DCHECK_LE(0, GetPosition());
Handle<Script> script =
wasm::GetScript(Handle<JSObject>::cast(wasm_instance_));
Handle<Script> script(wasm_instance_->compiled_module()->script(), isolate_);
DCHECK(script->IsUserJavaScript());
return Script::GetLineNumber(script, GetPosition()) + 1;
}
int AsmJsWasmStackFrame::GetColumnNumber() {
DCHECK_LE(0, GetPosition());
Handle<Script> script =
wasm::GetScript(Handle<JSObject>::cast(wasm_instance_));
Handle<Script> script(wasm_instance_->compiled_module()->script(), isolate_);
DCHECK(script->IsUserJavaScript());
return Script::GetColumnNumber(script, GetPosition()) + 1;
}

View File

@ -31,7 +31,8 @@ WasmInstanceObject* GetWasmInstanceOnStackTop(Isolate* isolate) {
Memory::Address_at(entry + StandardFrameConstants::kCallerPCOffset);
Code* code = isolate->inner_pointer_to_code_cache()->GetCacheEntry(pc)->code;
DCHECK_EQ(Code::WASM_FUNCTION, code->kind());
WasmInstanceObject* owning_instance = wasm::GetOwningWasmInstance(code);
WasmInstanceObject* owning_instance =
WasmInstanceObject::GetOwningInstance(code);
CHECK_NOT_NULL(owning_instance);
return owning_instance;
}

View File

@ -658,7 +658,7 @@ void WebAssemblyTableGrow(const v8::FunctionCallbackInfo<v8::Value>& args) {
}
int new_size = static_cast<int>(new_size64);
receiver->grow(i_isolate, static_cast<uint32_t>(new_size - old_size));
receiver->Grow(i_isolate, static_cast<uint32_t>(new_size - old_size));
if (new_size != old_size) {
i::Handle<i::FixedArray> new_array =
@ -723,10 +723,15 @@ void WebAssemblyTableSet(const v8::FunctionCallbackInfo<v8::Value>& args) {
return;
}
i::wasm::TableSet(&thrower, i_isolate, receiver, index,
value->IsNull(i_isolate)
? i::Handle<i::JSFunction>::null()
: i::Handle<i::JSFunction>::cast(value));
if (index < 0 || index >= receiver->functions()->length()) {
thrower.RangeError("index out of bounds");
return;
}
i::WasmTableObject::Set(i_isolate, receiver, static_cast<int32_t>(index),
value->IsNull(i_isolate)
? i::Handle<i::JSFunction>::null()
: i::Handle<i::JSFunction>::cast(value));
}
// WebAssembly.Memory.grow(num) -> num

View File

@ -364,20 +364,6 @@ std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name) {
return os;
}
WasmInstanceObject* GetOwningWasmInstance(Code* code) {
DisallowHeapAllocation no_gc;
DCHECK(code->kind() == Code::WASM_FUNCTION ||
code->kind() == Code::WASM_INTERPRETER_ENTRY);
FixedArray* deopt_data = code->deoptimization_data();
DCHECK_EQ(code->kind() == Code::WASM_INTERPRETER_ENTRY ? 1 : 2,
deopt_data->length());
Object* weak_link = deopt_data->get(0);
DCHECK(weak_link->IsWeakCell());
WeakCell* cell = WeakCell::cast(weak_link);
if (cell->cleared()) return nullptr;
return WasmInstanceObject::cast(cell->value());
}
WasmModule::WasmModule(std::unique_ptr<Zone> owned)
: signature_zone(std::move(owned)) {}
@ -446,42 +432,6 @@ void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables,
}
}
void TableSet(ErrorThrower* thrower, Isolate* isolate,
Handle<WasmTableObject> table, int64_t index,
Handle<JSFunction> function) {
Handle<FixedArray> array(table->functions(), isolate);
if (index < 0 || index >= array->length()) {
thrower->RangeError("index out of bounds");
return;
}
int index32 = static_cast<int>(index);
Handle<FixedArray> dispatch_tables(table->dispatch_tables(), isolate);
WasmFunction* wasm_function = nullptr;
Handle<Code> code = Handle<Code>::null();
Handle<Object> value = isolate->factory()->null_value();
if (!function.is_null()) {
wasm_function = GetWasmFunctionForExport(isolate, function);
// The verification that {function} is an export was done
// by the caller.
DCHECK_NOT_NULL(wasm_function);
code = UnwrapExportWrapper(function);
value = Handle<Object>::cast(function);
}
UpdateDispatchTables(isolate, dispatch_tables, index32, wasm_function, code);
array->set(index32, *value);
}
Handle<Script> GetScript(Handle<JSObject> instance) {
WasmCompiledModule* compiled_module =
WasmInstanceObject::cast(*instance)->compiled_module();
return handle(compiled_module->script());
}
bool IsWasmCodegenAllowed(Isolate* isolate, Handle<Context> context) {
// TODO(wasm): Once wasm has its own CSP policy, we should introduce a
// separate callback that includes information about the module about to be
@ -962,7 +912,8 @@ Handle<Code> CompileLazy(Isolate* isolate) {
// Then this is a direct call (otherwise we would have attached the instance
// via deopt data to the lazy compile stub). Just use the instance of the
// caller.
instance = handle(GetOwningWasmInstance(*caller_code), isolate);
instance =
handle(WasmInstanceObject::GetOwningInstance(*caller_code), isolate);
}
int offset =
static_cast<int>(it.frame()->pc() - caller_code->instruction_start());

View File

@ -274,11 +274,6 @@ std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name);
// If no debug info exists yet, it is created automatically.
Handle<WasmDebugInfo> GetDebugInfo(Handle<JSObject> wasm);
// Get the script of the wasm module. If the origin of the module is asm.js, the
// returned Script will be a JavaScript Script of Script::TYPE_NORMAL, otherwise
// it's of type TYPE_WASM.
Handle<Script> GetScript(Handle<JSObject> instance);
V8_EXPORT_PRIVATE MaybeHandle<WasmModuleObject> CreateModuleObjectFromBytes(
Isolate* isolate, const byte* start, const byte* end, ErrorThrower* thrower,
ModuleOrigin origin, Handle<Script> asm_js_script,
@ -300,11 +295,6 @@ V8_EXPORT_PRIVATE Handle<JSArray> GetCustomSections(
// function index, the inner one by the local index.
Handle<FixedArray> DecodeLocalNames(Isolate*, Handle<WasmCompiledModule>);
// Assumed to be called with a code object associated to a wasm module instance.
// Intended to be called from runtime functions.
// Returns nullptr on failing to get owning instance.
WasmInstanceObject* GetOwningWasmInstance(Code* code);
Handle<JSArrayBuffer> NewArrayBuffer(
Isolate*, size_t size, bool enable_guard_regions,
SharedFlag shared = SharedFlag::kNotShared);
@ -327,10 +317,6 @@ WasmFunction* GetWasmFunctionForExport(Isolate* isolate, Handle<Object> target);
// {export_wrapper} is known to be an export.
Handle<Code> UnwrapExportWrapper(Handle<JSFunction> export_wrapper);
void TableSet(ErrorThrower* thrower, Isolate* isolate,
Handle<WasmTableObject> table, int64_t index,
Handle<JSFunction> function);
void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables,
int index, WasmFunction* function, Handle<Code> code);

View File

@ -219,7 +219,7 @@ Handle<FixedArray> WasmTableObject::AddDispatchTable(
return new_dispatch_tables;
}
void WasmTableObject::grow(Isolate* isolate, uint32_t count) {
void WasmTableObject::Grow(Isolate* isolate, uint32_t count) {
Handle<FixedArray> dispatch_tables(this->dispatch_tables());
DCHECK_EQ(0, dispatch_tables->length() % 4);
uint32_t old_size = functions()->length();
@ -274,6 +274,29 @@ void WasmTableObject::grow(Isolate* isolate, uint32_t count) {
}
}
void WasmTableObject::Set(Isolate* isolate, Handle<WasmTableObject> table,
int32_t index, Handle<JSFunction> function) {
Handle<FixedArray> array(table->functions(), isolate);
Handle<FixedArray> dispatch_tables(table->dispatch_tables(), isolate);
WasmFunction* wasm_function = nullptr;
Handle<Code> code = Handle<Code>::null();
Handle<Object> value = isolate->factory()->null_value();
if (!function.is_null()) {
wasm_function = wasm::GetWasmFunctionForExport(isolate, function);
// The verification that {function} is an export was done
// by the caller.
DCHECK_NOT_NULL(wasm_function);
code = wasm::UnwrapExportWrapper(function);
value = Handle<Object>::cast(function);
}
UpdateDispatchTables(isolate, dispatch_tables, index, wasm_function, code);
array->set(index, *value);
}
namespace {
Handle<JSArrayBuffer> GrowMemoryBuffer(Isolate* isolate,
@ -539,6 +562,20 @@ uint32_t WasmInstanceObject::GetMaxMemoryPages() {
return FLAG_wasm_max_mem_pages;
}
WasmInstanceObject* WasmInstanceObject::GetOwningInstance(Code* code) {
DisallowHeapAllocation no_gc;
DCHECK(code->kind() == Code::WASM_FUNCTION ||
code->kind() == Code::WASM_INTERPRETER_ENTRY);
FixedArray* deopt_data = code->deoptimization_data();
DCHECK_EQ(code->kind() == Code::WASM_INTERPRETER_ENTRY ? 1 : 2,
deopt_data->length());
Object* weak_link = deopt_data->get(0);
DCHECK(weak_link->IsWeakCell());
WeakCell* cell = WeakCell::cast(weak_link);
if (cell->cleared()) return nullptr;
return WasmInstanceObject::cast(cell->value());
}
bool WasmExportedFunction::IsWasmExportedFunction(Object* object) {
if (!object->IsJSFunction()) return false;
Handle<JSFunction> js_function(JSFunction::cast(object));

View File

@ -91,7 +91,7 @@ class WasmTableObject : public JSObject {
inline uint32_t current_length();
inline bool has_maximum_length();
void grow(Isolate* isolate, uint32_t count);
void Grow(Isolate* isolate, uint32_t count);
static Handle<WasmTableObject> New(Isolate* isolate, uint32_t initial,
int64_t maximum,
@ -100,6 +100,9 @@ class WasmTableObject : public JSObject {
Isolate* isolate, Handle<WasmTableObject> table,
Handle<WasmInstanceObject> instance, int table_index,
Handle<FixedArray> function_table, Handle<FixedArray> signature_table);
static void Set(Isolate* isolate, Handle<WasmTableObject> table,
int32_t index, Handle<JSFunction> function);
};
// Representation of a WebAssembly.Memory JavaScript-level object.
@ -200,6 +203,11 @@ class WasmInstanceObject : public JSObject {
uint32_t pages);
uint32_t GetMaxMemoryPages();
// Assumed to be called with a code object associated to a wasm module
// instance. Intended to be called from runtime functions. Returns nullptr on
// failing to get owning instance.
static WasmInstanceObject* GetOwningInstance(Code* code);
};
// A WASM function that is wrapped and exported to JavaScript.
@ -216,6 +224,8 @@ class WasmExportedFunction : public JSFunction {
MaybeHandle<String> maybe_name,
int func_index, int arity,
Handle<Code> export_wrapper);
Handle<Code> GetWasmCode();
};
// Information shared by all WasmCompiledModule objects for the same module.