js: make vlib/v/util compile on JS backend (#12660)
parent
be5446bfa4
commit
0da7e2f8ab
|
@ -83,3 +83,9 @@ pub fn error_with_code(message string, code int) IError {
|
|||
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
|
||||
}
|
||||
|
|
|
@ -955,3 +955,23 @@ pub fn tos(jsstr JS.String) string {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -11,3 +11,7 @@ pub fn utf8_str_visible_length(s string) int {
|
|||
pub fn utf8_str_len(s string) int {
|
||||
return s.len
|
||||
}
|
||||
|
||||
pub fn utf8_char_len(b byte) int {
|
||||
return ((0xe5000000 >> ((b >> 3) & 0x1e)) & 3) + 1
|
||||
}
|
||||
|
|
|
@ -125,3 +125,14 @@ pub fn getwd() string {
|
|||
|
||||
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')
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -282,12 +282,14 @@ pub fn cached_read_source_file(path string) ?string {
|
|||
if isnil(cache) {
|
||||
cache = &SourceCache{}
|
||||
}
|
||||
|
||||
if path.len == 0 {
|
||||
unsafe { cache.sources.free() }
|
||||
unsafe { free(cache) }
|
||||
cache = &SourceCache(0)
|
||||
return error('memory source file cache cleared')
|
||||
}
|
||||
|
||||
// eprintln('>> cached_read_source_file path: $path')
|
||||
if res := cache.sources[path] {
|
||||
// eprintln('>> cached')
|
||||
|
@ -300,26 +302,6 @@ pub fn cached_read_source_file(path string) ?string {
|
|||
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 {
|
||||
return match s {
|
||||
'+' { '_plus' }
|
||||
|
@ -528,3 +510,7 @@ pub fn free_caches() {
|
|||
cached_read_source_file('') or { '' }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_file(file_path string) ?string {
|
||||
return unsafe { cached_read_source_file(file_path) }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue