Skip to content

Commit

Permalink
remove list code
Browse files Browse the repository at this point in the history
add eq handling to java type metatables
add more tests and slight test re-org
  • Loading branch information
jbalint committed Feb 22, 2013
1 parent c9f9849 commit 66377a6
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 179 deletions.
4 changes: 2 additions & 2 deletions Makefile.win32
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 7 additions & 31 deletions debuglib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ end
-- | |__| | \ / | | | | | | _| |_ | |___| (_| | | | |_) | (_| | (__| <\__ \
-- \____/ \/ |_| |_| |_| |_____| \_____\__,_|_|_|_.__/ \__,_|\___|_|\_\___/

-- ============================================================
-- Handle the callback when a breakpoint is hit
-- ============================================================
function cb_breakpoint(thread, method_id, location)
return true
end
Expand Down Expand Up @@ -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
29 changes: 28 additions & 1 deletion java_bridge.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;")
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = {
Expand All @@ -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
Expand Down
107 changes: 0 additions & 107 deletions list.c

This file was deleted.

38 changes: 0 additions & 38 deletions list.h

This file was deleted.

File renamed without changes.
53 changes: 53 additions & 0 deletions test/test_java_bridge.lua
Original file line number Diff line number Diff line change
@@ -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()
File renamed without changes.

0 comments on commit 66377a6

Please sign in to comment.