Skip to content

Commit 13921ac

Browse files
committed
Create a scoped flag to select JS object vs hash literals
1 parent 3f57808 commit 13921ac

18 files changed

+41
-22
lines changed

=template.pyj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# vim:fileencoding=utf-8
22
# License: BSD Copyright: %YEAR%, %USER% <%MAIL%>
3+
from __python__ import hash_literals
34

45
%HERE%

src/ast.pyj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# vim:fileencoding=utf-8
2-
# License: BSD
2+
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3+
from __python__ import hash_literals
34

45
from utils import noop, string_template
56

@@ -286,11 +287,12 @@ AST_SetComprehension = DEFNODE('SetComprehension', None, {
286287
'$documentation': 'A set comprehension',
287288
}, AST_ListComprehension)
288289

289-
AST_DictComprehension = DEFNODE('DictComprehension', 'value_statement is_pydict', {
290+
AST_DictComprehension = DEFNODE('DictComprehension', 'value_statement is_pydict is_jshash', {
290291
'$documentation': 'A set comprehension',
291292
'$propdoc': {
292293
'value_statement': "[AST_Node] statement to perform on each value before returning it",
293294
'is_pydict': "[bool] True if this comprehension is for a python dict",
295+
'is_jshash': "[bool] True if this comprehension is for a js hash",
294296
},
295297
'_walk': def(visitor):
296298
node = this
@@ -944,11 +946,12 @@ AST_Array = DEFNODE("Array", "elements", {
944946
,
945947
})
946948

947-
AST_Object = DEFNODE("Object", "properties is_pydict", {
949+
AST_Object = DEFNODE("Object", "properties is_pydict is_jshash", {
948950
'$documentation': "An object literal",
949951
'$propdoc': {
950952
'properties': "[AST_ObjectProperty*] array of properties",
951-
'is_pydict': "[bool] True if this object is a python dict literal"
953+
'is_pydict': "[bool] True if this object is a python dict literal",
954+
'is_jshash': "[bool] True if this object is a js hash literal",
952955
},
953956
'_walk': def(visitor):
954957
node = this

src/errors.pyj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vim:fileencoding=utf-8
2-
# License: BSD
3-
# Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
2+
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3+
from __python__ import hash_literals
44

55
class SyntaxError(Error):
66

src/lib/gettext.pyj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def install(translation_data):
518518
t.install()
519519
return t
520520

521-
has_prop = Object.prototype.hasOwnProperty
521+
has_prop = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty)
522522

523523
class Translations:
524524

@@ -536,14 +536,14 @@ class Translations:
536536
def gettext(self, text):
537537
for t in self.translations:
538538
m = t[0]['entries']
539-
if has_prop.call(m, text):
539+
if has_prop(m, text):
540540
return m[text][0]
541541
return text
542542

543543
def ngettext(self, text, plural, n):
544544
for t in self.translations:
545545
m = t[0]['entries']
546-
if has_prop.call(m, text):
546+
if has_prop(m, text):
547547
idx = t[1](n)
548548
return m[text][idx] or (text if n is 1 else plural)
549549
return text if n is 1 else plural

src/output/classes.pyj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# vim:fileencoding=utf-8
22
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3+
from __python__ import hash_literals
34

45
from ast import AST_Class, AST_Method
56
from output.functions import decorate, function_definition, function_annotation

src/output/codegen.pyj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# vim:fileencoding=utf-8
2-
# License: BSD
2+
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3+
from __python__ import hash_literals
34

45
# globals:console,regenerate,writefile
56

@@ -20,7 +21,6 @@ from ast import (
2021
AST_Verbatim, AST_While, AST_With, AST_Yield, TreeWalker, AST_Existential
2122
)
2223
from output.exceptions import print_try, print_catch, print_finally
23-
from output.stream import OutputStream
2424
from output.classes import print_class
2525
from output.literals import print_array, print_obj_literal, print_object, print_set, print_regexp
2626
from output.loops import print_do_loop, print_while_loop, print_for_loop_body, print_for_in, print_list_comprehension

src/output/exceptions.pyj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# vim:fileencoding=utf-8
22
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3+
from __python__ import hash_literals
34

45
from output.statements import print_bracketed
56

src/output/functions.pyj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# vim:fileencoding=utf-8
22
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
33
# globals:regenerate
4+
from __python__ import hash_literals
45

56
from ast import AST_ClassCall, AST_New, has_calls, AST_Dot, AST_SymbolRef
67
from output.stream import OutputStream

src/output/literals.pyj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# vim:fileencoding=utf-8
22
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3+
from __python__ import hash_literals
34

45
from ast import AST_Binary
56

@@ -30,7 +31,7 @@ def print_obj_literal(self, output):
3031
if self.is_pydict:
3132
output.spaced.apply(output, 'var ρσ_d = ρσ_dict()'.split(' '))
3233
else:
33-
output.spaced.apply(output, 'var ρσ_d = Object.create(null)'.split(' '))
34+
output.spaced('var', 'ρσ_d', '=', ('Object.create(null)' if self.is_jshash else '{}'))
3435
output.end_statement()
3536
self.properties.forEach(def(prop, i):
3637
output.indent()
@@ -65,7 +66,7 @@ def print_object(self, output):
6566
if self.properties.length > 0:
6667
print_obj_literal(self, output)
6768
else:
68-
output.print("Object.create(null)")
69+
output.print("Object.create(null)" if self.is_jshash else '{}')
6970

7071
def print_set(self, output):
7172
if self.items.length is 0:

src/output/loops.pyj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# vim:fileencoding=utf-8
22
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
33
# globals: regenerate
4+
from __python__ import hash_literals
45

56
from ast import AST_BaseCall, AST_SymbolRef, AST_Array, AST_Unary, AST_Number, has_calls
67
from output.stream import OutputStream
@@ -207,7 +208,7 @@ def print_for_in(self, output):
207208
self._do_print_body(output)
208209

209210
def print_list_comprehension(self, output):
210-
result_obj = {'ListComprehension':'[]', 'DictComprehension':'Object.create(null)', 'SetComprehension':'ρσ_set()'}[self.TYPE]
211+
result_obj = {'ListComprehension':'[]', 'DictComprehension':('Object.create(null)' if self.is_jshash else '{}'), 'SetComprehension':'ρσ_set()'}[self.TYPE]
211212
is_generator = self.TYPE is 'GeneratorComprehension'
212213
es5 = output.option('js_version') is 5
213214
if self.TYPE is 'DictComprehension':

0 commit comments

Comments
 (0)