js: make vlib/v/util compile on JS backend (#12660)

pull/12666/head
playX 2021-12-03 13:25:36 +03:00 committed by GitHub
parent be5446bfa4
commit 0da7e2f8ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 20 deletions

View File

@ -83,3 +83,9 @@ pub fn error_with_code(message string, code int) IError {
code: code code: code
} }
} }
// free allows for manually freeing memory allocated at the address `ptr`. no-op on JS backend
[unsafe]
pub fn free(ptr voidptr) {
_ := ptr
}

View File

@ -955,3 +955,23 @@ pub fn tos(jsstr JS.String) string {
return res return res
} }
pub fn (s string) compare(a string) int {
min_len := if s.len < a.len { s.len } else { a.len }
for i in 0 .. min_len {
if s[i] < a[i] {
return -1
}
if s[i] > a[i] {
return 1
}
}
if s.len < a.len {
return -1
}
if s.len > a.len {
return 1
}
return 0
}

View File

@ -11,3 +11,7 @@ pub fn utf8_str_visible_length(s string) int {
pub fn utf8_str_len(s string) int { pub fn utf8_str_len(s string) int {
return s.len return s.len
} }
pub fn utf8_char_len(b byte) int {
return ((0xe5000000 >> ((b >> 3) & 0x1e)) & 3) + 1
}

View File

@ -125,3 +125,14 @@ pub fn getwd() string {
return res return res
} }
pub fn getuid() int {
res := 0
#if (process.getuid) res.val = process.getuid();
return res
}
pub fn execvp(cmd string, args []string) ? {
panic('os.execvp() is not available on JS backend')
}

View File

@ -0,0 +1,17 @@
module util
pub fn skip_bom(file_content string) string {
mut raw_text := file_content
// BOM check
if raw_text.len >= 3 {
unsafe {
c_text := raw_text.str
if c_text[0] == 0xEF && c_text[1] == 0xBB && c_text[2] == 0xBF {
// skip three BOM bytes
offset_from_begin := 3
raw_text = tos(c_text[offset_from_begin], vstrlen(c_text) - offset_from_begin)
}
}
}
return raw_text
}

View File

@ -0,0 +1,15 @@
module util
pub fn skip_bom(file_content string) string {
mut raw_text := file_content
if raw_text.len >= 3 {
js_text := raw_text.str
_ := js_text
#if (js_text.charCodeAt(0) == 0xEF && js_text.charCodeAt(1) == 0xBB && js_text.charCodeAt(2) == 0xBF)
{
#raw_text.str = js_text.slice(3,js_text.length);
}
}
return raw_text
}

View File

@ -282,12 +282,14 @@ pub fn cached_read_source_file(path string) ?string {
if isnil(cache) { if isnil(cache) {
cache = &SourceCache{} cache = &SourceCache{}
} }
if path.len == 0 { if path.len == 0 {
unsafe { cache.sources.free() } unsafe { cache.sources.free() }
unsafe { free(cache) } unsafe { free(cache) }
cache = &SourceCache(0) cache = &SourceCache(0)
return error('memory source file cache cleared') return error('memory source file cache cleared')
} }
// eprintln('>> cached_read_source_file path: $path') // eprintln('>> cached_read_source_file path: $path')
if res := cache.sources[path] { if res := cache.sources[path] {
// eprintln('>> cached') // eprintln('>> cached')
@ -300,26 +302,6 @@ pub fn cached_read_source_file(path string) ?string {
return res return res
} }
pub fn read_file(file_path string) ?string {
return unsafe { cached_read_source_file(file_path) }
}
pub fn skip_bom(file_content string) string {
mut raw_text := file_content
// BOM check
if raw_text.len >= 3 {
unsafe {
c_text := raw_text.str
if c_text[0] == 0xEF && c_text[1] == 0xBB && c_text[2] == 0xBF {
// skip three BOM bytes
offset_from_begin := 3
raw_text = tos(c_text[offset_from_begin], vstrlen(c_text) - offset_from_begin)
}
}
}
return raw_text
}
pub fn replace_op(s string) string { pub fn replace_op(s string) string {
return match s { return match s {
'+' { '_plus' } '+' { '_plus' }
@ -528,3 +510,7 @@ pub fn free_caches() {
cached_read_source_file('') or { '' } cached_read_source_file('') or { '' }
} }
} }
pub fn read_file(file_path string) ?string {
return unsafe { cached_read_source_file(file_path) }
}