Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Source/Noesis.Javascript/JavaScript.Net.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="JavascriptContext.h" />
<ClInclude Include="JavascriptDebugger.h" />
<ClInclude Include="JavascriptException.h" />
<ClInclude Include="JavascriptExternal.h" />
<ClInclude Include="JavascriptFunction.h" />
Expand All @@ -176,6 +177,7 @@
<ItemGroup>
<ClCompile Include="AssemblyInfo.cpp" />
<ClCompile Include="JavascriptContext.cpp" />
<ClCompile Include="JavascriptDebugger.cpp" />
<ClCompile Include="JavascriptException.cpp" />
<ClCompile Include="JavascriptExternal.cpp" />
<ClCompile Include="JavascriptFunction.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions Source/Noesis.Javascript/JavaScript.Net.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<ClInclude Include="JavascriptStackFrame.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="JavascriptDebugger.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="AssemblyInfo.cpp">
Expand All @@ -59,6 +62,9 @@
<ClCompile Include="JavascriptFunction.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="JavascriptDebugger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
33 changes: 30 additions & 3 deletions Source/Noesis.Javascript/JavascriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "JavascriptContext.h"

#include "SystemInterop.h"
#include "JavascriptDebugger.h"
#include "JavascriptException.h"
#include "JavascriptExternal.h"
#include "JavascriptFunction.h"
Expand Down Expand Up @@ -172,15 +173,27 @@ JavascriptContext::JavascriptContext()
HandleScope scope(isolate);
mContext = new Persistent<Context>(isolate, Context::New(isolate));
terminateRuns = false;
mDebugger = nullptr;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

JavascriptContext::~JavascriptContext()
{
// Guard against double-Dispose: isolate is nulled at the end so a second call is a no-op.
if (isolate == nullptr) return;

{
v8::Locker v8ThreadLock(isolate);
v8::Isolate::Scope isolate_scope(isolate);

// Notify attached debugger before V8 cleanup so it can disconnect cleanly.
if (mDebugger != nullptr)
{
mDebugger->OnContextDisposing();
mDebugger = nullptr;
}

for each (WrappedJavascriptExternal wrapped in mExternals->Values)
delete wrapped.Pointer;
// Clean up JavascriptFunction wrappers
Expand Down Expand Up @@ -216,7 +229,10 @@ JavascriptContext::~JavascriptContext()
delete mTypeToConstructorMapping;
}
if (isolate != NULL)
{
isolate->Dispose();
isolate = nullptr; // Null so double-Dispose is safe (guard check above).
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -350,8 +366,14 @@ JavascriptContext::Run(System::String^ iScript)
JavascriptScope scope(this);
//SetStackLimit();
HandleScope handleScope(isolate);

// Flush any queued debugger commands (e.g., Debugger.enable, setBreakpoint)
// before executing so they take effect for this run.
if (mDebugger != nullptr)
mDebugger->ProcessPendingCommands();

MaybeLocal<Value> ret;

Local<Script> compiledScript = CompileScript(isolate, script);

{
Expand All @@ -361,7 +383,7 @@ JavascriptContext::Run(System::String^ iScript)
if (ret.IsEmpty())
throw gcnew JavascriptException(tryCatch);
}

return JavascriptInterop::ConvertFromV8(ret.ToLocalChecked());
}

Expand All @@ -383,7 +405,12 @@ JavascriptContext::Run(System::String^ iScript, System::String^ iScriptResourceN
JavascriptScope scope(this);
//SetStackLimit();
HandleScope handleScope(isolate);
MaybeLocal<Value> ret;

// Flush any queued debugger commands before executing.
if (mDebugger != nullptr)
mDebugger->ProcessPendingCommands();

MaybeLocal<Value> ret;

Local<Script> compiledScript = CompileScript(isolate, script, scriptResourceName);

Expand Down
10 changes: 9 additions & 1 deletion Source/Noesis.Javascript/JavascriptContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ using namespace std;
////////////////////////////////////////////////////////////////////////////////////////////////////

class JavascriptExternal;
ref class JavascriptDebugger; // Forward declaration (defined in JavascriptDebugger.h)

[System::Flags]
public enum class SetParameterOptions : int
Expand Down Expand Up @@ -237,11 +238,15 @@ public ref class JavascriptContext: public System::IDisposable
//void SetStackLimit();

static JavascriptContext^ GetCurrent();

static v8::Isolate *GetCurrentIsolate();

Local<v8::Object> GetGlobal();

// Accessors for debugger integration
v8::Isolate* GetIsolate() { return isolate; }
v8::Persistent<v8::Context>* GetContextPersistent() { return mContext; }

v8::Locker *Enter([System::Runtime::InteropServices::Out] JavascriptContext^% old_context);

void Exit(v8::Locker *locker, JavascriptContext^ old_context);
Expand Down Expand Up @@ -271,6 +276,9 @@ public ref class JavascriptContext: public System::IDisposable
System::Collections::Generic::Dictionary<int, WrappedJavascriptFunction>^ mFunctions;

System::Collections::Generic::Dictionary<System::String^, WrappedMethod>^ mMethods;

// Attached debugger, if any. Set by JavascriptDebugger constructor.
JavascriptDebugger^ mDebugger;
protected:
// By entering an isolate before using a context, we can have multiple
// contexts used simultaneously in different threads.
Expand Down
Loading