1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2026-01-12 05:23:17 +00:00

Fixed circular references impacting GC

This commit is contained in:
Bryan Roe
2021-03-11 11:17:39 -08:00
parent 8f6bb1a527
commit d1e1d67a88
7 changed files with 334 additions and 67 deletions

View File

@@ -387,6 +387,36 @@ duk_ret_t ILibDuktape_SpawnedProcess_SIGCHLD_sink(duk_context *ctx)
return(0);
}
#endif
void ILibDuktape_SpawnedProcess_exitHandler_immediateSink(duk_context *ctx, void ** args, int argsLen)
{
duk_push_this(ctx); // [immediate]
duk_get_prop_string(ctx, -1, "process"); // [immediate][spawnedProcess]
duk_del_prop_string(ctx, -2, "process");
if (duk_has_prop_string(ctx, -1, "stdout"))
{
duk_get_prop_string(ctx, -1, "stdout"); // [immediate][spawnedProcess][stdout]
duk_prepare_method_call(ctx, -1, "removeAllListeners"); // [immediate][spawnedProcess][stdout][removeAll][this]
duk_pcall_method(ctx, 0); duk_pop_2(ctx); // [immediate][spawnedProcess]
}
if (duk_has_prop_string(ctx, -1, "stderr"))
{
duk_get_prop_string(ctx, -1, "stderr"); // [immediate][spawnedProcess][stderr]
duk_prepare_method_call(ctx, -1, "removeAllListeners"); // [immediate][spawnedProcess][stderr][removeAll][this]
duk_pcall_method(ctx, 0); duk_pop_2(ctx); // [immediate][spawnedProcess]
}
duk_prepare_method_call(ctx, -1, "removeAllListeners"); // [immediate][spawnedProcess][removeAll][this]
duk_pcall_method(ctx, 0); duk_pop_3(ctx); // ...
}
duk_ret_t ILibDuktape_SpawnedProcess_exitHandler(duk_context *ctx)
{
void *i = ILibDuktape_Immediate(ctx, (void*[]) { ctx }, 1, ILibDuktape_SpawnedProcess_exitHandler_immediateSink);
duk_push_heapptr(ctx, i); // [immediate]
duk_push_this(ctx); // [immediate][spawnedProcess]
duk_put_prop_string(ctx, -2, "process"); // [immediate]
duk_pop(ctx); // ...
return(0);
}
ILibDuktape_ChildProcess_SubProcess* ILibDuktape_ChildProcess_SpawnedProcess_PUSH(duk_context *ctx, ILibProcessPipe_Process mProcess, void *callback)
{
duk_push_object(ctx); // [ChildProcess]
@@ -449,11 +479,6 @@ ILibDuktape_ChildProcess_SubProcess* ILibDuktape_ChildProcess_SpawnedProcess_PUS
ILibDuktape_ChildProcess_SubProcess_StdErrHandler,
ILibDuktape_ChildProcess_SubProcess_SendOK, retVal);
}
else
{
if (callback != NULL) { ILibDuktape_EventEmitter_AddOnce(emitter, "exit", callback); }
}
#if defined(_POSIX)
ILibDuktape_EventEmitter_SetupOn(ctx, ILibDuktape_GetProcessObject(ctx), "SIGCHLD"); // [child][on][process][SIGCHLD]
@@ -466,6 +491,9 @@ ILibDuktape_ChildProcess_SubProcess* ILibDuktape_ChildProcess_SpawnedProcess_PUS
#endif
ILibDuktape_CreateEventWithSetterEx(ctx, "descriptorMetadata", ILibDuktape_SpawnedProcess_descriptorSetter);
if (callback != NULL) { ILibDuktape_EventEmitter_AddOnce(emitter, "exit", callback); }
ILibDuktape_EventEmitter_AddOnceEx3(ctx, -1, "exit", ILibDuktape_SpawnedProcess_exitHandler);
return(retVal);
}

View File

