WasmLibrary.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeLibraryPch.h"
  6. #ifdef ENABLE_WASM
  7. #include "../WasmReader/WasmReaderPch.h"
  8. // Included for AsmJsDefaultEntryThunk
  9. #include "Language/InterpreterStackFrame.h"
  10. namespace Js
  11. {
  12. Var WasmLibrary::WasmLazyTrapCallback(RecyclableObject *callee, CallInfo, ...)
  13. {
  14. WasmScriptFunction* asmFunction = static_cast<WasmScriptFunction*>(callee);
  15. Assert(asmFunction);
  16. ScriptContext * scriptContext = asmFunction->GetScriptContext();
  17. Assert(scriptContext);
  18. auto error = asmFunction->GetFunctionBody()->GetAsmJsFunctionInfo()->GetLazyError();
  19. JavascriptExceptionOperators::Throw(error, scriptContext);
  20. }
  21. void WasmLibrary::ResetFunctionBodyDefaultEntryPoint(FunctionBody* body)
  22. {
  23. body->GetDefaultFunctionEntryPointInfo()->SetIsAsmJSFunction(true);
  24. body->GetDefaultFunctionEntryPointInfo()->jsMethod = AsmJsDefaultEntryThunk;
  25. body->SetOriginalEntryPoint(AsmJsDefaultEntryThunk);
  26. // Reset jit status for this function
  27. body->SetIsAsmJsFullJitScheduled(false);
  28. Assert(body->HasValidEntryPoint());
  29. }
  30. }
  31. #endif // ENABLE_WASM
  32. Js::JavascriptMethod Js::WasmLibrary::EnsureWasmEntrypoint(Js::ScriptFunction* func)
  33. {
  34. #ifdef ENABLE_WASM
  35. if (func->GetFunctionBody()->IsWasmFunction())
  36. {
  37. FunctionBody* body = func->GetFunctionBody();
  38. AsmJsFunctionInfo* info = body->GetAsmJsFunctionInfo();
  39. ScriptContext* scriptContext = func->GetScriptContext();
  40. Js::FunctionEntryPointInfo * entrypointInfo = (Js::FunctionEntryPointInfo*)func->GetEntryPointInfo();
  41. if (info->GetLazyError())
  42. {
  43. // We might have parsed this in the past and there was an error
  44. entrypointInfo->jsMethod = WasmLibrary::WasmLazyTrapCallback;
  45. }
  46. else if (body->GetByteCodeCount() == 0)
  47. {
  48. Wasm::WasmReaderInfo* readerInfo = info->GetWasmReaderInfo();
  49. AssertOrFailFast(readerInfo);
  50. try
  51. {
  52. Wasm::WasmBytecodeGenerator::GenerateFunctionBytecode(scriptContext, readerInfo);
  53. entrypointInfo->jsMethod = AsmJsDefaultEntryThunk;
  54. WAsmJs::JitFunctionIfReady(func);
  55. }
  56. catch (Wasm::WasmCompilationException& ex)
  57. {
  58. AutoFreeExceptionMessage autoCleanExceptionMessage;
  59. char16* exceptionMessage = WebAssemblyModule::FormatExceptionMessage(&ex, &autoCleanExceptionMessage, readerInfo->m_module, body);
  60. JavascriptLibrary *library = scriptContext->GetLibrary();
  61. JavascriptError *pError = library->CreateWebAssemblyCompileError();
  62. JavascriptError::SetErrorMessage(pError, WASMERR_WasmCompileError, exceptionMessage, scriptContext);
  63. entrypointInfo->jsMethod = WasmLibrary::WasmLazyTrapCallback;
  64. info->SetLazyError(pError);
  65. }
  66. }
  67. // The function has already been parsed, just fix up the entry point
  68. else if (body->GetIsAsmJsFullJitScheduled())
  69. {
  70. Js::FunctionEntryPointInfo* defaultEntryPoint = (Js::FunctionEntryPointInfo*)body->GetDefaultEntryPointInfo();
  71. func->ChangeEntryPoint(defaultEntryPoint, defaultEntryPoint->jsMethod);
  72. }
  73. else
  74. {
  75. entrypointInfo->jsMethod = AsmJsDefaultEntryThunk;
  76. }
  77. Assert(body->HasValidEntryPoint());
  78. Js::JavascriptMethod jsMethod = func->GetEntryPointInfo()->jsMethod;
  79. // We are already in AsmJsDefaultEntryThunk so return null so it just keeps going
  80. return jsMethod == AsmJsDefaultEntryThunk ? nullptr : jsMethod;
  81. }
  82. #endif
  83. return nullptr;
  84. }