Skip to content

Commit 1ade7ca

Browse files
committed
modify Wasm.instantiateModule to call new JS API
1 parent 227cf79 commit 1ade7ca

13 files changed

Lines changed: 69 additions & 115 deletions

lib/Runtime/Library/WasmLibrary.cpp

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace Js
1313
{
1414
const unsigned int WasmLibrary::experimentalVersion = Wasm::experimentalVersion;
1515

16-
1716
Var WasmLibrary::instantiateModule(RecyclableObject* function, CallInfo callInfo, ...)
1817
{
1918
PROBE_STACK(function->GetScriptContext(), Js::Constants::MinStackDefault);
@@ -40,18 +39,18 @@ namespace Js
4039
{
4140
JavascriptError::ThrowTypeError(scriptContext, JSERR_This_NeedTypedArray, _u("[Wasm].instantiateModule(typedArray,)"));
4241
}
43-
4442
BYTE* buffer;
4543
uint byteLength;
44+
Var bufferSrc = args[1];
4645
if (isTypedArray)
4746
{
48-
Js::TypedArrayBase* array = Js::TypedArrayBase::FromVar(args[1]);
47+
Js::TypedArrayBase* array = Js::TypedArrayBase::FromVar(bufferSrc);
4948
buffer = array->GetByteBuffer();
5049
byteLength = array->GetByteLength();
5150
}
5251
else
5352
{
54-
Js::ArrayBuffer* arrayBuffer = Js::ArrayBuffer::FromVar(args[1]);
53+
Js::ArrayBuffer* arrayBuffer = Js::ArrayBuffer::FromVar(bufferSrc);
5554
buffer = arrayBuffer->GetBuffer();
5655
byteLength = arrayBuffer->GetByteLength();
5756
}
@@ -60,47 +59,8 @@ namespace Js
6059
{
6160
JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject, _u("[Wasm].instantiateModule(,ffi)"));
6261
}
63-
#if 0
64-
Js::Var ffi = args[2];
65-
66-
CompileScriptException se;
67-
Js::Var exportObject;
68-
Js::Var start = nullptr;
69-
Js::Utf8SourceInfo* utf8SourceInfo;
70-
BEGIN_LEAVE_SCRIPT_INTERNAL(scriptContext)
71-
exportObject = WasmLibrary::LoadWasmScript(
72-
scriptContext,
73-
(const char16*)buffer,
74-
args[1],
75-
nullptr, // source info
76-
&se,
77-
&utf8SourceInfo,
78-
byteLength,
79-
ffi,
80-
&start
81-
);
82-
END_LEAVE_SCRIPT_INTERNAL(scriptContext);
83-
84-
HRESULT hr = se.ei.scode;
85-
if (FAILED(hr))
86-
{
87-
if (hr == E_OUTOFMEMORY || hr == VBSERR_OutOfMemory || hr == VBSERR_OutOfStack || hr == ERRnoMemory)
88-
{
89-
Js::Throw::OutOfMemory();
90-
}
91-
JavascriptError::ThrowParserError(scriptContext, hr, &se);
92-
}
93-
94-
if (exportObject && start)
95-
{
96-
Js::ScriptFunction* f = Js::AsmJsScriptFunction::FromVar(start);
97-
Js::CallInfo info(Js::CallFlags_New, 1);
98-
Js::Arguments startArg(info, &start);
99-
Js::JavascriptFunction::CallFunction<true>(f, f->GetEntryPoint(), startArg);
100-
}
101-
return exportObject;
102-
#endif
103-
return nullptr;
62+
WebAssemblyModule * module = WebAssemblyModule::CreateModule(scriptContext, buffer, byteLength, false, bufferSrc);
63+
return WebAssemblyInstance::CreateInstance(module, args[2]);
10464
}
10565

10666
Var WasmLibrary::EntryCompile(RecyclableObject* function, CallInfo callInfo, ...)

lib/Runtime/Library/WebAssemblyInstance.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ WebAssemblyInstance::NewInstance(RecyclableObject* function, CallInfo callInfo,
7979
JavascriptError::ThrowTypeError(scriptContext, JSERR_NeedObject, _u("WebAssembly.Instance"));
8080
}
8181

