Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add EmbeddedScriptLoader ; change request.js to embedded resource #6

Open
wants to merge 95 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
36c7689
add EmbeddedScriptLoader ; change request.js to embedded resource
owindog Dec 24, 2017
fb25fc3
添加对sql的支持
owindog Dec 24, 2017
b5b1ce4
up
owindog Dec 24, 2017
7bcea00
sql up
owindog Dec 28, 2017
f577f8c
up
owindog Dec 28, 2017
26b5f2b
改 http 组件
owindog Dec 28, 2017
8c5bac1
up
owindog Dec 28, 2017
c30a16b
sql up
owindog Dec 29, 2017
43e31ca
up
owindog Dec 29, 2017
38c6ebd
up
owindog Dec 29, 2017
75f6fba
添加tabris
owindog Dec 30, 2017
fd2e50e
up
owindog Dec 30, 2017
d1b0378
packageid 命名规范
owindog Dec 30, 2017
4caf198
抽象db执行 可扩展
owindog Dec 30, 2017
ced61ea
添加log 处理器
owindog Dec 30, 2017
64754c5
up
owindog Dec 30, 2017
353e556
增加tabris winform 工具
owindog Dec 30, 2017
db4afa8
up
owindog Dec 30, 2017
8ca87b5
Update README.md
yuzd Dec 30, 2017
b087d4a
添加gif说明
owindog Dec 30, 2017
c0830d8
增加抽象的dbExcutor实现 catch db执行详细Exception
owindog Dec 31, 2017
2e37628
Merge branches 'master' and 'master' of github.com:yuzd/ClearScript.M…
owindog Dec 31, 2017
b8595ca
改成tab
owindog Dec 31, 2017
e30eb92
tab联动
owindog Dec 31, 2017
bf792d7
每个Runtimer 有一个 RequireManager
owindog Jan 1, 2018
149d7e2
up
owindog Jan 1, 2018
fd017a1
winform 分开 编辑 保存功能
owindog Jan 1, 2018
ac94627
up
owindog Jan 1, 2018
5d375cc
up
owindog Jan 1, 2018
e8df28a
剥离sql 和 sqlexcutor
yuzd Jan 2, 2018
249a4ee
编辑器添加复制粘贴功能
owindog Jan 2, 2018
6d07b4d
增加代码格式化 注释和反注释功能
owindog Jan 6, 2018
77d790e
代码编辑器增加搜索功能
owindog Jan 6, 2018
5b9bcc2
增加js代码错误提示功能
owindog Jan 6, 2018
57a9656
修改 codemirrow 的 scrollbar的样式
owindog Jan 6, 2018
5161590
添加release 包
owindog Jan 6, 2018
9e46d38
优化版本 V1.1 Release
owindog Jan 7, 2018
bad1043
优化log处理器
owindog Jan 7, 2018
fafc8da
release v1.2版本
owindog Jan 7, 2018
d4b1b8a
Update README.md
yuzd Jan 7, 2018
aa853d6
up
owindog Jan 9, 2018
9fab3f0
ClearScript升级到 5.5.1.1
owindog Jan 14, 2018
868c9ce
debuger up
owindog Jan 16, 2018
cbdc96e
brower控件改成用cefsharp
owindog Jan 17, 2018
bb8107d
cef menuhander
owindog Jan 18, 2018
7d0adfc
up
owindog Jan 18, 2018
25e2f53
up
owindog Jan 20, 2018
9dafe8c
布局调整
owindog Jan 21, 2018
0e21919
加入 remote debugger 功能
owindog Jan 21, 2018
27b0fd8
up
owindog Jan 21, 2018
e2d47b1
debugger 功能gif介绍
owindog Jan 21, 2018
5012287
Update README.md
yuzd Jan 21, 2018
31218bd
Update README.md
yuzd Jan 21, 2018
170e7a6
删除无用代码
owindog Jan 21, 2018
af46464
clearscript升级到5.4.10(最后一个支持v8 inspector的版本)
owindog Jan 21, 2018
88452ee
js 代码编辑器优化
owindog Jan 22, 2018
7792dae
代码自动提示优化
yuzd Jan 23, 2018
4549291
代码联想优化
owindog Jan 23, 2018
75dfd95
更新
owindog Jan 23, 2018
baf0c54
代码提示更加智能
owindog Jan 24, 2018
37cfddb
代码着色优化
yuzd Jan 25, 2018
defa708
支持require 相对地址 多级
owindog Jan 27, 2018
8861edf
优化closebrackets闭合时选中
owindog Jan 28, 2018
0510914
Update README.md
yuzd Jan 28, 2018
18ba0e9
增加右键菜单代码提示功能
owindog Jan 28, 2018
76171e9
Merge branch 'master' of github.com:yuzd/ClearScript.Manager
owindog Jan 28, 2018
3b1f4ee
增加对 {{}} 的脚本处理
owindog Jan 28, 2018
e6a38c9
完善{{}}语法糖
owindog Jan 29, 2018
5cca50e
comment 自动优化,function scope优化
yuzd Feb 19, 2018
7814f13
增加类似resharper的快速模糊查找并定位某个function的位置 Alt+\
yuzd Feb 21, 2018
95cad8e
Ctrl+Click 直接跳转到function定义处
yuzd Feb 21, 2018
b96e64d
up
yuzd Feb 21, 2018
caea0df
Alt + \ => function对应的comment也展示
yuzd Feb 21, 2018
4ebb095
up
owindog Feb 22, 2018
dab324f
Update README.md
yuzd Feb 22, 2018
7a7ed43
comment bug 优化
owindog Feb 22, 2018
f38e789
Merge branch 'master' of github.com:yuzd/ClearScript.Manager
owindog Feb 22, 2018
bcb3ba7
增加webview 实现winform的 hybird js 控制view
owindog Mar 11, 2018
ef9d9c3
webview组件增加timer功能
owindog Mar 11, 2018
66ae719
http处理器 增加 CookieContainer参数
owindog Mar 11, 2018
d68a770
cef 组件升级到最新版
owindog Mar 14, 2018
233f586
增加log clear 功能
owindog Mar 14, 2018
99f9373
up
yuzd Mar 15, 2018
8ea993e
function解析优化
yuzd Mar 19, 2018
0da4719
优化 function 下的回车 indenty
owindog Mar 19, 2018
d255623
up
owindog Mar 20, 2018
8f1f6a0
up 针对 function代码块后的 回车 优化
owindog Mar 21, 2018
e6db9d9
up
owindog Mar 21, 2018
412027d
up
yuzd Mar 23, 2018
d778fb5
timer组件独立出来
owindog Mar 23, 2018
d1c8dd5
增加clr组件
owindog Mar 24, 2018
8f3704d
增加clr File组件的部分语法提示
owindog Mar 24, 2018
df51d3f
增加utf8转换
owindog Mar 25, 2018
ad0bdd2
完善csharp的IO组件的语法只能提示
owindog Mar 25, 2018
a8119e8
改进js代码的智能提示
owindog Mar 27, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,12 @@ Generated_Code #added for RIA/Silverlight projects
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
src/.vs/ClearScript.Manager/v15/Server/sqlite3/db.lock
src/.vs/ClearScript.Manager/v15/Server/sqlite3/storage.ide
src/.vs/ClearScript.Manager/v15/Server/sqlite3/storage.ide-shm
src/.vs/ClearScript.Manager/v15/Server/sqlite3/storage.ide-wal
src/Tabris.Winform/.vs/Tabris.Winform/v15/Server/sqlite3/db.lock
src/Tabris.Winform/.vs/Tabris.Winform/v15/Server/sqlite3/storage.ide
src/Tabris.Winform/.vs/Tabris.Winform/v15/Server/sqlite3/storage.ide-shm
src/Tabris.Winform/.vs/Tabris.Winform/v15/Server/sqlite3/storage.ide-wal
src/.vs/ClearScript.Manager/v15/sqlite3/storage.ide
293 changes: 9 additions & 284 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,286 +1,11 @@
# ClearScript.Manager

