Call a Generated JS Function

608 views
Skip to first unread message

Alexander Varga

unread,
Aug 8, 2013, 4:52:40 PM8/8/13
to sku...@googlegroups.com
I have a bunch of python functions that Skulpt can convert to JS.
Is there any way to call those (now JS) functions, from additional unconverted JS code?

In other words, is an equivalent of the following possible?:

pythonCode = "def square(x): return x*x";
eval(Sk.importMainWithBody("<stdin>", false, pythonCode));
alert(square(3));

Thanks,
Alex

Alvaro Castaneda

unread,
Aug 8, 2013, 5:55:53 PM8/8/13
to sku...@googlegroups.com
I'am also interested in this, calling a JS function from Skulpt

thanks


--
 
---
You received this message because you are subscribed to the Google Groups "Skulpt" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skulpt+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alexander Varga

unread,
Aug 9, 2013, 12:40:56 AM8/9/13
to sku...@googlegroups.com
If square is the function, try:

Sk.globals.square.func_code(3)

Alvaro Castaneda

unread,
Aug 9, 2013, 10:53:34 AM8/9/13
to sku...@googlegroups.com
Ok that is calling a function from JS, I mean that is JS code, is it possible to call a JS function I created on the same document, call it from with in the python code?

Example code

<script>
function myFunction(name,job)
{
alert("Welcome " + name + ", the " + job);
}
</script>


// then from python trying to call that function

for i in range(10):
    print myFunction(i, "developer")


is that possible?

thanks for the help




--
Message has been deleted

David Holmes

unread,
Oct 25, 2013, 9:57:53 AM10/25/13
to sku...@googlegroups.com
Hi,
I've been working on cleaning up the ffi (Foreign Function Interface) in my own Skulpt branch with a view to offering it back.

Here is an example of some of the calls.

Let me know if this sounds interesting.

Thanks,

David

<script type="text/javascript">
function outf(text)
{
var output = document.getElementById("output");
text = text.replace(/</g, '&lt;');
output.innerHTML = output.innerHTML + text;
}
function run()
{
var prog = document.getElementById("code").value;
var output = document.getElementById("output");
output.innerHTML = '';
Sk.configure({output:outf});
try
{
var module = Sk.importMainWithBody("<stdin>", false, prog);

var Dog = Sk.ffi.buildClass(module, function($gbl, $loc) {
$loc.__init__ = Sk.ffi.functionPy(function(selfPy) {
Sk.ffi.referenceToPy({}, "Dog", undefined, selfPy);
});
}, "Dog", []);

var a = Sk.ffi.gattr(module, 'a');
var runMethod = Sk.ffi.gattr(a, 'run');
var retvalPy = Sk.ffi.callsim(runMethod, Sk.ffi.numberToIntPy(4));
alert(Sk.ffi.remapToJs(retvalPy));

var foo = Sk.ffi.gattr(module, 'foo');
Sk.ffi.callsim(foo, Sk.ffi.stringToPy("This string was passed to Python from JavaScript"));

var bar = Sk.ffi.gattr(module, 'bar');
Sk.ffi.callsim(bar, Sk.ffi.remapToPy(function(argPy) {
alert(Sk.ffi.remapToJs(argPy));
}));

var dogObject = Sk.ffi.callsim(Dog);
Sk.ffi.callsim(foo, dogObject);
}
catch (e)
{
alert(e);
}
}
</script>
<form>
<textarea id="code" rows="24" cols="80">
class Test:
def run(self, b):
self.a = 3 + b
return self.a

print "This string was defined and printed from Python"

a = Test()

def foo(arg):
print type(arg)
print arg

def bar(callback):
callback("This string was generated in Python")

def baz():
pass
</textarea><br>
<button onclick="run()" type="button">Run</button>
</form>

<pre id="output"></pre>


On Fri, Oct 25, 2013 at 3:34 AM, Ryan Davis <epi...@gmail.com> wrote:
Yes, you can do that.  You define your function, then you define it as a property of the Sk.globals object.  You then need to create a sort of "intermediate layer" to account for the way Skulpt handles it's objects and their values.  Like this:

Sk.globals.myFunction = function( pyName, pyJob ) {
    //Now, in Skulpt, string and number objects' values are stored as their 'value' property, so you have to access that.
   var name = pyName.value, job = pyJob.value;
  myFunction( name, job );
}
I'm actually working for an abstraction layer to make this simpler.  It should be on my Github page fairly soon.

Ryan Davis

unread,
Oct 26, 2013, 3:51:38 AM10/26/13
to sku...@googlegroups.com
Yes, you can do that.  You define your function, then you define it as a property of the Sk.globals object.  You then need to create a sort of "intermediate layer" to account for the way Skulpt handles it's objects and their values.  Like this:

Sk.globals.myFunction = function( pyName, pyJob ) {
    //Now, in Skulpt, string, list, tuple, and number objects' values are stored as their 'v' property, so you have to access that.
   var name = pyName.v, job = pyJob.value;
  myFunction( name, job );
}
I'm actually working on an abstraction layer to make this simpler.  It should be on my Github page fairly soon.

EDIT:  For one, it's not the 'value' attribute, it's the 'v' attribute.
Secondly, I wanted to post the link to my fork of Skulpt.  In the 'dist' folder, there's a file called "python.js".  That's the file that has the interface layer for exchanging data freely between the languages.  Check it out, tell me what you think.

On Friday, August 9, 2013 9:53:34 AM UTC-5, varomix DaGreit wrote:

Ryan Davis

unread,
Oct 26, 2013, 4:00:10 AM10/26/13
to sku...@googlegroups.com
lol.  I forgot to actually post the link.  https://github.com/DavisDevelopment/skulpt

Ryan Davis

unread,
Oct 26, 2013, 4:02:31 AM10/26/13
to sku...@googlegroups.com
Assuming I'm reading it correctly, I think you're doing some pretty nice work.  Maybe share a link to your branch?  I'd love to look at the changes you've made.

Alvaro Castaneda

unread,
Oct 26, 2013, 2:28:56 PM10/26/13
to sku...@googlegroups.com

Thanks Ryan I'll try it that's exactly what I need
Cheers

David Holmes

unread,
Oct 27, 2013, 8:36:15 AM10/27/13
to sku...@googlegroups.com
Ryan,
Sorry for the delay.

My active project is here.


I have this fork too, but I think it's a bit messed up and needs re-forking.


In either case, look for the ffi.js file. I think you should be able to drop it in.

David
Reply all
Reply to author
Forward
0 new messages