Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] javascript UDFs receive json arguments as empty objects #3231

Closed
dark-ether opened this issue Dec 1, 2021 · 3 comments · Fixed by #3451
Closed

[bug] javascript UDFs receive json arguments as empty objects #3231

dark-ether opened this issue Dec 1, 2021 · 3 comments · Fixed by #3451
Assignees
Labels
bug tested This issue has been QA tested by someone other than the developer.

Comments

@dark-ether
Copy link
Contributor

Describe the Bug

if you have a js UDF which takes a json it will receive a empty object instead of whatever argument you give it

To Reproduce

  1. create a token name lib:test and set allow uri acess
  2. create test macro with
function test(arrayOfObject){	
MapTool.chat.broadcast(JSON.stringify(arrayOfObject));
return arrayOfObject;
}

MTScript.registerMacro("test",test);
  1. create define test macro with [r:js.evalURI("test","lib://test/macro/test")] command
  2. create use test with
[h:objectWithArray = json.append("[]","array1","dsdas,dsd","array2",4)]
[h:js.test(objectWithArray)]
  1. see that ouput to chat is empty object instead of array with 4 values

Expected Behaviour

arguments that the js UDF receives should be the ones i use, and not always an empty object

Screenshots

No response

MapTool Info

1.11.0

Desktop

arch linux

Additional Context

No response

@dark-ether dark-ether added the bug label Dec 1, 2021
@perkinslr
Copy link
Contributor

The object passed in to the js UDF is a com.google.gson.JsonArray, which is a mostly-opaque type to javascript. Calling JSON.stringify on it returns the "own properties" of the argument, which is empty, hence the empty object as the output.

Currently, the only exposed interface for com.google.gson.* is string conversion, with complex types then requiring deserialization.

function test(arrayOfObject){	
  let myArray = JSON.parse(new String(arrayOfObject))
  MapTool.chat.broadcast(JSON.stringify(myArray));
  return myArray;
}

MTScript.registerMacro("test",test);

Until a week or so ago, I was under the impression that MapTool kept json data internally as strings, so I did not handle gson data coming in to js UDFs. I will see if I can adjust the GraalVM-js security policy to allow full access to gson data. Or failing that, handle converting gson data to graalvm-js data automatically.

@cwisniew
Copy link
Member

cwisniew commented Dec 2, 2021

I think converting between gson and graalvm-js is best way to go, I would hate to get into the position where a upgrade to gson (or change to some other library) would break people's macros.

@Phergus Phergus added the tested This issue has been QA tested by someone other than the developer. label Jul 12, 2022
@Phergus
Copy link
Contributor

Phergus commented Jul 12, 2022

Tested using test case described above. Working as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug tested This issue has been QA tested by someone other than the developer.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants