mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
js: fix string.bytes codegen, readline, add tests for strings (#12060)
This commit is contained in:
@@ -111,10 +111,18 @@ fn (mut g JsGen) sym_to_js_typ(sym ast.TypeSymbol) string {
|
||||
return styp
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn (mut g JsGen) base_type(t ast.Type) string {
|
||||
mut styp := g.cc_type(t, true)
|
||||
return styp
|
||||
}
|
||||
*/
|
||||
fn (mut g JsGen) base_type(_t ast.Type) string {
|
||||
t := g.unwrap_generic(_t)
|
||||
share := t.share()
|
||||
mut styp := if share == .atomic_t { t.atomic_typename() } else { g.cc_type(t, true) }
|
||||
return styp
|
||||
}
|
||||
|
||||
pub fn (mut g JsGen) typ(t ast.Type) string {
|
||||
sym := g.table.get_final_type_symbol(t)
|
||||
|
||||
@@ -106,7 +106,7 @@ fn (mut g JsGen) method_call(node ast.CallExpr) {
|
||||
g.inc_indent()
|
||||
g.writeln('try {')
|
||||
g.inc_indent()
|
||||
g.write('return builtin.unwrap(')
|
||||
g.write('return unwrap(')
|
||||
}
|
||||
if node.name == 'str' {
|
||||
mut rec_type := node.receiver_type
|
||||
|
||||
@@ -8,7 +8,8 @@ fn (mut g JsGen) gen_plain_infix_expr(node ast.InfixExpr) {
|
||||
l_sym := g.table.get_final_type_symbol(it.left_type)
|
||||
r_sym := g.table.get_final_type_symbol(it.right_type)
|
||||
greater_typ := g.greater_typ(it.left_type, it.right_type)
|
||||
g.write('new ${g.typ(greater_typ)}( ')
|
||||
cast_ty := if greater_typ == it.left_type { l_sym.cname } else { r_sym.cname }
|
||||
g.write('new ${g.js_name(cast_ty)}( ')
|
||||
g.cast_stack << greater_typ
|
||||
if (l_sym.kind == .i64 || l_sym.kind == .u64) || (r_sym.kind == .i64 || r_sym.kind == .u64) {
|
||||
g.write('BigInt(')
|
||||
|
||||
@@ -1398,7 +1398,7 @@ fn (mut g JsGen) gen_expr_stmt_no_semi(it ast.ExprStmt) {
|
||||
|
||||
// cc_type whether to prefix 'struct' or not (C__Foo -> struct Foo)
|
||||
fn (mut g JsGen) cc_type(typ ast.Type, is_prefix_struct bool) string {
|
||||
sym := g.table.get_final_type_symbol(g.unwrap_generic(typ))
|
||||
sym := g.table.get_type_symbol(g.unwrap_generic(typ))
|
||||
mut styp := sym.cname
|
||||
match mut sym.info {
|
||||
ast.Struct, ast.Interface, ast.SumType {
|
||||
@@ -2306,10 +2306,10 @@ fn (mut g JsGen) gen_index_expr(expr ast.IndexExpr) {
|
||||
left_typ := g.table.get_type_symbol(expr.left_type)
|
||||
// TODO: Handle splice setting if it's implemented
|
||||
if expr.index is ast.RangeExpr {
|
||||
if left_typ.kind == .array {
|
||||
g.write('array_slice(')
|
||||
} else {
|
||||
if left_typ.kind == .string {
|
||||
g.write('string_slice(')
|
||||
} else {
|
||||
g.write('array_slice(')
|
||||
}
|
||||
g.expr(expr.left)
|
||||
if expr.left_type.is_ptr() {
|
||||
|
||||
Reference in New Issue
Block a user