82+
return CreateInstance(module, importObject);
83+
}
84+
85+
WebAssemblyInstance *
86+
WebAssemblyInstance::CreateInstance(WebAssemblyModule * module, Var importObject)
87+
{
88+
ScriptContext * scriptContext = module->GetScriptContext();
8289
Var* moduleEnvironmentPtr = RecyclerNewArrayZ(scriptContext->GetRecycler(), Var, module->GetModuleEnvironmentSize());
8390
Var* heap = moduleEnvironmentPtr + module->GetHeapOffset();
8491
WebAssemblyInstance * newInstance = RecyclerNewZ(scriptContext->GetRecycler(), WebAssemblyInstance, module, scriptContext->GetLibrary()->GetWebAssemblyInstanceType());
@@ -106,7 +113,7 @@ WebAssemblyInstance::NewInstance(RecyclableObject* function, CallInfo callInfo,
106113
Js::Arguments startArg(info, &start);
107114
Js::JavascriptFunction::CallFunction<true>(f, f->GetEntryPoint(), startArg);
108115
}
109-
116+
110117
return newInstance;
111118
}
112119

@@ -248,14 +255,14 @@ void WebAssemblyInstance::BuildObject(WebAssemblyModule * wasmModule, ScriptCont
248255
static Var GetImportVariable(Wasm::WasmImport* wi, ScriptContext* ctx, Var ffi)
249256
{
250257
PropertyRecord const * modPropertyRecord = nullptr;
251-
char16* modName = wi->modName;
258+
const char16* modName = wi->modName;
252259
uint32 modNameLen = wi->modNameLen;
253260
ctx->GetOrAddPropertyRecord(modName, modNameLen, &modPropertyRecord);
254261
Var modProp = JavascriptOperators::OP_GetProperty(ffi, modPropertyRecord->GetPropertyId(), ctx);
255262

256263

257264

258-
char16* name = wi->fnName;
265+
const char16* name = wi->fnName;
259266
uint32 nameLen = wi->fnNameLen;
260267
Var prop = nullptr;
261268
if (nameLen > 0)

lib/Runtime/Library/WebAssemblyInstance.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace Js
2121
static bool Is(Var aValue);
2222
static WebAssemblyInstance * FromVar(Var aValue);
2323

24+
static WebAssemblyInstance * CreateInstance(WebAssemblyModule * module, Var importObject);
2425
private:
2526
WebAssemblyInstance(WebAssemblyModule * wasmModule, DynamicType * type);
2627

lib/Runtime/Library/WebAssemblyModule.cpp

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Js
1313
{
14-
WebAssemblyModule::WebAssemblyModule(Js::ScriptContext* scriptContext, byte* binaryBuffer, uint binaryBufferLength, DynamicType * type) :
14+
WebAssemblyModule::WebAssemblyModule(Js::ScriptContext* scriptContext, const byte* binaryBuffer, uint binaryBufferLength, DynamicType * type) :
1515
DynamicObject(type),
1616
m_memory(),
1717
m_alloc(_u("WebAssemblyModule"), scriptContext->GetThreadContext()->GetPageAllocator(), Js::Throw::OutOfMemory),
@@ -98,44 +98,31 @@ WebAssemblyModule::NewInstance(RecyclableObject* function, CallInfo callInfo, ..
9898
byteLength = arrayBuffer->GetByteLength();
9999
}
100100

101-
CompileScriptException se;
102-
Js::Utf8SourceInfo* utf8SourceInfo;
103-
104-
return CompileModule(scriptContext, (const char16*)buffer, nullptr, &se, &utf8SourceInfo, byteLength, false, bufferSrc);
101+
return CreateModule(scriptContext, buffer, byteLength, false, bufferSrc);
105102
}
106103

107104
/* static */
108105
WebAssemblyModule *
109-
WebAssemblyModule::CompileModule(
106+
WebAssemblyModule::CreateModule(
110107
ScriptContext* scriptContext,
111-
const char16* script,
112-
SRCINFO const * pSrcInfo,
113-
CompileScriptException * pse,
114-
Utf8SourceInfo** ppSourceInfo,
108+
const byte* buffer,
115109
const uint lengthBytes,
116110
bool validateOnly,
117111
Var bufferSrc)
118112
{
119-
WebAssemblyModule * WebAssemblyModule = nullptr;
120-
if (pSrcInfo == nullptr)
121-
{
122-
pSrcInfo = scriptContext->cache->noContextGlobalSourceInfo;
123-
}
124-
125113
AutoProfilingPhase wasmPhase(scriptContext, Js::WasmPhase);
126114
Unused(wasmPhase);
127115

128-
Assert(pse != nullptr);
116+
WebAssemblyModule * WebAssemblyModule = nullptr;
129117
Wasm::WasmReaderInfo * readerInfo = nullptr;
130118
Js::FunctionBody * currentBody = nullptr;
131119
try
132120
{
133121
Js::AutoDynamicCodeReference dynamicFunctionReference(scriptContext);
134-
*ppSourceInfo = nullptr;
135-
136-
*ppSourceInfo = Utf8SourceInfo::New(scriptContext, (LPCUTF8)script, lengthBytes / sizeof(char16), lengthBytes, pSrcInfo, false);
122+
SRCINFO const * srcInfo = scriptContext->cache->noContextGlobalSourceInfo;
123+
Js::Utf8SourceInfo* utf8SourceInfo = Utf8SourceInfo::New(scriptContext, (LPCUTF8)buffer, lengthBytes / sizeof(char16), lengthBytes, srcInfo, false);
137124

138-
Wasm::WasmModuleGenerator bytecodeGen(scriptContext, *ppSourceInfo, (byte*)script, lengthBytes, bufferSrc);
125+
Wasm::WasmModuleGenerator bytecodeGen(scriptContext, utf8SourceInfo, (byte*)buffer, lengthBytes, bufferSrc);
139126

140127
WebAssemblyModule = bytecodeGen.GenerateModule();
141128

@@ -407,7 +394,7 @@ void WebAssemblyModule::AllocateFunctionExports(uint32 entries)
407394
m_exportCount = entries;
408395
}
409396

410-
void WebAssemblyModule::SetExport(uint32 iExport, uint32 funcIndex, char16* exportName, uint32 nameLength, Wasm::ExternalKinds::ExternalKind kind)
397+
void WebAssemblyModule::SetExport(uint32 iExport, uint32 funcIndex, const char16* exportName, uint32 nameLength, Wasm::ExternalKinds::ExternalKind kind)
411398
{
412399
m_exports[iExport].funcIndex = funcIndex;
413400
m_exports[iExport].nameLength = nameLength;
@@ -432,7 +419,7 @@ WebAssemblyModule::AllocateFunctionImports(uint32 entries)
432419
}
433420

434421
void
435-
WebAssemblyModule::SetFunctionImport(uint32 i, uint32 sigId, char16* modName, uint32 modNameLen, char16* fnName, uint32 fnNameLen, Wasm::ExternalKinds::ExternalKind kind)
422+
WebAssemblyModule::SetFunctionImport(uint32 i, uint32 sigId, const char16* modName, uint32 modNameLen, const char16* fnName, uint32 fnNameLen, Wasm::ExternalKinds::ExternalKind kind)
436423
{
437424
m_imports[i].sigId = sigId;
438425
m_imports[i].modNameLen = modNameLen;
@@ -442,7 +429,7 @@ WebAssemblyModule::SetFunctionImport(uint32 i, uint32 sigId, char16* modName, ui
442429
}
443430

444431
void
445-
WebAssemblyModule::AddGlobalImport(char16* modName, uint32 modNameLen, char16* fnName, uint32 fnNameLen, Wasm::ExternalKinds::ExternalKind kind, Wasm::WasmGlobal* importedGlobal)
432+
WebAssemblyModule::AddGlobalImport(const char16* modName, uint32 modNameLen, const char16* fnName, uint32 fnNameLen, Wasm::ExternalKinds::ExternalKind kind, Wasm::WasmGlobal* importedGlobal)
446433
{
447434
Wasm::WasmImport* wi = Anew(&m_alloc, Wasm::WasmImport);
448435
wi->sigId = 0;

lib/Runtime/Library/WebAssemblyModule.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,9 @@ class WebAssemblyModule : public DynamicObject
4949
static bool Is(Var aValue);
5050
static WebAssemblyModule * FromVar(Var aValue);
5151

52-
static WebAssemblyModule * CompileModule(
52+
static WebAssemblyModule * CreateModule(
5353
ScriptContext* scriptContext,
54-
const char16* script,
55-
SRCINFO const * pSrcInfo,
56-
CompileScriptException * pse,
57-
Utf8SourceInfo** ppSourceInfo,
54+
const byte* buffer,
5855
const uint lengthBytes,
5956
bool validateOnly = false,
6057
Var bufferSrc = nullptr);
@@ -72,7 +69,7 @@ class WebAssemblyModule : public DynamicObject
7269
} m_memory;
7370

7471
public:
75-
WebAssemblyModule(Js::ScriptContext* scriptContext, byte* binaryBuffer, uint binaryBufferLength, DynamicType * type);
72+
WebAssemblyModule(Js::ScriptContext* scriptContext, const byte* binaryBuffer, uint binaryBufferLength, DynamicType * type);
7673

7774
// The index used by those methods is the function index as describe by the WebAssembly design, ie: imports first then wasm functions
7875
uint32 GetMaxFunctionIndex() const;
@@ -105,15 +102,15 @@ class WebAssemblyModule : public DynamicObject
105102

106103
void AllocateFunctionExports(uint32 entries);
107104
uint GetExportCount() const { return m_exportCount; }
108-
void SetExport(uint32 iExport, uint32 funcIndex, char16* exportName, uint32 nameLength, Wasm::ExternalKinds::ExternalKind kind);
105+
void SetExport(uint32 iExport, uint32 funcIndex, const char16* exportName, uint32 nameLength, Wasm::ExternalKinds::ExternalKind kind);
109106
Wasm::WasmExport* GetFunctionExport(uint32 iExport) const;
110107

111108
void AllocateFunctionImports(uint32 entries);
112109
uint32 GetImportCount() const { return m_importCount; }
113110
void SetImportCount(uint count) { m_importCount = count; }
114-
void SetFunctionImport(uint32 i, uint32 sigId, char16* modName, uint32 modNameLen, char16* fnName, uint32 fnNameLen, Wasm::ExternalKinds::ExternalKind kind);
111+
void SetFunctionImport(uint32 i, uint32 sigId, const char16* modName, uint32 modNameLen, const char16* fnName, uint32 fnNameLen, Wasm::ExternalKinds::ExternalKind kind);
115112
Wasm::WasmImport* GetFunctionImport(uint32 i) const;
116-
void AddGlobalImport(char16* modName, uint32 modNameLen, char16* fnName, uint32 fnNameLen, Wasm::ExternalKinds::ExternalKind kind, Wasm::WasmGlobal* importedGlobal);
113+
void AddGlobalImport(const char16* modName, uint32 modNameLen, const char16* fnName, uint32 fnNameLen, Wasm::ExternalKinds::ExternalKind kind, Wasm::WasmGlobal* importedGlobal);
117114

118115
void AllocateDataSegs(uint32 count);
119116
bool AddDataSeg(Wasm::WasmDataSegment* seg, uint32 index);

lib/WasmReader/WasmBinaryReader.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ LanguageTypes::ToWasmType(int8 binType)
5151
}
5252
}
5353

54-
WasmBinaryReader::WasmBinaryReader(ArenaAllocator* alloc, Js::WebAssemblyModule * module, byte* source, size_t length) :
54+
WasmBinaryReader::WasmBinaryReader(ArenaAllocator* alloc, Js::WebAssemblyModule * module, const byte* source, size_t length) :
5555
m_module(module),
5656
m_curFuncEnd(nullptr),
5757
m_alloc(alloc)
@@ -70,7 +70,7 @@ void WasmBinaryReader::InitializeReader()
7070
#if DBG_DUMP
7171
if (DO_WASM_TRACE_SECTION)
7272
{
73-
byte* startModule = m_pc;
73+
const byte* startModule = m_pc;
7474

7575
bool doRead = true;
7676
SectionCode prevSect = bSectInvalid;
@@ -295,7 +295,7 @@ WasmBinaryReader::ReadFunctionHeaders()
295295
funcInfo->m_readerInfo.startOffset = (m_pc - m_start);
296296
CheckBytesLeft(funcSize);
297297
TRACE_WASM_DECODER(_u("Function body header: index = %u, size = %u"), i, funcSize);
298-
byte* end = m_pc + funcSize;
298+
const byte* end = m_pc + funcSize;
299299
m_pc = end;
300300
}
301301
return m_pc == m_currentSection.end;
@@ -675,7 +675,7 @@ void WasmBinaryReader::ReadExportTable()
675675
for (uint32 iExport = 0; iExport < entries; iExport++)
676676
{
677677
uint32 nameLength;
678-
char16* exportName = ReadInlineName(length, nameLength);
678+
const char16* exportName = ReadInlineName(length, nameLength);
679679

680680
ExternalKinds::ExternalKind kind = (ExternalKinds::ExternalKind)ReadConst<int8>();
681681
uint32 index = LEB128(length);
@@ -891,19 +891,19 @@ WasmBinaryReader::ReadGlobalsSection()
891891
}
892892
}
893893

894-
char16* WasmBinaryReader::ReadInlineName(uint32& length, uint32& nameLength)
894+
const char16* WasmBinaryReader::ReadInlineName(uint32& length, uint32& nameLength)
895895
{
896896
nameLength = LEB128(length);
897897
CheckBytesLeft(nameLength);
898-
LPUTF8 rawName = m_pc;
898+
LPCUTF8 rawName = m_pc;
899899

900900
m_pc += nameLength;
901901
length += nameLength;
902902

903903
return CvtUtf8Str(rawName, nameLength);
904904
}
905905

906-
char16* WasmBinaryReader::CvtUtf8Str(LPUTF8 name, uint32 nameLen)
906+
const char16* WasmBinaryReader::CvtUtf8Str(LPCUTF8 name, uint32 nameLen)
907907
{
908908
utf8::DecodeOptions decodeOptions = utf8::doDefault;
909909
charcount_t utf16Len = utf8::ByteIndexIntoCharacterIndex(name, nameLen, decodeOptions);
@@ -912,7 +912,7 @@ char16* WasmBinaryReader::CvtUtf8Str(LPUTF8 name, uint32 nameLen)
912912
{
913913
Js::Throw::OutOfMemory();
914914
}
915-
utf8::DecodeIntoAndNullTerminate((char16*)contents, name, utf16Len, decodeOptions);
915+
utf8::DecodeIntoAndNullTerminate(contents, name, utf16Len, decodeOptions);
916916
return contents;
917917
}
918918

@@ -931,8 +931,8 @@ WasmBinaryReader::ReadImportEntries()
931931
for (uint32 i = 0; i < entries; ++i)
932932
{
933933
uint32 modNameLen = 0, fnNameLen = 0;
934-
char16* modName = ReadInlineName(len, modNameLen);
935-
char16* fnName = ReadInlineName(len, fnNameLen);
934+
const char16* modName = ReadInlineName(len, modNameLen);
935+
const char16* fnName = ReadInlineName(len, fnNameLen);
936936

937937
ExternalKinds::ExternalKind kind = (ExternalKinds::ExternalKind)ReadConst<int8>();
938938
TRACE_WASM_DECODER(_u("Import #%u: \"%s\".\"%s\", kind: %d"), i, modName, fnName, kind);

lib/WasmReader/WasmBinaryReader.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ namespace Wasm
2323
struct SectionHeader
2424
{
2525
SectionCode code;
26-
byte* start;
27-
byte* end;
26+
const byte* start;
27+
const byte* end;
2828
};
2929

3030
static const unsigned int experimentalVersion = 0xd;
3131

3232
class WasmBinaryReader
3333
{
3434
public:
35-
WasmBinaryReader(ArenaAllocator* alloc, Js::WebAssemblyModule * module, byte* source, size_t length);
35+
WasmBinaryReader(ArenaAllocator* alloc, Js::WebAssemblyModule * module, const byte* source, size_t length);
3636

3737
void InitializeReader();
3838
bool ReadNextSection(SectionCode nextSection);
@@ -80,8 +80,8 @@ namespace Wasm
8080
// Primitive reader
8181
template <WasmTypes::WasmType type> void ConstNode();
8282
template <typename T> T ReadConst();
83-
char16* ReadInlineName(uint32& length, uint32& nameLength);
84-
char16* CvtUtf8Str(LPUTF8 name, uint32 nameLen);
83+
const char16* ReadInlineName(uint32& length, uint32& nameLength);
84+
const char16* CvtUtf8Str(LPCUTF8 name, uint32 nameLen);
8585
template<typename MaxAllowedType = UINT>
8686
MaxAllowedType LEB128(UINT &length, bool sgn = false);
8787
template<typename MaxAllowedType = INT>
@@ -96,7 +96,7 @@ namespace Wasm
9696

9797
ArenaAllocator* m_alloc;
9898
uint m_funcNumber;
99-
byte* m_start, *m_end, *m_pc, *m_curFuncEnd;
99+
const byte* m_start, *m_end, *m_pc, *m_curFuncEnd;
100100
SectionHeader m_currentSection;
101101
ReaderState m_funcState; // func AST level
102102

0 commit comments

Comments
 (0)