hxnodejs icon indicating copy to clipboard operation
hxnodejs copied to clipboard

Printing the result of hxToBytes will hang the runtime

Open kevinresol opened this issue 5 years ago • 2 comments

class Main {
	static function main() {
		var buf = js.node.Buffer.from('foo');
		var bytes = buf.hxToBytes();
		trace(bytes);
	}
}

Run the above code with node 12+, it will hang forever. node 10 or below is fine though.

However, when run with a debugger (vscode), it will crash with the following error.

Waiting for the debugger to disconnect...
internal/util/inspect.js:1249
  throw err;
  ^

SyntaxError: Invalid regular expression: /(.{2})/: Stack overflow
    at String.replace (<anonymous>)
    at Buffer.inspect (buffer.js:814:41)
    at formatValue (internal/util/inspect.js:693:31)
    at formatProperty (internal/util/inspect.js:1558:11)
    at formatRaw (internal/util/inspect.js:933:9)
    at formatValue (internal/util/inspect.js:721:10)
    at inspect (internal/util/inspect.js:264:10)
    at Buffer.inspect (buffer.js:831:14)
    at formatValue (internal/util/inspect.js:693:31)
    at formatProperty (internal/util/inspect.js:1558:11)
Process exited with code 1

It could be fixed by commentting out a line in the gennerated js:

js_node_buffer__$Buffer_Helper.bytesOfBuffer = function(b) {
	var o = Object.create(haxe_io_Bytes.prototype);
	o.length = b.byteLength;
	// o.b = b; <-- commentting this line will fix the hang
	b.bufferValue = b;
	b.hxBytes = o;
	b.bytes = b;
	return o;
};

kevinresol avatar Jul 21 '20 14:07 kevinresol

This is the minimum js I reduced:

var haxe_io_Bytes = function() { };
var b = require("buffer").Buffer.from('foo');
var o = Object.create(haxe_io_Bytes.prototype);
o.l = b.byteLength;
o.b = b;
b.b = b;
b.o = o;
console.log('',o);

Apparently all the 4 assignments are required to cause the hang.

I wonder why hxToBytes has to generate so many recursive references?

kevinresol avatar Jul 21 '20 14:07 kevinresol

I wonder why hxToBytes has to generate so many recursive references?

I think this comes from haxe.io.Bytes, but I'm not sure why these references are there either, maybe this could be cleaned up. Probably it has something to do with re-using the underlying bytes somehow.

nadako avatar Jul 21 '20 15:07 nadako