transporter icon indicating copy to clipboard operation
transporter copied to clipboard

goja doesn't support extending message object

Open kiavashi opened this issue 9 years ago • 3 comments

Problem

goja doesn't support extending the object.

In goja/README.md, it specifies that some types are not extensible, but it doesn't say that for map[string]interface{}.

It seems this may be as simple as a documentation bug for either goja, transporter, or both. But if it's not a bug and the message object is indeed non-extensible then maybe it doesn't apply to the transform use case in transporter.

I was able to get around this using otto.

Stack Trace:

panic: TypeError: Host object is not extensible

goroutine 18 [running]:
github.com/compose/transporter/vendor/github.com/dop251/goja.(*Runtime).wrapJSFunc.func1(0xc4202c20a0, 0x1, 0x1, 0xc4202c20a0, 0x0, 0x1)
	/Users/JP/gocode/src/github.com/compose/transporter/vendor/github.com/dop251/goja/runtime.go:1206 +0x609
reflect.callReflect(0xc4202c2080, 0xc4202bf990)
	/usr/local/Cellar/go/1.8/libexec/src/reflect/value.go:508 +0x38f
reflect.makeFuncStub(0xc4202ad2f0, 0x2161ec0, 0xc420245fa0, 0x1943dc0, 0xc42028e028, 0x0, 0x0, 0xc42023a480, 0x0, 0xc42029a000, ...)
	/usr/local/Cellar/go/1.8/libexec/src/reflect/asm_amd64.s:17 +0x40
github.com/compose/transporter/function/gojajs.(*Goja).transformOne(0xc4201e6480, 0x215f0a0, 0xc42022a270, 0x0, 0xc4201e27a0, 0xb, 0x1)
	/Users/JP/gocode/src/github.com/compose/transporter/function/gojajs/goja.go:107 +0x4cc
github.com/compose/transporter/function/gojajs.(*Goja).Apply(0xc4201e6480, 0x215f0a0, 0xc42022a270, 0x1, 0x8, 0xc42028e000, 0x4)
	/Users/JP/gocode/src/github.com/compose/transporter/function/gojajs/goja.go:59 +0x4c
github.com/compose/transporter/pipeline.(*Node).applyTransforms(0xc4200e4b40, 0x215f0a0, 0xc42022a270, 0x0, 0xc42023a1e0, 0xc4201e88d0, 0xc420234900)
	/Users/JP/gocode/src/github.com/compose/transporter/pipeline/node.go:239 +0x373
github.com/compose/transporter/pipeline.(*Node).write(0xc4200e4b40, 0x215f0a0, 0xc42022a270, 0x0, 0x0, 0x0, 0x0)
	/Users/JP/gocode/src/github.com/compose/transporter/pipeline/node.go:205 +0x8a
github.com/compose/transporter/pipeline.(*Node).(github.com/compose/transporter/pipeline.write)-fm(0x215f0a0, 0xc42022a270, 0xb, 0xc42025ed01, 0xc420230201, 0x10)
	/Users/JP/gocode/src/github.com/compose/transporter/pipeline/node.go:201 +0x3e
github.com/compose/transporter/pipe.(*Pipe).Listen(0xc4201bca80, 0xc4202bfee0, 0xc420195680, 0xc420230210, 0x1)
	/Users/JP/gocode/src/github.com/compose/transporter/pipe/pipe.go:86 +0x1b7
github.com/compose/transporter/pipeline.(*Node).listen(0xc4200e4b40, 0x0, 0x0)
	/Users/JP/gocode/src/github.com/compose/transporter/pipeline/node.go:201 +0x1f0
github.com/compose/transporter/pipeline.(*Node).Start(0xc4200e4b40, 0x0, 0x1055cf1)
	/Users/JP/gocode/src/github.com/compose/transporter/pipeline/node.go:166 +0x246
github.com/compose/transporter/pipeline.(*Node).Start.func1(0xc4200e4b40)
	/Users/JP/gocode/src/github.com/compose/transporter/pipeline/node.go:158 +0x2b
created by github.com/compose/transporter/pipeline.(*Node).Start
	/Users/JP/gocode/src/github.com/compose/transporter/pipeline/node.go:159 +0x206

System info:

  • Transporter version: 0.3.1
  • OS X 10.11.1

Recreate

input.json

{"number":1234567890}
{"number":1234567890}

pipeline.js

'use strict';

var source = file({uri: 'input.json'});
var transform = goja({filename: 'goja-host-object-error.js'});
var sink = file({uri: 'stdout://'});

t.Source(source).Transform(transform).Save(sink);

goja-host-object-error.js

'use strict';

function transform(message) {
     // Any of the following cause the same error
    message.op = 'skip';
    message.data.something = 'value';
    message.data.number = 123;

    return message;
}

kiavashi avatar Apr 26 '17 20:04 kiavashi

you cannot use dot notation (.) when referencing the object

there's a README showing an example of how to access and manipulate the object with goja.

jipperinbham avatar Apr 26 '17 20:04 jipperinbham

ok got it working. I had tried that syntax before but it didn't work. I had to also remove 'use strict';.

but once I removed 'use strict', dot notation also works.

Updated goja-host-object-error.js

function transform(message) {
    message.op = "skip";
    message.data.something = "value";
    message.data.number = 123;

    return message;
}

Thanks!

kiavashi avatar Apr 26 '17 20:04 kiavashi

that's good to know, I'll look at updating the README soon

jipperinbham avatar Apr 29 '17 18:04 jipperinbham