1 Star 0 Fork 19

stone264 / scriptparser

forked from shengu / scriptparser 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

简介

ScriptParser是一款可以在.net的环境下,解析执行javascript语法的开源组件,其主要定位如下:

  1. 在.net环境下,动态执行部分业务逻辑,以实现在不修改原生程序的基础上,动态的扩展一些功能。
  2. 提供一套可以用javascript来控制逻辑的模板机制,以便在.net环境下,可以动态生成文本(比如:某些情况下,代替.aspx生成html页面等)

支持语法

目前ECMAScript 5的语法,除了with语法、Property、正则表达式等个别语法之外,其它的都有支持。 支持的预设类型:Object、Array、Number、String、Date,以及部分预设方法:parseInt、parseFloat、stringify、eval等。 支持对DataTable的访问。 支持开发者根据需要,添加需要在javascript中调用的类、对象、方法及属性。

其它

  1. 由于javascript对象的特殊性,所有需要传入到javascript中去访问的对象、方法,都需要直接或间接的转化成继承于IScriptObject的对象。

.net中的对象,如果没有特别处理,会使用ScriptTypeScriptNativeObjectScriptNativeArrayScriptNativeFunction进行包装后,供javascript的语句中调用。

  1. 该组件的解析和执行过程,使用内存结构来模拟线程栈的操作,以避免解析到恶意嵌套的语法,引发堆栈溢出,从而导致进程中止。

Demo

请参见ScriptTest项目

JavaScript的解析过程

//解析javascript语法,创建ScriptParser
ScriptParser parser = ScriptParser.Parse("var i = 0; return i + inc;");

//创建执行上下文:ScriptContext
ScriptContext context = new ScriptContext();

//添加变量,以及方法映射
context.AddValue("inc", 100);

//执行
parser.Execute(context);

//获取执行结果
object result = context.Result.ToValue(context);

模板的解析过程

  1. 创建一个包含output和write方法的类
class ScriptResult
{
    private string source;
    private StringBuilder builder;

    public ScriptResult(string source)
    {
        this.source = source;
        this.builder = new StringBuilder();
    }

    [ScriptMapping("output")]
    public void Output(int index, int length)
    {
        builder.Append(source.Substring(index, length));
    }

    [ScriptMapping("write")]
    public void Write(IScriptObject value, ScriptContext context)
    {
        builder.Append(value.ToValueString(context));
    }

    public override string ToString()
    {
        return builder.ToString();
    }
}
  1. 生成可以执行的javascript,并将ScriptResult映射到ScriptContext中执行
//将包含<@XXX></@XXX>的标签,转成可以执行的javascript语法
TagParser reader = new TagParser();
string script = reader.Parse(text, "output", "write", true);

//解析javascript语法
ScriptParser parser = ScriptParser.Parse(script);

ScriptContext context = new ScriptContext();
//将output和write的方法映射添加到执行的上下文中
ScriptResult result = new ScriptResult(text);
context.AddMappings(result);

//执行javascript脚本
parser.Execute(context);

//读取结果
string content = result.ToString();

QQ交流群

  1. 792893075快速加群
MIT License Copyright (c) 2021 shengu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一款在.net环境下,解析执行Javascript的开源组件 展开 收起
C#
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/wuqr32/scriptparser.git
git@gitee.com:wuqr32/scriptparser.git
wuqr32
scriptparser
scriptparser
master

搜索帮助