Skip to content

Commit

Permalink
Use native teardown API
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Nov 26, 2024
1 parent d82060a commit dbe6d26
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
4 changes: 0 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ BinPackParameters: false
ContinuationIndentWidth: 2
ColumnLimit: 0
SpaceAfterCStyleCast: true
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterFunctionDeclarationName: true
AfterFunctionDefinitionName: true
68 changes: 45 additions & 23 deletions binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ typedef struct {
js_ref_t *on_end;
js_ref_t *on_read;
js_ref_t *on_close;

js_deferred_teardown_t *teardown;
bool exiting;
} bare_pipe_t;

enum {
Expand All @@ -35,7 +38,7 @@ enum {
typedef utf8_t bare_pipe_path_t[4096 + 1 /* NULL */];

static void
bare_pipe__on_connection (uv_stream_t *server, int status) {
bare_pipe__on_connection(uv_stream_t *server, int status) {
int err;

bare_pipe_t *pipe = (bare_pipe_t *) server;
Expand Down Expand Up @@ -79,7 +82,7 @@ bare_pipe__on_connection (uv_stream_t *server, int status) {
}

static void
bare_pipe__on_connect (uv_connect_t *req, int status) {
bare_pipe__on_connect(uv_connect_t *req, int status) {
int err;

bare_pipe_t *pipe = (bare_pipe_t *) req->data;
Expand Down Expand Up @@ -123,7 +126,7 @@ bare_pipe__on_connect (uv_connect_t *req, int status) {
}

static void
bare_pipe__on_write (uv_write_t *req, int status) {
bare_pipe__on_write(uv_write_t *req, int status) {
int err;

bare_pipe_t *pipe = (bare_pipe_t *) req->data;
Expand Down Expand Up @@ -167,7 +170,7 @@ bare_pipe__on_write (uv_write_t *req, int status) {
}

static void
bare_pipe__on_shutdown (uv_shutdown_t *req, int status) {
bare_pipe__on_shutdown(uv_shutdown_t *req, int status) {
int err;

bare_pipe_t *pipe = (bare_pipe_t *) req->data;
Expand Down Expand Up @@ -211,7 +214,7 @@ bare_pipe__on_shutdown (uv_shutdown_t *req, int status) {
}

static void
bare_pipe__on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
bare_pipe__on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
if (nread == UV_EOF) nread = 0;
else if (nread == 0) return;

Expand Down Expand Up @@ -264,13 +267,15 @@ bare_pipe__on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
}

static void
bare_pipe__on_close (uv_handle_t *handle) {
bare_pipe__on_close(uv_handle_t *handle) {
int err;

bare_pipe_t *pipe = (bare_pipe_t *) handle;

js_env_t *env = pipe->env;

if (pipe->exiting) goto finalize;

js_handle_scope_t *scope;
err = js_open_handle_scope(env, &scope);
assert(err == 0);
Expand All @@ -285,6 +290,13 @@ bare_pipe__on_close (uv_handle_t *handle) {

js_call_function(env, ctx, on_close, 0, NULL, NULL);

err = js_close_handle_scope(env, scope);
assert(err == 0);

finalize:
err = js_finish_deferred_teardown_callback(pipe->teardown);
assert(err == 0);

err = js_delete_reference(env, pipe->on_connection);
assert(err == 0);

Expand All @@ -305,20 +317,26 @@ bare_pipe__on_close (uv_handle_t *handle) {

err = js_delete_reference(env, pipe->ctx);
assert(err == 0);
}

err = js_close_handle_scope(env, scope);
assert(err == 0);
static void
bare_pipe__on_teardown(js_deferred_teardown_t *handle, void *data) {
bare_pipe_t *pipe = (bare_pipe_t *) data;

pipe->exiting = true;

uv_close((uv_handle_t *) &pipe->handle, bare_pipe__on_close);
}

static void
bare_pipe__on_alloc (uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
bare_pipe__on_alloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
bare_pipe_t *pipe = (bare_pipe_t *) handle;

*buf = pipe->read;
}

static js_value_t *
bare_pipe_init (js_env_t *env, js_callback_info_t *info) {
bare_pipe_init(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 8;
Expand Down Expand Up @@ -346,6 +364,7 @@ bare_pipe_init (js_env_t *env, js_callback_info_t *info) {
}

pipe->env = env;
pipe->exiting = false;

err = js_get_typedarray_info(env, argv[0], NULL, (void **) &pipe->read.base, (size_t *) &pipe->read.len, NULL, NULL);
assert(err == 0);
Expand All @@ -371,11 +390,14 @@ bare_pipe_init (js_env_t *env, js_callback_info_t *info) {
err = js_create_reference(env, argv[7], 1, &pipe->on_close);
assert(err == 0);

err = js_add_deferred_teardown_callback(env, bare_pipe__on_teardown, (void *) pipe, &pipe->teardown);
assert(err == 0);

return handle;
}

static js_value_t *
bare_pipe_connect (js_env_t *env, js_callback_info_t *info) {
bare_pipe_connect(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 2;
Expand Down Expand Up @@ -409,7 +431,7 @@ bare_pipe_connect (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_open (js_env_t *env, js_callback_info_t *info) {
bare_pipe_open(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 2;
Expand Down Expand Up @@ -453,7 +475,7 @@ bare_pipe_open (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_bind (js_env_t *env, js_callback_info_t *info) {
bare_pipe_bind(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 3;
Expand Down Expand Up @@ -494,7 +516,7 @@ bare_pipe_bind (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_accept (js_env_t *env, js_callback_info_t *info) {
bare_pipe_accept(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 2;
Expand Down Expand Up @@ -524,7 +546,7 @@ bare_pipe_accept (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_writev (js_env_t *env, js_callback_info_t *info) {
bare_pipe_writev(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 2;
Expand Down Expand Up @@ -574,7 +596,7 @@ bare_pipe_writev (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_end (js_env_t *env, js_callback_info_t *info) {
bare_pipe_end(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand Down Expand Up @@ -604,7 +626,7 @@ bare_pipe_end (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_resume (js_env_t *env, js_callback_info_t *info) {
bare_pipe_resume(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand Down Expand Up @@ -632,7 +654,7 @@ bare_pipe_resume (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_pause (js_env_t *env, js_callback_info_t *info) {
bare_pipe_pause(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand Down Expand Up @@ -660,7 +682,7 @@ bare_pipe_pause (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_close (js_env_t *env, js_callback_info_t *info) {
bare_pipe_close(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand All @@ -681,7 +703,7 @@ bare_pipe_close (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_ref (js_env_t *env, js_callback_info_t *info) {
bare_pipe_ref(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand All @@ -702,7 +724,7 @@ bare_pipe_ref (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_unref (js_env_t *env, js_callback_info_t *info) {
bare_pipe_unref(js_env_t *env, js_callback_info_t *info) {
int err;

size_t argc = 1;
Expand All @@ -723,7 +745,7 @@ bare_pipe_unref (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_pipe (js_env_t *env, js_callback_info_t *info) {
bare_pipe_pipe(js_env_t *env, js_callback_info_t *info) {
int err;

uv_file fds[2];
Expand Down Expand Up @@ -752,7 +774,7 @@ bare_pipe_pipe (js_env_t *env, js_callback_info_t *info) {
}

static js_value_t *
bare_pipe_exports (js_env_t *env, js_value_t *exports) {
bare_pipe_exports(js_env_t *env, js_value_t *exports) {
int err;

#define V(name, fn) \
Expand Down
21 changes: 0 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ module.exports = exports = class Pipe extends Duplex {
} else if (typeof path === 'string') {
this.connect(path)
}

Pipe._pipes.add(this)
}

get connecting() {
Expand Down Expand Up @@ -202,15 +200,13 @@ module.exports = exports = class Pipe extends Duplex {
if (this._state & constants.state.CLOSING) return
this._state |= constants.state.CLOSING
binding.close(this._handle)
Pipe._pipes.delete(this)
}

_destroy(err, cb) {
if (this._state & constants.state.CLOSING) return cb(err)
this._state |= constants.state.CLOSING
this._pendingDestroy = cb
binding.close(this._handle)
Pipe._pipes.delete(this)
}

_continueOpen(err) {
Expand Down Expand Up @@ -309,8 +305,6 @@ module.exports = exports = class Pipe extends Duplex {

this._continueOpen()
}

static _pipes = new Set()
}

exports.Pipe = exports
Expand Down Expand Up @@ -343,8 +337,6 @@ exports.Server = class PipeServer extends EventEmitter {
this._handle = null

if (onconnection) this.on('connection', onconnection)

PipeServer._servers.add(this)
}

get listening() {
Expand Down Expand Up @@ -440,7 +432,6 @@ exports.Server = class PipeServer extends EventEmitter {
if (this._state & constants.state.CLOSING && this._connections.size === 0) {
if (this._handle !== null) binding.close(this._handle)
else queueMicrotask(() => this.emit('close'))
PipeServer._servers.delete(this)
}
}

Expand Down Expand Up @@ -491,8 +482,6 @@ exports.Server = class PipeServer extends EventEmitter {
if (err) this.emit('error', err)
else this.emit('close')
}

static _servers = new Set()
}

exports.constants = constants
Expand All @@ -516,16 +505,6 @@ exports.createServer = function createServer(opts, onconnection) {
return new exports.Server(opts, onconnection)
}

Bare.on('exit', () => {
for (const pipe of Pipe._pipes) {
pipe.destroy()
}

for (const server of Server._servers) {
server.close()
}
})

const empty = Buffer.alloc(0)

function noop() {}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bare-pipe",
"version": "3.3.8",
"version": "4.0.0-0",
"description": "Native I/O pipes for JavaScript",
"exports": {
".": "./index.js",
Expand Down Expand Up @@ -30,6 +30,9 @@
"url": "https://github.com/holepunchto/bare-pipe/issues"
},
"homepage": "https://github.com/holepunchto/bare-pipe#readme",
"engines": {
"bare": ">=1.7.0"
},
"dependencies": {
"bare-events": "^2.0.0",
"bare-stream": "^2.0.0"
Expand Down

0 comments on commit dbe6d26

Please sign in to comment.