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
}
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)}({ ')

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_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();
})();
/**

View File

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

View File

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