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

jq flags -r effect seems can't cancel between calls ? #15

Open
yurenchen000 opened this issue Feb 4, 2020 · 11 comments
Open

jq flags -r effect seems can't cancel between calls ? #15

yurenchen000 opened this issue Feb 4, 2020 · 11 comments

Comments

@yurenchen000
Copy link
Collaborator

yurenchen000 commented Feb 4, 2020

version: release-0.5.1 jq.wasm.js

run in chrome F12.

// ------ worked well
> jq.raw('{"a":"hello"}', '.a')
< ""hello""

> jq.raw('{"a":"hello"}', '.a', ['-c'])
< ""hello""

// ------ once call with `-r`, 
> jq.raw('{"a":"hello"}', '.a', ['-r'])
< "hello"

// ------ somethings err? following call can't cancel `-r` flag
> jq.raw('{"a":"hello"}', '.a')
< "hello"

> jq.raw('{"a":"hello"}', '.a', ['-c'])
< "hello"

then refresh page,
and worked well again.

@fiatjaf
Copy link
Owner

fiatjaf commented Feb 4, 2020

Oh, you mean once you pass a flag it keeps being applied to next calls?

@yurenchen000
Copy link
Collaborator Author

Oh, you mean once you pass a flag it keeps being applied to next calls?

yes

@yurenchen000 yurenchen000 changed the title jq flags -r seems can't cancel ? jq flags -r effect seems can't cancel between calls ? Feb 5, 2020
@yurenchen000
Copy link
Collaborator Author

yurenchen000 commented Feb 6, 2020

seems some parse error state also persist:

// run in linux chrome 74.0.3729.131 64bit
// linux/win10 firefox 72.0.2 64bit also occur

> jq.raw('{"a":"hello"}', '.a')
< ""hello""

// try parse err
> jq.raw('{"a":,"hello"}', '.a')
< jq.wasm.js:6 Uncaught parse error: Expected value before ',' at line 1, column 6

// following call is sick, infected
> jq.raw('{"a":"hello"}', '.a')
< ""

@fiatjaf
Copy link
Owner

fiatjaf commented Feb 6, 2020

This is very odd. I took a look at it but can't see where the error is. Maybe we need an Emscripten specialist.

@yurenchen000
Copy link
Collaborator Author

yurenchen000 commented Feb 6, 2020

// init bin arr once
var res = await fetch('jq_github/jq.wasm.wasm')
var arr = await res.arrayBuffer()


// reload asm when necessary
var mod = await WebAssembly.instantiate(arr, info) //info from jq.wasm.js createWasm()
Module.asm = mod.instance.exports   //Module from jq.wasm.js 

I use this code reload the asm, which can fix the parse err state,
but -r effect still exist.

@yurenchen000
Copy link
Collaborator Author

yurenchen000 commented Feb 6, 2020

> jq.raw('\n12', '.')
< "12"

> jq.raw('123\n', '.')
< "123"

>  jq.raw('1234', '.')
< ""

input end with '\n' also fall into unexpected state,
and no any err detect/throw out.

// reload asm also can fix this, but we can't detect this state normally,
// unless check with an additional test call.

@yurenchen000
Copy link
Collaborator Author

yurenchen000 commented Feb 6, 2020

had a glance at jq c code.

jq@341a5fc main.c:147

static int options = 0;

I guess the global static var options ( without re-init with 0 )
cause flags remain every time we call main()

maybe need clean the stack & heap before next time call main

@fiatjaf
Copy link
Owner

fiatjaf commented Feb 7, 2020

Wow, I guess we've found our Emscripten specialist!

I have no idea of how to do that. It would be great if you could open a pull request.

@yurenchen000
Copy link
Collaborator Author

yurenchen000 commented Jul 9, 2023

sorry for late response, busy year after year.

options var residue

AFAIK, wasm no api to reset stack or global var.
so as workaround, patched the src/main.c to reset the options.


why?

-r output raw strings, not JSON texts

but there no way to cancel it's effect, once called with -r:

> jq.raw('"abc"', '.')
'"abc"'
> jq.raw('"abc"', '.', ['-r'])
'abc'

//--- jq into bad state: we didn't pass '-r' anymore, but it still effect
//    no way to get non-raw output (json string)
> jq.raw('"abc"', '.')
'abc'
> jq.raw('"abc"', '.', ['-c'])
'abc'

cause in jq/src/main.c
options is a global var, and only bit set, no bit clear operate in main
https://github.com/jqlang/jq/blob/341a5fcab3/src/main.c#L333


the RP here
#27

@yurenchen000
Copy link
Collaborator Author

yurenchen000 commented Jul 9, 2023

stdin broken after error

first mentioned here:
#15 (comment)

> jq.raw('123', '.')
'123'
> jq.raw(',123', '.')
jq.wasm.js:6 Uncaught parse error: Expected value before ',' at line 1, column 1

raw @ jq.wasm.js:6
(anonymous) @ VM824:1

//----- jq into a bad state, no response anymore //I found it's about stdin broken
> jq.raw('123', '.')
''
> jq.raw('123', '.')
''

cause after main, c may close stdin. (and next call it read nothing)
we use explicit position arg /dev/stdin, so that c main will open it at every call.


ref: man 3 exit

All open stdio(3) streams are flushed and closed


I made a PR here to fix stdin,
also clear un-closed FS.streams[] after main call.

#28

@yurenchen000
Copy link
Collaborator Author

yurenchen000 commented Jul 9, 2023

about stdout, stderr

current code only show err when stdout got nothing
https://github.com/fiatjaf/jq-web/blob/0.5.1/post.js#L23


but, sometimes it can output both stdout,stderr.
then the err msg can't see in jq-web

$ echo '"abc",' | jq
"abc"
parse error: Expected value before ',' at line 1, column 6

a PR here,
I didn't do much, just print to console on demand

#29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants