Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow object as string #243

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/expression.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,9 @@ class AsExpression extends UnaryExpression {
// ok
success = true;
}
} else if (this._type.equals(Type.stringType)) {
// ok: any-but-null => string
success = true;
} else if (exprType instanceof PrimitiveType) {
if (this._type instanceof PrimitiveType) {
// ok: primitive => primitive
Expand Down
5 changes: 5 additions & 0 deletions src/jsemitter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,11 @@ class _AsExpressionEmitter extends _ExpressionEmitter {
new _AsNoConvertExpressionEmitter(this._emitter, new AsNoConvertExpression(this._expr.getToken(), this._expr.getExpr(), this._expr.getType())).emit(outerOpPrecedence);
return;
}
if (destType.equals(Type.stringType)) {
var prec = _AdditiveExpressionEmitter._operatorPrecedence;
this._emitWithParens(outerOpPrecedence, prec, prec, null, " + \"\"");
return;
}
}
if (srcType.resolveIfNullable().equals(Type.booleanType)) {
// from boolean
Expand Down
18 changes: 18 additions & 0 deletions t/run/346.object-as-string.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*EXPECTED
foo
null
*/

class _Main {
override function toString() : string {
return "foo";
}

static function main(args : string[]) : void {
var object = new _Main;
log object as string;
object = null;
log object as string;
}
}
// vim: set expandtab tabstop=2 shiftwidth=2 ft=jsx:
29 changes: 29 additions & 0 deletions t/run/347.generic-as-string.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*EXPECTED
C: foo
C: _Main
C: null
*/

class C.<T> {
var value : T;
function constructor(value : T) {
this.value = value;
}

function debugLog() : void {
log "C: " + this.value as string;
}
}

class _Main {
override function toString() : string {
return "_Main";
}

static function main(args : string[]) : void {
new C.<string>("foo").debugLog();
new C.<_Main>(new _Main).debugLog();
new C.<_Main>(null).debugLog();
}
}
// vim: set expandtab tabstop=2 shiftwidth=2 ft=jsx: