Skip to content

Commit

Permalink
调整Android版本为2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vimfung committed Apr 19, 2019
1 parent ee67676 commit 675b2ff
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 149 deletions.
4 changes: 2 additions & 2 deletions Sample/Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ dependencies {
implementation 'com.android.support:appcompat-v7:24.1.0'
implementation 'com.blankj:utilcode:1.12.0'
// compile 'org.jooq:joor-java-8:0.9.7'
// compile 'com.github.vimfung.luascriptcore:luascriptcore:2.3.0'
// implementation 'com.github.vimfung.luascriptcore:luascriptcore:2.3.2'

// compile project(':luascriptcore');
implementation project(':luascriptcore');
debugImplementation project(':luascriptcore');
}
Binary file removed Sample/Android/app/src/main/.DS_Store
Binary file not shown.
5 changes: 3 additions & 2 deletions Sample/Android/app/src/main/assets/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local json = require("dkjson");

local tbl = getDeviceInfo();

local str = json.encode (tbl, { indent = true })
local str = json.encode (tbl, { indent = true });

print (str)

print (str)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.vimfung.luascriptcore.sample;

import android.content.Context;
import android.util.Log;

import cn.vimfung.luascriptcore.LuaContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import cn.vimfung.luascriptcore.LuaContext;
import cn.vimfung.luascriptcore.LuaExceptionHandler;
import cn.vimfung.luascriptcore.LuaMethodHandler;
import cn.vimfung.luascriptcore.LuaScriptController;
import cn.vimfung.luascriptcore.LuaTuple;
import cn.vimfung.luascriptcore.LuaValue;

Expand All @@ -30,6 +35,7 @@ public class MainActivity extends AppCompatActivity {

final Object obj = new Object();
private LuaContext _luaContext;
private LuaScriptController _scriptController;
private boolean _hasRegMethod;
private boolean _hasRegModule;
private boolean _hasRegClass;
Expand Down Expand Up @@ -65,76 +71,6 @@ public void onClick(View v) {
LuaValue retValue = _luaContext.evalScript("print(10);return 'Hello World';");
Log.v("lsc", retValue.toString());

//List Native -> Lua
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("hahahahahah");
arrayList.add("Hello World");
LuaValue value = new LuaValue(arrayList);

_luaContext.evalScript("function printArray (arr) print('--------', #arr); for i,v in ipairs(arr) do print(v); end end");

LuaValue[] args = new LuaValue[] {value};
_luaContext.callMethod("printArray", args);

//List Lua -> Native
LuaValue arrValue = _luaContext.evalScript("return {1, 'Hello World'};");
Log.v("lsc", arrValue.toString());
ArrayList arrayList1 = arrValue.toArrayList();
for (Object obj : arrayList1)
{
Log.v("LTN ArrayList", obj.toString());
}
List<?> list = arrValue.toList();
for (Object obj : list)
{
Log.v("LTN List", obj.toString());
}

// Map Native -> Lua
HashMap<String, String> map = new HashMap<>();
map.put("Hello", "World");
map.put("aaa", "bbb");
map.put("ccc", null);
LuaValue mapValue = new LuaValue(map);

_luaContext.evalScript("function printMap (map) print('--------', #map); for k,v in pairs(map) do print(k, v); end end");

LuaValue[] mapArgs = new LuaValue[] {mapValue};
_luaContext.callMethod("printMap", mapArgs);

//Map Lua -> Native
LuaValue mapRetValue = _luaContext.evalScript("return {aaa=1, bbb='Hello World'};");
Log.v("lsc", mapRetValue.toString());
HashMap hashMap = mapRetValue.toHashMap();
for (Object obj : hashMap.keySet())
{
Log.v("LTN HashMap", hashMap.get(obj).toString());
}

Map<?, ?> map2 = mapRetValue.toMap();
for (Object obj : map2.keySet())
{
Log.v("LTN Map", map2.get(obj).toString());
}

ArrayList<HashMap<String, String>> testList = new ArrayList<>();

HashMap<String, String> m1 = new HashMap<>();
m1.put("aaa", "bbb");
m1.put("ccc", "dddd");
testList.add(m1);

HashMap<String, String> m2 = new HashMap<>();
m2.put("eee", "ffff");
m2.put("ggg", "hhh");
testList.add(m2);

LuaValue testValue = new LuaValue(testList);
_luaContext.evalScript("function printArray (arr) print('--------', #arr); for i,v in ipairs(arr) do print('------ elm = ', v); for m,n in pairs(v) do print(m, n); end end end");

// LuaValue[] args = new LuaValue[] {testValue};
// _luaContext.callMethod("printArray", args);

}
});
}
Expand All @@ -147,6 +83,8 @@ public void onClick(View v) {
@Override
public void onClick(View v) {

// _scriptController.forceExit();

if (!_hasRegMethod)
{
//注册方法
Expand All @@ -161,8 +99,6 @@ public LuaValue onExecute(LuaValue[] arguments) {
devInfoMap.put("systemName", Build.PRODUCT);
devInfoMap.put("systemVersion", Build.VERSION.RELEASE);

regMethodBtn.setText("Hello World!");

return new LuaValue(devInfoMap);

}
Expand All @@ -173,7 +109,6 @@ public LuaValue onExecute(LuaValue[] arguments) {

//调用脚本
_luaContext.evalScriptFromFile("main.lua");

}
});
}
Expand Down Expand Up @@ -224,10 +159,18 @@ public void onClick(View v) {
regClsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
_luaContext.evalScript("local p = Person:createPersonError(); print(p);");
// _luaContext.evalScript("local p = Person:createPersonError(); print(p);");
_luaContext.evalScript("print(Chinese); function Chinese.prototype:init() print('Chinese create'); end; local person = Chinese(); print(person); person.name = 'vimfung'; print(person.name); person:speak(); person:walk();");
_luaContext.evalScript("print(Person); local obj = Person:createObj(); Person:CheckObj(obj);");
_luaContext.evalScript("local p = Chinese(\"vimfung\"); print(p.name);");
_luaContext.evalScript("local p = Person(); print(\"int value\", p:getIntArray()[1][1]); print(\"Integer value\", p:getIntegerArray()[1][1]);" +
" print(\"long value\", p:getLongArray()[1][1]); print(\"Long value\", p:getLong2Array()[1][1]); " +
"print(\"float value\", p:getFloatArray()[1][1]); print(\"Float value\", p:getFloat2Array()[1][1]); " +
"print(\"double value\", p:getDoubleArray()[1][1]); print(\"Double value\", p:getDouble2Array()[1][1]);" +
" print(\"boolean value\", p:getBooleanArray()[1][1]); print(\"Boolean value\", p:getBoolean2Array()[1][1]);");
_luaContext.evalScript("local p = Person(); print(p:callFloatMethod(20)); print(p:callFloat2Method(20));");
_luaContext.evalScript("local p = Person(); for i=1, 1000 do p:log(\"Hello World\"); end");

}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ else if (parentItem.equals("Path"))
else if (parentItem.equals("Thread"))
{
modules.add("Run Thread");
modules.add("Stop Thread");
}
else if (parentItem.equals("HTTP"))
{
Expand Down Expand Up @@ -153,6 +154,10 @@ else if (finalParentItem.equals("Thread"))
{
luaContext.evalScript("Thread_Sample_run()");
}
else if (item.equals("Stop Thread"))
{
luaContext.evalScript("Thread_Sample_stop()");
}
}
else if (finalParentItem.equals("HTTP"))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package cn.vimfung.luascriptcore.sample;

import android.support.annotation.NonNull;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import cn.vimfung.luascriptcore.LuaContext;
import cn.vimfung.luascriptcore.LuaExclude;
Expand Down Expand Up @@ -42,6 +47,134 @@ public Person ()

}

public List<int[]> getIntArray()
{
ArrayList<int[]> val = new ArrayList<>();

int[] arr1 = new int[] {1, 2, 3};
val.add(arr1);

int[] arr2 = new int[] {4, 5, 6};
val.add(arr2);

return val;
}

public List<Integer[]> getIntegerArray()
{
ArrayList<Integer[]> val = new ArrayList<>();

Integer item1 = new Integer(7);
Integer item2 = new Integer(8);
Integer item3 = new Integer(9);
Integer[] arr1 = new Integer[] {item1, item2, item3};
val.add(arr1);

return val;
}

public List<long[]> getLongArray()
{
ArrayList<long[]> val = new ArrayList<>();

long[] arr1 = new long[] {10, 11, 12};
val.add(arr1);

return val;
}

public List<Long[]> getLong2Array()
{
ArrayList<Long[]> val = new ArrayList<>();

Long item1 = new Long(13);
Long item2 = new Long(14);
Long item3 = new Long(15);
Long[] arr1 = new Long[] {item1, item2, item3};
val.add(arr1);

return val;
}

public List<float[]> getFloatArray()
{
ArrayList<float[]> val = new ArrayList<>();

float[] arr1 = new float[] {1.1f, 1.2f, 1.3f};
val.add(arr1);

return val;
}

public List<Float[]> getFloat2Array()
{
ArrayList<Float[]> val = new ArrayList<>();

Float item1 = new Float(1.4f);
Float item2 = new Float(1.5f);
Float item3 = new Float(1.6f);
Float[] arr1 = new Float[] {item1, item2, item3};
val.add(arr1);

return val;
}

public List<double[]> getDoubleArray()
{
ArrayList<double[]> val = new ArrayList<>();

double[] arr1 = new double[] {1.7f, 1.8f, 1.9f};
val.add(arr1);

return val;
}

public List<Double[]> getDouble2Array()
{
ArrayList<Double[]> val = new ArrayList<>();

Double item1 = new Double(10.4f);
Double item2 = new Double(10.5f);
Double item3 = new Double(10.6f);
Double[] arr1 = new Double[] {item1, item2, item3};
val.add(arr1);

return val;
}

public List<boolean[]> getBooleanArray()
{
ArrayList<boolean[]> val = new ArrayList<>();

boolean[] arr1 = new boolean[] {true, false, true};
val.add(arr1);

return val;
}

public List<Boolean[]> getBoolean2Array()
{
ArrayList<Boolean[]> val = new ArrayList<>();

Boolean item1 = new Boolean(false);
Boolean item2 = new Boolean(true);
Boolean item3 = new Boolean(false);
Boolean[] arr1 = new Boolean[] {item1, item2, item3};
val.add(arr1);

return val;
}

public float callFloatMethod(float value)
{
return value + 1024;
}

public float callFloat2Method(Float value)
{
return value + 1024;
}

public Person (String name)
{
this.name = name;
Expand Down Expand Up @@ -87,7 +220,7 @@ public static LuaTuple test(String a, String b)
return tuple;
}

public static void log (String msg)
public void log (String msg)
{
Log.v("lsc===", msg);
}
Expand Down Expand Up @@ -127,9 +260,13 @@ public static void releaseHandler2(LuaFunction handler)

public static void callHandler2()
{
if (_func2 != null)
{
if (_func2 != null) {
_func2.getSource().toFunction().invoke(null);
}
}

public byte[] getBuffer()
{
return new byte[512];
}
}
Loading

0 comments on commit 675b2ff

Please sign in to comment.