Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
iterate all assemblies for type binding
Browse files Browse the repository at this point in the history
  • Loading branch information
ialex32x committed Nov 8, 2019
1 parent 483c909 commit e6cc6e5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 35 deletions.
27 changes: 25 additions & 2 deletions unity/Assets/Duktape/Editor/BindingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,15 @@ public void Collect()
OnPreCollectAssemblies();
AddAssemblies(false, prefs.explicitAssemblies.ToArray());
AddAssemblies(true, prefs.implicitAssemblies.ToArray());
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (var i = 0; i < assemblies.Length; i++)
{
var assembly = assemblies[i];
if (!assembly.IsDynamic)
{
AddAssemblies(false, assembly.FullName);
}
}
OnPostCollectAssemblies();

ExportAssemblies(_explicitAssemblies, false);
Expand All @@ -1075,11 +1084,25 @@ public void AddAssemblies(bool implicitExport, params string[] assemblyNames)
{
if (implicitExport)
{
_implicitAssemblies.AddRange(assemblyNames);
for (var i = 0; i < assemblyNames.Length; i++)
{
var assemblyName = assemblyNames[i];
if (!_implicitAssemblies.Contains(assemblyName) && !_explicitAssemblies.Contains(assemblyName))
{
_implicitAssemblies.Add(assemblyName);
}
}
}
else
{
_explicitAssemblies.AddRange(assemblyNames);
for (var i = 0; i < assemblyNames.Length; i++)
{
var assemblyName = assemblyNames[i];
if (!_implicitAssemblies.Contains(assemblyName) && !_explicitAssemblies.Contains(assemblyName))
{
_explicitAssemblies.Add(assemblyName);
}
}
}
}

Expand Down
77 changes: 46 additions & 31 deletions unity/Assets/Duktape/Source/DuktapeVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,56 +318,71 @@ private static duk_ret_t cb_load_module(IntPtr ctx)
private IEnumerator _InitializeStep(IDuktapeListener listener, int step)
{
var ctx = DuktapeDLL.duk_create_heap_default();

_ctx = new DuktapeContext(this, ctx);
DuktapeAux.duk_open(ctx);
DuktapeVM.duk_open_module(ctx);
DuktapeDLL.duk_unity_open(ctx);
DuktapeDLL.duk_push_global_object(ctx);
DuktapeJSBuiltins.reg(ctx);
listener?.OnTypesBinding(this);
var exportedTypes = this.GetType().Assembly.GetExportedTypes();
var bindingTypes = new List<Type>(exportedTypes.Length);
var ctxAsArgs = new object[] { ctx };
for (int i = 0, size = exportedTypes.Length; i < size; i++)
var bindingTypes = new List<Type>();
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int assemblyIndex = 0, assemblyCount = assemblies.Length; assemblyIndex < assemblyCount; assemblyIndex++)
{
var type = exportedTypes[i];
#if UNITY_EDITOR
if (type.IsDefined(typeof(JSAutoRunAttribute), false))
var assembly = assemblies[assemblyIndex];
try
{
try
if (assembly.IsDynamic)
{
var run = type.GetMethod("Run", BindingFlags.Static | BindingFlags.Public);
if (run != null)
{
run.Invoke(null, null);
}
continue;
}
catch (Exception exception)
var exportedTypes = assembly.GetExportedTypes();
for (int i = 0, size = exportedTypes.Length; i < size; i++)
{
Debug.LogWarning($"JSAutoRun failed: {exception}");
}
continue;
}
var type = exportedTypes[i];
#if UNITY_EDITOR
if (type.IsDefined(typeof(JSAutoRunAttribute), false))
{
try
{
var run = type.GetMethod("Run", BindingFlags.Static | BindingFlags.Public);
if (run != null)
{
run.Invoke(null, null);
}
}
catch (Exception exception)
{
Debug.LogWarning($"JSAutoRun failed: {exception}");
}
continue;
}
#endif
var attributes = type.GetCustomAttributes(typeof(JSBindingAttribute), false);
if (attributes.Length == 1)
{
var jsBinding = attributes[0] as JSBindingAttribute;
if (jsBinding.Version == 0 || jsBinding.Version == VERSION)
{
bindingTypes.Add(type);
}
else
{
if (listener != null)
var attributes = type.GetCustomAttributes(typeof(JSBindingAttribute), false);
if (attributes.Length == 1)
{
listener.OnBindingError(this, type);
var jsBinding = attributes[0] as JSBindingAttribute;
if (jsBinding.Version == 0 || jsBinding.Version == VERSION)
{
bindingTypes.Add(type);
}
else
{
if (listener != null)
{
listener.OnBindingError(this, type);
}
}
}
}
}
catch (Exception e)
{
Debug.LogErrorFormat("assembly: {0}, {1}", assembly, e);
}
}

var numRegInvoked = bindingTypes.Count;
for (var i = 0; i < numRegInvoked; ++i)
{
Expand Down
3 changes: 2 additions & 1 deletion unity/Assets/Examples/Scripts/out/code.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e6cc6e5

Please sign in to comment.