Skip to content

Commit 1d521f5

Browse files
committed
[1.8>1.9] [MERGE #4582 @obastemur] More module tests and implement RegisterModuleSource
Merge pull request #4582 from obastemur:module_register__ - Implement RegisterModuleSource - es6-modules: add more test cases and move it to separate folder Fixes #4577
2 parents 50e5667 + c3c7923 commit 1d521f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+4398
-151
lines changed

bin/ch/WScriptJsrt.cpp

Lines changed: 97 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,19 @@ JsValueRef WScriptJsrt::LoadScriptFileHelper(JsValueRef callee, JsValueRef *argu
201201
hr = Helpers::LoadScriptFromFile(*fileName, fileContent);
202202
if (FAILED(hr))
203203
{
204-
fwprintf(stderr, _u("Couldn't load file.\n"));
205-
}
206-
else
207-
{
208-
returnValue = LoadScript(callee, *fileName, fileContent, *scriptInjectType ? *scriptInjectType : "self", isSourceModule, WScriptJsrt::FinalizeFree, true);
204+
// check if have it registered
205+
AutoString *data;
206+
if (!SourceMap::Find(fileName, &data))
207+
{
208+
fprintf(stderr, "Couldn't load file '%s'\n", fileName.GetString());
209+
IfJsrtErrorSetGo(ChakraRTInterface::JsGetUndefinedValue(&returnValue));
210+
return returnValue;
211+
}
212+
213+
fileContent = data->GetString();
209214
}
215+
216+
returnValue = LoadScript(callee, *fileName, fileContent, *scriptInjectType ? *scriptInjectType : "self", isSourceModule, WScriptJsrt::FinalizeFree, true);
210217
}
211218
}
212219

@@ -395,15 +402,15 @@ JsErrorCode WScriptJsrt::LoadModuleFromString(LPCSTR fileName, LPCSTR fileConten
395402

396403
// ParseModuleSource is sync, while additional fetch & evaluation are async.
397404
unsigned int fileContentLength = (fileContent == nullptr) ? 0 : (unsigned int)strlen(fileContent);
398-
405+
399406
if (isFile && fullName)
400407
{
401408
JsValueRef moduleUrl;
402409
ChakraRTInterface::JsCreateString(fullName, strlen(fullName), &moduleUrl);
403410
errorCode = ChakraRTInterface::JsSetModuleHostInfo(requestModule, JsModuleHostInfo_Url, moduleUrl);
404411
IfJsrtErrorFail(errorCode, errorCode);
405412
}
406-
413+
407414
errorCode = ChakraRTInterface::JsParseModuleSource(requestModule, dwSourceCookie, (LPBYTE)fileContent,
408415
fileContentLength, JsParseModuleSourceFlags_DataIsUTF8, &errorObject);
409416
if ((errorCode != JsNoError) && errorObject != JS_INVALID_REFERENCE && fileContent != nullptr && !HostConfigFlags::flags.IgnoreScriptErrorCode)
@@ -857,6 +864,7 @@ bool WScriptJsrt::Initialize()
857864
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "LoadBinaryFile", LoadBinaryFileCallback));
858865
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "LoadTextFile", LoadTextFileCallback));
859866
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "Flag", FlagCallback));
867+
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "RegisterModuleSource", RegisterModuleSourceCallback));
860868

861869
// ToDo Remove
862870
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "Edit", EmptyCallback));
@@ -1045,6 +1053,32 @@ void CALLBACK WScriptJsrt::JsContextBeforeCollectCallback(JsRef contextRef, void
10451053
}
10461054
#endif
10471055

