skulpt / skulpt

Skulpt is a Javascript implementation of the Python programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling an already loaded function in Skulpt

sandeep-gh opened this issue · comments

I have gone through documentations as best I could but couldn't figure how to achieve a few simple things-- essentially, I want to load a function in the Sk runtime and later invoke the function maybe with some arguments.
To illustrate, below is the code snippet (full code attached at last):

    prog = `def square(x):
    return x*x
`
    
    var module = Sk.importMainWithBody("<stdin>", false, prog);

What I want to do, is call the square function. Either via Python or via javascript:
I tried as follows:

  • for python
prog=`square(5)`
Sk.importMainWithBody("<stdin>", false, prog);
  • for javascript
var pyf_square = module.tp$getattr('square');
var ret = Sk.misceval.callsim(runMet, 10);

Neither of them work.

It seems this should be fairly straightforward for Skulpt to do. I am missing something trivial but can't find it.

Full code

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script> 
<script src="js/skulpt.min.js" type="text/javascript"></script>
<script src="js/skulpt-stdlib.js" type="text/javascript"></script>
    

</head> 

<body>
  <!-------------the facade --->
  <h3>Try This</h3> 
  <form> 
    <button type="button" onclick="runit()">Run</button> 
  </form> 
  <pre id="output" ></pre> 
  <!-------------end --->

  <script type="text/javascript">
    //-----------------setup crud------------
    function builtinRead(x) {
    if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
            throw "File not found: '" + x + "'";
    return Sk.builtinFiles["files"][x];
    }

    function outf(text) { 
    var outhe = document.getElementById("output"); 
    outhe.innerHTML = outhe.innerHTML + text; 
    }
    var pre = document.getElementById("output");
    pre.innerHTML = '';
    Sk.pre = "output";
    Sk.configure({output:outf, read:builtinRead}); 
    //--------------------end--------------------------

    prog = `def square(x):
    return x*x
`
    
    var module = Sk.importMainWithBody("<stdin>", false, prog);
    var pyf_square = module.tp$getattr('square');
    var ret = Sk.misceval.callsim(runMet, 10);
    console.log(ret)
</script> 


</body> 

</html>