js: generate es5 methods for valueOf() and toString() (#12928)
Co-authored-by: pancake <pancake@nopcode.org>pull/12938/head
parent
2693631643
commit
cb65f2ff44
|
@ -308,12 +308,22 @@ fn (mut g JsGen) gen_builtin_prototype(c BuiltinPrototypeConfig) {
|
|||
g.writeln('$c.extras,')
|
||||
}
|
||||
|
||||
g.writeln('valueOf() { return $c.value_of },')
|
||||
g.writeln('toString() { return $c.to_string },')
|
||||
// g.writeln('eq(other) { return $c.eq },')
|
||||
g.writeln('\$toJS() { return $c.to_jsval }, ')
|
||||
if c.has_strfn {
|
||||
g.writeln('str() { return new string(this.toString()) }')
|
||||
if g.pref.output_es5 {
|
||||
g.writeln('valueOf: (function() { return $c.value_of }).bind(this),')
|
||||
g.writeln('toString: (function() { return $c.to_string }).bind(this),')
|
||||
g.writeln('\$toJS: (function() { return $c.to_jsval }).bind(this), ')
|
||||
if c.has_strfn {
|
||||
g.writeln('str: (function() { return new string(this.toString())).bind(this) }')
|
||||
}
|
||||
// g.writeln('eq: (function(other) { return $c.eq }).bind(this),')
|
||||
} else {
|
||||
g.writeln('valueOf() { return $c.value_of },')
|
||||
g.writeln('toString() { return $c.to_string },')
|
||||
g.writeln('\$toJS() { return $c.to_jsval }, ')
|
||||
if c.has_strfn {
|
||||
g.writeln('str() { return new string(this.toString()) }')
|
||||
}
|
||||
// g.writeln('eq(other) { return $c.eq },')
|
||||
}
|
||||
g.dec_indent()
|
||||
g.writeln('};\n')
|
||||
|
|
|
@ -1914,7 +1914,11 @@ fn (mut g JsGen) gen_struct_decl(node ast.StructDecl) {
|
|||
// gen toString method
|
||||
fn_names := fns.map(it.name)
|
||||
if 'toString' !in fn_names {
|
||||
g.writeln('toString() {')
|
||||
if g.pref.output_es5 {
|
||||
g.writeln('toString: (function() {')
|
||||
} else {
|
||||
g.writeln('toString() {')
|
||||
}
|
||||
g.inc_indent()
|
||||
g.write('return `$js_name {')
|
||||
for i, field in node.fields {
|
||||
|
@ -1930,7 +1934,11 @@ fn (mut g JsGen) gen_struct_decl(node ast.StructDecl) {
|
|||
}
|
||||
g.writeln('}`')
|
||||
g.dec_indent()
|
||||
g.writeln('},')
|
||||
if g.pref.output_es5 {
|
||||
g.writeln('}).bind(this),')
|
||||
} else {
|
||||
g.writeln('},')
|
||||
}
|
||||
}
|
||||
for field in node.fields {
|
||||
typ := g.typ(field.typ)
|
||||
|
@ -1946,8 +1954,11 @@ fn (mut g JsGen) gen_struct_decl(node ast.StructDecl) {
|
|||
g.writeln(',')
|
||||
}
|
||||
}
|
||||
g.writeln('\$toJS() { return this; }')
|
||||
|
||||
if g.pref.output_es5 {
|
||||
g.writeln('\$toJS: (function() { return this; }).bind(this)')
|
||||
} else {
|
||||
g.writeln('\$toJS() { return this; }')
|
||||
}
|
||||
g.writeln('};\n')
|
||||
g.dec_indent()
|
||||
|
||||
|
|
Loading…
Reference in New Issue