static void Compile() { EditorUtility.DisplayProgressBar("compile ts", "create jsEnv", 0); JsEnv env = new JsEnv(); bool result = env.Eval <bool>(@" try { const moduleRequire = require('module').createRequire('" + Application.dataPath + @"/Examples/Editor/03_NodeTSCAndHotReload/js~/') moduleRequire('ts-node').register({ compilerOptions: { 'strict': false, 'strictNullChecks': false, 'strictPropertyInitialization': false, 'target': 'ES6', 'module': 'commonjs', 'sourceMap': true, 'moduleResolution': 'node', 'typeRoots': [ './node_modules/@types' ] } }) moduleRequire('./src/compile.ts') true; } catch(e) { console.error(e); console.error('Some error triggered. Maybe you should run `npm i` in `js~` directory'); false; } "); if (!result) { EditorUtility.ClearProgressBar(); } env.Dispose(); env = null; }
public void Array() { var jsEnv = new JsEnv(new TxtLoader()); var ret = jsEnv.Eval <string>(@" const CS = require('csharp'); let obj = new CS.Puerts.UnitTest.ArrayTest(); let sum = 0; for (var i = 0; i < 10; i++) { for (var j = 0; j < obj['a' + i].Length; j++) { sum += Number(obj['a' + i].get_Item(j)); } } for (var i = 0; i < obj.astr.Length; i++) { sum += obj.astr.get_Item(i); } for (var i = 0; i < obj.ab.Length; i++) { sum += obj.ab.get_Item(i); } let sum2 = 0; for (var i = 0; i < 10; i++) { for (var j = 0; j < obj['a' + i].Length; j++) { obj['a' + i].set_Item(j, obj['a' + i].get_Item(j) + obj['a' + i].get_Item(j)); } } for (var i = 0; i < 10; i++) { for (var j = 0; j < obj['a' + i].Length; j++) { sum2 += Number(obj['a' + i].get_Item(j)); } } //CS.System.Console.WriteLine('sum = ' + sum2 ); sum + sum2; "); jsEnv.Dispose(); Assert.AreEqual("240hellojohntruefalsetruefalse480", ret); }
void Start() { var jsEnv = new JsEnv(); jsEnv.Eval(@" const CS = require('csharp'); let obj = new CS.PuertsTest.JsObjectTest(); let jsObj = {'c': 100}; obj.Setter = (path, value) => { let tmp = jsObj; let nodes = path.split('.'); let lastNode = nodes.pop(); nodes.forEach(n => { if (typeof tmp[n] === 'undefined') tmp[n] = {}; tmp = tmp[n]; }); tmp[lastNode] = value; } obj.Getter = (path) => { let tmp = jsObj; let nodes = path.split('.'); let lastNode = nodes.pop(); nodes.forEach(n => { if (typeof tmp != 'undefined') tmp = tmp[n]; }); return tmp[lastNode]; } obj.SetSomeData(); obj.GetSomeData(); console.log(JSON.stringify(jsObj)); const a = {a: 1}; obj.storeJSObject(a); console.log(obj.getStoredJSObject() == a) "); jsEnv.Dispose(); }
public void TimerTest4() { var jsEnv = new JsEnv(new TxtLoader()); var timer = jsEnv.Eval <Timer>(@" const CS = require('csharp'); let timer = new CS.Puerts.UnitTest.Timer(Date.now()); Date.now = ()=>timer.Now(); let obj = CS.Puerts.UnitTest.TimerTest; let i = 0; let id = setInterval(()=>{obj.TimerTest4Num = ++i;},500); setTimeout(()=>{clearInterval(id);},200); timer; "); for (int i = 0; i < 100; i++) { Assert.AreEqual(0, TimerTest4Num); timer.Tick(); jsEnv.Tick(); } jsEnv.Dispose(); }
public void SetIntervalTest2() { var jsEnv = new JsEnv(new TxtLoader()); jsEnv.Eval(@" const CS = require('csharp'); let timer = new CS.Puerts.UnitTest.Timer(); Date.now = ()=>timer.Now(); let obj = CS.Puerts.UnitTest.TimerTest; let i = 0; let j = 0; setInterval(()=>{obj.SetInterval2TestNum1 = ++i;},1000); setInterval(()=>{obj.SetIntervalTestNum2 = ++j;},500); "); for (int i = 0; i < 100; i++) { Assert.AreEqual(i / 10, SetInterval2TestNum1); Assert.AreEqual(i / 5, SetIntervalTestNum2); jsEnv.Tick(); } jsEnv.Dispose(); }
public void GenericDelegate() { var jsEnv = new JsEnv(new TxtLoader()); var ret = jsEnv.Eval <double>(@" const CS = require('csharp'); let obj = new CS.Puerts.UnitTest.JsObjectTest(); let jsObj = {'c': 100}; obj.Setter = (path, value) => { let tmp = jsObj; let nodes = path.split('.'); let lastNode = nodes.pop(); nodes.forEach(n => { if (typeof tmp[n] === 'undefined') tmp[n] = {}; tmp = tmp[n]; }); tmp[lastNode] = value; } obj.Getter = (path) => { let tmp = jsObj; let nodes = path.split('.'); let lastNode = nodes.pop(); nodes.forEach(n => { if (typeof tmp != 'undefined') tmp = tmp[n]; }); return tmp[lastNode]; } obj.SetSomeData(); obj.GetSomeData(); jsObj.a + jsObj.c; "); jsEnv.Dispose(); Assert.AreEqual(101, ret); }
public void SetIntervalTest() { SetIntervalTestNum = 0; var jsEnv = new JsEnv(new TxtLoader()); var timer = jsEnv.Eval <Timer>(@" const CS = require('csharp'); let timer = new CS.Puerts.UnitTest.Timer(Date.now()); Date.now = ()=>timer.Now(); let obj = CS.Puerts.UnitTest.TimerTest; let i = 0; setInterval(()=>{obj.SetIntervalTestNum = ++i;},1000); timer; "); for (int i = 0; i < 100; i++) { Assert.AreEqual(i / 10, SetIntervalTestNum); timer.Tick(); jsEnv.Tick(); } jsEnv.Dispose(); }
public static void GenerateCode(string saveTo, bool tsOnly = false) { filters = Configure.GetFilters(); var configure = Configure.GetConfigureByTags(new List <string>() { "Puerts.BindingAttribute", "Puerts.BlittableCopyAttribute", "Puerts.TypingAttribute", }); var genTypes = configure["Puerts.BindingAttribute"].Select(kv => kv.Key) .Where(o => o is Type) .Cast <Type>() .Where(t => !t.IsGenericTypeDefinition); var blittableCopyTypes = new HashSet <Type>(configure["Puerts.BlittableCopyAttribute"].Select(kv => kv.Key) .Where(o => o is Type) .Cast <Type>() .Where(t => t.IsValueType && !t.IsPrimitive) .Distinct()); var tsTypes = configure["Puerts.TypingAttribute"].Select(kv => kv.Key) .Where(o => o is Type) .Cast <Type>() .Where(t => !t.IsGenericTypeDefinition) .Concat(genTypes) .Distinct(); using (var jsEnv = new JsEnv()) { var templateGetter = jsEnv.Eval <Func <string, Func <object, string> > >("require('puerts/gencode/main.js')"); var wrapRender = templateGetter("type.tpl"); if (!tsOnly) { var typeGenInfos = new List <TypeGenInfo>(); foreach (var type in genTypes) { if (type.IsEnum || type.IsArray || (IsDelegate(type) && type != typeof(Delegate))) { continue; } TypeGenInfo typeGenInfo = ToTypeGenInfo(type); typeGenInfo.BlittableCopy = blittableCopyTypes.Contains(type); typeGenInfos.Add(typeGenInfo); string filePath = saveTo + typeGenInfo.WrapClassName + ".cs"; string fileContext = wrapRender(typeGenInfo); using (StreamWriter textWriter = new StreamWriter(filePath, false, Encoding.UTF8)) { textWriter.Write(fileContext); textWriter.Flush(); } } var autoRegisterRender = templateGetter("autoreg.tpl"); using (StreamWriter textWriter = new StreamWriter(saveTo + "AutoStaticCodeRegister.cs", false, Encoding.UTF8)) { string fileContext = autoRegisterRender(typeGenInfos.ToArray()); textWriter.Write(fileContext); textWriter.Flush(); } } var typingRender = templateGetter("typing.tpl"); using (StreamWriter textWriter = new StreamWriter(saveTo + "Typing/csharp/index.d.ts", false, Encoding.UTF8)) { string fileContext = typingRender(ToTypingGenInfo(tsTypes)); textWriter.Write(fileContext); textWriter.Flush(); } } }
static void GenerateCode(string saveTo) { var configure = Configure.GetConfigureByTags(new List <string>() { "Puerts.BindingAttribute", }); var genTypes = configure["Puerts.BindingAttribute"].Select(kv => kv.Key) .Where(o => o is Type) .Cast <Type>() .Where(t => !t.IsGenericTypeDefinition); var refTypes = new List <Type>(); foreach (var type in genTypes) { AddRef(refTypes, type); foreach (var field in type.GetFields(Flags)) { AddRef(refTypes, field.FieldType); } foreach (var prop in type.GetProperties(Flags)) { AddRef(refTypes, (prop.CanRead ? prop.GetMethod : prop.SetMethod).ReturnType); } foreach (var method in type.GetMethods(Flags)) { AddRef(refTypes, method.ReturnType); foreach (var param in method.GetParameters()) { AddRef(refTypes, param.ParameterType); } } } var allowVoid = AllowArgumentsLength("UsingAction"); var allowReturn = AllowArgumentsLength("UsingFunc"); var genInfos = new List <GenInfo>(); foreach (var type in refTypes.Distinct()) { if (HasValueParameter(type)) { var info = ToGenInfo(type); if (info.HasReturn && !allowReturn.Contains(info.Parameters.Length) || !info.HasReturn && !allowVoid.Contains(info.Parameters.Length)) { UnityEngine.Debug.LogWarning(string.Format("Method parameter length don't match:{0} \nSource type: {1} ", info, type.ToString())); continue; } if (genInfos.Contains(info)) { continue; } genInfos.Add(info); } } using (var jsEnv = new JsEnv()) { var templateGetter = jsEnv.Eval <Func <string, Func <object, string> > >("require('puerts/gencode/main.js')"); var autoUsing = templateGetter("autousing.tpl"); using (StreamWriter textWriter = new StreamWriter(saveTo + "AutoStaticCodeUsing.cs", false, Encoding.UTF8)) { string fileContext = autoUsing(genInfos.OrderBy(o => (o.Name + "<" + string.Join(", ", o.Parameters))).ToArray()); textWriter.Write(fileContext); textWriter.Flush(); } } }
private void Awake() { jsEnv = new JsEnv(); Hotfix.Init(jsEnv); jsEnv.Eval(@"require('test_hotfix');"); }
void Start() { jsEnv = new JsEnv(); jsEnv.Eval("require('QuickStart')"); }
public static void Eval(string chunk, string chunkName = "chunk") { jsEnv.Eval(chunk, chunkName); }
public static void GenerateWrapper(string saveTo, ILoader loader = null) { if (Utils.filters == null) { Utils.filters = Configure.GetFilters(); configure = Configure.GetConfigureByTags(new List <string>() { "Puerts.BindingAttribute", "Puerts.BlittableCopyAttribute", "Puerts.TypingAttribute", }); genTypes = configure["Puerts.BindingAttribute"].Select(kv => kv.Key) .Where(o => o is Type) .Cast <Type>() .Where(t => !t.IsGenericTypeDefinition && !t.Name.StartsWith("<")) .Distinct() .ToList(); } var blittableCopyTypes = new HashSet <Type>(configure["Puerts.BlittableCopyAttribute"].Select(kv => kv.Key) .Where(o => o is Type) .Cast <Type>() .Where(t => !t.IsPrimitive && Utils.isBlittableType(t)) .Distinct()); if (loader == null) { loader = new DefaultLoader(); } using (var jsEnv = new JsEnv(loader)) { var wrapRender = jsEnv.Eval <Func <Wrapper.TypeGenInfo, string> >("require('puerts/templates/wrapper.tpl.cjs')"); var typeGenInfos = new List <Wrapper.TypeGenInfo>(); Dictionary <string, bool> makeFileUniqueMap = new Dictionary <string, bool>(); foreach (var type in genTypes) { if (type.IsEnum || type.IsArray || (Generator.Utils.IsDelegate(type) && type != typeof(Delegate))) { continue; } Wrapper.TypeGenInfo typeGenInfo = Wrapper.TypeGenInfo.FromType(type, genTypes); typeGenInfo.BlittableCopy = blittableCopyTypes.Contains(type); typeGenInfos.Add(typeGenInfo); string filePath = saveTo + typeGenInfo.WrapClassName + ".cs"; int uniqueId = 1; while (makeFileUniqueMap.ContainsKey(filePath.ToLower())) { filePath = saveTo + typeGenInfo.WrapClassName + "_" + uniqueId + ".cs"; uniqueId++; } makeFileUniqueMap.Add(filePath.ToLower(), true); string fileContext = wrapRender(typeGenInfo); using (StreamWriter textWriter = new StreamWriter(filePath, false, Encoding.UTF8)) { textWriter.Write(fileContext); textWriter.Flush(); } } var autoRegisterRender = jsEnv.Eval <Func <Wrapper.TypeGenInfo[], string> >("require('puerts/templates/wrapper-reg.tpl.cjs')"); using (StreamWriter textWriter = new StreamWriter(saveTo + "AutoStaticCodeRegister.cs", false, Encoding.UTF8)) { string fileContext = autoRegisterRender(typeGenInfos.ToArray()); textWriter.Write(fileContext); textWriter.Flush(); } } }