1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

jsgen: object equality checks, optimise casting and start builtin implementation (#9068)

This commit is contained in:
Leah Lundqvist
2021-03-04 14:02:16 +01:00
committed by GitHub
parent 5e0e44eb69
commit 65e888230a
6 changed files with 581 additions and 74 deletions

View File

@ -12,7 +12,9 @@ pub struct JS.String {
length JS.Number
}
pub struct JS.Boolean {}
pub struct JS.Array {}
pub struct JS.Array {
length JS.Number
}
pub struct JS.Map {}
// Type prototype functions
@ -22,12 +24,18 @@ fn (v JS.Boolean) toString() JS.String
fn (v JS.Array) toString() JS.String
fn (v JS.Map) toString() JS.String
fn (v JS.String) slice(a int, b int) JS.String
// 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
return len
}
// Top level functions
fn JS.eval(string) any
fn JS.parseInt(string, f64) f64
fn JS.parseFloat(string) f64
fn JS.parseInt(string, f64) JS.Number
fn JS.parseFloat(string) JS.Number
fn JS.isNaN(f64) bool
fn JS.isFinite(f64) bool
fn JS.decodeURI(string) string
@ -84,3 +92,18 @@ fn JS.Math.tan(f64) f64
// JSON
fn JS.JSON.stringify(any) string
fn JS.JSON.parse(string) any
// String
fn (v JS.String) slice(a int, b int) JS.String
fn (v JS.String) split(dot JS.String) []JS.String
fn (s JS.String) indexOf(needle JS.String) int
fn (s JS.String) lastIndexOf(needle JS.String) int
fn (s JS.String) charAt(i int) JS.String
fn (s JS.String) charCodeAt(i int) byte
fn (s JS.String) toUpperCase() JS.String
fn (s JS.String) toLowerCase() JS.String
fn (s JS.String) concat(a JS.String) JS.String
fn (s JS.String) includes(substr JS.String) bool
fn (s JS.String) ends_with(substr JS.String) bool
fn (s JS.String) starts_with(substr JS.String) bool

View File

@ -2,8 +2,162 @@ module builtin
pub struct string {
str JS.String
len u32
}
pub fn (s string) slice(a int, b int) string {
return string(s.str.slice(a, b))
}
pub fn (s string) after(dot string) string {
return string(s.str.slice(s.str.lastIndexOf(dot.str) + 1, int(s.str.length)))
}
pub fn (s string) after_char(dot byte) string {
// TODO: Implement after byte
return s
}
pub fn (s string) all_after(dot string) string {
return string(s.str.slice(s.str.indexOf(dot.str) + 1, int(s.str.length)))
}
// why does this exist?
pub fn (s string) all_after_last(dot string) string {
return s.after(dot)
}
pub fn (s string) all_before(dot string) string {
return string(s.str.slice(0, s.str.indexOf(dot.str)))
}
pub fn (s string) all_before_last(dot string) string {
return string(s.str.slice(0, s.str.lastIndexOf(dot.str)))
}
pub fn (s string) bool() bool {
return s == 'true'
}
pub fn (s string) split(dot string) []string {
return s.str.split(dot.str).map(string(it))
}
pub fn (s string) bytes() []byte {
sep := ''
return s.str.split(sep.str).map(it.charCodeAt(0))
}
pub fn (s string) capitalize() string {
part := string(s.str.slice(1, int(s.str.length)))
return string(s.str.charAt(0).toUpperCase().concat(part.str))
}
pub fn (s string) clone() string {
return string(s.str)
}
pub fn (s string) contains(substr string) bool {
return s.str.includes(substr.str)
}
pub fn (s string) contains_any(chars string) bool {
sep := ''
for x in chars.str.split(sep.str) {
if s.str.includes(x) {
return true
}
}
return false
}
pub fn (s string) contains_any_substr(chars []string) bool {
for x in chars {
if s.str.includes(x.str) {
return true
}
}
return false
}
pub fn (s string) count(substr string) int {
// TODO: "error: `[]JS.String` is not a struct" when returning arr.length or arr.len
arr := s.str.split(substr.str)
return native_str_arr_len(arr)
}
pub fn (s string) ends_with(p string) bool {
return s.str.ends_with(p.str)
}
pub fn (s string) starts_with(p string) bool {
return s.str.starts_with(p.str)
}
pub fn (s string) fields() []string {
return []// s.str.split()
}
pub fn (s string) find_between(start string, end string) string {
return string(s.str.slice(s.str.indexOf(start.str), s.str.indexOf(end.str) + 1))
}
// unnecessary in the JS backend, implemented for api parity.
pub fn (s string) free () {}
pub fn (s string) hash () int {
mut h := u32(0)
if h == 0 && s.len > 0 {
for c in s {
h = h * 31 + u32(c)
}
}
return int(h)
}
// int returns the value of the string as an integer `'1'.int() == 1`.
pub fn (s string) int() int {
return int(JS.parseInt(s))
}
// i64 returns the value of the string as i64 `'1'.i64() == i64(1)`.
pub fn (s string) i64() i64 {
return i64(JS.parseInt(s))
}
// i8 returns the value of the string as i8 `'1'.i8() == i8(1)`.
pub fn (s string) i8() i8 {
return i8(JS.parseInt(s))
}
// i16 returns the value of the string as i16 `'1'.i16() == i16(1)`.
pub fn (s string) i16() i16 {
return i16(JS.parseInt(s))
}
// f32 returns the value of the string as f32 `'1.0'.f32() == f32(1)`.
pub fn (s string) f32() f32 {
// return C.atof(charptr(s.str))
return f32(JS.parseFloat(s))
}
// f64 returns the value of the string as f64 `'1.0'.f64() == f64(1)`.
pub fn (s string) f64() f64 {
return f64(JS.parseFloat(s))
}
// u16 returns the value of the string as u16 `'1'.u16() == u16(1)`.
pub fn (s string) u16() u16 {
return u16(JS.parseInt(s))
}
// u32 returns the value of the string as u32 `'1'.u32() == u32(1)`.
pub fn (s string) u32() u32 {
return u32(JS.parseInt(s))
}
// u64 returns the value of the string as u64 `'1'.u64() == u64(1)`.
pub fn (s string) u64() u64 {
return u64(JS.parseInt(s))
}