1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/runtime/runtime-utils.h"
6
7 #include "src/arguments.h"
8 #include "src/assembler.h"
9 #include "src/compiler/wasm-compiler.h"
10 #include "src/conversions.h"
11 #include "src/debug/debug.h"
12 #include "src/factory.h"
13 #include "src/frames-inl.h"
14 #include "src/objects-inl.h"
15 #include "src/v8memory.h"
16 #include "src/wasm/wasm-module.h"
17
18 namespace v8 {
19 namespace internal {
20
RUNTIME_FUNCTION(Runtime_WasmMemorySize)21 RUNTIME_FUNCTION(Runtime_WasmMemorySize) {
22 HandleScope scope(isolate);
23 DCHECK_EQ(0, args.length());
24
25 Handle<JSObject> module_instance;
26 {
27 // Get the module JSObject
28 DisallowHeapAllocation no_allocation;
29 const Address entry = Isolate::c_entry_fp(isolate->thread_local_top());
30 Address pc =
31 Memory::Address_at(entry + StandardFrameConstants::kCallerPCOffset);
32 Code* code =
33 isolate->inner_pointer_to_code_cache()->GetCacheEntry(pc)->code;
34 Object* owning_instance = wasm::GetOwningWasmInstance(code);
35 CHECK_NOT_NULL(owning_instance);
36 module_instance = handle(JSObject::cast(owning_instance), isolate);
37 }
38 return *isolate->factory()->NewNumberFromInt(
39 wasm::GetInstanceMemorySize(isolate, module_instance));
40 }
41
RUNTIME_FUNCTION(Runtime_WasmGrowMemory)42 RUNTIME_FUNCTION(Runtime_WasmGrowMemory) {
43 HandleScope scope(isolate);
44 DCHECK_EQ(1, args.length());
45 CONVERT_UINT32_ARG_CHECKED(delta_pages, 0);
46 Handle<JSObject> module_instance;
47 {
48 // Get the module JSObject
49 DisallowHeapAllocation no_allocation;
50 const Address entry = Isolate::c_entry_fp(isolate->thread_local_top());
51 Address pc =
52 Memory::Address_at(entry + StandardFrameConstants::kCallerPCOffset);
53 Code* code =
54 isolate->inner_pointer_to_code_cache()->GetCacheEntry(pc)->code;
55 Object* owning_instance = wasm::GetOwningWasmInstance(code);
56 CHECK_NOT_NULL(owning_instance);
57 module_instance = handle(JSObject::cast(owning_instance), isolate);
58 }
59 return *isolate->factory()->NewNumberFromInt(
60 wasm::GrowInstanceMemory(isolate, module_instance, delta_pages));
61 }
62
RUNTIME_FUNCTION(Runtime_WasmThrowTypeError)63 RUNTIME_FUNCTION(Runtime_WasmThrowTypeError) {
64 HandleScope scope(isolate);
65 DCHECK_EQ(0, args.length());
66 THROW_NEW_ERROR_RETURN_FAILURE(
67 isolate, NewTypeError(MessageTemplate::kWasmTrapTypeError));
68 }
69
RUNTIME_FUNCTION(Runtime_WasmThrow)70 RUNTIME_FUNCTION(Runtime_WasmThrow) {
71 HandleScope scope(isolate);
72 DCHECK_EQ(2, args.length());
73 CONVERT_SMI_ARG_CHECKED(lower, 0);
74 CONVERT_SMI_ARG_CHECKED(upper, 1);
75
76 const int32_t thrown_value = (upper << 16) | lower;
77
78 return isolate->Throw(*isolate->factory()->NewNumberFromInt(thrown_value));
79 }
80
RUNTIME_FUNCTION(Runtime_WasmGetCaughtExceptionValue)81 RUNTIME_FUNCTION(Runtime_WasmGetCaughtExceptionValue) {
82 HandleScope scope(isolate);
83 DCHECK_EQ(1, args.length());
84 Object* exception = args[0];
85 // The unwinder will only deliver exceptions to wasm if the exception is a
86 // Number or a Smi (which we have just converted to a Number.) This logic
87 // lives in Isolate::is_catchable_by_wasm(Object*).
88 CHECK(exception->IsNumber());
89 return exception;
90 }
91
92 } // namespace internal
93 } // namespace v8
94