2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-06-20 13:22:49 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// This file contains JS functions present in both node and the browser.
|
|
|
|
// They have been ported from their TypeScript definitions.
|
|
|
|
|
|
|
|
module builtin
|
|
|
|
|
2021-11-11 13:36:32 +01:00
|
|
|
pub interface JS.Object {}
|
2021-11-06 14:52:24 +01:00
|
|
|
|
2021-11-11 13:36:32 +01:00
|
|
|
pub interface JS.BigInt {
|
|
|
|
JS.Any
|
|
|
|
}
|
|
|
|
|
|
|
|
pub interface JS.Number {
|
|
|
|
JS.Any
|
|
|
|
}
|
2021-03-24 19:39:59 +01:00
|
|
|
|
2021-11-11 13:36:32 +01:00
|
|
|
pub interface JS.String {
|
|
|
|
JS.Any
|
2020-12-08 17:49:20 +01:00
|
|
|
length JS.Number
|
2021-11-11 13:36:32 +01:00
|
|
|
charAt(index JS.Number) JS.String
|
|
|
|
charCodeAt(index JS.Number) JS.Number
|
|
|
|
toUpperCase() JS.String
|
|
|
|
toLowerCase() JS.String
|
|
|
|
concat(a JS.String) JS.String
|
|
|
|
includes(substr JS.String) JS.Boolean
|
|
|
|
endsWith(substr JS.String) JS.Boolean
|
|
|
|
startsWith(substr JS.String) JS.Boolean
|
|
|
|
slice(a JS.Number, b JS.Number) JS.String
|
|
|
|
split(dot JS.String) JS.Array
|
|
|
|
indexOf(needle JS.String) JS.Number
|
|
|
|
lastIndexOf(needle JS.String) JS.Number
|
2020-12-08 17:49:20 +01:00
|
|
|
}
|
2021-03-24 19:39:59 +01:00
|
|
|
|
2021-11-11 13:36:32 +01:00
|
|
|
pub interface JS.Boolean {
|
|
|
|
JS.Any
|
|
|
|
}
|
2021-03-24 19:39:59 +01:00
|
|
|
|
2021-11-11 13:36:32 +01:00
|
|
|
pub interface JS.Map {
|
|
|
|
JS.Any
|
|
|
|
size JS.Number
|
|
|
|
clear()
|
|
|
|
delete(key JS.Any) JS.Boolean
|
|
|
|
get(key JS.Any) JS.Any
|
|
|
|
has(key JS.Any) JS.Any
|
|
|
|
set(key JS.Any, val JS.Any)
|
|
|
|
}
|
|
|
|
|
|
|
|
#function Any(val) { return val; }
|
|
|
|
|
|
|
|
pub interface JS.Any {}
|
|
|
|
|
|
|
|
pub interface JS.Array {
|
|
|
|
JS.Any // map(fn (JS.Any) JS.Any) JS.Array
|
|
|
|
map(JS.Any) JS.Array
|
|
|
|
push(JS.Any) JS.Any
|
|
|
|
pop() JS.Any
|
|
|
|
at(JS.Number) JS.Any
|
|
|
|
mut:
|
2021-03-04 14:02:16 +01:00
|
|
|
length JS.Number
|
|
|
|
}
|
2021-03-24 19:39:59 +01:00
|
|
|
|
2021-11-11 13:36:32 +01:00
|
|
|
pub fn JS.Array.prototype.constructor(...any) JS.Array
|
2020-12-08 17:49:20 +01:00
|
|
|
|
2021-06-10 07:33:46 +02:00
|
|
|
// browser: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Error
|
|
|
|
// node: https://nodejs.org/api/errors.html#errors_class_error
|
|
|
|
pub struct JS.Error {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
message string
|
|
|
|
stack string
|
|
|
|
}
|
|
|
|
|
2020-12-08 17:49:20 +01:00
|
|
|
// Type prototype functions
|
|
|
|
fn (v JS.String) toString() JS.String
|
|
|
|
fn (v JS.Number) toString() JS.String
|
|
|
|
fn (v JS.Boolean) toString() JS.String
|
|
|
|
fn (v JS.Array) toString() JS.String
|
|
|
|
fn (v JS.Map) toString() JS.String
|
|
|
|
|
2021-03-04 14:02:16 +01:00
|
|
|
// Hack for "`[]JS.String` is not a struct" when returning arr.length or arr.len
|
|
|
|
// TODO: Fix []JS.String not a struct error
|
|
|
|
fn native_str_arr_len(arr []JS.String) int {
|
|
|
|
len := 0
|
|
|
|
#len = arr.length
|
2021-03-24 19:39:59 +01:00
|
|
|
|
2021-03-04 14:02:16 +01:00
|
|
|
return len
|
|
|
|
}
|
2020-12-08 17:49:20 +01:00
|
|
|
|
2020-06-20 13:22:49 +02:00
|
|
|
// Top level functions
|
|
|
|
fn JS.eval(string) any
|
2021-03-04 14:02:16 +01:00
|
|
|
fn JS.parseInt(string, f64) JS.Number
|
|
|
|
fn JS.parseFloat(string) JS.Number
|
2020-06-20 13:22:49 +02:00
|
|
|
fn JS.isNaN(f64) bool
|
|
|
|
fn JS.isFinite(f64) bool
|
|
|
|
fn JS.decodeURI(string) string
|
|
|
|
fn JS.decodeURIComponent(string) string
|
|
|
|
fn JS.encodeURI(string) string
|
2021-03-24 19:39:59 +01:00
|
|
|
|
|
|
|
type EncodeURIComponentArg = bool | f64 | string
|
|
|
|
|
2020-06-20 13:22:49 +02:00
|
|
|
fn JS.encodeURIComponent(EncodeURIComponentArg) string
|
|
|
|
fn JS.escape(string) string
|
|
|
|
fn JS.unescape(string) string
|
|
|
|
|
|
|
|
// console
|
|
|
|
fn JS.console.assert(bool, ...any)
|
|
|
|
fn JS.console.clear()
|
|
|
|
fn JS.console.count(string)
|
|
|
|
fn JS.console.countReset(string)
|
|
|
|
fn JS.console.debug(...any)
|
|
|
|
fn JS.console.dir(any, any)
|
|
|
|
fn JS.console.dirxml(...any)
|
|
|
|
fn JS.console.error(...any)
|
|
|
|
fn JS.console.exception(string, ...any)
|
|
|
|
fn JS.console.group(...any)
|
|
|
|
fn JS.console.groupCollapsed(...any)
|
|
|
|
fn JS.console.groupEnd()
|
|
|
|
fn JS.console.info(...any)
|
|
|
|
fn JS.console.log(...any)
|
|
|
|
fn JS.console.table(any, []string)
|
|
|
|
fn JS.console.time(string)
|
|
|
|
fn JS.console.timeEnd(string)
|
|
|
|
fn JS.console.timeLog(string, ...any)
|
|
|
|
fn JS.console.timeStamp(string)
|
|
|
|
fn JS.console.trace(...any)
|
|
|
|
fn JS.console.warn(...any)
|
|
|
|
|
|
|
|
// Math
|
|
|
|
fn JS.Math.abs(f64) f64
|
|
|
|
fn JS.Math.acos(f64) f64
|
|
|
|
fn JS.Math.asin(f64) f64
|
|
|
|
fn JS.Math.atan(f64) f64
|
|
|
|
fn JS.Math.atan2(f64, f64) f64
|
|
|
|
fn JS.Math.ceil(f64) f64
|
|
|
|
fn JS.Math.cos(f64) f64
|
|
|
|
fn JS.Math.exp(f64) f64
|
|
|
|
fn JS.Math.floor(f64) f64
|
|
|
|
fn JS.Math.log(f64) f64
|
|
|
|
fn JS.Math.max(...f64) f64
|
|
|
|
fn JS.Math.min(...f64) f64
|
|
|
|
fn JS.Math.pow(f64, f64) f64
|
|
|
|
fn JS.Math.random() f64
|
|
|
|
fn JS.Math.round(f64) f64
|
|
|
|
fn JS.Math.sin(f64) f64
|
|
|
|
fn JS.Math.sqrt(f64) f64
|
|
|
|
fn JS.Math.tan(f64) f64
|
2020-12-08 17:49:20 +01:00
|
|
|
|
|
|
|
// JSON
|
|
|
|
fn JS.JSON.stringify(any) string
|
2021-01-18 13:20:06 +01:00
|
|
|
fn JS.JSON.parse(string) any
|