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

JavaSript backend (early stage)

This commit is contained in:
Alexander Medvednikov
2019-09-14 23:48:30 +03:00
parent 982a162fbf
commit 5cc81b91cb
31 changed files with 1818 additions and 584 deletions

130
vlib/builtin/js/array.v Normal file
View File

@ -0,0 +1,130 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
import strings
struct array {
pub:
data voidptr
len int
cap int
element_size int
}
/*
// Private function, used by V (`nums := []int`)
fn new_array(mylen, cap, elm_size int) array {
arr := array {
len: mylen
cap: cap
element_size: elm_size
}
return arr
}
// TODO
pub fn _make(len, cap, elm_size int) array {
return new_array(len, cap, elm_size)
}
*/
fn array_repeat(val voidptr, nr_repeats, elm_size int) array {
return val
}
pub fn (a array) repeat2(nr_repeats int) array {
#return Array(a[0]).fill(nr_repeats)
return a
}
pub fn (a mut array) sort_with_compare(compare voidptr) {
}
pub fn (a mut array) insert(i int, val voidptr) {
}
pub fn (a mut array) prepend(val voidptr) {
a.insert(0, val)
}
pub fn (a mut array) delete_elm(idx int) {
}
/*
pub fn (a array) first() voidptr {
if a.len == 0 {
panic('array.first: empty array')
}
return a.data + 0
}
pub fn (a array) last() voidptr {
if a.len == 0 {
panic('array.last: empty array')
}
return a.data + (a.len - 1) * a.element_size
}
*/
pub fn (s array) left(n int) array {
if n >= s.len {
return s
}
return s.slice(0, n)
}
pub fn (s array) right(n int) array {
if n >= s.len {
return s
}
return s.slice(n, s.len)
}
pub fn (s array) slice(start, _end int) array {
return s
}
pub fn (a array) reverse() array {
return a
}
pub fn (a array) clone() array {
return a
}
pub fn (a array) free() {
}
// "[ 'a', 'b', 'c' ]"
pub fn (a []string) str() string {
mut sb := strings.new_builder(a.len * 3)
sb.write('[')
for i := 0; i < a.len; i++ {
val := a[i]
sb.write('"')
sb.write(val)
sb.write('"')
if i < a.len - 1 {
sb.write(', ')
}
}
sb.write(']')
return sb.str()
}
pub fn (b []byte) hex() string {
return 'sdf'
}
pub fn (arr mut array) _push_many(val voidptr, size int) {
}
pub fn free(voidptr) {
}

35
vlib/builtin/js/builtin.v Normal file
View File

@ -0,0 +1,35 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
pub fn exit(code int) {
println('js.exit()')
}
// isnil returns true if an object is nil (only for C objects).
pub fn isnil(v voidptr) bool {
return v == 0
}
pub fn panic(s string) {
println('V panic: ' + s)
exit(1)
}
pub fn println(s string) {
#console.log(s)
}
pub fn eprintln(s string) {
#console.log(s)
}
pub fn print(s string) {
#console.log(s)
}

99
vlib/builtin/js/int.v Normal file
View File

@ -0,0 +1,99 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
#include <float.h>
#include <math.h>
pub fn (d double) str() string {
return '0'
}
pub fn (d f64) str() string {
return '0'
}
pub fn (d f32) str() string {
return '0'
}
pub fn ptr_str(ptr voidptr) string {
return '0'
}
// compare floats using C epsilon
pub fn (a f64) eq(b f64) bool {
//return C.fabs(a - b) <= C.DBL_EPSILON
return (a - b) <= 0.01
}
// fn (nn i32) str() string {
// return i
// }
pub fn (nn int) str() string {
return '0'
}
pub fn (nn u32) str() string {
return '0'
}
pub fn (nn u8) str() string {
return '0'
}
pub fn (nn i64) str() string {
return '0'
}
pub fn (nn u64) str() string {
return '0'
}
pub fn (b bool) str() string {
if b {
return 'true'
}
return 'false'
}
pub fn (n int) hex() string {
return '0'
}
pub fn (n i64) hex() string {
return '0'
}
pub fn (a []byte) contains(val byte) bool {
for aa in a {
if aa == val {
return true
}
}
return false
}
pub fn (c rune) str() string {
return '0'
}
pub fn (c byte) str() string {
return '0'
}
pub fn (c byte) is_capital() bool {
return c >= `A` && c <= `Z`
}
pub fn (b []byte) clone() []byte {
mut res := [byte(0)].repeat2(b.len)
for i := 0; i < b.len; i++ {
res[i] = b[i]
}
return res
}

70
vlib/builtin/js/map.v Normal file
View File

@ -0,0 +1,70 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
import strings
struct map {
obj voidptr
}
//fn (m mut map) insert(n mut mapnode, key string, val voidptr) {
//}
//////fn (n & mapnode) find(key string, out voidptr, element_size int) bool{
//return false
//}
// same as `find`, but doesn't return a value. Used by `exists`
//fn (n & mapnode) find2(key string, element_size int) bool{
//return false
//}
fn (m mut map) _set(key string, val voidptr) {
}
//fn preorder_keys(node &mapnode, keys mut []string, key_i int) int {
//return 0
//}
pub fn (m mut map) keys() []string {
return ['']
}
fn (m map) get(key string, out voidptr) bool {
return false
}
pub fn (m mut map) delete(key string) {
}
fn (m map) _exists(key string) bool {
return false
}
pub fn (m map) print() {
println('<<<<<<<<')
println('>>>>>>>>>>')
}
pub fn (m map) free() {
// C.free(m.table)
// C.free(m.keys_table)
}
pub fn (m map_string) str() string {
/*
if m.size == 0 {
return '{}'
}
*/
mut sb := strings.new_builder(50)
sb.writeln('{')
for key, val in m {
//sb.writeln(' "$key" => "$val"')
}
sb.writeln('}')
return sb.str()
}

31
vlib/builtin/js/option.v Normal file
View File

@ -0,0 +1,31 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
struct Option {
data [255]byte
error string
ok bool
}
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
fn opt_ok(data voidptr, size int) Option {
if size >= 255 {
panic('option size too big')
}
res := Option {
ok: true
}
C.memcpy(res.data, data, size)
return res
}
pub fn error(s string) Option {
return Option {
error: s
}
}

318
vlib/builtin/js/string.v Normal file
View File

