Skip to content

Commit

Permalink
[1.9>master] [1.8>1.9] [MERGE #4582 @obastemur] More module tests and…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
obastemur committed Jan 22, 2018
2 parents 8ce2142 + 1d521f5 commit fa60541
Show file tree
Hide file tree
Showing 69 changed files with 4,395 additions and 148 deletions.
129 changes: 94 additions & 35 deletions bin/ch/WScriptJsrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,19 @@ JsValueRef WScriptJsrt::LoadScriptFileHelper(JsValueRef callee, JsValueRef *argu
hr = Helpers::LoadScriptFromFile(*fileName, fileContent);
if (FAILED(hr))
{
fwprintf(stderr, _u("Couldn't load file.\n"));
}
else
{
returnValue = LoadScript(callee, *fileName, fileContent, *scriptInjectType ? *scriptInjectType : "self", isSourceModule, WScriptJsrt::FinalizeFree, true);
// check if have it registered
AutoString *data;
if (!SourceMap::Find(fileName, &data))
{
fprintf(stderr, "Couldn't load file '%s'\n", fileName.GetString());
IfJsrtErrorSetGo(ChakraRTInterface::JsGetUndefinedValue(&returnValue));
return returnValue;
}

fileContent = data->GetString();
}

returnValue = LoadScript(callee, *fileName, fileContent, *scriptInjectType ? *scriptInjectType : "self", isSourceModule, WScriptJsrt::FinalizeFree, true);
}
}

Expand Down Expand Up @@ -857,6 +864,7 @@ bool WScriptJsrt::Initialize()
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "LoadBinaryFile", LoadBinaryFileCallback));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "LoadTextFile", LoadTextFileCallback));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "Flag", FlagCallback));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "RegisterModuleSource", RegisterModuleSourceCallback));

// ToDo Remove
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(wscript, "Edit", EmptyCallback));
Expand Down Expand Up @@ -1046,6 +1054,32 @@ void CALLBACK WScriptJsrt::JsContextBeforeCollectCallback(JsRef contextRef, void
}
#endif

FileNode * SourceMap::root = nullptr;
JsValueRef __stdcall WScriptJsrt::RegisterModuleSourceCallback(JsValueRef callee, bool isConstructCall,
JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
HRESULT hr = E_FAIL;
JsValueRef returnValue = JS_INVALID_REFERENCE;
JsErrorCode errorCode = JsNoError;

if (argumentCount < 3)
{
IfJsrtErrorSetGo(ChakraRTInterface::JsGetUndefinedValue(&returnValue));
}
else
{
AutoString fileName;
AutoString data;
IfJsrtErrorSetGo(fileName.Initialize(arguments[1]));
IfJsrtErrorSetGo(data.Initialize(arguments[2]));

SourceMap::Add(fileName, data);
}

Error:
return returnValue;
}

JsValueRef __stdcall WScriptJsrt::LoadTextFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
HRESULT hr = E_FAIL;
Expand All @@ -1070,14 +1104,21 @@ JsValueRef __stdcall WScriptJsrt::LoadTextFileCallback(JsValueRef callee, bool i

if (FAILED(hr))
{
fwprintf(stderr, _u("Couldn't load file.\n"));
IfJsrtErrorSetGo(ChakraRTInterface::JsGetUndefinedValue(&returnValue));
}
else
{
IfJsrtErrorSetGo(ChakraRTInterface::JsCreateString(
fileContent, lengthBytes, &returnValue));
// check if have it registered
AutoString *data;
if (!SourceMap::Find(fileName, &data))
{
fprintf(stderr, "Couldn't load file '%s'\n", fileName.GetString());
IfJsrtErrorSetGo(ChakraRTInterface::JsGetUndefinedValue(&returnValue));
return returnValue;
}

fileContent = data->GetString();
lengthBytes = (UINT) data->GetLength();
}

IfJsrtErrorSetGo(ChakraRTInterface::JsCreateString(
fileContent, lengthBytes, &returnValue));
}
}

