From 882b6587f5d6e6dd74b49da0d51040ced5882212 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Sun, 9 Jun 2019 20:41:07 +0300 Subject: [PATCH 1/3] Buffer handling improvements in examples/extras * Allow non-plain buffers in some examples/extras. * Eventloop .write() binding buffer handling fix, relied on obsolete Duktape.Buffer. * Client socket example fix. --- examples/cmdline/duk_cmdline.c | 4 ++-- examples/eventloop/c_eventloop.js | 5 ++++- examples/eventloop/client-socket-test.js | 13 +++---------- examples/eventloop/ecma_eventloop.js | 5 ++++- examples/eventloop/fileio.c | 4 ++-- examples/eventloop/server-socket-test.js | 1 - examples/eventloop/socket.c | 2 +- extras/logging/duk_logging.c | 2 +- extras/print-alert/duk_print_alert.c | 8 ++++---- 9 files changed, 21 insertions(+), 23 deletions(-) diff --git a/examples/cmdline/duk_cmdline.c b/examples/cmdline/duk_cmdline.c index 65c61902..a7d238b9 100644 --- a/examples/cmdline/duk_cmdline.c +++ b/examples/cmdline/duk_cmdline.c @@ -234,7 +234,7 @@ static duk_ret_t wrapped_compile_execute(duk_context *ctx, void *udata) { duk_dup_top(ctx); duk_dump_function(ctx); - bc_ptr = duk_require_buffer(ctx, -1, &bc_len); + bc_ptr = duk_require_buffer_data(ctx, -1, &bc_len); filename = duk_require_string(ctx, -5); #if defined(EMSCRIPTEN) if (filename[0] == '/') { @@ -920,7 +920,7 @@ static duk_ret_t fileio_write_file(duk_context *ctx) { } len = 0; - buf = (char *) duk_to_buffer(ctx, 1, &len); + buf = (char *) duk_require_buffer_data(ctx, 1, &len); for (off = 0; off < len;) { size_t got; got = fwrite((const void *) (buf + off), 1, len - off, f); diff --git a/examples/eventloop/c_eventloop.js b/examples/eventloop/c_eventloop.js index 3001c7e2..5b3c7c93 100644 --- a/examples/eventloop/c_eventloop.js +++ b/examples/eventloop/c_eventloop.js @@ -185,5 +185,8 @@ EventLoop.setReader = function(fd, cb_read) { EventLoop.write = function(fd, data) { // This simple example doesn't have support for write blocking / draining - var rc = Socket.write(fd, Duktape.Buffer(data)); + if (typeof data === 'string') { + data = new TextEncoder().encode(data); + } + var rc = Socket.write(fd, data); } diff --git a/examples/eventloop/client-socket-test.js b/examples/eventloop/client-socket-test.js index 08bc7cd4..d08b875f 100644 --- a/examples/eventloop/client-socket-test.js +++ b/examples/eventloop/client-socket-test.js @@ -1,23 +1,16 @@ - var HOST = 'localhost'; var PORT = 80; -var EXIT_TIMEOUT = 300e3; - -print('automatic exit after ' + (EXIT_TIMEOUT / 1e3) + ' seconds'); -setTimeout(function () { - print('exit timer'); - EventLoop.requestExit(); -}, EXIT_TIMEOUT); EventLoop.connect(HOST, PORT, function (fd) { print('connected to ' + HOST + ':' + PORT + ', fd', fd); EventLoop.setReader(fd, function (fd, data) { print('read from fd', fd); - print(data); - EventLoop.close(fd); + print(new TextDecoder().decode(data)); + // Read until completion, socket is closed by server. }); EventLoop.write(fd, "GET / HTTP/1.1\r\n" + "Host: " + HOST + "\r\n" + "User-Agent: client-socket-test.js\r\n" + + "Connection: close\r\n" + "\r\n"); }); diff --git a/examples/eventloop/ecma_eventloop.js b/examples/eventloop/ecma_eventloop.js index 6ef91e4a..21e18f0b 100644 --- a/examples/eventloop/ecma_eventloop.js +++ b/examples/eventloop/ecma_eventloop.js @@ -355,7 +355,10 @@ EventLoop.setReader = function(fd, cb_read) { EventLoop.write = function(fd, data) { // This simple example doesn't have support for write blocking / draining - var rc = Socket.write(fd, Duktape.Buffer(data)); + if (typeof data === 'string') { + data = new TextEncoder().encode(data); + } + var rc = Socket.write(fd, data); } /* diff --git a/examples/eventloop/fileio.c b/examples/eventloop/fileio.c index bcf2b265..974e2d7e 100644 --- a/examples/eventloop/fileio.c +++ b/examples/eventloop/fileio.c @@ -55,7 +55,7 @@ void fileio_push_file_buffer(duk_context *ctx, const char *filename) { /* Push file as a string. */ void fileio_push_file_string(duk_context *ctx, const char *filename) { fileio_push_file_buffer(ctx, filename); - if (duk_is_buffer(ctx, -1)) { + if (duk_is_buffer_data(ctx, -1)) { duk_buffer_to_string(ctx, -1); } } @@ -63,7 +63,7 @@ void fileio_push_file_string(duk_context *ctx, const char *filename) { static int fileio_readfile(duk_context *ctx) { const char *filename = duk_to_string(ctx, 0); fileio_push_file_buffer(ctx, filename); - if (!duk_is_buffer(ctx, -1)) { + if (!duk_is_buffer_data(ctx, -1)) { return DUK_RET_ERROR; } return 1; diff --git a/examples/eventloop/server-socket-test.js b/examples/eventloop/server-socket-test.js index 68510af0..e3c00864 100644 --- a/examples/eventloop/server-socket-test.js +++ b/examples/eventloop/server-socket-test.js @@ -1,4 +1,3 @@ - var HOST = 'localhost' var PORT = 12345; var EXIT_TIMEOUT = 300e3; diff --git a/examples/eventloop/socket.c b/examples/eventloop/socket.c index 055e3802..9c391fa1 100644 --- a/examples/eventloop/socket.c +++ b/examples/eventloop/socket.c @@ -250,7 +250,7 @@ static int socket_write(duk_context *ctx) { size_t len; ssize_t rc; - data = duk_to_buffer(ctx, 1, &len); + data = duk_require_buffer_data(ctx, 1, &len); /* MSG_NOSIGNAL: avoid SIGPIPE */ #if defined(__APPLE__) diff --git a/extras/logging/duk_logging.c b/extras/logging/duk_logging.c index 9be2c1c2..1b91d95e 100644 --- a/extras/logging/duk_logging.c +++ b/extras/logging/duk_logging.c @@ -109,7 +109,7 @@ static duk_ret_t duk__logger_prototype_raw(duk_context *ctx) { const char *data; duk_size_t data_len; - data = (const char *) duk_require_buffer(ctx, 0, &data_len); + data = (const char *) duk_require_buffer_data(ctx, 0, &data_len); fwrite((const void *) data, 1, data_len, stderr); fputc((int) '\n', stderr); #if defined(DUK_LOGGING_FLUSH) diff --git a/extras/print-alert/duk_print_alert.c b/extras/print-alert/duk_print_alert.c index ec1259ff..5ea2fd50 100644 --- a/extras/print-alert/duk_print_alert.c +++ b/extras/print-alert/duk_print_alert.c @@ -23,8 +23,8 @@ static duk_ret_t duk__print_alert_helper(duk_context *ctx, FILE *fh) { * arguments, join them with a single space, and append a newline. */ - if (nargs == 1 && duk_is_buffer(ctx, 0)) { - buf = (const duk_uint8_t *) duk_get_buffer(ctx, 0, &sz_buf); + if (nargs == 1 && duk_is_buffer_data(ctx, 0)) { + buf = (const duk_uint8_t *) duk_get_buffer_data(ctx, 0, &sz_buf); fwrite((const void *) buf, 1, (size_t) sz_buf, fh); } else { duk_push_string(ctx, " "); @@ -57,8 +57,8 @@ static duk_ret_t duk__print_alert_helper(duk_context *ctx, FILE *fh) { * arguments, join them with a single space, and append a newline. */ - if (nargs == 1 && duk_is_buffer(ctx, 0)) { - buf = (const duk_uint8_t *) duk_get_buffer(ctx, 0, &sz_buf); + if (nargs == 1 && duk_is_buffer_data(ctx, 0)) { + buf = (const duk_uint8_t *) duk_get_buffer_data(ctx, 0, &sz_buf); } else if (nargs > 0) { duk_idx_t i; duk_size_t sz_str; From 453f0ff5ee49a47d80638e1d22539099b4ca18cd Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Sun, 9 Jun 2019 22:33:55 +0300 Subject: [PATCH 2/3] Test fixes for changed 'duk' behavior --- tests/ecmascript/test-bi-nodejs-buffer-valueof.js | 2 +- ...test-bi-plain-buffer-nodejs-buffer-proto-methods.js | 5 ++++- .../ecmascript/test-bi-plain-buffer-object-methods.js | 10 ++++++++-- tests/ecmascript/test-bi-typedarray-view-basic.js | 2 +- tests/ecmascript/test-dev-buffer-interop.js | 6 +++--- tests/ecmascript/test-dev-typedarray-view-1.js | 6 +++--- 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tests/ecmascript/test-bi-nodejs-buffer-valueof.js b/tests/ecmascript/test-bi-nodejs-buffer-valueof.js index 6a2bf831..62301977 100644 --- a/tests/ecmascript/test-bi-nodejs-buffer-valueof.js +++ b/tests/ecmascript/test-bi-nodejs-buffer-valueof.js @@ -26,7 +26,7 @@ function nodejsBufferValueOfTest() { // Object.prototype.valueOf() returns the buffer as is b = new Buffer('ABCDEFGH'); - print(b.valueOf()); + print('' + b.valueOf()); print(typeof b.valueOf()); print(b.valueOf() === b); } diff --git a/tests/ecmascript/test-bi-plain-buffer-nodejs-buffer-proto-methods.js b/tests/ecmascript/test-bi-plain-buffer-nodejs-buffer-proto-methods.js index bce689a0..f80b2d4b 100644 --- a/tests/ecmascript/test-bi-plain-buffer-nodejs-buffer-proto-methods.js +++ b/tests/ecmascript/test-bi-plain-buffer-nodejs-buffer-proto-methods.js @@ -28,6 +28,7 @@ false false true - fill +false [object Uint8Array] |6162111111111111116a6b6c6d6e6f70| - copy, source plain buffer, target Node.js Buffer @@ -114,7 +115,9 @@ function nodejsBufferPrototypeMethodTest() { resetValues(); print('- fill'); - print(Buffer.prototype.fill.call(pb, 0x11, 2, 9)); + var res = Buffer.prototype.fill.call(pb, 0x11, 2, 9); + print(res === pb); // false, promoted to object + print(Object.prototype.toString.call(res)); print(Duktape.enc('jx', pb)); resetValues(); diff --git a/tests/ecmascript/test-bi-plain-buffer-object-methods.js b/tests/ecmascript/test-bi-plain-buffer-object-methods.js index f4f456e8..a59c12a2 100644 --- a/tests/ecmascript/test-bi-plain-buffer-object-methods.js +++ b/tests/ecmascript/test-bi-plain-buffer-object-methods.js @@ -40,10 +40,12 @@ noSuch undefined 1 true 100 255 - defineProperty +false [object Uint8Array] 99 undefined - defineProperties +false [object Uint8Array] 99 undefined @@ -133,13 +135,17 @@ function objectMethodTest() { resetValues(); print('- defineProperty'); - print(Object.defineProperty(pb, 'newProp', { value: 1234 })); + var ret = Object.defineProperty(pb, 'newProp', { value: 1234 }); + print(pb === ret); // no match because of upgrade + print(Object.prototype.toString.call(ret)); print(pb[2]); print(pb.newProp); resetValues(); print('- defineProperties'); - print(Object.defineProperties(pb, { newProp: { value: 1234 } })); + var ret = Object.defineProperties(pb, { newProp: { value: 1234 } }); + print(pb === ret); // no match because of upgrade + print(Object.prototype.toString.call(ret)); print(pb[2]); print(pb.newProp); diff --git a/tests/ecmascript/test-bi-typedarray-view-basic.js b/tests/ecmascript/test-bi-typedarray-view-basic.js index da09c662..14df403c 100644 --- a/tests/ecmascript/test-bi-typedarray-view-basic.js +++ b/tests/ecmascript/test-bi-typedarray-view-basic.js @@ -26,7 +26,7 @@ function basicViewTest() { var v, v2; var i; - print(b); + print(Object.prototype.toString.call(b)); print(b.byteLength); print(bufferToHex(b)); diff --git a/tests/ecmascript/test-dev-buffer-interop.js b/tests/ecmascript/test-dev-buffer-interop.js index 801d5771..0757a124 100644 --- a/tests/ecmascript/test-dev-buffer-interop.js +++ b/tests/ecmascript/test-dev-buffer-interop.js @@ -46,8 +46,8 @@ function plainBufferTest() { d = getPlainBuffer(b); print(typeof a, typeof b, typeof c, typeof d); print(c === d); // not the same - print(a); - print(b); + print('' + a); + print('' + b); // ArrayBuffer constructor doesn't accept another buffer so there's // no buffer copy/embed behavior to test at the moment. @@ -143,7 +143,7 @@ function nodejsConcatTest() { b3 = new Uint8Array([ 0x61, 0x62, 0x63, 0x64, 0x65 ]).subarray(1); res = Buffer.concat([ b1, b2, b3 ]); - print(res); + print('' + res); } try { diff --git a/tests/ecmascript/test-dev-typedarray-view-1.js b/tests/ecmascript/test-dev-typedarray-view-1.js index 51263493..a458a49c 100644 --- a/tests/ecmascript/test-dev-typedarray-view-1.js +++ b/tests/ecmascript/test-dev-typedarray-view-1.js @@ -24,15 +24,15 @@ true function test() { var b = new ArrayBuffer(16); - print(b) + print(Object.prototype.toString.call(b)); print(b.length, b.byteLength, b.byteOffset); var v = new Uint32Array(b); - print(v); + print(Object.prototype.toString.call(v)); print(v.length, v.byteLength, v.byteOffset); var w = v.subarray(1) - print(w) + print(Object.prototype.toString.call(w)); print(w.length, w.byteLength, w.byteOffset); print(ArrayBuffer.isView(b)); From f3d42f8af648d34f6dc4604e705b3c71c740a444 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Sun, 9 Jun 2019 22:17:28 +0300 Subject: [PATCH 3/3] Releases: examples/extras buffer improvements --- RELEASES.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/RELEASES.rst b/RELEASES.rst index 56a7fbe7..ebd9e599 100644 --- a/RELEASES.rst +++ b/RELEASES.rst @@ -3425,6 +3425,12 @@ Planned compiled functions), context dump array for duk_push_context_dump(), and error tracedata (GH-2089) +* Accept non-plain buffer types in some examples/extras (cmdline, eventloop, + logging, print-alert) (GH-2107) + +* Fix eventloop example .write() method buffer handling which relied on + Duktape.Buffer, also fix a related TCP client example bug (GH-2107) + * Fix incorrect handling of zero-length dynamic buffer in base-64 fast path decoder (GH-2027, GH-2088)