From 1d0ebfb691012d8bba4155ed22ac0f9212933858 Mon Sep 17 00:00:00 2001 From: Leah Lundqvist Date: Thu, 28 May 2020 14:38:10 +0200 Subject: [PATCH] jsgen: add hash statement --- vlib/v/gen/js/js.v | 6 ++++- vlib/v/gen/js/tests/hello/hello.js.v | 5 ++++ vlib/v/gen/js/tests/js.js | 38 ++++++++++++++++++---------- vlib/v/gen/js/tests/js.v | 8 +++--- vlib/v/parser/comptime.v | 8 ++++++ 5 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 vlib/v/gen/js/tests/hello/hello.js.v diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 11e8918293..f2608d8e05 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -446,7 +446,7 @@ fn (mut g JsGen) stmt(node ast.Stmt) { // skip: JS has no goto } ast.HashStmt { - // skip: nothing with # in JS + g.gen_hash_stmt(it) } ast.Import { g.gen_import_stmt(it) @@ -1010,6 +1010,10 @@ fn (mut g JsGen) gen_return_stmt(it ast.Return) { 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) { g.writeln(g.doc.gen_fac_fn(node.fields)) g.write('function ${g.js_name(node.name)}({ ') diff --git a/vlib/v/gen/js/tests/hello/hello.js.v b/vlib/v/gen/js/tests/hello/hello.js.v new file mode 100644 index 0000000000..4216b86b41 --- /dev/null +++ b/vlib/v/gen/js/tests/hello/hello.js.v @@ -0,0 +1,5 @@ +module hello + +pub fn raw_js_log() { + #console.log('hello') +} \ No newline at end of file diff --git a/vlib/v/gen/js/tests/js.js b/vlib/v/gen/js/tests/js.js index f14affdd6e..34f496bc9f 100644 --- a/vlib/v/gen/js/tests/js.js +++ b/vlib/v/gen/js/tests/js.js @@ -1,5 +1,5 @@ -// V_COMMIT_HASH 0de70e8 -// V_CURRENT_COMMIT_HASH 4271eb4 +// V_COMMIT_HASH 5423a15 +// V_CURRENT_COMMIT_HASH 941404d // Generated by the V compiler "use strict"; @@ -40,14 +40,22 @@ const builtin = (function () { /** @namespace hello */ const hello = (function () { + /** + * @returns {void} + * @function + */ + function raw_js_log() { + console.log('hello') + } + /** * @param {{foo?: string}} values - values for this class fields * @constructor */ - function A({ foo = "" }) { + function Aaa({ foo = "" }) { this.foo = foo }; - A.prototype = { + Aaa.prototype = { /** @type {string} - foo */ foo: "", /** @@ -66,12 +74,12 @@ const hello = (function () { * @param {{}} values - values for this class fields * @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 v_debugger() { - const v = new B({}); + const v = new Bbb({}); return "Hello"; } @@ -93,8 +101,9 @@ const hello = (function () { /* module exports */ return { - A, - C, + raw_js_log, + Aaa, + Ccc, v_debugger, excited, }; @@ -103,14 +112,14 @@ const hello = (function () { /** @namespace main */ 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 */ function Foo({ a = {} }) { this.a = a }; Foo.prototype = { - /** @type {hl["A"]["prototype"]} - a */ + /** @type {hl["Aaa"]["prototype"]} - a */ a: {} }; @@ -176,11 +185,11 @@ const main = (function (hl) { a *= 2; a += 3; builtin.println(a); - const b = new hl.A({}); + const b = new hl.Aaa({}); b.update("an update"); builtin.println(b); const c = new Foo({ - a: new hl.A({}) + a: new hl.Aaa({}) }); c.a.update("another update"); builtin.println(c); @@ -241,6 +250,7 @@ const main = (function (hl) { anon_consumer(hl.excited(), function (message) { builtin.println(message); }); + hl.raw_js_log(); })(); /** diff --git a/vlib/v/gen/js/tests/js.v b/vlib/v/gen/js/tests/js.v index 37e74e5393..b68d5b90ec 100644 --- a/vlib/v/gen/js/tests/js.v +++ b/vlib/v/gen/js/tests/js.v @@ -8,7 +8,7 @@ const ( ) struct Foo { - a hl.A + a hl.Aaa } struct Companies { @@ -35,11 +35,11 @@ fn main() { a += 3 println(a) // TODO: Handle string interpolation - b := hl.A{} + b := hl.Aaa{} b.update('an update') println(b) - c := Foo{ hl.A{} } + c := Foo{ hl.Aaa{} } c.a.update('another update') println(c) @@ -87,6 +87,8 @@ fn main() { anon_consumer(hl.excited(), fn (message string) { println(message) }) + + hl.raw_js_log() } fn anon_consumer (greeting string, anon fn(message string)) { diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index f6bbfc700c..043451b5d2 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -17,6 +17,14 @@ const ( fn (mut p Parser) hash() ast.HashStmt { val := p.tok.lit 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') { // #flag linux -lm mut flag := val[5..]