js: support `[export: 'AnotherName']` for FnDecl codegen (#11377)

pull/11383/head
playX 2021-09-03 13:00:24 +03:00 committed by GitHub
parent 67ab5b858b
commit a4df418a68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -5,9 +5,14 @@ pub fn js_throw(s any) {
#throw s #throw s
} }
# let globalPrint;
$if js_freestanding {
# globalPrint = globalThis.print
}
pub fn println(s string) { pub fn println(s string) {
$if js_freestanding { $if js_freestanding {
#print(s.str) #globalPrint(s.str)
} $else { } $else {
#console.log(s.str) #console.log(s.str)
} }
@ -23,7 +28,7 @@ pub fn print(s string) {
pub fn eprintln(s string) { pub fn eprintln(s string) {
$if js_freestanding { $if js_freestanding {
#print(s.str) #globalPrint(s.str)
} $else { } $else {
#console.error(s.str) #console.error(s.str)
} }

View File

@ -1452,7 +1452,14 @@ fn (mut g JsGen) gen_method_decl(it ast.FnDecl, typ FnGenType) {
g.stmts(it.stmts) g.stmts(it.stmts)
g.writeln('}') g.writeln('}')
for attr in it.attrs {
match attr.name {
'export' {
g.writeln('globalThis.$attr.arg = ${g.js_name(it.name)};')
}
else {}
}
}
if is_main { if is_main {
g.write(')();') g.write(')();')
} else if typ != .struct_method { } else if typ != .struct_method {