1056+
FileNode * SourceMap::root = nullptr;
1057+
JsValueRef __stdcall WScriptJsrt::RegisterModuleSourceCallback(JsValueRef callee, bool isConstructCall,
1058+
JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
1059+
{
1060+
HRESULT hr = E_FAIL;
1061+
JsValueRef returnValue = JS_INVALID_REFERENCE;
1062+
JsErrorCode errorCode = JsNoError;
1063+
1064+
if (argumentCount < 3)
1065+
{
1066+
IfJsrtErrorSetGo(ChakraRTInterface::JsGetUndefinedValue(&returnValue));
1067+
}
1068+
else
1069+
{
1070+
AutoString fileName;
1071+
AutoString data;
1072+
IfJsrtErrorSetGo(fileName.Initialize(arguments[1]));
1073+
IfJsrtErrorSetGo(data.Initialize(arguments[2]));
1074+
1075+
SourceMap::Add(fileName, data);
1076+
}
1077+
1078+
Error:
1079+
return returnValue;
1080+
}
1081+
10481082
JsValueRef __stdcall WScriptJsrt::LoadTextFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
10491083
{
10501084
HRESULT hr = E_FAIL;
@@ -1069,14 +1103,21 @@ JsValueRef __stdcall WScriptJsrt::LoadTextFileCallback(JsValueRef callee, bool i
10691103

10701104
if (FAILED(hr))
10711105
{
1072-
fwprintf(stderr, _u("Couldn't load file.\n"));
1073-
IfJsrtErrorSetGo(ChakraRTInterface::JsGetUndefinedValue(&returnValue));
1074-
}
1075-
else
1076-
{
1077-
IfJsrtErrorSetGo(ChakraRTInterface::JsCreateString(
1078-
fileContent, lengthBytes, &returnValue));
1106+
// check if have it registered
1107+
AutoString *data;
1108+
if (!SourceMap::Find(fileName, &data))
1109+
{
1110+
fprintf(stderr, "Couldn't load file '%s'\n", fileName.GetString());
1111+
IfJsrtErrorSetGo(ChakraRTInterface::JsGetUndefinedValue(&returnValue));
1112+
return returnValue;
1113+
}
1114+
1115+
fileContent = data->GetString();
1116+
lengthBytes = (UINT) data->GetLength();
10791117
}
1118+
1119+
IfJsrtErrorSetGo(ChakraRTInterface::JsCreateString(
1120+
fileContent, lengthBytes, &returnValue));
10801121
}
10811122
}
10821123

@@ -1094,6 +1135,7 @@ JsValueRef __stdcall WScriptJsrt::LoadBinaryFileCallback(JsValueRef callee,
10941135
HRESULT hr = E_FAIL;
10951136
JsValueRef returnValue = JS_INVALID_REFERENCE;
10961137
JsErrorCode errorCode = JsNoError;
1138+
bool isHeapAlloc = true;
10971139

10981140
if (argumentCount < 2)
10991141
{
@@ -1111,29 +1153,42 @@ JsValueRef __stdcall WScriptJsrt::LoadBinaryFileCallback(JsValueRef callee,
11111153
UINT lengthBytes = 0;
11121154

11131155
hr = Helpers::LoadBinaryFile(*fileName, fileContent, lengthBytes);
1156+
11141157
if (FAILED(hr))
11151158
{
1116-
fwprintf(stderr, _u("Couldn't load file.\n"));
1159+
// check if have it registered
1160+
AutoString *data;
1161+
if (!SourceMap::Find(fileName, &data))
1162+
{
1163+
fprintf(stderr, "Couldn't load file '%s'\n", fileName.GetString());
1164+
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsGetUndefinedValue(&returnValue), Error);
1165+
return returnValue;
1166+
}
1167+
1168+
isHeapAlloc = false;
1169+
fileContent = data->GetString();
1170+
lengthBytes = (UINT) data->GetLength();
1171+
}
1172+
1173+
JsValueRef arrayBuffer;
1174+
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsCreateArrayBuffer(lengthBytes, &arrayBuffer), ErrorStillFree);
1175+
BYTE* buffer;
1176+
unsigned int bufferLength;
1177+
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsGetArrayBufferStorage(arrayBuffer, &buffer, &bufferLength), ErrorStillFree);
1178+
if (bufferLength < lengthBytes)
1179+
{
1180+
fwprintf(stderr, _u("Array buffer size is insufficient to store the binary file.\n"));
11171181
}
11181182
else
11191183
{
1120-
JsValueRef arrayBuffer;
1121-
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsCreateArrayBuffer(lengthBytes, &arrayBuffer), ErrorStillFree);
1122-
BYTE* buffer;
1123-
unsigned int bufferLength;
1124-
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsGetArrayBufferStorage(arrayBuffer, &buffer, &bufferLength), ErrorStillFree);
1125-
if (bufferLength < lengthBytes)
1126-
{
1127-
fwprintf(stderr, _u("Array buffer size is insufficient to store the binary file.\n"));
1128-
}
1129-
else
1184+
if (memcpy_s(buffer, bufferLength, (BYTE*)fileContent, lengthBytes) == 0)
11301185
{
1131-
if (memcpy_s(buffer, bufferLength, (BYTE*)fileContent, lengthBytes) == 0)
1132-
{
1133-
returnValue = arrayBuffer;
1134-
}
1186+
returnValue = arrayBuffer;
11351187
}
1188+
}
11361189
ErrorStillFree:
1190+
if (isHeapAlloc)
1191+
{
11371192
HeapFree(GetProcessHeap(), 0, (void*)fileContent);
11381193
}
11391194
}
@@ -1365,7 +1420,7 @@ bool WScriptJsrt::PrintException(LPCSTR fileName, JsErrorCode jsErrorCode)
13651420

13661421
int line;
13671422
int column;
1368-
1423+
13691424
IfJsrtErrorFail(CreatePropertyIdFromString("line", &linePropertyId), false);
13701425
IfJsrtErrorFail(ChakraRTInterface::JsGetProperty(exception, linePropertyId, &lineProperty), false);
13711426
IfJsrtErrorFail(ChakraRTInterface::JsNumberToInt(lineProperty, &line), false);
@@ -1551,19 +1606,23 @@ HRESULT WScriptJsrt::ModuleMessage::Call(LPCSTR fileName)
15511606

15521607
if (FAILED(hr))
15531608
{
1554-
if (!HostConfigFlags::flags.MuteHostErrorMsgIsEnabled)
1609+
// check if have it registered
1610+
AutoString *data;
1611+
if (!SourceMap::Find(specifierStr, &data))
15551612
{
1556-
fprintf(stderr, "Couldn't load file.\n");
1613+
if (!HostConfigFlags::flags.MuteHostErrorMsgIsEnabled)
1614+
{
1615+
fprintf(stderr, "Couldn't load file '%s'\n", specifierStr.GetString());
1616+
}
1617+
LoadScript(nullptr, *specifierStr, nullptr, "module", true, WScriptJsrt::FinalizeFree, false);
1618+
goto Error;
15571619
}
1558-
1559-
LoadScript(nullptr, *specifierStr, nullptr, "module", true, WScriptJsrt::FinalizeFree, false);
1560-
}
1561-
else
1562-
{
1563-
LoadScript(nullptr, *specifierStr, fileContent, "module", true, WScriptJsrt::FinalizeFree, true);
1620+
fileContent = data->GetString();
15641621
}
1622+
LoadScript(nullptr, *specifierStr, fileContent, "module", true, WScriptJsrt::FinalizeFree, true);
15651623
}
15661624
}
1625+
Error:
15671626
return errorCode;
15681627
}
15691628

bin/ch/WScriptJsrt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ class WScriptJsrt
119119

120120
static JsValueRef CALLBACK LoadBinaryFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
121121
static JsValueRef CALLBACK LoadTextFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
122+
static JsValueRef CALLBACK RegisterModuleSourceCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
122123
static JsValueRef CALLBACK FlagCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
123124

124125
static JsValueRef CALLBACK BroadcastCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);

bin/ch/stdafx.h

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ class AutoString
181181
data_wide(nullptr), errorCode(JsNoError), dontFree(false)
182182
{ }
183183

184+
AutoString(AutoString &autoString):length(autoString.length),
185+
data(autoString.data), data_wide(autoString.data_wide),
186+
errorCode(JsNoError), dontFree(false)
187+
{
188+
autoString.dontFree = true; // take over the ownership
189+
}
190+
184191
AutoString(JsValueRef value):length(0), data(nullptr),
185192
data_wide(nullptr), errorCode(JsNoError), dontFree(false)
186193
{
@@ -279,7 +286,49 @@ class AutoString
279286
}
280287

281288
char* operator*() { return data; }
282-
char** operator&() { return &data; }
289+
};
290+
291+
struct FileNode
292+
{
293+
AutoString data;
294+
AutoString path;
295+
FileNode * next;
296+
FileNode(AutoString &path_, AutoString &data_):
297+
path(path_), data(data_), next(nullptr) {
298+
path_.MakePersistent();
299+
data_.MakePersistent();
300+
}
301+
};
302+
303+
class SourceMap
304+
{
305+
static FileNode * root;
306+
public:
307+
static void Add(AutoString &path, AutoString &data)
308+
{
309+
// SourceMap lifetime == process lifetime
310+
FileNode * node = new FileNode(path, data);
311+
if (root != nullptr)
312+
{
313+
node->next = root;
314+
}
315+
root = node;
316+
}
317+
318+
static bool Find(AutoString &path, AutoString ** out)
319+
{
320+
FileNode * node = root;
321+
while(node != nullptr)
322+
{
323+
if (strncmp(node->path.GetString(), path.GetString(), path.GetLength()) == 0)
324+
{
325+
*out = &(node->data);
326+
return true;
327+
}
328+
node = node->next;
329+
}
330+
return false;
331+
}
283332
};
284333

285334
inline JsErrorCode CreatePropertyIdFromString(const char* str, JsPropertyIdRef *Id)

0 commit comments

Comments
 (0)