Expand Down Expand Up @@ -1213,6 +1254,7 @@ JsValueRef __stdcall WScriptJsrt::LoadBinaryFileCallback(JsValueRef callee,
HRESULT hr = E_FAIL;
JsValueRef returnValue = JS_INVALID_REFERENCE;
JsErrorCode errorCode = JsNoError;
bool isHeapAlloc = true;

if (argumentCount < 2)
{
Expand All @@ -1230,29 +1272,42 @@ JsValueRef __stdcall WScriptJsrt::LoadBinaryFileCallback(JsValueRef callee,
UINT lengthBytes = 0;

hr = Helpers::LoadBinaryFile(*fileName, fileContent, lengthBytes);

if (FAILED(hr))
{
fwprintf(stderr, _u("Couldn't load file.\n"));
// check if have it registered
AutoString *data;
if (!SourceMap::Find(fileName, &data))
{
fprintf(stderr, "Couldn't load file '%s'\n", fileName.GetString());
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsGetUndefinedValue(&returnValue), Error);
return returnValue;
}

isHeapAlloc = false;
fileContent = data->GetString();
lengthBytes = (UINT) data->GetLength();
}

JsValueRef arrayBuffer;
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsCreateArrayBuffer(lengthBytes, &arrayBuffer), ErrorStillFree);
BYTE* buffer;
unsigned int bufferLength;
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsGetArrayBufferStorage(arrayBuffer, &buffer, &bufferLength), ErrorStillFree);
if (bufferLength < lengthBytes)
{
fwprintf(stderr, _u("Array buffer size is insufficient to store the binary file.\n"));
}
else
{
JsValueRef arrayBuffer;
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsCreateArrayBuffer(lengthBytes, &arrayBuffer), ErrorStillFree);
BYTE* buffer;
unsigned int bufferLength;
IfJsrtErrorSetGoLabel(ChakraRTInterface::JsGetArrayBufferStorage(arrayBuffer, &buffer, &bufferLength), ErrorStillFree);
if (bufferLength < lengthBytes)
if (memcpy_s(buffer, bufferLength, (BYTE*)fileContent, lengthBytes) == 0)
{
fwprintf(stderr, _u("Array buffer size is insufficient to store the binary file.\n"));
}
else
{
if (memcpy_s(buffer, bufferLength, (BYTE*)fileContent, lengthBytes) == 0)
{
returnValue = arrayBuffer;
}
returnValue = arrayBuffer;
}
}
ErrorStillFree:
if (isHeapAlloc)
{
HeapFree(GetProcessHeap(), 0, (void*)fileContent);
}
}
Expand Down Expand Up @@ -1670,19 +1725,23 @@ HRESULT WScriptJsrt::ModuleMessage::Call(LPCSTR fileName)

if (FAILED(hr))
{
if (!HostConfigFlags::flags.MuteHostErrorMsgIsEnabled)
// check if have it registered
AutoString *data;
if (!SourceMap::Find(specifierStr, &data))
{
fprintf(stderr, "Couldn't load file.\n");
if (!HostConfigFlags::flags.MuteHostErrorMsgIsEnabled)
{
fprintf(stderr, "Couldn't load file '%s'\n", specifierStr.GetString());
}
LoadScript(nullptr, *specifierStr, nullptr, "module", true, WScriptJsrt::FinalizeFree, false);
goto Error;
}

LoadScript(nullptr, *specifierStr, nullptr, "module", true, WScriptJsrt::FinalizeFree, false);
}
else
{
LoadScript(nullptr, *specifierStr, fileContent, "module", true, WScriptJsrt::FinalizeFree, true);
fileContent = data->GetString();
}
LoadScript(nullptr, *specifierStr, fileContent, "module", true, WScriptJsrt::FinalizeFree, true);
}
}
Error:
return errorCode;
}

Expand Down
1 change: 1 addition & 0 deletions bin/ch/WScriptJsrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class WScriptJsrt

static JsValueRef CALLBACK LoadBinaryFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
static JsValueRef CALLBACK LoadTextFileCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
static JsValueRef CALLBACK RegisterModuleSourceCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
static JsValueRef CALLBACK FlagCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);
static JsValueRef CALLBACK ReadLineStdinCallback(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState);

Expand Down
51 changes: 50 additions & 1 deletion bin/ch/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ class AutoString
data_wide(nullptr), errorCode(JsNoError), dontFree(false)
{ }

AutoString(AutoString &autoString):length(autoString.length),
data(autoString.data), data_wide(autoString.data_wide),
errorCode(JsNoError), dontFree(false)
{
autoString.dontFree = true; // take over the ownership
}

AutoString(JsValueRef value):length(0), data(nullptr),
data_wide(nullptr), errorCode(JsNoError), dontFree(false)
{
Expand Down Expand Up @@ -279,7 +286,49 @@ class AutoString
}

char* operator*() { return data; }
char** operator&() { return &data; }
};

