jsgen: add hash statement

pull/5099/head
Leah Lundqvist 2020-05-28 14:38:10 +02:00 committed by GitHub
parent 2943bdc1f6
commit 1d0ebfb691
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 18 deletions

View File

@ -446,7 +446,7 @@ fn (mut g JsGen) stmt(node ast.Stmt) {
// skip: JS has no goto // skip: JS has no goto
} }
ast.HashStmt { ast.HashStmt {
// skip: nothing with # in JS g.gen_hash_stmt(it)
} }
ast.Import { ast.Import {
g.gen_import_stmt(it) g.gen_import_stmt(it)
@ -1010,6 +1010,10 @@ fn (mut g JsGen) gen_return_stmt(it ast.Return) {
g.writeln(';') g.writeln(';')
} }
fn (mut g JsGen) gen_hash_stmt(it ast.HashStmt) {
g.writeln(it.val)
}
fn (mut g JsGen) gen_struct_decl(node ast.StructDecl) { fn (mut g JsGen) gen_struct_decl(node ast.StructDecl) {
g.writeln(g.doc.gen_fac_fn(node.fields)) g.writeln(g.doc.gen_fac_fn(node.fields))
g.write('function ${g.js_name(node.name)}({ ') g.write('function ${g.js_name(node.name)}({ ')

View File

@ -0,0 +1,5 @@
module hello
pub fn raw_js_log() {
#console.log('hello')
}

View File

@ -1,5 +1,5 @@
// V_COMMIT_HASH 0de70e8 // V_COMMIT_HASH 5423a15
// V_CURRENT_COMMIT_HASH 4271eb4 // V_CURRENT_COMMIT_HASH 941404d
// Generated by the V compiler // Generated by the V compiler
"use strict"; "use strict";
@ -40,14 +40,22 @@ const builtin = (function () {
/** @namespace hello */ /** @namespace hello */
const hello = (function () { const hello = (function () {
/**
* @returns {void}
* @function
*/
function raw_js_log() {
console.log('hello')
}
/** /**
* @param {{foo?: string}} values - values for this class fields * @param {{foo?: string}} values - values for this class fields
* @constructor * @constructor
*/ */
function A({ foo = "" }) { function Aaa({ foo = "" }) {
this.foo = foo this.foo = foo
}; };
A.prototype = { Aaa.prototype = {
/** @type {string} - foo */ /** @type {string} - foo */
foo: "", foo: "",
/** /**
@ -66,12 +74,12 @@ const hello = (function () {
* @param {{}} values - values for this class fields * @param {{}} values - values for this class fields
* @constructor * @constructor
*/ */
function B({ }) { function Bbb({ }) {
}; };
B.prototype = { Bbb.prototype = {
}; };
const C = Object.freeze({ const Ccc = Object.freeze({
}); });
/** /**
@ -79,7 +87,7 @@ const hello = (function () {
* @function * @function
*/ */
function v_debugger() { function v_debugger() {
const v = new B({}); const v = new Bbb({});
return "Hello"; return "Hello";
} }
@ -93,8 +101,9 @@ const hello = (function () {
/* module exports */ /* module exports */
return { return {
A, raw_js_log,
C, Aaa,
Ccc,
v_debugger, v_debugger,
excited, excited,
}; };
@ -103,14 +112,14 @@ const hello = (function () {
/** @namespace main */ /** @namespace main */
const main = (function (hl) { const main = (function (hl) {
/** /**
* @param {{a?: hl["A"]["prototype"]}} values - values for this class fields * @param {{a?: hl["Aaa"]["prototype"]}} values - values for this class fields
* @constructor * @constructor
*/ */
function Foo({ a = {} }) { function Foo({ a = {} }) {
this.a = a this.a = a
}; };
Foo.prototype = { Foo.prototype = {
/** @type {hl["A"]["prototype"]} - a */ /** @type {hl["Aaa"]["prototype"]} - a */
a: {} a: {}
}; };
@ -176,11 +185,11 @@ const main = (function (hl) {
a *= 2; a *= 2;
a += 3; a += 3;
builtin.println(a); builtin.println(a);
const b = new hl.A({}); const b = new hl.Aaa({});
b.update("an update"); b.update("an update");
builtin.println(b); builtin.println(b);
const c = new Foo({ const c = new Foo({
a: new hl.A({}) a: new hl.Aaa({})
}); });
c.a.update("another update"); c.a.update("another update");
builtin.println(c); builtin.println(c);
@ -241,6 +250,7 @@ const main = (function (hl) {
anon_consumer(hl.excited(), function (message) { anon_consumer(hl.excited(), function (message) {
builtin.println(message); builtin.println(message);
}); });
hl.raw_js_log();
})(); })();
/** /**

View File

@ -8,7 +8,7 @@ const (
) )
struct Foo { struct Foo {
a hl.A a hl.Aaa
} }
struct Companies { struct Companies {
@ -35,11 +35,11 @@ fn main() {
a += 3 a += 3
println(a) // TODO: Handle string interpolation println(a) // TODO: Handle string interpolation
b := hl.A{} b := hl.Aaa{}
b.update('an update') b.update('an update')
println(b) println(b)
c := Foo{ hl.A{} } c := Foo{ hl.Aaa{} }
c.a.update('another update') c.a.update('another update')
println(c) println(c)
@ -87,6 +87,8 @@ fn main() {
anon_consumer(hl.excited(), fn (message string) { anon_consumer(hl.excited(), fn (message string) {
println(message) println(message)
}) })
hl.raw_js_log()
} }
fn anon_consumer (greeting string, anon fn(message string)) { fn anon_consumer (greeting string, anon fn(message string)) {

View File

@ -17,6 +17,14 @@ const (
fn (mut p Parser) hash() ast.HashStmt { fn (mut p Parser) hash() ast.HashStmt {
val := p.tok.lit val := p.tok.lit
p.next() p.next()
if p.pref.backend == .js {
if !p.file_name.ends_with('.js.v') {
p.error('Hash statements are only allowed in backend specific files such "x.js.v"')
}
if p.mod == 'main' {
p.error('Hash statements are not allowed in the main module. Please place them in a separate module.')
}
}
if val.starts_with('flag') { if val.starts_with('flag') {
// #flag linux -lm // #flag linux -lm
mut flag := val[5..] mut flag := val[5..]