Fixing string keys in pattern matching on objects (ticket 325)

This commit is contained in:
Jeremy Ashkenas
2010-04-25 11:07:09 -04:00
parent 212ad45be4
commit 08c877ec7b
3 changed files with 13 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
(function(){
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, compact, del, flatten, helpers, literal, merge, statement, utility;
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, CurryNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, IndexNode, LiteralNode, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, Scope, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, compact, del, flatten, helpers, literal, merge, statement, utility;
var __extends = function(child, parent) {
var ctor = function(){ };
ctor.prototype = parent.prototype;
@@ -927,7 +927,7 @@
// See the [ECMAScript Harmony Wiki](http://wiki.ecmascript.org/doku.php?id=harmony:destructuring)
// for details.
AssignNode.prototype.compile_pattern_match = function compile_pattern_match(o) {
var _a, _b, _c, access_class, assigns, code, i, idx, obj, oindex, olength, splat, val, val_var, value;
var _a, _b, _c, access_class, assigns, code, i, idx, is_string, obj, oindex, olength, splat, val, val_var, value;
val_var = o.scope.free_variable();
value = this.value.is_statement() ? ClosureNode.wrap(this.value) : this.value;
assigns = [("" + this.tab + val_var + " = " + (value.compile(o)) + ";")];
@@ -943,7 +943,8 @@
obj = _c[0];
idx = _c[1];
}
access_class = this.variable.is_array() ? IndexNode : AccessorNode;
is_string = idx.value && idx.value.match(IS_STRING);
access_class = is_string || this.variable.is_array() ? IndexNode : AccessorNode;
if (obj instanceof SplatNode && !splat) {
val = literal(obj.compile_value(o, val_var, (oindex = this.variable.base.objects.indexOf(obj)), (olength = this.variable.base.objects.length) - oindex - 1));
splat = true;
@@ -1755,6 +1756,8 @@
TRAILING_WHITESPACE = /\s+$/gm;
// Keep this identifier regex in sync with the Lexer.
IDENTIFIER = /^[a-zA-Z\$_](\w|\$)*$/;
// Is a literal value a string?
IS_STRING = /^['"]/;
// Utility Functions
// -----------------
// Handy helper for a generating LiteralNode.