From 1174f2e9bd7a320291b5c2dfe3cd730a93ade084 Mon Sep 17 00:00:00 2001 From: "Fuji, Goro" Date: Sun, 25 Aug 2013 10:51:45 +0900 Subject: [PATCH 1/3] allow `object as string` as a (logically) shortcut of `o != null ? o.toString() : "null"` --- src/expression.jsx | 6 +++++- src/jsemitter.jsx | 5 +++++ t/run/346.object-as-string.jsx | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 t/run/346.object-as-string.jsx diff --git a/src/expression.jsx b/src/expression.jsx index e334d09a..eb6ecab8 100644 --- a/src/expression.jsx +++ b/src/expression.jsx @@ -989,7 +989,11 @@ class AsExpression extends UnaryExpression { // possibly unsafe conversions var exprType = this._expr.getType().resolveIfNullable(); var success = false; - if (exprType.equals(Type.nullType)) { + if (this._type.equals(Type.stringType)) { + // ok: any => string + success = true; + } + else if (exprType.equals(Type.nullType)) { if (this._type instanceof ObjectType || this._type instanceof FunctionType) { // ok success = true; diff --git a/src/jsemitter.jsx b/src/jsemitter.jsx index 8099c935..f5af8ea7 100644 --- a/src/jsemitter.jsx +++ b/src/jsemitter.jsx @@ -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 diff --git a/t/run/346.object-as-string.jsx b/t/run/346.object-as-string.jsx new file mode 100644 index 00000000..c3804d3f --- /dev/null +++ b/t/run/346.object-as-string.jsx @@ -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: From 6b7f7367513f0a5132887354c155b6909f6aa6c6 Mon Sep 17 00:00:00 2001 From: "Fuji, Goro" Date: Sun, 25 Aug 2013 11:06:28 +0900 Subject: [PATCH 2/3] there's no reason to allow `null as string` tested in t/compile_error/144.null-as-primitive.jsx --- src/expression.jsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/expression.jsx b/src/expression.jsx index eb6ecab8..1ed12212 100644 --- a/src/expression.jsx +++ b/src/expression.jsx @@ -989,15 +989,14 @@ class AsExpression extends UnaryExpression { // possibly unsafe conversions var exprType = this._expr.getType().resolveIfNullable(); var success = false; - if (this._type.equals(Type.stringType)) { - // ok: any => string - success = true; - } - else if (exprType.equals(Type.nullType)) { + if (exprType.equals(Type.nullType)) { if (this._type instanceof ObjectType || this._type instanceof FunctionType) { // 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 From b9dc1461bd46d4b8a7a4f65a3242ef0091f62192 Mon Sep 17 00:00:00 2001 From: "Fuji, Goro" Date: Sun, 25 Aug 2013 12:11:38 +0900 Subject: [PATCH 3/3] add another test for `object as string` --- t/run/347.generic-as-string.jsx | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 t/run/347.generic-as-string.jsx diff --git a/t/run/347.generic-as-string.jsx b/t/run/347.generic-as-string.jsx new file mode 100644 index 00000000..d882777f --- /dev/null +++ b/t/run/347.generic-as-string.jsx @@ -0,0 +1,29 @@ +/*EXPECTED +C: foo +C: _Main +C: null +*/ + +class C. { + 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.("foo").debugLog(); + new C.<_Main>(new _Main).debugLog(); + new C.<_Main>(null).debugLog(); + } +} +// vim: set expandtab tabstop=2 shiftwidth=2 ft=jsx: