@@ -201,12 +201,19 @@ JsValueRef WScriptJsrt::LoadScriptFileHelper(JsValueRef callee, JsValueRef *argu
201
201
hr = Helpers::LoadScriptFromFile (*fileName, fileContent);
202
202
if (FAILED (hr))
203
203
{
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 ();
209
214
}
215
+
216
+ returnValue = LoadScript (callee, *fileName, fileContent, *scriptInjectType ? *scriptInjectType : " self" , isSourceModule, WScriptJsrt::FinalizeFree, true );
210
217
}
211
218
}
212
219
@@ -395,15 +402,15 @@ JsErrorCode WScriptJsrt::LoadModuleFromString(LPCSTR fileName, LPCSTR fileConten
395
402
396
403
// ParseModuleSource is sync, while additional fetch & evaluation are async.
397
404
unsigned int fileContentLength = (fileContent == nullptr ) ? 0 : (unsigned int )strlen (fileContent);
398
-
405
+
399
406
if (isFile && fullName)
400
407
{
401
408
JsValueRef moduleUrl;
402
409
ChakraRTInterface::JsCreateString (fullName, strlen (fullName), &moduleUrl);
403
410
errorCode = ChakraRTInterface::JsSetModuleHostInfo (requestModule, JsModuleHostInfo_Url, moduleUrl);
404
411
IfJsrtErrorFail (errorCode, errorCode);
405
412
}
406
-
413
+
407
414
errorCode = ChakraRTInterface::JsParseModuleSource (requestModule, dwSourceCookie, (LPBYTE)fileContent,
408
415
fileContentLength, JsParseModuleSourceFlags_DataIsUTF8, &errorObject);
409
416
if ((errorCode != JsNoError) && errorObject != JS_INVALID_REFERENCE && fileContent != nullptr && !HostConfigFlags::flags.IgnoreScriptErrorCode )
@@ -857,6 +864,7 @@ bool WScriptJsrt::Initialize()
857
864
IfFalseGo (WScriptJsrt::InstallObjectsOnObject (wscript, " LoadBinaryFile" , LoadBinaryFileCallback));
858
865
IfFalseGo (WScriptJsrt::InstallObjectsOnObject (wscript, " LoadTextFile" , LoadTextFileCallback));
859
866
IfFalseGo (WScriptJsrt::InstallObjectsOnObject (wscript, " Flag" , FlagCallback));
867
+ IfFalseGo (WScriptJsrt::InstallObjectsOnObject (wscript, " RegisterModuleSource" , RegisterModuleSourceCallback));
860
868
861
869
// ToDo Remove
862
870
IfFalseGo (WScriptJsrt::InstallObjectsOnObject (wscript, " Edit" , EmptyCallback));
@@ -1045,6 +1053,32 @@ void CALLBACK WScriptJsrt::JsContextBeforeCollectCallback(JsRef contextRef, void
1045
1053
}
1046
1054
#endif
1047
1055
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
+
1048
1082
JsValueRef __stdcall WScriptJsrt::LoadTextFileCallback (JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
1049
1083
{
1050
1084
HRESULT hr = E_FAIL;
@@ -1069,14 +1103,21 @@ JsValueRef __stdcall WScriptJsrt::LoadTextFileCallback(JsValueRef callee, bool i
1069
1103
1070
1104
if (FAILED (hr))
1071
1105
{
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 ();
1079
1117
}
1118
+
1119
+ IfJsrtErrorSetGo (ChakraRTInterface::JsCreateString (
1120
+ fileContent, lengthBytes, &returnValue));
1080
1121
}
1081
1122
}
1082
1123
@@ -1094,6 +1135,7 @@ JsValueRef __stdcall WScriptJsrt::LoadBinaryFileCallback(JsValueRef callee,
1094
1135
HRESULT hr = E_FAIL;
1095
1136
JsValueRef returnValue = JS_INVALID_REFERENCE;
1096
1137
JsErrorCode errorCode = JsNoError;
1138
+ bool isHeapAlloc = true ;
1097
1139
1098
1140
if (argumentCount < 2 )
1099
1141
{
@@ -1111,29 +1153,42 @@ JsValueRef __stdcall WScriptJsrt::LoadBinaryFileCallback(JsValueRef callee,
1111
1153
UINT lengthBytes = 0 ;
1112
1154
1113
1155
hr = Helpers::LoadBinaryFile (*fileName, fileContent, lengthBytes);
1156
+
1114
1157
if (FAILED (hr))
1115
1158
{
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 " ));
1117
1181
}
1118
1182
else
1119
1183
{
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 )
1130
1185
{
1131
- if (memcpy_s (buffer, bufferLength, (BYTE*)fileContent, lengthBytes) == 0 )
1132
- {
1133
- returnValue = arrayBuffer;
1134
- }
1186
+ returnValue = arrayBuffer;
1135
1187
}
1188
+ }
1136
1189
ErrorStillFree:
1190
+ if (isHeapAlloc)
1191
+ {
1137
1192
HeapFree (GetProcessHeap (), 0 , (void *)fileContent);
1138
1193
}
1139
1194
}
@@ -1365,7 +1420,7 @@ bool WScriptJsrt::PrintException(LPCSTR fileName, JsErrorCode jsErrorCode)
1365
1420
1366
1421
int line;
1367
1422
int column;
1368
-
1423
+
1369
1424
IfJsrtErrorFail (CreatePropertyIdFromString (" line" , &linePropertyId), false );
1370
1425
IfJsrtErrorFail (ChakraRTInterface::JsGetProperty (exception, linePropertyId, &lineProperty), false );
1371
1426
IfJsrtErrorFail (ChakraRTInterface::JsNumberToInt (lineProperty, &line), false );
@@ -1551,19 +1606,23 @@ HRESULT WScriptJsrt::ModuleMessage::Call(LPCSTR fileName)
1551
1606
1552
1607
if (FAILED (hr))
1553
1608
{
1554
- if (!HostConfigFlags::flags.MuteHostErrorMsgIsEnabled )
1609
+ // check if have it registered
1610
+ AutoString *data;
1611
+ if (!SourceMap::Find (specifierStr, &data))
1555
1612
{
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;
1557
1619
}
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 ();
1564
1621
}
1622
+ LoadScript (nullptr , *specifierStr, fileContent, " module" , true , WScriptJsrt::FinalizeFree, true );
1565
1623
}
1566
1624
}
1625
+ Error:
1567
1626
return errorCode;
1568
1627
}
1569
1628
0 commit comments