Browse Source

Merge pull request #2107 from svaarala/examples-extras-nonplain-buffers

Accept non-plain buffers in some examples/extras, eventloop example buffer fixes
pull/2110/head
Sami Vaarala 5 years ago
committed by GitHub
parent
commit
d724eb08ab
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      RELEASES.rst
  2. 4
      examples/cmdline/duk_cmdline.c
  3. 5
      examples/eventloop/c_eventloop.js
  4. 13
      examples/eventloop/client-socket-test.js
  5. 5
      examples/eventloop/ecma_eventloop.js
  6. 4
      examples/eventloop/fileio.c
  7. 1
      examples/eventloop/server-socket-test.js
  8. 2
      examples/eventloop/socket.c
  9. 2
      extras/logging/duk_logging.c
  10. 8
      extras/print-alert/duk_print_alert.c
  11. 2
      tests/ecmascript/test-bi-nodejs-buffer-valueof.js
  12. 5
      tests/ecmascript/test-bi-plain-buffer-nodejs-buffer-proto-methods.js
  13. 10
      tests/ecmascript/test-bi-plain-buffer-object-methods.js
  14. 2
      tests/ecmascript/test-bi-typedarray-view-basic.js
  15. 6
      tests/ecmascript/test-dev-buffer-interop.js
  16. 6
      tests/ecmascript/test-dev-typedarray-view-1.js

6
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)

4
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);

5
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);
}

13
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");
});

5
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);
}
/*

4
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;

1
examples/eventloop/server-socket-test.js

@ -1,4 +1,3 @@
var HOST = 'localhost'
var PORT = 12345;
var EXIT_TIMEOUT = 300e3;

2
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__)

2
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)

8
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;

2
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);
}

5
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();

10
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);

2
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));

6
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 {

6
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));

Loading…
Cancel
Save