@@ -436,6 +436,19 @@ duk_ret_t ILibDuktape_GenericMarshal_Variable_bignum_SET(duk_context *ctx)
return(0);
}
#endif
duk_ret_t ILibDuktape_GenericMarshal_Variable_metadata_get(duk_context *ctx)
{
duk_push_this(ctx);
duk_get_prop_string(ctx, -1, ILibDuktape_EventEmitter_FinalizerDebugMessage);
return(1);
}
duk_ret_t ILibDuktape_GenericMarshal_Variable_metadata_set(duk_context *ctx)
{
duk_push_this(ctx); // [var]
duk_dup(ctx, 0); // [var][value]
duk_put_prop_string(ctx, -2, ILibDuktape_EventEmitter_FinalizerDebugMessage);
return(0);
}
void ILibDuktape_GenericMarshal_Variable_PUSH(duk_context *ctx, void *ptr, int size)
{
duk_push_object(ctx); // [var]
@@ -463,6 +476,10 @@ void ILibDuktape_GenericMarshal_Variable_PUSH(duk_context *ctx, void *ptr, int s
ILibDuktape_CreateInstanceMethod(ctx, "autoFree", ILibDuktape_GenericMarshal_Variable_autoFree, DUK_VARARGS);
ILibDuktape_CreateInstanceMethod(ctx, "pointerBuffer", ILibDuktape_GenericMarshal_Variable_pointerBuffer, 0);
ILibDuktape_CreateInstanceMethod(ctx, "getPointerPointer", ILibDuktape_GenericMarshal_Variable_pointerpointer, 0);
ILibDuktape_CreateEventWithGetterAndSetterEx(ctx, "descriptorMetadata", ILibDuktape_GenericMarshal_Variable_metadata_get, ILibDuktape_GenericMarshal_Variable_metadata_set);
duk_push_sprintf(ctx, "Size: %d", size);
duk_put_prop_string(ctx, -2, ILibDuktape_EventEmitter_FinalizerDebugMessage);
ILibDuktape_CreateFinalizer(ctx, ILibDuktape_GenericMarshal_Variable_Finalizer);
}
@@ -889,6 +906,7 @@ void ILibDuktape_GenericMarshal_MethodInvokeAsync_ChainDispatch(void *chain, voi
duk_context *ctx = data->ctx;
duk_push_heapptr(data->ctx, data->promise); // [promise]
duk_del_prop_string(data->ctx, -1, "_varArray");
duk_get_prop_string(data->ctx, -1, "_RES"); // [promise][resolver]
duk_swap_top(data->ctx, -2); // [resolver][this]
ILibDuktape_GenericMarshal_Variable_PUSH(data->ctx, (void*)(PTRSIZE)data->vars, (int)sizeof(void*)); // [resolver][this][var]
@@ -1048,6 +1066,7 @@ void ILibDuktape_GenericMarshal_MethodInvokeAsync_Done_chain(void *chain, void*
}
duk_push_heapptr(data->ctx, data->promise); // [promise]
duk_del_prop_string(data->ctx, -1, "_varArray");
duk_get_prop_string(data->ctx, -1, "_RES"); // [promise][resolver]
duk_swap_top(data->ctx, -2); // [resolver][this]
ILibDuktape_GenericMarshal_Variable_PUSH(data->ctx, (void*)data->workAvailable, (int)sizeof(void*)); // [resolver][this][var]
@@ -1090,6 +1109,12 @@ duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync_promfin(duk_context *ctx)
}
return(0);
}
duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync_PromiseSettled(duk_context *ctx)
{
duk_push_this(ctx); // [promise]
duk_del_prop_string(ctx, -1, "nativeProxy");
return(0);
}
duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync(duk_context *ctx)
{
void *redirectionPtr = NULL, *redirectionPtrName = NULL;
@@ -1136,6 +1161,9 @@ duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync(duk_context *ctx)
data->chain = duk_ctx_chain(ctx);
duk_push_current_function(ctx); // [promise][func]
duk_push_sprintf(ctx, "%s_Async", Duktape_GetStringPropertyValue(ctx, -1, "_funcName", "??"));
duk_put_prop_string(ctx, -3, ILibDuktape_EventEmitter_FinalizerDebugMessage);
data->fptr = Duktape_GetPointerProperty(ctx, -1, "_address");
data->methodName = Duktape_GetStringPropertyValue(ctx, -1, "_funcName", NULL);
data->waitingForResult = WAITING_FOR_RESULT__DISPATCHER;
@@ -1143,6 +1171,7 @@ duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync(duk_context *ctx)
data->workStarted = ggd->dispatch->WorkerThreadHandle;
data->vars = (PTRSIZE*)ILibMemory_SmartAllocate(sizeof(PTRSIZE)*parms);
data->fptr_redirection = NULL;
duk_pop(ctx); // [promise]
}
else
{
@@ -1185,6 +1214,10 @@ duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync(duk_context *ctx)
duk_eval_string(ctx, "require('promise');"); // [func][promise]
duk_push_c_function(ctx, ILibDuktape_GenericMarshal_MethodInvokeAsync_promise, 2);
duk_new(ctx, 1);
duk_get_prop_string(ctx, -1, "_internal");
duk_push_sprintf(ctx, "%s_Async", Duktape_GetStringPropertyValue(ctx, -3, "_funcName", "??"));
duk_put_prop_string(ctx, -2, ILibDuktape_EventEmitter_FinalizerDebugMessage);
duk_pop(ctx);
ILibDuktape_CreateFinalizer(ctx, ILibDuktape_GenericMarshal_MethodInvokeAsync_promfin);
duk_push_pointer(ctx, data); duk_put_prop_string(ctx, -2, "_data");
data->promise = duk_get_heapptr(ctx, -1);
@@ -1209,8 +1242,7 @@ duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync(duk_context *ctx)
{
if (duk_is_object(ctx, i))
{
duk_get_prop_string(ctx, i, "_ptr");
data->vars[i] = (PTRSIZE)duk_to_pointer(ctx, -1);
data->vars[i] = (PTRSIZE)Duktape_GetPointerProperty(ctx, i, "_ptr");
}
else if (duk_is_number(ctx, i))
{
@@ -1240,13 +1272,15 @@ duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync(duk_context *ctx)
}
#endif
duk_push_heapptr(ctx, data->promise); // [promise]
duk_push_current_function(ctx); // [promise][func]
duk_get_prop_string(ctx, -1, "_obj"); // [promise][func][obj]
duk_remove(ctx, -2); // [promise][obj]
void *hptr = Duktape_GetPointerProperty(ctx, -1, "_obj");
duk_pop(ctx); // [promise]
duk_push_heapptr(ctx, hptr); // [promise][obj]
duk_put_prop_string(ctx, -2, "nativeProxy");// [promise]
duk_get_prop_string(ctx, -1, "_internal");
ILibDuktape_EventEmitter_AddOn_Infrastructure(ctx, -1, "settled", ILibDuktape_GenericMarshal_MethodInvokeAsync_PromiseSettled);
duk_pop(ctx);
return(1);
}
duk_ret_t ILibDuktape_GenericMarshal_MethodInvokeAsync_wait(duk_context *ctx)
@@ -1458,7 +1492,8 @@ duk_ret_t ILibDuktape_GenericMarshal_CreateMethod(duk_context *ctx)
duk_push_string(ctx, funcName); duk_put_prop_string(ctx, -2, "_funcName");
duk_push_c_function(ctx, ILibDuktape_GenericMarshal_MethodInvokeAsync_abort, 0); // [obj][func][func][func]
duk_put_prop_string(ctx, -2, "abort"); // [obj][func][func]
duk_push_this(ctx); duk_put_prop_string(ctx, -2, "_obj");
duk_push_pointer(ctx, duk_get_heapptr(ctx, -3)); duk_put_prop_string(ctx, -2, "_obj");
duk_push_c_function(ctx, ILibDuktape_GenericMarshal_MethodInvokeAsync_thread, 0); duk_put_prop_string(ctx, -2, "thread");
duk_push_c_function(ctx, ILibDuktape_GenericMarshal_MethodInvokeAsync_wait, DUK_VARARGS); duk_put_prop_string(ctx, -2, "wait");
duk_push_c_function(ctx, ILibDuktape_GenericMarshal_MethodInvokeAsync_thread_id, 0); duk_put_prop_string(ctx, -2, "threadId");

File diff suppressed because one or more lines are too long

View File

@@ -38,6 +38,7 @@ limitations under the License.
#define ILibDuktape_EventEmitter_EventTable "\xFF_EventEmitter_EventTable"
#define ILibDuktape_EventEmitter_CountTable "\xFF_EventEmitter_CountTable"
#define ILibDuktape_EventEmitter_References "\xFF_EventReferences"
extern void ILibDuktape_GenericMarshal_Variable_PUSH(duk_context *ctx, void *ptr, int size);
#ifdef ILIBEVENTEMITTER_REFHOLD
int gEventEmitterReferenceHold = ILIBEVENTEMITTER_REFHOLD;
@@ -1213,7 +1214,7 @@ duk_ret_t ILibDuktape_EventEmitter_allProperties(duk_context *ctx)
const char *tmp;
duk_push_array(ctx); // [arr]
duk_dup(ctx, 0); // [arr][obj]
duk_enum(ctx, -1, (all == 0 ? 0 : DUK_ENUM_INCLUDE_NONENUMERABLE) | DUK_ENUM_INCLUDE_HIDDEN | DUK_ENUM_INCLUDE_SYMBOLS);;
duk_enum(ctx, -1, (all == 0 ? 0 : DUK_ENUM_INCLUDE_NONENUMERABLE) | (all == 2 ? DUK_ENUM_OWN_PROPERTIES_ONLY : 0) | DUK_ENUM_INCLUDE_HIDDEN | DUK_ENUM_INCLUDE_SYMBOLS);;
while (duk_next(ctx, -1, 0)) // [arr][obj][enum][key]
{
tmp = NULL;
@@ -1342,13 +1343,56 @@ duk_ret_t ILibDuktape_EventEmitter_setFinalizerMetadata(duk_context *ctx)
duk_put_prop_string(ctx, -2, ILibDuktape_EventEmitter_FinalizerDebugMessage);
return(0);
}
duk_ret_t ILibDuktape_RefCountPointer(duk_context *ctx)
duk_ret_t ILibDuktape_RefCountPointer_eval(duk_context *ctx)
{
duk_push_this(ctx);
duk_int_t *t = _get_refcount_ptr(ctx, -1);
duk_push_pointer(ctx, t);
duk_int_t *t = (duk_int_t*)Duktape_GetPointerProperty(ctx, -1, "_ptr");
if (t == NULL) { return(ILibDuktape_Error(ctx, "ERROR")); }
duk_push_int(ctx, *t);
return(1);
}
duk_ret_t ILibDuktape_RefCountPointer_set(duk_context *ctx)
{
duk_push_this(ctx);
duk_int_t *t = (duk_int_t*)Duktape_GetPointerProperty(ctx, -1, "_ptr");
if (t == NULL) { return(ILibDuktape_Error(ctx, "ERROR")); }
*t = duk_require_int(ctx, 0);
return(0);
}
duk_ret_t ILibDuktape_RefCountPointer(duk_context *ctx)
{
int nargs = duk_get_top(ctx);
duk_push_this(ctx);
char *tmp = Duktape_GetStringPropertyValue(ctx, -1, ILibDuktape_OBJID, "");
if (nargs == 1) { duk_dup(ctx, 0); }
duk_int_t *t = _get_refcount_ptr(ctx, -1);
if (nargs == 1) { duk_pop(ctx); }
ILibDuktape_GenericMarshal_Variable_PUSH(ctx, t, sizeof(void*));
duk_push_sprintf(ctx, "_get_refcount_ptr(%s)", tmp); duk_put_prop_string(ctx, -2, ILibDuktape_EventEmitter_FinalizerDebugMessage);
ILibDuktape_CreateInstanceMethod(ctx, "eval", ILibDuktape_RefCountPointer_eval, 0);
ILibDuktape_CreateInstanceMethod(ctx, "set", ILibDuktape_RefCountPointer_set, 1);
return(1);
}
duk_ret_t ILibDuktape_EventEmitter_getProperty(duk_context *ctx)
{
duk_push_this(ctx); // [obj]
char *tmp = (char*)duk_push_sprintf(ctx, "%s", duk_get_string(ctx, 0));// [obj][string]
if (tmp[0] == '?') { tmp[0] = '\xFF'; }
duk_get_prop_string(ctx, -2, tmp);
tmp = (char*)duk_get_string(ctx, -1);
return(1);
}
duk_ret_t ILibDuktape_EventEmitter_setProperty(duk_context *ctx)
{
char *s = (char*)duk_get_string(ctx, 0);
char *s2 = (char*)duk_get_string(ctx, 1);
duk_push_this(ctx); // [obj]
char *tmp = (char*)duk_push_sprintf(ctx, "%s", duk_get_string(ctx, 0));// [obj][string]
duk_dup(ctx, 1); // [obj][string][val]
if (tmp[0] == '?') { tmp[0] = '\xFF'; }
duk_put_prop_string(ctx, -3, tmp);
return(0);
}
void ILibDuktape_EventEmitter_PUSH(duk_context *ctx, void *chain)
{
duk_push_object(ctx); // [emitter]
@@ -1357,11 +1401,14 @@ void ILibDuktape_EventEmitter_PUSH(duk_context *ctx, void *chain)
ILibDuktape_CreateInstanceMethod(ctx, "moderated", ILibDuktape_EventEmitter_moderated, DUK_VARARGS);
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "all", 1, "allProperties", ILibDuktape_EventEmitter_allProperties, 1);
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "all", 0, "hiddenProperties", ILibDuktape_EventEmitter_allProperties, 1);
ILibDuktape_CreateInstanceMethodWithIntProperty(ctx, "all", 2, "allOwnProperties", ILibDuktape_EventEmitter_allProperties, 1);
ILibDuktape_CreateInstanceMethod(ctx, "showReferences", ILibDuktape_EventEmitter_showReferences, DUK_VARARGS);
ILibDuktape_CreateInstanceMethod(ctx, "addHiddenReference", ILibDuktape_EventEmitter_addHidden, 2);
ILibDuktape_CreateInstanceMethod(ctx, "deleteProperty", ILibDuktape_EventEmitter_deleteProperty, 2);
ILibDuktape_CreateInstanceMethod(ctx, "setFinalizerMetadata", ILibDuktape_EventEmitter_setFinalizerMetadata, 1);
ILibDuktape_CreateInstanceMethod(ctx, "_refCountPointer", ILibDuktape_RefCountPointer, 0);
ILibDuktape_CreateInstanceMethod(ctx, "_refCountPointer", ILibDuktape_RefCountPointer, DUK_VARARGS);
ILibDuktape_CreateInstanceMethod(ctx, "getProperty", ILibDuktape_EventEmitter_getProperty, 1);
ILibDuktape_CreateInstanceMethod(ctx, "setProperty", ILibDuktape_EventEmitter_setProperty, 2);
}
void ILibDuktape_EventEmitter_Init(duk_context *ctx)
{

View File

@@ -99915,3 +99915,14 @@ duk_int_t* _get_refcount_ptr(void *thr, duk_idx_t idx)
duk_heaphdr *h = (DUK_TVAL_IS_HEAP_ALLOCATED(tv) ? DUK_TVAL_GET_HEAPHDR(tv) : NULL);
return(&(DUK_HEAPHDR_GET_REFCOUNT(h)));
}
void* _duk_get_first_object(void *ctx)
{
duk_hthread *thr = (duk_hthread*)ctx;
return(thr->heap->heap_allocated);
}
void* _duk_get_next_object(void *ctx, void *heapptr)
{
duk_hthread *thr = (duk_hthread*)ctx;
return(DUK_HEAPHDR_GET_NEXT(thr->heap, ((duk_heaphdr*)heapptr)));
}

