Skip to content
Browse files

support operators ".", "[]" against variant, to ease the pain of deco…

…ding JSON
  • Loading branch information...
1 parent db2778c commit a9bb09d64029ffedc140673a53fa8c38e5c0ae04 @kazuho kazuho committed Jun 4, 2012
View
29 src/expression.js
@@ -1081,8 +1081,8 @@ var PropertyExpression = exports.PropertyExpression = UnaryExpression.extend({
return false;
}
if (exprType.resolveIfMayBeUndefined().equals(Type.variantType)) {
- context.errors.push(new CompileError(this._identifierToken, "cannot obtain a member of variant, use: (<<expr>> as Map.<type>)[<<name>>]"));
- return false;
+ this._type = Type.variantType;
+ return true;
}
var classDef = exprType.getClassDef();
if (classDef == null) {
@@ -1319,11 +1319,18 @@ var ArrayExpression = exports.ArrayExpression = BinaryExpression.extend({
}
// obtain classDef
var expr1Type = this._expr1.getType().resolveIfMayBeUndefined();
- if (! (expr1Type instanceof ObjectType)) {
- context.errors.push(new CompileError(this._token, "cannot apply operator[] against a non-object"));
- return false;
+ if (expr1Type instanceof ObjectType) {
+ return this._analyzeApplicationOnObject(expr1Type);
+ } else if (expr1Type.equals(Type.variantType)) {
+ return this._analyzeApplicationOnVariant();
+ return true;
}
- var expr1ClassDef = expr1Type.getClassDef();;
+ context.errors.push(new CompileError(this._token, "cannot apply []; the operator is only applicable against an array or an variant"));
+ return false;
+ },
+
+ _analyzeApplicationOnObject: function (expr1Type) {
+ var expr1ClassDef = expr1Type.getClassDef();
// obtain type of operator []
var accessorType = expr1ClassDef.getMemberTypeByName("__native_index_operator__", false, ClassDefinition.GET_MEMBER_MODE_ALL);
if (accessorType == null) {
@@ -1348,6 +1355,16 @@ var ArrayExpression = exports.ArrayExpression = BinaryExpression.extend({
return true;
},
+ _analyzeApplicationOnVariant: function () {
+ var expr2Type = this._expr2.getType().resolveIfMayBeUndefined();
+ if (! (expr2Type.equals(Type.stringType) || expr2Type.isConvertibleTo(Type.numberType))) {
+ context.errors.push(new CompileError("the argument of variant[] should be a string or a number"));
+ return false;
+ }
+ this._type = Type.variantType;
+ return true;
+ },
+
getType: function () {
return this._type;
},
View
6 t/compile_error/106.property-expr-on-variant.jsx
@@ -1,6 +0,0 @@
-class Test {
- static function run() : void {
- var v : variant = 0;
- var g = v.a;
- }
-}
View
36 t/run/137.variant-accessors.jsx
@@ -0,0 +1,36 @@
+/*EXPECTED
+1
+hello
+bar
+bar
+bar
+bar
+2
+hello
+world
+*/
+class Test {
+ static function run() : void {
+ var j : variant = {
+ n: 1,
+ s: "hello",
+ m: {
+ foo: "bar"
+ },
+ a: [
+ "hello",
+ "world"
+ ]
+ } : Map.<variant>;
+ log j.n;
+ log j["s"];
+ log j.m.foo;
+ log j.m["foo"];
+ log j["m"].foo;
+ log j["m"]["foo"];
+ log j.a.length;
+ for (var i = 0; i < j.a.length as number; ++i) {
+ log j.a[i];
+ }
+ }
+}
View
26 t/run/138.json-complex.jsx
@@ -0,0 +1,26 @@
+/*EXPECTED
+1
+hello
+bar
+bar
+bar
+bar
+2
+hello
+world
+*/
+class Test {
+ static function run() : void {
+ var j = JSON.parse('{"n":1,"s":"hello","m":{"foo":"bar"},"a":["hello","world"]}');
+ log j.n;
+ log j["s"];
+ log j.m.foo;
+ log j.m["foo"];
+ log j["m"].foo;
+ log j["m"]["foo"];
+ log j.a.length;
+ for (var i = 0; i < j.a.length as number; ++i) {
+ log j.a[i];
+ }
+ }
+}

0 comments on commit a9bb09d

Please sign in to comment.
Something went wrong with that request. Please try again.