Skip to content

Commit bb86a85

Browse files
author
hartsantler
committed
added string.join
1 parent 0c40d75 commit bb86a85

5 files changed

Lines changed: 70 additions & 7 deletions

File tree

pythonscript.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// PythonScript Runtime - regenerated on: Mon Oct 14 00:26:47 2013
1+
// PythonScript Runtime - regenerated on: Mon Oct 14 02:00:23 2013
22
var jsrange = function(num) {
33
"Emulates Python's range function";
44
var i, r;
@@ -602,6 +602,34 @@ return false;
602602
}
603603

604604
String.prototype.endswith=func;
605+
var func = function(a) {
606+
var i, out;
607+
out = "";
608+
if(a instanceof Array) {
609+
arr = a;
610+
}
611+
else {
612+
arr = a.__dict__.js_object;
613+
}
614+
615+
i = 0;
616+
var iter = arr;
617+
for (var value=0; value < iter.length; value++) {
618+
var backup = value;
619+
value = iter[value];
620+
out += value;
621+
i += 1;
622+
if(i < arr.length) {
623+
out += this;
624+
}
625+
626+
value = backup;
627+
}
628+
629+
return out;
630+
}
631+
632+
String.prototype.join=func;
605633
}
606634
window["_setup_str_prototype"] = _setup_str_prototype
607635

pythonscript/pythonjs.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,26 @@ def visit_keyword(self, node):
128128

129129
def visit_Call(self, node):
130130
name = self.visit(node.func)
131-
if name == 'JSObject':
131+
if name == 'instanceof': ## this gets used by "with javascript:" blocks to test if an instance is a JavaScript type
132+
args = map(self.visit, node.args)
133+
if len(args) == 2:
134+
return '%s instanceof %s' %tuple(args)
135+
else:
136+
raise SyntaxError( args )
137+
138+
elif name == 'JSObject':
132139
if node.keywords:
133140
kwargs = map(self.visit, node.keywords)
134141
f = lambda x: '"%s": %s' % (x[0], x[1])
135142
out = ', '.join(map(f, kwargs))
136143
return '{%s}' % out
137144
else:
138145
return 'Object()'
139-
if name == 'var':
146+
elif name == 'var':
140147
args = map(self.visit, node.args)
141148
out = ', '.join(args)
142149
return 'var %s' % out
143-
if name == 'JSArray':
150+
elif name == 'JSArray':
144151
if node.args:
145152
args = map(self.visit, node.args)
146153
out = ', '.join(args)

runtime/builtins.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def func(a):
1212
return True
1313
else:
1414
return False
15-
#String.prototype.startswith = func ## the problem with this is "this" gets lost when get_attribute(f,'__call__') is used.
1615

1716
@String.prototype.endswith
1817
def func(a):
@@ -21,6 +20,21 @@ def func(a):
2120
else:
2221
return False
2322

23+
@String.prototype.join
24+
def func(a):
25+
out = ''
26+
if instanceof(a, Array):
27+
arr = a
28+
else:
29+
arr = a.__dict__.js_object
30+
i = 0
31+
for value in arr:
32+
out += value
33+
i += 1
34+
if i < arr.length:
35+
out += this
36+
return out
37+
2438
_setup_str_prototype()
2539

2640
def range(num):

runtime/pythonpythonjs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def wrapper(args,kwargs): return attr.apply(object, args)
121121
## F.pythonscript_function or F.is_wrapper, we could check if object has these props:
122122
## bases, __name__, __dict__, __call__
123123
#print 'wrapping something external', object, attribute
124-
def wrapper(args,kwargs): return attr.apply(object, args)
124+
125+
def wrapper(args,kwargs): return attr.apply(object, args) ## THIS IS CORRECT
126+
#def wrapper(args,kwargs): return attr.call(object, args) ## this is not correct
125127
wrapper.is_wrapper = True
126128
return wrapper
127129
else:

tests/test_string.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def test():
9-
global a, b
9+
global a, b, c
1010
a = 'hello'
1111
b = 'world'
1212
print a+b
@@ -18,13 +18,25 @@
1818
if a.endswith('lo'):
1919
print 'WITH - endswith test passed'
2020

21+
22+
print 'WITH - testing string.join...'
23+
d = ','.join( [1,2,3,4] )
24+
print( d )
25+
26+
print '--testing string.join...'
27+
c = [1,2,3,4,5]
28+
d = ','.join( c )
29+
print( d )
30+
2131
if a.startswith('he'):
2232
print 'startswith test passed'
2333

2434
if a.endswith('llo'):
2535
print 'endswith test passed'
2636

2737

38+
print 'tests complete'
39+
2840
</script>
2941

3042
</head>

0 commit comments

Comments
 (0)