Skip to content

Commit

Permalink
parser, fmt: parse methods on JS interfaces, write JS method bodies (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Mar 25, 2024
1 parent 842efdf commit e7ed073
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion vlib/v/fmt/fmt.v
Expand Up @@ -1104,7 +1104,7 @@ fn (mut f Fmt) fn_body(node ast.FnDecl) {
defer {
f.fn_scope = prev_fn_scope
}
if node.language == .v {
if node.language == .v || (node.is_method && node.language == .js) {
if !node.no_body {
f.write(' {')
pre_comments := node.comments.filter(it.pos.pos < node.name_pos.pos)
Expand Down
11 changes: 11 additions & 0 deletions vlib/v/fmt/tests/interface_js_interop_method_keep.vv
@@ -0,0 +1,11 @@
pub interface JS.CanvasRenderingContext2D {
setLineDash(JS.Array)
}

pub fn (ctx JS.CanvasRenderingContext2D) set_line_dash(arr []f64) {
tmp := JS.Array.prototype.constructor()
for x in arr {
tmp.push(JS.Number(x))
}
ctx.setLineDash(tmp)
}
2 changes: 1 addition & 1 deletion vlib/v/parser/fn.v
Expand Up @@ -577,7 +577,7 @@ run them via `v file.v` instead',
mut stmts := []ast.Stmt{}
body_start_pos := p.tok.pos()
if p.tok.kind == .lcbr {
if language != .v {
if language != .v && !(language == .js && type_sym.info is ast.Interface) {
p.error_with_pos('interop functions cannot have a body', body_start_pos)
}
p.inside_fn = true
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/parser/tests/interop_js_func_body_err.out
@@ -0,0 +1,3 @@
vlib/v/parser/tests/interop_js_func_body_err.vv:1:43: error: interop functions cannot have a body
1 | fn JS.state_is_ready_on_leave(retval int) {}
| ^
1 change: 1 addition & 0 deletions vlib/v/parser/tests/interop_js_func_body_err.vv
@@ -0,0 +1 @@
fn JS.state_is_ready_on_leave(retval int) {}

0 comments on commit e7ed073

Please sign in to comment.