Skip to content

Boolean negation/NOT prepends "null ", causing JS syntax error #61

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

Closed
willheslam opened this issue Jul 8, 2024 · 2 comments
Closed

Comments

@willheslam
Copy link
Contributor

willheslam commented Jul 8, 2024

import GLSL from "glsl-transpiler";

const glsl = new GLSL({ });

const result = glsl(`!foo;`);

result === '\nnull ! foo;'

! isn't included in the "examplary source, containing all possible tokens" inside test/api.js or test/fixture/*.glsl

Under lib/operators.js, boolean complement and negation have been commented out since
d68a9ed
so it looks like this has been this way for a while.

Uncommenting out either of the operators causes the tests to fail with

~/glsl-transpiler$ npm test

> [email protected] test
> node test/index.js

undefined:3
out[0] = a[0] ! b[0]
              ^

SyntaxError: Unexpected token '!'
    at new Function (<anonymous>)
    at createOperations (file:///home/willheslam/glsl-transpiler/lib/stdlib.js:99:30)

My guess is this is because ! and ~ are unary operators, not binary.

This gives a clue as to why null is being prepended - perhaps glsl-transpiler is treating the ! token as though it's binary, and null is the missing first operand! Hopefully it's a straight forward fix because GLSL and JS are 1:1 here, at least syntactically.

processOperation in operators.js already seems to handle unary operators like i++ and -x so hopefully it can also apply to ! and ~.

A workaround for now is just to negate manually, i.e. foo ? false : true or foo == false.

@dy
Copy link
Member

dy commented Jul 8, 2024

Ok, thanks for the report @willheslam!
I've fixed the issue, not convinced it was the best way but it works.
Cheers

@willheslam
Copy link
Contributor Author

Wow, thanks @dy, that was quick!
I've added a small follow up PR to fix a typo:
#62

Please let me know if anything needs changing.

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