diff --git a/microscript/ILibDuktape_Polyfills.c b/microscript/ILibDuktape_Polyfills.c index 58e39c8..e2ff4f1 100644 --- a/microscript/ILibDuktape_Polyfills.c +++ b/microscript/ILibDuktape_Polyfills.c @@ -2515,7 +2515,29 @@ duk_ret_t ILibDuktape_DescriptorEvents_Remove(duk_context *ctx) duk_push_this(ctx); // [obj] duk_get_prop_string(ctx, -1, ILibDuktape_DescriptorEvents_Table); // [obj][table] duk_dup(ctx, 0); // [obj][table][key] - duk_del_prop(ctx, -2); // [obj][table] + if (!duk_is_null_or_undefined(ctx, 1) && duk_is_object(ctx, 1)) + { + duk_get_prop(ctx, -2); // [obj][table][value] + if (duk_is_null_or_undefined(ctx, -1)) { return(0); } + duk_get_prop_string(ctx, -1, ILibDuktape_DescriptorEvents_Options); //..[table][value][options] + if (duk_has_prop_string(ctx, 1, "readset")) { duk_push_false(ctx); duk_put_prop_string(ctx, -2, "readset"); } + if (duk_has_prop_string(ctx, 1, "writeset")) { duk_push_false(ctx); duk_put_prop_string(ctx, -2, "writeset"); } + if (duk_has_prop_string(ctx, 1, "errorset")) { duk_push_false(ctx); duk_put_prop_string(ctx, -2, "errorset"); } + if( Duktape_GetBooleanProperty(ctx, -1, "readset", 0) == 0 && + Duktape_GetBooleanProperty(ctx, -1, "writeset", 0) == 0 && + Duktape_GetBooleanProperty(ctx, -1, "errorset", 0) == 0) + { + // No FD_SET watchers, so we can remove the entire object + duk_pop_2(ctx); // [obj][table] + duk_dup(ctx, 0); // [obj][table][key] + duk_del_prop(ctx, -2); // [obj][table] + } + } + else + { + // Remove All FD_SET watchers for this FD + duk_del_prop(ctx, -2); // [obj][table] + } return(0); } #ifdef WIN32 @@ -2616,6 +2638,20 @@ duk_ret_t ILibDuktape_DescriptorEvents_Add(duk_context *ctx) duk_push_this(ctx); // [obj] duk_get_prop_string(ctx, -1, ILibDuktape_DescriptorEvents_Table); // [obj][table] + duk_dup(ctx, 0); // [obj][table][key] + if (duk_has_prop(ctx, -2)) // [obj][table] + { + // There's already a watcher, so let's just merge the FD_SETS + duk_dup(ctx, 0); // [obj][table][key] + duk_get_prop(ctx, -2); // [obj][table][value] + duk_get_prop_string(ctx, -1, ILibDuktape_DescriptorEvents_Options); //..[table][value][options] + if (Duktape_GetBooleanProperty(ctx, 1, "readset", 0) != 0) { duk_push_true(ctx); duk_put_prop_string(ctx, -2, "readset"); } + if (Duktape_GetBooleanProperty(ctx, 1, "writeset", 0) != 0) { duk_push_true(ctx); duk_put_prop_string(ctx, -2, "writeset"); } + if (Duktape_GetBooleanProperty(ctx, 1, "errorset", 0) != 0) { duk_push_true(ctx); duk_put_prop_string(ctx, -2, "errorset"); } + duk_pop(ctx); // [obj][table][value] + return(1); + } + duk_push_object(ctx); // [obj][table][value] duk_dup(ctx, 0); // [obj][table][value][key] duk_dup(ctx, -2); // [obj][table][value][key][value] @@ -2745,7 +2781,7 @@ void ILibDuktape_DescriptorEvents_Push(duk_context *ctx, void *chain) ((void**)link->ExtraMemoryPtr)[0] = ctx; ((void**)link->ExtraMemoryPtr)[1] = duk_get_heapptr(ctx, -1); ILibDuktape_CreateInstanceMethod(ctx, "addDescriptor", ILibDuktape_DescriptorEvents_Add, 2); - ILibDuktape_CreateInstanceMethod(ctx, "removeDescriptor", ILibDuktape_DescriptorEvents_Remove, 1); + ILibDuktape_CreateInstanceMethod(ctx, "removeDescriptor", ILibDuktape_DescriptorEvents_Remove, DUK_VARARGS); ILibDuktape_CreateInstanceMethod(ctx, "getDescriptorCount", ILibDuktape_DescriptorEvents_GetCount, 0); ILibAddToChain(chain, link); diff --git a/microscript/ILibDuktape_fs.c b/microscript/ILibDuktape_fs.c index a7bcd93..e5179f5 100644 --- a/microscript/ILibDuktape_fs.c +++ b/microscript/ILibDuktape_fs.c @@ -373,12 +373,14 @@ duk_ret_t ILibDuktape_fs_read_readsetSink(duk_context *ctx) duk_pop_2(ctx); // [DescriptorEvents][pending] if (duk_get_length(ctx, -1) == 0) { - // No more pending I/O operations, so we can cleanup the DescriptorEvents + // No more pending read I/O operations, so we can cleanup the DescriptorEvents duk_eval_string(ctx, "require('DescriptorEvents');"); // .....[DescriptorEvents] duk_get_prop_string(ctx, -1, "removeDescriptor"); // .....[DescriptorEvents][removeDescriptor] duk_swap_top(ctx, -2); // .....[removeDescriptor][this] duk_push_int(ctx, fd); // .....[removeDescriptor][this][fd] - duk_pcall_method(ctx, 1); duk_pop(ctx); // [DescriptorEvents][pending] + duk_push_object(ctx); // .....[removeDescriptor][this][fd][options] + duk_push_true(ctx); duk_put_prop_string(ctx, -2, "readset");//..[removeDescriptor][this][fd][options] + duk_pcall_method(ctx, 2); duk_pop(ctx); // [DescriptorEvents][pending] duk_eval_string(ctx, "require('fs');"); // [DescriptorEvents][pending][FS] duk_get_prop_string(ctx, -1,FS_EVENT_R_DESCRIPTORS);// [DescriptorEvents][pending][FS][table] @@ -463,7 +465,9 @@ duk_ret_t ILibDuktape_fs_write_writeset_sink(duk_context *ctx) duk_get_prop_string(ctx, -1, "removeDescriptor"); // ... [eventdescriptors][remove] duk_swap_top(ctx, -2); // ... [remove][this] duk_push_int(ctx, fd); // ... [remove][this][fd] - duk_call_method(ctx, 1); // ... [ret] + duk_push_object(ctx); // ... [remove][this][fd][options] + duk_push_true(ctx); duk_put_prop_string(ctx, -2, "writeset");//[remove][this][fd][options] + duk_call_method(ctx, 2); // ... [ret] } return(0); } diff --git a/modules/heci.js b/modules/heci.js index 5c64283..09305c7 100644 --- a/modules/heci.js +++ b/modules/heci.js @@ -299,59 +299,69 @@ function heci_create() { // Clean up all Handles and Descriptors console.log('DISCONNECT on ' + this._hashCode()); - - - // - // doIoctl() - // - if (this._descriptorEvent) + if (process.platform == 'linux') { - if (this._overlapped) { require('DescriptorEvents').removeDescriptor(this._overlapped.hEvent); } - this._descriptorEvent = null; - } - if (this._overlapped) - { - kernel32.CloseHandle(this._overlapped.hEvent); - this._overlapped = null; + if(this._descriptor != null) + { + require('DescriptorEvents').removeDescriptor(this._descriptor); + require('fs').closeSync(this._descriptor); + this._descriptor = null; + } } - // - // Read - // - if (this._rDescriptorEvent) + if (process.platform == 'win32') { - if (this._readoverlapped) { require('DescriptorEvents').removeDescriptor(this._readoverlapped.hEvent); } - this._rDescriptorEvent = null; - } - if (this._readoverlapped) - { - kernel32.CloseHandle(this._readoverlapped.hEvent); - this._readoverlapped = null; - } + // + // doIoctl() + // + if (this._descriptorEvent) + { + if (this._overlapped) { require('DescriptorEvents').removeDescriptor(this._overlapped.hEvent); } + this._descriptorEvent = null; + } + if (this._overlapped) + { + kernel32.CloseHandle(this._overlapped.hEvent); + this._overlapped = null; + } - // - // Write - // - if (this._wDescriptorEvent) - { - if (this._writeoverlapped) { require('DescriptorEvents').removeDescriptor(this._writeoverlapped.hEvent); } - this._wDescriptorEvent = null; - } - if (this._writeoverlapped) - { - kernel32.CloseHandle(this._writeoverlapped.hEvent); - this._writeoverlapped = null; - } + // + // Read + // + if (this._rDescriptorEvent) + { + if (this._readoverlapped) { require('DescriptorEvents').removeDescriptor(this._readoverlapped.hEvent); } + this._rDescriptorEvent = null; + } + if (this._readoverlapped) + { + kernel32.CloseHandle(this._readoverlapped.hEvent); + this._readoverlapped = null; + } - // - // HECI - // - if (this._descriptor) - { - kernel32.CloseHandle(this._descriptor); - this._descriptor = null; - } + // + // Write + // + if (this._wDescriptorEvent) + { + if (this._writeoverlapped) { require('DescriptorEvents').removeDescriptor(this._writeoverlapped.hEvent); } + this._wDescriptorEvent = null; + } + if (this._writeoverlapped) + { + kernel32.CloseHandle(this._writeoverlapped.hEvent); + this._writeoverlapped = null; + } + // + // HECI + // + if (this._descriptor) + { + kernel32.CloseHandle(this._descriptor); + this._descriptor = null; + } + } }; ret.doIoctl = function doIoctl(code, inputBuffer, outputBuffer, callback) { @@ -470,7 +480,7 @@ function heci_create() } } - require('fs').write(this._descriptor, chunk.buffer, ret._processWrite_linux_signaled, { metadata: 'heci.session', session: this }); + require('fs').write(this._descriptor, chunk.buffer, this._processWrite_linux_signaled, { metadata: 'heci.session', session: this }); }; ret._processWrite_linux_signaled = function _processWrite_linux_signaled(status, bytesWritten, buffer, options) { @@ -550,19 +560,8 @@ function heci_create() { // We can read more, because data is still flowing console.log('READING MORE on ' + options.session._hashCode()); - var result = kernel32.ReadFile(this.session._descriptor, this.session._readbuffer, this.session._readbuffer._size, 0, this.session._readoverlapped); - if (result.Val != 0 || result._LastError == ERROR_IO_PENDING) - { - options.session._processRead(); - return (true); - } - else - { - console.log('Sometype of error: ' + result._LastError); - this.session.push(null); - } + options.session._processRead(); } - }; ret._processRead = function _processRead() {