| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WASM_RUN_UTILS_H | 5 #ifndef WASM_RUN_UTILS_H |
| 6 #define WASM_RUN_UTILS_H | 6 #define WASM_RUN_UTILS_H |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <stdlib.h> | 9 #include <stdlib.h> |
| 10 #include <string.h> | 10 #include <string.h> |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 152 |
| 153 // Pseudo-randomly intialize the memory. | 153 // Pseudo-randomly intialize the memory. |
| 154 void RandomizeMemory(unsigned int seed = 88) { | 154 void RandomizeMemory(unsigned int seed = 88) { |
| 155 byte* raw = raw_mem_start<byte>(); | 155 byte* raw = raw_mem_start<byte>(); |
| 156 byte* end = raw_mem_end<byte>(); | 156 byte* end = raw_mem_end<byte>(); |
| 157 v8::base::RandomNumberGenerator rng; | 157 v8::base::RandomNumberGenerator rng; |
| 158 rng.SetSeed(seed); | 158 rng.SetSeed(seed); |
| 159 rng.NextBytes(raw, end - raw); | 159 rng.NextBytes(raw, end - raw); |
| 160 } | 160 } |
| 161 | 161 |
| 162 int AddFunction(FunctionSig* sig, Handle<Code> code) { | 162 uint32_t AddFunction(FunctionSig* sig, Handle<Code> code) { |
| 163 if (module->functions.size() == 0) { | 163 if (module->functions.size() == 0) { |
| 164 // TODO(titzer): Reserving space here to avoid the underlying WasmFunction | 164 // TODO(titzer): Reserving space here to avoid the underlying WasmFunction |
| 165 // structs from moving. | 165 // structs from moving. |
| 166 module->functions.reserve(kMaxFunctions); | 166 module->functions.reserve(kMaxFunctions); |
| 167 } | 167 } |
| 168 uint32_t index = static_cast<uint32_t>(module->functions.size()); | 168 uint32_t index = static_cast<uint32_t>(module->functions.size()); |
| 169 module->functions.push_back( | 169 module->functions.push_back( |
| 170 {sig, index, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false}); | 170 {sig, index, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false}); |
| 171 instance->function_code.push_back(code); | 171 instance->function_code.push_back(code); |
| 172 DCHECK_LT(index, kMaxFunctions); // limited for testing. | 172 DCHECK_LT(index, kMaxFunctions); // limited for testing. |
| 173 return index; | 173 return index; |
| 174 } | 174 } |
| 175 | 175 |
| 176 uint32_t AddJsFunction(FunctionSig* sig, const char* source) { |
| 177 Handle<JSFunction> jsfunc = Handle<JSFunction>::cast(v8::Utils::OpenHandle( |
| 178 *v8::Local<v8::Function>::Cast(CompileRun(source)))); |
| 179 uint32_t index = AddFunction(sig, Handle<Code>::null()); |
| 180 Isolate* isolate = module->shared_isolate; |
| 181 WasmName module_name = {"test", 4}; |
| 182 WasmName function_name = {nullptr, 0}; |
| 183 Handle<Code> code = CompileWasmToJSWrapper(isolate, this, jsfunc, sig, |
| 184 module_name, function_name); |
| 185 instance->function_code[index] = code; |
| 186 return index; |
| 187 } |
| 188 |
| 189 Handle<JSFunction> WrapCode(uint32_t index) { |
| 190 Isolate* isolate = module->shared_isolate; |
| 191 // Wrap the code so it can be called as a JS function. |
| 192 Handle<String> name = isolate->factory()->NewStringFromStaticChars("main"); |
| 193 Handle<JSObject> module_object = Handle<JSObject>(0, isolate); |
| 194 Handle<Code> code = instance->function_code[index]; |
| 195 WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context()); |
| 196 return compiler::CompileJSToWasmWrapper(isolate, this, name, code, |
| 197 module_object, index); |
| 198 } |
| 199 |
| 176 void SetFunctionCode(uint32_t index, Handle<Code> code) { | 200 void SetFunctionCode(uint32_t index, Handle<Code> code) { |
| 177 instance->function_code[index] = code; | 201 instance->function_code[index] = code; |
| 178 } | 202 } |
| 179 | 203 |
| 180 void AddIndirectFunctionTable(int* functions, int table_size) { | 204 void AddIndirectFunctionTable(int* functions, int table_size) { |
| 181 Isolate* isolate = module->shared_isolate; | 205 Isolate* isolate = module->shared_isolate; |
| 182 Handle<FixedArray> fixed = | 206 Handle<FixedArray> fixed = |
| 183 isolate->factory()->NewFixedArray(2 * table_size); | 207 isolate->factory()->NewFixedArray(2 * table_size); |
| 184 instance->function_table = fixed; | 208 instance->function_table = fixed; |
| 185 DCHECK_EQ(0u, module->function_table.size()); | 209 DCHECK_EQ(0u, module->function_table.size()); |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 if (p1 == MachineType::None()) return 1; | 617 if (p1 == MachineType::None()) return 1; |
| 594 if (p2 == MachineType::None()) return 2; | 618 if (p2 == MachineType::None()) return 2; |
| 595 if (p3 == MachineType::None()) return 3; | 619 if (p3 == MachineType::None()) return 3; |
| 596 return 4; | 620 return 4; |
| 597 } | 621 } |
| 598 }; | 622 }; |
| 599 | 623 |
| 600 } // namespace | 624 } // namespace |
| 601 | 625 |
| 602 #endif | 626 #endif |
| OLD | NEW |