struct FileNode
{
AutoString data;
AutoString path;
FileNode * next;
FileNode(AutoString &path_, AutoString &data_):
path(path_), data(data_), next(nullptr) {
path_.MakePersistent();
data_.MakePersistent();
}
};

class SourceMap
{
static FileNode * root;
public:
static void Add(AutoString &path, AutoString &data)
{
// SourceMap lifetime == process lifetime
FileNode * node = new FileNode(path, data);
if (root != nullptr)
{
node->next = root;
}
root = node;
}

static bool Find(AutoString &path, AutoString ** out)
{
FileNode * node = root;
while(node != nullptr)
{
if (strncmp(node->path.GetString(), path.GetString(), path.GetLength()) == 0)
{
*out = &(node->data);
return true;
}
node = node->next;
}
return false;
}
};

inline JsErrorCode CreatePropertyIdFromString(const char* str, JsPropertyIdRef *Id)
Expand Down
112 changes: 0 additions & 112 deletions test/es6/rlexe.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1326,104 +1326,6 @@
<tags>exclude_dynapogo,exclude_xplat</tags>
</default>
</test>
<test>
<default>
<files>moduletest1.js</files>
<compile-flags>-ES6Module</compile-flags>
<tags>exclude_dynapogo,exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>moduletest2.js</files>
<compile-flags>-ES6Module</compile-flags>
<tags>exclude_dynapogo,exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>module-syntax.js</files>
<compile-flags>-MuteHostErrorMsg -ES6Module -args summary -endargs</compile-flags>
<tags>exclude_dynapogo,exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>module-syntax1.js</files>
<compile-flags>-ES6Module -args summary -endargs</compile-flags>
<tags>exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>module-functionality.js</files>
<compile-flags>-MuteHostErrorMsg -ES6Module -args summary -endargs</compile-flags>
<tags>exclude_dynapogo,exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>moduleUrlInError.js</files>
<tags>exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>dynamic-module-functionality.js</files>
<compile-flags>-ES6Module -ESDynamicImport -args summary -endargs</compile-flags>
<tags>exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>dynamic-module-import-specifier.js</files>
<compile-flags>-MuteHostErrorMsg -ESDynamicImport -ES6Module -args summary -endargs</compile-flags>
<tags>exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>module-syntax.js</files>
<compile-flags>-MuteHostErrorMsg -ES6Module -force:deferparse -args summary -endargs</compile-flags>
<tags>exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>module-syntax1.js</files>
<compile-flags>-ES6Module -force:deferparse -args summary -endargs</compile-flags>
<tags>exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>module-namespace.js</files>
<compile-flags>-ES6Module -Es6ToStringTag -args summary -endargs</compile-flags>
<tags>exclude_dynapogo,exclude_drt,exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>module-bugfixes.js</files>
<compile-flags>-MuteHostErrorMsg -ES6Module -args summary -endargs</compile-flags>
<tags>exclude_dynapogo,exclude_sanitize_address,bugfix</tags>
</default>
</test>
<test>
<default>
<files>bug_OS12095746.js</files>
<compile-flags>-MuteHostErrorMsg -IgnoreScriptErrorCode -TraceHostCallback -ES6Module -ESDynamicImport</compile-flags>
<tags>exclude_dynapogo,exclude_sanitize_address,bugfix,exclude_drt</tags>
<baseline>bug_OS12095746.baseline</baseline>
</default>
</test>
<test>
<default>
<files>test_bug_2645.js</files>
<compile-flags>-ES6Module</compile-flags>
<tags>exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>OS_5500719.js</files>
Expand Down Expand Up @@ -1458,13 +1360,6 @@
<tags>BugFix</tags>
</default>
</test>
<test>
<default>
<files>bug_issue_3076.js</files>
<compile-flags>-force:deferparse -ESDynamicImport </compile-flags>
<tags>BugFix,exclude_sanitize_address</tags>
</default>
</test>
<test>
<default>
<files>bug_issue_3247.js</files>
Expand Down Expand Up @@ -1522,13 +1417,6 @@
<compile-flags>-force:deferparse -args summary -endargs</compile-flags>
</default>
</test>
<test>
<default>
<files>bug_OS14562349.js</files>
<tags>BugFix</tags>
<compile-flags>-ESDynamicImport -args summary -endargs</compile-flags>
</default>
</test>
<test>
<default>
<files>DeferParseLambda.js</files>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit fa60541

Please sign in to comment.