@ -0,0 +1,318 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
struct string {
//mut:
//hash_cache int
pub:
str byteptr
len int
}
// For C strings only
fn C.strlen(s byteptr) int
fn todo() { }
pub fn (a string) clone() string {
return a
}
pub fn (s string) replace(rep, with_ string) string {
return s
}
pub fn (s string) int() int {
return 0
}
pub fn (s string) i64() i64 {
return 0
}
pub fn (s string) f32() f32 {
return 0.0
}
pub fn (s string) f64() f64 {
return 0.0
}
pub fn (s string) u32() u32 {
return u32(0)
}
pub fn (s string) u64() u64 {
return u64(0)
}
pub fn (s string) split(delim string) []string {
return s.split(delim)
}
pub fn (s string) split_single(delim byte) []string {
return s.split(delim.str())
}
pub fn (s string) split_into_lines() []string {
return s.split('\n')
}
// 'hello'.left(2) => 'he'
pub fn (s string) left(n int) string {
if n >= s.len {
return s
}
return s.substr(0, n)
}
// 'hello'.right(2) => 'llo'
pub fn (s string) right(n int) string {
if n >= s.len {
return ''
}
return s.substr(n, s.len)
}
pub fn (s string) substr(start, end int) string {
return 'a'
}
pub fn (s string) index(p string) int {
return -1
}
pub fn (s string) index_any(chars string) int {
return -1
}
pub fn (s string) last_index(p string) int {
return -1
}
pub fn (s string) index_after(p string, start int) int {
return -1
}
// counts occurrences of substr in s
pub fn (s string) count(substr string) int {
return 0 // TODO can never get here - v doesn't know that
}
pub fn (s string) contains(p string) bool {
return false
}
pub fn (s string) starts_with(p string) bool {
return false
}
pub fn (s string) ends_with(p string) bool {
return false
}
// TODO only works with ASCII
pub fn (s string) to_lower() string {
return s
}
pub fn (s string) to_upper() string {
return s
}
pub fn (s string) capitalize() string {
return s
}
pub fn (s string) title() string {
return s
}
// 'hey [man] how you doin'
// find_between('[', ']') == 'man'
pub fn (s string) find_between(start, end string) string {
start_pos := s.index(start)
if start_pos == -1 {
return ''
}
// First get everything to the right of 'start'
val := s.right(start_pos + start.len)
end_pos := val.index(end)
if end_pos == -1 {
return val
}
return val.left(end_pos)
}
// TODO generic
pub fn (ar []string) contains(val string) bool {
for s in ar {
if s == val {
return true
}
}
return false
}
// TODO generic
pub fn (ar []int) contains(val int) bool {
for i, s in ar {
if s == val {
return true
}
}
return false
}
fn is_space(c byte) bool {
return C.isspace(c)
}
pub fn (c byte) is_space() bool {
return is_space(c)
}
pub fn (s string) trim_space() string {
#return s.str.trim(' ');
return ''
}
pub fn (s string) trim(cutset string) string {
#return s.str.trim(cutset);
return ''
}
pub fn (s string) trim_left(cutset string) string {
#return s.str.trimLeft(cutset);
return ''
}
pub fn (s string) trim_right(cutset string) string {
#return s.str.trimRight(cutset);
return ''
}
// fn print_cur_thread() {
// //C.printf("tid = %08x \n", pthread_self());
// }
pub fn (s mut []string) sort() {
}
pub fn (s mut []string) sort_ignore_case() {
}
pub fn (s mut []string) sort_by_len() {
}
fn (s string) at(idx int) byte {
if idx < 0 || idx >= s.len {
panic('string index out of range')
}
return s.str[idx]
}
pub fn (c byte) is_digit() bool {
return c >= `0` && c <= `9`
}
pub fn (c byte) is_hex_digit() bool {
return c.is_digit() || (c >= `a` && c <= `f`) || (c >= `A` && c <= `F`)
}
pub fn (c byte) is_oct_digit() bool {
return c >= `0` && c <= `7`
}
pub fn (c byte) is_letter() bool {
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`)
}
pub fn (s string) free() {
}
/*
fn (arr []string) free() {
for s in arr {
s.free()
}
C.free(arr.data)
}
*/
// all_before('23:34:45.234', '.') == '23:34:45'
pub fn (s string) all_before(dot string) string {
pos := s.index(dot)
if pos == -1 {
return s
}
return s.left(pos)
}
pub fn (s string) all_before_last(dot string) string {
pos := s.last_index(dot)
if pos == -1 {
return s
}
return s.left(pos)
}
pub fn (s string) all_after(dot string) string {
pos := s.last_index(dot)
if pos == -1 {
return s
}
return s.right(pos + dot.len)
}
// fn (s []string) substr(a, b int) string {
// return join_strings(s.slice_fast(a, b))
// }
pub fn (a []string) join(del string) string {
return ''
}
pub fn (s []string) join_lines() string {
return s.join('\n')
}
pub fn (s string) reverse() string {
return s
}
pub fn (s string) limit(max int) string {
if s.len <= max {
return s
}
return s.substr(0, max)
}
// TODO is_white_space()
pub fn (c byte) is_white() bool {
i := int(c)
return i == 10 || i == 32 || i == 9 || i == 13 || c == `\r`
}
pub fn (s string) hash() int {
//mut h := s.hash_cache
mut h := 0
if h == 0 && s.len > 0 {
for c in s {
h = h * 31 + int(c)
}
}
return h
}
pub fn (s string) bytes() []byte {
if s.len == 0 {
return []byte
}
mut buf := [byte(0)].repeat2(s.len)
C.memcpy(buf.data, s.str, s.len)
return buf
}

27
vlib/builtin/js/utf8.v Normal file
View File

@ -0,0 +1,27 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module builtin
pub fn utf8_char_len(b byte) int {
return (( 0xe5000000 >> (( b >> 3 ) & 0x1e )) & 3 ) + 1
}
// Convert utf32 to utf8
// utf32 == Codepoint
pub fn utf32_to_str(code u32) string {
return ''
}
// TODO copypasta
pub fn utf32_to_str_no_malloc(code u32, buf voidptr) string {
return ''
}
// Convert utf8 to utf32
pub fn (_rune string) utf32_code() int {
return 0
}