View File

@@ -16,6 +16,12 @@ limitations under the License.
var refTable = {};
function promiseInitializer(r,j)
{
this._res = r;
this._rej = j;
}
function getRootPromise(obj)
{
while(obj.parentPromise)
@@ -35,6 +41,20 @@ function event_forwarder(sourceObj, sourceName, targetObj, targetName)
sourceObj.on(sourceName, targetObj.emit.bind(targetObj));
}
function return_resolved()
{
var parms = ['resolved'];
for (var ai in arguments)
{
parms.push(arguments[ai]);
}
this._XSLF.emit.apply(this._XSLF, parms);
}
function return_rejected()
{
this._XSLF.promise.__childPromise._rej(e);
}
function Promise(promiseFunc)
{
this._ObjectID = 'promise';
@@ -58,7 +78,21 @@ function Promise(promiseFunc)
this._up = value;
}
});
Object.defineProperty(this, "descriptorMetadata",
{
get: function ()
{
return (require('events').getProperty.call(this._internal, '?_FinalizerDebugMessage'));
},
set: function (value)
{
require('events').setProperty.call(this._internal, '?_FinalizerDebugMessage', value);
}
});
this._internal.on('~', function ()
{
this.completedArgs = [];
});
this._internal.on('newListener', (function (eventName, eventCallback)
{
//console.log('newListener', eventName, 'errors/' + this.errors + ' completed/' + this.completed);
@@ -98,7 +132,6 @@ function Promise(promiseFunc)
eventCallback.apply(this, []);
}
}).internal);
this._internal.resolver = (function _resolver()
{
if (_resolver._self.completed) { return; }
@@ -123,19 +156,7 @@ function Promise(promiseFunc)
{
var pr = getRootPromise(_resolver._self.promise);
args[1]._XSLF = _resolver._self;
args[1].then(function _returnResolved()
{
var parms = ['resolved'];
for (var ai in arguments)
{
parms.push(arguments[ai]);
}
this._XSLF.emit.apply(this._XSLF, parms);
},
function _returnRejected(e)
{
this._XSLF.promise.__childPromise._rej(e);
});
args[1].then(return_resolved, return_rejected);
}
else
{
@@ -181,13 +202,16 @@ function Promise(promiseFunc)
};
this.then = function (resolved, rejected)
{
if (resolved) { this._internal.once('resolved', event_switcher(this, resolved).func.internal); }
if (resolved)
{
this._internal.once('resolved', event_switcher(this, resolved).func.internal);
}
if (rejected)
{
this._internal.once('rejected', event_switcher(this, rejected).func.internal);
}
var retVal = new Promise(function (r, j) { this._rej = j; });
var retVal = new Promise(promiseInitializer);
retVal.parentPromise = this;
if (this._internal.completed)
@@ -218,9 +242,9 @@ function Promise(promiseFunc)
this._internal.once('resolved', retVal._internal.resolver);
this._internal.once('rejected', retVal._internal.rejector);
}
this.__childPromise = retVal;
return (retVal);
this.__childPromise = retVal;
return(retVal);
};
this._internal.resolver._self = this._internal;
@@ -266,6 +290,8 @@ function Promise(promiseFunc)
delete this._up;
delete this.__childPromise;
delete this.promise;
this.removeAllListeners('resolved');
this.removeAllListeners('rejected');
}).internal);
}

