| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 #include <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "src/base/atomic-utils.h" | 7 #include "src/base/atomic-utils.h" |
| 8 #include "src/code-stubs.h" | 8 #include "src/code-stubs.h" |
| 9 | 9 |
| 10 #include "src/macro-assembler.h" | 10 #include "src/macro-assembler.h" |
| (...skipping 1654 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1665 thrower->Failed("Wasm decoding failed", result); | 1665 thrower->Failed("Wasm decoding failed", result); |
| 1666 return nothing; | 1666 return nothing; |
| 1667 } | 1667 } |
| 1668 MaybeHandle<FixedArray> compiled_module = | 1668 MaybeHandle<FixedArray> compiled_module = |
| 1669 decoded_module->CompileFunctions(isolate, thrower); | 1669 decoded_module->CompileFunctions(isolate, thrower); |
| 1670 if (compiled_module.is_null()) return nothing; | 1670 if (compiled_module.is_null()) return nothing; |
| 1671 | 1671 |
| 1672 return CreateCompiledModuleObject(isolate, compiled_module.ToHandleChecked(), | 1672 return CreateCompiledModuleObject(isolate, compiled_module.ToHandleChecked(), |
| 1673 origin); | 1673 origin); |
| 1674 } | 1674 } |
| 1675 | |
| 1676 namespace testing { | |
| 1677 | |
| 1678 int32_t CompileAndRunWasmModule(Isolate* isolate, const byte* module_start, | |
| 1679 const byte* module_end, bool asm_js) { | |
| 1680 HandleScope scope(isolate); | |
| 1681 Zone zone(isolate->allocator()); | |
| 1682 ErrorThrower thrower(isolate, "CompileAndRunWasmModule"); | |
| 1683 | |
| 1684 // Decode the module, but don't verify function bodies, since we'll | |
| 1685 // be compiling them anyway. | |
| 1686 ModuleResult decoding_result = | |
| 1687 DecodeWasmModule(isolate, &zone, module_start, module_end, false, | |
| 1688 asm_js ? kAsmJsOrigin : kWasmOrigin); | |
| 1689 | |
| 1690 std::unique_ptr<const WasmModule> module(decoding_result.val); | |
| 1691 if (decoding_result.failed()) { | |
| 1692 // Module verification failed. throw. | |
| 1693 thrower.Error("WASM.compileRun() failed: %s", | |
| 1694 decoding_result.error_msg.get()); | |
| 1695 return -1; | |
| 1696 } | |
| 1697 | |
| 1698 if (module->import_table.size() > 0) { | |
| 1699 thrower.Error("Not supported: module has imports."); | |
| 1700 } | |
| 1701 if (module->export_table.size() == 0) { | |
| 1702 thrower.Error("Not supported: module has no exports."); | |
| 1703 } | |
| 1704 | |
| 1705 if (thrower.error()) return -1; | |
| 1706 MaybeHandle<FixedArray> compiled_module = | |
| 1707 module->CompileFunctions(isolate, &thrower); | |
| 1708 | |
| 1709 if (compiled_module.is_null()) return -1; | |
| 1710 Handle<JSObject> instance = | |
| 1711 WasmModule::Instantiate(isolate, compiled_module.ToHandleChecked(), | |
| 1712 Handle<JSReceiver>::null(), | |
| 1713 Handle<JSArrayBuffer>::null()) | |
| 1714 .ToHandleChecked(); | |
| 1715 | |
| 1716 return CallFunction(isolate, instance, &thrower, asm_js ? "caller" : "main", | |
| 1717 0, nullptr, asm_js); | |
| 1718 } | |
| 1719 | |
| 1720 int32_t CallFunction(Isolate* isolate, Handle<JSObject> instance, | |
| 1721 ErrorThrower* thrower, const char* name, int argc, | |
| 1722 Handle<Object> argv[], bool asm_js) { | |
| 1723 Handle<JSObject> exports_object; | |
| 1724 if (asm_js) { | |
| 1725 exports_object = instance; | |
| 1726 } else { | |
| 1727 Handle<Name> exports = isolate->factory()->InternalizeUtf8String("exports"); | |
| 1728 exports_object = Handle<JSObject>::cast( | |
| 1729 JSObject::GetProperty(instance, exports).ToHandleChecked()); | |
| 1730 } | |
| 1731 Handle<Name> main_name = isolate->factory()->NewStringFromAsciiChecked(name); | |
| 1732 PropertyDescriptor desc; | |
| 1733 Maybe<bool> property_found = JSReceiver::GetOwnPropertyDescriptor( | |
| 1734 isolate, exports_object, main_name, &desc); | |
| 1735 if (!property_found.FromMaybe(false)) return -1; | |
| 1736 | |
| 1737 Handle<JSFunction> main_export = Handle<JSFunction>::cast(desc.value()); | |
| 1738 | |
| 1739 // Call the JS function. | |
| 1740 Handle<Object> undefined = isolate->factory()->undefined_value(); | |
| 1741 MaybeHandle<Object> retval = | |
| 1742 Execution::Call(isolate, main_export, undefined, argc, argv); | |
| 1743 | |
| 1744 // The result should be a number. | |
| 1745 if (retval.is_null()) { | |
| 1746 thrower->Error("WASM.compileRun() failed: Invocation was null"); | |
| 1747 return -1; | |
| 1748 } | |
| 1749 Handle<Object> result = retval.ToHandleChecked(); | |
| 1750 if (result->IsSmi()) { | |
| 1751 return Smi::cast(*result)->value(); | |
| 1752 } | |
| 1753 if (result->IsHeapNumber()) { | |
| 1754 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); | |
| 1755 } | |
| 1756 thrower->Error("WASM.compileRun() failed: Return value should be number"); | |
| 1757 return -1; | |
| 1758 } | |
| 1759 | |
| 1760 } // namespace testing | |
| 1761 } // namespace wasm | 1675 } // namespace wasm |
| 1762 } // namespace internal | 1676 } // namespace internal |
| 1763 } // namespace v8 | 1677 } // namespace v8 |
| OLD | NEW |