[![Build status](https://ci.appveyor.com/api/projects/status/b43oj4a3ccj7fm31?svg=true)](https://ci.appveyor.com/project/eswann/clearscript-manager)

ClearScript Manager was created to encapsulate the use of the ClearScript V8 engine in multi-use scenarios, like in a hosted server project (Ex: for use in a Web App or Web API).

ClearScript is an awesome library that was created to allow execution of JavaScript via V8 from the .Net runtime.
It's a great project but it needed a few extra things in certain situations that aren't in the core goals of ClearScript itself.
ClearScript also runs VBScript and JScript but those are not in the scope of ClearScript.Manager at the current time.

It should be noted that the package also installs the latest version of ClearScript (5.4) via ClearScript.Installer.
In addition, both ClearScript and ClearScript.Manager are compiled against .Net 4.5.

Here are a couple of the related discussions on the clearscript forum:

<https://clearscript.codeplex.com/discussions/535693>
<https://clearscript.codeplex.com/discussions/533516>

And the ClearScript site: https://clearscript.codeplex.com

Along those lines, ClearScript.Manager does the following to make certain things easier in your server project:

* Downloads and adds the ClearScript dlls appropriately.
* Creates a configurable pool of V8 Runtimes that are cached and reused.
- Pools have a configurable number of max instances.
- Behavior when attempting to retrieve a V8 Runtime is to block until a V8 engine becomes available.
* Because V8 Runtimes have affinity for compiled scripts, it compiles and caches scripts for each V8 Runtime instance.
* Attempts to better contain running V8 scripts by:
- Setting up a Task in which the script is run with a configurable timeout.
- Allow easy management of the memory usage of each instance of the V8 Runtime and sets the limits to a much lower threshold than the default V8 settings.

## Configuration Parameters
These settings can be set manually or added to the AppSettings of your application config file.

//V8 Max Executable Size in bytes:
int MaxExecutableBytes { get; }

//V8 Max New Space in bytes:
int MaxNewSpaceBytes { get; }

//V8 Max Old Space in bytes:
int MaxOldSpaceBytes { get; }

//Default script timeout in ms.
//If the script timeout is set to 0, the script will not execute within a Task with a timeout.
int ScriptTimeoutMilliSeconds { get; }

//Max number of simultaneous V8 Runtimes:
int RuntimeMaxCount { get; }

//Per Runtime, the maximum number of cached scripts:
int ScriptCacheMaxCount { get; }

//The default script cache expiration in seconds:
int ScriptCacheExpirationSeconds { get; }

//Is V8 debugging enabled
bool V8DebugEnabled {get;}

//What is the V8 Debug port to connect on. Default is 9222.
bool V8DebugPort {get;}


## Using the Runtime Manager

### Running Basic JavaScript
Grab a runtime manager and use the ExecuteAsync method.

var manager = new RuntimeManager(new ManagerSettings());
await manager.ExecuteAsync("test", "var i = 0; i++;");

The V8 scripting engine can be accessed as the return from ExecuteAsync() in order to access the script variables via dynamic properties.

var manager = new RuntimeManager(new ManagerSettings());
var engine = await manager.ExecuteAsync("test", "var i = 0; i++;");
var val = engine.Script.i;


### Running Multiple Scripts
To run multiple scripts, use the ExecuteAsync that accepts an enumerable of scripts. Scripts will be run in order.
Settings will be applied to all scripts in the collection. Scripts can be actual code, a local file path or a Uri.
Using an http Uri is dependent on the ClearScript.Manager.Http package as that installs the newest Microsoft.Net.Http package.

await manager.ExecuteAsync(new List<IncludeScript>
{
new IncludeScript {Uri = ".\\TestMainScript.js", ScriptId = "testScript"},
new IncludeScript {Code = "subject.TestString = 'test string3';", ScriptId = "testScript3"},
//Requires ClearScript.Manager.Http
new IncludeScript {Uri = "https://www.myscriptishere.org/script.txt", ScriptId = "TestScript4"}
});


### Execution Options
Execution options are the new way of passing options to the ClearScript runtime manager.

/// Objects to inject into the JavaScript runtime.
public IEnumerable<HostObject> HostObjects

/// Types to make available to the JavaScript runtime.
public IEnumerable<HostType> HostTypes

/// Indicates that this script should be added to the script cache once compiled.
/// Default is True.
public bool AddToCache

/// External JavaScripts to import before executing the current script.
public IList<IncludeScript> Scripts

#### Passing in Host Objects
Host objects are a way of passing an object instance to JavaScript from .Net.

var subject = new TestObject{Name = "Name", Count = 0};
var manager = new RuntimeManager(new ManualManagerSettings());

await manager.ExecuteAsync("testscript", "subject.Count = 10;",
new ExecutionOptions{HostObjects =
new List<HostObject> {new HostObject {Name = "subject", Target = subject}}});

#### Passing in Host Types
Host types allow you to instantiate a .Net type or types in the JavaScript runtime.

var manager = new RuntimeManager(new ManagerSettings());
var hostType = new HostType
{
Name = "MathStuff",
Type = typeof(System.Math)
};
var subject = new TestObject();

await manager.ExecuteAsync("testscript", "subject.Result = MathStuff.Pow(10,2);",
new ExecutionOptions{
HostObjects = new List<HostObject> { new HostObject { Name = "subject", Target = subject } },
HostTypes = new List<HostType> { hostType }});

#### Passing in Included Scripts
Included scripts can also be run by setting the IncludeScripts property on the ExecutionOptions. These scripts are
intended to set up reused libraries and will be run before the execution of the main script.

A script can be set up in a couple of ways:
* Set a file path or url in the Uri property.
* Set the code property to the code to execute.

The script will be compiled and cached in the same way as a normal script.

public class IncludeScript
{
/// Unique name of the script to execute.
public string Name { get; set; }

/// Uri (file or Url) of the script to execute. Need to include script code or script Url.
public string Uri { get; set; }

/// Code of the script to include. Need to include script code or script Url.
public string Code { get; set; }
}


### Using Require
Another way to include scripts and host objects is to use the require. Require is meant to bring in dependencies for the current script
in the manner that javascript frameworks (like node) do. So instead of including scripts, your script can instead use require
in the script itself.

//In javscript, import the request object/library
//FYI: request is a require package included as part of the ClearScript.Manager.Http nuget package
var request = require('request');
//Now use it to perform a request
request({url: 'http://api.icndb.com/jokes/random/1', json: true},
function (error, response, body) {
subject.Response = response;
subject.Body = body;
subject.Joke = body.value[0].joke;
scriptAwaiter.Callback();
});


#### Require can accept:

* The name of an existing RequirePackage

var subject = new TestObject();
var manager = new RuntimeManager(new ManualManagerSettings {ScriptTimeoutMilliSeconds = 0});

//Use the require manager to add a package - probably at startup
RequireManager.RegisterPackage(
new RequiredPackage { PackageId = "testRequire", ScriptUri = ".\\TestRequire.js" });

//Require the package from inside of javascript
await manager.ExecuteAsync("testscript",
"var testObject = require('testRequire'); subject.Count = 10;
subject.TestString = testObject.getText();",
new ExecutionOptions
{
HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}},
});

* A file path to a local script

var subject = new TestObject();
var manager = new RuntimeManager(new ManualManagerSettings {ScriptTimeoutMilliSeconds = 0});

await manager.ExecuteAsync("testscript",
@"var testObject = require('.\\TestRequire.js'); subject.Count = 10;
subject.TestString = testObject.getText();",
new ExecutionOptions
{
HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}},
});

* An http Uri to a script (if ClearScript.Manager.Http is installed)

var subject = new TestObject();
var manager = new RuntimeManager(new ManualManagerSettings {ScriptTimeoutMilliSeconds = 0});

await manager.ExecuteAsync("testscript",
@"var testObject = require('https://randomendpoint.com/testRequire'); subject.Count = 10;
subject.TestString = testObject.getText();",
new ExecutionOptions
{
HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}},
});


### Using the Manager Pool

#### Initialize the Manager Pool

//ManagerSettings should contain the maximum pool count
ManagerPool.InitializeCurrentPool(new ManagerSettings());

#### Using the Manager Scope to automatically Get/Return a Runtime Manager

using (var scope = new ManagerScope())
{
await scope.RuntimeManager.ExecuteAsync(name, "var i = 0; i++;");
}

#### Getting and Returning a Runtime Manager

var runtimeManager = ManagerPool.CurrentPool.GetRuntime()
ManagerPool.CurrentPool.ReturnToPool(runtimeManager);


##ClearScript.Manager.Http
This package enables a couple of things. As mentioned [above](#running-multiple-scripts),
it lets script references use an http uri as a loading source. But in addition, it also allows you to make node-like http calls from
within your script. Currently, this has only been tested in Http Get scenarios, will add better support shortly.

public async void Basic_Http_Get_Body_Is_Retrieved()
{
var subject = new TestObject();
var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 });

//Add packages that will be required to perform an http request.
//These packages are included as part of ClearScript.Manager.Http.
Requirer.RegisterPackage(new HttpPackage());
Requirer.RegisterPackage(new RequestPackage());

//Add in the host object to operate on
var options = new ExecutionOptions();
options.HostObjects.Add(new HostObject { Name = "subject", Target = subject });

//Create a script awaiter...this is to force .net to wait until the javascript is finished on the callback
//Notice that the scriptAwaiter.Callback is called in javscript http callback.
//The ScriptAwaiter is included in the ClearScript.Manager.Http package.
var scriptAwaiter = new ScriptAwaiter();
options.HostObjects.Add(new HostObject { Name = "scriptAwaiter", Target = scriptAwaiter });

//Grab a random Chuck Norris joke and assign it to our object in the callback
var code = "var request = require('request');" +
"request({url: 'http://api.icndb.com/jokes/random/1', json: true}," +
" function (error, response, body) {subject.Response = response; subject.Body = body; subject.Joke = body.value[0].joke; scriptAwaiter.Callback();});";

//Execute the script and await the script awaiter so that we pause until the javascript callback has completed.
await manager.ExecuteAsync("testScript", code, options);
await scriptAwaiter.T;

//Hilarity ensues
subject.Joke.ShouldNotBeNull();
}

#### Included Require Packages
* HttpPackage (http) - Gives access to the .Net Http object.
* RequestPackage (request) - Gives access to a node-like request object.


Additional examples are available in the unit tests.
# 该工具主要用js作为sql的执行上下文。
# 目前有 sql http log 三大组件。 sql 和 log 的执行器可以自由扩展。
# js编辑器有 remoteDebugger 代码智能提示 语法高亮 语法错误提示 智能搜索 代码收缩 打标签 等功能
# [js在线编辑器DEMO](https://yuzd.github.io/ClearScript.Manager/)

![image](https://github.com/yuzd/ClearScript.Manager/blob/master/tabris.gif)
# 支持debugger版本 download:https://pan.baidu.com/s/1ghgZ1tD
![image](https://github.com/yuzd/ClearScript.Manager/blob/master/tabris4.gif)
![image](https://github.com/yuzd/ClearScript.Manager/blob/master/tabris3.gif)
Loading