parser, fmt, gen: support js"string literal"#24653
Conversation
|
Connected to Huly®: V_0.6-23012 |
|
What is a JS string? |
|
JavaScript has 2 types of strings - primitive strings and String objects. Primitive strings do not have any methods or properties, while String objects have multiples of both. Basically, it is the same as the difference between C strings and V strings. |
| /* | ||
| TODO(StunxFS): Uncomment when the PR is merged |
There was a problem hiding this comment.
Why uncomment it later, and not now?
V is bootstrapped in the PR, and the test should be runnable imho as it is?
spytheman
left a comment
There was a problem hiding this comment.
Good work.
@medvednikov what do you think about it (adding syntax support for js'strings') ?
| v_current_commit_hash string // same as old C.V_CURRENT_COMMIT_HASH | ||
| assign_stmt_attr string // for `x := [1,2,3] @[freed]` | ||
|
|
||
| js_string ast.Type = ast.void_type // when `js"string literal"` is used, `js_string` will be equal to `JS.String` |
There was a problem hiding this comment.
There's no need to have the default value ast.void_type
|
For some strange reason, >> compilation failed:
jsgen error: could not generate string method voidptr_str for type 'voidptr'If I generate .js from a normal file with the same content, everything compiles and runs perfectly: $ cat main.v
fn main() {
s := js'hello V'
assert s.charAt(0) == js'h'
assert s.charAt(6) == js'V'
assert s.charCodeAt(0) == JS.Number(104)
assert s.toUpperCase() == js'HELLO V'
assert s.toLowerCase() == js'hello v'
assert s.concat(js' from JS') == js'hello V from JS'
assert s.includes(js' ') == JS.Boolean(true)
assert s.startsWith(js'hello') == JS.Boolean(true)
assert s.endsWith(js'V') == JS.Boolean(true)
}
$ v -b js main.v
$ node main.js
$ |
|
First impression... it's related to line 152 in checker.v, where you explicitly set the type to |
If I remove the default value from the field, V panics: |
|
Looks good to me. A simple change. |
|
Looks cool, but for what purposes is this necessary? |
This PR aims to add native JS string support to the compiler. It will now be possible to write
js"Hello"just like you would writec"Hello".This test ran successfully locally (using JS Browser):