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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ialex32x committed Aug 16, 2019
2 parents 4604e61 + bb1f0cc commit cc33116
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 34 deletions.
95 changes: 67 additions & 28 deletions unity/Assets/Duktape/Source/DuktapeAux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static void PrintError(IntPtr ctx, int idx, string filename)
public static void duk_open(IntPtr ctx)
{
DuktapeDLL.duk_push_global_object(ctx);
DuktapeDLL.duk_push_c_function(ctx, duk_print, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_push_c_function(ctx, duk_print_log, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_put_prop_string(ctx, -2, "print");

DuktapeDLL.duk_push_c_function(ctx, duk_dofile, 1);
Expand Down Expand Up @@ -116,11 +116,46 @@ public static int duk_addSearchPath(IntPtr ctx)
}

[AOT.MonoPInvokeCallback(typeof(DuktapeDLL.duk_c_function))]
public static int duk_print(IntPtr ctx)
public static int duk_print_log(IntPtr ctx)
{
Debug.Log(duk_print_string(ctx, 0, printStacktrace));
return 0;
}

[AOT.MonoPInvokeCallback(typeof(DuktapeDLL.duk_c_function))]
public static int duk_print_warn(IntPtr ctx)
{
Debug.LogWarning(duk_print_string(ctx, 0, printStacktrace));
return 0;
}

[AOT.MonoPInvokeCallback(typeof(DuktapeDLL.duk_c_function))]
public static int duk_print_err(IntPtr ctx)
{
Debug.LogError(duk_print_string(ctx, 0, printStacktrace));
return 0;
}

[AOT.MonoPInvokeCallback(typeof(DuktapeDLL.duk_c_function))]
public static int duk_assert(IntPtr ctx)
{
if (!DuktapeDLL.duk_get_boolean(ctx, 0))
{
var msg = string.Empty;
if (DuktapeDLL.duk_get_top(ctx) > 1)
{
msg = duk_print_string(ctx, 1, false);
}
return DuktapeDLL.duk_generic_error(ctx, "assertion failed: " + msg);
}
return 0;
}

private static string duk_print_string(IntPtr ctx, int startIndex, bool withStacktrace)
{
var narg = DuktapeDLL.duk_get_top(ctx);
var str = string.Empty;
for (int i = 0; i < narg; i++)
for (int i = startIndex; i < narg; i++)
{
object o;
if (DuktapeBinding.duk_get_object(ctx, i, out o))
Expand All @@ -132,35 +167,39 @@ public static int duk_print(IntPtr ctx)
str += DuktapeDLL.duk_safe_to_string(ctx, i) + " ";
}
}
if (printStacktrace)
if (withStacktrace)
{
str += $"\n{duk_get_stacktrace(ctx)}";
}
return str;
}

private static string duk_get_stacktrace(IntPtr ctx)
{
var stacktrace = "stacktrace:\n";
for (int i = -2; ; i--)
{
var stacktrace = "stacktrace:\n";
for (int i = -2; ; i--)
DuktapeDLL.duk_inspect_callstack_entry(ctx, i);
if (!DuktapeDLL.duk_is_undefined(ctx, -1))
{
DuktapeDLL.duk_inspect_callstack_entry(ctx, i);
if (!DuktapeDLL.duk_is_undefined(ctx, -1))
{
DuktapeDLL.duk_get_prop_string(ctx, -1, "lineNumber");
var lineNumber = DuktapeDLL.duk_to_int(ctx, -1);
DuktapeDLL.duk_get_prop_string(ctx, -2, "function");
DuktapeDLL.duk_get_prop_string(ctx, -1, "name");
var funcName = DuktapeDLL.duk_safe_to_string(ctx, -1);
DuktapeDLL.duk_get_prop_string(ctx, -2, "fileName");
var fileName = DuktapeDLL.duk_safe_to_string(ctx, -1);
DuktapeDLL.duk_pop_n(ctx, 4);
stacktrace += (duk_source_position ?? default_duk_source_position)(ctx, funcName, fileName, lineNumber);
stacktrace += "\n";
}
else
{
DuktapeDLL.duk_pop(ctx);
break;
}
DuktapeDLL.duk_get_prop_string(ctx, -1, "lineNumber");
var lineNumber = DuktapeDLL.duk_to_int(ctx, -1);
DuktapeDLL.duk_get_prop_string(ctx, -2, "function");
DuktapeDLL.duk_get_prop_string(ctx, -1, "name");
var funcName = DuktapeDLL.duk_safe_to_string(ctx, -1);
DuktapeDLL.duk_get_prop_string(ctx, -2, "fileName");
var fileName = DuktapeDLL.duk_safe_to_string(ctx, -1);
DuktapeDLL.duk_pop_n(ctx, 4);
stacktrace += (duk_source_position ?? default_duk_source_position)(ctx, funcName, fileName, lineNumber);
stacktrace += "\n";
}
else
{
DuktapeDLL.duk_pop(ctx);
break;
}
str += $"\n{stacktrace}";
}
Debug.Log(str);
return 0;
return stacktrace;
}

public static string default_duk_source_position(IntPtr ctx, string funcName, string fileName, int lineNumber)
Expand Down
2 changes: 1 addition & 1 deletion unity/Assets/Duktape/Source/DuktapeBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected static int object_dtor(IntPtr ctx)
if (DuktapeDLL.duk_get_prop_string(ctx, 0, DuktapeVM.OBJ_PROP_NATIVE))
{
var id = DuktapeDLL.duk_get_int(ctx, -1);
DuktapeVM.GetObjectCache(ctx).RemoveObject(id);
DuktapeVM.GetObjectCache(ctx)?.RemoveObject(id);
}
DuktapeDLL.duk_pop(ctx); // pop native
return 0;
Expand Down
2 changes: 1 addition & 1 deletion unity/Assets/Duktape/Source/DuktapeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void onDestroy()

public static DuktapeVM GetVM(IntPtr ctx)
{
return DuktapeVM.GetContext(ctx)._vm;
return DuktapeVM.GetContext(ctx)?._vm;
}

// 获取全局函数并调用 (do not cache it)
Expand Down
6 changes: 6 additions & 0 deletions unity/Assets/Duktape/Source/DuktapeDLL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,15 @@ public static duk_bool_t duk_is_uri_error(IntPtr ctx, duk_idx_t idx)
[DllImport(DUKTAPEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern duk_bool_t duk_get_boolean(IntPtr ctx, duk_idx_t idx);

[DllImport(DUKTAPEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern duk_bool_t duk_get_boolean_default(IntPtr ctx, duk_idx_t idx, duk_bool_t def_value);

[DllImport(DUKTAPEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern duk_double_t duk_get_number(IntPtr ctx, duk_idx_t idx);

[DllImport(DUKTAPEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern duk_double_t duk_get_number_default(IntPtr ctx, duk_idx_t idx, duk_double_t def_value);

[DllImport(DUKTAPEDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern duk_int_t duk_get_int(IntPtr ctx, duk_idx_t idx);

Expand Down
12 changes: 9 additions & 3 deletions unity/Assets/Duktape/Source/DuktapeJSBuiltins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,18 @@ public static void reg(IntPtr ctx)

{
duk_begin_namespace(ctx, "console");
DuktapeDLL.duk_push_c_function(ctx, DuktapeAux.duk_print, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_push_c_function(ctx, DuktapeAux.duk_print_log, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_put_prop_string(ctx, -2, "log");
DuktapeDLL.duk_push_c_function(ctx, DuktapeAux.duk_print, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_push_c_function(ctx, DuktapeAux.duk_print_log, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_put_prop_string(ctx, -2, "info");
DuktapeDLL.duk_push_c_function(ctx, DuktapeAux.duk_print_log, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_put_prop_string(ctx, -2, "debug");
DuktapeDLL.duk_push_c_function(ctx, DuktapeAux.duk_print_warn, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_put_prop_string(ctx, -2, "warn");
DuktapeDLL.duk_push_c_function(ctx, DuktapeAux.duk_print, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_push_c_function(ctx, DuktapeAux.duk_print_err, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_put_prop_string(ctx, -2, "error");
DuktapeDLL.duk_push_c_function(ctx, DuktapeAux.duk_assert, DuktapeDLL.DUK_VARARGS);
DuktapeDLL.duk_put_prop_string(ctx, -2, "assert");
duk_end_namespace(ctx);
}

Expand Down
6 changes: 5 additions & 1 deletion unity/Assets/Duktape/Source/DuktapeVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static DuktapeVM GetVM(IntPtr ctx)

public static ObjectCache GetObjectCache(IntPtr ctx)
{
return DuktapeContext.GetVM(ctx)._objectCache;
return DuktapeContext.GetVM(ctx)?._objectCache;
}

public void AddSearchPath(string path)
Expand Down Expand Up @@ -514,6 +514,10 @@ public void Destroy()
var ctx = _ctx.rawValue;
_ctx.onDestroy();
_ctx = null;
_lastContextPtr = IntPtr.Zero;
_lastContext = null;
_contexts.Clear();
_objectCache.Clear();
DuktapeDLL.duk_destroy_heap(ctx);
// Debug.LogWarning("duk_destroy_heap");
}
Expand Down
7 changes: 7 additions & 0 deletions unity/Assets/Duktape/Source/ObjectCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public class ObjectCache
// host object => jsvalue heapptr (dangerous)
private Dictionary<object, IntPtr> _rmap = new Dictionary<object, IntPtr>(EqualityComparer.Default);

public void Clear()
{
_index = 0;
_map.Clear();
_rmap.Clear();
}

public void AddJSValue(object o, IntPtr heapptr)
{
if (o != null)
Expand Down

0 comments on commit cc33116

Please sign in to comment.