Package: jsengine
JSEngineThis is a simple wrapper of Javascript engines, it wraps the Javascript
interpreter for Python use.There are two ways to call interpreters, via dynamic library loading is internal
call which is faster than the other one, via subprocess is external call.System's built-in Javascript interpreter:macOS: JavascriptCoreLinux: Gjs on Gnome, CJS on Cinnamon, etc.Windows: Chakra (internal call, but not applicable to Windows 7)Python bindings (Recommend, internal call):QuickJSPyChakraPyMiniRacer (V8)(Caused bya scope issue,
its work process is similar to external call now, to fix the scope issue,Esprimais needed)Any installed external Javascript interpreters, e.g.SpiderMonkey, Node.js, QuickJS, etc.JSEngine used to be part ofYKDL,
which created by@coslyk.InstallationInstall frompip install jsengineOr download and Install from source codepython setup.py installCompatibilityPython >= 2.7Usageimportjsenginejsengine.eval('"Hello, world!"')# => 'Hello, world!'Use a JSEngine context.try:ctx1=jsengine.jsengine()exceptjsengine.RuntimeError:...# do something if uselessifjsengine.JSEngineisNone:...# do something if uselesselse:ctx2=jsengine.JSEngine("""function add(x, y) {return x + y;}""")ctx1.eval('1 + 1')# => 2# call funtionctx2.call("add",1,2)# => 3# append new scriptctx1.append("""function square(x) {return x ** 2;}""")ctx1.call("square",9)# => 81Use a specified external Javascript interpreter.binary=binary_nameorbinary_pathkwargs={'name':'None or any string',# see ExternalInterpreterNameAlias.keys()'tempfile':True,# use tempfile or not. Default is False, fallback is True'evalstring':True,# can run command string as Javascript or can not,# just like '-e script_code'# instead of True, supported argument can be passed,# e.g. '--eval', '--execute''args':[args1,args2,...]# arguments used for interpreter}# case 1interpreter=jsengine.ExternalInterpreter.get(binary,**kwargs)ifinterpreter:# foundctx=jsengine.ExternalJSEngine(interpreter)# case 2ifjsengine.set_external_interpreter(binary,**kwargs):# set default external interpreter OKctx=jsengine.ExternalJSEngine()# case 3, maybe get default fallback instead of your specifiedtry:ctx=jsengine.ExternalJSEngine(interpreter=binary,**kwargs)exceptjsengine.RuntimeError:...# do something if uselessUse threading lock. Javascript source itself always be ran in single threaded,
that just make the APIs can be used in multithreadeding.jsengine.set_threading(True)# MUST enable befor using, it's disabled by defaultctx_quickjs=jsengine.QuickJSEngine()ctx_chakra=jsengine.ChakraJSEngine()# internal chakra will create an extra thread per contextctx_v8=jsengine.V8JSEngine()ctx_exter=jsengine.ExternalJSEngine()# external interpreter will be called one by one with context...# do multithreadingjsengine.set_threading(False)# disable is not necessaryInternal VS. ExternalQuickJSEngineChakraJSEngineV8JSEngine (esprima)V8JSEngine **ExternalJSEngineLoad backend onimportimport or initinitinitevery fetch resultLoading speedfastestfastvery slow with py3fastvery slowPerformancehighesthighlow, if much resultslow, if much resultsFetch resultrun the passedrun the passedrun the passedrun all/full sourcerun all/full source* Fetch results means calleval()/call().** V8JSEngine is now similar to ExternalJSEngine which caused by scope issue.LicenseJSEngine is released under theMIT License.