Skip to content

Commit

Permalink
version 1.1.0 fix #528 #470
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieyang authored Nov 1, 2021
1 parent a3bb432 commit 83167f1
Show file tree
Hide file tree
Showing 15 changed files with 609 additions and 326 deletions.
103 changes: 75 additions & 28 deletions unity/Assets/Puerts/Src/Editor/Generator.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ namespace PuertsStaticWrap
{
return new IntPtr(result);
}{{}else{}}return Puerts.Utils.GetObjectPtr((int)data, typeof({{=it.Name}}), result);{{}}}
}{{~}}
}
{{~}}
}
{{~}}{{?}}
{{?!it.Constructor || (it.Constructor.OverloadCount != 1)}}Puerts.PuertsDLL.ThrowException(isolate, "invalid arguments to {{=it.Name}} constructor");{{?}}
Expand All @@ -194,6 +195,7 @@ namespace PuertsStaticWrap
return IntPtr.Zero;
}
{{~it.Methods :method}}
{{?!method.IsLazyMember}}
[Puerts.MonoPInvokeCallback(typeof(Puerts.V8FunctionCallback))]
{{?it.BlittableCopy && !method.IsStatic}}unsafe {{?}}private static void {{=(method.IsStatic ? "F" : "M")}}_{{=method.Name}}(IntPtr isolate, IntPtr info, IntPtr self, int paramLen, long data)
{
Expand All @@ -218,7 +220,8 @@ namespace PuertsStaticWrap
{{?!overload.IsVoid}}{{=setReturn(overload)}};{{?}}
{{?!it.BlittableCopy && !method.IsStatic}}{{=setSelf(it)}}{{?}}
{{?method.HasOverloads}}return;{{?}}
}{{~}}
}
{{~}}
}
{{~}}
{{?method.HasOverloads}}Puerts.PuertsDLL.ThrowException(isolate, "invalid arguments to {{=method.Name}}");{{?}}
Expand All @@ -228,8 +231,11 @@ namespace PuertsStaticWrap
Puerts.PuertsDLL.ThrowException(isolate, "c# exception:" + e.Message + ",stack:" + e.StackTrace);
}
}
{{?}}
{{~}}

{{~it.Properties :property}}
{{?!property.IsLazyMember}}
{{?property.HasGetter}}
[Puerts.MonoPInvokeCallback(typeof(Puerts.V8FunctionCallback))]
{{?it.BlittableCopy && !property.IsStatic}}unsafe {{?}}private static void G_{{=property.Name}}(IntPtr isolate, IntPtr info, IntPtr self, int paramLen, long data)
Expand Down Expand Up @@ -261,8 +267,11 @@ namespace PuertsStaticWrap
Puerts.PuertsDLL.ThrowException(isolate, "c# exception:" + e.Message + ",stack:" + e.StackTrace);
}
}{{?}}
{{?}}
{{~}}

{{?it.GetIndexs.Length > 0}}
{{?!it.IsLazyMember}}
[Puerts.MonoPInvokeCallback(typeof(Puerts.V8FunctionCallback))]
{{?it.BlittableCopy}}unsafe {{?}}private static void GetItem(IntPtr isolate, IntPtr info, IntPtr self, int paramLen, long data)
{
Expand All @@ -285,7 +294,10 @@ namespace PuertsStaticWrap
Puerts.PuertsDLL.ThrowException(isolate, "c# exception:" + e.Message + ",stack:" + e.StackTrace);
}
}
{{?}}{{?it.SetIndexs.Length > 0}}
{{?}}
{{?}}
{{?it.SetIndexs.Length > 0}}
{{?!it.IsLazyMember}}
[Puerts.MonoPInvokeCallback(typeof(Puerts.V8FunctionCallback))]
{{?it.BlittableCopy}}unsafe {{?}}private static void SetItem(IntPtr isolate, IntPtr info, IntPtr self, int paramLen, long data)
{
Expand All @@ -307,8 +319,11 @@ namespace PuertsStaticWrap
{
Puerts.PuertsDLL.ThrowException(isolate, "c# exception:" + e.Message + ",stack:" + e.StackTrace);
}
}{{?}}
}
{{?}}
{{?}}
{{~it.Operators :operator}}
{{?!operator.IsLazyMember}}
[Puerts.MonoPInvokeCallback(typeof(Puerts.V8FunctionCallback))]
private static void O_{{=operator.Name}}(IntPtr isolate, IntPtr info, IntPtr self, int paramLen, long data)
{
Expand All @@ -329,7 +344,8 @@ namespace PuertsStaticWrap
var result = {{=operatorCall(operator.Name, overload.ParameterInfos.Length, overload)}};
{{?!overload.IsVoid}}{{=setReturn(overload)}};{{?}}
{{?operator.HasOverloads}}return;{{?}}
}{{~}}
}
{{~}}
}
{{~}}
{{?operator.HasOverloads}}Puerts.PuertsDLL.ThrowException(isolate, "invalid arguments to {{=operator.Name}}");{{?}}
Expand All @@ -339,8 +355,10 @@ namespace PuertsStaticWrap
Puerts.PuertsDLL.ThrowException(isolate, "c# exception:" + e.Message + ",stack:" + e.StackTrace);
}
}
{{?}}
{{~}}
{{~it.Events :eventInfo}}
{{?!eventInfo.IsLazyMember}}
{{?eventInfo.HasAdd}}
[Puerts.MonoPInvokeCallback(typeof(Puerts.V8FunctionCallback))]
{{?it.BlittableCopy && !eventInfo.IsStatic}}unsafe {{?}}private static void A_{{=eventInfo.Name}}(IntPtr isolate, IntPtr info, IntPtr self, int paramLen, long data)
Expand Down Expand Up @@ -371,6 +389,7 @@ namespace PuertsStaticWrap
Puerts.PuertsDLL.ThrowException(isolate, "c# exception:" + e.Message + ",stack:" + e.StackTrace);
}
}
{{?}}
{{?}}{{~}}
public static Puerts.TypeRegisterInfo GetRegisterInfo()
{
Expand All @@ -380,18 +399,53 @@ namespace PuertsStaticWrap
Constructor = Constructor,
Methods = new System.Collections.Generic.Dictionary<Puerts.MethodKey, Puerts.V8FunctionCallback>()
{
{{~it.Methods :method}}{ new Puerts.MethodKey {Name = "{{=method.Name}}", IsStatic = {{=method.IsStatic}}}, {{=(method.IsStatic ? "F" : "M")}}_{{=method.Name}} },
{{~}}{{?it.GetIndexs.Length > 0}}{ new Puerts.MethodKey {Name = "get_Item", IsStatic = false}, GetItem },
{{?}}{{?it.SetIndexs.Length > 0}}{ new Puerts.MethodKey {Name = "set_Item", IsStatic = false}, SetItem},
{{?}}{{~it.Operators :operator}}{ new Puerts.MethodKey {Name = "{{=operator.Name}}", IsStatic = true}, O_{{=operator.Name}}},
{{~}}{{~it.Events :eventInfo}}{{?eventInfo.HasAdd}}{ new Puerts.MethodKey {Name = "add_{{=eventInfo.Name}}", IsStatic = {{=eventInfo.IsStatic}}}, A_{{=eventInfo.Name}}},
{{?}}{{?eventInfo.HasRemove}}{ new Puerts.MethodKey {Name = "remove_{{=eventInfo.Name}}", IsStatic = {{=eventInfo.IsStatic}}}, R_{{=eventInfo.Name}}},
{{~it.Methods :method}}{{?!method.IsLazyMember}}
{ new Puerts.MethodKey {Name = "{{=method.Name}}", IsStatic = {{=method.IsStatic}}}, {{=(method.IsStatic ? "F" : "M")}}_{{=method.Name}} },
{{?}}{{~}}
{{?it.GetIndexs.Length > 0}}{{?!it.IsLazyMember}}
{ new Puerts.MethodKey {Name = "get_Item", IsStatic = false}, GetItem },
{{?}}{{?}}
{{?it.SetIndexs.Length > 0}}{{?!it.IsLazyMember}}
{ new Puerts.MethodKey {Name = "set_Item", IsStatic = false}, SetItem },
{{?}}{{?}}
{{~it.Operators :operator}}{{?!operator.IsLazyMember}}
{ new Puerts.MethodKey {Name = "{{=operator.Name}}", IsStatic = true}, O_{{=operator.Name}} },
{{?}}{{~}}
{{~it.Events :eventInfo}}{{?!eventInfo.IsLazyMember}}
{{?eventInfo.HasAdd}} { new Puerts.MethodKey {Name = "add_{{=eventInfo.Name}}", IsStatic = {{=eventInfo.IsStatic}}}, A_{{=eventInfo.Name}} }, {{?}}
{{?eventInfo.HasRemove}} { new Puerts.MethodKey {Name = "remove_{{=eventInfo.Name}}", IsStatic = {{=eventInfo.IsStatic}}}, R_{{=eventInfo.Name}} }, {{?}}
{{?}}{{~}}
},
Properties = new System.Collections.Generic.Dictionary<string, Puerts.PropertyRegisterInfo>()
{
{{~it.Properties :property}}{"{{=property.Name}}", new Puerts.PropertyRegisterInfo(){ IsStatic = {{=property.IsStatic}}, Getter = {{=property.HasGetter?"G_"+property.Name:"null"}}, Setter = {{=property.HasSetter?"S_"+property.Name:"null"}}} },
{{~}}
{{~it.Properties :property}}{{?!property.IsLazyMember}}
{"{{=property.Name}}", new Puerts.PropertyRegisterInfo(){ IsStatic = {{=property.IsStatic}}, Getter = {{=property.HasGetter?"G_"+property.Name:"null"}}, Setter = {{=property.HasSetter?"S_"+property.Name:"null"}}} },
{{?}}{{~}}
},
LazyMethods = new System.Collections.Generic.Dictionary<Puerts.MethodKey, Puerts.V8FunctionCallback>()
{
{{~it.Methods :method}}{{?method.IsLazyMember}}
{ new Puerts.MethodKey {Name = "{{=method.Name}}", IsStatic = {{=method.IsStatic}}}, null},
{{?}}{{~}}
{{?it.GetIndexs.Length > 0}}{{?it.IsLazyMember}}
{ new Puerts.MethodKey {Name = "get_Item", IsStatic = false}, null },
{{?}}{{?}}
{{?it.SetIndexs.Length > 0}}{{?it.IsLazyMember}}
{ new Puerts.MethodKey {Name = "set_Item", IsStatic = false}, null},
{{?}}{{?}}
{{~it.Operators :operator}}{{?operator.IsLazyMember}}
{ new Puerts.MethodKey {Name = "{{=operator.Name}}", IsStatic = true}, null},
{{?}}{{~}}
{{~it.Events :eventInfo}}{{?eventInfo.IsLazyMember}}
{{?eventInfo.HasAdd}} { new Puerts.MethodKey {Name = "add_{{=eventInfo.Name}}", IsStatic = {{=eventInfo.IsStatic}}}, null}, {{?}}
{{?eventInfo.HasRemove}} { new Puerts.MethodKey {Name = "remove_{{=eventInfo.Name}}", IsStatic = {{=eventInfo.IsStatic}}}, null}, {{?}}
{{?}}{{~}}
},
LazyProperties = new System.Collections.Generic.Dictionary<string, Puerts.PropertyRegisterInfo>()
{
{{~it.Properties :property}}{{?property.IsLazyMember}}
{"{{=property.Name}}", new Puerts.PropertyRegisterInfo(){ IsStatic = {{=property.IsStatic}} } },
{{?}}{{~}}
}
};
}
Expand Down
60 changes: 23 additions & 37 deletions unity/Assets/Puerts/Src/JsEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@ namespace Puerts
public delegate void FunctionCallback(IntPtr isolate, IntPtr info, IntPtr self, int argumentsLen);
public delegate object ConstructorCallback(IntPtr isolate, IntPtr info, int argumentsLen);