View File

@@ -30,11 +30,16 @@ function WindowsMessagePump(options)
emitterUtils.createEvent('exit');
this._msg = GM.CreateVariable(GM.PointerSize == 4 ? 28 : 48);
this._msg.descriptorMetadata = 'MESSAGE';
this._kernel32 = GM.CreateNativeProxy('Kernel32.dll');
this._kernel32.mp = this;
this._kernel32.CreateMethod('GetLastError');
this._kernel32.CreateMethod('GetModuleHandleA');
this._user32 = GM.CreateNativeProxy('User32.dll');
this._user32.mp = this;
this._user32.CreateMethod('CreateWindowExW');
@@ -48,13 +53,14 @@ function WindowsMessagePump(options)
this._user32.CreateMethod('ShowWindow');
this._user32.CreateMethod('TranslateMessage');
this.wndclass = GM.CreateVariable(GM.PointerSize == 4 ? 48 : 80);
this.wndclass.descriptorMetadata = 'wndclass';
this.wndclass.mp = this;
this.wndclass.hinstance = this._kernel32.GetModuleHandleA(0);
//this.wndclass.cname = GM.CreateVariable('MainWWWClass');
this.wndclass.cnamew = GM.CreateVariable('MainWWWClass', { wide: true });
this.wndclass.wndproc = GM.GetGenericGlobalCallback(4);
this.wndclass.wndproc.descriptorMetadata = 'wndproc';
this.wndclass.wndproc.mp = this;
this.wndclass.toBuffer().writeUInt32LE(this.wndclass._size);
this.wndclass.cnamew.pointerBuffer().copy(this.wndclass.Deref(GM.PointerSize == 4 ? 40 : 64, GM.PointerSize).toBuffer());
@@ -63,6 +69,11 @@ function WindowsMessagePump(options)
this.wndclass.wndproc.on('GlobalCallback', function onWndProc(xhwnd, xmsg, wparam, lparam)
{
var processed = false;
xhwnd.descriptorMetadata = 'win-message-pump: xhwnd';
xmsg.descriptorMetadata = 'win-message-pump: xmsg';
wparam.descriptorMetadata = 'win-message-pump: wparam';
lparam.descriptorMetadata = 'win-message-pump: lparam';
if (this.mp._hwnd != null && this.mp._hwnd.Val == xhwnd.Val)
{
// This is for us
@@ -75,11 +86,15 @@ function WindowsMessagePump(options)
{
// We need to call DefWindowProcA, becuase this message was not handled
var p = this.mp._user32.DefWindowProcW.async(d, xhwnd, xmsg, wparam, lparam);
p.descriptorMetadata = 'win-message-pump: DefWindowProcW(1)';
p.dispatcher = this;
p.then(function (ret)
var xk = p.then(function (ret)
{
this.dispatcher.EndDispatcher(ret);
});
xk.descriptorMetadata = 'DefWindowProcW_Result'
xk = null;
p = null;
}
else
{
@@ -92,21 +107,22 @@ function WindowsMessagePump(options)
{
// This message was generated from our CreateWindowExA method
processed = true;
var d = this.StartDispatcher();
this.mp.emit('message', { message: xmsg.Val, wparam: wparam.Val, lparam: lparam.Val, lparam_hex: lparam.pointerBuffer().toString('hex'), hwnd: xhwnd, dispatcher: d });
var msgRet = this.mp.emit_returnValue('message');
if (msgRet == null)
{
{
// We need to call DefWindowProcA, becuase this message was not handled
var p = this.mp._user32.DefWindowProcW.async(d, xhwnd, xmsg, wparam, lparam);
p.descriptorMetadata = 'win-message-pump: DefWindowProcW(2)';
p.dispatcher = this;
p.then(function (ret)
{
this.dispatcher.EndDispatcher(ret);
});
p = null;
}
else
{
@@ -116,11 +132,15 @@ function WindowsMessagePump(options)
}
}
if (processed) { _debugGC(); }
xhwnd = null;
xmsg = null;
wparam = null;
lparam = null;
});
this._user32.RegisterClassExW.async(this.wndclass).then(function ()
{
this.descriptorMetadata = 'RegisterClassExW';
if (!this.nativeProxy.mp._options) { this.nativeProxy.mp._options = {}; }
if (!this.nativeProxy.mp._options.window) { this.nativeProxy.mp._options.window = {}; }
if (this.nativeProxy.mp._options.window.exstyles == null) { this.nativeProxy.mp._options.window.exstyles = 0x00000088; } // TopMost Tool Window
@@ -133,8 +153,9 @@ function WindowsMessagePump(options)
this.nativeProxy.CreateWindowExW.async(this.nativeProxy.RegisterClassExW.async, this.nativeProxy.mp._options.window.exstyles, this.nativeProxy.mp.wndclass.cnamew,
this.nativeProxy.mp._options.window.title == null ? 0 : GM.CreateVariable(this.nativeProxy.mp._options.window.title, { wide: true }), this.nativeProxy.mp._options.window.winstyles, this.nativeProxy.mp._options.window.x, this.nativeProxy.mp._options.window.y,
this.nativeProxy.mp._options.window.width, this.nativeProxy.mp._options.window.height, 0, 0, 0, 0)
.then(function(h)
.then(function (h)
{
this.descriptorMetadata = 'CreateWindowExW';
if (h.Val == 0)
{
// Error creating hidden window
@@ -150,34 +171,43 @@ function WindowsMessagePump(options)
});
this._startPump = function _startPump()
{
this._user32.GetMessageW.async(this._user32.RegisterClassExW.async, this._msg, this._hwnd, 0, 0).then(function (r)
{
if(r.Val > 0)
this._user32.GetMessageW.async(this._user32.RegisterClassExW.async, this._msg, this._hwnd, 0, 0)
.then(function (r)
{
this.nativeProxy.TranslateMessage.async(this.nativeProxy.RegisterClassExW.async, this.nativeProxy.mp._msg).then(function ()
var val = r.Val;
r.descriptorMetadata = 'RegisterClassExW_result';
r = null;
if (val > 0)
{
this.nativeProxy.DispatchMessageW.async(this.nativeProxy.RegisterClassExW.async, this.nativeProxy.mp._msg).then(function ()
this.nativeProxy.TranslateMessage.async(this.nativeProxy.RegisterClassExW.async, this.nativeProxy.mp._msg).then(function ()
{
this.nativeProxy.mp._startPump();
this.descriptorMetadata = 'TranslateMessage';
this.nativeProxy.DispatchMessageW.async(this.nativeProxy.RegisterClassExW.async, this.nativeProxy.mp._msg).then(function ()
{
this.descriptorMetadata = 'DispatchMessageW';
this.nativeProxy.mp._startPump();
});
});
});
}
else
{
// We got a 'QUIT' message
this.nativeProxy.DestroyWindow.async(this.nativeProxy.RegisterClassExW.async, this.nativeProxy.mp._hwnd).then(function ()
}
else
{
this.nativeProxy.RegisterClassExW.async.abort();
delete this.nativeProxy.mp._hwnd;
this.nativeProxy.mp.emit('exit', 0);
this.nativeProxy.mp.wndclass.wndproc.removeAllListeners('GlobalCallback');
this.nativeProxy.mp.wndclass.wndproc = null;
});
}
}, function (err) { this.nativeProxy.mp.stop(); });
// We got a 'QUIT' message
this.nativeProxy.DestroyWindow.async(this.nativeProxy.RegisterClassExW.async, this.nativeProxy.mp._hwnd).then(function ()
{
this.descriptorMetadata = 'DestroyWindow';
this.nativeProxy.RegisterClassExW.async.abort();
delete this.nativeProxy.mp._hwnd;
this.nativeProxy.mp.wndclass.wndproc.removeAllListeners('GlobalCallback');
this.nativeProxy.mp.wndclass.wndproc.mp = null;
this.nativeProxy.mp.wndclass.wndproc = null;
this.nativeProxy.mp.emit('exit', 0);
});
}
}, function (err)
{
this.nativeProxy.mp.stop();
});
}
this.stop = function stop()
{
if (this._hwnd)
@@ -196,10 +226,23 @@ function WindowsMessagePump(options)
};
this.once('~', function ()
{
this._msg = null;
this.stop();
});
this.once('exit', (function ()
{
setImmediate(function (j)
{
j._kernel32.mp = null;
j._user32.mp = null;
if (j.wndclass) { j.wndclass.mp = null; }
if (j.wndclass.wndproc) { j.wndclass.wndproc.mp = null; }
j.removeAllListeners();
}, this);
}).internal);
}
module.exports = WindowsMessagePump;
module.exports.WindowStyles =
{
@@ -218,3 +261,5 @@ module.exports.WindowStylesEx =
WS_EX_NOREDIRECTIONBITMAP: 0x00200000, WS_EX_RIGHT: 0x00001000, WS_EX_RIGHTSCROLLBAR: 0x00000000, WS_EX_RTLREADING: 0x00002000,
WS_EX_STATICEDGE: 0x00020000, WS_EX_TOOLWINDOW: 0x00000080, WS_EX_TOPMOST: 0x00000008, WS_EX_TRANSPARENT: 0x00000020, WS_EX_WINDOWEDGE: 0x00000100
};