js: use JS.String instead of string in builtin javascript functions (#13004)

pull/13010/head
pancake 2022-01-01 08:17:08 +01:00 committed by GitHub
parent 7622ff3f54
commit 7b4ba66720
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 27 deletions

View File

@ -123,26 +123,26 @@ fn native_str_arr_len(arr []JS.String) int {
} }
// Top level functions // Top level functions
fn JS.eval(string) any fn JS.eval(JS.String) JS.Any
fn JS.parseInt(string, f64) JS.Number fn JS.parseInt(JS.String, f64) JS.Number
fn JS.parseFloat(string) JS.Number fn JS.parseFloat(JS.String) JS.Number
fn JS.isNaN(f64) bool fn JS.isNaN(f64) bool
fn JS.isFinite(f64) bool fn JS.isFinite(f64) bool
fn JS.decodeURI(string) string fn JS.decodeURI(JS.String) JS.String
fn JS.decodeURIComponent(string) string fn JS.decodeURIComponent(JS.String) JS.String
fn JS.encodeURI(string) string fn JS.encodeURI(JS.String) JS.String
type EncodeURIComponentArg = bool | f64 | string type EncodeURIComponentArg = JS.String | bool | f64
fn JS.encodeURIComponent(EncodeURIComponentArg) string fn JS.encodeURIComponent(EncodeURIComponentArg) JS.String
fn JS.escape(string) string fn JS.escape(JS.String) JS.String
fn JS.unescape(string) string fn JS.unescape(JS.String) JS.String
// console // console
fn JS.console.assert(bool, ...any) fn JS.console.assert(bool, ...any)
fn JS.console.clear() fn JS.console.clear()
fn JS.console.count(string) fn JS.console.count(JS.String)
fn JS.console.countReset(string) fn JS.console.countReset(JS.String)
fn JS.console.debug(...any) fn JS.console.debug(...any)
fn JS.console.dir(any, any) fn JS.console.dir(any, any)
fn JS.console.dirxml(...any) fn JS.console.dirxml(...any)
@ -154,8 +154,8 @@ fn JS.console.groupEnd()
fn JS.console.info(...any) fn JS.console.info(...any)
fn JS.console.log(...any) fn JS.console.log(...any)
fn JS.console.table(any, []string) fn JS.console.table(any, []string)
fn JS.console.time(string) fn JS.console.time(JS.String)
fn JS.console.timeEnd(string) fn JS.console.timeEnd(JS.String)
fn JS.console.timeLog(string, ...any) fn JS.console.timeLog(string, ...any)
fn JS.console.timeStamp(string) fn JS.console.timeStamp(string)
fn JS.console.trace(...any) fn JS.console.trace(...any)
@ -182,5 +182,5 @@ fn JS.Math.sqrt(f64) f64
fn JS.Math.tan(f64) f64 fn JS.Math.tan(f64) f64
// JSON // JSON
fn JS.JSON.stringify(any) string fn JS.JSON.stringify(any) JS.String
fn JS.JSON.parse(string) any fn JS.JSON.parse(string) any

View File

@ -205,53 +205,53 @@ pub fn (s string) hash() int {
// int returns the value of the string as an integer `'1'.int() == 1`. // int returns the value of the string as an integer `'1'.int() == 1`.
pub fn (s string) int() int { pub fn (s string) int() int {
return int(JS.parseInt(s)) return int(JS.parseInt(s.str))
} }
// i64 returns the value of the string as i64 `'1'.i64() == i64(1)`. // i64 returns the value of the string as i64 `'1'.i64() == i64(1)`.
pub fn (s string) i64() i64 { pub fn (s string) i64() i64 {
return i64(JS.parseInt(s)) return i64(JS.parseInt(s.str))
} }
// i8 returns the value of the string as i8 `'1'.i8() == i8(1)`. // i8 returns the value of the string as i8 `'1'.i8() == i8(1)`.
pub fn (s string) i8() i8 { pub fn (s string) i8() i8 {
return i8(JS.parseInt(s)) return i8(JS.parseInt(s.str))
} }
// i16 returns the value of the string as i16 `'1'.i16() == i16(1)`. // i16 returns the value of the string as i16 `'1'.i16() == i16(1)`.
pub fn (s string) i16() i16 { pub fn (s string) i16() i16 {
return i16(JS.parseInt(s)) return i16(JS.parseInt(s.str))
} }
// f32 returns the value of the string as f32 `'1.0'.f32() == f32(1)`. // f32 returns the value of the string as f32 `'1.0'.f32() == f32(1)`.
pub fn (s string) f32() f32 { pub fn (s string) f32() f32 {
// return C.atof(&char(s.str)) // return C.atof(&char(s.str))
return f32(JS.parseFloat(s)) return f32(JS.parseFloat(s.str))
} }
// f64 returns the value of the string as f64 `'1.0'.f64() == f64(1)`. // f64 returns the value of the string as f64 `'1.0'.f64() == f64(1)`.
pub fn (s string) f64() f64 { pub fn (s string) f64() f64 {
return f64(JS.parseFloat(s)) return f64(JS.parseFloat(s.str))
} }
// u16 returns the value of the string as u16 `'1'.u16() == u16(1)`. // u16 returns the value of the string as u16 `'1'.u16() == u16(1)`.
pub fn (s string) u16() u16 { pub fn (s string) u16() u16 {
return u16(JS.parseInt(s)) return u16(JS.parseInt(s.str))
} }
// u32 returns the value of the string as u32 `'1'.u32() == u32(1)`. // u32 returns the value of the string as u32 `'1'.u32() == u32(1)`.
pub fn (s string) u32() u32 { pub fn (s string) u32() u32 {
return u32(JS.parseInt(s)) return u32(JS.parseInt(s.str))
} }
// u64 returns the value of the string as u64 `'1'.u64() == u64(1)`. // u64 returns the value of the string as u64 `'1'.u64() == u64(1)`.
pub fn (s string) u64() u64 { pub fn (s string) u64() u64 {
return u64(JS.parseInt(s)) return u64(JS.parseInt(s.str))
} }
pub fn (s string) byte() u64 { pub fn (s string) byte() u64 {
res := byte(0) res := byte(0)
#res.val = byte(JS.parseInt(s)) #res.val = byte(JS.parseInt(s.str))
return res return res
} }

View File

@ -30,7 +30,18 @@ fn class(extends string, instanceof int) {
fn main() { fn main() {
println('Hello from V.js!') println('Hello from V.js!')
println(JS.Math.atan2(1, 0)) println(JS.Math.atan2(1, 0))
println(JS.eval("console.log('Hello!')")) non := JS.eval("console.log('Hello!')".str)
if isnil (non) {
println('non=nil')
}
ren := int(JS.eval('3'.str))
if ren != 0 {
println('ren=$ren')
}
res := string(JS.eval('"3"'.str))
if res != '' {
println('res=$res')
}
mut a := 1 mut a := 1
a *= 2 a *= 2
a += 3 a += 3
@ -43,7 +54,7 @@ fn main() {
println(c) println(c)
println('int(1.5) == "${int(1.5)}"') println('int(1.5) == "${int(1.5)}"')
d := int(10) + f32(127) d := int(10) + f32(127)
println('typeof (int + f32) == "${typeof(d)}"') println('typeof (int + f32) == "${typeof(d).name}"')
_ = 'done' _ = 'done'
{ {
_ = 'block' _ = 'block'