public enum JsEnvMode
{
Default = 0,
Node = 1,
External = 2
}

public enum TypeRegisterMode
{
Mixed = 0,
GeneratedCodeFirst = 1,
GeneratedCodeOnly = 2
}

public class JsEnv : IDisposable
{
internal readonly int Idx;
Expand All @@ -52,10 +38,6 @@ public class JsEnv : IDisposable

public static List<JsEnv> jsEnvs = new List<JsEnv>();

public TypeRegisterMode TypeRegisterMode = TypeRegisterMode.Mixed;

public JsEnvMode mode;

#if UNITY_EDITOR
public delegate void JsEnvCreateCallback(JsEnv env, ILoader loader, int debugPort);
public delegate void JsEnvDisposeCallback(JsEnv env);
Expand All @@ -65,45 +47,36 @@ public class JsEnv : IDisposable
public int debugPort;
#endif

public JsEnv(JsEnvMode mode = JsEnvMode.Default)
: this(new DefaultLoader(), -1, mode, IntPtr.Zero, IntPtr.Zero)
public JsEnv()
: this(new DefaultLoader(), -1, IntPtr.Zero, IntPtr.Zero)
{
}

public JsEnv(ILoader loader, int debugPort = -1, JsEnvMode mode = JsEnvMode.Default)
: this(loader, debugPort, mode, IntPtr.Zero, IntPtr.Zero)
public JsEnv(ILoader loader, int debugPort = -1)
: this(loader, debugPort, IntPtr.Zero, IntPtr.Zero)
{
}

public JsEnv(ILoader loader, IntPtr externalRuntime, IntPtr externalContext)
: this(loader, -1, JsEnvMode.External, externalRuntime, externalContext)
: this(loader, -1, externalRuntime, externalContext)
{
}

public JsEnv(ILoader loader, int debugPort, IntPtr externalRuntime, IntPtr externalContext)
: this(loader, debugPort, JsEnvMode.External, externalRuntime, externalContext)
{
}

public JsEnv(ILoader loader, int debugPort, JsEnvMode mode, IntPtr externalRuntime, IntPtr externalContext)
{
const int libVersionExpect = 13;
const int libVersionExpect = 14;
int libVersion = PuertsDLL.GetLibVersion();
if (libVersion != libVersionExpect)
{
throw new InvalidProgramException("expect lib version " + libVersionExpect + ", but got " + libVersion);
}
// PuertsDLL.SetLogCallback(LogCallback, LogWarningCallback, LogErrorCallback);
this.loader = loader;
this.mode = mode;
if (mode == JsEnvMode.External)

if (externalRuntime != IntPtr.Zero)
{
isolate = PuertsDLL.CreateJSEngineWithExternalEnv(externalRuntime, externalContext);
}
else if (mode == JsEnvMode.Node)
{
isolate = PuertsDLL.CreateJSEngineWithNode();
}
else
{
isolate = PuertsDLL.CreateJSEngine();
Expand Down Expand Up @@ -175,21 +148,34 @@ public JsEnv(ILoader loader, int debugPort, JsEnvMode mode, IntPtr externalRunti
PuertsDLL.CreateInspector(isolate, debugPort);
}

bool isNode = PuertsDLL.GetLibBackend() == 1;
ExecuteFile("puerts/init.js");
ExecuteFile("puerts/log.js");
ExecuteFile("puerts/cjsload.js");
ExecuteFile("puerts/modular.js");
ExecuteFile("puerts/csharp.js");
if (mode != JsEnvMode.Node)
#if !PUERTS_GENERAL
if (!isNode)
{
#endif
ExecuteFile("puerts/timer.js");
#if !PUERTS_GENERAL
}
#endif
ExecuteFile("puerts/events.js");
ExecuteFile("puerts/promises.js");
if (mode != JsEnvMode.Node)
#if !PUERTS_GENERAL
if (!isNode)
{
#endif
ExecuteFile("puerts/polyfill.js");
#if !PUERTS_GENERAL
}
else
{
ExecuteFile("puerts/nodepatch.js");
}
#endif

#if UNITY_EDITOR
if (OnJsEnvCreate != null)
Expand Down
13 changes: 5 additions & 8 deletions unity/Assets/Puerts/Src/PuertsDLL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,10 @@ public class PuertsDLL
public static extern int GetLibVersion();

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern bool IsJSEngineBackendSupported(JsEnvMode mode);
public static extern int GetLibBackend();

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateJSEngine();

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateJSEngineWithNode();

[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CreateJSEngineWithExternalEnv(IntPtr externalRuntime, IntPtr externalContext);
Expand Down Expand Up @@ -150,7 +147,7 @@ public static void SetGeneralDestructor(IntPtr isolate, V8DestructorCallback gen
SetGeneralDestructor(isolate, fn);
}

#if PUERTS_GENERAL
#if PUERTS_GENERAL && !PUERTS_GENERAL_OSX
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr Eval(IntPtr isolate, byte[] code, string path);

Expand Down Expand Up @@ -244,7 +241,7 @@ public static bool RegisterProperty(IntPtr isolate, int classID, string name, bo
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void ReturnNumber(IntPtr isolate, IntPtr info, double number);

#if PUERTS_GENERAL
#if PUERTS_GENERAL && !PUERTS_GENERAL_OSX
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl, EntryPoint = "ReturnString")]
public static extern void __ReturnString(IntPtr isolate, IntPtr info, byte[] str);
#else
Expand All @@ -260,7 +257,7 @@ public static void ReturnString(IntPtr isolate, IntPtr info, string str)
}
else
{
#if PUERTS_GENERAL
#if PUERTS_GENERAL && !PUERTS_GENERAL_OSX
__ReturnString(isolate, info, Encoding.UTF8.GetBytes(str));
#else
__ReturnString(isolate, info, str);
Expand Down Expand Up @@ -347,7 +344,7 @@ public static long GetBigIntFromValueChecked(IntPtr isolate, IntPtr value, bool
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetDateToOutValue(IntPtr isolate, IntPtr value, double date);

#if PUERTS_GENERAL
#if PUERTS_GENERAL && !PUERTS_GENERAL_OSX
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetStringToOutValue(IntPtr isolate, IntPtr value, byte[] str);

Expand Down
9 changes: 9 additions & 0 deletions unity/Assets/Puerts/Src/Resources/puerts/nodepatch.js.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function() {
process.on('uncaughtException', (e) => { console.error(e); })
process.exit = function() {
console.log('`process.exit` is not allowed in puerts')
}
process.kill = function() {
console.log('`process.kill` is not allowed in puerts')
}
})()
8 changes: 8 additions & 0 deletions unity/Assets/Puerts/Src/StaticWrapRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public struct MethodKey
{
public string Name;
public bool IsStatic;

//TODO 目前只是为了在group by的时候用一下
public bool IsLazyMember;
public bool IsExtension;
}

public class TypeRegisterInfo
Expand All @@ -31,5 +35,9 @@ public class TypeRegisterInfo
public Dictionary<MethodKey, V8FunctionCallback> Methods;

public Dictionary<string, PropertyRegisterInfo> Properties;

public Dictionary<MethodKey, V8FunctionCallback> LazyMethods;

public Dictionary<string, PropertyRegisterInfo> LazyProperties;
}
}
Loading

0 comments on commit 83167f1

Please sign in to comment.