Skip to content

Commit 53d66da

Browse files
author
hartsantler
committed
optimized core
1 parent 1d11db2 commit 53d66da

4 files changed

Lines changed: 222 additions & 97 deletions

File tree

pythonjs.js

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// PythonScript Runtime - regenerated on: Fri Nov 22 01:47:08 2013
1+
// PythonScript Runtime - regenerated on: Fri Nov 22 16:33:44 2013
22
__NULL_OBJECT__ = Object.create(null);
33
if ("window" in this && "document" in this) {
44
__NODEJS__ = false;
@@ -47,25 +47,20 @@ adapt_arguments = function(handler) {
4747
__get__ = function(object, attribute) {
4848
"Retrieve an attribute, method, property, or wrapper function.\n\n method are actually functions which are converted to methods by\n prepending their arguments with the current object. Properties are\n not functions!\n\n DOM support:\n http://stackoverflow.com/questions/14202699/document-createelement-not-working\n https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof\n\n Direct JavaScript Calls:\n if an external javascript function is found, and it was not a wrapper that was generated here,\n check the function for a 'cached_wrapper' attribute, if none is found then generate a new\n wrapper, cache it on the function, and return the wrapper.\n ";
4949
if (attribute == "__call__") {
50-
if ({}.toString.call(object) === '[object Function]') {
51-
if (object.pythonscript_function === true) {
52-
return object;
50+
if (object.pythonscript_function || object.is_wrapper) {
51+
return object;
52+
} else {
53+
if (object.cached_wrapper) {
54+
return object.cached_wrapper;
5355
} else {
54-
if (object.is_wrapper !== undefined) {
55-
return object;
56-
} else {
57-
var cached = object.cached_wrapper;
58-
if (cached) {
59-
return cached;
60-
} else {
61-
var wrapper = function(args, kwargs) {
62-
return object.apply(undefined, args);
63-
}
64-
65-
wrapper.is_wrapper = true;
66-
object.cached_wrapper = wrapper;
67-
return wrapper;
56+
if ({}.toString.call(object) === '[object Function]') {
57+
var wrapper = function(args, kwargs) {
58+
return object.apply(undefined, args);
6859
}
60+
61+
wrapper.is_wrapper = true;
62+
object.cached_wrapper = wrapper;
63+
return wrapper;
6964
}
7065
}
7166
}
@@ -126,25 +121,17 @@ __get__ = function(object, attribute) {
126121
}
127122
if (attribute in __class__.__unbound_methods__) {
128123
attr = __class__.__unbound_methods__[attribute];
129-
var method = function() {
130-
var args;
131-
args = Array.prototype.slice.call(arguments);
132-
if (args[0] instanceof Array && {}.toString.call(args[1]) === '[object Object]' && args.length == 2) {
133-
/*pass*/
134-
} else {
135-
args = [args, Object()];
124+
if (attr.fastdef) {
125+
var method = function(args, kwargs) {
126+
if (arguments && arguments[0]) {
127+
arguments[0].splice(0, 0, object);
128+
return attr.apply(this, arguments);
129+
} else {
130+
return attr([object], { });
131+
}
136132
}
137-
args[0].splice(0, 0, object);
138-
return attr.apply(this, args);
139-
}
140133

141-
method.is_wrapper = true;
142-
object[attribute] = method;
143-
return method;
144-
}
145-
attr = __class__[attribute];
146-
if (attribute in __class__) {
147-
if ({}.toString.call(attr) === '[object Function]') {
134+
} else {
148135
var method = function() {
149136
var args;
150137
args = Array.prototype.slice.call(arguments);
@@ -157,6 +144,38 @@ __get__ = function(object, attribute) {
157144
return attr.apply(this, args);
158145
}
159146

147+
}
148+
method.is_wrapper = true;
149+
object[attribute] = method;
150+
return method;
151+
}
152+
attr = __class__[attribute];
153+
if (attribute in __class__) {
154+
if ({}.toString.call(attr) === '[object Function]') {
155+
if (attr.fastdef) {
156+
var method = function(args, kwargs) {
157+
if (arguments && arguments[0]) {
158+
arguments[0].splice(0, 0, object);
159+
return attr.apply(this, arguments);
160+
} else {
161+
return attr([object], { });
162+
}
163+
}
164+
165+
} else {
166+
var method = function() {
167+
var args;
168+
args = Array.prototype.slice.call(arguments);
169+
if (args[0] instanceof Array && {}.toString.call(args[1]) === '[object Object]' && args.length == 2) {
170+
/*pass*/
171+
} else {
172+
args = [args, Object()];
173+
}
174+
args[0].splice(0, 0, object);
175+
return attr.apply(this, args);
176+
}
177+
178+
}
160179
method.is_wrapper = true;
161180
object[attribute] = method;
162181
return method;
@@ -173,18 +192,30 @@ __get__ = function(object, attribute) {
173192
attr = _get_upstream_attribute(base, attribute);
174193
if (attr) {
175194
if ({}.toString.call(attr) === '[object Function]') {
176-
var method = function() {
177-
var args;
178-
args = Array.prototype.slice.call(arguments);
179-
if (args[0] instanceof Array && {}.toString.call(args[1]) === '[object Object]' && args.length == 2) {
180-
/*pass*/
181-
} else {
182-
args = [args, Object()];
195+
if (attr.fastdef) {
196+
var method = function(args, kwargs) {
197+
if (arguments && arguments[0]) {
198+
arguments[0].splice(0, 0, object);
199+
return attr.apply(this, arguments);
200+
} else {
201+
return attr([object], { });
202+
}
183203
}
184-
args[0].splice(0, 0, object);
185-
return attr.apply(this, args);
186-
}
187204

205+
} else {
206+
var method = function() {
207+
var args;
208+
args = Array.prototype.slice.call(arguments);
209+
if (args[0] instanceof Array && {}.toString.call(args[1]) === '[object Object]' && args.length == 2) {
210+
/*pass*/
211+
} else {
212+
args = [args, Object()];
213+
}
214+
args[0].splice(0, 0, object);
215+
return attr.apply(this, args);
216+
}
217+
218+
}
188219
method.is_wrapper = true;
189220
object[attribute] = method;
190221
return method;
@@ -1283,6 +1314,7 @@ ord = function(args, kwargs) {
12831314
ord.NAME = "ord";
12841315
ord.args_signature = ["char"];
12851316
ord.kwargs_signature = { };
1317+
ord.fastdef = true;
12861318
ord.types_signature = { };
12871319
ord.pythonscript_function = true;
12881320
chr = function(args, kwargs) {
@@ -1293,6 +1325,7 @@ chr = function(args, kwargs) {
12931325
chr.NAME = "chr";
12941326
chr.args_signature = ["num"];
12951327
chr.kwargs_signature = { };
1328+
chr.fastdef = true;
12961329
chr.types_signature = { };
12971330
chr.pythonscript_function = true;
12981331
var Iterator, __Iterator_attrs, __Iterator_parents;
@@ -1677,6 +1710,7 @@ __list___getitem__ = function(args, kwargs) {
16771710
__list___getitem__.NAME = "__list___getitem__";
16781711
__list___getitem__.args_signature = ["self", "index"];
16791712
__list___getitem__.kwargs_signature = { };
1713+
__list___getitem__.fastdef = true;
16801714
__list___getitem__.types_signature = { };
16811715
__list___getitem__.pythonscript_function = true;
16821716
__list_attrs["__getitem__"] = __list___getitem__;
@@ -1690,6 +1724,7 @@ __list___setitem__ = function(args, kwargs) {
16901724
__list___setitem__.NAME = "__list___setitem__";
16911725
__list___setitem__.args_signature = ["self", "index", "value"];
16921726
__list___setitem__.kwargs_signature = { };
1727+
__list___setitem__.fastdef = true;
16931728
__list___setitem__.types_signature = { };
16941729
__list___setitem__.pythonscript_function = true;
16951730
__list_attrs["__setitem__"] = __list___setitem__;

pythonjs/python_to_pythonjs.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ def get_pythonjs_function_name(self, name):
141141
return '__%s_%s' %(self.name, name) ## class name
142142

143143
def check_for_parent_with(self, method=None, property=None, operator=None, class_attribute=None):
144-
log('check_for_parent_with: %s'%locals())
145-
log('self.parents: %s'%self.parents)
146144

147145
for parent_name in self.parents:
148146
if not self.compiler.is_known_class_name( parent_name ):
@@ -925,6 +923,10 @@ def visit_Assign(self, node):
925923
elif isinstance(target, Name):
926924
node_value = self.visit( node.value ) ## node.value may have extra attributes after being visited
927925

926+
if writer.is_at_global_level():
927+
log('GLOBAL: %s : %s'%(target.id, node_value))
928+
self._globals[ target.id ] = None
929+
928930
if isinstance(node.value, Call) and hasattr(node.value.func, 'id') and node.value.func.id in self._classes:
929931
self._instances[ target.id ] = node.value.func.id ## keep track of instances
930932
elif isinstance(node.value, Call) and isinstance(node.value.func, Name) and node.value.func.id in self._function_return_types:
@@ -948,12 +950,19 @@ def visit_Assign(self, node):
948950
elif hasattr(node.value, 'returns_type'):
949951
self._instances[ target.id ] = node.value.returns_type
950952
elif target.id in self._instances:
951-
self._instances.pop( target.id )
953+
if target.id in self._globals:
954+
pass
955+
else:
956+
log('--forget: %s'%target.id)
957+
type = self._instances.pop( target.id )
958+
log('----%s'%type)
952959

953960
if target.id in self._instances:
954961
type = self._instances[ target.id ]
962+
log('typed assignment: %s is-type %s' %(target.id,type))
955963
if writer.is_at_global_level():
956964
self._globals[ target.id ] = type
965+
log('known global:%s - %s'%(target.id,type))
957966

958967
if self._with_static_type:
959968
if type == 'list':
@@ -965,6 +974,11 @@ def visit_Assign(self, node):
965974

966975
writer.write('%s = %s' % (target.id, node_value))
967976
else:
977+
if target.id in self._globals and self._globals[target.id] is None:
978+
self._globals[target.id] = type
979+
self._instances[ target.id ] = type
980+
log('set global type: %s'%type)
981+
968982
writer.write('%s = %s' % (target.id, node_value))
969983
else:
970984
writer.write('%s = %s' % (target.id, node_value))
@@ -1382,6 +1396,8 @@ def retrieve_vars(body):
13821396
writer.write( '%s.args_signature = [%s]' %(node.name, ','.join(['"%s"'%n.id for n in node.args.args])) )
13831397
defaults = ['%s:%s'%(self.visit(x[0]), self.visit(x[1])) for x in zip(node.args.args[-len(node.args.defaults):], node.args.defaults) ]
13841398
writer.write( '%s.kwargs_signature = {%s}' %(node.name, ','.join(defaults)) )
1399+
if self._with_fastdef or fastdef:
1400+
writer.write('%s.fastdef = True' %node.name)
13851401

13861402
#types = ['%s:%s'%(self.visit(x[0]), '"%s"'%type(self.visit(x[1])).__name__ ) for x in zip(node.args.args[-len(node.args.defaults):], node.args.defaults) ]
13871403
types = []

0 commit comments

Comments
 (0)