From 66377a6485971a7f54ada54a7efa487670332706 Mon Sep 17 00:00:00 2001 From: Jess Balint Date: Fri, 22 Feb 2013 12:56:03 -0600 Subject: [PATCH] remove list code add eq handling to java type metatables add more tests and slight test re-org --- Makefile.win32 | 4 +- debuglib.lua | 38 ++----- java_bridge.lua | 29 +++++- list.c | 107 -------------------- list.h | 38 ------- test_basic.lua => test/test_basic.lua | 0 test/test_java_bridge.lua | 53 ++++++++++ test_lua_java.lua => test/test_lua_java.lua | 0 8 files changed, 90 insertions(+), 179 deletions(-) delete mode 100644 list.c delete mode 100644 list.h rename test_basic.lua => test/test_basic.lua (100%) create mode 100644 test/test_java_bridge.lua rename test_lua_java.lua => test/test_lua_java.lua (100%) diff --git a/Makefile.win32 b/Makefile.win32 index 4ce11cc..0c3dc00 100644 --- a/Makefile.win32 +++ b/Makefile.win32 @@ -8,8 +8,8 @@ LDFLAGS += /LIBPATH:"%LUA_HOME%"\lib .c.obj: $(CC) $(CFLAGS) /c $< -yt.dll: yt.obj list.obj lua_interface.obj lua_java.obj jni_util.obj +yt.dll: yt.obj lua_interface.obj lua_java.obj jni_util.obj link /DLL /DEBUG /OUT:yt.dll /INCREMENTAL:NO $(LDFLAGS) $^ lua52.lib clean: - -rm yt.obj list.obj lua_interface.obj lua_java.obj jni_util.obj yt.dll + -rm yt.obj lua_interface.obj lua_java.obj jni_util.obj yt.dll diff --git a/debuglib.lua b/debuglib.lua index 431493c..3e9dfc8 100644 --- a/debuglib.lua +++ b/debuglib.lua @@ -195,6 +195,9 @@ end -- | |__| | \ / | | | | | | _| |_ | |___| (_| | | | |_) | (_| | (__| <\__ \ -- \____/ \/ |_| |_| |_| |_____| \_____\__,_|_|_|_.__/ \__,_|\___|_|\_\___/ +-- ============================================================ +-- Handle the callback when a breakpoint is hit +-- ============================================================ function cb_breakpoint(thread, method_id, location) return true end @@ -295,35 +298,8 @@ function x() print(dump(lj_get_class_methods(lj_find_class("java/lang/String")))) end -function y() - local s = lj_call_method(lj_get_current_thread(), xtoString, "L", 0) - print(s) - local y = lj_call_method(s, xconcat, "L", 1, "STR", ", lol") - print(y) -end - -function run_test() - loadfile("test_basic.lua")() - loadfile("test_lua_java.lua")() -end - -function z() -- throwing a lua assertion "not enough elements in the stack" - local x = lj_get_current_thread() - local y = lj_get_method_id("java/lang/Thread", "activeCount", "", "I") - lj_call_method(x, y, "I", 0) - print(lj_get_current_thread().getName()) -end - -function z1() - local x = lj_get_current_thread() - local y = lj_get_field_id("java/lang/Thread", "tid", "J") - print(lj_get_field(x, y, false)) - local z = lj_get_field_id("java/lang/Thread", "MIN_PRIORITY", "I") - print(z) - print(dump(x.fields)) - print(dump(lj_get_field_modifiers_table(lj_get_field_modifiers(z)))) - print(lj_get_field(x.class, z, true)) - print(x.MIN_PRIORITY) - print(x.MAX_PRIORITY) - print(x.me) +function run_tests() + loadfile("test/test_basic.lua")() + loadfile("test/test_lua_java.lua")() + loadfile("test/test_java_bridge.lua")() end diff --git a/java_bridge.lua b/java_bridge.lua index 678b980..7f84a5e 100644 --- a/java_bridge.lua +++ b/java_bridge.lua @@ -22,6 +22,11 @@ jmethod_id_mt.__tostring = function(method_id) method_id.name, method_id.sig) end +jmethod_id_mt.__eq = function(m1, m2) + return m1.name == m2.name and + m1.sig == m2.sig and + m1.class == m2.class +end jmethod_id_mt.__index = function(method_id, k) if k == "name" then return lj_get_method_name(method_id).name @@ -54,6 +59,11 @@ jfield_id_mt.__tostring = function(field_id) field_id.name, field_id.sig) end +jfield_id_mt.__eq = function(f1, f2) + return m1.name == m2.name and + m1.sig == m2.sig and + m1.class == m2.class +end jfield_id_mt.__index = function(field_id, k) if k == "name" then return lj_get_field_name(field_id).name @@ -74,6 +84,14 @@ jobject_mt.__tostring = function(object) lj_pointer_to_string(object), lj_toString(object)) end +jobject_mt.__eq = function(o1, o2) + -- only handle class comparison + if lj_toString(o1.class) == "class java.lang.Class" then + return lj_toString(o1) == lj_toString(o2) + end + + return false +end jobject_mt.__index = function(object, key) -- we cannot use anything that would result in calling this function recursively local getclass_method_id = lj_get_method_id("java/lang/Object", "getClass", "", "Ljava/lang/Class;") @@ -106,8 +124,9 @@ jobject_mt.__index = function(object, key) end end +-- ============================================================ +-- search up the class hierarchy for methods called `name' function find_methods(class, name) - -- search up the class hierarchy for methods local methods = {} local superclass_method_id = lj_get_method_id("java/lang/Class", "getSuperclass", "", "Ljava/lang/Class;") while class do @@ -122,6 +141,8 @@ function find_methods(class, name) return methods end +-- ============================================================ +-- search up the class hierarchy for the first field called `name' function find_field(class, name) local superclass_method_id = lj_get_method_id("java/lang/Class", "getSuperclass", "", "Ljava/lang/Class;") while class do @@ -135,6 +156,9 @@ function find_field(class, name) return nil end +-- ============================================================ +-- create a callable closure to call one of the +-- `possible_methods' on `object' function new_jcallable_method(object, possible_methods) local jcm = {} local mt = { @@ -143,6 +167,9 @@ function new_jcallable_method(object, possible_methods) return setmetatable(jcm, mt) end +-- ============================================================ +-- perform the actual method call. this will match the `args' +-- to one of the `possible_methods' function call_java_method(object, possible_methods, args) local method_id = nil if #possible_methods == 1 then diff --git a/list.c b/list.c deleted file mode 100644 index a801342..0000000 --- a/list.c +++ /dev/null @@ -1,107 +0,0 @@ -#include "list.h" -#include -#include - -/* list_add: Add an element to the list and return the new list - * head. This allows the initial list pointer to be NULL and a - * new head will be created. - */ -list *list_add(list *l, void *d) -{ - list *orig = l; - list *lnew = LMALLOC(sizeof(list)); - assert(lnew); - memset(lnew, 0, sizeof(list)); - lnew->d = d; - for(; l && l->n; l = l->n); - if(l) - { -#ifdef LIST_HAS_ID - lnew->id = l->id + 1; -#endif - l->n = lnew; - return orig; - } - else - { - l = lnew; - return l; - } -} - -/* list_next: Grab the next element of the list and it's - * data element. If there's no next element, the data pointer - * will not be altered. Returns the next list node. - */ -list *list_next(list *l, void **d) -{ - if(l && l->n) - { - l = l->n; - if(d) - *d = l->d; - return l; - } - else - return NULL; -} - -/* list_delete_elem: Delete an element in the list matching a given data - * pointer. There is no indication whether or not an element is actually - * removed from the list. Returns the new list head. - */ -list *list_delete_elem(list *l, void *d) -{ - list *prev = NULL; - list *orig = l; - for(; l && l->d != d && l->n; prev = l, l = l->n); - if(l && l->d == d) - { - if(orig == l) - orig = l->n; - else if(prev) - prev->n = l->n; - LFREE(l); - } - return orig; -} - -#ifdef LIST_HAS_ID -/* list_get_at: Find the element of the list with the specified id. If a - * pointer d is given, the data will be return if list node is found. Returns - * the list node if found, NULL otherwise. - */ -list *list_get_at(list *l, int id, void **d) -{ - for(; l && l->id != id && l->n; l = l->n); - if(l->id == id) - { - if(d) - *d = l->d; - return l; - } - else - return NULL; -} -#endif /* LIST_HAS_ID */ - -/* - * Find the last element of the given list. - */ -list *list_end(list *l) -{ - for(; l && l->n; l = l->n); - return l; -} - -/* - * Get the data element for the head of the given list. This can be used - * as an iterator: - * for(l = list_start(some_list, &some_data); l; l = list_next(l, &some_list)) - */ -list *list_start(list *l, void **d) -{ - if(l && d) - *d = l->d; - return l; -} diff --git a/list.h b/list.h deleted file mode 100644 index c766341..0000000 --- a/list.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Linked list - * - * Optional cpp macros: - * * LMALLOC - you can #define your own malloc(), such as g_malloc - * * LFREE - same for free() - * * LIST_HAS_ID - #define this to add an id counter on each list element - */ -#ifndef LIST_H_ -#define LIST_H_ - -#include - -typedef struct list { - struct list *n; - void *d; -#ifdef LIST_HAS_ID - int id; /* autoincrement id counter, for convenience */ -#endif -} list; - -#ifndef LMALLOC -#define LMALLOC malloc -#endif -#ifndef LFREE -#define LFREE free -#endif - -list *list_add(list *l, void *d); -list *list_next(list *l, void **d); -list *list_delete_elem(list *l, void *d); -#ifdef LIST_HAS_ID -list *list_get_at(list *l, int id, void **d); -#endif -list *list_end(list *l); -list *list_start(list *l, void **d); - -#endif /* LIST_H_ */ diff --git a/test_basic.lua b/test/test_basic.lua similarity index 100% rename from test_basic.lua rename to test/test_basic.lua diff --git a/test/test_java_bridge.lua b/test/test_java_bridge.lua new file mode 100644 index 0000000..c2af14a --- /dev/null +++ b/test/test_java_bridge.lua @@ -0,0 +1,53 @@ + -- _ ____ _ _ _______ _ + -- | | | _ \ (_) | | |__ __| | | + -- | | __ ___ ____ _| |_) |_ __ _ __| | __ _ ___ | | ___ ___| |_ ___ + -- _ | |/ _` \ \ / / _` | _ <| '__| |/ _` |/ _` |/ _ \ | |/ _ \/ __| __/ __| + -- | |__| | (_| |\ V / (_| | |_) | | | | (_| | (_| | __/ | | __/\__ \ |_\__ \ + -- \____/ \__,_| \_/ \__,_|____/|_| |_|\__,_|\__, |\___| |_|\___||___/\__|___/ + -- __/ | + -- |___/ + +print("*****************************") +print("Running java_bridge tests....") + +function test_method_id_comparison() + io.write("Running test_method_id_comparison().....") + + local m1 = lj_get_method_id("java/lang/String", "toUpperCase", "", "Ljava/lang/String;") + local m2 = lj_get_method_id("java/lang/String", "toUpperCase", "", "Ljava/lang/String;") + assert(m1 and m2) + -- should have different addresses in the string representation + assert(string.format("%s", m1) ~= string.format("%s", m2)) + -- should compare as equal + assert(m1 == m2) + + local m3 = lj_get_method_id("java/lang/String", "toString", "", "Ljava/lang/String;") + local m4 = lj_get_method_id("java/lang/Class", "toString", "", "Ljava/lang/String;") + assert(m3 and m4) + -- different classes means different methods + assert(m3 ~= m4) + assert(m1 ~= m3) + + local m5 = lj_get_method_id("blabbity bla", "asd", "", "V") + assert(m5 == nil) + + print("ok") +end + +-- not comprehensive +function test_basic_field_access() + io.write("Running test_basic_field_access().....") + + local t = lj_get_current_thread() + + -- static field + assert(t.MIN_PRIORITY == 1) + + -- private instance field + assert(lj_toString(t) == lj_toString(t.me)) + + print("ok") +end + +test_method_id_comparison() +test_basic_field_access() \ No newline at end of file diff --git a/test_lua_java.lua b/test/test_lua_java.lua similarity index 100% rename from test_lua_java.lua rename to test/test_lua_java.lua