@@ -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+
10481082JsValueRef __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+ }
11361189ErrorStillFree:
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
0 commit comments