js,parser: allow declaring methods on JS interfaces for easier FFI (#12447)

pull/12458/head
playX 2021-11-13 12:08:17 +03:00 committed by GitHub
parent 3bb1c3f930
commit 6c32c544e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View File

@ -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 {

View File

@ -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