Permalink
Browse files

Run scripts with the JSC C API

This is an effort to avoid leaking memory when running many scripts
within the same process.
  • Loading branch information...
1 parent 0b1aa34 commit 291b91f7149eb1f0d582238e029085fc63a81b77 @mrobinson mrobinson committed Oct 12, 2013
@@ -1,6 +1,8 @@
// TestRunnerComponent.cpp
#include "TestRunnerComponent.h"
+#include <JavaScriptCore/JSContextRef.h>
+#include <JavaScriptCore/JSStringRef.h>
#include <string>
#include <windows.h>
@@ -13,28 +15,73 @@ ScriptRunner::ScriptRunner()
{
}
-std::string convertPlatformStringtoUTF8(Platform::String^ in)
+static std::string convertPlatformStringtoUTF8(Platform::String^ in)
{
size_t sizeRequired = WideCharToMultiByte(CP_UTF8, 0, in->Data(), in->Length(), NULL, 0, NULL, NULL);
std::string result(sizeRequired, 0);
WideCharToMultiByte(CP_UTF8, 0, in->Data(), in->Length(), &result[0], sizeRequired, NULL, NULL);
return result;
}
-Platform::String^ convertUTF8ToPlatformString(std::string utf8)
+static Platform::String^ convertUTF8ToPlatformString(std::string utf8)
{
size_t sizeRequired = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
std::wstring result(sizeRequired, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &result[0], sizeRequired);
return ref new Platform::String(result.c_str());
}
+static std::string convertJSStringToUTF8(JSStringRef jsString)
+{
+ size_t bytesNeeded = JSStringGetMaximumUTF8CStringSize(jsString);
+
+ std::string result;
+ result.resize(bytesNeeded, '\0');
+ JSStringGetUTF8CString(jsString, &result[0], bytesNeeded);
+
+ return result;
+}
+
+static std::string jsValueGetString(JSContextRef context, JSValueRef value)
+{
+ JSStringRef jsString = JSValueToStringCopy(context, value, NULL);
+ std::string result = convertJSStringToUTF8(jsString);
+ JSStringRelease(jsString);
+ return result;
+}
+
ScriptResult^ ScriptRunner::RunScript(Platform::String^ script, Platform::String^ fileName)
{
std::string scriptString = convertPlatformStringtoUTF8(script);
std::string fileNameString = convertPlatformStringtoUTF8(fileName);
std::string exceptionString;
- bool success = !jscmainRepeatable(scriptString.c_str(), fileNameString.c_str(), exceptionString);
+
+ JSGlobalContextRef context = JSGlobalContextCreate(NULL);
+ JSStringRef jsScriptString = JSStringCreateWithUTF8CString(scriptString.c_str());
+ JSStringRef jsFilenameString = JSStringCreateWithUTF8CString(fileNameString.c_str());
+ JSValueRef exception = NULL;
+
+ JSValueRef result = JSEvaluateScript(context, jsScriptString, NULL, jsFilenameString, 0, &exception);
+ bool success = result && !exception;
+ JSStringRelease(jsScriptString);
+ JSStringRelease(jsFilenameString);
+
+ if (result)
+ JSValueUnprotect(context, result);
+
+ if (exception) {
+ JSStringRef jsStackString = JSStringCreateWithUTF8CString("stack");
+ JSValueRef stack = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), jsStackString, NULL);
+ JSStringRelease(jsStackString);
+
+ exceptionString = jsValueGetString(context, exception) + "\n" + jsValueGetString(context, stack);
+
+ JSValueUnprotect(context, stack);
+ JSValueUnprotect(context, exception);
+ }
+
+ JSGarbageCollect(context);
+ JSGlobalContextRelease(context);
return ref new ScriptResult(success, fileName, convertUTF8ToPlatformString(exceptionString));
}
@@ -90,6 +90,7 @@
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalUsingDirectories>$(WindowsSDK_MetadataPath);$(AdditionalUsingDirectories)</AdditionalUsingDirectories>
<CompileAsWinRT>true</CompileAsWinRT>
+ <SuppressStartupBanner>true</SuppressStartupBanner>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@@ -138,16 +139,6 @@
<ClInclude Include="TestRunnerComponent.h" />
</ItemGroup>
<ItemGroup>
- <ClCompile Include="..\jsc.cpp">
- <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">false</CompileAsWinRT>
- <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">_CRT_SECURE_NO_WARNINGS;_WINRT_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
- <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">_CRT_SECURE_NO_WARNINGS;_WINRT_DLL;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
- <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">_CRT_SECURE_NO_WARNINGS;_WINRT_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
- <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">_CRT_SECURE_NO_WARNINGS;_WINRT_DLL;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
- <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">false</CompileAsWinRT>
- <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</CompileAsWinRT>
- <CompileAsWinRT Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</CompileAsWinRT>
- </ClCompile>
<ClCompile Include="TestRunnerComponent.cpp" />
</ItemGroup>
<ItemGroup>

0 comments on commit 291b91f

Please sign in to comment.