Giter VIP home page Giter VIP logo

pangojs's Introduction

GO 语言编写的 执行 js 脚本的程序

使用

找到 gojs.exe 的路径,cmd 命令下执行 gojs 后面跟js路径

gojs a.js

js 脚本demo

var s = function (){
    var b=1;
    var c=2
    return b+c;
}

var s2 = function (){
    var b=1;
    var c=3;
    var data = new Date();
    return b+c+data;
}

/* 
 * 此处是关键 会在控制台打印输出 可以多个 console.log()
 */
console.log(s())  

由于在 window 下 没有很好的 js 脚本解析器

所以在这里用 otto 编译个 js 脚本解析器

window 下 建议将 gojs.exe 加入环境变量

cmd 命令运行 要想获得 js 返回值 console.log(s()) 即可

php 通过 shell 环境执行命令

<?php
$output = shell_exec('gojs a.js');
echo "<pre>$output</pre>";
?>

当前 php 有 V8 引擎库 可以之间执行 js 脚本

python 简单

import os
str = os.popen("gojs a.js").read()
a = str.split("\n")
for b in a:
   print b

python 高级

# coding=utf-8

import shlex
import datetime
import subprocess
import time
 
def execute_command(cmdstring, cwd=None, timeout=None, shell=False):
    """执行一个SHELL命令
            封装了subprocess的Popen方法, 支持超时判断,支持读取stdout和stderr
           参数:
        cwd: 运行命令时更改路径,如果被设定,子进程会直接先更改当前路径到cwd
        timeout: 超时时间,秒,支持小数,精度0.1秒
        shell: 是否通过shell运行
    Returns: return_code
    Raises:  Exception: 执行超时
    """
    if shell:
        cmdstring_list = cmdstring
    else:
        cmdstring_list = shlex.split(cmdstring)
    if timeout:
        end_time = datetime.datetime.now() + datetime.timedelta(seconds=timeout)
    
    #没有指定标准输出和错误输出的管道,因此会打印到屏幕上;
    sub = subprocess.Popen(cmdstring_list, cwd=cwd, stdin=subprocess.PIPE,shell=shell,bufsize=4096)
    
    #subprocess.poll()方法:检查子进程是否结束了,如果结束了,设定并返回码,放在subprocess.returncode变量中 
    while sub.poll() is None:
        time.sleep(0.1)
        if timeout:
            if end_time <= datetime.datetime.now():
                raise Exception("Timeout:%s"%cmdstring)
            
    return str(sub.returncode)
 
if __name__=="__main__":
    print execute_command("gojs a.js")

pangojs's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.