Skip to content

Commit

Permalink
serialize arguments passed to load()
Browse files Browse the repository at this point in the history
  • Loading branch information
bobode committed Nov 4, 2013
1 parent 1fee40f commit ff95f33
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion D2BS.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define XP_WIN

#define D2BS_VERSION "1.5.1806" //uptodate with d branch 1765 ff 20b
#define D2BS_VERSION "1.5.1807" //uptodate with d branch 1765 ff 20b

#include <windows.h>
#include <vector>
Expand Down
11 changes: 10 additions & 1 deletion JSCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,16 @@ JSAPI_FUNC(my_load)

sprintf_s(buf, sizeof(buf), "%s\\%s", Vars.szScriptPath, file);
StringReplace(buf, '/', '\\', _MAX_PATH+_MAX_FNAME);
Script* newScript = ScriptEngine::CompileFile(buf, scriptState, argc-1, JS_ARGV(cx, vp)+1);

JSAutoStructuredCloneBuffer** autoBuffer = new JSAutoStructuredCloneBuffer*;
for(uintN i = 1; i < argc; i++)
{
autoBuffer[i-1] = new JSAutoStructuredCloneBuffer;
autoBuffer[i-1]->write(cx, JS_ARGV(cx, vp)[i]);
}


Script* newScript = ScriptEngine::CompileFile(buf, scriptState, argc-1, autoBuffer);
JS_free(cx, file);
if(newScript)
{
Expand Down
3 changes: 3 additions & 0 deletions JSUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ JSAPI_PROP(unit_getProperty)

UnitAny* pUnit = D2CLIENT_FindUnit(lpUnit->dwUnitId, lpUnit->dwType);
if(!pUnit)
{
JS_THIS(cx, vp.address()).setBoolean(false);
return JS_TRUE;
}
Room1* pRoom = NULL;

switch(JSVAL_TO_INT(ID))
Expand Down
25 changes: 21 additions & 4 deletions Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

using namespace std;

Script::Script(const char* file, ScriptState state, uintN argc, jsval* argv) :
Script::Script(const char* file, ScriptState state, uintN argc, JSAutoStructuredCloneBuffer** argv) :
context(NULL), globalObject(NULL), scriptObject(NULL), script(NULL), execCount(0),
isAborted(false), isPaused(false), isReallyPaused(false), scriptState(state),
threadHandle(INVALID_HANDLE_VALUE), threadId(0), argc(argc), argv(argv)
Expand Down Expand Up @@ -196,19 +196,36 @@ void Script::Run(void)
isAborted = false;

jsval main = INT_TO_JSVAL(1), dummy = INT_TO_JSVAL(1);

JS_BeginRequest(GetContext());

//args passed from load
jsval* argvalue = new jsval[argc];
for(uintN i = 0; i < argc; i++)
argv[i]->read(context, &argvalue[i]);

for(int j = 0 ; j < argc; j++)
JS_AddValueRoot(context, &argvalue[j]);



JS_AddValueRoot(GetContext(), &main);
JS_AddValueRoot(GetContext(), &dummy);
if(JS_ExecuteScript(GetContext(), globalObject, script, &dummy) != JS_FALSE &&
JS_GetProperty(GetContext(), globalObject, "main", &main) != JS_FALSE &&
JSVAL_IS_FUNCTION(GetContext(), main))
{
JS_CallFunctionValue(GetContext(), globalObject, main, this->argc, this->argv, &dummy);
JS_CallFunctionValue(GetContext(), globalObject, main, this->argc, argvalue, &dummy);
}
JS_RemoveValueRoot(GetContext(), &main);
JS_RemoveValueRoot(GetContext(), &dummy);

for(int j = 0 ; j < argc; j++)
JS_RemoveValueRoot(GetContext(), &argvalue[j]);
for(uintN i = 0; i < argc; i++)
{
argv[i]->clear();
delete argv[i];
}

JS_EndRequest(GetContext());

execCount++;
Expand Down
4 changes: 2 additions & 2 deletions Script.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Script
JSRuntime* runtime;
myUnit* me;
uintN argc;
jsval* argv;
JSAutoStructuredCloneBuffer** argv;

JSObject *globalObject, *scriptObject;
bool isLocked, isPaused, isReallyPaused, isAborted;
Expand All @@ -59,7 +59,7 @@ class Script

CRITICAL_SECTION lock;

Script(const char* file, ScriptState state, uintN argc = 0, jsval* argv = NULL);
Script(const char* file, ScriptState state, uintN argc = 0, JSAutoStructuredCloneBuffer** argv = NULL);
Script(const Script&);
Script& operator=(const Script&);
~Script(void);
Expand Down
3 changes: 2 additions & 1 deletion ScriptEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ bool __fastcall DisposeScript(Script* script, void*, uint);
bool __fastcall StopScript(Script* script, void* argv, uint argc);


Script* ScriptEngine::CompileFile(const char* file, ScriptState state, uintN argc, jsval* argv, bool recompile)
Script* ScriptEngine::CompileFile(const char* file, ScriptState state, uintN argc, JSAutoStructuredCloneBuffer** argv, bool recompile)
{
if(GetState() != Running)
return NULL;
char* fileName = _strdup(file);
_strlwr_s(fileName, strlen(file)+1);

try
{
Script* script = new Script(fileName, state, argc, argv);
Expand Down
2 changes: 1 addition & 1 deletion ScriptEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ScriptEngine

static void FlushCache(void);

static Script* CompileFile(const char* file, ScriptState state, uintN argc = 0, jsval* argv = NULL, bool recompile = false);
static Script* CompileFile(const char* file, ScriptState state, uintN argc = 0, JSAutoStructuredCloneBuffer** argv = NULL, bool recompile = false);
static void RunCommand(const char* command);
static void DisposeScript(Script* script);

Expand Down

0 comments on commit ff95f33

Please sign in to comment.