skulpt / skulpt

Skulpt is a Javascript implementation of the Python programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Create Custom Lib To Evaluate JS from within Python Interpreter

chrisjd20 opened this issue · comments

Could someone help me understand how to make an ultra simple lib to include in python?

Specifically, I want to be able to run a string as json from within the module. I can see that Sk.builtin.str("window").jseval("console.log(1)") does this but what I want is something like:

image

Is there like an ultra basic example of how to make a lib that can be included to accomplish this?

Ive been trying to find documentation but havent seen any.

Ok, figured it out.

$builtinmodule = function (name) {
    var mod = {__name__: new Sk.builtin.str("elf")};
    
    var elf = function ($gbl, $loc) {
        $loc.__init__ = new Sk.builtin.func(function (self) {
            self.window = Sk.builtin.str("window")
            if (!self.window){
                self.window = Sk.builtin.str("window")
            }
        });

        $loc.__repr__ = new Sk.builtin.func(function (self) {
            return new Sk.builtin.str('[Elf Object]')
        })

        $loc.__str__ = $loc.__repr__;

        $loc.jseval = new Sk.builtin.func(function (self, jsstring) {
            return self.window.jseval(jsstring);
        });
    };

    mod.elf = Sk.misceval.buildClass(mod, elf, "elf", []);

    return mod;
};

image