WebAssemblySource.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "RuntimeLanguagePch.h"
  6. #include "WebAssemblySource.h"
  7. #ifdef ENABLE_WASM
  8. namespace Js
  9. {
  10. WebAssemblySource::WebAssemblySource(Var source, bool createNewContext, ScriptContext * scriptContext) :
  11. buffer(nullptr), bufferLength(0), sourceInfo(nullptr)
  12. {
  13. ReadBufferSource(source, scriptContext);
  14. CreateSourceInfo(createNewContext, scriptContext);
  15. }
  16. WebAssemblySource::WebAssemblySource(byte* source, uint bufferLength, bool createNewContext, ScriptContext* scriptContext):
  17. buffer(source), bufferLength(bufferLength)
  18. {
  19. CreateSourceInfo(createNewContext, scriptContext);
  20. }
  21. void WebAssemblySource::ReadBufferSource(Var val, ScriptContext * scriptContext)
  22. {
  23. BYTE* srcBuffer;
  24. if (Js::VarIs<Js::TypedArrayBase>(val))
  25. {
  26. Js::TypedArrayBase* array = Js::VarTo<Js::TypedArrayBase>(val);
  27. srcBuffer = array->GetByteBuffer();
  28. bufferLength = array->GetByteLength();
  29. }
  30. else if (Js::VarIs<Js::ArrayBuffer>(val))
  31. {
  32. Js::ArrayBuffer* arrayBuffer = Js::VarTo<Js::ArrayBuffer>(val);
  33. srcBuffer = arrayBuffer->GetBuffer();
  34. bufferLength = arrayBuffer->GetByteLength();
  35. }
  36. else
  37. {
  38. // The buffer was not a TypedArray nor an ArrayBuffer
  39. JavascriptError::ThrowTypeError(scriptContext, WASMERR_NeedBufferSource);
  40. }
  41. Assert(srcBuffer || bufferLength == 0);
  42. if (bufferLength > 0)
  43. {
  44. // copy buffer so external changes to it don't cause issues when defer parsing
  45. buffer = RecyclerNewArrayLeaf(scriptContext->GetRecycler(), byte, bufferLength);
  46. js_memcpy_s(buffer, bufferLength, srcBuffer, bufferLength);
  47. }
  48. }
  49. void WebAssemblySource::CreateSourceInfo(bool createNewContext, ScriptContext* scriptContext)
  50. {
  51. SRCINFO si = {
  52. /* sourceContextInfo */ nullptr,
  53. /* dlnHost */ 0,
  54. /* ulColumnHost */ 0,
  55. /* lnMinHost */ 0,
  56. /* ichMinHost */ 0,
  57. /* ichLimHost */ 0,
  58. /* ulCharOffset */ 0,
  59. /* mod */ 0,
  60. /* grfsi */ 0
  61. };
  62. SRCINFO const * srcInfo = scriptContext->Cache()->noContextGlobalSourceInfo;
  63. if (createNewContext && CONFIG_FLAG(WasmAssignModuleID))
  64. {
  65. // It is not legal to assign a SourceContextInfo on dynamic code, but it is usefull for debugging
  66. // Only do it if the specified as a test config
  67. si.sourceContextInfo = scriptContext->CreateSourceContextInfo(scriptContext->GetNextSourceContextId(), nullptr, 0, nullptr);
  68. srcInfo = &si;
  69. }
  70. // Note: We don't have real "source info" for Wasm. Following are just placeholders.
  71. // Hack: Wasm handles debugging differently. Fake this as "LibraryCode" so that
  72. // normal script debugging code ignores this source info and its functions.
  73. const int32 cchLength = static_cast<int32>(bufferLength / sizeof(char16));
  74. sourceInfo = Utf8SourceInfo::NewWithNoCopy(
  75. scriptContext, (LPCUTF8)buffer, cchLength, bufferLength, srcInfo, /*isLibraryCode*/true);
  76. scriptContext->SaveSourceNoCopy(sourceInfo, cchLength, /*isCesu8*/false);
  77. }
  78. }
  79. #endif