js,vfmt: fix formatting for JS types; add fetch API (#12608)
parent
9825c7e06c
commit
ddec89f9ee
|
@ -271,6 +271,11 @@ pub interface JS.Document {
|
||||||
queryCommandState(commandId JS.String) JS.String
|
queryCommandState(commandId JS.String) JS.String
|
||||||
write(text ...JS.Any)
|
write(text ...JS.Any)
|
||||||
writeln(text ...JS.Any)
|
writeln(text ...JS.Any)
|
||||||
|
exitFullscreen() JS.Promise
|
||||||
|
exitPictureInPicture() JS.Promise
|
||||||
|
exitPointerLock()
|
||||||
|
requestPointerLock()
|
||||||
|
requestFullScreen() JS.Promise
|
||||||
mut:
|
mut:
|
||||||
bgColor JS.String
|
bgColor JS.String
|
||||||
fgColor JS.String
|
fgColor JS.String
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
module js
|
||||||
|
|
||||||
|
import js.promise
|
||||||
|
|
||||||
|
pub fn JS.fetch(input JS.String, init JS.Object) JS.Promise
|
||||||
|
|
||||||
|
pub interface JS.Body {
|
||||||
|
body JS.Uint8Array
|
||||||
|
bodyUse JS.Boolean
|
||||||
|
blob() JS.Promise
|
||||||
|
json() JS.Promise
|
||||||
|
text() JS.Promise
|
||||||
|
}
|
||||||
|
|
||||||
|
pub interface JS.Response {
|
||||||
|
JS.Body
|
||||||
|
ok JS.Boolean
|
||||||
|
redirected JS.Boolean
|
||||||
|
status JS.Number
|
||||||
|
statusText JS.String
|
||||||
|
url JS.String
|
||||||
|
clone() JS.Response
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fetch(input string, init map[string]JS.Any) promise.Promise<JS.Response, JS.String> {
|
||||||
|
p_init := JS.Any(voidptr(0))
|
||||||
|
p := promise.Promise<JS.Response,String>{p_init}
|
||||||
|
|
||||||
|
#let obj = {}; for (let [key,val] of init.map) { obj[key] = val; }
|
||||||
|
#p.promise = fetch(input.str,obj);
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
|
@ -68,12 +68,14 @@ pub fn JS.Promise.all(JS.Array) JS.Promise
|
||||||
pub fn JS.Promise.allSettled(JS.Array) JS.Promise
|
pub fn JS.Promise.allSettled(JS.Array) JS.Promise
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
pub type JsAny = JS.Any
|
||||||
|
|
||||||
// all takes an iterable of promises as an input, and returns a single Promise that resolves to an array of
|
// all takes an iterable of promises as an input, and returns a single Promise that resolves to an array of
|
||||||
// the results of the input promises
|
// the results of the input promises
|
||||||
pub fn all(array []JS.Promise) Promise<JS.Array, JS.Any> {
|
pub fn all(array []JS.Promise) Promise<JS.Array, js.promise.JsAny> {
|
||||||
mut promise := JS.Promise(JS.Any(voidptr(0)))
|
mut promise := JS.Promise(JS.Any(voidptr(0)))
|
||||||
#promise = Promise.all(array.arr.arr);
|
#promise = Promise.all(array.arr.arr);
|
||||||
|
|
||||||
return Promise<Array,Any>{promise}
|
return Promise<JS.Array,JsAny>{promise}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
module promise
|
||||||
|
|
||||||
fn test_promise() {
|
fn test_promise() {
|
||||||
// TODO: For some reason compiler errors: "error: unknown function: js.promise.new", fix this
|
// TODO: For some reason compiler errors: "error: unknown function: js.promise.new", fix this
|
||||||
/*
|
/*
|
||||||
p := promise.new<int, f64>(fn (resolve_ fn (x int), reject_ fn (x f64)) {
|
p := new<int, f64>(fn (resolve_ fn (x int), reject_ fn (x f64)) {
|
||||||
println('Promise code')
|
println('Promise code')
|
||||||
assert true
|
assert true
|
||||||
resolve_(42)
|
resolve_(42)
|
||||||
|
|
|
@ -203,7 +203,7 @@ pub fn (mut f Fmt) no_cur_mod(typename string) string {
|
||||||
|
|
||||||
// foo.bar.fn() => bar.fn()
|
// foo.bar.fn() => bar.fn()
|
||||||
pub fn (mut f Fmt) short_module(name string) string {
|
pub fn (mut f Fmt) short_module(name string) string {
|
||||||
if !name.contains('.') {
|
if !name.contains('.') || name.starts_with('JS.') {
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
if name in f.mod2alias {
|
if name in f.mod2alias {
|
||||||
|
@ -1613,7 +1613,14 @@ fn (mut f Fmt) write_generic_call_if_require(node ast.CallExpr) {
|
||||||
if node.concrete_types.len > 0 {
|
if node.concrete_types.len > 0 {
|
||||||
f.write('<')
|
f.write('<')
|
||||||
for i, concrete_type in node.concrete_types {
|
for i, concrete_type in node.concrete_types {
|
||||||
f.write(f.short_module(f.table.type_to_str_using_aliases(concrete_type, f.mod2alias)))
|
mut name := f.table.type_to_str_using_aliases(concrete_type, f.mod2alias)
|
||||||
|
tsym := f.table.get_type_symbol(concrete_type)
|
||||||
|
if tsym.language != .js && !tsym.name.starts_with('JS.') {
|
||||||
|
name = f.short_module(name)
|
||||||
|
} else if tsym.language == .js && !tsym.name.starts_with('JS.') {
|
||||||
|
name = 'JS.' + name
|
||||||
|
}
|
||||||
|
f.write(name)
|
||||||
if i != node.concrete_types.len - 1 {
|
if i != node.concrete_types.len - 1 {
|
||||||
f.write(', ')
|
f.write(', ')
|
||||||
}
|
}
|
||||||
|
|
|
@ -1678,25 +1678,11 @@ fn (mut g JsGen) gen_for_stmt(it ast.ForStmt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g JsGen) gen_go_expr(node ast.GoExpr) {
|
fn (mut g JsGen) gen_go_expr(node ast.GoExpr) {
|
||||||
// TODO Handle joinable expressions
|
|
||||||
// node.is_expr
|
|
||||||
mut name := g.js_name(node.call_expr.name)
|
|
||||||
if node.call_expr.is_method {
|
|
||||||
receiver_sym := g.table.get_type_symbol(node.call_expr.receiver_type)
|
|
||||||
name = receiver_sym.name + '.' + name
|
|
||||||
}
|
|
||||||
|
|
||||||
g.writeln('await new Promise(function(resolve){')
|
g.writeln('await new Promise(function(resolve){')
|
||||||
g.inc_indent()
|
g.inc_indent()
|
||||||
g.write('${name}(')
|
g.write('resolve(')
|
||||||
for i, arg in node.call_expr.args {
|
g.expr(node.call_expr)
|
||||||
g.expr(arg.expr)
|
g.write(');')
|
||||||
if i < node.call_expr.args.len - 1 {
|
|
||||||
g.write(', ')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
g.writeln(');')
|
|
||||||
g.writeln('resolve();')
|
|
||||||
g.dec_indent()
|
g.dec_indent()
|
||||||
g.writeln('});')
|
g.writeln('});')
|
||||||
}
|
}
|
||||||
|
@ -3207,7 +3193,10 @@ fn (mut g JsGen) gen_string_literal(it ast.StringLiteral) {
|
||||||
|
|
||||||
fn (mut g JsGen) gen_struct_init(it ast.StructInit) {
|
fn (mut g JsGen) gen_struct_init(it ast.StructInit) {
|
||||||
type_sym := g.table.get_type_symbol(it.typ)
|
type_sym := g.table.get_type_symbol(it.typ)
|
||||||
name := type_sym.name
|
mut name := type_sym.name
|
||||||
|
if name.contains('<') {
|
||||||
|
name = name[0..name.index('<') or { name.len }]
|
||||||
|
}
|
||||||
if it.fields.len == 0 && type_sym.kind != .interface_ {
|
if it.fields.len == 0 && type_sym.kind != .interface_ {
|
||||||
if type_sym.kind == .struct_ && type_sym.language == .js {
|
if type_sym.kind == .struct_ && type_sym.language == .js {
|
||||||
g.write('{}')
|
g.write('{}')
|
||||||
|
|
Loading…
Reference in New Issue