From 6c32c544e1a418d11d6435c895057a0c1233cffc Mon Sep 17 00:00:00 2001 From: playX Date: Sat, 13 Nov 2021 12:08:17 +0300 Subject: [PATCH] js,parser: allow declaring methods on JS interfaces for easier FFI (#12447) --- vlib/v/gen/js/fn.v | 11 +++++++---- vlib/v/parser/fn.v | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/vlib/v/gen/js/fn.v b/vlib/v/gen/js/fn.v index 2019624743..0729edb574 100644 --- a/vlib/v/gen/js/fn.v +++ b/vlib/v/gen/js/fn.v @@ -404,7 +404,7 @@ fn (mut g JsGen) is_used_by_main(node ast.FnDecl) bool { fn (mut g JsGen) gen_fn_decl(it ast.FnDecl) { res := g.fn_gen_type(it) - if it.language == .js { + if it.language == .js && it.no_body { for attr in it.attrs { match attr.name { 'wasm_import' { @@ -415,7 +415,6 @@ fn (mut g JsGen) gen_fn_decl(it ast.FnDecl) { else {} } } - return } if g.inside_builtin { @@ -483,17 +482,21 @@ fn (mut g JsGen) gen_method_decl(it ast.FnDecl, typ FnGenType) { } name = g.cc_type(node.receiver.typ, false) + '_' + name } - name = g.js_name(name) name = g.generic_fn_name(g.table.cur_concrete_types, name, true) if name in parser.builtin_functions { name = 'builtin__$name' } - has_go := fn_has_go(it) if it.is_pub && !it.is_method { g.push_pub_var(name) } + if it.language == .js && it.is_method { + g.writeln('${g.typ(it.receiver.typ)}.prototype.$it.name = ') + } + + has_go := fn_has_go(it) + is_main := it.name == 'main.main' g.gen_attrs(it.attrs) if is_main { diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 7edab448a1..ea2e5e4a8d 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -465,7 +465,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { mut stmts := []ast.Stmt{} body_start_pos := p.peek_tok.position() if p.tok.kind == .lcbr { - if language != .v { + if language != .v && language != .js { p.error_with_pos('interop functions cannot have a body', p.tok.position()) } p.inside_fn = true