Skip to content

Commit

Permalink
1、修复Android平台下LuaFunction释放时机出错问题
Browse files Browse the repository at this point in the history
2、增加LuaManagedValue,用于管理Lua对象在原生层的生命周期。


Former-commit-id: 81f41c0f87bddfc576f23b267d2fbbe2247002e7
  • Loading branch information
vimfung committed May 24, 2017
1 parent a0329e2 commit 61b0630
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ public void onClick(View v) {

_luaContext.registerModule(Person.class);

_luaContext.evalScript("local test = function() print('test func') end; test(); Person.retainHandler(test);");
_luaContext.evalScript("print('-------------1'); Person.callHandler(); Person.releaseHandler();");
_luaContext.evalScript("print('-------------2'); Person.callHandler();");
_luaContext.evalScript("local test = function() print('test func') end; test(); Person.retainHandler2(test);");
_luaContext.evalScript("print('-------------1'); Person.callHandler2(); Person.releaseHandler2();");
_luaContext.evalScript("print('-------------2'); Person.callHandler2();");

}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import cn.vimfung.luascriptcore.LuaContext;
import cn.vimfung.luascriptcore.LuaFunction;
import cn.vimfung.luascriptcore.LuaManagedValue;
import cn.vimfung.luascriptcore.LuaTuple;
import cn.vimfung.luascriptcore.LuaValue;
import cn.vimfung.luascriptcore.modules.oo.LuaObjectClass;
Expand Down Expand Up @@ -72,4 +73,24 @@ public static void callHandler()
_func.toFunction().invoke(null);
}
}

private static LuaManagedValue _func2 = null;

public static void retainHandler2(LuaFunction handler)
{
_func2 = new LuaManagedValue(new LuaValue(handler), Env.defaultContext());
}

public static void releaseHandler2(LuaFunction handler)
{
_func2 = null;
}

public static void callHandler2()
{
if (_func2 != null)
{
_func2.getSource().toFunction().invoke(null);
}
}
}
4 changes: 2 additions & 2 deletions Sample/iOS_OSX/Sample-iOS/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.vimfung.luascriptcore;

import android.util.Log;

/**
* 管理值对象,用于将LuaValue的值所指向的Lua对象生命周期维持在LuaManagedValue释放后进行。
* Created by vimfung on 2017/5/24.
*/
public class LuaManagedValue extends LuaBaseObject
{
private LuaValue _source;
private LuaContext _context;

/**
* 初始化对象
* @param value 原始值对象
* @param context 上下文对象
*/
public LuaManagedValue(LuaValue value, LuaContext context)
{
_source = value;
_context = context;

context.retainValue(_source);
}

/**
* 获取源值
* @return 值对象
*/
public LuaValue getSource()
{
return _source;
}

@Override
protected void finalize() throws Throwable
{
_context.releaseValue(_source);
super.finalize();
}
}
1 change: 1 addition & 0 deletions Source/iOS_OSX/Code/LuaScriptCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "LSCFunction.h"
#import "LSCPointer.h"
#import "LSCTuple.h"
#import "LSCManagedValue.h"

//面向对象模块
#import "LSCObjectClass.h"
Expand Down
14 changes: 14 additions & 0 deletions Source/lua-common/LuaFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ LuaFunction::LuaFunction(LuaContext *context, int index)
{
_context = context;
_linkId = StringUtils::format("%p", this);

LuaValue *value = LuaValue::FunctionValue(this);
_context->retainValue(value);
value -> release();
}

LuaFunction::~LuaFunction()
{
if (_context != NULL)
{
LuaValue *value = LuaValue::FunctionValue(this);
_context->releaseValue(value);
value -> release();
}
}

std::string LuaFunction::typeName()
Expand Down
5 changes: 5 additions & 0 deletions Source/lua-common/LuaFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ namespace cn {
* @param decoder 解码器
*/
LuaFunction (LuaObjectDecoder *decoder);

/**
* 释放对象
*/
~LuaFunction();

/**
获取类型名称
Expand Down

0 comments on commit 61b0630

Please sign in to comment.