最近研发BDC 云开发部署平台的数据路由及服务管理器
意外作出了一个javascript的服务器,可以通过js调用并执行任何java(包括 所有java 内核基本库)及C#类库,并最终由 C# 执行你提交的javascript代码!
解决方案下载地址:
http://pan.baidu.com/share/link?shareid=2118966359&uk=201606611
不敢藏私,特与大家分享!部分代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.mozilla.javascript;
using javax.net;//org.mozilla.javascript包:该包内的对象是实现javascript的主要对象。
//org.mozilla.javascript.ast包:rhino中语法分析器生成的ast表示类都在该包内。
//org.mozilla.javascript.debug包:该包实现了javascript的debug功能。
//org.mozilla.javascript.optimizer包:该包实现了javascipt生成代码的代码优化。
//org.mozilla.javascript.regexp包:该包实现了javascript正侧表达式的功能。namespace javascriptServer
{
    class MainClass
    {
        private const string script =
            @"
            var swingNames = JavaImporter();
            swingNames.importPackage(Packages.java.lang); 
            swingNames.importPackage(java.io);
            swingNames.importPackage(Packages.System);
            swingNames.importPackage(Packages.System.Text);
            swingNames.importPackage(Packages.TestRhino);
            //swingNames.importClass(ClassLibrary.dll);
            function Add()
            {
                return '123';
            }            function foo(me)
            {
                return fromCSharp(me) + me + ""!!!"";
            }
                        function output(str)
            {
                return str + ""结果"";
            }
            
            var str = foo(2000);
            PrintLn(str);
            java.lang.System.out.println('测试调用 javascript 系统函数');            var hello = Hello.getInstance();
            var sh = Hello.sHello();
            PrintLn(sh);
            ";        public static java.lang.Integer FromCSharp(java.lang.Integer i) //, java.lang.Integer j)
        {
            return i; // string.Format("  {0} the ", i); //, j);
        }        public static void PrintLn(String s) //, java.lang.Integer j)
        {
            //return i; // string.Format("  {0} the ", i); //, j);
            Console.WriteLine(s);
            return;
        }        public static void Main(string[] args)
        {
            Context cx = Context.enter();  //获取环境设置            //cx.setClassShutter(new ClassShutter());
            Scriptable scope = cx.initStandardObjects(); //初始化本地对象            //scope.put("x", scope, new java.lang.Integer(20));    //输入参数设置              Hello hello = new Hello();
            scope.put("Hello", scope, hello);  // typeof(Hello)); // 对象放入全局对象中            ContextFactory cf = new ContextFactory();            //注册 c# 方法
            java.lang.Class myJClass = typeof(MainClass);
            java.lang.reflect.Member method = myJClass.getMethod("FromCSharp", typeof(java.lang.Integer)); //new java.lang.Class[] { typeof(java.lang.Integer) }); //, typeof(java.lang.Integer) });
            Scriptable function = new FunctionObject("fromCSharp", method, scope);
            scope.put("fromCSharp", scope, function);            method = myJClass.getMethod("PrintLn", typeof(java.lang.String)); //new java.lang.Class[] { typeof(java.lang.Integer) }); //, typeof(java.lang.Integer) });
            function = new FunctionObject("PrintLn", method, scope);
            scope.put("PrintLn", scope, function);            int ix = 1; int iy = 2;                        //执行脚本
            cx.evaluateString(scope, script, "myjs", 1, null);
            //Console.WriteLine("从执行结果中获取结果"+ scope.get("result", scope));     //结果输出
            //编译脚本
            Script s = cx.compileString(script, "myjs", 1, null);
            s.exec(cx, scope);            ScriptableObject.putProperty(scope, "ix", ix);
            ScriptableObject.putProperty(scope, "iy", iy);
            object fooFunctionObj = scope.get("foo", scope);
                        
            if (!(fooFunctionObj is Function))
                Console.WriteLine("foo 不是函数");
            else
            {
                Function fooFunction = (Function)fooFunctionObj;
                //调用 js 方法
                object result = fooFunction.call(cx, scope, scope, new object[] { new java.lang.Integer(100) });                Console.WriteLine(result);
            }            Console.ReadLine();
        }        /// <summary>        /// 实现类在javascript中使用的安全性验证
        /// </summary>
        private class ClassShutter : org.mozilla.javascript.ClassShutter
        {
            public bool visibleToScripts(string str)
            {
                Console.WriteLine("类在javascript中被使用: {0}", str);
                return false;
            }
        }
    }
}