diff --git a/.gitmodules b/.gitmodules index 680170eed8..ffc74c0643 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "plugin"] path = plugin url = https://github.com/cocos2d-x/plugin-x.git +[submodule "tools/bindings-generator"] + path = tools/bindings-generator + url = git://github.com/cocos2d/bindings-generator.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 272b51ffa9..075489b2be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ if(WIN32 OR APPLE) option(USE_CHIPMUNK "Use chipmunk for physics library" ON) option(USE_BOX2D "Use box2d for physics library" OFF) option(DEBUG_MODE "Debug or release?" ON) +option(BUILD_LIBS_LUA "Build lua libraries" OFF) option(BUILD_GUI "Build GUI library" ON) option(BUILD_NETWORK "Build network library" ON) option(BUILD_STORAGE "Build storage library" ON) @@ -46,12 +47,13 @@ option(BUILD_EDITOR_COCOSTUDIO "Build editor support for cocostudio" ON) option(BUILD_EDITOR_COCOSBUILDER "Build editor support for cocosbuilder" ON) option(BUILD_TestCpp "Only build TestCpp sample" ON) - +option(BUILD_TestLua "Only build TestLua sample" OFF) else()#temp option(USE_CHIPMUNK "Use chipmunk for physics library" ON) option(USE_BOX2D "Use box2d for physics library" OFF) option(DEBUG_MODE "Debug or release?" ON) +option(BUILD_LIBS_LUA "Build lua libraries" ON) option(BUILD_GUI "Build GUI library" ON) option(BUILD_NETWORK "Build network library" ON) option(BUILD_STORAGE "Build storage library" ON) @@ -61,7 +63,7 @@ option(BUILD_EDITOR_COCOSTUDIO "Build editor support for cocostudio" ON) option(BUILD_EDITOR_COCOSBUILDER "Build editor support for cocosbuilder" ON) option(BUILD_TestCpp "Only build TestCpp sample" ON) - +option(BUILD_TestLua "Only build TestLua sample" ON) endif()#temp @@ -273,10 +275,24 @@ if(BUILD_EDITOR_COCOSTUDIO) add_subdirectory(cocos/editor-support/cocostudio) endif(BUILD_EDITOR_COCOSTUDIO) +## Scripting +if(BUILD_LIBS_LUA) +# lua +add_subdirectory(external/lua/lua) + +# tolua +add_subdirectory(external/lua/tolua) + +# luabinding +add_subdirectory(cocos/scripting/lua-bindings) +endif(BUILD_LIBS_LUA) + # build tests if(BUILD_TestCpp) add_subdirectory(samples/cpp-tests) endif(BUILD_TestCpp) - +if(BUILD_TestLua) +add_subdirectory(samples/lua-tests/project) +endif(BUILD_TestLua) diff --git a/build/android-build.py b/build/android-build.py index 6f0e311bc8..3ef3ae6f2a 100755 --- a/build/android-build.py +++ b/build/android-build.py @@ -148,18 +148,18 @@ def copy_resources(target, app_android_root): # copy resources(cpp samples and lua samples) os.mkdir(assets_dir) - resources_dir = os.path.join(app_android_root, "../Resources") + resources_dir = os.path.join(app_android_root, "../../res") if os.path.isdir(resources_dir): copy_files(resources_dir, assets_dir) # lua samples should copy lua script if target in LUA_SAMPLES: - resources_dir = os.path.join(app_android_root, "../../../cocos/scripting/lua/script") + resources_dir = os.path.join(app_android_root, "../../../../cocos/scripting/lua-bindings/script") copy_files(resources_dir, assets_dir) # TestLua shared resources with TestCpp if target == "testlua": - resources_dir = os.path.join(app_android_root, "../../cpp-tests/Resources") + resources_dir = os.path.join(app_android_root, "../../../cpp-tests/Resources") copy_files(resources_dir, assets_dir) def build_samples(target,ndk_build_param,android_platform,build_mode): @@ -190,7 +190,7 @@ def build_samples(target,ndk_build_param,android_platform,build_mode): if target == 'testcpp': app_android_root = os.path.join(cocos_root, 'samples/cpp-tests/proj.android') elif target == 'testlua': - app_android_root = os.path.join(cocos_root, 'samples/lua-tests/proj.android') + app_android_root = os.path.join(cocos_root, 'samples/lua-tests/project/proj.android') else: print 'unknown target: %s' % target continue diff --git a/build/cocos2d-win32.vc2012.sln b/build/cocos2d-win32.vc2012.sln index efd0cc400f..b7fd3b9d54 100644 --- a/build/cocos2d-win32.vc2012.sln +++ b/build/cocos2d-win32.vc2012.sln @@ -25,6 +25,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libSpine", "..\cocos\editor EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libLocalStorage", "..\cocos\storage\local-storage\proj.win32\libLocalStorage.vcxproj", "{632A8F38-D0F0-4D22-86B3-D69F5E6BF63A}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblua", "..\cocos\scripting\lua-bindings\proj.win32\liblua.vcxproj", "{DDC3E27F-004D-4DD4-9DD3-931A013D2159}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestLua", "..\samples\lua-tests\project\proj.win32\TestLua.win32.vcxproj", "{4E6A7A0E-DDD8-4BAA-8B22-C964069364ED}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -79,6 +83,14 @@ Global {632A8F38-D0F0-4D22-86B3-D69F5E6BF63A}.Debug|Win32.Build.0 = Debug|Win32 {632A8F38-D0F0-4D22-86B3-D69F5E6BF63A}.Release|Win32.ActiveCfg = Release|Win32 {632A8F38-D0F0-4D22-86B3-D69F5E6BF63A}.Release|Win32.Build.0 = Release|Win32 + {DDC3E27F-004D-4DD4-9DD3-931A013D2159}.Debug|Win32.ActiveCfg = Debug|Win32 + {DDC3E27F-004D-4DD4-9DD3-931A013D2159}.Debug|Win32.Build.0 = Debug|Win32 + {DDC3E27F-004D-4DD4-9DD3-931A013D2159}.Release|Win32.ActiveCfg = Release|Win32 + {DDC3E27F-004D-4DD4-9DD3-931A013D2159}.Release|Win32.Build.0 = Release|Win32 + {4E6A7A0E-DDD8-4BAA-8B22-C964069364ED}.Debug|Win32.ActiveCfg = Debug|Win32 + {4E6A7A0E-DDD8-4BAA-8B22-C964069364ED}.Debug|Win32.Build.0 = Debug|Win32 + {4E6A7A0E-DDD8-4BAA-8B22-C964069364ED}.Release|Win32.ActiveCfg = Release|Win32 + {4E6A7A0E-DDD8-4BAA-8B22-C964069364ED}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id index 9db1f58951..4936b3446c 100644 --- a/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_samples.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -b3cf4fb2027b85ae59d2cf5a2db7323a0cdfcf95 \ No newline at end of file +828f0bfcdb53dea52131e3bd9fb86ba153449816 \ No newline at end of file diff --git a/build/cocos2d_samples.xcodeproj/xcshareddata/xcschemes/Test lua Mac.xcscheme b/build/cocos2d_samples.xcodeproj/xcshareddata/xcschemes/Test lua Mac.xcscheme new file mode 100644 index 0000000000..70f1231cab --- /dev/null +++ b/build/cocos2d_samples.xcodeproj/xcshareddata/xcschemes/Test lua Mac.xcscheme @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/build/cocos2d_samples.xcodeproj/xcshareddata/xcschemes/Test lua iOS.xcscheme b/build/cocos2d_samples.xcodeproj/xcshareddata/xcschemes/Test lua iOS.xcscheme new file mode 100644 index 0000000000..39fa95f160 --- /dev/null +++ b/build/cocos2d_samples.xcodeproj/xcshareddata/xcschemes/Test lua iOS.xcscheme @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cocos/scripting/lua-bindings/Android.mk b/cocos/scripting/lua-bindings/Android.mk new file mode 100644 index 0000000000..0c148e9ab8 --- /dev/null +++ b/cocos/scripting/lua-bindings/Android.mk @@ -0,0 +1,103 @@ +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE := cocos_lua_static + +LOCAL_MODULE_FILENAME := liblua + +LOCAL_SRC_FILES := manual/CCLuaBridge.cpp \ + manual/CCLuaEngine.cpp \ + manual/CCLuaStack.cpp \ + manual/lua_debugger.c \ + manual/CCLuaValue.cpp \ + manual/Cocos2dxLuaLoader.cpp \ + manual/CCBProxy.cpp \ + manual/Lua_web_socket.cpp \ + manual/LuaOpengl.cpp \ + manual/LuaScriptHandlerMgr.cpp \ + manual/LuaBasicConversions.cpp \ + manual/LuaSkeletonAnimation.cpp \ + manual/lua_cocos2dx_manual.cpp \ + manual/lua_cocos2dx_extension_manual.cpp \ + manual/lua_cocos2dx_coco_studio_manual.cpp \ + manual/lua_cocos2dx_gui_manual.cpp \ + manual/lua_cocos2dx_spine_manual.cpp \ + manual/lua_cocos2dx_physics_manual.cpp \ + manual/lua_cocos2dx_deprecated.cpp \ + manual/lua_xml_http_request.cpp \ + manual/platform/android/CCLuaJavaBridge.cpp \ + manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp \ + manual/tolua_fix.cpp \ + manual/lua_extensions.c \ + auto/lua_cocos2dx_auto.cpp \ + auto/lua_cocos2dx_extension_auto.cpp \ + auto/lua_cocos2dx_studio_auto.cpp \ + auto/lua_cocos2dx_gui_auto.cpp \ + auto/lua_cocos2dx_spine_auto.cpp \ + auto/lua_cocos2dx_physics_auto.cpp \ + ../../../external/lua/tolua/tolua_event.c \ + ../../../external/lua/tolua/tolua_is.c \ + ../../../external/lua/tolua/tolua_map.c \ + ../../../external/lua/tolua/tolua_push.c \ + ../../../external/lua/tolua/tolua_to.c \ + ../../../external/lua/luasocket/auxiliar.c \ + ../../../external/lua/luasocket/luasocket_buffer.c \ + ../../../external/lua/luasocket/except.c \ + ../../../external/lua/luasocket/inet.c \ + ../../../external/lua/luasocket/luasocket_io.c \ + ../../../external/lua/luasocket/luasocket.c \ + ../../../external/lua/luasocket/mime.c \ + ../../../external/lua/luasocket/options.c \ + ../../../external/lua/luasocket/select.c \ + ../../../external/lua/luasocket/serial.c \ + ../../../external/lua/luasocket/socket_scripts.c \ + ../../../external/lua/luasocket/tcp.c \ + ../../../external/lua/luasocket/timeout.c \ + ../../../external/lua/luasocket/udp.c \ + ../../../external/lua/luasocket/unix.c \ + ../../../external/lua/luasocket/usocket.c + + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../external/lua/tolua \ + $(LOCAL_PATH)/../../../external/lua/luajit/include \ + $(LOCAL_PATH)/../../../external/lua \ + $(LOCAL_PATH)/../../../extensions \ + $(LOCAL_PATH)/../../editor-support/spine \ + $(LOCAL_PATH)/../../editor-support/cocosbuilder \ + $(LOCAL_PATH)/../../editor-support/cocostudio \ + $(LOCAL_PATH)/../../gui \ + $(LOCAL_PATH)/auto \ + $(LOCAL_PATH)/manual \ + $(LOCAL_PATH)/manual/platform/android \ + $(LOCAL_PATH)/manual/platform/android/jni + + +LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../../external/lua/tolua \ + $(LOCAL_PATH)/../../../external/lua/luajit/include \ + $(LOCAL_PATH)/../external \ + $(LOCAL_PATH)/auto \ + $(LOCAL_PATH)/manual + + +LOCAL_WHOLE_STATIC_LIBRARIES := luajit_static +LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static +LOCAL_WHOLE_STATIC_LIBRARIES += cocos2dx_static +LOCAL_WHOLE_STATIC_LIBRARIES += websockets_static +LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static +LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static +LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static +LOCAL_WHOLE_STATIC_LIBRARIES += spine_static + +LOCAL_CFLAGS += -Wno-psabi +LOCAL_EXPORT_CFLAGS += -Wno-psabi + +include $(BUILD_STATIC_LIBRARY) + +$(call import-module,lua/luajit/prebuilt/android) +$(call import-module,extensions) +$(call import-module,2d) +$(call import-module,websockets/prebuilt/android) +$(call import-module,network) +$(call import-module,editor-support/cocostudio) +$(call import-module,editor-support/cocosbuilder) +$(call import-module,editor-support/spine) diff --git a/cocos/scripting/lua-bindings/CMakeLists.txt b/cocos/scripting/lua-bindings/CMakeLists.txt new file mode 100644 index 0000000000..d854cdff76 --- /dev/null +++ b/cocos/scripting/lua-bindings/CMakeLists.txt @@ -0,0 +1,56 @@ +set(LUABINDING_SRC + auto/lua_cocos2dx_auto.cpp + auto/lua_cocos2dx_extension_auto.cpp + auto/lua_cocos2dx_studio_auto.cpp + auto/lua_cocos2dx_gui_auto.cpp + auto/lua_cocos2dx_spine_auto.cpp + auto/lua_cocos2dx_physics_auto.cpp + manual/tolua_fix.cpp + manual/CCLuaBridge.cpp + manual/CCLuaEngine.cpp + manual/CCLuaStack.cpp + manual/CCLuaValue.cpp + manual/Cocos2dxLuaLoader.cpp + manual/CCBProxy.cpp + manual/LuaOpengl.cpp + manual/LuaScriptHandlerMgr.cpp + manual/LuaBasicConversions.cpp + manual/lua_cocos2dx_manual.cpp + manual/lua_cocos2dx_extension_manual.cpp + manual/lua_cocos2dx_coco_studio_manual.cpp + manual/lua_cocos2dx_gui_manual.cpp + manual/lua_cocos2dx_spine_manual.cpp + manual/lua_cocos2dx_physics_manual.cpp + manual/lua_cocos2dx_deprecated.cpp + manual/lua_xml_http_request.cpp + manual/LuaSkeletonAnimation.cpp +) + +include_directories( + auto + manual + ../../editor-support/cocosbuilder + ../../editor-support/cocostudio + ../../editor-support/spine + ../../gui + ../../../external/lua/lua + ../../../external/lua/tolua +) + + +add_library(luabinding STATIC + ${LUABINDING_SRC} +) + +target_link_libraries(luabinding + tolua + lua + spine +) + +set_target_properties(luabinding + PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" +) + diff --git a/cocos/scripting/lua-bindings/auto/api/Action.lua b/cocos/scripting/lua-bindings/auto/api/Action.lua new file mode 100644 index 0000000000..24e8e3cc12 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Action.lua @@ -0,0 +1,68 @@ + +-------------------------------- +-- @module Action +-------------------------------- +-- @function [parent=#Action] startWithTarget +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#Action] setOriginalTarget +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#Action] clone +-- @param self +-- @return Action#Action ret (return value: cc.Action) + +-------------------------------- +-- @function [parent=#Action] getOriginalTarget +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#Action] stop +-- @param self + +-------------------------------- +-- @function [parent=#Action] update +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Action] getTarget +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#Action] step +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Action] setTag +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Action] getTag +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Action] setTarget +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#Action] isDone +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Action] reverse +-- @param self +-- @return Action#Action ret (return value: cc.Action) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ActionCamera.lua b/cocos/scripting/lua-bindings/auto/api/ActionCamera.lua new file mode 100644 index 0000000000..beb8f1b0fd --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ActionCamera.lua @@ -0,0 +1,44 @@ + +-------------------------------- +-- @module ActionCamera +-------------------------------- +-- overload function: setEye(float, float, float) +-- +-- overload function: setEye(kmVec3) +-- +-- @function [parent=#ActionCamera] setEye +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#ActionCamera] getEye +-- @param self +-- @return kmVec3#kmVec3 ret (return value: kmVec3) + +-------------------------------- +-- @function [parent=#ActionCamera] setUp +-- @param self +-- @param #kmVec3 kmvec3 + +-------------------------------- +-- @function [parent=#ActionCamera] getCenter +-- @param self +-- @return kmVec3#kmVec3 ret (return value: kmVec3) + +-------------------------------- +-- @function [parent=#ActionCamera] setCenter +-- @param self +-- @param #kmVec3 kmvec3 + +-------------------------------- +-- @function [parent=#ActionCamera] getUp +-- @param self +-- @return kmVec3#kmVec3 ret (return value: kmVec3) + +-------------------------------- +-- @function [parent=#ActionCamera] ActionCamera +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ActionEase.lua b/cocos/scripting/lua-bindings/auto/api/ActionEase.lua new file mode 100644 index 0000000000..b84f3ca32c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ActionEase.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module ActionEase +-------------------------------- +-- @function [parent=#ActionEase] getInnerAction +-- @param self +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ActionInstant.lua b/cocos/scripting/lua-bindings/auto/api/ActionInstant.lua new file mode 100644 index 0000000000..625cfd4e32 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ActionInstant.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module ActionInstant +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ActionInterval.lua b/cocos/scripting/lua-bindings/auto/api/ActionInterval.lua new file mode 100644 index 0000000000..531221b05a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ActionInterval.lua @@ -0,0 +1,19 @@ + +-------------------------------- +-- @module ActionInterval +-------------------------------- +-- @function [parent=#ActionInterval] getAmplitudeRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ActionInterval] setAmplitudeRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ActionInterval] getElapsed +-- @param self +-- @return float#float ret (return value: float) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ActionManager.lua b/cocos/scripting/lua-bindings/auto/api/ActionManager.lua new file mode 100644 index 0000000000..83fad47dc4 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ActionManager.lua @@ -0,0 +1,73 @@ + +-------------------------------- +-- @module ActionManager +-------------------------------- +-- @function [parent=#ActionManager] getActionByTag +-- @param self +-- @param #int int +-- @param #cc.Node node +-- @return Action#Action ret (return value: cc.Action) + +-------------------------------- +-- @function [parent=#ActionManager] removeActionByTag +-- @param self +-- @param #int int +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#ActionManager] removeAllActions +-- @param self + +-------------------------------- +-- @function [parent=#ActionManager] addAction +-- @param self +-- @param #cc.Action action +-- @param #cc.Node node +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ActionManager] resumeTarget +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#ActionManager] update +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ActionManager] getNumberOfRunningActionsInTarget +-- @param self +-- @param #cc.Node node +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#ActionManager] removeAllActionsFromTarget +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#ActionManager] resumeTargets +-- @param self +-- @param #array_table array + +-------------------------------- +-- @function [parent=#ActionManager] removeAction +-- @param self +-- @param #cc.Action action + +-------------------------------- +-- @function [parent=#ActionManager] pauseTarget +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#ActionManager] pauseAllRunningActions +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#ActionManager] ActionManager +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ActionManagerEx.lua b/cocos/scripting/lua-bindings/auto/api/ActionManagerEx.lua new file mode 100644 index 0000000000..f3a32bf505 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ActionManagerEx.lua @@ -0,0 +1,36 @@ + +-------------------------------- +-- @module ActionManagerEx +-------------------------------- +-- overload function: playActionByName(char, char, cc.CallFunc) +-- +-- overload function: playActionByName(char, char) +-- +-- @function [parent=#ActionManagerEx] playActionByName +-- @param self +-- @param #char char +-- @param #char char +-- @param #cc.CallFunc callfunc +-- @return ActionObject#ActionObject ret (retunr value: ccs.ActionObject) + +-------------------------------- +-- @function [parent=#ActionManagerEx] getActionByName +-- @param self +-- @param #char char +-- @param #char char +-- @return ActionObject#ActionObject ret (return value: ccs.ActionObject) + +-------------------------------- +-- @function [parent=#ActionManagerEx] releaseActions +-- @param self + +-------------------------------- +-- @function [parent=#ActionManagerEx] destroyInstance +-- @param self + +-------------------------------- +-- @function [parent=#ActionManagerEx] getInstance +-- @param self +-- @return ActionManagerEx#ActionManagerEx ret (return value: ccs.ActionManagerEx) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ActionObject.lua b/cocos/scripting/lua-bindings/auto/api/ActionObject.lua new file mode 100644 index 0000000000..a38d4dba46 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ActionObject.lua @@ -0,0 +1,95 @@ + +-------------------------------- +-- @module ActionObject +-------------------------------- +-- @function [parent=#ActionObject] setCurrentTime +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ActionObject] pause +-- @param self + +-------------------------------- +-- @function [parent=#ActionObject] setName +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#ActionObject] setUnitTime +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ActionObject] getTotalTime +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ActionObject] getName +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#ActionObject] stop +-- @param self + +-------------------------------- +-- overload function: play(cc.CallFunc) +-- +-- overload function: play() +-- +-- @function [parent=#ActionObject] play +-- @param self +-- @param #cc.CallFunc callfunc + +-------------------------------- +-- @function [parent=#ActionObject] getCurrentTime +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ActionObject] removeActionNode +-- @param self +-- @param #ccs.ActionNode actionnode + +-------------------------------- +-- @function [parent=#ActionObject] getLoop +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ActionObject] addActionNode +-- @param self +-- @param #ccs.ActionNode actionnode + +-------------------------------- +-- @function [parent=#ActionObject] getUnitTime +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ActionObject] isPlaying +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ActionObject] updateToFrameByTime +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ActionObject] setLoop +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ActionObject] simulationActionUpdate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ActionObject] ActionObject +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ActionTween.lua b/cocos/scripting/lua-bindings/auto/api/ActionTween.lua new file mode 100644 index 0000000000..7fa63c49ad --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ActionTween.lua @@ -0,0 +1,22 @@ + +-------------------------------- +-- @module ActionTween +-------------------------------- +-- @function [parent=#ActionTween] initWithDuration +-- @param self +-- @param #float float +-- @param #string str +-- @param #float float +-- @param #float float +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ActionTween] create +-- @param self +-- @param #float float +-- @param #string str +-- @param #float float +-- @param #float float +-- @return ActionTween#ActionTween ret (return value: cc.ActionTween) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Animate.lua b/cocos/scripting/lua-bindings/auto/api/Animate.lua new file mode 100644 index 0000000000..9790543a3d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Animate.lua @@ -0,0 +1,24 @@ + +-------------------------------- +-- @module Animate +-------------------------------- +-- overload function: getAnimation() +-- +-- overload function: getAnimation() +-- +-- @function [parent=#Animate] getAnimation +-- @param self +-- @return Animation#Animation ret (retunr value: cc.Animation) + +-------------------------------- +-- @function [parent=#Animate] setAnimation +-- @param self +-- @param #cc.Animation animation + +-------------------------------- +-- @function [parent=#Animate] create +-- @param self +-- @param #cc.Animation animation +-- @return Animate#Animate ret (return value: cc.Animate) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Animation.lua b/cocos/scripting/lua-bindings/auto/api/Animation.lua new file mode 100644 index 0000000000..6f70ad8895 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Animation.lua @@ -0,0 +1,94 @@ + +-------------------------------- +-- @module Animation +-------------------------------- +-- @function [parent=#Animation] getLoops +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#Animation] addSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe + +-------------------------------- +-- @function [parent=#Animation] setRestoreOriginalFrame +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Animation] clone +-- @param self +-- @return Animation#Animation ret (return value: cc.Animation) + +-------------------------------- +-- @function [parent=#Animation] getDuration +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Animation] setFrames +-- @param self +-- @param #array_table array + +-------------------------------- +-- @function [parent=#Animation] getFrames +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#Animation] setLoops +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#Animation] setDelayPerUnit +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Animation] addSpriteFrameWithFile +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#Animation] getTotalDelayUnits +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Animation] getDelayPerUnit +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Animation] getRestoreOriginalFrame +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Animation] addSpriteFrameWithTexture +-- @param self +-- @param #cc.Texture2D texture2d +-- @param #rect_table rect + +-------------------------------- +-- overload function: create(array_table, float, unsigned int) +-- +-- overload function: create() +-- +-- @function [parent=#Animation] create +-- @param self +-- @param #array_table array +-- @param #float float +-- @param #unsigned int int +-- @return Animation#Animation ret (retunr value: cc.Animation) + +-------------------------------- +-- @function [parent=#Animation] createWithSpriteFrames +-- @param self +-- @param #array_table array +-- @param #float float +-- @return Animation#Animation ret (return value: cc.Animation) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/AnimationCache.lua b/cocos/scripting/lua-bindings/auto/api/AnimationCache.lua new file mode 100644 index 0000000000..5cc624e91c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/AnimationCache.lua @@ -0,0 +1,50 @@ + +-------------------------------- +-- @module AnimationCache +-------------------------------- +-- @function [parent=#AnimationCache] getAnimation +-- @param self +-- @param #string str +-- @return Animation#Animation ret (return value: cc.Animation) + +-------------------------------- +-- @function [parent=#AnimationCache] addAnimation +-- @param self +-- @param #cc.Animation animation +-- @param #string str + +-------------------------------- +-- @function [parent=#AnimationCache] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#AnimationCache] addAnimationsWithDictionary +-- @param self +-- @param #map_table map +-- @param #string str + +-------------------------------- +-- @function [parent=#AnimationCache] removeAnimation +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#AnimationCache] addAnimationsWithFile +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#AnimationCache] destroyInstance +-- @param self + +-------------------------------- +-- @function [parent=#AnimationCache] getInstance +-- @param self +-- @return AnimationCache#AnimationCache ret (return value: cc.AnimationCache) + +-------------------------------- +-- @function [parent=#AnimationCache] AnimationCache +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/AnimationData.lua b/cocos/scripting/lua-bindings/auto/api/AnimationData.lua new file mode 100644 index 0000000000..9810307206 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/AnimationData.lua @@ -0,0 +1,29 @@ + +-------------------------------- +-- @module AnimationData +-------------------------------- +-- @function [parent=#AnimationData] getMovement +-- @param self +-- @param #string str +-- @return MovementData#MovementData ret (return value: ccs.MovementData) + +-------------------------------- +-- @function [parent=#AnimationData] getMovementCount +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#AnimationData] addMovement +-- @param self +-- @param #ccs.MovementData movementdata + +-------------------------------- +-- @function [parent=#AnimationData] create +-- @param self +-- @return AnimationData#AnimationData ret (return value: ccs.AnimationData) + +-------------------------------- +-- @function [parent=#AnimationData] AnimationData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/AnimationFrame.lua b/cocos/scripting/lua-bindings/auto/api/AnimationFrame.lua new file mode 100644 index 0000000000..be020885e6 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/AnimationFrame.lua @@ -0,0 +1,51 @@ + +-------------------------------- +-- @module AnimationFrame +-------------------------------- +-- @function [parent=#AnimationFrame] setSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe + +-------------------------------- +-- overload function: getUserInfo() +-- +-- overload function: getUserInfo() +-- +-- @function [parent=#AnimationFrame] getUserInfo +-- @param self +-- @return map_table#map_table ret (retunr value: map_table) + +-------------------------------- +-- @function [parent=#AnimationFrame] setDelayUnits +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#AnimationFrame] clone +-- @param self +-- @return AnimationFrame#AnimationFrame ret (return value: cc.AnimationFrame) + +-------------------------------- +-- @function [parent=#AnimationFrame] getSpriteFrame +-- @param self +-- @return SpriteFrame#SpriteFrame ret (return value: cc.SpriteFrame) + +-------------------------------- +-- @function [parent=#AnimationFrame] getDelayUnits +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#AnimationFrame] setUserInfo +-- @param self +-- @param #map_table map + +-------------------------------- +-- @function [parent=#AnimationFrame] create +-- @param self +-- @param #cc.SpriteFrame spriteframe +-- @param #float float +-- @param #map_table map +-- @return AnimationFrame#AnimationFrame ret (return value: cc.AnimationFrame) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Application.lua b/cocos/scripting/lua-bindings/auto/api/Application.lua new file mode 100644 index 0000000000..c571332321 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Application.lua @@ -0,0 +1,24 @@ + +-------------------------------- +-- @module Application +-------------------------------- +-- @function [parent=#Application] getTargetPlatform +-- @param self +-- @return ApplicationProtocol::Platform#ApplicationProtocol::Platform ret (return value: cc.ApplicationProtocol::Platform) + +-------------------------------- +-- @function [parent=#Application] setAnimationInterval +-- @param self +-- @param #double double + +-------------------------------- +-- @function [parent=#Application] getCurrentLanguage +-- @param self +-- @return LanguageType#LanguageType ret (return value: cc.LanguageType) + +-------------------------------- +-- @function [parent=#Application] getInstance +-- @param self +-- @return Application#Application ret (return value: cc.Application) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Armature.lua b/cocos/scripting/lua-bindings/auto/api/Armature.lua new file mode 100644 index 0000000000..a01abe6930 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Armature.lua @@ -0,0 +1,143 @@ + +-------------------------------- +-- @module Armature +-------------------------------- +-- @function [parent=#Armature] getBone +-- @param self +-- @param #string str +-- @return Bone#Bone ret (return value: ccs.Bone) + +-------------------------------- +-- @function [parent=#Armature] changeBoneParent +-- @param self +-- @param #ccs.Bone bone +-- @param #string str + +-------------------------------- +-- @function [parent=#Armature] setAnimation +-- @param self +-- @param #ccs.ArmatureAnimation armatureanimation + +-------------------------------- +-- @function [parent=#Armature] getBoneAtPoint +-- @param self +-- @param #float float +-- @param #float float +-- @return Bone#Bone ret (return value: ccs.Bone) + +-------------------------------- +-- @function [parent=#Armature] getArmatureTransformDirty +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Armature] setVersion +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Armature] updateOffsetPoint +-- @param self + +-------------------------------- +-- @function [parent=#Armature] getParentBone +-- @param self +-- @return Bone#Bone ret (return value: ccs.Bone) + +-------------------------------- +-- @function [parent=#Armature] setArmatureData +-- @param self +-- @param #ccs.ArmatureData armaturedata + +-------------------------------- +-- @function [parent=#Armature] removeBone +-- @param self +-- @param #ccs.Bone bone +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Armature] getBatchNode +-- @param self +-- @return BatchNode#BatchNode ret (return value: ccs.BatchNode) + +-------------------------------- +-- @function [parent=#Armature] getName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- overload function: init(string) +-- +-- overload function: init() +-- +-- overload function: init(string, ccs.Bone) +-- +-- @function [parent=#Armature] init +-- @param self +-- @param #string str +-- @param #ccs.Bone bone +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#Armature] setParentBone +-- @param self +-- @param #ccs.Bone bone + +-------------------------------- +-- @function [parent=#Armature] drawContour +-- @param self + +-------------------------------- +-- @function [parent=#Armature] setBatchNode +-- @param self +-- @param #ccs.BatchNode batchnode + +-------------------------------- +-- @function [parent=#Armature] setName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#Armature] addBone +-- @param self +-- @param #ccs.Bone bone +-- @param #string str + +-------------------------------- +-- @function [parent=#Armature] getArmatureData +-- @param self +-- @return ArmatureData#ArmatureData ret (return value: ccs.ArmatureData) + +-------------------------------- +-- @function [parent=#Armature] getVersion +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Armature] getAnimation +-- @param self +-- @return ArmatureAnimation#ArmatureAnimation ret (return value: ccs.ArmatureAnimation) + +-------------------------------- +-- @function [parent=#Armature] getBoneDic +-- @param self +-- @return map_table#map_table ret (return value: map_table) + +-------------------------------- +-- overload function: create(string) +-- +-- overload function: create() +-- +-- overload function: create(string, ccs.Bone) +-- +-- @function [parent=#Armature] create +-- @param self +-- @param #string str +-- @param #ccs.Bone bone +-- @return Armature#Armature ret (retunr value: ccs.Armature) + +-------------------------------- +-- @function [parent=#Armature] Armature +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ArmatureAnimation.lua b/cocos/scripting/lua-bindings/auto/api/ArmatureAnimation.lua new file mode 100644 index 0000000000..54f138acbc --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ArmatureAnimation.lua @@ -0,0 +1,105 @@ + +-------------------------------- +-- @module ArmatureAnimation +-------------------------------- +-- @function [parent=#ArmatureAnimation] getSpeedScale +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ArmatureAnimation] pause +-- @param self + +-------------------------------- +-- @function [parent=#ArmatureAnimation] setSpeedScale +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ArmatureAnimation] init +-- @param self +-- @param #ccs.Armature armature +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ArmatureAnimation] playWithIndexes +-- @param self +-- @param #array_table array +-- @param #int int +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ArmatureAnimation] play +-- @param self +-- @param #string str +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#ArmatureAnimation] gotoAndPause +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#ArmatureAnimation] resume +-- @param self + +-------------------------------- +-- @function [parent=#ArmatureAnimation] stop +-- @param self + +-------------------------------- +-- @function [parent=#ArmatureAnimation] update +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ArmatureAnimation] getAnimationData +-- @param self +-- @return AnimationData#AnimationData ret (return value: ccs.AnimationData) + +-------------------------------- +-- @function [parent=#ArmatureAnimation] playWithIndex +-- @param self +-- @param #int int +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#ArmatureAnimation] getCurrentMovementID +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#ArmatureAnimation] setAnimationData +-- @param self +-- @param #ccs.AnimationData animationdata + +-------------------------------- +-- @function [parent=#ArmatureAnimation] gotoAndPlay +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#ArmatureAnimation] playWithNames +-- @param self +-- @param #array_table array +-- @param #int int +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ArmatureAnimation] getMovementCount +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#ArmatureAnimation] create +-- @param self +-- @param #ccs.Armature armature +-- @return ArmatureAnimation#ArmatureAnimation ret (return value: ccs.ArmatureAnimation) + +-------------------------------- +-- @function [parent=#ArmatureAnimation] ArmatureAnimation +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ArmatureData.lua b/cocos/scripting/lua-bindings/auto/api/ArmatureData.lua new file mode 100644 index 0000000000..c09e50e2ad --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ArmatureData.lua @@ -0,0 +1,29 @@ + +-------------------------------- +-- @module ArmatureData +-------------------------------- +-- @function [parent=#ArmatureData] addBoneData +-- @param self +-- @param #ccs.BoneData bonedata + +-------------------------------- +-- @function [parent=#ArmatureData] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ArmatureData] getBoneData +-- @param self +-- @param #string str +-- @return BoneData#BoneData ret (return value: ccs.BoneData) + +-------------------------------- +-- @function [parent=#ArmatureData] create +-- @param self +-- @return ArmatureData#ArmatureData ret (return value: ccs.ArmatureData) + +-------------------------------- +-- @function [parent=#ArmatureData] ArmatureData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ArmatureDataManager.lua b/cocos/scripting/lua-bindings/auto/api/ArmatureDataManager.lua new file mode 100644 index 0000000000..bc65fafe65 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ArmatureDataManager.lua @@ -0,0 +1,115 @@ + +-------------------------------- +-- @module ArmatureDataManager +-------------------------------- +-- @function [parent=#ArmatureDataManager] getAnimationDatas +-- @param self +-- @return map_table#map_table ret (return value: map_table) + +-------------------------------- +-- @function [parent=#ArmatureDataManager] removeAnimationData +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#ArmatureDataManager] addArmatureData +-- @param self +-- @param #string str +-- @param #ccs.ArmatureData armaturedata +-- @param #string str + +-------------------------------- +-- overload function: addArmatureFileInfo(string, string, string) +-- +-- overload function: addArmatureFileInfo(string) +-- +-- @function [parent=#ArmatureDataManager] addArmatureFileInfo +-- @param self +-- @param #string str +-- @param #string str +-- @param #string str + +-------------------------------- +-- @function [parent=#ArmatureDataManager] removeArmatureFileInfo +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#ArmatureDataManager] getTextureDatas +-- @param self +-- @return map_table#map_table ret (return value: map_table) + +-------------------------------- +-- @function [parent=#ArmatureDataManager] getTextureData +-- @param self +-- @param #string str +-- @return TextureData#TextureData ret (return value: ccs.TextureData) + +-------------------------------- +-- @function [parent=#ArmatureDataManager] getArmatureData +-- @param self +-- @param #string str +-- @return ArmatureData#ArmatureData ret (return value: ccs.ArmatureData) + +-------------------------------- +-- @function [parent=#ArmatureDataManager] getAnimationData +-- @param self +-- @param #string str +-- @return AnimationData#AnimationData ret (return value: ccs.AnimationData) + +-------------------------------- +-- @function [parent=#ArmatureDataManager] addAnimationData +-- @param self +-- @param #string str +-- @param #ccs.AnimationData animationdata +-- @param #string str + +-------------------------------- +-- @function [parent=#ArmatureDataManager] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ArmatureDataManager] removeArmatureData +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#ArmatureDataManager] getArmatureDatas +-- @param self +-- @return map_table#map_table ret (return value: map_table) + +-------------------------------- +-- @function [parent=#ArmatureDataManager] removeTextureData +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#ArmatureDataManager] addTextureData +-- @param self +-- @param #string str +-- @param #ccs.TextureData texturedata +-- @param #string str + +-------------------------------- +-- @function [parent=#ArmatureDataManager] isAutoLoadSpriteFile +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ArmatureDataManager] addSpriteFrameFromFile +-- @param self +-- @param #string str +-- @param #string str +-- @param #string str + +-------------------------------- +-- @function [parent=#ArmatureDataManager] destroyInstance +-- @param self + +-------------------------------- +-- @function [parent=#ArmatureDataManager] getInstance +-- @param self +-- @return ArmatureDataManager#ArmatureDataManager ret (return value: ccs.ArmatureDataManager) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ArmatureDisplayData.lua b/cocos/scripting/lua-bindings/auto/api/ArmatureDisplayData.lua new file mode 100644 index 0000000000..5aea40a605 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ArmatureDisplayData.lua @@ -0,0 +1,13 @@ + +-------------------------------- +-- @module ArmatureDisplayData +-------------------------------- +-- @function [parent=#ArmatureDisplayData] create +-- @param self +-- @return ArmatureDisplayData#ArmatureDisplayData ret (return value: ccs.ArmatureDisplayData) + +-------------------------------- +-- @function [parent=#ArmatureDisplayData] ArmatureDisplayData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/AssetsManager.lua b/cocos/scripting/lua-bindings/auto/api/AssetsManager.lua new file mode 100644 index 0000000000..81aa919049 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/AssetsManager.lua @@ -0,0 +1,77 @@ + +-------------------------------- +-- @module AssetsManager +-------------------------------- +-- @function [parent=#AssetsManager] setStoragePath +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#AssetsManager] setPackageUrl +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#AssetsManager] checkUpdate +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#AssetsManager] getStoragePath +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#AssetsManager] update +-- @param self + +-------------------------------- +-- @function [parent=#AssetsManager] setConnectionTimeout +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#AssetsManager] setVersionFileUrl +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#AssetsManager] getPackageUrl +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#AssetsManager] getConnectionTimeout +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#AssetsManager] getVersion +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#AssetsManager] getVersionFileUrl +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#AssetsManager] deleteVersion +-- @param self + +-------------------------------- +-- @function [parent=#AssetsManager] create +-- @param self +-- @param #char char +-- @param #char char +-- @param #char char +-- @param #function func +-- @param #function func +-- @param #function func +-- @return AssetsManager#AssetsManager ret (return value: cc.AssetsManager) + +-------------------------------- +-- @function [parent=#AssetsManager] AssetsManager +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/AtlasNode.lua b/cocos/scripting/lua-bindings/auto/api/AtlasNode.lua new file mode 100644 index 0000000000..f11f35f550 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/AtlasNode.lua @@ -0,0 +1,47 @@ + +-------------------------------- +-- @module AtlasNode +-------------------------------- +-- @function [parent=#AtlasNode] updateAtlasValues +-- @param self + +-------------------------------- +-- @function [parent=#AtlasNode] getTexture +-- @param self +-- @return Texture2D#Texture2D ret (return value: cc.Texture2D) + +-------------------------------- +-- @function [parent=#AtlasNode] setTextureAtlas +-- @param self +-- @param #cc.TextureAtlas textureatlas + +-------------------------------- +-- @function [parent=#AtlasNode] getTextureAtlas +-- @param self +-- @return TextureAtlas#TextureAtlas ret (return value: cc.TextureAtlas) + +-------------------------------- +-- @function [parent=#AtlasNode] getQuadsToDraw +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#AtlasNode] setTexture +-- @param self +-- @param #cc.Texture2D texture2d + +-------------------------------- +-- @function [parent=#AtlasNode] setQuadsToDraw +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#AtlasNode] create +-- @param self +-- @param #string str +-- @param #int int +-- @param #int int +-- @param #int int +-- @return AtlasNode#AtlasNode ret (return value: cc.AtlasNode) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/BaseData.lua b/cocos/scripting/lua-bindings/auto/api/BaseData.lua new file mode 100644 index 0000000000..63e7660ae1 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/BaseData.lua @@ -0,0 +1,23 @@ + +-------------------------------- +-- @module BaseData +-------------------------------- +-- @function [parent=#BaseData] getColor +-- @param self +-- @return color4B_table#color4B_table ret (return value: color4B_table) + +-------------------------------- +-- @function [parent=#BaseData] setColor +-- @param self +-- @param #color4B_table color4b + +-------------------------------- +-- @function [parent=#BaseData] create +-- @param self +-- @return BaseData#BaseData ret (return value: ccs.BaseData) + +-------------------------------- +-- @function [parent=#BaseData] BaseData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/BatchNode.lua b/cocos/scripting/lua-bindings/auto/api/BatchNode.lua new file mode 100644 index 0000000000..6d60cb747c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/BatchNode.lua @@ -0,0 +1,14 @@ + +-------------------------------- +-- @module BatchNode +-------------------------------- +-- @function [parent=#BatchNode] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#BatchNode] create +-- @param self +-- @return BatchNode#BatchNode ret (return value: ccs.BatchNode) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/BezierBy.lua b/cocos/scripting/lua-bindings/auto/api/BezierBy.lua new file mode 100644 index 0000000000..f7ca4174ea --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/BezierBy.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module BezierBy +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/BezierTo.lua b/cocos/scripting/lua-bindings/auto/api/BezierTo.lua new file mode 100644 index 0000000000..4aff14c786 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/BezierTo.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module BezierTo +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Blink.lua b/cocos/scripting/lua-bindings/auto/api/Blink.lua new file mode 100644 index 0000000000..7f2573abff --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Blink.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module Blink +-------------------------------- +-- @function [parent=#Blink] create +-- @param self +-- @param #float float +-- @param #int int +-- @return Blink#Blink ret (return value: cc.Blink) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Bone.lua b/cocos/scripting/lua-bindings/auto/api/Bone.lua new file mode 100644 index 0000000000..8e8eb8a9f0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Bone.lua @@ -0,0 +1,194 @@ + +-------------------------------- +-- @module Bone +-------------------------------- +-- @function [parent=#Bone] isTransformDirty +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Bone] isIgnoreMovementBoneData +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Bone] updateZOrder +-- @param self + +-------------------------------- +-- @function [parent=#Bone] getDisplayRenderNode +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#Bone] isBlendDirty +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Bone] addChildBone +-- @param self +-- @param #ccs.Bone bone + +-------------------------------- +-- @function [parent=#Bone] getWorldInfo +-- @param self +-- @return BaseData#BaseData ret (return value: ccs.BaseData) + +-------------------------------- +-- @function [parent=#Bone] getTween +-- @param self +-- @return Tween#Tween ret (return value: ccs.Tween) + +-------------------------------- +-- @function [parent=#Bone] getParentBone +-- @param self +-- @return Bone#Bone ret (return value: ccs.Bone) + +-------------------------------- +-- @function [parent=#Bone] updateColor +-- @param self + +-------------------------------- +-- @function [parent=#Bone] getName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#Bone] setTransformDirty +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Bone] getDisplayRenderNodeType +-- @param self +-- @return DisplayType#DisplayType ret (return value: ccs.DisplayType) + +-------------------------------- +-- @function [parent=#Bone] removeDisplay +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Bone] setBoneData +-- @param self +-- @param #ccs.BoneData bonedata + +-------------------------------- +-- overload function: init(string) +-- +-- overload function: init() +-- +-- @function [parent=#Bone] init +-- @param self +-- @param #string str +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#Bone] setParentBone +-- @param self +-- @param #ccs.Bone bone + +-------------------------------- +-- overload function: addDisplay(cc.Node, int) +-- +-- overload function: addDisplay(ccs.DisplayData, int) +-- +-- @function [parent=#Bone] addDisplay +-- @param self +-- @param #ccs.DisplayData displaydata +-- @param #int int + +-------------------------------- +-- @function [parent=#Bone] setName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#Bone] removeFromParent +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Bone] getColliderDetector +-- @param self +-- @return ColliderDetector#ColliderDetector ret (return value: ccs.ColliderDetector) + +-------------------------------- +-- @function [parent=#Bone] getChildArmature +-- @param self +-- @return Armature#Armature ret (return value: ccs.Armature) + +-------------------------------- +-- @function [parent=#Bone] getTweenData +-- @param self +-- @return FrameData#FrameData ret (return value: ccs.FrameData) + +-------------------------------- +-- @function [parent=#Bone] changeDisplayWithIndex +-- @param self +-- @param #int int +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Bone] changeDisplayWithName +-- @param self +-- @param #string str +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Bone] setArmature +-- @param self +-- @param #ccs.Armature armature + +-------------------------------- +-- @function [parent=#Bone] setBlendDirty +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Bone] removeChildBone +-- @param self +-- @param #ccs.Bone bone +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Bone] setChildArmature +-- @param self +-- @param #ccs.Armature armature + +-------------------------------- +-- @function [parent=#Bone] getNodeToArmatureTransform +-- @param self +-- @return kmMat4#kmMat4 ret (return value: kmMat4) + +-------------------------------- +-- @function [parent=#Bone] getDisplayManager +-- @param self +-- @return DisplayManager#DisplayManager ret (return value: ccs.DisplayManager) + +-------------------------------- +-- @function [parent=#Bone] getArmature +-- @param self +-- @return Armature#Armature ret (return value: ccs.Armature) + +-------------------------------- +-- @function [parent=#Bone] getBoneData +-- @param self +-- @return BoneData#BoneData ret (return value: ccs.BoneData) + +-------------------------------- +-- overload function: create(string) +-- +-- overload function: create() +-- +-- @function [parent=#Bone] create +-- @param self +-- @param #string str +-- @return Bone#Bone ret (retunr value: ccs.Bone) + +-------------------------------- +-- @function [parent=#Bone] Bone +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/BoneData.lua b/cocos/scripting/lua-bindings/auto/api/BoneData.lua new file mode 100644 index 0000000000..70835dcb3c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/BoneData.lua @@ -0,0 +1,29 @@ + +-------------------------------- +-- @module BoneData +-------------------------------- +-- @function [parent=#BoneData] getDisplayData +-- @param self +-- @param #int int +-- @return DisplayData#DisplayData ret (return value: ccs.DisplayData) + +-------------------------------- +-- @function [parent=#BoneData] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#BoneData] addDisplayData +-- @param self +-- @param #ccs.DisplayData displaydata + +-------------------------------- +-- @function [parent=#BoneData] create +-- @param self +-- @return BoneData#BoneData ret (return value: ccs.BoneData) + +-------------------------------- +-- @function [parent=#BoneData] BoneData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Button.lua b/cocos/scripting/lua-bindings/auto/api/Button.lua new file mode 100644 index 0000000000..45e77d33c5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Button.lua @@ -0,0 +1,134 @@ + +-------------------------------- +-- @module Button +-------------------------------- +-- @function [parent=#Button] getTitleText +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#Button] setTitleFontSize +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Button] setScale9Enabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Button] getCapInsetsDisabledRenderer +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#Button] setTitleColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#Button] setCapInsetsDisabledRenderer +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Button] setCapInsets +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Button] loadTextureDisabled +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Button] setTitleText +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#Button] setCapInsetsNormalRenderer +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Button] loadTexturePressed +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Button] setTitleFontName +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#Button] getCapInsetsNormalRenderer +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#Button] getCapInsetsPressedRenderer +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#Button] loadTextures +-- @param self +-- @param #char char +-- @param #char char +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Button] isScale9Enabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Button] loadTextureNormal +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Button] setCapInsetsPressedRenderer +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Button] getTitleFontSize +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Button] getTitleFontName +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#Button] getTitleColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#Button] setPressedActionEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Button] create +-- @param self +-- @return Button#Button ret (return value: ccui.Button) + +-------------------------------- +-- @function [parent=#Button] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#Button] Button +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/CCBAnimationManager.lua b/cocos/scripting/lua-bindings/auto/api/CCBAnimationManager.lua new file mode 100644 index 0000000000..9cb4eda7fd --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/CCBAnimationManager.lua @@ -0,0 +1,199 @@ + +-------------------------------- +-- @module CCBAnimationManager +-------------------------------- +-- @function [parent=#CCBAnimationManager] moveAnimationsFromNode +-- @param self +-- @param #cc.Node node +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#CCBAnimationManager] setAutoPlaySequenceId +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getDocumentCallbackNames +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] actionForSoundChannel +-- @param self +-- @param #cc.CCBSequenceProperty ccbsequenceproperty +-- @return Sequence#Sequence ret (return value: cc.Sequence) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] setBaseValue +-- @param self +-- @param #cc.Value value +-- @param #cc.Node node +-- @param #string str + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getDocumentOutletNodes +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getLastCompletedSequenceName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] setRootNode +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#CCBAnimationManager] runAnimationsForSequenceNamedTweenDuration +-- @param self +-- @param #char char +-- @param #float float + +-------------------------------- +-- @function [parent=#CCBAnimationManager] addDocumentOutletName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getSequences +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getRootContainerSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] setDocumentControllerName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#CCBAnimationManager] setObject +-- @param self +-- @param #cc.Ref ref +-- @param #cc.Node node +-- @param #string str + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getContainerSize +-- @param self +-- @param #cc.Node node +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] actionForCallbackChannel +-- @param self +-- @param #cc.CCBSequenceProperty ccbsequenceproperty +-- @return Sequence#Sequence ret (return value: cc.Sequence) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getDocumentOutletNames +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] addDocumentCallbackControlEvents +-- @param self +-- @param #cc.Control::EventType eventtype + +-------------------------------- +-- @function [parent=#CCBAnimationManager] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getKeyframeCallbacks +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getDocumentCallbackControlEvents +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] setRootContainerSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#CCBAnimationManager] runAnimationsForSequenceIdTweenDuration +-- @param self +-- @param #int int +-- @param #float float + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getRunningSequenceName +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getAutoPlaySequenceId +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] addDocumentCallbackName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getRootNode +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] addDocumentOutletNode +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getSequenceDuration +-- @param self +-- @param #char char +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] addDocumentCallbackNode +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#CCBAnimationManager] runAnimationsForSequenceNamed +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getSequenceId +-- @param self +-- @param #char char +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getDocumentCallbackNodes +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] setSequences +-- @param self +-- @param #array_table array + +-------------------------------- +-- @function [parent=#CCBAnimationManager] debug +-- @param self + +-------------------------------- +-- @function [parent=#CCBAnimationManager] getDocumentControllerName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#CCBAnimationManager] CCBAnimationManager +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/CCBReader.lua b/cocos/scripting/lua-bindings/auto/api/CCBReader.lua new file mode 100644 index 0000000000..118fd252c7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/CCBReader.lua @@ -0,0 +1,115 @@ + +-------------------------------- +-- @module CCBReader +-------------------------------- +-- @function [parent=#CCBReader] addOwnerOutletName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#CCBReader] getOwnerCallbackNames +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBReader] addDocumentCallbackControlEvents +-- @param self +-- @param #cc.Control::EventType eventtype + +-------------------------------- +-- @function [parent=#CCBReader] setCCBRootPath +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#CCBReader] addOwnerOutletNode +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#CCBReader] getOwnerCallbackNodes +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBReader] readSoundKeyframesForSeq +-- @param self +-- @param #cc.CCBSequence ccbsequence +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#CCBReader] getCCBRootPath +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#CCBReader] getOwnerCallbackControlEvents +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBReader] getOwnerOutletNodes +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBReader] readUTF8 +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#CCBReader] addOwnerCallbackControlEvents +-- @param self +-- @param #cc.Control::EventType eventtype + +-------------------------------- +-- @function [parent=#CCBReader] getOwnerOutletNames +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBReader] setAnimationManager +-- @param self +-- @param #cc.CCBAnimationManager ccbanimationmanager + +-------------------------------- +-- @function [parent=#CCBReader] readCallbackKeyframesForSeq +-- @param self +-- @param #cc.CCBSequence ccbsequence +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#CCBReader] getAnimationManagersForNodes +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBReader] getNodesWithAnimationManagers +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#CCBReader] getAnimationManager +-- @param self +-- @return CCBAnimationManager#CCBAnimationManager ret (return value: cc.CCBAnimationManager) + +-------------------------------- +-- @function [parent=#CCBReader] setResolutionScale +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: CCBReader(cc.CCBReader) +-- +-- overload function: CCBReader(cc.NodeLoaderLibrary, cc.CCBMemberVariableAssigner, cc.CCBSelectorResolver, cc.NodeLoaderListener) +-- +-- overload function: CCBReader() +-- +-- @function [parent=#CCBReader] CCBReader +-- @param self +-- @param #cc.NodeLoaderLibrary nodeloaderlibrary +-- @param #cc.CCBMemberVariableAssigner ccbmembervariableassigner +-- @param #cc.CCBSelectorResolver ccbselectorresolver +-- @param #cc.NodeLoaderListener nodeloaderlistener + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/CallFunc.lua b/cocos/scripting/lua-bindings/auto/api/CallFunc.lua new file mode 100644 index 0000000000..b019450023 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/CallFunc.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module CallFunc +-------------------------------- +-- @function [parent=#CallFunc] execute +-- @param self + +-------------------------------- +-- @function [parent=#CallFunc] getTargetCallback +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#CallFunc] setTargetCallback +-- @param self +-- @param #cc.Ref ref + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/CardinalSplineBy.lua b/cocos/scripting/lua-bindings/auto/api/CardinalSplineBy.lua new file mode 100644 index 0000000000..26f659268f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/CardinalSplineBy.lua @@ -0,0 +1,8 @@ + +-------------------------------- +-- @module CardinalSplineBy +-------------------------------- +-- @function [parent=#CardinalSplineBy] CardinalSplineBy +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/CardinalSplineTo.lua b/cocos/scripting/lua-bindings/auto/api/CardinalSplineTo.lua new file mode 100644 index 0000000000..755dc507ea --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/CardinalSplineTo.lua @@ -0,0 +1,26 @@ + +-------------------------------- +-- @module CardinalSplineTo +-------------------------------- +-- @function [parent=#CardinalSplineTo] getPoints +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#CardinalSplineTo] updatePosition +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#CardinalSplineTo] initWithDuration +-- @param self +-- @param #float float +-- @param #point_table pointarray +-- @param #float float +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#CardinalSplineTo] CardinalSplineTo +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/CatmullRomBy.lua b/cocos/scripting/lua-bindings/auto/api/CatmullRomBy.lua new file mode 100644 index 0000000000..b7ac597190 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/CatmullRomBy.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module CatmullRomBy +-------------------------------- +-- @function [parent=#CatmullRomBy] initWithDuration +-- @param self +-- @param #float float +-- @param #point_table pointarray +-- @return bool#bool ret (return value: bool) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/CatmullRomTo.lua b/cocos/scripting/lua-bindings/auto/api/CatmullRomTo.lua new file mode 100644 index 0000000000..c4875accea --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/CatmullRomTo.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module CatmullRomTo +-------------------------------- +-- @function [parent=#CatmullRomTo] initWithDuration +-- @param self +-- @param #float float +-- @param #point_table pointarray +-- @return bool#bool ret (return value: bool) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/CheckBox.lua b/cocos/scripting/lua-bindings/auto/api/CheckBox.lua new file mode 100644 index 0000000000..d8cfb06f75 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/CheckBox.lua @@ -0,0 +1,68 @@ + +-------------------------------- +-- @module CheckBox +-------------------------------- +-- @function [parent=#CheckBox] getSelectedState +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#CheckBox] loadTextureBackGroundSelected +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#CheckBox] loadTextureBackGroundDisabled +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#CheckBox] loadTextureFrontCross +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#CheckBox] loadTextures +-- @param self +-- @param #char char +-- @param #char char +-- @param #char char +-- @param #char char +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#CheckBox] loadTextureBackGround +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#CheckBox] setSelectedState +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#CheckBox] loadTextureFrontCrossDisabled +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#CheckBox] create +-- @param self +-- @return CheckBox#CheckBox ret (return value: ccui.CheckBox) + +-------------------------------- +-- @function [parent=#CheckBox] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#CheckBox] CheckBox +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ClippingNode.lua b/cocos/scripting/lua-bindings/auto/api/ClippingNode.lua new file mode 100644 index 0000000000..95c99c5290 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ClippingNode.lua @@ -0,0 +1,44 @@ + +-------------------------------- +-- @module ClippingNode +-------------------------------- +-- @function [parent=#ClippingNode] isInverted +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ClippingNode] setInverted +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ClippingNode] setStencil +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#ClippingNode] getAlphaThreshold +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ClippingNode] getStencil +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#ClippingNode] setAlphaThreshold +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: create(cc.Node) +-- +-- overload function: create() +-- +-- @function [parent=#ClippingNode] create +-- @param self +-- @param #cc.Node node +-- @return ClippingNode#ClippingNode ret (retunr value: cc.ClippingNode) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ComAttribute.lua b/cocos/scripting/lua-bindings/auto/api/ComAttribute.lua new file mode 100644 index 0000000000..1a56b59927 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ComAttribute.lua @@ -0,0 +1,72 @@ + +-------------------------------- +-- @module ComAttribute +-------------------------------- +-- @function [parent=#ComAttribute] getFloat +-- @param self +-- @param #string str +-- @param #float float +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ComAttribute] getString +-- @param self +-- @param #string str +-- @param #string str +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#ComAttribute] setFloat +-- @param self +-- @param #string str +-- @param #float float + +-------------------------------- +-- @function [parent=#ComAttribute] setString +-- @param self +-- @param #string str +-- @param #string str + +-------------------------------- +-- @function [parent=#ComAttribute] getBool +-- @param self +-- @param #string str +-- @param #bool bool +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ComAttribute] setInt +-- @param self +-- @param #string str +-- @param #int int + +-------------------------------- +-- @function [parent=#ComAttribute] parse +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ComAttribute] getInt +-- @param self +-- @param #string str +-- @param #int int +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#ComAttribute] setBool +-- @param self +-- @param #string str +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ComAttribute] create +-- @param self +-- @return ComAttribute#ComAttribute ret (return value: ccs.ComAttribute) + +-------------------------------- +-- @function [parent=#ComAttribute] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ComAudio.lua b/cocos/scripting/lua-bindings/auto/api/ComAudio.lua new file mode 100644 index 0000000000..ed17a04cff --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ComAudio.lua @@ -0,0 +1,156 @@ + +-------------------------------- +-- @module ComAudio +-------------------------------- +-- @function [parent=#ComAudio] stopAllEffects +-- @param self + +-------------------------------- +-- @function [parent=#ComAudio] getEffectsVolume +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ComAudio] stopEffect +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#ComAudio] getBackgroundMusicVolume +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ComAudio] willPlayBackgroundMusic +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ComAudio] setBackgroundMusicVolume +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ComAudio] end +-- @param self + +-------------------------------- +-- overload function: stopBackgroundMusic() +-- +-- overload function: stopBackgroundMusic(bool) +-- +-- @function [parent=#ComAudio] stopBackgroundMusic +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ComAudio] pauseBackgroundMusic +-- @param self + +-------------------------------- +-- @function [parent=#ComAudio] isBackgroundMusicPlaying +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ComAudio] isLoop +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ComAudio] resumeAllEffects +-- @param self + +-------------------------------- +-- @function [parent=#ComAudio] pauseAllEffects +-- @param self + +-------------------------------- +-- @function [parent=#ComAudio] preloadBackgroundMusic +-- @param self +-- @param #char char + +-------------------------------- +-- overload function: playBackgroundMusic(char) +-- +-- overload function: playBackgroundMusic(char, bool) +-- +-- overload function: playBackgroundMusic() +-- +-- @function [parent=#ComAudio] playBackgroundMusic +-- @param self +-- @param #char char +-- @param #bool bool + +-------------------------------- +-- overload function: playEffect(char) +-- +-- overload function: playEffect(char, bool) +-- +-- overload function: playEffect() +-- +-- @function [parent=#ComAudio] playEffect +-- @param self +-- @param #char char +-- @param #bool bool +-- @return unsigned int#unsigned int ret (retunr value: unsigned int) + +-------------------------------- +-- @function [parent=#ComAudio] preloadEffect +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#ComAudio] setLoop +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ComAudio] unloadEffect +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#ComAudio] rewindBackgroundMusic +-- @param self + +-------------------------------- +-- @function [parent=#ComAudio] pauseEffect +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#ComAudio] resumeBackgroundMusic +-- @param self + +-------------------------------- +-- @function [parent=#ComAudio] setFile +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#ComAudio] setEffectsVolume +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ComAudio] getFile +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#ComAudio] resumeEffect +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#ComAudio] create +-- @param self +-- @return ComAudio#ComAudio ret (return value: ccs.ComAudio) + +-------------------------------- +-- @function [parent=#ComAudio] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ComController.lua b/cocos/scripting/lua-bindings/auto/api/ComController.lua new file mode 100644 index 0000000000..abb9f781d9 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ComController.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module ComController +-------------------------------- +-- @function [parent=#ComController] create +-- @param self +-- @return ComController#ComController ret (return value: ccs.ComController) + +-------------------------------- +-- @function [parent=#ComController] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#ComController] ComController +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ComRender.lua b/cocos/scripting/lua-bindings/auto/api/ComRender.lua new file mode 100644 index 0000000000..47cc709273 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ComRender.lua @@ -0,0 +1,30 @@ + +-------------------------------- +-- @module ComRender +-------------------------------- +-- @function [parent=#ComRender] setNode +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#ComRender] getNode +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- overload function: create(cc.Node, char) +-- +-- overload function: create() +-- +-- @function [parent=#ComRender] create +-- @param self +-- @param #cc.Node node +-- @param #char char +-- @return ComRender#ComRender ret (retunr value: ccs.ComRender) + +-------------------------------- +-- @function [parent=#ComRender] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Component.lua b/cocos/scripting/lua-bindings/auto/api/Component.lua new file mode 100644 index 0000000000..9ebea04fda --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Component.lua @@ -0,0 +1,49 @@ + +-------------------------------- +-- @module Component +-------------------------------- +-- @function [parent=#Component] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Component] setName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#Component] isEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Component] update +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Component] getOwner +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#Component] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Component] setOwner +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#Component] getName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#Component] create +-- @param self +-- @return Component#Component ret (return value: cc.Component) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ContourData.lua b/cocos/scripting/lua-bindings/auto/api/ContourData.lua new file mode 100644 index 0000000000..0c1705d8eb --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ContourData.lua @@ -0,0 +1,23 @@ + +-------------------------------- +-- @module ContourData +-------------------------------- +-- @function [parent=#ContourData] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ContourData] addVertex +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ContourData] create +-- @param self +-- @return ContourData#ContourData ret (return value: ccs.ContourData) + +-------------------------------- +-- @function [parent=#ContourData] ContourData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Control.lua b/cocos/scripting/lua-bindings/auto/api/Control.lua new file mode 100644 index 0000000000..4f05c72a54 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Control.lua @@ -0,0 +1,95 @@ + +-------------------------------- +-- @module Control +-------------------------------- +-- @function [parent=#Control] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Control] onTouchMoved +-- @param self +-- @param #cc.Touch touch +-- @param #cc.Event event + +-------------------------------- +-- @function [parent=#Control] getState +-- @param self +-- @return Control::State#Control::State ret (return value: cc.Control::State) + +-------------------------------- +-- @function [parent=#Control] onTouchEnded +-- @param self +-- @param #cc.Touch touch +-- @param #cc.Event event + +-------------------------------- +-- @function [parent=#Control] sendActionsForControlEvents +-- @param self +-- @param #cc.Control::EventType eventtype + +-------------------------------- +-- @function [parent=#Control] setSelected +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Control] isEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Control] onTouchCancelled +-- @param self +-- @param #cc.Touch touch +-- @param #cc.Event event + +-------------------------------- +-- @function [parent=#Control] needsLayout +-- @param self + +-------------------------------- +-- @function [parent=#Control] onTouchBegan +-- @param self +-- @param #cc.Touch touch +-- @param #cc.Event event +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Control] hasVisibleParents +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Control] isSelected +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Control] isTouchInside +-- @param self +-- @param #cc.Touch touch +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Control] setHighlighted +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Control] getTouchLocation +-- @param self +-- @param #cc.Touch touch +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Control] isHighlighted +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Control] create +-- @param self +-- @return Control#Control ret (return value: cc.Control) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ControlButton.lua b/cocos/scripting/lua-bindings/auto/api/ControlButton.lua new file mode 100644 index 0000000000..a531876630 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ControlButton.lua @@ -0,0 +1,234 @@ + +-------------------------------- +-- @module ControlButton +-------------------------------- +-- @function [parent=#ControlButton] isPushed +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlButton] setSelected +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlButton] setTitleLabelForState +-- @param self +-- @param #cc.Node node +-- @param #cc.Control::State state + +-------------------------------- +-- @function [parent=#ControlButton] setAdjustBackgroundImage +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlButton] setHighlighted +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlButton] setZoomOnTouchDown +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlButton] setTitleForState +-- @param self +-- @param #string str +-- @param #cc.Control::State state + +-------------------------------- +-- @function [parent=#ControlButton] setLabelAnchorPoint +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ControlButton] getLabelAnchorPoint +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#ControlButton] getTitleTTFSizeForState +-- @param self +-- @param #cc.Control::State state +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlButton] setTitleTTFForState +-- @param self +-- @param #string str +-- @param #cc.Control::State state + +-------------------------------- +-- @function [parent=#ControlButton] setTitleTTFSizeForState +-- @param self +-- @param #float float +-- @param #cc.Control::State state + +-------------------------------- +-- @function [parent=#ControlButton] setTitleLabel +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#ControlButton] setPreferredSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#ControlButton] getCurrentTitleColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#ControlButton] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlButton] getBackgroundSpriteForState +-- @param self +-- @param #cc.Control::State state +-- @return Scale9Sprite#Scale9Sprite ret (return value: cc.Scale9Sprite) + +-------------------------------- +-- @function [parent=#ControlButton] getHorizontalOrigin +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#ControlButton] needsLayout +-- @param self + +-------------------------------- +-- overload function: getCurrentTitle() +-- +-- overload function: getCurrentTitle() +-- +-- @function [parent=#ControlButton] getCurrentTitle +-- @param self +-- @return string#string ret (retunr value: string) + +-------------------------------- +-- @function [parent=#ControlButton] getScaleRatio +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlButton] getTitleTTFForState +-- @param self +-- @param #cc.Control::State state +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#ControlButton] getBackgroundSprite +-- @param self +-- @return Scale9Sprite#Scale9Sprite ret (return value: cc.Scale9Sprite) + +-------------------------------- +-- @function [parent=#ControlButton] getTitleColorForState +-- @param self +-- @param #cc.Control::State state +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#ControlButton] setTitleColorForState +-- @param self +-- @param #color3B_table color3b +-- @param #cc.Control::State state + +-------------------------------- +-- @function [parent=#ControlButton] doesAdjustBackgroundImage +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlButton] setBackgroundSpriteFrameForState +-- @param self +-- @param #cc.SpriteFrame spriteframe +-- @param #cc.Control::State state + +-------------------------------- +-- @function [parent=#ControlButton] setBackgroundSpriteForState +-- @param self +-- @param #cc.Scale9Sprite scale9sprite +-- @param #cc.Control::State state + +-------------------------------- +-- @function [parent=#ControlButton] setScaleRatio +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlButton] setBackgroundSprite +-- @param self +-- @param #cc.Scale9Sprite scale9sprite + +-------------------------------- +-- @function [parent=#ControlButton] getTitleLabel +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#ControlButton] getPreferredSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#ControlButton] getVerticalMargin +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#ControlButton] getTitleLabelForState +-- @param self +-- @param #cc.Control::State state +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#ControlButton] setMargins +-- @param self +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#ControlButton] setTitleBMFontForState +-- @param self +-- @param #string str +-- @param #cc.Control::State state + +-------------------------------- +-- @function [parent=#ControlButton] getTitleBMFontForState +-- @param self +-- @param #cc.Control::State state +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#ControlButton] getZoomOnTouchDown +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlButton] getTitleForState +-- @param self +-- @param #cc.Control::State state +-- @return string#string ret (return value: string) + +-------------------------------- +-- overload function: create(cc.Scale9Sprite) +-- +-- overload function: create() +-- +-- overload function: create(cc.Node, cc.Scale9Sprite) +-- +-- overload function: create(string, string, float) +-- +-- @function [parent=#ControlButton] create +-- @param self +-- @param #string str +-- @param #string str +-- @param #float float +-- @return ControlButton#ControlButton ret (retunr value: cc.ControlButton) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ControlColourPicker.lua b/cocos/scripting/lua-bindings/auto/api/ControlColourPicker.lua new file mode 100644 index 0000000000..738dd32e51 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ControlColourPicker.lua @@ -0,0 +1,70 @@ + +-------------------------------- +-- @module ControlColourPicker +-------------------------------- +-- @function [parent=#ControlColourPicker] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlColourPicker] getHuePicker +-- @param self +-- @return ControlHuePicker#ControlHuePicker ret (return value: cc.ControlHuePicker) + +-------------------------------- +-- @function [parent=#ControlColourPicker] setColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#ControlColourPicker] hueSliderValueChanged +-- @param self +-- @param #cc.Ref ref +-- @param #cc.Control::EventType eventtype + +-------------------------------- +-- @function [parent=#ControlColourPicker] getcolourPicker +-- @param self +-- @return ControlSaturationBrightnessPicker#ControlSaturationBrightnessPicker ret (return value: cc.ControlSaturationBrightnessPicker) + +-------------------------------- +-- @function [parent=#ControlColourPicker] setBackground +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlColourPicker] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlColourPicker] setcolourPicker +-- @param self +-- @param #cc.ControlSaturationBrightnessPicker controlsaturationbrightnesspicker + +-------------------------------- +-- @function [parent=#ControlColourPicker] colourSliderValueChanged +-- @param self +-- @param #cc.Ref ref +-- @param #cc.Control::EventType eventtype + +-------------------------------- +-- @function [parent=#ControlColourPicker] setHuePicker +-- @param self +-- @param #cc.ControlHuePicker controlhuepicker + +-------------------------------- +-- @function [parent=#ControlColourPicker] getBackground +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlColourPicker] create +-- @param self +-- @return ControlColourPicker#ControlColourPicker ret (return value: cc.ControlColourPicker) + +-------------------------------- +-- @function [parent=#ControlColourPicker] ControlColourPicker +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ControlHuePicker.lua b/cocos/scripting/lua-bindings/auto/api/ControlHuePicker.lua new file mode 100644 index 0000000000..bfafd369cf --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ControlHuePicker.lua @@ -0,0 +1,72 @@ + +-------------------------------- +-- @module ControlHuePicker +-------------------------------- +-- @function [parent=#ControlHuePicker] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlHuePicker] initWithTargetAndPos +-- @param self +-- @param #cc.Node node +-- @param #point_table point +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlHuePicker] setHue +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlHuePicker] getStartPos +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#ControlHuePicker] getHue +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlHuePicker] getSlider +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlHuePicker] setBackground +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlHuePicker] setHuePercentage +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlHuePicker] getBackground +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlHuePicker] getHuePercentage +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlHuePicker] setSlider +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlHuePicker] create +-- @param self +-- @param #cc.Node node +-- @param #point_table point +-- @return ControlHuePicker#ControlHuePicker ret (return value: cc.ControlHuePicker) + +-------------------------------- +-- @function [parent=#ControlHuePicker] ControlHuePicker +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ControlPotentiometer.lua b/cocos/scripting/lua-bindings/auto/api/ControlPotentiometer.lua new file mode 100644 index 0000000000..7247a6195a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ControlPotentiometer.lua @@ -0,0 +1,115 @@ + +-------------------------------- +-- @module ControlPotentiometer +-------------------------------- +-- @function [parent=#ControlPotentiometer] setPreviousLocation +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ControlPotentiometer] setValue +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlPotentiometer] getProgressTimer +-- @param self +-- @return ProgressTimer#ProgressTimer ret (return value: cc.ProgressTimer) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] getMaximumValue +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] angleInDegreesBetweenLineFromPoint_toPoint_toLineFromPoint_toPoint +-- @param self +-- @param #point_table point +-- @param #point_table point +-- @param #point_table point +-- @param #point_table point +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] potentiometerBegan +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ControlPotentiometer] setMaximumValue +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlPotentiometer] getMinimumValue +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] setThumbSprite +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlPotentiometer] getValue +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] getPreviousLocation +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] distanceBetweenPointAndPoint +-- @param self +-- @param #point_table point +-- @param #point_table point +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] potentiometerEnded +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ControlPotentiometer] setProgressTimer +-- @param self +-- @param #cc.ProgressTimer progresstimer + +-------------------------------- +-- @function [parent=#ControlPotentiometer] setMinimumValue +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlPotentiometer] getThumbSprite +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] initWithTrackSprite_ProgressTimer_ThumbSprite +-- @param self +-- @param #cc.Sprite sprite +-- @param #cc.ProgressTimer progresstimer +-- @param #cc.Sprite sprite +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] potentiometerMoved +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ControlPotentiometer] create +-- @param self +-- @param #char char +-- @param #char char +-- @param #char char +-- @return ControlPotentiometer#ControlPotentiometer ret (return value: cc.ControlPotentiometer) + +-------------------------------- +-- @function [parent=#ControlPotentiometer] ControlPotentiometer +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ControlSaturationBrightnessPicker.lua b/cocos/scripting/lua-bindings/auto/api/ControlSaturationBrightnessPicker.lua new file mode 100644 index 0000000000..df422daaf7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ControlSaturationBrightnessPicker.lua @@ -0,0 +1,62 @@ + +-------------------------------- +-- @module ControlSaturationBrightnessPicker +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] getShadow +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] initWithTargetAndPos +-- @param self +-- @param #cc.Node node +-- @param #point_table point +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] getStartPos +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] getOverlay +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] getSlider +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] getBackground +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] getSaturation +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] getBrightness +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] create +-- @param self +-- @param #cc.Node node +-- @param #point_table point +-- @return ControlSaturationBrightnessPicker#ControlSaturationBrightnessPicker ret (return value: cc.ControlSaturationBrightnessPicker) + +-------------------------------- +-- @function [parent=#ControlSaturationBrightnessPicker] ControlSaturationBrightnessPicker +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ControlSlider.lua b/cocos/scripting/lua-bindings/auto/api/ControlSlider.lua new file mode 100644 index 0000000000..c388ad7e51 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ControlSlider.lua @@ -0,0 +1,149 @@ + +-------------------------------- +-- @module ControlSlider +-------------------------------- +-- @function [parent=#ControlSlider] getSelectedThumbSprite +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlSlider] locationFromTouch +-- @param self +-- @param #cc.Touch touch +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#ControlSlider] setSelectedThumbSprite +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlSlider] setProgressSprite +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlSlider] getMaximumAllowedValue +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlSlider] getMinimumAllowedValue +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlSlider] getMinimumValue +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlSlider] setThumbSprite +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlSlider] setMinimumValue +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlSlider] setMinimumAllowedValue +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlSlider] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlSlider] setValue +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlSlider] setMaximumValue +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlSlider] needsLayout +-- @param self + +-------------------------------- +-- @function [parent=#ControlSlider] getBackgroundSprite +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- overload function: initWithSprites(cc.Sprite, cc.Sprite, cc.Sprite, cc.Sprite) +-- +-- overload function: initWithSprites(cc.Sprite, cc.Sprite, cc.Sprite) +-- +-- @function [parent=#ControlSlider] initWithSprites +-- @param self +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#ControlSlider] getMaximumValue +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlSlider] isTouchInside +-- @param self +-- @param #cc.Touch touch +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlSlider] getValue +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ControlSlider] getThumbSprite +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlSlider] getProgressSprite +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlSlider] setBackgroundSprite +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlSlider] setMaximumAllowedValue +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: create(cc.Sprite, cc.Sprite, cc.Sprite) +-- +-- overload function: create(char, char, char) +-- +-- overload function: create(char, char, char, char) +-- +-- overload function: create(cc.Sprite, cc.Sprite, cc.Sprite, cc.Sprite) +-- +-- @function [parent=#ControlSlider] create +-- @param self +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @return ControlSlider#ControlSlider ret (retunr value: cc.ControlSlider) + +-------------------------------- +-- @function [parent=#ControlSlider] ControlSlider +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ControlStepper.lua b/cocos/scripting/lua-bindings/auto/api/ControlStepper.lua new file mode 100644 index 0000000000..0593d6d5ad --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ControlStepper.lua @@ -0,0 +1,121 @@ + +-------------------------------- +-- @module ControlStepper +-------------------------------- +-- @function [parent=#ControlStepper] setMinusSprite +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlStepper] getMinusLabel +-- @param self +-- @return LabelTTF#LabelTTF ret (return value: cc.LabelTTF) + +-------------------------------- +-- @function [parent=#ControlStepper] setWraps +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlStepper] isContinuous +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlStepper] getMinusSprite +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlStepper] updateLayoutUsingTouchLocation +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ControlStepper] setValueWithSendingEvent +-- @param self +-- @param #double double +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlStepper] getPlusLabel +-- @param self +-- @return LabelTTF#LabelTTF ret (return value: cc.LabelTTF) + +-------------------------------- +-- @function [parent=#ControlStepper] stopAutorepeat +-- @param self + +-------------------------------- +-- @function [parent=#ControlStepper] setMinimumValue +-- @param self +-- @param #double double + +-------------------------------- +-- @function [parent=#ControlStepper] getPlusSprite +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ControlStepper] setPlusSprite +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ControlStepper] setMinusLabel +-- @param self +-- @param #cc.LabelTTF labelttf + +-------------------------------- +-- @function [parent=#ControlStepper] setValue +-- @param self +-- @param #double double + +-------------------------------- +-- @function [parent=#ControlStepper] setStepValue +-- @param self +-- @param #double double + +-------------------------------- +-- @function [parent=#ControlStepper] setMaximumValue +-- @param self +-- @param #double double + +-------------------------------- +-- @function [parent=#ControlStepper] update +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ControlStepper] startAutorepeat +-- @param self + +-------------------------------- +-- @function [parent=#ControlStepper] initWithMinusSpriteAndPlusSprite +-- @param self +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlStepper] getValue +-- @param self +-- @return double#double ret (return value: double) + +-------------------------------- +-- @function [parent=#ControlStepper] setPlusLabel +-- @param self +-- @param #cc.LabelTTF labelttf + +-------------------------------- +-- @function [parent=#ControlStepper] create +-- @param self +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @return ControlStepper#ControlStepper ret (return value: cc.ControlStepper) + +-------------------------------- +-- @function [parent=#ControlStepper] ControlStepper +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ControlSwitch.lua b/cocos/scripting/lua-bindings/auto/api/ControlSwitch.lua new file mode 100644 index 0000000000..b94ede5035 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ControlSwitch.lua @@ -0,0 +1,69 @@ + +-------------------------------- +-- @module ControlSwitch +-------------------------------- +-- @function [parent=#ControlSwitch] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- overload function: setOn(bool) +-- +-- overload function: setOn(bool, bool) +-- +-- @function [parent=#ControlSwitch] setOn +-- @param self +-- @param #bool bool +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ControlSwitch] isOn +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- overload function: initWithMaskSprite(cc.Sprite, cc.Sprite, cc.Sprite, cc.Sprite, cc.LabelTTF, cc.LabelTTF) +-- +-- overload function: initWithMaskSprite(cc.Sprite, cc.Sprite, cc.Sprite, cc.Sprite) +-- +-- @function [parent=#ControlSwitch] initWithMaskSprite +-- @param self +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.LabelTTF labelttf +-- @param #cc.LabelTTF labelttf +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#ControlSwitch] hasMoved +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ControlSwitch] locationFromTouch +-- @param self +-- @param #cc.Touch touch +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- overload function: create(cc.Sprite, cc.Sprite, cc.Sprite, cc.Sprite) +-- +-- overload function: create(cc.Sprite, cc.Sprite, cc.Sprite, cc.Sprite, cc.LabelTTF, cc.LabelTTF) +-- +-- @function [parent=#ControlSwitch] create +-- @param self +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.Sprite sprite +-- @param #cc.LabelTTF labelttf +-- @param #cc.LabelTTF labelttf +-- @return ControlSwitch#ControlSwitch ret (retunr value: cc.ControlSwitch) + +-------------------------------- +-- @function [parent=#ControlSwitch] ControlSwitch +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/DelayTime.lua b/cocos/scripting/lua-bindings/auto/api/DelayTime.lua new file mode 100644 index 0000000000..2d4b2e0b8c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/DelayTime.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module DelayTime +-------------------------------- +-- @function [parent=#DelayTime] create +-- @param self +-- @param #float float +-- @return DelayTime#DelayTime ret (return value: cc.DelayTime) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Director.lua b/cocos/scripting/lua-bindings/auto/api/Director.lua new file mode 100644 index 0000000000..92d9e11159 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Director.lua @@ -0,0 +1,258 @@ + +-------------------------------- +-- @module Director +-------------------------------- +-- @function [parent=#Director] pause +-- @param self + +-------------------------------- +-- @function [parent=#Director] setContentScaleFactor +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Director] getContentScaleFactor +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Director] getWinSizeInPixels +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Director] getDeltaTime +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Director] setGLDefaultValues +-- @param self + +-------------------------------- +-- @function [parent=#Director] setActionManager +-- @param self +-- @param #cc.ActionManager actionmanager + +-------------------------------- +-- @function [parent=#Director] setAlphaBlending +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Director] popToRootScene +-- @param self + +-------------------------------- +-- @function [parent=#Director] getNotificationNode +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#Director] getWinSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Director] getTextureCache +-- @param self +-- @return TextureCache#TextureCache ret (return value: cc.TextureCache) + +-------------------------------- +-- @function [parent=#Director] isSendCleanupToScene +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Director] getVisibleOrigin +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Director] mainLoop +-- @param self + +-------------------------------- +-- @function [parent=#Director] setDepthTest +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Director] getFrameRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Director] getSecondsPerFrame +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Director] convertToUI +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Director] setDefaultValues +-- @param self + +-------------------------------- +-- @function [parent=#Director] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Director] setScheduler +-- @param self +-- @param #cc.Scheduler scheduler + +-------------------------------- +-- @function [parent=#Director] startAnimation +-- @param self + +-------------------------------- +-- @function [parent=#Director] getRunningScene +-- @param self +-- @return Scene#Scene ret (return value: cc.Scene) + +-------------------------------- +-- @function [parent=#Director] setViewport +-- @param self + +-------------------------------- +-- @function [parent=#Director] stopAnimation +-- @param self + +-------------------------------- +-- @function [parent=#Director] popToSceneStackLevel +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Director] resume +-- @param self + +-------------------------------- +-- @function [parent=#Director] isNextDeltaTimeZero +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Director] end +-- @param self + +-------------------------------- +-- @function [parent=#Director] setOpenGLView +-- @param self +-- @param #cc.GLView glview + +-------------------------------- +-- @function [parent=#Director] convertToGL +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Director] purgeCachedData +-- @param self + +-------------------------------- +-- @function [parent=#Director] getTotalFrames +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#Director] runWithScene +-- @param self +-- @param #cc.Scene scene + +-------------------------------- +-- @function [parent=#Director] setNotificationNode +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#Director] drawScene +-- @param self + +-------------------------------- +-- @function [parent=#Director] popScene +-- @param self + +-------------------------------- +-- @function [parent=#Director] isDisplayStats +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Director] setProjection +-- @param self +-- @param #cc.Director::Projection projection + +-------------------------------- +-- @function [parent=#Director] getConsole +-- @param self +-- @return Console#Console ret (return value: cc.Console) + +-------------------------------- +-- @function [parent=#Director] getZEye +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Director] setNextDeltaTimeZero +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Director] getVisibleSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Director] getScheduler +-- @param self +-- @return Scheduler#Scheduler ret (return value: cc.Scheduler) + +-------------------------------- +-- @function [parent=#Director] pushScene +-- @param self +-- @param #cc.Scene scene + +-------------------------------- +-- @function [parent=#Director] getAnimationInterval +-- @param self +-- @return double#double ret (return value: double) + +-------------------------------- +-- @function [parent=#Director] isPaused +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Director] setDisplayStats +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Director] replaceScene +-- @param self +-- @param #cc.Scene scene + +-------------------------------- +-- @function [parent=#Director] setAnimationInterval +-- @param self +-- @param #double double + +-------------------------------- +-- @function [parent=#Director] getActionManager +-- @param self +-- @return ActionManager#ActionManager ret (return value: cc.ActionManager) + +-------------------------------- +-- @function [parent=#Director] getInstance +-- @param self +-- @return Director#Director ret (return value: cc.Director) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/DisplayData.lua b/cocos/scripting/lua-bindings/auto/api/DisplayData.lua new file mode 100644 index 0000000000..6254b517c5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/DisplayData.lua @@ -0,0 +1,24 @@ + +-------------------------------- +-- @module DisplayData +-------------------------------- +-- @function [parent=#DisplayData] copy +-- @param self +-- @param #ccs.DisplayData displaydata + +-------------------------------- +-- @function [parent=#DisplayData] changeDisplayToTexture +-- @param self +-- @param #string str +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#DisplayData] create +-- @param self +-- @return DisplayData#DisplayData ret (return value: ccs.DisplayData) + +-------------------------------- +-- @function [parent=#DisplayData] DisplayData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/DisplayManager.lua b/cocos/scripting/lua-bindings/auto/api/DisplayManager.lua new file mode 100644 index 0000000000..51f3ecead1 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/DisplayManager.lua @@ -0,0 +1,118 @@ + +-------------------------------- +-- @module DisplayManager +-------------------------------- +-- @function [parent=#DisplayManager] getDisplayRenderNode +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#DisplayManager] getAnchorPointInPoints +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#DisplayManager] getDisplayRenderNodeType +-- @param self +-- @return DisplayType#DisplayType ret (return value: ccs.DisplayType) + +-------------------------------- +-- @function [parent=#DisplayManager] removeDisplay +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#DisplayManager] setForceChangeDisplay +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#DisplayManager] init +-- @param self +-- @param #ccs.Bone bone +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#DisplayManager] getContentSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#DisplayManager] getBoundingBox +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- overload function: addDisplay(cc.Node, int) +-- +-- overload function: addDisplay(ccs.DisplayData, int) +-- +-- @function [parent=#DisplayManager] addDisplay +-- @param self +-- @param #ccs.DisplayData displaydata +-- @param #int int + +-------------------------------- +-- overload function: containPoint(float, float) +-- +-- overload function: containPoint(point_table) +-- +-- @function [parent=#DisplayManager] containPoint +-- @param self +-- @param #float float +-- @param #float float +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#DisplayManager] changeDisplayWithIndex +-- @param self +-- @param #int int +-- @param #bool bool + +-------------------------------- +-- @function [parent=#DisplayManager] changeDisplayWithName +-- @param self +-- @param #string str +-- @param #bool bool + +-------------------------------- +-- @function [parent=#DisplayManager] isForceChangeDisplay +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#DisplayManager] getCurrentDisplayIndex +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#DisplayManager] getAnchorPoint +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#DisplayManager] getDecorativeDisplayList +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#DisplayManager] isVisible +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#DisplayManager] setVisible +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#DisplayManager] create +-- @param self +-- @param #ccs.Bone bone +-- @return DisplayManager#DisplayManager ret (return value: ccs.DisplayManager) + +-------------------------------- +-- @function [parent=#DisplayManager] DisplayManager +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/DrawNode.lua b/cocos/scripting/lua-bindings/auto/api/DrawNode.lua new file mode 100644 index 0000000000..3b1395515d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/DrawNode.lua @@ -0,0 +1,61 @@ + +-------------------------------- +-- @module DrawNode +-------------------------------- +-- @function [parent=#DrawNode] drawQuadraticBezier +-- @param self +-- @param #point_table point +-- @param #point_table point +-- @param #point_table point +-- @param #unsigned int int +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#DrawNode] onDraw +-- @param self +-- @param #kmMat4 kmmat4 +-- @param #bool bool + +-------------------------------- +-- @function [parent=#DrawNode] clear +-- @param self + +-------------------------------- +-- @function [parent=#DrawNode] drawTriangle +-- @param self +-- @param #point_table point +-- @param #point_table point +-- @param #point_table point +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#DrawNode] drawDot +-- @param self +-- @param #point_table point +-- @param #float float +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#DrawNode] drawCubicBezier +-- @param self +-- @param #point_table point +-- @param #point_table point +-- @param #point_table point +-- @param #point_table point +-- @param #unsigned int int +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#DrawNode] drawSegment +-- @param self +-- @param #point_table point +-- @param #point_table point +-- @param #float float +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#DrawNode] create +-- @param self +-- @return DrawNode#DrawNode ret (return value: cc.DrawNode) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseBackIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseBackIn.lua new file mode 100644 index 0000000000..72c45e3547 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseBackIn.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseBackIn +-------------------------------- +-- @function [parent=#EaseBackIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseBackIn#EaseBackIn ret (return value: cc.EaseBackIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseBackInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseBackInOut.lua new file mode 100644 index 0000000000..89600126c9 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseBackInOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseBackInOut +-------------------------------- +-- @function [parent=#EaseBackInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseBackInOut#EaseBackInOut ret (return value: cc.EaseBackInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseBackOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseBackOut.lua new file mode 100644 index 0000000000..1cb1ef0020 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseBackOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseBackOut +-------------------------------- +-- @function [parent=#EaseBackOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseBackOut#EaseBackOut ret (return value: cc.EaseBackOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseBezierAction.lua b/cocos/scripting/lua-bindings/auto/api/EaseBezierAction.lua new file mode 100644 index 0000000000..40888faf06 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseBezierAction.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module EaseBezierAction +-------------------------------- +-- @function [parent=#EaseBezierAction] setBezierParamer +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#EaseBezierAction] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseBezierAction#EaseBezierAction ret (return value: cc.EaseBezierAction) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseBounce.lua b/cocos/scripting/lua-bindings/auto/api/EaseBounce.lua new file mode 100644 index 0000000000..073f0b5de0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseBounce.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module EaseBounce +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseBounceIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseBounceIn.lua new file mode 100644 index 0000000000..9282a1c3bf --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseBounceIn.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseBounceIn +-------------------------------- +-- @function [parent=#EaseBounceIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseBounceIn#EaseBounceIn ret (return value: cc.EaseBounceIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseBounceInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseBounceInOut.lua new file mode 100644 index 0000000000..9812f9e831 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseBounceInOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseBounceInOut +-------------------------------- +-- @function [parent=#EaseBounceInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseBounceInOut#EaseBounceInOut ret (return value: cc.EaseBounceInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseBounceOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseBounceOut.lua new file mode 100644 index 0000000000..8b0f6cb190 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseBounceOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseBounceOut +-------------------------------- +-- @function [parent=#EaseBounceOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseBounceOut#EaseBounceOut ret (return value: cc.EaseBounceOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseCircleActionIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseCircleActionIn.lua new file mode 100644 index 0000000000..1a620dedd9 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseCircleActionIn.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseCircleActionIn +-------------------------------- +-- @function [parent=#EaseCircleActionIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseCircleActionIn#EaseCircleActionIn ret (return value: cc.EaseCircleActionIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseCircleActionInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseCircleActionInOut.lua new file mode 100644 index 0000000000..07a92ad3b5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseCircleActionInOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseCircleActionInOut +-------------------------------- +-- @function [parent=#EaseCircleActionInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseCircleActionInOut#EaseCircleActionInOut ret (return value: cc.EaseCircleActionInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseCircleActionOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseCircleActionOut.lua new file mode 100644 index 0000000000..dfb8264986 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseCircleActionOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseCircleActionOut +-------------------------------- +-- @function [parent=#EaseCircleActionOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseCircleActionOut#EaseCircleActionOut ret (return value: cc.EaseCircleActionOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseCubicActionIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseCubicActionIn.lua new file mode 100644 index 0000000000..eded3af4dd --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseCubicActionIn.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseCubicActionIn +-------------------------------- +-- @function [parent=#EaseCubicActionIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseCubicActionIn#EaseCubicActionIn ret (return value: cc.EaseCubicActionIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseCubicActionInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseCubicActionInOut.lua new file mode 100644 index 0000000000..e6b509b18e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseCubicActionInOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseCubicActionInOut +-------------------------------- +-- @function [parent=#EaseCubicActionInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseCubicActionInOut#EaseCubicActionInOut ret (return value: cc.EaseCubicActionInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseCubicActionOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseCubicActionOut.lua new file mode 100644 index 0000000000..c028f30f27 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseCubicActionOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseCubicActionOut +-------------------------------- +-- @function [parent=#EaseCubicActionOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseCubicActionOut#EaseCubicActionOut ret (return value: cc.EaseCubicActionOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseElastic.lua b/cocos/scripting/lua-bindings/auto/api/EaseElastic.lua new file mode 100644 index 0000000000..fc3b59dc31 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseElastic.lua @@ -0,0 +1,14 @@ + +-------------------------------- +-- @module EaseElastic +-------------------------------- +-- @function [parent=#EaseElastic] setPeriod +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#EaseElastic] getPeriod +-- @param self +-- @return float#float ret (return value: float) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseElasticIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseElasticIn.lua new file mode 100644 index 0000000000..031427012b --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseElasticIn.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module EaseElasticIn +-------------------------------- +-- overload function: create(cc.ActionInterval) +-- +-- overload function: create(cc.ActionInterval, float) +-- +-- @function [parent=#EaseElasticIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @param #float float +-- @return EaseElasticIn#EaseElasticIn ret (retunr value: cc.EaseElasticIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseElasticInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseElasticInOut.lua new file mode 100644 index 0000000000..5fa77fdf44 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseElasticInOut.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module EaseElasticInOut +-------------------------------- +-- overload function: create(cc.ActionInterval) +-- +-- overload function: create(cc.ActionInterval, float) +-- +-- @function [parent=#EaseElasticInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @param #float float +-- @return EaseElasticInOut#EaseElasticInOut ret (retunr value: cc.EaseElasticInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseElasticOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseElasticOut.lua new file mode 100644 index 0000000000..2092a5e457 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseElasticOut.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module EaseElasticOut +-------------------------------- +-- overload function: create(cc.ActionInterval) +-- +-- overload function: create(cc.ActionInterval, float) +-- +-- @function [parent=#EaseElasticOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @param #float float +-- @return EaseElasticOut#EaseElasticOut ret (retunr value: cc.EaseElasticOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseExponentialIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseExponentialIn.lua new file mode 100644 index 0000000000..4a6e475214 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseExponentialIn.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseExponentialIn +-------------------------------- +-- @function [parent=#EaseExponentialIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseExponentialIn#EaseExponentialIn ret (return value: cc.EaseExponentialIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseExponentialInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseExponentialInOut.lua new file mode 100644 index 0000000000..180c5fb6bd --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseExponentialInOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseExponentialInOut +-------------------------------- +-- @function [parent=#EaseExponentialInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseExponentialInOut#EaseExponentialInOut ret (return value: cc.EaseExponentialInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseExponentialOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseExponentialOut.lua new file mode 100644 index 0000000000..7022bfb980 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseExponentialOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseExponentialOut +-------------------------------- +-- @function [parent=#EaseExponentialOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseExponentialOut#EaseExponentialOut ret (return value: cc.EaseExponentialOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseIn.lua new file mode 100644 index 0000000000..669a3dd41d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseIn.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module EaseIn +-------------------------------- +-- @function [parent=#EaseIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @param #float float +-- @return EaseIn#EaseIn ret (return value: cc.EaseIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseInOut.lua new file mode 100644 index 0000000000..520bf95f9b --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseInOut.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module EaseInOut +-------------------------------- +-- @function [parent=#EaseInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @param #float float +-- @return EaseInOut#EaseInOut ret (return value: cc.EaseInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseOut.lua new file mode 100644 index 0000000000..0a9e2581ea --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseOut.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module EaseOut +-------------------------------- +-- @function [parent=#EaseOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @param #float float +-- @return EaseOut#EaseOut ret (return value: cc.EaseOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseQuadraticActionIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseQuadraticActionIn.lua new file mode 100644 index 0000000000..81132ad946 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseQuadraticActionIn.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseQuadraticActionIn +-------------------------------- +-- @function [parent=#EaseQuadraticActionIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseQuadraticActionIn#EaseQuadraticActionIn ret (return value: cc.EaseQuadraticActionIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseQuadraticActionInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseQuadraticActionInOut.lua new file mode 100644 index 0000000000..db288ddff0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseQuadraticActionInOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseQuadraticActionInOut +-------------------------------- +-- @function [parent=#EaseQuadraticActionInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseQuadraticActionInOut#EaseQuadraticActionInOut ret (return value: cc.EaseQuadraticActionInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseQuadraticActionOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseQuadraticActionOut.lua new file mode 100644 index 0000000000..d20294e98e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseQuadraticActionOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseQuadraticActionOut +-------------------------------- +-- @function [parent=#EaseQuadraticActionOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseQuadraticActionOut#EaseQuadraticActionOut ret (return value: cc.EaseQuadraticActionOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseQuarticActionIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseQuarticActionIn.lua new file mode 100644 index 0000000000..4c0e3cf004 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseQuarticActionIn.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseQuarticActionIn +-------------------------------- +-- @function [parent=#EaseQuarticActionIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseQuarticActionIn#EaseQuarticActionIn ret (return value: cc.EaseQuarticActionIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseQuarticActionInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseQuarticActionInOut.lua new file mode 100644 index 0000000000..174c793580 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseQuarticActionInOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseQuarticActionInOut +-------------------------------- +-- @function [parent=#EaseQuarticActionInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseQuarticActionInOut#EaseQuarticActionInOut ret (return value: cc.EaseQuarticActionInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseQuarticActionOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseQuarticActionOut.lua new file mode 100644 index 0000000000..a4b48536a9 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseQuarticActionOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseQuarticActionOut +-------------------------------- +-- @function [parent=#EaseQuarticActionOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseQuarticActionOut#EaseQuarticActionOut ret (return value: cc.EaseQuarticActionOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseQuinticActionIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseQuinticActionIn.lua new file mode 100644 index 0000000000..ed40819c2c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseQuinticActionIn.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseQuinticActionIn +-------------------------------- +-- @function [parent=#EaseQuinticActionIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseQuinticActionIn#EaseQuinticActionIn ret (return value: cc.EaseQuinticActionIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseQuinticActionInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseQuinticActionInOut.lua new file mode 100644 index 0000000000..4272c67c37 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseQuinticActionInOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseQuinticActionInOut +-------------------------------- +-- @function [parent=#EaseQuinticActionInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseQuinticActionInOut#EaseQuinticActionInOut ret (return value: cc.EaseQuinticActionInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseQuinticActionOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseQuinticActionOut.lua new file mode 100644 index 0000000000..fc50ca0487 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseQuinticActionOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseQuinticActionOut +-------------------------------- +-- @function [parent=#EaseQuinticActionOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseQuinticActionOut#EaseQuinticActionOut ret (return value: cc.EaseQuinticActionOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseRateAction.lua b/cocos/scripting/lua-bindings/auto/api/EaseRateAction.lua new file mode 100644 index 0000000000..30bbe7f221 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseRateAction.lua @@ -0,0 +1,14 @@ + +-------------------------------- +-- @module EaseRateAction +-------------------------------- +-- @function [parent=#EaseRateAction] setRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#EaseRateAction] getRate +-- @param self +-- @return float#float ret (return value: float) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseSineIn.lua b/cocos/scripting/lua-bindings/auto/api/EaseSineIn.lua new file mode 100644 index 0000000000..564aa381c7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseSineIn.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseSineIn +-------------------------------- +-- @function [parent=#EaseSineIn] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseSineIn#EaseSineIn ret (return value: cc.EaseSineIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseSineInOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseSineInOut.lua new file mode 100644 index 0000000000..18de34e92a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseSineInOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseSineInOut +-------------------------------- +-- @function [parent=#EaseSineInOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseSineInOut#EaseSineInOut ret (return value: cc.EaseSineInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EaseSineOut.lua b/cocos/scripting/lua-bindings/auto/api/EaseSineOut.lua new file mode 100644 index 0000000000..cba5817cd4 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EaseSineOut.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EaseSineOut +-------------------------------- +-- @function [parent=#EaseSineOut] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return EaseSineOut#EaseSineOut ret (return value: cc.EaseSineOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EditBox.lua b/cocos/scripting/lua-bindings/auto/api/EditBox.lua new file mode 100644 index 0000000000..184fbc8ab0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EditBox.lua @@ -0,0 +1,111 @@ + +-------------------------------- +-- @module EditBox +-------------------------------- +-- @function [parent=#EditBox] getText +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#EditBox] setPlaceholderFontName +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#EditBox] getPlaceHolder +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#EditBox] setFontName +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#EditBox] setPlaceholderFontSize +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#EditBox] setInputMode +-- @param self +-- @param #cc.EditBox::InputMode inputmode + +-------------------------------- +-- @function [parent=#EditBox] setPlaceholderFontColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#EditBox] setFontColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#EditBox] setPlaceholderFont +-- @param self +-- @param #char char +-- @param #int int + +-------------------------------- +-- @function [parent=#EditBox] setFontSize +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#EditBox] initWithSizeAndBackgroundSprite +-- @param self +-- @param #size_table size +-- @param #cc.Scale9Sprite scale9sprite +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#EditBox] setPlaceHolder +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#EditBox] setReturnType +-- @param self +-- @param #cc.EditBox::KeyboardReturnType keyboardreturntype + +-------------------------------- +-- @function [parent=#EditBox] setInputFlag +-- @param self +-- @param #cc.EditBox::InputFlag inputflag + +-------------------------------- +-- @function [parent=#EditBox] getMaxLength +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#EditBox] setText +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#EditBox] setMaxLength +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#EditBox] setFont +-- @param self +-- @param #char char +-- @param #int int + +-------------------------------- +-- @function [parent=#EditBox] create +-- @param self +-- @param #size_table size +-- @param #cc.Scale9Sprite scale9sprite +-- @param #cc.Scale9Sprite scale9sprite +-- @param #cc.Scale9Sprite scale9sprite +-- @return EditBox#EditBox ret (return value: cc.EditBox) + +-------------------------------- +-- @function [parent=#EditBox] EditBox +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Event.lua b/cocos/scripting/lua-bindings/auto/api/Event.lua new file mode 100644 index 0000000000..f9b49b565d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Event.lua @@ -0,0 +1,23 @@ + +-------------------------------- +-- @module Event +-------------------------------- +-- @function [parent=#Event] isStopped +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Event] getType +-- @param self +-- @return Event::Type#Event::Type ret (return value: cc.Event::Type) + +-------------------------------- +-- @function [parent=#Event] getCurrentTarget +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#Event] stopPropagation +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventAcceleration.lua b/cocos/scripting/lua-bindings/auto/api/EventAcceleration.lua new file mode 100644 index 0000000000..96f962872f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventAcceleration.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module EventAcceleration +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventCustom.lua b/cocos/scripting/lua-bindings/auto/api/EventCustom.lua new file mode 100644 index 0000000000..353d4db5b9 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventCustom.lua @@ -0,0 +1,14 @@ + +-------------------------------- +-- @module EventCustom +-------------------------------- +-- @function [parent=#EventCustom] getEventName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#EventCustom] EventCustom +-- @param self +-- @param #string str + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventDispatcher.lua b/cocos/scripting/lua-bindings/auto/api/EventDispatcher.lua new file mode 100644 index 0000000000..edbfec945d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventDispatcher.lua @@ -0,0 +1,85 @@ + +-------------------------------- +-- @module EventDispatcher +-------------------------------- +-- @function [parent=#EventDispatcher] pauseEventListenersForTarget +-- @param self +-- @param #cc.Node node +-- @param #bool bool + +-------------------------------- +-- @function [parent=#EventDispatcher] addEventListenerWithSceneGraphPriority +-- @param self +-- @param #cc.EventListener eventlistener +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#EventDispatcher] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#EventDispatcher] addEventListenerWithFixedPriority +-- @param self +-- @param #cc.EventListener eventlistener +-- @param #int int + +-------------------------------- +-- @function [parent=#EventDispatcher] removeEventListener +-- @param self +-- @param #cc.EventListener eventlistener + +-------------------------------- +-- @function [parent=#EventDispatcher] resumeEventListenersForTarget +-- @param self +-- @param #cc.Node node +-- @param #bool bool + +-------------------------------- +-- @function [parent=#EventDispatcher] removeEventListenersForTarget +-- @param self +-- @param #cc.Node node +-- @param #bool bool + +-------------------------------- +-- @function [parent=#EventDispatcher] setPriority +-- @param self +-- @param #cc.EventListener eventlistener +-- @param #int int + +-------------------------------- +-- @function [parent=#EventDispatcher] addCustomEventListener +-- @param self +-- @param #string str +-- @param #function func +-- @return EventListenerCustom#EventListenerCustom ret (return value: cc.EventListenerCustom) + +-------------------------------- +-- @function [parent=#EventDispatcher] dispatchEvent +-- @param self +-- @param #cc.Event event + +-------------------------------- +-- @function [parent=#EventDispatcher] removeAllEventListeners +-- @param self + +-------------------------------- +-- @function [parent=#EventDispatcher] removeCustomEventListeners +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#EventDispatcher] isEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#EventDispatcher] removeEventListenersForType +-- @param self +-- @param #cc.EventListener::Type type + +-------------------------------- +-- @function [parent=#EventDispatcher] EventDispatcher +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventKeyboard.lua b/cocos/scripting/lua-bindings/auto/api/EventKeyboard.lua new file mode 100644 index 0000000000..01a8715397 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventKeyboard.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module EventKeyboard +-------------------------------- +-- @function [parent=#EventKeyboard] EventKeyboard +-- @param self +-- @param #cc.EventKeyboard::KeyCode keycode +-- @param #bool bool + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListener.lua b/cocos/scripting/lua-bindings/auto/api/EventListener.lua new file mode 100644 index 0000000000..82ef017363 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListener.lua @@ -0,0 +1,14 @@ + +-------------------------------- +-- @module EventListener +-------------------------------- +-- @function [parent=#EventListener] clone +-- @param self +-- @return EventListener#EventListener ret (return value: cc.EventListener) + +-------------------------------- +-- @function [parent=#EventListener] checkAvailable +-- @param self +-- @return bool#bool ret (return value: bool) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerAcceleration.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerAcceleration.lua new file mode 100644 index 0000000000..6a7a27c266 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerAcceleration.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module EventListenerAcceleration +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerCustom.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerCustom.lua new file mode 100644 index 0000000000..4b4e72f3e0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerCustom.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module EventListenerCustom +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerKeyboard.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerKeyboard.lua new file mode 100644 index 0000000000..76a19a8621 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerKeyboard.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module EventListenerKeyboard +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerMouse.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerMouse.lua new file mode 100644 index 0000000000..978ee1c212 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerMouse.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module EventListenerMouse +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContact.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContact.lua new file mode 100644 index 0000000000..c4fd4d1ac6 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContact.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module EventListenerPhysicsContact +-------------------------------- +-- @function [parent=#EventListenerPhysicsContact] create +-- @param self +-- @return EventListenerPhysicsContact#EventListenerPhysicsContact ret (return value: cc.EventListenerPhysicsContact) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContactWithBodies.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContactWithBodies.lua new file mode 100644 index 0000000000..888f390470 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContactWithBodies.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module EventListenerPhysicsContactWithBodies +-------------------------------- +-- @function [parent=#EventListenerPhysicsContactWithBodies] hitTest +-- @param self +-- @param #cc.PhysicsShape physicsshape +-- @param #cc.PhysicsShape physicsshape +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#EventListenerPhysicsContactWithBodies] create +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @return EventListenerPhysicsContactWithBodies#EventListenerPhysicsContactWithBodies ret (return value: cc.EventListenerPhysicsContactWithBodies) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContactWithGroup.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContactWithGroup.lua new file mode 100644 index 0000000000..ebee8a8d2a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContactWithGroup.lua @@ -0,0 +1,17 @@ + +-------------------------------- +-- @module EventListenerPhysicsContactWithGroup +-------------------------------- +-- @function [parent=#EventListenerPhysicsContactWithGroup] hitTest +-- @param self +-- @param #cc.PhysicsShape physicsshape +-- @param #cc.PhysicsShape physicsshape +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#EventListenerPhysicsContactWithGroup] create +-- @param self +-- @param #int int +-- @return EventListenerPhysicsContactWithGroup#EventListenerPhysicsContactWithGroup ret (return value: cc.EventListenerPhysicsContactWithGroup) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContactWithShapes.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContactWithShapes.lua new file mode 100644 index 0000000000..a8ec760d44 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerPhysicsContactWithShapes.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module EventListenerPhysicsContactWithShapes +-------------------------------- +-- @function [parent=#EventListenerPhysicsContactWithShapes] hitTest +-- @param self +-- @param #cc.PhysicsShape physicsshape +-- @param #cc.PhysicsShape physicsshape +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#EventListenerPhysicsContactWithShapes] create +-- @param self +-- @param #cc.PhysicsShape physicsshape +-- @param #cc.PhysicsShape physicsshape +-- @return EventListenerPhysicsContactWithShapes#EventListenerPhysicsContactWithShapes ret (return value: cc.EventListenerPhysicsContactWithShapes) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerTouchAllAtOnce.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerTouchAllAtOnce.lua new file mode 100644 index 0000000000..b7aed9c410 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerTouchAllAtOnce.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module EventListenerTouchAllAtOnce +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventListenerTouchOneByOne.lua b/cocos/scripting/lua-bindings/auto/api/EventListenerTouchOneByOne.lua new file mode 100644 index 0000000000..0e2238b061 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventListenerTouchOneByOne.lua @@ -0,0 +1,14 @@ + +-------------------------------- +-- @module EventListenerTouchOneByOne +-------------------------------- +-- @function [parent=#EventListenerTouchOneByOne] isSwallowTouches +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#EventListenerTouchOneByOne] setSwallowTouches +-- @param self +-- @param #bool bool + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventMouse.lua b/cocos/scripting/lua-bindings/auto/api/EventMouse.lua new file mode 100644 index 0000000000..39c7e628d9 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventMouse.lua @@ -0,0 +1,51 @@ + +-------------------------------- +-- @module EventMouse +-------------------------------- +-- @function [parent=#EventMouse] getMouseButton +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#EventMouse] setScrollData +-- @param self +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#EventMouse] setMouseButton +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#EventMouse] getScrollY +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#EventMouse] getScrollX +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#EventMouse] getCursorX +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#EventMouse] getCursorY +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#EventMouse] setCursorPosition +-- @param self +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#EventMouse] EventMouse +-- @param self +-- @param #cc.EventMouse::MouseEventType mouseeventtype + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/EventTouch.lua b/cocos/scripting/lua-bindings/auto/api/EventTouch.lua new file mode 100644 index 0000000000..15a0232910 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/EventTouch.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module EventTouch +-------------------------------- +-- @function [parent=#EventTouch] getEventCode +-- @param self +-- @return EventTouch::EventCode#EventTouch::EventCode ret (return value: cc.EventTouch::EventCode) + +-------------------------------- +-- @function [parent=#EventTouch] setEventCode +-- @param self +-- @param #cc.EventTouch::EventCode eventcode + +-------------------------------- +-- @function [parent=#EventTouch] EventTouch +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FadeIn.lua b/cocos/scripting/lua-bindings/auto/api/FadeIn.lua new file mode 100644 index 0000000000..7bac39c98a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FadeIn.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module FadeIn +-------------------------------- +-- @function [parent=#FadeIn] setReverseAction +-- @param self +-- @param #cc.FadeTo fadeto + +-------------------------------- +-- @function [parent=#FadeIn] create +-- @param self +-- @param #float float +-- @return FadeIn#FadeIn ret (return value: cc.FadeIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FadeOut.lua b/cocos/scripting/lua-bindings/auto/api/FadeOut.lua new file mode 100644 index 0000000000..7d759c4d4e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FadeOut.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module FadeOut +-------------------------------- +-- @function [parent=#FadeOut] setReverseAction +-- @param self +-- @param #cc.FadeTo fadeto + +-------------------------------- +-- @function [parent=#FadeOut] create +-- @param self +-- @param #float float +-- @return FadeOut#FadeOut ret (return value: cc.FadeOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FadeOutBLTiles.lua b/cocos/scripting/lua-bindings/auto/api/FadeOutBLTiles.lua new file mode 100644 index 0000000000..69b458bbaf --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FadeOutBLTiles.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module FadeOutBLTiles +-------------------------------- +-- @function [parent=#FadeOutBLTiles] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @return FadeOutBLTiles#FadeOutBLTiles ret (return value: cc.FadeOutBLTiles) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FadeOutDownTiles.lua b/cocos/scripting/lua-bindings/auto/api/FadeOutDownTiles.lua new file mode 100644 index 0000000000..8ede2c7e06 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FadeOutDownTiles.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module FadeOutDownTiles +-------------------------------- +-- @function [parent=#FadeOutDownTiles] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @return FadeOutDownTiles#FadeOutDownTiles ret (return value: cc.FadeOutDownTiles) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FadeOutTRTiles.lua b/cocos/scripting/lua-bindings/auto/api/FadeOutTRTiles.lua new file mode 100644 index 0000000000..cf9961ddec --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FadeOutTRTiles.lua @@ -0,0 +1,34 @@ + +-------------------------------- +-- @module FadeOutTRTiles +-------------------------------- +-- @function [parent=#FadeOutTRTiles] turnOnTile +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#FadeOutTRTiles] turnOffTile +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#FadeOutTRTiles] transformTile +-- @param self +-- @param #point_table point +-- @param #float float + +-------------------------------- +-- @function [parent=#FadeOutTRTiles] testFunc +-- @param self +-- @param #size_table size +-- @param #float float +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#FadeOutTRTiles] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @return FadeOutTRTiles#FadeOutTRTiles ret (return value: cc.FadeOutTRTiles) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FadeOutUpTiles.lua b/cocos/scripting/lua-bindings/auto/api/FadeOutUpTiles.lua new file mode 100644 index 0000000000..595be3b7d1 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FadeOutUpTiles.lua @@ -0,0 +1,17 @@ + +-------------------------------- +-- @module FadeOutUpTiles +-------------------------------- +-- @function [parent=#FadeOutUpTiles] transformTile +-- @param self +-- @param #point_table point +-- @param #float float + +-------------------------------- +-- @function [parent=#FadeOutUpTiles] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @return FadeOutUpTiles#FadeOutUpTiles ret (return value: cc.FadeOutUpTiles) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FadeTo.lua b/cocos/scripting/lua-bindings/auto/api/FadeTo.lua new file mode 100644 index 0000000000..1a33af625e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FadeTo.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module FadeTo +-------------------------------- +-- @function [parent=#FadeTo] create +-- @param self +-- @param #float float +-- @param #unsigned char char +-- @return FadeTo#FadeTo ret (return value: cc.FadeTo) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FileUtils.lua b/cocos/scripting/lua-bindings/auto/api/FileUtils.lua new file mode 100644 index 0000000000..517dcd59f5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FileUtils.lua @@ -0,0 +1,102 @@ + +-------------------------------- +-- @module FileUtils +-------------------------------- +-- @function [parent=#FileUtils] fullPathForFilename +-- @param self +-- @param #string str +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#FileUtils] getStringFromFile +-- @param self +-- @param #string str +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#FileUtils] setFilenameLookupDictionary +-- @param self +-- @param #map_table map + +-------------------------------- +-- @function [parent=#FileUtils] isAbsolutePath +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#FileUtils] loadFilenameLookupDictionaryFromFile +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#FileUtils] isPopupNotify +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#FileUtils] getValueVectorFromFile +-- @param self +-- @param #string str +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#FileUtils] writeToFile +-- @param self +-- @param #map_table map +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#FileUtils] getValueMapFromFile +-- @param self +-- @param #string str +-- @return map_table#map_table ret (return value: map_table) + +-------------------------------- +-- @function [parent=#FileUtils] addSearchResolutionsOrder +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#FileUtils] addSearchPath +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#FileUtils] isFileExist +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#FileUtils] purgeCachedEntries +-- @param self + +-------------------------------- +-- @function [parent=#FileUtils] fullPathFromRelativeFile +-- @param self +-- @param #string str +-- @param #string str +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#FileUtils] setPopupNotify +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#FileUtils] getWritablePath +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#FileUtils] destroyInstance +-- @param self + +-------------------------------- +-- @function [parent=#FileUtils] getInstance +-- @param self +-- @return FileUtils#FileUtils ret (return value: cc.FileUtils) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FiniteTimeAction.lua b/cocos/scripting/lua-bindings/auto/api/FiniteTimeAction.lua new file mode 100644 index 0000000000..9b77997e1c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FiniteTimeAction.lua @@ -0,0 +1,14 @@ + +-------------------------------- +-- @module FiniteTimeAction +-------------------------------- +-- @function [parent=#FiniteTimeAction] setDuration +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#FiniteTimeAction] getDuration +-- @param self +-- @return float#float ret (return value: float) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FlipX.lua b/cocos/scripting/lua-bindings/auto/api/FlipX.lua new file mode 100644 index 0000000000..1d28d16969 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FlipX.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module FlipX +-------------------------------- +-- @function [parent=#FlipX] create +-- @param self +-- @param #bool bool +-- @return FlipX#FlipX ret (return value: cc.FlipX) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FlipX3D.lua b/cocos/scripting/lua-bindings/auto/api/FlipX3D.lua new file mode 100644 index 0000000000..736bd72e38 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FlipX3D.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module FlipX3D +-------------------------------- +-- @function [parent=#FlipX3D] create +-- @param self +-- @param #float float +-- @return FlipX3D#FlipX3D ret (return value: cc.FlipX3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FlipY.lua b/cocos/scripting/lua-bindings/auto/api/FlipY.lua new file mode 100644 index 0000000000..a26ae24a69 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FlipY.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module FlipY +-------------------------------- +-- @function [parent=#FlipY] create +-- @param self +-- @param #bool bool +-- @return FlipY#FlipY ret (return value: cc.FlipY) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FlipY3D.lua b/cocos/scripting/lua-bindings/auto/api/FlipY3D.lua new file mode 100644 index 0000000000..5381e36cf0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FlipY3D.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module FlipY3D +-------------------------------- +-- @function [parent=#FlipY3D] create +-- @param self +-- @param #float float +-- @return FlipY3D#FlipY3D ret (return value: cc.FlipY3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Follow.lua b/cocos/scripting/lua-bindings/auto/api/Follow.lua new file mode 100644 index 0000000000..0e7abc3220 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Follow.lua @@ -0,0 +1,21 @@ + +-------------------------------- +-- @module Follow +-------------------------------- +-- @function [parent=#Follow] setBoudarySet +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Follow] isBoundarySet +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Follow] create +-- @param self +-- @param #cc.Node node +-- @param #rect_table rect +-- @return Follow#Follow ret (return value: cc.Follow) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/FrameData.lua b/cocos/scripting/lua-bindings/auto/api/FrameData.lua new file mode 100644 index 0000000000..c23f2f372a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/FrameData.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module FrameData +-------------------------------- +-- @function [parent=#FrameData] copy +-- @param self +-- @param #ccs.BaseData basedata + +-------------------------------- +-- @function [parent=#FrameData] create +-- @param self +-- @return FrameData#FrameData ret (return value: ccs.FrameData) + +-------------------------------- +-- @function [parent=#FrameData] FrameData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/GLProgram.lua b/cocos/scripting/lua-bindings/auto/api/GLProgram.lua new file mode 100644 index 0000000000..9a1cb0d159 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/GLProgram.lua @@ -0,0 +1,154 @@ + +-------------------------------- +-- @module GLProgram +-------------------------------- +-- @function [parent=#GLProgram] getFragmentShaderLog +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#GLProgram] initWithByteArrays +-- @param self +-- @param #char char +-- @param #char char +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWithMatrix4fv +-- @param self +-- @param #int int +-- @param #float float +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#GLProgram] initWithFilenames +-- @param self +-- @param #string str +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#GLProgram] getUniformLocationForName +-- @param self +-- @param #char char +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#GLProgram] use +-- @param self + +-------------------------------- +-- @function [parent=#GLProgram] getVertexShaderLog +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- overload function: setUniformsForBuiltins(kmMat4) +-- +-- overload function: setUniformsForBuiltins() +-- +-- @function [parent=#GLProgram] setUniformsForBuiltins +-- @param self +-- @param #kmMat4 kmmat4 + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWith3i +-- @param self +-- @param #int int +-- @param #int int +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWith3iv +-- @param self +-- @param #int int +-- @param #int int +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#GLProgram] updateUniforms +-- @param self + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWith4iv +-- @param self +-- @param #int int +-- @param #int int +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#GLProgram] getUniformLocation +-- @param self +-- @param #char char +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWith1i +-- @param self +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWith2iv +-- @param self +-- @param #int int +-- @param #int int +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWithMatrix3fv +-- @param self +-- @param #int int +-- @param #float float +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#GLProgram] reset +-- @param self + +-------------------------------- +-- @function [parent=#GLProgram] bindAttribLocation +-- @param self +-- @param #char char +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#GLProgram] getAttribLocation +-- @param self +-- @param #char char +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWithMatrix2fv +-- @param self +-- @param #int int +-- @param #float float +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWith4i +-- @param self +-- @param #int int +-- @param #int int +-- @param #int int +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#GLProgram] link +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#GLProgram] setUniformLocationWith2i +-- @param self +-- @param #int int +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#GLProgram] GLProgram +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/GLView.lua b/cocos/scripting/lua-bindings/auto/api/GLView.lua new file mode 100644 index 0000000000..181ad10c7f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/GLView.lua @@ -0,0 +1,34 @@ + +-------------------------------- +-- @module GLView +-------------------------------- +-- @function [parent=#GLView] setIMEKeyboardState +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#GLView] isOpenGLReady +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#GLView] createWithRect +-- @param self +-- @param #string str +-- @param #rect_table rect +-- @param #float float +-- @return GLView#GLView ret (return value: cc.GLView) + +-------------------------------- +-- @function [parent=#GLView] create +-- @param self +-- @param #string str +-- @return GLView#GLView ret (return value: cc.GLView) + +-------------------------------- +-- @function [parent=#GLView] createWithFullScreen +-- @param self +-- @param #string str +-- @return GLView#GLView ret (return value: cc.GLView) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/GLViewProtocol.lua b/cocos/scripting/lua-bindings/auto/api/GLViewProtocol.lua new file mode 100644 index 0000000000..1247d08bf6 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/GLViewProtocol.lua @@ -0,0 +1,120 @@ + +-------------------------------- +-- @module GLViewProtocol +-------------------------------- +-- @function [parent=#GLViewProtocol] setFrameSize +-- @param self +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#GLViewProtocol] getViewPortRect +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#GLViewProtocol] setIMEKeyboardState +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#GLViewProtocol] setScissorInPoints +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#GLViewProtocol] getViewName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#GLViewProtocol] isOpenGLReady +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#GLViewProtocol] end +-- @param self + +-------------------------------- +-- @function [parent=#GLViewProtocol] getScaleY +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#GLViewProtocol] getScaleX +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#GLViewProtocol] getVisibleOrigin +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#GLViewProtocol] getFrameSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#GLViewProtocol] getDesignResolutionSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#GLViewProtocol] pollInputEvents +-- @param self + +-------------------------------- +-- @function [parent=#GLViewProtocol] swapBuffers +-- @param self + +-------------------------------- +-- @function [parent=#GLViewProtocol] setDesignResolutionSize +-- @param self +-- @param #float float +-- @param #float float +-- @param #ResolutionPolicy resolutionpolicy + +-------------------------------- +-- @function [parent=#GLViewProtocol] getResolutionPolicy +-- @param self +-- @return ResolutionPolicy#ResolutionPolicy ret (return value: ResolutionPolicy) + +-------------------------------- +-- @function [parent=#GLViewProtocol] setViewPortInPoints +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#GLViewProtocol] getScissorRect +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#GLViewProtocol] setViewName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#GLViewProtocol] getVisibleRect +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#GLViewProtocol] getVisibleSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#GLViewProtocol] isScissorEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/GUIReader.lua b/cocos/scripting/lua-bindings/auto/api/GUIReader.lua new file mode 100644 index 0000000000..eaf01fd1b9 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/GUIReader.lua @@ -0,0 +1,30 @@ + +-------------------------------- +-- @module GUIReader +-------------------------------- +-- @function [parent=#GUIReader] widgetFromJsonFile +-- @param self +-- @param #char char +-- @return Widget#Widget ret (return value: ccui.Widget) + +-------------------------------- +-- @function [parent=#GUIReader] getFilePath +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#GUIReader] getVersionInteger +-- @param self +-- @param #char char +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#GUIReader] destroyInstance +-- @param self + +-------------------------------- +-- @function [parent=#GUIReader] getInstance +-- @param self +-- @return GUIReader#GUIReader ret (return value: ccs.GUIReader) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Grid3D.lua b/cocos/scripting/lua-bindings/auto/api/Grid3D.lua new file mode 100644 index 0000000000..aa15b0bf83 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Grid3D.lua @@ -0,0 +1,20 @@ + +-------------------------------- +-- @module Grid3D +-------------------------------- +-- overload function: create(size_table) +-- +-- overload function: create(size_table, cc.Texture2D, bool) +-- +-- @function [parent=#Grid3D] create +-- @param self +-- @param #size_table size +-- @param #cc.Texture2D texture2d +-- @param #bool bool +-- @return Grid3D#Grid3D ret (retunr value: cc.Grid3D) + +-------------------------------- +-- @function [parent=#Grid3D] Grid3D +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Grid3DAction.lua b/cocos/scripting/lua-bindings/auto/api/Grid3DAction.lua new file mode 100644 index 0000000000..df754d0dfd --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Grid3DAction.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module Grid3DAction +-------------------------------- +-- @function [parent=#Grid3DAction] getGrid +-- @param self +-- @return GridBase#GridBase ret (return value: cc.GridBase) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/GridAction.lua b/cocos/scripting/lua-bindings/auto/api/GridAction.lua new file mode 100644 index 0000000000..afa48c5fed --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/GridAction.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module GridAction +-------------------------------- +-- @function [parent=#GridAction] getGrid +-- @param self +-- @return GridBase#GridBase ret (return value: cc.GridBase) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/GridBase.lua b/cocos/scripting/lua-bindings/auto/api/GridBase.lua new file mode 100644 index 0000000000..bb9f3edb4a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/GridBase.lua @@ -0,0 +1,103 @@ + +-------------------------------- +-- @module GridBase +-------------------------------- +-- @function [parent=#GridBase] setGridSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#GridBase] calculateVertexPoints +-- @param self + +-------------------------------- +-- @function [parent=#GridBase] afterDraw +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#GridBase] beforeDraw +-- @param self + +-------------------------------- +-- @function [parent=#GridBase] isTextureFlipped +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#GridBase] getGridSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#GridBase] getStep +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#GridBase] set2DProjection +-- @param self + +-------------------------------- +-- @function [parent=#GridBase] setStep +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#GridBase] setTextureFlipped +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#GridBase] blit +-- @param self + +-------------------------------- +-- @function [parent=#GridBase] setActive +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#GridBase] getReuseGrid +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- overload function: initWithSize(size_table) +-- +-- overload function: initWithSize(size_table, cc.Texture2D, bool) +-- +-- @function [parent=#GridBase] initWithSize +-- @param self +-- @param #size_table size +-- @param #cc.Texture2D texture2d +-- @param #bool bool +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#GridBase] setReuseGrid +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#GridBase] isActive +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#GridBase] reuse +-- @param self + +-------------------------------- +-- overload function: create(size_table) +-- +-- overload function: create(size_table, cc.Texture2D, bool) +-- +-- @function [parent=#GridBase] create +-- @param self +-- @param #size_table size +-- @param #cc.Texture2D texture2d +-- @param #bool bool +-- @return GridBase#GridBase ret (retunr value: cc.GridBase) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Helper.lua b/cocos/scripting/lua-bindings/auto/api/Helper.lua new file mode 100644 index 0000000000..565e5922a5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Helper.lua @@ -0,0 +1,32 @@ + +-------------------------------- +-- @module Helper +-------------------------------- +-- @function [parent=#Helper] seekActionWidgetByActionTag +-- @param self +-- @param #ccui.Widget widget +-- @param #int int +-- @return Widget#Widget ret (return value: ccui.Widget) + +-------------------------------- +-- @function [parent=#Helper] seekWidgetByTag +-- @param self +-- @param #ccui.Widget widget +-- @param #int int +-- @return Widget#Widget ret (return value: ccui.Widget) + +-------------------------------- +-- @function [parent=#Helper] seekWidgetByRelativeName +-- @param self +-- @param #ccui.Widget widget +-- @param #char char +-- @return Widget#Widget ret (return value: ccui.Widget) + +-------------------------------- +-- @function [parent=#Helper] seekWidgetByName +-- @param self +-- @param #ccui.Widget widget +-- @param #char char +-- @return Widget#Widget ret (return value: ccui.Widget) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Hide.lua b/cocos/scripting/lua-bindings/auto/api/Hide.lua new file mode 100644 index 0000000000..7baf4c3784 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Hide.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module Hide +-------------------------------- +-- @function [parent=#Hide] create +-- @param self +-- @return Hide#Hide ret (return value: cc.Hide) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Image.lua b/cocos/scripting/lua-bindings/auto/api/Image.lua new file mode 100644 index 0000000000..55e397fc9f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Image.lua @@ -0,0 +1,86 @@ + +-------------------------------- +-- @module Image +-------------------------------- +-- @function [parent=#Image] hasPremultipliedAlpha +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Image] getDataLen +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#Image] saveToFile +-- @param self +-- @param #string str +-- @param #bool bool +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Image] getBitPerPixel +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Image] hasAlpha +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Image] isCompressed +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Image] getHeight +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Image] initWithImageFile +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Image] getWidth +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Image] isPremultipliedAlpha +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Image] getFileType +-- @param self +-- @return Image::Format#Image::Format ret (return value: cc.Image::Format) + +-------------------------------- +-- @function [parent=#Image] getNumberOfMipmaps +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Image] getRenderFormat +-- @param self +-- @return Texture2D::PixelFormat#Texture2D::PixelFormat ret (return value: cc.Texture2D::PixelFormat) + +-------------------------------- +-- @function [parent=#Image] getData +-- @param self +-- @return unsigned char#unsigned char ret (return value: unsigned char) + +-------------------------------- +-- @function [parent=#Image] getMipmaps +-- @param self +-- @return _MipmapInfo#_MipmapInfo ret (return value: cc._MipmapInfo) + +-------------------------------- +-- @function [parent=#Image] Image +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ImageView.lua b/cocos/scripting/lua-bindings/auto/api/ImageView.lua new file mode 100644 index 0000000000..58e10a2206 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ImageView.lua @@ -0,0 +1,49 @@ + +-------------------------------- +-- @module ImageView +-------------------------------- +-- @function [parent=#ImageView] loadTexture +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#ImageView] setScale9Enabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ImageView] setTextureRect +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#ImageView] setCapInsets +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#ImageView] getCapInsets +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#ImageView] isScale9Enabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ImageView] create +-- @param self +-- @return ImageView#ImageView ret (return value: ccui.ImageView) + +-------------------------------- +-- @function [parent=#ImageView] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#ImageView] ImageView +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/JumpBy.lua b/cocos/scripting/lua-bindings/auto/api/JumpBy.lua new file mode 100644 index 0000000000..b42df90e34 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/JumpBy.lua @@ -0,0 +1,13 @@ + +-------------------------------- +-- @module JumpBy +-------------------------------- +-- @function [parent=#JumpBy] create +-- @param self +-- @param #float float +-- @param #point_table point +-- @param #float float +-- @param #int int +-- @return JumpBy#JumpBy ret (return value: cc.JumpBy) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/JumpTiles3D.lua b/cocos/scripting/lua-bindings/auto/api/JumpTiles3D.lua new file mode 100644 index 0000000000..ca146738d9 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/JumpTiles3D.lua @@ -0,0 +1,33 @@ + +-------------------------------- +-- @module JumpTiles3D +-------------------------------- +-- @function [parent=#JumpTiles3D] getAmplitudeRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#JumpTiles3D] setAmplitude +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#JumpTiles3D] setAmplitudeRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#JumpTiles3D] getAmplitude +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#JumpTiles3D] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #unsigned int int +-- @param #float float +-- @return JumpTiles3D#JumpTiles3D ret (return value: cc.JumpTiles3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/JumpTo.lua b/cocos/scripting/lua-bindings/auto/api/JumpTo.lua new file mode 100644 index 0000000000..5071658f89 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/JumpTo.lua @@ -0,0 +1,13 @@ + +-------------------------------- +-- @module JumpTo +-------------------------------- +-- @function [parent=#JumpTo] create +-- @param self +-- @param #float float +-- @param #point_table point +-- @param #float float +-- @param #int int +-- @return JumpTo#JumpTo ret (return value: cc.JumpTo) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Label.lua b/cocos/scripting/lua-bindings/auto/api/Label.lua new file mode 100644 index 0000000000..7448bfb204 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Label.lua @@ -0,0 +1,185 @@ + +-------------------------------- +-- @module Label +-------------------------------- +-- @function [parent=#Label] enableShadow +-- @param self + +-------------------------------- +-- @function [parent=#Label] setDimensions +-- @param self +-- @param #unsigned int int +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#Label] getString +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#Label] disableEffect +-- @param self + +-------------------------------- +-- @function [parent=#Label] getWidth +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#Label] getCommonLineHeight +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Label] setWidth +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#Label] getMaxLineWidth +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#Label] getHorizontalAlignment +-- @param self +-- @return TextHAlignment#TextHAlignment ret (return value: cc.TextHAlignment) + +-------------------------------- +-- @function [parent=#Label] setString +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#Label] getHeight +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#Label] setBMFontFilePath +-- @param self +-- @param #string str +-- @param #point_table point +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Label] getStringLength +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Label] setLineBreakWithoutSpace +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Label] getStringNumLines +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Label] enableOutline +-- @param self +-- @param #color4B_table color4b +-- @param #int int + +-------------------------------- +-- overload function: setCharMap(cc.Texture2D, int, int, int) +-- +-- overload function: setCharMap(string, int, int, int) +-- +-- overload function: setCharMap(string) +-- +-- @function [parent=#Label] setCharMap +-- @param self +-- @param #string str +-- @param #int int +-- @param #int int +-- @param #int int +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#Label] setMaxLineWidth +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#Label] setVerticalAlignment +-- @param self +-- @param #cc.TextVAlignment textvalignment +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Label] getVerticalAlignment +-- @param self +-- @return TextVAlignment#TextVAlignment ret (return value: cc.TextVAlignment) + +-------------------------------- +-- @function [parent=#Label] setHeight +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#Label] enableGlow +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#Label] getLetter +-- @param self +-- @param #int int +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#Label] getTextAlignment +-- @param self +-- @return TextHAlignment#TextHAlignment ret (return value: cc.TextHAlignment) + +-------------------------------- +-- @function [parent=#Label] setHorizontalAlignment +-- @param self +-- @param #cc.TextHAlignment texthalignment +-- @param #bool bool + +-------------------------------- +-- overload function: setAlignment(cc.TextHAlignment, cc.TextVAlignment, bool) +-- +-- overload function: setAlignment(cc.TextHAlignment, bool) +-- +-- @function [parent=#Label] setAlignment +-- @param self +-- @param #cc.TextHAlignment texthalignment +-- @param #cc.TextVAlignment textvalignment +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Label] createWithBMFont +-- @param self +-- @param #string str +-- @param #string str +-- @param #cc.TextHAlignment texthalignment +-- @param #int int +-- @param #point_table point +-- @return Label#Label ret (return value: cc.Label) + +-------------------------------- +-- @function [parent=#Label] create +-- @param self +-- @return Label#Label ret (return value: cc.Label) + +-------------------------------- +-- overload function: createWithCharMap(cc.Texture2D, int, int, int) +-- +-- overload function: createWithCharMap(string, int, int, int) +-- +-- overload function: createWithCharMap(string) +-- +-- @function [parent=#Label] createWithCharMap +-- @param self +-- @param #string str +-- @param #int int +-- @param #int int +-- @param #int int +-- @return Label#Label ret (retunr value: cc.Label) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/LabelAtlas.lua b/cocos/scripting/lua-bindings/auto/api/LabelAtlas.lua new file mode 100644 index 0000000000..95a8167be6 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/LabelAtlas.lua @@ -0,0 +1,50 @@ + +-------------------------------- +-- @module LabelAtlas +-------------------------------- +-- @function [parent=#LabelAtlas] setString +-- @param self +-- @param #string str + +-------------------------------- +-- overload function: initWithString(string, string) +-- +-- overload function: initWithString(string, string, int, int, int) +-- +-- overload function: initWithString(string, cc.Texture2D, int, int, int) +-- +-- @function [parent=#LabelAtlas] initWithString +-- @param self +-- @param #string str +-- @param #cc.Texture2D texture2d +-- @param #int int +-- @param #int int +-- @param #int int +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#LabelAtlas] updateAtlasValues +-- @param self + +-------------------------------- +-- @function [parent=#LabelAtlas] getString +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- overload function: create(string, string, int, int, int) +-- +-- overload function: create() +-- +-- overload function: create(string, string) +-- +-- @function [parent=#LabelAtlas] create +-- @param self +-- @param #string str +-- @param #string str +-- @param #int int +-- @param #int int +-- @param #int int +-- @return LabelAtlas#LabelAtlas ret (retunr value: cc.LabelAtlas) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/LabelBMFont.lua b/cocos/scripting/lua-bindings/auto/api/LabelBMFont.lua new file mode 100644 index 0000000000..3dd36dad1a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/LabelBMFont.lua @@ -0,0 +1,94 @@ + +-------------------------------- +-- @module LabelBMFont +-------------------------------- +-- @function [parent=#LabelBMFont] setLineBreakWithoutSpace +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#LabelBMFont] getBlendFunc +-- @param self +-- @return BlendFunc#BlendFunc ret (return value: cc.BlendFunc) + +-------------------------------- +-- @function [parent=#LabelBMFont] isOpacityModifyRGB +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#LabelBMFont] getLetter +-- @param self +-- @param #int int +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#LabelBMFont] getString +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#LabelBMFont] setBlendFunc +-- @param self +-- @param #cc.BlendFunc blendfunc + +-------------------------------- +-- @function [parent=#LabelBMFont] setString +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#LabelBMFont] initWithString +-- @param self +-- @param #string str +-- @param #string str +-- @param #float float +-- @param #cc.TextHAlignment texthalignment +-- @param #point_table point +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#LabelBMFont] setOpacityModifyRGB +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#LabelBMFont] getFntFile +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#LabelBMFont] setFntFile +-- @param self +-- @param #string str +-- @param #point_table point + +-------------------------------- +-- @function [parent=#LabelBMFont] setAlignment +-- @param self +-- @param #cc.TextHAlignment texthalignment + +-------------------------------- +-- @function [parent=#LabelBMFont] setWidth +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: create() +-- +-- overload function: create(string, string, float, cc.TextHAlignment, point_table) +-- +-- @function [parent=#LabelBMFont] create +-- @param self +-- @param #string str +-- @param #string str +-- @param #float float +-- @param #cc.TextHAlignment texthalignment +-- @param #point_table point +-- @return LabelBMFont#LabelBMFont ret (retunr value: cc.LabelBMFont) + +-------------------------------- +-- @function [parent=#LabelBMFont] LabelBMFont +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/LabelTTF.lua b/cocos/scripting/lua-bindings/auto/api/LabelTTF.lua new file mode 100644 index 0000000000..87a85a52e5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/LabelTTF.lua @@ -0,0 +1,162 @@ + +-------------------------------- +-- @module LabelTTF +-------------------------------- +-- @function [parent=#LabelTTF] enableShadow +-- @param self +-- @param #size_table size +-- @param #float float +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#LabelTTF] setDimensions +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#LabelTTF] getFontSize +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#LabelTTF] getString +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#LabelTTF] setTextDefinition +-- @param self +-- @param #cc.FontDefinition fontdefinition + +-------------------------------- +-- @function [parent=#LabelTTF] setFontName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#LabelTTF] getHorizontalAlignment +-- @param self +-- @return TextHAlignment#TextHAlignment ret (return value: cc.TextHAlignment) + +-------------------------------- +-- @function [parent=#LabelTTF] initWithStringAndTextDefinition +-- @param self +-- @param #string str +-- @param #cc.FontDefinition fontdefinition +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#LabelTTF] setString +-- @param self +-- @param #string str + +-------------------------------- +-- overload function: initWithString(string, string, float, size_table, cc.TextHAlignment) +-- +-- overload function: initWithString(string, string, float) +-- +-- overload function: initWithString(string, string, float, size_table, cc.TextHAlignment, cc.TextVAlignment) +-- +-- @function [parent=#LabelTTF] initWithString +-- @param self +-- @param #string str +-- @param #string str +-- @param #float float +-- @param #size_table size +-- @param #cc.TextHAlignment texthalignment +-- @param #cc.TextVAlignment textvalignment +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#LabelTTF] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#LabelTTF] setFontFillColor +-- @param self +-- @param #color3B_table color3b +-- @param #bool bool + +-------------------------------- +-- @function [parent=#LabelTTF] enableStroke +-- @param self +-- @param #color3B_table color3b +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#LabelTTF] getDimensions +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#LabelTTF] setVerticalAlignment +-- @param self +-- @param #cc.TextVAlignment textvalignment + +-------------------------------- +-- @function [parent=#LabelTTF] setFontSize +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#LabelTTF] getVerticalAlignment +-- @param self +-- @return TextVAlignment#TextVAlignment ret (return value: cc.TextVAlignment) + +-------------------------------- +-- @function [parent=#LabelTTF] getTextDefinition +-- @param self +-- @return FontDefinition#FontDefinition ret (return value: cc.FontDefinition) + +-------------------------------- +-- @function [parent=#LabelTTF] getFontName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#LabelTTF] setHorizontalAlignment +-- @param self +-- @param #cc.TextHAlignment texthalignment + +-------------------------------- +-- @function [parent=#LabelTTF] disableShadow +-- @param self + +-------------------------------- +-- @function [parent=#LabelTTF] disableStroke +-- @param self + +-------------------------------- +-- overload function: create(string, string, float, size_table, cc.TextHAlignment) +-- +-- overload function: create(string, string, float) +-- +-- overload function: create(string, string, float, size_table, cc.TextHAlignment, cc.TextVAlignment) +-- +-- overload function: create() +-- +-- @function [parent=#LabelTTF] create +-- @param self +-- @param #string str +-- @param #string str +-- @param #float float +-- @param #size_table size +-- @param #cc.TextHAlignment texthalignment +-- @param #cc.TextVAlignment textvalignment +-- @return LabelTTF#LabelTTF ret (retunr value: cc.LabelTTF) + +-------------------------------- +-- @function [parent=#LabelTTF] createWithFontDefinition +-- @param self +-- @param #string str +-- @param #cc.FontDefinition fontdefinition +-- @return LabelTTF#LabelTTF ret (return value: cc.LabelTTF) + +-------------------------------- +-- @function [parent=#LabelTTF] LabelTTF +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Layer.lua b/cocos/scripting/lua-bindings/auto/api/Layer.lua new file mode 100644 index 0000000000..dc09c20df3 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Layer.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module Layer +-------------------------------- +-- @function [parent=#Layer] create +-- @param self +-- @return Layer#Layer ret (return value: cc.Layer) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/LayerColor.lua b/cocos/scripting/lua-bindings/auto/api/LayerColor.lua new file mode 100644 index 0000000000..53648a1237 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/LayerColor.lua @@ -0,0 +1,34 @@ + +-------------------------------- +-- @module LayerColor +-------------------------------- +-- @function [parent=#LayerColor] changeWidthAndHeight +-- @param self +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#LayerColor] changeHeight +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#LayerColor] changeWidth +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: create(color4B_table, float, float) +-- +-- overload function: create() +-- +-- overload function: create(color4B_table) +-- +-- @function [parent=#LayerColor] create +-- @param self +-- @param #color4B_table color4b +-- @param #float float +-- @param #float float +-- @return LayerColor#LayerColor ret (retunr value: cc.LayerColor) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/LayerGradient.lua b/cocos/scripting/lua-bindings/auto/api/LayerGradient.lua new file mode 100644 index 0000000000..196478762a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/LayerGradient.lua @@ -0,0 +1,92 @@ + +-------------------------------- +-- @module LayerGradient +-------------------------------- +-- @function [parent=#LayerGradient] getStartColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#LayerGradient] isCompressedInterpolation +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#LayerGradient] getStartOpacity +-- @param self +-- @return unsigned char#unsigned char ret (return value: unsigned char) + +-------------------------------- +-- @function [parent=#LayerGradient] setVector +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#LayerGradient] setStartOpacity +-- @param self +-- @param #unsigned char char + +-------------------------------- +-- @function [parent=#LayerGradient] setCompressedInterpolation +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#LayerGradient] setEndOpacity +-- @param self +-- @param #unsigned char char + +-------------------------------- +-- @function [parent=#LayerGradient] getVector +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- overload function: initWithColor(color4B_table, color4B_table) +-- +-- overload function: initWithColor() +-- +-- overload function: initWithColor(color4B_table, color4B_table, point_table) +-- +-- @function [parent=#LayerGradient] initWithColor +-- @param self +-- @param #color4B_table color4b +-- @param #color4B_table color4b +-- @param #point_table point +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#LayerGradient] setEndColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#LayerGradient] getEndColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#LayerGradient] getEndOpacity +-- @param self +-- @return unsigned char#unsigned char ret (return value: unsigned char) + +-------------------------------- +-- @function [parent=#LayerGradient] setStartColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- overload function: create(color4B_table, color4B_table) +-- +-- overload function: create() +-- +-- overload function: create(color4B_table, color4B_table, point_table) +-- +-- @function [parent=#LayerGradient] create +-- @param self +-- @param #color4B_table color4b +-- @param #color4B_table color4b +-- @param #point_table point +-- @return LayerGradient#LayerGradient ret (retunr value: cc.LayerGradient) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/LayerMultiplex.lua b/cocos/scripting/lua-bindings/auto/api/LayerMultiplex.lua new file mode 100644 index 0000000000..907636c0aa --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/LayerMultiplex.lua @@ -0,0 +1,19 @@ + +-------------------------------- +-- @module LayerMultiplex +-------------------------------- +-- @function [parent=#LayerMultiplex] switchToAndReleaseMe +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#LayerMultiplex] addLayer +-- @param self +-- @param #cc.Layer layer + +-------------------------------- +-- @function [parent=#LayerMultiplex] switchTo +-- @param self +-- @param #int int + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Layout.lua b/cocos/scripting/lua-bindings/auto/api/Layout.lua new file mode 100644 index 0000000000..bd313ad92b --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Layout.lua @@ -0,0 +1,168 @@ + +-------------------------------- +-- @module Layout +-------------------------------- +-- @function [parent=#Layout] setBackGroundColorVector +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#Layout] setClippingType +-- @param self +-- @param #ccui.LayoutClippingType layoutclippingtype + +-------------------------------- +-- @function [parent=#Layout] setBackGroundColorType +-- @param self +-- @param #ccui.LayoutBackGroundColorType layoutbackgroundcolortype + +-------------------------------- +-- @function [parent=#Layout] setBackGroundImageColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#Layout] getBackGroundColorVector +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Layout] getClippingType +-- @param self +-- @return LayoutClippingType#LayoutClippingType ret (return value: ccui.LayoutClippingType) + +-------------------------------- +-- @function [parent=#Layout] removeBackGroundImage +-- @param self + +-------------------------------- +-- @function [parent=#Layout] getBackGroundColorOpacity +-- @param self +-- @return unsigned char#unsigned char ret (return value: unsigned char) + +-------------------------------- +-- @function [parent=#Layout] isClippingEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Layout] setBackGroundImageOpacity +-- @param self +-- @param #unsigned char char + +-------------------------------- +-- @function [parent=#Layout] setBackGroundImage +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- overload function: setBackGroundColor(color3B_table, color3B_table) +-- +-- overload function: setBackGroundColor(color3B_table) +-- +-- @function [parent=#Layout] setBackGroundColor +-- @param self +-- @param #color3B_table color3b +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#Layout] requestDoLayout +-- @param self + +-------------------------------- +-- @function [parent=#Layout] getBackGroundImageCapInsets +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#Layout] getBackGroundColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#Layout] setClippingEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Layout] getBackGroundImageColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#Layout] isBackGroundImageScale9Enabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Layout] getBackGroundColorType +-- @param self +-- @return LayoutBackGroundColorType#LayoutBackGroundColorType ret (return value: ccui.LayoutBackGroundColorType) + +-------------------------------- +-- @function [parent=#Layout] getBackGroundEndColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#Layout] setBackGroundColorOpacity +-- @param self +-- @param #unsigned char char + +-------------------------------- +-- @function [parent=#Layout] getBackGroundImageOpacity +-- @param self +-- @return unsigned char#unsigned char ret (return value: unsigned char) + +-------------------------------- +-- @function [parent=#Layout] setBackGroundImageCapInsets +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Layout] getBackGroundImageTextureSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Layout] getLayoutType +-- @param self +-- @return LayoutType#LayoutType ret (return value: ccui.LayoutType) + +-------------------------------- +-- @function [parent=#Layout] getBackGroundStartColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#Layout] hitTest +-- @param self +-- @param #point_table point +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Layout] setBackGroundImageScale9Enabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Layout] setLayoutType +-- @param self +-- @param #ccui.LayoutType layouttype + +-------------------------------- +-- @function [parent=#Layout] create +-- @param self +-- @return Layout#Layout ret (return value: ccui.Layout) + +-------------------------------- +-- @function [parent=#Layout] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#Layout] Layout +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/LayoutParameter.lua b/cocos/scripting/lua-bindings/auto/api/LayoutParameter.lua new file mode 100644 index 0000000000..55cb2ccd17 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/LayoutParameter.lua @@ -0,0 +1,33 @@ + +-------------------------------- +-- @module LayoutParameter +-------------------------------- +-- @function [parent=#LayoutParameter] clone +-- @param self +-- @return LayoutParameter#LayoutParameter ret (return value: ccui.LayoutParameter) + +-------------------------------- +-- @function [parent=#LayoutParameter] getLayoutType +-- @param self +-- @return LayoutParameterType#LayoutParameterType ret (return value: ccui.LayoutParameterType) + +-------------------------------- +-- @function [parent=#LayoutParameter] createCloneInstance +-- @param self +-- @return LayoutParameter#LayoutParameter ret (return value: ccui.LayoutParameter) + +-------------------------------- +-- @function [parent=#LayoutParameter] copyProperties +-- @param self +-- @param #ccui.LayoutParameter layoutparameter + +-------------------------------- +-- @function [parent=#LayoutParameter] create +-- @param self +-- @return LayoutParameter#LayoutParameter ret (return value: ccui.LayoutParameter) + +-------------------------------- +-- @function [parent=#LayoutParameter] LayoutParameter +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Lens3D.lua b/cocos/scripting/lua-bindings/auto/api/Lens3D.lua new file mode 100644 index 0000000000..c908c4b489 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Lens3D.lua @@ -0,0 +1,38 @@ + +-------------------------------- +-- @module Lens3D +-------------------------------- +-- @function [parent=#Lens3D] setPosition +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#Lens3D] setConcave +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Lens3D] setLensEffect +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Lens3D] getPosition +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Lens3D] getLensEffect +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Lens3D] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #point_table point +-- @param #float float +-- @return Lens3D#Lens3D ret (return value: cc.Lens3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/LinearLayoutParameter.lua b/cocos/scripting/lua-bindings/auto/api/LinearLayoutParameter.lua new file mode 100644 index 0000000000..c1509ba4f1 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/LinearLayoutParameter.lua @@ -0,0 +1,23 @@ + +-------------------------------- +-- @module LinearLayoutParameter +-------------------------------- +-- @function [parent=#LinearLayoutParameter] setGravity +-- @param self +-- @param #ccui.LinearGravity lineargravity + +-------------------------------- +-- @function [parent=#LinearLayoutParameter] getGravity +-- @param self +-- @return LinearGravity#LinearGravity ret (return value: ccui.LinearGravity) + +-------------------------------- +-- @function [parent=#LinearLayoutParameter] create +-- @param self +-- @return LinearLayoutParameter#LinearLayoutParameter ret (return value: ccui.LinearLayoutParameter) + +-------------------------------- +-- @function [parent=#LinearLayoutParameter] LinearLayoutParameter +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Liquid.lua b/cocos/scripting/lua-bindings/auto/api/Liquid.lua new file mode 100644 index 0000000000..ea120cd160 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Liquid.lua @@ -0,0 +1,37 @@ + +-------------------------------- +-- @module Liquid +-------------------------------- +-- @function [parent=#Liquid] getAmplitudeRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Liquid] setAmplitude +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Liquid] getAmplitude +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Liquid] setAmplitudeRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Liquid] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #unsigned int int +-- @param #float float +-- @return Liquid#Liquid ret (return value: cc.Liquid) + +-------------------------------- +-- @function [parent=#Liquid] Liquid +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ListView.lua b/cocos/scripting/lua-bindings/auto/api/ListView.lua new file mode 100644 index 0000000000..834e28d8a7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ListView.lua @@ -0,0 +1,101 @@ + +-------------------------------- +-- @module ListView +-------------------------------- +-- @function [parent=#ListView] getIndex +-- @param self +-- @param #ccui.Widget widget +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#ListView] removeAllItems +-- @param self + +-------------------------------- +-- @function [parent=#ListView] setGravity +-- @param self +-- @param #ccui.ListViewGravity listviewgravity + +-------------------------------- +-- @function [parent=#ListView] pushBackCustomItem +-- @param self +-- @param #ccui.Widget widget + +-------------------------------- +-- @function [parent=#ListView] getItems +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#ListView] removeItem +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#ListView] getCurSelectedIndex +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#ListView] insertDefaultItem +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#ListView] setItemsMargin +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ListView] refreshView +-- @param self + +-------------------------------- +-- @function [parent=#ListView] removeLastItem +-- @param self + +-------------------------------- +-- @function [parent=#ListView] getItemsMargin +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ListView] getItem +-- @param self +-- @param #long long +-- @return Widget#Widget ret (return value: ccui.Widget) + +-------------------------------- +-- @function [parent=#ListView] setItemModel +-- @param self +-- @param #ccui.Widget widget + +-------------------------------- +-- @function [parent=#ListView] requestRefreshView +-- @param self + +-------------------------------- +-- @function [parent=#ListView] pushBackDefaultItem +-- @param self + +-------------------------------- +-- @function [parent=#ListView] insertCustomItem +-- @param self +-- @param #ccui.Widget widget +-- @param #long long + +-------------------------------- +-- @function [parent=#ListView] create +-- @param self +-- @return ListView#ListView ret (return value: ccui.ListView) + +-------------------------------- +-- @function [parent=#ListView] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#ListView] ListView +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/LoadingBar.lua b/cocos/scripting/lua-bindings/auto/api/LoadingBar.lua new file mode 100644 index 0000000000..81043e1021 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/LoadingBar.lua @@ -0,0 +1,64 @@ + +-------------------------------- +-- @module LoadingBar +-------------------------------- +-- @function [parent=#LoadingBar] setPercent +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#LoadingBar] loadTexture +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#LoadingBar] setDirection +-- @param self +-- @param #ccui.LoadingBarType loadingbartype + +-------------------------------- +-- @function [parent=#LoadingBar] setScale9Enabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#LoadingBar] setCapInsets +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#LoadingBar] getDirection +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#LoadingBar] getCapInsets +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#LoadingBar] isScale9Enabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#LoadingBar] getPercent +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#LoadingBar] create +-- @param self +-- @return LoadingBar#LoadingBar ret (return value: ccui.LoadingBar) + +-------------------------------- +-- @function [parent=#LoadingBar] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#LoadingBar] LoadingBar +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Menu.lua b/cocos/scripting/lua-bindings/auto/api/Menu.lua new file mode 100644 index 0000000000..d5f3f49990 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Menu.lua @@ -0,0 +1,32 @@ + +-------------------------------- +-- @module Menu +-------------------------------- +-- @function [parent=#Menu] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Menu] alignItemsVertically +-- @param self + +-------------------------------- +-- @function [parent=#Menu] isEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Menu] alignItemsHorizontallyWithPadding +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Menu] alignItemsVerticallyWithPadding +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Menu] alignItemsHorizontally +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MenuItem.lua b/cocos/scripting/lua-bindings/auto/api/MenuItem.lua new file mode 100644 index 0000000000..a9d7b728ec --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MenuItem.lua @@ -0,0 +1,36 @@ + +-------------------------------- +-- @module MenuItem +-------------------------------- +-- @function [parent=#MenuItem] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#MenuItem] activate +-- @param self + +-------------------------------- +-- @function [parent=#MenuItem] isEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#MenuItem] selected +-- @param self + +-------------------------------- +-- @function [parent=#MenuItem] isSelected +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#MenuItem] unselected +-- @param self + +-------------------------------- +-- @function [parent=#MenuItem] rect +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MenuItemAtlasFont.lua b/cocos/scripting/lua-bindings/auto/api/MenuItemAtlasFont.lua new file mode 100644 index 0000000000..26853472b2 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MenuItemAtlasFont.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module MenuItemAtlasFont +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MenuItemFont.lua b/cocos/scripting/lua-bindings/auto/api/MenuItemFont.lua new file mode 100644 index 0000000000..027c7a052a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MenuItemFont.lua @@ -0,0 +1,44 @@ + +-------------------------------- +-- @module MenuItemFont +-------------------------------- +-- @function [parent=#MenuItemFont] getFontSizeObj +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#MenuItemFont] getFontNameObj +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#MenuItemFont] setFontSizeObj +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#MenuItemFont] setFontNameObj +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#MenuItemFont] setFontName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#MenuItemFont] getFontSize +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#MenuItemFont] getFontName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#MenuItemFont] setFontSize +-- @param self +-- @param #int int + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MenuItemImage.lua b/cocos/scripting/lua-bindings/auto/api/MenuItemImage.lua new file mode 100644 index 0000000000..f005666e82 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MenuItemImage.lua @@ -0,0 +1,19 @@ + +-------------------------------- +-- @module MenuItemImage +-------------------------------- +-- @function [parent=#MenuItemImage] setDisabledSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe + +-------------------------------- +-- @function [parent=#MenuItemImage] setSelectedSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe + +-------------------------------- +-- @function [parent=#MenuItemImage] setNormalSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MenuItemLabel.lua b/cocos/scripting/lua-bindings/auto/api/MenuItemLabel.lua new file mode 100644 index 0000000000..3e25866838 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MenuItemLabel.lua @@ -0,0 +1,29 @@ + +-------------------------------- +-- @module MenuItemLabel +-------------------------------- +-- @function [parent=#MenuItemLabel] getDisabledColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#MenuItemLabel] setString +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#MenuItemLabel] setLabel +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#MenuItemLabel] setDisabledColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#MenuItemLabel] getLabel +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MenuItemSprite.lua b/cocos/scripting/lua-bindings/auto/api/MenuItemSprite.lua new file mode 100644 index 0000000000..b8fdcd63c0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MenuItemSprite.lua @@ -0,0 +1,47 @@ + +-------------------------------- +-- @module MenuItemSprite +-------------------------------- +-- @function [parent=#MenuItemSprite] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#MenuItemSprite] selected +-- @param self + +-------------------------------- +-- @function [parent=#MenuItemSprite] setNormalImage +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#MenuItemSprite] setDisabledImage +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#MenuItemSprite] setSelectedImage +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#MenuItemSprite] getDisabledImage +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#MenuItemSprite] getSelectedImage +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#MenuItemSprite] getNormalImage +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#MenuItemSprite] unselected +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MenuItemToggle.lua b/cocos/scripting/lua-bindings/auto/api/MenuItemToggle.lua new file mode 100644 index 0000000000..25fd532397 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MenuItemToggle.lua @@ -0,0 +1,29 @@ + +-------------------------------- +-- @module MenuItemToggle +-------------------------------- +-- @function [parent=#MenuItemToggle] setSubItems +-- @param self +-- @param #array_table array + +-------------------------------- +-- @function [parent=#MenuItemToggle] getSelectedIndex +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#MenuItemToggle] addSubItem +-- @param self +-- @param #cc.MenuItem menuitem + +-------------------------------- +-- @function [parent=#MenuItemToggle] getSelectedItem +-- @param self +-- @return MenuItem#MenuItem ret (return value: cc.MenuItem) + +-------------------------------- +-- @function [parent=#MenuItemToggle] setSelectedIndex +-- @param self +-- @param #unsigned int int + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MotionStreak.lua b/cocos/scripting/lua-bindings/auto/api/MotionStreak.lua new file mode 100644 index 0000000000..454aba7b9d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MotionStreak.lua @@ -0,0 +1,57 @@ + +-------------------------------- +-- @module MotionStreak +-------------------------------- +-- @function [parent=#MotionStreak] reset +-- @param self + +-------------------------------- +-- @function [parent=#MotionStreak] setTexture +-- @param self +-- @param #cc.Texture2D texture2d + +-------------------------------- +-- @function [parent=#MotionStreak] getTexture +-- @param self +-- @return Texture2D#Texture2D ret (return value: cc.Texture2D) + +-------------------------------- +-- @function [parent=#MotionStreak] tintWithColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#MotionStreak] setStartingPositionInitialized +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#MotionStreak] isStartingPositionInitialized +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#MotionStreak] isFastMode +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#MotionStreak] setFastMode +-- @param self +-- @param #bool bool + +-------------------------------- +-- overload function: create(float, float, float, color3B_table, cc.Texture2D) +-- +-- overload function: create(float, float, float, color3B_table, string) +-- +-- @function [parent=#MotionStreak] create +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #color3B_table color3b +-- @param #string str +-- @return MotionStreak#MotionStreak ret (retunr value: cc.MotionStreak) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MoveBy.lua b/cocos/scripting/lua-bindings/auto/api/MoveBy.lua new file mode 100644 index 0000000000..315aa61475 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MoveBy.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module MoveBy +-------------------------------- +-- @function [parent=#MoveBy] create +-- @param self +-- @param #float float +-- @param #point_table point +-- @return MoveBy#MoveBy ret (return value: cc.MoveBy) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MoveTo.lua b/cocos/scripting/lua-bindings/auto/api/MoveTo.lua new file mode 100644 index 0000000000..ad822b7cda --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MoveTo.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module MoveTo +-------------------------------- +-- @function [parent=#MoveTo] create +-- @param self +-- @param #float float +-- @param #point_table point +-- @return MoveTo#MoveTo ret (return value: cc.MoveTo) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MovementBoneData.lua b/cocos/scripting/lua-bindings/auto/api/MovementBoneData.lua new file mode 100644 index 0000000000..d6e2e98de6 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MovementBoneData.lua @@ -0,0 +1,29 @@ + +-------------------------------- +-- @module MovementBoneData +-------------------------------- +-- @function [parent=#MovementBoneData] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#MovementBoneData] getFrameData +-- @param self +-- @param #int int +-- @return FrameData#FrameData ret (return value: ccs.FrameData) + +-------------------------------- +-- @function [parent=#MovementBoneData] addFrameData +-- @param self +-- @param #ccs.FrameData framedata + +-------------------------------- +-- @function [parent=#MovementBoneData] create +-- @param self +-- @return MovementBoneData#MovementBoneData ret (return value: ccs.MovementBoneData) + +-------------------------------- +-- @function [parent=#MovementBoneData] MovementBoneData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/MovementData.lua b/cocos/scripting/lua-bindings/auto/api/MovementData.lua new file mode 100644 index 0000000000..e33de3cb17 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/MovementData.lua @@ -0,0 +1,24 @@ + +-------------------------------- +-- @module MovementData +-------------------------------- +-- @function [parent=#MovementData] getMovementBoneData +-- @param self +-- @param #string str +-- @return MovementBoneData#MovementBoneData ret (return value: ccs.MovementBoneData) + +-------------------------------- +-- @function [parent=#MovementData] addMovementBoneData +-- @param self +-- @param #ccs.MovementBoneData movementbonedata + +-------------------------------- +-- @function [parent=#MovementData] create +-- @param self +-- @return MovementData#MovementData ret (return value: ccs.MovementData) + +-------------------------------- +-- @function [parent=#MovementData] MovementData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Node.lua b/cocos/scripting/lua-bindings/auto/api/Node.lua new file mode 100644 index 0000000000..d68c53dff9 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Node.lua @@ -0,0 +1,674 @@ + +-------------------------------- +-- @module Node +-------------------------------- +-- overload function: addChild(cc.Node, int) +-- +-- overload function: addChild(cc.Node) +-- +-- overload function: addChild(cc.Node, int, int) +-- +-- @function [parent=#Node] addChild +-- @param self +-- @param #cc.Node node +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#Node] removeComponent +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Node] setPhysicsBody +-- @param self +-- @param #cc.PhysicsBody physicsbody + +-------------------------------- +-- overload function: getShaderProgram() +-- +-- overload function: getShaderProgram() +-- +-- @function [parent=#Node] getShaderProgram +-- @param self +-- @return GLProgram#GLProgram ret (retunr value: cc.GLProgram) + +-------------------------------- +-- @function [parent=#Node] getDescription +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#Node] setRotationSkewY +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] setOpacityModifyRGB +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] setCascadeOpacityEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- overload function: getChildren() +-- +-- overload function: getChildren() +-- +-- @function [parent=#Node] getChildren +-- @param self +-- @return array_table#array_table ret (retunr value: array_table) + +-------------------------------- +-- @function [parent=#Node] pause +-- @param self + +-------------------------------- +-- @function [parent=#Node] convertToWorldSpaceAR +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Node] isIgnoreAnchorPointForPosition +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Node] updateDisplayedOpacity +-- @param self +-- @param #unsigned char char + +-------------------------------- +-- @function [parent=#Node] setRotation +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] setScaleZ +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] setScaleY +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] setScaleX +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] setRotationSkewX +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] removeAllComponents +-- @param self + +-------------------------------- +-- @function [parent=#Node] _setLocalZOrder +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Node] getTag +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Node] getNodeToWorldAffineTransform +-- @param self +-- @return AffineTransform#AffineTransform ret (return value: cc.AffineTransform) + +-------------------------------- +-- @function [parent=#Node] getNodeToWorldTransform +-- @param self +-- @return kmMat4#kmMat4 ret (return value: kmMat4) + +-------------------------------- +-- @function [parent=#Node] getPosition3D +-- @param self +-- @return Vertex3F#Vertex3F ret (return value: cc.Vertex3F) + +-------------------------------- +-- @function [parent=#Node] removeChild +-- @param self +-- @param #cc.Node node +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] convertToWorldSpace +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Node] getScene +-- @param self +-- @return Scene#Scene ret (return value: cc.Scene) + +-------------------------------- +-- @function [parent=#Node] getEventDispatcher +-- @param self +-- @return EventDispatcher#EventDispatcher ret (return value: cc.EventDispatcher) + +-------------------------------- +-- @function [parent=#Node] setSkewX +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] setSkewY +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] getOpacity +-- @param self +-- @return unsigned char#unsigned char ret (return value: unsigned char) + +-------------------------------- +-- @function [parent=#Node] convertTouchToNodeSpace +-- @param self +-- @param #cc.Touch touch +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- overload function: removeAllChildrenWithCleanup(bool) +-- +-- overload function: removeAllChildrenWithCleanup() +-- +-- @function [parent=#Node] removeAllChildrenWithCleanup +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] getNodeToParentAffineTransform +-- @param self +-- @return AffineTransform#AffineTransform ret (return value: cc.AffineTransform) + +-------------------------------- +-- @function [parent=#Node] isCascadeOpacityEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Node] setParent +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#Node] getRotation3D +-- @param self +-- @return Vertex3F#Vertex3F ret (return value: cc.Vertex3F) + +-------------------------------- +-- @function [parent=#Node] getNodeToParentTransform +-- @param self +-- @return kmMat4#kmMat4 ret (return value: kmMat4) + +-------------------------------- +-- @function [parent=#Node] convertTouchToNodeSpaceAR +-- @param self +-- @param #cc.Touch touch +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Node] convertToNodeSpace +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Node] resume +-- @param self + +-------------------------------- +-- @function [parent=#Node] getPhysicsBody +-- @param self +-- @return PhysicsBody#PhysicsBody ret (return value: cc.PhysicsBody) + +-------------------------------- +-- overload function: setPosition(float, float) +-- +-- overload function: setPosition(point_table) +-- +-- @function [parent=#Node] setPosition +-- @param self +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] stopActionByTag +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Node] reorderChild +-- @param self +-- @param #cc.Node node +-- @param #int int + +-------------------------------- +-- @function [parent=#Node] ignoreAnchorPointForPosition +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] setPositionZ +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] setRotation3D +-- @param self +-- @param #cc.Vertex3F vertex3f + +-------------------------------- +-- @function [parent=#Node] setPositionX +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] setNodeToParentTransform +-- @param self +-- @param #kmMat4 kmmat4 + +-------------------------------- +-- @function [parent=#Node] getAnchorPoint +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Node] getNumberOfRunningActions +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#Node] updateTransform +-- @param self + +-------------------------------- +-- @function [parent=#Node] isVisible +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Node] getChildrenCount +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#Node] convertToNodeSpaceAR +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Node] addComponent +-- @param self +-- @param #cc.Component component +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- overload function: visit() +-- +-- overload function: visit(cc.Renderer, kmMat4, bool) +-- +-- @function [parent=#Node] visit +-- @param self +-- @param #cc.Renderer renderer +-- @param #kmMat4 kmmat4 +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] setShaderProgram +-- @param self +-- @param #cc.GLProgram glprogram + +-------------------------------- +-- @function [parent=#Node] getRotation +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] getAnchorPointInPoints +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Node] runAction +-- @param self +-- @param #cc.Action action +-- @return Action#Action ret (return value: cc.Action) + +-------------------------------- +-- @function [parent=#Node] setScheduler +-- @param self +-- @param #cc.Scheduler scheduler + +-------------------------------- +-- @function [parent=#Node] stopAllActions +-- @param self + +-------------------------------- +-- @function [parent=#Node] getSkewX +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] getSkewY +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] getDisplayedColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#Node] getActionByTag +-- @param self +-- @param #int int +-- @return Action#Action ret (return value: cc.Action) + +-------------------------------- +-- overload function: setAdditionalTransform(cc.AffineTransform) +-- +-- overload function: setAdditionalTransform(kmMat4) +-- +-- @function [parent=#Node] setAdditionalTransform +-- @param self +-- @param #kmMat4 kmmat4 + +-------------------------------- +-- @function [parent=#Node] getDisplayedOpacity +-- @param self +-- @return unsigned char#unsigned char ret (return value: unsigned char) + +-------------------------------- +-- @function [parent=#Node] getLocalZOrder +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- overload function: getScheduler() +-- +-- overload function: getScheduler() +-- +-- @function [parent=#Node] getScheduler +-- @param self +-- @return Scheduler#Scheduler ret (retunr value: cc.Scheduler) + +-------------------------------- +-- @function [parent=#Node] getParentToNodeAffineTransform +-- @param self +-- @return AffineTransform#AffineTransform ret (return value: cc.AffineTransform) + +-------------------------------- +-- @function [parent=#Node] getOrderOfArrival +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Node] setActionManager +-- @param self +-- @param #cc.ActionManager actionmanager + +-------------------------------- +-- @function [parent=#Node] setColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#Node] isRunning +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- overload function: getParent() +-- +-- overload function: getParent() +-- +-- @function [parent=#Node] getParent +-- @param self +-- @return Node#Node ret (retunr value: cc.Node) + +-------------------------------- +-- @function [parent=#Node] getPositionZ +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] getPositionY +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] getPositionX +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] removeChildByTag +-- @param self +-- @param #int int +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] setPositionY +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] updateDisplayedColor +-- @param self +-- @param #color3B_table color3b + +-------------------------------- +-- @function [parent=#Node] setVisible +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] getParentToNodeTransform +-- @param self +-- @return kmMat4#kmMat4 ret (return value: kmMat4) + +-------------------------------- +-- @function [parent=#Node] setGlobalZOrder +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: setScale(float, float) +-- +-- overload function: setScale(float) +-- +-- @function [parent=#Node] setScale +-- @param self +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] getChildByTag +-- @param self +-- @param #int int +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#Node] setOrderOfArrival +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Node] getScaleZ +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] getScaleY +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] getScaleX +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] setLocalZOrder +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Node] getWorldToNodeAffineTransform +-- @param self +-- @return AffineTransform#AffineTransform ret (return value: cc.AffineTransform) + +-------------------------------- +-- @function [parent=#Node] setCascadeColorEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] setOpacity +-- @param self +-- @param #unsigned char char + +-------------------------------- +-- @function [parent=#Node] cleanup +-- @param self + +-------------------------------- +-- @function [parent=#Node] getComponent +-- @param self +-- @param #string str +-- @return Component#Component ret (return value: cc.Component) + +-------------------------------- +-- @function [parent=#Node] getContentSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Node] getColor +-- @param self +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#Node] getBoundingBox +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#Node] setEventDispatcher +-- @param self +-- @param #cc.EventDispatcher eventdispatcher + +-------------------------------- +-- @function [parent=#Node] getGlobalZOrder +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- overload function: draw() +-- +-- overload function: draw(cc.Renderer, kmMat4, bool) +-- +-- @function [parent=#Node] draw +-- @param self +-- @param #cc.Renderer renderer +-- @param #kmMat4 kmmat4 +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] setUserObject +-- @param self +-- @param #cc.Ref ref + +-------------------------------- +-- overload function: removeFromParentAndCleanup(bool) +-- +-- overload function: removeFromParentAndCleanup() +-- +-- @function [parent=#Node] removeFromParentAndCleanup +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Node] setPosition3D +-- @param self +-- @param #cc.Vertex3F vertex3f + +-------------------------------- +-- @function [parent=#Node] update +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Node] sortAllChildren +-- @param self + +-------------------------------- +-- @function [parent=#Node] getWorldToNodeTransform +-- @param self +-- @return kmMat4#kmMat4 ret (return value: kmMat4) + +-------------------------------- +-- @function [parent=#Node] getScale +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] getRotationSkewX +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] getRotationSkewY +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Node] setTag +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Node] isCascadeColorEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Node] isOpacityModifyRGB +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Node] stopAction +-- @param self +-- @param #cc.Action action + +-------------------------------- +-- overload function: getActionManager() +-- +-- overload function: getActionManager() +-- +-- @function [parent=#Node] getActionManager +-- @param self +-- @return ActionManager#ActionManager ret (retunr value: cc.ActionManager) + +-------------------------------- +-- @function [parent=#Node] create +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/NodeGrid.lua b/cocos/scripting/lua-bindings/auto/api/NodeGrid.lua new file mode 100644 index 0000000000..fa4f136767 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/NodeGrid.lua @@ -0,0 +1,28 @@ + +-------------------------------- +-- @module NodeGrid +-------------------------------- +-- @function [parent=#NodeGrid] setTarget +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- overload function: getGrid() +-- +-- overload function: getGrid() +-- +-- @function [parent=#NodeGrid] getGrid +-- @param self +-- @return GridBase#GridBase ret (retunr value: cc.GridBase) + +-------------------------------- +-- @function [parent=#NodeGrid] setGrid +-- @param self +-- @param #cc.GridBase gridbase + +-------------------------------- +-- @function [parent=#NodeGrid] create +-- @param self +-- @return NodeGrid#NodeGrid ret (return value: cc.NodeGrid) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/OrbitCamera.lua b/cocos/scripting/lua-bindings/auto/api/OrbitCamera.lua new file mode 100644 index 0000000000..1d5026af32 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/OrbitCamera.lua @@ -0,0 +1,39 @@ + +-------------------------------- +-- @module OrbitCamera +-------------------------------- +-- @function [parent=#OrbitCamera] sphericalRadius +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#OrbitCamera] initWithDuration +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#OrbitCamera] create +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @return OrbitCamera#OrbitCamera ret (return value: cc.OrbitCamera) + +-------------------------------- +-- @function [parent=#OrbitCamera] OrbitCamera +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PageTurn3D.lua b/cocos/scripting/lua-bindings/auto/api/PageTurn3D.lua new file mode 100644 index 0000000000..ac37b17467 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PageTurn3D.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module PageTurn3D +-------------------------------- +-- @function [parent=#PageTurn3D] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @return PageTurn3D#PageTurn3D ret (return value: cc.PageTurn3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PageView.lua b/cocos/scripting/lua-bindings/auto/api/PageView.lua new file mode 100644 index 0000000000..2063952c10 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PageView.lua @@ -0,0 +1,71 @@ + +-------------------------------- +-- @module PageView +-------------------------------- +-- @function [parent=#PageView] getCurPageIndex +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#PageView] addWidgetToPage +-- @param self +-- @param #ccui.Widget widget +-- @param #long long +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PageView] getPage +-- @param self +-- @param #long long +-- @return Layout#Layout ret (return value: ccui.Layout) + +-------------------------------- +-- @function [parent=#PageView] removePage +-- @param self +-- @param #ccui.Layout layout + +-------------------------------- +-- @function [parent=#PageView] insertPage +-- @param self +-- @param #ccui.Layout layout +-- @param #int int + +-------------------------------- +-- @function [parent=#PageView] scrollToPage +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#PageView] removePageAtIndex +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#PageView] getPages +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#PageView] removeAllPages +-- @param self + +-------------------------------- +-- @function [parent=#PageView] addPage +-- @param self +-- @param #ccui.Layout layout + +-------------------------------- +-- @function [parent=#PageView] create +-- @param self +-- @return PageView#PageView ret (return value: ccui.PageView) + +-------------------------------- +-- @function [parent=#PageView] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#PageView] PageView +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParallaxNode.lua b/cocos/scripting/lua-bindings/auto/api/ParallaxNode.lua new file mode 100644 index 0000000000..80ba88325e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParallaxNode.lua @@ -0,0 +1,36 @@ + +-------------------------------- +-- @module ParallaxNode +-------------------------------- +-- overload function: getParallaxArray() +-- +-- overload function: getParallaxArray() +-- +-- @function [parent=#ParallaxNode] getParallaxArray +-- @param self +-- @return _ccArray#_ccArray ret (retunr value: cc._ccArray) + +-------------------------------- +-- @function [parent=#ParallaxNode] addChild +-- @param self +-- @param #cc.Node node +-- @param #int int +-- @param #point_table point +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ParallaxNode] removeAllChildrenWithCleanup +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ParallaxNode] setParallaxArray +-- @param self +-- @param #cc._ccArray _ccarray + +-------------------------------- +-- @function [parent=#ParallaxNode] create +-- @param self +-- @return ParallaxNode#ParallaxNode ret (return value: cc.ParallaxNode) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleBatchNode.lua b/cocos/scripting/lua-bindings/auto/api/ParticleBatchNode.lua new file mode 100644 index 0000000000..1fc8d31812 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleBatchNode.lua @@ -0,0 +1,60 @@ + +-------------------------------- +-- @module ParticleBatchNode +-------------------------------- +-- @function [parent=#ParticleBatchNode] setTexture +-- @param self +-- @param #cc.Texture2D texture2d + +-------------------------------- +-- @function [parent=#ParticleBatchNode] disableParticle +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#ParticleBatchNode] getTexture +-- @param self +-- @return Texture2D#Texture2D ret (return value: cc.Texture2D) + +-------------------------------- +-- @function [parent=#ParticleBatchNode] setTextureAtlas +-- @param self +-- @param #cc.TextureAtlas textureatlas + +-------------------------------- +-- @function [parent=#ParticleBatchNode] removeAllChildrenWithCleanup +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ParticleBatchNode] getTextureAtlas +-- @param self +-- @return TextureAtlas#TextureAtlas ret (return value: cc.TextureAtlas) + +-------------------------------- +-- @function [parent=#ParticleBatchNode] insertChild +-- @param self +-- @param #cc.ParticleSystem particlesystem +-- @param #int int + +-------------------------------- +-- @function [parent=#ParticleBatchNode] removeChildAtIndex +-- @param self +-- @param #int int +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ParticleBatchNode] create +-- @param self +-- @param #string str +-- @param #int int +-- @return ParticleBatchNode#ParticleBatchNode ret (return value: cc.ParticleBatchNode) + +-------------------------------- +-- @function [parent=#ParticleBatchNode] createWithTexture +-- @param self +-- @param #cc.Texture2D texture2d +-- @param #int int +-- @return ParticleBatchNode#ParticleBatchNode ret (return value: cc.ParticleBatchNode) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleDisplayData.lua b/cocos/scripting/lua-bindings/auto/api/ParticleDisplayData.lua new file mode 100644 index 0000000000..9a5099fabc --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleDisplayData.lua @@ -0,0 +1,13 @@ + +-------------------------------- +-- @module ParticleDisplayData +-------------------------------- +-- @function [parent=#ParticleDisplayData] create +-- @param self +-- @return ParticleDisplayData#ParticleDisplayData ret (return value: ccs.ParticleDisplayData) + +-------------------------------- +-- @function [parent=#ParticleDisplayData] ParticleDisplayData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleExplosion.lua b/cocos/scripting/lua-bindings/auto/api/ParticleExplosion.lua new file mode 100644 index 0000000000..f8d3bccb12 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleExplosion.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleExplosion +-------------------------------- +-- @function [parent=#ParticleExplosion] create +-- @param self +-- @return ParticleExplosion#ParticleExplosion ret (return value: cc.ParticleExplosion) + +-------------------------------- +-- @function [parent=#ParticleExplosion] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleExplosion#ParticleExplosion ret (return value: cc.ParticleExplosion) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleFire.lua b/cocos/scripting/lua-bindings/auto/api/ParticleFire.lua new file mode 100644 index 0000000000..5ae44b4045 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleFire.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleFire +-------------------------------- +-- @function [parent=#ParticleFire] create +-- @param self +-- @return ParticleFire#ParticleFire ret (return value: cc.ParticleFire) + +-------------------------------- +-- @function [parent=#ParticleFire] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleFire#ParticleFire ret (return value: cc.ParticleFire) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleFireworks.lua b/cocos/scripting/lua-bindings/auto/api/ParticleFireworks.lua new file mode 100644 index 0000000000..b074b8b494 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleFireworks.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleFireworks +-------------------------------- +-- @function [parent=#ParticleFireworks] create +-- @param self +-- @return ParticleFireworks#ParticleFireworks ret (return value: cc.ParticleFireworks) + +-------------------------------- +-- @function [parent=#ParticleFireworks] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleFireworks#ParticleFireworks ret (return value: cc.ParticleFireworks) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleFlower.lua b/cocos/scripting/lua-bindings/auto/api/ParticleFlower.lua new file mode 100644 index 0000000000..a5fb4482ac --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleFlower.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleFlower +-------------------------------- +-- @function [parent=#ParticleFlower] create +-- @param self +-- @return ParticleFlower#ParticleFlower ret (return value: cc.ParticleFlower) + +-------------------------------- +-- @function [parent=#ParticleFlower] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleFlower#ParticleFlower ret (return value: cc.ParticleFlower) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleGalaxy.lua b/cocos/scripting/lua-bindings/auto/api/ParticleGalaxy.lua new file mode 100644 index 0000000000..64fe8a0cb5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleGalaxy.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleGalaxy +-------------------------------- +-- @function [parent=#ParticleGalaxy] create +-- @param self +-- @return ParticleGalaxy#ParticleGalaxy ret (return value: cc.ParticleGalaxy) + +-------------------------------- +-- @function [parent=#ParticleGalaxy] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleGalaxy#ParticleGalaxy ret (return value: cc.ParticleGalaxy) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleMeteor.lua b/cocos/scripting/lua-bindings/auto/api/ParticleMeteor.lua new file mode 100644 index 0000000000..630deb7b20 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleMeteor.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleMeteor +-------------------------------- +-- @function [parent=#ParticleMeteor] create +-- @param self +-- @return ParticleMeteor#ParticleMeteor ret (return value: cc.ParticleMeteor) + +-------------------------------- +-- @function [parent=#ParticleMeteor] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleMeteor#ParticleMeteor ret (return value: cc.ParticleMeteor) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleRain.lua b/cocos/scripting/lua-bindings/auto/api/ParticleRain.lua new file mode 100644 index 0000000000..adec94d477 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleRain.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleRain +-------------------------------- +-- @function [parent=#ParticleRain] create +-- @param self +-- @return ParticleRain#ParticleRain ret (return value: cc.ParticleRain) + +-------------------------------- +-- @function [parent=#ParticleRain] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleRain#ParticleRain ret (return value: cc.ParticleRain) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleSmoke.lua b/cocos/scripting/lua-bindings/auto/api/ParticleSmoke.lua new file mode 100644 index 0000000000..c2f64bec54 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleSmoke.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleSmoke +-------------------------------- +-- @function [parent=#ParticleSmoke] create +-- @param self +-- @return ParticleSmoke#ParticleSmoke ret (return value: cc.ParticleSmoke) + +-------------------------------- +-- @function [parent=#ParticleSmoke] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleSmoke#ParticleSmoke ret (return value: cc.ParticleSmoke) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleSnow.lua b/cocos/scripting/lua-bindings/auto/api/ParticleSnow.lua new file mode 100644 index 0000000000..1d46180f1a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleSnow.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleSnow +-------------------------------- +-- @function [parent=#ParticleSnow] create +-- @param self +-- @return ParticleSnow#ParticleSnow ret (return value: cc.ParticleSnow) + +-------------------------------- +-- @function [parent=#ParticleSnow] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleSnow#ParticleSnow ret (return value: cc.ParticleSnow) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleSpiral.lua b/cocos/scripting/lua-bindings/auto/api/ParticleSpiral.lua new file mode 100644 index 0000000000..5a435b0d33 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleSpiral.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleSpiral +-------------------------------- +-- @function [parent=#ParticleSpiral] create +-- @param self +-- @return ParticleSpiral#ParticleSpiral ret (return value: cc.ParticleSpiral) + +-------------------------------- +-- @function [parent=#ParticleSpiral] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleSpiral#ParticleSpiral ret (return value: cc.ParticleSpiral) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleSun.lua b/cocos/scripting/lua-bindings/auto/api/ParticleSun.lua new file mode 100644 index 0000000000..32d361092f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleSun.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module ParticleSun +-------------------------------- +-- @function [parent=#ParticleSun] create +-- @param self +-- @return ParticleSun#ParticleSun ret (return value: cc.ParticleSun) + +-------------------------------- +-- @function [parent=#ParticleSun] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleSun#ParticleSun ret (return value: cc.ParticleSun) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleSystem.lua b/cocos/scripting/lua-bindings/auto/api/ParticleSystem.lua new file mode 100644 index 0000000000..a79302d7c3 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleSystem.lua @@ -0,0 +1,513 @@ + +-------------------------------- +-- @module ParticleSystem +-------------------------------- +-- @function [parent=#ParticleSystem] getStartSizeVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getTexture +-- @param self +-- @return Texture2D#Texture2D ret (return value: cc.Texture2D) + +-------------------------------- +-- @function [parent=#ParticleSystem] isFull +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ParticleSystem] getBatchNode +-- @param self +-- @return ParticleBatchNode#ParticleBatchNode ret (return value: cc.ParticleBatchNode) + +-------------------------------- +-- @function [parent=#ParticleSystem] getStartColor +-- @param self +-- @return color4F_table#color4F_table ret (return value: color4F_table) + +-------------------------------- +-- @function [parent=#ParticleSystem] getPositionType +-- @param self +-- @return ParticleSystem::PositionType#ParticleSystem::PositionType ret (return value: cc.ParticleSystem::PositionType) + +-------------------------------- +-- @function [parent=#ParticleSystem] setPosVar +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ParticleSystem] getEndSpin +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setRotatePerSecondVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getStartSpinVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getRadialAccelVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getEndSizeVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setRotation +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setTangentialAccel +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setScaleY +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setScaleX +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getRadialAccel +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setStartRadius +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setRotatePerSecond +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setEndSize +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getGravity +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#ParticleSystem] getTangentialAccel +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setEndRadius +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getSpeed +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getAngle +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setEndColor +-- @param self +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#ParticleSystem] setStartSpin +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setDuration +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setTexture +-- @param self +-- @param #cc.Texture2D texture2d + +-------------------------------- +-- @function [parent=#ParticleSystem] getPosVar +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#ParticleSystem] updateWithNoTime +-- @param self + +-------------------------------- +-- @function [parent=#ParticleSystem] isBlendAdditive +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ParticleSystem] getSpeedVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setPositionType +-- @param self +-- @param #cc.ParticleSystem::PositionType positiontype + +-------------------------------- +-- @function [parent=#ParticleSystem] stopSystem +-- @param self + +-------------------------------- +-- @function [parent=#ParticleSystem] getSourcePosition +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#ParticleSystem] setLifeVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setTotalParticles +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#ParticleSystem] setEndColorVar +-- @param self +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#ParticleSystem] updateQuadWithParticle +-- @param self +-- @param #cc.sParticle sparticle +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ParticleSystem] getAtlasIndex +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#ParticleSystem] getStartSize +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setStartSpinVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] resetSystem +-- @param self + +-------------------------------- +-- @function [parent=#ParticleSystem] setAtlasIndex +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#ParticleSystem] setTangentialAccelVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setEndRadiusVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getEndRadius +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] isOpacityModifyRGB +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ParticleSystem] isActive +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ParticleSystem] setRadialAccelVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setStartSize +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setSpeed +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getStartSpin +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getRotatePerSecond +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] initParticle +-- @param self +-- @param #cc.sParticle sparticle + +-------------------------------- +-- @function [parent=#ParticleSystem] setEmitterMode +-- @param self +-- @param #cc.ParticleSystem::Mode mode + +-------------------------------- +-- @function [parent=#ParticleSystem] getDuration +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setSourcePosition +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ParticleSystem] getEndSpinVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setBlendAdditive +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ParticleSystem] setLife +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setAngleVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setRotationIsDir +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ParticleSystem] setEndSizeVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setAngle +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setBatchNode +-- @param self +-- @param #cc.ParticleBatchNode particlebatchnode + +-------------------------------- +-- @function [parent=#ParticleSystem] getTangentialAccelVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getEmitterMode +-- @param self +-- @return ParticleSystem::Mode#ParticleSystem::Mode ret (return value: cc.ParticleSystem::Mode) + +-------------------------------- +-- @function [parent=#ParticleSystem] setEndSpinVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getAngleVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setStartColor +-- @param self +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#ParticleSystem] getRotatePerSecondVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getEndSize +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getLife +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setSpeedVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setAutoRemoveOnFinish +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ParticleSystem] setGravity +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ParticleSystem] postStep +-- @param self + +-------------------------------- +-- @function [parent=#ParticleSystem] setEmissionRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getEndColorVar +-- @param self +-- @return color4F_table#color4F_table ret (return value: color4F_table) + +-------------------------------- +-- @function [parent=#ParticleSystem] getRotationIsDir +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ParticleSystem] setScale +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getEmissionRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getEndColor +-- @param self +-- @return color4F_table#color4F_table ret (return value: color4F_table) + +-------------------------------- +-- @function [parent=#ParticleSystem] getLifeVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setStartSizeVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setOpacityModifyRGB +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ParticleSystem] addParticle +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ParticleSystem] getStartRadius +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getParticleCount +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#ParticleSystem] getStartRadiusVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] setStartColorVar +-- @param self +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#ParticleSystem] setEndSpin +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] setRadialAccel +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] isAutoRemoveOnFinish +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ParticleSystem] getTotalParticles +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#ParticleSystem] setStartRadiusVar +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ParticleSystem] getEndRadiusVar +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ParticleSystem] getStartColorVar +-- @param self +-- @return color4F_table#color4F_table ret (return value: color4F_table) + +-------------------------------- +-- @function [parent=#ParticleSystem] create +-- @param self +-- @param #string str +-- @return ParticleSystem#ParticleSystem ret (return value: cc.ParticleSystem) + +-------------------------------- +-- @function [parent=#ParticleSystem] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleSystem#ParticleSystem ret (return value: cc.ParticleSystem) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ParticleSystemQuad.lua b/cocos/scripting/lua-bindings/auto/api/ParticleSystemQuad.lua new file mode 100644 index 0000000000..de9ab07eff --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ParticleSystemQuad.lua @@ -0,0 +1,31 @@ + +-------------------------------- +-- @module ParticleSystemQuad +-------------------------------- +-- @function [parent=#ParticleSystemQuad] setDisplayFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe + +-------------------------------- +-- @function [parent=#ParticleSystemQuad] setTextureWithRect +-- @param self +-- @param #cc.Texture2D texture2d +-- @param #rect_table rect + +-------------------------------- +-- overload function: create(string) +-- +-- overload function: create() +-- +-- @function [parent=#ParticleSystemQuad] create +-- @param self +-- @param #string str +-- @return ParticleSystemQuad#ParticleSystemQuad ret (retunr value: cc.ParticleSystemQuad) + +-------------------------------- +-- @function [parent=#ParticleSystemQuad] createWithTotalParticles +-- @param self +-- @param #int int +-- @return ParticleSystemQuad#ParticleSystemQuad ret (return value: cc.ParticleSystemQuad) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsBody.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsBody.lua new file mode 100644 index 0000000000..f67992b694 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsBody.lua @@ -0,0 +1,376 @@ + +-------------------------------- +-- @module PhysicsBody +-------------------------------- +-- @function [parent=#PhysicsBody] isGravityEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#PhysicsBody] resetForces +-- @param self + +-------------------------------- +-- @function [parent=#PhysicsBody] getVelocityLimit +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsBody] setGroup +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsBody] getMass +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsBody] getCollisionBitmask +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsBody] getRotationOffset +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsBody] getRotation +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsBody] getMoment +-- @param self +-- @param #float float +-- @return float#float ret (return value: float) + +-------------------------------- +-- overload function: applyImpulse(point_table, point_table) +-- +-- overload function: applyImpulse(point_table) +-- +-- @function [parent=#PhysicsBody] applyImpulse +-- @param self +-- @param #point_table point +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsBody] setRotationOffset +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: applyForce(point_table, point_table) +-- +-- overload function: applyForce(point_table) +-- +-- @function [parent=#PhysicsBody] applyForce +-- @param self +-- @param #point_table point +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsBody] addShape +-- @param self +-- @param #cc.PhysicsShape physicsshape +-- @param #bool bool +-- @return PhysicsShape#PhysicsShape ret (return value: cc.PhysicsShape) + +-------------------------------- +-- @function [parent=#PhysicsBody] applyTorque +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] getAngularVelocityLimit +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsBody] setAngularVelocityLimit +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] getVelocity +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsBody] getLinearDamping +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsBody] removeAllShapes +-- @param self + +-------------------------------- +-- @function [parent=#PhysicsBody] setAngularDamping +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] setVelocityLimit +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] setResting +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PhysicsBody] getPositionOffset +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsBody] setCategoryBitmask +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsBody] getWorld +-- @param self +-- @return PhysicsWorld#PhysicsWorld ret (return value: cc.PhysicsWorld) + +-------------------------------- +-- @function [parent=#PhysicsBody] getAngularVelocity +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsBody] getPosition +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsBody] setEnable +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PhysicsBody] setGravityEnable +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PhysicsBody] getGroup +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsBody] setMoment +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] getTag +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsBody] local2World +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsBody] getCategoryBitmask +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsBody] setDynamic +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PhysicsBody] getFirstShape +-- @param self +-- @return PhysicsShape#PhysicsShape ret (return value: cc.PhysicsShape) + +-------------------------------- +-- @function [parent=#PhysicsBody] getShapes +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#PhysicsBody] getContactTestBitmask +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsBody] setAngularVelocity +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] world2Local +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsBody] isEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- overload function: removeShape(int, bool) +-- +-- overload function: removeShape(cc.PhysicsShape, bool) +-- +-- @function [parent=#PhysicsBody] removeShape +-- @param self +-- @param #cc.PhysicsShape physicsshape +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PhysicsBody] setMass +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] addMoment +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] setVelocity +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsBody] setLinearDamping +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] setCollisionBitmask +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsBody] setPositionOffset +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsBody] setRotationEnable +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PhysicsBody] isRotationEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#PhysicsBody] getAngularDamping +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsBody] getVelocityAtLocalPoint +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsBody] isResting +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#PhysicsBody] addMass +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsBody] getShape +-- @param self +-- @param #int int +-- @return PhysicsShape#PhysicsShape ret (return value: cc.PhysicsShape) + +-------------------------------- +-- @function [parent=#PhysicsBody] setTag +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsBody] getVelocityAtWorldPoint +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsBody] setContactTestBitmask +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsBody] removeFromWorld +-- @param self + +-------------------------------- +-- @function [parent=#PhysicsBody] isDynamic +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#PhysicsBody] getNode +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#PhysicsBody] createBox +-- @param self +-- @param #size_table size +-- @param #cc.PhysicsMaterial physicsmaterial +-- @param #point_table point +-- @return PhysicsBody#PhysicsBody ret (return value: cc.PhysicsBody) + +-------------------------------- +-- @function [parent=#PhysicsBody] createEdgeSegment +-- @param self +-- @param #point_table point +-- @param #point_table point +-- @param #cc.PhysicsMaterial physicsmaterial +-- @param #float float +-- @return PhysicsBody#PhysicsBody ret (return value: cc.PhysicsBody) + +-------------------------------- +-- overload function: create(float) +-- +-- overload function: create() +-- +-- overload function: create(float, float) +-- +-- @function [parent=#PhysicsBody] create +-- @param self +-- @param #float float +-- @param #float float +-- @return PhysicsBody#PhysicsBody ret (retunr value: cc.PhysicsBody) + +-------------------------------- +-- @function [parent=#PhysicsBody] createEdgeBox +-- @param self +-- @param #size_table size +-- @param #cc.PhysicsMaterial physicsmaterial +-- @param #float float +-- @param #point_table point +-- @return PhysicsBody#PhysicsBody ret (return value: cc.PhysicsBody) + +-------------------------------- +-- @function [parent=#PhysicsBody] createCircle +-- @param self +-- @param #float float +-- @param #cc.PhysicsMaterial physicsmaterial +-- @param #point_table point +-- @return PhysicsBody#PhysicsBody ret (return value: cc.PhysicsBody) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsContact.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsContact.lua new file mode 100644 index 0000000000..ae4cd514c0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsContact.lua @@ -0,0 +1,24 @@ + +-------------------------------- +-- @module PhysicsContact +-------------------------------- +-- @function [parent=#PhysicsContact] getContactData +-- @param self +-- @return PhysicsContactData#PhysicsContactData ret (return value: cc.PhysicsContactData) + +-------------------------------- +-- @function [parent=#PhysicsContact] getEventCode +-- @param self +-- @return PhysicsContact::EventCode#PhysicsContact::EventCode ret (return value: cc.PhysicsContact::EventCode) + +-------------------------------- +-- @function [parent=#PhysicsContact] getShapeA +-- @param self +-- @return PhysicsShape#PhysicsShape ret (return value: cc.PhysicsShape) + +-------------------------------- +-- @function [parent=#PhysicsContact] getShapeB +-- @param self +-- @return PhysicsShape#PhysicsShape ret (return value: cc.PhysicsShape) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsContactPostSolve.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsContactPostSolve.lua new file mode 100644 index 0000000000..9ee6b2bfd6 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsContactPostSolve.lua @@ -0,0 +1,19 @@ + +-------------------------------- +-- @module PhysicsContactPostSolve +-------------------------------- +-- @function [parent=#PhysicsContactPostSolve] getFriction +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsContactPostSolve] getSurfaceVelocity +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsContactPostSolve] getRestitution +-- @param self +-- @return float#float ret (return value: float) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsContactPreSolve.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsContactPreSolve.lua new file mode 100644 index 0000000000..0971058e55 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsContactPreSolve.lua @@ -0,0 +1,38 @@ + +-------------------------------- +-- @module PhysicsContactPreSolve +-------------------------------- +-- @function [parent=#PhysicsContactPreSolve] getFriction +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsContactPreSolve] getRestitution +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsContactPreSolve] setFriction +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsContactPreSolve] ignore +-- @param self + +-------------------------------- +-- @function [parent=#PhysicsContactPreSolve] getSurfaceVelocity +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsContactPreSolve] setSurfaceVelocity +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsContactPreSolve] setRestitution +-- @param self +-- @param #float float + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsDebugDraw.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsDebugDraw.lua new file mode 100644 index 0000000000..66e9efb13b --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsDebugDraw.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module PhysicsDebugDraw +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJoint.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJoint.lua new file mode 100644 index 0000000000..ab7b41656e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJoint.lua @@ -0,0 +1,68 @@ + +-------------------------------- +-- @module PhysicsJoint +-------------------------------- +-- @function [parent=#PhysicsJoint] getBodyA +-- @param self +-- @return PhysicsBody#PhysicsBody ret (return value: cc.PhysicsBody) + +-------------------------------- +-- @function [parent=#PhysicsJoint] getBodyB +-- @param self +-- @return PhysicsBody#PhysicsBody ret (return value: cc.PhysicsBody) + +-------------------------------- +-- @function [parent=#PhysicsJoint] getMaxForce +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJoint] setMaxForce +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJoint] isEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#PhysicsJoint] setEnable +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PhysicsJoint] setCollisionEnable +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PhysicsJoint] getWorld +-- @param self +-- @return PhysicsWorld#PhysicsWorld ret (return value: cc.PhysicsWorld) + +-------------------------------- +-- @function [parent=#PhysicsJoint] setTag +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsJoint] removeFormWorld +-- @param self + +-------------------------------- +-- @function [parent=#PhysicsJoint] isCollisionEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#PhysicsJoint] getTag +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsJoint] destroy +-- @param self +-- @param #cc.PhysicsJoint physicsjoint + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointDistance.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointDistance.lua new file mode 100644 index 0000000000..46275610ed --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointDistance.lua @@ -0,0 +1,23 @@ + +-------------------------------- +-- @module PhysicsJointDistance +-------------------------------- +-- @function [parent=#PhysicsJointDistance] setDistance +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointDistance] getDistance +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointDistance] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #point_table point +-- @param #point_table point +-- @return PhysicsJointDistance#PhysicsJointDistance ret (return value: cc.PhysicsJointDistance) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointFixed.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointFixed.lua new file mode 100644 index 0000000000..02cff92e5f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointFixed.lua @@ -0,0 +1,12 @@ + +-------------------------------- +-- @module PhysicsJointFixed +-------------------------------- +-- @function [parent=#PhysicsJointFixed] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #point_table point +-- @return PhysicsJointFixed#PhysicsJointFixed ret (return value: cc.PhysicsJointFixed) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointGear.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointGear.lua new file mode 100644 index 0000000000..d0bfd887fd --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointGear.lua @@ -0,0 +1,33 @@ + +-------------------------------- +-- @module PhysicsJointGear +-------------------------------- +-- @function [parent=#PhysicsJointGear] setRatio +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointGear] getPhase +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointGear] setPhase +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointGear] getRatio +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointGear] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #float float +-- @param #float float +-- @return PhysicsJointGear#PhysicsJointGear ret (return value: cc.PhysicsJointGear) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointGroove.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointGroove.lua new file mode 100644 index 0000000000..d167283aac --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointGroove.lua @@ -0,0 +1,44 @@ + +-------------------------------- +-- @module PhysicsJointGroove +-------------------------------- +-- @function [parent=#PhysicsJointGroove] setAnchr2 +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsJointGroove] setGrooveA +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsJointGroove] setGrooveB +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsJointGroove] getGrooveA +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsJointGroove] getGrooveB +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsJointGroove] getAnchr2 +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsJointGroove] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #point_table point +-- @param #point_table point +-- @param #point_table point +-- @return PhysicsJointGroove#PhysicsJointGroove ret (return value: cc.PhysicsJointGroove) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointLimit.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointLimit.lua new file mode 100644 index 0000000000..b8472579a0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointLimit.lua @@ -0,0 +1,59 @@ + +-------------------------------- +-- @module PhysicsJointLimit +-------------------------------- +-- @function [parent=#PhysicsJointLimit] setAnchr2 +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsJointLimit] setAnchr1 +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsJointLimit] setMax +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointLimit] getAnchr2 +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsJointLimit] getAnchr1 +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsJointLimit] getMin +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointLimit] getMax +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointLimit] setMin +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: construct(cc.PhysicsBody, cc.PhysicsBody, point_table, point_table, float, float) +-- +-- overload function: construct(cc.PhysicsBody, cc.PhysicsBody, point_table, point_table) +-- +-- @function [parent=#PhysicsJointLimit] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #point_table point +-- @param #point_table point +-- @param #float float +-- @param #float float +-- @return PhysicsJointLimit#PhysicsJointLimit ret (retunr value: cc.PhysicsJointLimit) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointMotor.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointMotor.lua new file mode 100644 index 0000000000..f40d92c105 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointMotor.lua @@ -0,0 +1,22 @@ + +-------------------------------- +-- @module PhysicsJointMotor +-------------------------------- +-- @function [parent=#PhysicsJointMotor] setRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointMotor] getRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointMotor] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #float float +-- @return PhysicsJointMotor#PhysicsJointMotor ret (return value: cc.PhysicsJointMotor) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointPin.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointPin.lua new file mode 100644 index 0000000000..41bf80587e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointPin.lua @@ -0,0 +1,12 @@ + +-------------------------------- +-- @module PhysicsJointPin +-------------------------------- +-- @function [parent=#PhysicsJointPin] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #point_table point +-- @return PhysicsJointPin#PhysicsJointPin ret (return value: cc.PhysicsJointPin) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointRatchet.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointRatchet.lua new file mode 100644 index 0000000000..b0370c4339 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointRatchet.lua @@ -0,0 +1,43 @@ + +-------------------------------- +-- @module PhysicsJointRatchet +-------------------------------- +-- @function [parent=#PhysicsJointRatchet] getAngle +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointRatchet] setAngle +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointRatchet] setPhase +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointRatchet] getPhase +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointRatchet] setRatchet +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointRatchet] getRatchet +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointRatchet] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #float float +-- @param #float float +-- @return PhysicsJointRatchet#PhysicsJointRatchet ret (return value: cc.PhysicsJointRatchet) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointRotaryLimit.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointRotaryLimit.lua new file mode 100644 index 0000000000..376385c24a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointRotaryLimit.lua @@ -0,0 +1,37 @@ + +-------------------------------- +-- @module PhysicsJointRotaryLimit +-------------------------------- +-- @function [parent=#PhysicsJointRotaryLimit] getMax +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointRotaryLimit] setMin +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointRotaryLimit] setMax +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointRotaryLimit] getMin +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- overload function: construct(cc.PhysicsBody, cc.PhysicsBody) +-- +-- overload function: construct(cc.PhysicsBody, cc.PhysicsBody, float, float) +-- +-- @function [parent=#PhysicsJointRotaryLimit] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #float float +-- @param #float float +-- @return PhysicsJointRotaryLimit#PhysicsJointRotaryLimit ret (retunr value: cc.PhysicsJointRotaryLimit) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointRotarySpring.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointRotarySpring.lua new file mode 100644 index 0000000000..283a002ce6 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointRotarySpring.lua @@ -0,0 +1,43 @@ + +-------------------------------- +-- @module PhysicsJointRotarySpring +-------------------------------- +-- @function [parent=#PhysicsJointRotarySpring] getDamping +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointRotarySpring] setRestAngle +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointRotarySpring] getStiffness +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointRotarySpring] setStiffness +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointRotarySpring] setDamping +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointRotarySpring] getRestAngle +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointRotarySpring] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #float float +-- @param #float float +-- @return PhysicsJointRotarySpring#PhysicsJointRotarySpring ret (return value: cc.PhysicsJointRotarySpring) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsJointSpring.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsJointSpring.lua new file mode 100644 index 0000000000..8080d5d73d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsJointSpring.lua @@ -0,0 +1,65 @@ + +-------------------------------- +-- @module PhysicsJointSpring +-------------------------------- +-- @function [parent=#PhysicsJointSpring] setAnchr2 +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] setAnchr1 +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] getDamping +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] setStiffness +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] getRestLength +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] getAnchr2 +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] getAnchr1 +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] getStiffness +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] setRestLength +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] setDamping +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsJointSpring] construct +-- @param self +-- @param #cc.PhysicsBody physicsbody +-- @param #cc.PhysicsBody physicsbody +-- @param #point_table point +-- @param #point_table point +-- @param #float float +-- @param #float float +-- @return PhysicsJointSpring#PhysicsJointSpring ret (return value: cc.PhysicsJointSpring) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShape.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShape.lua new file mode 100644 index 0000000000..4792debb04 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShape.lua @@ -0,0 +1,150 @@ + +-------------------------------- +-- @module PhysicsShape +-------------------------------- +-- @function [parent=#PhysicsShape] getFriction +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShape] setGroup +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsShape] setDensity +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsShape] getMass +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShape] getMaterial +-- @param self +-- @return PhysicsMaterial#PhysicsMaterial ret (return value: cc.PhysicsMaterial) + +-------------------------------- +-- @function [parent=#PhysicsShape] getCollisionBitmask +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsShape] getArea +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShape] setCategoryBitmask +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsShape] getGroup +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsShape] setMoment +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsShape] containsPoint +-- @param self +-- @param #point_table point +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#PhysicsShape] getCategoryBitmask +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsShape] getType +-- @param self +-- @return PhysicsShape::Type#PhysicsShape::Type ret (return value: cc.PhysicsShape::Type) + +-------------------------------- +-- @function [parent=#PhysicsShape] getContactTestBitmask +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsShape] getCenter +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsShape] getDensity +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShape] setMass +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsShape] getTag +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsShape] calculateDefaultMoment +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShape] setCollisionBitmask +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsShape] getMoment +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShape] getOffset +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsShape] getRestitution +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShape] setFriction +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsShape] setMaterial +-- @param self +-- @param #cc.PhysicsMaterial physicsmaterial + +-------------------------------- +-- @function [parent=#PhysicsShape] setTag +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsShape] setContactTestBitmask +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsShape] setRestitution +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsShape] getBody +-- @param self +-- @return PhysicsBody#PhysicsBody ret (return value: cc.PhysicsBody) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeBox.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeBox.lua new file mode 100644 index 0000000000..84e9fcc05b --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeBox.lua @@ -0,0 +1,36 @@ + +-------------------------------- +-- @module PhysicsShapeBox +-------------------------------- +-- @function [parent=#PhysicsShapeBox] getPointsCount +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsShapeBox] getSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#PhysicsShapeBox] create +-- @param self +-- @param #size_table size +-- @param #cc.PhysicsMaterial physicsmaterial +-- @param #point_table point +-- @return PhysicsShapeBox#PhysicsShapeBox ret (return value: cc.PhysicsShapeBox) + +-------------------------------- +-- @function [parent=#PhysicsShapeBox] calculateArea +-- @param self +-- @param #size_table size +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShapeBox] calculateMoment +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #point_table point +-- @return float#float ret (return value: float) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeCircle.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeCircle.lua new file mode 100644 index 0000000000..834877fbff --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeCircle.lua @@ -0,0 +1,31 @@ + +-------------------------------- +-- @module PhysicsShapeCircle +-------------------------------- +-- @function [parent=#PhysicsShapeCircle] getRadius +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShapeCircle] create +-- @param self +-- @param #float float +-- @param #cc.PhysicsMaterial physicsmaterial +-- @param #point_table point +-- @return PhysicsShapeCircle#PhysicsShapeCircle ret (return value: cc.PhysicsShapeCircle) + +-------------------------------- +-- @function [parent=#PhysicsShapeCircle] calculateArea +-- @param self +-- @param #float float +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#PhysicsShapeCircle] calculateMoment +-- @param self +-- @param #float float +-- @param #float float +-- @param #point_table point +-- @return float#float ret (return value: float) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeBox.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeBox.lua new file mode 100644 index 0000000000..ff309ac8a4 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeBox.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module PhysicsShapeEdgeBox +-------------------------------- +-- @function [parent=#PhysicsShapeEdgeBox] getPointsCount +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsShapeEdgeBox] create +-- @param self +-- @param #size_table size +-- @param #cc.PhysicsMaterial physicsmaterial +-- @param #float float +-- @param #point_table point +-- @return PhysicsShapeEdgeBox#PhysicsShapeEdgeBox ret (return value: cc.PhysicsShapeEdgeBox) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeChain.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeChain.lua new file mode 100644 index 0000000000..373adbca00 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeChain.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module PhysicsShapeEdgeChain +-------------------------------- +-- @function [parent=#PhysicsShapeEdgeChain] getPointsCount +-- @param self +-- @return int#int ret (return value: int) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgePolygon.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgePolygon.lua new file mode 100644 index 0000000000..8a9dea0b9c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgePolygon.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module PhysicsShapeEdgePolygon +-------------------------------- +-- @function [parent=#PhysicsShapeEdgePolygon] getPointsCount +-- @param self +-- @return int#int ret (return value: int) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeSegment.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeSegment.lua new file mode 100644 index 0000000000..602d2a48d1 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShapeEdgeSegment.lua @@ -0,0 +1,23 @@ + +-------------------------------- +-- @module PhysicsShapeEdgeSegment +-------------------------------- +-- @function [parent=#PhysicsShapeEdgeSegment] getPointB +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsShapeEdgeSegment] getPointA +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsShapeEdgeSegment] create +-- @param self +-- @param #point_table point +-- @param #point_table point +-- @param #cc.PhysicsMaterial physicsmaterial +-- @param #float float +-- @return PhysicsShapeEdgeSegment#PhysicsShapeEdgeSegment ret (return value: cc.PhysicsShapeEdgeSegment) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsShapePolygon.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsShapePolygon.lua new file mode 100644 index 0000000000..9c0b737f03 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsShapePolygon.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module PhysicsShapePolygon +-------------------------------- +-- @function [parent=#PhysicsShapePolygon] getPointsCount +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsShapePolygon] getPoint +-- @param self +-- @param #int int +-- @return point_table#point_table ret (return value: point_table) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/PhysicsWorld.lua b/cocos/scripting/lua-bindings/auto/api/PhysicsWorld.lua new file mode 100644 index 0000000000..a3512a36e7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/PhysicsWorld.lua @@ -0,0 +1,95 @@ + +-------------------------------- +-- @module PhysicsWorld +-------------------------------- +-- @function [parent=#PhysicsWorld] getGravity +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#PhysicsWorld] getAllBodies +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#PhysicsWorld] setGravity +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#PhysicsWorld] getSpeed +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- overload function: removeBody(int) +-- +-- overload function: removeBody(cc.PhysicsBody) +-- +-- @function [parent=#PhysicsWorld] removeBody +-- @param self +-- @param #cc.PhysicsBody physicsbody + +-------------------------------- +-- @function [parent=#PhysicsWorld] removeJoint +-- @param self +-- @param #cc.PhysicsJoint physicsjoint +-- @param #bool bool + +-------------------------------- +-- @function [parent=#PhysicsWorld] getUpdateRate +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsWorld] setSpeed +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#PhysicsWorld] getShapes +-- @param self +-- @param #point_table point +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#PhysicsWorld] removeAllJoints +-- @param self + +-------------------------------- +-- @function [parent=#PhysicsWorld] getShape +-- @param self +-- @param #point_table point +-- @return PhysicsShape#PhysicsShape ret (return value: cc.PhysicsShape) + +-------------------------------- +-- @function [parent=#PhysicsWorld] removeAllBodies +-- @param self + +-------------------------------- +-- @function [parent=#PhysicsWorld] getDebugDrawMask +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#PhysicsWorld] setDebugDrawMask +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsWorld] getBody +-- @param self +-- @param #int int +-- @return PhysicsBody#PhysicsBody ret (return value: cc.PhysicsBody) + +-------------------------------- +-- @function [parent=#PhysicsWorld] setUpdateRate +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#PhysicsWorld] addJoint +-- @param self +-- @param #cc.PhysicsJoint physicsjoint + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Place.lua b/cocos/scripting/lua-bindings/auto/api/Place.lua new file mode 100644 index 0000000000..ab899d512c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Place.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module Place +-------------------------------- +-- @function [parent=#Place] create +-- @param self +-- @param #point_table point +-- @return Place#Place ret (return value: cc.Place) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ProgressFromTo.lua b/cocos/scripting/lua-bindings/auto/api/ProgressFromTo.lua new file mode 100644 index 0000000000..df4dc08b42 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ProgressFromTo.lua @@ -0,0 +1,12 @@ + +-------------------------------- +-- @module ProgressFromTo +-------------------------------- +-- @function [parent=#ProgressFromTo] create +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @return ProgressFromTo#ProgressFromTo ret (return value: cc.ProgressFromTo) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ProgressTimer.lua b/cocos/scripting/lua-bindings/auto/api/ProgressTimer.lua new file mode 100644 index 0000000000..ee4c0b69e0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ProgressTimer.lua @@ -0,0 +1,74 @@ + +-------------------------------- +-- @module ProgressTimer +-------------------------------- +-- @function [parent=#ProgressTimer] isReverseDirection +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ProgressTimer] setBarChangeRate +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ProgressTimer] getPercentage +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#ProgressTimer] setSprite +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#ProgressTimer] getType +-- @param self +-- @return ProgressTimer::Type#ProgressTimer::Type ret (return value: cc.ProgressTimer::Type) + +-------------------------------- +-- @function [parent=#ProgressTimer] getSprite +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#ProgressTimer] setMidpoint +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ProgressTimer] getBarChangeRate +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- overload function: setReverseDirection(bool) +-- +-- overload function: setReverseDirection(bool) +-- +-- @function [parent=#ProgressTimer] setReverseDirection +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ProgressTimer] getMidpoint +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#ProgressTimer] setPercentage +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ProgressTimer] setType +-- @param self +-- @param #cc.ProgressTimer::Type type + +-------------------------------- +-- @function [parent=#ProgressTimer] create +-- @param self +-- @param #cc.Sprite sprite +-- @return ProgressTimer#ProgressTimer ret (return value: cc.ProgressTimer) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ProgressTo.lua b/cocos/scripting/lua-bindings/auto/api/ProgressTo.lua new file mode 100644 index 0000000000..5e64a84499 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ProgressTo.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module ProgressTo +-------------------------------- +-- @function [parent=#ProgressTo] create +-- @param self +-- @param #float float +-- @param #float float +-- @return ProgressTo#ProgressTo ret (return value: cc.ProgressTo) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Ref.lua b/cocos/scripting/lua-bindings/auto/api/Ref.lua new file mode 100644 index 0000000000..900db37e4c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Ref.lua @@ -0,0 +1,17 @@ + +-------------------------------- +-- @module Ref +-------------------------------- +-- @function [parent=#Ref] release +-- @param self + +-------------------------------- +-- @function [parent=#Ref] retain +-- @param self + +-------------------------------- +-- @function [parent=#Ref] getReferenceCount +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/RelativeLayoutParameter.lua b/cocos/scripting/lua-bindings/auto/api/RelativeLayoutParameter.lua new file mode 100644 index 0000000000..a9a239ad52 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/RelativeLayoutParameter.lua @@ -0,0 +1,43 @@ + +-------------------------------- +-- @module RelativeLayoutParameter +-------------------------------- +-- @function [parent=#RelativeLayoutParameter] setAlign +-- @param self +-- @param #ccui.RelativeAlign relativealign + +-------------------------------- +-- @function [parent=#RelativeLayoutParameter] setRelativeToWidgetName +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#RelativeLayoutParameter] getRelativeName +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#RelativeLayoutParameter] getRelativeToWidgetName +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#RelativeLayoutParameter] setRelativeName +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#RelativeLayoutParameter] getAlign +-- @param self +-- @return RelativeAlign#RelativeAlign ret (return value: ccui.RelativeAlign) + +-------------------------------- +-- @function [parent=#RelativeLayoutParameter] create +-- @param self +-- @return RelativeLayoutParameter#RelativeLayoutParameter ret (return value: ccui.RelativeLayoutParameter) + +-------------------------------- +-- @function [parent=#RelativeLayoutParameter] RelativeLayoutParameter +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/RemoveSelf.lua b/cocos/scripting/lua-bindings/auto/api/RemoveSelf.lua new file mode 100644 index 0000000000..deab99d1ca --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/RemoveSelf.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module RemoveSelf +-------------------------------- +-- @function [parent=#RemoveSelf] create +-- @param self +-- @return RemoveSelf#RemoveSelf ret (return value: cc.RemoveSelf) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/RenderTexture.lua b/cocos/scripting/lua-bindings/auto/api/RenderTexture.lua new file mode 100644 index 0000000000..5472fef86e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/RenderTexture.lua @@ -0,0 +1,170 @@ + +-------------------------------- +-- @module RenderTexture +-------------------------------- +-- @function [parent=#RenderTexture] setVirtualViewport +-- @param self +-- @param #point_table point +-- @param #rect_table rect +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#RenderTexture] clearStencil +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#RenderTexture] getClearDepth +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#RenderTexture] getClearStencil +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#RenderTexture] end +-- @param self + +-------------------------------- +-- @function [parent=#RenderTexture] setClearStencil +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#RenderTexture] setSprite +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#RenderTexture] getSprite +-- @param self +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#RenderTexture] isAutoDraw +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#RenderTexture] setKeepMatrix +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#RenderTexture] setClearFlags +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#RenderTexture] begin +-- @param self + +-------------------------------- +-- overload function: saveToFile(string, cc.Image::Format) +-- +-- overload function: saveToFile(string) +-- +-- @function [parent=#RenderTexture] saveToFile +-- @param self +-- @param #string str +-- @param #cc.Image::Format format +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#RenderTexture] setAutoDraw +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#RenderTexture] setClearColor +-- @param self +-- @param #color4F_table color4f + +-------------------------------- +-- @function [parent=#RenderTexture] endToLua +-- @param self + +-------------------------------- +-- overload function: beginWithClear(float, float, float, float, float) +-- +-- overload function: beginWithClear(float, float, float, float) +-- +-- overload function: beginWithClear(float, float, float, float, float, int) +-- +-- @function [parent=#RenderTexture] beginWithClear +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #int int + +-------------------------------- +-- @function [parent=#RenderTexture] clearDepth +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#RenderTexture] getClearColor +-- @param self +-- @return color4F_table#color4F_table ret (return value: color4F_table) + +-------------------------------- +-- @function [parent=#RenderTexture] clear +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#RenderTexture] getClearFlags +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#RenderTexture] newImage +-- @param self +-- @return Image#Image ret (return value: cc.Image) + +-------------------------------- +-- @function [parent=#RenderTexture] setClearDepth +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: initWithWidthAndHeight(int, int, cc.Texture2D::PixelFormat, unsigned int) +-- +-- overload function: initWithWidthAndHeight(int, int, cc.Texture2D::PixelFormat) +-- +-- @function [parent=#RenderTexture] initWithWidthAndHeight +-- @param self +-- @param #int int +-- @param #int int +-- @param #cc.Texture2D::PixelFormat pixelformat +-- @param #unsigned int int +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- overload function: create(int, int, cc.Texture2D::PixelFormat) +-- +-- overload function: create(int, int, cc.Texture2D::PixelFormat, unsigned int) +-- +-- overload function: create(int, int) +-- +-- @function [parent=#RenderTexture] create +-- @param self +-- @param #int int +-- @param #int int +-- @param #cc.Texture2D::PixelFormat pixelformat +-- @param #unsigned int int +-- @return RenderTexture#RenderTexture ret (retunr value: cc.RenderTexture) + +-------------------------------- +-- @function [parent=#RenderTexture] RenderTexture +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Repeat.lua b/cocos/scripting/lua-bindings/auto/api/Repeat.lua new file mode 100644 index 0000000000..e17bcc5c0f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Repeat.lua @@ -0,0 +1,21 @@ + +-------------------------------- +-- @module Repeat +-------------------------------- +-- @function [parent=#Repeat] setInnerAction +-- @param self +-- @param #cc.FiniteTimeAction finitetimeaction + +-------------------------------- +-- @function [parent=#Repeat] getInnerAction +-- @param self +-- @return FiniteTimeAction#FiniteTimeAction ret (return value: cc.FiniteTimeAction) + +-------------------------------- +-- @function [parent=#Repeat] create +-- @param self +-- @param #cc.FiniteTimeAction finitetimeaction +-- @param #unsigned int int +-- @return Repeat#Repeat ret (return value: cc.Repeat) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/RepeatForever.lua b/cocos/scripting/lua-bindings/auto/api/RepeatForever.lua new file mode 100644 index 0000000000..85e9b0c6ea --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/RepeatForever.lua @@ -0,0 +1,20 @@ + +-------------------------------- +-- @module RepeatForever +-------------------------------- +-- @function [parent=#RepeatForever] setInnerAction +-- @param self +-- @param #cc.ActionInterval actioninterval + +-------------------------------- +-- @function [parent=#RepeatForever] getInnerAction +-- @param self +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#RepeatForever] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return RepeatForever#RepeatForever ret (return value: cc.RepeatForever) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ReuseGrid.lua b/cocos/scripting/lua-bindings/auto/api/ReuseGrid.lua new file mode 100644 index 0000000000..87d1b725d7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ReuseGrid.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module ReuseGrid +-------------------------------- +-- @function [parent=#ReuseGrid] create +-- @param self +-- @param #int int +-- @return ReuseGrid#ReuseGrid ret (return value: cc.ReuseGrid) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Ripple3D.lua b/cocos/scripting/lua-bindings/auto/api/Ripple3D.lua new file mode 100644 index 0000000000..b0946ad4f3 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Ripple3D.lua @@ -0,0 +1,45 @@ + +-------------------------------- +-- @module Ripple3D +-------------------------------- +-- @function [parent=#Ripple3D] setAmplitudeRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Ripple3D] getAmplitudeRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Ripple3D] setAmplitude +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Ripple3D] getAmplitude +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Ripple3D] setPosition +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#Ripple3D] getPosition +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Ripple3D] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #point_table point +-- @param #float float +-- @param #unsigned int int +-- @param #float float +-- @return Ripple3D#Ripple3D ret (return value: cc.Ripple3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/RotateBy.lua b/cocos/scripting/lua-bindings/auto/api/RotateBy.lua new file mode 100644 index 0000000000..e69b7a0129 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/RotateBy.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module RotateBy +-------------------------------- +-- overload function: create(float, float, float) +-- +-- overload function: create(float, float) +-- +-- overload function: create(float, cc.Vertex3F) +-- +-- @function [parent=#RotateBy] create +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @return RotateBy#RotateBy ret (retunr value: cc.RotateBy) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/RotateTo.lua b/cocos/scripting/lua-bindings/auto/api/RotateTo.lua new file mode 100644 index 0000000000..a927eb8c6a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/RotateTo.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module RotateTo +-------------------------------- +-- overload function: create(float, float) +-- +-- overload function: create(float, float, float) +-- +-- @function [parent=#RotateTo] create +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @return RotateTo#RotateTo ret (retunr value: cc.RotateTo) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Scale9Sprite.lua b/cocos/scripting/lua-bindings/auto/api/Scale9Sprite.lua new file mode 100644 index 0000000000..7b27a4ec24 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Scale9Sprite.lua @@ -0,0 +1,189 @@ + +-------------------------------- +-- @module Scale9Sprite +-------------------------------- +-- @function [parent=#Scale9Sprite] resizableSpriteWithCapInsets +-- @param self +-- @param #rect_table rect +-- @return Scale9Sprite#Scale9Sprite ret (return value: cc.Scale9Sprite) + +-------------------------------- +-- @function [parent=#Scale9Sprite] setInsetBottom +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: initWithSpriteFrameName(char) +-- +-- overload function: initWithSpriteFrameName(char, rect_table) +-- +-- @function [parent=#Scale9Sprite] initWithSpriteFrameName +-- @param self +-- @param #char char +-- @param #rect_table rect +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#Scale9Sprite] setInsetTop +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Scale9Sprite] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Scale9Sprite] setPreferredSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#Scale9Sprite] setSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe + +-------------------------------- +-- overload function: initWithBatchNode(cc.SpriteBatchNode, rect_table, rect_table) +-- +-- overload function: initWithBatchNode(cc.SpriteBatchNode, rect_table, bool, rect_table) +-- +-- @function [parent=#Scale9Sprite] initWithBatchNode +-- @param self +-- @param #cc.SpriteBatchNode spritebatchnode +-- @param #rect_table rect +-- @param #bool bool +-- @param #rect_table rect +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#Scale9Sprite] getInsetBottom +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Scale9Sprite] getCapInsets +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#Scale9Sprite] updateWithBatchNode +-- @param self +-- @param #cc.SpriteBatchNode spritebatchnode +-- @param #rect_table rect +-- @param #bool bool +-- @param #rect_table rect +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Scale9Sprite] getInsetRight +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Scale9Sprite] getOriginalSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- overload function: initWithFile(char, rect_table) +-- +-- overload function: initWithFile(char, rect_table, rect_table) +-- +-- overload function: initWithFile(rect_table, char) +-- +-- overload function: initWithFile(char) +-- +-- @function [parent=#Scale9Sprite] initWithFile +-- @param self +-- @param #char char +-- @param #rect_table rect +-- @param #rect_table rect +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#Scale9Sprite] getInsetTop +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Scale9Sprite] setInsetLeft +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: initWithSpriteFrame(cc.SpriteFrame) +-- +-- overload function: initWithSpriteFrame(cc.SpriteFrame, rect_table) +-- +-- @function [parent=#Scale9Sprite] initWithSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe +-- @param #rect_table rect +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#Scale9Sprite] getPreferredSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Scale9Sprite] setCapInsets +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Scale9Sprite] getInsetLeft +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Scale9Sprite] setInsetRight +-- @param self +-- @param #float float + +-------------------------------- +-- overload function: create(char, rect_table, rect_table) +-- +-- overload function: create() +-- +-- overload function: create(rect_table, char) +-- +-- overload function: create(char, rect_table) +-- +-- overload function: create(char) +-- +-- @function [parent=#Scale9Sprite] create +-- @param self +-- @param #char char +-- @param #rect_table rect +-- @param #rect_table rect +-- @return Scale9Sprite#Scale9Sprite ret (retunr value: cc.Scale9Sprite) + +-------------------------------- +-- overload function: createWithSpriteFrameName(char, rect_table) +-- +-- overload function: createWithSpriteFrameName(char) +-- +-- @function [parent=#Scale9Sprite] createWithSpriteFrameName +-- @param self +-- @param #char char +-- @param #rect_table rect +-- @return Scale9Sprite#Scale9Sprite ret (retunr value: cc.Scale9Sprite) + +-------------------------------- +-- overload function: createWithSpriteFrame(cc.SpriteFrame, rect_table) +-- +-- overload function: createWithSpriteFrame(cc.SpriteFrame) +-- +-- @function [parent=#Scale9Sprite] createWithSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe +-- @param #rect_table rect +-- @return Scale9Sprite#Scale9Sprite ret (retunr value: cc.Scale9Sprite) + +-------------------------------- +-- @function [parent=#Scale9Sprite] Scale9Sprite +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ScaleBy.lua b/cocos/scripting/lua-bindings/auto/api/ScaleBy.lua new file mode 100644 index 0000000000..13e45bee3f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ScaleBy.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module ScaleBy +-------------------------------- +-- overload function: create(float, float, float) +-- +-- overload function: create(float, float) +-- +-- @function [parent=#ScaleBy] create +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @return ScaleBy#ScaleBy ret (retunr value: cc.ScaleBy) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ScaleTo.lua b/cocos/scripting/lua-bindings/auto/api/ScaleTo.lua new file mode 100644 index 0000000000..9e7a5f37c4 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ScaleTo.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module ScaleTo +-------------------------------- +-- overload function: create(float, float, float) +-- +-- overload function: create(float, float) +-- +-- @function [parent=#ScaleTo] create +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @return ScaleTo#ScaleTo ret (retunr value: cc.ScaleTo) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Scene.lua b/cocos/scripting/lua-bindings/auto/api/Scene.lua new file mode 100644 index 0000000000..478824f214 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Scene.lua @@ -0,0 +1,19 @@ + +-------------------------------- +-- @module Scene +-------------------------------- +-- @function [parent=#Scene] getPhysicsWorld +-- @param self +-- @return PhysicsWorld#PhysicsWorld ret (return value: cc.PhysicsWorld) + +-------------------------------- +-- @function [parent=#Scene] create +-- @param self +-- @return Scene#Scene ret (return value: cc.Scene) + +-------------------------------- +-- @function [parent=#Scene] createWithPhysics +-- @param self +-- @return Scene#Scene ret (return value: cc.Scene) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SceneReader.lua b/cocos/scripting/lua-bindings/auto/api/SceneReader.lua new file mode 100644 index 0000000000..1af228cb42 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SceneReader.lua @@ -0,0 +1,35 @@ + +-------------------------------- +-- @module SceneReader +-------------------------------- +-- @function [parent=#SceneReader] setTarget +-- @param self +-- @param #function func + +-------------------------------- +-- @function [parent=#SceneReader] createNodeWithSceneFile +-- @param self +-- @param #string str +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#SceneReader] getNodeByTag +-- @param self +-- @param #int int +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#SceneReader] destroyInstance +-- @param self + +-------------------------------- +-- @function [parent=#SceneReader] sceneReaderVersion +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#SceneReader] getInstance +-- @param self +-- @return SceneReader#SceneReader ret (return value: ccs.SceneReader) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Scheduler.lua b/cocos/scripting/lua-bindings/auto/api/Scheduler.lua new file mode 100644 index 0000000000..27d44ef951 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Scheduler.lua @@ -0,0 +1,23 @@ + +-------------------------------- +-- @module Scheduler +-------------------------------- +-- @function [parent=#Scheduler] setTimeScale +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Scheduler] performFunctionInCocosThread +-- @param self +-- @param #function func + +-------------------------------- +-- @function [parent=#Scheduler] getTimeScale +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Scheduler] Scheduler +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ScrollView.lua b/cocos/scripting/lua-bindings/auto/api/ScrollView.lua new file mode 100644 index 0000000000..05ce95c9ee --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ScrollView.lua @@ -0,0 +1,179 @@ + +-------------------------------- +-- @module ScrollView +-------------------------------- +-- @function [parent=#ScrollView] scrollToTop +-- @param self +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] scrollToPercentHorizontal +-- @param self +-- @param #float float +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] isInertiaScrollEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ScrollView] scrollToPercentBothDirection +-- @param self +-- @param #point_table point +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] getDirection +-- @param self +-- @return SCROLLVIEW_DIR#SCROLLVIEW_DIR ret (return value: ccui.SCROLLVIEW_DIR) + +-------------------------------- +-- @function [parent=#ScrollView] scrollToBottomLeft +-- @param self +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] getInnerContainer +-- @param self +-- @return Layout#Layout ret (return value: ccui.Layout) + +-------------------------------- +-- @function [parent=#ScrollView] jumpToBottom +-- @param self + +-------------------------------- +-- @function [parent=#ScrollView] setDirection +-- @param self +-- @param #ccui.SCROLLVIEW_DIR scrollview_dir + +-------------------------------- +-- @function [parent=#ScrollView] scrollToTopLeft +-- @param self +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] jumpToTopRight +-- @param self + +-------------------------------- +-- @function [parent=#ScrollView] jumpToBottomLeft +-- @param self + +-------------------------------- +-- @function [parent=#ScrollView] setInnerContainerSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#ScrollView] getInnerContainerSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#ScrollView] isBounceEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#ScrollView] jumpToPercentVertical +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ScrollView] setInertiaScrollEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] jumpToTopLeft +-- @param self + +-------------------------------- +-- @function [parent=#ScrollView] jumpToPercentHorizontal +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#ScrollView] jumpToBottomRight +-- @param self + +-------------------------------- +-- @function [parent=#ScrollView] setBounceEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] jumpToTop +-- @param self + +-------------------------------- +-- @function [parent=#ScrollView] scrollToLeft +-- @param self +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] jumpToPercentBothDirection +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#ScrollView] scrollToPercentVertical +-- @param self +-- @param #float float +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] scrollToBottom +-- @param self +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] scrollToBottomRight +-- @param self +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] jumpToLeft +-- @param self + +-------------------------------- +-- @function [parent=#ScrollView] scrollToRight +-- @param self +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] jumpToRight +-- @param self + +-------------------------------- +-- @function [parent=#ScrollView] scrollToTopRight +-- @param self +-- @param #float float +-- @param #bool bool + +-------------------------------- +-- @function [parent=#ScrollView] create +-- @param self +-- @return ScrollView#ScrollView ret (return value: ccui.ScrollView) + +-------------------------------- +-- @function [parent=#ScrollView] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#ScrollView] ScrollView +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Sequence.lua b/cocos/scripting/lua-bindings/auto/api/Sequence.lua new file mode 100644 index 0000000000..e770e7190c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Sequence.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module Sequence +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ShaderCache.lua b/cocos/scripting/lua-bindings/auto/api/ShaderCache.lua new file mode 100644 index 0000000000..3bf11af57b --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ShaderCache.lua @@ -0,0 +1,37 @@ + +-------------------------------- +-- @module ShaderCache +-------------------------------- +-- @function [parent=#ShaderCache] reloadDefaultShaders +-- @param self + +-------------------------------- +-- @function [parent=#ShaderCache] addProgram +-- @param self +-- @param #cc.GLProgram glprogram +-- @param #string str + +-------------------------------- +-- @function [parent=#ShaderCache] getProgram +-- @param self +-- @param #string str +-- @return GLProgram#GLProgram ret (return value: cc.GLProgram) + +-------------------------------- +-- @function [parent=#ShaderCache] loadDefaultShaders +-- @param self + +-------------------------------- +-- @function [parent=#ShaderCache] destroyInstance +-- @param self + +-------------------------------- +-- @function [parent=#ShaderCache] getInstance +-- @param self +-- @return ShaderCache#ShaderCache ret (return value: cc.ShaderCache) + +-------------------------------- +-- @function [parent=#ShaderCache] ShaderCache +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Shaky3D.lua b/cocos/scripting/lua-bindings/auto/api/Shaky3D.lua new file mode 100644 index 0000000000..90375bb571 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Shaky3D.lua @@ -0,0 +1,17 @@ + +-------------------------------- +-- @module Shaky3D +-------------------------------- +-- @function [parent=#Shaky3D] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #int int +-- @param #bool bool +-- @return Shaky3D#Shaky3D ret (return value: cc.Shaky3D) + +-------------------------------- +-- @function [parent=#Shaky3D] Shaky3D +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ShakyTiles3D.lua b/cocos/scripting/lua-bindings/auto/api/ShakyTiles3D.lua new file mode 100644 index 0000000000..7c47dec8e8 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ShakyTiles3D.lua @@ -0,0 +1,13 @@ + +-------------------------------- +-- @module ShakyTiles3D +-------------------------------- +-- @function [parent=#ShakyTiles3D] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #int int +-- @param #bool bool +-- @return ShakyTiles3D#ShakyTiles3D ret (return value: cc.ShakyTiles3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ShatteredTiles3D.lua b/cocos/scripting/lua-bindings/auto/api/ShatteredTiles3D.lua new file mode 100644 index 0000000000..537a3f70db --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ShatteredTiles3D.lua @@ -0,0 +1,13 @@ + +-------------------------------- +-- @module ShatteredTiles3D +-------------------------------- +-- @function [parent=#ShatteredTiles3D] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #int int +-- @param #bool bool +-- @return ShatteredTiles3D#ShatteredTiles3D ret (return value: cc.ShatteredTiles3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Show.lua b/cocos/scripting/lua-bindings/auto/api/Show.lua new file mode 100644 index 0000000000..b787fe9124 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Show.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module Show +-------------------------------- +-- @function [parent=#Show] create +-- @param self +-- @return Show#Show ret (return value: cc.Show) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ShuffleTiles.lua b/cocos/scripting/lua-bindings/auto/api/ShuffleTiles.lua new file mode 100644 index 0000000000..013d95fbb7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ShuffleTiles.lua @@ -0,0 +1,30 @@ + +-------------------------------- +-- @module ShuffleTiles +-------------------------------- +-- @function [parent=#ShuffleTiles] placeTile +-- @param self +-- @param #point_table point +-- @param #cc.Tile tile + +-------------------------------- +-- @function [parent=#ShuffleTiles] shuffle +-- @param self +-- @param #unsigned int int +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#ShuffleTiles] getDelta +-- @param self +-- @param #size_table size +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#ShuffleTiles] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #unsigned int int +-- @return ShuffleTiles#ShuffleTiles ret (return value: cc.ShuffleTiles) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SimpleAudioEngine.lua b/cocos/scripting/lua-bindings/auto/api/SimpleAudioEngine.lua new file mode 100644 index 0000000000..0c41707144 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SimpleAudioEngine.lua @@ -0,0 +1,117 @@ + +-------------------------------- +-- @module SimpleAudioEngine +-------------------------------- +-- @function [parent=#SimpleAudioEngine] preloadBackgroundMusic +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] stopBackgroundMusic +-- @param self + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] stopAllEffects +-- @param self + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] getBackgroundMusicVolume +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] resumeBackgroundMusic +-- @param self + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] setBackgroundMusicVolume +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] preloadEffect +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] isBackgroundMusicPlaying +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] getEffectsVolume +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] willPlayBackgroundMusic +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] pauseEffect +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] playEffect +-- @param self +-- @param #char char +-- @param #bool bool +-- @param #float float +-- @param #float float +-- @param #float float +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] rewindBackgroundMusic +-- @param self + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] playBackgroundMusic +-- @param self +-- @param #char char +-- @param #bool bool + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] resumeAllEffects +-- @param self + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] setEffectsVolume +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] stopEffect +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] pauseBackgroundMusic +-- @param self + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] pauseAllEffects +-- @param self + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] unloadEffect +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] resumeEffect +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] end +-- @param self + +-------------------------------- +-- @function [parent=#SimpleAudioEngine] getInstance +-- @param self +-- @return SimpleAudioEngine#SimpleAudioEngine ret (return value: cc.SimpleAudioEngine) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Skeleton.lua b/cocos/scripting/lua-bindings/auto/api/Skeleton.lua new file mode 100644 index 0000000000..26450b884b --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Skeleton.lua @@ -0,0 +1,38 @@ + +-------------------------------- +-- @module Skeleton +-------------------------------- +-- @function [parent=#Skeleton] setToSetupPose +-- @param self + +-------------------------------- +-- @function [parent=#Skeleton] setBlendFunc +-- @param self +-- @param #cc.BlendFunc blendfunc + +-------------------------------- +-- @function [parent=#Skeleton] onDraw +-- @param self +-- @param #kmMat4 kmmat4 +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Skeleton] setSlotsToSetupPose +-- @param self + +-------------------------------- +-- @function [parent=#Skeleton] getBlendFunc +-- @param self +-- @return BlendFunc#BlendFunc ret (return value: cc.BlendFunc) + +-------------------------------- +-- @function [parent=#Skeleton] setSkin +-- @param self +-- @param #char char +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Skeleton] setBonesToSetupPose +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SkeletonAnimation.lua b/cocos/scripting/lua-bindings/auto/api/SkeletonAnimation.lua new file mode 100644 index 0000000000..0dc15cd0e1 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SkeletonAnimation.lua @@ -0,0 +1,49 @@ + +-------------------------------- +-- @module SkeletonAnimation +-------------------------------- +-- @function [parent=#SkeletonAnimation] addAnimation +-- @param self +-- @param #int int +-- @param #char char +-- @param #bool bool +-- @param #float float +-- @return spTrackEntry#spTrackEntry ret (return value: spTrackEntry) + +-------------------------------- +-- @function [parent=#SkeletonAnimation] getCurrent +-- @param self +-- @return spTrackEntry#spTrackEntry ret (return value: spTrackEntry) + +-------------------------------- +-- @function [parent=#SkeletonAnimation] setMix +-- @param self +-- @param #char char +-- @param #char char +-- @param #float float + +-------------------------------- +-- @function [parent=#SkeletonAnimation] setAnimation +-- @param self +-- @param #int int +-- @param #char char +-- @param #bool bool +-- @return spTrackEntry#spTrackEntry ret (return value: spTrackEntry) + +-------------------------------- +-- @function [parent=#SkeletonAnimation] clearTracks +-- @param self + +-------------------------------- +-- @function [parent=#SkeletonAnimation] clearTrack +-- @param self + +-------------------------------- +-- @function [parent=#SkeletonAnimation] onAnimationStateEvent +-- @param self +-- @param #int int +-- @param #spEventType speventtype +-- @param #spEvent spevent +-- @param #int int + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SkewBy.lua b/cocos/scripting/lua-bindings/auto/api/SkewBy.lua new file mode 100644 index 0000000000..5a4c3fe0dd --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SkewBy.lua @@ -0,0 +1,12 @@ + +-------------------------------- +-- @module SkewBy +-------------------------------- +-- @function [parent=#SkewBy] create +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @return SkewBy#SkewBy ret (return value: cc.SkewBy) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SkewTo.lua b/cocos/scripting/lua-bindings/auto/api/SkewTo.lua new file mode 100644 index 0000000000..bae9ddba3b --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SkewTo.lua @@ -0,0 +1,12 @@ + +-------------------------------- +-- @module SkewTo +-------------------------------- +-- @function [parent=#SkewTo] create +-- @param self +-- @param #float float +-- @param #float float +-- @param #float float +-- @return SkewTo#SkewTo ret (return value: cc.SkewTo) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Skin.lua b/cocos/scripting/lua-bindings/auto/api/Skin.lua new file mode 100644 index 0000000000..22e20b8061 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Skin.lua @@ -0,0 +1,60 @@ + +-------------------------------- +-- @module Skin +-------------------------------- +-- @function [parent=#Skin] getBone +-- @param self +-- @return Bone#Bone ret (return value: ccs.Bone) + +-------------------------------- +-- @function [parent=#Skin] getNodeToWorldTransformAR +-- @param self +-- @return kmMat4#kmMat4 ret (return value: kmMat4) + +-------------------------------- +-- @function [parent=#Skin] initWithFile +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Skin] getDisplayName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#Skin] updateArmatureTransform +-- @param self + +-------------------------------- +-- @function [parent=#Skin] initWithSpriteFrameName +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Skin] setBone +-- @param self +-- @param #ccs.Bone bone + +-------------------------------- +-- overload function: create(string) +-- +-- overload function: create() +-- +-- @function [parent=#Skin] create +-- @param self +-- @param #string str +-- @return Skin#Skin ret (retunr value: ccs.Skin) + +-------------------------------- +-- @function [parent=#Skin] createWithSpriteFrameName +-- @param self +-- @param #string str +-- @return Skin#Skin ret (return value: ccs.Skin) + +-------------------------------- +-- @function [parent=#Skin] Skin +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Slider.lua b/cocos/scripting/lua-bindings/auto/api/Slider.lua new file mode 100644 index 0000000000..b2793869c7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Slider.lua @@ -0,0 +1,101 @@ + +-------------------------------- +-- @module Slider +-------------------------------- +-- @function [parent=#Slider] setPercent +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Slider] loadSlidBallTextureDisabled +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Slider] loadSlidBallTextureNormal +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Slider] loadBarTexture +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Slider] loadProgressBarTexture +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Slider] loadSlidBallTextures +-- @param self +-- @param #char char +-- @param #char char +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Slider] setCapInsetProgressBarRebderer +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Slider] setCapInsetsBarRenderer +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Slider] getCapInsetsProgressBarRebderer +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#Slider] setScale9Enabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Slider] setCapInsets +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Slider] loadSlidBallTexturePressed +-- @param self +-- @param #char char +-- @param #ccui.TextureResType texturerestype + +-------------------------------- +-- @function [parent=#Slider] isScale9Enabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Slider] getCapInsetsBarRenderer +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#Slider] getPercent +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Slider] create +-- @param self +-- @return Slider#Slider ret (return value: ccui.Slider) + +-------------------------------- +-- @function [parent=#Slider] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#Slider] Slider +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Spawn.lua b/cocos/scripting/lua-bindings/auto/api/Spawn.lua new file mode 100644 index 0000000000..af95c460d7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Spawn.lua @@ -0,0 +1,4 @@ + +-------------------------------- +-- @module Spawn +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Speed.lua b/cocos/scripting/lua-bindings/auto/api/Speed.lua new file mode 100644 index 0000000000..219d9fbbde --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Speed.lua @@ -0,0 +1,31 @@ + +-------------------------------- +-- @module Speed +-------------------------------- +-- @function [parent=#Speed] setInnerAction +-- @param self +-- @param #cc.ActionInterval actioninterval + +-------------------------------- +-- @function [parent=#Speed] setSpeed +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Speed] getInnerAction +-- @param self +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#Speed] getSpeed +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Speed] create +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @param #float float +-- @return Speed#Speed ret (return value: cc.Speed) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SplitCols.lua b/cocos/scripting/lua-bindings/auto/api/SplitCols.lua new file mode 100644 index 0000000000..821f093efa --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SplitCols.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module SplitCols +-------------------------------- +-- @function [parent=#SplitCols] create +-- @param self +-- @param #float float +-- @param #unsigned int int +-- @return SplitCols#SplitCols ret (return value: cc.SplitCols) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SplitRows.lua b/cocos/scripting/lua-bindings/auto/api/SplitRows.lua new file mode 100644 index 0000000000..f2e5566cc0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SplitRows.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module SplitRows +-------------------------------- +-- @function [parent=#SplitRows] create +-- @param self +-- @param #float float +-- @param #unsigned int int +-- @return SplitRows#SplitRows ret (return value: cc.SplitRows) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Sprite.lua b/cocos/scripting/lua-bindings/auto/api/Sprite.lua new file mode 100644 index 0000000000..aa4eba19a7 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Sprite.lua @@ -0,0 +1,181 @@ + +-------------------------------- +-- @module Sprite +-------------------------------- +-- overload function: setSpriteFrame(cc.SpriteFrame) +-- +-- overload function: setSpriteFrame(string) +-- +-- @function [parent=#Sprite] setSpriteFrame +-- @param self +-- @param #string str + +-------------------------------- +-- overload function: setTexture(cc.Texture2D) +-- +-- overload function: setTexture(string) +-- +-- @function [parent=#Sprite] setTexture +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#Sprite] getTexture +-- @param self +-- @return Texture2D#Texture2D ret (return value: cc.Texture2D) + +-------------------------------- +-- @function [parent=#Sprite] setFlippedY +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Sprite] setFlippedX +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Sprite] getBatchNode +-- @param self +-- @return SpriteBatchNode#SpriteBatchNode ret (return value: cc.SpriteBatchNode) + +-------------------------------- +-- @function [parent=#Sprite] getOffsetPosition +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Sprite] removeAllChildrenWithCleanup +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Sprite] updateTransform +-- @param self + +-------------------------------- +-- overload function: setTextureRect(rect_table, bool, size_table) +-- +-- overload function: setTextureRect(rect_table) +-- +-- @function [parent=#Sprite] setTextureRect +-- @param self +-- @param #rect_table rect +-- @param #bool bool +-- @param #size_table size + +-------------------------------- +-- @function [parent=#Sprite] isFrameDisplayed +-- @param self +-- @param #cc.SpriteFrame spriteframe +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Sprite] getAtlasIndex +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#Sprite] setBatchNode +-- @param self +-- @param #cc.SpriteBatchNode spritebatchnode + +-------------------------------- +-- @function [parent=#Sprite] setDisplayFrameWithAnimationName +-- @param self +-- @param #string str +-- @param #long long + +-------------------------------- +-- @function [parent=#Sprite] setTextureAtlas +-- @param self +-- @param #cc.TextureAtlas textureatlas + +-------------------------------- +-- @function [parent=#Sprite] getSpriteFrame +-- @param self +-- @return SpriteFrame#SpriteFrame ret (return value: cc.SpriteFrame) + +-------------------------------- +-- @function [parent=#Sprite] isDirty +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Sprite] setAtlasIndex +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#Sprite] setDirty +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Sprite] isTextureRectRotated +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Sprite] getTextureRect +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#Sprite] getTextureAtlas +-- @param self +-- @return TextureAtlas#TextureAtlas ret (return value: cc.TextureAtlas) + +-------------------------------- +-- @function [parent=#Sprite] isFlippedX +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Sprite] isFlippedY +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Sprite] setVertexRect +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- overload function: create(string) +-- +-- overload function: create() +-- +-- overload function: create(string, rect_table) +-- +-- @function [parent=#Sprite] create +-- @param self +-- @param #string str +-- @param #rect_table rect +-- @return Sprite#Sprite ret (retunr value: cc.Sprite) + +-------------------------------- +-- overload function: createWithTexture(cc.Texture2D, rect_table, bool) +-- +-- overload function: createWithTexture(cc.Texture2D) +-- +-- @function [parent=#Sprite] createWithTexture +-- @param self +-- @param #cc.Texture2D texture2d +-- @param #rect_table rect +-- @param #bool bool +-- @return Sprite#Sprite ret (retunr value: cc.Sprite) + +-------------------------------- +-- @function [parent=#Sprite] createWithSpriteFrameName +-- @param self +-- @param #string str +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#Sprite] createWithSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SpriteBatchNode.lua b/cocos/scripting/lua-bindings/auto/api/SpriteBatchNode.lua new file mode 100644 index 0000000000..36d4238188 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SpriteBatchNode.lua @@ -0,0 +1,122 @@ + +-------------------------------- +-- @module SpriteBatchNode +-------------------------------- +-- @function [parent=#SpriteBatchNode] appendChild +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#SpriteBatchNode] reorderBatch +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#SpriteBatchNode] initWithTexture +-- @param self +-- @param #cc.Texture2D texture2d +-- @param #long long +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] lowestAtlasIndexInChild +-- @param self +-- @param #cc.Sprite sprite +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] atlasIndexForChild +-- @param self +-- @param #cc.Sprite sprite +-- @param #int int +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] setTextureAtlas +-- @param self +-- @param #cc.TextureAtlas textureatlas + +-------------------------------- +-- @function [parent=#SpriteBatchNode] rebuildIndexInOrder +-- @param self +-- @param #cc.Sprite sprite +-- @param #long long +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] increaseAtlasCapacity +-- @param self + +-------------------------------- +-- @function [parent=#SpriteBatchNode] getTextureAtlas +-- @param self +-- @return TextureAtlas#TextureAtlas ret (return value: cc.TextureAtlas) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] insertQuadFromSprite +-- @param self +-- @param #cc.Sprite sprite +-- @param #long long + +-------------------------------- +-- overload function: init() +-- +-- overload function: init(string, long) +-- +-- @function [parent=#SpriteBatchNode] init +-- @param self +-- @param #string str +-- @param #long long +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] setTexture +-- @param self +-- @param #cc.Texture2D texture2d + +-------------------------------- +-- @function [parent=#SpriteBatchNode] getTexture +-- @param self +-- @return Texture2D#Texture2D ret (return value: cc.Texture2D) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] highestAtlasIndexInChild +-- @param self +-- @param #cc.Sprite sprite +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] removeChildAtIndex +-- @param self +-- @param #long long +-- @param #bool bool + +-------------------------------- +-- @function [parent=#SpriteBatchNode] removeAllChildrenWithCleanup +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#SpriteBatchNode] removeSpriteFromAtlas +-- @param self +-- @param #cc.Sprite sprite + +-------------------------------- +-- @function [parent=#SpriteBatchNode] create +-- @param self +-- @param #string str +-- @param #long long +-- @return SpriteBatchNode#SpriteBatchNode ret (return value: cc.SpriteBatchNode) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] createWithTexture +-- @param self +-- @param #cc.Texture2D texture2d +-- @param #long long +-- @return SpriteBatchNode#SpriteBatchNode ret (return value: cc.SpriteBatchNode) + +-------------------------------- +-- @function [parent=#SpriteBatchNode] SpriteBatchNode +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SpriteDisplayData.lua b/cocos/scripting/lua-bindings/auto/api/SpriteDisplayData.lua new file mode 100644 index 0000000000..6873c946b3 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SpriteDisplayData.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module SpriteDisplayData +-------------------------------- +-- @function [parent=#SpriteDisplayData] copy +-- @param self +-- @param #ccs.DisplayData displaydata + +-------------------------------- +-- @function [parent=#SpriteDisplayData] create +-- @param self +-- @return SpriteDisplayData#SpriteDisplayData ret (return value: ccs.SpriteDisplayData) + +-------------------------------- +-- @function [parent=#SpriteDisplayData] SpriteDisplayData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SpriteFrame.lua b/cocos/scripting/lua-bindings/auto/api/SpriteFrame.lua new file mode 100644 index 0000000000..1c05dc217a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SpriteFrame.lua @@ -0,0 +1,145 @@ + +-------------------------------- +-- @module SpriteFrame +-------------------------------- +-- @function [parent=#SpriteFrame] clone +-- @param self +-- @return SpriteFrame#SpriteFrame ret (return value: cc.SpriteFrame) + +-------------------------------- +-- @function [parent=#SpriteFrame] setRotated +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#SpriteFrame] setTexture +-- @param self +-- @param #cc.Texture2D texture2d + +-------------------------------- +-- overload function: initWithTexture(cc.Texture2D, rect_table, bool, point_table, size_table) +-- +-- overload function: initWithTexture(cc.Texture2D, rect_table) +-- +-- @function [parent=#SpriteFrame] initWithTexture +-- @param self +-- @param #cc.Texture2D texture2d +-- @param #rect_table rect +-- @param #bool bool +-- @param #point_table point +-- @param #size_table size +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#SpriteFrame] setRectInPixels +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#SpriteFrame] getTexture +-- @param self +-- @return Texture2D#Texture2D ret (return value: cc.Texture2D) + +-------------------------------- +-- @function [parent=#SpriteFrame] getRect +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#SpriteFrame] setOffsetInPixels +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#SpriteFrame] getRectInPixels +-- @param self +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#SpriteFrame] setOriginalSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#SpriteFrame] getOriginalSizeInPixels +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#SpriteFrame] setOriginalSizeInPixels +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#SpriteFrame] setOffset +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#SpriteFrame] getOffset +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#SpriteFrame] isRotated +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- overload function: initWithTextureFilename(string, rect_table, bool, point_table, size_table) +-- +-- overload function: initWithTextureFilename(string, rect_table) +-- +-- @function [parent=#SpriteFrame] initWithTextureFilename +-- @param self +-- @param #string str +-- @param #rect_table rect +-- @param #bool bool +-- @param #point_table point +-- @param #size_table size +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#SpriteFrame] setRect +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#SpriteFrame] getOffsetInPixels +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#SpriteFrame] getOriginalSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- overload function: create(string, rect_table, bool, point_table, size_table) +-- +-- overload function: create(string, rect_table) +-- +-- @function [parent=#SpriteFrame] create +-- @param self +-- @param #string str +-- @param #rect_table rect +-- @param #bool bool +-- @param #point_table point +-- @param #size_table size +-- @return SpriteFrame#SpriteFrame ret (retunr value: cc.SpriteFrame) + +-------------------------------- +-- overload function: createWithTexture(cc.Texture2D, rect_table, bool, point_table, size_table) +-- +-- overload function: createWithTexture(cc.Texture2D, rect_table) +-- +-- @function [parent=#SpriteFrame] createWithTexture +-- @param self +-- @param #cc.Texture2D texture2d +-- @param #rect_table rect +-- @param #bool bool +-- @param #point_table point +-- @param #size_table size +-- @return SpriteFrame#SpriteFrame ret (retunr value: cc.SpriteFrame) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/SpriteFrameCache.lua b/cocos/scripting/lua-bindings/auto/api/SpriteFrameCache.lua new file mode 100644 index 0000000000..6fb668e85d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/SpriteFrameCache.lua @@ -0,0 +1,65 @@ + +-------------------------------- +-- @module SpriteFrameCache +-------------------------------- +-- overload function: addSpriteFramesWithFile(string, string) +-- +-- overload function: addSpriteFramesWithFile(string) +-- +-- overload function: addSpriteFramesWithFile(string, cc.Texture2D) +-- +-- @function [parent=#SpriteFrameCache] addSpriteFramesWithFile +-- @param self +-- @param #string str +-- @param #cc.Texture2D texture2d + +-------------------------------- +-- @function [parent=#SpriteFrameCache] addSpriteFrame +-- @param self +-- @param #cc.SpriteFrame spriteframe +-- @param #string str + +-------------------------------- +-- @function [parent=#SpriteFrameCache] removeUnusedSpriteFrames +-- @param self + +-------------------------------- +-- @function [parent=#SpriteFrameCache] getSpriteFrameByName +-- @param self +-- @param #string str +-- @return SpriteFrame#SpriteFrame ret (return value: cc.SpriteFrame) + +-------------------------------- +-- @function [parent=#SpriteFrameCache] removeSpriteFramesFromFile +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#SpriteFrameCache] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#SpriteFrameCache] removeSpriteFrames +-- @param self + +-------------------------------- +-- @function [parent=#SpriteFrameCache] removeSpriteFramesFromTexture +-- @param self +-- @param #cc.Texture2D texture2d + +-------------------------------- +-- @function [parent=#SpriteFrameCache] removeSpriteFrameByName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#SpriteFrameCache] destroyInstance +-- @param self + +-------------------------------- +-- @function [parent=#SpriteFrameCache] getInstance +-- @param self +-- @return SpriteFrameCache#SpriteFrameCache ret (return value: cc.SpriteFrameCache) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/StopGrid.lua b/cocos/scripting/lua-bindings/auto/api/StopGrid.lua new file mode 100644 index 0000000000..3d5fdb8290 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/StopGrid.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module StopGrid +-------------------------------- +-- @function [parent=#StopGrid] create +-- @param self +-- @return StopGrid#StopGrid ret (return value: cc.StopGrid) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TMXLayer.lua b/cocos/scripting/lua-bindings/auto/api/TMXLayer.lua new file mode 100644 index 0000000000..248fbce6a4 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TMXLayer.lua @@ -0,0 +1,142 @@ + +-------------------------------- +-- @module TMXLayer +-------------------------------- +-- @function [parent=#TMXLayer] getTileGIDAt +-- @param self +-- @param #point_table point +-- @param #cc.TMXTileFlags_ tmxtileflags_ +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- @function [parent=#TMXLayer] getPositionAt +-- @param self +-- @param #point_table point +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#TMXLayer] setLayerOrientation +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#TMXLayer] releaseMap +-- @param self + +-------------------------------- +-- @function [parent=#TMXLayer] setTiles +-- @param self +-- @param #unsigned int int + +-------------------------------- +-- @function [parent=#TMXLayer] getLayerSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#TMXLayer] setMapTileSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#TMXLayer] getLayerOrientation +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#TMXLayer] setProperties +-- @param self +-- @param #map_table map + +-------------------------------- +-- @function [parent=#TMXLayer] setLayerName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#TMXLayer] removeTileAt +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#TMXLayer] initWithTilesetInfo +-- @param self +-- @param #cc.TMXTilesetInfo tmxtilesetinfo +-- @param #cc.TMXLayerInfo tmxlayerinfo +-- @param #cc.TMXMapInfo map +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TMXLayer] setupTiles +-- @param self + +-------------------------------- +-- overload function: setTileGID(unsigned int, point_table, cc.TMXTileFlags_) +-- +-- overload function: setTileGID(unsigned int, point_table) +-- +-- @function [parent=#TMXLayer] setTileGID +-- @param self +-- @param #unsigned int int +-- @param #point_table point +-- @param #cc.TMXTileFlags_ tmxtileflags_ + +-------------------------------- +-- @function [parent=#TMXLayer] getMapTileSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#TMXLayer] getProperty +-- @param self +-- @param #string str +-- @return Value#Value ret (return value: cc.Value) + +-------------------------------- +-- @function [parent=#TMXLayer] setLayerSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#TMXLayer] getLayerName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#TMXLayer] setTileSet +-- @param self +-- @param #cc.TMXTilesetInfo tmxtilesetinfo + +-------------------------------- +-- @function [parent=#TMXLayer] getTileSet +-- @param self +-- @return TMXTilesetInfo#TMXTilesetInfo ret (return value: cc.TMXTilesetInfo) + +-------------------------------- +-- overload function: getProperties() +-- +-- overload function: getProperties() +-- +-- @function [parent=#TMXLayer] getProperties +-- @param self +-- @return map_table#map_table ret (retunr value: map_table) + +-------------------------------- +-- @function [parent=#TMXLayer] getTileAt +-- @param self +-- @param #point_table point +-- @return Sprite#Sprite ret (return value: cc.Sprite) + +-------------------------------- +-- @function [parent=#TMXLayer] create +-- @param self +-- @param #cc.TMXTilesetInfo tmxtilesetinfo +-- @param #cc.TMXLayerInfo tmxlayerinfo +-- @param #cc.TMXMapInfo map +-- @return TMXLayer#TMXLayer ret (return value: cc.TMXLayer) + +-------------------------------- +-- @function [parent=#TMXLayer] TMXLayer +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TMXLayerInfo.lua b/cocos/scripting/lua-bindings/auto/api/TMXLayerInfo.lua new file mode 100644 index 0000000000..be3f08f41d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TMXLayerInfo.lua @@ -0,0 +1,18 @@ + +-------------------------------- +-- @module TMXLayerInfo +-------------------------------- +-- @function [parent=#TMXLayerInfo] setProperties +-- @param self +-- @param #map_table map + +-------------------------------- +-- @function [parent=#TMXLayerInfo] getProperties +-- @param self +-- @return map_table#map_table ret (return value: map_table) + +-------------------------------- +-- @function [parent=#TMXLayerInfo] TMXLayerInfo +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TMXMapInfo.lua b/cocos/scripting/lua-bindings/auto/api/TMXMapInfo.lua new file mode 100644 index 0000000000..73b1458bcf --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TMXMapInfo.lua @@ -0,0 +1,202 @@ + +-------------------------------- +-- @module TMXMapInfo +-------------------------------- +-- @function [parent=#TMXMapInfo] setObjectGroups +-- @param self +-- @param #array_table array + +-------------------------------- +-- @function [parent=#TMXMapInfo] setTileSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#TMXMapInfo] initWithTMXFile +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TMXMapInfo] getOrientation +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#TMXMapInfo] isStoringCharacters +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TMXMapInfo] setLayers +-- @param self +-- @param #array_table array + +-------------------------------- +-- @function [parent=#TMXMapInfo] parseXMLFile +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TMXMapInfo] getParentElement +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#TMXMapInfo] setTMXFileName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#TMXMapInfo] parseXMLString +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- overload function: getLayers() +-- +-- overload function: getLayers() +-- +-- @function [parent=#TMXMapInfo] getLayers +-- @param self +-- @return array_table#array_table ret (retunr value: array_table) + +-------------------------------- +-- overload function: getTilesets() +-- +-- overload function: getTilesets() +-- +-- @function [parent=#TMXMapInfo] getTilesets +-- @param self +-- @return array_table#array_table ret (retunr value: array_table) + +-------------------------------- +-- @function [parent=#TMXMapInfo] getParentGID +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#TMXMapInfo] setParentElement +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#TMXMapInfo] initWithXML +-- @param self +-- @param #string str +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TMXMapInfo] setParentGID +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#TMXMapInfo] getLayerAttribs +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#TMXMapInfo] getTileSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#TMXMapInfo] getTileProperties +-- @param self +-- @return map_table#map_table ret (return value: map_table) + +-------------------------------- +-- overload function: getObjectGroups() +-- +-- overload function: getObjectGroups() +-- +-- @function [parent=#TMXMapInfo] getObjectGroups +-- @param self +-- @return array_table#array_table ret (retunr value: array_table) + +-------------------------------- +-- @function [parent=#TMXMapInfo] getTMXFileName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#TMXMapInfo] setCurrentString +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#TMXMapInfo] setProperties +-- @param self +-- @param #map_table map + +-------------------------------- +-- @function [parent=#TMXMapInfo] setOrientation +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#TMXMapInfo] setTileProperties +-- @param self +-- @param #map_table map + +-------------------------------- +-- @function [parent=#TMXMapInfo] setMapSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#TMXMapInfo] setStoringCharacters +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#TMXMapInfo] getMapSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#TMXMapInfo] setTilesets +-- @param self +-- @param #array_table array + +-------------------------------- +-- overload function: getProperties() +-- +-- overload function: getProperties() +-- +-- @function [parent=#TMXMapInfo] getProperties +-- @param self +-- @return map_table#map_table ret (retunr value: map_table) + +-------------------------------- +-- @function [parent=#TMXMapInfo] getCurrentString +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#TMXMapInfo] setLayerAttribs +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#TMXMapInfo] create +-- @param self +-- @param #string str +-- @return TMXMapInfo#TMXMapInfo ret (return value: cc.TMXMapInfo) + +-------------------------------- +-- @function [parent=#TMXMapInfo] createWithXML +-- @param self +-- @param #string str +-- @param #string str +-- @return TMXMapInfo#TMXMapInfo ret (return value: cc.TMXMapInfo) + +-------------------------------- +-- @function [parent=#TMXMapInfo] TMXMapInfo +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TMXObjectGroup.lua b/cocos/scripting/lua-bindings/auto/api/TMXObjectGroup.lua new file mode 100644 index 0000000000..60f6b029a5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TMXObjectGroup.lua @@ -0,0 +1,68 @@ + +-------------------------------- +-- @module TMXObjectGroup +-------------------------------- +-- @function [parent=#TMXObjectGroup] setPositionOffset +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#TMXObjectGroup] getProperty +-- @param self +-- @param #string str +-- @return Value#Value ret (return value: cc.Value) + +-------------------------------- +-- @function [parent=#TMXObjectGroup] getPositionOffset +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#TMXObjectGroup] getObject +-- @param self +-- @param #string str +-- @return map_table#map_table ret (return value: map_table) + +-------------------------------- +-- overload function: getObjects() +-- +-- overload function: getObjects() +-- +-- @function [parent=#TMXObjectGroup] getObjects +-- @param self +-- @return array_table#array_table ret (retunr value: array_table) + +-------------------------------- +-- @function [parent=#TMXObjectGroup] setGroupName +-- @param self +-- @param #string str + +-------------------------------- +-- overload function: getProperties() +-- +-- overload function: getProperties() +-- +-- @function [parent=#TMXObjectGroup] getProperties +-- @param self +-- @return map_table#map_table ret (retunr value: map_table) + +-------------------------------- +-- @function [parent=#TMXObjectGroup] getGroupName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#TMXObjectGroup] setProperties +-- @param self +-- @param #map_table map + +-------------------------------- +-- @function [parent=#TMXObjectGroup] setObjects +-- @param self +-- @param #array_table array + +-------------------------------- +-- @function [parent=#TMXObjectGroup] TMXObjectGroup +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TMXTiledMap.lua b/cocos/scripting/lua-bindings/auto/api/TMXTiledMap.lua new file mode 100644 index 0000000000..637be57094 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TMXTiledMap.lua @@ -0,0 +1,89 @@ + +-------------------------------- +-- @module TMXTiledMap +-------------------------------- +-- @function [parent=#TMXTiledMap] setObjectGroups +-- @param self +-- @param #array_table array + +-------------------------------- +-- @function [parent=#TMXTiledMap] getProperty +-- @param self +-- @param #string str +-- @return Value#Value ret (return value: cc.Value) + +-------------------------------- +-- @function [parent=#TMXTiledMap] setMapSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#TMXTiledMap] getObjectGroup +-- @param self +-- @param #string str +-- @return TMXObjectGroup#TMXObjectGroup ret (return value: cc.TMXObjectGroup) + +-------------------------------- +-- overload function: getObjectGroups() +-- +-- overload function: getObjectGroups() +-- +-- @function [parent=#TMXTiledMap] getObjectGroups +-- @param self +-- @return array_table#array_table ret (retunr value: array_table) + +-------------------------------- +-- @function [parent=#TMXTiledMap] getTileSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#TMXTiledMap] getMapSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#TMXTiledMap] getProperties +-- @param self +-- @return map_table#map_table ret (return value: map_table) + +-------------------------------- +-- @function [parent=#TMXTiledMap] setTileSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#TMXTiledMap] setProperties +-- @param self +-- @param #map_table map + +-------------------------------- +-- @function [parent=#TMXTiledMap] getLayer +-- @param self +-- @param #string str +-- @return TMXLayer#TMXLayer ret (return value: cc.TMXLayer) + +-------------------------------- +-- @function [parent=#TMXTiledMap] getMapOrientation +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#TMXTiledMap] setMapOrientation +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#TMXTiledMap] create +-- @param self +-- @param #string str +-- @return TMXTiledMap#TMXTiledMap ret (return value: cc.TMXTiledMap) + +-------------------------------- +-- @function [parent=#TMXTiledMap] createWithXML +-- @param self +-- @param #string str +-- @param #string str +-- @return TMXTiledMap#TMXTiledMap ret (return value: cc.TMXTiledMap) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TMXTilesetInfo.lua b/cocos/scripting/lua-bindings/auto/api/TMXTilesetInfo.lua new file mode 100644 index 0000000000..1e4efd72bf --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TMXTilesetInfo.lua @@ -0,0 +1,14 @@ + +-------------------------------- +-- @module TMXTilesetInfo +-------------------------------- +-- @function [parent=#TMXTilesetInfo] getRectForGID +-- @param self +-- @param #unsigned int int +-- @return rect_table#rect_table ret (return value: rect_table) + +-------------------------------- +-- @function [parent=#TMXTilesetInfo] TMXTilesetInfo +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TableView.lua b/cocos/scripting/lua-bindings/auto/api/TableView.lua new file mode 100644 index 0000000000..75fbba115c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TableView.lua @@ -0,0 +1,69 @@ + +-------------------------------- +-- @module TableView +-------------------------------- +-- @function [parent=#TableView] updateCellAtIndex +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#TableView] setVerticalFillOrder +-- @param self +-- @param #cc.TableView::VerticalFillOrder verticalfillorder + +-------------------------------- +-- @function [parent=#TableView] scrollViewDidZoom +-- @param self +-- @param #cc.ScrollView scrollview + +-------------------------------- +-- @function [parent=#TableView] _updateContentSize +-- @param self + +-------------------------------- +-- @function [parent=#TableView] getVerticalFillOrder +-- @param self +-- @return TableView::VerticalFillOrder#TableView::VerticalFillOrder ret (return value: cc.TableView::VerticalFillOrder) + +-------------------------------- +-- @function [parent=#TableView] removeCellAtIndex +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#TableView] initWithViewSize +-- @param self +-- @param #size_table size +-- @param #cc.Node node +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TableView] scrollViewDidScroll +-- @param self +-- @param #cc.ScrollView scrollview + +-------------------------------- +-- @function [parent=#TableView] reloadData +-- @param self + +-------------------------------- +-- @function [parent=#TableView] insertCellAtIndex +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#TableView] cellAtIndex +-- @param self +-- @param #long long +-- @return TableViewCell#TableViewCell ret (return value: cc.TableViewCell) + +-------------------------------- +-- @function [parent=#TableView] dequeueCell +-- @param self +-- @return TableViewCell#TableViewCell ret (return value: cc.TableViewCell) + +-------------------------------- +-- @function [parent=#TableView] TableView +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TableViewCell.lua b/cocos/scripting/lua-bindings/auto/api/TableViewCell.lua new file mode 100644 index 0000000000..d118477c1a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TableViewCell.lua @@ -0,0 +1,27 @@ + +-------------------------------- +-- @module TableViewCell +-------------------------------- +-- @function [parent=#TableViewCell] reset +-- @param self + +-------------------------------- +-- @function [parent=#TableViewCell] getIdx +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#TableViewCell] setIdx +-- @param self +-- @param #long long + +-------------------------------- +-- @function [parent=#TableViewCell] create +-- @param self +-- @return TableViewCell#TableViewCell ret (return value: cc.TableViewCell) + +-------------------------------- +-- @function [parent=#TableViewCell] TableViewCell +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TargetedAction.lua b/cocos/scripting/lua-bindings/auto/api/TargetedAction.lua new file mode 100644 index 0000000000..503d3c897f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TargetedAction.lua @@ -0,0 +1,25 @@ + +-------------------------------- +-- @module TargetedAction +-------------------------------- +-- overload function: getForcedTarget() +-- +-- overload function: getForcedTarget() +-- +-- @function [parent=#TargetedAction] getForcedTarget +-- @param self +-- @return Node#Node ret (retunr value: cc.Node) + +-------------------------------- +-- @function [parent=#TargetedAction] setForcedTarget +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#TargetedAction] create +-- @param self +-- @param #cc.Node node +-- @param #cc.FiniteTimeAction finitetimeaction +-- @return TargetedAction#TargetedAction ret (return value: cc.TargetedAction) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Text.lua b/cocos/scripting/lua-bindings/auto/api/Text.lua new file mode 100644 index 0000000000..2ad87d7508 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Text.lua @@ -0,0 +1,93 @@ + +-------------------------------- +-- @module Text +-------------------------------- +-- @function [parent=#Text] getStringLength +-- @param self +-- @return long#long ret (return value: long) + +-------------------------------- +-- @function [parent=#Text] setFontName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#Text] setTouchScaleChangeEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Text] getFontSize +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Text] getTextVerticalAlignment +-- @param self +-- @return TextVAlignment#TextVAlignment ret (return value: cc.TextVAlignment) + +-------------------------------- +-- @function [parent=#Text] getStringValue +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#Text] setText +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#Text] getTextHorizontalAlignment +-- @param self +-- @return TextHAlignment#TextHAlignment ret (return value: cc.TextHAlignment) + +-------------------------------- +-- @function [parent=#Text] getTextAreaSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Text] setTextVerticalAlignment +-- @param self +-- @param #cc.TextVAlignment textvalignment + +-------------------------------- +-- @function [parent=#Text] setFontSize +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Text] isTouchScaleChangeEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Text] setTextHorizontalAlignment +-- @param self +-- @param #cc.TextHAlignment texthalignment + +-------------------------------- +-- @function [parent=#Text] getFontName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#Text] setTextAreaSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#Text] create +-- @param self +-- @return Text#Text ret (return value: ccui.Text) + +-------------------------------- +-- @function [parent=#Text] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#Text] Text +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TextAtlas.lua b/cocos/scripting/lua-bindings/auto/api/TextAtlas.lua new file mode 100644 index 0000000000..8ed5b484d0 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TextAtlas.lua @@ -0,0 +1,37 @@ + +-------------------------------- +-- @module TextAtlas +-------------------------------- +-- @function [parent=#TextAtlas] setProperty +-- @param self +-- @param #string str +-- @param #string str +-- @param #int int +-- @param #int int +-- @param #string str + +-------------------------------- +-- @function [parent=#TextAtlas] getStringValue +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#TextAtlas] setStringValue +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#TextAtlas] create +-- @param self +-- @return TextAtlas#TextAtlas ret (return value: ccui.TextAtlas) + +-------------------------------- +-- @function [parent=#TextAtlas] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#TextAtlas] TextAtlas +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TextBMFont.lua b/cocos/scripting/lua-bindings/auto/api/TextBMFont.lua new file mode 100644 index 0000000000..ae9242f7dd --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TextBMFont.lua @@ -0,0 +1,33 @@ + +-------------------------------- +-- @module TextBMFont +-------------------------------- +-- @function [parent=#TextBMFont] setFntFile +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#TextBMFont] getStringValue +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#TextBMFont] setText +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#TextBMFont] create +-- @param self +-- @return TextBMFont#TextBMFont ret (return value: ccui.TextBMFont) + +-------------------------------- +-- @function [parent=#TextBMFont] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#TextBMFont] TextBMFont +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TextField.lua b/cocos/scripting/lua-bindings/auto/api/TextField.lua new file mode 100644 index 0000000000..f42a948044 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TextField.lua @@ -0,0 +1,182 @@ + +-------------------------------- +-- @module TextField +-------------------------------- +-- @function [parent=#TextField] setAttachWithIME +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#TextField] getFontSize +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#TextField] getStringValue +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#TextField] getDeleteBackward +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TextField] getPlaceHolder +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#TextField] getAttachWithIME +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TextField] setFontName +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#TextField] getInsertText +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TextField] setInsertText +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#TextField] getDetachWithIME +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TextField] setTextVerticalAlignment +-- @param self +-- @param #cc.TextVAlignment textvalignment + +-------------------------------- +-- @function [parent=#TextField] didNotSelectSelf +-- @param self + +-------------------------------- +-- @function [parent=#TextField] getFontName +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#TextField] setTextAreaSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#TextField] attachWithIME +-- @param self + +-------------------------------- +-- @function [parent=#TextField] setPasswordEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#TextField] getPasswordStyleText +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#TextField] setMaxLengthEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#TextField] setPasswordStyleText +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#TextField] setDeleteBackward +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#TextField] setFontSize +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#TextField] setPlaceHolder +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#TextField] isPasswordEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TextField] setTextHorizontalAlignment +-- @param self +-- @param #cc.TextHAlignment texthalignment + +-------------------------------- +-- @function [parent=#TextField] getMaxLength +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#TextField] isMaxLengthEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TextField] setDetachWithIME +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#TextField] setText +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#TextField] setTouchAreaEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#TextField] hitTest +-- @param self +-- @param #point_table point +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TextField] setMaxLength +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#TextField] setTouchSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#TextField] getTouchSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#TextField] create +-- @param self +-- @return TextField#TextField ret (return value: ccui.TextField) + +-------------------------------- +-- @function [parent=#TextField] createInstance +-- @param self +-- @return Ref#Ref ret (return value: cc.Ref) + +-------------------------------- +-- @function [parent=#TextField] TextField +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Texture2D.lua b/cocos/scripting/lua-bindings/auto/api/Texture2D.lua new file mode 100644 index 0000000000..95e52baa3a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Texture2D.lua @@ -0,0 +1,171 @@ + +-------------------------------- +-- @module Texture2D +-------------------------------- +-- @function [parent=#Texture2D] getShaderProgram +-- @param self +-- @return GLProgram#GLProgram ret (return value: cc.GLProgram) + +-------------------------------- +-- @function [parent=#Texture2D] getMaxT +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Texture2D] getStringForFormat +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- overload function: initWithImage(cc.Image, cc.Texture2D::PixelFormat) +-- +-- overload function: initWithImage(cc.Image) +-- +-- @function [parent=#Texture2D] initWithImage +-- @param self +-- @param #cc.Image image +-- @param #cc.Texture2D::PixelFormat pixelformat +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#Texture2D] setShaderProgram +-- @param self +-- @param #cc.GLProgram glprogram + +-------------------------------- +-- @function [parent=#Texture2D] getMaxS +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Texture2D] hasPremultipliedAlpha +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Texture2D] initWithMipmaps +-- @param self +-- @param #cc._MipmapInfo map +-- @param #int int +-- @param #cc.Texture2D::PixelFormat pixelformat +-- @param #int int +-- @param #int int +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Texture2D] getPixelsHigh +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- overload function: getBitsPerPixelForFormat(cc.Texture2D::PixelFormat) +-- +-- overload function: getBitsPerPixelForFormat() +-- +-- @function [parent=#Texture2D] getBitsPerPixelForFormat +-- @param self +-- @param #cc.Texture2D::PixelFormat pixelformat +-- @return unsigned int#unsigned int ret (retunr value: unsigned int) + +-------------------------------- +-- @function [parent=#Texture2D] getName +-- @param self +-- @return unsigned int#unsigned int ret (return value: unsigned int) + +-------------------------------- +-- overload function: initWithString(char, cc.FontDefinition) +-- +-- overload function: initWithString(char, char, float, size_table, cc.TextHAlignment, cc.TextVAlignment) +-- +-- @function [parent=#Texture2D] initWithString +-- @param self +-- @param #char char +-- @param #char char +-- @param #float float +-- @param #size_table size +-- @param #cc.TextHAlignment texthalignment +-- @param #cc.TextVAlignment textvalignment +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#Texture2D] setMaxT +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Texture2D] drawInRect +-- @param self +-- @param #rect_table rect + +-------------------------------- +-- @function [parent=#Texture2D] getContentSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Texture2D] setAliasTexParameters +-- @param self + +-------------------------------- +-- @function [parent=#Texture2D] setAntiAliasTexParameters +-- @param self + +-------------------------------- +-- @function [parent=#Texture2D] generateMipmap +-- @param self + +-------------------------------- +-- @function [parent=#Texture2D] getDescription +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#Texture2D] getPixelFormat +-- @param self +-- @return Texture2D::PixelFormat#Texture2D::PixelFormat ret (return value: cc.Texture2D::PixelFormat) + +-------------------------------- +-- @function [parent=#Texture2D] getContentSizeInPixels +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Texture2D] getPixelsWide +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Texture2D] drawAtPoint +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#Texture2D] hasMipmaps +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Texture2D] setMaxS +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Texture2D] setDefaultAlphaPixelFormat +-- @param self +-- @param #cc.Texture2D::PixelFormat pixelformat + +-------------------------------- +-- @function [parent=#Texture2D] getDefaultAlphaPixelFormat +-- @param self +-- @return Texture2D::PixelFormat#Texture2D::PixelFormat ret (return value: cc.Texture2D::PixelFormat) + +-------------------------------- +-- @function [parent=#Texture2D] PVRImagesHavePremultipliedAlpha +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Texture2D] Texture2D +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TextureCache.lua b/cocos/scripting/lua-bindings/auto/api/TextureCache.lua new file mode 100644 index 0000000000..d8e74f724a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TextureCache.lua @@ -0,0 +1,63 @@ + +-------------------------------- +-- @module TextureCache +-------------------------------- +-- @function [parent=#TextureCache] reloadTexture +-- @param self +-- @param #string str +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TextureCache] removeTextureForKey +-- @param self +-- @param #string str + +-------------------------------- +-- @function [parent=#TextureCache] removeAllTextures +-- @param self + +-------------------------------- +-- @function [parent=#TextureCache] getDescription +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#TextureCache] getCachedTextureInfo +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- overload function: addImage(cc.Image, string) +-- +-- overload function: addImage(string) +-- +-- @function [parent=#TextureCache] addImage +-- @param self +-- @param #cc.Image image +-- @param #string str +-- @return Texture2D#Texture2D ret (retunr value: cc.Texture2D) + +-------------------------------- +-- @function [parent=#TextureCache] getTextureForKey +-- @param self +-- @param #string str +-- @return Texture2D#Texture2D ret (return value: cc.Texture2D) + +-------------------------------- +-- @function [parent=#TextureCache] removeUnusedTextures +-- @param self + +-------------------------------- +-- @function [parent=#TextureCache] removeTexture +-- @param self +-- @param #cc.Texture2D texture2d + +-------------------------------- +-- @function [parent=#TextureCache] waitForQuit +-- @param self + +-------------------------------- +-- @function [parent=#TextureCache] TextureCache +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TextureData.lua b/cocos/scripting/lua-bindings/auto/api/TextureData.lua new file mode 100644 index 0000000000..4407e185fe --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TextureData.lua @@ -0,0 +1,29 @@ + +-------------------------------- +-- @module TextureData +-------------------------------- +-- @function [parent=#TextureData] getContourData +-- @param self +-- @param #int int +-- @return ContourData#ContourData ret (return value: ccs.ContourData) + +-------------------------------- +-- @function [parent=#TextureData] init +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TextureData] addContourData +-- @param self +-- @param #ccs.ContourData contourdata + +-------------------------------- +-- @function [parent=#TextureData] create +-- @param self +-- @return TextureData#TextureData ret (return value: ccs.TextureData) + +-------------------------------- +-- @function [parent=#TextureData] TextureData +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TileMapAtlas.lua b/cocos/scripting/lua-bindings/auto/api/TileMapAtlas.lua new file mode 100644 index 0000000000..0b5d510520 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TileMapAtlas.lua @@ -0,0 +1,52 @@ + +-------------------------------- +-- @module TileMapAtlas +-------------------------------- +-- @function [parent=#TileMapAtlas] initWithTileFile +-- @param self +-- @param #string str +-- @param #string str +-- @param #int int +-- @param #int int +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TileMapAtlas] releaseMap +-- @param self + +-------------------------------- +-- @function [parent=#TileMapAtlas] getTGAInfo +-- @param self +-- @return sImageTGA#sImageTGA ret (return value: cc.sImageTGA) + +-------------------------------- +-- @function [parent=#TileMapAtlas] getTileAt +-- @param self +-- @param #point_table point +-- @return color3B_table#color3B_table ret (return value: color3B_table) + +-------------------------------- +-- @function [parent=#TileMapAtlas] setTile +-- @param self +-- @param #color3B_table color3b +-- @param #point_table point + +-------------------------------- +-- @function [parent=#TileMapAtlas] setTGAInfo +-- @param self +-- @param #cc.sImageTGA simagetga + +-------------------------------- +-- @function [parent=#TileMapAtlas] create +-- @param self +-- @param #string str +-- @param #string str +-- @param #int int +-- @param #int int +-- @return TileMapAtlas#TileMapAtlas ret (return value: cc.TileMapAtlas) + +-------------------------------- +-- @function [parent=#TileMapAtlas] TileMapAtlas +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TiledGrid3D.lua b/cocos/scripting/lua-bindings/auto/api/TiledGrid3D.lua new file mode 100644 index 0000000000..cbb80d54dc --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TiledGrid3D.lua @@ -0,0 +1,20 @@ + +-------------------------------- +-- @module TiledGrid3D +-------------------------------- +-- overload function: create(size_table) +-- +-- overload function: create(size_table, cc.Texture2D, bool) +-- +-- @function [parent=#TiledGrid3D] create +-- @param self +-- @param #size_table size +-- @param #cc.Texture2D texture2d +-- @param #bool bool +-- @return TiledGrid3D#TiledGrid3D ret (retunr value: cc.TiledGrid3D) + +-------------------------------- +-- @function [parent=#TiledGrid3D] TiledGrid3D +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TiledGrid3DAction.lua b/cocos/scripting/lua-bindings/auto/api/TiledGrid3DAction.lua new file mode 100644 index 0000000000..c09f4c477c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TiledGrid3DAction.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module TiledGrid3DAction +-------------------------------- +-- @function [parent=#TiledGrid3DAction] getGrid +-- @param self +-- @return GridBase#GridBase ret (return value: cc.GridBase) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Timer.lua b/cocos/scripting/lua-bindings/auto/api/Timer.lua new file mode 100644 index 0000000000..4565065f96 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Timer.lua @@ -0,0 +1,34 @@ + +-------------------------------- +-- @module Timer +-------------------------------- +-- @function [parent=#Timer] getInterval +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Timer] setupTimerWithInterval +-- @param self +-- @param #float float +-- @param #unsigned int int +-- @param #float float + +-------------------------------- +-- @function [parent=#Timer] setInterval +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Timer] update +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Timer] trigger +-- @param self + +-------------------------------- +-- @function [parent=#Timer] cancel +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TintBy.lua b/cocos/scripting/lua-bindings/auto/api/TintBy.lua new file mode 100644 index 0000000000..e79b425d49 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TintBy.lua @@ -0,0 +1,13 @@ + +-------------------------------- +-- @module TintBy +-------------------------------- +-- @function [parent=#TintBy] create +-- @param self +-- @param #float float +-- @param #short short +-- @param #short short +-- @param #short short +-- @return TintBy#TintBy ret (return value: cc.TintBy) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TintTo.lua b/cocos/scripting/lua-bindings/auto/api/TintTo.lua new file mode 100644 index 0000000000..c84d250427 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TintTo.lua @@ -0,0 +1,13 @@ + +-------------------------------- +-- @module TintTo +-------------------------------- +-- @function [parent=#TintTo] create +-- @param self +-- @param #float float +-- @param #unsigned char char +-- @param #unsigned char char +-- @param #unsigned char char +-- @return TintTo#TintTo ret (return value: cc.TintTo) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/ToggleVisibility.lua b/cocos/scripting/lua-bindings/auto/api/ToggleVisibility.lua new file mode 100644 index 0000000000..c7ee14b671 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/ToggleVisibility.lua @@ -0,0 +1,9 @@ + +-------------------------------- +-- @module ToggleVisibility +-------------------------------- +-- @function [parent=#ToggleVisibility] create +-- @param self +-- @return ToggleVisibility#ToggleVisibility ret (return value: cc.ToggleVisibility) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Touch.lua b/cocos/scripting/lua-bindings/auto/api/Touch.lua new file mode 100644 index 0000000000..c654da17e6 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Touch.lua @@ -0,0 +1,55 @@ + +-------------------------------- +-- @module Touch +-------------------------------- +-- @function [parent=#Touch] getPreviousLocationInView +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Touch] getLocation +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Touch] getDelta +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Touch] getStartLocationInView +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Touch] getStartLocation +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Touch] getID +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Touch] setTouchInfo +-- @param self +-- @param #int int +-- @param #float float +-- @param #float float + +-------------------------------- +-- @function [parent=#Touch] getLocationInView +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Touch] getPreviousLocation +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Touch] Touch +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionCrossFade.lua b/cocos/scripting/lua-bindings/auto/api/TransitionCrossFade.lua new file mode 100644 index 0000000000..4520edb09e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionCrossFade.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionCrossFade +-------------------------------- +-- @function [parent=#TransitionCrossFade] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionCrossFade#TransitionCrossFade ret (return value: cc.TransitionCrossFade) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionEaseScene.lua b/cocos/scripting/lua-bindings/auto/api/TransitionEaseScene.lua new file mode 100644 index 0000000000..467d202c96 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionEaseScene.lua @@ -0,0 +1,10 @@ + +-------------------------------- +-- @module TransitionEaseScene +-------------------------------- +-- @function [parent=#TransitionEaseScene] easeActionWithAction +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionFade.lua b/cocos/scripting/lua-bindings/auto/api/TransitionFade.lua new file mode 100644 index 0000000000..9e0d884963 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionFade.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionFade +-------------------------------- +-- overload function: create(float, cc.Scene) +-- +-- overload function: create(float, cc.Scene, color3B_table) +-- +-- @function [parent=#TransitionFade] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #color3B_table color3b +-- @return TransitionFade#TransitionFade ret (retunr value: cc.TransitionFade) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionFadeBL.lua b/cocos/scripting/lua-bindings/auto/api/TransitionFadeBL.lua new file mode 100644 index 0000000000..c865c8c47f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionFadeBL.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionFadeBL +-------------------------------- +-- @function [parent=#TransitionFadeBL] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionFadeBL#TransitionFadeBL ret (return value: cc.TransitionFadeBL) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionFadeDown.lua b/cocos/scripting/lua-bindings/auto/api/TransitionFadeDown.lua new file mode 100644 index 0000000000..0b89050a82 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionFadeDown.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionFadeDown +-------------------------------- +-- @function [parent=#TransitionFadeDown] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionFadeDown#TransitionFadeDown ret (return value: cc.TransitionFadeDown) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionFadeTR.lua b/cocos/scripting/lua-bindings/auto/api/TransitionFadeTR.lua new file mode 100644 index 0000000000..f1c55c7390 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionFadeTR.lua @@ -0,0 +1,23 @@ + +-------------------------------- +-- @module TransitionFadeTR +-------------------------------- +-- @function [parent=#TransitionFadeTR] easeActionWithAction +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionFadeTR] actionWithSize +-- @param self +-- @param #size_table size +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionFadeTR] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionFadeTR#TransitionFadeTR ret (return value: cc.TransitionFadeTR) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionFadeUp.lua b/cocos/scripting/lua-bindings/auto/api/TransitionFadeUp.lua new file mode 100644 index 0000000000..7cbcf4de1f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionFadeUp.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionFadeUp +-------------------------------- +-- @function [parent=#TransitionFadeUp] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionFadeUp#TransitionFadeUp ret (return value: cc.TransitionFadeUp) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionFlipAngular.lua b/cocos/scripting/lua-bindings/auto/api/TransitionFlipAngular.lua new file mode 100644 index 0000000000..e88b257c17 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionFlipAngular.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionFlipAngular +-------------------------------- +-- overload function: create(float, cc.Scene) +-- +-- overload function: create(float, cc.Scene, cc.TransitionScene::Orientation) +-- +-- @function [parent=#TransitionFlipAngular] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #cc.TransitionScene::Orientation orientation +-- @return TransitionFlipAngular#TransitionFlipAngular ret (retunr value: cc.TransitionFlipAngular) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionFlipX.lua b/cocos/scripting/lua-bindings/auto/api/TransitionFlipX.lua new file mode 100644 index 0000000000..2a26b2c88a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionFlipX.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionFlipX +-------------------------------- +-- overload function: create(float, cc.Scene) +-- +-- overload function: create(float, cc.Scene, cc.TransitionScene::Orientation) +-- +-- @function [parent=#TransitionFlipX] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #cc.TransitionScene::Orientation orientation +-- @return TransitionFlipX#TransitionFlipX ret (retunr value: cc.TransitionFlipX) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionFlipY.lua b/cocos/scripting/lua-bindings/auto/api/TransitionFlipY.lua new file mode 100644 index 0000000000..ab4247b725 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionFlipY.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionFlipY +-------------------------------- +-- overload function: create(float, cc.Scene) +-- +-- overload function: create(float, cc.Scene, cc.TransitionScene::Orientation) +-- +-- @function [parent=#TransitionFlipY] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #cc.TransitionScene::Orientation orientation +-- @return TransitionFlipY#TransitionFlipY ret (retunr value: cc.TransitionFlipY) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionJumpZoom.lua b/cocos/scripting/lua-bindings/auto/api/TransitionJumpZoom.lua new file mode 100644 index 0000000000..2902d2f9f5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionJumpZoom.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionJumpZoom +-------------------------------- +-- @function [parent=#TransitionJumpZoom] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionJumpZoom#TransitionJumpZoom ret (return value: cc.TransitionJumpZoom) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionMoveInB.lua b/cocos/scripting/lua-bindings/auto/api/TransitionMoveInB.lua new file mode 100644 index 0000000000..2e75878a74 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionMoveInB.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionMoveInB +-------------------------------- +-- @function [parent=#TransitionMoveInB] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionMoveInB#TransitionMoveInB ret (return value: cc.TransitionMoveInB) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionMoveInL.lua b/cocos/scripting/lua-bindings/auto/api/TransitionMoveInL.lua new file mode 100644 index 0000000000..565a5fcc67 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionMoveInL.lua @@ -0,0 +1,22 @@ + +-------------------------------- +-- @module TransitionMoveInL +-------------------------------- +-- @function [parent=#TransitionMoveInL] action +-- @param self +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionMoveInL] easeActionWithAction +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionMoveInL] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionMoveInL#TransitionMoveInL ret (return value: cc.TransitionMoveInL) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionMoveInR.lua b/cocos/scripting/lua-bindings/auto/api/TransitionMoveInR.lua new file mode 100644 index 0000000000..cad4b24418 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionMoveInR.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionMoveInR +-------------------------------- +-- @function [parent=#TransitionMoveInR] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionMoveInR#TransitionMoveInR ret (return value: cc.TransitionMoveInR) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionMoveInT.lua b/cocos/scripting/lua-bindings/auto/api/TransitionMoveInT.lua new file mode 100644 index 0000000000..7008c16a02 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionMoveInT.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionMoveInT +-------------------------------- +-- @function [parent=#TransitionMoveInT] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionMoveInT#TransitionMoveInT ret (return value: cc.TransitionMoveInT) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionPageTurn.lua b/cocos/scripting/lua-bindings/auto/api/TransitionPageTurn.lua new file mode 100644 index 0000000000..7a67c11b38 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionPageTurn.lua @@ -0,0 +1,30 @@ + +-------------------------------- +-- @module TransitionPageTurn +-------------------------------- +-- @function [parent=#TransitionPageTurn] actionWithSize +-- @param self +-- @param #size_table size +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionPageTurn] initWithDuration +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #bool bool +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#TransitionPageTurn] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #bool bool +-- @return TransitionPageTurn#TransitionPageTurn ret (return value: cc.TransitionPageTurn) + +-------------------------------- +-- @function [parent=#TransitionPageTurn] TransitionPageTurn +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionProgress.lua b/cocos/scripting/lua-bindings/auto/api/TransitionProgress.lua new file mode 100644 index 0000000000..afacb14a5a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionProgress.lua @@ -0,0 +1,15 @@ + +-------------------------------- +-- @module TransitionProgress +-------------------------------- +-- @function [parent=#TransitionProgress] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionProgress#TransitionProgress ret (return value: cc.TransitionProgress) + +-------------------------------- +-- @function [parent=#TransitionProgress] TransitionProgress +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionProgressHorizontal.lua b/cocos/scripting/lua-bindings/auto/api/TransitionProgressHorizontal.lua new file mode 100644 index 0000000000..5d0b6ab882 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionProgressHorizontal.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionProgressHorizontal +-------------------------------- +-- @function [parent=#TransitionProgressHorizontal] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionProgressHorizontal#TransitionProgressHorizontal ret (return value: cc.TransitionProgressHorizontal) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionProgressInOut.lua b/cocos/scripting/lua-bindings/auto/api/TransitionProgressInOut.lua new file mode 100644 index 0000000000..08be8ffbc5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionProgressInOut.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionProgressInOut +-------------------------------- +-- @function [parent=#TransitionProgressInOut] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionProgressInOut#TransitionProgressInOut ret (return value: cc.TransitionProgressInOut) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionProgressOutIn.lua b/cocos/scripting/lua-bindings/auto/api/TransitionProgressOutIn.lua new file mode 100644 index 0000000000..5f6302ce40 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionProgressOutIn.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionProgressOutIn +-------------------------------- +-- @function [parent=#TransitionProgressOutIn] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionProgressOutIn#TransitionProgressOutIn ret (return value: cc.TransitionProgressOutIn) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionProgressRadialCCW.lua b/cocos/scripting/lua-bindings/auto/api/TransitionProgressRadialCCW.lua new file mode 100644 index 0000000000..08203ae5de --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionProgressRadialCCW.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionProgressRadialCCW +-------------------------------- +-- @function [parent=#TransitionProgressRadialCCW] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionProgressRadialCCW#TransitionProgressRadialCCW ret (return value: cc.TransitionProgressRadialCCW) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionProgressRadialCW.lua b/cocos/scripting/lua-bindings/auto/api/TransitionProgressRadialCW.lua new file mode 100644 index 0000000000..136c6376b2 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionProgressRadialCW.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionProgressRadialCW +-------------------------------- +-- @function [parent=#TransitionProgressRadialCW] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionProgressRadialCW#TransitionProgressRadialCW ret (return value: cc.TransitionProgressRadialCW) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionProgressVertical.lua b/cocos/scripting/lua-bindings/auto/api/TransitionProgressVertical.lua new file mode 100644 index 0000000000..9440f3facc --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionProgressVertical.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionProgressVertical +-------------------------------- +-- @function [parent=#TransitionProgressVertical] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionProgressVertical#TransitionProgressVertical ret (return value: cc.TransitionProgressVertical) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionRotoZoom.lua b/cocos/scripting/lua-bindings/auto/api/TransitionRotoZoom.lua new file mode 100644 index 0000000000..0d18b28d42 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionRotoZoom.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionRotoZoom +-------------------------------- +-- @function [parent=#TransitionRotoZoom] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionRotoZoom#TransitionRotoZoom ret (return value: cc.TransitionRotoZoom) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionScene.lua b/cocos/scripting/lua-bindings/auto/api/TransitionScene.lua new file mode 100644 index 0000000000..24dc9cce4a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionScene.lua @@ -0,0 +1,19 @@ + +-------------------------------- +-- @module TransitionScene +-------------------------------- +-- @function [parent=#TransitionScene] finish +-- @param self + +-------------------------------- +-- @function [parent=#TransitionScene] hideOutShowIn +-- @param self + +-------------------------------- +-- @function [parent=#TransitionScene] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionScene#TransitionScene ret (return value: cc.TransitionScene) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionSceneOriented.lua b/cocos/scripting/lua-bindings/auto/api/TransitionSceneOriented.lua new file mode 100644 index 0000000000..2f42272bbc --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionSceneOriented.lua @@ -0,0 +1,12 @@ + +-------------------------------- +-- @module TransitionSceneOriented +-------------------------------- +-- @function [parent=#TransitionSceneOriented] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #cc.TransitionScene::Orientation orientation +-- @return TransitionSceneOriented#TransitionSceneOriented ret (return value: cc.TransitionSceneOriented) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionShrinkGrow.lua b/cocos/scripting/lua-bindings/auto/api/TransitionShrinkGrow.lua new file mode 100644 index 0000000000..b270f34e37 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionShrinkGrow.lua @@ -0,0 +1,17 @@ + +-------------------------------- +-- @module TransitionShrinkGrow +-------------------------------- +-- @function [parent=#TransitionShrinkGrow] easeActionWithAction +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionShrinkGrow] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionShrinkGrow#TransitionShrinkGrow ret (return value: cc.TransitionShrinkGrow) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionSlideInB.lua b/cocos/scripting/lua-bindings/auto/api/TransitionSlideInB.lua new file mode 100644 index 0000000000..e95fb7e32c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionSlideInB.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionSlideInB +-------------------------------- +-- @function [parent=#TransitionSlideInB] action +-- @param self +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionSlideInB] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionSlideInB#TransitionSlideInB ret (return value: cc.TransitionSlideInB) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionSlideInL.lua b/cocos/scripting/lua-bindings/auto/api/TransitionSlideInL.lua new file mode 100644 index 0000000000..f87a7eb4e8 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionSlideInL.lua @@ -0,0 +1,22 @@ + +-------------------------------- +-- @module TransitionSlideInL +-------------------------------- +-- @function [parent=#TransitionSlideInL] action +-- @param self +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionSlideInL] easeActionWithAction +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionSlideInL] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionSlideInL#TransitionSlideInL ret (return value: cc.TransitionSlideInL) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionSlideInR.lua b/cocos/scripting/lua-bindings/auto/api/TransitionSlideInR.lua new file mode 100644 index 0000000000..99997d3f72 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionSlideInR.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionSlideInR +-------------------------------- +-- @function [parent=#TransitionSlideInR] action +-- @param self +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionSlideInR] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionSlideInR#TransitionSlideInR ret (return value: cc.TransitionSlideInR) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionSlideInT.lua b/cocos/scripting/lua-bindings/auto/api/TransitionSlideInT.lua new file mode 100644 index 0000000000..a66a56f0df --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionSlideInT.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionSlideInT +-------------------------------- +-- @function [parent=#TransitionSlideInT] action +-- @param self +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionSlideInT] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionSlideInT#TransitionSlideInT ret (return value: cc.TransitionSlideInT) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionSplitCols.lua b/cocos/scripting/lua-bindings/auto/api/TransitionSplitCols.lua new file mode 100644 index 0000000000..d56417705c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionSplitCols.lua @@ -0,0 +1,22 @@ + +-------------------------------- +-- @module TransitionSplitCols +-------------------------------- +-- @function [parent=#TransitionSplitCols] action +-- @param self +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionSplitCols] easeActionWithAction +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionSplitCols] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionSplitCols#TransitionSplitCols ret (return value: cc.TransitionSplitCols) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionSplitRows.lua b/cocos/scripting/lua-bindings/auto/api/TransitionSplitRows.lua new file mode 100644 index 0000000000..cfa9a6e10e --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionSplitRows.lua @@ -0,0 +1,11 @@ + +-------------------------------- +-- @module TransitionSplitRows +-------------------------------- +-- @function [parent=#TransitionSplitRows] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionSplitRows#TransitionSplitRows ret (return value: cc.TransitionSplitRows) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionTurnOffTiles.lua b/cocos/scripting/lua-bindings/auto/api/TransitionTurnOffTiles.lua new file mode 100644 index 0000000000..3f10345109 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionTurnOffTiles.lua @@ -0,0 +1,17 @@ + +-------------------------------- +-- @module TransitionTurnOffTiles +-------------------------------- +-- @function [parent=#TransitionTurnOffTiles] easeActionWithAction +-- @param self +-- @param #cc.ActionInterval actioninterval +-- @return ActionInterval#ActionInterval ret (return value: cc.ActionInterval) + +-------------------------------- +-- @function [parent=#TransitionTurnOffTiles] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @return TransitionTurnOffTiles#TransitionTurnOffTiles ret (return value: cc.TransitionTurnOffTiles) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionZoomFlipAngular.lua b/cocos/scripting/lua-bindings/auto/api/TransitionZoomFlipAngular.lua new file mode 100644 index 0000000000..928ed09a9f --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionZoomFlipAngular.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionZoomFlipAngular +-------------------------------- +-- overload function: create(float, cc.Scene) +-- +-- overload function: create(float, cc.Scene, cc.TransitionScene::Orientation) +-- +-- @function [parent=#TransitionZoomFlipAngular] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #cc.TransitionScene::Orientation orientation +-- @return TransitionZoomFlipAngular#TransitionZoomFlipAngular ret (retunr value: cc.TransitionZoomFlipAngular) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionZoomFlipX.lua b/cocos/scripting/lua-bindings/auto/api/TransitionZoomFlipX.lua new file mode 100644 index 0000000000..5e8d601c38 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionZoomFlipX.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionZoomFlipX +-------------------------------- +-- overload function: create(float, cc.Scene) +-- +-- overload function: create(float, cc.Scene, cc.TransitionScene::Orientation) +-- +-- @function [parent=#TransitionZoomFlipX] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #cc.TransitionScene::Orientation orientation +-- @return TransitionZoomFlipX#TransitionZoomFlipX ret (retunr value: cc.TransitionZoomFlipX) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TransitionZoomFlipY.lua b/cocos/scripting/lua-bindings/auto/api/TransitionZoomFlipY.lua new file mode 100644 index 0000000000..d7c66d7fbb --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TransitionZoomFlipY.lua @@ -0,0 +1,16 @@ + +-------------------------------- +-- @module TransitionZoomFlipY +-------------------------------- +-- overload function: create(float, cc.Scene) +-- +-- overload function: create(float, cc.Scene, cc.TransitionScene::Orientation) +-- +-- @function [parent=#TransitionZoomFlipY] create +-- @param self +-- @param #float float +-- @param #cc.Scene scene +-- @param #cc.TransitionScene::Orientation orientation +-- @return TransitionZoomFlipY#TransitionZoomFlipY ret (retunr value: cc.TransitionZoomFlipY) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/TurnOffTiles.lua b/cocos/scripting/lua-bindings/auto/api/TurnOffTiles.lua new file mode 100644 index 0000000000..3a036fa401 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/TurnOffTiles.lua @@ -0,0 +1,32 @@ + +-------------------------------- +-- @module TurnOffTiles +-------------------------------- +-- @function [parent=#TurnOffTiles] turnOnTile +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#TurnOffTiles] turnOffTile +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#TurnOffTiles] shuffle +-- @param self +-- @param #unsigned int int +-- @param #unsigned int int + +-------------------------------- +-- overload function: create(float, size_table, unsigned int) +-- +-- overload function: create(float, size_table) +-- +-- @function [parent=#TurnOffTiles] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #unsigned int int +-- @return TurnOffTiles#TurnOffTiles ret (retunr value: cc.TurnOffTiles) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Tween.lua b/cocos/scripting/lua-bindings/auto/api/Tween.lua new file mode 100644 index 0000000000..4d9b1b670d --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Tween.lua @@ -0,0 +1,49 @@ + +-------------------------------- +-- @module Tween +-------------------------------- +-- @function [parent=#Tween] getAnimation +-- @param self +-- @return ArmatureAnimation#ArmatureAnimation ret (return value: ccs.ArmatureAnimation) + +-------------------------------- +-- @function [parent=#Tween] gotoAndPause +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Tween] play +-- @param self +-- @param #ccs.MovementBoneData movementbonedata +-- @param #int int +-- @param #int int +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#Tween] gotoAndPlay +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Tween] init +-- @param self +-- @param #ccs.Bone bone +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Tween] setAnimation +-- @param self +-- @param #ccs.ArmatureAnimation armatureanimation + +-------------------------------- +-- @function [parent=#Tween] create +-- @param self +-- @param #ccs.Bone bone +-- @return Tween#Tween ret (return value: ccs.Tween) + +-------------------------------- +-- @function [parent=#Tween] Tween +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Twirl.lua b/cocos/scripting/lua-bindings/auto/api/Twirl.lua new file mode 100644 index 0000000000..2854555d06 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Twirl.lua @@ -0,0 +1,48 @@ + +-------------------------------- +-- @module Twirl +-------------------------------- +-- @function [parent=#Twirl] setAmplitudeRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Twirl] getAmplitudeRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Twirl] setAmplitude +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Twirl] getAmplitude +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Twirl] setPosition +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#Twirl] getPosition +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Twirl] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #point_table point +-- @param #unsigned int int +-- @param #float float +-- @return Twirl#Twirl ret (return value: cc.Twirl) + +-------------------------------- +-- @function [parent=#Twirl] Twirl +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/UserDefault.lua b/cocos/scripting/lua-bindings/auto/api/UserDefault.lua new file mode 100644 index 0000000000..a694227a02 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/UserDefault.lua @@ -0,0 +1,107 @@ + +-------------------------------- +-- @module UserDefault +-------------------------------- +-- @function [parent=#UserDefault] setIntegerForKey +-- @param self +-- @param #char char +-- @param #int int + +-------------------------------- +-- overload function: getFloatForKey(char, float) +-- +-- overload function: getFloatForKey(char) +-- +-- @function [parent=#UserDefault] getFloatForKey +-- @param self +-- @param #char char +-- @param #float float +-- @return float#float ret (retunr value: float) + +-------------------------------- +-- overload function: getBoolForKey(char, bool) +-- +-- overload function: getBoolForKey(char) +-- +-- @function [parent=#UserDefault] getBoolForKey +-- @param self +-- @param #char char +-- @param #bool bool +-- @return bool#bool ret (retunr value: bool) + +-------------------------------- +-- @function [parent=#UserDefault] setDoubleForKey +-- @param self +-- @param #char char +-- @param #double double + +-------------------------------- +-- @function [parent=#UserDefault] setFloatForKey +-- @param self +-- @param #char char +-- @param #float float + +-------------------------------- +-- overload function: getStringForKey(char, string) +-- +-- overload function: getStringForKey(char) +-- +-- @function [parent=#UserDefault] getStringForKey +-- @param self +-- @param #char char +-- @param #string str +-- @return string#string ret (retunr value: string) + +-------------------------------- +-- @function [parent=#UserDefault] setStringForKey +-- @param self +-- @param #char char +-- @param #string str + +-------------------------------- +-- @function [parent=#UserDefault] flush +-- @param self + +-------------------------------- +-- overload function: getIntegerForKey(char, int) +-- +-- overload function: getIntegerForKey(char) +-- +-- @function [parent=#UserDefault] getIntegerForKey +-- @param self +-- @param #char char +-- @param #int int +-- @return int#int ret (retunr value: int) + +-------------------------------- +-- overload function: getDoubleForKey(char, double) +-- +-- overload function: getDoubleForKey(char) +-- +-- @function [parent=#UserDefault] getDoubleForKey +-- @param self +-- @param #char char +-- @param #double double +-- @return double#double ret (retunr value: double) + +-------------------------------- +-- @function [parent=#UserDefault] setBoolForKey +-- @param self +-- @param #char char +-- @param #bool bool + +-------------------------------- +-- @function [parent=#UserDefault] destroyInstance +-- @param self + +-------------------------------- +-- @function [parent=#UserDefault] getXMLFilePath +-- @param self +-- @return string#string ret (return value: string) + +-------------------------------- +-- @function [parent=#UserDefault] isXMLFileExist +-- @param self +-- @return bool#bool ret (return value: bool) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Waves.lua b/cocos/scripting/lua-bindings/auto/api/Waves.lua new file mode 100644 index 0000000000..ec017b7e0a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Waves.lua @@ -0,0 +1,39 @@ + +-------------------------------- +-- @module Waves +-------------------------------- +-- @function [parent=#Waves] getAmplitudeRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Waves] setAmplitude +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Waves] getAmplitude +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Waves] setAmplitudeRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Waves] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #unsigned int int +-- @param #float float +-- @param #bool bool +-- @param #bool bool +-- @return Waves#Waves ret (return value: cc.Waves) + +-------------------------------- +-- @function [parent=#Waves] Waves +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Waves3D.lua b/cocos/scripting/lua-bindings/auto/api/Waves3D.lua new file mode 100644 index 0000000000..8971a94063 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Waves3D.lua @@ -0,0 +1,33 @@ + +-------------------------------- +-- @module Waves3D +-------------------------------- +-- @function [parent=#Waves3D] getAmplitudeRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Waves3D] setAmplitude +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Waves3D] setAmplitudeRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#Waves3D] getAmplitude +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Waves3D] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #unsigned int int +-- @param #float float +-- @return Waves3D#Waves3D ret (return value: cc.Waves3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/WavesTiles3D.lua b/cocos/scripting/lua-bindings/auto/api/WavesTiles3D.lua new file mode 100644 index 0000000000..11fccd28c5 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/WavesTiles3D.lua @@ -0,0 +1,33 @@ + +-------------------------------- +-- @module WavesTiles3D +-------------------------------- +-- @function [parent=#WavesTiles3D] getAmplitudeRate +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#WavesTiles3D] setAmplitude +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#WavesTiles3D] setAmplitudeRate +-- @param self +-- @param #float float + +-------------------------------- +-- @function [parent=#WavesTiles3D] getAmplitude +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#WavesTiles3D] create +-- @param self +-- @param #float float +-- @param #size_table size +-- @param #unsigned int int +-- @param #float float +-- @return WavesTiles3D#WavesTiles3D ret (return value: cc.WavesTiles3D) + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/Widget.lua b/cocos/scripting/lua-bindings/auto/api/Widget.lua new file mode 100644 index 0000000000..91edad6820 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/Widget.lua @@ -0,0 +1,305 @@ + +-------------------------------- +-- @module Widget +-------------------------------- +-- @function [parent=#Widget] setSizePercent +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#Widget] isFlippedX +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Widget] getCustomSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Widget] setFlippedY +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Widget] setFlippedX +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Widget] getNodeByTag +-- @param self +-- @param #int int +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#Widget] getTouchEndPos +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Widget] setPositionPercent +-- @param self +-- @param #point_table point + +-------------------------------- +-- @function [parent=#Widget] getNodes +-- @param self +-- @return array_table#array_table ret (return value: array_table) + +-------------------------------- +-- @function [parent=#Widget] setPositionType +-- @param self +-- @param #ccui.PositionType positiontype + +-------------------------------- +-- @function [parent=#Widget] getName +-- @param self +-- @return char#char ret (return value: char) + +-------------------------------- +-- @function [parent=#Widget] isIgnoreContentAdaptWithSize +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Widget] updateSizeAndPosition +-- @param self + +-------------------------------- +-- @function [parent=#Widget] getBottomInParent +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Widget] getActionTag +-- @param self +-- @return int#int ret (return value: int) + +-------------------------------- +-- @function [parent=#Widget] getLayoutParameter +-- @param self +-- @param #ccui.LayoutParameterType layoutparametertype +-- @return LayoutParameter#LayoutParameter ret (return value: ccui.LayoutParameter) + +-------------------------------- +-- @function [parent=#Widget] getPositionType +-- @param self +-- @return PositionType#PositionType ret (return value: ccui.PositionType) + +-------------------------------- +-- @function [parent=#Widget] setName +-- @param self +-- @param #char char + +-------------------------------- +-- @function [parent=#Widget] getChildByName +-- @param self +-- @param #char char +-- @return Widget#Widget ret (return value: ccui.Widget) + +-------------------------------- +-- @function [parent=#Widget] isEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Widget] removeNodeByTag +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Widget] isTouchEnabled +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Widget] getContentSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Widget] getWorldPosition +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Widget] didNotSelectSelf +-- @param self + +-------------------------------- +-- @function [parent=#Widget] setFocused +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Widget] setTouchEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Widget] clone +-- @param self +-- @return Widget#Widget ret (return value: ccui.Widget) + +-------------------------------- +-- @function [parent=#Widget] getTouchMovePos +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Widget] setEnabled +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Widget] getVirtualRenderer +-- @param self +-- @return Node#Node ret (return value: cc.Node) + +-------------------------------- +-- @function [parent=#Widget] setBrightStyle +-- @param self +-- @param #ccui.BrightStyle brightstyle + +-------------------------------- +-- overload function: addNode(cc.Node, int) +-- +-- overload function: addNode(cc.Node) +-- +-- overload function: addNode(cc.Node, int, int) +-- +-- @function [parent=#Widget] addNode +-- @param self +-- @param #cc.Node node +-- @param #int int +-- @param #int int + +-------------------------------- +-- @function [parent=#Widget] setLayoutParameter +-- @param self +-- @param #ccui.LayoutParameter layoutparameter + +-------------------------------- +-- @function [parent=#Widget] getSizePercent +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Widget] getTouchStartPos +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Widget] getLeftInParent +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Widget] setActionTag +-- @param self +-- @param #int int + +-------------------------------- +-- @function [parent=#Widget] ignoreContentAdaptWithSize +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Widget] isBright +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Widget] clippingParentAreaContainPoint +-- @param self +-- @param #point_table point +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Widget] getTopInParent +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Widget] getWidgetType +-- @param self +-- @return WidgetType#WidgetType ret (return value: ccui.WidgetType) + +-------------------------------- +-- @function [parent=#Widget] getSize +-- @param self +-- @return size_table#size_table ret (return value: size_table) + +-------------------------------- +-- @function [parent=#Widget] getRightInParent +-- @param self +-- @return float#float ret (return value: float) + +-------------------------------- +-- @function [parent=#Widget] getSizeType +-- @param self +-- @return SizeType#SizeType ret (return value: ccui.SizeType) + +-------------------------------- +-- @function [parent=#Widget] removeNode +-- @param self +-- @param #cc.Node node + +-------------------------------- +-- @function [parent=#Widget] removeAllNodes +-- @param self + +-------------------------------- +-- @function [parent=#Widget] getPositionPercent +-- @param self +-- @return point_table#point_table ret (return value: point_table) + +-------------------------------- +-- @function [parent=#Widget] hitTest +-- @param self +-- @param #point_table point +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Widget] isFocused +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Widget] isFlippedY +-- @param self +-- @return bool#bool ret (return value: bool) + +-------------------------------- +-- @function [parent=#Widget] setSizeType +-- @param self +-- @param #ccui.SizeType sizetype + +-------------------------------- +-- @function [parent=#Widget] checkChildInfo +-- @param self +-- @param #int int +-- @param #ccui.Widget widget +-- @param #point_table point + +-------------------------------- +-- @function [parent=#Widget] setSize +-- @param self +-- @param #size_table size + +-------------------------------- +-- @function [parent=#Widget] setBright +-- @param self +-- @param #bool bool + +-------------------------------- +-- @function [parent=#Widget] create +-- @param self +-- @return Widget#Widget ret (return value: ccui.Widget) + +-------------------------------- +-- @function [parent=#Widget] Widget +-- @param self + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_auto_api.lua b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_auto_api.lua new file mode 100644 index 0000000000..581cbe0664 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_auto_api.lua @@ -0,0 +1,1199 @@ +-------------------------------- +-- @module cc + +-------------------------------------------------------- +-- the cc Ref +-- @field [parent=#cc] Ref#Ref Ref preloaded module + + +-------------------------------------------------------- +-- the cc Action +-- @field [parent=#cc] Action#Action Action preloaded module + + +-------------------------------------------------------- +-- the cc FiniteTimeAction +-- @field [parent=#cc] FiniteTimeAction#FiniteTimeAction FiniteTimeAction preloaded module + + +-------------------------------------------------------- +-- the cc Speed +-- @field [parent=#cc] Speed#Speed Speed preloaded module + + +-------------------------------------------------------- +-- the cc Follow +-- @field [parent=#cc] Follow#Follow Follow preloaded module + + +-------------------------------------------------------- +-- the cc GLProgram +-- @field [parent=#cc] GLProgram#GLProgram GLProgram preloaded module + + +-------------------------------------------------------- +-- the cc Touch +-- @field [parent=#cc] Touch#Touch Touch preloaded module + + +-------------------------------------------------------- +-- the cc Event +-- @field [parent=#cc] Event#Event Event preloaded module + + +-------------------------------------------------------- +-- the cc EventTouch +-- @field [parent=#cc] EventTouch#EventTouch EventTouch preloaded module + + +-------------------------------------------------------- +-- the cc EventKeyboard +-- @field [parent=#cc] EventKeyboard#EventKeyboard EventKeyboard preloaded module + + +-------------------------------------------------------- +-- the cc Texture2D +-- @field [parent=#cc] Texture2D#Texture2D Texture2D preloaded module + + +-------------------------------------------------------- +-- the cc EventListener +-- @field [parent=#cc] EventListener#EventListener EventListener preloaded module + + +-------------------------------------------------------- +-- the cc EventDispatcher +-- @field [parent=#cc] EventDispatcher#EventDispatcher EventDispatcher preloaded module + + +-------------------------------------------------------- +-- the cc Node +-- @field [parent=#cc] Node#Node Node preloaded module + + +-------------------------------------------------------- +-- the cc SpriteFrame +-- @field [parent=#cc] SpriteFrame#SpriteFrame SpriteFrame preloaded module + + +-------------------------------------------------------- +-- the cc AnimationFrame +-- @field [parent=#cc] AnimationFrame#AnimationFrame AnimationFrame preloaded module + + +-------------------------------------------------------- +-- the cc Animation +-- @field [parent=#cc] Animation#Animation Animation preloaded module + + +-------------------------------------------------------- +-- the cc ActionInterval +-- @field [parent=#cc] ActionInterval#ActionInterval ActionInterval preloaded module + + +-------------------------------------------------------- +-- the cc Sequence +-- @field [parent=#cc] Sequence#Sequence Sequence preloaded module + + +-------------------------------------------------------- +-- the cc Repeat +-- @field [parent=#cc] Repeat#Repeat Repeat preloaded module + + +-------------------------------------------------------- +-- the cc RepeatForever +-- @field [parent=#cc] RepeatForever#RepeatForever RepeatForever preloaded module + + +-------------------------------------------------------- +-- the cc Spawn +-- @field [parent=#cc] Spawn#Spawn Spawn preloaded module + + +-------------------------------------------------------- +-- the cc RotateTo +-- @field [parent=#cc] RotateTo#RotateTo RotateTo preloaded module + + +-------------------------------------------------------- +-- the cc RotateBy +-- @field [parent=#cc] RotateBy#RotateBy RotateBy preloaded module + + +-------------------------------------------------------- +-- the cc MoveBy +-- @field [parent=#cc] MoveBy#MoveBy MoveBy preloaded module + + +-------------------------------------------------------- +-- the cc MoveTo +-- @field [parent=#cc] MoveTo#MoveTo MoveTo preloaded module + + +-------------------------------------------------------- +-- the cc SkewTo +-- @field [parent=#cc] SkewTo#SkewTo SkewTo preloaded module + + +-------------------------------------------------------- +-- the cc SkewBy +-- @field [parent=#cc] SkewBy#SkewBy SkewBy preloaded module + + +-------------------------------------------------------- +-- the cc JumpBy +-- @field [parent=#cc] JumpBy#JumpBy JumpBy preloaded module + + +-------------------------------------------------------- +-- the cc JumpTo +-- @field [parent=#cc] JumpTo#JumpTo JumpTo preloaded module + + +-------------------------------------------------------- +-- the cc BezierBy +-- @field [parent=#cc] BezierBy#BezierBy BezierBy preloaded module + + +-------------------------------------------------------- +-- the cc BezierTo +-- @field [parent=#cc] BezierTo#BezierTo BezierTo preloaded module + + +-------------------------------------------------------- +-- the cc ScaleTo +-- @field [parent=#cc] ScaleTo#ScaleTo ScaleTo preloaded module + + +-------------------------------------------------------- +-- the cc ScaleBy +-- @field [parent=#cc] ScaleBy#ScaleBy ScaleBy preloaded module + + +-------------------------------------------------------- +-- the cc Blink +-- @field [parent=#cc] Blink#Blink Blink preloaded module + + +-------------------------------------------------------- +-- the cc FadeTo +-- @field [parent=#cc] FadeTo#FadeTo FadeTo preloaded module + + +-------------------------------------------------------- +-- the cc FadeIn +-- @field [parent=#cc] FadeIn#FadeIn FadeIn preloaded module + + +-------------------------------------------------------- +-- the cc FadeOut +-- @field [parent=#cc] FadeOut#FadeOut FadeOut preloaded module + + +-------------------------------------------------------- +-- the cc TintTo +-- @field [parent=#cc] TintTo#TintTo TintTo preloaded module + + +-------------------------------------------------------- +-- the cc TintBy +-- @field [parent=#cc] TintBy#TintBy TintBy preloaded module + + +-------------------------------------------------------- +-- the cc DelayTime +-- @field [parent=#cc] DelayTime#DelayTime DelayTime preloaded module + + +-------------------------------------------------------- +-- the cc Animate +-- @field [parent=#cc] Animate#Animate Animate preloaded module + + +-------------------------------------------------------- +-- the cc TargetedAction +-- @field [parent=#cc] TargetedAction#TargetedAction TargetedAction preloaded module + + +-------------------------------------------------------- +-- the cc ActionCamera +-- @field [parent=#cc] ActionCamera#ActionCamera ActionCamera preloaded module + + +-------------------------------------------------------- +-- the cc OrbitCamera +-- @field [parent=#cc] OrbitCamera#OrbitCamera OrbitCamera preloaded module + + +-------------------------------------------------------- +-- the cc ActionManager +-- @field [parent=#cc] ActionManager#ActionManager ActionManager preloaded module + + +-------------------------------------------------------- +-- the cc ActionEase +-- @field [parent=#cc] ActionEase#ActionEase ActionEase preloaded module + + +-------------------------------------------------------- +-- the cc EaseRateAction +-- @field [parent=#cc] EaseRateAction#EaseRateAction EaseRateAction preloaded module + + +-------------------------------------------------------- +-- the cc EaseIn +-- @field [parent=#cc] EaseIn#EaseIn EaseIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseOut +-- @field [parent=#cc] EaseOut#EaseOut EaseOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseInOut +-- @field [parent=#cc] EaseInOut#EaseInOut EaseInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseExponentialIn +-- @field [parent=#cc] EaseExponentialIn#EaseExponentialIn EaseExponentialIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseExponentialOut +-- @field [parent=#cc] EaseExponentialOut#EaseExponentialOut EaseExponentialOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseExponentialInOut +-- @field [parent=#cc] EaseExponentialInOut#EaseExponentialInOut EaseExponentialInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseSineIn +-- @field [parent=#cc] EaseSineIn#EaseSineIn EaseSineIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseSineOut +-- @field [parent=#cc] EaseSineOut#EaseSineOut EaseSineOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseSineInOut +-- @field [parent=#cc] EaseSineInOut#EaseSineInOut EaseSineInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseElastic +-- @field [parent=#cc] EaseElastic#EaseElastic EaseElastic preloaded module + + +-------------------------------------------------------- +-- the cc EaseElasticIn +-- @field [parent=#cc] EaseElasticIn#EaseElasticIn EaseElasticIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseElasticOut +-- @field [parent=#cc] EaseElasticOut#EaseElasticOut EaseElasticOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseElasticInOut +-- @field [parent=#cc] EaseElasticInOut#EaseElasticInOut EaseElasticInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseBounce +-- @field [parent=#cc] EaseBounce#EaseBounce EaseBounce preloaded module + + +-------------------------------------------------------- +-- the cc EaseBounceIn +-- @field [parent=#cc] EaseBounceIn#EaseBounceIn EaseBounceIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseBounceOut +-- @field [parent=#cc] EaseBounceOut#EaseBounceOut EaseBounceOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseBounceInOut +-- @field [parent=#cc] EaseBounceInOut#EaseBounceInOut EaseBounceInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseBackIn +-- @field [parent=#cc] EaseBackIn#EaseBackIn EaseBackIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseBackOut +-- @field [parent=#cc] EaseBackOut#EaseBackOut EaseBackOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseBackInOut +-- @field [parent=#cc] EaseBackInOut#EaseBackInOut EaseBackInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseBezierAction +-- @field [parent=#cc] EaseBezierAction#EaseBezierAction EaseBezierAction preloaded module + + +-------------------------------------------------------- +-- the cc EaseQuadraticActionIn +-- @field [parent=#cc] EaseQuadraticActionIn#EaseQuadraticActionIn EaseQuadraticActionIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseQuadraticActionOut +-- @field [parent=#cc] EaseQuadraticActionOut#EaseQuadraticActionOut EaseQuadraticActionOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseQuadraticActionInOut +-- @field [parent=#cc] EaseQuadraticActionInOut#EaseQuadraticActionInOut EaseQuadraticActionInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseQuarticActionIn +-- @field [parent=#cc] EaseQuarticActionIn#EaseQuarticActionIn EaseQuarticActionIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseQuarticActionOut +-- @field [parent=#cc] EaseQuarticActionOut#EaseQuarticActionOut EaseQuarticActionOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseQuarticActionInOut +-- @field [parent=#cc] EaseQuarticActionInOut#EaseQuarticActionInOut EaseQuarticActionInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseQuinticActionIn +-- @field [parent=#cc] EaseQuinticActionIn#EaseQuinticActionIn EaseQuinticActionIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseQuinticActionOut +-- @field [parent=#cc] EaseQuinticActionOut#EaseQuinticActionOut EaseQuinticActionOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseQuinticActionInOut +-- @field [parent=#cc] EaseQuinticActionInOut#EaseQuinticActionInOut EaseQuinticActionInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseCircleActionIn +-- @field [parent=#cc] EaseCircleActionIn#EaseCircleActionIn EaseCircleActionIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseCircleActionOut +-- @field [parent=#cc] EaseCircleActionOut#EaseCircleActionOut EaseCircleActionOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseCircleActionInOut +-- @field [parent=#cc] EaseCircleActionInOut#EaseCircleActionInOut EaseCircleActionInOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseCubicActionIn +-- @field [parent=#cc] EaseCubicActionIn#EaseCubicActionIn EaseCubicActionIn preloaded module + + +-------------------------------------------------------- +-- the cc EaseCubicActionOut +-- @field [parent=#cc] EaseCubicActionOut#EaseCubicActionOut EaseCubicActionOut preloaded module + + +-------------------------------------------------------- +-- the cc EaseCubicActionInOut +-- @field [parent=#cc] EaseCubicActionInOut#EaseCubicActionInOut EaseCubicActionInOut preloaded module + + +-------------------------------------------------------- +-- the cc ActionInstant +-- @field [parent=#cc] ActionInstant#ActionInstant ActionInstant preloaded module + + +-------------------------------------------------------- +-- the cc Show +-- @field [parent=#cc] Show#Show Show preloaded module + + +-------------------------------------------------------- +-- the cc Hide +-- @field [parent=#cc] Hide#Hide Hide preloaded module + + +-------------------------------------------------------- +-- the cc ToggleVisibility +-- @field [parent=#cc] ToggleVisibility#ToggleVisibility ToggleVisibility preloaded module + + +-------------------------------------------------------- +-- the cc RemoveSelf +-- @field [parent=#cc] RemoveSelf#RemoveSelf RemoveSelf preloaded module + + +-------------------------------------------------------- +-- the cc FlipX +-- @field [parent=#cc] FlipX#FlipX FlipX preloaded module + + +-------------------------------------------------------- +-- the cc FlipY +-- @field [parent=#cc] FlipY#FlipY FlipY preloaded module + + +-------------------------------------------------------- +-- the cc Place +-- @field [parent=#cc] Place#Place Place preloaded module + + +-------------------------------------------------------- +-- the cc CallFunc +-- @field [parent=#cc] CallFunc#CallFunc CallFunc preloaded module + + +-------------------------------------------------------- +-- the cc GridAction +-- @field [parent=#cc] GridAction#GridAction GridAction preloaded module + + +-------------------------------------------------------- +-- the cc Grid3DAction +-- @field [parent=#cc] Grid3DAction#Grid3DAction Grid3DAction preloaded module + + +-------------------------------------------------------- +-- the cc TiledGrid3DAction +-- @field [parent=#cc] TiledGrid3DAction#TiledGrid3DAction TiledGrid3DAction preloaded module + + +-------------------------------------------------------- +-- the cc StopGrid +-- @field [parent=#cc] StopGrid#StopGrid StopGrid preloaded module + + +-------------------------------------------------------- +-- the cc ReuseGrid +-- @field [parent=#cc] ReuseGrid#ReuseGrid ReuseGrid preloaded module + + +-------------------------------------------------------- +-- the cc Waves3D +-- @field [parent=#cc] Waves3D#Waves3D Waves3D preloaded module + + +-------------------------------------------------------- +-- the cc FlipX3D +-- @field [parent=#cc] FlipX3D#FlipX3D FlipX3D preloaded module + + +-------------------------------------------------------- +-- the cc FlipY3D +-- @field [parent=#cc] FlipY3D#FlipY3D FlipY3D preloaded module + + +-------------------------------------------------------- +-- the cc Lens3D +-- @field [parent=#cc] Lens3D#Lens3D Lens3D preloaded module + + +-------------------------------------------------------- +-- the cc Ripple3D +-- @field [parent=#cc] Ripple3D#Ripple3D Ripple3D preloaded module + + +-------------------------------------------------------- +-- the cc Shaky3D +-- @field [parent=#cc] Shaky3D#Shaky3D Shaky3D preloaded module + + +-------------------------------------------------------- +-- the cc Liquid +-- @field [parent=#cc] Liquid#Liquid Liquid preloaded module + + +-------------------------------------------------------- +-- the cc Waves +-- @field [parent=#cc] Waves#Waves Waves preloaded module + + +-------------------------------------------------------- +-- the cc Twirl +-- @field [parent=#cc] Twirl#Twirl Twirl preloaded module + + +-------------------------------------------------------- +-- the cc PageTurn3D +-- @field [parent=#cc] PageTurn3D#PageTurn3D PageTurn3D preloaded module + + +-------------------------------------------------------- +-- the cc ProgressTo +-- @field [parent=#cc] ProgressTo#ProgressTo ProgressTo preloaded module + + +-------------------------------------------------------- +-- the cc ProgressFromTo +-- @field [parent=#cc] ProgressFromTo#ProgressFromTo ProgressFromTo preloaded module + + +-------------------------------------------------------- +-- the cc ShakyTiles3D +-- @field [parent=#cc] ShakyTiles3D#ShakyTiles3D ShakyTiles3D preloaded module + + +-------------------------------------------------------- +-- the cc ShatteredTiles3D +-- @field [parent=#cc] ShatteredTiles3D#ShatteredTiles3D ShatteredTiles3D preloaded module + + +-------------------------------------------------------- +-- the cc ShuffleTiles +-- @field [parent=#cc] ShuffleTiles#ShuffleTiles ShuffleTiles preloaded module + + +-------------------------------------------------------- +-- the cc FadeOutTRTiles +-- @field [parent=#cc] FadeOutTRTiles#FadeOutTRTiles FadeOutTRTiles preloaded module + + +-------------------------------------------------------- +-- the cc FadeOutBLTiles +-- @field [parent=#cc] FadeOutBLTiles#FadeOutBLTiles FadeOutBLTiles preloaded module + + +-------------------------------------------------------- +-- the cc FadeOutUpTiles +-- @field [parent=#cc] FadeOutUpTiles#FadeOutUpTiles FadeOutUpTiles preloaded module + + +-------------------------------------------------------- +-- the cc FadeOutDownTiles +-- @field [parent=#cc] FadeOutDownTiles#FadeOutDownTiles FadeOutDownTiles preloaded module + + +-------------------------------------------------------- +-- the cc TurnOffTiles +-- @field [parent=#cc] TurnOffTiles#TurnOffTiles TurnOffTiles preloaded module + + +-------------------------------------------------------- +-- the cc WavesTiles3D +-- @field [parent=#cc] WavesTiles3D#WavesTiles3D WavesTiles3D preloaded module + + +-------------------------------------------------------- +-- the cc JumpTiles3D +-- @field [parent=#cc] JumpTiles3D#JumpTiles3D JumpTiles3D preloaded module + + +-------------------------------------------------------- +-- the cc SplitRows +-- @field [parent=#cc] SplitRows#SplitRows SplitRows preloaded module + + +-------------------------------------------------------- +-- the cc SplitCols +-- @field [parent=#cc] SplitCols#SplitCols SplitCols preloaded module + + +-------------------------------------------------------- +-- the cc ActionTween +-- @field [parent=#cc] ActionTween#ActionTween ActionTween preloaded module + + +-------------------------------------------------------- +-- the cc CardinalSplineTo +-- @field [parent=#cc] CardinalSplineTo#CardinalSplineTo CardinalSplineTo preloaded module + + +-------------------------------------------------------- +-- the cc CardinalSplineBy +-- @field [parent=#cc] CardinalSplineBy#CardinalSplineBy CardinalSplineBy preloaded module + + +-------------------------------------------------------- +-- the cc CatmullRomTo +-- @field [parent=#cc] CatmullRomTo#CatmullRomTo CatmullRomTo preloaded module + + +-------------------------------------------------------- +-- the cc CatmullRomBy +-- @field [parent=#cc] CatmullRomBy#CatmullRomBy CatmullRomBy preloaded module + + +-------------------------------------------------------- +-- the cc AtlasNode +-- @field [parent=#cc] AtlasNode#AtlasNode AtlasNode preloaded module + + +-------------------------------------------------------- +-- the cc DrawNode +-- @field [parent=#cc] DrawNode#DrawNode DrawNode preloaded module + + +-------------------------------------------------------- +-- the cc LabelAtlas +-- @field [parent=#cc] LabelAtlas#LabelAtlas LabelAtlas preloaded module + + +-------------------------------------------------------- +-- the cc Director +-- @field [parent=#cc] Director#Director Director preloaded module + + +-------------------------------------------------------- +-- the cc GridBase +-- @field [parent=#cc] GridBase#GridBase GridBase preloaded module + + +-------------------------------------------------------- +-- the cc Grid3D +-- @field [parent=#cc] Grid3D#Grid3D Grid3D preloaded module + + +-------------------------------------------------------- +-- the cc TiledGrid3D +-- @field [parent=#cc] TiledGrid3D#TiledGrid3D TiledGrid3D preloaded module + + +-------------------------------------------------------- +-- the cc Sprite +-- @field [parent=#cc] Sprite#Sprite Sprite preloaded module + + +-------------------------------------------------------- +-- the cc LabelTTF +-- @field [parent=#cc] LabelTTF#LabelTTF LabelTTF preloaded module + + +-------------------------------------------------------- +-- the cc SpriteBatchNode +-- @field [parent=#cc] SpriteBatchNode#SpriteBatchNode SpriteBatchNode preloaded module + + +-------------------------------------------------------- +-- the cc Label +-- @field [parent=#cc] Label#Label Label preloaded module + + +-------------------------------------------------------- +-- the cc LabelBMFont +-- @field [parent=#cc] LabelBMFont#LabelBMFont LabelBMFont preloaded module + + +-------------------------------------------------------- +-- the cc Layer +-- @field [parent=#cc] Layer#Layer Layer preloaded module + + +-------------------------------------------------------- +-- the cc LayerColor +-- @field [parent=#cc] LayerColor#LayerColor LayerColor preloaded module + + +-------------------------------------------------------- +-- the cc LayerGradient +-- @field [parent=#cc] LayerGradient#LayerGradient LayerGradient preloaded module + + +-------------------------------------------------------- +-- the cc LayerMultiplex +-- @field [parent=#cc] LayerMultiplex#LayerMultiplex LayerMultiplex preloaded module + + +-------------------------------------------------------- +-- the cc Scene +-- @field [parent=#cc] Scene#Scene Scene preloaded module + + +-------------------------------------------------------- +-- the cc TransitionEaseScene +-- @field [parent=#cc] TransitionEaseScene#TransitionEaseScene TransitionEaseScene preloaded module + + +-------------------------------------------------------- +-- the cc TransitionScene +-- @field [parent=#cc] TransitionScene#TransitionScene TransitionScene preloaded module + + +-------------------------------------------------------- +-- the cc TransitionSceneOriented +-- @field [parent=#cc] TransitionSceneOriented#TransitionSceneOriented TransitionSceneOriented preloaded module + + +-------------------------------------------------------- +-- the cc TransitionRotoZoom +-- @field [parent=#cc] TransitionRotoZoom#TransitionRotoZoom TransitionRotoZoom preloaded module + + +-------------------------------------------------------- +-- the cc TransitionJumpZoom +-- @field [parent=#cc] TransitionJumpZoom#TransitionJumpZoom TransitionJumpZoom preloaded module + + +-------------------------------------------------------- +-- the cc TransitionMoveInL +-- @field [parent=#cc] TransitionMoveInL#TransitionMoveInL TransitionMoveInL preloaded module + + +-------------------------------------------------------- +-- the cc TransitionMoveInR +-- @field [parent=#cc] TransitionMoveInR#TransitionMoveInR TransitionMoveInR preloaded module + + +-------------------------------------------------------- +-- the cc TransitionMoveInT +-- @field [parent=#cc] TransitionMoveInT#TransitionMoveInT TransitionMoveInT preloaded module + + +-------------------------------------------------------- +-- the cc TransitionMoveInB +-- @field [parent=#cc] TransitionMoveInB#TransitionMoveInB TransitionMoveInB preloaded module + + +-------------------------------------------------------- +-- the cc TransitionSlideInL +-- @field [parent=#cc] TransitionSlideInL#TransitionSlideInL TransitionSlideInL preloaded module + + +-------------------------------------------------------- +-- the cc TransitionSlideInR +-- @field [parent=#cc] TransitionSlideInR#TransitionSlideInR TransitionSlideInR preloaded module + + +-------------------------------------------------------- +-- the cc TransitionSlideInB +-- @field [parent=#cc] TransitionSlideInB#TransitionSlideInB TransitionSlideInB preloaded module + + +-------------------------------------------------------- +-- the cc TransitionSlideInT +-- @field [parent=#cc] TransitionSlideInT#TransitionSlideInT TransitionSlideInT preloaded module + + +-------------------------------------------------------- +-- the cc TransitionShrinkGrow +-- @field [parent=#cc] TransitionShrinkGrow#TransitionShrinkGrow TransitionShrinkGrow preloaded module + + +-------------------------------------------------------- +-- the cc TransitionFlipX +-- @field [parent=#cc] TransitionFlipX#TransitionFlipX TransitionFlipX preloaded module + + +-------------------------------------------------------- +-- the cc TransitionFlipY +-- @field [parent=#cc] TransitionFlipY#TransitionFlipY TransitionFlipY preloaded module + + +-------------------------------------------------------- +-- the cc TransitionFlipAngular +-- @field [parent=#cc] TransitionFlipAngular#TransitionFlipAngular TransitionFlipAngular preloaded module + + +-------------------------------------------------------- +-- the cc TransitionZoomFlipX +-- @field [parent=#cc] TransitionZoomFlipX#TransitionZoomFlipX TransitionZoomFlipX preloaded module + + +-------------------------------------------------------- +-- the cc TransitionZoomFlipY +-- @field [parent=#cc] TransitionZoomFlipY#TransitionZoomFlipY TransitionZoomFlipY preloaded module + + +-------------------------------------------------------- +-- the cc TransitionZoomFlipAngular +-- @field [parent=#cc] TransitionZoomFlipAngular#TransitionZoomFlipAngular TransitionZoomFlipAngular preloaded module + + +-------------------------------------------------------- +-- the cc TransitionFade +-- @field [parent=#cc] TransitionFade#TransitionFade TransitionFade preloaded module + + +-------------------------------------------------------- +-- the cc TransitionCrossFade +-- @field [parent=#cc] TransitionCrossFade#TransitionCrossFade TransitionCrossFade preloaded module + + +-------------------------------------------------------- +-- the cc TransitionTurnOffTiles +-- @field [parent=#cc] TransitionTurnOffTiles#TransitionTurnOffTiles TransitionTurnOffTiles preloaded module + + +-------------------------------------------------------- +-- the cc TransitionSplitCols +-- @field [parent=#cc] TransitionSplitCols#TransitionSplitCols TransitionSplitCols preloaded module + + +-------------------------------------------------------- +-- the cc TransitionSplitRows +-- @field [parent=#cc] TransitionSplitRows#TransitionSplitRows TransitionSplitRows preloaded module + + +-------------------------------------------------------- +-- the cc TransitionFadeTR +-- @field [parent=#cc] TransitionFadeTR#TransitionFadeTR TransitionFadeTR preloaded module + + +-------------------------------------------------------- +-- the cc TransitionFadeBL +-- @field [parent=#cc] TransitionFadeBL#TransitionFadeBL TransitionFadeBL preloaded module + + +-------------------------------------------------------- +-- the cc TransitionFadeUp +-- @field [parent=#cc] TransitionFadeUp#TransitionFadeUp TransitionFadeUp preloaded module + + +-------------------------------------------------------- +-- the cc TransitionFadeDown +-- @field [parent=#cc] TransitionFadeDown#TransitionFadeDown TransitionFadeDown preloaded module + + +-------------------------------------------------------- +-- the cc TransitionPageTurn +-- @field [parent=#cc] TransitionPageTurn#TransitionPageTurn TransitionPageTurn preloaded module + + +-------------------------------------------------------- +-- the cc TransitionProgress +-- @field [parent=#cc] TransitionProgress#TransitionProgress TransitionProgress preloaded module + + +-------------------------------------------------------- +-- the cc TransitionProgressRadialCCW +-- @field [parent=#cc] TransitionProgressRadialCCW#TransitionProgressRadialCCW TransitionProgressRadialCCW preloaded module + + +-------------------------------------------------------- +-- the cc TransitionProgressRadialCW +-- @field [parent=#cc] TransitionProgressRadialCW#TransitionProgressRadialCW TransitionProgressRadialCW preloaded module + + +-------------------------------------------------------- +-- the cc TransitionProgressHorizontal +-- @field [parent=#cc] TransitionProgressHorizontal#TransitionProgressHorizontal TransitionProgressHorizontal preloaded module + + +-------------------------------------------------------- +-- the cc TransitionProgressVertical +-- @field [parent=#cc] TransitionProgressVertical#TransitionProgressVertical TransitionProgressVertical preloaded module + + +-------------------------------------------------------- +-- the cc TransitionProgressInOut +-- @field [parent=#cc] TransitionProgressInOut#TransitionProgressInOut TransitionProgressInOut preloaded module + + +-------------------------------------------------------- +-- the cc TransitionProgressOutIn +-- @field [parent=#cc] TransitionProgressOutIn#TransitionProgressOutIn TransitionProgressOutIn preloaded module + + +-------------------------------------------------------- +-- the cc MenuItem +-- @field [parent=#cc] MenuItem#MenuItem MenuItem preloaded module + + +-------------------------------------------------------- +-- the cc MenuItemLabel +-- @field [parent=#cc] MenuItemLabel#MenuItemLabel MenuItemLabel preloaded module + + +-------------------------------------------------------- +-- the cc MenuItemAtlasFont +-- @field [parent=#cc] MenuItemAtlasFont#MenuItemAtlasFont MenuItemAtlasFont preloaded module + + +-------------------------------------------------------- +-- the cc MenuItemFont +-- @field [parent=#cc] MenuItemFont#MenuItemFont MenuItemFont preloaded module + + +-------------------------------------------------------- +-- the cc MenuItemSprite +-- @field [parent=#cc] MenuItemSprite#MenuItemSprite MenuItemSprite preloaded module + + +-------------------------------------------------------- +-- the cc MenuItemImage +-- @field [parent=#cc] MenuItemImage#MenuItemImage MenuItemImage preloaded module + + +-------------------------------------------------------- +-- the cc MenuItemToggle +-- @field [parent=#cc] MenuItemToggle#MenuItemToggle MenuItemToggle preloaded module + + +-------------------------------------------------------- +-- the cc Menu +-- @field [parent=#cc] Menu#Menu Menu preloaded module + + +-------------------------------------------------------- +-- the cc ClippingNode +-- @field [parent=#cc] ClippingNode#ClippingNode ClippingNode preloaded module + + +-------------------------------------------------------- +-- the cc MotionStreak +-- @field [parent=#cc] MotionStreak#MotionStreak MotionStreak preloaded module + + +-------------------------------------------------------- +-- the cc ProgressTimer +-- @field [parent=#cc] ProgressTimer#ProgressTimer ProgressTimer preloaded module + + +-------------------------------------------------------- +-- the cc Image +-- @field [parent=#cc] Image#Image Image preloaded module + + +-------------------------------------------------------- +-- the cc RenderTexture +-- @field [parent=#cc] RenderTexture#RenderTexture RenderTexture preloaded module + + +-------------------------------------------------------- +-- the cc NodeGrid +-- @field [parent=#cc] NodeGrid#NodeGrid NodeGrid preloaded module + + +-------------------------------------------------------- +-- the cc ParticleBatchNode +-- @field [parent=#cc] ParticleBatchNode#ParticleBatchNode ParticleBatchNode preloaded module + + +-------------------------------------------------------- +-- the cc ParticleSystem +-- @field [parent=#cc] ParticleSystem#ParticleSystem ParticleSystem preloaded module + + +-------------------------------------------------------- +-- the cc ParticleSystemQuad +-- @field [parent=#cc] ParticleSystemQuad#ParticleSystemQuad ParticleSystemQuad preloaded module + + +-------------------------------------------------------- +-- the cc ParticleFire +-- @field [parent=#cc] ParticleFire#ParticleFire ParticleFire preloaded module + + +-------------------------------------------------------- +-- the cc ParticleFireworks +-- @field [parent=#cc] ParticleFireworks#ParticleFireworks ParticleFireworks preloaded module + + +-------------------------------------------------------- +-- the cc ParticleSun +-- @field [parent=#cc] ParticleSun#ParticleSun ParticleSun preloaded module + + +-------------------------------------------------------- +-- the cc ParticleGalaxy +-- @field [parent=#cc] ParticleGalaxy#ParticleGalaxy ParticleGalaxy preloaded module + + +-------------------------------------------------------- +-- the cc ParticleFlower +-- @field [parent=#cc] ParticleFlower#ParticleFlower ParticleFlower preloaded module + + +-------------------------------------------------------- +-- the cc ParticleMeteor +-- @field [parent=#cc] ParticleMeteor#ParticleMeteor ParticleMeteor preloaded module + + +-------------------------------------------------------- +-- the cc ParticleSpiral +-- @field [parent=#cc] ParticleSpiral#ParticleSpiral ParticleSpiral preloaded module + + +-------------------------------------------------------- +-- the cc ParticleExplosion +-- @field [parent=#cc] ParticleExplosion#ParticleExplosion ParticleExplosion preloaded module + + +-------------------------------------------------------- +-- the cc ParticleSmoke +-- @field [parent=#cc] ParticleSmoke#ParticleSmoke ParticleSmoke preloaded module + + +-------------------------------------------------------- +-- the cc ParticleSnow +-- @field [parent=#cc] ParticleSnow#ParticleSnow ParticleSnow preloaded module + + +-------------------------------------------------------- +-- the cc ParticleRain +-- @field [parent=#cc] ParticleRain#ParticleRain ParticleRain preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerCustom +-- @field [parent=#cc] EventListenerCustom#EventListenerCustom EventListenerCustom preloaded module + + +-------------------------------------------------------- +-- the cc EventCustom +-- @field [parent=#cc] EventCustom#EventCustom EventCustom preloaded module + + +-------------------------------------------------------- +-- the cc FileUtils +-- @field [parent=#cc] FileUtils#FileUtils FileUtils preloaded module + + +-------------------------------------------------------- +-- the cc Application +-- @field [parent=#cc] Application#Application Application preloaded module + + +-------------------------------------------------------- +-- the cc GLViewProtocol +-- @field [parent=#cc] GLViewProtocol#GLViewProtocol GLViewProtocol preloaded module + + +-------------------------------------------------------- +-- the cc GLView +-- @field [parent=#cc] GLView#GLView GLView preloaded module + + +-------------------------------------------------------- +-- the cc ShaderCache +-- @field [parent=#cc] ShaderCache#ShaderCache ShaderCache preloaded module + + +-------------------------------------------------------- +-- the cc AnimationCache +-- @field [parent=#cc] AnimationCache#AnimationCache AnimationCache preloaded module + + +-------------------------------------------------------- +-- the cc SpriteFrameCache +-- @field [parent=#cc] SpriteFrameCache#SpriteFrameCache SpriteFrameCache preloaded module + + +-------------------------------------------------------- +-- the cc UserDefault +-- @field [parent=#cc] UserDefault#UserDefault UserDefault preloaded module + + +-------------------------------------------------------- +-- the cc TextureCache +-- @field [parent=#cc] TextureCache#TextureCache TextureCache preloaded module + + +-------------------------------------------------------- +-- the cc ParallaxNode +-- @field [parent=#cc] ParallaxNode#ParallaxNode ParallaxNode preloaded module + + +-------------------------------------------------------- +-- the cc TMXObjectGroup +-- @field [parent=#cc] TMXObjectGroup#TMXObjectGroup TMXObjectGroup preloaded module + + +-------------------------------------------------------- +-- the cc TMXLayerInfo +-- @field [parent=#cc] TMXLayerInfo#TMXLayerInfo TMXLayerInfo preloaded module + + +-------------------------------------------------------- +-- the cc TMXTilesetInfo +-- @field [parent=#cc] TMXTilesetInfo#TMXTilesetInfo TMXTilesetInfo preloaded module + + +-------------------------------------------------------- +-- the cc TMXMapInfo +-- @field [parent=#cc] TMXMapInfo#TMXMapInfo TMXMapInfo preloaded module + + +-------------------------------------------------------- +-- the cc TMXLayer +-- @field [parent=#cc] TMXLayer#TMXLayer TMXLayer preloaded module + + +-------------------------------------------------------- +-- the cc TMXTiledMap +-- @field [parent=#cc] TMXTiledMap#TMXTiledMap TMXTiledMap preloaded module + + +-------------------------------------------------------- +-- the cc TileMapAtlas +-- @field [parent=#cc] TileMapAtlas#TileMapAtlas TileMapAtlas preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerTouchOneByOne +-- @field [parent=#cc] EventListenerTouchOneByOne#EventListenerTouchOneByOne EventListenerTouchOneByOne preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerTouchAllAtOnce +-- @field [parent=#cc] EventListenerTouchAllAtOnce#EventListenerTouchAllAtOnce EventListenerTouchAllAtOnce preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerKeyboard +-- @field [parent=#cc] EventListenerKeyboard#EventListenerKeyboard EventListenerKeyboard preloaded module + + +-------------------------------------------------------- +-- the cc EventMouse +-- @field [parent=#cc] EventMouse#EventMouse EventMouse preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerMouse +-- @field [parent=#cc] EventListenerMouse#EventListenerMouse EventListenerMouse preloaded module + + +-------------------------------------------------------- +-- the cc EventAcceleration +-- @field [parent=#cc] EventAcceleration#EventAcceleration EventAcceleration preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerAcceleration +-- @field [parent=#cc] EventListenerAcceleration#EventListenerAcceleration EventListenerAcceleration preloaded module + + +-------------------------------------------------------- +-- the cc Timer +-- @field [parent=#cc] Timer#Timer Timer preloaded module + + +-------------------------------------------------------- +-- the cc Scheduler +-- @field [parent=#cc] Scheduler#Scheduler Scheduler preloaded module + + +-------------------------------------------------------- +-- the cc Component +-- @field [parent=#cc] Component#Component Component preloaded module + + +-------------------------------------------------------- +-- the cc SimpleAudioEngine +-- @field [parent=#cc] SimpleAudioEngine#SimpleAudioEngine SimpleAudioEngine preloaded module + + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_extension_auto_api.lua b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_extension_auto_api.lua new file mode 100644 index 0000000000..3dee47c133 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_extension_auto_api.lua @@ -0,0 +1,89 @@ +-------------------------------- +-- @module cc + +-------------------------------------------------------- +-- the cc Scale9Sprite +-- @field [parent=#cc] Scale9Sprite#Scale9Sprite Scale9Sprite preloaded module + + +-------------------------------------------------------- +-- the cc Control +-- @field [parent=#cc] Control#Control Control preloaded module + + +-------------------------------------------------------- +-- the cc ControlButton +-- @field [parent=#cc] ControlButton#ControlButton ControlButton preloaded module + + +-------------------------------------------------------- +-- the cc ControlHuePicker +-- @field [parent=#cc] ControlHuePicker#ControlHuePicker ControlHuePicker preloaded module + + +-------------------------------------------------------- +-- the cc ControlSaturationBrightnessPicker +-- @field [parent=#cc] ControlSaturationBrightnessPicker#ControlSaturationBrightnessPicker ControlSaturationBrightnessPicker preloaded module + + +-------------------------------------------------------- +-- the cc ControlColourPicker +-- @field [parent=#cc] ControlColourPicker#ControlColourPicker ControlColourPicker preloaded module + + +-------------------------------------------------------- +-- the cc ControlPotentiometer +-- @field [parent=#cc] ControlPotentiometer#ControlPotentiometer ControlPotentiometer preloaded module + + +-------------------------------------------------------- +-- the cc ControlSlider +-- @field [parent=#cc] ControlSlider#ControlSlider ControlSlider preloaded module + + +-------------------------------------------------------- +-- the cc ControlStepper +-- @field [parent=#cc] ControlStepper#ControlStepper ControlStepper preloaded module + + +-------------------------------------------------------- +-- the cc ControlSwitch +-- @field [parent=#cc] ControlSwitch#ControlSwitch ControlSwitch preloaded module + + +-------------------------------------------------------- +-- the cc ScrollView +-- @field [parent=#cc] ScrollView#ScrollView ScrollView preloaded module + + +-------------------------------------------------------- +-- the cc TableViewCell +-- @field [parent=#cc] TableViewCell#TableViewCell TableViewCell preloaded module + + +-------------------------------------------------------- +-- the cc TableView +-- @field [parent=#cc] TableView#TableView TableView preloaded module + + +-------------------------------------------------------- +-- the cc EditBox +-- @field [parent=#cc] EditBox#EditBox EditBox preloaded module + + +-------------------------------------------------------- +-- the cc AssetsManager +-- @field [parent=#cc] AssetsManager#AssetsManager AssetsManager preloaded module + + +-------------------------------------------------------- +-- the cc CCBAnimationManager +-- @field [parent=#cc] CCBAnimationManager#CCBAnimationManager CCBAnimationManager preloaded module + + +-------------------------------------------------------- +-- the cc CCBReader +-- @field [parent=#cc] CCBReader#CCBReader CCBReader preloaded module + + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_gui_auto_api.lua b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_gui_auto_api.lua new file mode 100644 index 0000000000..918cb1f330 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_gui_auto_api.lua @@ -0,0 +1,94 @@ +-------------------------------- +-- @module ccui + +-------------------------------------------------------- +-- the ccui LayoutParameter +-- @field [parent=#ccui] LayoutParameter#LayoutParameter LayoutParameter preloaded module + + +-------------------------------------------------------- +-- the ccui LinearLayoutParameter +-- @field [parent=#ccui] LinearLayoutParameter#LinearLayoutParameter LinearLayoutParameter preloaded module + + +-------------------------------------------------------- +-- the ccui RelativeLayoutParameter +-- @field [parent=#ccui] RelativeLayoutParameter#RelativeLayoutParameter RelativeLayoutParameter preloaded module + + +-------------------------------------------------------- +-- the ccui Widget +-- @field [parent=#ccui] Widget#Widget Widget preloaded module + + +-------------------------------------------------------- +-- the ccui Layout +-- @field [parent=#ccui] Layout#Layout Layout preloaded module + + +-------------------------------------------------------- +-- the ccui Button +-- @field [parent=#ccui] Button#Button Button preloaded module + + +-------------------------------------------------------- +-- the ccui CheckBox +-- @field [parent=#ccui] CheckBox#CheckBox CheckBox preloaded module + + +-------------------------------------------------------- +-- the ccui ImageView +-- @field [parent=#ccui] ImageView#ImageView ImageView preloaded module + + +-------------------------------------------------------- +-- the ccui Text +-- @field [parent=#ccui] Text#Text Text preloaded module + + +-------------------------------------------------------- +-- the ccui TextAtlas +-- @field [parent=#ccui] TextAtlas#TextAtlas TextAtlas preloaded module + + +-------------------------------------------------------- +-- the ccui LoadingBar +-- @field [parent=#ccui] LoadingBar#LoadingBar LoadingBar preloaded module + + +-------------------------------------------------------- +-- the ccui ScrollView +-- @field [parent=#ccui] ScrollView#ScrollView ScrollView preloaded module + + +-------------------------------------------------------- +-- the ccui ListView +-- @field [parent=#ccui] ListView#ListView ListView preloaded module + + +-------------------------------------------------------- +-- the ccui Slider +-- @field [parent=#ccui] Slider#Slider Slider preloaded module + + +-------------------------------------------------------- +-- the ccui TextField +-- @field [parent=#ccui] TextField#TextField TextField preloaded module + + +-------------------------------------------------------- +-- the ccui TextBMFont +-- @field [parent=#ccui] TextBMFont#TextBMFont TextBMFont preloaded module + + +-------------------------------------------------------- +-- the ccui PageView +-- @field [parent=#ccui] PageView#PageView PageView preloaded module + + +-------------------------------------------------------- +-- the ccui Helper +-- @field [parent=#ccui] Helper#Helper Helper preloaded module + + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_physics_auto_api.lua b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_physics_auto_api.lua new file mode 100644 index 0000000000..81d3a65579 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_physics_auto_api.lua @@ -0,0 +1,154 @@ +-------------------------------- +-- @module cc + +-------------------------------------------------------- +-- the cc PhysicsShape +-- @field [parent=#cc] PhysicsShape#PhysicsShape PhysicsShape preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsShapeCircle +-- @field [parent=#cc] PhysicsShapeCircle#PhysicsShapeCircle PhysicsShapeCircle preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsShapeBox +-- @field [parent=#cc] PhysicsShapeBox#PhysicsShapeBox PhysicsShapeBox preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsShapePolygon +-- @field [parent=#cc] PhysicsShapePolygon#PhysicsShapePolygon PhysicsShapePolygon preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsShapeEdgeSegment +-- @field [parent=#cc] PhysicsShapeEdgeSegment#PhysicsShapeEdgeSegment PhysicsShapeEdgeSegment preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsShapeEdgeBox +-- @field [parent=#cc] PhysicsShapeEdgeBox#PhysicsShapeEdgeBox PhysicsShapeEdgeBox preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsShapeEdgePolygon +-- @field [parent=#cc] PhysicsShapeEdgePolygon#PhysicsShapeEdgePolygon PhysicsShapeEdgePolygon preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsShapeEdgeChain +-- @field [parent=#cc] PhysicsShapeEdgeChain#PhysicsShapeEdgeChain PhysicsShapeEdgeChain preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsBody +-- @field [parent=#cc] PhysicsBody#PhysicsBody PhysicsBody preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsWorld +-- @field [parent=#cc] PhysicsWorld#PhysicsWorld PhysicsWorld preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsDebugDraw +-- @field [parent=#cc] PhysicsDebugDraw#PhysicsDebugDraw PhysicsDebugDraw preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsContact +-- @field [parent=#cc] PhysicsContact#PhysicsContact PhysicsContact preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsContactPreSolve +-- @field [parent=#cc] PhysicsContactPreSolve#PhysicsContactPreSolve PhysicsContactPreSolve preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsContactPostSolve +-- @field [parent=#cc] PhysicsContactPostSolve#PhysicsContactPostSolve PhysicsContactPostSolve preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerPhysicsContact +-- @field [parent=#cc] EventListenerPhysicsContact#EventListenerPhysicsContact EventListenerPhysicsContact preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerPhysicsContactWithBodies +-- @field [parent=#cc] EventListenerPhysicsContactWithBodies#EventListenerPhysicsContactWithBodies EventListenerPhysicsContactWithBodies preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerPhysicsContactWithShapes +-- @field [parent=#cc] EventListenerPhysicsContactWithShapes#EventListenerPhysicsContactWithShapes EventListenerPhysicsContactWithShapes preloaded module + + +-------------------------------------------------------- +-- the cc EventListenerPhysicsContactWithGroup +-- @field [parent=#cc] EventListenerPhysicsContactWithGroup#EventListenerPhysicsContactWithGroup EventListenerPhysicsContactWithGroup preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJoint +-- @field [parent=#cc] PhysicsJoint#PhysicsJoint PhysicsJoint preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointFixed +-- @field [parent=#cc] PhysicsJointFixed#PhysicsJointFixed PhysicsJointFixed preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointLimit +-- @field [parent=#cc] PhysicsJointLimit#PhysicsJointLimit PhysicsJointLimit preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointPin +-- @field [parent=#cc] PhysicsJointPin#PhysicsJointPin PhysicsJointPin preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointDistance +-- @field [parent=#cc] PhysicsJointDistance#PhysicsJointDistance PhysicsJointDistance preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointSpring +-- @field [parent=#cc] PhysicsJointSpring#PhysicsJointSpring PhysicsJointSpring preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointGroove +-- @field [parent=#cc] PhysicsJointGroove#PhysicsJointGroove PhysicsJointGroove preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointRotarySpring +-- @field [parent=#cc] PhysicsJointRotarySpring#PhysicsJointRotarySpring PhysicsJointRotarySpring preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointRotaryLimit +-- @field [parent=#cc] PhysicsJointRotaryLimit#PhysicsJointRotaryLimit PhysicsJointRotaryLimit preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointRatchet +-- @field [parent=#cc] PhysicsJointRatchet#PhysicsJointRatchet PhysicsJointRatchet preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointGear +-- @field [parent=#cc] PhysicsJointGear#PhysicsJointGear PhysicsJointGear preloaded module + + +-------------------------------------------------------- +-- the cc PhysicsJointMotor +-- @field [parent=#cc] PhysicsJointMotor#PhysicsJointMotor PhysicsJointMotor preloaded module + + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_spine_auto_api.lua b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_spine_auto_api.lua new file mode 100644 index 0000000000..a8bdf11a2a --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_spine_auto_api.lua @@ -0,0 +1,14 @@ +-------------------------------- +-- @module sp + +-------------------------------------------------------- +-- the sp Skeleton +-- @field [parent=#sp] Skeleton#Skeleton Skeleton preloaded module + + +-------------------------------------------------------- +-- the sp SkeletonAnimation +-- @field [parent=#sp] SkeletonAnimation#SkeletonAnimation SkeletonAnimation preloaded module + + +return nil diff --git a/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_studio_auto_api.lua b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_studio_auto_api.lua new file mode 100644 index 0000000000..c13a5472e3 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/api/lua_cocos2dx_studio_auto_api.lua @@ -0,0 +1,149 @@ +-------------------------------- +-- @module ccs + +-------------------------------------------------------- +-- the ccs ActionObject +-- @field [parent=#ccs] ActionObject#ActionObject ActionObject preloaded module + + +-------------------------------------------------------- +-- the ccs ActionManagerEx +-- @field [parent=#ccs] ActionManagerEx#ActionManagerEx ActionManagerEx preloaded module + + +-------------------------------------------------------- +-- the ccs BaseData +-- @field [parent=#ccs] BaseData#BaseData BaseData preloaded module + + +-------------------------------------------------------- +-- the ccs DisplayData +-- @field [parent=#ccs] DisplayData#DisplayData DisplayData preloaded module + + +-------------------------------------------------------- +-- the ccs SpriteDisplayData +-- @field [parent=#ccs] SpriteDisplayData#SpriteDisplayData SpriteDisplayData preloaded module + + +-------------------------------------------------------- +-- the ccs ArmatureDisplayData +-- @field [parent=#ccs] ArmatureDisplayData#ArmatureDisplayData ArmatureDisplayData preloaded module + + +-------------------------------------------------------- +-- the ccs ParticleDisplayData +-- @field [parent=#ccs] ParticleDisplayData#ParticleDisplayData ParticleDisplayData preloaded module + + +-------------------------------------------------------- +-- the ccs BoneData +-- @field [parent=#ccs] BoneData#BoneData BoneData preloaded module + + +-------------------------------------------------------- +-- the ccs ArmatureData +-- @field [parent=#ccs] ArmatureData#ArmatureData ArmatureData preloaded module + + +-------------------------------------------------------- +-- the ccs FrameData +-- @field [parent=#ccs] FrameData#FrameData FrameData preloaded module + + +-------------------------------------------------------- +-- the ccs MovementBoneData +-- @field [parent=#ccs] MovementBoneData#MovementBoneData MovementBoneData preloaded module + + +-------------------------------------------------------- +-- the ccs MovementData +-- @field [parent=#ccs] MovementData#MovementData MovementData preloaded module + + +-------------------------------------------------------- +-- the ccs AnimationData +-- @field [parent=#ccs] AnimationData#AnimationData AnimationData preloaded module + + +-------------------------------------------------------- +-- the ccs ContourData +-- @field [parent=#ccs] ContourData#ContourData ContourData preloaded module + + +-------------------------------------------------------- +-- the ccs TextureData +-- @field [parent=#ccs] TextureData#TextureData TextureData preloaded module + + +-------------------------------------------------------- +-- the ccs Tween +-- @field [parent=#ccs] Tween#Tween Tween preloaded module + + +-------------------------------------------------------- +-- the ccs DisplayManager +-- @field [parent=#ccs] DisplayManager#DisplayManager DisplayManager preloaded module + + +-------------------------------------------------------- +-- the ccs Bone +-- @field [parent=#ccs] Bone#Bone Bone preloaded module + + +-------------------------------------------------------- +-- the ccs BatchNode +-- @field [parent=#ccs] BatchNode#BatchNode BatchNode preloaded module + + +-------------------------------------------------------- +-- the ccs ArmatureAnimation +-- @field [parent=#ccs] ArmatureAnimation#ArmatureAnimation ArmatureAnimation preloaded module + + +-------------------------------------------------------- +-- the ccs ArmatureDataManager +-- @field [parent=#ccs] ArmatureDataManager#ArmatureDataManager ArmatureDataManager preloaded module + + +-------------------------------------------------------- +-- the ccs Armature +-- @field [parent=#ccs] Armature#Armature Armature preloaded module + + +-------------------------------------------------------- +-- the ccs Skin +-- @field [parent=#ccs] Skin#Skin Skin preloaded module + + +-------------------------------------------------------- +-- the ccs ComAttribute +-- @field [parent=#ccs] ComAttribute#ComAttribute ComAttribute preloaded module + + +-------------------------------------------------------- +-- the ccs ComAudio +-- @field [parent=#ccs] ComAudio#ComAudio ComAudio preloaded module + + +-------------------------------------------------------- +-- the ccs ComController +-- @field [parent=#ccs] ComController#ComController ComController preloaded module + + +-------------------------------------------------------- +-- the ccs ComRender +-- @field [parent=#ccs] ComRender#ComRender ComRender preloaded module + + +-------------------------------------------------------- +-- the ccs GUIReader +-- @field [parent=#ccs] GUIReader#GUIReader GUIReader preloaded module + + +-------------------------------------------------------- +-- the ccs SceneReader +-- @field [parent=#ccs] SceneReader#SceneReader SceneReader preloaded module + + +return nil diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp.REMOVED.git-id b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp.REMOVED.git-id new file mode 100644 index 0000000000..af8ffc1e17 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp.REMOVED.git-id @@ -0,0 +1 @@ +b13893cb4ba1a930cf74e33ea68728455cb31f4a \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.hpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.hpp new file mode 100644 index 0000000000..6b553c6193 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.hpp @@ -0,0 +1,1539 @@ +#ifndef __cocos2dx_h__ +#define __cocos2dx_h__ + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +int register_all_cocos2dx(lua_State* tolua_S); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#endif // __cocos2dx_h__ diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_extension_auto.cpp.REMOVED.git-id b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_extension_auto.cpp.REMOVED.git-id new file mode 100644 index 0000000000..7d3b4412fd --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_extension_auto.cpp.REMOVED.git-id @@ -0,0 +1 @@ +337366fa551a6f02e7f44e832e351fad51895ca5 \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_extension_auto.hpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_extension_auto.hpp new file mode 100644 index 0000000000..9a70c65430 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_extension_auto.hpp @@ -0,0 +1,369 @@ +#ifndef __cocos2dx_extension_h__ +#define __cocos2dx_extension_h__ + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +int register_all_cocos2dx_extension(lua_State* tolua_S); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#endif // __cocos2dx_extension_h__ diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_gui_auto.cpp.REMOVED.git-id b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_gui_auto.cpp.REMOVED.git-id new file mode 100644 index 0000000000..f894d8b69c --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_gui_auto.cpp.REMOVED.git-id @@ -0,0 +1 @@ +8a35e56a7f283d9816a34997ab75c41201e1c1da \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_gui_auto.hpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_gui_auto.hpp new file mode 100644 index 0000000000..472d1be093 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_gui_auto.hpp @@ -0,0 +1,351 @@ +#ifndef __cocos2dx_ui_h__ +#define __cocos2dx_ui_h__ + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +int register_all_cocos2dx_ui(lua_State* tolua_S); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#endif // __cocos2dx_ui_h__ diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.cpp.REMOVED.git-id b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.cpp.REMOVED.git-id new file mode 100644 index 0000000000..fc065ab321 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.cpp.REMOVED.git-id @@ -0,0 +1 @@ +6f6bb7571ab92f9757a753c243a5061fd64a3fbd \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.hpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.hpp new file mode 100644 index 0000000000..3f9e257c26 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_physics_auto.hpp @@ -0,0 +1,266 @@ +#ifndef __cocos2dx_physics_h__ +#define __cocos2dx_physics_h__ + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +int register_all_cocos2dx_physics(lua_State* tolua_S); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#endif // __cocos2dx_physics_h__ diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_spine_auto.cpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_spine_auto.cpp new file mode 100644 index 0000000000..02abdfff42 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_spine_auto.cpp @@ -0,0 +1,771 @@ +#include "lua_cocos2dx_spine_auto.hpp" +#include "spine-cocos2dx.h" +#include "tolua_fix.h" +#include "LuaBasicConversions.h" + + + +int lua_cocos2dx_spine_Skeleton_setToSetupPose(lua_State* tolua_S) +{ + int argc = 0; + spine::Skeleton* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.Skeleton",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::Skeleton*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_Skeleton_setToSetupPose'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + if(!ok) + return 0; + cobj->setToSetupPose(); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "setToSetupPose",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_Skeleton_setToSetupPose'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_Skeleton_setBlendFunc(lua_State* tolua_S) +{ + int argc = 0; + spine::Skeleton* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.Skeleton",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::Skeleton*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_Skeleton_setBlendFunc'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 1) + { + cocos2d::BlendFunc arg0; + + #pragma warning NO CONVERSION TO NATIVE FOR BlendFunc; + if(!ok) + return 0; + cobj->setBlendFunc(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "setBlendFunc",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_Skeleton_setBlendFunc'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_Skeleton_onDraw(lua_State* tolua_S) +{ + int argc = 0; + spine::Skeleton* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.Skeleton",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::Skeleton*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_Skeleton_onDraw'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 2) + { + kmMat4 arg0; + bool arg1; + + #pragma warning NO CONVERSION TO NATIVE FOR kmMat4; + + ok &= luaval_to_boolean(tolua_S, 3,&arg1); + if(!ok) + return 0; + cobj->onDraw(arg0, arg1); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "onDraw",argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_Skeleton_onDraw'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_Skeleton_setSlotsToSetupPose(lua_State* tolua_S) +{ + int argc = 0; + spine::Skeleton* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.Skeleton",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::Skeleton*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_Skeleton_setSlotsToSetupPose'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + if(!ok) + return 0; + cobj->setSlotsToSetupPose(); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "setSlotsToSetupPose",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_Skeleton_setSlotsToSetupPose'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_Skeleton_getBlendFunc(lua_State* tolua_S) +{ + int argc = 0; + spine::Skeleton* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.Skeleton",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::Skeleton*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_Skeleton_getBlendFunc'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + if(!ok) + return 0; + const cocos2d::BlendFunc& ret = cobj->getBlendFunc(); + #pragma warning NO CONVERSION FROM NATIVE FOR BlendFunc; + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getBlendFunc",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_Skeleton_getBlendFunc'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_Skeleton_setSkin(lua_State* tolua_S) +{ + int argc = 0; + spine::Skeleton* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.Skeleton",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::Skeleton*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_Skeleton_setSkin'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 1) + { + const char* arg0; + + std::string arg0_tmp; ok &= luaval_to_std_string(tolua_S, 2, &arg0_tmp); arg0 = arg0_tmp.c_str(); + if(!ok) + return 0; + bool ret = cobj->setSkin(arg0); + tolua_pushboolean(tolua_S,(bool)ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "setSkin",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_Skeleton_setSkin'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_Skeleton_setBonesToSetupPose(lua_State* tolua_S) +{ + int argc = 0; + spine::Skeleton* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.Skeleton",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::Skeleton*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_Skeleton_setBonesToSetupPose'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + if(!ok) + return 0; + cobj->setBonesToSetupPose(); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "setBonesToSetupPose",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_Skeleton_setBonesToSetupPose'.",&tolua_err); +#endif + + return 0; +} +static int lua_cocos2dx_spine_Skeleton_finalize(lua_State* tolua_S) +{ + printf("luabindings: finalizing LUA object (Skeleton)"); + return 0; +} + +int lua_register_cocos2dx_spine_Skeleton(lua_State* tolua_S) +{ + tolua_usertype(tolua_S,"sp.Skeleton"); + tolua_cclass(tolua_S,"Skeleton","sp.Skeleton","cc.Node",NULL); + + tolua_beginmodule(tolua_S,"Skeleton"); + tolua_function(tolua_S,"setToSetupPose",lua_cocos2dx_spine_Skeleton_setToSetupPose); + tolua_function(tolua_S,"setBlendFunc",lua_cocos2dx_spine_Skeleton_setBlendFunc); + tolua_function(tolua_S,"onDraw",lua_cocos2dx_spine_Skeleton_onDraw); + tolua_function(tolua_S,"setSlotsToSetupPose",lua_cocos2dx_spine_Skeleton_setSlotsToSetupPose); + tolua_function(tolua_S,"getBlendFunc",lua_cocos2dx_spine_Skeleton_getBlendFunc); + tolua_function(tolua_S,"setSkin",lua_cocos2dx_spine_Skeleton_setSkin); + tolua_function(tolua_S,"setBonesToSetupPose",lua_cocos2dx_spine_Skeleton_setBonesToSetupPose); + tolua_endmodule(tolua_S); + std::string typeName = typeid(spine::Skeleton).name(); + g_luaType[typeName] = "sp.Skeleton"; + g_typeCast["Skeleton"] = "sp.Skeleton"; + return 1; +} + +int lua_cocos2dx_spine_SkeletonAnimation_addAnimation(lua_State* tolua_S) +{ + int argc = 0; + spine::SkeletonAnimation* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::SkeletonAnimation*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_SkeletonAnimation_addAnimation'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 3) + { + int arg0; + const char* arg1; + bool arg2; + + ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0); + + std::string arg1_tmp; ok &= luaval_to_std_string(tolua_S, 3, &arg1_tmp); arg1 = arg1_tmp.c_str(); + + ok &= luaval_to_boolean(tolua_S, 4,&arg2); + if(!ok) + return 0; + spTrackEntry* ret = cobj->addAnimation(arg0, arg1, arg2); + #pragma warning NO CONVERSION FROM NATIVE FOR spTrackEntry*; + return 1; + } + if (argc == 4) + { + int arg0; + const char* arg1; + bool arg2; + double arg3; + + ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0); + + std::string arg1_tmp; ok &= luaval_to_std_string(tolua_S, 3, &arg1_tmp); arg1 = arg1_tmp.c_str(); + + ok &= luaval_to_boolean(tolua_S, 4,&arg2); + + ok &= luaval_to_number(tolua_S, 5,&arg3); + if(!ok) + return 0; + spTrackEntry* ret = cobj->addAnimation(arg0, arg1, arg2, arg3); + #pragma warning NO CONVERSION FROM NATIVE FOR spTrackEntry*; + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "addAnimation",argc, 3); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_SkeletonAnimation_addAnimation'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_SkeletonAnimation_getCurrent(lua_State* tolua_S) +{ + int argc = 0; + spine::SkeletonAnimation* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::SkeletonAnimation*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_SkeletonAnimation_getCurrent'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + if(!ok) + return 0; + spTrackEntry* ret = cobj->getCurrent(); + #pragma warning NO CONVERSION FROM NATIVE FOR spTrackEntry*; + return 1; + } + if (argc == 1) + { + int arg0; + + ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0); + if(!ok) + return 0; + spTrackEntry* ret = cobj->getCurrent(arg0); + #pragma warning NO CONVERSION FROM NATIVE FOR spTrackEntry*; + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getCurrent",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_SkeletonAnimation_getCurrent'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_SkeletonAnimation_setMix(lua_State* tolua_S) +{ + int argc = 0; + spine::SkeletonAnimation* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::SkeletonAnimation*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_SkeletonAnimation_setMix'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 3) + { + const char* arg0; + const char* arg1; + double arg2; + + std::string arg0_tmp; ok &= luaval_to_std_string(tolua_S, 2, &arg0_tmp); arg0 = arg0_tmp.c_str(); + + std::string arg1_tmp; ok &= luaval_to_std_string(tolua_S, 3, &arg1_tmp); arg1 = arg1_tmp.c_str(); + + ok &= luaval_to_number(tolua_S, 4,&arg2); + if(!ok) + return 0; + cobj->setMix(arg0, arg1, arg2); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "setMix",argc, 3); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_SkeletonAnimation_setMix'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_SkeletonAnimation_setAnimation(lua_State* tolua_S) +{ + int argc = 0; + spine::SkeletonAnimation* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::SkeletonAnimation*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_SkeletonAnimation_setAnimation'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 3) + { + int arg0; + const char* arg1; + bool arg2; + + ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0); + + std::string arg1_tmp; ok &= luaval_to_std_string(tolua_S, 3, &arg1_tmp); arg1 = arg1_tmp.c_str(); + + ok &= luaval_to_boolean(tolua_S, 4,&arg2); + if(!ok) + return 0; + spTrackEntry* ret = cobj->setAnimation(arg0, arg1, arg2); + #pragma warning NO CONVERSION FROM NATIVE FOR spTrackEntry*; + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "setAnimation",argc, 3); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_SkeletonAnimation_setAnimation'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_SkeletonAnimation_clearTracks(lua_State* tolua_S) +{ + int argc = 0; + spine::SkeletonAnimation* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::SkeletonAnimation*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_SkeletonAnimation_clearTracks'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + if(!ok) + return 0; + cobj->clearTracks(); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "clearTracks",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_SkeletonAnimation_clearTracks'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_SkeletonAnimation_clearTrack(lua_State* tolua_S) +{ + int argc = 0; + spine::SkeletonAnimation* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::SkeletonAnimation*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_SkeletonAnimation_clearTrack'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + if(!ok) + return 0; + cobj->clearTrack(); + return 0; + } + if (argc == 1) + { + int arg0; + + ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0); + if(!ok) + return 0; + cobj->clearTrack(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "clearTrack",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_SkeletonAnimation_clearTrack'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_spine_SkeletonAnimation_onAnimationStateEvent(lua_State* tolua_S) +{ + int argc = 0; + spine::SkeletonAnimation* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (spine::SkeletonAnimation*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_spine_SkeletonAnimation_onAnimationStateEvent'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 4) + { + int arg0; + spEventType arg1; + spEvent* arg2; + int arg3; + + ok &= luaval_to_int32(tolua_S, 2,(int *)&arg0); + + ok &= luaval_to_int32(tolua_S, 3,(int *)&arg1); + + #pragma warning NO CONVERSION TO NATIVE FOR spEvent*; + + ok &= luaval_to_int32(tolua_S, 5,(int *)&arg3); + if(!ok) + return 0; + cobj->onAnimationStateEvent(arg0, arg1, arg2, arg3); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "onAnimationStateEvent",argc, 4); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_spine_SkeletonAnimation_onAnimationStateEvent'.",&tolua_err); +#endif + + return 0; +} +static int lua_cocos2dx_spine_SkeletonAnimation_finalize(lua_State* tolua_S) +{ + printf("luabindings: finalizing LUA object (SkeletonAnimation)"); + return 0; +} + +int lua_register_cocos2dx_spine_SkeletonAnimation(lua_State* tolua_S) +{ + tolua_usertype(tolua_S,"sp.SkeletonAnimation"); + tolua_cclass(tolua_S,"SkeletonAnimation","sp.SkeletonAnimation","sp.Skeleton",NULL); + + tolua_beginmodule(tolua_S,"SkeletonAnimation"); + tolua_function(tolua_S,"addAnimation",lua_cocos2dx_spine_SkeletonAnimation_addAnimation); + tolua_function(tolua_S,"getCurrent",lua_cocos2dx_spine_SkeletonAnimation_getCurrent); + tolua_function(tolua_S,"setMix",lua_cocos2dx_spine_SkeletonAnimation_setMix); + tolua_function(tolua_S,"setAnimation",lua_cocos2dx_spine_SkeletonAnimation_setAnimation); + tolua_function(tolua_S,"clearTracks",lua_cocos2dx_spine_SkeletonAnimation_clearTracks); + tolua_function(tolua_S,"clearTrack",lua_cocos2dx_spine_SkeletonAnimation_clearTrack); + tolua_function(tolua_S,"onAnimationStateEvent",lua_cocos2dx_spine_SkeletonAnimation_onAnimationStateEvent); + tolua_endmodule(tolua_S); + std::string typeName = typeid(spine::SkeletonAnimation).name(); + g_luaType[typeName] = "sp.SkeletonAnimation"; + g_typeCast["SkeletonAnimation"] = "sp.SkeletonAnimation"; + return 1; +} +TOLUA_API int register_all_cocos2dx_spine(lua_State* tolua_S) +{ + tolua_open(tolua_S); + + tolua_module(tolua_S,"sp",0); + tolua_beginmodule(tolua_S,"sp"); + + lua_register_cocos2dx_spine_Skeleton(tolua_S); + lua_register_cocos2dx_spine_SkeletonAnimation(tolua_S); + + tolua_endmodule(tolua_S); + return 1; +} + diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_spine_auto.hpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_spine_auto.hpp new file mode 100644 index 0000000000..cc7c903dca --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_spine_auto.hpp @@ -0,0 +1,29 @@ +#ifndef __cocos2dx_spine_h__ +#define __cocos2dx_spine_h__ + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +int register_all_cocos2dx_spine(lua_State* tolua_S); + + + + + + + + + + + + + + + + +#endif // __cocos2dx_spine_h__ diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_studio_auto.cpp.REMOVED.git-id b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_studio_auto.cpp.REMOVED.git-id new file mode 100644 index 0000000000..548c0c9831 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_studio_auto.cpp.REMOVED.git-id @@ -0,0 +1 @@ +b1abe1fd74f3d67444e32b3a3a0cad5c86b4269a \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_studio_auto.hpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_studio_auto.hpp new file mode 100644 index 0000000000..34f648f832 --- /dev/null +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_studio_auto.hpp @@ -0,0 +1,310 @@ +#ifndef __cocos2dx_studio_h__ +#define __cocos2dx_studio_h__ + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +int register_all_cocos2dx_studio(lua_State* tolua_S); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#endif // __cocos2dx_studio_h__ diff --git a/cocos/scripting/lua-bindings/manual/CCBProxy.cpp b/cocos/scripting/lua-bindings/manual/CCBProxy.cpp new file mode 100644 index 0000000000..f0ba9f92f8 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCBProxy.cpp @@ -0,0 +1,164 @@ +/**************************************************************************** + Copyright (c) 2013 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "CCBProxy.h" +#include "LuaScriptHandlerMgr.h" + +CCBReader* CCBProxy::createCCBReader() +{ + NodeLoaderLibrary *ccNodeLoaderLibrary = NodeLoaderLibrary::getInstance(); + CCBReader * pCCBReader = new CCBReader(ccNodeLoaderLibrary); + pCCBReader->autorelease(); + + return pCCBReader; +} +Node* CCBProxy::readCCBFromFile(const char *pszFileName,CCBReader* pCCBReader,bool bSetOwner) +{ + if (NULL == pCCBReader || NULL == pszFileName || 0 == strlen(pszFileName)) { + return NULL; + } + Node *pNode = NULL; + if (bSetOwner) { + pNode = pCCBReader->readNodeGraphFromFile(pszFileName,this); + } + else + { + pNode = pCCBReader->readNodeGraphFromFile(pszFileName); + } + + + return pNode; +} + +const char* CCBProxy::getNodeTypeName(Node* pNode) +{ + if (NULL == pNode) { + return NULL; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.LabelTTF"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.LabelBMFont"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.Sprite"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.ControlButton"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.LayerGradient"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.LayerColor"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.LayerGradient"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.Menu"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.MenuItemAtlasFont"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.MenuItemFont"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.MenuItemLabel"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.MenuItemImage"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.MenuItemToggle"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.MenuItemSprite"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.MenuItem"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.Layer"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.String"; + } + + if (NULL != dynamic_cast(pNode)) { + return "cc.ParticleSystemQuad"; + } + + return "No Support"; +} + +void CCBProxy::setCallback(Node* node,int handle, int controlEvents) +{ + if (nullptr == node) { + return; + } + + if (nullptr != dynamic_cast(node)) + { + MenuItem *menuItem = dynamic_cast(node); + if (nullptr != menuItem) { + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)menuItem, handle, ScriptHandlerMgr::HandlerType::MENU_CLICKED); + } + } + else if (NULL != dynamic_cast(node)) + { + Control* control = dynamic_cast(node); + if (nullptr != control) + { + for (int i = 0; i < kControlEventTotalNumber; i++) + { + if ((controlEvents & (1 << i))) + { + ScriptHandlerMgr::HandlerType handlerType = ScriptHandlerMgr::HandlerType((int)ScriptHandlerMgr::HandlerType::CONTROL_TOUCH_DOWN + i); + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)control, handle, handlerType); + } + } + } + } +} + diff --git a/cocos/scripting/lua-bindings/manual/CCBProxy.h b/cocos/scripting/lua-bindings/manual/CCBProxy.h new file mode 100644 index 0000000000..a073db913b --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCBProxy.h @@ -0,0 +1,31 @@ +#ifndef __CCBPROXY_H_ +#define __CCBPROXY_H_ + +#include "cocos2d.h" +#include "CCLuaEngine.h" +#include "SimpleAudioEngine.h" +#include "extensions/cocos-ext.h" +#include "cocosbuilder/CocosBuilder.h" + +USING_NS_CC; +USING_NS_CC_EXT; +using namespace cocosbuilder; + + +class CCBProxy : public Layer{ +public: + CCBProxy() { } + virtual ~ CCBProxy(){ } + CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(CCBProxy, create); + CCBReader* createCCBReader(); + Node* readCCBFromFile(const char *pszFileName,CCBReader* pCCBReader,bool bSetOwner = false); + const char* getNodeTypeName(Node* pNode); + void setCallback(Node* node,int handle, int controlEvents = 0); +}; + +class CCBLayerLoader:public LayerLoader{ +public: + CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(CCBLayerLoader, loader); +}; + +#endif // __CCBPROXY_H_ diff --git a/cocos/scripting/lua-bindings/manual/CCLuaBridge.cpp b/cocos/scripting/lua-bindings/manual/CCLuaBridge.cpp new file mode 100644 index 0000000000..50aaca89e5 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCLuaBridge.cpp @@ -0,0 +1,245 @@ +/**************************************************************************** + Copyright (c) 2013 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "CCLuaBridge.h" + +NS_CC_BEGIN + +lua_State *LuaBridge::s_luaState = NULL; +int LuaBridge::s_newFunctionId = 0; + +LuaStack *LuaBridge::getStack(void) +{ + return LuaEngine::getInstance()->getLuaStack(); +} + +int LuaBridge::pushLuaFunctionById(int functionId) +{ + lua_State *L = s_luaState; + int top = lua_gettop(L); + /* L: */ + lua_pushstring(L, LUA_BRIDGE_REGISTRY_FUNCTION); /* L: key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f_id */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + return 0; + } + + lua_pushnil(L); /* L: f_id nil */ + while (lua_next(L, -2) != 0) /* L: f_id f id */ + { + int value = lua_tonumber(L, -1); + lua_pop(L, 1); /* L: f_id f */ + if (value == functionId) + { + return -1; + } + } /* L: f_id */ + + lua_settop(L, top); + return 0; +} + +int LuaBridge::retainLuaFunctionById(int functionId) +{ + lua_State *L = s_luaState; + + lua_pushstring(L, LUA_BRIDGE_REGISTRY_RETAIN); /* L: key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: id_r */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + return 0; + } + + // get counter + lua_pushinteger(L, functionId); /* L: id_r id */ + lua_rawget(L, -2); /* L: id_r r */ + if (lua_type(L, -1) != LUA_TNUMBER) + { + lua_pop(L, 2); + return 0; + } + + // increase counter + int retainCount = lua_tonumber(L, -1); + retainCount++; + lua_pop(L, 1); /* L: id_r */ + lua_pushinteger(L, functionId); /* L: id_r id */ + lua_pushinteger(L, retainCount); /* L: id_r id r */ + lua_rawset(L, -3); /* id_r[id] = r, L: id_r */ + lua_pop(L, 1); + + CCLOG("CCLuaBridge::retainLuaFunctionById(%d) - retain count = %d", functionId, retainCount); + + return retainCount; + +} + +int LuaBridge::releaseLuaFunctionById(int functionId) +{ + lua_State *L = s_luaState; + /* L: */ + lua_pushstring(L, LUA_BRIDGE_REGISTRY_FUNCTION); /* L: key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f_id */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + CCLOG("CCLuaBridge::releaseLuaFunctionById() - LUA_BRIDGE_REGISTRY_FUNCTION not exists"); + return 0; + } + + lua_pushstring(L, LUA_BRIDGE_REGISTRY_RETAIN); /* L: f_id key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f_id id_r */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 2); + CCLOG("CCLuaBridge::releaseLuaFunctionById() - LUA_BRIDGE_REGISTRY_RETAIN not exists"); + return 0; + } + + lua_pushinteger(L, functionId); /* L: f_id id_r id */ + lua_rawget(L, -2); /* L: f_id id_r r */ + if (lua_type(L, -1) != LUA_TNUMBER) + { + lua_pop(L, 3); + CCLOG("CCLuaBridge::releaseLuaFunctionById() - function id %d not found", functionId); + return 0; + } + + int retainCount = lua_tonumber(L, -1); + retainCount--; + + if (retainCount > 0) + { + // update counter + lua_pop(L, 1); /* L: f_id id_r */ + lua_pushinteger(L, functionId); /* L: f_id id_r id */ + lua_pushinteger(L, retainCount); /* L: f_id id_r id r */ + lua_rawset(L, -3); /* id_r[id] = r, L: f_id id_r */ + lua_pop(L, 2); + CCLOG("CCLuaBridge::releaseLuaFunctionById() - function id %d retain count = %d", functionId, retainCount); + return retainCount; + } + + // remove lua function reference + lua_pop(L, 1); /* L: f_id id_r */ + lua_pushinteger(L, functionId); /* L: f_id id_r id */ + lua_pushnil(L); /* L: f_id id_r id nil */ + lua_rawset(L, -3); /* id_r[id] = nil, L: f_id id_r */ + + lua_pop(L, 1); /* L: f_id */ + lua_pushnil(L); /* L: f_id nil */ + while (lua_next(L, -2) != 0) /* L: f_id f id */ + { + int value = lua_tonumber(L, -1); + lua_pop(L, 1); /* L: f_id f */ + if (value == functionId) + { + lua_pushnil(L); /* L: f_id f nil */ + lua_rawset(L, -3); /* f_id[f] = nil, L: f_id */ + break; + } + } /* L: f_id */ + + lua_pop(L, 1); + CCLOG("CCLuaBridge::releaseLuaFunctionById() - function id %d released", functionId); + return 0; +} + +int LuaBridge::retainLuaFunction(lua_State *L, int functionIndex, int *retainCountReturn) +{ + /* L: f ... */ + lua_pushstring(L, LUA_BRIDGE_REGISTRY_FUNCTION); /* L: f ... key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f ... f_id */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + lua_newtable(L); + lua_pushstring(L, LUA_BRIDGE_REGISTRY_FUNCTION); + lua_pushvalue(L, -2); + lua_rawset(L, LUA_REGISTRYINDEX); + } + + lua_pushstring(L, LUA_BRIDGE_REGISTRY_RETAIN); /* L: f ... f_id key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f ... f_id id_r */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + lua_newtable(L); + lua_pushstring(L, LUA_BRIDGE_REGISTRY_RETAIN); + lua_pushvalue(L, -2); + lua_rawset(L, LUA_REGISTRYINDEX); + } + + // get function id + lua_pushvalue(L, functionIndex - 2); /* L: f ... f_id id_r f */ + lua_rawget(L, -3); /* L: f ... f_id id_r id */ + + int functionId; + if (lua_type(L, -1) != LUA_TNUMBER) + { + // first retain, create new id + lua_pop(L, 1); /* L: f ... f_id id_r */ + s_newFunctionId++; + functionId = s_newFunctionId; + + lua_pushvalue(L, functionIndex - 2); /* L: f ... f_id id_r f */ + lua_pushinteger(L, functionId); /* L: f ... f_id id_r f id */ + lua_rawset(L, -4); /* f_id[f] = id, L: f ... f_id id_r */ + lua_pushinteger(L, functionId); /* L: f ... f_id id_r id */ + } + else + { + functionId = lua_tonumber(L, -1); + } + + // get function retain + lua_pushvalue(L, -1); /* L: f ... f_id id_r id id */ + lua_rawget(L, -3); /* L: f ... f_id id_r id r */ + int retainCount = 1; + if (lua_type(L, -1) != LUA_TNUMBER) + { + // first retain, set retain count = 1 + lua_pop(L, 1); + lua_pushinteger(L, retainCount); + } + else + { + // add retain count + retainCount = lua_tonumber(L, -1); + retainCount++; + lua_pop(L, 1); + lua_pushinteger(L, retainCount); + } + + lua_rawset(L, -3); /* id_r[id] = r, L: f ... f_id id_r */ + lua_pop(L, 2); /* L: f ... */ + + if (retainCountReturn) *retainCountReturn = retainCount; + return functionId; +} + +NS_CC_END diff --git a/cocos/scripting/lua-bindings/manual/CCLuaBridge.h b/cocos/scripting/lua-bindings/manual/CCLuaBridge.h new file mode 100644 index 0000000000..0b5ba061c5 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCLuaBridge.h @@ -0,0 +1,71 @@ +/**************************************************************************** + Copyright (c) 2013 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#ifndef __LUA_BRIDGE_H_ +#define __LUA_BRIDGE_H_ + +#include +#include +#include "CCLuaEngine.h" + +extern "C" { +#include "lua.h" +} + +using namespace std; + +NS_CC_BEGIN + +typedef enum { + kLuaBridgeErrorOk = 0, + kLuaBridgeErrorInvalidParameters = -1, + kLuaBridgeErrorClassNotFound = -2, + kLuaBridgeErrorMethodNotFound = -3, + kLuaBridgeErrorExceptionOccurred = -4, + kLuaBridgeErrorMethodSignature = -5, + kLuaBridgeErrorJavaVMError = -6, +} LuaBridgeError; + +#define LUA_BRIDGE_REGISTRY_FUNCTION "lua_bridge_function_id" // table[function] = id +#define LUA_BRIDGE_REGISTRY_RETAIN "lua_bridge_function_id_retain" // table[id] = retain count + +class LuaBridge +{ +public: + static LuaStack *getStack(void); + static int pushLuaFunctionById(int functionId); + + static int retainLuaFunctionById(int functionId); + static int releaseLuaFunctionById(int functionId); + +protected: + static int retainLuaFunction(lua_State *L, int functionIndex, int *retainCountReturn); + + static lua_State *s_luaState; + static int s_newFunctionId; +}; + +NS_CC_END + +#endif // __LUA_BRIDGE_H_ diff --git a/cocos/scripting/lua-bindings/manual/CCLuaEngine.cpp b/cocos/scripting/lua-bindings/manual/CCLuaEngine.cpp new file mode 100644 index 0000000000..a5169cf738 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCLuaEngine.cpp @@ -0,0 +1,1084 @@ +/**************************************************************************** + Copyright (c) 2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "CCLuaEngine.h" +#include "tolua_fix.h" +#include "cocos2d.h" +#include "CCArray.h" +#include "CCScheduler.h" +#include "extensions/GUI/CCControlExtension/CCControl.h" +#include "LuaOpengl.h" +#include "lua_cocos2dx_manual.hpp" +#include "lua_cocos2dx_extension_manual.h" +#include "lua_cocos2dx_coco_studio_manual.hpp" +#include "lua_cocos2dx_gui_manual.hpp" + +NS_CC_BEGIN + +LuaEngine* LuaEngine::_defaultEngine = NULL; + +LuaEngine* LuaEngine::getInstance(void) +{ + if (!_defaultEngine) + { + _defaultEngine = new LuaEngine(); + _defaultEngine->init(); + } + return _defaultEngine; +} + +LuaEngine::~LuaEngine(void) +{ + CC_SAFE_RELEASE(_stack); + _defaultEngine = NULL; +} + +bool LuaEngine::init(void) +{ + _stack = LuaStack::create(); + _stack->retain(); + executeScriptFile("DeprecatedEnum.lua"); + executeScriptFile("DeprecatedClass.lua"); + executeScriptFile("Deprecated.lua"); + return true; +} + +void LuaEngine::addSearchPath(const char* path) +{ + _stack->addSearchPath(path); +} + +void LuaEngine::addLuaLoader(lua_CFunction func) +{ + _stack->addLuaLoader(func); +} + +void LuaEngine::removeScriptObjectByObject(Ref* pObj) +{ + _stack->removeScriptObjectByObject(pObj); + ScriptHandlerMgr::getInstance()->removeObjectAllHandlers(pObj); +} + +void LuaEngine::removeScriptHandler(int nHandler) +{ + _stack->removeScriptHandler(nHandler); +} + +int LuaEngine::executeString(const char *codes) +{ + int ret = _stack->executeString(codes); + _stack->clean(); + return ret; +} + +int LuaEngine::executeScriptFile(const char* filename) +{ + int ret = _stack->executeScriptFile(filename); + _stack->clean(); + return ret; +} + +int LuaEngine::executeGlobalFunction(const char* functionName) +{ + int ret = _stack->executeGlobalFunction(functionName); + _stack->clean(); + return ret; +} + +int LuaEngine::executeNodeEvent(Node* pNode, int nAction) +{ + return 0; +} + +int LuaEngine::executeMenuItemEvent(MenuItem* pMenuItem) +{ + return 0; +} + +int LuaEngine::executeNotificationEvent(NotificationCenter* pNotificationCenter, const char* pszName) +{ + int nHandler = pNotificationCenter->getObserverHandlerByName(pszName); + if (!nHandler) return 0; + + _stack->pushString(pszName); + int ret = _stack->executeFunctionByHandler(nHandler, 1); + _stack->clean(); + return ret; +} + +int LuaEngine::executeCallFuncActionEvent(CallFunc* pAction, Ref* pTarget/* = NULL*/) +{ + return 0; +} + +int LuaEngine::executeSchedule(int nHandler, float dt, Node* pNode/* = NULL*/) +{ + if (!nHandler) return 0; + _stack->pushFloat(dt); + int ret = _stack->executeFunctionByHandler(nHandler, 1); + _stack->clean(); + return ret; +} + +int LuaEngine::executeLayerTouchEvent(Layer* pLayer, int eventType, Touch *pTouch) +{ + return 0; +} + +int LuaEngine::executeLayerTouchesEvent(Layer* pLayer, int eventType, Set *pTouches) +{ + return 0; +} + +int LuaEngine::executeLayerKeypadEvent(Layer* pLayer, int eventType) +{ + return 0; +} + +int LuaEngine::executeAccelerometerEvent(Layer* pLayer, Acceleration* pAccelerationValue) +{ + return 0; +} + +int LuaEngine::executeEvent(int nHandler, const char* pEventName, Ref* pEventSource /* = NULL*/, const char* pEventSourceClassName /* = NULL*/) +{ + _stack->pushString(pEventName); + if (pEventSource) + { + _stack->pushObject(pEventSource, pEventSourceClassName ? pEventSourceClassName : "cc.Ref"); + } + int ret = _stack->executeFunctionByHandler(nHandler, pEventSource ? 2 : 1); + _stack->clean(); + return ret; +} + +bool LuaEngine::handleAssert(const char *msg) +{ + bool ret = _stack->handleAssert(msg); + _stack->clean(); + return ret; +} + +int LuaEngine::reallocateScriptHandler(int nHandler) +{ + int nRet = _stack->reallocateScriptHandler(nHandler); + _stack->clean(); + return nRet; +} + +bool LuaEngine::parseConfig(ConfigType type, const std::string& str) +{ + lua_getglobal(_stack->getLuaState(), "__onParseConfig"); + if (!lua_isfunction(_stack->getLuaState(), -1)) + { + CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", "__onParseConfig"); + lua_pop(_stack->getLuaState(), 1); + return false; + } + + _stack->pushInt((int)type); + _stack->pushString(str.c_str()); + + return _stack->executeFunction(2); +} + +int LuaEngine::sendEvent(ScriptEvent* evt) +{ + if (NULL == evt) + return 0; + + switch (evt->type) + { + case kNodeEvent: + { + return handleNodeEvent(evt->data); + } + break; + case kMenuClickedEvent: + { + return handleMenuClickedEvent(evt->data); + } + break; + case kCallFuncEvent: + { + return handleCallFuncActionEvent(evt->data); + } + break; + case kScheduleEvent: + { + return handleScheduler(evt->data); + } + break; + case kTouchEvent: + { + return handleTouchEvent(evt->data); + } + break; + case kTouchesEvent: + { + return handleTouchesEvent(evt->data); + } + break; + case kKeypadEvent: + { + return handleKeypadEvent(evt->data); + } + break; + case kAccelerometerEvent: + { + return handleAccelerometerEvent(evt->data); + } + break; + case kCommonEvent: + { + return handleCommonEvent(evt->data); + } + break; + case kControlEvent: + { + return handlerControlEvent(evt->data); + } + break; + default: + break; + } + + return 0; +} + +int LuaEngine::handleNodeEvent(void* data) +{ + if (NULL == data) + return 0; + + BasicScriptData* basicScriptData = (BasicScriptData*)data; + if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::NODE); + + if (0 == handler) + return 0; + + int action = *((int*)(basicScriptData->value)); + switch (action) + { + case kNodeOnEnter: + _stack->pushString("enter"); + break; + + case kNodeOnExit: + _stack->pushString("exit"); + break; + + case kNodeOnEnterTransitionDidFinish: + _stack->pushString("enterTransitionFinish"); + break; + + case kNodeOnExitTransitionDidStart: + _stack->pushString("exitTransitionStart"); + break; + + case kNodeOnCleanup: + _stack->pushString("cleanup"); + break; + + default: + return 0; + } + int ret = _stack->executeFunctionByHandler(handler, 1); + _stack->clean(); + return ret; +} + +int LuaEngine::handleMenuClickedEvent(void* data) +{ + if (NULL == data) + return 0; + + BasicScriptData* basicScriptData = (BasicScriptData*)data; + if (NULL == basicScriptData->nativeObject) + return 0; + + MenuItem* menuItem = static_cast(basicScriptData->nativeObject); + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(menuItem, ScriptHandlerMgr::HandlerType::MENU_CLICKED); + if (0 == handler) + return 0; + + _stack->pushInt(menuItem->getTag()); + _stack->pushObject(menuItem, "cc.MenuItem"); + int ret = _stack->executeFunctionByHandler(handler, 2); + _stack->clean(); + return ret; +} + +int LuaEngine::handleCallFuncActionEvent(void* data) +{ + if (NULL == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (NULL == basicScriptData->nativeObject) + return 0; + + int handler =ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::CALLFUNC); + + if (0 == handler) + return 0; + + Ref* target = static_cast(basicScriptData->value); + if (NULL != target) + { + _stack->pushObject(target, "cc.Node"); + } + int ret = _stack->executeFunctionByHandler(handler, target ? 1 : 0); + _stack->clean(); + return ret; +} + +int LuaEngine::handleScheduler(void* data) +{ + if (NULL == data) + return 0; + + SchedulerScriptData* schedulerInfo = static_cast(data); + + _stack->pushFloat(schedulerInfo->elapse); + int ret = _stack->executeFunctionByHandler(schedulerInfo->handler, 1); + _stack->clean(); + + return ret; +} + +int LuaEngine::handleKeypadEvent(void* data) +{ + if (NULL == data) + return 0; + + KeypadScriptData* keypadScriptData = static_cast(data); + if (NULL == keypadScriptData->nativeObject) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(keypadScriptData->nativeObject, ScriptHandlerMgr::HandlerType::KEYPAD); + + if (0 == handler) + return 0; + + EventKeyboard::KeyCode action = keypadScriptData->actionType; + + switch(action) + { + case EventKeyboard::KeyCode::KEY_BACKSPACE: + _stack->pushString("backClicked"); + break; + case EventKeyboard::KeyCode::KEY_MENU: + _stack->pushString("menuClicked"); + break; + default: + break; + } + + int ret = _stack->executeFunctionByHandler(handler, 1); + _stack->clean(); + return ret; +} + +int LuaEngine::handleAccelerometerEvent(void* data) +{ + if (NULL == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (NULL == basicScriptData->nativeObject || NULL == basicScriptData->value) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::ACCELEROMETER); + if (0 == handler) + return 0; + + Acceleration* accelerationValue = static_cast(basicScriptData->value); + _stack->pushFloat(accelerationValue->x); + _stack->pushFloat(accelerationValue->y); + _stack->pushFloat(accelerationValue->z); + _stack->pushFloat(accelerationValue->timestamp); + int ret = _stack->executeFunctionByHandler(handler, 4); + _stack->clean(); + return ret; +} + +int LuaEngine::handleCommonEvent(void* data) +{ + if (NULL == data) + return 0; + + CommonScriptData* commonInfo = static_cast(data); + if (NULL == commonInfo->eventName || 0 == commonInfo->handler) + return 0; + + _stack->pushString(commonInfo->eventName); + if (NULL != commonInfo->eventSource) + { + if (NULL != commonInfo->eventSourceClassName && strlen(commonInfo->eventSourceClassName) > 0) + { + _stack->pushObject(commonInfo->eventSource, commonInfo->eventSourceClassName); + } + else + { + _stack->pushObject(commonInfo->eventSource, "cc.Ref"); + } + } + int ret = _stack->executeFunctionByHandler(commonInfo->handler, commonInfo->eventSource ? 2 : 1); + _stack->clean(); + return ret; +} + +int LuaEngine::handleTouchEvent(void* data) +{ + if (NULL == data) + return 0; + + TouchScriptData* touchScriptData = static_cast(data); + if (NULL == touchScriptData->nativeObject || NULL == touchScriptData->touch) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)touchScriptData->nativeObject, ScriptHandlerMgr::HandlerType::TOUCHES); + + if (0 == handler) + return 0; + + switch (touchScriptData->actionType) + { + case EventTouch::EventCode::BEGAN: + _stack->pushString("began"); + break; + + case EventTouch::EventCode::MOVED: + _stack->pushString("moved"); + break; + + case EventTouch::EventCode::ENDED: + _stack->pushString("ended"); + break; + + case EventTouch::EventCode::CANCELLED: + _stack->pushString("cancelled"); + break; + + default: + return 0; + } + + int ret = 0; + + Touch* touch = touchScriptData->touch; + if (NULL != touch) { + const Point pt = Director::getInstance()->convertToGL(touch->getLocationInView()); + _stack->pushFloat(pt.x); + _stack->pushFloat(pt.y); + ret = _stack->executeFunctionByHandler(handler, 3); + } + _stack->clean(); + return ret; +} + +int LuaEngine::handleTouchesEvent(void* data) +{ + if (NULL == data) + return 0; + + TouchesScriptData* touchesScriptData = static_cast(data); + if (NULL == touchesScriptData->nativeObject || touchesScriptData->touches.size() == 0) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)touchesScriptData->nativeObject, ScriptHandlerMgr::HandlerType::TOUCHES); + + if (0 == handler) + return 0; + + switch (touchesScriptData->actionType) + { + case EventTouch::EventCode::BEGAN: + _stack->pushString("began"); + break; + + case EventTouch::EventCode::MOVED: + _stack->pushString("moved"); + break; + + case EventTouch::EventCode::ENDED: + _stack->pushString("ended"); + break; + + case EventTouch::EventCode::CANCELLED: + _stack->pushString("cancelled"); + break; + + default: + return 0; + } + + Director* pDirector = Director::getInstance(); + lua_State *L = _stack->getLuaState(); + int ret = 0; + + lua_newtable(L); + int i = 1; + for (auto& touch : touchesScriptData->touches) + { + Point pt = pDirector->convertToGL(touch->getLocationInView()); + lua_pushnumber(L, pt.x); + lua_rawseti(L, -2, i++); + lua_pushnumber(L, pt.y); + lua_rawseti(L, -2, i++); + lua_pushinteger(L, touch->getID()); + lua_rawseti(L, -2, i++); + } + ret = _stack->executeFunctionByHandler(handler, 2); + + _stack->clean(); + return ret; +} + +int LuaEngine::handlerControlEvent(void* data) +{ + if ( NULL == data ) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (NULL == basicScriptData->nativeObject) + return 0; + + int controlEvents = *((int*)(basicScriptData->value)); + + int handler = 0; + int ret = 0; + + for (int i = 0; i < kControlEventTotalNumber; i++) + { + if ((controlEvents & (1 << i))) + { + ScriptHandlerMgr::HandlerType controlHandler = ScriptHandlerMgr::HandlerType((int)ScriptHandlerMgr::HandlerType::CONTROL_TOUCH_DOWN + i); + handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, controlHandler); + + if (0 != handler) + { + _stack->pushObject((Ref*)basicScriptData->nativeObject, "cc.Ref"); + _stack->pushInt(controlEvents); + ret = _stack->executeFunctionByHandler(handler, 2); + _stack->clean(); + } + } + } + + return ret; +} + +int LuaEngine::handleEventAcc(void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, ScriptHandlerMgr::HandlerType::EVENT_ACC); + if (0 == handler) + return 0; + + lua_State* L = _stack->getLuaState(); + + LuaEventAccelerationData* eventListennerAcc = static_cast(basicScriptData->value); + toluafix_pushusertype_ccobject(L, eventListennerAcc->event->_ID, &(eventListennerAcc->event->_luaID), (void*)(eventListennerAcc->event),"cc.Event"); + Acceleration* accleration = static_cast(eventListennerAcc->acc); + lua_pushnumber(L,accleration->x); + lua_pushnumber(L,accleration->y); + lua_pushnumber(L,accleration->z); + lua_pushnumber(L,accleration->timestamp); + int ret = _stack->executeFunctionByHandler(handler, 5); + _stack->clean(); + return ret; +} + +int LuaEngine::handleEventKeyboard(ScriptHandlerMgr::HandlerType type, void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value) + return 0; + + LuaEventKeyboarData* keyboardData = static_cast(basicScriptData->value); + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, type); + if (0 == handler) + return 0; + + lua_State* L = _stack->getLuaState(); + lua_pushinteger(L, keyboardData->keyCode); + toluafix_pushusertype_ccobject(L, keyboardData->event->_ID, &(keyboardData->event->_luaID), (void*)(keyboardData->event),"cc.Event"); + int ret = _stack->executeFunctionByHandler(handler, 2); + _stack->clean(); + return ret; +} + +int LuaEngine::handleEventTouch(ScriptHandlerMgr::HandlerType type, void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value) + return 0; + + LuaEventTouchData* touchData = static_cast(basicScriptData->value); + if (nullptr == touchData->touch || nullptr == touchData->event) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler(basicScriptData->nativeObject, type); + if (0 == handler) + return 0; + + int ret = 0; + + Touch* touch = touchData->touch; + if (NULL != touch) { + _stack->pushObject(touchData->touch, "cc.Touch"); + _stack->pushObject(touchData->event, "cc.Event"); + ret = _stack->executeFunctionByHandler(handler, 2); + } + _stack->clean(); + + return ret; +} + +int LuaEngine::handleEventTouches(ScriptHandlerMgr::HandlerType type,void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value) + return 0; + + LuaEventTouchesData * touchesData = static_cast(basicScriptData->value); + if (nullptr == touchesData->event || touchesData->touches.size() == 0) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)basicScriptData->nativeObject, type); + + if (0 == handler) + return 0; + + lua_State *L = _stack->getLuaState(); + int ret = 0; + + lua_newtable(L); + int i = 1; + for (auto& touch : touchesData->touches) + { + _stack->pushInt(i); + _stack->pushObject(touch, "cc.Touch"); + lua_rawset(L, -3); + ++i; + } + _stack->pushObject(touchesData->event, "cc.Event"); + + ret = _stack->executeFunctionByHandler(handler, 2); + _stack->clean(); + return ret; +} + +int LuaEngine::handleEventMouse(ScriptHandlerMgr::HandlerType type, void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* basicScriptData = static_cast(data); + if (nullptr == basicScriptData->nativeObject || nullptr == basicScriptData->value) + return 0; + + LuaEventMouseData * mouseData = static_cast(basicScriptData->value); + if (nullptr == mouseData->event ) + return 0; + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)basicScriptData->nativeObject, type); + + if (0 == handler) + return 0; + + _stack->pushObject(mouseData->event, "cc.Event"); + int ret = _stack->executeFunctionByHandler(handler, 1); + _stack->clean(); + + return ret; +} + +int LuaEngine::handleEvenCustom(void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData * basicData = static_cast(data); + if (NULL == basicData->nativeObject || nullptr == basicData->value ) + return 0; + + EventCustom* eventCustom = static_cast(basicData->value); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)basicData->nativeObject, ScriptHandlerMgr::HandlerType::EVENT_CUSTIOM); + + if (0 == handler) + return 0; + + lua_State* L = _stack->getLuaState(); + toluafix_pushusertype_ccobject(L, eventCustom->_ID, &(eventCustom->_luaID), (void*)(eventCustom),"cc.EventCustom"); + int ret = _stack->executeFunctionByHandler(handler, 1); + _stack->clean(); + + return ret; +} + +int LuaEngine::handleEvent(ScriptHandlerMgr::HandlerType type,void* data) +{ + switch (type) + { + case ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL: + case ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM: + case ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED: + case ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT: + case ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT: + case ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE: + { + return handleTableViewEvent(type, data); + } + break; + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS: + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR: + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS: + { + return handleAssetsManagerEvent(type, data); + } + break; + case ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER: + { + return handleStudioEventListener(type, data); + } + break; + case ScriptHandlerMgr::HandlerType::ARMATURE_EVENT: + { + return handleArmatureWrapper(type, data); + } + break; + case ScriptHandlerMgr::HandlerType::EVENT_ACC: + { + return handleEventAcc(data); + } + break; + case ScriptHandlerMgr::HandlerType::EVENT_KEYBOARD_PRESSED: + case ScriptHandlerMgr::HandlerType::EVENT_KEYBOARD_RELEASED: + { + return handleEventKeyboard(type,data); + } + break; + case ScriptHandlerMgr::HandlerType::EVENT_CUSTIOM: + { + return handleEvenCustom(data); + } + break; + case ScriptHandlerMgr::HandlerType::EVENT_TOUCH_BEGAN: + case ScriptHandlerMgr::HandlerType::EVENT_TOUCH_MOVED: + case ScriptHandlerMgr::HandlerType::EVENT_TOUCH_ENDED: + case ScriptHandlerMgr::HandlerType::EVENT_TOUCH_CANCELLED: + { + return handleEventTouch(type, data); + } + break; + case ScriptHandlerMgr::HandlerType::EVENT_TOUCHES_BEGAN: + case ScriptHandlerMgr::HandlerType::EVENT_TOUCHES_MOVED: + case ScriptHandlerMgr::HandlerType::EVENT_TOUCHES_ENDED: + case ScriptHandlerMgr::HandlerType::EVENT_TOUCHES_CANCELLED: + { + return handleEventTouches(type, data); + } + break; + case ScriptHandlerMgr::HandlerType::EVENT_MOUSE_DOWN: + case ScriptHandlerMgr::HandlerType::EVENT_MOUSE_UP: + case ScriptHandlerMgr::HandlerType::EVENT_MOUSE_MOVE: + case ScriptHandlerMgr::HandlerType::EVENT_MOUSE_SCROLL: + { + return handleEventMouse(type, data); + } + break; + default: + break; + } + + return 0; +} + +int LuaEngine::handleEvent(ScriptHandlerMgr::HandlerType type, void* data, int numResults, const std::function& func) +{ + switch (type) + { + case ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX: + case ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX: + case ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS: + { + return handleTableViewEvent(type, data, numResults,func); + } + break; + default: + break; + } + + return 0; +} + +int LuaEngine::handleTableViewEvent(ScriptHandlerMgr::HandlerType type,void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* eventData = static_cast(data); + if (nullptr == eventData->nativeObject || nullptr == eventData->value) + return 0; + + LuaTableViewEventData* tableViewData = static_cast(eventData->value); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, type); + + if (0 == handler) + return 0; + + Ref* obj = static_cast(eventData->nativeObject); + if (nullptr == obj) + return 0; + + int ret = 0; + switch (type) + { + case ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL: + case ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM: + { + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"cc.TableView"); + ret = _stack->executeFunctionByHandler(handler, 1); + } + break; + case ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED: + case ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT: + case ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT: + case ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE: + { + Ref* cellObject = static_cast(tableViewData->value); + if (nullptr == cellObject) { + break; + } + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"cc.TableView"); + toluafix_pushusertype_ccobject(_stack->getLuaState(), cellObject->_ID, &(cellObject->_luaID), (void*)(cellObject),"cc.TableViewCell"); + ret = _stack->executeFunctionByHandler(handler, 2); + } + break; + default: + break; + } + + return ret; + +} + +int LuaEngine::handleTableViewEvent(ScriptHandlerMgr::HandlerType handlerType,void* data, int numResults, const std::function& func) +{ + if (nullptr == data || numResults <= 0) + return 0; + + BasicScriptData* eventData = static_cast(data); + if (nullptr == eventData->nativeObject || nullptr == eventData->value) + return 0; + + LuaTableViewEventData* tableViewData = static_cast(eventData->value); + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, handlerType); + + if (0 == handler) + return 0; + + Ref* obj = static_cast(eventData->nativeObject); + if (nullptr == obj) + return 0; + + int ret = 0; + switch (handlerType) + { + case ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX: + { + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"cc.TableView"); + _stack->pushLong(*((ssize_t*)tableViewData->value)); + ret = _stack->executeFunction(handler, 2, 2, func); + } + break; + case ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX: + { + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"cc.TableView"); + _stack->pushLong(*((ssize_t*)tableViewData->value)); + ret = _stack->executeFunction(handler, 2, 1, func); + } + break; + case ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS: + { + toluafix_pushusertype_ccobject(_stack->getLuaState(), obj->_ID, &(obj->_luaID), (void*)(obj),"cc.TableView"); + ret = _stack->executeFunction(handler, 1, 1, func); + } + break; + default: + break; + } + + return ret; +} + +int LuaEngine::handleAssetsManagerEvent(ScriptHandlerMgr::HandlerType type,void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* eventData = static_cast(data); + if (nullptr == eventData->nativeObject || nullptr == eventData->value) + return 0; + + LuaAssetsManagerEventData* assetsManagerData = static_cast(eventData->value); + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, type); + + if (0 == handler) + return 0; + + int ret = 0; + switch (type) + { + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS: + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR: + { + _stack->pushInt(assetsManagerData->value); + ret = _stack->executeFunctionByHandler(handler, 1); + } + break; + + case ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS: + { + ret = _stack->executeFunctionByHandler(handler, 0); + } + break; + + default: + break; + } + + return ret; +} + +int LuaEngine::handleStudioEventListener(ScriptHandlerMgr::HandlerType type,void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* eventData = static_cast(data); + if (nullptr == eventData->nativeObject || nullptr == eventData->value) + return 0; + + LuaStudioEventListenerData* listenerData = static_cast(eventData->value); + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER); + + if (0 == handler) + return 0; + + _stack->pushObject(listenerData->objTarget, "cc.Ref"); + _stack->pushInt(listenerData->eventType); + + _stack->executeFunctionByHandler(handler, 2); + _stack->clean(); + + return 0; +} + +int LuaEngine::handleArmatureWrapper(ScriptHandlerMgr::HandlerType type,void* data) +{ + if (nullptr == data) + return 0; + + BasicScriptData* eventData = static_cast(data); + if (nullptr == eventData->nativeObject || nullptr == eventData->value) + return 0; + + LuaArmatureWrapperEventData* wrapperData = static_cast(eventData->value); + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)eventData->nativeObject, ScriptHandlerMgr::HandlerType::ARMATURE_EVENT); + + if (0 == handler) + return 0; + + switch (wrapperData->eventType) + { + case LuaArmatureWrapperEventData::LuaArmatureWrapperEventType::MOVEMENT_EVENT: + { + LuaArmatureMovementEventData* movementData = static_cast(wrapperData->eventData); + + _stack->pushObject(movementData->objTarget, "ccs.Armature"); + _stack->pushInt(movementData->movementType); + _stack->pushString(movementData->movementID.c_str()); + _stack->executeFunctionByHandler(handler, 3); + } + break; + case LuaArmatureWrapperEventData::LuaArmatureWrapperEventType::FRAME_EVENT: + { + LuaArmatureFrameEventData* frameData = static_cast(wrapperData->eventData); + + _stack->pushObject(frameData->objTarget, "ccs.Bone"); + _stack->pushString(frameData->frameEventName.c_str()); + _stack->pushInt(frameData->originFrameIndex); + _stack->pushInt(frameData->currentFrameIndex); + _stack->executeFunctionByHandler(handler, 4); + } + break; + case LuaArmatureWrapperEventData::LuaArmatureWrapperEventType::FILE_ASYNC: + { + _stack->pushFloat(*(float*)wrapperData->eventData); + _stack->executeFunctionByHandler(handler, 1); + } + break; + default: + break; + } + + _stack->clean(); + + return 0; +} + +int LuaEngine::reload(const char* moduleFileName) +{ + return _stack->reload(moduleFileName); +} + +NS_CC_END diff --git a/cocos/scripting/lua-bindings/manual/CCLuaEngine.h b/cocos/scripting/lua-bindings/manual/CCLuaEngine.h new file mode 100644 index 0000000000..04c176b85f --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCLuaEngine.h @@ -0,0 +1,167 @@ +/**************************************************************************** + Copyright (c) 2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#ifndef __CC_LUA_ENGINE_H__ +#define __CC_LUA_ENGINE_H__ + +extern "C" { +#include "lua.h" +} + +#include "cocos2d.h" +#include "CCScriptSupport.h" +#include "CCLuaStack.h" +#include "CCLuaValue.h" +#include "LuaScriptHandlerMgr.h" + +NS_CC_BEGIN + +// Lua support for cocos2d-x +class LuaEngine : public ScriptEngineProtocol +{ +public: + static LuaEngine* getInstance(void); + CC_DEPRECATED_ATTRIBUTE static LuaEngine* defaultEngine(void) { return LuaEngine::getInstance(); } + virtual ~LuaEngine(void); + + virtual ccScriptType getScriptType() { + return kScriptTypeLua; + }; + + LuaStack *getLuaStack(void) { + return _stack; + } + + /** + @brief Add a path to find lua files in + @param path to be added to the Lua path + */ + virtual void addSearchPath(const char* path); + + /** + @brief Add lua loader, now it is used on android + */ + virtual void addLuaLoader(lua_CFunction func); + + /** + @brief reload script code contained in the given string. + @param moduleFileName String object holding the filename of the script file that is to be executed + @return 0 if the string is excuted correctly. + @return other if the string is excuted wrongly. + */ + virtual int reload(const char* moduleFileName); + + /** + @brief Remove Object from lua state + @param object to remove + */ + virtual void removeScriptObjectByObject(Ref* object); + + /** + @brief Remove Lua function reference + */ + virtual void removeScriptHandler(int nHandler); + + /** + @brief Reallocate Lua function reference + */ + virtual int reallocateScriptHandler(int nHandler); + + /** + @brief Execute script code contained in the given string. + @param codes holding the valid script code that should be executed. + @return 0 if the string is excuted correctly. + @return other if the string is excuted wrongly. + */ + virtual int executeString(const char* codes); + + /** + @brief Execute a script file. + @param filename String object holding the filename of the script file that is to be executed + */ + virtual int executeScriptFile(const char* filename); + + /** + @brief Execute a scripted global function. + @brief The function should not take any parameters and should return an integer. + @param functionName String object holding the name of the function, in the global script environment, that is to be executed. + @return The integer value returned from the script function. + */ + virtual int executeGlobalFunction(const char* functionName); + + virtual int executeNodeEvent(Node* pNode, int nAction); + virtual int executeMenuItemEvent(MenuItem* pMenuItem); + virtual int executeNotificationEvent(NotificationCenter* pNotificationCenter, const char* pszName); + virtual int executeCallFuncActionEvent(CallFunc* pAction, Ref* pTarget = NULL); + virtual int executeSchedule(int nHandler, float dt, Node* pNode = NULL); + virtual int executeLayerTouchesEvent(Layer* pLayer, int eventType, Set *pTouches); + virtual int executeLayerTouchEvent(Layer* pLayer, int eventType, Touch *pTouch); + virtual int executeLayerKeypadEvent(Layer* pLayer, int eventType); + /** execute a accelerometer event */ + virtual int executeAccelerometerEvent(Layer* pLayer, Acceleration* pAccelerationValue); + virtual int executeEvent(int nHandler, const char* pEventName, Ref* pEventSource = NULL, const char* pEventSourceClassName = NULL); + + virtual bool handleAssert(const char *msg); + + virtual bool parseConfig(ConfigType type, const std::string& str) override; + virtual int sendEvent(ScriptEvent* message) override; + virtual int handleEvent(ScriptHandlerMgr::HandlerType type,void* data); + virtual int handleEvent(ScriptHandlerMgr::HandlerType type, void* data, int numResults, const std::function& func); +private: + LuaEngine(void) + : _stack(NULL) + { + } + bool init(void); + int handleNodeEvent(void* data); + int handleMenuClickedEvent(void* data); + int handleCallFuncActionEvent(void* data); + int handleScheduler(void* data); + int handleKeypadEvent(void* data); + int handleAccelerometerEvent(void* data); + int handleCommonEvent(void* data); + int handleTouchEvent(void* data); + int handleTouchesEvent(void* data); + int handlerControlEvent(void* data); + int handleEvenCustom(void* data); + int handleAssetsManagerEvent(ScriptHandlerMgr::HandlerType type,void* data); + int handleTableViewEvent(ScriptHandlerMgr::HandlerType type,void* data); + int handleTableViewEvent(ScriptHandlerMgr::HandlerType type,void* data, int numResults, const std::function& func); + int handleStudioEventListener(ScriptHandlerMgr::HandlerType type,void* data); + int handleArmatureWrapper(ScriptHandlerMgr::HandlerType type,void* data); + int handleEventAcc(void* data); + int handleEventKeyboard(ScriptHandlerMgr::HandlerType type,void* data); + int handleEventTouch(ScriptHandlerMgr::HandlerType type, void* data); + int handleEventTouches(ScriptHandlerMgr::HandlerType type, void* data); + int handleEventMouse(ScriptHandlerMgr::HandlerType type, void* data); + +private: + static LuaEngine* _defaultEngine; + LuaStack *_stack; +}; + +NS_CC_END + +#endif // __CC_LUA_ENGINE_H__ diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp new file mode 100644 index 0000000000..7023b8f9cd --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp @@ -0,0 +1,688 @@ +/**************************************************************************** + Copyright (c) 2011-2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "CCLuaStack.h" +#include "tolua_fix.h" +extern "C" { +#include "lua.h" +#include "tolua++.h" +#include "lualib.h" +#include "lauxlib.h" +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) +#include "lua_extensions.h" +#endif +} + +#include "Cocos2dxLuaLoader.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) +#include "platform/ios/CCLuaObjcBridge.h" +#endif + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) +#include "platform/android/CCLuaJavaBridge.h" +#endif + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) +#include "Lua_web_socket.h" +#endif +#include "LuaOpengl.h" +#include "LuaScriptHandlerMgr.h" +#include "lua_cocos2dx_auto.hpp" +#include "lua_cocos2dx_extension_auto.hpp" +#include "lua_cocos2dx_manual.hpp" +#include "LuaBasicConversions.h" +#include "lua_cocos2dx_extension_manual.h" +#include "lua_cocos2dx_deprecated.h" +#include "lua_xml_http_request.h" +#include "lua_cocos2dx_studio_auto.hpp" +#include "lua_cocos2dx_coco_studio_manual.hpp" +#include "lua_cocos2dx_spine_auto.hpp" +#include "lua_cocos2dx_spine_manual.hpp" +#include "lua_cocos2dx_physics_auto.hpp" +#include "lua_cocos2dx_physics_manual.hpp" +#include "lua_cocos2dx_gui_auto.hpp" +#include "lua_cocos2dx_gui_manual.hpp" + +namespace { +int lua_print(lua_State * luastate) +{ + int nargs = lua_gettop(luastate); + + std::string t; + for (int i=1; i <= nargs; i++) + { + if (lua_istable(luastate, i)) + t += "table"; + else if (lua_isnone(luastate, i)) + t += "none"; + else if (lua_isnil(luastate, i)) + t += "nil"; + else if (lua_isboolean(luastate, i)) + { + if (lua_toboolean(luastate, i) != 0) + t += "true"; + else + t += "false"; + } + else if (lua_isfunction(luastate, i)) + t += "function"; + else if (lua_islightuserdata(luastate, i)) + t += "lightuserdata"; + else if (lua_isthread(luastate, i)) + t += "thread"; + else + { + const char * str = lua_tostring(luastate, i); + if (str) + t += lua_tostring(luastate, i); + else + t += lua_typename(luastate, lua_type(luastate, i)); + } + if (i!=nargs) + t += "\t"; + } + CCLOG("[LUA-print] %s", t.c_str()); + + return 0; +} +} // namespace { + +NS_CC_BEGIN + +LuaStack::~LuaStack() +{ + if (nullptr != _state) + { + lua_close(_state); + } +} + +LuaStack *LuaStack::create(void) +{ + LuaStack *stack = new LuaStack(); + stack->init(); + stack->autorelease(); + return stack; +} + +LuaStack *LuaStack::attach(lua_State *L) +{ + LuaStack *stack = new LuaStack(); + stack->initWithLuaState(L); + stack->autorelease(); + return stack; +} + +bool LuaStack::init(void) +{ + _state = lua_open(); + luaL_openlibs(_state); + toluafix_open(_state); + + // Register our version of the global "print" function + const luaL_reg global_functions [] = { + {"print", lua_print}, + {NULL, NULL} + }; + luaL_register(_state, "_G", global_functions); +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) + luaopen_lua_extensions(_state); +#endif + g_luaType.clear(); + register_all_cocos2dx(_state); + register_all_cocos2dx_extension(_state); + register_all_cocos2dx_deprecated(_state); + register_cocos2dx_extension_CCBProxy(_state); + tolua_opengl_open(_state); + register_all_cocos2dx_ui(_state); + register_all_cocos2dx_studio(_state); + register_all_cocos2dx_manual(_state); + register_all_cocos2dx_extension_manual(_state); + register_all_cocos2dx_manual_deprecated(_state); + register_all_cocos2dx_coco_studio_manual(_state); + register_all_cocos2dx_ui_manual(_state); + register_all_cocos2dx_spine(_state); + register_all_cocos2dx_spine_manual(_state); + register_glnode_manual(_state); +#if CC_USE_PHYSICS + register_all_cocos2dx_physics(_state); + register_all_cocos2dx_physics_manual(_state); +#endif +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) + LuaObjcBridge::luaopen_luaoc(_state); +#endif + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) + LuaJavaBridge::luaopen_luaj(_state); +#endif + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) + tolua_web_socket_open(_state); + register_web_socket_manual(_state); +#endif + + register_xml_http_request(_state); + + tolua_script_handler_mgr_open(_state); + + // add cocos2dx loader + addLuaLoader(cocos2dx_lua_loader); + + return true; +} + +bool LuaStack::initWithLuaState(lua_State *L) +{ + _state = L; + return true; +} + +void LuaStack::addSearchPath(const char* path) +{ + lua_getglobal(_state, "package"); /* L: package */ + lua_getfield(_state, -1, "path"); /* get package.path, L: package path */ + const char* cur_path = lua_tostring(_state, -1); + lua_pushfstring(_state, "%s;%s/?.lua", cur_path, path); /* L: package path newpath */ + lua_setfield(_state, -3, "path"); /* package.path = newpath, L: package path */ + lua_pop(_state, 2); /* L: - */ +} + +void LuaStack::addLuaLoader(lua_CFunction func) +{ + if (!func) return; + + // stack content after the invoking of the function + // get loader table + lua_getglobal(_state, "package"); /* L: package */ + lua_getfield(_state, -1, "loaders"); /* L: package, loaders */ + + // insert loader into index 2 + lua_pushcfunction(_state, func); /* L: package, loaders, func */ + for (int i = lua_objlen(_state, -2) + 1; i > 2; --i) + { + lua_rawgeti(_state, -2, i - 1); /* L: package, loaders, func, function */ + // we call lua_rawgeti, so the loader table now is at -3 + lua_rawseti(_state, -3, i); /* L: package, loaders, func */ + } + lua_rawseti(_state, -2, 2); /* L: package, loaders */ + + // set loaders into package + lua_setfield(_state, -2, "loaders"); /* L: package */ + + lua_pop(_state, 1); +} + + +void LuaStack::removeScriptObjectByObject(Ref* pObj) +{ + toluafix_remove_ccobject_by_refid(_state, pObj->_luaID); +} + +void LuaStack::removeScriptHandler(int nHandler) +{ + toluafix_remove_function_by_refid(_state, nHandler); +} + +int LuaStack::executeString(const char *codes) +{ + luaL_loadstring(_state, codes); + return executeFunction(0); +} + +int LuaStack::executeScriptFile(const char* filename) +{ +#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) + std::string code("require \""); + code.append(filename); + code.append("\""); + return executeString(code.c_str()); +#else + std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename); + ++_callFromLua; + int nRet = luaL_dofile(_state, fullPath.c_str()); + --_callFromLua; + CC_ASSERT(_callFromLua >= 0); + // lua_gc(_state, LUA_GCCOLLECT, 0); + + if (nRet != 0) + { + CCLOG("[LUA ERROR] %s", lua_tostring(_state, -1)); + lua_pop(_state, 1); + return nRet; + } + return 0; +#endif +} + +int LuaStack::executeGlobalFunction(const char* functionName) +{ + lua_getglobal(_state, functionName); /* query function by name, stack: function */ + if (!lua_isfunction(_state, -1)) + { + CCLOG("[LUA ERROR] name '%s' does not represent a Lua function", functionName); + lua_pop(_state, 1); + return 0; + } + return executeFunction(0); +} + +void LuaStack::clean(void) +{ + lua_settop(_state, 0); +} + +void LuaStack::pushInt(int intValue) +{ + lua_pushinteger(_state, intValue); +} + +void LuaStack::pushFloat(float floatValue) +{ + lua_pushnumber(_state, floatValue); +} + +void LuaStack::pushLong(long longValue) +{ + lua_pushnumber(_state, longValue); +} + +void LuaStack::pushBoolean(bool boolValue) +{ + lua_pushboolean(_state, boolValue); +} + +void LuaStack::pushString(const char* stringValue) +{ + lua_pushstring(_state, stringValue); +} + +void LuaStack::pushString(const char* stringValue, int length) +{ + lua_pushlstring(_state, stringValue, length); +} + +void LuaStack::pushNil(void) +{ + lua_pushnil(_state); +} + +void LuaStack::pushObject(Ref* objectValue, const char* typeName) +{ + toluafix_pushusertype_ccobject(_state, objectValue->_ID, &objectValue->_luaID, objectValue, typeName); +} + +void LuaStack::pushLuaValue(const LuaValue& value) +{ + const LuaValueType type = value.getType(); + if (type == LuaValueTypeInt) + { + return pushInt(value.intValue()); + } + else if (type == LuaValueTypeFloat) + { + return pushFloat(value.floatValue()); + } + else if (type == LuaValueTypeBoolean) + { + return pushBoolean(value.booleanValue()); + } + else if (type == LuaValueTypeString) + { + return pushString(value.stringValue().c_str()); + } + else if (type == LuaValueTypeDict) + { + pushLuaValueDict(value.dictValue()); + } + else if (type == LuaValueTypeArray) + { + pushLuaValueArray(value.arrayValue()); + } + else if (type == LuaValueTypeObject) + { + pushObject(value.ccobjectValue(), value.getObjectTypename().c_str()); + } +} + +void LuaStack::pushLuaValueDict(const LuaValueDict& dict) +{ + lua_newtable(_state); /* L: table */ + for (LuaValueDictIterator it = dict.begin(); it != dict.end(); ++it) + { + lua_pushstring(_state, it->first.c_str()); /* L: table key */ + pushLuaValue(it->second); /* L: table key value */ + lua_rawset(_state, -3); /* table.key = value, L: table */ + } +} + +void LuaStack::pushLuaValueArray(const LuaValueArray& array) +{ + lua_newtable(_state); /* L: table */ + int index = 1; + for (LuaValueArrayIterator it = array.begin(); it != array.end(); ++it) + { + pushLuaValue(*it); /* L: table value */ + lua_rawseti(_state, -2, index); /* table[index] = value, L: table */ + ++index; + } +} + +bool LuaStack::pushFunctionByHandler(int nHandler) +{ + toluafix_get_function_by_refid(_state, nHandler); /* L: ... func */ + if (!lua_isfunction(_state, -1)) + { + CCLOG("[LUA ERROR] function refid '%d' does not reference a Lua function", nHandler); + lua_pop(_state, 1); + return false; + } + return true; +} + +int LuaStack::executeFunction(int numArgs) +{ + int functionIndex = -(numArgs + 1); + if (!lua_isfunction(_state, functionIndex)) + { + CCLOG("value at stack [%d] is not function", functionIndex); + lua_pop(_state, numArgs + 1); // remove function and arguments + return 0; + } + + int traceback = 0; + lua_getglobal(_state, "__G__TRACKBACK__"); /* L: ... func arg1 arg2 ... G */ + if (!lua_isfunction(_state, -1)) + { + lua_pop(_state, 1); /* L: ... func arg1 arg2 ... */ + } + else + { + lua_insert(_state, functionIndex - 1); /* L: ... G func arg1 arg2 ... */ + traceback = functionIndex - 1; + } + + int error = 0; + ++_callFromLua; + error = lua_pcall(_state, numArgs, 1, traceback); /* L: ... [G] ret */ + --_callFromLua; + if (error) + { + if (traceback == 0) + { + CCLOG("[LUA ERROR] %s", lua_tostring(_state, - 1)); /* L: ... error */ + lua_pop(_state, 1); // remove error message from stack + } + else /* L: ... G error */ + { + lua_pop(_state, 2); // remove __G__TRACKBACK__ and error message from stack + } + return 0; + } + + // get return value + int ret = 0; + if (lua_isnumber(_state, -1)) + { + ret = lua_tointeger(_state, -1); + } + else if (lua_isboolean(_state, -1)) + { + ret = lua_toboolean(_state, -1); + } + // remove return value from stack + lua_pop(_state, 1); /* L: ... [G] */ + + if (traceback) + { + lua_pop(_state, 1); // remove __G__TRACKBACK__ from stack /* L: ... */ + } + + return ret; +} + +int LuaStack::executeFunctionByHandler(int nHandler, int numArgs) +{ + int ret = 0; + if (pushFunctionByHandler(nHandler)) /* L: ... arg1 arg2 ... func */ + { + if (numArgs > 0) + { + lua_insert(_state, -(numArgs + 1)); /* L: ... func arg1 arg2 ... */ + } + ret = executeFunction(numArgs); + } + lua_settop(_state, 0); + return ret; +} + +bool LuaStack::handleAssert(const char *msg) +{ + if (_callFromLua == 0) return false; + + lua_pushfstring(_state, "ASSERT FAILED ON LUA EXECUTE: %s", msg ? msg : "unknown"); + lua_error(_state); + return true; +} + +int LuaStack::reallocateScriptHandler(int nHandler) +{ + LUA_FUNCTION nNewHandle = -1; + + if (pushFunctionByHandler(nHandler)) + { + nNewHandle = toluafix_ref_function(_state,lua_gettop(_state),0); + } +/* + toluafix_get_function_by_refid(_state,nNewHandle); + if (!lua_isfunction(_state, -1)) + { + CCLOG("Error!"); + } + lua_settop(_state, 0); +*/ + return nNewHandle; + +} + +int LuaStack::executeFunctionReturnArray(int handler,int numArgs,int numResults,Array& resultArray) +{ + if (pushFunctionByHandler(handler)) /* L: ... arg1 arg2 ... func */ + { + if (numArgs > 0) + { + lua_insert(_state, -(numArgs + 1)); /* L: ... func arg1 arg2 ... */ + int functionIndex = -(numArgs + 1); + if (!lua_isfunction(_state, functionIndex)) + { + CCLOG("value at stack [%d] is not function", functionIndex); + lua_pop(_state, numArgs + 1); // remove function and arguments + return 0; + } + + int traceback = 0; + lua_getglobal(_state, "__G__TRACKBACK__"); /* L: ... func arg1 arg2 ... G */ + if (!lua_isfunction(_state, -1)) + { + lua_pop(_state, 1); /* L: ... func arg1 arg2 ... */ + } + else + { + lua_insert(_state, functionIndex - 1); /* L: ... G func arg1 arg2 ... */ + traceback = functionIndex - 1; + } + + int error = 0; + ++_callFromLua; + error = lua_pcall(_state, numArgs, numResults, traceback); /* L: ... [G] ret1 ret2 ... retResults*/ + --_callFromLua; + if (error) + { + if (traceback == 0) + { + CCLOG("[LUA ERROR] %s", lua_tostring(_state, - 1)); /* L: ... error */ + lua_pop(_state, 1); // remove error message from stack + } + else /* L: ... G error */ + { + lua_pop(_state, 2); // remove __G__TRACKBACK__ and error message from stack + } + return 0; + } + + // get return value,don't pass LUA_MULTRET to numResults, + if (numResults <= 0) + return 0; + + for (int i = 0 ; i < numResults; i++) + { + if (lua_type(_state, -1) == LUA_TBOOLEAN) { + + bool value = lua_toboolean(_state, -1); + resultArray.addObject(Bool::create(value)) ; + + }else if (lua_type(_state, -1) == LUA_TNUMBER) { + + double value = lua_tonumber(_state, -1); + resultArray.addObject(Double::create(value)); + + }else if (lua_type(_state, -1) == LUA_TSTRING) { + + const char* value = lua_tostring(_state, -1); + resultArray.addObject(String::create(value)); + + }else{ + + resultArray.addObject(static_cast(tolua_tousertype(_state, -1, NULL))); + } + // remove return value from stack + lua_pop(_state, 1); /* L: ... [G] ret1 ret2 ... ret*/ + } + /* L: ... [G]*/ + + if (traceback) + { + lua_pop(_state, 1); // remove __G__TRACKBACK__ from stack /* L: ... */ + } + } + } + lua_settop(_state, 0); + return 1; +} + +int LuaStack::executeFunction(int handler, int numArgs, int numResults, const std::function& func) +{ + if (pushFunctionByHandler(handler)) /* L: ... arg1 arg2 ... func */ + { + if (numArgs > 0) + { + lua_insert(_state, -(numArgs + 1)); /* L: ... func arg1 arg2 ... */ + } + + int functionIndex = -(numArgs + 1); + + if (!lua_isfunction(_state, functionIndex)) + { + CCLOG("value at stack [%d] is not function", functionIndex); + lua_pop(_state, numArgs + 1); // remove function and arguments + return 0; + } + + int traceCallback = 0; + lua_getglobal(_state, "__G__TRACKBACK__"); /* L: ... func arg1 arg2 ... G */ + if (!lua_isfunction(_state, -1)) + { + lua_pop(_state, 1); /* L: ... func arg1 arg2 ... */ + } + else + { + lua_insert(_state, functionIndex - 1); /* L: ... G func arg1 arg2 ... */ + traceCallback = functionIndex - 1; + } + + int error = 0; + ++_callFromLua; + error = lua_pcall(_state, numArgs, numResults, traceCallback); /* L: ... [G] ret1 ret2 ... retResults*/ + --_callFromLua; + + if (error) + { + if (traceCallback == 0) + { + CCLOG("[LUA ERROR] %s", lua_tostring(_state, - 1)); /* L: ... error */ + lua_pop(_state, 1); // remove error message from stack + } + else /* L: ... G error */ + { + lua_pop(_state, 2); // remove __G__TRACKBACK__ and error message from stack + } + return 0; + } + + // get return value,don't pass LUA_MULTRET to numResults, + do { + + if (numResults <= 0 || nullptr == func) + break; + + func(_state, numResults); + + } while (0); + + if (traceCallback) + { + lua_pop(_state, 1); // remove __G__TRACKBACK__ from stack /* L: ... */ + } + } + + return 1; +} + +int LuaStack::reload(const char* moduleFileName) +{ + if (nullptr == moduleFileName || strlen(moduleFileName) == 0) + { + CCLOG("moudulFileName is null"); + return 1; + } + + lua_getglobal(_state, "package"); /* L: package */ + lua_getfield(_state, -1, "loaded"); /* L: package loaded */ + lua_pushstring(_state, moduleFileName); + lua_gettable(_state, -2); /* L:package loaded module */ + if (!lua_isnil(_state, -1)) + { + lua_pushstring(_state, moduleFileName); /* L:package loaded module name */ + lua_pushnil(_state); /* L:package loaded module name nil*/ + lua_settable(_state, -4); /* L:package loaded module */ + } + lua_pop(_state, 3); + + std::string name = moduleFileName; + std::string require = "require \'" + name + "\'"; + return executeString(require.c_str()); +} + +NS_CC_END diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.h b/cocos/scripting/lua-bindings/manual/CCLuaStack.h new file mode 100644 index 0000000000..8ade08fd0f --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.h @@ -0,0 +1,148 @@ +/**************************************************************************** + Copyright (c) 2011-2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#ifndef __CC_LUA_STACK_H_ +#define __CC_LUA_STACK_H_ + +extern "C" { +#include "lua.h" +} + +#include "cocos2d.h" +#include "CCLuaValue.h" + +NS_CC_BEGIN + +class LuaStack : public Ref +{ +public: + static LuaStack *create(void); + static LuaStack *attach(lua_State *L); + + virtual ~LuaStack(); + + /** + @brief Method used to get a pointer to the lua_State that the script module is attached to. + @return A pointer to the lua_State that the script module is attached to. + */ + lua_State* getLuaState(void) { + return _state; + } + + /** + @brief Add a path to find lua files in + @param path to be added to the Lua path + */ + virtual void addSearchPath(const char* path); + + /** + @brief Add lua loader, now it is used on android + */ + virtual void addLuaLoader(lua_CFunction func); + + /** + @brief reload script code contained in the given string. + @param moduleFileName String object holding the filename of the script file that is to be executed + @return 0 if the string is excuted correctly. + @return other if the string is excuted wrongly. + */ + virtual int reload(const char* moduleFileName); + + /** + @brief Remove Object from lua state + @param object The object to be removed. + */ + virtual void removeScriptObjectByObject(Ref* object); + + /** + @brief Remove Lua function reference + */ + virtual void removeScriptHandler(int nHandler); + + /** + @brief Remove Lua function reference + */ + virtual int reallocateScriptHandler(int nHandler); + + /** + @brief Execute script code contained in the given string. + @param codes holding the valid script code that should be executed. + @return 0 if the string is excuted correctly. + @return other if the string is excuted wrongly. + */ + virtual int executeString(const char* codes); + + /** + @brief Execute a script file. + @param filename String object holding the filename of the script file that is to be executed + */ + virtual int executeScriptFile(const char* filename); + + /** + @brief Execute a scripted global function. + @brief The function should not take any parameters and should return an integer. + @param functionName String object holding the name of the function, in the global script environment, that is to be executed. + @return The integer value returned from the script function. + */ + virtual int executeGlobalFunction(const char* functionName); + + virtual void clean(void); + virtual void pushInt(int intValue); + virtual void pushFloat(float floatValue); + virtual void pushLong(long longValue); + virtual void pushBoolean(bool boolValue); + virtual void pushString(const char* stringValue); + virtual void pushString(const char* stringValue, int length); + virtual void pushNil(void); + virtual void pushObject(Ref* objectValue, const char* typeName); + virtual void pushLuaValue(const LuaValue& value); + virtual void pushLuaValueDict(const LuaValueDict& dict); + virtual void pushLuaValueArray(const LuaValueArray& array); + virtual bool pushFunctionByHandler(int nHandler); + virtual int executeFunction(int numArgs); + + virtual int executeFunctionByHandler(int nHandler, int numArgs); + virtual int executeFunctionReturnArray(int handler,int numArgs,int numResults,Array& resultArray); + virtual int executeFunction(int handler, int numArgs, int numResults, const std::function& func); + + virtual bool handleAssert(const char *msg); + +protected: + LuaStack(void) + : _state(NULL) + , _callFromLua(0) + { + } + + bool init(void); + bool initWithLuaState(lua_State *L); + + lua_State *_state; + int _callFromLua; +}; + +NS_CC_END + +#endif // __CC_LUA_STACK_H_ diff --git a/cocos/scripting/lua-bindings/manual/CCLuaValue.cpp b/cocos/scripting/lua-bindings/manual/CCLuaValue.cpp new file mode 100644 index 0000000000..1365ee6216 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCLuaValue.cpp @@ -0,0 +1,158 @@ +/**************************************************************************** + Copyright (c) 2011-2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "CCLuaValue.h" + + +NS_CC_BEGIN + +const LuaValue LuaValue::intValue(const int intValue) +{ + LuaValue value; + value._type = LuaValueTypeInt; + value._field.intValue = intValue; + return value; +} + +const LuaValue LuaValue::floatValue(const float floatValue) +{ + LuaValue value; + value._type = LuaValueTypeFloat; + value._field.floatValue = floatValue; + return value; +} + +const LuaValue LuaValue::booleanValue(const bool booleanValue) +{ + LuaValue value; + value._type = LuaValueTypeBoolean; + value._field.booleanValue = booleanValue; + return value; +} + +const LuaValue LuaValue::stringValue(const char* stringValue) +{ + LuaValue value; + value._type = LuaValueTypeString; + value._field.stringValue = new std::string(stringValue ? stringValue : ""); + return value; +} + +const LuaValue LuaValue::stringValue(const std::string& stringValue) +{ + LuaValue value; + value._type = LuaValueTypeString; + value._field.stringValue = new std::string(stringValue); + return value; +} + +const LuaValue LuaValue::dictValue(const LuaValueDict& dictValue) +{ + LuaValue value; + value._type = LuaValueTypeDict; + value._field.dictValue = new LuaValueDict(dictValue); + return value; +} + +const LuaValue LuaValue::arrayValue(const LuaValueArray& arrayValue) +{ + LuaValue value; + value._type = LuaValueTypeArray; + value._field.arrayValue = new LuaValueArray(arrayValue); + return value; +} + +const LuaValue LuaValue::ccobjectValue(Ref* ccobjectValue, const char* objectTypename) +{ + LuaValue value; + value._type = LuaValueTypeObject; + value._field.ccobjectValue = ccobjectValue; + ccobjectValue->retain(); + value._ccobjectType = new std::string(objectTypename); + return value; +} + +const LuaValue LuaValue::ccobjectValue(Ref* ccobjectValue, const std::string& objectTypename) +{ + return LuaValue::ccobjectValue(ccobjectValue, objectTypename.c_str()); +} + +LuaValue::LuaValue(const LuaValue& rhs) +{ + copy(rhs); +} + +LuaValue& LuaValue::operator=(const LuaValue& rhs) +{ + if (this != &rhs) copy(rhs); + return *this; +} + +LuaValue::~LuaValue(void) +{ + if (_type == LuaValueTypeString) + { + delete _field.stringValue; + } + else if (_type == LuaValueTypeDict) + { + delete _field.dictValue; + } + else if (_type == LuaValueTypeArray) + { + delete _field.arrayValue; + } + else if (_type == LuaValueTypeObject) + { + _field.ccobjectValue->release(); + delete _ccobjectType; + } +} + +void LuaValue::copy(const LuaValue& rhs) +{ + memcpy(&_field, &rhs._field, sizeof(_field)); + _type = rhs._type; + if (_type == LuaValueTypeString) + { + _field.stringValue = new std::string(*rhs._field.stringValue); + } + else if (_type == LuaValueTypeDict) + { + _field.dictValue = new LuaValueDict(*rhs._field.dictValue); + } + else if (_type == LuaValueTypeArray) + { + _field.arrayValue = new LuaValueArray(*rhs._field.arrayValue); + } + else if (_type == LuaValueTypeObject) + { + _field.ccobjectValue = rhs._field.ccobjectValue; + _field.ccobjectValue->retain(); + _ccobjectType = new std::string(*rhs._ccobjectType); + } +} + +NS_CC_END diff --git a/cocos/scripting/lua-bindings/manual/CCLuaValue.h b/cocos/scripting/lua-bindings/manual/CCLuaValue.h new file mode 100644 index 0000000000..2a915789f6 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/CCLuaValue.h @@ -0,0 +1,147 @@ +/**************************************************************************** + Copyright (c) 2011-2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#ifndef __CC_LUA_VALUE_H_ +#define __CC_LUA_VALUE_H_ + +#include +#include +#include + +extern "C" { +#include "lua.h" +} + +#include "ccTypes.h" +#include "CCRef.h" + +#if CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY +using std::memset; +using std::memcpy; +#endif + +NS_CC_BEGIN + +typedef int LUA_FUNCTION; +typedef int LUA_TABLE; +typedef int LUA_STRING; + +class LuaValue; + +typedef std::map LuaValueDict; +typedef LuaValueDict::const_iterator LuaValueDictIterator; +typedef std::list LuaValueArray; +typedef LuaValueArray::const_iterator LuaValueArrayIterator; + +typedef enum { + LuaValueTypeInt, + LuaValueTypeFloat, + LuaValueTypeBoolean, + LuaValueTypeString, + LuaValueTypeDict, + LuaValueTypeArray, + LuaValueTypeObject +} LuaValueType; + +typedef union { + int intValue; + float floatValue; + bool booleanValue; + std::string* stringValue; + LuaValueDict* dictValue; + LuaValueArray* arrayValue; + Ref* ccobjectValue; +} LuaValueField; + +class LuaValue +{ +public: + static const LuaValue intValue(const int intValue); + static const LuaValue floatValue(const float floatValue); + static const LuaValue booleanValue(const bool booleanValue); + static const LuaValue stringValue(const char* stringValue); + static const LuaValue stringValue(const std::string& stringValue); + static const LuaValue dictValue(const LuaValueDict& dictValue); + static const LuaValue arrayValue(const LuaValueArray& arrayValue); + static const LuaValue ccobjectValue(Ref* ccobjectValue, const char* objectTypename); + static const LuaValue ccobjectValue(Ref* ccobjectValue, const std::string& objectTypename); + + LuaValue(void) + : _type(LuaValueTypeInt) + , _ccobjectType(NULL) + { + memset(&_field, 0, sizeof(_field)); + } + LuaValue(const LuaValue& rhs); + LuaValue& operator=(const LuaValue& rhs); + ~LuaValue(void); + + const LuaValueType getType(void) const { + return _type; + } + + const std::string& getObjectTypename(void) const { + return *_ccobjectType; + } + + int intValue(void) const { + return _field.intValue; + } + + float floatValue(void) const { + return _field.floatValue; + } + + bool booleanValue(void) const { + return _field.booleanValue; + } + + const std::string& stringValue(void) const { + return *_field.stringValue; + } + + const LuaValueDict& dictValue(void) const { + return *_field.dictValue; + } + + const LuaValueArray& arrayValue(void) const { + return *_field.arrayValue; + } + + Ref* ccobjectValue(void) const { + return _field.ccobjectValue; + } + +private: + LuaValueField _field; + LuaValueType _type; + std::string* _ccobjectType; + + void copy(const LuaValue& rhs); +}; + +NS_CC_END + +#endif // __CC_LUA_VALUE_H_ diff --git a/cocos/scripting/lua-bindings/manual/Cocos2dxLuaLoader.cpp b/cocos/scripting/lua-bindings/manual/Cocos2dxLuaLoader.cpp new file mode 100644 index 0000000000..6409c99f61 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/Cocos2dxLuaLoader.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** + Copyright (c) 2011-2012 cocos2d-x.org + Copyright (c) 2013-2014 Chukong Technologies Inc. + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#include "Cocos2dxLuaLoader.h" +#include +#include + +using namespace cocos2d; + +extern "C" +{ + int cocos2dx_lua_loader(lua_State *L) + { + std::string filename(luaL_checkstring(L, 1)); + size_t pos = filename.rfind(".lua"); + if (pos != std::string::npos) + { + filename = filename.substr(0, pos); + } + + pos = filename.find_first_of("."); + while (pos != std::string::npos) + { + filename.replace(pos, 1, "/"); + pos = filename.find_first_of("."); + } + filename.append(".lua"); + + Data data = FileUtils::getInstance()->getDataFromFile(filename); + + if (!data.isNull()) + { + if (luaL_loadbuffer(L, (char*)data.getBytes(), data.getSize(), filename.c_str()) != 0) + { + luaL_error(L, "error loading module %s from file %s :\n\t%s", + lua_tostring(L, 1), filename.c_str(), lua_tostring(L, -1)); + } + } + else + { + log("can not get file data of %s", filename.c_str()); + } + + return 1; + } +} diff --git a/cocos/scripting/lua-bindings/manual/Cocos2dxLuaLoader.h b/cocos/scripting/lua-bindings/manual/Cocos2dxLuaLoader.h new file mode 100644 index 0000000000..e251bac27b --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/Cocos2dxLuaLoader.h @@ -0,0 +1,39 @@ +/**************************************************************************** +Copyright (c) 2011-2012 cocos2d-x.org +Copyright (c) 2013-2014 Chukong Technologies Inc. + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#ifndef __COCOS2DX_LUA_LOADER_H__ +#define __COCOS2DX_LUA_LOADER_H__ + +#include "cocos2d.h" + +extern "C" +{ +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" + +extern int cocos2dx_lua_loader(lua_State *L); +} + +#endif // __COCOS2DX_LUA_LOADER_H__ \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/manual/LuaBasicConversions.cpp b/cocos/scripting/lua-bindings/manual/LuaBasicConversions.cpp new file mode 100644 index 0000000000..87bc3c3f4d --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/LuaBasicConversions.cpp @@ -0,0 +1,2225 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "LuaBasicConversions.h" +#include "tolua_fix.h" + +std::unordered_map g_luaType; +std::unordered_map g_typeCast; + +#if COCOS2D_DEBUG >=1 +void luaval_to_native_err(lua_State* L,const char* msg,tolua_Error* err) +{ + if (NULL == L || NULL == err || NULL == msg || 0 == strlen(msg)) + return; + + if (msg[0] == '#') + { + const char* expected = err->type; + const char* provided = tolua_typename(L,err->index); + if (msg[1]=='f') + { + int narg = err->index; + if (err->array) + CCLOG("%s\n argument #%d is array of '%s'; array of '%s' expected.\n",msg+2,narg,provided,expected); + else + CCLOG("%s\n argument #%d is '%s'; '%s' expected.\n",msg+2,narg,provided,expected); + } + else if (msg[1]=='v') + { + if (err->array) + CCLOG("%s\n value is array of '%s'; array of '%s' expected.\n",msg+2,provided,expected); + else + CCLOG("%s\n value is '%s'; '%s' expected.\n",msg+2,provided,expected); + } + } +} +#endif + +#ifdef __cplusplus +extern "C" { +#endif +extern int lua_isusertype (lua_State* L, int lo, const char* type); +#ifdef __cplusplus +} +#endif + +bool luaval_is_usertype(lua_State* L,int lo,const char* type, int def) +{ + if (def && lua_gettop(L)=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = (unsigned short)tolua_tonumber(L, lo, 0); + } + + return ok; +} + + +bool luaval_to_int32(lua_State* L,int lo,int* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_isnumber(L,lo,0,&tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = (int)tolua_tonumber(L, lo, 0); + } + + return ok; +} + +bool luaval_to_uint32(lua_State* L, int lo, unsigned int* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_isnumber(L,lo,0,&tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = (unsigned int)tolua_tonumber(L, lo, 0); + } + + return ok; +} + +bool luaval_to_uint16(lua_State* L,int lo,uint16_t* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_isnumber(L,lo,0,&tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = (unsigned char)tolua_tonumber(L, lo, 0); + } + + return ok; +} + +bool luaval_to_boolean(lua_State* L,int lo,bool* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_isboolean(L,lo,0,&tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = (bool)tolua_toboolean(L, lo, 0); + } + + return ok; +} + +bool luaval_to_number(lua_State* L,int lo,double* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_isnumber(L,lo,0,&tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = tolua_tonumber(L, lo, 0); + } + + return ok; +} + +bool luaval_to_long_long(lua_State* L,int lo,long long* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_isnumber(L,lo,0,&tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = (long long)tolua_tonumber(L, lo, 0); + } + + return ok; +} + +bool luaval_to_std_string(lua_State* L, int lo, std::string* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_iscppstring(L,lo,0,&tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = tolua_tocppstring(L,lo,NULL); + } + + return ok; +} + +bool luaval_to_point(lua_State* L,int lo,Point* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + + if (ok) + { + lua_pushstring(L, "x"); + lua_gettable(L, lo); + outValue->x = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_pushstring(L, "y"); + lua_gettable(L, lo); + outValue->y = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1); + lua_pop(L, 1); + } + return ok; +} + +bool luaval_to_physics_material(lua_State* L,int lo,PhysicsMaterial* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + + if (ok) + { + lua_pushstring(L, "density"); + lua_gettable(L, lo); + outValue->density = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_pushstring(L, "restitution"); + lua_gettable(L, lo); + outValue->restitution = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_pushstring(L, "friction"); + lua_gettable(L, lo); + outValue->friction = lua_isnil(L, -1) ? 0 : lua_tonumber(L, -1); + lua_pop(L, 1); + } + return ok; +} + +bool luaval_to_ssize(lua_State* L,int lo, ssize_t* outValue) +{ + return luaval_to_long(L, lo, reinterpret_cast(outValue)); +} + +bool luaval_to_long(lua_State* L,int lo, long* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_isnumber(L,lo,0,&tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = (long)tolua_tonumber(L, lo, 0); + } + + return ok; +} + +bool luaval_to_ulong(lua_State* L,int lo, unsigned long* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_isnumber(L,lo,0,&tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + *outValue = (unsigned long)tolua_tonumber(L, lo, 0); + } + + return ok; +} + +bool luaval_to_size(lua_State* L,int lo,Size* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + lua_pushstring(L, "width"); /* L: paramStack key */ + lua_gettable(L,lo);/* L: paramStack paramStack[lo][key] */ + outValue->width = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1);/* L: paramStack*/ + + lua_pushstring(L, "height"); + lua_gettable(L,lo); + outValue->height = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + } + + return ok; +} + +bool luaval_to_rect(lua_State* L,int lo,Rect* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + lua_pushstring(L, "x"); + lua_gettable(L,lo); + outValue->origin.x = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "y"); + lua_gettable(L,lo); + outValue->origin.y = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "width"); + lua_gettable(L,lo); + outValue->size.width = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "height"); + lua_gettable(L,lo); + outValue->size.height = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + } + + return ok; +} + +bool luaval_to_color4b(lua_State* L,int lo,Color4B* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if(ok) + { + lua_pushstring(L, "r"); + lua_gettable(L,lo); + outValue->r = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "g"); + lua_gettable(L,lo); + outValue->g = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "b"); + lua_gettable(L,lo); + outValue->b = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "a"); + lua_gettable(L,lo); + outValue->a = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + } + + return ok; +} + +bool luaval_to_color4f(lua_State* L,int lo,Color4F* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + lua_pushstring(L, "r"); + lua_gettable(L,lo); + outValue->r = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "g"); + lua_gettable(L,lo); + outValue->g = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "b"); + lua_gettable(L,lo); + outValue->b = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "a"); + lua_gettable(L,lo); + outValue->a = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + } + + return ok; +} + +bool luaval_to_color3b(lua_State* L,int lo,Color3B* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + lua_pushstring(L, "r"); + lua_gettable(L,lo); + outValue->r = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "g"); + lua_gettable(L,lo); + outValue->g = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "b"); + lua_gettable(L,lo); + outValue->b = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + } + + return ok; +} + +bool luaval_to_affinetransform(lua_State* L,int lo, AffineTransform* outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + lua_pushstring(L, "a"); + lua_gettable(L,lo); + outValue->a = lua_isnil(L,-1) ? 0 : (float)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "b"); + lua_gettable(L,lo); + outValue->b = lua_isnil(L,-1) ? 0 : (float)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "c"); + lua_gettable(L,lo); + outValue->b = lua_isnil(L,-1) ? 0 : (float)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "d"); + lua_gettable(L,lo); + outValue->b = lua_isnil(L,-1) ? 0 : (float)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "tx"); + lua_gettable(L,lo); + outValue->b = lua_isnil(L,-1) ? 0 : (float)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "ty"); + lua_gettable(L,lo); + outValue->b = lua_isnil(L,-1) ? 0 : (float)lua_tonumber(L,-1); + lua_pop(L,1); + } + return ok; +} + +bool luaval_to_fontdefinition(lua_State* L, int lo, FontDefinition* outValue ) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); + ok = false; +#endif + } + + if (ok) + { + // defaul values + const char * defautlFontName = "Arial"; + const int defaultFontSize = 32; + TextHAlignment defaultTextAlignment = TextHAlignment::LEFT; + TextVAlignment defaultTextVAlignment = TextVAlignment::TOP; + + // by default shadow and stroke are off + outValue->_shadow._shadowEnabled = false; + outValue->_stroke._strokeEnabled = false; + + // white text by default + outValue->_fontFillColor = Color3B::WHITE; + + lua_pushstring(L, "fontName"); + lua_gettable(L,lo); + outValue->_fontName = tolua_tocppstring(L,lo,defautlFontName); + lua_pop(L,1); + + lua_pushstring(L, "fontSize"); + lua_gettable(L,lo); + outValue->_fontSize = lua_isnil(L,-1) ? defaultFontSize : (int)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "fontAlignmentH"); + lua_gettable(L,lo); + outValue->_alignment = lua_isnil(L,-1) ? defaultTextAlignment : (TextHAlignment)(int)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "fontAlignmentV"); + lua_gettable(L,lo); + outValue->_vertAlignment = lua_isnil(L,-1) ? defaultTextVAlignment : (TextVAlignment)(int)lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "fontFillColor"); + lua_gettable(L,lo); + if (!lua_isnil(L,-1)) + { + luaval_to_color3b(L, -1, &outValue->_fontFillColor); + } + lua_pop(L,1); + + lua_pushstring(L, "fontDimensions"); + lua_gettable(L,lo); + if (!lua_isnil(L,-1)) + { + luaval_to_size(L, -1, &outValue->_dimensions); + } + lua_pop(L,1); + + lua_pushstring(L, "shadowEnabled"); + lua_gettable(L,lo); + if (!lua_isnil(L,-1)) + { + luaval_to_boolean(L, -1, &outValue->_shadow._shadowEnabled); + if (outValue->_shadow._shadowEnabled) + { + // default shadow values + outValue->_shadow._shadowOffset = Size(5, 5); + outValue->_shadow._shadowBlur = 1; + outValue->_shadow._shadowOpacity = 1; + } + + lua_pushstring(L, "shadowOffset"); + lua_gettable(L,lo); + if (!lua_isnil(L,-1)) + { + luaval_to_size(L, -1, &outValue->_shadow._shadowOffset); + } + lua_pop(L,1); + + lua_pushstring(L, "shadowBlur"); + lua_gettable(L,lo); + if (!lua_isnil(L,-1)) + { + outValue->_shadow._shadowBlur = (float)lua_tonumber(L,-1); + } + lua_pop(L,1); + + lua_pushstring(L, "shadowOpacity"); + lua_gettable(L,lo); + if (!lua_isnil(L,-1)) + { + outValue->_shadow._shadowOpacity = lua_tonumber(L,-1); + } + lua_pop(L,1); + } + lua_pop(L,1); + + lua_pushstring(L, "strokeEnabled"); + lua_gettable(L,lo); + if (!lua_isnil(L,-1)) + { + luaval_to_boolean(L, -1, &outValue->_stroke._strokeEnabled); + if (outValue->_stroke._strokeEnabled) + { + // default stroke values + outValue->_stroke._strokeSize = 1; + outValue->_stroke._strokeColor = Color3B::BLUE; + + lua_pushstring(L, "strokeColor"); + lua_gettable(L,lo); + if (!lua_isnil(L,-1)) + { + luaval_to_color3b(L, -1, &outValue->_stroke._strokeColor); + } + lua_pop(L,1); + + lua_pushstring(L, "strokeSize"); + lua_gettable(L,lo); + if (!lua_isnil(L,-1)) + { + outValue->_stroke._strokeSize = (float)lua_tonumber(L,-1); + } + lua_pop(L,1); + } + } + lua_pop(L,1); + } + + + return ok; +} + +bool luaval_to_array(lua_State* L,int lo, Array** outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + size_t len = lua_objlen(L, lo); + if (len > 0) + { + Array* arr = Array::createWithCapacity(len); + if (NULL == arr) + return false; + + for (int i = 0; i < len; i++) + { + lua_pushnumber(L,i + 1); + lua_gettable(L,lo); + if (lua_isnil(L,-1)) + { + lua_pop(L, 1); + continue; + } + + if (lua_isuserdata(L, -1)) + { + Ref* obj = static_cast(tolua_tousertype(L, -1, NULL) ); + if (NULL != obj) + { + arr->addObject(obj); + } + } + else if(lua_istable(L, -1)) + { + lua_pushnumber(L,1); + lua_gettable(L,-2); + if (lua_isnil(L, -1) ) + { + lua_pop(L,1); + Dictionary* dictVal = NULL; + if (luaval_to_dictionary(L,-1,&dictVal)) + { + arr->addObject(dictVal); + } + } + else + { + lua_pop(L,1); + Array* arrVal = NULL; + if(luaval_to_array(L, -1, &arrVal)) + { + arr->addObject(arrVal); + } + } + } + else if(lua_isstring(L, -1)) + { + std::string stringValue = ""; + if(luaval_to_std_string(L, -1, &stringValue) ) + { + arr->addObject(String::create(stringValue)); + } + } + else if(lua_isboolean(L, -1)) + { + bool boolVal = false; + if (luaval_to_boolean(L, -1, &boolVal)) + { + arr->addObject(Bool::create(boolVal)); + } + } + else if(lua_isnumber(L, -1)) + { + arr->addObject(Double::create(tolua_tonumber(L, -1, 0))); + } + else + { + CCASSERT(false, "not supported type"); + } + lua_pop(L, 1); + } + + *outValue = arr; + } + } + + return ok; +} + +bool luaval_to_dictionary(lua_State* L,int lo, Dictionary** outValue) +{ + if (NULL == L || NULL == outValue) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + std::string stringKey = ""; + std::string stringValue = ""; + bool boolVal = false; + Dictionary* dict = NULL; + lua_pushnil(L); /* L: lotable ..... nil */ + while ( 0 != lua_next(L, lo ) ) /* L: lotable ..... key value */ + { + if (!lua_isstring(L, -2)) + { + lua_pop(L, 1); + continue; + } + + if (NULL == dict) + { + dict = Dictionary::create(); + } + + if(luaval_to_std_string(L, -2, &stringKey)) + { + if (lua_isuserdata(L, -1)) + { + Ref* obj = static_cast(tolua_tousertype(L, -1, NULL) ); + if (NULL != obj) + { + //get the key to string + dict->setObject(obj, stringKey); + } + } + else if(lua_istable(L, -1)) + { + lua_pushnumber(L,1); + lua_gettable(L,-2); + if (lua_isnil(L, -1) ) + { + lua_pop(L,1); + Dictionary* dictVal = NULL; + if (luaval_to_dictionary(L,-1,&dictVal)) + { + dict->setObject(dictVal,stringKey); + } + } + else + { + lua_pop(L,1); + Array* arrVal = NULL; + if(luaval_to_array(L, -1, &arrVal)) + { + dict->setObject(arrVal,stringKey); + } + } + } + else if(lua_isstring(L, -1)) + { + if(luaval_to_std_string(L, -1, &stringValue)) + { + dict->setObject(String::create(stringValue), stringKey); + } + } + else if(lua_isboolean(L, -1)) + { + if (luaval_to_boolean(L, -1, &boolVal)) + { + dict->setObject(Bool::create(boolVal),stringKey); + } + } + else if(lua_isnumber(L, -1)) + { + dict->setObject(Double::create(tolua_tonumber(L, -1, 0)),stringKey); + } + else + { + CCASSERT(false, "not supported type"); + } + } + + lua_pop(L, 1); /* L: lotable ..... key */ + } + + /* L: lotable ..... */ + } + + return ok; +} + +bool luaval_to_array_of_Point(lua_State* L,int lo,Point **points, int *numPoints) +{ + if (NULL == L) + return false; + + bool ok = true; + + tolua_Error tolua_err; + + if (!tolua_istable(L, lo, 0, &tolua_err) ) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + size_t len = lua_objlen(L, lo); + if (len > 0) + { + Point* array = (Point*)malloc(sizeof(Point) * len); + if (NULL == array) + return false; + for (uint32_t i = 0; i < len; ++i) + { + lua_pushnumber(L,i + 1); + lua_gettable(L,lo); + if (!tolua_istable(L,-1, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + lua_pop(L, 1); + free(array); + return false; + } + ok &= luaval_to_point(L, lua_gettop(L), &array[i]); + if (!ok) + { + lua_pop(L, 1); + free(array); + return false; + } + lua_pop(L, 1); + } + + *numPoints = len; + *points = array; + } + } + return ok; +} + + +bool luavals_variadic_to_array(lua_State* L,int argc, Array** ret) +{ + if (nullptr == L || argc == 0 ) + return false; + + bool ok = true; + + Array* array = Array::create(); + for (int i = 0; i < argc; i++) + { + double num = 0.0; + if (lua_isnumber(L, i + 2)) + { + ok &= luaval_to_number(L, i + 2, &num); + if (!ok) + break; + + array->addObject(Integer::create((int)num)); + } + else if (lua_isstring(L, i + 2)) + { + std::string str = lua_tostring(L, i + 2); + array->addObject(String::create(str)); + } + else if (lua_isuserdata(L, i + 2)) + { + tolua_Error err; + if (!tolua_isusertype(L, i + 2, "cc.Ref", 0, &err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&err); +#endif + ok = false; + break; + } + Ref* obj = static_cast(tolua_tousertype(L, i + 2, nullptr)); + array->addObject(obj); + } + } + + *ret = array; + + return ok; +} + +bool luavals_variadic_to_ccvaluevector(lua_State* L, int argc, cocos2d::ValueVector* ret) +{ + if (nullptr == L || argc == 0 ) + return false; + + for (int i = 0; i < argc; i++) + { + if(lua_istable(L, i + 2)) + { + lua_pushnumber(L, 1); + lua_gettable(L, i + 2); + if (lua_isnil(L, -1) ) + { + lua_pop(L,1); + ValueMap dictVal; + if (luaval_to_ccvaluemap(L, i + 2, &dictVal)) + { + ret->push_back(Value(dictVal)); + } + } + else + { + lua_pop(L,1); + ValueVector arrVal; + if(luaval_to_ccvaluevector(L, i + 2, &arrVal)) + { + ret->push_back(Value(arrVal)); + } + } + } + else if(lua_isstring(L, i + 2)) + { + std::string stringValue = ""; + if(luaval_to_std_string(L, i + 2, &stringValue) ) + { + ret->push_back(Value(stringValue)); + } + } + else if(lua_isboolean(L, i + 2)) + { + bool boolVal = false; + if (luaval_to_boolean(L, i + 2, &boolVal)) + { + ret->push_back(Value(boolVal)); + } + } + else if(lua_isnumber(L, i + 2)) + { + ret->push_back(Value(tolua_tonumber(L, i + 2, 0))); + } + else + { + CCASSERT(false, "not supported type"); + } + } + + return true; + } + +bool luaval_to_ccvalue(lua_State* L, int lo, cocos2d::Value* ret) +{ + if ( nullptr == L || nullptr == ret) + return false; + + bool ok = true; + + tolua_Error tolua_err; + + if (tolua_istable(L, lo, 0, &tolua_err)) + { + lua_pushnumber(L,1); + lua_gettable(L,lo); + + if (lua_isnil(L, -1) ) /** if table[1] = nil,we don't think it is a pure array */ + { + lua_pop(L,1); + ValueMap dictVal; + if (luaval_to_ccvaluemap(L, lo, &dictVal)) + { + *ret = Value(dictVal); + } + } + else + { + lua_pop(L,1); + ValueVector arrVal; + if (luaval_to_ccvaluevector(L, lo, &arrVal)) + { + *ret = Value(arrVal); + } + } + } + else if (tolua_isstring(L, lo, 0, &tolua_err)) + { + std::string stringValue = ""; + if (luaval_to_std_string(L, lo, &stringValue)) + { + *ret = Value(stringValue); + } + } + else if (tolua_isboolean(L, lo, 0, &tolua_err)) + { + bool boolVal = false; + if (luaval_to_boolean(L, lo, &boolVal)) + { + *ret = Value(boolVal); + } + } + else if (tolua_isnumber(L, lo, 0, &tolua_err)) + { + *ret = Value(tolua_tonumber(L, lo, 0)); + } + + return ok; +} +bool luaval_to_ccvaluemap(lua_State* L, int lo, cocos2d::ValueMap* ret) +{ + if ( nullptr == L || nullptr == ret) + return false; + + tolua_Error tolua_err; + bool ok = true; + if (!tolua_istable(L, lo, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + std::string stringKey = ""; + std::string stringValue = ""; + bool boolVal = false; + ValueMap& dict = *ret; + lua_pushnil(L); /* first key L: lotable ..... nil */ + while ( 0 != lua_next(L, lo ) ) /* L: lotable ..... key value */ + { + if (!lua_isstring(L, -2)) + { + lua_pop(L, 1); /* removes 'value'; keep 'key' for next iteration*/ + continue; + } + + if(luaval_to_std_string(L, -2, &stringKey)) + { + + if(lua_istable(L, -1)) + { + lua_pushnumber(L,1); + lua_gettable(L,-2); + + if (lua_isnil(L, -1) ) /** if table[1] = nil,we don't think it is a pure array */ + { + lua_pop(L,1); + ValueMap dictVal; + if (luaval_to_ccvaluemap(L, -1, &dictVal)) + { + dict[stringKey] = Value(dictVal); + } + } + else + { + lua_pop(L,1); + ValueVector arrVal; + if (luaval_to_ccvaluevector(L, -1, &arrVal)) + { + dict[stringKey] = Value(arrVal); + } + } + } + else if(lua_isstring(L, -1)) + { + if(luaval_to_std_string(L, -1, &stringValue)) + { + dict[stringKey] = Value(stringValue); + } + } + else if(lua_isboolean(L, -1)) + { + if (luaval_to_boolean(L, -1, &boolVal)) + { + dict[stringKey] = Value(boolVal); + } + } + else if(lua_isnumber(L, -1)) + { + dict[stringKey] = Value(tolua_tonumber(L, -1, 0)); + } + else + { + CCASSERT(false, "not supported type"); + } + } + + lua_pop(L, 1); /* L: lotable ..... key */ + } + } + + return ok; +} +bool luaval_to_ccvaluemapintkey(lua_State* L, int lo, cocos2d::ValueMapIntKey* ret) +{ + if (nullptr == L || nullptr == ret) + return false; + + tolua_Error tolua_err; + bool ok = true; + if (!tolua_istable(L, lo, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + std::string stringKey = ""; + std::string stringValue = ""; + int intKey = 0; + bool boolVal = false; + ValueMapIntKey& dict = *ret; + lua_pushnil(L); /* first key L: lotable ..... nil */ + while ( 0 != lua_next(L, lo ) ) /* L: lotable ..... key value */ + { + if (!lua_isstring(L, -2)) + { + lua_pop(L, 1); /* removes 'value'; keep 'key' for next iteration*/ + continue; + } + + if(luaval_to_std_string(L, -2, &stringKey)) + { + intKey = atoi(stringKey.c_str()); + if(lua_istable(L, -1)) + { + lua_pushnumber(L,1); + lua_gettable(L,-2); + + if (lua_isnil(L, -1) ) /** if table[1] = nil,we don't think it is a pure array */ + { + lua_pop(L,1); + ValueMap dictVal; + if (luaval_to_ccvaluemap(L, -1, &dictVal)) + { + dict[intKey] = Value(dictVal); + } + } + else + { + lua_pop(L,1); + ValueVector arrVal; + if (luaval_to_ccvaluevector(L, -1, &arrVal)) + { + dict[intKey] = Value(arrVal); + } + } + } + else if(lua_isstring(L, -1)) + { + if(luaval_to_std_string(L, -1, &stringValue)) + { + dict[intKey] = Value(stringValue); + } + } + else if(lua_isboolean(L, -1)) + { + if (luaval_to_boolean(L, -1, &boolVal)) + { + dict[intKey] = Value(boolVal); + } + } + else if(lua_isnumber(L, -1)) + { + dict[intKey] = Value(tolua_tonumber(L, -1, 0)); + } + else + { + CCASSERT(false, "not supported type"); + } + } + + lua_pop(L, 1); /* L: lotable ..... key */ + } + } + + return ok; +} +bool luaval_to_ccvaluevector(lua_State* L, int lo, cocos2d::ValueVector* ret) +{ + if (nullptr == L || nullptr == ret) + return false; + + tolua_Error tolua_err; + bool ok = true; + if (!tolua_istable(L, lo, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + size_t len = lua_objlen(L, lo); + for (int i = 0; i < len; i++) + { + lua_pushnumber(L,i + 1); + lua_gettable(L,lo); + if (lua_isnil(L,-1)) + { + lua_pop(L, 1); + continue; + } + + if(lua_istable(L, -1)) + { + lua_pushnumber(L,1); + lua_gettable(L,-2); + if (lua_isnil(L, -1) ) + { + lua_pop(L,1); + ValueMap dictVal; + if (luaval_to_ccvaluemap(L, -1, &dictVal)) + { + ret->push_back(Value(dictVal)); + } + } + else + { + lua_pop(L,1); + ValueVector arrVal; + if(luaval_to_ccvaluevector(L, -1, &arrVal)) + { + ret->push_back(Value(arrVal)); + } + } + } + else if(lua_isstring(L, -1)) + { + std::string stringValue = ""; + if(luaval_to_std_string(L, -1, &stringValue) ) + { + ret->push_back(Value(stringValue)); + } + } + else if(lua_isboolean(L, -1)) + { + bool boolVal = false; + if (luaval_to_boolean(L, -1, &boolVal)) + { + ret->push_back(Value(boolVal)); + } + } + else if(lua_isnumber(L, -1)) + { + ret->push_back(Value(tolua_tonumber(L, -1, 0))); + } + else + { + CCASSERT(false, "not supported type"); + } + lua_pop(L, 1); + } + } + + return ok; +} + +bool luaval_to_std_vector_string(lua_State* L, int lo, std::vector* ret) +{ + if (nullptr == L || nullptr == ret || lua_gettop(L) < lo) + return false; + + tolua_Error tolua_err; + bool ok = true; + if (!tolua_istable(L, lo, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + size_t len = lua_objlen(L, lo); + std::string value = ""; + for (int i = 0; i < len; i++) + { + lua_pushnumber(L, i + 1); + lua_gettable(L,lo); + if(lua_isstring(L, -1)) + { + ok = luaval_to_std_string(L, -1, &value); + if(ok) + ret->push_back(value); + } + else + { + CCASSERT(false, "string type is needed"); + } + + lua_pop(L, 1); + } + } + + return ok; +} + +bool luaval_to_std_vector_int(lua_State* L, int lo, std::vector* ret) +{ + if (nullptr == L || nullptr == ret || lua_gettop(L) < lo) + return false; + + tolua_Error tolua_err; + bool ok = true; + if (!tolua_istable(L, lo, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + size_t len = lua_objlen(L, lo); + for (int i = 0; i < len; i++) + { + lua_pushnumber(L, i + 1); + lua_gettable(L,lo); + if(lua_isnumber(L, -1)) + { + ret->push_back((int)tolua_tonumber(L, -1, 0)); + } + else + { + CCASSERT(false, "int type is needed"); + } + + lua_pop(L, 1); + } + } + + return ok; +} + +void points_to_luaval(lua_State* L,const Point* pt, int count) +{ + if (NULL == L) + return; + lua_newtable(L); + for (int i = 1; i <= count; ++i) + { + lua_pushnumber(L, i); + point_to_luaval(L, pt[i-1]); + lua_rawset(L, -3); + } +} + +void point_to_luaval(lua_State* L,const Point& pt) +{ + if (NULL == L) + return; + lua_newtable(L); /* L: table */ + lua_pushstring(L, "x"); /* L: table key */ + lua_pushnumber(L, (lua_Number) pt.x); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "y"); /* L: table key */ + lua_pushnumber(L, (lua_Number) pt.y); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void physics_material_to_luaval(lua_State* L,const PhysicsMaterial& pm) +{ + if (NULL == L) + return; + lua_newtable(L); /* L: table */ + lua_pushstring(L, "density"); /* L: table key */ + lua_pushnumber(L, (lua_Number) pm.density); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "restitution"); /* L: table key */ + lua_pushnumber(L, (lua_Number) pm.restitution); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "friction"); /* L: table key */ + lua_pushnumber(L, (lua_Number) pm.friction); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void physics_raycastinfo_to_luaval(lua_State* L, const PhysicsRayCastInfo& info) +{ + if (NULL == L) + return; + + lua_newtable(L); /* L: table */ + + lua_pushstring(L, "shape"); /* L: table key */ + PhysicsShape* shape = info.shape; + if (shape == nullptr) + { + lua_pushnil(L); + }else + { + int ID = (int)(shape->_ID); + int* luaID = &(shape->_luaID); + toluafix_pushusertype_ccobject(L, ID, luaID, (void*)shape,"cc.PhysicsShape"); + } + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "start"); /* L: table key */ + point_to_luaval(L, info.start); + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "ended"); /* L: table key */ + point_to_luaval(L, info.end); + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "contact"); /* L: table key */ + point_to_luaval(L, info.contact); + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "normal"); /* L: table key */ + point_to_luaval(L, info.normal); + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "fraction"); /* L: table key */ + lua_pushnumber(L, (lua_Number) info.fraction); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void physics_contactdata_to_luaval(lua_State* L, const PhysicsContactData* data) +{ + if (nullptr == L || nullptr == data) + return; + + lua_newtable(L); /* L: table */ + + lua_pushstring(L, "points"); + points_to_luaval(L, data->points, data->count); + lua_rawset(L, -3); + + lua_pushstring(L, "normal"); + point_to_luaval(L, data->normal); + lua_rawset(L, -3); + + lua_pushstring(L, "POINT_MAX"); + lua_pushnumber(L, data->POINT_MAX); + lua_rawset(L, -3); +} + +void size_to_luaval(lua_State* L,const Size& sz) +{ + if (NULL == L) + return; + lua_newtable(L); /* L: table */ + lua_pushstring(L, "width"); /* L: table key */ + lua_pushnumber(L, (lua_Number) sz.width); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "height"); /* L: table key */ + lua_pushnumber(L, (lua_Number) sz.height); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void rect_to_luaval(lua_State* L,const Rect& rt) +{ + if (NULL == L) + return; + lua_newtable(L); /* L: table */ + lua_pushstring(L, "x"); /* L: table key */ + lua_pushnumber(L, (lua_Number) rt.origin.x); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "y"); /* L: table key */ + lua_pushnumber(L, (lua_Number) rt.origin.y); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "width"); /* L: table key */ + lua_pushnumber(L, (lua_Number) rt.size.width); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "height"); /* L: table key */ + lua_pushnumber(L, (lua_Number) rt.size.height); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void color4b_to_luaval(lua_State* L,const Color4B& cc) +{ + if (NULL == L) + return; + lua_newtable(L); /* L: table */ + lua_pushstring(L, "r"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.r); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "g"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.g); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "b"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.b); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "a"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.a); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void color4f_to_luaval(lua_State* L,const Color4F& cc) +{ + if (NULL == L) + return; + lua_newtable(L); /* L: table */ + lua_pushstring(L, "r"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.r); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "g"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.g); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "b"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.b); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "a"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.a); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void color3b_to_luaval(lua_State* L,const Color3B& cc) +{ + if (NULL == L) + return; + lua_newtable(L); /* L: table */ + lua_pushstring(L, "r"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.r); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "g"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.g); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "b"); /* L: table key */ + lua_pushnumber(L, (lua_Number) cc.b); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void affinetransform_to_luaval(lua_State* L,const AffineTransform& inValue) +{ + if (NULL == L) + return; + + lua_newtable(L); /* L: table */ + lua_pushstring(L, "a"); /* L: table key */ + lua_pushnumber(L, (lua_Number) inValue.a); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "b"); /* L: table key */ + lua_pushnumber(L, (lua_Number) inValue.b); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "c"); /* L: table key */ + lua_pushnumber(L, (lua_Number) inValue.c); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "d"); /* L: table key */ + lua_pushnumber(L, (lua_Number) inValue.d); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "tx"); /* L: table key */ + lua_pushnumber(L, (lua_Number) inValue.d); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "ty"); /* L: table key */ + lua_pushnumber(L, (lua_Number) inValue.d); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void fontdefinition_to_luaval(lua_State* L,const FontDefinition& inValue) +{ + if (NULL == L) + return; + + lua_newtable(L); /* L: table */ + lua_pushstring(L, "fontName"); /* L: table key */ + tolua_pushcppstring(L, inValue._fontName); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "fontSize"); /* L: table key */ + lua_pushnumber(L,(lua_Number)inValue._fontSize); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "fontAlignmentH"); /* L: table key */ + lua_pushnumber(L, (lua_Number) inValue._alignment); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "fontAlignmentV"); /* L: table key */ + lua_pushnumber(L, (lua_Number) inValue._vertAlignment); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "fontFillColor"); /* L: table key */ + color3b_to_luaval(L, inValue._fontFillColor); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + lua_pushstring(L, "fontDimensions"); /* L: table key */ + size_to_luaval(L, inValue._dimensions); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + + //Shadow + lua_pushstring(L, "shadowEnabled"); /* L: table key */ + lua_pushboolean(L, inValue._shadow._shadowEnabled); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "shadowOffset"); /* L: table key */ + size_to_luaval(L, inValue._shadow._shadowOffset); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "shadowBlur"); /* L: table key */ + lua_pushnumber(L, (lua_Number)inValue._shadow._shadowBlur); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "shadowOpacity"); /* L: table key */ + lua_pushnumber(L, (lua_Number)inValue._shadow._shadowOpacity); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + + //Stroke + lua_pushstring(L, "shadowEnabled"); /* L: table key */ + lua_pushboolean(L, inValue._stroke._strokeEnabled); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "strokeColor"); /* L: table key */ + color3b_to_luaval(L, inValue._stroke._strokeColor); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ + + lua_pushstring(L, "strokeSize"); /* L: table key */ + lua_pushnumber(L, (lua_Number)inValue._stroke._strokeSize); /* L: table key value*/ + lua_rawset(L, -3); /* table[key] = value, L: table */ +} + +void array_to_luaval(lua_State* L,Array* inValue) +{ + lua_newtable(L); + + if (nullptr == L || nullptr == inValue) + return; + + Ref* obj = nullptr; + + std::string className = ""; + String* strVal = nullptr; + Dictionary* dictVal = nullptr; + Array* arrVal = nullptr; + Double* doubleVal = nullptr; + Bool* boolVal = nullptr; + Float* floatVal = nullptr; + Integer* intVal = nullptr; + int indexTable = 1; + + CCARRAY_FOREACH(inValue, obj) + { + if (nullptr == obj) + continue; + + std::string typeName = typeid(*obj).name(); + auto iter = g_luaType.find(typeName); + if (g_luaType.end() != iter) + { + className = iter->second; + if (nullptr != dynamic_cast(obj)) + { + lua_pushnumber(L, (lua_Number)indexTable); + int ID = (obj) ? (int)obj->_ID : -1; + int* luaID = (obj) ? &obj->_luaID : NULL; + toluafix_pushusertype_ccobject(L, ID, luaID, (void*)obj,className.c_str()); + lua_rawset(L, -3); + obj->retain(); + ++indexTable; + } + } + else if((strVal = dynamic_cast(obj))) + { + lua_pushnumber(L, (lua_Number)indexTable); + lua_pushstring(L, strVal->getCString()); + lua_rawset(L, -3); + ++indexTable; + } + else if ((dictVal = dynamic_cast(obj))) + { + dictionary_to_luaval(L, dictVal); + } + else if ((arrVal = dynamic_cast(obj))) + { + array_to_luaval(L, arrVal); + } + else if ((doubleVal = dynamic_cast(obj))) + { + lua_pushnumber(L, (lua_Number)indexTable); + lua_pushnumber(L, (lua_Number)doubleVal->getValue()); + lua_rawset(L, -3); + ++indexTable; + } + else if ((floatVal = dynamic_cast(obj))) + { + lua_pushnumber(L, (lua_Number)indexTable); + lua_pushnumber(L, (lua_Number)floatVal->getValue()); + lua_rawset(L, -3); + ++indexTable; + } + else if ((intVal = dynamic_cast(obj))) + { + lua_pushnumber(L, (lua_Number)indexTable); + lua_pushinteger(L, (lua_Integer)intVal->getValue()); + lua_rawset(L, -3); + ++indexTable; + } + else if ((boolVal = dynamic_cast(obj))) + { + lua_pushnumber(L, (lua_Number)indexTable); + lua_pushboolean(L, boolVal->getValue()); + lua_rawset(L, -3); + ++indexTable; + } + else + { + CCASSERT(false, "the type isn't suppored."); + } + } +} + +void dictionary_to_luaval(lua_State* L, Dictionary* dict) +{ + lua_newtable(L); + + if (nullptr == L || nullptr == dict) + return; + + DictElement* element = nullptr; + + std::string className = ""; + String* strVal = nullptr; + Dictionary* dictVal = nullptr; + Array* arrVal = nullptr; + Double* doubleVal = nullptr; + Bool* boolVal = nullptr; + Float* floatVal = nullptr; + Integer* intVal = nullptr; + + CCDICT_FOREACH(dict, element) + { + if (NULL == element) + continue; + + std::string typeName = typeid(element->getObject()).name(); + + auto iter = g_luaType.find(typeName); + if (g_luaType.end() != iter) + { + className = iter->second; + if ( nullptr != dynamic_cast(element->getObject())) + { + lua_pushstring(L, element->getStrKey()); + int ID = (element->getObject()) ? (int)element->getObject()->_ID : -1; + int* luaID = (element->getObject()) ? &(element->getObject()->_luaID) : NULL; + toluafix_pushusertype_ccobject(L, ID, luaID, (void*)element->getObject(),className.c_str()); + lua_rawset(L, -3); + element->getObject()->retain(); + } + } + else if((strVal = dynamic_cast(element->getObject()))) + { + lua_pushstring(L, element->getStrKey()); + lua_pushstring(L, strVal->getCString()); + lua_rawset(L, -3); + } + else if ((dictVal = dynamic_cast(element->getObject()))) + { + dictionary_to_luaval(L, dictVal); + } + else if ((arrVal = dynamic_cast(element->getObject()))) + { + array_to_luaval(L, arrVal); + } + else if ((doubleVal = dynamic_cast(element->getObject()))) + { + lua_pushstring(L, element->getStrKey()); + lua_pushnumber(L, (lua_Number)doubleVal->getValue()); + lua_rawset(L, -3); + } + else if ((floatVal = dynamic_cast(element->getObject()))) + { + lua_pushstring(L, element->getStrKey()); + lua_pushnumber(L, (lua_Number)floatVal->getValue()); + lua_rawset(L, -3); + } + else if ((intVal = dynamic_cast(element->getObject()))) + { + lua_pushstring(L, element->getStrKey()); + lua_pushinteger(L, (lua_Integer)intVal->getValue()); + lua_rawset(L, -3); + } + else if ((boolVal = dynamic_cast(element->getObject()))) + { + lua_pushstring(L, element->getStrKey()); + lua_pushboolean(L, boolVal->getValue()); + lua_rawset(L, -3); + } + else + { + CCASSERT(false, "the type isn't suppored."); + } + } +} + +void ccvalue_to_luaval(lua_State* L,const cocos2d::Value& inValue) +{ + const Value& obj = inValue; + switch (obj.getType()) + { + case Value::Type::BOOLEAN: + lua_pushboolean(L, obj.asBool()); + break; + case Value::Type::FLOAT: + case Value::Type::DOUBLE: + lua_pushnumber(L, obj.asDouble()); + break; + case Value::Type::INTEGER: + lua_pushinteger(L, obj.asInt()); + break; + case Value::Type::STRING: + lua_pushstring(L, obj.asString().c_str()); + break; + case Value::Type::VECTOR: + ccvaluevector_to_luaval(L, obj.asValueVector()); + break; + case Value::Type::MAP: + ccvaluemap_to_luaval(L, obj.asValueMap()); + break; + case Value::Type::INT_KEY_MAP: + ccvaluemapintkey_to_luaval(L, obj.asIntKeyMap()); + break; + default: + break; + } +} +void ccvaluemap_to_luaval(lua_State* L,const cocos2d::ValueMap& inValue) +{ + lua_newtable(L); + + if (nullptr == L) + return; + + for (auto iter = inValue.begin(); iter != inValue.end(); ++iter) + { + std::string key = iter->first; + const Value& obj = iter->second; + switch (obj.getType()) + { + case Value::Type::BOOLEAN: + { + lua_pushstring(L, key.c_str()); + lua_pushboolean(L, obj.asBool()); + lua_rawset(L, -3); + } + break; + case Value::Type::FLOAT: + case Value::Type::DOUBLE: + { + lua_pushstring(L, key.c_str()); + lua_pushnumber(L, obj.asDouble()); + lua_rawset(L, -3); + } + break; + case Value::Type::INTEGER: + { + lua_pushstring(L, key.c_str()); + lua_pushinteger(L, obj.asInt()); + lua_rawset(L, -3); + } + break; + case Value::Type::STRING: + { + lua_pushstring(L, key.c_str()); + lua_pushstring(L, obj.asString().c_str()); + lua_rawset(L, -3); + } + break; + case Value::Type::VECTOR: + { + lua_pushstring(L, key.c_str()); + ccvaluevector_to_luaval(L, obj.asValueVector()); + lua_rawset(L, -3); + } + break; + case Value::Type::MAP: + { + lua_pushstring(L, key.c_str()); + ccvaluemap_to_luaval(L, obj.asValueMap()); + lua_rawset(L, -3); + } + break; + case Value::Type::INT_KEY_MAP: + { + lua_pushstring(L, key.c_str()); + ccvaluemapintkey_to_luaval(L, obj.asIntKeyMap()); + lua_rawset(L, -3); + } + break; + default: + break; + } + } +} +void ccvaluemapintkey_to_luaval(lua_State* L, const cocos2d::ValueMapIntKey& inValue) +{ + lua_newtable(L); + + if (nullptr == L) + return; + + for (auto iter = inValue.begin(); iter != inValue.end(); ++iter) + { + std::stringstream keyss; + keyss << iter->first; + std::string key = keyss.str(); + + const Value& obj = iter->second; + + switch (obj.getType()) + { + case Value::Type::BOOLEAN: + { + lua_pushstring(L, key.c_str()); + lua_pushboolean(L, obj.asBool()); + lua_rawset(L, -3); + } + break; + case Value::Type::FLOAT: + case Value::Type::DOUBLE: + { + lua_pushstring(L, key.c_str()); + lua_pushnumber(L, obj.asDouble()); + lua_rawset(L, -3); + } + break; + case Value::Type::INTEGER: + { + lua_pushstring(L, key.c_str()); + lua_pushinteger(L, obj.asInt()); + lua_rawset(L, -3); + } + break; + case Value::Type::STRING: + { + lua_pushstring(L, key.c_str()); + lua_pushstring(L, obj.asString().c_str()); + lua_rawset(L, -3); + } + break; + case Value::Type::VECTOR: + { + lua_pushstring(L, key.c_str()); + ccvaluevector_to_luaval(L, obj.asValueVector()); + lua_rawset(L, -3); + } + break; + case Value::Type::MAP: + { + lua_pushstring(L, key.c_str()); + ccvaluemap_to_luaval(L, obj.asValueMap()); + lua_rawset(L, -3); + } + break; + case Value::Type::INT_KEY_MAP: + { + lua_pushstring(L, key.c_str()); + ccvaluemapintkey_to_luaval(L, obj.asIntKeyMap()); + lua_rawset(L, -3); + } + break; + default: + break; + } + } +} +void ccvaluevector_to_luaval(lua_State* L, const cocos2d::ValueVector& inValue) +{ + lua_newtable(L); + + if (nullptr == L) + return; + + int index = 1; + for (const auto& obj : inValue) + { + switch (obj.getType()) + { + case Value::Type::BOOLEAN: + { + lua_pushnumber(L, (lua_Number)index); + lua_pushboolean(L, obj.asBool()); + lua_rawset(L, -3); + ++index; + } + break; + case Value::Type::FLOAT: + case Value::Type::DOUBLE: + { + lua_pushnumber(L, (lua_Number)index); + lua_pushnumber(L, obj.asDouble()); + lua_rawset(L, -3); + ++index; + } + break; + case Value::Type::INTEGER: + { + lua_pushnumber(L, (lua_Number)index); + lua_pushnumber(L, obj.asInt()); + lua_rawset(L, -3); + ++index; + } + break; + case Value::Type::STRING: + { + lua_pushnumber(L, (lua_Number)index); + lua_pushstring(L, obj.asString().c_str()); + lua_rawset(L, -3); + ++index; + } + break; + case Value::Type::VECTOR: + { + lua_pushnumber(L, (lua_Number)index); + ccvaluevector_to_luaval(L, obj.asValueVector()); + lua_rawset(L, -3); + ++index; + } + break; + case Value::Type::MAP: + { + lua_pushnumber(L, (lua_Number)index); + ccvaluemap_to_luaval(L, obj.asValueMap()); + lua_rawset(L, -3); + ++index; + } + break; + case Value::Type::INT_KEY_MAP: + { + lua_pushnumber(L, (lua_Number)index); + ccvaluemapintkey_to_luaval(L, obj.asIntKeyMap()); + lua_rawset(L, -3); + ++index; + } + break; + default: + break; + } + } +} diff --git a/cocos/scripting/lua-bindings/manual/LuaBasicConversions.h b/cocos/scripting/lua-bindings/manual/LuaBasicConversions.h new file mode 100644 index 0000000000..377ef863b8 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/LuaBasicConversions.h @@ -0,0 +1,344 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef __COCOS2DX_SCRIPTING_LUA_COCOS2DXSUPPORT_LUABAISCCONVERSIONS_H__ +#define __COCOS2DX_SCRIPTING_LUA_COCOS2DXSUPPORT_LUABAISCCONVERSIONS_H__ + +extern "C" { +#include "lua.h" +#include "tolua++.h" +} +#include "tolua_fix.h" +#include "cocos2d.h" + +using namespace cocos2d; + +extern std::unordered_map g_luaType; +extern std::unordered_map g_typeCast; + +#if COCOS2D_DEBUG >=1 +void luaval_to_native_err(lua_State* L,const char* msg,tolua_Error* err); +#endif + +#define LUA_PRECONDITION( condition, ...) if( ! (condition) ) { \ +cocos2d::log("lua: ERROR: File %s: Line: %d, Function: %s", __FILE__, __LINE__, __FUNCTION__ ); \ +cocos2d::log(__VA_ARGS__); \ +} \ + +extern bool luaval_is_usertype(lua_State* L,int lo,const char* type, int def); +// to native +extern bool luaval_to_ushort(lua_State* L, int lo, unsigned short* outValue); +extern bool luaval_to_int32(lua_State* L,int lo,int* outValue); +extern bool luaval_to_uint32(lua_State* L, int lo, unsigned int* outValue); +extern bool luaval_to_uint16(lua_State* L,int lo,uint16_t* outValue); +extern bool luaval_to_boolean(lua_State* L,int lo,bool* outValue); +extern bool luaval_to_number(lua_State* L,int lo,double* outValue); +extern bool luaval_to_long_long(lua_State* L,int lo,long long* outValue); +extern bool luaval_to_std_string(lua_State* L, int lo, std::string* outValue); +extern bool luaval_to_long(lua_State* L,int lo, long* outValue); +extern bool luaval_to_ssize(lua_State* L,int lo, ssize_t* outValue); + +extern bool luaval_to_point(lua_State* L,int lo,Point* outValue); +extern bool luaval_to_size(lua_State* L,int lo,Size* outValue); +extern bool luaval_to_rect(lua_State* L,int lo,Rect* outValue); +extern bool luaval_to_color3b(lua_State* L,int lo,Color3B* outValue); +extern bool luaval_to_color4b(lua_State* L,int lo,Color4B* outValue); +extern bool luaval_to_color4f(lua_State* L,int lo,Color4F* outValue); +extern bool luaval_to_physics_material(lua_State* L,int lo, cocos2d::PhysicsMaterial* outValue); +extern bool luaval_to_affinetransform(lua_State* L,int lo, AffineTransform* outValue); +extern bool luaval_to_fontdefinition(lua_State* L, int lo, FontDefinition* outValue ); +extern bool luaval_to_array(lua_State* L,int lo, Array** outValue); +extern bool luaval_to_dictionary(lua_State* L,int lo, Dictionary** outValue); +extern bool luaval_to_array_of_Point(lua_State* L,int lo,Point **points, int *numPoints); +extern bool luavals_variadic_to_array(lua_State* L,int argc, Array** ret); +extern bool luavals_variadic_to_ccvaluevector(lua_State* L, int argc, cocos2d::ValueVector* ret); + +template +bool luavals_variadic_to_ccvector( lua_State* L, int argc, cocos2d::Vector* ret) +{ + if (nullptr == L || argc == 0 ) + return false; + + bool ok = true; + + for (int i = 0; i < argc; i++) + { + if (lua_isuserdata(L, i + 2)) + { + tolua_Error err; + + if (!tolua_isusertype(L, i + 2, "cc.Ref", 0, &err)) + { + ok = false; + break; + } + T obj = static_cast(tolua_tousertype(L, i + 2, nullptr)); + ret->pushBack(obj); + } + } + + return ok; +} + +template +bool luaval_to_ccvector(lua_State* L, int lo , cocos2d::Vector* ret) +{ + if (nullptr == L || nullptr == ret) + return false; + + bool ok = true; + + tolua_Error tolua_err; + if (!tolua_istable(L, lo, 0, &tolua_err) ) + ok = false; + + if (ok) + { + size_t len = lua_objlen(L, lo); + for (int i = 0; i < len; i++) + { + lua_pushnumber(L, i + 1); + lua_gettable(L, lo); + + if (lua_isnil(L, -1) || !lua_isuserdata(L, -1)) + { + lua_pop(L, 1); + continue; + } + + + T cobj = static_cast(tolua_tousertype(L, -1, NULL) ); + if (NULL != cobj) + ret->pushBack(cobj); + + lua_pop(L, 1); + } + } + + return ok; +} + +bool luaval_to_std_vector_string(lua_State* L, int lo, std::vector* ret); +bool luaval_to_std_vector_int(lua_State* L, int lo, std::vector* ret); + +template +bool luaval_to_ccmap_string_key(lua_State* L, int lo, cocos2d::Map* ret) +{ + if(nullptr == L || nullptr == ret || lua_gettop(L) < lo) + return false; + + tolua_Error tolua_err; + bool ok = true; + if (!tolua_istable(L, lo, 0, &tolua_err)) + { +#if COCOS2D_DEBUG >=1 + luaval_to_native_err(L,"#ferror:",&tolua_err); +#endif + ok = false; + } + + if (ok) + { + std::string stringKey = ""; + lua_pushnil(L); /* first key L: lotable ..... nil */ + while ( 0 != lua_next(L, lo ) ) /* L: lotable ..... key value */ + { + if (!lua_isstring(L, -2)) + { + lua_pop(L, 1); /* removes 'value'; keep 'key' for next iteration*/ + continue; + } + + if (lua_isnil(L, -1) || !lua_isuserdata(L, -1)) + { + lua_pop(L, 1); + continue; + } + + luaval_to_std_string(L, -2, &stringKey); + T obj = static_cast(tolua_tousertype(L, -1, NULL) ); + if (nullptr != obj) + ret->insert(stringKey, obj); + + lua_pop(L, 1); /* L: lotable ..... key */ + } + } + + return ok; +} + + +extern bool luaval_to_ccvalue(lua_State* L, int lo, cocos2d::Value* ret); +extern bool luaval_to_ccvaluemap(lua_State* L, int lo, cocos2d::ValueMap* ret); +extern bool luaval_to_ccvaluemapintkey(lua_State* L, int lo, cocos2d::ValueMapIntKey* ret); +extern bool luaval_to_ccvaluevector(lua_State* L, int lo, cocos2d::ValueVector* ret); + +template +bool luaval_to_object(lua_State* L, int lo, const char* type, T** ret) +{ + if(nullptr == L || lua_gettop(L) < lo) + return false; + + if (!luaval_is_usertype(L, lo, type, 0)) + return false; + + *ret = static_cast(tolua_tousertype(L, lo, 0)); + + if (nullptr == ret) + LUA_PRECONDITION(ret, "Invalid Native Object"); + + return true; +} + + +// from native +extern void point_to_luaval(lua_State* L,const Point& pt); +extern void points_to_luaval(lua_State* L,const Point* pt, int count); +extern void size_to_luaval(lua_State* L,const Size& sz); +extern void rect_to_luaval(lua_State* L,const Rect& rt); +extern void color3b_to_luaval(lua_State* L,const Color3B& cc); +extern void color4b_to_luaval(lua_State* L,const Color4B& cc); +extern void color4f_to_luaval(lua_State* L,const Color4F& cc); +extern void physics_material_to_luaval(lua_State* L,const PhysicsMaterial& pm); +extern void physics_raycastinfo_to_luaval(lua_State* L, const PhysicsRayCastInfo& info); +extern void physics_contactdata_to_luaval(lua_State* L, const PhysicsContactData* data); +extern void affinetransform_to_luaval(lua_State* L,const AffineTransform& inValue); +extern void fontdefinition_to_luaval(lua_State* L,const FontDefinition& inValue); +extern void array_to_luaval(lua_State* L,Array* inValue); +extern void dictionary_to_luaval(lua_State* L, Dictionary* dict); + +template +void ccvector_to_luaval(lua_State* L,const cocos2d::Vector& inValue) +{ + lua_newtable(L); + + if (nullptr == L) + return; + + int indexTable = 1; + for (const auto& obj : inValue) + { + if (nullptr == obj) + continue; + + + if (nullptr != dynamic_cast(obj)) + { + std::string typeName = typeid(*obj).name(); + auto iter = g_luaType.find(typeName); + if (g_luaType.end() != iter) + { + lua_pushnumber(L, (lua_Number)indexTable); + int ID = (obj) ? (int)obj->_ID : -1; + int* luaID = (obj) ? &obj->_luaID : NULL; + toluafix_pushusertype_ccobject(L, ID, luaID, (void*)obj,iter->second.c_str()); + lua_rawset(L, -3); + obj->retain(); + ++indexTable; + } + } + } +} + +template +void ccmap_string_key_to_luaval(lua_State* L, const cocos2d::Map& v) +{ + lua_newtable(L); + + if(nullptr == L) + return; + + for (auto iter = v.begin(); iter != v.end(); ++iter) + { + std::string key = iter->first; + T obj = iter->second; + if (nullptr != dynamic_cast(obj)) + { + std::string name = typeid(*obj).name(); + auto typeIter = g_luaType.find(name); + if (g_luaType.end() != typeIter) + { + lua_pushstring(L, name.c_str()); + int ID = (obj) ? (int)obj->_ID : -1; + int* luaID = (obj) ? &obj->_luaID : NULL; + toluafix_pushusertype_ccobject(L, ID, luaID, (void*)obj,typeIter->second.c_str()); + lua_rawset(L, -3); + } + } + } +} + +void ccvalue_to_luaval(lua_State* L,const cocos2d::Value& inValue); +void ccvaluemap_to_luaval(lua_State* L,const cocos2d::ValueMap& inValue); +void ccvaluemapintkey_to_luaval(lua_State* L, const cocos2d::ValueMapIntKey& inValue); +void ccvaluevector_to_luaval(lua_State* L, const cocos2d::ValueVector& inValue); + +/** + Because all override functions wouldn't be bound,so we must use `typeid` to get the real class name + */ +template +const char* getLuaTypeName(T* ret,const char* type) +{ + if (nullptr != ret) + { + std::string hashName = typeid(*ret).name(); + auto iter = g_luaType.find(hashName); + if(g_luaType.end() != iter) + { + return iter->second.c_str(); + } + else + { + return type; + } + } + + return nullptr; +} + +template +void object_to_luaval(lua_State* L,const char* type, T* ret) +{ + if(nullptr != ret) + { + + cocos2d::Ref* dynObject = dynamic_cast(ret); + + if (nullptr != dynObject) + { + int ID = (int)(dynObject->_ID) ; + int* luaID = &(dynObject->_luaID); + toluafix_pushusertype_ccobject(L,ID, luaID, (void*)ret,type); + } + else + { + tolua_pushusertype(L,(void*)ret,getLuaTypeName(ret, type)); + } + } + else + { + lua_pushnil(L); + } +} + +#endif //__COCOS2DX_SCRIPTING_LUA_COCOS2DXSUPPORT_LUABAISCCONVERSIONS_H__ diff --git a/cocos/scripting/lua-bindings/manual/LuaOpengl.cpp.REMOVED.git-id b/cocos/scripting/lua-bindings/manual/LuaOpengl.cpp.REMOVED.git-id new file mode 100644 index 0000000000..934d2eaa40 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/LuaOpengl.cpp.REMOVED.git-id @@ -0,0 +1 @@ +0bdb68570761c39f7a34fe78107f59f37790e849 \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/manual/LuaOpengl.h b/cocos/scripting/lua-bindings/manual/LuaOpengl.h new file mode 100644 index 0000000000..66e08ee309 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/LuaOpengl.h @@ -0,0 +1,45 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef __LUA_OPENGL_H__ +#define __LUA_OPENGL_H__ + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +#include "CCNode.h" + +class GLNode:public cocos2d::Node +{ + virtual void draw(cocos2d::Renderer *renderer, const kmMat4& transform, bool transformUpdated) override; +}; + +TOLUA_API int tolua_opengl_open(lua_State* tolua_S); +TOLUA_API int register_glnode_manual(lua_State* tolua_S); + +#endif //__LUA_OPENGL_H__ diff --git a/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.cpp b/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.cpp new file mode 100644 index 0000000000..a690f8dd42 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.cpp @@ -0,0 +1,383 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#include "LuaScriptHandlerMgr.h" +#include +#include +#include "tolua_fix.h" +#include "cocos2d.h" +#include "extensions/cocos-ext.h" +#include "CCLuaStack.h" +#include "CCLuaValue.h" +#include "CCLuaEngine.h" +#include "Lua_web_socket.h" +#include "LuaOpengl.h" + +using namespace cocos2d; +using namespace cocos2d::extension; + +NS_CC_BEGIN + +ScheduleHandlerDelegate* ScheduleHandlerDelegate::create() +{ + ScheduleHandlerDelegate *ret = new ScheduleHandlerDelegate(); + if (NULL != ret ) + { + ret->autorelease(); + return ret; + } + else + { + CC_SAFE_DELETE(ret); + return NULL; + } +} + +void ScheduleHandlerDelegate::scheduleFunc(float elapse) +{ + +} + +void ScheduleHandlerDelegate::update(float elapse) +{ + +} + + +LuaCallFunc * LuaCallFunc::create(const std::function& func) +{ + auto ret = new LuaCallFunc(); + + if (ret && ret->initWithFunction(func) ) { + ret->autorelease(); + return ret; + } + + CC_SAFE_DELETE(ret); + return NULL; +} + +void LuaCallFunc::execute() +{ + if (_functionLua) + { + _functionLua((void*)this,_target); + } + else + { + CallFuncN::execute(); + } +} + +bool LuaCallFunc::initWithFunction(const std::function &func) +{ + _functionLua = func; + return true; +} + +LuaCallFunc* LuaCallFunc::clone() const +{ + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::CALLFUNC); + + if (0 == handler) + return NULL; + + auto ret = new LuaCallFunc(); + + if( _functionLua ) + { + ret->initWithFunction(_functionLua); + } + + ret->autorelease(); + + int newscriptHandler = cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->reallocateScriptHandler(handler); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)ret, newscriptHandler, ScriptHandlerMgr::HandlerType::CALLFUNC); + + return ret; +} + +ScriptHandlerMgr* ScriptHandlerMgr::_scriptHandlerMgr = NULL; + +ScriptHandlerMgr::ScriptHandlerMgr() +{ + +} +ScriptHandlerMgr::~ScriptHandlerMgr() +{ + CC_SAFE_DELETE(_scriptHandlerMgr); +} +ScriptHandlerMgr* ScriptHandlerMgr::getInstance() +{ + if (NULL == _scriptHandlerMgr) + { + _scriptHandlerMgr = new ScriptHandlerMgr(); + _scriptHandlerMgr->init(); + } + return _scriptHandlerMgr; +} + +void ScriptHandlerMgr::init() +{ + _mapObjectHandlers.clear(); +} + +void ScriptHandlerMgr::addObjectHandler(void* object,int handler,ScriptHandlerMgr::HandlerType handlerType) +{ + if (NULL == object) + return; + + //may be not need + removeObjectHandler(object,handlerType); + + auto iter = _mapObjectHandlers.find(object); + VecHandlerPairs vecHandlers; + vecHandlers.clear(); + if (_mapObjectHandlers.end() != iter) + { + vecHandlers = iter->second; + } + + HandlerPair eventHanler = std::make_pair(handlerType, handler); + vecHandlers.push_back(eventHanler); + _mapObjectHandlers[object] = vecHandlers; +} +void ScriptHandlerMgr::removeObjectHandler(void* object,ScriptHandlerMgr::HandlerType handlerType) +{ + if (NULL == object || _mapObjectHandlers.empty()) + return; + + auto iterMap = _mapObjectHandlers.find(object); + if (_mapObjectHandlers.end() == iterMap) + return; + + if (iterMap->second.empty()) + return; + + auto iterVec = iterMap->second.begin(); + bool exist = false; + for (; iterVec != iterMap->second.end(); ++iterVec) + { + if (iterVec->first == handlerType) + { + exist = true; + break; + } + } + + if (exist) + { + LuaEngine::getInstance()->removeScriptHandler(iterVec->second); + iterMap->second.erase(iterVec); + } + +} +int ScriptHandlerMgr::getObjectHandler(void* object,ScriptHandlerMgr::HandlerType handlerType) +{ + if (NULL == object || _mapObjectHandlers.empty() ) + return 0; + + auto iter = _mapObjectHandlers.find(object); + + if (_mapObjectHandlers.end() != iter) + { + auto iterVec = (iter->second).begin(); + for (; iterVec != (iter->second).end(); iterVec++) + { + if (iterVec->first == handlerType) + { + return iterVec->second; + } + } + } + + return 0; +} +void ScriptHandlerMgr::removeObjectAllHandlers(void* object) +{ + if (NULL == object || _mapObjectHandlers.empty()) + return; + + auto iter = _mapObjectHandlers.find(object); + + if (_mapObjectHandlers.end() != iter) + { + if (!iter->second.empty()) + { + auto iterVec = iter->second.begin(); + for (; iterVec != iter->second.end(); ++iterVec) + { + LuaEngine::getInstance()->removeScriptHandler(iterVec->second); + } + (iter->second).clear(); + } + _mapObjectHandlers.erase(iter); + } +} + +NS_CC_END + + +static void tolua_reg_script_handler_mgr_type(lua_State* tolua_S) +{ + tolua_usertype(tolua_S, "ScheduleHandlerDelegate"); + tolua_usertype(tolua_S, "ScriptHandlerMgr"); +} + +/* method: getInstance of class ScriptHandlerMgr */ +#ifndef TOLUA_DISABLE_tolua_Cocos2d_ScriptHandlerMgr_getInstance00 +static int tolua_Cocos2d_ScriptHandlerMgr_getInstance00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if (!tolua_isusertable(tolua_S,1,"ScriptHandlerMgr",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) ) + goto tolua_lerror; + else +#endif + { + ScriptHandlerMgr* tolua_ret = (ScriptHandlerMgr*) ScriptHandlerMgr::getInstance(); + tolua_pushusertype(tolua_S,(void*)tolua_ret,"ScriptHandlerMgr"); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'getInstance'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* method: registerScriptHandler of class ScriptHandlerMgr */ +static int tolua_Cocos2d_ScriptHandlerMgr_registerScriptHandler00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"ScriptHandlerMgr",0,&tolua_err) || + !tolua_isusertype(tolua_S, 2, "cc.Ref", 0, &tolua_err) || + !toluafix_isfunction(tolua_S, 3, "LUA_FUNCTION", 0, &tolua_err) || + !tolua_isnumber(tolua_S, 4, 0, &tolua_err) || + !tolua_isnoobj(tolua_S,5,&tolua_err) ) + goto tolua_lerror; + else +#endif + { + cocos2d::ScriptHandlerMgr* scriptHanlderMgr = static_cast(tolua_tousertype(tolua_S,1,0)); +#ifndef TOLUA_RELEASE + if (nullptr == scriptHanlderMgr) + { + tolua_error(tolua_S,"invalid 'scriptHanlderMgr' in function 'tolua_Cocos2d_ScriptHandlerMgr_registerScriptHandler00'\n", NULL); + return 0; + } +#endif + LUA_FUNCTION handler = toluafix_ref_function(tolua_S,3,0); + ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)(int)tolua_tonumber(tolua_S, 4, 0); + scriptHanlderMgr->addObjectHandler(tolua_tousertype(tolua_S, 2, 0), handler,handlerType); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'registerScriptHandler'.",&tolua_err); + return 0; +#endif +} + +/* method: unregisterScriptHandler of class ScriptHandlerMgr */ +static int tolua_Cocos2d_ScriptHandlerMgr_unregisterScriptHandler00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"ScriptHandlerMgr",0,&tolua_err) || + !tolua_isusertype(tolua_S, 2, "cc.Ref", 0, &tolua_err) || + !tolua_isnumber(tolua_S, 3, 0, &tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) ) + goto tolua_lerror; + else +#endif + { + cocos2d::ScriptHandlerMgr* scriptHanlderMgr = static_cast(tolua_tousertype(tolua_S,1,0)); +#ifndef TOLUA_RELEASE + if (nullptr == scriptHanlderMgr) + { + tolua_error(tolua_S,"invalid 'scriptHanlderMgr' in function 'tolua_Cocos2d_ScriptHandlerMgr_unregisterScriptHandler00'\n", NULL); + return 0; + } +#endif + ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)(int)tolua_tonumber(tolua_S, 3, 0); + scriptHanlderMgr->removeObjectHandler(tolua_tousertype(tolua_S, 2, 0), handlerType); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'unregisterScriptHandler'.",&tolua_err); + return 0; +#endif +} + +/* method: removeObjectAllHandlers of class ScriptHandlerMgr */ +static int tolua_Cocos2d_ScriptHandlerMgr_removeObjectAllHandlers00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"ScriptHandlerMgr",0,&tolua_err) || + !tolua_isusertype(tolua_S, 2, "cc.Ref", 0, &tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) ) + goto tolua_lerror; + else +#endif + { + cocos2d::ScriptHandlerMgr* scriptHanlderMgr = static_cast(tolua_tousertype(tolua_S,1,0)); +#ifndef TOLUA_RELEASE + if (nullptr == scriptHanlderMgr) + { + tolua_error(tolua_S,"invalid 'scriptHanlderMgr' in function 'tolua_Cocos2d_ScriptHandlerMgr_removeObjectAllHandlers00'\n", NULL); + return 0; + } +#endif + scriptHanlderMgr->removeObjectAllHandlers(tolua_tousertype(tolua_S, 2, 0)); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'removeObjectAllHandlers'.",&tolua_err); + return 0; +#endif +} + +TOLUA_API int tolua_script_handler_mgr_open(lua_State* tolua_S) +{ + tolua_open(tolua_S); + tolua_reg_script_handler_mgr_type(tolua_S); + tolua_module(tolua_S, NULL,0); + tolua_beginmodule(tolua_S, NULL); + tolua_cclass(tolua_S,"ScriptHandlerMgr","ScriptHandlerMgr","",NULL); + tolua_beginmodule(tolua_S, "ScriptHandlerMgr"); + tolua_function(tolua_S, "getInstance", tolua_Cocos2d_ScriptHandlerMgr_getInstance00); + tolua_function(tolua_S, "registerScriptHandler", tolua_Cocos2d_ScriptHandlerMgr_registerScriptHandler00); + tolua_function(tolua_S, "unregisterScriptHandler", tolua_Cocos2d_ScriptHandlerMgr_unregisterScriptHandler00); + tolua_function(tolua_S, "removeObjectAllHandlers", tolua_Cocos2d_ScriptHandlerMgr_removeObjectAllHandlers00); + tolua_endmodule(tolua_S); + tolua_endmodule(tolua_S); + return 1; +} diff --git a/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.h b/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.h new file mode 100644 index 0000000000..08dcd2f4a8 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.h @@ -0,0 +1,189 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef __LUA_SCRIPT_HANDLER_MGR_H__ +#define __LUA_SCRIPT_HANDLER_MGR_H__ + + +extern "C" { +#include "tolua++.h" +} + + +#include "CCRef.h" +#include "ccMacros.h" +#include "CCActionInstant.h" +#include +#include + +NS_CC_BEGIN + +class ScheduleHandlerDelegate; + + +typedef std::vector VecShedule; +typedef std::map MapNodeSchedules; + +class ScheduleHandlerDelegate:public cocos2d::Ref +{ +public: + ScheduleHandlerDelegate():_isUpdateSchedule(false) + {} + virtual ~ScheduleHandlerDelegate() + {} + + static ScheduleHandlerDelegate* create(); + + void scheduleFunc(float elapse); + + virtual void update(float elapse); + + void setUpdateSchedule(bool isUpdateSchedule){ _isUpdateSchedule = isUpdateSchedule; } + bool isUpdateSchedule(){ return _isUpdateSchedule; } +private: + bool _isUpdateSchedule; +}; + +class LuaCallFunc:public cocos2d::CallFuncN +{ +public: + LuaCallFunc():_functionLua(nullptr) + {} + virtual ~LuaCallFunc() + {} + + static LuaCallFunc* create(const std::function& func); + bool initWithFunction(const std::function& func); + virtual LuaCallFunc* clone() const; + virtual void execute() override; +protected: + /** + */ + std::function _functionLua; + +}; + +class ScriptHandlerMgr +{ +public: + enum class HandlerType: int + { + NODE = 0, + MENU_CLICKED, + CALLFUNC, + SCHEDULE, + TOUCHES, + KEYPAD, + ACCELEROMETER, + + CONTROL_TOUCH_DOWN, + CONTROL_TOUCH_DRAG_INSIDE, + CONTROL_TOUCH_DRAG_OUTSIDE, + CONTROL_TOUCH_DRAG_ENTER, + CONTROL_TOUCH_DRAG_EXIT, + CONTROL_TOUCH_UP_INSIDE, + CONTROL_TOUCH_UP_OUTSIDE, + CONTROL_TOUCH_UP_CANCEL, + CONTROL_VALUE_CHANGED, + + WEBSOCKET_OPEN, + WEBSOCKET_MESSAGE, + WEBSOCKET_CLOSE, + WEBSOCKET_ERROR, + + GL_NODE_DRAW, + + SCROLLVIEW_SCROLL, + SCROLLVIEW_ZOOM, + + TABLECELL_TOUCHED, + TABLECELL_HIGHLIGHT, + TABLECELL_UNHIGHLIGHT, + TABLECELL_WILL_RECYCLE, + TABLECELL_SIZE_FOR_INDEX, + TABLECELL_AT_INDEX, + TABLEVIEW_NUMS_OF_CELLS, + + XMLHTTPREQUEST_READY_STATE_CHANGE, + + ASSETSMANAGER_PROGRESS, + ASSETSMANAGER_SUCCESS, + ASSETSMANAGER_ERROR, + + STUDIO_EVENT_LISTENER, + ARMATURE_EVENT, + + EVENT_ACC, + EVENT_CUSTIOM, + + EVENT_KEYBOARD_PRESSED, + EVENT_KEYBOARD_RELEASED, + + EVENT_TOUCH_BEGAN, + EVENT_TOUCH_MOVED, + EVENT_TOUCH_ENDED, + EVENT_TOUCH_CANCELLED, + + EVENT_TOUCHES_BEGAN, + EVENT_TOUCHES_MOVED, + EVENT_TOUCHES_ENDED, + EVENT_TOUCHES_CANCELLED, + + EVENT_MOUSE_DOWN, + EVENT_MOUSE_UP, + EVENT_MOUSE_MOVE, + EVENT_MOUSE_SCROLL, + + EVENT_SPINE, + + EVENT_PHYSICS_CONTACT_BEGIN, + EVENT_PHYSICS_CONTACT_PRESOLVE, + EVENT_PHYSICS_CONTACT_POSTSOLVE, + EVENT_PHYSICS_CONTACT_SEPERATE, + }; + + typedef int Handler; + typedef std::pair HandlerPair; + typedef std::vector VecHandlerPairs; + typedef std::map MapObjectHandlers; + + ScriptHandlerMgr(void); + virtual ~ScriptHandlerMgr(void); + static ScriptHandlerMgr* getInstance(void); + + void addObjectHandler(void* object,int handler,ScriptHandlerMgr::HandlerType handlerType); + void removeObjectHandler(void* object,ScriptHandlerMgr::HandlerType handlerType); + int getObjectHandler(void* object,ScriptHandlerMgr::HandlerType handlerType); + void removeObjectAllHandlers(void* object); + +private: + void init(void); + static ScriptHandlerMgr* _scriptHandlerMgr; + MapObjectHandlers _mapObjectHandlers; +}; + +NS_CC_END + +TOLUA_API int tolua_script_handler_mgr_open(lua_State* tolua_S); + +#endif //__LUA_SCRIPT_HANDLER_MGR_H__ diff --git a/cocos/scripting/lua-bindings/manual/LuaSkeletonAnimation.cpp b/cocos/scripting/lua-bindings/manual/LuaSkeletonAnimation.cpp new file mode 100644 index 0000000000..92cc7699bd --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/LuaSkeletonAnimation.cpp @@ -0,0 +1,123 @@ + /**************************************************************************** + Copyright (c) 2013 Edward Zhou + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "LuaSkeletonAnimation.h" +#include "cocos2d.h" +#include "LuaScriptHandlerMgr.h" +#include "CCLuaStack.h" +#include "CCLuaEngine.h" + +using namespace spine; +USING_NS_CC; + +static int SendSpineEventToLua(int nHandler, spine::SkeletonAnimation* node, int trackIndex, spEventType type, spEvent* event, int loopCount) +{ + if (nHandler <= 0) { + return 0; + } + + if (NULL == ScriptEngineManager::getInstance()->getScriptEngine()) { + return 0; + } + + LuaStack *pStack = LuaEngine::getInstance()->getLuaStack(); + if (NULL == pStack) { + return 0; + } + + lua_State *tolua_s = pStack->getLuaState(); + if (NULL == tolua_s) { + return 0; + } + + int nRet = 0; + + spTrackEntry* entry = spAnimationState_getCurrent(node->state, trackIndex); + std::string animationName = (entry && entry->animation) ? entry->animation->name : ""; + std::string eventType = ""; + + switch (type) { + case ANIMATION_START: + eventType = "start"; + break; + case ANIMATION_END: + eventType = "end"; + break; + case ANIMATION_COMPLETE: + eventType = "complete"; + break; + case ANIMATION_EVENT: + eventType = "event"; + break; + } + + LuaValueDict spineEvent; + spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("type", LuaValue::stringValue(eventType))); + spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("trackIndex", LuaValue::intValue(trackIndex))); + spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("animation", LuaValue::stringValue(animationName))); + spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("loopCount", LuaValue::intValue(loopCount))); + + if (NULL != event) { + LuaValueDict eventData; + eventData.insert(eventData.end(), LuaValueDict::value_type("name", LuaValue::stringValue(event->data->name))); + eventData.insert(eventData.end(), LuaValueDict::value_type("intValue", LuaValue::intValue(event->intValue))); + eventData.insert(eventData.end(), LuaValueDict::value_type("floatValue", LuaValue::floatValue(event->floatValue))); + eventData.insert(eventData.end(), LuaValueDict::value_type("stringValue", LuaValue::stringValue(event->stringValue))); + spineEvent.insert(spineEvent.end(), LuaValueDict::value_type("eventData", LuaValue::dictValue(eventData))); + } + + pStack->pushLuaValueDict(spineEvent); + nRet = pStack->executeFunctionByHandler(nHandler, 1); + pStack->clean(); + return nRet; + +} + +LuaSkeletonAnimation::LuaSkeletonAnimation (const char* skeletonDataFile, const char* atlasFile, float scale) +: spine::SkeletonAnimation(skeletonDataFile, atlasFile, scale) +{ + this->setAnimationListener(this, animationStateEvent_selector(LuaSkeletonAnimation::animationStateEvent)); +} + + +LuaSkeletonAnimation::~LuaSkeletonAnimation() +{ + ScriptHandlerMgr::getInstance()->removeObjectAllHandlers((void*)this); +} + +LuaSkeletonAnimation* LuaSkeletonAnimation::createWithFile (const char* skeletonDataFile, const char* atlasFile, float scale) +{ + LuaSkeletonAnimation* node = new LuaSkeletonAnimation(skeletonDataFile, atlasFile, scale); + node->autorelease(); + return node; +} + +void LuaSkeletonAnimation::animationStateEvent (spine::SkeletonAnimation* node, int trackIndex, spEventType type, spEvent* event, int loopCount) +{ + int nHandler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::EVENT_SPINE); + if (0 != nHandler) { + SendSpineEventToLua(nHandler, node, trackIndex, type, event, loopCount); + } +} diff --git a/cocos/scripting/lua-bindings/manual/LuaSkeletonAnimation.h b/cocos/scripting/lua-bindings/manual/LuaSkeletonAnimation.h new file mode 100644 index 0000000000..7872ad7cbb --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/LuaSkeletonAnimation.h @@ -0,0 +1,42 @@ + /**************************************************************************** + Copyright (c) 2013 Edward Zhou + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#ifndef LUASKELETONANIMATION_H_ +#define LUASKELETONANIMATION_H_ +#include "spine-cocos2dx.h" + +class LuaSkeletonAnimation: public spine::SkeletonAnimation { +private: + void animationStateEvent (spine::SkeletonAnimation* node, int trackIndex, spEventType type, spEvent* event, int loopCount); + +public: + static LuaSkeletonAnimation* createWithFile (const char* skeletonDataFile, const char* atlasFile, float scale = 1); + + LuaSkeletonAnimation (const char* skeletonDataFile, const char* atlasFile, float scale = 1); + + virtual ~LuaSkeletonAnimation(); +}; + +#endif /* LUASKELETONANIMATION_H_ */ diff --git a/cocos/scripting/lua-bindings/manual/Lua_web_socket.cpp b/cocos/scripting/lua-bindings/manual/Lua_web_socket.cpp new file mode 100644 index 0000000000..6a22ce66d0 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/Lua_web_socket.cpp @@ -0,0 +1,468 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) +#include "Lua_web_socket.h" +#include +#include +#include "tolua_fix.h" +#include "cocos2d.h" +#include "CCLuaStack.h" +#include "CCLuaValue.h" +#include "CCLuaEngine.h" +#include "LuaScriptHandlerMgr.h" + +using namespace cocos2d; + + +static int SendBinaryMessageToLua(int nHandler,const unsigned char* pTable,int nLength) +{ + if (NULL == pTable || nHandler <= 0) { + return 0; + } + + if (NULL == ScriptEngineManager::getInstance()->getScriptEngine()) { + return 0; + } + + LuaStack *pStack = LuaEngine::getInstance()->getLuaStack(); + if (NULL == pStack) { + return 0; + } + + lua_State *tolua_s = pStack->getLuaState(); + if (NULL == tolua_s) { + return 0; + } + + int nRet = 0; + LuaValueArray array; + for (int i = 0 ; i < nLength; i++) { + LuaValue value = LuaValue::intValue(pTable[i]); + array.push_back(value); + } + + pStack->pushLuaValueArray(array); + nRet = pStack->executeFunctionByHandler(nHandler, 1); + pStack->clean(); + return nRet; +} + + + +LuaWebSocket::~LuaWebSocket() +{ + ScriptHandlerMgr::getInstance()->removeObjectAllHandlers((void*)this); +} + +void LuaWebSocket::onOpen(WebSocket* ws) +{ + LuaWebSocket* luaWs = dynamic_cast(ws); + if (NULL != luaWs) { + int nHandler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this,ScriptHandlerMgr::HandlerType::WEBSOCKET_OPEN); + if (0 != nHandler) { + CommonScriptData data(nHandler,""); + ScriptEvent event(kCommonEvent,(void*)&data); + ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + } + } +} + +void LuaWebSocket::onMessage(WebSocket* ws, const WebSocket::Data& data) +{ + LuaWebSocket* luaWs = dynamic_cast(ws); + if (NULL != luaWs) { + if (data.isBinary) { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this,ScriptHandlerMgr::HandlerType::WEBSOCKET_MESSAGE); + if (0 != handler) { + SendBinaryMessageToLua(handler, (const unsigned char*)data.bytes, data.len); + } + } + else{ + + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this,ScriptHandlerMgr::HandlerType::WEBSOCKET_MESSAGE); + if (0 != handler) + { + LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); + if (nullptr != stack) + { + stack->pushString(data.bytes,data.len); + stack->executeFunctionByHandler(handler, 1); + } + } + } + } +} + +void LuaWebSocket::onClose(WebSocket* ws) +{ + LuaWebSocket* luaWs = dynamic_cast(ws); + if (NULL != luaWs) { + int nHandler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this,ScriptHandlerMgr::HandlerType::WEBSOCKET_CLOSE); + if (0 != nHandler) + { + CommonScriptData data(nHandler,""); + ScriptEvent event(kCommonEvent,(void*)&data); + ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + } + } +} + +void LuaWebSocket::onError(WebSocket* ws, const WebSocket::ErrorCode& error) +{ + LuaWebSocket* luaWs = dynamic_cast(ws); + if (NULL != luaWs) { + int nHandler = 0;//luaWs->getScriptHandler(LuaWebSocket::kWebSocketScriptHandlerError); + if (0 != nHandler) + { + CommonScriptData data(nHandler,""); + ScriptEvent event(kCommonEvent,(void*)&data); + ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + } + } +} + + + +#ifdef __cplusplus +static int tolua_collect_WebSocket (lua_State* tolua_S) +{ + LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0); + Mtolua_delete(self); + return 0; +} +#endif +/* function to release collected object via destructor */ +static void tolua_reg_Web_Socket_type(lua_State* tolua_S) +{ + tolua_usertype(tolua_S, "cc.WebSocket"); +} + +/* method: create of class WebSocket */ +#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_create00 +static int tolua_Cocos2d_WebSocket_create00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"cc.WebSocket",0,&tolua_err) || + !tolua_isstring(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char* urlName = ((const char*) tolua_tostring(tolua_S,2,0)); + LuaWebSocket *wSocket = new LuaWebSocket(); + wSocket->init(*wSocket, urlName); + tolua_pushusertype(tolua_S,(void*)wSocket,"cc.WebSocket"); + tolua_register_gc(tolua_S,lua_gettop(tolua_S)); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* method: createByAProtocol of class WebSocket */ +#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_createByAProtocol00 +static int tolua_Cocos2d_WebSocket_createByAProtocol00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"cc.WebSocket",0,&tolua_err) || + !tolua_isstring(tolua_S,2,0,&tolua_err) || + !tolua_isstring(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char *urlName = ((const char*) tolua_tostring(tolua_S,2,0)); + const char *protocol = ((const char*) tolua_tostring(tolua_S,3,0)); + std::vector protocols; + protocols.push_back(protocol); + LuaWebSocket *wSocket = new LuaWebSocket(); + wSocket->init(*wSocket, urlName,&protocols); + tolua_pushusertype(tolua_S,(void*)wSocket,"cc.WebSocket"); + tolua_register_gc(tolua_S,lua_gettop(tolua_S)); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createByAProtocol'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* method: createByAProtocol of class WebSocket */ +#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_createByProtocolArray00 +static int tolua_Cocos2d_WebSocket_createByProtocolArray00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"cc.WebSocket",0,&tolua_err) || + !tolua_isstring(tolua_S,2,0,&tolua_err) || + !tolua_isusertable(tolua_S,3,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char *urlName = ((const char*) tolua_tostring(tolua_S,2,0)); + Array* protocolArray = ((Array*) tolua_tousertype(tolua_S,3,0)); + std::vector protocols; + if (NULL != protocolArray) { + Ref* pObj = NULL; + CCARRAY_FOREACH(protocolArray, pObj) + { + String* pStr = static_cast(pObj); + if (NULL != pStr) { + protocols.push_back(pStr->getCString()); + } + } + } + LuaWebSocket *wSocket = new LuaWebSocket(); + wSocket->init(*wSocket, urlName,&protocols); + tolua_pushusertype(tolua_S,(void*)wSocket,"cc.WebSocket"); + tolua_register_gc(tolua_S,lua_gettop(tolua_S)); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createByProtocolArray'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* method: getReadyState of class WebSocket */ +#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_getReadyState00 +static int tolua_Cocos2d_WebSocket_getReadyState00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"cc.WebSocket",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaWebSocket *self = (LuaWebSocket*)tolua_tousertype(tolua_S,1,0); + int tolua_ret = -1; + if (NULL != self) { + tolua_ret = (int)self->getReadyState(); + } + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'getReadyState'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + + +/* method: close of class WebSocket */ +#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_close00 +static int tolua_Cocos2d_WebSocket_close00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"cc.WebSocket",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0); + if (NULL != self ) { + self->close(); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'getReadyState'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + +/* method: sendString of class WebSocket */ +#ifndef TOLUA_DISABLE_tolua_Cocos2d_WebSocket_sendString00 +static int tolua_Cocos2d_WebSocket_sendString00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S, 1, "cc.WebSocket", 0, &tolua_err) || + !tolua_isstring(tolua_S, 2, 0, &tolua_err) || + !tolua_isnoobj(tolua_S, 3, &tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0); + size_t size = 0; + const char* data = (const char*) lua_tolstring(tolua_S, 2, &size); + if ( NULL == data) + return 0; + + if (strlen(data) != size) + { + self->send((const unsigned char*)data, size); + } + else + { + self->send(data); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'sendString'.",&tolua_err); + return 0; +#endif +} +#endif //#ifndef TOLUA_DISABLE + + +TOLUA_API int tolua_web_socket_open(lua_State* tolua_S){ + tolua_open(tolua_S); + tolua_reg_Web_Socket_type(tolua_S); + tolua_module(tolua_S,"cc",0); + tolua_beginmodule(tolua_S,"cc"); + #ifdef __cplusplus + tolua_cclass(tolua_S,"WebSocket","cc.WebSocket","",tolua_collect_WebSocket); + #else + tolua_cclass(tolua_S,"WebSocket","cc.WebSocket","",NULL); + #endif + tolua_beginmodule(tolua_S,"WebSocket"); + tolua_function(tolua_S, "create", tolua_Cocos2d_WebSocket_create00); + tolua_function(tolua_S, "createByAProtocol", tolua_Cocos2d_WebSocket_createByAProtocol00); + tolua_function(tolua_S, "createByProtocolArray", tolua_Cocos2d_WebSocket_createByProtocolArray00); + tolua_function(tolua_S, "getReadyState", tolua_Cocos2d_WebSocket_getReadyState00); + tolua_function(tolua_S, "close", tolua_Cocos2d_WebSocket_close00); + tolua_function(tolua_S, "sendString", tolua_Cocos2d_WebSocket_sendString00); + tolua_endmodule(tolua_S); + tolua_endmodule(tolua_S); + return 1; +} + +int tolua_Cocos2d_WebSocket_registerScriptHandler00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"cc.WebSocket",0,&tolua_err) || + !toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0); + if (NULL != self ) { + int handler = ( toluafix_ref_function(tolua_S,2,0)); + ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)((int)tolua_tonumber(tolua_S,3,0) + (int)ScriptHandlerMgr::HandlerType::WEBSOCKET_OPEN); + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, handlerType); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'registerScriptHandler'.",&tolua_err); + return 0; +#endif +} + +int tolua_Cocos2d_WebSocket_unregisterScriptHandler00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"cc.WebSocket",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaWebSocket* self = (LuaWebSocket*) tolua_tousertype(tolua_S,1,0); + if (NULL != self ) { + ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType)((int)tolua_tonumber(tolua_S,2,0) + (int)ScriptHandlerMgr::HandlerType::WEBSOCKET_OPEN); + + ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)self, handlerType); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'unregisterScriptHandler'.",&tolua_err); + return 0; +#endif +} + +TOLUA_API int register_web_socket_manual(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0 ; + + lua_pushstring(tolua_S,"cc.WebSocket"); + lua_rawget(tolua_S,LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"registerScriptHandler"); + lua_pushcfunction(tolua_S,tolua_Cocos2d_WebSocket_registerScriptHandler00); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"unregisterScriptHandler"); + lua_pushcfunction(tolua_S,tolua_Cocos2d_WebSocket_unregisterScriptHandler00); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + return 1; +} + +#endif//(CC_TARGET_PLATFORM == CC_PLATFORM_IOS ... diff --git a/cocos/scripting/lua-bindings/manual/Lua_web_socket.h b/cocos/scripting/lua-bindings/manual/Lua_web_socket.h new file mode 100644 index 0000000000..d2463af3c2 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/Lua_web_socket.h @@ -0,0 +1,61 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef __LUA_WEB_SOCKET_H__ +#define __LUA_WEB_SOCKET_H__ + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +#include "network/WebSocket.h" +class LuaWebSocket: public cocos2d::network::WebSocket,public cocos2d::network::WebSocket::Delegate +{ +public: + virtual ~LuaWebSocket(); + virtual void onOpen(WebSocket* ws) override; + virtual void onMessage(WebSocket* ws, const WebSocket::Data& data) override; + virtual void onClose(WebSocket* ws) override; + virtual void onError(WebSocket* ws, const WebSocket::ErrorCode& error) override; + + enum WebSocketScriptHandlerType + { + kWebSocketScriptHandlerOpen, + kWebSocketScriptHandlerMessage, + kWebSocketScriptHandlerClose, + kWebSocketScriptHandlerError, + }; +}; + +TOLUA_API int tolua_web_socket_open(lua_State* tolua_S); +TOLUA_API int register_web_socket_manual(lua_State* tolua_S); + +#endif //(CC_TARGET_PLATFORM == CC_PLATFORM_IOS ... + +#endif //__LUA_WEB_SOCKET_H__ diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_coco_studio_manual.cpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_coco_studio_manual.cpp new file mode 100644 index 0000000000..203fb74fe7 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_coco_studio_manual.cpp @@ -0,0 +1,419 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#include "lua_cocos2dx_coco_studio_manual.hpp" +#include "cocos2d.h" +#include "tolua_fix.h" +#include "LuaBasicConversions.h" +#include "LuaScriptHandlerMgr.h" +#include "CCLuaValue.h" +#include "CocoStudio.h" +#include "CCLuaEngine.h" + +using namespace cocostudio; + +class LuaArmatureWrapper:public Ref +{ +public: + LuaArmatureWrapper(); + virtual ~LuaArmatureWrapper(); + + virtual void addArmatureFileInfoAsyncCallback(float percent); +}; + +LuaArmatureWrapper::LuaArmatureWrapper() +{ + +} + +LuaArmatureWrapper::~LuaArmatureWrapper() +{ + +} + +void LuaArmatureWrapper::addArmatureFileInfoAsyncCallback(float percent) +{ + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::ARMATURE_EVENT); + + if (0 != handler) + { + LuaArmatureWrapperEventData wrapperData(LuaArmatureWrapperEventData::LuaArmatureWrapperEventType::FILE_ASYNC , (void*)&percent); + + BasicScriptData data(this,(void*)&wrapperData); + + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ARMATURE_EVENT, (void*)&data); + } +} + +static int lua_cocos2dx_ArmatureAnimation_setMovementEventCallFunc(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + ArmatureAnimation* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccs.ArmatureAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_ArmatureAnimation_setMovementEventCallFunc'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + LuaArmatureWrapper* wrapper = new LuaArmatureWrapper(); + wrapper->autorelease(); + + Vector vec; + vec.pushBack(wrapper); + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)wrapper, handler, ScriptHandlerMgr::HandlerType::ARMATURE_EVENT); + + self->setMovementEventCallFunc([=](Armature *armature, MovementEventType movementType, const std::string& movementID){ + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)vec.at(0), ScriptHandlerMgr::HandlerType::ARMATURE_EVENT); + + if (0 != handler) + { + std::string strMovementID = movementID; + LuaArmatureMovementEventData movementData(armature,(int)movementType, strMovementID); + + LuaArmatureWrapperEventData wrapperData(LuaArmatureWrapperEventData::LuaArmatureWrapperEventType::MOVEMENT_EVENT , (void*)&movementData); + + BasicScriptData data((void*)vec.at(0),(void*)&wrapperData); + + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ARMATURE_EVENT, (void*)&data); + } + }); + return 0; + } + + CCLOG("'setMovementEventCallFunc' function of ArmatureAnimation has wrong number of arguments: %d, was expecting %d\n", argc, 1); + + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'setMovementEventCallFunc'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_ArmatureAnimation_setFrameEventCallFunc(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + ArmatureAnimation* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccs.ArmatureAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_ArmatureAnimation_setFrameEventCallFunc'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err) ) + { + goto tolua_lerror; + } +#endif + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + LuaArmatureWrapper* wrapper = new LuaArmatureWrapper(); + wrapper->autorelease(); + + Vector vec; + vec.pushBack(wrapper); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)wrapper, handler, ScriptHandlerMgr::HandlerType::ARMATURE_EVENT); + + self->setFrameEventCallFunc([=](Bone *bone, const std::string& frameEventName, int originFrameIndex, int currentFrameIndex){ + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)vec.at(0), ScriptHandlerMgr::HandlerType::ARMATURE_EVENT); + + if (0 != handler) + { + std::string strFrameEventName(frameEventName); + + LuaArmatureFrameEventData frameData(bone,frameEventName,originFrameIndex,currentFrameIndex); + + LuaArmatureWrapperEventData wrapperData(LuaArmatureWrapperEventData::LuaArmatureWrapperEventType::FRAME_EVENT , (void*)&frameData); + + BasicScriptData data((void*)vec.at(0),(void*)&wrapperData); + + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ARMATURE_EVENT, (void*)&data); + } + }); + + return 0; + } + + + CCLOG("'setFrameEventCallFunc' function of ArmatureAnimation has wrong number of arguments: %d, was expecting %d\n", argc, 1); + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'setFrameEventCallFunc'.",&tolua_err); + return 0; +#endif +} + +static void extendArmatureAnimation(lua_State* L) +{ + lua_pushstring(L, "ccs.ArmatureAnimation"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "setMovementEventCallFunc", lua_cocos2dx_ArmatureAnimation_setMovementEventCallFunc); + tolua_function(L, "setFrameEventCallFunc", lua_cocos2dx_ArmatureAnimation_setFrameEventCallFunc); + } + lua_pop(L, 1); +} + +static int lua_cocos2dx_ArmatureDataManager_addArmatureFileInfoAsyncCallFunc(lua_State* L) +{ + if (nullptr == L) + return 0 ; + + int argc = 0; + ArmatureDataManager* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccs.ArmatureDataManager",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_ArmatureDataManager_addArmatureFileInfoAsyncCallFunc'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + + if (2 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(L, 2, 0, &tolua_err) || + !toluafix_isfunction(L,3,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + const char* configFilePath = tolua_tostring(L, 2, ""); + LUA_FUNCTION handler = ( toluafix_ref_function(L, 3, 0)); + + LuaArmatureWrapper* wrapper = new LuaArmatureWrapper(); + wrapper->autorelease(); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)wrapper, handler, ScriptHandlerMgr::HandlerType::ARMATURE_EVENT); + + self->addArmatureFileInfoAsync(configFilePath, wrapper, schedule_selector(LuaArmatureWrapper::addArmatureFileInfoAsyncCallback)); + + return 0; + } + else if (4 == argc) + { +#if COCOS2D_DEBUG >= 1 + if ( !tolua_isstring(L, 2, 0, &tolua_err) || + !tolua_isstring(L, 3, 0, &tolua_err) || + !tolua_isstring(L, 4, 0, &tolua_err) || + !toluafix_isfunction(L,5,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + const char* imagePath = tolua_tostring(L, 2, ""); + const char* plistPath = tolua_tostring(L, 3, ""); + const char* configFilePath = tolua_tostring(L, 4, ""); + + LUA_FUNCTION handler = ( toluafix_ref_function(L,5,0)); + + LuaArmatureWrapper* wrapper = new LuaArmatureWrapper(); + wrapper->autorelease(); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)wrapper, handler, ScriptHandlerMgr::HandlerType::ARMATURE_EVENT); + + self->addArmatureFileInfoAsync(imagePath, plistPath,configFilePath,wrapper, schedule_selector(LuaArmatureWrapper::addArmatureFileInfoAsyncCallback)); + + return 0; + } + + CCLOG("'addArmatureFileInfoAsync' function of ArmatureDataManager has wrong number of arguments: %d, was expecting %d\n", argc, 1); + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addArmatureFileInfoAsync'.",&tolua_err); + return 0; +#endif +} + +static void extendArmatureDataManager(lua_State* L) +{ + lua_pushstring(L, "ccs.ArmatureDataManager"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "addArmatureFileInfoAsync", lua_cocos2dx_ArmatureDataManager_addArmatureFileInfoAsyncCallFunc); + } + lua_pop(L, 1); +} + +static int lua_cocos2dx_extension_Bone_setIgnoreMovementBoneData(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + cocostudio::Bone* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccs.Bone",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_extension_Bone_setIgnoreMovementBoneData'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isboolean(L, 2, 0, &tolua_err)) + goto tolua_lerror; +#endif + bool ignore = (bool)tolua_toboolean(L, 2, 0); + self->setIgnoreMovementBoneData(ignore); + return 0; + } + + CCLOG("'setIgnoreMovementBoneData' function of Bone has wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'setIgnoreMovementBoneData'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_extension_Bone_getIgnoreMovementBoneData(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + cocostudio::Bone* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccs.Bone",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_extension_Bone_getIgnoreMovementBoneData'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (0 == argc) + { + tolua_pushboolean(L, self->getIgnoreMovementBoneData()); + return 1; + } + + CCLOG("'getIgnoreMovementBoneData' function of Bone has wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'getIgnoreMovementBoneData'.",&tolua_err); + return 0; +#endif +} + +static void extendBone(lua_State* L) +{ + lua_pushstring(L, "ccs.Bone"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "setIgnoreMovementBoneData", lua_cocos2dx_extension_Bone_setIgnoreMovementBoneData); + tolua_function(L, "getIgnoreMovementBoneData", lua_cocos2dx_extension_Bone_getIgnoreMovementBoneData); + } + lua_pop(L, 1); +} + +int register_all_cocos2dx_coco_studio_manual(lua_State* L) +{ + if (nullptr == L) + return 0; + extendArmatureAnimation(L); + extendArmatureDataManager(L); + extendBone(L); + + return 0; +} \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_coco_studio_manual.hpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_coco_studio_manual.hpp new file mode 100644 index 0000000000..33146f254a --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_coco_studio_manual.hpp @@ -0,0 +1,80 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_COCO_STUDIO_MANUAL_H +#define COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_COCO_STUDIO_MANUAL_H + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +#include "CCRef.h" +#include + +TOLUA_API int register_all_cocos2dx_coco_studio_manual(lua_State* L); + +struct LuaArmatureWrapperEventData +{ + enum class LuaArmatureWrapperEventType + { + MOVEMENT_EVENT, + FRAME_EVENT, + FILE_ASYNC, + }; + + LuaArmatureWrapperEventType eventType; + void* eventData; + + LuaArmatureWrapperEventData(LuaArmatureWrapperEventType _eventType, void* _eventData):eventType(_eventType),eventData(_eventData) + { + } +}; + +struct LuaArmatureMovementEventData +{ + cocos2d::Ref* objTarget; + int movementType; + std::string movementID; + + LuaArmatureMovementEventData(cocos2d::Ref* _objTarget, int _movementType,const std::string& _movementID):objTarget(_objTarget),movementType(_movementType),movementID(_movementID) + { + } +}; + +struct LuaArmatureFrameEventData +{ + cocos2d::Ref* objTarget; + std::string frameEventName; + int originFrameIndex; + int currentFrameIndex; + + LuaArmatureFrameEventData( cocos2d::Ref* _objTarget, const std::string& _frameEventName, int _originFrameIndex, int _currentFrameIndex):objTarget(_objTarget), frameEventName(_frameEventName),originFrameIndex(_originFrameIndex), currentFrameIndex(_currentFrameIndex) + { + } +}; + +#endif // #ifndef COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_COCO_STUDIO_MANUAL_H diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_deprecated.cpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_deprecated.cpp new file mode 100644 index 0000000000..2b3c868114 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_deprecated.cpp @@ -0,0 +1,2351 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#include "lua_cocos2dx_deprecated.h" +#include "cocos2d.h" +#include "tolua_fix.h" +#include "LuaBasicConversions.h" +#include "LuaScriptHandlerMgr.h" +#include "CCLuaValue.h" +#include "cocos-ext.h" + +USING_NS_CC; +USING_NS_CC_EXT; + +template +bool array_to_vector_t_deprecated(Array& array,Vector& vec) +{ + if ( 0 == array.count() ) + return false; + + vec.clear(); + + for (int i = 0; i < array.count() ; i++) + { + T obj = dynamic_cast(array.getObjectAtIndex(i)); + if (nullptr != obj) + vec.pushBack(obj); + } + + return true; +} + +bool array_to_valuevector_deprecated(Array& array,ValueVector& valueVec) +{ + if (0 == array.count()) + return false; + + valueVec.clear(); + + String* strVal = nullptr; + Double* doubleVal = nullptr; + Bool* boolVal = nullptr; + Float* floatVal = nullptr; + Integer* intVal = nullptr; + + for (int i = 0; i < array.count(); i++) + { + if( (strVal = dynamic_cast(array.getObjectAtIndex(i)))) + { + valueVec.push_back(Value(strVal->getCString())); + } + else if ((doubleVal = dynamic_cast(array.getObjectAtIndex(i)))) + { + valueVec.push_back(Value(doubleVal->getValue())); + } + else if ((floatVal = dynamic_cast(array.getObjectAtIndex(i)))) + { + valueVec.push_back(Value(floatVal->getValue())); + } + else if ((intVal = dynamic_cast(array.getObjectAtIndex(i)))) + { + valueVec.push_back(Value(intVal->getValue())); + } + else if ((boolVal = dynamic_cast(array.getObjectAtIndex(i)))) + { + valueVec.push_back(Value(boolVal->getValue())); + } + else + { + CCASSERT(false, "the type isn't suppored."); + } + } + + return true; +} + +#define deprecatedClassTip(className) CCLOG("%s will be not binded in lua,please use the lua's table instead",className) +#define deprecatedFunctionTip(oldFun,newFun) CCLOG("%s was deprecated please use %s instead ",oldFun, newFun) +static int tolua_Cocos2d_CCPoint_new00(lua_State* tolua_S) +{ + deprecatedClassTip("CCPoint"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCPoint",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Point tolua_ret; + point_to_luaval(tolua_S, tolua_ret); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCPoint_new00_local(lua_State* tolua_S) +{ + deprecatedClassTip("CCPoint"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCPoint",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Point tolua_ret; + point_to_luaval(tolua_S, tolua_ret); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCPoint_new01(lua_State* tolua_S) +{ + deprecatedClassTip("CCPoint"); + + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCPoint",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else + { + float x = ((float) tolua_tonumber(tolua_S,2,0)); + float y = ((float) tolua_tonumber(tolua_S,3,0)); + Point tolua_ret(x,y); + point_to_luaval(tolua_S, tolua_ret); + } + return 1; +tolua_lerror: + return tolua_Cocos2d_CCPoint_new00(tolua_S); +} + + +static int tolua_Cocos2d_CCPoint_new01_local(lua_State* tolua_S) +{ + deprecatedClassTip("CCPoint"); + + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCPoint",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else + { + float x = ((float) tolua_tonumber(tolua_S,2,0)); + float y = ((float) tolua_tonumber(tolua_S,3,0)); + Point tolua_ret(x,y); + point_to_luaval(tolua_S, tolua_ret); + } + return 1; +tolua_lerror: + return tolua_Cocos2d_CCPoint_new00_local(tolua_S); +} + +static int tolua_collect_CCPoint (lua_State* tolua_S) +{ + Point* self = (Point*) tolua_tousertype(tolua_S,1,0); + Mtolua_delete(self); + return 0; +} + +static int register_cocos2dx_deprecated_Point(lua_State* tolua_S) +{ + tolua_usertype(tolua_S,"CCPoint"); +#ifdef __cplusplus + tolua_cclass(tolua_S,"CCPoint","CCPoint","",tolua_collect_CCPoint); +#else + tolua_cclass(tolua_S,"CCPoint","CCPoint","",NULL); +#endif + tolua_beginmodule(tolua_S,"CCPoint"); + tolua_function(tolua_S,"new",tolua_Cocos2d_CCPoint_new00); + tolua_function(tolua_S,"new_local",tolua_Cocos2d_CCPoint_new00_local); + tolua_function(tolua_S,".call",tolua_Cocos2d_CCPoint_new00_local); + tolua_function(tolua_S,"new",tolua_Cocos2d_CCPoint_new01); + tolua_function(tolua_S,"new_local",tolua_Cocos2d_CCPoint_new01_local); + tolua_function(tolua_S,".call",tolua_Cocos2d_CCPoint_new01_local); + tolua_endmodule(tolua_S); + + return 1; +} + +static int tolua_Cocos2d_CCRect_new00(lua_State* tolua_S) +{ + deprecatedClassTip("CCRect"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCRect",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Rect tolua_ret; + rect_to_luaval(tolua_S, tolua_ret); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCRect_new00_local(lua_State* tolua_S) +{ + deprecatedClassTip("CCRect"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCRect",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Rect tolua_ret; + rect_to_luaval(tolua_S, tolua_ret); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCRect_new01(lua_State* tolua_S) +{ + deprecatedClassTip("CCRect"); + + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCRect",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnumber(tolua_S,4,0,&tolua_err) || + !tolua_isnumber(tolua_S,5,0,&tolua_err) || + !tolua_isnoobj(tolua_S,6,&tolua_err) + ) + goto tolua_lerror; + else + { + float x = ((float) tolua_tonumber(tolua_S,2,0)); + float y = ((float) tolua_tonumber(tolua_S,3,0)); + float width = ((float) tolua_tonumber(tolua_S,4,0)); + float height = ((float) tolua_tonumber(tolua_S,5,0)); + Rect tolua_ret(x, y, width, height); + rect_to_luaval(tolua_S, tolua_ret); + } + return 1; +tolua_lerror: + return tolua_Cocos2d_CCRect_new00(tolua_S); +} + + +static int tolua_Cocos2d_CCRect_new01_local(lua_State* tolua_S) +{ + deprecatedClassTip("CCRect"); + + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCRect",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnumber(tolua_S,4,0,&tolua_err) || + !tolua_isnumber(tolua_S,5,0,&tolua_err) || + !tolua_isnoobj(tolua_S,6,&tolua_err) + ) + goto tolua_lerror; + else + { + float x = ((float) tolua_tonumber(tolua_S,2,0)); + float y = ((float) tolua_tonumber(tolua_S,3,0)); + float width = ((float) tolua_tonumber(tolua_S,4,0)); + float height = ((float) tolua_tonumber(tolua_S,5,0)); + Rect tolua_ret(x, y, width, height); + rect_to_luaval(tolua_S, tolua_ret); + } + return 1; +tolua_lerror: + return tolua_Cocos2d_CCRect_new00_local(tolua_S); +} + +static int register_cocos2dx_deprecated_Rect(lua_State* tolua_S) +{ + + tolua_usertype(tolua_S,"CCRect"); + tolua_cclass(tolua_S,"CCRect","CCRect","",NULL); + tolua_beginmodule(tolua_S,"CCRect"); + tolua_function(tolua_S,"new",tolua_Cocos2d_CCRect_new00); + tolua_function(tolua_S,"new_local",tolua_Cocos2d_CCRect_new00_local); + tolua_function(tolua_S,".call",tolua_Cocos2d_CCRect_new00_local); + tolua_function(tolua_S,"new",tolua_Cocos2d_CCRect_new01); + tolua_function(tolua_S,"new_local",tolua_Cocos2d_CCRect_new01_local); + tolua_function(tolua_S,".call",tolua_Cocos2d_CCRect_new01_local); + tolua_endmodule(tolua_S); + + return 1; +} + +static int tolua_Cocos2d_CCSize_new00(lua_State* tolua_S) +{ + deprecatedClassTip("CCSize"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCSize",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Size tolua_ret; + size_to_luaval(tolua_S, tolua_ret); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCSize_new00_local(lua_State* tolua_S) +{ + deprecatedClassTip("CCSize"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCSize",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Size tolua_ret; + size_to_luaval(tolua_S, tolua_ret); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'new'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCSize_new01(lua_State* tolua_S) +{ + deprecatedClassTip("CCSize"); + + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCSize",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else + { + float width = ((float) tolua_tonumber(tolua_S,2,0)); + float height = ((float) tolua_tonumber(tolua_S,3,0)); + Size tolua_ret(width, height); + size_to_luaval(tolua_S, tolua_ret); + } + return 1; +tolua_lerror: + return tolua_Cocos2d_CCSize_new00(tolua_S); +} + + + +static int tolua_Cocos2d_CCSize_new01_local(lua_State* tolua_S) +{ + + deprecatedClassTip("CCSize"); + + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCSize",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else + { + float width = ((float) tolua_tonumber(tolua_S,2,0)); + float height = ((float) tolua_tonumber(tolua_S,3,0)); + Size tolua_ret(width, height); + size_to_luaval(tolua_S, tolua_ret); + } + return 1; +tolua_lerror: + return tolua_Cocos2d_CCSize_new00_local(tolua_S); +} + + +static int register_cocos2dx_deprecated_Size(lua_State* tolua_S) +{ + + tolua_usertype(tolua_S,"CCSize"); + tolua_cclass(tolua_S,"CCSize","CCSize","",NULL); + tolua_beginmodule(tolua_S,"CCSize"); + tolua_function(tolua_S,"new",tolua_Cocos2d_CCSize_new00); + tolua_function(tolua_S,"new_local",tolua_Cocos2d_CCSize_new00_local); + tolua_function(tolua_S,".call",tolua_Cocos2d_CCSize_new00_local); + tolua_function(tolua_S,"new",tolua_Cocos2d_CCSize_new01); + tolua_function(tolua_S,"new_local",tolua_Cocos2d_CCSize_new01_local); + tolua_function(tolua_S,".call",tolua_Cocos2d_CCSize_new01_local); + tolua_endmodule(tolua_S); + + return 1; +} + + +static int tolua_Cocos2d_CCArray_create00(lua_State* tolua_S) +{ + + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + { + Array* tolua_ret = (Array*) Array::create(); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCArray"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_createWithObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"cc.Ref",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Ref* pObject = ((Ref*) tolua_tousertype(tolua_S,2,0)); + { + Array* tolua_ret = (Array*) Array::createWithObject(pObject); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCArray"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithObject'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_createWithArray00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* otherArray = ((Array*) tolua_tousertype(tolua_S,2,0)); + { + Array* tolua_ret = (Array*) Array::createWithArray(otherArray); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCArray"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithArray'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_createWithCapacity00(lua_State* tolua_S) +{ + + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + unsigned int capacity = ((unsigned int) tolua_tonumber(tolua_S,2,0)); + { + Array* tolua_ret = (Array*) Array::createWithCapacity(capacity); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCArray"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithCapacity'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_createWithContentsOfFile00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isstring(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char* pFileName = ((const char*) tolua_tostring(tolua_S,2,0)); + { + Array* tolua_ret = (Array*) Array::createWithContentsOfFile(pFileName); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCArray"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithContentsOfFile'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_count00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'count'", NULL); +#endif + { + unsigned int tolua_ret = (unsigned int) self->count(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'count'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_capacity00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'capacity'", NULL); +#endif + { + unsigned int tolua_ret = (unsigned int) self->capacity(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'capacity'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_indexOfObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"cc.Ref",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Ref* object = ((Ref*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'indexOfObject'", NULL); +#endif + { + unsigned int tolua_ret = (unsigned int) self->getIndexOfObject(object); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'indexOfObject'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_objectAtIndex00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + unsigned int index = ((unsigned int) tolua_tonumber(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'objectAtIndex'", NULL); +#endif + { + Ref* tolua_ret = (Ref*) self->getObjectAtIndex(index); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.Ref"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'objectAtIndex'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_lastObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'lastObject'", NULL); +#endif + { + Ref* tolua_ret = (Ref*) self->getLastObject(); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.Ref"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lastObject'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_randomObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'randomObject'", NULL); +#endif + { + Ref* tolua_ret = (Ref*) self->getRandomObject(); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.Ref"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'randomObject'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_isEqualToArray00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Array* pOtherArray = ((Array*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'isEqualToArray'", NULL); +#endif + { + bool tolua_ret = (bool) self->isEqualToArray(pOtherArray); + tolua_pushboolean(tolua_S,(bool)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'isEqualToArray'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_containsObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"cc.Ref",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Ref* object = ((Ref*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'containsObject'", NULL); +#endif + { + bool tolua_ret = (bool) self->containsObject(object); + tolua_pushboolean(tolua_S,(bool)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'containsObject'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_addObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"cc.Ref",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Ref* object = ((Ref*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'addObject'", NULL); +#endif + { + self->addObject(object); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'addObject'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCArray_addObjectsFromArray00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Array* otherArray = ((Array*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'addObjectsFromArray'", NULL); +#endif + { + self->addObjectsFromArray(otherArray); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'addObjectsFromArray'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_insertObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"cc.Ref",0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Ref* object = ((Ref*) tolua_tousertype(tolua_S,2,0)); + unsigned int index = ((unsigned int) tolua_tonumber(tolua_S,3,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'insertObject'", NULL); +#endif + { + self->insertObject(object,index); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'insertObject'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCArray_removeLastObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isboolean(tolua_S,2,1,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + bool bReleaseObj = ((bool) tolua_toboolean(tolua_S,2,true)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'removeLastObject'", NULL); +#endif + { + self->removeLastObject(bReleaseObj); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'removeLastObject'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_removeObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"cc.Ref",0,&tolua_err) || + !tolua_isboolean(tolua_S,3,1,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Ref* object = ((Ref*) tolua_tousertype(tolua_S,2,0)); + bool bReleaseObj = ((bool) tolua_toboolean(tolua_S,3,true)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'removeObject'", NULL); +#endif + { + self->removeObject(object,bReleaseObj); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'removeObject'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_removeObjectAtIndex00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isboolean(tolua_S,3,1,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + unsigned int index = ((unsigned int) tolua_tonumber(tolua_S,2,0)); + bool bReleaseObj = ((bool) tolua_toboolean(tolua_S,3,true)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'removeObjectAtIndex'", NULL); +#endif + { + self->removeObjectAtIndex(index,bReleaseObj); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'removeObjectAtIndex'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCArray_removeObjectsInArray00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Array* otherArray = ((Array*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'removeObjectsInArray'", NULL); +#endif + { + self->removeObjectsInArray(otherArray); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'removeObjectsInArray'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCArray_removeAllObjects00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'removeAllObjects'", NULL); +#endif + { + self->removeAllObjects(); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'removeAllObjects'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCArray_fastRemoveObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"cc.Ref",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Ref* object = ((Ref*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'fastRemoveObject'", NULL); +#endif + { + self->fastRemoveObject(object); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'fastRemoveObject'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCArray_fastRemoveObjectAtIndex00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + unsigned int index = ((unsigned int) tolua_tonumber(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'fastRemoveObjectAtIndex'", NULL); +#endif + { + self->fastRemoveObjectAtIndex(index); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'fastRemoveObjectAtIndex'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_exchangeObject00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"cc.Ref",0,&tolua_err) || + !tolua_isusertype(tolua_S,3,"cc.Ref",0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + Ref* object1 = ((Ref*) tolua_tousertype(tolua_S,2,0)); + Ref* object2 = ((Ref*) tolua_tousertype(tolua_S,3,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'exchangeObject'", NULL); +#endif + { + self->exchangeObject(object1,object2); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'exchangeObject'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCArray_exchangeObjectAtIndex00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + unsigned int index1 = ((unsigned int) tolua_tonumber(tolua_S,2,0)); + unsigned int index2 = ((unsigned int) tolua_tonumber(tolua_S,3,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'exchangeObjectAtIndex'", NULL); +#endif + { + self->exchangeObjectAtIndex(index1,index2); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'exchangeObjectAtIndex'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_reverseObjects00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'reverseObjects'", NULL); +#endif + { + self->reverseObjects(); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'reverseObjects'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_reduceMemoryFootprint00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'reduceMemoryFootprint'", NULL); +#endif + { + self->reduceMemoryFootprint(); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'reduceMemoryFootprint'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCArray_replaceObjectAtIndex00(lua_State* tolua_S) +{ + deprecatedClassTip("CCArray"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCArray",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isusertype(tolua_S,3,"cc.Ref",0,&tolua_err) || + !tolua_isboolean(tolua_S,4,1,&tolua_err) || + !tolua_isnoobj(tolua_S,5,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* self = (Array*) tolua_tousertype(tolua_S,1,0); + unsigned int uIndex = ((unsigned int) tolua_tonumber(tolua_S,2,0)); + Ref* pObject = ((Ref*) tolua_tousertype(tolua_S,3,0)); + bool bReleaseObject = ((bool) tolua_toboolean(tolua_S,4,true)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'replaceObjectAtIndex'", NULL); +#endif + { + self->replaceObjectAtIndex(uIndex,pObject,bReleaseObject); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'replaceObjectAtIndex'.",&tolua_err); + return 0; +#endif +} + +static int register_cocos2dx_deprecated_Array(lua_State* tolua_S) +{ + tolua_usertype(tolua_S, "CCArray"); + + tolua_cclass(tolua_S,"CCArray","CCArray","CCObject",NULL); + tolua_beginmodule(tolua_S,"CCArray"); + tolua_function(tolua_S,"create",tolua_Cocos2d_CCArray_create00); + tolua_function(tolua_S,"createWithObject",tolua_Cocos2d_CCArray_createWithObject00); + tolua_function(tolua_S,"createWithArray",tolua_Cocos2d_CCArray_createWithArray00); + tolua_function(tolua_S,"createWithCapacity",tolua_Cocos2d_CCArray_createWithCapacity00); + tolua_function(tolua_S,"createWithContentsOfFile",tolua_Cocos2d_CCArray_createWithContentsOfFile00); + tolua_function(tolua_S,"count",tolua_Cocos2d_CCArray_count00); + tolua_function(tolua_S,"capacity",tolua_Cocos2d_CCArray_capacity00); + tolua_function(tolua_S,"indexOfObject",tolua_Cocos2d_CCArray_indexOfObject00); + tolua_function(tolua_S,"objectAtIndex",tolua_Cocos2d_CCArray_objectAtIndex00); + tolua_function(tolua_S,"lastObject",tolua_Cocos2d_CCArray_lastObject00); + tolua_function(tolua_S,"randomObject",tolua_Cocos2d_CCArray_randomObject00); + tolua_function(tolua_S,"isEqualToArray",tolua_Cocos2d_CCArray_isEqualToArray00); + tolua_function(tolua_S,"containsObject",tolua_Cocos2d_CCArray_containsObject00); + tolua_function(tolua_S,"addObject",tolua_Cocos2d_CCArray_addObject00); + tolua_function(tolua_S,"addObjectsFromArray",tolua_Cocos2d_CCArray_addObjectsFromArray00); + tolua_function(tolua_S,"insertObject",tolua_Cocos2d_CCArray_insertObject00); + tolua_function(tolua_S,"removeLastObject",tolua_Cocos2d_CCArray_removeLastObject00); + tolua_function(tolua_S,"removeObject",tolua_Cocos2d_CCArray_removeObject00); + tolua_function(tolua_S,"removeObjectAtIndex",tolua_Cocos2d_CCArray_removeObjectAtIndex00); + tolua_function(tolua_S,"removeObjectsInArray",tolua_Cocos2d_CCArray_removeObjectsInArray00); + tolua_function(tolua_S,"removeAllObjects",tolua_Cocos2d_CCArray_removeAllObjects00); + tolua_function(tolua_S,"fastRemoveObject",tolua_Cocos2d_CCArray_fastRemoveObject00); + tolua_function(tolua_S,"fastRemoveObjectAtIndex",tolua_Cocos2d_CCArray_fastRemoveObjectAtIndex00); + tolua_function(tolua_S,"exchangeObject",tolua_Cocos2d_CCArray_exchangeObject00); + tolua_function(tolua_S,"exchangeObjectAtIndex",tolua_Cocos2d_CCArray_exchangeObjectAtIndex00); + tolua_function(tolua_S,"reverseObjects",tolua_Cocos2d_CCArray_reverseObjects00); + tolua_function(tolua_S,"reduceMemoryFootprint",tolua_Cocos2d_CCArray_reduceMemoryFootprint00); + tolua_function(tolua_S,"replaceObjectAtIndex",tolua_Cocos2d_CCArray_replaceObjectAtIndex00); + tolua_endmodule(tolua_S); + + return 1; +} + +static int tolua_cocos2d_kmGLPushMatrix00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isnoobj(tolua_S,1,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + kmGLPushMatrix(); + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'kmGLPushMatrix'.",&tolua_err); + return 0; +#endif +} + +static int tolua_cocos2d_kmGLTranslatef00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isnumber(tolua_S,1,0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + float x = ((float) tolua_tonumber(tolua_S,1,0)); + float y = ((float) tolua_tonumber(tolua_S,2,0)); + float z = ((float) tolua_tonumber(tolua_S,3,0)); + { + kmGLTranslatef(x,y,z); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'kmGLTranslatef'.",&tolua_err); + return 0; +#endif +} + +static int tolua_cocos2d_kmGLPopMatrix00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isnoobj(tolua_S,1,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + { + kmGLPopMatrix(); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'kmGLPopMatrix'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_intValue00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const CCString",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const String* self = (const String*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'intValue'", NULL); +#endif + { + int tolua_ret = (int) self->intValue(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'intValue'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_uintValue00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const CCString",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const String* self = (const String*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'uintValue'", NULL); +#endif + { + unsigned int tolua_ret = (unsigned int) self->uintValue(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'uintValue'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_floatValue00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const CCString",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const String* self = (const String*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'floatValue'", NULL); +#endif + { + float tolua_ret = (float) self->floatValue(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'floatValue'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_doubleValue00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const CCString",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const String* self = (const String*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'doubleValue'", NULL); +#endif + { + double tolua_ret = (double) self->doubleValue(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'doubleValue'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_boolValue00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const CCString",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const String* self = (const String*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'boolValue'", NULL); +#endif + { + bool tolua_ret = (bool) self->boolValue(); + tolua_pushboolean(tolua_S,(bool)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'boolValue'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_getCString00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const CCString",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const String* self = (const String*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'getCString'", NULL); +#endif + { + const char* tolua_ret = (const char*) self->getCString(); + tolua_pushstring(tolua_S,(const char*)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'getCString'.",&tolua_err); + return 0; +#endif +} + + + +static int tolua_Cocos2d_CCString_length00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const CCString",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const String* self = (const String*) tolua_tousertype(tolua_S,1,0); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'length'", NULL); +#endif + { + unsigned int tolua_ret = (unsigned int) self->length(); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'length'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_compare00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"const CCString",0,&tolua_err) || + !tolua_isstring(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const String* self = (const String*) tolua_tousertype(tolua_S,1,0); + const char* str = ((const char*) tolua_tostring(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'compare'", NULL); +#endif + { + int tolua_ret = (int) self->compare(str); + tolua_pushnumber(tolua_S,(lua_Number)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'compare'.",&tolua_err); + return 0; +#endif +} + + + +static int tolua_Cocos2d_CCString_isEqual00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"CCString",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"const CCObject",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + String* self = (String*) tolua_tousertype(tolua_S,1,0); + const Ref* pObject = ((const Ref*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'isEqual'", NULL); +#endif + { + bool tolua_ret = (bool) self->isEqual(pObject); + tolua_pushboolean(tolua_S,(bool)tolua_ret); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'isEqual'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_create00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCString",0,&tolua_err) || + !tolua_isstring(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char* pStr = ((const char*) tolua_tostring(tolua_S,2,0)); + { + String* tolua_ret = (String*) String::create(pStr); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCString"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_createWithData00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCString",0,&tolua_err) || + !tolua_isstring(tolua_S,2,0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + unsigned char* pData = ((unsigned char*) tolua_tostring(tolua_S,2,0)); + unsigned long nLen = ((unsigned long) tolua_tonumber(tolua_S,3,0)); + { + String* tolua_ret = (String*) String::createWithData(pData,nLen); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCString"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithData'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_Cocos2d_CCString_createWithContentsOfFile00(lua_State* tolua_S) +{ + deprecatedClassTip("CCString"); + +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"CCString",0,&tolua_err) || + !tolua_isstring(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + const char* pszFileName = ((const char*) tolua_tostring(tolua_S,2,0)); + { + String* tolua_ret = (String*) String::createWithContentsOfFile(pszFileName); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"CCString"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithContentsOfFile'.",&tolua_err); + return 0; +#endif +} + +static int register_cocos2dx_deprecated_String(lua_State* tolua_S) +{ + tolua_usertype(tolua_S, "CCString"); + tolua_cclass(tolua_S,"CCString","CCString","cc.Ref",NULL); + tolua_beginmodule(tolua_S,"CCString"); + tolua_function(tolua_S,"intValue",tolua_Cocos2d_CCString_intValue00); + tolua_function(tolua_S,"uintValue",tolua_Cocos2d_CCString_uintValue00); + tolua_function(tolua_S,"floatValue",tolua_Cocos2d_CCString_floatValue00); + tolua_function(tolua_S,"doubleValue",tolua_Cocos2d_CCString_doubleValue00); + tolua_function(tolua_S,"boolValue",tolua_Cocos2d_CCString_boolValue00); + tolua_function(tolua_S,"getCString",tolua_Cocos2d_CCString_getCString00); + tolua_function(tolua_S,"length",tolua_Cocos2d_CCString_length00); + tolua_function(tolua_S,"compare",tolua_Cocos2d_CCString_compare00); + tolua_function(tolua_S,"isEqual",tolua_Cocos2d_CCString_isEqual00); + tolua_function(tolua_S,"create",tolua_Cocos2d_CCString_create00); + tolua_function(tolua_S,"createWithData",tolua_Cocos2d_CCString_createWithData00); + tolua_function(tolua_S,"createWithContentsOfFile",tolua_Cocos2d_CCString_createWithContentsOfFile00); + tolua_endmodule(tolua_S); + return 1; +} + + +int register_all_cocos2dx_deprecated(lua_State* tolua_S) +{ + tolua_open(tolua_S); + + tolua_module(tolua_S,NULL,0); + tolua_beginmodule(tolua_S,NULL); + register_cocos2dx_deprecated_Point(tolua_S); + register_cocos2dx_deprecated_Rect(tolua_S); + register_cocos2dx_deprecated_Size(tolua_S); + register_cocos2dx_deprecated_Array(tolua_S); + register_cocos2dx_deprecated_String(tolua_S); + tolua_function(tolua_S,"kmGLPushMatrix",tolua_cocos2d_kmGLPushMatrix00); + tolua_function(tolua_S,"kmGLTranslatef",tolua_cocos2d_kmGLTranslatef00); + tolua_function(tolua_S,"kmGLPopMatrix",tolua_cocos2d_kmGLPopMatrix00); + tolua_endmodule(tolua_S); + + return 0; +} + +extern int lua_cocos2dx_Animation_createWithSpriteFrames(lua_State* tolua_S); + +static int tolua_cocos2d_Animation_createWithSpriteFrames_deprecated00(lua_State* tolua_S) +{ + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"Animation",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnumber(tolua_S,3,0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else + { + Array* arrayOfSpriteFrameNames = ((Array*) tolua_tousertype(tolua_S,2,0)); + Vector vec; + array_to_vector_t_deprecated(*arrayOfSpriteFrameNames, vec); + float delay = ((float) tolua_tonumber(tolua_S,3,0)); + cocos2d::Animation* tolua_ret = (cocos2d::Animation*) cocos2d::Animation::createWithSpriteFrames(vec,delay); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.Animation"); + + } + return 1; +tolua_lerror: + return lua_cocos2dx_Animation_createWithSpriteFrames(tolua_S); +} + +static int tolua_cocos2d_Animation_createWithSpriteFrames_deprecated01(lua_State* tolua_S) +{ + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"Animation",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else + { + Array* arrayOfSpriteFrameNames = ((Array*) tolua_tousertype(tolua_S,2,0)); + Vector vec; + array_to_vector_t_deprecated(*arrayOfSpriteFrameNames, vec); + cocos2d::Animation* tolua_ret = (cocos2d::Animation*) cocos2d::Animation::createWithSpriteFrames(vec); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.Animation"); + + } + return 1; +tolua_lerror: + return tolua_cocos2d_Animation_createWithSpriteFrames_deprecated00(tolua_S); +} + +static void extendAnimationDeprecated(lua_State* tolua_S) +{ + lua_pushstring(tolua_S,"Animation"); + lua_rawget(tolua_S,LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"createWithSpriteFrames"); + lua_pushcfunction(tolua_S,tolua_cocos2d_Animation_createWithSpriteFrames_deprecated00); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"createWithSpriteFrames"); + lua_pushcfunction(tolua_S,tolua_cocos2d_Animation_createWithSpriteFrames_deprecated01); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); +} + +static int tolua_cocos2d_Sequence_createWithTwoActions(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"Sequence",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"FiniteTimeAction",0,&tolua_err) || + !tolua_isusertype(tolua_S,3,"FiniteTimeAction",0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + FiniteTimeAction* pActionOne = ((FiniteTimeAction*) tolua_tousertype(tolua_S,2,0)); + FiniteTimeAction* pActionTwo = ((FiniteTimeAction*) tolua_tousertype(tolua_S,3,0)); + { + Sequence* tolua_ret = (Sequence*) Sequence::createWithTwoActions(pActionOne,pActionTwo); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.Sequence"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithTwoActions'.",&tolua_err); + return 0; +#endif +} + +extern int tolua_cocos2d_Sequence_create(lua_State* tolua_S); +static int tolua_Cocos2d_Sequence_create_deprecated00(lua_State* tolua_S) +{ + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"Sequence",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else + { + Array* actions = ((Array*) tolua_tousertype(tolua_S,2,0)); + Vector vec; + array_to_vector_t_deprecated(*actions, vec); + Sequence* tolua_ret = (Sequence*) Sequence::create(vec); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.Sequence"); + } + return 1; +tolua_lerror: + return tolua_cocos2d_Sequence_create(tolua_S); +} + +static int extendSequenceDeprecated(lua_State* tolua_S) +{ + lua_pushstring(tolua_S,"Sequence"); + lua_rawget(tolua_S,LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + tolua_function(tolua_S, "createWithTwoActions",tolua_cocos2d_Sequence_createWithTwoActions); + tolua_function(tolua_S, "create", tolua_Cocos2d_Sequence_create_deprecated00); + } + lua_pop(tolua_S, 1); + + return 1; +} + +#ifdef __cplusplus +extern "C" { +#endif +extern int tolua_bnd_cast(lua_State* tolua_S); +#ifdef __cplusplus +} +#endif + +static int tolua_bnd_cast_deprecated00(lua_State* tolua_S) +{ + void* v = nullptr; + std::string strValue = ""; + strValue = tolua_tostring(tolua_S,2,NULL); + int pos = strValue.find("CC"); + if (pos == 0 && + std::string::npos == strValue.find("CCBAnimationManager") && + std::string::npos == strValue.find("CCString") && + std::string::npos == strValue.find("CCPoint") && + std::string::npos == strValue.find("CCRect") && + std::string::npos == strValue.find("CCSize") && + std::string::npos == strValue.find("CCArray")) + { + strValue = strValue.substr(2); + if (lua_islightuserdata(tolua_S, 1)) { + v = tolua_touserdata(tolua_S, 1, NULL); + } else { + v = tolua_tousertype(tolua_S, 1, 0); + }; + if (v && !strValue.empty()) + tolua_pushusertype(tolua_S,v,strValue.c_str()); + else + lua_pushnil(tolua_S); + return 1; + } + else + { + std::string castName = tolua_tostring(tolua_S,2,NULL); + auto iter = g_typeCast.find(castName); + if (iter != g_typeCast.end() ) + { + CCLOG("Cast name %s doesn't include modular name which it belongs to,please add the modular name",iter->first.c_str()); + tolua_pushstring(tolua_S, iter->second.c_str()); + lua_insert(tolua_S, 2); + lua_pop(tolua_S, 1); + } + return tolua_bnd_cast(tolua_S); + } +} + +static int extendToluaDeprecated(lua_State* tolua_S) +{ + lua_getglobal(tolua_S, "_G"); + if (lua_istable(tolua_S,-1))//stack:...,_G, + { + lua_pushstring(tolua_S,"tolua");//stack:_G,keyValue + lua_gettable(tolua_S, -2);//stack:_G,toluaModule + if (lua_istable(tolua_S,-1)) + { + tolua_function(tolua_S, "cast", tolua_bnd_cast_deprecated00); + } + lua_pop(tolua_S, 1);//statck:_G + } + lua_pop(tolua_S, 1);//statck:... + + return 1; +} + +static int tolua_cocos2d_Spawn_createWithTwoActions_deprcated00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"Spawn",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"FiniteTimeAction",0,&tolua_err) || + !tolua_isusertype(tolua_S,3,"FiniteTimeAction",0,&tolua_err) || + !tolua_isnoobj(tolua_S,4,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + FiniteTimeAction* pAction1 = ((FiniteTimeAction*) tolua_tousertype(tolua_S,2,0)); + FiniteTimeAction* pAction2 = ((FiniteTimeAction*) tolua_tousertype(tolua_S,3,0)); + { + Spawn* tolua_ret = (Spawn*) Spawn::createWithTwoActions(pAction1,pAction2); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.Spawn"); + } + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithTwoActions'.",&tolua_err); + return 0; +#endif +} + +static int extendSpawnDeprecated(lua_State* tolua_S) +{ + lua_pushstring(tolua_S,"Spawn"); + lua_rawget(tolua_S,LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + tolua_function(tolua_S, "createWithTwoActions", tolua_cocos2d_Spawn_createWithTwoActions_deprcated00); + } + lua_pop(tolua_S, 1); + return 1; +} + + +static int tolua_cocos2d_Menu_createWithArray00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"Menu",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* arrayOfItems = ((Array*) tolua_tousertype(tolua_S,2,0)); + Vector vec; + array_to_vector_t_deprecated(*arrayOfItems, vec); + Menu* tolua_ret = (Menu*) Menu::createWithArray(vec); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.Menu"); + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithArray'.",&tolua_err); + return 0; +#endif +} + +static int tolua_cocos2d_Menu_alignItemsInColumnsWithArray00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"Menu",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Menu* self = (Menu*) tolua_tousertype(tolua_S,1,0); + Array* rows = ((Array*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'alignItemsInColumnsWithArray'", NULL); +#endif + ValueVector valueVector; + array_to_valuevector_deprecated(*rows, valueVector); + self->alignItemsInColumnsWithArray(valueVector); + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'alignItemsInColumnsWithArray'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_cocos2d_Menu_alignItemsInRowsWithArray00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"Menu",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Menu* self = (Menu*) tolua_tousertype(tolua_S,1,0); + Array* columns = ((Array*) tolua_tousertype(tolua_S,2,0)); +#ifndef TOLUA_RELEASE + if (!self) tolua_error(tolua_S,"invalid 'self' in function 'alignItemsInRowsWithArray'", NULL); +#endif + ValueVector valueVector; + array_to_valuevector_deprecated(*columns, valueVector); + self->alignItemsInRowsWithArray(valueVector); + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'alignItemsInRowsWithArray'.",&tolua_err); + return 0; +#endif +} + +static int extendMenuDeprecated(lua_State* tolua_S) +{ + lua_pushstring(tolua_S,"Menu"); + lua_rawget(tolua_S,LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + tolua_function(tolua_S, "createWithArray", tolua_cocos2d_Menu_createWithArray00); + tolua_function(tolua_S, "alignItemsInColumnsWithArray", tolua_cocos2d_Menu_alignItemsInColumnsWithArray00); + tolua_function(tolua_S, "alignItemsInRowsWithArray", tolua_cocos2d_Menu_alignItemsInRowsWithArray00); + } + lua_pop(tolua_S, 1); + return 1; +} + +static int tolua_cocos2d_LayerMultiplex_createWithArray00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertable(tolua_S,1,"LayerMultiplex",0,&tolua_err) || + !tolua_isusertype(tolua_S,2,"CCArray",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + Array* arrayOfLayers = ((Array*) tolua_tousertype(tolua_S,2,0)); + Vector vec; + array_to_vector_t_deprecated(*arrayOfLayers, vec); + LayerMultiplex* tolua_ret = (LayerMultiplex*) LayerMultiplex::createWithArray(vec); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.LayerMultiplex"); + + } + return 1; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createWithArray'.",&tolua_err); + return 0; +#endif +} + +static int extendLayerMultiplexDeprecated(lua_State* tolua_S) +{ + lua_pushstring(tolua_S,"LayerMultiplex"); + lua_rawget(tolua_S,LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + tolua_function(tolua_S, "createWithArray", tolua_cocos2d_LayerMultiplex_createWithArray00); + } + lua_pop(tolua_S, 1); + return 1; +} + + +int register_all_cocos2dx_manual_deprecated(lua_State* tolua_S) +{ + if (NULL == tolua_S) + return 0; + + extendAnimationDeprecated(tolua_S); + extendSequenceDeprecated(tolua_S); + extendToluaDeprecated(tolua_S); + extendMenuDeprecated(tolua_S); + extendLayerMultiplexDeprecated(tolua_S); + return 0; +} diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_deprecated.h b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_deprecated.h new file mode 100644 index 0000000000..7db6757ee2 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_deprecated.h @@ -0,0 +1,38 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_LUA_COCOS2DX_DEPRECATED_H +#define COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_LUA_COCOS2DX_DEPRECATED_H + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +TOLUA_API int register_all_cocos2dx_deprecated(lua_State* tolua_S); +TOLUA_API int register_all_cocos2dx_manual_deprecated(lua_State* tolua_S); + +#endif // #ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_LUA_COCOS2DX_DEPRECATED_H diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_extension_manual.cpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_extension_manual.cpp new file mode 100644 index 0000000000..4becfde1ad --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_extension_manual.cpp @@ -0,0 +1,1469 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#include "lua_cocos2dx_extension_manual.h" +#include "cocos2d.h" +#include "tolua_fix.h" +#include "LuaBasicConversions.h" +#include "CCLuaValue.h" +#include "cocos-ext.h" +#include "CCBProxy.h" +#include "cocostudio/CocoStudio.h" + +USING_NS_CC; +USING_NS_CC_EXT; +using namespace cocostudio; + +class LuaScrollViewDelegate:public Ref, public ScrollViewDelegate +{ +public: + virtual ~LuaScrollViewDelegate() + {} + + virtual void scrollViewDidScroll(ScrollView* view) override + { + if (nullptr != view) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)view, ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL); + if (0 != handler) + { + CommonScriptData data(handler,""); + ScriptEvent event(kCommonEvent,(void*)&data); + LuaEngine::getInstance()->sendEvent(&event); + } + + } + } + + virtual void scrollViewDidZoom(ScrollView* view) override + { + if (nullptr != view) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)view, ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM); + if (0 != handler) + { + CommonScriptData data(handler,""); + ScriptEvent event(kCommonEvent,(void*)&data); + LuaEngine::getInstance()->sendEvent(&event); + } + } + } +}; + +static int tolua_cocos2dx_ScrollView_setDelegate(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + int argc = 0; + ScrollView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.ScrollView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (ScrollView*) tolua_tousertype(tolua_S,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2dx_ScrollView_setDelegate'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (0 == argc) + { + LuaScrollViewDelegate* delegate = new LuaScrollViewDelegate(); + if (nullptr == delegate) + return 0; + + self->setUserObject(delegate); + self->setDelegate(delegate); + + delegate->release(); + + return 0; + } + + CCLOG("'setDelegate' function of ScrollView wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'setDelegate'.",&tolua_err); + return 0; +#endif +} + +static int tolua_cocos2d_ScrollView_registerScriptHandler(lua_State* tolua_S) +{ + if (NULL == tolua_S) + return 0; + + int argc = 0; + ScrollView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.ScrollView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_ScrollView_registerScriptHandler'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(tolua_S) - 1; + if (2 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) || + !tolua_isnumber(tolua_S, 3, 0, &tolua_err) ) + { + goto tolua_lerror; + } +#endif + LUA_FUNCTION handler = ( toluafix_ref_function(tolua_S,2,0)); + ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType) ((int)tolua_tonumber(tolua_S,3,0) + (int)ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, handlerType); + return 0; + } + + CCLOG("'registerScriptHandler' function of ScrollView has wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'registerScriptHandler'.",&tolua_err); + return 0; +#endif +} + +static int tolua_cocos2d_ScrollView_unregisterScriptHandler(lua_State* tolua_S) +{ + if (NULL == tolua_S) + return 0; + + int argc = 0; + ScrollView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.ScrollView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_ScrollView_unregisterScriptHandler'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isnumber(tolua_S, 2, 0, &tolua_err)) + goto tolua_lerror; +#endif + ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType) ((int)tolua_tonumber(tolua_S,2,0) + (int)ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL); + ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)self, handlerType); + return 0; + } + + CCLOG("'unregisterScriptHandler' function of ScrollView has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'unregisterScriptHandler'.",&tolua_err); + return 0; +#endif +} + +static void extendScrollView(lua_State* tolua_S) +{ + lua_pushstring(tolua_S, "cc.ScrollView"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"setDelegate"); + lua_pushcfunction(tolua_S,tolua_cocos2dx_ScrollView_setDelegate ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"registerScriptHandler"); + lua_pushcfunction(tolua_S,tolua_cocos2d_ScrollView_registerScriptHandler ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"unregisterScriptHandler"); + lua_pushcfunction(tolua_S,tolua_cocos2d_ScrollView_unregisterScriptHandler ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); +} + + +static int tolua_cocos2d_Control_registerControlEventHandler(lua_State* tolua_S) +{ + if (NULL == tolua_S) + return 0; + + int argc = 0; + Control* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.Control",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_Control_registerControlEventHandler'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (2 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) || + !tolua_isnumber(tolua_S, 3, 0, &tolua_err) ) + { + goto tolua_lerror; + } +#endif + LUA_FUNCTION handler = ( toluafix_ref_function(tolua_S,2,0)); + int controlevent = (int)tolua_tonumber(tolua_S,3,0); + for (int i = 0; i < kControlEventTotalNumber; i++) + { + if ((controlevent & (1 << i))) + { + ScriptHandlerMgr::HandlerType handlerevent = ScriptHandlerMgr::HandlerType((int)ScriptHandlerMgr::HandlerType::CONTROL_TOUCH_DOWN + i); + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, handlerevent); + } + } + return 0; + } + + CCLOG("'registerControlEventHandler' function of Control has wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'registerControlEventHandler'.",&tolua_err); + return 0; +#endif +} + +static int tolua_cocos2d_control_unregisterControlEventHandler(lua_State* tolua_S) +{ + if (NULL == tolua_S) + return 0; + + int argc = 0; + Control* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.Control",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_control_unregisterControlEventHandler'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isnumber(tolua_S, 2, 0, &tolua_err)) + goto tolua_lerror; +#endif + int controlevent = (int)tolua_tonumber(tolua_S,2,0); + for (int i = 0; i < kControlEventTotalNumber; i++) + { + if ((controlevent & (1 << i))) + { + ScriptHandlerMgr::HandlerType handlerevent = ScriptHandlerMgr::HandlerType((int)ScriptHandlerMgr::HandlerType::CONTROL_TOUCH_DOWN + i); + ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)self, handlerevent); + break; + } + } + return 0; + } + + CCLOG("'unregisterControlEventHandler' function of Control has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'unregisterControlEventHandler'.",&tolua_err); + return 0; +#endif +} + +static void extendControl(lua_State* tolua_S) +{ + lua_pushstring(tolua_S, "cc.Control"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"registerControlEventHandler"); + lua_pushcfunction(tolua_S,tolua_cocos2d_Control_registerControlEventHandler ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"unregisterControlEventHandler"); + lua_pushcfunction(tolua_S,tolua_cocos2d_control_unregisterControlEventHandler ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); +} + +static int tolua_cocos2d_EditBox_registerScriptEditBoxHandler(lua_State* tolua_S) +{ + if (NULL == tolua_S) + return 0; + + int argc = 0; + EditBox* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.EditBox",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_EditBox_registerScriptEditBoxHandler'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + LUA_FUNCTION handler = ( toluafix_ref_function(tolua_S,2,0)); + self->registerScriptEditBoxHandler(handler); + return 0; + } + + CCLOG("'registerScriptEditBoxHandler' function of EditBox has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'registerScriptEditBoxHandler'.",&tolua_err); + return 0; +#endif + +} + +static int tolua_cocos2d_EditBox_unregisterScriptEditBoxHandler(lua_State* tolua_S) +{ + + if (NULL == tolua_S) + return 0; + + int argc = 0; + EditBox* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.EditBox",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_EditBox_unregisterScriptEditBoxHandler'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (0 == argc) + { + self->unregisterScriptEditBoxHandler(); + return 0; + } + + CCLOG("'unregisterScriptEditBoxHandler' function of EditBox has wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'unregisterScriptEditBoxHandler'.",&tolua_err); + return 0; +#endif +} + +static void extendEditBox(lua_State* tolua_S) +{ + lua_pushstring(tolua_S, "cc.EditBox"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"registerScriptEditBoxHandler"); + lua_pushcfunction(tolua_S,tolua_cocos2d_EditBox_registerScriptEditBoxHandler ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"unregisterScriptEditBoxHandler"); + lua_pushcfunction(tolua_S,tolua_cocos2d_EditBox_unregisterScriptEditBoxHandler ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); +} + +static int tolua_cocos2d_CCBProxy_create(lua_State* tolua_S) +{ + if (NULL == tolua_S) + return 0; + + int argc = 0; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertable(tolua_S,1,"cc.CCBProxy",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (0 == argc) + { + CCBProxy* tolua_ret = (CCBProxy*)CCBProxy::create(); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int *pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.CCBProxy"); + return 1; + } + + CCLOG("'create' function of CCBProxy has wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_cocos2d_CCBProxy_createCCBReader(lua_State* tolua_S) +{ + if (NULL == tolua_S) + return 0; + + int argc = 0; + CCBProxy* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.CCBProxy",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_CCBProxy_createCCBReader'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (0 == argc) + { + CCBReader* tolua_ret = (CCBReader*) self->createCCBReader(); + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, nID, pLuaID, (void*)tolua_ret,"cc.CCBReader"); + return 1; + } + + CCLOG("'createCCBReader' function of CCBProxy has wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'createCCBReader'.",&tolua_err); + return 0; +#endif +} + +static int tolua_cocos2d_CCBProxy_readCCBFromFile(lua_State* tolua_S) +{ + if (NULL == tolua_S) + return 0; + + int argc = 0; + CCBProxy* self = nullptr; + const char* ccbFilePath = nullptr; + CCBReader* ccbReader = nullptr; + bool setOwner = false; + Node* tolua_ret = nullptr; + int ID = 0; + int* luaID = nullptr; + + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.CCBProxy",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_CCBProxy_readCCBFromFile'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (2 == argc || 3 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(tolua_S, 2, 0, &tolua_err)|| + !tolua_isusertype(tolua_S,3,"cc.CCBReader",0,&tolua_err)|| + !tolua_isboolean(tolua_S,4,1,&tolua_err ) + ) + goto tolua_lerror; +#endif + ccbFilePath = ((const char*) tolua_tostring(tolua_S,2,0)); + ccbReader = ((CCBReader*) tolua_tousertype(tolua_S,3,0)); + setOwner = (bool) tolua_toboolean(tolua_S,4,-1); + tolua_ret = (Node*) self->readCCBFromFile(ccbFilePath, ccbReader, setOwner); + ID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + luaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, ID, luaID, (void*)tolua_ret,"cc.Node"); + return 1; + } + + CCLOG("'readCCBFromFile' function of CCBProxy has wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'readCCBFromFile'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_cocos2d_CCBProxy_getNodeTypeName(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + int argc = 0; + CCBProxy* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.CCBProxy",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_CCBProxy_getNodeTypeName'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.Node",0,&tolua_err)) goto tolua_lerror; +#endif + + Node* node = static_cast(tolua_tousertype(tolua_S,2,0)); + const char* tolua_ret = (const char*)self->getNodeTypeName(node); + tolua_pushstring(tolua_S,(const char*)tolua_ret); + return 1; + } + + CCLOG("'getNodeTypeName' function of CCBProxy has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'getNodeTypeName'.",&tolua_err); + return 0; +#endif +} + +static int tolua_cocos2d_CCBProxy_setCallback(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + int argc = 0; + CCBProxy* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.CCBProxy",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_CCBProxy_setCallback'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if ( argc >= 2 && argc <= 3 ) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,2,"cc.Node",0,&tolua_err) || + !toluafix_isfunction(tolua_S, 3, "LUA_FUNCTION", 0, &tolua_err) || + !tolua_isnumber(tolua_S, 4, 1, &tolua_err) + ) + goto tolua_lerror; +#endif + + Node* node = ((Node*)tolua_tousertype(tolua_S,2,0)); + LUA_FUNCTION funID = ( toluafix_ref_function(tolua_S,3,0)); + int controlEvents = (int)tolua_tonumber(tolua_S, 4, 1); + self->setCallback(node, funID, controlEvents); + return 0; + } + + CCLOG("'setCallback' function of CCBProxy has wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'setCallback'.",&tolua_err); + return 0; +#endif +} + +int register_cocos2dx_extension_CCBProxy(lua_State* tolua_S) +{ + tolua_module(tolua_S,"cc",0); + tolua_beginmodule(tolua_S,"cc"); + tolua_usertype(tolua_S,"cc.CCBProxy"); + tolua_cclass(tolua_S,"CCBProxy","cc.CCBProxy","cc.Layer",NULL); + tolua_beginmodule(tolua_S,"CCBProxy"); + tolua_function(tolua_S, "create", tolua_cocos2d_CCBProxy_create); + tolua_function(tolua_S, "createCCBReader", tolua_cocos2d_CCBProxy_createCCBReader); + tolua_function(tolua_S, "readCCBFromFile", tolua_cocos2d_CCBProxy_readCCBFromFile); + tolua_function(tolua_S, "getNodeTypeName", tolua_cocos2d_CCBProxy_getNodeTypeName); + tolua_function(tolua_S, "setCallback", tolua_cocos2d_CCBProxy_setCallback); + tolua_endmodule(tolua_S); + tolua_endmodule(tolua_S); + + std::string typeName = typeid(CCBProxy).name(); + g_luaType[typeName] = "cc.CCBProxy"; + return 1; +} + +static int tolua_cocos2d_CCBReader_load(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + int argc = 0; + CCBReader* self = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.CCBReader",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_CCBReader_load'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc >= 1 && argc <= 3) + { + const char* fileName = nullptr; + std::string fileName_tmp = ""; + ok &= luaval_to_std_string(tolua_S, 2, &fileName_tmp); + fileName = fileName_tmp.c_str(); + if (!ok) + return 0; + + if (1 == argc) + { + Node* tolua_ret = (Node*) self->readNodeGraphFromFile(fileName); + int ID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* luaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, ID, luaID, (void*)tolua_ret,"cc.Node"); + return 1; + } + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S, 3, "cc.Ref", 0, &tolua_err)) + goto tolua_lerror; +#endif + Ref* owner = static_cast(tolua_tousertype(tolua_S, 3, 0)); + //In lua owner always define in lua script by table, so owner is always nullptr + if (2 == argc) + { + Node* tolua_ret = (Node*) self->readNodeGraphFromFile(fileName,owner); + int ID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* luaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, ID, luaID, (void*)tolua_ret,"cc.Node"); + return 1; + } + + Size size; + ok &= luaval_to_size(tolua_S, 4, &size); + if (!ok) + return 0; + + Node* tolua_ret = (Node*) self->readNodeGraphFromFile(fileName,owner,size); + int ID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* luaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, ID, luaID, (void*)tolua_ret,"cc.Node"); + return 1; + + } + + CCLOG("'load' function of CCBReader has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'load'.",&tolua_err); + return 0; +#endif +} + +static void extendCCBReader(lua_State* tolua_S) +{ + lua_pushstring(tolua_S, "cc.CCBReader"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"load"); + lua_pushcfunction(tolua_S,tolua_cocos2d_CCBReader_load ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); +} + + +static int tolua_cocos2d_CCBAnimationManager_setCallFuncForLuaCallbackNamed(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + int argc = 0; + CCBAnimationManager* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,"cc.CCBAnimationManager",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2d_CCBAnimationManager_setCallFuncForLuaCallbackNamed'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (2 == argc) + { + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,2, "cc.CallFunc", 0, &tolua_err) || + !tolua_isstring(tolua_S, 3, 0, &tolua_err) ) + goto tolua_lerror; +#endif + + CallFunc* pCallFunc = static_cast(tolua_tousertype(tolua_S,2,0)); + const char* keyframeCallback = ((const char*) tolua_tostring(tolua_S,3,0)); + std::string strKey = ""; + if (NULL != keyframeCallback) { + strKey = keyframeCallback; + } + self->setCallFunc(pCallFunc, strKey); + + return 0; + } + + CCLOG("'setCallFuncForLuaCallbackNamed' function of CCBAnimationManager has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'setCallFuncForLuaCallbackNamed'.",&tolua_err); + return 0; +#endif +} + +static void extendCCBAnimationManager(lua_State* tolua_S) +{ + lua_pushstring(tolua_S, "cc.CCBAnimationManager"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"setCallFuncForLuaCallbackNamed"); + lua_pushcfunction(tolua_S,tolua_cocos2d_CCBAnimationManager_setCallFuncForLuaCallbackNamed ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); +} + +class LuaAssetsManagerDelegateProtocol:public Ref, public AssetsManagerDelegateProtocol +{ +public: + virtual ~LuaAssetsManagerDelegateProtocol() + {} + + virtual void onProgress(int percent) override + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS); + if (0 != handler) + { + LuaAssetsManagerEventData eventData(percent); + BasicScriptData data((void*)this,&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS, (void*)&data); + } + } + + virtual void onSuccess() override + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS); + if (0 != handler) + { + LuaAssetsManagerEventData eventData; + BasicScriptData data((void*)this,&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_SUCCESS, (void*)&data); + } + } + + virtual void onError(AssetsManager::ErrorCode errorCode) override + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR); + if (0 != handler) + { + LuaAssetsManagerEventData eventData((int)errorCode); + BasicScriptData data((void*)this,&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::ASSETSMANAGER_ERROR, (void*)&data); + } + } +}; + +static int lua_cocos2dx_AssetsManager_setDelegate(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + AssetsManager* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.AssetsManager",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (AssetsManager*) tolua_tousertype(L,1,0); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_AssetsManager_setDelegate'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (2 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L, 2, "LUA_FUNCTION", 0, &tolua_err) || + !tolua_isnumber(L, 3, 0, &tolua_err) ) + { + goto tolua_lerror; + } +#endif + LuaAssetsManagerDelegateProtocol* delegate = dynamic_cast( self->getDelegate()); + if (nullptr == delegate) + { + delegate = new LuaAssetsManagerDelegateProtocol(); + if (nullptr == delegate) + return 0; + + self->setUserObject(delegate); + self->setDelegate(delegate); + delegate->release(); + } + + LUA_FUNCTION handler = toluafix_ref_function(L, 2, 0); + ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType) ((int)tolua_tonumber(L,3,0) + (int)ScriptHandlerMgr::HandlerType::ASSETSMANAGER_PROGRESS); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)delegate, handler, handlerType); + return 0; + } + + CCLOG("'setDelegate' function of AssetsManager has wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'setDelegate'.",&tolua_err); + return 0; +#endif +} + +static void extendAssetsManager(lua_State* L) +{ + lua_pushstring(L, "cc.AssetsManager"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "setDelegate", lua_cocos2dx_AssetsManager_setDelegate); + } + lua_pop(L, 1); +} + +#define KEY_TABLEVIEW_DATA_SOURCE "TableViewDataSource" +#define KEY_TABLEVIEW_DELEGATE "TableViewDelegate" + +class LUA_TableViewDelegate:public Ref, public TableViewDelegate +{ +public: + LUA_TableViewDelegate(){} + + virtual ~LUA_TableViewDelegate(){} + + + virtual void scrollViewDidScroll(ScrollView* view) override + { + if (nullptr != view) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)view, ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL); + if (0 != handler) + { + LuaTableViewEventData eventData; + BasicScriptData data(view,&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL, (void*)&data); + } + } + } + + virtual void scrollViewDidZoom(ScrollView* view) override + { + if (nullptr != view) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)view, ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM); + if (0 != handler) + { + LuaTableViewEventData eventData; + BasicScriptData data(view,&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::SCROLLVIEW_ZOOM, (void*)&data); + } + } + } + + virtual void tableCellTouched(TableView* table, TableViewCell* cell) override + { + if (nullptr != table && nullptr != cell) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED); + if (0 != handler) + { + LuaTableViewEventData eventData(cell); + BasicScriptData data(table,&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_TOUCHED,(void*)&data); + } + } + } + + virtual void tableCellHighlight(TableView* table, TableViewCell* cell) override + { + if (nullptr != table && nullptr != cell) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT); + if (0 != handler) + { + LuaTableViewEventData eventData(cell); + BasicScriptData data(table,&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_HIGHLIGHT,(void*)&data); + } + } + } + + virtual void tableCellUnhighlight(TableView* table, TableViewCell* cell) override + { + if (nullptr != table && nullptr != cell) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT); + if (0 != handler) + { + LuaTableViewEventData eventData(cell); + BasicScriptData data(table,&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_UNHIGHLIGHT,(void*)&data); + } + } + } + + virtual void tableCellWillRecycle(TableView* table, TableViewCell* cell) override + { + if (nullptr != table && nullptr != cell) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE); + if (0 != handler) + { + LuaTableViewEventData eventData(cell); + BasicScriptData data(table,&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_WILL_RECYCLE,(void*)&data); + } + } + } +}; + +static int lua_cocos2dx_TableView_setDelegate(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + TableView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.TableView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (TableView*) tolua_tousertype(L,1,0); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_TableView_setDelegate'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (0 == argc) + { + LUA_TableViewDelegate* delegate = new LUA_TableViewDelegate(); + if (nullptr == delegate) + return 0; + + Dictionary* userDict = static_cast(self->getUserObject()); + if (nullptr == userDict) + { + userDict = new Dictionary(); + if (NULL == userDict) + return 0; + + self->setUserObject(userDict); + userDict->release(); + } + + userDict->setObject(delegate, KEY_TABLEVIEW_DELEGATE); + self->setDelegate(delegate); + delegate->release(); + + return 0; + } + + CCLOG("'setDelegate' function of TableView wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'setDelegate'.",&tolua_err); + return 0; +#endif +} + +class LUA_TableViewDataSource:public Ref,public TableViewDataSource +{ +public: + LUA_TableViewDataSource(){} + virtual ~LUA_TableViewDataSource(){} + + virtual Size tableCellSizeForIndex(TableView *table, ssize_t idx) override + { + if (nullptr != table ) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX); + if (0 != handler) + { + LuaTableViewEventData eventData(&idx); + BasicScriptData data(table,&eventData); + float width = 0.0; + float height = 0.0; + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_SIZE_FOR_INDEX, (void*)&data,2,[&](lua_State* L,int numReturn){ + CCASSERT(numReturn == 2, "tableCellSizeForIndex return count error"); + ValueVector vec; + width = (float)tolua_tonumber(L, -1, 0); + lua_pop(L, 1); + height = (float)tolua_tonumber(L, -1, 0); + lua_pop(L, 1); + }); + + return Size(width, height); + } + } + + return Size::ZERO; + } + + virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx) override + { + if (nullptr != table ) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX); + if (0 != handler) + { + LuaTableViewEventData eventData(&idx); + BasicScriptData data(table,&eventData); + TableViewCell* viewCell = nullptr; + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLECELL_AT_INDEX, (void*)&data, 1, [&](lua_State* L, int numReturn){ + CCASSERT(numReturn == 1, "tableCellAtIndex return count error"); + viewCell = static_cast(tolua_tousertype(L, -1, nullptr)); + lua_pop(L, 1); + }); + + return viewCell; + } + } + + return NULL; + } + + virtual ssize_t numberOfCellsInTableView(TableView *table) override + { + if (nullptr != table ) + { + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)table, ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS); + if (0 != handler) + { + LuaTableViewEventData eventData; + BasicScriptData data(table,&eventData); + ssize_t counts = 0; + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::TABLEVIEW_NUMS_OF_CELLS, (void*)&data,1, [&](lua_State* L, int numReturn){ + CCASSERT(numReturn == 1, "numberOfCellsInTableView return count error"); + counts = (ssize_t)tolua_tonumber(L, -1, 0); + lua_pop(L, 1); + }); + return counts; + } + } + return 0; + } +}; + +static int lua_cocos2dx_TableView_setDataSource(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + TableView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.TableView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (TableView*) tolua_tousertype(L,1,0); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_TableView_setDataSource'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (0 == argc) + { + LUA_TableViewDataSource* dataSource = new LUA_TableViewDataSource(); + if (nullptr == dataSource) + return 0; + + Dictionary* userDict = static_cast(self->getUserObject()); + if (nullptr == userDict) + { + userDict = new Dictionary(); + if (NULL == userDict) + return 0; + + self->setUserObject(userDict); + userDict->release(); + } + + userDict->setObject(dataSource, KEY_TABLEVIEW_DATA_SOURCE); + + self->setDataSource(dataSource); + + dataSource->release(); + + return 0; + } + + CCLOG("'setDataSource' function of TableView wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'setDataSource'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_TableView_create(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertable(L,1,"cc.TableView",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(L) - 1; + + if (2 == argc || 1 == argc) + { + LUA_TableViewDataSource* dataSource = new LUA_TableViewDataSource(); + Size size; + ok &= luaval_to_size(L, 2, &size); + + TableView* ret = nullptr; + + if (1 == argc) + { + ret = TableView::create(dataSource, size); + } + else + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(L,3,"cc.Node",0,&tolua_err)) goto tolua_lerror; +#endif + Node* node = static_cast(tolua_tousertype(L, 3, nullptr)); + ret = TableView::create(dataSource, size, node); + } + + if (nullptr == ret) + return 0; + + ret->reloadData(); + + Dictionary* userDict = new Dictionary(); + userDict->setObject(dataSource, KEY_TABLEVIEW_DATA_SOURCE); + ret->setUserObject(userDict); + userDict->release(); + + dataSource->release(); + + + int nID = (int)ret->_ID; + int* pLuaID = &ret->_luaID; + toluafix_pushusertype_ccobject(L, nID, pLuaID, (void*)ret,"cc.TableView"); + + return 1; + } + CCLOG("'create' function of TableView wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'create'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2d_TableView_registerScriptHandler(lua_State* L) +{ + if (NULL == L) + return 0; + + int argc = 0; + TableView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.TableView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'tolua_cocos2d_TableView_registerScriptHandler'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + if (2 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err) || + !tolua_isnumber(L, 3, 0, &tolua_err) ) + { + goto tolua_lerror; + } +#endif + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType) ((int)tolua_tonumber(L,3,0) + (int)ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, handlerType); + return 0; + } + + CCLOG("'registerScriptHandler' function of TableView has wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'registerScriptHandler'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2d_TableView_unregisterScriptHandler(lua_State* L) +{ + if (NULL == L) + return 0; + + int argc = 0; + TableView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.TableView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2d_TableView_unregisterScriptHandler'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isnumber(L, 2, 0, &tolua_err)) + goto tolua_lerror; +#endif + ScriptHandlerMgr::HandlerType handlerType = (ScriptHandlerMgr::HandlerType) ((int)tolua_tonumber(L,2,0) + (int)ScriptHandlerMgr::HandlerType::SCROLLVIEW_SCROLL); + ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)self, handlerType); + return 0; + } + + CCLOG("'unregisterScriptHandler' function of TableView has wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'unregisterScriptHandler'.",&tolua_err); + return 0; +#endif +} + +static void extendTableView(lua_State* L) +{ + lua_pushstring(L, "cc.TableView"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "setDelegate", lua_cocos2dx_TableView_setDelegate); + tolua_function(L, "setDataSource", lua_cocos2dx_TableView_setDataSource); + tolua_function(L, "create", lua_cocos2dx_TableView_create); + tolua_function(L, "registerScriptHandler", lua_cocos2d_TableView_registerScriptHandler); + tolua_function(L, "unregisterScriptHandler", lua_cocos2d_TableView_unregisterScriptHandler); + } + lua_pop(L, 1); +} + +int register_all_cocos2dx_extension_manual(lua_State* tolua_S) +{ + extendControl(tolua_S); + extendEditBox(tolua_S); + extendCCBReader(tolua_S); + extendCCBAnimationManager(tolua_S); + extendAssetsManager(tolua_S); + extendScrollView(tolua_S); + extendTableView(tolua_S); + return 0; +} diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_extension_manual.h b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_extension_manual.h new file mode 100644 index 0000000000..b150bf3cbd --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_extension_manual.h @@ -0,0 +1,61 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_LUA_COCOS2DX_EXTENSION_MANUAL_H +#define COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_LUA_COCOS2DX_EXTENSION_MANUAL_H + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +#include "LuaScriptHandlerMgr.h" + +TOLUA_API int register_all_cocos2dx_extension_manual(lua_State* tolua_S); +TOLUA_API int register_cocos2dx_extension_CCBProxy(lua_State* tolua_S); + +struct LuaAssetsManagerEventData +{ + int value; + + LuaAssetsManagerEventData(int _value = 0):value(_value) + { + } +}; + +struct LuaTableViewEventData +{ + void* value; + + // Constructor + LuaTableViewEventData(void* _value = nullptr) + :value(_value) + { + } +}; + + +#endif // #ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_LUA_COCOS2DX_EXTENSION_MANUAL_H diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_gui_manual.cpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_gui_manual.cpp new file mode 100644 index 0000000000..2fb9b15452 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_gui_manual.cpp @@ -0,0 +1,765 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#include "lua_cocos2dx_gui_manual.hpp" +#include "cocos2d.h" +#include "tolua_fix.h" +#include "LuaBasicConversions.h" +#include "LuaScriptHandlerMgr.h" +#include "CCLuaValue.h" +#include "CocosGUI.h" +#include "CCLuaEngine.h" + +using namespace ui; + +class LuaCocoStudioEventListener:public Ref +{ +public: + LuaCocoStudioEventListener(); + virtual ~LuaCocoStudioEventListener(); + + static LuaCocoStudioEventListener* create(); + + virtual void eventCallbackFunc(Ref* sender,int eventType); +}; + +LuaCocoStudioEventListener::LuaCocoStudioEventListener() +{ + +} + +LuaCocoStudioEventListener::~LuaCocoStudioEventListener() +{ + +} + +LuaCocoStudioEventListener* LuaCocoStudioEventListener::create() +{ + LuaCocoStudioEventListener* listener = new LuaCocoStudioEventListener(); + if (nullptr == listener) + return nullptr; + + listener->autorelease(); + + return listener; +} + +void LuaCocoStudioEventListener::eventCallbackFunc(Ref* sender,int eventType) +{ + int handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER); + + if (0 != handler) + { + LuaStudioEventListenerData eventData(sender,eventType); + BasicScriptData data(this,(void*)&eventData); + LuaEngine::getInstance()->handleEvent(ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER, (void*)&data); + } +} + +static int lua_cocos2dx_Widget_addTouchEventListener(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + Widget* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.Widget",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_Widget_addTouchEventListener'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + LuaCocoStudioEventListener* listener = LuaCocoStudioEventListener::create(); + if (nullptr == listener) + { + tolua_error(L,"LuaCocoStudioEventListener create fail\n", NULL); + return 0; + } + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)listener, handler, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER); + + __Dictionary* dict = static_cast<__Dictionary*>(self->getUserObject()); + if (nullptr == dict) + { + dict = Dictionary::create(); + self->setUserObject(dict); + } + dict->setObject(listener, "widgetTouchEvent"); + + self->addTouchEventListener(listener, toucheventselector(LuaCocoStudioEventListener::eventCallbackFunc)); + + return 0; + } + + CCLOG("'addTouchEventListener' function of Widget has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addTouchEventListener'.",&tolua_err); + return 0; +#endif +} + +static void extendWidget(lua_State* L) +{ + lua_pushstring(L, "ccui.Widget"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "addTouchEventListener", lua_cocos2dx_Widget_addTouchEventListener); + } + lua_pop(L, 1); +} + +static int lua_cocos2dx_CheckBox_addEventListenerCheckBox(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + CheckBox* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.CheckBox",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_CheckBox_addEventListenerCheckBox'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + LuaCocoStudioEventListener* listener = LuaCocoStudioEventListener::create(); + if (nullptr == listener) + { + tolua_error(L,"LuaCocoStudioEventListener create fail\n", NULL); + return 0; + } + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)listener, handler, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER); + + __Dictionary* dict = static_cast<__Dictionary*>(self->getUserObject()); + if (nullptr == dict) + { + dict = Dictionary::create(); + self->setUserObject(dict); + } + dict->setObject(listener, "checkBoxEventListener"); + + self->addEventListenerCheckBox(listener, checkboxselectedeventselector(LuaCocoStudioEventListener::eventCallbackFunc)); + + return 0; + } + + CCLOG("'addEventListenerCheckBox' function of CheckBox has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addEventListenerCheckBox'.",&tolua_err); + return 0; +#endif +} + + +static void extendCheckBox(lua_State* L) +{ + lua_pushstring(L, "ccui.CheckBox"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "addEventListenerCheckBox", lua_cocos2dx_CheckBox_addEventListenerCheckBox); + } + lua_pop(L, 1); +} + +static int lua_cocos2dx_Slider_addEventListenerSlider(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + Slider* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.Slider",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_Slider_addEventListenerSlider'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err) ) + { + goto tolua_lerror; + } +#endif + LuaCocoStudioEventListener* listener = LuaCocoStudioEventListener::create(); + if (nullptr == listener) + { + tolua_error(L,"LuaCocoStudioEventListener create fail\n", NULL); + return 0; + } + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)listener, handler, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER); + + __Dictionary* dict = static_cast<__Dictionary*>(self->getUserObject()); + if (nullptr == dict) + { + dict = Dictionary::create(); + self->setUserObject(dict); + } + dict->setObject(listener, "sliderEventListener"); + + self->addEventListenerSlider(listener, sliderpercentchangedselector(LuaCocoStudioEventListener::eventCallbackFunc)); + + return 0; + } + + CCLOG("'addEventListenerSlider' function of Slider has wrong number of arguments: %d, was expecting %d\n", argc, 1); + + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addEventListenerSlider'.",&tolua_err); + return 0; +#endif +} + +static void extendSlider(lua_State* L) +{ + lua_pushstring(L, "ccui.Slider"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "addEventListenerSlider", lua_cocos2dx_Slider_addEventListenerSlider); + } + lua_pop(L, 1); +} + +static int lua_cocos2dx_TextField_addEventListenerTextField(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + TextField* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.TextField",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_TextField_addEventListenerTextField'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + LuaCocoStudioEventListener* listener = LuaCocoStudioEventListener::create(); + if (nullptr == listener) + { + tolua_error(L,"LuaCocoStudioEventListener create fail\n", NULL); + return 0; + } + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)listener, handler, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER); + + __Dictionary* dict = static_cast<__Dictionary*>(self->getUserObject()); + if (nullptr == dict) + { + dict = Dictionary::create(); + self->setUserObject(dict); + } + dict->setObject(listener, "textfieldEventListener"); + + self->addEventListenerTextField(listener, textfieldeventselector(LuaCocoStudioEventListener::eventCallbackFunc)); + + return 0; + } + + CCLOG("'addEventListenerTextField' function of TextField has wrong number of arguments: %d, was expecting %d\n", argc, 1); + + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addEventListenerTextField'.",&tolua_err); + return 0; +#endif +} + +static void extendTextField(lua_State* L) +{ + lua_pushstring(L, "ccui.TextField"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "addEventListenerTextField", lua_cocos2dx_TextField_addEventListenerTextField); + } + lua_pop(L, 1); +} + +static int lua_cocos2dx_PageView_addEventListenerPageView(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + PageView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.PageView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_PageView_addEventListenerPageView'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err) ) + { + goto tolua_lerror; + } +#endif + LuaCocoStudioEventListener* listener = LuaCocoStudioEventListener::create(); + if (nullptr == listener) + { + tolua_error(L,"LuaCocoStudioEventListener create fail\n", NULL); + return 0; + } + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)listener, handler, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER); + + __Dictionary* dict = static_cast<__Dictionary*>(self->getUserObject()); + if (nullptr == dict) + { + dict = Dictionary::create(); + self->setUserObject(dict); + } + dict->setObject(listener, "pageViewEventListener"); + + self->addEventListenerPageView(listener, pagevieweventselector(LuaCocoStudioEventListener::eventCallbackFunc)); + + return 0; + } + + CCLOG("'addEventListenerPageView' function of PageView has wrong number of arguments: %d, was expecting %d\n", argc, 1); + + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addEventListenerPageView'.",&tolua_err); + return 0; +#endif +} + +static void extendPageView(lua_State* L) +{ + lua_pushstring(L, "ccui.PageView"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "addEventListenerPageView", lua_cocos2dx_PageView_addEventListenerPageView); + } + lua_pop(L, 1); +} + +static int lua_cocos2dx_ScrollView_addEventListenerScrollView(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + ScrollView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.ScrollView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_ScrollView_addEventListenerScrollView'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + LuaCocoStudioEventListener* listener = LuaCocoStudioEventListener::create(); + if (nullptr == listener) + { + tolua_error(L,"LuaCocoStudioEventListener create fail\n", NULL); + return 0; + } + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)listener, handler, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER); + + __Dictionary* dict = static_cast<__Dictionary*>(self->getUserObject()); + if (nullptr == dict) + { + dict = Dictionary::create(); + self->setUserObject(dict); + } + dict->setObject(listener, "scrollViewEventListener"); + + self->addEventListenerScrollView(listener, scrollvieweventselector(LuaCocoStudioEventListener::eventCallbackFunc)); + + return 0; + } + + CCLOG("'addEventListenerScrollView' function of ScrollView has wrong number of arguments: %d, was expecting %d\n", argc, 1); + + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addEventListenerScrollView'.",&tolua_err); + return 0; +#endif +} + +static void extendScrollView(lua_State* L) +{ + lua_pushstring(L, "ccui.ScrollView"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "addEventListenerScrollView", lua_cocos2dx_ScrollView_addEventListenerScrollView); + } + lua_pop(L, 1); +} + +static int lua_cocos2dx_ListView_addEventListenerListView(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + ListView* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.ListView",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_ListView_addEventListenerListView'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + LuaCocoStudioEventListener* listener = LuaCocoStudioEventListener::create(); + if (nullptr == listener) + { + tolua_error(L,"LuaCocoStudioEventListener create fail\n", NULL); + return 0; + } + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)listener, handler, ScriptHandlerMgr::HandlerType::STUDIO_EVENT_LISTENER); + + __Dictionary* dict = static_cast<__Dictionary*>(self->getUserObject()); + if (nullptr == dict) + { + dict = Dictionary::create(); + self->setUserObject(dict); + } + dict->setObject(listener, "listViewEventListener"); + + self->addEventListenerListView(listener, listvieweventselector(LuaCocoStudioEventListener::eventCallbackFunc)); + + return 0; + } + + CCLOG("'addEventListenerListView' function of ListView has wrong number of arguments: %d, was expecting %d\n", argc, 1); + + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addEventListenerListView'.",&tolua_err); + return 0; +#endif +} + +static void extendListView(lua_State* L) +{ + lua_pushstring(L, "ccui.ListView"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "addEventListenerListView", lua_cocos2dx_ListView_addEventListenerListView); + } + lua_pop(L, 1); +} + +static int lua_cocos2dx_LayoutParameter_setMargin(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + LayoutParameter* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.LayoutParameter",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_LayoutParameter_setMargin'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_istable(L, 2, 0, &tolua_err)) + { + goto tolua_lerror; + } +#endif + + Margin margin; + lua_pushstring(L, "left"); + lua_gettable(L,2); + margin.left = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "top"); + lua_gettable(L,2); + margin.top = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "right"); + lua_gettable(L,2); + margin.right = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + lua_pushstring(L, "bottom"); + lua_gettable(L,2); + margin.bottom = lua_isnil(L,-1) ? 0 : lua_tonumber(L,-1); + lua_pop(L,1); + + self->setMargin(margin); + return 0; + } + + CCLOG("'setMargin' function of LayoutParameter has wrong number of arguments: %d, was expecting %d\n", argc, 1); + + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'setMargin'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_LayoutParameter_getMargin(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + LayoutParameter* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.LayoutParameter",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_LayoutParameter_getMargin'\n", NULL); + return 0; + } +#endif + argc = lua_gettop(L) - 1; + + if (0 == argc) + { + Margin margin = self->getMargin(); + + lua_newtable(L); + + lua_pushstring(L, "left"); + lua_pushnumber(L, (lua_Number) margin.left); + lua_rawset(L, -3); + + lua_pushstring(L, "top"); + lua_pushnumber(L, (lua_Number) margin.top); + lua_rawset(L, -3); + + lua_pushstring(L, "right"); + lua_pushnumber(L, (lua_Number) margin.right); + lua_rawset(L, -3); + + lua_pushstring(L, "bottom"); + lua_pushnumber(L, (lua_Number) margin.bottom); + lua_rawset(L, -3); + + return 1; + } + + CCLOG("'getMargin' function of LayoutParameter has wrong number of arguments: %d, was expecting %d\n", argc, 0); + + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'getMargin'.",&tolua_err); + return 0; +#endif +} + +static void extendLayoutParameter(lua_State* L) +{ + lua_pushstring(L, "ccui.LayoutParameter"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "setMargin", lua_cocos2dx_LayoutParameter_setMargin); + tolua_function(L, "getMargin", lua_cocos2dx_LayoutParameter_getMargin); + } + lua_pop(L, 1); +} + +int register_all_cocos2dx_ui_manual(lua_State* L) +{ + if (nullptr == L) + return 0; + extendWidget(L); + extendCheckBox(L); + extendSlider(L); + extendTextField(L); + extendPageView(L); + extendScrollView(L); + extendListView(L); + extendLayoutParameter(L); + + return 0; +} diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_gui_manual.hpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_gui_manual.hpp new file mode 100644 index 0000000000..a1846cb815 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_gui_manual.hpp @@ -0,0 +1,48 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_GUI_MANUAL_H +#define COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_GUI_MANUAL_H + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +#include "CCRef.h" + +TOLUA_API int register_all_cocos2dx_ui_manual(lua_State* L); + +struct LuaStudioEventListenerData +{ + cocos2d::Ref* objTarget; + int eventType; + + LuaStudioEventListenerData(cocos2d::Ref* _objTarget, int _eventType):objTarget(_objTarget),eventType(_eventType) + { + } +}; +#endif // #ifndef COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_GUI_MANUAL_H diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp.REMOVED.git-id b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp.REMOVED.git-id new file mode 100644 index 0000000000..174bfa1384 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp.REMOVED.git-id @@ -0,0 +1 @@ +eb7d91c43b6712c049d8afbbdff2952ed47537fb \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.hpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.hpp new file mode 100644 index 0000000000..d8dae90200 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.hpp @@ -0,0 +1,114 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_GENERATED_LUA_COCOS2DX_MANUAL_H +#define COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_GENERATED_LUA_COCOS2DX_MANUAL_H + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +#include "cocos2d.h" +#include "LuaScriptHandlerMgr.h" + +NS_CC_BEGIN +class LuaEventListenerCustom +{ +public: + static EventListenerCustom* create(const std::string& eventName); +}; + +class LuaEventListenerAcceleration +{ +public: + static EventListenerAcceleration* create(); +}; +NS_CC_END + +USING_NS_CC; + +TOLUA_API int register_all_cocos2dx_manual(lua_State* tolua_S); + +TOLUA_API int register_cocos2dx_event_releated(lua_State* tolua_S); + +struct LuaEventAccelerationData +{ + void* acc; + Event* event; + + LuaEventAccelerationData(void* inAcc,Event* inEvent) + :acc(inAcc),event(inEvent) + { + } +}; + +struct LuaEventKeyboarData +{ + int keyCode; + Event* event; + + LuaEventKeyboarData(int inKeyCode,Event* inEvent) + :keyCode(inKeyCode),event(inEvent) + { + } +}; + +struct LuaEventTouchData +{ + Touch* touch; + Event* event; + + LuaEventTouchData(Touch* inTouch, Event* inEvent) + :touch(inTouch), + event(inEvent) + { + } +}; + +struct LuaEventTouchesData +{ + std::vector touches; + Event* event; + + LuaEventTouchesData(std::vector inTouches, Event* inEvent) + :touches(inTouches), + event(inEvent) + { + } +}; + +struct LuaEventMouseData +{ + Event* event; + + LuaEventMouseData(Event* inEvent) + :event(inEvent) + { + } +}; + +#endif // #ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_GENERATED_LUA_COCOS2DX_MANUAL_H diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.cpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.cpp new file mode 100644 index 0000000000..b5792b3444 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.cpp @@ -0,0 +1,1619 @@ +#include "lua_cocos2dx_manual.hpp" + +#if CC_USE_PHYSICS +#include "tolua_fix.h" +#include "LuaBasicConversions.h" +#include "CCLuaValue.h" +#include "CCLuaEngine.h" + +#ifndef CC_SAFE_FREE +#define CC_SAFE_FREE(p) { if(p) free(p); p = nullptr; } +#endif + +int lua_cocos2dx_physics_PhysicsBody_getJoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsBody* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsBody",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsBody*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsBody_getJoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + do { + + if(!ok) + return 0; + auto& ret = cobj->getJoints(); + + lua_newtable(tolua_S); + + if (ret.empty()) + return 1; + + auto iter = ret.begin(); + int indexTable = 1; + for (; iter != ret.end(); ++iter) + { + if (nullptr == *iter) + continue; + + lua_pushnumber(tolua_S, (lua_Number)indexTable); + tolua_pushusertype(tolua_S,(void*)(*iter), getLuaTypeName(*iter, "cc.PhysicsJoint")); + lua_rawset(tolua_S, -3); + ++indexTable; + } + } while (0); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getJoints",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsBody_getJoints'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsWorld_getScene(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsWorld* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsWorld",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsWorld*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsWorld_getScene'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + if(!ok) + return 0; + cocos2d::Scene& ret = cobj->getScene(); + do { + + std::string hashName = typeid(ret).name(); + auto iter = g_luaType.find(hashName); + std::string className = ""; + if(iter != g_luaType.end()){ + className = iter->second.c_str(); + } else { + className = "cc.Scene"; + } + + int ID = (int)(ret._ID); + int* luaID = &(ret._luaID); + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)(&ret),className.c_str()); + + }while (0); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getScene",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsWorld_getScene'.",&tolua_err); +#endif + + return 0; +} + + +int lua_cocos2dx_physics_PhysicsWorld_rayCast(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsWorld* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsWorld",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsWorld*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsWorld_rayCast'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 3) + { + std::function arg0; + cocos2d::Point arg1; + cocos2d::Point arg2; + LUA_FUNCTION handler = toluafix_ref_function(tolua_S, 2, 0); + do { + arg0 = [handler, tolua_S](cocos2d::PhysicsWorld &world, const cocos2d::PhysicsRayCastInfo &info, void * data) -> bool + { + tolua_pushusertype(tolua_S, (void*)(&world), getLuaTypeName(&world, "cc.PhysicsWorld")); + physics_raycastinfo_to_luaval(tolua_S, info); + return LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 2); + }; + } while(0); + + ok &= luaval_to_point(tolua_S, 3, &arg1); + ok &= luaval_to_point(tolua_S, 4, &arg2); + if(!ok) + return 0; + cobj->rayCast(arg0, arg1, arg2, nullptr); + toluafix_remove_function_by_refid(tolua_S, handler); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "rayCast",argc, 4); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsWorld_rayCast'.",&tolua_err); +#endif + + return 0; +} +int lua_cocos2dx_physics_PhysicsWorld_queryRect(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsWorld* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsWorld",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsWorld*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsWorld_queryRect'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 2) + { + std::function arg0; + cocos2d::Rect arg1; + LUA_FUNCTION handler = toluafix_ref_function(tolua_S, 2, 0); + do { + arg0 = [handler, tolua_S](cocos2d::PhysicsWorld &world, cocos2d::PhysicsShape &shape, void * data) -> bool + { + tolua_pushusertype(tolua_S, (void*)(&world), getLuaTypeName(&world, "cc.PhysicsWorld")); + toluafix_pushusertype_ccobject(tolua_S, shape._ID, &shape._luaID, (void*)(&shape), "cc.PhysicsShape"); + return LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 2); + }; + } while(0); + + ok &= luaval_to_rect(tolua_S, 3, &arg1); + if(!ok) + return 0; + cobj->queryRect(arg0, arg1, nullptr); + toluafix_remove_function_by_refid(tolua_S, handler); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "queryRect",argc, 3); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsWorld_queryRect'.",&tolua_err); +#endif + + return 0; +} + + +int lua_cocos2dx_physics_PhysicsWorld_queryPoint(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsWorld* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsWorld",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsWorld*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsWorld_queryPoint'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 2) + { + std::function arg0; + cocos2d::Point arg1; + LUA_FUNCTION handler = toluafix_ref_function(tolua_S, 2, 0); + do { + arg0 = [handler, tolua_S](cocos2d::PhysicsWorld &world, cocos2d::PhysicsShape &shape, void * data) -> bool + { + tolua_pushusertype(tolua_S, (void*)(&world), getLuaTypeName(&world, "cc.PhysicsWorld")); + toluafix_pushusertype_ccobject(tolua_S, shape._ID, &shape._luaID, (void*)(&shape), "cc.PhysicsShape"); + return LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 2); + }; + assert(false); + } while(0) + ; + ok &= luaval_to_point(tolua_S, 3, &arg1); + if(!ok) + return 0; + cobj->queryPoint(arg0, arg1, nullptr); + toluafix_remove_function_by_refid(tolua_S, handler); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "queryPoint",argc, 3); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsWorld_queryPoint'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsBody_createPolygon(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsBody",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0 = nullptr; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createPolygon(arg0, arg1); + CC_SAFE_FREE(arg0); + do { + if (nullptr != ret) + { + int ID = ret->_ID; + int* luaID = &ret->_luaID; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret, "cc.PhysicsBody"); + } + else + { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createPolygon(arg0, arg1, arg2); + CC_SAFE_FREE(arg0); + do { + + if (nullptr != ret) + { + int ID = ret->_ID; + int* luaID = &ret->_luaID; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret, "cc.PhysicsBody"); + } + else + { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 3) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + cocos2d::Point arg3; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + ok &= luaval_to_point(tolua_S, 4, &arg3); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createPolygon(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg0); + do { + if (nullptr != ret) + { + int ID = ret->_ID; + int* luaID = &ret->_luaID; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret, "cc.PhysicsBody"); + } + else + { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "createPolygon",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsBody_createPolygon'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsBody_createEdgePolygon(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsBody",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgePolygon(arg0, arg1); + CC_SAFE_FREE(arg0); + do { + if (nullptr != ret) + { + int ID = ret->_ID; + int* luaID = &ret->_luaID; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret, "cc.PhysicsBody"); + } + else + { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1; + cocos2d::PhysicsMaterial arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgePolygon(arg0, arg1, arg2); + CC_SAFE_FREE(arg0); + do { + if (nullptr != ret) + { + int ID = ret->_ID; + int* luaID = &ret->_luaID; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret, "cc.PhysicsBody"); + } + else + { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 3) + { + cocos2d::Point* arg0; + int arg1; + cocos2d::PhysicsMaterial arg2; + double arg3; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + ok &= luaval_to_number(tolua_S, 4,&arg3); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgePolygon(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg0); + do { + if (nullptr != ret) + { + int ID = ret->_ID; + int* luaID = &ret->_luaID; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret, "cc.PhysicsBody"); + } + else + { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "createEdgePolygon",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsBody_createEdgePolygon'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsBody_createEdgeChain(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsBody",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgeChain(arg0, arg1); + CC_SAFE_FREE(arg0); + do { + if (nullptr != ret) + { + int ID = ret->_ID; + int* luaID = &ret->_luaID; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret, "cc.PhysicsBody"); + } + else + { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1; + cocos2d::PhysicsMaterial arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgeChain(arg0, arg1, arg2); + CC_SAFE_FREE(arg0); + do { + if (nullptr != ret) + { + int ID = ret->_ID; + int* luaID = &ret->_luaID; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret, "cc.PhysicsBody"); + } + else + { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + if (argc == 3) + { + cocos2d::Point* arg0; + int arg1; + cocos2d::PhysicsMaterial arg2; + double arg3; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + ok &= luaval_to_number(tolua_S, 4,&arg3); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsBody* ret = cocos2d::PhysicsBody::createEdgeChain(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg0); + do { + if (nullptr != ret) + { + int ID = ret->_ID; + int* luaID = &ret->_luaID; + toluafix_pushusertype_ccobject(tolua_S,ID, luaID, (void*)ret, "cc.PhysicsBody"); + } + else + { + lua_pushnil(tolua_S); + } + } while (0); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "createEdgeChain",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsBody_createEdgeChain'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsShape_recenterPoints(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShape",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShape::recenterPoints(arg0, arg1); + points_to_luaval(tolua_S, arg0, arg1); + CC_SAFE_FREE(arg0); + + return 0; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::Point arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_point(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShape::recenterPoints(arg0, arg1, arg2); + points_to_luaval(tolua_S, arg0, arg1); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "recenterPoints",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShape_recenterPoints'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsShape_getPolyonCenter(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShape",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::Point ret = cocos2d::PhysicsShape::getPolyonCenter(arg0, arg1); + CC_SAFE_FREE(arg0); + point_to_luaval(tolua_S, ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "getPolyonCenter",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShape_getPolyonCenter'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapeBox_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapeBox* cobj = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeBox",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapeBox*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeBox_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + cocos2d::Point arg0[4]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, 4); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeBox_getPoints'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapePolygon_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapePolygon* cobj = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapePolygon",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapePolygon*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapePolygon_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + int count = cobj->getPointsCount(); + cocos2d::Point* arg0 = new cocos2d::Point[count]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, count); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapePolygon_getPoints'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapePolygon_create(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShapePolygon",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShapePolygon* ret = cocos2d::PhysicsShapePolygon::create(arg0, arg1); + CC_SAFE_FREE(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapePolygon",(cocos2d::PhysicsShapePolygon*)ret); + return 1; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShapePolygon* ret = cocos2d::PhysicsShapePolygon::create(arg0, arg1, arg2); + CC_SAFE_FREE(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapePolygon",(cocos2d::PhysicsShapePolygon*)ret); + return 1; + } + if (argc == 3) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + cocos2d::Point arg3; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + ok &= luaval_to_point(tolua_S, 4, &arg3); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShapePolygon* ret = cocos2d::PhysicsShapePolygon::create(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapePolygon",(cocos2d::PhysicsShapePolygon*)ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "create",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapePolygon_create'.",&tolua_err); +#endif + return 0; +} +int lua_cocos2dx_physics_PhysicsShapePolygon_calculateArea(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShapePolygon",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + double ret = cocos2d::PhysicsShapePolygon::calculateArea(arg0, arg1); + CC_SAFE_FREE(arg0); + tolua_pushnumber(tolua_S,(lua_Number)ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "calculateArea",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapePolygon_calculateArea'.",&tolua_err); +#endif + return 0; +} +int lua_cocos2dx_physics_PhysicsShapePolygon_calculateMoment(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShapePolygon",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 2) + { + double arg0; + cocos2d::Point* arg1; + int arg2 = 0; + ok &= luaval_to_number(tolua_S, 2,&arg0); + do { + ok = luaval_to_array_of_Point(tolua_S, 3, &arg1, &arg2); + if (nullptr == arg1){ + LUA_PRECONDITION( arg1, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg1); + return 0; + } + double ret = cocos2d::PhysicsShapePolygon::calculateMoment(arg0, arg1, arg2); + CC_SAFE_FREE(arg1); + tolua_pushnumber(tolua_S,(lua_Number)ret); + return 1; + } + if (argc == 2) + { + double arg0; + cocos2d::Point* arg1; + int arg2 = 0; + cocos2d::Point arg3; + ok &= luaval_to_number(tolua_S, 2,&arg0); + do { + ok = luaval_to_array_of_Point(tolua_S, 3, &arg1, &arg2); + if (nullptr == arg1){ + LUA_PRECONDITION( arg1, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_point(tolua_S, 4, &arg3); + if(!ok) + { + CC_SAFE_FREE(arg1); + return 0; + } + double ret = cocos2d::PhysicsShapePolygon::calculateMoment(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg1); + tolua_pushnumber(tolua_S,(lua_Number)ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "calculateMoment",argc, 3); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapePolygon_calculateMoment'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapeEdgeBox* cobj = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeEdgeBox",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapeEdgeBox*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + int count = cobj->getPointsCount(); + cocos2d::Point* arg0 = new cocos2d::Point[count]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, count); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPoints'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapeEdgePolygon* cobj = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeEdgePolygon",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapeEdgePolygon*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + int count = cobj->getPointsCount(); + cocos2d::Point* arg0 = new cocos2d::Point[count]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, count); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPoints'.",&tolua_err); +#endif + + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapeEdgeChain_getPoints(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::PhysicsShapeEdgeChain* cobj = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.PhysicsShapeEdgeChain",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::PhysicsShapeEdgeChain*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_physics_PhysicsShapeEdgeChain_getPoints'", NULL); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 0) + { + int count = cobj->getPointsCount(); + cocos2d::Point* arg0 = new cocos2d::Point[count]; + cobj->getPoints(arg0); + points_to_luaval(tolua_S, arg0, count); + CC_SAFE_FREE(arg0); + return 0; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "getPoints",argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgeChain_getPoints'.",&tolua_err); +#endif + + return 0; +} + +static int tolua_cocos2dx_EventListenerPhysicsContact_registerScriptHandler(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + int argc = 0; + EventListenerPhysicsContact* self = nullptr; +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S, 1, "cc.EventListenerPhysicsContact", 0, &tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2dx_EventListenerPhysicsContact_registerScriptHandler'\n", nullptr); + return 0; + } +#endif + argc = lua_gettop(tolua_S) - 1; + + if (argc == 2) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) || + !tolua_isnumber(tolua_S, 3, 0, &tolua_err)) + { + goto tolua_lerror; + } +#endif + LUA_FUNCTION handler = toluafix_ref_function(tolua_S,2,0); + ScriptHandlerMgr::HandlerType type = static_cast((int)tolua_tonumber(tolua_S, 3, 0)); + switch (type) + { + case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_BEGIN: + { + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type); + + self->onContactBegin = [handler](PhysicsContact& contact) -> bool{ + LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); + stack->pushObject(&contact, "cc.PhysicsContact"); + bool ret = stack->executeFunctionByHandler(handler, 1); + stack->clean(); + + return ret; + }; + } + break; + case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_PRESOLVE: + { + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type); + + self->onContactPreSolve = [handler](PhysicsContact& contact, PhysicsContactPreSolve& solve) -> bool{ + LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); + stack->pushObject(&contact, "cc.PhysicsContact"); + tolua_pushusertype(stack->getLuaState(), &solve, "cc.PhysicsContactPreSolve"); + bool ret = stack->executeFunctionByHandler(handler, 2); + stack->clean(); + + return ret; + }; + } + break; + case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_POSTSOLVE: + { + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type); + + self->onContactPostSolve = [handler](PhysicsContact& contact, const PhysicsContactPostSolve& solve){ + LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); + stack->pushObject(&contact, "cc.PhysicsContact"); + tolua_pushusertype(stack->getLuaState(), const_cast(&solve), "cc.PhysicsContactPostSolve"); + stack->executeFunctionByHandler(handler, 2); + stack->clean(); + }; + } + break; + case ScriptHandlerMgr::HandlerType::EVENT_PHYSICS_CONTACT_SEPERATE: + { + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, type); + + self->onContactSeperate = [handler](PhysicsContact& contact){ + LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); + stack->pushObject(&contact, "cc.PhysicsContact"); + stack->executeFunctionByHandler(handler, 1); + stack->clean(); + }; + } + break; + default: + break; + } + return 0; + } + + CCLOG("'registerScriptHandler' has wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'registerScriptHandler'.",&tolua_err); + return 0; +#endif +} + +int lua_cocos2dx_physics_PhysicsShapeEdgePolygon_create(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShapeEdgePolygon",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShapeEdgePolygon* ret = cocos2d::PhysicsShapeEdgePolygon::create(arg0, arg1); + CC_SAFE_FREE(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapeEdgePolygon",(cocos2d::PhysicsShapeEdgePolygon*)ret); + return 1; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShapeEdgePolygon* ret = cocos2d::PhysicsShapeEdgePolygon::create(arg0, arg1, arg2); + CC_SAFE_FREE(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapeEdgePolygon",(cocos2d::PhysicsShapeEdgePolygon*)ret); + return 1; + } + if (argc == 3) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + double arg3; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + ok &= luaval_to_number(tolua_S, 4,&arg3); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShapeEdgePolygon* ret = cocos2d::PhysicsShapeEdgePolygon::create(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapeEdgePolygon",(cocos2d::PhysicsShapeEdgePolygon*)ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "create",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgePolygon_create'.",&tolua_err); +#endif + return 0; +} + +int lua_cocos2dx_physics_PhysicsShapeEdgeChain_create(lua_State* tolua_S) +{ + int argc = 0; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertable(tolua_S,1,"cc.PhysicsShapeEdgeChain",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { + cocos2d::Point* arg0; + int arg1 = 0; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShapeEdgeChain* ret = cocos2d::PhysicsShapeEdgeChain::create(arg0, arg1); + CC_SAFE_FREE(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapeEdgeChain",(cocos2d::PhysicsShapeEdgeChain*)ret); + return 1; + } + if (argc == 2) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShapeEdgeChain* ret = cocos2d::PhysicsShapeEdgeChain::create(arg0, arg1, arg2); + CC_SAFE_FREE(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapeEdgeChain",(cocos2d::PhysicsShapeEdgeChain*)ret); + return 1; + } + if (argc == 3) + { + cocos2d::Point* arg0; + int arg1 = 0; + cocos2d::PhysicsMaterial arg2; + double arg3; + do { + ok = luaval_to_array_of_Point(tolua_S, 2, &arg0, &arg1); + if (nullptr == arg0){ + LUA_PRECONDITION( arg0, "Invalid Native Object"); + }} while (0); + ok &= luaval_to_physics_material(tolua_S, 3, &arg2); + ok &= luaval_to_number(tolua_S, 4,&arg3); + if(!ok) + { + CC_SAFE_FREE(arg0); + return 0; + } + cocos2d::PhysicsShapeEdgeChain* ret = cocos2d::PhysicsShapeEdgeChain::create(arg0, arg1, arg2, arg3); + CC_SAFE_FREE(arg0); + object_to_luaval(tolua_S, "cc.PhysicsShapeEdgeChain",(cocos2d::PhysicsShapeEdgeChain*)ret); + return 1; + } + CCLOG("%s has wrong number of arguments: %d, was expecting %d\n ", "create",argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_physics_PhysicsShapeEdgeChain_create'.",&tolua_err); +#endif + return 0; +} + +int register_all_cocos2dx_physics_manual(lua_State* tolua_S) +{ + lua_pushstring(tolua_S, "cc.PhysicsBody"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getJoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsBody_getJoints ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"createPolygon"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsBody_createPolygon ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"createEdgeChain"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsBody_createEdgeChain ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"createEdgePolygon"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsBody_createEdgePolygon ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "cc.PhysicsShape"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"recenterPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShape_recenterPoints ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"getPolyonCenter"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShape_getPolyonCenter ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "cc.PhysicsShapeBox"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeBox_getPoints ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "cc.PhysicsShapeEdgeBox"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeEdgeBox_getPoints ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "cc.PhysicsShapePolygon"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapePolygon_getPoints ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"create"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapePolygon_create ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"calculateArea"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapePolygon_calculateArea ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"calculateMoment"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapePolygon_calculateMoment ); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "cc.PhysicsShapeEdgePolygon"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeEdgePolygon_getPoints ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"create"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeEdgePolygon_create); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "cc.PhysicsShapeEdgeChain"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getPoints"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeEdgeChain_getPoints); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"create"); + lua_pushcfunction(tolua_S,lua_cocos2dx_physics_PhysicsShapeEdgeChain_create); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "cc.PhysicsWorld"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + lua_pushstring(tolua_S,"getScene"); + lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_getScene ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"queryPoint"); + lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_queryPoint ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"queryRect"); + lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_queryRect ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S,"rayCast"); + lua_pushcfunction(tolua_S, lua_cocos2dx_physics_PhysicsWorld_rayCast ); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_NONE"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_NONE); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_SHAPE"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_SHAPE); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_JOINT"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_JOINT); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_CONTACT"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_CONTACT); + lua_rawset(tolua_S,-3); + lua_pushstring(tolua_S, "DEBUGDRAW_ALL"); + lua_pushnumber(tolua_S, PhysicsWorld::DEBUGDRAW_ALL); + lua_rawset(tolua_S,-3); + } + lua_pop(tolua_S, 1); + + lua_pushstring(tolua_S, "cc.EventListenerPhysicsContact"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + tolua_function(tolua_S, "registerScriptHandler", tolua_cocos2dx_EventListenerPhysicsContact_registerScriptHandler); + } + lua_pop(tolua_S, 1); + + tolua_constant(tolua_S, "PHYSICS_INFINITY", PHYSICS_INFINITY); + + return 0; +} + +#endif \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.hpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.hpp new file mode 100644 index 0000000000..bc9f32b165 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_physics_manual.hpp @@ -0,0 +1,21 @@ +#ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_GENERATED_LUA_COCOS2DX_PHYSICS_MANUAL_H +#define COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_GENERATED_LUA_COCOS2DX_PHYSICS_MANUAL_H + +#if CC_USE_PHYSICS + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +#include "cocos2d.h" +#include "LuaScriptHandlerMgr.h" + +int register_all_cocos2dx_physics_manual(lua_State* tolua_S); + +#endif // CC_USE_PHYSICS + +#endif // #ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_GENERATED_LUA_COCOS2DX_PHYSICS_MANUAL_H diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_spine_manual.cpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_spine_manual.cpp new file mode 100644 index 0000000000..15ffe45d00 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_spine_manual.cpp @@ -0,0 +1,336 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#include "lua_cocos2dx_spine_manual.hpp" +#include "cocos2d.h" +#include "tolua_fix.h" +#include "LuaBasicConversions.h" +#include "LuaScriptHandlerMgr.h" +#include "CCLuaValue.h" +#include "spine.h" +#include "spine-cocos2dx.h" +#include "LuaSkeletonAnimation.h" + +using namespace spine; + +// setBlendFunc +template +static int tolua_cocos2dx_setBlendFunc(lua_State* tolua_S,const char* className) +{ + if (NULL == tolua_S || NULL == className || strlen(className) == 0) + return 0; + + int argc = 0; + T* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S,1,className,0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); + + argc = lua_gettop(tolua_S) - 1; + if (2 == argc) + { + GLenum src, dst; + if (!luaval_to_int32(tolua_S, 2, (int32_t*)&src)) + return 0; + + if (!luaval_to_int32(tolua_S, 3, (int32_t*)&dst)) + return 0; + + BlendFunc blendFunc = {src, dst}; + self->setBlendFunc(blendFunc); + return 0; + } + + + CCLOG("'setBlendFunc' has wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'setBlendFunc'.",&tolua_err); + return 0; +#endif +} + + +static int lua_cocos2dx_CCSkeletonAnimation_createWithFile(lua_State* L) +{ + if (nullptr == L) + return 0 ; + + int argc = 0; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertable(L,1,"sp.SkeletonAnimation",0,&tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(L) - 1; + + if (2 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(L, 2, 0, &tolua_err) || + !tolua_isstring(L, 3 ,0, &tolua_err)) + { + goto tolua_lerror; + } +#endif + const char* skeletonDataFile = tolua_tostring(L, 2, ""); + const char* atlasFile = tolua_tostring(L, 3, "");; + + auto tolua_ret = LuaSkeletonAnimation::createWithFile(skeletonDataFile, atlasFile); + + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(L, nID, pLuaID, (void*)tolua_ret,"sp.SkeletonAnimation"); + return 1; + } else if (3 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(L, 2, 0, &tolua_err) || + !tolua_isstring(L, 3 ,0, &tolua_err) || + !tolua_isnumber(L, 4 ,0, &tolua_err)) + { + goto tolua_lerror; + } +#endif + const char* skeletonDataFile = tolua_tostring(L, 2, ""); + const char* atlasFile = tolua_tostring(L, 3, ""); + LUA_NUMBER scale = tolua_tonumber(L, 4, 1); + + auto tolua_ret = LuaSkeletonAnimation::createWithFile(skeletonDataFile, atlasFile, scale); + + int nID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* pLuaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(L, nID, pLuaID, (void*)tolua_ret,"sp.SkeletonAnimation"); + return 1; + } + + CCLOG("'createWithFile' function of SkeletonAnimation has wrong number of arguments: %d, was expecting %d\n", argc, 2); + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'createWithFile'.",&tolua_err); + return 0; +#endif +} + +int tolua_Cocos2d_CCSkeletonAnimation_registerSpineEventHandler00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err) || + !toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaSkeletonAnimation* self = (LuaSkeletonAnimation*) tolua_tousertype(tolua_S,1,0); + if (NULL != self ) { + int handler = ( toluafix_ref_function(tolua_S,2,0)); + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, ScriptHandlerMgr::HandlerType::EVENT_SPINE); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'registerSpineEventHandler'.",&tolua_err); + return 0; +#endif +} + +int tolua_Cocos2d_CCSkeletonAnimation_unregisterSpineEventHandler00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err) || + !tolua_isnoobj(tolua_S,2,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaSkeletonAnimation* self = (LuaSkeletonAnimation*) tolua_tousertype(tolua_S,1,0); + if (NULL != self ) { + ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)self, ScriptHandlerMgr::HandlerType::EVENT_SPINE); + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'unregisterScriptHandler'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCSkeletonAnimation_setTimeScale00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err) || + !tolua_isnumber(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaSkeletonAnimation* self = (LuaSkeletonAnimation*) tolua_tousertype(tolua_S,1,0); + if (NULL != self ) { + LUA_NUMBER scale = tolua_tonumber(tolua_S, 2, 1); + self->timeScale = scale; + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'setTimeScale'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCSkeletonAnimation_setDebugSlots00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err) || + !tolua_isboolean(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaSkeletonAnimation* self = (LuaSkeletonAnimation*) tolua_tousertype(tolua_S,1,0); + if (NULL != self ) { + bool debugSlots = tolua_toboolean(tolua_S, 2, 1); + self->debugSlots = debugSlots; + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'setDebugSlots'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCSkeletonAnimation_setDebugBones00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err) || + !tolua_isboolean(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaSkeletonAnimation* self = (LuaSkeletonAnimation*) tolua_tousertype(tolua_S,1,0); + if (NULL != self ) { + bool debugBones = tolua_toboolean(tolua_S, 2, 1); + self->debugBones = debugBones; + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'setDebugBones'.",&tolua_err); + return 0; +#endif +} + +static int tolua_Cocos2d_CCSkeletonAnimation_setPremultipliedAlpha00(lua_State* tolua_S) +{ +#ifndef TOLUA_RELEASE + tolua_Error tolua_err; + if ( + !tolua_isusertype(tolua_S,1,"sp.SkeletonAnimation",0,&tolua_err) || + !tolua_isboolean(tolua_S,2,0,&tolua_err) || + !tolua_isnoobj(tolua_S,3,&tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LuaSkeletonAnimation* self = (LuaSkeletonAnimation*) tolua_tousertype(tolua_S,1,0); + if (NULL != self ) { + bool premultipliedAlpha = tolua_toboolean(tolua_S, 2, 1); + self->premultipliedAlpha = premultipliedAlpha; + } + } + return 0; +#ifndef TOLUA_RELEASE +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'setPremultipliedAlpha'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_spine_SkeletoneAnimation_setBlendFunc(lua_State* tolua_S) +{ + return tolua_cocos2dx_setBlendFunc(tolua_S,"sp.SkeletonAnimation"); +} + + +static void extendCCSkeletonAnimation(lua_State* L) +{ + lua_pushstring(L, "sp.SkeletonAnimation"); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_istable(L,-1)) + { + tolua_function(L, "create", lua_cocos2dx_CCSkeletonAnimation_createWithFile); + tolua_function(L, "registerSpineEventHandler", tolua_Cocos2d_CCSkeletonAnimation_registerSpineEventHandler00); + tolua_function(L, "unregisterSpineEventHandler", tolua_Cocos2d_CCSkeletonAnimation_unregisterSpineEventHandler00); + tolua_function(L, "setBlendFunc", tolua_spine_SkeletoneAnimation_setBlendFunc); + tolua_function(L, "setTimeScale", tolua_Cocos2d_CCSkeletonAnimation_setTimeScale00); + tolua_function(L, "setDebugSlots", tolua_Cocos2d_CCSkeletonAnimation_setDebugSlots00); + tolua_function(L, "setDebugBones", tolua_Cocos2d_CCSkeletonAnimation_setDebugBones00); + tolua_function(L, "setPremultipliedAlpha", tolua_Cocos2d_CCSkeletonAnimation_setPremultipliedAlpha00); + } + lua_pop(L, 1); +} + +int register_all_cocos2dx_spine_manual(lua_State* L) +{ + if (nullptr == L) + return 0; + + extendCCSkeletonAnimation(L); + + return 0; +} diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_spine_manual.hpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_spine_manual.hpp new file mode 100644 index 0000000000..8bda268aea --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_spine_manual.hpp @@ -0,0 +1,40 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_SPINE_MANUAL_H +#define COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_SPINE_MANUAL_H + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +#include "CCRef.h" + +TOLUA_API int register_all_cocos2dx_spine_manual(lua_State* L); + + +#endif // #ifndef COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_SPINE_MANUAL_H diff --git a/cocos/scripting/lua-bindings/manual/lua_debugger.c.REMOVED.git-id b/cocos/scripting/lua-bindings/manual/lua_debugger.c.REMOVED.git-id new file mode 100644 index 0000000000..b474b19a28 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_debugger.c.REMOVED.git-id @@ -0,0 +1 @@ +fe27a3bee5f56dc93ee7ae540adb0d88245d8072 \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/manual/lua_debugger.h b/cocos/scripting/lua-bindings/manual/lua_debugger.h new file mode 100644 index 0000000000..39b1579e6f --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_debugger.h @@ -0,0 +1,23 @@ + +/* debugger.h.h */ + +#ifndef __LUA_MODULES_A51AA4C53D84E3F9A6C1C027E02078B1_H_ +#define __LUA_MODULES_A51AA4C53D84E3F9A6C1C027E02078B1_H_ + +#if __cplusplus +extern "C" { +#endif + +#include "lua.h" + +void luaopen_debugger(lua_State* L); + +/* +int luaopen_lua_m_ldt_debugger(lua_State* L); +*/ + +#if __cplusplus +} +#endif + +#endif /* __LUA_MODULES_A51AA4C53D84E3F9A6C1C027E02078B1_H_ */ diff --git a/cocos/scripting/lua-bindings/manual/lua_extensions.c b/cocos/scripting/lua-bindings/manual/lua_extensions.c new file mode 100644 index 0000000000..91f3a7ba37 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_extensions.c @@ -0,0 +1,37 @@ + +#include "lua_extensions.h" + +#if __cplusplus +extern "C" { +#endif +// socket +#include "luasocket/luasocket.h" +#include "luasocket/mime.h" +#include "luasocket/socket_scripts.h" + +static luaL_Reg luax_exts[] = { + {"socket.core", luaopen_socket_core}, + {"mime.core", luaopen_mime_core}, + {NULL, NULL} +}; + +void luaopen_lua_extensions(lua_State *L) +{ + // load extensions + luaL_Reg* lib = luax_exts; + lua_getglobal(L, "package"); + lua_getfield(L, -1, "preload"); + for (; lib->func; lib++) + { + lua_pushcfunction(L, lib->func); + lua_setfield(L, -2, lib->name); + } + lua_pop(L, 2); + + // load extensions script + luaopen_socket_scripts(L); +} + +#if __cplusplus +} // extern "C" +#endif diff --git a/cocos/scripting/lua-bindings/manual/lua_extensions.h b/cocos/scripting/lua-bindings/manual/lua_extensions.h new file mode 100644 index 0000000000..67727f057f --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_extensions.h @@ -0,0 +1,23 @@ + +#ifndef __LUA_EXTRA_H_ +#define __LUA_EXTRA_H_ + +#if defined(_USRDLL) + #define LUA_EXTENSIONS_DLL __declspec(dllexport) +#else /* use a DLL library */ + #define LUA_EXTENSIONS_DLL +#endif + +#if __cplusplus +extern "C" { +#endif + +#include "lauxlib.h" + +void LUA_EXTENSIONS_DLL luaopen_lua_extensions(lua_State *L); + +#if __cplusplus +} +#endif + +#endif /* __LUA_EXTRA_H_ */ diff --git a/cocos/scripting/lua-bindings/manual/lua_xml_http_request.cpp b/cocos/scripting/lua-bindings/manual/lua_xml_http_request.cpp new file mode 100644 index 0000000000..e036363f0f --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_xml_http_request.cpp @@ -0,0 +1,1104 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#include "lua_xml_http_request.h" +#include +#include "tolua_fix.h" +#include "CCLuaStack.h" +#include "CCLuaValue.h" +#include "CCLuaEngine.h" +#include "LuaScriptHandlerMgr.h" + + +using namespace cocos2d; +using namespace std; + +LuaMinXmlHttpRequest::LuaMinXmlHttpRequest():_isNetwork(true) +{ + _httpHeader.clear(); + _requestHeader.clear(); + _withCredentialsValue = true; + _httpRequest = new network::HttpRequest(); +} + +LuaMinXmlHttpRequest::~LuaMinXmlHttpRequest() +{ + _httpHeader.clear(); + _requestHeader.clear(); +} + +/** + * @brief Implementation for header retrieving. + * @param header + */ +void LuaMinXmlHttpRequest::_gotHeader(string header) +{ + // Get Header and Set StatusText + // Split String into Tokens + char * cstr = new char [header.length()+1]; + + // check for colon. + unsigned found_header_field = header.find_first_of(":"); + + if (found_header_field != std::string::npos) + { + // Found a header field. + string http_field; + string http_value; + + http_field = header.substr(0,found_header_field); + http_value = header.substr(found_header_field+1, header.length()); + + // Get rid of all \n + if (!http_value.empty() && http_value[http_value.size() - 1] == '\n') { + http_value.erase(http_value.size() - 1); + } + + _httpHeader[http_field] = http_value; + + } + else + { + // Seems like we have the response Code! Parse it and check for it. + char * pch; + strcpy(cstr, header.c_str()); + + pch = strtok(cstr," "); + while (pch != NULL) + { + + stringstream ss; + string val; + + ss << pch; + val = ss.str(); + unsigned found_http = val.find("HTTP"); + + // Check for HTTP Header to set statusText + if (found_http != std::string::npos) { + + stringstream mystream; + + // Get Response Status + pch = strtok (NULL, " "); + mystream << pch; + + pch = strtok (NULL, " "); + mystream << " " << pch; + + _statusText = mystream.str(); + + } + + pch = strtok (NULL, " "); + } + } + + CC_SAFE_DELETE_ARRAY(cstr); +} + +/** + * @brief Set Request header for next call. + * @param field Name of the Header to be set. + * @param value Value of the Headerfield + */ +void LuaMinXmlHttpRequest::setRequestHeader(const char* field, const char* value) +{ + stringstream header_s; + stringstream value_s; + string header; + + map::iterator iter = _requestHeader.find(field); + + // Concatenate values when header exists. + if (iter != _requestHeader.end()) + { + value_s << iter->second << "," << value; + } + else + { + value_s << value; + } + + _requestHeader[field] = value_s.str(); +} + +/** + * @brief If headers has been set, pass them to curl. + * + */ +void LuaMinXmlHttpRequest::_setHttpRequestHeader() +{ + std::vector header; + + for (auto it = _requestHeader.begin(); it != _requestHeader.end(); ++it) + { + const char* first = it->first.c_str(); + const char* second = it->second.c_str(); + size_t len = sizeof(char) * (strlen(first) + 3 + strlen(second)); + char* test = (char*) malloc(len); + memset(test, 0,len); + + strcpy(test, first); + strcpy(test + strlen(first) , ": "); + strcpy(test + strlen(first) + 2, second); + + header.push_back(test); + + free(test); + + } + + if (!header.empty()) + { + _httpRequest->setHeaders(header); + } + +} + +/** + * @brief Send out request and fire callback when done. + */ +void LuaMinXmlHttpRequest::_sendRequest() +{ + _httpRequest->setResponseCallback(this, httpresponse_selector(LuaMinXmlHttpRequest::handle_requestResponse)); + network::HttpClient::getInstance()->send(_httpRequest); + _httpRequest->release(); +} + + +/** + * @brief Callback for HTTPRequest. Handles the response and invokes Callback. + * @param sender Object which initialized callback + * @param respone Response object + * @js NA + */ +void LuaMinXmlHttpRequest::handle_requestResponse(network::HttpClient *sender, network::HttpResponse *response) +{ + if (0 != strlen(response->getHttpRequest()->getTag())) + { + CCLOG("%s completed", response->getHttpRequest()->getTag()); + } + + int statusCode = response->getResponseCode(); + char statusString[64] = {}; + sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag()); + + if (!response->isSucceed()) + { + CCLOG("response failed"); + CCLOG("error buffer: %s", response->getErrorBuffer()); + return; + } + + // set header + std::vector *headers = response->getResponseHeader(); + + char* concatHeader = (char*) malloc(headers->size() + 1); + std::string header(headers->begin(), headers->end()); + strcpy(concatHeader, header.c_str()); + + std::istringstream stream(concatHeader); + std::string line; + while(std::getline(stream, line)) { + _gotHeader(line); + } + + /** get the response data **/ + std::vector *buffer = response->getResponseData(); + char* concatenated = (char*) malloc(buffer->size() + 1); + std::string s2(buffer->begin(), buffer->end()); + strcpy(concatenated, s2.c_str()); + + if (statusCode == 200) + { + //Succeeded + _status = 200; + _readyState = DONE; + _data << concatenated; + _dataSize = buffer->size(); + } + else + { + _status = 0; + } + // Free Memory. + free((void*) concatHeader); + free((void*) concatenated); + + // call back lua function --TODO + int handler = cocos2d::ScriptHandlerMgr::getInstance()->getObjectHandler((void*)this, cocos2d::ScriptHandlerMgr::HandlerType::XMLHTTPREQUEST_READY_STATE_CHANGE ); + + if (0 != handler) + { + cocos2d::CommonScriptData data(handler,""); + cocos2d::ScriptEvent event(cocos2d::ScriptEventType::kCommonEvent,(void*)&data); + cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event); + } +} + +void LuaMinXmlHttpRequest::getByteData(unsigned char* byteData) +{ + _data.read((char*)byteData, _dataSize); +} + +/* function to regType */ +static void lua_reg_xml_http_request(lua_State* L) +{ + tolua_usertype(L, "cc.XMLHttpRequest"); +} + +static int lua_collect_xml_http_request (lua_State* L) +{ + LuaMinXmlHttpRequest* self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); + Mtolua_delete(self); + return 0; +} + +static int lua_cocos2dx_XMLHttpRequest_constructor(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + argc = lua_gettop(L)-1; + if (argc == 0) + { + self = new LuaMinXmlHttpRequest(); + self->autorelease(); + int ID = self? (int)self->_ID : -1; + int* luaID = self? &self->_luaID : NULL; + toluafix_pushusertype_ccobject(L, ID, luaID, (void*)self, "cc.XMLHttpRequest"); + return 1; + } + + CCLOG("%s has wrong number of arguments: %d, was expecting %d \n", "XMLHttpRequest",argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_cocos2dx_XMLHttpRequest_constructor'.",&tolua_err); + return 0; +#endif +} + +static int lua_get_XMLHttpRequest_responseType(lua_State* L) +{ + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_get_XMLHttpRequest_responseType'\n", nullptr); + return 0; + } +#endif + + tolua_pushnumber(L, (lua_Number)self->getResponseType()); + return 1; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_get_XMLHttpRequest_responseType'.",&tolua_err); + return 0; +#endif +} + +static int lua_set_XMLHttpRequest_responseType(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_set_XMLHttpRequest_responseType'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isnumber(L, 2, 0, &tolua_err)) + goto tolua_lerror; +#endif + int responseType = (int)tolua_tonumber(L,2,0); + + self->setResponseType((LuaMinXmlHttpRequest::ResponseType)responseType); + + return 0; + } + + CCLOG("'setResponseType' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_set_XMLHttpRequest_responseType'.",&tolua_err); + return 0; +#endif +} + +static int lua_get_XMLHttpRequest_withCredentials(lua_State* L) +{ + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_get_XMLHttpRequest_withCredentials'\n", nullptr); + return 0; + } +#endif + + tolua_pushboolean(L, self->getWithCredentialsValue()); + return 1; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_get_XMLHttpRequest_withCredentials'.",&tolua_err); + return 0; +#endif +} + +static int lua_set_XMLHttpRequest_withCredentials(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_set_XMLHttpRequest_withCredentials'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isboolean(L, 2, 0, &tolua_err)) + goto tolua_lerror; +#endif + self->setWithCredentialsValue((bool)tolua_toboolean(L, 2, 0)); + return 0; + } + + CCLOG("'setWithCredentials' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_set_XMLHttpRequest_withCredentials'.",&tolua_err); + return 0; +#endif +} + +static int lua_get_XMLHttpRequest_timeout(lua_State* L) +{ + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_get_XMLHttpRequest_timeout'\n", nullptr); + return 0; + } +#endif + + tolua_pushnumber(L, (lua_Number)self->getTimeout()); + return 1; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_get_XMLHttpRequest_timeout'.",&tolua_err); + return 0; +#endif +} + +static int lua_set_XMLHttpRequest_timeout(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_set_XMLHttpRequest_timeout'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isnumber(L, 2, 0, &tolua_err)) + goto tolua_lerror; +#endif + self->setTimeout((unsigned)tolua_tonumber(L, 2, 0)); + return 0; + } + + CCLOG("'setTimeout' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_set_XMLHttpRequest_timeout'.",&tolua_err); + return 0; +#endif +} + +static int lua_get_XMLHttpRequest_readyState(lua_State* L) +{ + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_get_XMLHttpRequest_readyState'\n", nullptr); + return 0; + } +#endif + + lua_pushinteger(L, (lua_Integer)self->getReadyState()); + + return 1; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_get_XMLHttpRequest_readyState'.",&tolua_err); + return 0; +#endif +} + +static int lua_get_XMLHttpRequest_status(lua_State* L) +{ + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_get_XMLHttpRequest_status'\n", nullptr); + return 0; + } +#endif + + lua_pushinteger(L, (lua_Integer)self->getStatus()); + + return 1; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_get_XMLHttpRequest_status'.",&tolua_err); + return 0; +#endif +} + +static int lua_get_XMLHttpRequest_statusText(lua_State* L) +{ + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_get_XMLHttpRequest_statusText'\n", nullptr); + return 0; + } +#endif + + lua_pushstring(L, self->getStatusText().c_str()); + + return 1; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_get_XMLHttpRequest_statusText'.",&tolua_err); + return 0; +#endif +} + +static int lua_get_XMLHttpRequest_responseText(lua_State* L) +{ + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_get_XMLHttpRequest_responseText'\n", nullptr); + return 0; + } +#endif + lua_pushstring(L, self->getDataStr().c_str()); + return 1; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_get_XMLHttpRequest_responseText'.",&tolua_err); + return 0; +#endif +} + +static int lua_get_XMLHttpRequest_response(lua_State* L) +{ + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_get_XMLHttpRequest_response'\n", nullptr); + return 0; + } +#endif + + if (self->getResponseType() == LuaMinXmlHttpRequest::ResponseType::JSON) + { + lua_pushstring(L, self->getDataStr().c_str()); + return 1; + } + else if(self->getResponseType() == LuaMinXmlHttpRequest::ResponseType::ARRAY_BUFFER) + { + LuaStack *pStack = LuaEngine::getInstance()->getLuaStack(); + if (NULL == pStack) { + return 0; + } + + lua_State *tolua_s = pStack->getLuaState(); + if (NULL == tolua_s) { + return 0; + } + + int nRet = 0; + LuaValueArray array; + + uint8_t* tmpData = new uint8_t[self->getDataSize()]; + if (nullptr == tmpData) + { + return 0; + } + + self->getByteData(tmpData); + + for (int i = 0 ; i < self->getDataSize(); i++) + { + LuaValue value = LuaValue::intValue(tmpData[i]); + array.push_back(value); + } + + pStack->pushLuaValueArray(array); + + CC_SAFE_DELETE_ARRAY(tmpData); + return 1; + } + else + { + lua_pushstring(L, self->getDataStr().c_str()); + return 1; + } + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_get_XMLHttpRequest_response'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_XMLHttpRequest_open(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_XMLHttpRequest_open'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if ( argc >= 2) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(L, 2, 0, &tolua_err) || + !tolua_isstring(L, 3, 0, &tolua_err)) + goto tolua_lerror; +#endif + + std::string method = tolua_tostring(L, 2, ""); + std::string url = tolua_tostring(L, 3, ""); + bool async = true; + if (argc > 2) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isboolean(L, 4, 0, &tolua_err) ) + goto tolua_lerror; +#endif + async = tolua_toboolean(L, 4, 0); + } + + self->setUrl(url); + self->setMethod(method); + self->setReadyState(1); + self->setAsync(async); + + if (url.length() > 5 && url.compare(url.length() - 5, 5, ".json") == 0 ) + { + self->setResponseType(LuaMinXmlHttpRequest::ResponseType::JSON); + } + + if (nullptr != self->getHttpRequest()) + { + if (method.compare("post") == 0 || method.compare("POST") == 0) + { + self->getHttpRequest()->setRequestType(network::HttpRequest::Type::POST); + } + else + { + self->getHttpRequest()->setRequestType(network::HttpRequest::Type::GET); + } + + self->getHttpRequest()->setUrl(url.c_str()); + + } + + self->setIsNetWork(true); + self->setReadyState(LuaMinXmlHttpRequest::OPENED); + + return 0; + } + + CCLOG("'open' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_cocos2dx_XMLHttpRequest_open'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_XMLHttpRequest_send(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + //std::string data = ""; + const char* data = NULL; + size_t size = 0; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_XMLHttpRequest_send'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if ( 1 == argc ) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(L, 2, 0, &tolua_err)) + goto tolua_lerror; +#endif + //data = tolua_tostring(L, 2, ""); + data = (const char*) lua_tolstring(L, 2, &size); + } + + if (size > 0 && + (self->getMethod().compare("post") == 0 || self->getMethod().compare("POST") == 0) && + nullptr != self->getHttpRequest()) + { + self->getHttpRequest()->setRequestData(data,size); + } + + self->_setHttpRequestHeader(); + self->_sendRequest(); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_cocos2dx_XMLHttpRequest_send'.",&tolua_err); + return 0; +#endif +} + +/** + * @brief abort function Placeholder! + */ +static int lua_cocos2dx_XMLHttpRequest_abort(lua_State* L) +{ + return 0; +} + +static int lua_cocos2dx_XMLHttpRequest_setRequestHeader(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + const char* field = ""; + const char* value = ""; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_XMLHttpRequest_setRequestHeader'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if ( 2 == argc ) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(L, 2, 0, &tolua_err) || + !tolua_isstring(L, 3, 0, &tolua_err) ) + goto tolua_lerror; +#endif + + field = tolua_tostring(L, 2, ""); + value = tolua_tostring(L, 3, ""); + self->setRequestHeader(field, value); + return 0; + } + + CCLOG("'setRequestHeader' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_cocos2dx_XMLHttpRequest_setRequestHeader'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_XMLHttpRequest_getAllResponseHeaders(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + + stringstream responseheaders; + string responseheader = ""; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_XMLHttpRequest_getAllResponseHeaders'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if ( 0 == argc ) + { + map httpHeader = self->getHttpHeader(); + + for (auto it = httpHeader.begin(); it != httpHeader.end(); ++it) + { + responseheaders << it->first << ": "<< it->second << "\n"; + } + + responseheader = responseheaders.str(); + tolua_pushstring(L, responseheader.c_str()); + return 1; + } + + CCLOG("'getAllResponseHeaders' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_cocos2dx_XMLHttpRequest_getAllResponseHeaders'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_XMLHttpRequest_getResponseHeader(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + + string responseheader = ""; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_XMLHttpRequest_getAllResponseHeaders'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if ( 1 == argc ) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(L, 2, 0, &tolua_err) ) + goto tolua_lerror; +#endif + responseheader = tolua_tostring(L, 2, ""); + + stringstream streamData; + streamData << responseheader; + + string value = streamData.str(); + + + auto iter = self->getHttpHeader().find(value); + if (iter != self->getHttpHeader().end()) + { + tolua_pushstring(L, (iter->second).c_str()); + return 1; + } + } + + CCLOG("'getResponseHeader' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_cocos2dx_XMLHttpRequest_getAllResponseHeaders'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_XMLHttpRequest_registerScriptHandler(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + + string responseheader = ""; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_XMLHttpRequest_registerScriptHandler'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) + goto tolua_lerror; +#endif + + int handler = ( toluafix_ref_function(L,2,0)); + cocos2d::ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, cocos2d::ScriptHandlerMgr::HandlerType::XMLHTTPREQUEST_READY_STATE_CHANGE); + return 0; + } + + CCLOG("'registerScriptHandler' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_cocos2dx_XMLHttpRequest_registerScriptHandler'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_XMLHttpRequest_unregisterScriptHandler(lua_State* L) +{ + int argc = 0; + LuaMinXmlHttpRequest* self = nullptr; + + string responseheader = ""; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"cc.XMLHttpRequest",0,&tolua_err)) goto tolua_lerror; +#endif + + self = (LuaMinXmlHttpRequest*) tolua_tousertype(L,1,0); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) + { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_XMLHttpRequest_unregisterScriptHandler'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (0 == argc) + { + cocos2d::ScriptHandlerMgr::getInstance()->removeObjectHandler((void*)self, cocos2d::ScriptHandlerMgr::HandlerType::XMLHTTPREQUEST_READY_STATE_CHANGE); + + return 0; + } + + CCLOG("'unregisterScriptHandler' function of XMLHttpRequest wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'lua_cocos2dx_XMLHttpRequest_unregisterScriptHandler'.",&tolua_err); + return 0; +#endif + +} + + +TOLUA_API int register_xml_http_request(lua_State* L) +{ + tolua_open(L); + lua_reg_xml_http_request(L); + tolua_module(L,"cc",0); + tolua_beginmodule(L,"cc"); + tolua_cclass(L,"XMLHttpRequest","cc.XMLHttpRequest","cc.Ref",lua_collect_xml_http_request); + tolua_beginmodule(L,"XMLHttpRequest"); + tolua_variable(L, "responseType", lua_get_XMLHttpRequest_responseType, lua_set_XMLHttpRequest_responseType); + tolua_variable(L, "withCredentials", lua_get_XMLHttpRequest_withCredentials, lua_set_XMLHttpRequest_withCredentials); + tolua_variable(L, "timeout", lua_get_XMLHttpRequest_timeout, lua_set_XMLHttpRequest_timeout); + tolua_variable(L, "readyState", lua_get_XMLHttpRequest_readyState, nullptr); + tolua_variable(L, "status",lua_get_XMLHttpRequest_status,nullptr); + tolua_variable(L, "statusText", lua_get_XMLHttpRequest_statusText, nullptr); + tolua_variable(L, "responseText", lua_get_XMLHttpRequest_responseText, nullptr); + tolua_variable(L, "response", lua_get_XMLHttpRequest_response, nullptr); + tolua_function(L, "new", lua_cocos2dx_XMLHttpRequest_constructor); + tolua_function(L, "open", lua_cocos2dx_XMLHttpRequest_open); + tolua_function(L, "send", lua_cocos2dx_XMLHttpRequest_send); + tolua_function(L, "abort", lua_cocos2dx_XMLHttpRequest_abort); + tolua_function(L, "setRequestHeader", lua_cocos2dx_XMLHttpRequest_setRequestHeader); + tolua_function(L, "getAllResponseHeaders", lua_cocos2dx_XMLHttpRequest_getAllResponseHeaders); + tolua_function(L, "getResponseHeader", lua_cocos2dx_XMLHttpRequest_getResponseHeader); + tolua_function(L, "registerScriptHandler", lua_cocos2dx_XMLHttpRequest_registerScriptHandler); + tolua_function(L, "unregisterScriptHandler", lua_cocos2dx_XMLHttpRequest_unregisterScriptHandler); + tolua_endmodule(L); + tolua_endmodule(L); + return 1; +} diff --git a/cocos/scripting/lua-bindings/manual/lua_xml_http_request.h b/cocos/scripting/lua-bindings/manual/lua_xml_http_request.h new file mode 100644 index 0000000000..036cad42d9 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/lua_xml_http_request.h @@ -0,0 +1,125 @@ +/**************************************************************************** + Copyright (c) 2013-2014 Chukong Technologies Inc. + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#ifndef __COCOS_SCRIPTING_LUA_BINDINGS_LUA_XML_HTTP_REQUEST_H__ +#define __COCOS_SCRIPTING_LUA_BINDINGS_LUA_XML_HTTP_REQUEST_H__ + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +#include +#include + +#include "network/HttpClient.h" + +class LuaMinXmlHttpRequest : public cocos2d::Ref +{ +public: + enum class ResponseType + { + STRING, + ARRAY_BUFFER, + BLOB, + DOCUMENT, + JSON + }; + + // Ready States (http://www.w3.org/TR/XMLHttpRequest/#interface-xmlhttprequest) + static const unsigned short UNSENT = 0; + static const unsigned short OPENED = 1; + static const unsigned short HEADERS_RECEIVED = 2; + static const unsigned short LOADING = 3; + static const unsigned short DONE = 4; + + LuaMinXmlHttpRequest(); + ~LuaMinXmlHttpRequest(); + + void handle_requestResponse(cocos2d::network::HttpClient *sender, cocos2d::network::HttpResponse *response); + + inline void setResponseType(ResponseType type) { _responseType = type; } + inline ResponseType getResponseType() {return _responseType; } + + inline void setWithCredentialsValue(bool value) { _withCredentialsValue = value; } + inline bool getWithCredentialsValue() {return _withCredentialsValue; } + + inline void setTimeout(unsigned timeOut) {_timeout = timeOut; } + inline unsigned getTimeout() { return _timeout;} + + inline void setReadyState(int readyState) { _readyState = readyState; } + inline int getReadyState() { return _readyState ;} + + inline cocos2d::network::HttpRequest* getHttpRequest() { return _httpRequest; } + inline int getStatus() { return _status; } + inline std::string getStatusText() { return _statusText ;} + + inline std::string getUrl(){return _url;} + inline void setUrl(std::string url) { _url = url ;} + + inline std::string getMethod(){return _meth;} + inline void setMethod(std::string meth) { _meth = meth ; } + + inline void setAsync(bool isAsync){ _isAsync = isAsync; } + inline void setIsNetWork(bool isNetWork) {_isNetwork = isNetWork; } + + void _setHttpRequestHeader(); + void _sendRequest(); + void setRequestHeader(const char* field, const char* value); + + std::map getHttpHeader() { return _httpHeader ;} + + void getByteData(unsigned char* byteData); + + inline std::string getDataStr() { return _data.str(); } + + inline size_t getDataSize() { return _dataSize; } + +private: + void _gotHeader(std::string header); + + + std::string _url; + std::string _meth; + std::string _type; + std::stringstream _data; + size_t _dataSize; + int _readyState; + int _status; + std::string _statusText; + ResponseType _responseType; + unsigned _timeout; + bool _isAsync; + cocos2d::network::HttpRequest* _httpRequest; + bool _isNetwork; + bool _withCredentialsValue; + std::map _httpHeader; + std::map _requestHeader; +}; + +TOLUA_API int register_xml_http_request(lua_State* L); + +#endif //#ifndef __COCOS_SCRIPTING_LUA_BINDINGS_LUA_XML_HTTP_REQUEST_H__ diff --git a/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp b/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp new file mode 100644 index 0000000000..14b48ddd17 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.cpp @@ -0,0 +1,618 @@ + +#include "CCLuaJavaBridge.h" +#include "platform/android/jni/JniHelper.h" +#include + +#define LOG_TAG "luajc" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) + +extern "C" { +#include "tolua_fix.h" +} + +LuaJavaBridge::CallInfo::~CallInfo(void) +{ + if (m_returnType == TypeString && m_ret.stringValue) + { + delete m_ret.stringValue; + } +} + +bool LuaJavaBridge::CallInfo::execute(void) +{ + switch (m_returnType) + { + case TypeVoid: + m_env->CallStaticVoidMethod(m_classID, m_methodID); + break; + + case TypeInteger: + m_ret.intValue = m_env->CallStaticIntMethod(m_classID, m_methodID); + break; + + case TypeFloat: + m_ret.floatValue = m_env->CallStaticFloatMethod(m_classID, m_methodID); + break; + + case TypeBoolean: + m_ret.boolValue = m_env->CallStaticBooleanMethod(m_classID, m_methodID); + break; + + case TypeString: + m_retjs = (jstring)m_env->CallStaticObjectMethod(m_classID, m_methodID); + const char *stringBuff = m_env->GetStringUTFChars(m_retjs, 0); + m_ret.stringValue = new string(stringBuff); + m_env->ReleaseStringUTFChars(m_retjs, stringBuff); + break; + } + + if (m_env->ExceptionCheck() == JNI_TRUE) + { + m_env->ExceptionDescribe(); + m_env->ExceptionClear(); + m_error = LUAJ_ERR_EXCEPTION_OCCURRED; + return false; + } + + return true; +} + + +bool LuaJavaBridge::CallInfo::executeWithArgs(jvalue *args) +{ + switch (m_returnType) + { + case TypeVoid: + m_env->CallStaticVoidMethodA(m_classID, m_methodID, args); + break; + + case TypeInteger: + m_ret.intValue = m_env->CallStaticIntMethodA(m_classID, m_methodID, args); + break; + + case TypeFloat: + m_ret.floatValue = m_env->CallStaticFloatMethodA(m_classID, m_methodID, args); + break; + + case TypeBoolean: + m_ret.boolValue = m_env->CallStaticBooleanMethodA(m_classID, m_methodID, args); + break; + + case TypeString: + m_retjs = (jstring)m_env->CallStaticObjectMethodA(m_classID, m_methodID, args); + const char *stringBuff = m_env->GetStringUTFChars(m_retjs, 0); + m_ret.stringValue = new string(stringBuff); + m_env->ReleaseStringUTFChars(m_retjs, stringBuff); + break; + } + + if (m_env->ExceptionCheck() == JNI_TRUE) + { + m_env->ExceptionDescribe(); + m_env->ExceptionClear(); + m_error = LUAJ_ERR_EXCEPTION_OCCURRED; + return false; + } + + return true; +} + +int LuaJavaBridge::CallInfo::pushReturnValue(lua_State *L) +{ + if (m_error != LUAJ_ERR_OK) + { + lua_pushinteger(L, m_error); + return 1; + } + + switch (m_returnType) + { + case TypeInteger: + lua_pushinteger(L, m_ret.intValue); + return 1; + case TypeFloat: + lua_pushnumber(L, m_ret.floatValue); + return 1; + case TypeBoolean: + lua_pushboolean(L, m_ret.boolValue); + return 1; + case TypeString: + lua_pushstring(L, m_ret.stringValue->c_str()); + return 1; + } + + return 0; +} + + +bool LuaJavaBridge::CallInfo::validateMethodSig(void) +{ + size_t len = m_methodSig.length(); + if (len < 3 || m_methodSig[0] != '(') // min sig is "()V" + { + m_error = LUAJ_ERR_INVALID_SIGNATURES; + return false; + } + + size_t pos = 1; + while (pos < len && m_methodSig[pos] != ')') + { + LuaJavaBridge::ValueType type = checkType(m_methodSig, &pos); + if (type == TypeInvalid) return false; + + m_argumentsCount++; + m_argumentsType.push_back(type); + pos++; + } + + if (pos >= len || m_methodSig[pos] != ')') + { + m_error = LUAJ_ERR_INVALID_SIGNATURES; + return false; + } + + pos++; + m_returnType = checkType(m_methodSig, &pos); + return true; +} + +LuaJavaBridge::ValueType LuaJavaBridge::CallInfo::checkType(const string& sig, size_t *pos) +{ + switch (sig[*pos]) + { + case 'I': + return TypeInteger; + case 'F': + return TypeFloat; + case 'Z': + return TypeBoolean; + case 'V': + return TypeVoid; + case 'L': + size_t pos2 = sig.find_first_of(';', *pos + 1); + if (pos2 == string::npos) + { + m_error = LUAJ_ERR_INVALID_SIGNATURES; + return TypeInvalid; + } + + const string t = sig.substr(*pos, pos2 - *pos + 1); + if (t.compare("Ljava/lang/String;") == 0) + { + *pos = pos2; + return TypeString; + } + else if (t.compare("Ljava/util/Vector;") == 0) + { + *pos = pos2; + return TypeVector; + } + else + { + m_error = LUAJ_ERR_TYPE_NOT_SUPPORT; + return TypeInvalid; + } + } + + m_error = LUAJ_ERR_TYPE_NOT_SUPPORT; + return TypeInvalid; +} + + +bool LuaJavaBridge::CallInfo::getMethodInfo(void) +{ + m_methodID = 0; + m_env = 0; + + JavaVM* jvm = cocos2d::JniHelper::getJavaVM(); + jint ret = jvm->GetEnv((void**)&m_env, JNI_VERSION_1_4); + switch (ret) { + case JNI_OK: + break; + + case JNI_EDETACHED : + if (jvm->AttachCurrentThread(&m_env, NULL) < 0) + { + LOGD("%s", "Failed to get the environment using AttachCurrentThread()"); + m_error = LUAJ_ERR_VM_THREAD_DETACHED; + return false; + } + break; + + case JNI_EVERSION : + default : + LOGD("%s", "Failed to get the environment using GetEnv()"); + m_error = LUAJ_ERR_VM_FAILURE; + return false; + } + jstring _jstrClassName = m_env->NewStringUTF(m_className.c_str()); + m_classID = (jclass) m_env->CallObjectMethod(cocos2d::JniHelper::classloader, + cocos2d::JniHelper::loadclassMethod_methodID, + _jstrClassName); + + if (NULL == m_classID) { + LOGD("Classloader failed to find class of %s", m_className.c_str()); + } + + m_env->DeleteLocalRef(_jstrClassName); + m_methodID = m_env->GetStaticMethodID(m_classID, m_methodName.c_str(), m_methodSig.c_str()); + if (!m_methodID) + { + m_env->ExceptionClear(); + LOGD("Failed to find method id of %s.%s %s", + m_className.c_str(), + m_methodName.c_str(), + m_methodSig.c_str()); + m_error = LUAJ_ERR_METHOD_NOT_FOUND; + return false; + } + + return true; +} + +/* ---------------------------------------- */ + +lua_State *LuaJavaBridge::s_luaState = NULL; +int LuaJavaBridge::s_newFunctionId = 0; + +void LuaJavaBridge::luaopen_luaj(lua_State *L) +{ + s_luaState = L; + lua_newtable(L); + lua_pushstring(L, "callStaticMethod"); + lua_pushcfunction(L, LuaJavaBridge::callJavaStaticMethod); + lua_rawset(L, -3); + lua_setglobal(L, "LuaJavaBridge"); +} + +/* +args: + const char *className + const char *methodName + LUA_TABLE args + const char *sig +*/ +int LuaJavaBridge::callJavaStaticMethod(lua_State *L) +{ + if (!lua_isstring(L, -4) || !lua_isstring(L, -3) || !lua_istable(L, -2) || !lua_isstring(L, -1)) + { + lua_pushboolean(L, 0); + lua_pushinteger(L, LUAJ_ERR_INVALID_SIGNATURES); + return 2; + } + + LOGD("%s", "LuaJavaBridge::callJavaStaticMethod(lua_State *L)"); + + const char *className = lua_tostring(L, -4); + const char *methodName = lua_tostring(L, -3); + const char *methodSig = lua_tostring(L, -1); + + CallInfo call(className, methodName, methodSig); + + // check args + lua_pop(L, 1); /* L: args */ + int count = fetchArrayElements(L, -1); /* L: args e1 e2 e3 e4 ... */ + jvalue *args = NULL; + if (count > 0) + { + args = new jvalue[count]; + for (int i = 0; i < count; ++i) + { + int index = -count + i; + switch (call.argumentTypeAtIndex(i)) + { + case TypeInteger: + if (lua_isfunction(L, index)) + { + args[i].i = retainLuaFunction(L, index, NULL); + } + else + { + args[i].i = (int)lua_tonumber(L, index); + } + break; + + case TypeFloat: + args[i].f = lua_tonumber(L, index); + break; + + case TypeBoolean: + args[i].z = lua_toboolean(L, index) != 0 ? JNI_TRUE : JNI_FALSE; + break; + + case TypeString: + default: + args[i].l = call.getEnv()->NewStringUTF(lua_tostring(L, index)); + break; + } + } + lua_pop(L, count); /* L: args */ + } + + bool success = args ? call.executeWithArgs(args) : call.execute(); + if (args) delete []args; + + if (!success) + { + LOGD("LuaJavaBridge::callJavaStaticMethod(\"%s\", \"%s\", args, \"%s\") EXECUTE FAILURE, ERROR CODE: %d", + className, methodName, methodSig, call.getErrorCode()); + + lua_pushboolean(L, 0); + lua_pushinteger(L, call.getErrorCode()); + return 2; + } + + LOGD("LuaJavaBridge::callJavaStaticMethod(\"%s\", \"%s\", args, \"%s\") SUCCESS", + className, methodName, methodSig); + + lua_pushboolean(L, 1); + return 1 + call.pushReturnValue(L); +} + +// increase lua function refernece counter, return counter +int LuaJavaBridge::retainLuaFunctionById(int functionId) +{ + lua_State *L = s_luaState; + + lua_pushstring(L, LUAJ_REGISTRY_RETAIN); /* L: key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: id_r */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + return 0; + } + + // get counter + lua_pushinteger(L, functionId); /* L: id_r id */ + lua_rawget(L, -2); /* L: id_r r */ + if (lua_type(L, -1) != LUA_TNUMBER) + { + lua_pop(L, 2); + return 0; + } + + // increase counter + int retainCount = lua_tonumber(L, -1); + retainCount++; + lua_pop(L, 1); /* L: id_r */ + lua_pushinteger(L, functionId); /* L: id_r id */ + lua_pushinteger(L, retainCount); /* L: id_r id r */ + lua_rawset(L, -3); /* id_r[id] = r, L: id_r */ + lua_pop(L, 1); + + LOGD("luajretainLuaFunctionById(%d) - retain count = %d", functionId, retainCount); + + return retainCount; +} + +// decrease lua function reference counter, return counter +int LuaJavaBridge::releaseLuaFunctionById(int functionId) +{ + lua_State *L = s_luaState; + /* L: */ + lua_pushstring(L, LUAJ_REGISTRY_FUNCTION); /* L: key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f_id */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + LOGD("%s", "luajreleaseLuaFunctionById() - LUAJ_REGISTRY_FUNCTION not exists"); + return 0; + } + + lua_pushstring(L, LUAJ_REGISTRY_RETAIN); /* L: f_id key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f_id id_r */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 2); + LOGD("%s", "luajreleaseLuaFunctionById() - LUAJ_REGISTRY_RETAIN not exists"); + return 0; + } + + lua_pushinteger(L, functionId); /* L: f_id id_r id */ + lua_rawget(L, -2); /* L: f_id id_r r */ + if (lua_type(L, -1) != LUA_TNUMBER) + { + lua_pop(L, 3); + LOGD("luajreleaseLuaFunctionById() - function id %d not found", functionId); + return 0; + } + + int retainCount = lua_tonumber(L, -1); + retainCount--; + + if (retainCount > 0) + { + // update counter + lua_pop(L, 1); /* L: f_id id_r */ + lua_pushinteger(L, functionId); /* L: f_id id_r id */ + lua_pushinteger(L, retainCount); /* L: f_id id_r id r */ + lua_rawset(L, -3); /* id_r[id] = r, L: f_id id_r */ + lua_pop(L, 2); + LOGD("luajreleaseLuaFunctionById() - function id %d retain count = %d", functionId, retainCount); + return retainCount; + } + + // remove lua function reference + lua_pop(L, 1); /* L: f_id id_r */ + lua_pushinteger(L, functionId); /* L: f_id id_r id */ + lua_pushnil(L); /* L: f_id id_r id nil */ + lua_rawset(L, -3); /* id_r[id] = nil, L: f_id id_r */ + + lua_pop(L, 1); /* L: f_id */ + lua_pushnil(L); /* L: f_id nil */ + while (lua_next(L, -2) != 0) /* L: f_id f id */ + { + int value = lua_tonumber(L, -1); + lua_pop(L, 1); /* L: f_id f */ + if (value == functionId) + { + lua_pushnil(L); /* L: f_id f nil */ + lua_rawset(L, -3); /* f_id[f] = nil, L: f_id */ + break; + } + } /* L: f_id */ + + lua_pop(L, 1); + LOGD("luajreleaseLuaFunctionById() - function id %d released", functionId); + return 0; +} + +int LuaJavaBridge::callLuaFunctionById(int functionId, const char *arg) +{ + lua_State *L = s_luaState; + int top = lua_gettop(L); + /* L: */ + lua_pushstring(L, LUAJ_REGISTRY_FUNCTION); /* L: key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f_id */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + return -1; + } + + lua_pushnil(L); /* L: f_id nil */ + while (lua_next(L, -2) != 0) /* L: f_id f id */ + { + int value = lua_tonumber(L, -1); + lua_pop(L, 1); /* L: f_id f */ + if (value == functionId) + { + lua_pushstring(L, arg); /* L: f_id f arg */ + int ok = lua_pcall(L, 1, 1, 0); /* L: f_id ret|err */ + int ret; + if (ok == 0) + { + ret = lua_tonumber(L, -1); + } + else + { + ret = -ok; + } + + lua_settop(L, top); + return ret; + } + } /* L: f_id */ + + lua_settop(L, top); + return -1; +} + +// call lua global function +int LuaJavaBridge::callLuaGlobalFunction(const char *functionName, const char *arg) +{ + lua_State *L = s_luaState; + + int ret = -1; + int top = lua_gettop(L); + + lua_getglobal(L, functionName); + if (lua_isfunction(L, -1)) + { + lua_pushstring(L, arg); + int ok = lua_pcall(L, 1, 1, 0); + if (ok == 0) + { + ret = lua_tonumber(L, -1); + } + else + { + ret = -ok; + } + } + + lua_settop(L, top); + return ret; +} + +// ---------------------------------------- + +// increase lua function reference counter, return functionId +int LuaJavaBridge::retainLuaFunction(lua_State *L, int functionIndex, int *retainCountReturn) +{ + /* L: f ... */ + lua_pushstring(L, LUAJ_REGISTRY_FUNCTION); /* L: f ... key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f ... f_id */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + lua_newtable(L); + lua_pushstring(L, LUAJ_REGISTRY_FUNCTION); + lua_pushvalue(L, -2); + lua_rawset(L, LUA_REGISTRYINDEX); + } + + lua_pushstring(L, LUAJ_REGISTRY_RETAIN); /* L: f ... f_id key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* L: f ... f_id id_r */ + if (!lua_istable(L, -1)) + { + lua_pop(L, 1); + lua_newtable(L); + lua_pushstring(L, LUAJ_REGISTRY_RETAIN); + lua_pushvalue(L, -2); + lua_rawset(L, LUA_REGISTRYINDEX); + } + + // get function id + lua_pushvalue(L, functionIndex - 2); /* L: f ... f_id id_r f */ + lua_rawget(L, -3); /* L: f ... f_id id_r id */ + + int functionId; + if (lua_type(L, -1) != LUA_TNUMBER) + { + // first retain, create new id + lua_pop(L, 1); /* L: f ... f_id id_r */ + s_newFunctionId++; + functionId = s_newFunctionId; + + lua_pushvalue(L, functionIndex - 2); /* L: f ... f_id id_r f */ + lua_pushinteger(L, functionId); /* L: f ... f_id id_r f id */ + lua_rawset(L, -4); /* f_id[f] = id, L: f ... f_id id_r */ + lua_pushinteger(L, functionId); /* L: f ... f_id id_r id */ + } + else + { + functionId = lua_tonumber(L, -1); + } + + // get function retain + lua_pushvalue(L, -1); /* L: f ... f_id id_r id id */ + lua_rawget(L, -3); /* L: f ... f_id id_r id r */ + int retainCount = 1; + if (lua_type(L, -1) != LUA_TNUMBER) + { + // first retain, set retain count = 1 + lua_pop(L, 1); + lua_pushinteger(L, retainCount); + } + else + { + // add retain count + retainCount = lua_tonumber(L, -1); + retainCount++; + lua_pop(L, 1); + lua_pushinteger(L, retainCount); + } + + lua_rawset(L, -3); /* id_r[id] = r, L: f ... f_id id_r */ + lua_pop(L, 2); /* L: f ... */ + + if (retainCountReturn) *retainCountReturn = retainCount; + return functionId; +} + +int LuaJavaBridge::fetchArrayElements(lua_State *L, int index) +{ + int count = 0; + do + { + lua_rawgeti(L, index - count, count + 1); + if (lua_isnil(L, -1)) + { + lua_pop(L, 1); + break; + } + ++count; + } while (1); + return count; +} diff --git a/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.h b/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.h new file mode 100644 index 0000000000..5bc526dcb0 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/platform/android/CCLuaJavaBridge.h @@ -0,0 +1,138 @@ +#ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_PLATFORM_ANDROID_LUA_JAVA_BRIDGE_H +#define COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_PLATFORM_ANDROID_LUA_JAVA_BRIDGE_H + +#include +#include +#include +#include "cocos2d.h" + +using namespace std; + +extern "C" { +#include "lua.h" +} + +using namespace cocos2d; + +#define LUAJ_ERR_OK 0 +#define LUAJ_ERR_TYPE_NOT_SUPPORT (-1) +#define LUAJ_ERR_INVALID_SIGNATURES (-2) +#define LUAJ_ERR_METHOD_NOT_FOUND (-3) +#define LUAJ_ERR_EXCEPTION_OCCURRED (-4) +#define LUAJ_ERR_VM_THREAD_DETACHED (-5) +#define LUAJ_ERR_VM_FAILURE (-6) + +#define LUAJ_REGISTRY_FUNCTION "luaj_function_id" // table[function] = id +#define LUAJ_REGISTRY_RETAIN "luaj_function_id_retain" // table[id] = retain count + + +class LuaJavaBridge +{ +public: + static void luaopen_luaj(lua_State *L); + + static int retainLuaFunctionById(int functionId); + static int releaseLuaFunctionById(int functionId); + + static int callLuaFunctionById(int functionId, const char *arg); + static int callLuaGlobalFunction(const char *functionName, const char *arg); + +private: + typedef enum + { + TypeInvalid = -1, + TypeVoid = 0, + TypeInteger = 1, + TypeFloat = 2, + TypeBoolean = 3, + TypeString = 4, + TypeVector = 5, + TypeFunction= 6, + } ValueType; + + typedef vector ValueTypes; + + typedef union + { + int intValue; + float floatValue; + int boolValue; + string *stringValue; + } ReturnValue; + + class CallInfo + { + public: + CallInfo(const char *className, const char *methodName, const char *methodSig) + : m_valid(false) + , m_error(LUAJ_ERR_OK) + , m_className(className) + , m_methodName(methodName) + , m_methodSig(methodSig) + , m_returnType(TypeVoid) + , m_argumentsCount(0) + , m_retjs(NULL) + , m_env(NULL) + , m_classID(NULL) + , m_methodID(NULL) + { + memset(&m_ret, 0, sizeof(m_ret)); + m_valid = validateMethodSig() && getMethodInfo(); + } + ~CallInfo(void); + + bool isValid(void) { + return m_valid; + } + + int getErrorCode(void) { + return m_error; + } + + JNIEnv *getEnv(void) { + return m_env; + } + + int argumentTypeAtIndex(size_t index) { + return m_argumentsType.at(index); + } + + bool execute(void); + bool executeWithArgs(jvalue *args); + int pushReturnValue(lua_State *L); + + + private: + bool m_valid; + int m_error; + + string m_className; + string m_methodName; + string m_methodSig; + int m_argumentsCount; + ValueTypes m_argumentsType; + ValueType m_returnType; + + ReturnValue m_ret; + jstring m_retjs; + + JNIEnv *m_env; + jclass m_classID; + jmethodID m_methodID; + + bool validateMethodSig(void); + bool getMethodInfo(void); + ValueType checkType(const string& sig, size_t *pos); + }; + + static int callJavaStaticMethod(lua_State *L); + static int retainLuaFunction(lua_State *L, int functionIndex, int *retainCountReturn); + static int getMethodInfo(CallInfo *call, const char *className, const char *methodName, const char *paramCode); + static int fetchArrayElements(lua_State *L, int index); + static int callAndPushReturnValue(lua_State *L, CallInfo *call, jvalue *args); + + static lua_State *s_luaState; + static int s_newFunctionId; +}; + +#endif //COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_PLATFORM_ANDROID_LUA_JAVA_BRIDGE_H diff --git a/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp b/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp new file mode 100644 index 0000000000..498122e6b3 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.cpp @@ -0,0 +1,45 @@ + +#include "Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.h" + +#include + +#include "CCLuaJavaBridge.h" + +#define LOG_TAG "Cocos2dxLuaJavaBridge_java" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) + +extern "C" { + +JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaFunctionWithString + (JNIEnv *env, jclass cls, jint functionId, jstring value) +{ + const char *value_ = env->GetStringUTFChars(value, 0); + int ret = LuaJavaBridge::callLuaFunctionById(functionId, value_); + env->ReleaseStringUTFChars(value, value_); + return ret; +} + +JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaGlobalFunctionWithString + (JNIEnv *env, jclass cls, jstring luaFunctionName, jstring value) +{ + const char *luaFunctionName_ = env->GetStringUTFChars(luaFunctionName, 0); + const char *value_ = env->GetStringUTFChars(value, 0); + int ret = LuaJavaBridge::callLuaGlobalFunction(luaFunctionName_, value_); + env->ReleaseStringUTFChars(luaFunctionName, luaFunctionName_); + env->ReleaseStringUTFChars(value, value_); + return ret; +} + +JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_retainLuaFunction + (JNIEnv *env, jclass cls, jint luaFunctionId) +{ + return LuaJavaBridge::retainLuaFunctionById(luaFunctionId); +} + +JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_releaseLuaFunction + (JNIEnv *env, jclass cls, jint luaFunctionId) +{ + return LuaJavaBridge::releaseLuaFunctionById(luaFunctionId); +} + +} // extern "C" diff --git a/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.h b/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.h new file mode 100644 index 0000000000..9a8531ffec --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge.h @@ -0,0 +1,42 @@ +#ifndef COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_PLATFORM_ANDROID_JNI_JAVA_ORG_COCOS2DX_LIB_COCOS2DX_LUA_JAVA_BRIDEG_H +#define COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_PLATFORM_ANDROID_JNI_JAVA_ORG_COCOS2DX_LIB_COCOS2DX_LUA_JAVA_BRIDEG_H + +#include + +extern "C" { + +/* + * Class: Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge + * Method: callLuaFunctionWithString + * Signature: (ILjava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaFunctionWithString + (JNIEnv *, jclass, jint, jstring); + +/* + * Class: Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge + * Method: callLuaGlobalFunctionWithString + * Signature: (Ljava/lang/String;Ljava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_callLuaGlobalFunctionWithString + (JNIEnv *env, jclass, jstring, jstring); + +/* + * Class: Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge + * Method: retainLuaFunction + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_retainLuaFunction + (JNIEnv *env, jclass, jint); + +/* + * Class: Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge + * Method: releaseLuaFunction + * Signature: (I)I + */ +JNIEXPORT jint JNICALL Java_org_cocos2dx_lib_Cocos2dxLuaJavaBridge_releaseLuaFunction + (JNIEnv *env, jclass, jint); + +} // extern "C" + +#endif // COCOS2DX_SCRIPT_LUA_COCOS2DX_SUPPORT_PLATFORM_ANDROID_JNI_JAVA_ORG_COCOS2DX_LIB_COCOS2DX_LUA_JAVA_BRIDEG_H diff --git a/cocos/scripting/lua-bindings/manual/platform/ios/CCLuaObjcBridge.h b/cocos/scripting/lua-bindings/manual/platform/ios/CCLuaObjcBridge.h new file mode 100644 index 0000000000..d169dfeb7d --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/platform/ios/CCLuaObjcBridge.h @@ -0,0 +1,22 @@ + +#ifndef __LUA_OBJC_BRIDGE_H_ +#define __LUA_OBJC_BRIDGE_H_ + +#include "CCLuaBridge.h" + +NS_CC_BEGIN + +class LuaObjcBridge : public LuaBridge +{ +public: + static void luaopen_luaoc(lua_State *L); + +protected: + static int callObjcStaticMethod(lua_State *L); + + static void pushValue(lua_State *L, void *val); +}; + +NS_CC_END + +#endif // __LUA_OBJC_BRIDGE_H_ diff --git a/cocos/scripting/lua-bindings/manual/platform/ios/CCLuaObjcBridge.mm b/cocos/scripting/lua-bindings/manual/platform/ios/CCLuaObjcBridge.mm new file mode 100644 index 0000000000..e011d2ec91 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/platform/ios/CCLuaObjcBridge.mm @@ -0,0 +1,218 @@ + +#include "CCLuaObjcBridge.h" +#include + +NS_CC_BEGIN + +void LuaObjcBridge::luaopen_luaoc(lua_State *L) +{ + s_luaState = L; + lua_newtable(L); + lua_pushstring(L, "callStaticMethod"); + lua_pushcfunction(L, LuaObjcBridge::callObjcStaticMethod); + lua_rawset(L, -3); + lua_setglobal(L, "LuaObjcBridge"); +} + +/** + className + methodName + args + */ +int LuaObjcBridge::callObjcStaticMethod(lua_State *L) +{ + if (lua_gettop(L) != 3 || !lua_isstring(L, -3) || !lua_isstring(L, -2)) + { + lua_pushboolean(L, 0); + lua_pushinteger(L, kLuaBridgeErrorInvalidParameters); + return 2; + } + + const char *className = lua_tostring(L, -3); + const char *methodName = lua_tostring(L, -2); + if (!className || !methodName) + { + lua_pushboolean(L, 0); + lua_pushinteger(L, kLuaBridgeErrorInvalidParameters); + return 2; + } + + Class targetClass = NSClassFromString([NSString stringWithCString:className encoding:NSUTF8StringEncoding]); + if (!targetClass) + { + lua_pushboolean(L, 0); + lua_pushinteger(L, kLuaBridgeErrorClassNotFound); + return 2; + } + + SEL methodSel; + bool hasArguments = lua_istable(L, -1); + if (hasArguments) + { + NSString *methodName_ = [NSString stringWithCString:methodName encoding:NSUTF8StringEncoding]; + methodName_ = [NSString stringWithFormat:@"%@:", methodName_]; + methodSel = NSSelectorFromString(methodName_); + } + else + { + methodSel = NSSelectorFromString([NSString stringWithCString:methodName encoding:NSUTF8StringEncoding]); + } + if (methodSel == (SEL)0) + { + lua_pushboolean(L, 0); + lua_pushinteger(L, kLuaBridgeErrorMethodNotFound); + return 2; + } + + NSMethodSignature *methodSig = [targetClass methodSignatureForSelector:(SEL)methodSel]; + if (methodSig == nil) + { + lua_pushboolean(L, 0); + lua_pushinteger(L, kLuaBridgeErrorMethodSignature); + return 2; + } + + @try { + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig]; + [invocation setTarget:targetClass]; + [invocation setSelector:methodSel]; + NSUInteger returnLength = [methodSig methodReturnLength]; + const char *returnType = [methodSig methodReturnType]; + + if (hasArguments) + { + NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + lua_pushnil(L); + while (lua_next(L, -2)) + { + NSString *key = [NSString stringWithCString:lua_tostring(L, -2) encoding:NSUTF8StringEncoding]; + + switch (lua_type(L, -1)) + { + case LUA_TNUMBER: + [dict setObject:[NSNumber numberWithFloat:lua_tonumber(L, -1)] forKey:key]; + break; + + case LUA_TBOOLEAN: + [dict setObject:[NSNumber numberWithBool:lua_toboolean(L, -1)] forKey:key]; + break; + + case LUA_TSTRING: + [dict setObject:[NSString stringWithCString:lua_tostring(L, -1) encoding:NSUTF8StringEncoding] + forKey:key]; + break; + + case LUA_TFUNCTION: + int functionId = retainLuaFunction(L, -1, NULL); + [dict setObject:[NSNumber numberWithInt:functionId] forKey:key]; + break; + } + + lua_pop(L, 1); + } + + [invocation setArgument:&dict atIndex:2]; + [invocation invoke]; + } + else + { + [invocation invoke]; + } + + lua_pushboolean(L, 1); + if (returnLength > 0) + { + if (strcmp(returnType, "@") == 0) + { + id ret; + [invocation getReturnValue:&ret]; + pushValue(L, ret); + } + else if (strcmp(returnType, "c") == 0) // BOOL + { + char ret; + [invocation getReturnValue:&ret]; + lua_pushboolean(L, ret); + } + else if (strcmp(returnType, "i") == 0) // int + { + int ret; + [invocation getReturnValue:&ret]; + lua_pushinteger(L, ret); + } + else if (strcmp(returnType, "f") == 0) // float + { + float ret; + [invocation getReturnValue:&ret]; + lua_pushnumber(L, ret); + } + else + { + NSLog(@"not support return type = %s", returnType); + lua_pushnil(L); + } + } + else + { + lua_pushnil(L); + } + return 2; + } + @catch (NSException *exception) + { + NSLog(@"EXCEPTION THROW: %@", exception); + lua_pushboolean(L, 0); + lua_pushinteger(L, kLuaBridgeErrorExceptionOccurred); + return 2; + } +} + +void LuaObjcBridge::pushValue(lua_State *L, void *val) +{ + id oval = (id)val; + if (oval == nil) + { + lua_pushnil(L); + } + else if ([oval isKindOfClass:[NSNumber class]]) + { + NSNumber *number = (NSNumber *)oval; + const char *numberType = [number objCType]; + if (strcmp(numberType, @encode(BOOL)) == 0) + { + lua_pushboolean(L, [number boolValue]); + } + else if (strcmp(numberType, @encode(int)) == 0) + { + lua_pushinteger(L, [number intValue]); + } + else + { + lua_pushnumber(L, [number floatValue]); + } + } + else if ([oval isKindOfClass:[NSString class]]) + { + lua_pushstring(L, [oval cStringUsingEncoding:NSUTF8StringEncoding]); + } + else if ([oval isKindOfClass:[NSDictionary class]]) + { + lua_newtable(L); + + for (id key in oval) + { + const char *key_ = [[NSString stringWithFormat:@"%@", key] cStringUsingEncoding:NSUTF8StringEncoding]; + lua_pushstring(L, key_); + pushValue(L, [oval objectForKey:key]); + lua_rawset(L, -3); + } + + return; + } + else + { + lua_pushstring(L, [[NSString stringWithFormat:@"%@", oval] cStringUsingEncoding:NSUTF8StringEncoding]); + } +} + +NS_CC_END diff --git a/cocos/scripting/lua-bindings/manual/tolua_fix.cpp b/cocos/scripting/lua-bindings/manual/tolua_fix.cpp new file mode 100644 index 0000000000..bd72d5dad5 --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/tolua_fix.cpp @@ -0,0 +1,258 @@ + +#include "tolua_fix.h" +#include "CCRef.h" +#include "LuaBasicConversions.h" +#include + +using namespace cocos2d; + +static int s_function_ref_id = 0; + +TOLUA_API void toluafix_open(lua_State* L) +{ + lua_pushstring(L, TOLUA_REFID_PTR_MAPPING); + lua_newtable(L); + lua_rawset(L, LUA_REGISTRYINDEX); + + lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING); + lua_newtable(L); + lua_rawset(L, LUA_REGISTRYINDEX); + + lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING); + lua_newtable(L); + lua_rawset(L, LUA_REGISTRYINDEX); +} + +TOLUA_API int toluafix_pushusertype_ccobject(lua_State* L, + int refid, + int* p_refid, + void* ptr, + const char* type) +{ + if (ptr == NULL || p_refid == NULL) + { + lua_pushnil(L); + return -1; + } + + Ref* vPtr = static_cast(ptr); + const char* vType = getLuaTypeName(vPtr, type); + + if (*p_refid == 0) + { + *p_refid = refid; + + lua_pushstring(L, TOLUA_REFID_PTR_MAPPING); + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_ptr */ + lua_pushinteger(L, refid); /* stack: refid_ptr refid */ + lua_pushlightuserdata(L, vPtr); /* stack: refid_ptr refid ptr */ + + lua_rawset(L, -3); /* refid_ptr[refid] = ptr, stack: refid_ptr */ + lua_pop(L, 1); /* stack: - */ + + lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING); + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_type */ + lua_pushinteger(L, refid); /* stack: refid_type refid */ + lua_pushstring(L, vType); /* stack: refid_type refid type */ + lua_rawset(L, -3); /* refid_type[refid] = type, stack: refid_type */ + lua_pop(L, 1); /* stack: - */ + + //printf("[LUA] push CCObject OK - refid: %d, ptr: %x, type: %s\n", *p_refid, (int)ptr, type); + } + + tolua_pushusertype_and_addtoroot(L, vPtr, vType); + + return 0; +} + +TOLUA_API int toluafix_remove_ccobject_by_refid(lua_State* L, int refid) +{ + void* ptr = NULL; + const char* type = NULL; + void** ud = NULL; + if (refid == 0) return -1; + + // get ptr from tolua_refid_ptr_mapping + lua_pushstring(L, TOLUA_REFID_PTR_MAPPING); + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_ptr */ + lua_pushinteger(L, refid); /* stack: refid_ptr refid */ + lua_rawget(L, -2); /* stack: refid_ptr ptr */ + ptr = lua_touserdata(L, -1); + lua_pop(L, 1); /* stack: refid_ptr */ + if (ptr == NULL) + { + lua_pop(L, 1); + // Lua stack has closed, C++ object not in Lua. + // printf("[LUA ERROR] remove CCObject with NULL ptr, refid: %d\n", refid); + return -2; + } + + // remove ptr from tolua_refid_ptr_mapping + lua_pushinteger(L, refid); /* stack: refid_ptr refid */ + lua_pushnil(L); /* stack: refid_ptr refid nil */ + lua_rawset(L, -3); /* delete refid_ptr[refid], stack: refid_ptr */ + lua_pop(L, 1); /* stack: - */ + + + // get type from tolua_refid_type_mapping + lua_pushstring(L, TOLUA_REFID_TYPE_MAPPING); + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: refid_type */ + lua_pushinteger(L, refid); /* stack: refid_type refid */ + lua_rawget(L, -2); /* stack: refid_type type */ + if (lua_isnil(L, -1)) + { + lua_pop(L, 2); + printf("[LUA ERROR] remove CCObject with NULL type, refid: %d, ptr: %p\n", refid, ptr); + return -1; + } + + type = lua_tostring(L, -1); + lua_pop(L, 1); /* stack: refid_type */ + + // remove type from tolua_refid_type_mapping + lua_pushinteger(L, refid); /* stack: refid_type refid */ + lua_pushnil(L); /* stack: refid_type refid nil */ + lua_rawset(L, -3); /* delete refid_type[refid], stack: refid_type */ + lua_pop(L, 1); /* stack: - */ + + // get ubox + luaL_getmetatable(L, type); /* stack: mt */ + lua_pushstring(L, "tolua_ubox"); /* stack: mt key */ + lua_rawget(L, -2); /* stack: mt ubox */ + if (lua_isnil(L, -1)) + { + // use global ubox + lua_pop(L, 1); /* stack: mt */ + lua_pushstring(L, "tolua_ubox"); /* stack: mt key */ + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: mt ubox */ + }; + + + // cleanup root + tolua_remove_value_from_root(L, ptr); + + lua_pushlightuserdata(L, ptr); /* stack: mt ubox ptr */ + lua_rawget(L,-2); /* stack: mt ubox ud */ + if (lua_isnil(L, -1)) + { + // Lua object has released (GC), C++ object not in ubox. + //printf("[LUA ERROR] remove CCObject with NULL ubox, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type); + lua_pop(L, 3); + return -3; + } + + // cleanup peertable + lua_pushvalue(L, LUA_REGISTRYINDEX); + lua_setfenv(L, -2); + + ud = (void**)lua_touserdata(L, -1); + lua_pop(L, 1); /* stack: mt ubox */ + if (ud == NULL) + { + printf("[LUA ERROR] remove CCObject with NULL userdata, refid: %d, ptr: %p, type: %s\n", refid, ptr, type); + lua_pop(L, 2); + return -1; + } + + // clean userdata + *ud = NULL; + + lua_pushlightuserdata(L, ptr); /* stack: mt ubox ptr */ + lua_pushnil(L); /* stack: mt ubox ptr nil */ + lua_rawset(L, -3); /* ubox[ptr] = nil, stack: mt ubox */ + + lua_pop(L, 2); + //printf("[LUA] remove CCObject, refid: %d, ptr: %x, type: %s\n", refid, (int)ptr, type); + return 0; +} + +TOLUA_API int toluafix_ref_function(lua_State* L, int lo, int def) +{ + // function at lo + if (!lua_isfunction(L, lo)) return 0; + + s_function_ref_id++; + + lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING); + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: fun ... refid_fun */ + lua_pushinteger(L, s_function_ref_id); /* stack: fun ... refid_fun refid */ + lua_pushvalue(L, lo); /* stack: fun ... refid_fun refid fun */ + + lua_rawset(L, -3); /* refid_fun[refid] = fun, stack: fun ... refid_ptr */ + lua_pop(L, 1); /* stack: fun ... */ + + return s_function_ref_id; + + // lua_pushvalue(L, lo); /* stack: ... func */ + // return luaL_ref(L, LUA_REGISTRYINDEX); +} + +TOLUA_API void toluafix_get_function_by_refid(lua_State* L, int refid) +{ + lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING); + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: ... refid_fun */ + lua_pushinteger(L, refid); /* stack: ... refid_fun refid */ + lua_rawget(L, -2); /* stack: ... refid_fun fun */ + lua_remove(L, -2); /* stack: ... fun */ +} + +TOLUA_API void toluafix_remove_function_by_refid(lua_State* L, int refid) +{ + lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING); + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: ... refid_fun */ + lua_pushinteger(L, refid); /* stack: ... refid_fun refid */ + lua_pushnil(L); /* stack: ... refid_fun refid nil */ + lua_rawset(L, -3); /* refid_fun[refid] = fun, stack: ... refid_ptr */ + lua_pop(L, 1); /* stack: ... */ + + // luaL_unref(L, LUA_REGISTRYINDEX, refid); +} + +// check lua value is funciton +TOLUA_API int toluafix_isfunction(lua_State* L, int lo, const char* type, int def, tolua_Error* err) +{ + if (lua_gettop(L) >= abs(lo) && lua_isfunction(L, lo)) + { + return 1; + } + err->index = lo; + err->array = 0; + err->type = "[not function]"; + return 0; +} + +TOLUA_API int toluafix_totable(lua_State* L, int lo, int def) +{ + return lo; +} + +TOLUA_API int toluafix_istable(lua_State* L, int lo, const char* type, int def, tolua_Error* err) +{ + return tolua_istable(L, lo, def, err); +} + +TOLUA_API void toluafix_stack_dump(lua_State* L, const char* label) +{ + int i; + int top = lua_gettop(L); + printf("Total [%d] in lua stack: %s\n", top, label != 0 ? label : ""); + for (i = -1; i >= -top; i--) + { + int t = lua_type(L, i); + switch (t) + { + case LUA_TSTRING: + printf(" [%02d] string %s\n", i, lua_tostring(L, i)); + break; + case LUA_TBOOLEAN: + printf(" [%02d] boolean %s\n", i, lua_toboolean(L, i) ? "true" : "false"); + break; + case LUA_TNUMBER: + printf(" [%02d] number %g\n", i, lua_tonumber(L, i)); + break; + default: + printf(" [%02d] %s\n", i, lua_typename(L, t)); + } + } + printf("\n"); +} diff --git a/cocos/scripting/lua-bindings/manual/tolua_fix.h b/cocos/scripting/lua-bindings/manual/tolua_fix.h new file mode 100644 index 0000000000..a8a989f08e --- /dev/null +++ b/cocos/scripting/lua-bindings/manual/tolua_fix.h @@ -0,0 +1,26 @@ + +#ifndef __TOLUA_FIX_H_ +#define __TOLUA_FIX_H_ + +#include "tolua++.h" + +#define TOLUA_REFID_PTR_MAPPING "toluafix_refid_ptr_mapping" +#define TOLUA_REFID_TYPE_MAPPING "toluafix_refid_type_mapping" +#define TOLUA_REFID_FUNCTION_MAPPING "toluafix_refid_function_mapping" + +TOLUA_API void toluafix_open(lua_State* L); +TOLUA_API int toluafix_pushusertype_ccobject(lua_State* L, + int uid, + int* p_refid, + void* ptr, + const char* type); +TOLUA_API int toluafix_remove_ccobject_by_refid(lua_State* L, int refid); +TOLUA_API int toluafix_ref_function(lua_State* L, int lo, int def); +TOLUA_API void toluafix_get_function_by_refid(lua_State* L, int refid); +TOLUA_API void toluafix_remove_function_by_refid(lua_State* L, int refid); +TOLUA_API int toluafix_isfunction(lua_State* L, int lo, const char* type, int def, tolua_Error* err); +TOLUA_API int toluafix_totable(lua_State* L, int lo, int def); +TOLUA_API int toluafix_istable(lua_State* L, int lo, const char* type, int def, tolua_Error* err); +TOLUA_API void toluafix_stack_dump(lua_State* L, const char* label); + +#endif // __TOLUA_FIX_H_ diff --git a/cocos/scripting/lua-bindings/proj.ios_mac/cocos2d_lua_bindings.xcodeproj/project.pbxproj b/cocos/scripting/lua-bindings/proj.ios_mac/cocos2d_lua_bindings.xcodeproj/project.pbxproj new file mode 100644 index 0000000000..74c7a9210d --- /dev/null +++ b/cocos/scripting/lua-bindings/proj.ios_mac/cocos2d_lua_bindings.xcodeproj/project.pbxproj @@ -0,0 +1,1035 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 1A262AB818BEEF5900D2DB92 /* tolua_fix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A262AB718BEEF5900D2DB92 /* tolua_fix.cpp */; }; + 1A262AB918BEEF5900D2DB92 /* tolua_fix.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1A262AB718BEEF5900D2DB92 /* tolua_fix.cpp */; }; + 1AACE7B518BC45C200215002 /* lua_cocos2dx_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE74918BC45C200215002 /* lua_cocos2dx_auto.cpp */; }; + 1AACE7B618BC45C200215002 /* lua_cocos2dx_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE74918BC45C200215002 /* lua_cocos2dx_auto.cpp */; }; + 1AACE7B718BC45C200215002 /* lua_cocos2dx_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE74A18BC45C200215002 /* lua_cocos2dx_auto.hpp */; }; + 1AACE7B818BC45C200215002 /* lua_cocos2dx_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE74A18BC45C200215002 /* lua_cocos2dx_auto.hpp */; }; + 1AACE7BB18BC45C200215002 /* lua_cocos2dx_extension_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE74C18BC45C200215002 /* lua_cocos2dx_extension_auto.cpp */; }; + 1AACE7BC18BC45C200215002 /* lua_cocos2dx_extension_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE74C18BC45C200215002 /* lua_cocos2dx_extension_auto.cpp */; }; + 1AACE7BD18BC45C200215002 /* lua_cocos2dx_extension_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE74D18BC45C200215002 /* lua_cocos2dx_extension_auto.hpp */; }; + 1AACE7BE18BC45C200215002 /* lua_cocos2dx_extension_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE74D18BC45C200215002 /* lua_cocos2dx_extension_auto.hpp */; }; + 1AACE7C118BC45C200215002 /* lua_cocos2dx_gui_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE74F18BC45C200215002 /* lua_cocos2dx_gui_auto.cpp */; }; + 1AACE7C218BC45C200215002 /* lua_cocos2dx_gui_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE74F18BC45C200215002 /* lua_cocos2dx_gui_auto.cpp */; }; + 1AACE7C318BC45C200215002 /* lua_cocos2dx_gui_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75018BC45C200215002 /* lua_cocos2dx_gui_auto.hpp */; }; + 1AACE7C418BC45C200215002 /* lua_cocos2dx_gui_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75018BC45C200215002 /* lua_cocos2dx_gui_auto.hpp */; }; + 1AACE7C718BC45C200215002 /* lua_cocos2dx_physics_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE75218BC45C200215002 /* lua_cocos2dx_physics_auto.cpp */; }; + 1AACE7C818BC45C200215002 /* lua_cocos2dx_physics_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE75218BC45C200215002 /* lua_cocos2dx_physics_auto.cpp */; }; + 1AACE7C918BC45C200215002 /* lua_cocos2dx_physics_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75318BC45C200215002 /* lua_cocos2dx_physics_auto.hpp */; }; + 1AACE7CA18BC45C200215002 /* lua_cocos2dx_physics_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75318BC45C200215002 /* lua_cocos2dx_physics_auto.hpp */; }; + 1AACE7CD18BC45C200215002 /* lua_cocos2dx_spine_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE75518BC45C200215002 /* lua_cocos2dx_spine_auto.cpp */; }; + 1AACE7CE18BC45C200215002 /* lua_cocos2dx_spine_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE75518BC45C200215002 /* lua_cocos2dx_spine_auto.cpp */; }; + 1AACE7CF18BC45C200215002 /* lua_cocos2dx_spine_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75618BC45C200215002 /* lua_cocos2dx_spine_auto.hpp */; }; + 1AACE7D018BC45C200215002 /* lua_cocos2dx_spine_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75618BC45C200215002 /* lua_cocos2dx_spine_auto.hpp */; }; + 1AACE7D318BC45C200215002 /* lua_cocos2dx_studio_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE75818BC45C200215002 /* lua_cocos2dx_studio_auto.cpp */; }; + 1AACE7D418BC45C200215002 /* lua_cocos2dx_studio_auto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE75818BC45C200215002 /* lua_cocos2dx_studio_auto.cpp */; }; + 1AACE7D518BC45C200215002 /* lua_cocos2dx_studio_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75918BC45C200215002 /* lua_cocos2dx_studio_auto.hpp */; }; + 1AACE7D618BC45C200215002 /* lua_cocos2dx_studio_auto.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75918BC45C200215002 /* lua_cocos2dx_studio_auto.hpp */; }; + 1AACE7D918BC45C200215002 /* CCBProxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE75E18BC45C200215002 /* CCBProxy.cpp */; }; + 1AACE7DA18BC45C200215002 /* CCBProxy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE75E18BC45C200215002 /* CCBProxy.cpp */; }; + 1AACE7DB18BC45C200215002 /* CCBProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75F18BC45C200215002 /* CCBProxy.h */; }; + 1AACE7DC18BC45C200215002 /* CCBProxy.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE75F18BC45C200215002 /* CCBProxy.h */; }; + 1AACE7DD18BC45C200215002 /* CCLuaBridge.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76018BC45C200215002 /* CCLuaBridge.cpp */; }; + 1AACE7DE18BC45C200215002 /* CCLuaBridge.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76018BC45C200215002 /* CCLuaBridge.cpp */; }; + 1AACE7DF18BC45C200215002 /* CCLuaBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76118BC45C200215002 /* CCLuaBridge.h */; }; + 1AACE7E018BC45C200215002 /* CCLuaBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76118BC45C200215002 /* CCLuaBridge.h */; }; + 1AACE7E118BC45C200215002 /* CCLuaEngine.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76218BC45C200215002 /* CCLuaEngine.cpp */; }; + 1AACE7E218BC45C200215002 /* CCLuaEngine.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76218BC45C200215002 /* CCLuaEngine.cpp */; }; + 1AACE7E318BC45C200215002 /* CCLuaEngine.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76318BC45C200215002 /* CCLuaEngine.h */; }; + 1AACE7E418BC45C200215002 /* CCLuaEngine.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76318BC45C200215002 /* CCLuaEngine.h */; }; + 1AACE7E518BC45C200215002 /* CCLuaStack.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76418BC45C200215002 /* CCLuaStack.cpp */; }; + 1AACE7E618BC45C200215002 /* CCLuaStack.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76418BC45C200215002 /* CCLuaStack.cpp */; }; + 1AACE7E718BC45C200215002 /* CCLuaStack.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76518BC45C200215002 /* CCLuaStack.h */; }; + 1AACE7E818BC45C200215002 /* CCLuaStack.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76518BC45C200215002 /* CCLuaStack.h */; }; + 1AACE7E918BC45C200215002 /* CCLuaValue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76618BC45C200215002 /* CCLuaValue.cpp */; }; + 1AACE7EA18BC45C200215002 /* CCLuaValue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76618BC45C200215002 /* CCLuaValue.cpp */; }; + 1AACE7EB18BC45C200215002 /* CCLuaValue.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76718BC45C200215002 /* CCLuaValue.h */; }; + 1AACE7EC18BC45C200215002 /* CCLuaValue.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76718BC45C200215002 /* CCLuaValue.h */; }; + 1AACE7ED18BC45C200215002 /* Cocos2dxLuaLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76818BC45C200215002 /* Cocos2dxLuaLoader.cpp */; }; + 1AACE7EE18BC45C200215002 /* Cocos2dxLuaLoader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76818BC45C200215002 /* Cocos2dxLuaLoader.cpp */; }; + 1AACE7EF18BC45C200215002 /* Cocos2dxLuaLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76918BC45C200215002 /* Cocos2dxLuaLoader.h */; }; + 1AACE7F018BC45C200215002 /* Cocos2dxLuaLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76918BC45C200215002 /* Cocos2dxLuaLoader.h */; }; + 1AACE7F118BC45C200215002 /* lua_cocos2dx_coco_studio_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76A18BC45C200215002 /* lua_cocos2dx_coco_studio_manual.cpp */; }; + 1AACE7F218BC45C200215002 /* lua_cocos2dx_coco_studio_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76A18BC45C200215002 /* lua_cocos2dx_coco_studio_manual.cpp */; }; + 1AACE7F318BC45C200215002 /* lua_cocos2dx_coco_studio_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76B18BC45C200215002 /* lua_cocos2dx_coco_studio_manual.hpp */; }; + 1AACE7F418BC45C200215002 /* lua_cocos2dx_coco_studio_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76B18BC45C200215002 /* lua_cocos2dx_coco_studio_manual.hpp */; }; + 1AACE7F518BC45C200215002 /* lua_cocos2dx_deprecated.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76C18BC45C200215002 /* lua_cocos2dx_deprecated.cpp */; }; + 1AACE7F618BC45C200215002 /* lua_cocos2dx_deprecated.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76C18BC45C200215002 /* lua_cocos2dx_deprecated.cpp */; }; + 1AACE7F718BC45C200215002 /* lua_cocos2dx_deprecated.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76D18BC45C200215002 /* lua_cocos2dx_deprecated.h */; }; + 1AACE7F818BC45C200215002 /* lua_cocos2dx_deprecated.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76D18BC45C200215002 /* lua_cocos2dx_deprecated.h */; }; + 1AACE7F918BC45C200215002 /* lua_cocos2dx_extension_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76E18BC45C200215002 /* lua_cocos2dx_extension_manual.cpp */; }; + 1AACE7FA18BC45C200215002 /* lua_cocos2dx_extension_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE76E18BC45C200215002 /* lua_cocos2dx_extension_manual.cpp */; }; + 1AACE7FB18BC45C200215002 /* lua_cocos2dx_extension_manual.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76F18BC45C200215002 /* lua_cocos2dx_extension_manual.h */; }; + 1AACE7FC18BC45C200215002 /* lua_cocos2dx_extension_manual.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE76F18BC45C200215002 /* lua_cocos2dx_extension_manual.h */; }; + 1AACE7FD18BC45C200215002 /* lua_cocos2dx_gui_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77018BC45C200215002 /* lua_cocos2dx_gui_manual.cpp */; }; + 1AACE7FE18BC45C200215002 /* lua_cocos2dx_gui_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77018BC45C200215002 /* lua_cocos2dx_gui_manual.cpp */; }; + 1AACE7FF18BC45C200215002 /* lua_cocos2dx_gui_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77118BC45C200215002 /* lua_cocos2dx_gui_manual.hpp */; }; + 1AACE80018BC45C200215002 /* lua_cocos2dx_gui_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77118BC45C200215002 /* lua_cocos2dx_gui_manual.hpp */; }; + 1AACE80118BC45C200215002 /* lua_cocos2dx_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77218BC45C200215002 /* lua_cocos2dx_manual.cpp */; }; + 1AACE80218BC45C200215002 /* lua_cocos2dx_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77218BC45C200215002 /* lua_cocos2dx_manual.cpp */; }; + 1AACE80318BC45C200215002 /* lua_cocos2dx_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77318BC45C200215002 /* lua_cocos2dx_manual.hpp */; }; + 1AACE80418BC45C200215002 /* lua_cocos2dx_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77318BC45C200215002 /* lua_cocos2dx_manual.hpp */; }; + 1AACE80518BC45C200215002 /* lua_cocos2dx_physics_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77418BC45C200215002 /* lua_cocos2dx_physics_manual.cpp */; }; + 1AACE80618BC45C200215002 /* lua_cocos2dx_physics_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77418BC45C200215002 /* lua_cocos2dx_physics_manual.cpp */; }; + 1AACE80718BC45C200215002 /* lua_cocos2dx_physics_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77518BC45C200215002 /* lua_cocos2dx_physics_manual.hpp */; }; + 1AACE80818BC45C200215002 /* lua_cocos2dx_physics_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77518BC45C200215002 /* lua_cocos2dx_physics_manual.hpp */; }; + 1AACE80918BC45C200215002 /* lua_cocos2dx_spine_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77618BC45C200215002 /* lua_cocos2dx_spine_manual.cpp */; }; + 1AACE80A18BC45C200215002 /* lua_cocos2dx_spine_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77618BC45C200215002 /* lua_cocos2dx_spine_manual.cpp */; }; + 1AACE80B18BC45C200215002 /* lua_cocos2dx_spine_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77718BC45C200215002 /* lua_cocos2dx_spine_manual.hpp */; }; + 1AACE80C18BC45C200215002 /* lua_cocos2dx_spine_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77718BC45C200215002 /* lua_cocos2dx_spine_manual.hpp */; }; + 1AACE80D18BC45C200215002 /* lua_extensions.c in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77818BC45C200215002 /* lua_extensions.c */; }; + 1AACE80E18BC45C200215002 /* lua_extensions.c in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77818BC45C200215002 /* lua_extensions.c */; }; + 1AACE80F18BC45C200215002 /* lua_extensions.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77918BC45C200215002 /* lua_extensions.h */; }; + 1AACE81018BC45C200215002 /* lua_extensions.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77918BC45C200215002 /* lua_extensions.h */; }; + 1AACE81118BC45C200215002 /* Lua_web_socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77A18BC45C200215002 /* Lua_web_socket.cpp */; }; + 1AACE81218BC45C200215002 /* Lua_web_socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77A18BC45C200215002 /* Lua_web_socket.cpp */; }; + 1AACE81318BC45C200215002 /* Lua_web_socket.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77B18BC45C200215002 /* Lua_web_socket.h */; }; + 1AACE81418BC45C200215002 /* Lua_web_socket.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77B18BC45C200215002 /* Lua_web_socket.h */; }; + 1AACE81518BC45C200215002 /* lua_xml_http_request.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77C18BC45C200215002 /* lua_xml_http_request.cpp */; }; + 1AACE81618BC45C200215002 /* lua_xml_http_request.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77C18BC45C200215002 /* lua_xml_http_request.cpp */; }; + 1AACE81718BC45C200215002 /* lua_xml_http_request.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77D18BC45C200215002 /* lua_xml_http_request.h */; }; + 1AACE81818BC45C200215002 /* lua_xml_http_request.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77D18BC45C200215002 /* lua_xml_http_request.h */; }; + 1AACE81918BC45C200215002 /* LuaBasicConversions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77E18BC45C200215002 /* LuaBasicConversions.cpp */; }; + 1AACE81A18BC45C200215002 /* LuaBasicConversions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE77E18BC45C200215002 /* LuaBasicConversions.cpp */; }; + 1AACE81B18BC45C200215002 /* LuaBasicConversions.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77F18BC45C200215002 /* LuaBasicConversions.h */; }; + 1AACE81C18BC45C200215002 /* LuaBasicConversions.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE77F18BC45C200215002 /* LuaBasicConversions.h */; }; + 1AACE81D18BC45C200215002 /* LuaOpengl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE78018BC45C200215002 /* LuaOpengl.cpp */; }; + 1AACE81E18BC45C200215002 /* LuaOpengl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE78018BC45C200215002 /* LuaOpengl.cpp */; }; + 1AACE81F18BC45C200215002 /* LuaOpengl.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE78118BC45C200215002 /* LuaOpengl.h */; }; + 1AACE82018BC45C200215002 /* LuaOpengl.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE78118BC45C200215002 /* LuaOpengl.h */; }; + 1AACE82118BC45C200215002 /* LuaScriptHandlerMgr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE78218BC45C200215002 /* LuaScriptHandlerMgr.cpp */; }; + 1AACE82218BC45C200215002 /* LuaScriptHandlerMgr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE78218BC45C200215002 /* LuaScriptHandlerMgr.cpp */; }; + 1AACE82318BC45C200215002 /* LuaScriptHandlerMgr.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE78318BC45C200215002 /* LuaScriptHandlerMgr.h */; }; + 1AACE82418BC45C200215002 /* LuaScriptHandlerMgr.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE78318BC45C200215002 /* LuaScriptHandlerMgr.h */; }; + 1AACE82518BC45C200215002 /* LuaSkeletonAnimation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE78418BC45C200215002 /* LuaSkeletonAnimation.cpp */; }; + 1AACE82618BC45C200215002 /* LuaSkeletonAnimation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE78418BC45C200215002 /* LuaSkeletonAnimation.cpp */; }; + 1AACE82718BC45C200215002 /* LuaSkeletonAnimation.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE78518BC45C200215002 /* LuaSkeletonAnimation.h */; }; + 1AACE82818BC45C200215002 /* LuaSkeletonAnimation.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE78518BC45C200215002 /* LuaSkeletonAnimation.h */; }; + 1AACE83118BC45C200215002 /* CCLuaObjcBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE78E18BC45C200215002 /* CCLuaObjcBridge.h */; }; + 1AACE83218BC45C200215002 /* CCLuaObjcBridge.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE78E18BC45C200215002 /* CCLuaObjcBridge.h */; }; + 1AACE83318BC45C200215002 /* CCLuaObjcBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE78F18BC45C200215002 /* CCLuaObjcBridge.mm */; }; + 1AACE83418BC45C200215002 /* CCLuaObjcBridge.mm in Sources */ = {isa = PBXBuildFile; fileRef = 1AACE78F18BC45C200215002 /* CCLuaObjcBridge.mm */; }; + 1AACE87B18BC45C200215002 /* tolua_fix.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE7B418BC45C200215002 /* tolua_fix.h */; }; + 1AACE87C18BC45C200215002 /* tolua_fix.h in Headers */ = {isa = PBXBuildFile; fileRef = 1AACE7B418BC45C200215002 /* tolua_fix.h */; }; + 1ABCA1EA18CD8F470087CE3A /* lauxlib.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1E618CD8F470087CE3A /* lauxlib.h */; }; + 1ABCA1EB18CD8F470087CE3A /* lauxlib.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1E618CD8F470087CE3A /* lauxlib.h */; }; + 1ABCA1EC18CD8F470087CE3A /* lua.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1E718CD8F470087CE3A /* lua.h */; }; + 1ABCA1ED18CD8F470087CE3A /* lua.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1E718CD8F470087CE3A /* lua.h */; }; + 1ABCA1EE18CD8F470087CE3A /* luaconf.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1E818CD8F470087CE3A /* luaconf.h */; }; + 1ABCA1EF18CD8F470087CE3A /* luaconf.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1E818CD8F470087CE3A /* luaconf.h */; }; + 1ABCA1F018CD8F470087CE3A /* lualib.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1E918CD8F470087CE3A /* lualib.h */; }; + 1ABCA1F118CD8F470087CE3A /* lualib.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1E918CD8F470087CE3A /* lualib.h */; }; + 1ABCA1F418CD8F540087CE3A /* libluajit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1ABCA1F318CD8F540087CE3A /* libluajit.a */; }; + 1ABCA20018CD8F6E0087CE3A /* tolua_event.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1F918CD8F6E0087CE3A /* tolua_event.c */; }; + 1ABCA20118CD8F6E0087CE3A /* tolua_event.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1F918CD8F6E0087CE3A /* tolua_event.c */; }; + 1ABCA20218CD8F6E0087CE3A /* tolua_event.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1FA18CD8F6E0087CE3A /* tolua_event.h */; }; + 1ABCA20318CD8F6E0087CE3A /* tolua_event.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1FA18CD8F6E0087CE3A /* tolua_event.h */; }; + 1ABCA20418CD8F6E0087CE3A /* tolua_is.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1FB18CD8F6E0087CE3A /* tolua_is.c */; }; + 1ABCA20518CD8F6E0087CE3A /* tolua_is.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1FB18CD8F6E0087CE3A /* tolua_is.c */; }; + 1ABCA20618CD8F6E0087CE3A /* tolua_map.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1FC18CD8F6E0087CE3A /* tolua_map.c */; }; + 1ABCA20718CD8F6E0087CE3A /* tolua_map.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1FC18CD8F6E0087CE3A /* tolua_map.c */; }; + 1ABCA20818CD8F6E0087CE3A /* tolua_push.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1FD18CD8F6E0087CE3A /* tolua_push.c */; }; + 1ABCA20918CD8F6E0087CE3A /* tolua_push.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1FD18CD8F6E0087CE3A /* tolua_push.c */; }; + 1ABCA20A18CD8F6E0087CE3A /* tolua_to.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1FE18CD8F6E0087CE3A /* tolua_to.c */; }; + 1ABCA20B18CD8F6E0087CE3A /* tolua_to.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA1FE18CD8F6E0087CE3A /* tolua_to.c */; }; + 1ABCA20C18CD8F6E0087CE3A /* tolua++.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1FF18CD8F6E0087CE3A /* tolua++.h */; }; + 1ABCA20D18CD8F6E0087CE3A /* tolua++.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA1FF18CD8F6E0087CE3A /* tolua++.h */; }; + 1ABCA23118CD8F7D0087CE3A /* auxiliar.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA20F18CD8F7D0087CE3A /* auxiliar.c */; }; + 1ABCA23218CD8F7D0087CE3A /* auxiliar.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA20F18CD8F7D0087CE3A /* auxiliar.c */; }; + 1ABCA23318CD8F7D0087CE3A /* auxiliar.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21018CD8F7D0087CE3A /* auxiliar.h */; }; + 1ABCA23418CD8F7D0087CE3A /* auxiliar.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21018CD8F7D0087CE3A /* auxiliar.h */; }; + 1ABCA23518CD8F7D0087CE3A /* except.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21118CD8F7D0087CE3A /* except.c */; }; + 1ABCA23618CD8F7D0087CE3A /* except.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21118CD8F7D0087CE3A /* except.c */; }; + 1ABCA23718CD8F7D0087CE3A /* except.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21218CD8F7D0087CE3A /* except.h */; }; + 1ABCA23818CD8F7D0087CE3A /* except.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21218CD8F7D0087CE3A /* except.h */; }; + 1ABCA23918CD8F7D0087CE3A /* inet.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21318CD8F7D0087CE3A /* inet.c */; }; + 1ABCA23A18CD8F7D0087CE3A /* inet.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21318CD8F7D0087CE3A /* inet.c */; }; + 1ABCA23B18CD8F7D0087CE3A /* inet.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21418CD8F7D0087CE3A /* inet.h */; }; + 1ABCA23C18CD8F7D0087CE3A /* inet.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21418CD8F7D0087CE3A /* inet.h */; }; + 1ABCA23D18CD8F7D0087CE3A /* luasocket.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21518CD8F7D0087CE3A /* luasocket.c */; }; + 1ABCA23E18CD8F7D0087CE3A /* luasocket.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21518CD8F7D0087CE3A /* luasocket.c */; }; + 1ABCA23F18CD8F7D0087CE3A /* luasocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21618CD8F7D0087CE3A /* luasocket.h */; }; + 1ABCA24018CD8F7D0087CE3A /* luasocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21618CD8F7D0087CE3A /* luasocket.h */; }; + 1ABCA24118CD8F7D0087CE3A /* luasocket_buffer.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21718CD8F7D0087CE3A /* luasocket_buffer.c */; }; + 1ABCA24218CD8F7D0087CE3A /* luasocket_buffer.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21718CD8F7D0087CE3A /* luasocket_buffer.c */; }; + 1ABCA24318CD8F7D0087CE3A /* luasocket_buffer.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21818CD8F7D0087CE3A /* luasocket_buffer.h */; }; + 1ABCA24418CD8F7D0087CE3A /* luasocket_buffer.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21818CD8F7D0087CE3A /* luasocket_buffer.h */; }; + 1ABCA24518CD8F7D0087CE3A /* luasocket_io.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21918CD8F7D0087CE3A /* luasocket_io.c */; }; + 1ABCA24618CD8F7D0087CE3A /* luasocket_io.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21918CD8F7D0087CE3A /* luasocket_io.c */; }; + 1ABCA24718CD8F7D0087CE3A /* luasocket_io.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21A18CD8F7D0087CE3A /* luasocket_io.h */; }; + 1ABCA24818CD8F7D0087CE3A /* luasocket_io.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21A18CD8F7D0087CE3A /* luasocket_io.h */; }; + 1ABCA24918CD8F7D0087CE3A /* mime.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21B18CD8F7D0087CE3A /* mime.c */; }; + 1ABCA24A18CD8F7D0087CE3A /* mime.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21B18CD8F7D0087CE3A /* mime.c */; }; + 1ABCA24B18CD8F7D0087CE3A /* mime.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21C18CD8F7D0087CE3A /* mime.h */; }; + 1ABCA24C18CD8F7D0087CE3A /* mime.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21C18CD8F7D0087CE3A /* mime.h */; }; + 1ABCA24D18CD8F7D0087CE3A /* options.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21D18CD8F7D0087CE3A /* options.c */; }; + 1ABCA24E18CD8F7D0087CE3A /* options.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21D18CD8F7D0087CE3A /* options.c */; }; + 1ABCA24F18CD8F7D0087CE3A /* options.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21E18CD8F7D0087CE3A /* options.h */; }; + 1ABCA25018CD8F7D0087CE3A /* options.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA21E18CD8F7D0087CE3A /* options.h */; }; + 1ABCA25118CD8F7D0087CE3A /* select.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21F18CD8F7D0087CE3A /* select.c */; }; + 1ABCA25218CD8F7D0087CE3A /* select.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA21F18CD8F7D0087CE3A /* select.c */; }; + 1ABCA25318CD8F7D0087CE3A /* select.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22018CD8F7D0087CE3A /* select.h */; }; + 1ABCA25418CD8F7D0087CE3A /* select.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22018CD8F7D0087CE3A /* select.h */; }; + 1ABCA25518CD8F7D0087CE3A /* serial.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22118CD8F7D0087CE3A /* serial.c */; }; + 1ABCA25618CD8F7D0087CE3A /* serial.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22118CD8F7D0087CE3A /* serial.c */; }; + 1ABCA25718CD8F7D0087CE3A /* socket.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22218CD8F7D0087CE3A /* socket.h */; }; + 1ABCA25818CD8F7D0087CE3A /* socket.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22218CD8F7D0087CE3A /* socket.h */; }; + 1ABCA25918CD8F7D0087CE3A /* socket_scripts.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22318CD8F7D0087CE3A /* socket_scripts.c */; }; + 1ABCA25A18CD8F7D0087CE3A /* socket_scripts.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22318CD8F7D0087CE3A /* socket_scripts.c */; }; + 1ABCA25B18CD8F7D0087CE3A /* socket_scripts.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22418CD8F7D0087CE3A /* socket_scripts.h */; }; + 1ABCA25C18CD8F7D0087CE3A /* socket_scripts.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22418CD8F7D0087CE3A /* socket_scripts.h */; }; + 1ABCA25D18CD8F7D0087CE3A /* tcp.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22518CD8F7D0087CE3A /* tcp.c */; }; + 1ABCA25E18CD8F7D0087CE3A /* tcp.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22518CD8F7D0087CE3A /* tcp.c */; }; + 1ABCA25F18CD8F7D0087CE3A /* tcp.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22618CD8F7D0087CE3A /* tcp.h */; }; + 1ABCA26018CD8F7D0087CE3A /* tcp.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22618CD8F7D0087CE3A /* tcp.h */; }; + 1ABCA26118CD8F7D0087CE3A /* timeout.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22718CD8F7D0087CE3A /* timeout.c */; }; + 1ABCA26218CD8F7D0087CE3A /* timeout.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22718CD8F7D0087CE3A /* timeout.c */; }; + 1ABCA26318CD8F7D0087CE3A /* timeout.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22818CD8F7D0087CE3A /* timeout.h */; }; + 1ABCA26418CD8F7D0087CE3A /* timeout.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22818CD8F7D0087CE3A /* timeout.h */; }; + 1ABCA26518CD8F7D0087CE3A /* udp.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22918CD8F7D0087CE3A /* udp.c */; }; + 1ABCA26618CD8F7D0087CE3A /* udp.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22918CD8F7D0087CE3A /* udp.c */; }; + 1ABCA26718CD8F7D0087CE3A /* udp.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22A18CD8F7D0087CE3A /* udp.h */; }; + 1ABCA26818CD8F7D0087CE3A /* udp.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22A18CD8F7D0087CE3A /* udp.h */; }; + 1ABCA26918CD8F7D0087CE3A /* unix.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22B18CD8F7D0087CE3A /* unix.c */; }; + 1ABCA26A18CD8F7D0087CE3A /* unix.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22B18CD8F7D0087CE3A /* unix.c */; }; + 1ABCA26B18CD8F7D0087CE3A /* unix.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22C18CD8F7D0087CE3A /* unix.h */; }; + 1ABCA26C18CD8F7D0087CE3A /* unix.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22C18CD8F7D0087CE3A /* unix.h */; }; + 1ABCA26D18CD8F7D0087CE3A /* usocket.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22D18CD8F7D0087CE3A /* usocket.c */; }; + 1ABCA26E18CD8F7D0087CE3A /* usocket.c in Sources */ = {isa = PBXBuildFile; fileRef = 1ABCA22D18CD8F7D0087CE3A /* usocket.c */; }; + 1ABCA26F18CD8F7D0087CE3A /* usocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22E18CD8F7D0087CE3A /* usocket.h */; }; + 1ABCA27018CD8F7D0087CE3A /* usocket.h in Headers */ = {isa = PBXBuildFile; fileRef = 1ABCA22E18CD8F7D0087CE3A /* usocket.h */; }; + 1ABCA36318CD9D7F0087CE3A /* libluajit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 1ABCA1F618CD8F5F0087CE3A /* libluajit.a */; }; + C0FEF4D618BE0E70001F446C /* lua_debugger.c in Sources */ = {isa = PBXBuildFile; fileRef = C0FEF4D418BE0E70001F446C /* lua_debugger.c */; }; + C0FEF4D718BE0E70001F446C /* lua_debugger.c in Sources */ = {isa = PBXBuildFile; fileRef = C0FEF4D418BE0E70001F446C /* lua_debugger.c */; }; + C0FEF4D818BE0E70001F446C /* lua_debugger.h in Headers */ = {isa = PBXBuildFile; fileRef = C0FEF4D518BE0E70001F446C /* lua_debugger.h */; }; + C0FEF4D918BE0E70001F446C /* lua_debugger.h in Headers */ = {isa = PBXBuildFile; fileRef = C0FEF4D518BE0E70001F446C /* lua_debugger.h */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 1A262AB718BEEF5900D2DB92 /* tolua_fix.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tolua_fix.cpp; sourceTree = ""; }; + 1AACE74618BC45A000215002 /* libluabindings Mac.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libluabindings Mac.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + 1AACE74718BC45A000215002 /* libluabindings iOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libluabindings iOS.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + 1AACE74918BC45C200215002 /* lua_cocos2dx_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_auto.cpp; sourceTree = ""; }; + 1AACE74A18BC45C200215002 /* lua_cocos2dx_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_auto.hpp; sourceTree = ""; }; + 1AACE74C18BC45C200215002 /* lua_cocos2dx_extension_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_extension_auto.cpp; sourceTree = ""; }; + 1AACE74D18BC45C200215002 /* lua_cocos2dx_extension_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_extension_auto.hpp; sourceTree = ""; }; + 1AACE74F18BC45C200215002 /* lua_cocos2dx_gui_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_gui_auto.cpp; sourceTree = ""; }; + 1AACE75018BC45C200215002 /* lua_cocos2dx_gui_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_gui_auto.hpp; sourceTree = ""; }; + 1AACE75218BC45C200215002 /* lua_cocos2dx_physics_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_physics_auto.cpp; sourceTree = ""; }; + 1AACE75318BC45C200215002 /* lua_cocos2dx_physics_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_physics_auto.hpp; sourceTree = ""; }; + 1AACE75518BC45C200215002 /* lua_cocos2dx_spine_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_spine_auto.cpp; sourceTree = ""; }; + 1AACE75618BC45C200215002 /* lua_cocos2dx_spine_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_spine_auto.hpp; sourceTree = ""; }; + 1AACE75818BC45C200215002 /* lua_cocos2dx_studio_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_studio_auto.cpp; sourceTree = ""; }; + 1AACE75918BC45C200215002 /* lua_cocos2dx_studio_auto.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_studio_auto.hpp; sourceTree = ""; }; + 1AACE75E18BC45C200215002 /* CCBProxy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCBProxy.cpp; sourceTree = ""; }; + 1AACE75F18BC45C200215002 /* CCBProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCBProxy.h; sourceTree = ""; }; + 1AACE76018BC45C200215002 /* CCLuaBridge.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCLuaBridge.cpp; sourceTree = ""; }; + 1AACE76118BC45C200215002 /* CCLuaBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCLuaBridge.h; sourceTree = ""; }; + 1AACE76218BC45C200215002 /* CCLuaEngine.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCLuaEngine.cpp; sourceTree = ""; }; + 1AACE76318BC45C200215002 /* CCLuaEngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCLuaEngine.h; sourceTree = ""; }; + 1AACE76418BC45C200215002 /* CCLuaStack.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCLuaStack.cpp; sourceTree = ""; }; + 1AACE76518BC45C200215002 /* CCLuaStack.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCLuaStack.h; sourceTree = ""; }; + 1AACE76618BC45C200215002 /* CCLuaValue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCLuaValue.cpp; sourceTree = ""; }; + 1AACE76718BC45C200215002 /* CCLuaValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCLuaValue.h; sourceTree = ""; }; + 1AACE76818BC45C200215002 /* Cocos2dxLuaLoader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Cocos2dxLuaLoader.cpp; sourceTree = ""; }; + 1AACE76918BC45C200215002 /* Cocos2dxLuaLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Cocos2dxLuaLoader.h; sourceTree = ""; }; + 1AACE76A18BC45C200215002 /* lua_cocos2dx_coco_studio_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_coco_studio_manual.cpp; sourceTree = ""; }; + 1AACE76B18BC45C200215002 /* lua_cocos2dx_coco_studio_manual.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_coco_studio_manual.hpp; sourceTree = ""; }; + 1AACE76C18BC45C200215002 /* lua_cocos2dx_deprecated.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_deprecated.cpp; sourceTree = ""; }; + 1AACE76D18BC45C200215002 /* lua_cocos2dx_deprecated.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_cocos2dx_deprecated.h; sourceTree = ""; }; + 1AACE76E18BC45C200215002 /* lua_cocos2dx_extension_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_extension_manual.cpp; sourceTree = ""; }; + 1AACE76F18BC45C200215002 /* lua_cocos2dx_extension_manual.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_cocos2dx_extension_manual.h; sourceTree = ""; }; + 1AACE77018BC45C200215002 /* lua_cocos2dx_gui_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_gui_manual.cpp; sourceTree = ""; }; + 1AACE77118BC45C200215002 /* lua_cocos2dx_gui_manual.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_gui_manual.hpp; sourceTree = ""; }; + 1AACE77218BC45C200215002 /* lua_cocos2dx_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_manual.cpp; sourceTree = ""; }; + 1AACE77318BC45C200215002 /* lua_cocos2dx_manual.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_manual.hpp; sourceTree = ""; }; + 1AACE77418BC45C200215002 /* lua_cocos2dx_physics_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_physics_manual.cpp; sourceTree = ""; }; + 1AACE77518BC45C200215002 /* lua_cocos2dx_physics_manual.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_physics_manual.hpp; sourceTree = ""; }; + 1AACE77618BC45C200215002 /* lua_cocos2dx_spine_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_cocos2dx_spine_manual.cpp; sourceTree = ""; }; + 1AACE77718BC45C200215002 /* lua_cocos2dx_spine_manual.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_cocos2dx_spine_manual.hpp; sourceTree = ""; }; + 1AACE77818BC45C200215002 /* lua_extensions.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = lua_extensions.c; sourceTree = ""; }; + 1AACE77918BC45C200215002 /* lua_extensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_extensions.h; sourceTree = ""; }; + 1AACE77A18BC45C200215002 /* Lua_web_socket.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Lua_web_socket.cpp; sourceTree = ""; }; + 1AACE77B18BC45C200215002 /* Lua_web_socket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Lua_web_socket.h; sourceTree = ""; }; + 1AACE77C18BC45C200215002 /* lua_xml_http_request.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_xml_http_request.cpp; sourceTree = ""; }; + 1AACE77D18BC45C200215002 /* lua_xml_http_request.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_xml_http_request.h; sourceTree = ""; }; + 1AACE77E18BC45C200215002 /* LuaBasicConversions.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LuaBasicConversions.cpp; sourceTree = ""; }; + 1AACE77F18BC45C200215002 /* LuaBasicConversions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LuaBasicConversions.h; sourceTree = ""; }; + 1AACE78018BC45C200215002 /* LuaOpengl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LuaOpengl.cpp; sourceTree = ""; }; + 1AACE78118BC45C200215002 /* LuaOpengl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LuaOpengl.h; sourceTree = ""; }; + 1AACE78218BC45C200215002 /* LuaScriptHandlerMgr.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LuaScriptHandlerMgr.cpp; sourceTree = ""; }; + 1AACE78318BC45C200215002 /* LuaScriptHandlerMgr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LuaScriptHandlerMgr.h; sourceTree = ""; }; + 1AACE78418BC45C200215002 /* LuaSkeletonAnimation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LuaSkeletonAnimation.cpp; sourceTree = ""; }; + 1AACE78518BC45C200215002 /* LuaSkeletonAnimation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LuaSkeletonAnimation.h; sourceTree = ""; }; + 1AACE78E18BC45C200215002 /* CCLuaObjcBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCLuaObjcBridge.h; sourceTree = ""; }; + 1AACE78F18BC45C200215002 /* CCLuaObjcBridge.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CCLuaObjcBridge.mm; sourceTree = ""; }; + 1AACE7B418BC45C200215002 /* tolua_fix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tolua_fix.h; sourceTree = ""; }; + 1ABCA1E618CD8F470087CE3A /* lauxlib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lauxlib.h; sourceTree = ""; }; + 1ABCA1E718CD8F470087CE3A /* lua.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua.h; sourceTree = ""; }; + 1ABCA1E818CD8F470087CE3A /* luaconf.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = luaconf.h; sourceTree = ""; }; + 1ABCA1E918CD8F470087CE3A /* lualib.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lualib.h; sourceTree = ""; }; + 1ABCA1F318CD8F540087CE3A /* libluajit.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libluajit.a; sourceTree = ""; }; + 1ABCA1F618CD8F5F0087CE3A /* libluajit.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libluajit.a; sourceTree = ""; }; + 1ABCA1F918CD8F6E0087CE3A /* tolua_event.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = tolua_event.c; path = ../../../../external/lua/tolua/tolua_event.c; sourceTree = ""; }; + 1ABCA1FA18CD8F6E0087CE3A /* tolua_event.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = tolua_event.h; path = ../../../../external/lua/tolua/tolua_event.h; sourceTree = ""; }; + 1ABCA1FB18CD8F6E0087CE3A /* tolua_is.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = tolua_is.c; path = ../../../../external/lua/tolua/tolua_is.c; sourceTree = ""; }; + 1ABCA1FC18CD8F6E0087CE3A /* tolua_map.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = tolua_map.c; path = ../../../../external/lua/tolua/tolua_map.c; sourceTree = ""; }; + 1ABCA1FD18CD8F6E0087CE3A /* tolua_push.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = tolua_push.c; path = ../../../../external/lua/tolua/tolua_push.c; sourceTree = ""; }; + 1ABCA1FE18CD8F6E0087CE3A /* tolua_to.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = tolua_to.c; path = ../../../../external/lua/tolua/tolua_to.c; sourceTree = ""; }; + 1ABCA1FF18CD8F6E0087CE3A /* tolua++.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "tolua++.h"; path = "../../../../external/lua/tolua/tolua++.h"; sourceTree = ""; }; + 1ABCA20F18CD8F7D0087CE3A /* auxiliar.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = auxiliar.c; sourceTree = ""; }; + 1ABCA21018CD8F7D0087CE3A /* auxiliar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = auxiliar.h; sourceTree = ""; }; + 1ABCA21118CD8F7D0087CE3A /* except.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = except.c; sourceTree = ""; }; + 1ABCA21218CD8F7D0087CE3A /* except.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = except.h; sourceTree = ""; }; + 1ABCA21318CD8F7D0087CE3A /* inet.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = inet.c; sourceTree = ""; }; + 1ABCA21418CD8F7D0087CE3A /* inet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = inet.h; sourceTree = ""; }; + 1ABCA21518CD8F7D0087CE3A /* luasocket.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = luasocket.c; sourceTree = ""; }; + 1ABCA21618CD8F7D0087CE3A /* luasocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = luasocket.h; sourceTree = ""; }; + 1ABCA21718CD8F7D0087CE3A /* luasocket_buffer.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = luasocket_buffer.c; sourceTree = ""; }; + 1ABCA21818CD8F7D0087CE3A /* luasocket_buffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = luasocket_buffer.h; sourceTree = ""; }; + 1ABCA21918CD8F7D0087CE3A /* luasocket_io.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = luasocket_io.c; sourceTree = ""; }; + 1ABCA21A18CD8F7D0087CE3A /* luasocket_io.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = luasocket_io.h; sourceTree = ""; }; + 1ABCA21B18CD8F7D0087CE3A /* mime.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = mime.c; sourceTree = ""; }; + 1ABCA21C18CD8F7D0087CE3A /* mime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mime.h; sourceTree = ""; }; + 1ABCA21D18CD8F7D0087CE3A /* options.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = options.c; sourceTree = ""; }; + 1ABCA21E18CD8F7D0087CE3A /* options.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = options.h; sourceTree = ""; }; + 1ABCA21F18CD8F7D0087CE3A /* select.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = select.c; sourceTree = ""; }; + 1ABCA22018CD8F7D0087CE3A /* select.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = select.h; sourceTree = ""; }; + 1ABCA22118CD8F7D0087CE3A /* serial.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = serial.c; sourceTree = ""; }; + 1ABCA22218CD8F7D0087CE3A /* socket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = socket.h; sourceTree = ""; }; + 1ABCA22318CD8F7D0087CE3A /* socket_scripts.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = socket_scripts.c; sourceTree = ""; }; + 1ABCA22418CD8F7D0087CE3A /* socket_scripts.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = socket_scripts.h; sourceTree = ""; }; + 1ABCA22518CD8F7D0087CE3A /* tcp.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = tcp.c; sourceTree = ""; }; + 1ABCA22618CD8F7D0087CE3A /* tcp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tcp.h; sourceTree = ""; }; + 1ABCA22718CD8F7D0087CE3A /* timeout.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = timeout.c; sourceTree = ""; }; + 1ABCA22818CD8F7D0087CE3A /* timeout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = timeout.h; sourceTree = ""; }; + 1ABCA22918CD8F7D0087CE3A /* udp.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = udp.c; sourceTree = ""; }; + 1ABCA22A18CD8F7D0087CE3A /* udp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = udp.h; sourceTree = ""; }; + 1ABCA22B18CD8F7D0087CE3A /* unix.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = unix.c; sourceTree = ""; }; + 1ABCA22C18CD8F7D0087CE3A /* unix.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = unix.h; sourceTree = ""; }; + 1ABCA22D18CD8F7D0087CE3A /* usocket.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = usocket.c; sourceTree = ""; }; + 1ABCA22E18CD8F7D0087CE3A /* usocket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = usocket.h; sourceTree = ""; }; + C0FEF4D418BE0E70001F446C /* lua_debugger.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = lua_debugger.c; sourceTree = ""; }; + C0FEF4D518BE0E70001F446C /* lua_debugger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lua_debugger.h; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 1A11972D178526AA00D62A44 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 1ABCA1F418CD8F540087CE3A /* libluajit.a in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 1A6FB51817854BC200CDF010 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 1ABCA36318CD9D7F0087CE3A /* libluajit.a in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 1551A334158F2AB200E66CFE = { + isa = PBXGroup; + children = ( + 1ABCA1E218CD8F230087CE3A /* external */, + 1AACE74818BC45C200215002 /* auto */, + 1AACE75B18BC45C200215002 /* manual */, + 1AACE74618BC45A000215002 /* libluabindings Mac.a */, + 1AACE74718BC45A000215002 /* libluabindings iOS.a */, + ); + sourceTree = ""; + }; + 1AACE74818BC45C200215002 /* auto */ = { + isa = PBXGroup; + children = ( + 1AACE74918BC45C200215002 /* lua_cocos2dx_auto.cpp */, + 1AACE74A18BC45C200215002 /* lua_cocos2dx_auto.hpp */, + 1AACE74C18BC45C200215002 /* lua_cocos2dx_extension_auto.cpp */, + 1AACE74D18BC45C200215002 /* lua_cocos2dx_extension_auto.hpp */, + 1AACE74F18BC45C200215002 /* lua_cocos2dx_gui_auto.cpp */, + 1AACE75018BC45C200215002 /* lua_cocos2dx_gui_auto.hpp */, + 1AACE75218BC45C200215002 /* lua_cocos2dx_physics_auto.cpp */, + 1AACE75318BC45C200215002 /* lua_cocos2dx_physics_auto.hpp */, + 1AACE75518BC45C200215002 /* lua_cocos2dx_spine_auto.cpp */, + 1AACE75618BC45C200215002 /* lua_cocos2dx_spine_auto.hpp */, + 1AACE75818BC45C200215002 /* lua_cocos2dx_studio_auto.cpp */, + 1AACE75918BC45C200215002 /* lua_cocos2dx_studio_auto.hpp */, + ); + name = auto; + path = ../auto; + sourceTree = ""; + }; + 1AACE75B18BC45C200215002 /* manual */ = { + isa = PBXGroup; + children = ( + 1AACE75E18BC45C200215002 /* CCBProxy.cpp */, + C0FEF4D418BE0E70001F446C /* lua_debugger.c */, + C0FEF4D518BE0E70001F446C /* lua_debugger.h */, + 1AACE75F18BC45C200215002 /* CCBProxy.h */, + 1AACE76018BC45C200215002 /* CCLuaBridge.cpp */, + 1AACE76118BC45C200215002 /* CCLuaBridge.h */, + 1AACE76218BC45C200215002 /* CCLuaEngine.cpp */, + 1AACE76318BC45C200215002 /* CCLuaEngine.h */, + 1AACE76418BC45C200215002 /* CCLuaStack.cpp */, + 1AACE76518BC45C200215002 /* CCLuaStack.h */, + 1AACE76618BC45C200215002 /* CCLuaValue.cpp */, + 1AACE76718BC45C200215002 /* CCLuaValue.h */, + 1AACE76818BC45C200215002 /* Cocos2dxLuaLoader.cpp */, + 1AACE76918BC45C200215002 /* Cocos2dxLuaLoader.h */, + 1AACE76A18BC45C200215002 /* lua_cocos2dx_coco_studio_manual.cpp */, + 1AACE76B18BC45C200215002 /* lua_cocos2dx_coco_studio_manual.hpp */, + 1AACE76C18BC45C200215002 /* lua_cocos2dx_deprecated.cpp */, + 1AACE76D18BC45C200215002 /* lua_cocos2dx_deprecated.h */, + 1AACE76E18BC45C200215002 /* lua_cocos2dx_extension_manual.cpp */, + 1AACE76F18BC45C200215002 /* lua_cocos2dx_extension_manual.h */, + 1AACE77018BC45C200215002 /* lua_cocos2dx_gui_manual.cpp */, + 1AACE77118BC45C200215002 /* lua_cocos2dx_gui_manual.hpp */, + 1AACE77218BC45C200215002 /* lua_cocos2dx_manual.cpp */, + 1AACE77318BC45C200215002 /* lua_cocos2dx_manual.hpp */, + 1AACE77418BC45C200215002 /* lua_cocos2dx_physics_manual.cpp */, + 1AACE77518BC45C200215002 /* lua_cocos2dx_physics_manual.hpp */, + 1AACE77618BC45C200215002 /* lua_cocos2dx_spine_manual.cpp */, + 1AACE77718BC45C200215002 /* lua_cocos2dx_spine_manual.hpp */, + 1AACE77818BC45C200215002 /* lua_extensions.c */, + 1AACE77918BC45C200215002 /* lua_extensions.h */, + 1AACE77A18BC45C200215002 /* Lua_web_socket.cpp */, + 1AACE77B18BC45C200215002 /* Lua_web_socket.h */, + 1AACE77C18BC45C200215002 /* lua_xml_http_request.cpp */, + 1AACE77D18BC45C200215002 /* lua_xml_http_request.h */, + 1AACE77E18BC45C200215002 /* LuaBasicConversions.cpp */, + 1AACE77F18BC45C200215002 /* LuaBasicConversions.h */, + 1AACE78018BC45C200215002 /* LuaOpengl.cpp */, + 1AACE78118BC45C200215002 /* LuaOpengl.h */, + 1AACE78218BC45C200215002 /* LuaScriptHandlerMgr.cpp */, + 1AACE78318BC45C200215002 /* LuaScriptHandlerMgr.h */, + 1AACE78418BC45C200215002 /* LuaSkeletonAnimation.cpp */, + 1AACE78518BC45C200215002 /* LuaSkeletonAnimation.h */, + 1AACE78618BC45C200215002 /* platform */, + 1A262AB718BEEF5900D2DB92 /* tolua_fix.cpp */, + 1AACE7B418BC45C200215002 /* tolua_fix.h */, + ); + name = manual; + path = ../manual; + sourceTree = ""; + }; + 1AACE78618BC45C200215002 /* platform */ = { + isa = PBXGroup; + children = ( + 1AACE78D18BC45C200215002 /* ios */, + ); + path = platform; + sourceTree = ""; + }; + 1AACE78D18BC45C200215002 /* ios */ = { + isa = PBXGroup; + children = ( + 1AACE78E18BC45C200215002 /* CCLuaObjcBridge.h */, + 1AACE78F18BC45C200215002 /* CCLuaObjcBridge.mm */, + ); + path = ios; + sourceTree = ""; + }; + 1ABCA1E218CD8F230087CE3A /* external */ = { + isa = PBXGroup; + children = ( + 1ABCA1E318CD8F2D0087CE3A /* luajit */, + 1ABCA20E18CD8F7D0087CE3A /* luasocket */, + 1ABCA1E418CD8F330087CE3A /* tolua */, + ); + name = external; + sourceTree = ""; + }; + 1ABCA1E318CD8F2D0087CE3A /* luajit */ = { + isa = PBXGroup; + children = ( + 1ABCA1E518CD8F470087CE3A /* include */, + 1ABCA1F218CD8F540087CE3A /* ios */, + 1ABCA1F518CD8F5F0087CE3A /* mac */, + ); + name = luajit; + sourceTree = ""; + }; + 1ABCA1E418CD8F330087CE3A /* tolua */ = { + isa = PBXGroup; + children = ( + 1ABCA1F918CD8F6E0087CE3A /* tolua_event.c */, + 1ABCA1FA18CD8F6E0087CE3A /* tolua_event.h */, + 1ABCA1FB18CD8F6E0087CE3A /* tolua_is.c */, + 1ABCA1FC18CD8F6E0087CE3A /* tolua_map.c */, + 1ABCA1FD18CD8F6E0087CE3A /* tolua_push.c */, + 1ABCA1FE18CD8F6E0087CE3A /* tolua_to.c */, + 1ABCA1FF18CD8F6E0087CE3A /* tolua++.h */, + ); + name = tolua; + sourceTree = ""; + }; + 1ABCA1E518CD8F470087CE3A /* include */ = { + isa = PBXGroup; + children = ( + 1ABCA1E618CD8F470087CE3A /* lauxlib.h */, + 1ABCA1E718CD8F470087CE3A /* lua.h */, + 1ABCA1E818CD8F470087CE3A /* luaconf.h */, + 1ABCA1E918CD8F470087CE3A /* lualib.h */, + ); + name = include; + path = ../../../../external/lua/luajit/include; + sourceTree = ""; + }; + 1ABCA1F218CD8F540087CE3A /* ios */ = { + isa = PBXGroup; + children = ( + 1ABCA1F318CD8F540087CE3A /* libluajit.a */, + ); + name = ios; + path = ../../../../external/lua/luajit/prebuilt/ios; + sourceTree = ""; + }; + 1ABCA1F518CD8F5F0087CE3A /* mac */ = { + isa = PBXGroup; + children = ( + 1ABCA1F618CD8F5F0087CE3A /* libluajit.a */, + ); + name = mac; + path = ../../../../external/lua/luajit/prebuilt/mac; + sourceTree = ""; + }; + 1ABCA20E18CD8F7D0087CE3A /* luasocket */ = { + isa = PBXGroup; + children = ( + 1ABCA20F18CD8F7D0087CE3A /* auxiliar.c */, + 1ABCA21018CD8F7D0087CE3A /* auxiliar.h */, + 1ABCA21118CD8F7D0087CE3A /* except.c */, + 1ABCA21218CD8F7D0087CE3A /* except.h */, + 1ABCA21318CD8F7D0087CE3A /* inet.c */, + 1ABCA21418CD8F7D0087CE3A /* inet.h */, + 1ABCA21518CD8F7D0087CE3A /* luasocket.c */, + 1ABCA21618CD8F7D0087CE3A /* luasocket.h */, + 1ABCA21718CD8F7D0087CE3A /* luasocket_buffer.c */, + 1ABCA21818CD8F7D0087CE3A /* luasocket_buffer.h */, + 1ABCA21918CD8F7D0087CE3A /* luasocket_io.c */, + 1ABCA21A18CD8F7D0087CE3A /* luasocket_io.h */, + 1ABCA21B18CD8F7D0087CE3A /* mime.c */, + 1ABCA21C18CD8F7D0087CE3A /* mime.h */, + 1ABCA21D18CD8F7D0087CE3A /* options.c */, + 1ABCA21E18CD8F7D0087CE3A /* options.h */, + 1ABCA21F18CD8F7D0087CE3A /* select.c */, + 1ABCA22018CD8F7D0087CE3A /* select.h */, + 1ABCA22118CD8F7D0087CE3A /* serial.c */, + 1ABCA22218CD8F7D0087CE3A /* socket.h */, + 1ABCA22318CD8F7D0087CE3A /* socket_scripts.c */, + 1ABCA22418CD8F7D0087CE3A /* socket_scripts.h */, + 1ABCA22518CD8F7D0087CE3A /* tcp.c */, + 1ABCA22618CD8F7D0087CE3A /* tcp.h */, + 1ABCA22718CD8F7D0087CE3A /* timeout.c */, + 1ABCA22818CD8F7D0087CE3A /* timeout.h */, + 1ABCA22918CD8F7D0087CE3A /* udp.c */, + 1ABCA22A18CD8F7D0087CE3A /* udp.h */, + 1ABCA22B18CD8F7D0087CE3A /* unix.c */, + 1ABCA22C18CD8F7D0087CE3A /* unix.h */, + 1ABCA22D18CD8F7D0087CE3A /* usocket.c */, + 1ABCA22E18CD8F7D0087CE3A /* usocket.h */, + ); + name = luasocket; + path = ../../../../external/lua/luasocket; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 1A11972F178526AA00D62A44 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 1ABCA26C18CD8F7D0087CE3A /* unix.h in Headers */, + 1AACE7E018BC45C200215002 /* CCLuaBridge.h in Headers */, + C0FEF4D918BE0E70001F446C /* lua_debugger.h in Headers */, + 1AACE7B818BC45C200215002 /* lua_cocos2dx_auto.hpp in Headers */, + 1AACE7E418BC45C200215002 /* CCLuaEngine.h in Headers */, + 1ABCA25C18CD8F7D0087CE3A /* socket_scripts.h in Headers */, + 1ABCA24818CD8F7D0087CE3A /* luasocket_io.h in Headers */, + 1AACE7F018BC45C200215002 /* Cocos2dxLuaLoader.h in Headers */, + 1AACE7DC18BC45C200215002 /* CCBProxy.h in Headers */, + 1ABCA20D18CD8F6E0087CE3A /* tolua++.h in Headers */, + 1ABCA24018CD8F7D0087CE3A /* luasocket.h in Headers */, + 1ABCA25818CD8F7D0087CE3A /* socket.h in Headers */, + 1ABCA23418CD8F7D0087CE3A /* auxiliar.h in Headers */, + 1AACE7E818BC45C200215002 /* CCLuaStack.h in Headers */, + 1ABCA1EF18CD8F470087CE3A /* luaconf.h in Headers */, + 1ABCA23C18CD8F7D0087CE3A /* inet.h in Headers */, + 1AACE7D018BC45C200215002 /* lua_cocos2dx_spine_auto.hpp in Headers */, + 1ABCA1EB18CD8F470087CE3A /* lauxlib.h in Headers */, + 1AACE81018BC45C200215002 /* lua_extensions.h in Headers */, + 1AACE82018BC45C200215002 /* LuaOpengl.h in Headers */, + 1ABCA26018CD8F7D0087CE3A /* tcp.h in Headers */, + 1AACE80418BC45C200215002 /* lua_cocos2dx_manual.hpp in Headers */, + 1AACE81818BC45C200215002 /* lua_xml_http_request.h in Headers */, + 1AACE87C18BC45C200215002 /* tolua_fix.h in Headers */, + 1AACE83218BC45C200215002 /* CCLuaObjcBridge.h in Headers */, + 1ABCA24418CD8F7D0087CE3A /* luasocket_buffer.h in Headers */, + 1AACE82418BC45C200215002 /* LuaScriptHandlerMgr.h in Headers */, + 1AACE7FC18BC45C200215002 /* lua_cocos2dx_extension_manual.h in Headers */, + 1AACE80018BC45C200215002 /* lua_cocos2dx_gui_manual.hpp in Headers */, + 1AACE7EC18BC45C200215002 /* CCLuaValue.h in Headers */, + 1AACE80C18BC45C200215002 /* lua_cocos2dx_spine_manual.hpp in Headers */, + 1AACE7C418BC45C200215002 /* lua_cocos2dx_gui_auto.hpp in Headers */, + 1ABCA25018CD8F7D0087CE3A /* options.h in Headers */, + 1ABCA1F118CD8F470087CE3A /* lualib.h in Headers */, + 1ABCA20318CD8F6E0087CE3A /* tolua_event.h in Headers */, + 1AACE82818BC45C200215002 /* LuaSkeletonAnimation.h in Headers */, + 1ABCA27018CD8F7D0087CE3A /* usocket.h in Headers */, + 1AACE80818BC45C200215002 /* lua_cocos2dx_physics_manual.hpp in Headers */, + 1AACE7BE18BC45C200215002 /* lua_cocos2dx_extension_auto.hpp in Headers */, + 1ABCA23818CD8F7D0087CE3A /* except.h in Headers */, + 1ABCA26418CD8F7D0087CE3A /* timeout.h in Headers */, + 1AACE81418BC45C200215002 /* Lua_web_socket.h in Headers */, + 1AACE81C18BC45C200215002 /* LuaBasicConversions.h in Headers */, + 1AACE7CA18BC45C200215002 /* lua_cocos2dx_physics_auto.hpp in Headers */, + 1AACE7F818BC45C200215002 /* lua_cocos2dx_deprecated.h in Headers */, + 1ABCA24C18CD8F7D0087CE3A /* mime.h in Headers */, + 1ABCA1ED18CD8F470087CE3A /* lua.h in Headers */, + 1AACE7F418BC45C200215002 /* lua_cocos2dx_coco_studio_manual.hpp in Headers */, + 1ABCA26818CD8F7D0087CE3A /* udp.h in Headers */, + 1ABCA25418CD8F7D0087CE3A /* select.h in Headers */, + 1AACE7D618BC45C200215002 /* lua_cocos2dx_studio_auto.hpp in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 1A6FB51B17854BC200CDF010 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 1ABCA26B18CD8F7D0087CE3A /* unix.h in Headers */, + 1AACE7DF18BC45C200215002 /* CCLuaBridge.h in Headers */, + C0FEF4D818BE0E70001F446C /* lua_debugger.h in Headers */, + 1AACE7B718BC45C200215002 /* lua_cocos2dx_auto.hpp in Headers */, + 1AACE7E318BC45C200215002 /* CCLuaEngine.h in Headers */, + 1ABCA25B18CD8F7D0087CE3A /* socket_scripts.h in Headers */, + 1ABCA24718CD8F7D0087CE3A /* luasocket_io.h in Headers */, + 1AACE7EF18BC45C200215002 /* Cocos2dxLuaLoader.h in Headers */, + 1AACE7DB18BC45C200215002 /* CCBProxy.h in Headers */, + 1ABCA20C18CD8F6E0087CE3A /* tolua++.h in Headers */, + 1ABCA23F18CD8F7D0087CE3A /* luasocket.h in Headers */, + 1ABCA25718CD8F7D0087CE3A /* socket.h in Headers */, + 1ABCA23318CD8F7D0087CE3A /* auxiliar.h in Headers */, + 1AACE7E718BC45C200215002 /* CCLuaStack.h in Headers */, + 1ABCA1EE18CD8F470087CE3A /* luaconf.h in Headers */, + 1ABCA23B18CD8F7D0087CE3A /* inet.h in Headers */, + 1AACE7CF18BC45C200215002 /* lua_cocos2dx_spine_auto.hpp in Headers */, + 1ABCA1EA18CD8F470087CE3A /* lauxlib.h in Headers */, + 1AACE80F18BC45C200215002 /* lua_extensions.h in Headers */, + 1AACE81F18BC45C200215002 /* LuaOpengl.h in Headers */, + 1ABCA25F18CD8F7D0087CE3A /* tcp.h in Headers */, + 1AACE80318BC45C200215002 /* lua_cocos2dx_manual.hpp in Headers */, + 1AACE81718BC45C200215002 /* lua_xml_http_request.h in Headers */, + 1AACE87B18BC45C200215002 /* tolua_fix.h in Headers */, + 1AACE83118BC45C200215002 /* CCLuaObjcBridge.h in Headers */, + 1ABCA24318CD8F7D0087CE3A /* luasocket_buffer.h in Headers */, + 1AACE82318BC45C200215002 /* LuaScriptHandlerMgr.h in Headers */, + 1AACE7FB18BC45C200215002 /* lua_cocos2dx_extension_manual.h in Headers */, + 1AACE7FF18BC45C200215002 /* lua_cocos2dx_gui_manual.hpp in Headers */, + 1AACE7EB18BC45C200215002 /* CCLuaValue.h in Headers */, + 1AACE80B18BC45C200215002 /* lua_cocos2dx_spine_manual.hpp in Headers */, + 1AACE7C318BC45C200215002 /* lua_cocos2dx_gui_auto.hpp in Headers */, + 1ABCA24F18CD8F7D0087CE3A /* options.h in Headers */, + 1ABCA1F018CD8F470087CE3A /* lualib.h in Headers */, + 1ABCA20218CD8F6E0087CE3A /* tolua_event.h in Headers */, + 1AACE82718BC45C200215002 /* LuaSkeletonAnimation.h in Headers */, + 1ABCA26F18CD8F7D0087CE3A /* usocket.h in Headers */, + 1AACE80718BC45C200215002 /* lua_cocos2dx_physics_manual.hpp in Headers */, + 1AACE7BD18BC45C200215002 /* lua_cocos2dx_extension_auto.hpp in Headers */, + 1ABCA23718CD8F7D0087CE3A /* except.h in Headers */, + 1ABCA26318CD8F7D0087CE3A /* timeout.h in Headers */, + 1AACE81318BC45C200215002 /* Lua_web_socket.h in Headers */, + 1AACE81B18BC45C200215002 /* LuaBasicConversions.h in Headers */, + 1AACE7C918BC45C200215002 /* lua_cocos2dx_physics_auto.hpp in Headers */, + 1AACE7F718BC45C200215002 /* lua_cocos2dx_deprecated.h in Headers */, + 1ABCA24B18CD8F7D0087CE3A /* mime.h in Headers */, + 1ABCA1EC18CD8F470087CE3A /* lua.h in Headers */, + 1AACE7F318BC45C200215002 /* lua_cocos2dx_coco_studio_manual.hpp in Headers */, + 1ABCA26718CD8F7D0087CE3A /* udp.h in Headers */, + 1ABCA25318CD8F7D0087CE3A /* select.h in Headers */, + 1AACE7D518BC45C200215002 /* lua_cocos2dx_studio_auto.hpp in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + 1A119716178526AA00D62A44 /* luabindings iOS */ = { + isa = PBXNativeTarget; + buildConfigurationList = 1A11978E178526AA00D62A44 /* Build configuration list for PBXNativeTarget "luabindings iOS" */; + buildPhases = ( + 1A119717178526AA00D62A44 /* Sources */, + 1A11972D178526AA00D62A44 /* Frameworks */, + 1A11972F178526AA00D62A44 /* Headers */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "luabindings iOS"; + productName = cocos2dx; + productReference = 1AACE74718BC45A000215002 /* libluabindings iOS.a */; + productType = "com.apple.product-type.library.static"; + }; + 1A6FB50417854BC200CDF010 /* luabindings Mac */ = { + isa = PBXNativeTarget; + buildConfigurationList = 1A6FB52D17854BC200CDF010 /* Build configuration list for PBXNativeTarget "luabindings Mac" */; + buildPhases = ( + 1A6FB50517854BC200CDF010 /* Sources */, + 1A6FB51817854BC200CDF010 /* Frameworks */, + 1A6FB51B17854BC200CDF010 /* Headers */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = "luabindings Mac"; + productName = cocos2dx; + productReference = 1AACE74618BC45A000215002 /* libluabindings Mac.a */; + productType = "com.apple.product-type.library.static"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 1551A336158F2AB200E66CFE /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0500; + ORGANIZATIONNAME = ""; + }; + buildConfigurationList = 1551A339158F2AB200E66CFE /* Build configuration list for PBXProject "cocos2d_lua_bindings" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + ); + mainGroup = 1551A334158F2AB200E66CFE; + productRefGroup = 1551A334158F2AB200E66CFE; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 1A6FB50417854BC200CDF010 /* luabindings Mac */, + 1A119716178526AA00D62A44 /* luabindings iOS */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 1A119717178526AA00D62A44 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 1AACE7C818BC45C200215002 /* lua_cocos2dx_physics_auto.cpp in Sources */, + 1AACE83418BC45C200215002 /* CCLuaObjcBridge.mm in Sources */, + 1ABCA20518CD8F6E0087CE3A /* tolua_is.c in Sources */, + 1AACE7F618BC45C200215002 /* lua_cocos2dx_deprecated.cpp in Sources */, + 1ABCA24E18CD8F7D0087CE3A /* options.c in Sources */, + 1A262AB918BEEF5900D2DB92 /* tolua_fix.cpp in Sources */, + 1AACE81E18BC45C200215002 /* LuaOpengl.cpp in Sources */, + 1AACE7C218BC45C200215002 /* lua_cocos2dx_gui_auto.cpp in Sources */, + 1AACE7CE18BC45C200215002 /* lua_cocos2dx_spine_auto.cpp in Sources */, + 1AACE80218BC45C200215002 /* lua_cocos2dx_manual.cpp in Sources */, + 1ABCA24618CD8F7D0087CE3A /* luasocket_io.c in Sources */, + 1ABCA23E18CD8F7D0087CE3A /* luasocket.c in Sources */, + 1ABCA25218CD8F7D0087CE3A /* select.c in Sources */, + 1ABCA24A18CD8F7D0087CE3A /* mime.c in Sources */, + 1ABCA26218CD8F7D0087CE3A /* timeout.c in Sources */, + 1ABCA26618CD8F7D0087CE3A /* udp.c in Sources */, + 1AACE82218BC45C200215002 /* LuaScriptHandlerMgr.cpp in Sources */, + 1ABCA23618CD8F7D0087CE3A /* except.c in Sources */, + 1ABCA26A18CD8F7D0087CE3A /* unix.c in Sources */, + 1AACE7F218BC45C200215002 /* lua_cocos2dx_coco_studio_manual.cpp in Sources */, + 1AACE80E18BC45C200215002 /* lua_extensions.c in Sources */, + 1AACE7EA18BC45C200215002 /* CCLuaValue.cpp in Sources */, + 1AACE7D418BC45C200215002 /* lua_cocos2dx_studio_auto.cpp in Sources */, + 1AACE7BC18BC45C200215002 /* lua_cocos2dx_extension_auto.cpp in Sources */, + 1ABCA20918CD8F6E0087CE3A /* tolua_push.c in Sources */, + 1AACE82618BC45C200215002 /* LuaSkeletonAnimation.cpp in Sources */, + 1ABCA25E18CD8F7D0087CE3A /* tcp.c in Sources */, + 1AACE80A18BC45C200215002 /* lua_cocos2dx_spine_manual.cpp in Sources */, + 1ABCA24218CD8F7D0087CE3A /* luasocket_buffer.c in Sources */, + 1AACE81A18BC45C200215002 /* LuaBasicConversions.cpp in Sources */, + 1AACE7EE18BC45C200215002 /* Cocos2dxLuaLoader.cpp in Sources */, + 1ABCA25A18CD8F7D0087CE3A /* socket_scripts.c in Sources */, + 1AACE7DA18BC45C200215002 /* CCBProxy.cpp in Sources */, + 1AACE81618BC45C200215002 /* lua_xml_http_request.cpp in Sources */, + 1ABCA23A18CD8F7D0087CE3A /* inet.c in Sources */, + 1AACE7E218BC45C200215002 /* CCLuaEngine.cpp in Sources */, + 1ABCA26E18CD8F7D0087CE3A /* usocket.c in Sources */, + 1AACE81218BC45C200215002 /* Lua_web_socket.cpp in Sources */, + 1AACE7FA18BC45C200215002 /* lua_cocos2dx_extension_manual.cpp in Sources */, + 1AACE7DE18BC45C200215002 /* CCLuaBridge.cpp in Sources */, + 1ABCA23218CD8F7D0087CE3A /* auxiliar.c in Sources */, + 1ABCA20118CD8F6E0087CE3A /* tolua_event.c in Sources */, + C0FEF4D718BE0E70001F446C /* lua_debugger.c in Sources */, + 1AACE7E618BC45C200215002 /* CCLuaStack.cpp in Sources */, + 1AACE80618BC45C200215002 /* lua_cocos2dx_physics_manual.cpp in Sources */, + 1AACE7B618BC45C200215002 /* lua_cocos2dx_auto.cpp in Sources */, + 1ABCA20718CD8F6E0087CE3A /* tolua_map.c in Sources */, + 1ABCA25618CD8F7D0087CE3A /* serial.c in Sources */, + 1ABCA20B18CD8F6E0087CE3A /* tolua_to.c in Sources */, + 1AACE7FE18BC45C200215002 /* lua_cocos2dx_gui_manual.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 1A6FB50517854BC200CDF010 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 1AACE7C718BC45C200215002 /* lua_cocos2dx_physics_auto.cpp in Sources */, + 1AACE83318BC45C200215002 /* CCLuaObjcBridge.mm in Sources */, + 1ABCA20418CD8F6E0087CE3A /* tolua_is.c in Sources */, + 1AACE7F518BC45C200215002 /* lua_cocos2dx_deprecated.cpp in Sources */, + 1ABCA24D18CD8F7D0087CE3A /* options.c in Sources */, + 1A262AB818BEEF5900D2DB92 /* tolua_fix.cpp in Sources */, + 1AACE81D18BC45C200215002 /* LuaOpengl.cpp in Sources */, + 1AACE7C118BC45C200215002 /* lua_cocos2dx_gui_auto.cpp in Sources */, + 1AACE7CD18BC45C200215002 /* lua_cocos2dx_spine_auto.cpp in Sources */, + 1AACE80118BC45C200215002 /* lua_cocos2dx_manual.cpp in Sources */, + 1ABCA24518CD8F7D0087CE3A /* luasocket_io.c in Sources */, + 1ABCA23D18CD8F7D0087CE3A /* luasocket.c in Sources */, + 1ABCA25118CD8F7D0087CE3A /* select.c in Sources */, + 1ABCA24918CD8F7D0087CE3A /* mime.c in Sources */, + 1ABCA26118CD8F7D0087CE3A /* timeout.c in Sources */, + 1ABCA26518CD8F7D0087CE3A /* udp.c in Sources */, + 1AACE82118BC45C200215002 /* LuaScriptHandlerMgr.cpp in Sources */, + 1ABCA23518CD8F7D0087CE3A /* except.c in Sources */, + 1ABCA26918CD8F7D0087CE3A /* unix.c in Sources */, + 1AACE7F118BC45C200215002 /* lua_cocos2dx_coco_studio_manual.cpp in Sources */, + 1AACE80D18BC45C200215002 /* lua_extensions.c in Sources */, + 1AACE7E918BC45C200215002 /* CCLuaValue.cpp in Sources */, + 1AACE7D318BC45C200215002 /* lua_cocos2dx_studio_auto.cpp in Sources */, + 1AACE7BB18BC45C200215002 /* lua_cocos2dx_extension_auto.cpp in Sources */, + 1ABCA20818CD8F6E0087CE3A /* tolua_push.c in Sources */, + 1AACE82518BC45C200215002 /* LuaSkeletonAnimation.cpp in Sources */, + 1ABCA25D18CD8F7D0087CE3A /* tcp.c in Sources */, + 1AACE80918BC45C200215002 /* lua_cocos2dx_spine_manual.cpp in Sources */, + 1ABCA24118CD8F7D0087CE3A /* luasocket_buffer.c in Sources */, + 1AACE81918BC45C200215002 /* LuaBasicConversions.cpp in Sources */, + 1AACE7ED18BC45C200215002 /* Cocos2dxLuaLoader.cpp in Sources */, + 1ABCA25918CD8F7D0087CE3A /* socket_scripts.c in Sources */, + 1AACE7D918BC45C200215002 /* CCBProxy.cpp in Sources */, + 1AACE81518BC45C200215002 /* lua_xml_http_request.cpp in Sources */, + 1ABCA23918CD8F7D0087CE3A /* inet.c in Sources */, + 1AACE7E118BC45C200215002 /* CCLuaEngine.cpp in Sources */, + 1ABCA26D18CD8F7D0087CE3A /* usocket.c in Sources */, + 1AACE81118BC45C200215002 /* Lua_web_socket.cpp in Sources */, + 1AACE7F918BC45C200215002 /* lua_cocos2dx_extension_manual.cpp in Sources */, + 1AACE7DD18BC45C200215002 /* CCLuaBridge.cpp in Sources */, + 1ABCA23118CD8F7D0087CE3A /* auxiliar.c in Sources */, + 1ABCA20018CD8F6E0087CE3A /* tolua_event.c in Sources */, + C0FEF4D618BE0E70001F446C /* lua_debugger.c in Sources */, + 1AACE7E518BC45C200215002 /* CCLuaStack.cpp in Sources */, + 1AACE80518BC45C200215002 /* lua_cocos2dx_physics_manual.cpp in Sources */, + 1AACE7B518BC45C200215002 /* lua_cocos2dx_auto.cpp in Sources */, + 1ABCA20618CD8F6E0087CE3A /* tolua_map.c in Sources */, + 1ABCA25518CD8F7D0087CE3A /* serial.c in Sources */, + 1ABCA20A18CD8F6E0087CE3A /* tolua_to.c in Sources */, + 1AACE7FD18BC45C200215002 /* lua_cocos2dx_gui_manual.cpp in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 1551A34A158F2AB200E66CFE /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = YES; + ARCHS = "$(ARCHS_STANDARD_64_BIT)"; + CLANG_CXX_LANGUAGE_STANDARD = "c++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + COPY_PHASE_STRIP = NO; + GCC_C_LANGUAGE_STANDARD = c99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "COCOS2D_DEBUG=1", + USE_FILE32API, + "CC_ENABLE_CHIPMUNK_INTEGRATION=1", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; + GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = YES; + GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES; + GCC_WARN_SHADOW = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + HEADER_SEARCH_PATHS = ""; + MACOSX_DEPLOYMENT_TARGET = 10.7; + ONLY_ACTIVE_ARCH = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + SKIP_INSTALL = YES; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)/../../../.. $(SRCROOT)/../../.. $(SRCROOT)/../../../base $(SRCROOT)/../../../2d $(SRCROOT)/../../../physics $(SRCROOT)/../../../math/kazmath $(SRCROOT)/../../../2d/platform $(SRCROOT)/../../../audio/include $(SRCROOT)/../../../editor-support $(SRCROOT)/../../../editor-support/spine $(SRCROOT)/../../../editor-support/cocostudio $(SRCROOT)/../../../editor-support/cocosbuilder $(SRCROOT)/../../../gui $(SRCROOT)/../../../storage $(SRCROOT)/../../../../extensions $(SRCROOT)/../../../../external $(SRCROOT)/../../../../external/chipmunk/include/chipmunk $(SRCROOT)/../../../../external/lua $(SRCROOT)/../../../../external/lua/luajit/include $(SRCROOT)/../../../../external/lua/tolua"; + }; + name = Debug; + }; + 1551A34B158F2AB200E66CFE /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = YES; + ARCHS = "$(ARCHS_STANDARD_64_BIT)"; + CLANG_CXX_LANGUAGE_STANDARD = "c++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + GCC_C_LANGUAGE_STANDARD = c99; + GCC_PREPROCESSOR_DEFINITIONS = ( + "CC_ENABLE_CHIPMUNK_INTEGRATION=1", + NDEBUG, + USE_FILE32API, + ); + GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES; + GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES; + GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = YES; + GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES; + GCC_WARN_SHADOW = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + HEADER_SEARCH_PATHS = ""; + MACOSX_DEPLOYMENT_TARGET = 10.7; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + SKIP_INSTALL = YES; + USER_HEADER_SEARCH_PATHS = "$(SRCROOT)/../../../.. $(SRCROOT)/../../.. $(SRCROOT)/../../../base $(SRCROOT)/../../../2d $(SRCROOT)/../../../physics $(SRCROOT)/../../../math/kazmath $(SRCROOT)/../../../2d/platform $(SRCROOT)/../../../audio/include $(SRCROOT)/../../../editor-support $(SRCROOT)/../../../editor-support/spine $(SRCROOT)/../../../editor-support/cocostudio $(SRCROOT)/../../../editor-support/cocosbuilder $(SRCROOT)/../../../gui $(SRCROOT)/../../../storage $(SRCROOT)/../../../../extensions $(SRCROOT)/../../../../external $(SRCROOT)/../../../../external/chipmunk/include/chipmunk $(SRCROOT)/../../../../external/lua $(SRCROOT)/../../../../external/lua/luajit/include $(SRCROOT)/../../../../external/lua/tolua"; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + 1A11978F178526AA00D62A44 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = YES; + ARCHS = "$(ARCHS_STANDARD)"; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = ""; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + CC_TARGET_OS_IPHONE, + ); + HEADER_SEARCH_PATHS = ""; + IPHONEOS_DEPLOYMENT_TARGET = 5.1; + LIBRARY_SEARCH_PATHS = "$(SRCROOT)/../../../../external/lua/luajit/prebuilt/ios"; + SDKROOT = iphoneos; + TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "$(inherited) $(SRCROOT)/../../../2d/platform/ios"; + }; + name = Debug; + }; + 1A119790178526AA00D62A44 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = YES; + ARCHS = "$(ARCHS_STANDARD)"; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = ""; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + CC_TARGET_OS_IPHONE, + ); + HEADER_SEARCH_PATHS = ""; + IPHONEOS_DEPLOYMENT_TARGET = 5.1; + LIBRARY_SEARCH_PATHS = "$(SRCROOT)/../../../../external/lua/luajit/prebuilt/ios"; + SDKROOT = iphoneos; + TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "$(inherited) $(SRCROOT)/../../../2d/platform/ios"; + }; + name = Release; + }; + 1A6FB52E17854BC200CDF010 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = YES; + ARCHS = "$(ARCHS_STANDARD_64_BIT)"; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = ""; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + CC_TARGET_OS_MAC, + CC_KEYBOARD_SUPPORT, + ); + HEADER_SEARCH_PATHS = ""; + IPHONEOS_DEPLOYMENT_TARGET = 5.0; + LIBRARY_SEARCH_PATHS = "$(SRCROOT)/../../../../external/lua/luajit/prebuilt/mac"; + TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "$(inherited) $(SRCROOT)/../../../2d/platform/mac $(SRCROOT)/../../../../external/glfw3/include/mac"; + }; + name = Debug; + }; + 1A6FB52F17854BC200CDF010 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = YES; + ARCHS = "$(ARCHS_STANDARD_64_BIT)"; + GCC_PRECOMPILE_PREFIX_HEADER = YES; + GCC_PREFIX_HEADER = ""; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + CC_TARGET_OS_MAC, + CC_KEYBOARD_SUPPORT, + ); + HEADER_SEARCH_PATHS = ""; + IPHONEOS_DEPLOYMENT_TARGET = 5.0; + LIBRARY_SEARCH_PATHS = "$(SRCROOT)/../../../../external/lua/luajit/prebuilt/mac"; + TARGETED_DEVICE_FAMILY = "1,2"; + USER_HEADER_SEARCH_PATHS = "$(inherited) $(SRCROOT)/../../../2d/platform/mac $(SRCROOT)/../../../../external/glfw3/include/mac"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 1551A339158F2AB200E66CFE /* Build configuration list for PBXProject "cocos2d_lua_bindings" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 1551A34A158F2AB200E66CFE /* Debug */, + 1551A34B158F2AB200E66CFE /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 1A11978E178526AA00D62A44 /* Build configuration list for PBXNativeTarget "luabindings iOS" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 1A11978F178526AA00D62A44 /* Debug */, + 1A119790178526AA00D62A44 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 1A6FB52D17854BC200CDF010 /* Build configuration list for PBXNativeTarget "luabindings Mac" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 1A6FB52E17854BC200CDF010 /* Debug */, + 1A6FB52F17854BC200CDF010 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 1551A336158F2AB200E66CFE /* Project object */; +} diff --git a/cocos/scripting/lua-bindings/proj.ios_mac/cocos2d_lua_bindings.xcodeproj/xcshareddata/xcschemes/luabindings Mac.xcscheme b/cocos/scripting/lua-bindings/proj.ios_mac/cocos2d_lua_bindings.xcodeproj/xcshareddata/xcschemes/luabindings Mac.xcscheme new file mode 100644 index 0000000000..01e86ee2ce --- /dev/null +++ b/cocos/scripting/lua-bindings/proj.ios_mac/cocos2d_lua_bindings.xcodeproj/xcshareddata/xcschemes/luabindings Mac.xcscheme @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cocos/scripting/lua-bindings/proj.ios_mac/cocos2d_lua_bindings.xcodeproj/xcshareddata/xcschemes/luabindings iOS.xcscheme b/cocos/scripting/lua-bindings/proj.ios_mac/cocos2d_lua_bindings.xcodeproj/xcshareddata/xcschemes/luabindings iOS.xcscheme new file mode 100644 index 0000000000..ed14d59435 --- /dev/null +++ b/cocos/scripting/lua-bindings/proj.ios_mac/cocos2d_lua_bindings.xcodeproj/xcshareddata/xcschemes/luabindings iOS.xcscheme @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/cocos/scripting/lua-bindings/proj.win32/liblua.vcxproj b/cocos/scripting/lua-bindings/proj.win32/liblua.vcxproj new file mode 100644 index 0000000000..b736737153 --- /dev/null +++ b/cocos/scripting/lua-bindings/proj.win32/liblua.vcxproj @@ -0,0 +1,249 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {DDC3E27F-004D-4DD4-9DD3-931A013D2159} + liblua + Win32Proj + + + + StaticLibrary + Unicode + v100 + v110 + v110_xp + + + StaticLibrary + Unicode + v100 + v110 + v110_xp + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.40219.1 + $(SolutionDir)$(Configuration).win32\ + $(Configuration).win32\ + $(SolutionDir)$(Configuration).win32\ + $(Configuration).win32\ + AllRules.ruleset + + + AllRules.ruleset + + + + + $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\lib;$(LibraryPath) + + + $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\lib;$(LibraryPath) + + + + Disabled + $(EngineRoot);$(EngineRoot)cocos;$(EngineRoot)cocos\editor-support;$(EngineRoot)cocos\editor-support\cocostudio;$(EngineRoot)cocos\editor-support\cocosbuilder;$(EngineRoot)cocos\editor-support\spine;$(EngineRoot)cocos\audio\include;$(EngineRoot)extensions;$(EngineRoot)extensions\network;$(EngineRoot)external;$(EngineRoot)external\libwebsockets\win32\include;$(EngineRoot)external\lua\tolua;$(EngineRoot)external\lua\luajit\include;$(EngineRoot)external\lua;$(EngineRoot)cocos\scripting\lua-bindings\auto;$(EngineRoot)cocos\scripting\lua-bindings\manual;%(AdditionalIncludeDirectories) + WIN32;_WINDOWS;_DEBUG;COCOS2D_DEBUG=1;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + false + EnableFastChecks + MultiThreadedDebugDLL + + + + + + + Level3 + OldStyle + 4800;4267;4251;4244;%(DisableSpecificWarnings) + true + + + %(AdditionalLibraryDirectories) + + + + + + + if not exist "$(OutDir)" mkdir "$(OutDir)" +xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\lua\luajit\prebuilt\win32\*.*" "$(OutDir)" + + + + + MaxSpeed + true + $(EngineRoot);$(EngineRoot)cocos;$(EngineRoot)cocos\editor-support;$(EngineRoot)cocos\editor-support\cocostudio;$(EngineRoot)cocos\editor-support\cocosbuilder;$(EngineRoot)cocos\editor-support\spine;$(EngineRoot)cocos\audio\include;$(EngineRoot)extensions;$(EngineRoot)extensions\network;$(EngineRoot)external;$(EngineRoot)external\libwebsockets\win32\include;$(EngineRoot)external\lua\tolua;$(EngineRoot)external\lua\luajit\include;$(EngineRoot)external\lua;$(EngineRoot)cocos\scripting\lua-bindings\auto;$(EngineRoot)cocos\scripting\lua-bindings\manual;%(AdditionalIncludeDirectories) + WIN32;NDEBUG;_WINDOWS;LIBLUA_EXPORTS;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + + + + + + Level3 + ProgramDatabase + 4800;4267;4251;4244;%(DisableSpecificWarnings) + true + + + libcocos2d.lib;%(AdditionalLibraryDirectories) + + + + + + + if not exist "$(OutDir)" mkdir "$(OutDir)" +xcopy /Y /Q "$(ProjectDir)..\..\..\..\external\lua\luajit\prebuilt\win32\*.*" "$(OutDir)" + + + + + + \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/proj.win32/liblua.vcxproj.filters b/cocos/scripting/lua-bindings/proj.win32/liblua.vcxproj.filters new file mode 100644 index 0000000000..c6c7a4dc80 --- /dev/null +++ b/cocos/scripting/lua-bindings/proj.win32/liblua.vcxproj.filters @@ -0,0 +1,365 @@ + + + + + {8d269c90-26ad-4ce8-b355-d8df01d59914} + + + {25c7b94b-a9a1-4b93-b285-93c9b620e2ad} + + + {af4b7d5f-f563-4069-ade8-c1a954e6fe5e} + + + {c36f2204-a252-47c0-b36c-b84cda218746} + + + + + auto + + + auto + + + auto + + + auto + + + auto + + + auto + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + + + auto + + + auto + + + auto + + + auto + + + auto + + + auto + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + manual + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + luasocket + + + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + script + + + \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/proj.win32/liblua.vcxproj.user b/cocos/scripting/lua-bindings/proj.win32/liblua.vcxproj.user new file mode 100644 index 0000000000..ace9a86acb --- /dev/null +++ b/cocos/scripting/lua-bindings/proj.win32/liblua.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/script/AudioEngine.lua b/cocos/scripting/lua-bindings/script/AudioEngine.lua new file mode 100644 index 0000000000..cc5d20a8ee --- /dev/null +++ b/cocos/scripting/lua-bindings/script/AudioEngine.lua @@ -0,0 +1,113 @@ +--Encapsulate SimpleAudioEngine to AudioEngine,Play music and sound effects. +local M = {} +local audioEngineInstance = cc.SimpleAudioEngine:getInstance() + +function M.stopAllEffects() + audioEngineInstance:stopAllEffects() +end + +function M.getMusicVolume() + return audioEngineInstance:getMusicVolume() +end + +function M.isMusicPlaying() + return audioEngineInstance:isMusicPlaying() +end + +function M.getEffectsVolume() + return audioEngineInstance:getEffectsVolume() +end + +function M.setMusicVolume(volume) + audioEngineInstance:setMusicVolume(volume) +end + +function M.stopEffect(handle) + audioEngineInstance:stopEffect(handle) +end + +function M.stopMusic(isReleaseData) + local releaseDataValue = false + if nil ~= isReleaseData then + releaseDataValue = isReleaseData + end + audioEngineInstance:stopMusic(releaseDataValue) +end + +function M.playMusic(filename, isLoop) + local loopValue = false + if nil ~= isLoop then + loopValue = isLoop + end + audioEngineInstance:playMusic(filename, loopValue) +end + +function M.pauseAllEffects() + audioEngineInstance:pauseAllEffects() +end + +function M.preloadMusic(filename) + audioEngineInstance:preloadMusic(filename) +end + +function M.resumeMusic() + audioEngineInstance:resumeMusic() +end + +function M.playEffect(filename, isLoop) + local loopValue = false + if nil ~= isLoop then + loopValue = isLoop + end + return audioEngineInstance:playEffect(filename, loopValue) +end + +function M.rewindMusic() + audioEngineInstance:rewindMusic() +end + +function M.willPlayMusic() + return audioEngineInstance:willPlayMusic() +end + +function M.unloadEffect(filename) + audioEngineInstance:unloadEffect(filename) +end + +function M.preloadEffect(filename) + audioEngineInstance:preloadEffect(filename) +end + +function M.setEffectsVolume(volume) + audioEngineInstance:setEffectsVolume(volume) +end + +function M.pauseEffect(handle) + audioEngineInstance:pauseEffect(handle) +end + +function M.resumeAllEffects(handle) + audioEngineInstance:resumeAllEffects() +end + +function M.pauseMusic() + audioEngineInstance:pauseMusic() +end + +function M.resumeEffect(handle) + audioEngineInstance:resumeEffect(handle) +end + +local modename = "AudioEngine" +local proxy = {} +local mt = { + __index = M, + __newindex = function (t ,k ,v) + print("attemp to update a read-only table") + end +} +setmetatable(proxy,mt) +_G[modename] = proxy +package.loaded[modename] = proxy + + diff --git a/cocos/scripting/lua-bindings/script/CCBReaderLoad.lua b/cocos/scripting/lua-bindings/script/CCBReaderLoad.lua new file mode 100644 index 0000000000..141a4e5df5 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/CCBReaderLoad.lua @@ -0,0 +1,121 @@ +ccb = ccb or {} + +function CCBReaderLoad(strFilePath,proxy,owner) + if nil == proxy then + return nil + end + + local ccbReader = proxy:createCCBReader() + local node = ccbReader:load(strFilePath) + local rootName = "" + --owner set in readCCBFromFile is proxy + if nil ~= owner then + --Callbacks + local ownerCallbackNames = ccbReader:getOwnerCallbackNames() + local ownerCallbackNodes = ccbReader:getOwnerCallbackNodes() + local ownerCallbackControlEvents = ccbReader:getOwnerCallbackControlEvents() + local i = 1 + for i = 1,table.getn(ownerCallbackNames) do + local callbackName = ownerCallbackNames[i] + local callbackNode = tolua.cast(ownerCallbackNodes[i],"cc.Node") + + if "function" == type(owner[callbackName]) then + proxy:setCallback(callbackNode, owner[callbackName], ownerCallbackControlEvents[i]) + else + print("Warning: Cannot find owner's lua function:" .. ":" .. callbackName .. " for ownerVar selector") + end + + end + + --Variables + local ownerOutletNames = ccbReader:getOwnerOutletNames() + local ownerOutletNodes = ccbReader:getOwnerOutletNodes() + + for i = 1, table.getn(ownerOutletNames) do + local outletName = ownerOutletNames[i] + local outletNode = tolua.cast(ownerOutletNodes[i],"cc.Node") + owner[outletName] = outletNode + end + end + + local nodesWithAnimationManagers = ccbReader:getNodesWithAnimationManagers() + local animationManagersForNodes = ccbReader:getAnimationManagersForNodes() + + for i = 1 , table.getn(nodesWithAnimationManagers) do + local innerNode = tolua.cast(nodesWithAnimationManagers[i], "cc.Node") + local animationManager = tolua.cast(animationManagersForNodes[i], "cc.CCBAnimationManager") + local documentControllerName = animationManager:getDocumentControllerName() + if "" == documentControllerName then + + end + if nil ~= ccb[documentControllerName] then + ccb[documentControllerName]["mAnimationManager"] = animationManager + end + + --Callbacks + local documentCallbackNames = animationManager:getDocumentCallbackNames() + local documentCallbackNodes = animationManager:getDocumentCallbackNodes() + local documentCallbackControlEvents = animationManager:getDocumentCallbackControlEvents() + + for i = 1,table.getn(documentCallbackNames) do + local callbackName = documentCallbackNames[i] + local callbackNode = tolua.cast(documentCallbackNodes[i],"cc.Node") + if "" ~= documentControllerName and nil ~= ccb[documentControllerName] then + if "function" == type(ccb[documentControllerName][callbackName]) then + proxy:setCallback(callbackNode, ccb[documentControllerName][callbackName], documentCallbackControlEvents[i]) + else + print("Warning: Cannot found lua function [" .. documentControllerName .. ":" .. callbackName .. "] for docRoot selector") + end + end + end + + --Variables + local documentOutletNames = animationManager:getDocumentOutletNames() + local documentOutletNodes = animationManager:getDocumentOutletNodes() + + for i = 1, table.getn(documentOutletNames) do + local outletName = documentOutletNames[i] + local outletNode = tolua.cast(documentOutletNodes[i],"cc.Node") + + if nil ~= ccb[documentControllerName] then + ccb[documentControllerName][outletName] = tolua.cast(outletNode, proxy:getNodeTypeName(outletNode)) + end + end + --[[ + if (typeof(controller.onDidLoadFromCCB) == "function") + controller.onDidLoadFromCCB(); + ]]-- + --Setup timeline callbacks + local keyframeCallbacks = animationManager:getKeyframeCallbacks() + + for i = 1 , table.getn(keyframeCallbacks) do + local callbackCombine = keyframeCallbacks[i] + local beignIndex,endIndex = string.find(callbackCombine,":") + local callbackType = tonumber(string.sub(callbackCombine,1,beignIndex - 1)) + local callbackName = string.sub(callbackCombine,endIndex + 1, -1) + --Document callback + + if 1 == callbackType and nil ~= ccb[documentControllerName] then + local callfunc = cc.CallFunc:create(ccb[documentControllerName][callbackName]) + animationManager:setCallFuncForLuaCallbackNamed(callfunc, callbackCombine); + elseif 2 == callbackType and nil ~= owner then --Owner callback + local callfunc = cc.CallFunc:create(owner[callbackName])--need check + animationManager:setCallFuncForLuaCallbackNamed(callfunc, callbackCombine) + end + end + --start animation + local autoPlaySeqId = animationManager:getAutoPlaySequenceId() + if -1 ~= autoPlaySeqId then + animationManager:runAnimationsForSequenceIdTweenDuration(autoPlaySeqId, 0) + end + end + + return node +end + + +local function CCBuilderReaderLoad(strFilePath,proxy,owner) + print("\n********** \n".."CCBuilderReaderLoad(strFilePath,proxy,owner)".." was deprecated please use ".. "CCBReaderLoad(strFilePath,proxy,owner)" .. " instead.\n**********") + return CCBReaderLoad(strFilePath,proxy,owner) +end +rawset(_G,"CCBuilderReaderLoad",CCBuilderReaderLoad) \ No newline at end of file diff --git a/cocos/scripting/lua-bindings/script/CocoStudio.lua b/cocos/scripting/lua-bindings/script/CocoStudio.lua new file mode 100644 index 0000000000..66eb80121f --- /dev/null +++ b/cocos/scripting/lua-bindings/script/CocoStudio.lua @@ -0,0 +1,373 @@ +require "json" +require "extern" + +ccs = ccs or {} + +function ccs.sendTriggerEvent(event) + local triggerObjArr = ccs.TriggerMng.getInstance():get(event) + + if nil == triggerObjArr then + return + end + + for i = 1, table.getn(triggerObjArr) do + local triObj = triggerObjArr[i] + if nil ~= triObj and triObj:detect() then + triObj:done() + end + end +end + +function ccs.registerTriggerClass(className, createFunc) + ccs.TInfo.new(className,createFunc) +end + +ccs.TInfo = class("TInfo") +ccs.TInfo._className = "" +ccs.TInfo._fun = nil + +function ccs.TInfo:ctor(c,f) + -- @param {String|ccs.TInfo}c + -- @param {Function}f + if nil ~= f then + self._className = c + self._fun = f + else + self._className = c._className + self._fun = c._fun + end + + ccs.ObjectFactory.getInstance():registerType(self) +end + +ccs.ObjectFactory = class("ObjectFactory") +ccs.ObjectFactory._typeMap = nil +ccs.ObjectFactory._instance = nil + +function ccs.ObjectFactory:ctor() + self._typeMap = {} +end + +function ccs.ObjectFactory.getInstance() + if nil == ccs.ObjectFactory._instance then + ccs.ObjectFactory._instance = ccs.ObjectFactory.new() + end + + return ccs.ObjectFactory._instance +end + +function ccs.ObjectFactory.destroyInstance() + ccs.ObjectFactory._instance = nil +end + +function ccs.ObjectFactory:createObject(classname) + local obj = nil + local t = self._typeMap[classname] + if nil ~= t then + obj = t._fun() + end + + return obj +end + +function ccs.ObjectFactory:registerType(t) + self._typeMap[t._className] = t +end + +ccs.TriggerObj = class("TriggerObj") +ccs.TriggerObj._cons = {} +ccs.TriggerObj._acts = {} +ccs.TriggerObj._enable = false +ccs.TriggerObj._id = 0 +ccs.TriggerObj._vInt = {} + +function ccs.TriggerObj.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TriggerObj) + return target +end + +function ccs.TriggerObj:ctor() + self:init() +end + +function ccs.TriggerObj:init() + self._id = 0 + self._enable = true + self._cons = {} + self._acts = {} + self._vInt = {} +end + +function ccs.TriggerObj:detect() + if (not self._enable) or (table.getn(self._cons) == 0) then + return true + end + + local ret = true + local obj = nil + for i = 1 , table.getn(self._cons) do + obj = self._cons[i] + if nil ~= obj and nil ~= obj.detect then + ret = ret and obj:detect() + end + end + return ret +end + +function ccs.TriggerObj:done() + if (not self._enable) or (table.getn(self._acts) == 0) then + return + end + + local obj = nil + for i = 1, table.getn(self._acts) do + obj = self._acts[i] + if nil ~= obj and obj.done then + obj:done() + end + end +end + +function ccs.TriggerObj:removeAll() + local obj = nil + for i=1, table.getn(self._cons) do + obj = self._cons[i] + if nil ~= obj then + obj:removeAll() + end + end + self._cons = {} + + for i=1, table.getn(self._acts) do + obj = self._acts[i] + if nil ~= obj then + obj:removeAll() + end + end + self._acts = {} +end + +function ccs.TriggerObj:serialize(jsonValue) + self._id = jsonValue["id"] + local count = 0 + + --condition + local cons = jsonValue["conditions"] + if nil ~= cons then + count = table.getn(cons) + for i = 1, count do + local subDict = cons[i] + local className = subDict["classname"] + if nil ~= className then + local obj = ccs.ObjectFactory.getInstance():createObject(className) + assert(nil ~= obj, string.format("class named %s can not implement!",className)) + obj:serialize(subDict) + obj:init() + table.insert(self._cons, obj) + end + end + end + + local actions = jsonValue["actions"] + if nil ~= actions then + count = table.getn(actions) + for i = 1,count do + local subAction = actions[i] + local className = subAction["classname"] + if nil ~= className then + local act = ccs.ObjectFactory.getInstance():createObject(className) + assert(nil ~= act ,string.format("class named %s can not implement!",className)) + act:serialize(subAction) + act:init() + table.insert(self._acts,act) + end + end + end + + local events = jsonValue["events"] + if nil ~= events then + count = table.getn(events) + for i = 1, count do + local subEveent = events[i] + local eventID = subEveent["id"] + if eventID >= 0 then + table.insert(self._vInt,eventID) + end + end + end +end + +function ccs.TriggerObj:getId() + return self._id +end + +function ccs.TriggerObj:setEnable(enable) + self._enable = enable +end + +function ccs.TriggerObj:getEvents() + return self._vInt +end + +ccs.TriggerMng = class("TriggerMng") +ccs.TriggerMng._eventTriggers = nil +ccs.TriggerMng._triggerObjs = nil +ccs.TriggerMng._movementDispatches = nil +ccs.TriggerMng._instance = nil + +function ccs.TriggerMng:ctor() + self._triggerObjs = {} + self._movementDispatches = {} + self._eventTriggers = {} +end + +function ccs.TriggerMng.getInstance() + if ccs.TriggerMng._instance == nil then + ccs.TriggerMng._instance = ccs.TriggerMng.new() + end + + return ccs.TriggerMng._instance +end + +function ccs.TriggerMng.destroyInstance() + if ccs.TriggerMng._instance ~= nil then + ccs.TriggerMng._instance:removeAll() + ccs.TriggerMng._instance = nil + end +end + +function ccs.TriggerMng:triggerMngVersion() + return "1.0.0.0" +end + +function ccs.TriggerMng:parse(jsonStr) + local parseTable = json.decode(jsonStr,1) + if nil == parseTable then + return + end + + local count = table.getn(parseTable) + for i = 1, count do + local subDict = parseTable[i] + local triggerObj = ccs.TriggerObj.new() + triggerObj:serialize(subDict) + local events = triggerObj:getEvents() + for j = 1, table.getn(events) do + local event = events[j] + self:add(event, triggerObj) + end + + self._triggerObjs[triggerObj:getId()] = triggerObj + end +end + +function ccs.TriggerMng:get(event) + return self._eventTriggers[event] +end + +function ccs.TriggerMng:getTriggerObj(id) + return self._triggerObjs[id] +end + +function ccs.TriggerMng:add(event,triggerObj) + local eventTriggers = self._eventTriggers[event] + if nil == eventTriggers then + eventTriggers = {} + end + + local exist = false + for i = 1, table.getn(eventTriggers) do + if eventTriggers[i] == triggers then + exist = true + break + end + end + + if not exist then + table.insert(eventTriggers,triggerObj) + self._eventTriggers[event] = eventTriggers + end +end + +function ccs.TriggerMng:removeAll( ) + for k in pairs(self._eventTriggers) do + local triObjArr = self._eventTriggers[k] + for j = 1, table.getn(triObjArr) do + local obj = triObjArr[j] + obj:removeAll() + end + end + self._eventTriggers = {} +end + +function ccs.TriggerMng:remove(event, obj) + + if nil ~= obj then + return self:removeObjByEvent(event, obj) + end + + assert(event >= 0,"event must be larger than 0") + if nil == self._eventTriggers then + return false + end + + local triObjects = self._eventTriggers[event] + if nil == triObjects then + return false + end + + for i = 1, table.getn(triObjects) do + local triObject = triggers[i] + if nil ~= triObject then + triObject:remvoeAll() + end + end + + self._eventTriggers[event] = nil + return true +end + +function ccs.TriggerMng:removeObjByEvent(event, obj) + assert(event >= 0,"event must be larger than 0") + if nil == self._eventTriggers then + return false + end + + local triObjects = self._eventTriggers[event] + if nil == triObjects then + return false + end + + for i = 1,table.getn(triObjects) do + local triObject = triObjects[i] + if nil ~= triObject and triObject == obj then + triObject:remvoeAll() + table.remove(triObjects, i) + return true + end + end +end + +function ccs.TriggerMng:removeTriggerObj(id) + local obj = self.getTriggerObj(id) + + if nil == obj then + return false + end + + local events = obj:getEvents() + for i = 1, table.getn(events) do + self:remove(events[i],obj) + end + + return true +end + +function ccs.TriggerMng:isEmpty() + return (not (nil == self._eventTriggers)) or table.getn(self._eventTriggers) <= 0 +end diff --git a/cocos/scripting/lua-bindings/script/Cocos2d.lua b/cocos/scripting/lua-bindings/script/Cocos2d.lua new file mode 100644 index 0000000000..1628b920e6 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/Cocos2d.lua @@ -0,0 +1,381 @@ +require "CocoStudio" + +cc = cc or {} + +cc.DIRECTOR_PROJECTION_2D = 0 +cc.DIRECTOR_PROJECTION_3D = 1 + +function cc.clampf(value, min_inclusive, max_inclusive) + -- body + local temp = 0 + if min_inclusive > max_inclusive then + temp = min_inclusive + min_inclusive = max_inclusive + max_inclusive = temp + end + + if value < min_inclusive then + return min_inclusive + elseif value < max_inclusive then + return value + else + return max_inclusive + end +end + +--Point +function cc.p(_x,_y) + if nil == _y then + return { x = _x.x, y = _x.y } + else + return { x = _x, y = _y } + end +end + +function cc.pAdd(pt1,pt2) + return {x = pt1.x + pt2.x , y = pt1.y + pt2.y } +end + +function cc.pSub(pt1,pt2) + return {x = pt1.x - pt2.x , y = pt1.y - pt2.y } +end + +function cc.pMul(pt1,factor) + return { x = pt1.x * factor , y = pt1.y * factor } +end + +function cc.pMidpoint(pt1,pt2) + return { x = (pt1.x + pt2.x) / 2.0 , y = ( pt1.y + pt2.y) / 2.0 } +end + +function cc.pForAngle(a) + return { x = math.cos(a), y = math.sin(a) } +end + +function cc.pGetLength(pt) + return math.sqrt( pt.x * pt.x + pt.y * pt.y ) +end + +function cc.pNormalize(pt) + local length = cc.pGetLength(pt) + if 0 == length then + return { x = 1.0,y = 0.0 } + end + + return { x = pt.x / length, y = pt.y / length } +end + +function cc.pCross(self,other) + return self.x * other.y - self.y * other.x +end + +function cc.pDot(self,other) + return self.x * other.x + self.y * other.y +end + +function cc.pToAngleSelf(self) + return math.atan2(self.y, self.x) +end + +function cc.pGetAngle(self,other) + local a2 = cc.pNormalize(self) + local b2 = cc.pNormalize(other) + local angle = math.atan2(cc.pCross(a2, b2), cc.pDot(a2, b2) ) + if angle < 1.192092896e-7 then + return 0.0 + end + + return angle +end + +function cc.pGetDistance(startP,endP) + return cc.pGetLength(cc.pSub(startP,endP)) +end + +function cc.pIsLineIntersect(A, B, C, D, s, t) + if ((A.x == B.x) and (A.y == B.y)) or ((C.x == D.x) and (C.y == D.y))then + return false, s, t + end + + local BAx = B.x - A.x + local BAy = B.y - A.y + local DCx = D.x - C.x + local DCy = D.y - C.y + local ACx = A.x - C.x + local ACy = A.y - C.y + + local denom = DCy * BAx - DCx * BAy + s = DCx * ACy - DCy * ACx + t = BAx * ACy - BAy * ACx + + if (denom == 0) then + if (s == 0 or t == 0) then + return true, s , t + end + + return false, s, t + end + + s = s / denom + t = t / denom + + return true,s,t +end + +function cc.pPerp(pt) + return { x = -pt.y, y = pt.x } +end + +function cc.RPerp(pt) + return { x = pt.y, y = -pt.x } +end + +function cc.pProject(pt1, pt2) + return { x = pt2.x * (cc.pDot(pt1,pt2) / cc.pDot(pt2,pt2)) , y = pt2.y * (cc.pDot(pt1,pt2) / cc.pDot(pt2,pt2)) } +end + +function cc.pRotate(pt1, pt2) + return { x = pt1.x * pt2.x - pt1.y * pt2.y, y = pt1.x * pt2.y + pt1.y * pt2.x } +end + +function cc.pUnrotate(pt1, pt2) + return { x = pt1.x * pt2.x + pt1.y * pt2.y, pt1.y * pt2.x - pt1.x * pt2.y } +end +--Calculates the square length of pt +function cc.pLengthSQ(pt) + return cc.pDot(pt,pt) +end +--Calculates the square distance between pt1 and pt2 +function cc.pDistanceSQ(pt1,pt2) + return cc.pLengthSQ(cc.pSub(pt1,pt2)) +end + +function cc.pGetClampPoint(pt1,pt2,pt3) + return { x = cc.clampf(pt1.x, pt2.x, pt3.x), y = cc.clampf(pt1.y, pt2.y, pt3.y) } +end + +function cc.pFromSize(sz) + return { x = sz.width, y = sz.height } +end + +function cc.pLerp(pt1,pt2,alpha) + return cc.pAdd(cc.pMul(pt1, 1.0 - alpha), cc.pMul(pt2,alpha) ) +end + +function cc.pFuzzyEqual(pt1,pt2,variance) + if (pt1.x - variance <= pt2.x) and (pt2.x <= pt1.x + variance) and (pt1.y - variance <= pt2.y) and (pt2.y <= pt1.y + variance) then + return true + else + return false + end +end + +function cc.pRotateByAngle(pt1, pt2, angle) + return cc.pAdd(pt2, cc.pRotate( cc.pSub(pt1, pt2),cc.pForAngle(angle))) +end + +function cc.pIsSegmentIntersect(pt1,pt2,pt3,pt4) + local s,t,ret = 0,0,false + ret,s,t =cc.pIsLineIntersect(pt1, pt2, pt3, pt4,s,t) + + if ret and s >= 0.0 and s <= 1.0 and t >= 0.0 and t <= 0.0 then + return true; + end + + return false +end + +function cc.pGetIntersectPoint(pt1,pt2,pt3,pt4) + local s,t, ret = 0,0,false + ret,s,t = cc.pIsLineIntersect(pt1,pt2,pt3,pt4,s,t) + if ret then + return cc.p(pt1.x + s * (pt2.x - pt1.x), pt1.y + s * (pt2.y - pt1.y)) + else + return cc.p(0,0) + end +end +--Size +function cc.size( _width,_height ) + return { width = _width, height = _height } +end + +--Rect +function cc.rect(_x,_y,_width,_height) + return { x = _x, y = _y, width = _width, height = _height } +end + +function cc.rectEqualToRect(rect1,rect2) + if ((rect1.x >= rect2.x) or (rect1.y >= rect2.y) or + ( rect1.x + rect1.width <= rect2.x + rect2.width) or + ( rect1.y + rect1.height <= rect2.y + rect2.height)) then + return false + end + + return true +end + +function cc.rectGetMaxX(rect) + return rect.x + rect.width +end + +function cc.rectGetMidX(rect) + return rect.x + rect.width / 2.0 +end + +function cc.rectGetMinX(rect) + return rect.x +end + +function cc.rectGetMaxY(rect) + return rect.y + rect.height +end + +function cc.rectGetMidY(rect) + return rect.y + rect.height / 2.0 +end + +function cc.rectGetMinY(rect) + return rect.y +end + +function cc.rectContainsPoint( rect, point ) + local ret = false + + if (point.x >= rect.x) and (point.x <= rect.x + rect.width) and + (point.y >= rect.y) and (point.y <= rect.y + rect.height) then + ret = true + end + + return ret +end + +function cc.rectIntersectsRect( rect1, rect2 ) + local intersect = not ( rect1.x > rect2.x + rect2.width or + rect1.x + rect1.width < rect2.x or + rect1.y > rect2.y + rect2.height or + rect1.y + rect1.height < rect2.y ) + + return intersect +end + +function cc.rectUnion( rect1, rect2 ) + local rect = cc.rect(0, 0, 0, 0) + rect.x = math.min(rect1.x, rect2.x) + rect.y = math.min(rect1.y, rect2.y) + rect.width = math.max(rect1.x + rect1.width, rect2.x + rect2.width) - rect.x + rect.height = math.max(rect1.y + rect1.height, rect2.y + rect2.height) - rect.y + return rect +end + +function cc.rectIntersection( rect1, rect2 ) + local intersection = cc.rect( + math.max(rect1.x, rect2.x), + math.max(rect1.y, rect2.y), + 0, 0) + + intersection.width = math.min(rect1.x + rect1.width, rect2.x + rect2.width) - intersection.x + intersection.height = math.min(rect1.y + rect1.height, rect2.y + rect2.height) - intersection.y + return intersection +end + +--Color3B +function cc.c3b( _r,_g,_b ) + return { r = _r, g = _g, b = _b } +end + +--Color4B +function cc.c4b( _r,_g,_b,_a ) + return { r = _r, g = _g, b = _b, a = _a } +end + +--Color4F +function cc.c4f( _r,_g,_b,_a ) + return { r = _r, g = _g, b = _b, a = _a } +end + +--Vertex2F +function cc.vertex2F(_x,_y) + return { x = _x, y = _y } +end + +--Vertex3F +function cc.Vertex3F(_x,_y,_z) + return { x = _x, y = _y, z = _z } +end + +--Tex2F +function cc.tex2F(_u,_v) + return { u = _u, v = _v } +end + +--PointSprite +function cc.PointSprite(_pos,_color,_size) + return { pos = _pos, color = _color, size = _size } +end + +--Quad2 +function cc.Quad2(_tl,_tr,_bl,_br) + return { tl = _tl, tr = _tr, bl = _bl, br = _br } +end + +--Quad3 +function cc.Quad3(_tl, _tr, _bl, _br) + return { tl = _tl, tr = _tr, bl = _bl, br = _br } +end + +--V2F_C4B_T2F +function cc.V2F_C4B_T2F(_vertices, _colors, _texCoords) + return { vertices = _vertices, colors = _colors, texCoords = _texCoords } +end + +--V2F_C4F_T2F +function cc.V2F_C4F_T2F(_vertices, _colors, _texCoords) + return { vertices = _vertices, colors = _colors, texCoords = _texCoords } +end + +--V3F_C4B_T2F +function cc.V3F_C4B_T2F(_vertices, _colors, _texCoords) + return { vertices = _vertices, colors = _colors, texCoords = _texCoords } +end + +--V2F_C4B_T2F_Quad +function cc.V2F_C4B_T2F_Quad(_bl, _br, _tl, _tr) + return { bl = _bl, br = _br, tl = _tl, tr = _tr } +end + +--V3F_C4B_T2F_Quad +function cc.V3F_C4B_T2F_Quad(_tl, _bl, _tr, _br) + return { tl = _tl, bl = _bl, tr = _tr, br = _br } +end + +--V2F_C4F_T2F_Quad +function cc.V2F_C4F_T2F_Quad(_bl, _br, _tl, _tr) + return { bl = _bl, br = _br, tl = _tl, tr = _tr } +end + +--T2F_Quad +function cc.T2F_Quad(_bl, _br, _tl, _tr) + return { bl = _bl, br = _br, tl = _tl, tr = _tr } +end + +--AnimationFrameData +function cc.AnimationFrameData( _texCoords, _delay, _size) + return { texCoords = _texCoords, delay = _delay, size = _size } +end + +--PhysicsMaterial +function cc.PhysicsMaterial(_density, _restitution, _friction) + return { density = _density, restitution = _restitution, friction = _friction } +end + +local ConfigType = +{ + NONE = 0, + COCOSTUDIO = 1, +} + +function __onParseConfig(configType,jasonStr) + if configType == ConfigType.COCOSTUDIO then + ccs.TriggerMng.getInstance():parse(jasonStr) + end +end diff --git a/cocos/scripting/lua-bindings/script/Cocos2dConstants.lua b/cocos/scripting/lua-bindings/script/Cocos2dConstants.lua new file mode 100644 index 0000000000..55f405a78b --- /dev/null +++ b/cocos/scripting/lua-bindings/script/Cocos2dConstants.lua @@ -0,0 +1,535 @@ +cc = cc or {} + +cc.SPRITE_INDEX_NOT_INITIALIZED = 0xffffffff +cc.TMX_ORIENTATION_HEX = 0x1 +cc.TMX_ORIENTATION_ISO = 0x2 +cc.TMX_ORIENTATION_ORTHO = 0x0 +cc.Z_COMPRESSION_BZIP2 = 0x1 +cc.Z_COMPRESSION_GZIP = 0x2 +cc.Z_COMPRESSION_NONE = 0x3 +cc.Z_COMPRESSION_ZLIB = 0x0 +cc.BLEND_DST = 0x303 +cc.BLEND_SRC = 0x1 +cc.DIRECTOR_IOS_USE_BACKGROUND_THREAD = 0x0 +cc.DIRECTOR_MAC_THREAD = 0x0 +cc.DIRECTOR_STATS_INTERVAL = 0.1 +cc.ENABLE_BOX2_D_INTEGRATION = 0x0 +cc.ENABLE_DEPRECATED = 0x1 +cc.ENABLE_GL_STATE_CACHE = 0x1 +cc.ENABLE_PROFILERS = 0x0 +cc.ENABLE_STACKABLE_ACTIONS = 0x1 +cc.FIX_ARTIFACTS_BY_STRECHING_TEXEL = 0x0 +cc.GL_ALL = 0x0 +cc.LABELATLAS_DEBUG_DRAW = 0x0 +cc.LABELBMFONT_DEBUG_DRAW = 0x0 +cc.MAC_USE_DISPLAY_LINK_THREAD = 0x0 +cc.MAC_USE_MAIN_THREAD = 0x2 +cc.MAC_USE_OWN_THREAD = 0x1 +cc.NODE_RENDER_SUBPIXEL = 0x1 +cc.PVRMIPMAP_MAX = 0x10 +cc.SPRITEBATCHNODE_RENDER_SUBPIXEL = 0x1 +cc.SPRITE_DEBUG_DRAW = 0x0 +cc.TEXTURE_ATLAS_USE_TRIANGLE_STRIP = 0x0 +cc.TEXTURE_ATLAS_USE_VAO = 0x1 +cc.USE_L_A88_LABELS = 0x1 +cc.ACTION_TAG_INVALID = -1 +cc.DEVICE_MAC = 0x6 +cc.DEVICE_MAC_RETINA_DISPLAY = 0x7 +cc.DEVICEI_PAD = 0x4 +cc.DEVICEI_PAD_RETINA_DISPLAY = 0x5 +cc.DEVICEI_PHONE = 0x0 +cc.DEVICEI_PHONE5 = 0x2 +cc.DEVICEI_PHONE5_RETINA_DISPLAY = 0x3 +cc.DEVICEI_PHONE_RETINA_DISPLAY = 0x1 +cc.DIRECTOR_PROJECTION2_D = 0x0 +cc.DIRECTOR_PROJECTION3_D = 0x1 +cc.DIRECTOR_PROJECTION_CUSTOM = 0x2 +cc.DIRECTOR_PROJECTION_DEFAULT = 0x1 +cc.FILE_UTILS_SEARCH_DIRECTORY_MODE = 0x1 +cc.FILE_UTILS_SEARCH_SUFFIX_MODE = 0x0 +cc.FLIPED_ALL = 0xe0000000 +cc.FLIPPED_MASK = 0x1fffffff +cc.IMAGE_FORMAT_JPEG = 0x0 +cc.IMAGE_FORMAT_PNG = 0x1 +cc.ITEM_SIZE = 0x20 +cc.LABEL_AUTOMATIC_WIDTH = -1 +cc.LINE_BREAK_MODE_CHARACTER_WRAP = 0x1 +cc.LINE_BREAK_MODE_CLIP = 0x2 +cc.LINE_BREAK_MODE_HEAD_TRUNCATION = 0x3 +cc.LINE_BREAK_MODE_MIDDLE_TRUNCATION = 0x5 +cc.LINE_BREAK_MODE_TAIL_TRUNCATION = 0x4 +cc.LINE_BREAK_MODE_WORD_WRAP = 0x0 +cc.MAC_VERSION_10_6 = 0xa060000 +cc.MAC_VERSION_10_7 = 0xa070000 +cc.MAC_VERSION_10_8 = 0xa080000 +cc.MENU_HANDLER_PRIORITY = -128 +cc.MENU_STATE_TRACKING_TOUCH = 0x1 +cc.MENU_STATE_WAITING = 0x0 +cc.NODE_TAG_INVALID = -1 +cc.PARTICLE_DURATION_INFINITY = -1 +cc.PARTICLE_MODE_GRAVITY = 0x0 +cc.PARTICLE_MODE_RADIUS = 0x1 +cc.PARTICLE_START_RADIUS_EQUAL_TO_END_RADIUS = -1 +cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE = -1 +cc.POSITION_TYPE_FREE = 0x0 +cc.POSITION_TYPE_GROUPED = 0x2 +cc.POSITION_TYPE_RELATIVE = 0x1 +cc.PRIORITY_NON_SYSTEM_MIN = -2147483647 +cc.PRIORITY_SYSTEM = -2147483648 +cc.PROGRESS_TIMER_TYPE_BAR = 0x1 +cc.PROGRESS_TIMER_TYPE_RADIAL = 0x0 +cc.REPEAT_FOREVER = 0xfffffffe +cc.RESOLUTION_MAC = 0x1 +cc.RESOLUTION_MAC_RETINA_DISPLAY = 0x2 +cc.RESOLUTION_UNKNOWN = 0x0 +cc.TMX_TILE_DIAGONAL_FLAG = 0x20000000 +cc.TMX_TILE_HORIZONTAL_FLAG = 0x80000000 +cc.TMX_TILE_VERTICAL_FLAG = 0x40000000 +cc.TEXT_ALIGNMENT_CENTER = 0x1 +cc.TEXT_ALIGNMENT_LEFT = 0x0 +cc.TEXT_ALIGNMENT_RIGHT = 0x2 + +cc.TEXTURE2_D_PIXEL_FORMAT_AUTO = 0x0 +cc.TEXTURE2_D_PIXEL_FORMAT_BGR_A8888 = 0x1 +cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888 = 0x2 +cc.TEXTURE2_D_PIXEL_FORMAT_RG_B888 = 0x3 +cc.TEXTURE2_D_PIXEL_FORMAT_RG_B565 = 0x4 +cc.TEXTURE2_D_PIXEL_FORMAT_A8 = 0x5 +cc.TEXTURE2_D_PIXEL_FORMAT_I8 = 0x6 +cc.TEXTURE2_D_PIXEL_FORMAT_A_I88 = 0x7 +cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444 = 0x8 +cc.TEXTURE2_D_PIXEL_FORMAT_RGB5_A1 = 0x9 +cc.TEXTURE2_D_PIXEL_FORMAT_PVRTC4 = 0xa +cc.TEXTURE2_D_PIXEL_FORMAT_PVRTC4A = 0xb +cc.TEXTURE2_D_PIXEL_FORMAT_PVRTC2 = 0xc +cc.TEXTURE2_D_PIXEL_FORMAT_PVRTC2A = 0xd +cc.TEXTURE2_D_PIXEL_FORMAT_ETC = 0xe +cc.TEXTURE2_D_PIXEL_FORMAT_S3TC_DXT1 = 0xf +cc.TEXTURE2_D_PIXEL_FORMAT_S3TC_DXT3 = 0x10 +cc.TEXTURE2_D_PIXEL_FORMAT_S3TC_DXT5 = 0x11 +cc.TEXTURE2_D_PIXEL_FORMAT_DEFAULT = 0x0 +cc.TOUCHES_ALL_AT_ONCE = 0x0 +cc.TOUCHES_ONE_BY_ONE = 0x1 +cc.TRANSITION_ORIENTATION_DOWN_OVER = 0x1 +cc.TRANSITION_ORIENTATION_LEFT_OVER = 0x0 +cc.TRANSITION_ORIENTATION_RIGHT_OVER = 0x1 +cc.TRANSITION_ORIENTATION_UP_OVER = 0x0 +cc.UNIFORM_COS_TIME = 0x5 +cc.UNIFORM_MV_MATRIX = 0x1 +cc.UNIFORM_MVP_MATRIX = 0x2 +cc.UNIFORM_P_MATRIX = 0x0 +cc.UNIFORM_RANDOM01 = 0x6 +cc.UNIFORM_SAMPLER = 0x7 +cc.UNIFORM_SIN_TIME = 0x4 +cc.UNIFORM_TIME = 0x3 +cc.UNIFORM_MAX = 0x8 +cc.VERTEX_ATTRIB_FLAG_COLOR = 0x2 +cc.VERTEX_ATTRIB_FLAG_NONE = 0x0 +cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX = 0x7 +cc.VERTEX_ATTRIB_FLAG_POSITION = 0x1 +cc.VERTEX_ATTRIB_FLAG_TEX_COORDS = 0x4 +cc.VERTEX_ATTRIB_COLOR = 0x1 +cc.VERTEX_ATTRIB_MAX = 0x3 +cc.VERTEX_ATTRIB_POSITION = 0x0 +cc.VERTEX_ATTRIB_TEX_COORDS = 0x2 +cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM = 0x2 +cc.VERTICAL_TEXT_ALIGNMENT_CENTER = 0x1 +cc.VERTICAL_TEXT_ALIGNMENT_TOP = 0x0 +cc.OS_VERSION_4_0 = 0x4000000 +cc.OS_VERSION_4_0_1 = 0x4000100 +cc.OS_VERSION_4_1 = 0x4010000 +cc.OS_VERSION_4_2 = 0x4020000 +cc.OS_VERSION_4_2_1 = 0x4020100 +cc.OS_VERSION_4_3 = 0x4030000 +cc.OS_VERSION_4_3_1 = 0x4030100 +cc.OS_VERSION_4_3_2 = 0x4030200 +cc.OS_VERSION_4_3_3 = 0x4030300 +cc.OS_VERSION_4_3_4 = 0x4030400 +cc.OS_VERSION_4_3_5 = 0x4030500 +cc.OS_VERSION_5_0 = 0x5000000 +cc.OS_VERSION_5_0_1 = 0x5000100 +cc.OS_VERSION_5_1_0 = 0x5010000 +cc.OS_VERSION_6_0_0 = 0x6000000 +cc.ANIMATION_FRAME_DISPLAYED_NOTIFICATION = 'CCAnimationFrameDisplayedNotification' +cc.CHIPMUNK_IMPORT = 'chipmunk.h' +cc.ATTRIBUTE_NAME_COLOR = 'a_color' +cc.ATTRIBUTE_NAME_POSITION = 'a_position' +cc.ATTRIBUTE_NAME_TEX_COORD = 'a_texCoord' +cc.SHADER_POSITION_COLOR = 'ShaderPositionColor' +cc.SHADER_POSITION_LENGTH_TEXURE_COLOR = 'ShaderPositionLengthTextureColor' +cc.SHADER_POSITION_TEXTURE = 'ShaderPositionTexture' +cc.SHADER_POSITION_TEXTURE_A8_COLOR = 'ShaderPositionTextureA8Color' +cc.SHADER_POSITION_TEXTURE_COLOR = 'ShaderPositionTextureColor' +cc.SHADER_POSITION_TEXTURE_COLOR_ALPHA_TEST = 'ShaderPositionTextureColorAlphaTest' +cc.SHADER_POSITION_TEXTURE_U_COLOR = 'ShaderPositionTexture_uColor' +cc.SHADER_POSITION_U_COLOR = 'ShaderPosition_uColor' +cc.UNIFORM_ALPHA_TEST_VALUE_S = 'CC_AlphaValue' +cc.UNIFORM_COS_TIME_S = 'CC_CosTime' +cc.UNIFORM_MV_MATRIX_S = 'CC_MVMatrix' +cc.UNIFORM_MVP_MATRIX_S = 'CC_MVPMatrix' +cc.UNIFORM_P_MATRIX_S = 'CC_PMatrix' +cc.UNIFORM_RANDOM01_S = 'CC_Random01' +cc.UNIFORM_SAMPLER_S = 'CC_Texture0' +cc.UNIFORM_SIN_TIME_S = 'CC_SinTime' +cc.UNIFORM_TIME_S = 'CC_Time' + +cc.PLATFORM_OS_WINDOWS = 0 +cc.PLATFORM_OS_LINUX = 1 +cc.PLATFORM_OS_MAC = 2 +cc.PLATFORM_OS_ANDROID = 3 +cc.PLATFORM_OS_IPHONE = 4 +cc.PLATFORM_OS_IPAD = 5 +cc.PLATFORM_OS_BLACKBERRY = 6 +cc.PLATFORM_OS_NACL = 7 +cc.PLATFORM_OS_EMSCRIPTEN = 8 +cc.PLATFORM_OS_TIZEN = 9 + +cc.SCROLLVIEW_SCRIPT_SCROLL = 0 +cc.SCROLLVIEW_SCRIPT_ZOOM = 1 +cc.TABLECELL_TOUCHED = 2 +cc.TABLECELL_HIGH_LIGHT = 3 +cc.TABLECELL_UNHIGH_LIGHT = 4 +cc.TABLECELL_WILL_RECYCLE = 5 +cc.TABLECELL_SIZE_FOR_INDEX = 6 +cc.TABLECELL_SIZE_AT_INDEX = 7 +cc.NUMBER_OF_CELLS_IN_TABLEVIEW = 8 + +cc.SCROLLVIEW_DIRECTION_NONE = -1 +cc.SCROLLVIEW_DIRECTION_HORIZONTAL = 0 +cc.SCROLLVIEW_DIRECTION_VERTICAL = 1 +cc.SCROLLVIEW_DIRECTION_BOTH = 2 + +cc.CONTROL_EVENTTYPE_TOUCH_DOWN = 1 +cc.CONTROL_EVENTTYPE_DRAG_INSIDE = 2 +cc.CONTROL_EVENTTYPE_DRAG_OUTSIDE = 4 +cc.CONTROL_EVENTTYPE_DRAG_ENTER = 8 +cc.CONTROL_EVENTTYPE_DRAG_EXIT = 16 +cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE = 32 +cc.CONTROL_EVENTTYPE_TOUCH_UP_OUTSIDE = 64 +cc.CONTROL_EVENTTYPE_TOUCH_CANCEL = 128 +cc.CONTROL_EVENTTYPE_VALUE_CHANGED = 256 + +cc.CONTROL_STATE_NORMAL = 1 +cc.CONTROL_STATE_HIGH_LIGHTED = 2 +cc.CONTROL_STATE_DISABLED = 4 +cc.CONTROL_STATE_SELECTED = 8 + + +cc.KEYBOARD_RETURNTYPE_DEFAULT = 0 +cc.KEYBOARD_RETURNTYPE_DONE = 1 +cc.KEYBOARD_RETURNTYPE_SEND = 2 +cc.KEYBOARD_RETURNTYPE_SEARCH = 3 +cc.KEYBOARD_RETURNTYPE_GO = 4 + + +cc.EDITBOX_INPUT_MODE_ANY = 0 +cc.EDITBOX_INPUT_MODE_EMAILADDR = 1 +cc.EDITBOX_INPUT_MODE_NUMERIC = 2 +cc.EDITBOX_INPUT_MODE_PHONENUMBER = 3 +cc.EDITBOX_INPUT_MODE_URL = 4 +cc.EDITBOX_INPUT_MODE_DECIMAL = 5 +cc.EDITBOX_INPUT_MODE_SINGLELINE = 6 + + +cc.EDITBOX_INPUT_FLAG_PASSWORD = 0 +cc.EDITBOX_INPUT_FLAG_SENSITIVE = 1 +cc.EDITBOX_INPUT_FLAG_INITIAL_CAPS_WORD = 2 +cc.EDITBOX_INPUT_FLAG_INITIAL_CAPS_SENTENCE = 3 +cc.EDITBOX_INPUT_FLAG_INITIAL_CAPS_ALL_CHARACTERS = 4 + +cc.LANGUAGE_ENGLISH = 0 +cc.LANGUAGE_CHINESE = 1 +cc.LANGUAGE_FRENCH = 2 +cc.LANGUAGE_ITALIAN = 3 +cc.LANGUAGE_GERMAN = 4 +cc.LANGUAGE_SPANISH = 5 +cc.LANGUAGE_RUSSIAN = 6 +cc.LANGUAGE_KOREAN = 7 +cc.LANGUAGE_JAPANESE = 8 +cc.LANGUAGE_HUNGARIAN = 9 +cc.LANGUAGE_PORTUGUESE = 10 +cc.LANGUAGE_ARABIC = 11 + +cc.NODE_ON_ENTER = 0 +cc.NODE_ON_EXIT = 1 +cc.NODE_ON_ENTER_TRANSITION_DID_FINISH = 2 +cc.NODE_ON_EXIT_TRANSITION_DID_START = 3 +cc.NODE_ON_CLEAN_UP = 4 + +cc.CONTROL_STEPPER_PART_MINUS = 0 +cc.CONTROL_STEPPER_PART_PLUS = 1 +cc.CONTROL_STEPPER_PART_NONE = 2 + +cc.TABLEVIEW_FILL_TOPDOWN = 0 +cc.TABLEVIEW_FILL_BOTTOMUP = 1 + +cc.WEBSOCKET_OPEN = 0 +cc.WEBSOCKET_MESSAGE = 1 +cc.WEBSOCKET_CLOSE = 2 +cc.WEBSOCKET_ERROR = 3 + +cc.WEBSOCKET_STATE_CONNECTING = 0 +cc.WEBSOCKET_STATE_OPEN = 1 +cc.WEBSOCKET_STATE_CLOSING = 2 +cc.WEBSOCKET_STATE_CLOSED = 3 + + +cc.XMLHTTPREQUEST_RESPONSE_STRING = 0 +cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER = 1 +cc.XMLHTTPREQUEST_RESPONSE_BLOB = 2 +cc.XMLHTTPREQUEST_RESPONSE_DOCUMENT = 3 +cc.XMLHTTPREQUEST_RESPONSE_JSON = 4 + +cc.ASSETSMANAGER_CREATE_FILE = 0 +cc.ASSETSMANAGER_NETWORK = 1 +cc.ASSETSMANAGER_NO_NEW_VERSION = 2 +cc.ASSETSMANAGER_UNCOMPRESS = 3 + +cc.ASSETSMANAGER_PROTOCOL_PROGRESS = 0 +cc.ASSETSMANAGER_PROTOCOL_SUCCESS = 1 +cc.ASSETSMANAGER_PROTOCOL_ERROR = 2 + +cc.Handler = cc.Handler or {} +cc.Handler.NODE = 0 +cc.Handler.MENU_CLICKED = 1 +cc.Handler.CALLFUNC = 2 +cc.Handler.SCHEDULE = 3 +cc.Handler.TOUCHES = 4 +cc.Handler.KEYPAD = 5 +cc.Handler.ACCELEROMETER = 6 +cc.Handler.CONTROL_TOUCH_DOWN = 7 +cc.Handler.CONTROL_TOUCH_DRAG_INSIDE = 8 +cc.Handler.CONTROL_TOUCH_DRAG_OUTSIDE = 9 +cc.Handler.CONTROL_TOUCH_DRAG_ENTER = 10 +cc.Handler.CONTROL_TOUCH_DRAG_EXIT = 11 +cc.Handler.CONTROL_TOUCH_UP_INSIDE = 12 +cc.Handler.CONTROL_TOUCH_UP_OUTSIDE = 13 +cc.Handler.CONTROL_TOUCH_UP_CANCEL = 14 +cc.Handler.CONTROL_VALUE_CHANGED = 15 +cc.Handler.WEBSOCKET_OPEN = 16 +cc.Handler.WEBSOCKET_MESSAGE = 17 +cc.Handler.WEBSOCKET_CLOSE = 18 +cc.Handler.WEBSOCKET_ERROR = 19 +cc.Handler.GL_NODE_DRAW = 20 +cc.Handler.SCROLLVIEW_SCROLL = 21 +cc.Handler.SCROLLVIEW_ZOOM = 22 +cc.Handler.TABLECELL_TOUCHED = 23 +cc.Handler.TABLECELL_HIGHLIGHT = 24 +cc.Handler.TABLECELL_UNHIGHLIGHT = 25 +cc.Handler.TABLECELL_WILL_RECYCLE = 26 +cc.Handler.TABLECELL_SIZE_FOR_INDEX = 27 +cc.Handler.TABLECELL_AT_INDEX = 28 +cc.Handler.TABLEVIEW_NUMS_OF_CELLS = 29 +cc.Handler.HTTPREQUEST_STATE_CHANGE = 30 +cc.Handler.ASSETSMANAGER_PROGRESS = 31 +cc.Handler.ASSETSMANAGER_SUCCESS = 32 +cc.Handler.ASSETSMANAGER_ERROR = 33 +cc.Handler.STUDIO_EVENT_LISTENER = 34 +cc.Handler.ARMATURE_EVENT = 35 +cc.Handler.EVENT_ACC = 36 +cc.Handler.EVENT_CUSTIOM = 37 +cc.Handler.EVENT_KEYBOARD_PRESSED = 38 +cc.Handler.EVENT_KEYBOARD_RELEASED = 39 +cc.Handler.EVENT_TOUCH_BEGAN = 40 +cc.Handler.EVENT_TOUCH_MOVED = 41 +cc.Handler.EVENT_TOUCH_ENDED = 42 +cc.Handler.EVENT_TOUCH_CANCELLED = 43 +cc.Handler.EVENT_TOUCHES_BEGAN = 44 +cc.Handler.EVENT_TOUCHES_MOVED = 45 +cc.Handler.EVENT_TOUCHES_ENDED = 46 +cc.Handler.EVENT_TOUCHES_CANCELLED = 47 +cc.Handler.EVENT_MOUSE_DOWN = 48 +cc.Handler.EVENT_MOUSE_UP = 49 +cc.Handler.EVENT_MOUSE_MOVE = 50 +cc.Handler.EVENT_MOUSE_SCROLL = 51 +cc.Handler.EVENT_SPINE = 52 +cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN = 53 +cc.Handler.EVENT_PHYSICS_CONTACT_PRESOLVE = 54 +cc.Handler.EVENT_PHYSICS_CONTACT_POSTSOLVE = 55 +cc.Handler.EVENT_PHYSICS_CONTACT_SEPERATE = 56 + +cc.EVENT_UNKNOWN = 0 +cc.EVENT_TOUCH_ONE_BY_ONE = 1 +cc.EVENT_TOUCH_ALL_AT_ONCE = 2 +cc.EVENT_KEYBOARD = 3 +cc.EVENT_MOUSE = 4 +cc.EVENT_ACCELERATION = 5 +cc.EVENT_CUSTOM = 6 + +cc.PHYSICSSHAPE_MATERIAL_DEFAULT = {density = 0.0, restitution = 0.5, friction = 0.5} +cc.PHYSICSBODY_MATERIAL_DEFAULT = {density = 0.1, restitution = 0.5, friction = 0.5} +cc.GLYPHCOLLECTION_DYNAMIC = 0 +cc.GLYPHCOLLECTION_NEHE = 1 +cc.GLYPHCOLLECTION_ASCII = 2 +cc.GLYPHCOLLECTION_CUSTOM = 3 + +cc.LabelEffect = +{ + NORMAL = 0, + OUTLINE = 1, + SHADOW = 2, + GLOW = 3, +} + +cc.KeyCode = +{ + KEY_NONE = 0, + KEY_PAUSE = 0x0013, + KEY_SCROLL_LOCK = 0x1014, + KEY_PRINT = 0x1061, + KEY_SYSREQ = 0x106A, + KEY_BREAK = 0x106B, + KEY_ESCAPE = 0x001B, + KEY_BACKSPACE = 0x0008, + KEY_TAB = 0x0009, + KEY_BACK_TAB = 0x0089, + KEY_RETURN = 0x000D, + KEY_CAPS_LOCK = 0x00E5, + KEY_SHIFT = 0x00E1, + KEY_CTRL = 0x00E3, + KEY_ALT = 0x00E9, + KEY_MENU = 0x1067, + KEY_HYPER = 0x10ED, + KEY_INSERT = 0x1063, + KEY_HOME = 0x1050, + KEY_PG_UP = 0x1055, + KEY_DELETE = 0x10FF, + KEY_END = 0x1057, + KEY_PG_DOWN = 0x1056, + KEY_LEFT_ARROW = 0x1051, + KEY_RIGHT_ARROW = 0x1053, + KEY_UP_ARROW = 0x1052, + KEY_DOWN_ARROW = 0x1054, + KEY_NUM_LOCK = 0x107F, + KEY_KP_PLUS = 0x10AB, + KEY_KP_MINUS = 0x10AD, + KEY_KP_MULTIPLY = 0x10AA, + KEY_KP_DIVIDE = 0x10AF, + KEY_KP_ENTER = 0x108D, + KEY_KP_HOME = 0x10B7, + KEY_KP_UP = 0x10B8, + KEY_KP_PG_UP = 0x10B9, + KEY_KP_LEFT = 0x10B4, + KEY_KP_FIVE = 0x10B5, + KEY_KP_RIGHT = 0x10B6, + KEY_KP_END = 0x10B1, + KEY_KP_DOWN = 0x10B2, + KEY_KP_PG_DOWN = 0x10B3, + KEY_KP_INSERT = 0x10B0, + KEY_KP_DELETE = 0x10AE, + KEY_F1 = 0x00BE, + KEY_F2 = 0x00BF, + KEY_F3 = 0x00C0, + KEY_F4 = 0x00C1, + KEY_F5 = 0x00C2, + KEY_F6 = 0x00C3, + KEY_F7 = 0x00C4, + KEY_F8 = 0x00C5, + KEY_F9 = 0x00C6, + KEY_F10 = 0x00C7, + KEY_F11 = 0x00C8, + KEY_F12 = 0x00C9, + KEY_SPACE = ' ', + KEY_EXCLAM = '!', + KEY_QUOTE = '"', + KEY_NUMBER = '#', + KEY_DOLLAR = '$', + KEY_PERCENT = '%', + KEY_CIRCUMFLEX = '^', + KEY_AMPERSAND = '&', + KEY_APOSTROPHE = '\'', + KEY_LEFT_PARENTHESIS = '(', + KEY_RIGHT_PARENTHESIS = ')', + KEY_ASTERISK = '*', + KEY_PLUS = '+', + KEY_COMMA = ',', + KEY_MINUS = '-', + KEY_PERIOD = '.', + KEY_SLASH = '/', + KEY_0 = '0', + KEY_1 = '1', + KEY_2 = '2', + KEY_3 = '3', + KEY_4 = '4', + KEY_5 = '5', + KEY_6 = '6', + KEY_7 = '7', + KEY_8 = '8', + KEY_9 = '9', + KEY_COLON = ':', + KEY_SEMICOLON = ';', + KEY_LESS_THAN = '<', + KEY_EQUAL = '=', + KEY_GREATER_THAN = '>', + KEY_QUESTION = '?', + KEY_AT = '@', + KEY_CAPITAL_A = 'A', + KEY_CAPITAL_B = 'B', + KEY_CAPITAL_C = 'C', + KEY_CAPITAL_D = 'D', + KEY_CAPITAL_E = 'E', + KEY_CAPITAL_F = 'F', + KEY_CAPITAL_G = 'G', + KEY_CAPITAL_H = 'H', + KEY_CAPITAL_I = 'I', + KEY_CAPITAL_J = 'J', + KEY_CAPITAL_K = 'K', + KEY_CAPITAL_L = 'L', + KEY_CAPITAL_M = 'M', + KEY_CAPITAL_N = 'N', + KEY_CAPITAL_O = 'O', + KEY_CAPITAL_P = 'P', + KEY_CAPITAL_Q = 'Q', + KEY_CAPITAL_R = 'R', + KEY_CAPITAL_S = 'S', + KEY_CAPITAL_T = 'T', + KEY_CAPITAL_U = 'U', + KEY_CAPITAL_V = 'V', + KEY_CAPITAL_W = 'W', + KEY_CAPITAL_X = 'X', + KEY_CAPITAL_Y = 'Y', + KEY_CAPITAL_Z = 'Z', + KEY_LEFT_BRACKET = '[', + KEY_BACK_SLASH = '\\', + KEY_RIGHT_BRACKET = ']', + KEY_UNDERSCORE = '_', + KEY_GRAVE = '`', + KEY_A = 'a', + KEY_B = 'b', + KEY_C = 'c', + KEY_D = 'd', + KEY_E = 'e', + KEY_F = 'f', + KEY_G = 'g', + KEY_H = 'h', + KEY_I = 'i', + KEY_J = 'j', + KEY_K = 'k', + KEY_L = 'l', + KEY_M = 'm', + KEY_N = 'n', + KEY_O = 'o', + KEY_P = 'p', + KEY_Q = 'q', + KEY_R = 'r', + KEY_S = 's', + KEY_T = 't', + KEY_U = 'u', + KEY_V = 'v', + KEY_W = 'w', + KEY_X = 'x', + KEY_Y = 'y', + KEY_Z = 'z', + KEY_LEFT_BRACE = '{', + KEY_BAR = '|', + KEY_RIGHT_BRACE = '}', + KEY_TILDE = '~', + KEY_EURO = 0x20AC, + KEY_POUND = 0x00A3, + KEY_YEN = 0x00A5, + KEY_MIDDLE_DOT = 0x0095, + KEY_SEARCH = 0xFFAA, +}; + + + diff --git a/cocos/scripting/lua-bindings/script/Deprecated.lua b/cocos/scripting/lua-bindings/script/Deprecated.lua new file mode 100644 index 0000000000..44523bd242 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/Deprecated.lua @@ -0,0 +1,1150 @@ +require "Cocos2d.lua" +--tip +local function deprecatedTip(old_name,new_name) + print("\n********** \n"..old_name.." was deprecated please use ".. new_name .. " instead.\n**********") +end + + +--functions of CCDirector will be deprecated,begin +local CCDirectorDeprecated = { } +function CCDirectorDeprecated.sharedDirector() + deprecatedTip("CCDirector:sharedDirector","cc.Director:getInstance") + return cc.Director:getInstance() +end +rawset(CCDirector,"sharedDirector",CCDirectorDeprecated.sharedDirector) +--functions of CCDirector will be deprecated,end + + +--functions of CCTextureCache will be deprecated begin +local TextureCacheDeprecated = {} +function TextureCacheDeprecated.getInstance(self) + deprecatedTip("cc.TextureCache:getInstance","cc.Director:getInstance():getTextureCache") + return cc.Director:getInstance():getTextureCache() +end +rawset(cc.TextureCache,"getInstance",TextureCacheDeprecated.getInstance) + +function TextureCacheDeprecated.destroyInstance(self) + deprecatedTip("cc.TextureCache:destroyInstance","cc.Director:getInstance():destroyTextureCache") + return cc.Director:getInstance():destroyTextureCache() +end +rawset(cc.TextureCache,"destroyInstance",TextureCacheDeprecated.destroyInstance) + +function TextureCacheDeprecated.dumpCachedTextureInfo(self) + deprecatedTip("self:dumpCachedTextureInfo","self:getCachedTextureInfo") + return print(self:getCachedTextureInfo()) +end +rawset(cc.TextureCache,"dumpCachedTextureInfo",TextureCacheDeprecated.dumpCachedTextureInfo) + +local CCTextureCacheDeprecated = { } +function CCTextureCacheDeprecated.sharedTextureCache() + deprecatedTip("CCTextureCache:sharedTextureCache","CCTextureCache:getInstance") + return cc.TextureCache:getInstance() +end +rawset(CCTextureCache,"sharedTextureCache",CCTextureCacheDeprecated.sharedTextureCache) + +function CCTextureCacheDeprecated.purgeSharedTextureCache() + deprecatedTip("CCTextureCache:purgeSharedTextureCache","CCTextureCache:destroyInstance") + return cc.TextureCache:destroyInstance() +end +rawset(CCTextureCache,"purgeSharedTextureCache",CCTextureCacheDeprecated.purgeSharedTextureCache) + +function CCTextureCacheDeprecated.addUIImage(self, image, key) + deprecatedTip("CCTextureCache:addUIImage","CCTextureCache:addImage") + return self:addImage(image,key) +end +rawset(CCTextureCache,"addUIImage",CCTextureCacheDeprecated.addUIImage) +--functions of CCTextureCache will be deprecated end + +--functions of CCAnimation will be deprecated begin +local CCAnimationDeprecated = {} +function CCAnimationDeprecated.addSpriteFrameWithFileName(self,...) + deprecatedTip("CCAnimationDeprecated:addSpriteFrameWithFileName","cc.Animation:addSpriteFrameWithFile") + return self:addSpriteFrameWithFile(...) +end +rawset(CCAnimation,"addSpriteFrameWithFileName",CCAnimationDeprecated.addSpriteFrameWithFileName) +--functions of CCAnimation will be deprecated end + + +--functions of CCAnimationCache will be deprecated begin +local CCAnimationCacheDeprecated = { } +function CCAnimationCacheDeprecated.sharedAnimationCache() + deprecatedTip("CCAnimationCache:sharedAnimationCache","CCAnimationCache:getInstance") + return CCAnimationCache:getInstance() +end +rawset(CCAnimationCache,"sharedAnimationCache",CCAnimationCacheDeprecated.sharedAnimationCache) + +function CCAnimationCacheDeprecated.purgeSharedAnimationCache() + deprecatedTip("CCAnimationCache:purgeSharedAnimationCache","CCAnimationCache:destroyInstance") + return CCAnimationCache:destroyInstance() +end +rawset(CCAnimationCache,"purgeSharedAnimationCache",CCAnimationCacheDeprecated.purgeSharedAnimationCache) + +function CCAnimationCacheDeprecated.addAnimationsWithFile(self,...) + deprecatedTip("CCAnimationCache:addAnimationsWithFile","cc.AnimationCache:addAnimations") + return self:addAnimations(...) +end +rawset(CCAnimationCache,"addAnimationsWithFile",CCAnimationCacheDeprecated.addAnimationsWithFile) + +function CCAnimationCacheDeprecated.animationByName(self,...) + deprecatedTip("CCAnimationCache:animationByName","cc.AnimationCache:getAnimation") + return self:getAnimation(...) +end +rawset(CCAnimationCache,"animationByName",CCAnimationCacheDeprecated.animationByName) + +function CCAnimationCacheDeprecated.removeAnimationByName(self) + deprecatedTip("CCAnimationCache:removeAnimationByName","cc.AnimationCache:removeAnimation") + return self:removeAnimation() +end +rawset(CCAnimationCache,"removeAnimationByName",CCAnimationCacheDeprecated.removeAnimationByName) +--functions of CCAnimationCache will be deprecated end + +--functions of CCFileUtils will be deprecated end +local CCFileUtilsDeprecated = { } +function CCFileUtilsDeprecated.sharedFileUtils() + deprecatedTip("CCFileUtils:sharedFileUtils","CCFileUtils:getInstance") + return cc.FileUtils:getInstance() +end +rawset(CCFileUtils,"sharedFileUtils",CCFileUtilsDeprecated.sharedFileUtils) + +function CCFileUtilsDeprecated.purgeFileUtils() + deprecatedTip("CCFileUtils:purgeFileUtils","CCFileUtils:destroyInstance") + return cc.FileUtils:destroyInstance() +end +rawset(CCFileUtils,"purgeFileUtils",CCFileUtilsDeprecated.purgeFileUtils) +--functions of CCFileUtils will be deprecated end + + +--functions of SimpleAudioEngine will be deprecated begin +local SimpleAudioEngineDeprecated = { } +function SimpleAudioEngineDeprecated.sharedEngine() + deprecatedTip("SimpleAudioEngine:sharedEngine","SimpleAudioEngine:getInstance") + return cc.SimpleAudioEngine:getInstance() +end +rawset(SimpleAudioEngine,"sharedEngine",SimpleAudioEngineDeprecated.sharedEngine) + +function SimpleAudioEngineDeprecated.playBackgroundMusic(self,...) + deprecatedTip("SimpleAudioEngine:playBackgroundMusic","SimpleAudioEngine:playMusic") + return self:playMusic(...) +end +rawset(SimpleAudioEngine,"playBackgroundMusic",SimpleAudioEngineDeprecated.playBackgroundMusic) +--functions of SimpleAudioEngine will be deprecated end + +--functions of CCMenu will be deprecated begin +local CCMenuDeprecated = { } +function CCMenuDeprecated.createWithItem(self,...) + deprecatedTip("CCMenuDeprecated:createWithItem","cc.Menu:createWithItem") + return self:create(...) +end +rawset(CCMenu,"createWithItem",CCMenuDeprecated.createWithItem) + +function CCMenuDeprecated.setHandlerPriority(self) + print("\n********** \n".."setHandlerPriority was deprecated in 3.0. \n**********") +end +rawset(CCMenu,"setHandlerPriority",CCMenuDeprecated.setHandlerPriority) +--functions of CCMenu will be deprecated end + +--functions of CCNode will be deprecated begin +local CCNodeDeprecated = { } + +function CCNodeDeprecated.boundingBox(self) + deprecatedTip("CCNode:boundingBox","cc.Node:getBoundingBox") + return self:getBoundingBox() +end +rawset(CCNode,"boundingBox",CCNodeDeprecated.boundingBox) + + +function CCNodeDeprecated.numberOfRunningActions(self) + deprecatedTip("CCNode:numberOfRunningActions","cc.Node:getNumberOfRunningActions") + return self:getNumberOfRunningActions() +end +rawset(CCNode,"numberOfRunningActions",CCNodeDeprecated.numberOfRunningActions) + + +function CCNodeDeprecated.removeFromParentAndCleanup(self,...) + deprecatedTip("CCNode:removeFromParentAndCleanup","cc.Node:removeFromParent") + return self:removeFromParent(...) +end +rawset(CCNode,"removeFromParentAndCleanup",CCNodeDeprecated.removeFromParentAndCleanup) +--functions of CCNode will be deprecated end + +--CCDrawPrimitives will be deprecated begin +local function CCDrawPrimitivesClassDeprecated() + deprecatedTip("CCDrawPrimitives","cc.DrawPrimitives") + return cc.DrawPrimitives +end +_G["CCDrawPrimitives"] = CCDrawPrimitivesClassDeprecated() +--functions of CCDrawPrimitives will be deprecated begin +local CCDrawPrimitivesDeprecated = { } +function CCDrawPrimitivesDeprecated.ccDrawPoint(pt) + deprecatedTip("ccDrawPoint","cc.DrawPrimitives.drawPoint") + return cc.DrawPrimitives.drawPoint(pt) +end +rawset(_G, "ccDrawPoint", CCDrawPrimitivesDeprecated.ccDrawPoint) + +function CCDrawPrimitivesDeprecated.ccDrawLine(origin,destination) + deprecatedTip("ccDrawLine","cc.DrawPrimitives.drawLine") + return cc.DrawPrimitives.drawLine(origin,destination) +end +rawset(_G, "ccDrawLine", CCDrawPrimitivesDeprecated.ccDrawLine) + +function CCDrawPrimitivesDeprecated.ccDrawRect(origin,destination) + deprecatedTip("ccDrawRect","cc.DrawPrimitives.drawRect") + return cc.DrawPrimitives.drawRect(origin,destination) +end +rawset(_G, "ccDrawRect", CCDrawPrimitivesDeprecated.ccDrawRect) + +function CCDrawPrimitivesDeprecated.ccDrawSolidRect(origin,destination,color) + deprecatedTip("ccDrawSolidRect","cc.DrawPrimitives.drawSolidRect") + return cc.DrawPrimitives.drawSolidRect(origin,destination,color) +end +rawset(_G, "ccDrawSolidRect", CCDrawPrimitivesDeprecated.ccDrawSolidRect) + +-- params:... may represent two param(xScale,yScale) or nil +function CCDrawPrimitivesDeprecated.ccDrawCircle(center,radius,angle,segments,drawLineToCenter,...) + deprecatedTip("ccDrawCircle","cc.DrawPrimitives.drawCircle") + return cc.DrawPrimitives.drawCircle(center,radius,angle,segments,drawLineToCenter,...) +end +rawset(_G, "ccDrawCircle", CCDrawPrimitivesDeprecated.ccDrawCircle) + +-- params:... may represent two param(xScale,yScale) or nil +function CCDrawPrimitivesDeprecated.ccDrawSolidCircle(center,radius,angle,segments,...) + deprecatedTip("ccDrawSolidCircle","cc.DrawPrimitives.drawSolidCircle") + return cc.DrawPrimitives.drawSolidCircle(center,radius,angle,segments,...) +end +rawset(_G, "ccDrawSolidCircle", CCDrawPrimitivesDeprecated.ccDrawSolidCircle) + +function CCDrawPrimitivesDeprecated.ccDrawQuadBezier(origin,control,destination,segments) + deprecatedTip("ccDrawQuadBezier","cc.DrawPrimitives.drawQuadBezier") + return cc.DrawPrimitives.drawQuadBezier(origin,control,destination,segments) +end +rawset(_G, "ccDrawQuadBezier", CCDrawPrimitivesDeprecated.ccDrawQuadBezier) + +function CCDrawPrimitivesDeprecated.ccDrawCubicBezier(origin,control1,control2,destination,segments) + deprecatedTip("ccDrawCubicBezier","cc.DrawPrimitives.drawCubicBezier") + return cc.DrawPrimitives.drawCubicBezier(origin,control1,control2,destination,segments) +end +rawset(_G, "ccDrawCubicBezier", CCDrawPrimitivesDeprecated.ccDrawCubicBezier) + +function CCDrawPrimitivesDeprecated.ccDrawCatmullRom(arrayOfControlPoints,segments) + deprecatedTip("ccDrawCatmullRom","cc.DrawPrimitives.drawCatmullRom") + return cc.DrawPrimitives.drawCatmullRom(arrayOfControlPoints,segments) +end +rawset(_G, "ccDrawCatmullRom", CCDrawPrimitivesDeprecated.ccDrawCatmullRom) + +function CCDrawPrimitivesDeprecated.ccDrawCardinalSpline(config,tension,segments) + deprecatedTip("ccDrawCardinalSpline","cc.DrawPrimitives.drawCardinalSpline") + return cc.DrawPrimitives.drawCardinalSpline(config,tension,segments) +end +rawset(_G, "ccDrawCardinalSpline", CCDrawPrimitivesDeprecated.ccDrawCardinalSpline) + +function CCDrawPrimitivesDeprecated.ccDrawColor4B(r,g,b,a) + deprecatedTip("ccDrawColor4B","cc.DrawPrimitives.drawColor4B") + return cc.DrawPrimitives.drawColor4B(r,g,b,a) +end +rawset(_G, "ccDrawColor4B", CCDrawPrimitivesDeprecated.ccDrawColor4B) + +function CCDrawPrimitivesDeprecated.ccDrawColor4F(r,g,b,a) + deprecatedTip("ccDrawColor4F","cc.DrawPrimitives.drawColor4F") + return cc.DrawPrimitives.drawColor4F(r,g,b,a) +end +rawset(_G, "ccDrawColor4F", CCDrawPrimitivesDeprecated.ccDrawColor4F) + +function CCDrawPrimitivesDeprecated.ccPointSize(pointSize) + deprecatedTip("ccPointSize","cc.DrawPrimitives.setPointSize") + return cc.DrawPrimitives.setPointSize(pointSize) +end +rawset(_G, "ccPointSize", CCDrawPrimitivesDeprecated.ccPointSize) +--functions of CCDrawPrimitives will be deprecated end +--CCDrawPrimitives will be deprecated end + +local CCProgressTimerDeprecated = {} +function CCProgressTimerDeprecated.setReverseProgress(self,...) + deprecatedTip("CCProgressTimer","CCProgressTimer:setReverseDirection") + return self:setReverseDirection(...) +end +rawset(CCProgressTimer,"setReverseProgress",CCProgressTimerDeprecated.setReverseProgress) + +--functions of CCSpriteFrameCache will be deprecated begin +local CCSpriteFrameCacheDeprecated = { } +function CCSpriteFrameCacheDeprecated.spriteFrameByName(self,szName) + deprecatedTip("CCSpriteFrameCache:spriteFrameByName","CCSpriteFrameCache:getSpriteFrameByName") + return self:getSpriteFrameByName(szName) +end +rawset(CCSpriteFrameCache,"spriteFrameByName",CCSpriteFrameCacheDeprecated.spriteFrameByName) + +function CCSpriteFrameCacheDeprecated.sharedSpriteFrameCache() + deprecatedTip("CCSpriteFrameCache:sharedSpriteFrameCache","CCSpriteFrameCache:getInstance") + return CCSpriteFrameCache:getInstance() +end +rawset(CCSpriteFrameCache,"sharedSpriteFrameCache",CCSpriteFrameCacheDeprecated.sharedSpriteFrameCache) + +function CCSpriteFrameCacheDeprecated.purgeSharedSpriteFrameCache() + deprecatedTip("CCSpriteFrameCache:purgeSharedSpriteFrameCache","CCSpriteFrameCache:destroyInstance") + return CCSpriteFrameCache:destroyInstance() +end +rawset(CCSpriteFrameCache,"purgeSharedSpriteFrameCache",CCSpriteFrameCacheDeprecated.purgeSharedSpriteFrameCache) + +function CCSpriteFrameCacheDeprecated.addSpriteFramesWithFile(self,...) + deprecatedTip("CCSpriteFrameCache:addSpriteFramesWithFile","CCSpriteFrameCache:addSpriteFrames") + return self:addSpriteFrames(...) +end +rawset(CCSpriteFrameCache,"addSpriteFramesWithFile",CCSpriteFrameCacheDeprecated.addSpriteFramesWithFile) + +function CCSpriteFrameCacheDeprecated.getSpriteFrameByName(self,...) + deprecatedTip("CCSpriteFrameCache:getSpriteFrameByName","CCSpriteFrameCache:getSpriteFrame") + return self:getSpriteFrame(...) +end +rawset(CCSpriteFrameCache,"getSpriteFrameByName",CCSpriteFrameCacheDeprecated.getSpriteFrameByName) +--functions of CCSpriteFrameCache will be deprecated end + +--functions of CCLabelAtlas will be deprecated begin +local CCLabelAtlasDeprecated = {} +function CCLabelAtlasDeprecated.create(self,...) + deprecatedTip("CCLabelAtlas:create","CCLabelAtlas:_create") + return self:_create(...) +end +rawset(CCLabelAtlas,"create",CCLabelAtlasDeprecated.create) +--functions of CCLabelAtlas will be deprecated end + + +--------------------------- +--global functions wil be deprecated, begin +local function CCRectMake(x,y,width,height) + deprecatedTip("CCRectMake(x,y,width,height)","cc.rect(x,y,width,height) in lua") + return cc.rect(x,y,width,height) +end +rawset(_G,"CCRectMake",CCRectMake) + +local function ccc3(r,g,b) + deprecatedTip("ccc3(r,g,b)","cc.c3b(r,g,b)") + return cc.c3b(r,g,b) +end +rawset(_G,"ccc3",ccc3) + +local function ccp(x,y) + deprecatedTip("ccp(x,y)","cc.p(x,y)") + return cc.p(x,y) +end +rawset(_G,"ccp",ccp) + +local function CCSizeMake(width,height) + deprecatedTip("CCSizeMake(width,height)","cc.size(width,height)") + return cc.size(width,height) +end +rawset(_G,"CCSizeMake",CCSizeMake) + +local function ccc4(r,g,b,a) + deprecatedTip("ccc4(r,g,b,a)","cc.c4b(r,g,b,a)") + return cc.c4b(r,g,b,a) +end +rawset(_G,"ccc4",ccc4) + +local function ccc4FFromccc3B(color3B) + deprecatedTip("ccc4FFromccc3B(color3B)","cc.c4f(color3B.r / 255.0,color3B.g / 255.0,color3B.b / 255.0,1.0)") + return cc.c4f(color3B.r/255.0, color3B.g/255.0, color3B.b/255.0, 1.0) +end +rawset(_G,"ccc4FFromccc3B",ccc4FFromccc3B) + +local function ccc4f(r,g,b,a) + deprecatedTip("ccc4f(r,g,b,a)","cc.c4f(r,g,b,a)") + return cc.c4f(r,g,b,a) +end +rawset(_G,"ccc4f",ccc4f) + +local function ccc4FFromccc4B(color4B) + deprecatedTip("ccc4FFromccc4B(color4B)","cc.c4f(color4B.r/255.0, color4B.g/255.0, color4B.b/255.0, color4B.a/255.0)") + return cc.c4f(color4B.r/255.0, color4B.g/255.0, color4B.b/255.0, color4B.a/255.0) +end +rawset(_G,"ccc4FFromccc4B",ccc4FFromccc4B) + +local function ccc4FEqual(a,b) + deprecatedTip("ccc4FEqual(a,b)","a:equals(b)") + return a:equals(b) +end +rawset(_G,"ccc4FEqual",ccc4FEqual) +--global functions wil be deprecated, end + + +--functions of _G will be deprecated begin +local function ccpLineIntersect(a,b,c,d,s,t) + deprecatedTip("ccpLineIntersect","cc.pIsLineIntersect") + return cc.pIsLineIntersect(a,b,c,d,s,t) +end +rawset(_G,"ccpLineIntersect",ccpLineIntersect) + + +local function CCPointMake(x,y) + deprecatedTip("CCPointMake(x,y)","cc.p(x,y)") + return cc.p(x,y) +end +rawset(_G,"CCPointMake",CCPointMake) + + + +local function ccpNeg(pt) + deprecatedTip("ccpNeg","cc.pSub") + return cc.pSub({x = 0,y = 0}, pt) +end +rawset(_G,"ccpNeg",ccpNeg) + +local function ccpAdd(pt1,pt2) + deprecatedTip("ccpAdd","cc.pAdd") + return cc.pAdd(pt1,pt2) +end +rawset(_G,"ccpAdd",ccpAdd) + +local function ccpSub(pt1,pt2) + deprecatedTip("ccpSub","cc.pSub") + return cc.pSub(pt1,pt2) +end +rawset(_G,"ccpSub",ccpSub) + +local function ccpMult(pt,factor) + deprecatedTip("ccpMult","cc.pMul") + return cc.pMul(pt,factor) +end +rawset(_G,"ccpMult",ccpMult) + +local function ccpMidpoint(pt1,pt2) + deprecatedTip("ccpMidpoint","cc.pMidpoint") + return cc.pMidpoint(pt1,pt2) +end +rawset(_G,"ccpMidpoint",ccpMidpoint) + +local function ccpDot(pt1,pt2) + deprecatedTip("ccpDot","cc.pDot") + return cc.pDot(pt1,pt2) +end +rawset(_G,"ccpDot",ccpDot) + +local function ccpCross(pt1,pt2) + deprecatedTip("ccpCross","cc.pCross") + return cc.pCross(pt1, pt2) +end +rawset(_G,"ccpCross",ccpCross) + +local function ccpPerp(pt) + deprecatedTip("ccpPerp","cc.pPerp") + return cc.pPerp(pt) +end +rawset(_G,"ccpPerp",ccpPerp) + +local function ccpRPerp(pt) + deprecatedTip("ccpRPerp","cc.RPerp") + return cc.RPerp(pt) +end +rawset(_G,"ccpRPerp",ccpRPerp) + +local function ccpProject(pt1,pt2) + deprecatedTip("ccpProject","cc.pProject") + return cc.pProject(pt1,pt2) +end +rawset(_G,"ccpProject",ccpProject) + +local function ccpRotate(pt1,pt2) + deprecatedTip("ccpRotate","cc.pRotate") + return cc.pRotate(pt1,pt2) +end +rawset(_G,"ccpRotate",ccpRotate) + +local function ccpUnrotate(pt1,pt2) + deprecatedTip("ccpUnrotate","cc.pUnrotate") + return cc.pUnrotate(pt1,pt2) +end +rawset(_G,"ccpUnrotate",ccpUnrotate) + +local function ccpLengthSQ(pt) + deprecatedTip("ccpLengthSQ","cc.pLengthSQ") + return cc.pLengthSQ(pt) +end +rawset(_G,"ccpLengthSQ",ccpLengthSQ) + +local function ccpDistanceSQ(pt1,pt2) + deprecatedTip("ccpDistanceSQ","cc.pDistanceSQ") + return cc.pDistanceSQ(pt1,pt2) +end +rawset(_G,"ccpDistanceSQ",ccpDistanceSQ) + +local function ccpLength(pt) + deprecatedTip("ccpLength","cc.pGetLength") + return cc.pGetLength(pt) +end +rawset(_G,"ccpLength",ccpLength) + +local function ccpDistance(pt1,pt2) + deprecatedTip("ccpDistance","cc.pGetDistance") + return cc.pGetDistance(pt1, pt2) +end +rawset(_G,"ccpDistance",ccpDistance) + +local function ccpNormalize(pt) + deprecatedTip("ccpNormalize","cc.pNormalize") + return cc.pNormalize(pt) +end +rawset(_G,"ccpNormalize",ccpNormalize) + +local function ccpForAngle(angle) + deprecatedTip("ccpForAngle","cc.pForAngle") + return cc.pForAngle(angle) +end +rawset(_G,"ccpForAngle",ccpForAngle) + +local function ccpToAngle(pt) + deprecatedTip("ccpToAngle","cc.pToAngleSelf") + return cc.pToAngleSelf(pt) +end +rawset(_G,"ccpToAngle",ccpToAngle) + +local function ccpClamp(pt1,pt2,pt3) + deprecatedTip("ccpClamp","cc.pGetClampPoint") + return cc.pGetClampPoint(pt1,pt2,pt3) +end +rawset(_G,"ccpClamp",ccpClamp) + + +local function ccpFromSize(sz) + deprecatedTip("ccpFromSize(sz)","cc.pFromSize") + return cc.pFromSize(sz) +end +rawset(_G,"ccpFromSize",ccpFromSize) + +local function ccpLerp(pt1,pt2,alpha) + deprecatedTip("ccpLerp","cc.pLerp") + return cc.pLerp(pt1,pt2,alpha) +end +rawset(_G,"ccpLerp",ccpLerp) + +local function ccpFuzzyEqual(pt1,pt2,variance) + deprecatedTip("ccpFuzzyEqual","cc.pFuzzyEqual") + return cc.pFuzzyEqual(pt1,pt2,variance) +end +rawset(_G,"ccpFuzzyEqual",ccpFuzzyEqual) + +local function ccpCompMult(pt1,pt2) + deprecatedTip("ccpCompMult","cc.p") + return cc.p(pt1.x * pt2.x , pt1.y * pt2.y) +end +rawset(_G,"ccpCompMult",ccpCompMult) + +local function ccpAngleSigned(pt1,pt2) + deprecatedTip("ccpAngleSigned","cc.pGetAngle") + return cc.pGetAngle(pt1, pt2) +end +rawset(_G,"ccpAngleSigned",ccpAngleSigned) + +local function ccpAngle(pt1,pt2) + deprecatedTip("ccpAngle","cc.pGetAngle") + return cc.pGetAngle(pt1,ptw) +end +rawset(_G,"ccpAngle",ccpAngle) + +local function ccpRotateByAngle(pt1,pt2,angle) + deprecatedTip("ccpRotateByAngle","cc.pRotateByAngle") + return cc.pRotateByAngle(pt1, pt2, angle) +end +rawset(_G,"ccpRotateByAngle",ccpRotateByAngle) + +local function ccpSegmentIntersect(pt1,pt2,pt3,pt4) + deprecatedTip("ccpSegmentIntersect","cc.pIsSegmentIntersect") + return cc.pIsSegmentIntersect(pt1,pt2,pt3,pt4) +end +rawset(_G,"ccpSegmentIntersect",ccpSegmentIntersect) + +local function ccpIntersectPoint(pt1,pt2,pt3,pt4) + deprecatedTip("ccpIntersectPoint","cc.pGetIntersectPoint") + return cc.pGetIntersectPoint(pt1,pt2,pt3,pt4) +end +rawset(_G,"ccpIntersectPoint",ccpIntersectPoint) + + +local function vertex2(x,y) + deprecatedTip("vertex2(x,y)","cc.vertex2F(x,y)") + return cc.vertex2F(x,y) +end +rawset(_G,"vertex2",vertex2) + +local function vertex3(x,y,z) + deprecatedTip("vertex3(x,y,z)","cc.Vertex3F(x,y,z)") + return cc.Vertex3F(x,y,z) +end +rawset(_G,"vertex3",vertex3) + +local function tex2(u,v) + deprecatedTip("tex2(u,v)","cc.tex2f(u,v)") + return cc.tex2f(u,v) +end +rawset(_G,"tex2",tex2) + +local function ccc4BFromccc4F(color4F) + deprecatedTip("ccc4BFromccc4F(color4F)","Color4B(color4F.r * 255.0, color4F.g * 255.0, color4F.b * 255.0, color4B.a * 255.0)") + return Color4B(color4F.r * 255.0, color4F.g * 255.0, color4F.b * 255.0, color4B.a * 255.0) +end +rawset(_G,"ccc4BFromccc4F",ccc4BFromccc4F) + +local function ccColor3BDeprecated() + deprecatedTip("ccColor3B","cc.c3b(0,0,0)") + return cc.c3b(0,0,0) +end +_G["ccColor3B"] = ccColor3BDeprecated + +local function ccColor4BDeprecated() + deprecatedTip("ccColor4B","cc.c4b(0,0,0,0)") + return cc.c4b(0,0,0,0) +end +_G["ccColor4B"] = ccColor4BDeprecated + +local function ccColor4FDeprecated() + deprecatedTip("ccColor4F","cc.c4f(0.0,0.0,0.0,0.0)") + return cc.c4f(0.0,0.0,0.0,0.0) +end +_G["ccColor4F"] = ccColor4FDeprecated + +local function ccVertex2FDeprecated() + deprecatedTip("ccVertex2F","cc.vertex2F(0.0,0.0)") + return cc.vertex2F(0.0,0.0) +end +_G["ccVertex2F"] = ccVertex2FDeprecated + +local function ccVertex3FDeprecated() + deprecatedTip("ccVertex3F","cc.Vertex3F(0.0, 0.0, 0.0)") + return cc.Vertex3F(0.0, 0.0, 0.0) +end +_G["ccVertex3F"] = ccVertex3FDeprecated + +local function ccTex2FDeprecated() + deprecatedTip("ccTex2F","cc.tex2F(0.0, 0.0)") + return cc.tex2F(0.0, 0.0) +end +_G["ccTex2F"] = ccTex2FDeprecated + +local function ccPointSpriteDeprecated() + deprecatedTip("ccPointSprite","cc.PointSprite(cc.vertex2F(0.0, 0.0),cc.c4b(0.0, 0.0, 0.0),0)") + return cc.PointSprite(cc.vertex2F(0.0, 0.0),cc.c4b(0.0, 0.0, 0.0),0) +end +_G["ccPointSprite"] = ccPointSpriteDeprecated + +local function ccQuad2Deprecated() + deprecatedTip("ccQuad2","cc.Quad2(cc.vertex2F(0.0, 0.0), cc.vertex2F(0.0, 0.0), cc.vertex2F(0.0, 0.0), cc.vertex2F(0.0, 0.0))") + return cc.Quad2(cc.vertex2F(0.0, 0.0), cc.vertex2F(0.0, 0.0), cc.vertex2F(0.0, 0.0), cc.vertex2F(0.0, 0.0)) +end +_G["ccQuad2"] = ccQuad2Deprecated + +local function ccQuad3Deprecated() + deprecatedTip("ccQuad3","cc.Quad3(cc.Vertex3F(0.0, 0.0 ,0.0), cc.Vertex3F(0.0, 0.0 ,0.0), cc.Vertex3F(0.0, 0.0 ,0.0), cc.Vertex3F(0.0, 0.0 ,0.0))") + return cc.Quad3(cc.Vertex3F(0.0, 0.0 ,0.0), cc.Vertex3F(0.0, 0.0 ,0.0), cc.Vertex3F(0.0, 0.0 ,0.0), cc.Vertex3F(0.0, 0.0 ,0.0)) +end +_G["ccQuad3"] = ccQuad3Deprecated + +local function ccV2FC4BT2FDeprecated() + deprecatedTip("ccV2F_C4B_T2F","cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0))") + return cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)) +end +_G["ccV2F_C4B_T2F"] = ccV2FC4BT2FDeprecated + + +local function ccV2FC4FT2FDeprecated() + deprecatedTip("ccV2F_C4F_T2F","cc.V2F_C4F_T2F(cc.vertex2F(0.0, 0.0), cc.c4f(0.0 , 0.0 , 0.0 , 0.0 ), cc.tex2F(0.0, 0.0))") + return cc.V2F_C4F_T2F(cc.vertex2F(0.0, 0.0), cc.c4f(0.0 , 0.0 , 0.0 , 0.0), cc.tex2F(0.0, 0.0)) +end +_G["ccV2F_C4F_T2F"] = ccV2FC4FT2FDeprecated + +local function ccV3FC4BT2FDeprecated() + deprecatedTip("ccV3F_C4B_T2F","cc.V3F_C4B_T2F(cc.vertex3F(0.0, 0.0, 0.0), cc.c4b(0 , 0 , 0, 0 ), cc.tex2F(0.0, 0.0))") + return cc.V3F_C4B_T2F(cc.vertex3F(0.0, 0.0, 0.0), cc.c4b(0 , 0 , 0, 0 ), cc.tex2F(0.0, 0.0)) +end +_G["ccV3F_C4B_T2F"] = ccV3FC4BT2FDeprecated + +local function ccV2FC4BT2FQuadDeprecated() + deprecatedTip("ccV2F_C4B_T2F_Quad","cc.V2F_C4B_T2F_Quad(cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)), cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)), cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)), cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)))") + return cc.V2F_C4B_T2F_Quad(cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)), cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)), cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)), cc.V2F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0))) +end +_G["ccV2F_C4B_T2F_Quad"] = ccV2FC4BT2FQuadDeprecated + +local function ccV3FC4BT2FQuadDeprecated() + deprecatedTip("ccV3F_C4B_T2F_Quad","cc.V3F_C4B_T2F_Quad(_tl, _bl, _tr, _br)") + return cc.V3F_C4B_T2F_Quad(cc.V3F_C4B_T2F(cc.vertex3F(0.0, 0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)), cc.V3F_C4B_T2F(cc.vertex3F(0.0, 0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)), cc.V3F_C4B_T2F(cc.vertex3F(0.0, 0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0)), cc.V3F_C4B_T2F(cc.vertex3F(0.0, 0.0, 0.0), cc.c4b(0 , 0, 0, 0 ), cc.tex2F(0.0, 0.0))) +end +_G["ccV3F_C4B_T2F_Quad"] = ccV3FC4BT2FQuadDeprecated + +local function ccV2FC4FT2FQuadDeprecated() + deprecatedTip("ccV2F_C4F_T2F_Quad","cc.V2F_C4F_T2F_Quad(_bl, _br, _tl, _tr)") + return cc.V2F_C4F_T2F_Quad(cc.V2F_C4F_T2F(cc.vertex2F(0.0, 0.0), cc.c4f(0.0 , 0.0, 0.0, 0.0 ), cc.tex2F(0.0, 0.0)), cc.V2F_C4F_T2F(cc.vertex2F(0.0, 0.0), cc.c4f(0.0 , 0.0, 0.0, 0.0 ), cc.tex2F(0.0, 0.0)), cc.V3F_C4B_T2F(cc.vertex2F(0.0, 0.0), cc.c4f(0.0 , 0.0, 0.0, 0.0 ), cc.tex2F(0.0, 0.0)), cc.V2F_C4F_T2F(cc.vertex2F(0.0, 0.0), cc.c4f(0.0 , 0.0, 0.0, 0.0 ), cc.tex2F(0.0, 0.0))) +end +_G["ccV2F_C4F_T2F_Quad"] = ccV2FC4FT2FQuadDeprecated + +local function ccT2FQuadDeprecated() + deprecatedTip("ccT2F_Quad","cc.T2F_Quad(_bl, _br, _tl, _tr)") + return cc.T2F_Quad(cc.tex2F(0.0,0.0), cc.tex2F(0.0,0.0), cc.tex2F(0.0,0.0), cc.tex2F(0.0,0.0)) +end +_G["ccT2F_Quad"] = ccT2FQuadDeprecated + +local function ccAnimationFrameDataDeprecated() + deprecatedTip("ccAnimationFrameData","cc.AnimationFrameData( _texCoords, _delay, _size)") + return cc.AnimationFrameData(cc.T2F_Quad(cc.tex2F(0.0,0.0), cc.tex2F(0.0,0.0), cc.tex2F(0.0,0.0), cc.tex2F(0.0,0.0)), 0, cc.size(0,0)) +end +_G["ccAnimationFrameData"] = ccAnimationFrameDataDeprecated + + + +local function tex2(u,v) + deprecatedTip("tex2(u,v)","cc.tex2f(u,v)") + return cc.tex2f(u,v) +end +rawset(_G,"tex2",tex2) + +--functions of CCControl will be deprecated end +local CCControlDeprecated = { } +function CCControlDeprecated.addHandleOfControlEvent(self,func,controlEvent) + deprecatedTip("addHandleOfControlEvent","registerControlEventHandler") + print("come in addHandleOfControlEvent") + self:registerControlEventHandler(func,controlEvent) +end +rawset(CCControl,"addHandleOfControlEvent",CCControlDeprecated.addHandleOfControlEvent) +--functions of CCControl will be deprecated end + +--Enums of CCTableView will be deprecated begin +rawset(CCTableView, "kTableViewScroll",cc.SCROLLVIEW_SCRIPT_SCROLL) +rawset(CCTableView,"kTableViewZoom",cc.SCROLLVIEW_SCRIPT_ZOOM) +rawset(CCTableView,"kTableCellTouched",cc.TABLECELL_TOUCHED) +rawset(CCTableView,"kTableCellSizeForIndex",cc.TABLECELL_SIZE_FOR_INDEX) +rawset(CCTableView,"kTableCellSizeAtIndex",cc.TABLECELL_SIZE_AT_INDEX) +rawset(CCTableView,"kNumberOfCellsInTableView",cc.NUMBER_OF_CELLS_IN_TABLEVIEW) +--Enums of CCTableView will be deprecated end + +--Enums of CCScrollView will be deprecated begin +rawset(CCScrollView, "kScrollViewScroll",cc.SCROLLVIEW_SCRIPT_SCROLL) +rawset(CCScrollView,"kScrollViewZoom",cc.SCROLLVIEW_SCRIPT_ZOOM) +--Enums of CCScrollView will be deprecated end + + + +--functions of CCApplication will be deprecated end +local CCApplicationDeprecated = { } +function CCApplicationDeprecated.sharedApplication() + deprecatedTip("CCApplication:sharedApplication","CCApplication:getInstance") + return CCApplication:getInstance() +end +rawset(CCApplication,"sharedApplication",CCApplicationDeprecated.sharedApplication) +--functions of CCApplication will be deprecated end + + +--functions of CCDirector will be deprecated end +local CCDirectorDeprecated = { } +function CCDirectorDeprecated.sharedDirector() + deprecatedTip("CCDirector:sharedDirector","CCDirector:getInstance") + return CCDirector:getInstance() +end +rawset(CCDirector,"sharedDirector",CCDirectorDeprecated.sharedDirector) +--functions of CCDirector will be deprecated end + + +--functions of CCUserDefault will be deprecated end +local CCUserDefaultDeprecated = { } +function CCUserDefaultDeprecated.sharedUserDefault() + deprecatedTip("CCUserDefault:sharedUserDefault","CCUserDefault:getInstance") + return CCUserDefault:getInstance() +end +rawset(CCUserDefault,"sharedUserDefault",CCUserDefaultDeprecated.sharedUserDefault) + +function CCUserDefaultDeprecated.purgeSharedUserDefault() + deprecatedTip("CCUserDefault:purgeSharedUserDefault","CCUserDefault:destroyInstance") + return CCUserDefault:destroyInstance() +end +rawset(CCUserDefault,"purgeSharedUserDefault",CCUserDefaultDeprecated.purgeSharedUserDefault) +--functions of CCUserDefault will be deprecated end + +--functions of CCGrid3DAction will be deprecated begin +local CCGrid3DActionDeprecated = { } +function CCGrid3DActionDeprecated.vertex(self,pt) + deprecatedTip("vertex","CCGrid3DAction:getVertex") + return self:getVertex(pt) +end +rawset(CCGrid3DAction,"vertex",CCGrid3DActionDeprecated.vertex) + +function CCGrid3DActionDeprecated.originalVertex(self,pt) + deprecatedTip("originalVertex","CCGrid3DAction:getOriginalVertex") + return self:getOriginalVertex(pt) +end +rawset(CCGrid3DAction,"originalVertex",CCGrid3DActionDeprecated.originalVertex) +--functions of CCGrid3DAction will be deprecated end + + +--functions of CCTiledGrid3DAction will be deprecated begin +local CCTiledGrid3DActionDeprecated = { } +function CCTiledGrid3DActionDeprecated.tile(self,pt) + deprecatedTip("tile","CCTiledGrid3DAction:getTile") + return self:getTile(pt) +end +rawset(CCTiledGrid3DAction,"tile",CCTiledGrid3DActionDeprecated.tile) + +function CCTiledGrid3DActionDeprecated.originalTile(self,pt) + deprecatedTip("originalTile","CCTiledGrid3DAction:getOriginalTile") + return self:getOriginalTile(pt) +end +rawset(CCTiledGrid3DAction,"originalTile",CCTiledGrid3DActionDeprecated.originalTile) +--functions of CCTiledGrid3DAction will be deprecated end + + +--functions of CCTexture2D will be deprecated begin +local CCTexture2DDeprecated = { } +function CCTexture2DDeprecated.stringForFormat(self) + deprecatedTip("Texture2D:stringForFormat","Texture2D:getStringForFormat") + return self:getStringForFormat() +end +rawset(CCTexture2D,"stringForFormat",CCTexture2DDeprecated.stringForFormat) + +function CCTexture2DDeprecated.bitsPerPixelForFormat(self) + deprecatedTip("Texture2D:bitsPerPixelForFormat","Texture2D:getBitsPerPixelForFormat") + return self:getBitsPerPixelForFormat() +end +rawset(CCTexture2D,"bitsPerPixelForFormat",CCTexture2DDeprecated.bitsPerPixelForFormat) + +function CCTexture2DDeprecated.bitsPerPixelForFormat(self,pixelFormat) + deprecatedTip("Texture2D:bitsPerPixelForFormat","Texture2D:getBitsPerPixelForFormat") + return self:getBitsPerPixelForFormat(pixelFormat) +end +rawset(CCTexture2D,"bitsPerPixelForFormat",CCTexture2DDeprecated.bitsPerPixelForFormat) + +function CCTexture2DDeprecated.defaultAlphaPixelFormat(self) + deprecatedTip("Texture2D:defaultAlphaPixelFormat","Texture2D:getDefaultAlphaPixelFormat") + return self:getDefaultAlphaPixelFormat() +end +rawset(CCTexture2D,"defaultAlphaPixelFormat",CCTexture2DDeprecated.defaultAlphaPixelFormat) +--functions of CCTexture2D will be deprecated end + + +--functions of CCTimer will be deprecated begin +local CCTimerDeprecated = { } +function CCTimerDeprecated.timerWithScriptHandler(handler,seconds) + deprecatedTip("CCTimer:timerWithScriptHandler","CCTimer:createWithScriptHandler") + return CCTimer:createWithScriptHandler(handler,seconds) +end +rawset(CCTimer,"timerWithScriptHandler",CCTimerDeprecated.timerWithScriptHandler) + +function CCTimerDeprecated.numberOfRunningActionsInTarget(self,target) + deprecatedTip("CCActionManager:numberOfRunningActionsInTarget","CCActionManager:getNumberOfRunningActionsInTarget") + return self:getNumberOfRunningActionsInTarget(target) +end +rawset(CCTimer,"numberOfRunningActionsInTarget",CCTimerDeprecated.numberOfRunningActionsInTarget) +--functions of CCTimer will be deprecated end + + +--functions of CCMenuItemFont will be deprecated begin +local CCMenuItemFontDeprecated = { } +function CCMenuItemFontDeprecated.fontSize() + deprecatedTip("CCMenuItemFont:fontSize","CCMenuItemFont:getFontSize") + return CCMenuItemFont:getFontSize() +end +rawset(CCMenuItemFont,"fontSize",CCMenuItemFontDeprecated.fontSize) + +function CCMenuItemFontDeprecated.fontName() + deprecatedTip("CCMenuItemFont:fontName","CCMenuItemFont:getFontName") + return CCMenuItemFont:getFontName() +end +rawset(CCMenuItemFont,"fontName",CCMenuItemFontDeprecated.fontName) + +function CCMenuItemFontDeprecated.fontSizeObj(self) + deprecatedTip("CCMenuItemFont:fontSizeObj","CCMenuItemFont:getFontSizeObj") + return self:getFontSizeObj() +end +rawset(CCMenuItemFont,"fontSizeObj",CCMenuItemFontDeprecated.fontSizeObj) + +function CCMenuItemFontDeprecated.fontNameObj(self) + deprecatedTip("CCMenuItemFont:fontNameObj","CCMenuItemFont:getFontNameObj") + return self:getFontNameObj() +end +rawset(CCMenuItemFont,"fontNameObj",CCMenuItemFontDeprecated.fontNameObj) +--functions of CCMenuItemFont will be deprecated end + + +--functions of CCMenuItemToggle will be deprecated begin +local CCMenuItemToggleDeprecated = { } +function CCMenuItemToggleDeprecated.selectedItem(self) + deprecatedTip("CCMenuItemToggle:selectedItem","CCMenuItemToggle:getSelectedItem") + return self:getSelectedItem() +end +rawset(CCMenuItemToggle,"selectedItem",CCMenuItemToggleDeprecated.selectedItem) +--functions of CCMenuItemToggle will be deprecated end + + +--functions of CCTileMapAtlas will be deprecated begin +local CCTileMapAtlasDeprecated = { } +function CCTileMapAtlasDeprecated.tileAt(self,pos) + deprecatedTip("CCTileMapAtlas:tileAt","CCTileMapAtlas:getTileAt") + return self:getTileAt(pos) +end +rawset(CCTileMapAtlas,"tileAt",CCTileMapAtlasDeprecated.tileAt) +--functions of CCTileMapAtlas will be deprecated end + + +--functions of CCTMXLayer will be deprecated begin +local CCTMXLayerDeprecated = { } +function CCTMXLayerDeprecated.tileAt(self,tileCoordinate) + deprecatedTip("CCTMXLayer:tileAt","CCTMXLayer:getTileAt") + return self:getTileAt(tileCoordinate) +end +rawset(CCTMXLayer,"tileAt",CCTMXLayerDeprecated.tileAt) + +function CCTMXLayerDeprecated.tileGIDAt(self,tileCoordinate) + deprecatedTip("CCTMXLayer:tileGIDAt","CCTMXLayer:getTileGIDAt") + return self:getTileGIDAt(tileCoordinate) +end +rawset(CCTMXLayer,"tileGIDAt",CCTMXLayerDeprecated.tileGIDAt) + +function CCTMXLayerDeprecated.positionAt(self,tileCoordinate) + deprecatedTip("CCTMXLayer:positionAt","CCTMXLayer:getPositionAt") + return self:getPositionAt(tileCoordinate) +end +rawset(CCTMXLayer,"positionAt",CCTMXLayerDeprecated.positionAt) + +function CCTMXLayerDeprecated.propertyNamed(self,propertyName) + deprecatedTip("CCTMXLayer:propertyNamed","CCTMXLayer:getProperty") + return self:getProperty(propertyName) +end +rawset(CCTMXLayer,"propertyNamed",CCTMXLayerDeprecated.propertyNamed) +--functions of CCTMXLayer will be deprecated end + +--functions of CCTMXTiledMap will be deprecated begin +local CCTMXTiledMapDeprecated = { } +function CCTMXTiledMapDeprecated.layerNamed(self,layerName) + deprecatedTip("CCTMXTiledMap:layerNamed","CCTMXTiledMap:getLayer") + return self:getLayer(layerName) +end +rawset(CCTMXTiledMap,"layerNamed", CCTMXTiledMapDeprecated.layerNamed) + +function CCTMXTiledMapDeprecated.propertyNamed(self,propertyName) + deprecatedTip("CCTMXTiledMap:propertyNamed","CCTMXTiledMap:getProperty") + return self:getProperty(propertyName) +end +rawset(CCTMXTiledMap,"propertyNamed", CCTMXTiledMapDeprecated.propertyNamed ) + +function CCTMXTiledMapDeprecated.propertiesForGID(self,GID) + deprecatedTip("CCTMXTiledMap:propertiesForGID","CCTMXTiledMap:getPropertiesForGID") + return self:getPropertiesForGID(GID) +end +rawset(CCTMXTiledMap,"propertiesForGID", CCTMXTiledMapDeprecated.propertiesForGID) + +function CCTMXTiledMapDeprecated.objectGroupNamed(self,groupName) + deprecatedTip("CCTMXTiledMap:objectGroupNamed","CCTMXTiledMap:getObjectGroup") + return self:getObjectGroup(groupName) +end +rawset(CCTMXTiledMap,"objectGroupNamed", CCTMXTiledMapDeprecated.objectGroupNamed) +--functions of CCTMXTiledMap will be deprecated end + + +--functions of CCTMXMapInfo will be deprecated begin +local CCTMXMapInfoDeprecated = { } +function CCTMXMapInfoDeprecated.getStoringCharacters(self) + deprecatedTip("CCTMXMapInfo:getStoringCharacters","CCTMXMapInfo:isStoringCharacters") + return self:isStoringCharacters() +end +rawset(CCTMXMapInfo,"getStoringCharacters", CCTMXMapInfoDeprecated.getStoringCharacters) + +function CCTMXMapInfoDeprecated.formatWithTMXFile(infoTable,tmxFile) + deprecatedTip("CCTMXMapInfo:formatWithTMXFile","CCTMXMapInfo:create") + return CCTMXMapInfo:create(tmxFile) +end +rawset(CCTMXMapInfo,"formatWithTMXFile", CCTMXMapInfoDeprecated.formatWithTMXFile) + +function CCTMXMapInfoDeprecated.formatWithXML(infoTable,tmxString,resourcePath) + deprecatedTip("CCTMXMapInfo:formatWithXML","TMXMapInfo:createWithXML") + return CCTMXMapInfo:createWithXML(tmxString,resourcePath) +end +rawset(CCTMXMapInfo,"formatWithXML", CCTMXMapInfoDeprecated.formatWithXML) +--functions of CCTMXMapInfo will be deprecated end + + +--functions of CCTMXObject will be deprecated begin +local CCTMXObjectGroupDeprecated = { } +function CCTMXObjectGroupDeprecated.propertyNamed(self,propertyName) + deprecatedTip("CCTMXObjectGroup:propertyNamed","CCTMXObjectGroup:getProperty") + return self:getProperty(propertyName) +end +rawset(CCTMXObjectGroup,"propertyNamed", CCTMXObjectGroupDeprecated.propertyNamed) + +function CCTMXObjectGroupDeprecated.objectNamed(self, objectName) + deprecatedTip("CCTMXObjectGroup:objectNamed","CCTMXObjectGroup:getObject") + return self:getObject(objectName) +end +rawset(CCTMXObjectGroup,"objectNamed", CCTMXObjectGroupDeprecated.objectNamed) +--functions of CCTMXObject will be deprecated end + + +--functions of WebSocket will be deprecated begin +local targetPlatform = CCApplication:getInstance():getTargetPlatform() +if (kTargetIphone == targetPlatform) or (kTargetIpad == targetPlatform) or (kTargetAndroid == targetPlatform) or (kTargetWindows == targetPlatform) then + local WebSocketDeprecated = { } + function WebSocketDeprecated.sendTextMsg(self, string) + deprecatedTip("WebSocket:sendTextMsg","WebSocket:sendString") + return self:sendString(string) + end + rawset(WebSocket,"sendTextMsg", WebSocketDeprecated.sendTextMsg) + + function WebSocketDeprecated.sendBinaryMsg(self, table,tablesize) + deprecatedTip("WebSocket:sendBinaryMsg","WebSocket:sendString") + string.char(unpack(table)) + return self:sendString(string.char(unpack(table))) + end + rawset(WebSocket,"sendBinaryMsg", WebSocketDeprecated.sendBinaryMsg) +end +--functions of WebSocket will be deprecated end + +--functions of CCRenderTexture will be deprecated begin +local CCRenderTextureDeprecated = { } +function CCRenderTextureDeprecated.newCCImage(self) + deprecatedTip("CCRenderTexture:newCCImage","CCRenderTexture:newImage") + return self:newImage() +end +rawset(CCRenderTexture, "newCCImage", CCRenderTextureDeprecated.newCCImage) +--functions of CCRenderTexture will be deprecated end + +--functions of Sprite will be deprecated begin +local CCSpriteDeprecated = { } +function CCSpriteDeprecated.setFlipX(self,flag) + deprecatedTip("CCSpriteDeprecated:setFlipX","CCSpriteDeprecated:setFlippedX") + return self:setFlippedX(flag) +end +rawset(cc.Sprite, "setFlipX", CCSpriteDeprecated.setFlipX) + +function CCSpriteDeprecated.setFlipY(self,flag) + deprecatedTip("CCSpriteDeprecated:setFlipY","CCSpriteDeprecated:setFlippedY") + return self:setFlippedY(flag) +end +rawset(cc.Sprite, "setFlipY", CCSpriteDeprecated.setFlipY) +--functions of Sprite will be deprecated end + + +--functions of Layer will be deprecated begin +local CCLayerDeprecated = {} +function CCLayerDeprecated.setKeypadEnabled( self, enabled) + return self:setKeyboardEnabled(enabled) +end +rawset(cc.Layer, "setKeypadEnabled", CCLayerDeprecated.setKeypadEnabled ) + +function CCLayerDeprecated.isKeypadEnabled(self) + return self:isKeyboardEnabled() +end +rawset(cc.Layer, "isKeypadEnabled", CCLayerDeprecated.isKeypadEnabled ) +--functions of Layer will be deprecated end + +--functions of ccs.GUIReader will be deprecated begin +local CCSGUIReaderDeprecated = { } +function CCSGUIReaderDeprecated.purgeGUIReader() + deprecatedTip("ccs.GUIReader:purgeGUIReader","ccs.GUIReader:destroyInstance") + return ccs.GUIReader:destroyInstance() +end +rawset(ccs.GUIReader,"purgeGUIReader",CCSGUIReaderDeprecated.purgeGUIReader) +--functions of ccs.GUIReader will be deprecated end + +--functions of ccs.ActionManagerEx will be deprecated begin +local CCSActionManagerExDeprecated = { } +function CCSActionManagerExDeprecated.destroyActionManager() + deprecatedTip("ccs.ActionManagerEx:destroyActionManager","ccs.ActionManagerEx:destroyInstance") + return ccs.ActionManagerEx:destroyInstance() +end +rawset(ccs.ActionManagerEx,"destroyActionManager",CCSActionManagerExDeprecated.destroyActionManager) +--functions of ccs.ActionManagerEx will be deprecated end + +--functions of ccs.SceneReader will be deprecated begin +local CCSSceneReaderDeprecated = { } +function CCSSceneReaderDeprecated.destroySceneReader(self) + deprecatedTip("ccs.SceneReader:destroySceneReader","ccs.SceneReader:destroyInstance") + return self:destroyInstance() +end +rawset(ccs.SceneReader,"destroySceneReader",CCSSceneReaderDeprecated.destroySceneReader) +--functions of ccs.SceneReader will be deprecated end + +--functions of CCArmatureDataManager will be deprecated begin +local CCArmatureDataManagerDeprecated = { } +function CCArmatureDataManagerDeprecated.sharedArmatureDataManager() + deprecatedTip("CCArmatureDataManager:sharedArmatureDataManager","ccs.ArmatureDataManager:getInstance") + return ccs.ArmatureDataManager:getInstance() +end +rawset(CCArmatureDataManager,"sharedArmatureDataManager",CCArmatureDataManagerDeprecated.sharedArmatureDataManager) + +function CCArmatureDataManagerDeprecated.purge() + deprecatedTip("CCArmatureDataManager:purge","ccs.ArmatureDataManager:destoryInstance") + return ccs.ArmatureDataManager:destoryInstance() +end +rawset(CCArmatureDataManager,"purge",CCArmatureDataManagerDeprecated.purge) +--functions of CCArmatureDataManager will be deprecated end + +--functions of GUIReader will be deprecated begin +local GUIReaderDeprecated = { } +function GUIReaderDeprecated.shareReader() + deprecatedTip("GUIReader:shareReader","ccs.GUIReader:getInstance") + return ccs.GUIReader:getInstance() +end +rawset(GUIReader,"shareReader",GUIReaderDeprecated.shareReader) + +function GUIReaderDeprecated.purgeGUIReader() + deprecatedTip("GUIReader:purgeGUIReader","ccs.GUIReader:destroyInstance") + return ccs.GUIReader:destroyInstance() +end +rawset(GUIReader,"purgeGUIReader",GUIReaderDeprecated.purgeGUIReader) +--functions of GUIReader will be deprecated end + +--functions of SceneReader will be deprecated begin +local SceneReaderDeprecated = { } +function SceneReaderDeprecated.sharedSceneReader() + deprecatedTip("SceneReader:sharedSceneReader","ccs.SceneReader:getInstance") + return ccs.SceneReader:getInstance() +end +rawset(SceneReader,"sharedSceneReader",SceneReaderDeprecated.sharedSceneReader) + +function SceneReaderDeprecated.purgeSceneReader(self) + deprecatedTip("SceneReader:purgeSceneReader","ccs.SceneReader:destroyInstance") + return self:destroyInstance() +end +rawset(SceneReader,"purgeSceneReader",SceneReaderDeprecated.purgeSceneReader) +--functions of SceneReader will be deprecated end + +--functions of cc.Node will be deprecated begin +local NodeDeprecated = { } +function NodeDeprecated.setZOrder(self,zOrder) + deprecatedTip("cc.Node:setZOrder","cc.Node:setLocalZOrder") + return self:setLocalZOrder(zOrder) +end +rawset(cc.Node,"setZOrder",NodeDeprecated.setZOrder) + +function NodeDeprecated.getZOrder(self) + deprecatedTip("cc.Node:getZOrder","cc.Node:getLocalZOrder") + return self:getLocalZOrder() +end +rawset(cc.Node,"getZOrder",NodeDeprecated.getZOrder) + +function NodeDeprecated.setVertexZ(self,vertexZ) + deprecatedTip("cc.Node:setVertexZ", "cc.Node:setPositionZ") + return self:setPositionZ(vertexZ) +end +rawset(cc.Node,"setVertexZ",NodeDeprecated.setVertexZ) + +function NodeDeprecated.getVertexZ(self) + deprecatedTip("cc.Node:getVertexZ", "cc.Node:getPositionZ") + return self:getPositionZ() +end +rawset(cc.Node, "getVertexZ", NodeDeprecated.getVertexZ) +--functions of cc.Node will be deprecated end + +--functions of cc.GLProgram will be deprecated begin +local GLProgram = { } +function GLProgram.initWithVertexShaderByteArray(self,vShaderByteArray, fShaderByteArray) + deprecatedTip("cc.GLProgram:initWithVertexShaderByteArray","cc.GLProgram:initWithByteArrays") + return self:initWithByteArrays(vShaderByteArray, fShaderByteArray) +end +rawset(cc.GLProgram,"initWithVertexShaderByteArray", GLProgram.initWithVertexShaderByteArray) + +function GLProgram.initWithVertexShaderFilename(self,vShaderByteArray, fShaderByteArray) + deprecatedTip("cc.GLProgram:initWithVertexShaderFilename","cc.GLProgram:initWithFilenames") + return self:initWithFilenames(vShaderByteArray, fShaderByteArray) +end +rawset(cc.GLProgram,"initWithVertexShaderFilename", GLProgram.initWithVertexShaderFilename) + +function GLProgram.addAttribute(self, attributeName, index) + deprecatedTip("cc.GLProgram:addAttribute","cc.GLProgram:bindAttribLocation") + return self:bindAttribLocation(attributeName, index) +end +rawset(cc.GLProgram,"addAttribute", GLProgram.addAttribute) +--functions of cc.GLProgram will be deprecated end + + diff --git a/cocos/scripting/lua-bindings/script/DeprecatedClass.lua b/cocos/scripting/lua-bindings/script/DeprecatedClass.lua new file mode 100644 index 0000000000..462c9ac137 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/DeprecatedClass.lua @@ -0,0 +1,2190 @@ +-- This is the DeprecatedClass + +DeprecatedClass = {} or DeprecatedClass + +--tip +local function deprecatedTip(old_name,new_name) + print("\n********** \n"..old_name.." was deprecated please use ".. new_name .. " instead.\n**********") +end + +--CCProgressTo class will be Deprecated,begin +function DeprecatedClass.CCProgressTo() + deprecatedTip("CCProgressTo","cc.ProgressTo") + return cc.ProgressTo +end +_G["CCProgressTo"] = DeprecatedClass.CCProgressTo() +--CCProgressTo class will be Deprecated,end + +--CCHide class will be Deprecated,begin +function DeprecatedClass.CCHide() + deprecatedTip("CCHide","cc.Hide") + return cc.Hide +end +_G["CCHide"] = DeprecatedClass.CCHide() +--CCHide class will be Deprecated,end + +--CCTransitionMoveInB class will be Deprecated,begin +function DeprecatedClass.CCTransitionMoveInB() + deprecatedTip("CCTransitionMoveInB","cc.TransitionMoveInB") + return cc.TransitionMoveInB +end +_G["CCTransitionMoveInB"] = DeprecatedClass.CCTransitionMoveInB() +--CCTransitionMoveInB class will be Deprecated,end + +--CCEaseSineIn class will be Deprecated,begin +function DeprecatedClass.CCEaseSineIn() + deprecatedTip("CCEaseSineIn","cc.EaseSineIn") + return cc.EaseSineIn +end +_G["CCEaseSineIn"] = DeprecatedClass.CCEaseSineIn() +--CCEaseSineIn class will be Deprecated,end + +--CCTransitionMoveInL class will be Deprecated,begin +function DeprecatedClass.CCTransitionMoveInL() + deprecatedTip("CCTransitionMoveInL","cc.TransitionMoveInL") + return cc.TransitionMoveInL +end +_G["CCTransitionMoveInL"] = DeprecatedClass.CCTransitionMoveInL() +--CCTransitionMoveInL class will be Deprecated,end + +--CCEaseInOut class will be Deprecated,begin +function DeprecatedClass.CCEaseInOut() + deprecatedTip("CCEaseInOut","cc.EaseInOut") + return cc.EaseInOut +end +_G["CCEaseInOut"] = DeprecatedClass.CCEaseInOut() +--CCEaseInOut class will be Deprecated,end + +--SimpleAudioEngine class will be Deprecated,begin +function DeprecatedClass.SimpleAudioEngine() + deprecatedTip("SimpleAudioEngine","cc.SimpleAudioEngine") + return cc.SimpleAudioEngine +end +_G["SimpleAudioEngine"] = DeprecatedClass.SimpleAudioEngine() +--SimpleAudioEngine class will be Deprecated,end + +--CCTransitionMoveInT class will be Deprecated,begin +function DeprecatedClass.CCTransitionMoveInT() + deprecatedTip("CCTransitionMoveInT","cc.TransitionMoveInT") + return cc.TransitionMoveInT +end +_G["CCTransitionMoveInT"] = DeprecatedClass.CCTransitionMoveInT() +--CCTransitionMoveInT class will be Deprecated,end + +--CCTransitionMoveInR class will be Deprecated,begin +function DeprecatedClass.CCTransitionMoveInR() + deprecatedTip("CCTransitionMoveInR","cc.TransitionMoveInR") + return cc.TransitionMoveInR +end +_G["CCTransitionMoveInR"] = DeprecatedClass.CCTransitionMoveInR() +--CCTransitionMoveInR class will be Deprecated,end + +--CCControlHuePicker class will be Deprecated,begin +function DeprecatedClass.CCControlHuePicker() + deprecatedTip("CCControlHuePicker","cc.ControlHuePicker") + return cc.ControlHuePicker +end +_G["CCControlHuePicker"] = DeprecatedClass.CCControlHuePicker() +--CCControlHuePicker class will be Deprecated,end + +--CCParticleSnow class will be Deprecated,begin +function DeprecatedClass.CCParticleSnow() + deprecatedTip("CCParticleSnow","cc.ParticleSnow") + return cc.ParticleSnow +end +_G["CCParticleSnow"] = DeprecatedClass.CCParticleSnow() +--CCParticleSnow class will be Deprecated,end + +--CCActionCamera class will be Deprecated,begin +function DeprecatedClass.CCActionCamera() + deprecatedTip("CCActionCamera","cc.ActionCamera") + return cc.ActionCamera +end +_G["CCActionCamera"] = DeprecatedClass.CCActionCamera() +--CCActionCamera class will be Deprecated,end + +--CCProgressFromTo class will be Deprecated,begin +function DeprecatedClass.CCProgressFromTo() + deprecatedTip("CCProgressFromTo","cc.ProgressFromTo") + return cc.ProgressFromTo +end +_G["CCProgressFromTo"] = DeprecatedClass.CCProgressFromTo() +--CCProgressFromTo class will be Deprecated,end + +--CCMoveTo class will be Deprecated,begin +function DeprecatedClass.CCMoveTo() + deprecatedTip("CCMoveTo","cc.MoveTo") + return cc.MoveTo +end +_G["CCMoveTo"] = DeprecatedClass.CCMoveTo() +--CCMoveTo class will be Deprecated,end + +--CCJumpBy class will be Deprecated,begin +function DeprecatedClass.CCJumpBy() + deprecatedTip("CCJumpBy","cc.JumpBy") + return cc.JumpBy +end +_G["CCJumpBy"] = DeprecatedClass.CCJumpBy() +--CCJumpBy class will be Deprecated,end + +--CCObject class will be Deprecated,begin +function DeprecatedClass.CCObject() + deprecatedTip("CCObject","cc.Object") + return cc.Object +end +_G["CCObject"] = DeprecatedClass.CCObject() +--CCObject class will be Deprecated,end + +--CCTransitionRotoZoom class will be Deprecated,begin +function DeprecatedClass.CCTransitionRotoZoom() + deprecatedTip("CCTransitionRotoZoom","cc.TransitionRotoZoom") + return cc.TransitionRotoZoom +end +_G["CCTransitionRotoZoom"] = DeprecatedClass.CCTransitionRotoZoom() +--CCTransitionRotoZoom class will be Deprecated,end + +--CCControlColourPicker class will be Deprecated,begin +function DeprecatedClass.CCControlColourPicker() + deprecatedTip("CCControlColourPicker","cc.ControlColourPicker") + return cc.ControlColourPicker +end +_G["CCControlColourPicker"] = DeprecatedClass.CCControlColourPicker() +--CCControlColourPicker class will be Deprecated,end + +--CCDirector class will be Deprecated,begin +function DeprecatedClass.CCDirector() + deprecatedTip("CCDirector","cc.Director") + return cc.Director +end +_G["CCDirector"] = DeprecatedClass.CCDirector() +--CCDirector class will be Deprecated,end + +--CCScheduler class will be Deprecated,begin +function DeprecatedClass.CCScheduler() + deprecatedTip("CCScheduler","cc.Scheduler") + return cc.Scheduler +end +_G["CCScheduler"] = DeprecatedClass.CCScheduler() +--CCScheduler class will be Deprecated,end + +--CCEaseElasticOut class will be Deprecated,begin +function DeprecatedClass.CCEaseElasticOut() + deprecatedTip("CCEaseElasticOut","cc.EaseElasticOut") + return cc.EaseElasticOut +end +_G["CCEaseElasticOut"] = DeprecatedClass.CCEaseElasticOut() +--CCEaseElasticOut class will be Deprecated,end + +--CCTableViewCell class will be Deprecated,begin +function DeprecatedClass.CCTableViewCell() + deprecatedTip("CCTableViewCell","cc.TableViewCell") + return cc.TableViewCell +end +_G["CCTableViewCell"] = DeprecatedClass.CCTableViewCell() +--CCTableViewCell class will be Deprecated,end + + +--CCEaseBackOut class will be Deprecated,begin +function DeprecatedClass.CCEaseBackOut() + deprecatedTip("CCEaseBackOut","cc.EaseBackOut") + return cc.EaseBackOut +end +_G["CCEaseBackOut"] = DeprecatedClass.CCEaseBackOut() +--CCEaseBackOut class will be Deprecated,end + +--CCParticleSystemQuad class will be Deprecated,begin +function DeprecatedClass.CCParticleSystemQuad() + deprecatedTip("CCParticleSystemQuad","cc.ParticleSystemQuad") + return cc.ParticleSystemQuad +end +_G["CCParticleSystemQuad"] = DeprecatedClass.CCParticleSystemQuad() +--CCParticleSystemQuad class will be Deprecated,end + +--CCMenuItemToggle class will be Deprecated,begin +function DeprecatedClass.CCMenuItemToggle() + deprecatedTip("CCMenuItemToggle","cc.MenuItemToggle") + return cc.MenuItemToggle +end +_G["CCMenuItemToggle"] = DeprecatedClass.CCMenuItemToggle() +--CCMenuItemToggle class will be Deprecated,end + +--CCStopGrid class will be Deprecated,begin +function DeprecatedClass.CCStopGrid() + deprecatedTip("CCStopGrid","cc.StopGrid") + return cc.StopGrid +end +_G["CCStopGrid"] = DeprecatedClass.CCStopGrid() +--CCStopGrid class will be Deprecated,end + +--CCTransitionScene class will be Deprecated,begin +function DeprecatedClass.CCTransitionScene() + deprecatedTip("CCTransitionScene","cc.TransitionScene") + return cc.TransitionScene +end +_G["CCTransitionScene"] = DeprecatedClass.CCTransitionScene() +--CCTransitionScene class will be Deprecated,end + +--CCSkewBy class will be Deprecated,begin +function DeprecatedClass.CCSkewBy() + deprecatedTip("CCSkewBy","cc.SkewBy") + return cc.SkewBy +end +_G["CCSkewBy"] = DeprecatedClass.CCSkewBy() +--CCSkewBy class will be Deprecated,end + +--CCLayer class will be Deprecated,begin +function DeprecatedClass.CCLayer() + deprecatedTip("CCLayer","cc.Layer") + return cc.Layer +end +_G["CCLayer"] = DeprecatedClass.CCLayer() +--CCLayer class will be Deprecated,end + +--CCEaseElastic class will be Deprecated,begin +function DeprecatedClass.CCEaseElastic() + deprecatedTip("CCEaseElastic","cc.EaseElastic") + return cc.EaseElastic +end +_G["CCEaseElastic"] = DeprecatedClass.CCEaseElastic() +--CCEaseElastic class will be Deprecated,end + +--CCTMXTiledMap class will be Deprecated,begin +function DeprecatedClass.CCTMXTiledMap() + deprecatedTip("CCTMXTiledMap","cc.TMXTiledMap") + return cc.TMXTiledMap +end +_G["CCTMXTiledMap"] = DeprecatedClass.CCTMXTiledMap() +--CCTMXTiledMap class will be Deprecated,end + +--CCGrid3DAction class will be Deprecated,begin +function DeprecatedClass.CCGrid3DAction() + deprecatedTip("CCGrid3DAction","cc.Grid3DAction") + return cc.Grid3DAction +end +_G["CCGrid3DAction"] = DeprecatedClass.CCGrid3DAction() +--CCGrid3DAction class will be Deprecated,end + +--CCFadeIn class will be Deprecated,begin +function DeprecatedClass.CCFadeIn() + deprecatedTip("CCFadeIn","cc.FadeIn") + return cc.FadeIn +end +_G["CCFadeIn"] = DeprecatedClass.CCFadeIn() +--CCFadeIn class will be Deprecated,end + +--CCNodeRGBA class will be Deprecated,begin +function DeprecatedClass.CCNodeRGBA() + deprecatedTip("CCNodeRGBA","cc.Node") + return cc.Node +end +_G["CCNodeRGBA"] = DeprecatedClass.CCNodeRGBA() +--CCNodeRGBA class will be Deprecated,end + +--NodeRGBA class will be Deprecated,begin +function DeprecatedClass.NodeRGBA() + deprecatedTip("cc.NodeRGBA","cc.Node") + return cc.Node +end +_G["cc"]["NodeRGBA"] = DeprecatedClass.NodeRGBA() +--NodeRGBA class will be Deprecated,end + +--CCAnimationCache class will be Deprecated,begin +function DeprecatedClass.CCAnimationCache() + deprecatedTip("CCAnimationCache","cc.AnimationCache") + return cc.AnimationCache +end +_G["CCAnimationCache"] = DeprecatedClass.CCAnimationCache() +--CCAnimationCache class will be Deprecated,end + +--CCFlipY3D class will be Deprecated,begin +function DeprecatedClass.CCFlipY3D() + deprecatedTip("CCFlipY3D","cc.FlipY3D") + return cc.FlipY3D +end +_G["CCFlipY3D"] = DeprecatedClass.CCFlipY3D() +--CCFlipY3D class will be Deprecated,end + +--CCEaseSineInOut class will be Deprecated,begin +function DeprecatedClass.CCEaseSineInOut() + deprecatedTip("CCEaseSineInOut","cc.EaseSineInOut") + return cc.EaseSineInOut +end +_G["CCEaseSineInOut"] = DeprecatedClass.CCEaseSineInOut() +--CCEaseSineInOut class will be Deprecated,end + +--CCTransitionFlipAngular class will be Deprecated,begin +function DeprecatedClass.CCTransitionFlipAngular() + deprecatedTip("CCTransitionFlipAngular","cc.TransitionFlipAngular") + return cc.TransitionFlipAngular +end +_G["CCTransitionFlipAngular"] = DeprecatedClass.CCTransitionFlipAngular() +--CCTransitionFlipAngular class will be Deprecated,end + +--CCControl class will be Deprecated,begin +function DeprecatedClass.CCControl() + deprecatedTip("CCControl","cc.Control") + return cc.Control +end +_G["CCControl"] = DeprecatedClass.CCControl() +--CCControl class will be Deprecated,end + +--CCEaseElasticInOut class will be Deprecated,begin +function DeprecatedClass.CCEaseElasticInOut() + deprecatedTip("CCEaseElasticInOut","cc.EaseElasticInOut") + return cc.EaseElasticInOut +end +_G["CCEaseElasticInOut"] = DeprecatedClass.CCEaseElasticInOut() +--CCEaseElasticInOut class will be Deprecated,end + +--CCEaseBounce class will be Deprecated,begin +function DeprecatedClass.CCEaseBounce() + deprecatedTip("CCEaseBounce","cc.EaseBounce") + return cc.EaseBounce +end +_G["CCEaseBounce"] = DeprecatedClass.CCEaseBounce() +--CCEaseBounce class will be Deprecated,end + +--CCShow class will be Deprecated,begin +function DeprecatedClass.CCShow() + deprecatedTip("CCShow","cc.Show") + return cc.Show +end +_G["CCShow"] = DeprecatedClass.CCShow() +--CCShow class will be Deprecated,end + +--CCEditBox class will be Deprecated,begin +function DeprecatedClass.CCEditBox() + deprecatedTip("CCEditBox","cc.EditBox") + return cc.EditBox +end +_G["CCEditBox"] = DeprecatedClass.CCEditBox() +--CCEditBox class will be Deprecated,end + +--CCFadeOut class will be Deprecated,begin +function DeprecatedClass.CCFadeOut() + deprecatedTip("CCFadeOut","cc.FadeOut") + return cc.FadeOut +end +_G["CCFadeOut"] = DeprecatedClass.CCFadeOut() +--CCFadeOut class will be Deprecated,end + +--CCCallFunc class will be Deprecated,begin +function DeprecatedClass.CCCallFunc() + deprecatedTip("CCCallFunc","cc.CallFunc") + return cc.CallFunc +end +_G["CCCallFunc"] = DeprecatedClass.CCCallFunc() +--CCCallFunc class will be Deprecated,end + +--CCWaves3D class will be Deprecated,begin +function DeprecatedClass.CCWaves3D() + deprecatedTip("CCWaves3D","cc.Waves3D") + return cc.Waves3D +end +_G["CCWaves3D"] = DeprecatedClass.CCWaves3D() +--CCWaves3D class will be Deprecated,end + +--CCFlipX3D class will be Deprecated,begin +function DeprecatedClass.CCFlipX3D() + deprecatedTip("CCFlipX3D","cc.FlipX3D") + return cc.FlipX3D +end +_G["CCFlipX3D"] = DeprecatedClass.CCFlipX3D() +--CCFlipX3D class will be Deprecated,end + +--CCParticleFireworks class will be Deprecated,begin +function DeprecatedClass.CCParticleFireworks() + deprecatedTip("CCParticleFireworks","cc.ParticleFireworks") + return cc.ParticleFireworks +end +_G["CCParticleFireworks"] = DeprecatedClass.CCParticleFireworks() +--CCParticleFireworks class will be Deprecated,end + +--CCMenuItemImage class will be Deprecated,begin +function DeprecatedClass.CCMenuItemImage() + deprecatedTip("CCMenuItemImage","cc.MenuItemImage") + return cc.MenuItemImage +end +_G["CCMenuItemImage"] = DeprecatedClass.CCMenuItemImage() +--CCMenuItemImage class will be Deprecated,end + +--CCParticleFire class will be Deprecated,begin +function DeprecatedClass.CCParticleFire() + deprecatedTip("CCParticleFire","cc.ParticleFire") + return cc.ParticleFire +end +_G["CCParticleFire"] = DeprecatedClass.CCParticleFire() +--CCParticleFire class will be Deprecated,end + +--CCMenuItem class will be Deprecated,begin +function DeprecatedClass.CCMenuItem() + deprecatedTip("CCMenuItem","cc.MenuItem") + return cc.MenuItem +end +_G["CCMenuItem"] = DeprecatedClass.CCMenuItem() +--CCMenuItem class will be Deprecated,end + +--CCActionEase class will be Deprecated,begin +function DeprecatedClass.CCActionEase() + deprecatedTip("CCActionEase","cc.ActionEase") + return cc.ActionEase +end +_G["CCActionEase"] = DeprecatedClass.CCActionEase() +--CCActionEase class will be Deprecated,end + +--CCTransitionSceneOriented class will be Deprecated,begin +function DeprecatedClass.CCTransitionSceneOriented() + deprecatedTip("CCTransitionSceneOriented","cc.TransitionSceneOriented") + return cc.TransitionSceneOriented +end +_G["CCTransitionSceneOriented"] = DeprecatedClass.CCTransitionSceneOriented() +--CCTransitionSceneOriented class will be Deprecated,end + +--CCTransitionZoomFlipAngular class will be Deprecated,begin +function DeprecatedClass.CCTransitionZoomFlipAngular() + deprecatedTip("CCTransitionZoomFlipAngular","cc.TransitionZoomFlipAngular") + return cc.TransitionZoomFlipAngular +end +_G["CCTransitionZoomFlipAngular"] = DeprecatedClass.CCTransitionZoomFlipAngular() +--CCTransitionZoomFlipAngular class will be Deprecated,end + +--CCEaseIn class will be Deprecated,begin +function DeprecatedClass.CCEaseIn() + deprecatedTip("CCEaseIn","cc.EaseIn") + return cc.EaseIn +end +_G["CCEaseIn"] = DeprecatedClass.CCEaseIn() +--CCEaseIn class will be Deprecated,end + +--CCEaseExponentialInOut class will be Deprecated,begin +function DeprecatedClass.CCEaseExponentialInOut() + deprecatedTip("CCEaseExponentialInOut","cc.EaseExponentialInOut") + return cc.EaseExponentialInOut +end +_G["CCEaseExponentialInOut"] = DeprecatedClass.CCEaseExponentialInOut() +--CCEaseExponentialInOut class will be Deprecated,end + +--CCTransitionFlipX class will be Deprecated,begin +function DeprecatedClass.CCTransitionFlipX() + deprecatedTip("CCTransitionFlipX","cc.TransitionFlipX") + return cc.TransitionFlipX +end +_G["CCTransitionFlipX"] = DeprecatedClass.CCTransitionFlipX() +--CCTransitionFlipX class will be Deprecated,end + +--CCEaseExponentialOut class will be Deprecated,begin +function DeprecatedClass.CCEaseExponentialOut() + deprecatedTip("CCEaseExponentialOut","cc.EaseExponentialOut") + return cc.EaseExponentialOut +end +_G["CCEaseExponentialOut"] = DeprecatedClass.CCEaseExponentialOut() +--CCEaseExponentialOut class will be Deprecated,end + +--CCLabel class will be Deprecated,begin +function DeprecatedClass.CCLabel() + deprecatedTip("CCLabel","cc.Label") + return cc.Label +end +_G["CCLabel"] = DeprecatedClass.CCLabel() +--CCLabel class will be Deprecated,end + +--CCApplication class will be Deprecated,begin +function DeprecatedClass.CCApplication() + deprecatedTip("CCApplication","cc.Application") + return cc.Application +end +_G["CCApplication"] = DeprecatedClass.CCApplication() +--CCApplication class will be Deprecated,end + +--CCControlSlider class will be Deprecated,begin +function DeprecatedClass.CCControlSlider() + deprecatedTip("CCControlSlider","cc.ControlSlider") + return cc.ControlSlider +end +_G["CCControlSlider"] = DeprecatedClass.CCControlSlider() +--CCControlSlider class will be Deprecated,end + +--CCDelayTime class will be Deprecated,begin +function DeprecatedClass.CCDelayTime() + deprecatedTip("CCDelayTime","cc.DelayTime") + return cc.DelayTime +end +_G["CCDelayTime"] = DeprecatedClass.CCDelayTime() +--CCDelayTime class will be Deprecated,end + +--CCLabelAtlas class will be Deprecated,begin +function DeprecatedClass.CCLabelAtlas() + deprecatedTip("CCLabelAtlas","cc.LabelAtlas") + return cc.LabelAtlas +end +_G["CCLabelAtlas"] = DeprecatedClass.CCLabelAtlas() +--CCLabelAtlas class will be Deprecated,end + +--CCLabelBMFont class will be Deprecated,begin +function DeprecatedClass.CCLabelBMFont() + deprecatedTip("CCLabelBMFont","cc.LabelBMFont") + return cc.LabelBMFont +end +_G["CCLabelBMFont"] = DeprecatedClass.CCLabelBMFont() +--CCLabelBMFont class will be Deprecated,end + +--CCScale9Sprite class will be Deprecated,begin +function DeprecatedClass.CCScale9Sprite() + deprecatedTip("CCScale9Sprite","cc.Scale9Sprite") + return cc.Scale9Sprite +end +_G["CCScale9Sprite"] = DeprecatedClass.CCScale9Sprite() +--CCScale9Sprite class will be Deprecated,end + +--CCFadeOutTRTiles class will be Deprecated,begin +function DeprecatedClass.CCFadeOutTRTiles() + deprecatedTip("CCFadeOutTRTiles","cc.FadeOutTRTiles") + return cc.FadeOutTRTiles +end +_G["CCFadeOutTRTiles"] = DeprecatedClass.CCFadeOutTRTiles() +--CCFadeOutTRTiles class will be Deprecated,end + +--CCEaseElasticIn class will be Deprecated,begin +function DeprecatedClass.CCEaseElasticIn() + deprecatedTip("CCEaseElasticIn","cc.EaseElasticIn") + return cc.EaseElasticIn +end +_G["CCEaseElasticIn"] = DeprecatedClass.CCEaseElasticIn() +--CCEaseElasticIn class will be Deprecated,end + +--CCParticleSpiral class will be Deprecated,begin +function DeprecatedClass.CCParticleSpiral() + deprecatedTip("CCParticleSpiral","cc.ParticleSpiral") + return cc.ParticleSpiral +end +_G["CCParticleSpiral"] = DeprecatedClass.CCParticleSpiral() +--CCParticleSpiral class will be Deprecated,end + +--CCBReader class will be Deprecated,begin +function DeprecatedClass.CCBReader() + deprecatedTip("CCBReader","cc.BReader") + return cc.BReader +end +_G["CCBReader"] = DeprecatedClass.CCBReader() +--CCBReader class will be Deprecated,end + +--CCFiniteTimeAction class will be Deprecated,begin +function DeprecatedClass.CCFiniteTimeAction() + deprecatedTip("CCFiniteTimeAction","cc.FiniteTimeAction") + return cc.FiniteTimeAction +end +_G["CCFiniteTimeAction"] = DeprecatedClass.CCFiniteTimeAction() +--CCFiniteTimeAction class will be Deprecated,end + +--CCFadeOutDownTiles class will be Deprecated,begin +function DeprecatedClass.CCFadeOutDownTiles() + deprecatedTip("CCFadeOutDownTiles","cc.FadeOutDownTiles") + return cc.FadeOutDownTiles +end +_G["CCFadeOutDownTiles"] = DeprecatedClass.CCFadeOutDownTiles() +--CCFadeOutDownTiles class will be Deprecated,end + +--CCJumpTiles3D class will be Deprecated,begin +function DeprecatedClass.CCJumpTiles3D() + deprecatedTip("CCJumpTiles3D","cc.JumpTiles3D") + return cc.JumpTiles3D +end +_G["CCJumpTiles3D"] = DeprecatedClass.CCJumpTiles3D() +--CCJumpTiles3D class will be Deprecated,end + +--CCEaseBackIn class will be Deprecated,begin +function DeprecatedClass.CCEaseBackIn() + deprecatedTip("CCEaseBackIn","cc.EaseBackIn") + return cc.EaseBackIn +end +_G["CCEaseBackIn"] = DeprecatedClass.CCEaseBackIn() +--CCEaseBackIn class will be Deprecated,end + +--CCSpriteBatchNode class will be Deprecated,begin +function DeprecatedClass.CCSpriteBatchNode() + deprecatedTip("CCSpriteBatchNode","cc.SpriteBatchNode") + return cc.SpriteBatchNode +end +_G["CCSpriteBatchNode"] = DeprecatedClass.CCSpriteBatchNode() +--CCSpriteBatchNode class will be Deprecated,end + +--CCParticleSystem class will be Deprecated,begin +function DeprecatedClass.CCParticleSystem() + deprecatedTip("CCParticleSystem","cc.ParticleSystem") + return cc.ParticleSystem +end +_G["CCParticleSystem"] = DeprecatedClass.CCParticleSystem() +--CCParticleSystem class will be Deprecated,end + +--CCActionTween class will be Deprecated,begin +function DeprecatedClass.CCActionTween() + deprecatedTip("CCActionTween","cc.ActionTween") + return cc.ActionTween +end +_G["CCActionTween"] = DeprecatedClass.CCActionTween() +--CCActionTween class will be Deprecated,end + +--CCTransitionFadeDown class will be Deprecated,begin +function DeprecatedClass.CCTransitionFadeDown() + deprecatedTip("CCTransitionFadeDown","cc.TransitionFadeDown") + return cc.TransitionFadeDown +end +_G["CCTransitionFadeDown"] = DeprecatedClass.CCTransitionFadeDown() +--CCTransitionFadeDown class will be Deprecated,end + +--CCParticleSun class will be Deprecated,begin +function DeprecatedClass.CCParticleSun() + deprecatedTip("CCParticleSun","cc.ParticleSun") + return cc.ParticleSun +end +_G["CCParticleSun"] = DeprecatedClass.CCParticleSun() +--CCParticleSun class will be Deprecated,end + +--CCTransitionProgressHorizontal class will be Deprecated,begin +function DeprecatedClass.CCTransitionProgressHorizontal() + deprecatedTip("CCTransitionProgressHorizontal","cc.TransitionProgressHorizontal") + return cc.TransitionProgressHorizontal +end +_G["CCTransitionProgressHorizontal"] = DeprecatedClass.CCTransitionProgressHorizontal() +--CCTransitionProgressHorizontal class will be Deprecated,end + +--CCRipple3D class will be Deprecated,begin +function DeprecatedClass.CCRipple3D() + deprecatedTip("CCRipple3D","cc.Ripple3D") + return cc.Ripple3D +end +_G["CCRipple3D"] = DeprecatedClass.CCRipple3D() +--CCRipple3D class will be Deprecated,end + +--CCTMXLayer class will be Deprecated,begin +function DeprecatedClass.CCTMXLayer() + deprecatedTip("CCTMXLayer","cc.TMXLayer") + return cc.TMXLayer +end +_G["CCTMXLayer"] = DeprecatedClass.CCTMXLayer() +--CCTMXLayer class will be Deprecated,end + +--CCFlipX class will be Deprecated,begin +function DeprecatedClass.CCFlipX() + deprecatedTip("CCFlipX","cc.FlipX") + return cc.FlipX +end +_G["CCFlipX"] = DeprecatedClass.CCFlipX() +--CCFlipX class will be Deprecated,end + +--CCFlipY class will be Deprecated,begin +function DeprecatedClass.CCFlipY() + deprecatedTip("CCFlipY","cc.FlipY") + return cc.FlipY +end +_G["CCFlipY"] = DeprecatedClass.CCFlipY() +--CCFlipY class will be Deprecated,end + +--CCTransitionSplitCols class will be Deprecated,begin +function DeprecatedClass.CCTransitionSplitCols() + deprecatedTip("CCTransitionSplitCols","cc.TransitionSplitCols") + return cc.TransitionSplitCols +end +_G["CCTransitionSplitCols"] = DeprecatedClass.CCTransitionSplitCols() +--CCTransitionSplitCols class will be Deprecated,end + +--CCTimer class will be Deprecated,begin +function DeprecatedClass.CCTimer() + deprecatedTip("CCTimer","cc.Timer") + return cc.Timer +end +_G["CCTimer"] = DeprecatedClass.CCTimer() +--CCTimer class will be Deprecated,end + +--CCFadeTo class will be Deprecated,begin +function DeprecatedClass.CCFadeTo() + deprecatedTip("CCFadeTo","cc.FadeTo") + return cc.FadeTo +end +_G["CCFadeTo"] = DeprecatedClass.CCFadeTo() +--CCFadeTo class will be Deprecated,end + +--CCBAnimationManager class will be Deprecated,begin +function DeprecatedClass.CCBAnimationManager() + deprecatedTip("CCBAnimationManager","cc.BAnimationManager") + return cc.BAnimationManager +end +_G["CCBAnimationManager"] = DeprecatedClass.CCBAnimationManager() +--CCBAnimationManager class will be Deprecated,end + +--CCRepeatForever class will be Deprecated,begin +function DeprecatedClass.CCRepeatForever() + deprecatedTip("CCRepeatForever","cc.RepeatForever") + return cc.RepeatForever +end +_G["CCRepeatForever"] = DeprecatedClass.CCRepeatForever() +--CCRepeatForever class will be Deprecated,end + +--CCPlace class will be Deprecated,begin +function DeprecatedClass.CCPlace() + deprecatedTip("CCPlace","cc.Place") + return cc.Place +end +_G["CCPlace"] = DeprecatedClass.CCPlace() +--CCPlace class will be Deprecated,end + +--CCScrollView class will be Deprecated,begin +function DeprecatedClass.CCScrollView() + deprecatedTip("CCScrollView","cc.ScrollView") + return cc.ScrollView +end +_G["CCScrollView"] = DeprecatedClass.CCScrollView() +--CCScrollView class will be Deprecated,end + + +--CCGLProgram class will be Deprecated,begin +function DeprecatedClass.CCGLProgram() + deprecatedTip("CCGLProgram","cc.GLProgram") + return cc.GLProgram +end +_G["CCGLProgram"] = DeprecatedClass.CCGLProgram() +--CCGLProgram class will be Deprecated,end + +--CCEaseBounceOut class will be Deprecated,begin +function DeprecatedClass.CCEaseBounceOut() + deprecatedTip("CCEaseBounceOut","cc.EaseBounceOut") + return cc.EaseBounceOut +end +_G["CCEaseBounceOut"] = DeprecatedClass.CCEaseBounceOut() +--CCEaseBounceOut class will be Deprecated,end + +--CCCardinalSplineBy class will be Deprecated,begin +function DeprecatedClass.CCCardinalSplineBy() + deprecatedTip("CCCardinalSplineBy","cc.CardinalSplineBy") + return cc.CardinalSplineBy +end +_G["CCCardinalSplineBy"] = DeprecatedClass.CCCardinalSplineBy() +--CCCardinalSplineBy class will be Deprecated,end + +--CCSpriteFrameCache class will be Deprecated,begin +function DeprecatedClass.CCSpriteFrameCache() + deprecatedTip("CCSpriteFrameCache","cc.SpriteFrameCache") + return cc.SpriteFrameCache +end +_G["CCSpriteFrameCache"] = DeprecatedClass.CCSpriteFrameCache() +--CCSpriteFrameCache class will be Deprecated,end + +--CCTransitionShrinkGrow class will be Deprecated,begin +function DeprecatedClass.CCTransitionShrinkGrow() + deprecatedTip("CCTransitionShrinkGrow","cc.TransitionShrinkGrow") + return cc.TransitionShrinkGrow +end +_G["CCTransitionShrinkGrow"] = DeprecatedClass.CCTransitionShrinkGrow() +--CCTransitionShrinkGrow class will be Deprecated,end + +--CCSplitCols class will be Deprecated,begin +function DeprecatedClass.CCSplitCols() + deprecatedTip("CCSplitCols","cc.SplitCols") + return cc.SplitCols +end +_G["CCSplitCols"] = DeprecatedClass.CCSplitCols() +--CCSplitCols class will be Deprecated,end + +--CCClippingNode class will be Deprecated,begin +function DeprecatedClass.CCClippingNode() + deprecatedTip("CCClippingNode","cc.ClippingNode") + return cc.ClippingNode +end +_G["CCClippingNode"] = DeprecatedClass.CCClippingNode() +--CCClippingNode class will be Deprecated,end + +--CCEaseBounceInOut class will be Deprecated,begin +function DeprecatedClass.CCEaseBounceInOut() + deprecatedTip("CCEaseBounceInOut","cc.EaseBounceInOut") + return cc.EaseBounceInOut +end +_G["CCEaseBounceInOut"] = DeprecatedClass.CCEaseBounceInOut() +--CCEaseBounceInOut class will be Deprecated,end + +--CCLiquid class will be Deprecated,begin +function DeprecatedClass.CCLiquid() + deprecatedTip("CCLiquid","cc.Liquid") + return cc.Liquid +end +_G["CCLiquid"] = DeprecatedClass.CCLiquid() +--CCLiquid class will be Deprecated,end + +--CCParticleFlower class will be Deprecated,begin +function DeprecatedClass.CCParticleFlower() + deprecatedTip("CCParticleFlower","cc.ParticleFlower") + return cc.ParticleFlower +end +_G["CCParticleFlower"] = DeprecatedClass.CCParticleFlower() +--CCParticleFlower class will be Deprecated,end + +--CCTableView class will be Deprecated,begin +function DeprecatedClass.CCTableView() + deprecatedTip("CCTableView","cc.TableView") + return cc.TableView +end +_G["CCTableView"] = DeprecatedClass.CCTableView() +--CCTableView class will be Deprecated,end + + +--CCParticleSmoke class will be Deprecated,begin +function DeprecatedClass.CCParticleSmoke() + deprecatedTip("CCParticleSmoke","cc.ParticleSmoke") + return cc.ParticleSmoke +end +_G["CCParticleSmoke"] = DeprecatedClass.CCParticleSmoke() +--CCParticleSmoke class will be Deprecated,end + +--CCImage class will be Deprecated,begin +function DeprecatedClass.CCImage() + deprecatedTip("CCImage","cc.Image") + return cc.Image +end +_G["CCImage"] = DeprecatedClass.CCImage() +--CCImage class will be Deprecated,end + +--CCTurnOffTiles class will be Deprecated,begin +function DeprecatedClass.CCTurnOffTiles() + deprecatedTip("CCTurnOffTiles","cc.TurnOffTiles") + return cc.TurnOffTiles +end +_G["CCTurnOffTiles"] = DeprecatedClass.CCTurnOffTiles() +--CCTurnOffTiles class will be Deprecated,end + +--CCBlink class will be Deprecated,begin +function DeprecatedClass.CCBlink() + deprecatedTip("CCBlink","cc.Blink") + return cc.Blink +end +_G["CCBlink"] = DeprecatedClass.CCBlink() +--CCBlink class will be Deprecated,end + +--CCShaderCache class will be Deprecated,begin +function DeprecatedClass.CCShaderCache() + deprecatedTip("CCShaderCache","cc.ShaderCache") + return cc.ShaderCache +end +_G["CCShaderCache"] = DeprecatedClass.CCShaderCache() +--CCShaderCache class will be Deprecated,end + +--CCJumpTo class will be Deprecated,begin +function DeprecatedClass.CCJumpTo() + deprecatedTip("CCJumpTo","cc.JumpTo") + return cc.JumpTo +end +_G["CCJumpTo"] = DeprecatedClass.CCJumpTo() +--CCJumpTo class will be Deprecated,end + +--CCAtlasNode class will be Deprecated,begin +function DeprecatedClass.CCAtlasNode() + deprecatedTip("CCAtlasNode","cc.AtlasNode") + return cc.AtlasNode +end +_G["CCAtlasNode"] = DeprecatedClass.CCAtlasNode() +--CCAtlasNode class will be Deprecated,end + +--CCTransitionJumpZoom class will be Deprecated,begin +function DeprecatedClass.CCTransitionJumpZoom() + deprecatedTip("CCTransitionJumpZoom","cc.TransitionJumpZoom") + return cc.TransitionJumpZoom +end +_G["CCTransitionJumpZoom"] = DeprecatedClass.CCTransitionJumpZoom() +--CCTransitionJumpZoom class will be Deprecated,end + +--CCTransitionProgressVertical class will be Deprecated,begin +function DeprecatedClass.CCTransitionProgressVertical() + deprecatedTip("CCTransitionProgressVertical","cc.TransitionProgressVertical") + return cc.TransitionProgressVertical +end +_G["CCTransitionProgressVertical"] = DeprecatedClass.CCTransitionProgressVertical() +--CCTransitionProgressVertical class will be Deprecated,end + +--CCAnimationFrame class will be Deprecated,begin +function DeprecatedClass.CCAnimationFrame() + deprecatedTip("CCAnimationFrame","cc.AnimationFrame") + return cc.AnimationFrame +end +_G["CCAnimationFrame"] = DeprecatedClass.CCAnimationFrame() +--CCAnimationFrame class will be Deprecated,end + +--CCTintTo class will be Deprecated,begin +function DeprecatedClass.CCTintTo() + deprecatedTip("CCTintTo","cc.TintTo") + return cc.TintTo +end +_G["CCTintTo"] = DeprecatedClass.CCTintTo() +--CCTintTo class will be Deprecated,end + +--CCTiledGrid3DAction class will be Deprecated,begin +function DeprecatedClass.CCTiledGrid3DAction() + deprecatedTip("CCTiledGrid3DAction","cc.TiledGrid3DAction") + return cc.TiledGrid3DAction +end +_G["CCTiledGrid3DAction"] = DeprecatedClass.CCTiledGrid3DAction() +--CCTiledGrid3DAction class will be Deprecated,end + +--CCTMXTilesetInfo class will be Deprecated,begin +function DeprecatedClass.CCTMXTilesetInfo() + deprecatedTip("CCTMXTilesetInfo","cc.TMXTilesetInfo") + return cc.TMXTilesetInfo +end +_G["CCTMXTilesetInfo"] = DeprecatedClass.CCTMXTilesetInfo() +--CCTMXTilesetInfo class will be Deprecated,end + +--CCTMXObjectGroup class will be Deprecated,begin +function DeprecatedClass.CCTMXObjectGroup() + deprecatedTip("CCTMXObjectGroup","cc.TMXObjectGroup") + return cc.TMXObjectGroup +end +_G["CCTMXObjectGroup"] = DeprecatedClass.CCTMXObjectGroup() +--CCTMXObjectGroup class will be Deprecated,end + +--CCParticleGalaxy class will be Deprecated,begin +function DeprecatedClass.CCParticleGalaxy() + deprecatedTip("CCParticleGalaxy","cc.ParticleGalaxy") + return cc.ParticleGalaxy +end +_G["CCParticleGalaxy"] = DeprecatedClass.CCParticleGalaxy() +--CCParticleGalaxy class will be Deprecated,end + +--CCTwirl class will be Deprecated,begin +function DeprecatedClass.CCTwirl() + deprecatedTip("CCTwirl","cc.Twirl") + return cc.Twirl +end +_G["CCTwirl"] = DeprecatedClass.CCTwirl() +--CCTwirl class will be Deprecated,end + +--CCMenuItemLabel class will be Deprecated,begin +function DeprecatedClass.CCMenuItemLabel() + deprecatedTip("CCMenuItemLabel","cc.MenuItemLabel") + return cc.MenuItemLabel +end +_G["CCMenuItemLabel"] = DeprecatedClass.CCMenuItemLabel() +--CCMenuItemLabel class will be Deprecated,end + +--CCLayerColor class will be Deprecated,begin +function DeprecatedClass.CCLayerColor() + deprecatedTip("CCLayerColor","cc.LayerColor") + return cc.LayerColor +end +_G["CCLayerColor"] = DeprecatedClass.CCLayerColor() +--CCLayerColor class will be Deprecated,end + +--CCFadeOutBLTiles class will be Deprecated,begin +function DeprecatedClass.CCFadeOutBLTiles() + deprecatedTip("CCFadeOutBLTiles","cc.FadeOutBLTiles") + return cc.FadeOutBLTiles +end +_G["CCFadeOutBLTiles"] = DeprecatedClass.CCFadeOutBLTiles() +--CCFadeOutBLTiles class will be Deprecated,end + +--CCTransitionProgress class will be Deprecated,begin +function DeprecatedClass.CCTransitionProgress() + deprecatedTip("CCTransitionProgress","cc.TransitionProgress") + return cc.TransitionProgress +end +_G["CCTransitionProgress"] = DeprecatedClass.CCTransitionProgress() +--CCTransitionProgress class will be Deprecated,end + +--CCEaseRateAction class will be Deprecated,begin +function DeprecatedClass.CCEaseRateAction() + deprecatedTip("CCEaseRateAction","cc.EaseRateAction") + return cc.EaseRateAction +end +_G["CCEaseRateAction"] = DeprecatedClass.CCEaseRateAction() +--CCEaseRateAction class will be Deprecated,end + +--CCLayerGradient class will be Deprecated,begin +function DeprecatedClass.CCLayerGradient() + deprecatedTip("CCLayerGradient","cc.LayerGradient") + return cc.LayerGradient +end +_G["CCLayerGradient"] = DeprecatedClass.CCLayerGradient() +--CCLayerGradient class will be Deprecated,end + +--CCMenuItemSprite class will be Deprecated,begin +function DeprecatedClass.CCMenuItemSprite() + deprecatedTip("CCMenuItemSprite","cc.MenuItemSprite") + return cc.MenuItemSprite +end +_G["CCMenuItemSprite"] = DeprecatedClass.CCMenuItemSprite() +--CCMenuItemSprite class will be Deprecated,end + +--CCNode class will be Deprecated,begin +function DeprecatedClass.CCNode() + deprecatedTip("CCNode","cc.Node") + return cc.Node +end +_G["CCNode"] = DeprecatedClass.CCNode() +--CCNode class will be Deprecated,end + +--CCToggleVisibility class will be Deprecated,begin +function DeprecatedClass.CCToggleVisibility() + deprecatedTip("CCToggleVisibility","cc.ToggleVisibility") + return cc.ToggleVisibility +end +_G["CCToggleVisibility"] = DeprecatedClass.CCToggleVisibility() +--CCToggleVisibility class will be Deprecated,end + +--CCRepeat class will be Deprecated,begin +function DeprecatedClass.CCRepeat() + deprecatedTip("CCRepeat","cc.Repeat") + return cc.Repeat +end +_G["CCRepeat"] = DeprecatedClass.CCRepeat() +--CCRepeat class will be Deprecated,end + +--CCRenderTexture class will be Deprecated,begin +function DeprecatedClass.CCRenderTexture() + deprecatedTip("CCRenderTexture","cc.RenderTexture") + return cc.RenderTexture +end +_G["CCRenderTexture"] = DeprecatedClass.CCRenderTexture() +--CCRenderTexture class will be Deprecated,end + +--CCTransitionFlipY class will be Deprecated,begin +function DeprecatedClass.CCTransitionFlipY() + deprecatedTip("CCTransitionFlipY","cc.TransitionFlipY") + return cc.TransitionFlipY +end +_G["CCTransitionFlipY"] = DeprecatedClass.CCTransitionFlipY() +--CCTransitionFlipY class will be Deprecated,end + +--CCLayerMultiplex class will be Deprecated,begin +function DeprecatedClass.CCLayerMultiplex() + deprecatedTip("CCLayerMultiplex","cc.LayerMultiplex") + return cc.LayerMultiplex +end +_G["CCLayerMultiplex"] = DeprecatedClass.CCLayerMultiplex() +--CCLayerMultiplex class will be Deprecated,end + +--CCTMXLayerInfo class will be Deprecated,begin +function DeprecatedClass.CCTMXLayerInfo() + deprecatedTip("CCTMXLayerInfo","cc.TMXLayerInfo") + return cc.TMXLayerInfo +end +_G["CCTMXLayerInfo"] = DeprecatedClass.CCTMXLayerInfo() +--CCTMXLayerInfo class will be Deprecated,end + +--CCEaseBackInOut class will be Deprecated,begin +function DeprecatedClass.CCEaseBackInOut() + deprecatedTip("CCEaseBackInOut","cc.EaseBackInOut") + return cc.EaseBackInOut +end +_G["CCEaseBackInOut"] = DeprecatedClass.CCEaseBackInOut() +--CCEaseBackInOut class will be Deprecated,end + +--CCActionInstant class will be Deprecated,begin +function DeprecatedClass.CCActionInstant() + deprecatedTip("CCActionInstant","cc.ActionInstant") + return cc.ActionInstant +end +_G["CCActionInstant"] = DeprecatedClass.CCActionInstant() +--CCActionInstant class will be Deprecated,end + +--CCTargetedAction class will be Deprecated,begin +function DeprecatedClass.CCTargetedAction() + deprecatedTip("CCTargetedAction","cc.TargetedAction") + return cc.TargetedAction +end +_G["CCTargetedAction"] = DeprecatedClass.CCTargetedAction() +--CCTargetedAction class will be Deprecated,end + +--CCDrawNode class will be Deprecated,begin +function DeprecatedClass.CCDrawNode() + deprecatedTip("CCDrawNode","cc.DrawNode") + return cc.DrawNode +end +_G["CCDrawNode"] = DeprecatedClass.CCDrawNode() +--CCDrawNode class will be Deprecated,end + +--CCTransitionTurnOffTiles class will be Deprecated,begin +function DeprecatedClass.CCTransitionTurnOffTiles() + deprecatedTip("CCTransitionTurnOffTiles","cc.TransitionTurnOffTiles") + return cc.TransitionTurnOffTiles +end +_G["CCTransitionTurnOffTiles"] = DeprecatedClass.CCTransitionTurnOffTiles() +--CCTransitionTurnOffTiles class will be Deprecated,end + +--CCRotateTo class will be Deprecated,begin +function DeprecatedClass.CCRotateTo() + deprecatedTip("CCRotateTo","cc.RotateTo") + return cc.RotateTo +end +_G["CCRotateTo"] = DeprecatedClass.CCRotateTo() +--CCRotateTo class will be Deprecated,end + +--CCTransitionSplitRows class will be Deprecated,begin +function DeprecatedClass.CCTransitionSplitRows() + deprecatedTip("CCTransitionSplitRows","cc.TransitionSplitRows") + return cc.TransitionSplitRows +end +_G["CCTransitionSplitRows"] = DeprecatedClass.CCTransitionSplitRows() +--CCTransitionSplitRows class will be Deprecated,end + +--CCTransitionProgressRadialCCW class will be Deprecated,begin +function DeprecatedClass.CCTransitionProgressRadialCCW() + deprecatedTip("CCTransitionProgressRadialCCW","cc.TransitionProgressRadialCCW") + return cc.TransitionProgressRadialCCW +end +_G["CCTransitionProgressRadialCCW"] = DeprecatedClass.CCTransitionProgressRadialCCW() +--CCTransitionProgressRadialCCW class will be Deprecated,end + +--CCScaleTo class will be Deprecated,begin +function DeprecatedClass.CCScaleTo() + deprecatedTip("CCScaleTo","cc.ScaleTo") + return cc.ScaleTo +end +_G["CCScaleTo"] = DeprecatedClass.CCScaleTo() +--CCScaleTo class will be Deprecated,end + +--CCTransitionPageTurn class will be Deprecated,begin +function DeprecatedClass.CCTransitionPageTurn() + deprecatedTip("CCTransitionPageTurn","cc.TransitionPageTurn") + return cc.TransitionPageTurn +end +_G["CCTransitionPageTurn"] = DeprecatedClass.CCTransitionPageTurn() +--CCTransitionPageTurn class will be Deprecated,end + +--CCParticleExplosion class will be Deprecated,begin +function DeprecatedClass.CCParticleExplosion() + deprecatedTip("CCParticleExplosion","cc.ParticleExplosion") + return cc.ParticleExplosion +end +_G["CCParticleExplosion"] = DeprecatedClass.CCParticleExplosion() +--CCParticleExplosion class will be Deprecated,end + +--CCMenu class will be Deprecated,begin +function DeprecatedClass.CCMenu() + deprecatedTip("CCMenu","cc.Menu") + return cc.Menu +end +_G["CCMenu"] = DeprecatedClass.CCMenu() +--CCMenu class will be Deprecated,end + +--CCTexture2D class will be Deprecated,begin +function DeprecatedClass.CCTexture2D() + deprecatedTip("CCTexture2D","cc.Texture2D") + return cc.Texture2D +end +_G["CCTexture2D"] = DeprecatedClass.CCTexture2D() +--CCTexture2D class will be Deprecated,end + +--CCActionManager class will be Deprecated,begin +function DeprecatedClass.CCActionManager() + deprecatedTip("CCActionManager","cc.ActionManager") + return cc.ActionManager +end +_G["CCActionManager"] = DeprecatedClass.CCActionManager() +--CCActionManager class will be Deprecated,end + +--CCParticleBatchNode class will be Deprecated,begin +function DeprecatedClass.CCParticleBatchNode() + deprecatedTip("CCParticleBatchNode","cc.ParticleBatchNode") + return cc.ParticleBatchNode +end +_G["CCParticleBatchNode"] = DeprecatedClass.CCParticleBatchNode() +--CCParticleBatchNode class will be Deprecated,end + +--CCTransitionZoomFlipX class will be Deprecated,begin +function DeprecatedClass.CCTransitionZoomFlipX() + deprecatedTip("CCTransitionZoomFlipX","cc.TransitionZoomFlipX") + return cc.TransitionZoomFlipX +end +_G["CCTransitionZoomFlipX"] = DeprecatedClass.CCTransitionZoomFlipX() +--CCTransitionZoomFlipX class will be Deprecated,end + +--CCControlPotentiometer class will be Deprecated,begin +function DeprecatedClass.CCControlPotentiometer() + deprecatedTip("CCControlPotentiometer","cc.ControlPotentiometer") + return cc.ControlPotentiometer +end +_G["CCControlPotentiometer"] = DeprecatedClass.CCControlPotentiometer() +--CCControlPotentiometer class will be Deprecated,end + +--CCScaleBy class will be Deprecated,begin +function DeprecatedClass.CCScaleBy() + deprecatedTip("CCScaleBy","cc.ScaleBy") + return cc.ScaleBy +end +_G["CCScaleBy"] = DeprecatedClass.CCScaleBy() +--CCScaleBy class will be Deprecated,end + +--CCTileMapAtlas class will be Deprecated,begin +function DeprecatedClass.CCTileMapAtlas() + deprecatedTip("CCTileMapAtlas","cc.TileMapAtlas") + return cc.TileMapAtlas +end +_G["CCTileMapAtlas"] = DeprecatedClass.CCTileMapAtlas() +--CCTileMapAtlas class will be Deprecated,end + +--CCAction class will be Deprecated,begin +function DeprecatedClass.CCAction() + deprecatedTip("CCAction","cc.Action") + return cc.Action +end +_G["CCAction"] = DeprecatedClass.CCAction() +--CCAction class will be Deprecated,end + +--CCLens3D class will be Deprecated,begin +function DeprecatedClass.CCLens3D() + deprecatedTip("CCLens3D","cc.Lens3D") + return cc.Lens3D +end +_G["CCLens3D"] = DeprecatedClass.CCLens3D() +--CCLens3D class will be Deprecated,end + +--CCAnimation class will be Deprecated,begin +function DeprecatedClass.CCAnimation() + deprecatedTip("CCAnimation","cc.Animation") + return cc.Animation +end +_G["CCAnimation"] = DeprecatedClass.CCAnimation() +--CCAnimation class will be Deprecated,end + +--CCTransitionSlideInT class will be Deprecated,begin +function DeprecatedClass.CCTransitionSlideInT() + deprecatedTip("CCTransitionSlideInT","cc.TransitionSlideInT") + return cc.TransitionSlideInT +end +_G["CCTransitionSlideInT"] = DeprecatedClass.CCTransitionSlideInT() +--CCTransitionSlideInT class will be Deprecated,end + +--CCSpawn class will be Deprecated,begin +function DeprecatedClass.CCSpawn() + deprecatedTip("CCSpawn","cc.Spawn") + return cc.Spawn +end +_G["CCSpawn"] = DeprecatedClass.CCSpawn() +--CCSpawn class will be Deprecated,end + +--CCSet class will be Deprecated,begin +function DeprecatedClass.CCSet() + deprecatedTip("CCSet","cc.Set") + return cc.Set +end +_G["CCSet"] = DeprecatedClass.CCSet() +--CCSet class will be Deprecated,end + +--CCShakyTiles3D class will be Deprecated,begin +function DeprecatedClass.CCShakyTiles3D() + deprecatedTip("CCShakyTiles3D","cc.ShakyTiles3D") + return cc.ShakyTiles3D +end +_G["CCShakyTiles3D"] = DeprecatedClass.CCShakyTiles3D() +--CCShakyTiles3D class will be Deprecated,end + +--CCPageTurn3D class will be Deprecated,begin +function DeprecatedClass.CCPageTurn3D() + deprecatedTip("CCPageTurn3D","cc.PageTurn3D") + return cc.PageTurn3D +end +_G["CCPageTurn3D"] = DeprecatedClass.CCPageTurn3D() +--CCPageTurn3D class will be Deprecated,end + +--CCGrid3D class will be Deprecated,begin +function DeprecatedClass.CCGrid3D() + deprecatedTip("CCGrid3D","cc.Grid3D") + return cc.Grid3D +end +_G["CCGrid3D"] = DeprecatedClass.CCGrid3D() +--CCGrid3D class will be Deprecated,end + +--CCTransitionProgressInOut class will be Deprecated,begin +function DeprecatedClass.CCTransitionProgressInOut() + deprecatedTip("CCTransitionProgressInOut","cc.TransitionProgressInOut") + return cc.TransitionProgressInOut +end +_G["CCTransitionProgressInOut"] = DeprecatedClass.CCTransitionProgressInOut() +--CCTransitionProgressInOut class will be Deprecated,end + +--CCTransitionFadeBL class will be Deprecated,begin +function DeprecatedClass.CCTransitionFadeBL() + deprecatedTip("CCTransitionFadeBL","cc.TransitionFadeBL") + return cc.TransitionFadeBL +end +_G["CCTransitionFadeBL"] = DeprecatedClass.CCTransitionFadeBL() +--CCTransitionFadeBL class will be Deprecated,end + +--CCCamera class will be Deprecated,begin +function DeprecatedClass.CCCamera() + deprecatedTip("CCCamera","cc.Camera") + return cc.Camera +end +_G["CCCamera"] = DeprecatedClass.CCCamera() +--CCCamera class will be Deprecated,end + +--CCLayerRGBA class will be Deprecated,begin +function DeprecatedClass.CCLayerRGBA() + deprecatedTip("CCLayerRGBA","cc.Layer") + return cc.Layer +end +_G["CCLayerRGBA"] = DeprecatedClass.CCLayerRGBA() +--CCLayerRGBA class will be Deprecated,end + +--LayerRGBA class will be Deprecated,begin +function DeprecatedClass.LayerRGBA() + deprecatedTip("cc.LayerRGBA","cc.Layer") + return cc.Layer +end +_G["cc"]["LayerRGBA"] = DeprecatedClass.LayerRGBA() +--LayerRGBA class will be Deprecated,end + +--CCBezierTo class will be Deprecated,begin +function DeprecatedClass.CCBezierTo() + deprecatedTip("CCBezierTo","cc.BezierTo") + return cc.BezierTo +end +_G["CCBezierTo"] = DeprecatedClass.CCBezierTo() +--CCBezierTo class will be Deprecated,end + +--CCControlButton class will be Deprecated,begin +function DeprecatedClass.CCControlButton() + deprecatedTip("CCControlButton","cc.ControlButton") + return cc.ControlButton +end +_G["CCControlButton"] = DeprecatedClass.CCControlButton() +--CCControlButton class will be Deprecated,end + +--CCFollow class will be Deprecated,begin +function DeprecatedClass.CCFollow() + deprecatedTip("CCFollow","cc.Follow") + return cc.Follow +end +_G["CCFollow"] = DeprecatedClass.CCFollow() +--CCFollow class will be Deprecated,end + +--CCTintBy class will be Deprecated,begin +function DeprecatedClass.CCTintBy() + deprecatedTip("CCTintBy","cc.TintBy") + return cc.TintBy +end +_G["CCTintBy"] = DeprecatedClass.CCTintBy() +--CCTintBy class will be Deprecated,end + +--CCActionInterval class will be Deprecated,begin +function DeprecatedClass.CCActionInterval() + deprecatedTip("CCActionInterval","cc.ActionInterval") + return cc.ActionInterval +end +_G["CCActionInterval"] = DeprecatedClass.CCActionInterval() +--CCActionInterval class will be Deprecated,end + +--CCAnimate class will be Deprecated,begin +function DeprecatedClass.CCAnimate() + deprecatedTip("CCAnimate","cc.Animate") + return cc.Animate +end +_G["CCAnimate"] = DeprecatedClass.CCAnimate() +--CCAnimate class will be Deprecated,end + +--CCProgressTimer class will be Deprecated,begin +function DeprecatedClass.CCProgressTimer() + deprecatedTip("CCProgressTimer","cc.ProgressTimer") + return cc.ProgressTimer +end +_G["CCProgressTimer"] = DeprecatedClass.CCProgressTimer() +--CCProgressTimer class will be Deprecated,end + +--CCParticleMeteor class will be Deprecated,begin +function DeprecatedClass.CCParticleMeteor() + deprecatedTip("CCParticleMeteor","cc.ParticleMeteor") + return cc.ParticleMeteor +end +_G["CCParticleMeteor"] = DeprecatedClass.CCParticleMeteor() +--CCParticleMeteor class will be Deprecated,end + +--CCTransitionFadeTR class will be Deprecated,begin +function DeprecatedClass.CCTransitionFadeTR() + deprecatedTip("CCTransitionFadeTR","cc.TransitionFadeTR") + return cc.TransitionFadeTR +end +_G["CCTransitionFadeTR"] = DeprecatedClass.CCTransitionFadeTR() +--CCTransitionFadeTR class will be Deprecated,end + +--CCCatmullRomTo class will be Deprecated,begin +function DeprecatedClass.CCCatmullRomTo() + deprecatedTip("CCCatmullRomTo","cc.CatmullRomTo") + return cc.CatmullRomTo +end +_G["CCCatmullRomTo"] = DeprecatedClass.CCCatmullRomTo() +--CCCatmullRomTo class will be Deprecated,end + +--CCTransitionZoomFlipY class will be Deprecated,begin +function DeprecatedClass.CCTransitionZoomFlipY() + deprecatedTip("CCTransitionZoomFlipY","cc.TransitionZoomFlipY") + return cc.TransitionZoomFlipY +end +_G["CCTransitionZoomFlipY"] = DeprecatedClass.CCTransitionZoomFlipY() +--CCTransitionZoomFlipY class will be Deprecated,end + +--CCTransitionCrossFade class will be Deprecated,begin +function DeprecatedClass.CCTransitionCrossFade() + deprecatedTip("CCTransitionCrossFade","cc.TransitionCrossFade") + return cc.TransitionCrossFade +end +_G["CCTransitionCrossFade"] = DeprecatedClass.CCTransitionCrossFade() +--CCTransitionCrossFade class will be Deprecated,end + +--CCGridBase class will be Deprecated,begin +function DeprecatedClass.CCGridBase() + deprecatedTip("CCGridBase","cc.GridBase") + return cc.GridBase +end +_G["CCGridBase"] = DeprecatedClass.CCGridBase() +--CCGridBase class will be Deprecated,end + +--CCSkewTo class will be Deprecated,begin +function DeprecatedClass.CCSkewTo() + deprecatedTip("CCSkewTo","cc.SkewTo") + return cc.SkewTo +end +_G["CCSkewTo"] = DeprecatedClass.CCSkewTo() +--CCSkewTo class will be Deprecated,end + +--CCCardinalSplineTo class will be Deprecated,begin +function DeprecatedClass.CCCardinalSplineTo() + deprecatedTip("CCCardinalSplineTo","cc.CardinalSplineTo") + return cc.CardinalSplineTo +end +_G["CCCardinalSplineTo"] = DeprecatedClass.CCCardinalSplineTo() +--CCCardinalSplineTo class will be Deprecated,end + +--CCTMXMapInfo class will be Deprecated,begin +function DeprecatedClass.CCTMXMapInfo() + deprecatedTip("CCTMXMapInfo","cc.TMXMapInfo") + return cc.TMXMapInfo +end +_G["CCTMXMapInfo"] = DeprecatedClass.CCTMXMapInfo() +--CCTMXMapInfo class will be Deprecated,end + +--CCEaseExponentialIn class will be Deprecated,begin +function DeprecatedClass.CCEaseExponentialIn() + deprecatedTip("CCEaseExponentialIn","cc.EaseExponentialIn") + return cc.EaseExponentialIn +end +_G["CCEaseExponentialIn"] = DeprecatedClass.CCEaseExponentialIn() +--CCEaseExponentialIn class will be Deprecated,end + +--CCReuseGrid class will be Deprecated,begin +function DeprecatedClass.CCReuseGrid() + deprecatedTip("CCReuseGrid","cc.ReuseGrid") + return cc.ReuseGrid +end +_G["CCReuseGrid"] = DeprecatedClass.CCReuseGrid() +--CCReuseGrid class will be Deprecated,end + +--CCMenuItemAtlasFont class will be Deprecated,begin +function DeprecatedClass.CCMenuItemAtlasFont() + deprecatedTip("CCMenuItemAtlasFont","cc.MenuItemAtlasFont") + return cc.MenuItemAtlasFont +end +_G["CCMenuItemAtlasFont"] = DeprecatedClass.CCMenuItemAtlasFont() +--CCMenuItemAtlasFont class will be Deprecated,end + +--CCSpriteFrame class will be Deprecated,begin +function DeprecatedClass.CCSpriteFrame() + deprecatedTip("CCSpriteFrame","cc.SpriteFrame") + return cc.SpriteFrame +end +_G["CCSpriteFrame"] = DeprecatedClass.CCSpriteFrame() +--CCSpriteFrame class will be Deprecated,end + +--CCSplitRows class will be Deprecated,begin +function DeprecatedClass.CCSplitRows() + deprecatedTip("CCSplitRows","cc.SplitRows") + return cc.SplitRows +end +_G["CCSplitRows"] = DeprecatedClass.CCSplitRows() +--CCSplitRows class will be Deprecated,end + +--CCControlStepper class will be Deprecated,begin +function DeprecatedClass.CCControlStepper() + deprecatedTip("CCControlStepper","cc.ControlStepper") + return cc.ControlStepper +end +_G["CCControlStepper"] = DeprecatedClass.CCControlStepper() +--CCControlStepper class will be Deprecated,end + +--CCSprite class will be Deprecated,begin +function DeprecatedClass.CCSprite() + deprecatedTip("CCSprite","cc.Sprite") + return cc.Sprite +end +_G["CCSprite"] = DeprecatedClass.CCSprite() +--CCSprite class will be Deprecated,end + +--CCOrbitCamera class will be Deprecated,begin +function DeprecatedClass.CCOrbitCamera() + deprecatedTip("CCOrbitCamera","cc.OrbitCamera") + return cc.OrbitCamera +end +_G["CCOrbitCamera"] = DeprecatedClass.CCOrbitCamera() +--CCOrbitCamera class will be Deprecated,end + +--CCUserDefault class will be Deprecated,begin +function DeprecatedClass.CCUserDefault() + deprecatedTip("CCUserDefault","cc.UserDefault") + return cc.UserDefault +end +_G["CCUserDefault"] = DeprecatedClass.CCUserDefault() +--CCUserDefault class will be Deprecated,end + +--CCFadeOutUpTiles class will be Deprecated,begin +function DeprecatedClass.CCFadeOutUpTiles() + deprecatedTip("CCFadeOutUpTiles","cc.FadeOutUpTiles") + return cc.FadeOutUpTiles +end +_G["CCFadeOutUpTiles"] = DeprecatedClass.CCFadeOutUpTiles() +--CCFadeOutUpTiles class will be Deprecated,end + +--CCParticleRain class will be Deprecated,begin +function DeprecatedClass.CCParticleRain() + deprecatedTip("CCParticleRain","cc.ParticleRain") + return cc.ParticleRain +end +_G["CCParticleRain"] = DeprecatedClass.CCParticleRain() +--CCParticleRain class will be Deprecated,end + +--CCWaves class will be Deprecated,begin +function DeprecatedClass.CCWaves() + deprecatedTip("CCWaves","cc.Waves") + return cc.Waves +end +_G["CCWaves"] = DeprecatedClass.CCWaves() +--CCWaves class will be Deprecated,end + +--CCEaseOut class will be Deprecated,begin +function DeprecatedClass.CCEaseOut() + deprecatedTip("CCEaseOut","cc.EaseOut") + return cc.EaseOut +end +_G["CCEaseOut"] = DeprecatedClass.CCEaseOut() +--CCEaseOut class will be Deprecated,end + +--CCEaseBounceIn class will be Deprecated,begin +function DeprecatedClass.CCEaseBounceIn() + deprecatedTip("CCEaseBounceIn","cc.EaseBounceIn") + return cc.EaseBounceIn +end +_G["CCEaseBounceIn"] = DeprecatedClass.CCEaseBounceIn() +--CCEaseBounceIn class will be Deprecated,end + +--CCMenuItemFont class will be Deprecated,begin +function DeprecatedClass.CCMenuItemFont() + deprecatedTip("CCMenuItemFont","cc.MenuItemFont") + return cc.MenuItemFont +end +_G["CCMenuItemFont"] = DeprecatedClass.CCMenuItemFont() +--CCMenuItemFont class will be Deprecated,end + +--CCEaseSineOut class will be Deprecated,begin +function DeprecatedClass.CCEaseSineOut() + deprecatedTip("CCEaseSineOut","cc.EaseSineOut") + return cc.EaseSineOut +end +_G["CCEaseSineOut"] = DeprecatedClass.CCEaseSineOut() +--CCEaseSineOut class will be Deprecated,end + +--CCTextureCache class will be Deprecated,begin +function DeprecatedClass.CCTextureCache() + deprecatedTip("CCTextureCache","cc.TextureCache") + return cc.TextureCache +end +_G["CCTextureCache"] = DeprecatedClass.CCTextureCache() +--CCTextureCache class will be Deprecated,end + +--CCTiledGrid3D class will be Deprecated,begin +function DeprecatedClass.CCTiledGrid3D() + deprecatedTip("CCTiledGrid3D","cc.TiledGrid3D") + return cc.TiledGrid3D +end +_G["CCTiledGrid3D"] = DeprecatedClass.CCTiledGrid3D() +--CCTiledGrid3D class will be Deprecated,end + +--CCRemoveSelf class will be Deprecated,begin +function DeprecatedClass.CCRemoveSelf() + deprecatedTip("CCRemoveSelf","cc.RemoveSelf") + return cc.RemoveSelf +end +_G["CCRemoveSelf"] = DeprecatedClass.CCRemoveSelf() +--CCRemoveSelf class will be Deprecated,end + +--CCControlSaturationBrightnessPicker class will be Deprecated,begin +function DeprecatedClass.CCControlSaturationBrightnessPicker() + deprecatedTip("CCControlSaturationBrightnessPicker","cc.ControlSaturationBrightnessPicker") + return cc.ControlSaturationBrightnessPicker +end +_G["CCControlSaturationBrightnessPicker"] = DeprecatedClass.CCControlSaturationBrightnessPicker() +--CCControlSaturationBrightnessPicker class will be Deprecated,end + +--CCLabelTTF class will be Deprecated,begin +function DeprecatedClass.CCLabelTTF() + deprecatedTip("CCLabelTTF","cc.LabelTTF") + return cc.LabelTTF +end +_G["CCLabelTTF"] = DeprecatedClass.CCLabelTTF() +--CCLabelTTF class will be Deprecated,end + +--CCTouch class will be Deprecated,begin +function DeprecatedClass.CCTouch() + deprecatedTip("CCTouch","cc.Touch") + return cc.Touch +end +_G["CCTouch"] = DeprecatedClass.CCTouch() +--CCTouch class will be Deprecated,end + +--CCMoveBy class will be Deprecated,begin +function DeprecatedClass.CCMoveBy() + deprecatedTip("CCMoveBy","cc.MoveBy") + return cc.MoveBy +end +_G["CCMoveBy"] = DeprecatedClass.CCMoveBy() +--CCMoveBy class will be Deprecated,end + +--CCMotionStreak class will be Deprecated,begin +function DeprecatedClass.CCMotionStreak() + deprecatedTip("CCMotionStreak","cc.MotionStreak") + return cc.MotionStreak +end +_G["CCMotionStreak"] = DeprecatedClass.CCMotionStreak() +--CCMotionStreak class will be Deprecated,end + +--CCRotateBy class will be Deprecated,begin +function DeprecatedClass.CCRotateBy() + deprecatedTip("CCRotateBy","cc.RotateBy") + return cc.RotateBy +end +_G["CCRotateBy"] = DeprecatedClass.CCRotateBy() +--CCRotateBy class will be Deprecated,end + +--CCFileUtils class will be Deprecated,begin +function DeprecatedClass.CCFileUtils() + deprecatedTip("CCFileUtils","cc.FileUtils") + return cc.FileUtils +end +_G["CCFileUtils"] = DeprecatedClass.CCFileUtils() +--CCFileUtils class will be Deprecated,end + +--CCBezierBy class will be Deprecated,begin +function DeprecatedClass.CCBezierBy() + deprecatedTip("CCBezierBy","cc.BezierBy") + return cc.BezierBy +end +_G["CCBezierBy"] = DeprecatedClass.CCBezierBy() +--CCBezierBy class will be Deprecated,end + +--CCTransitionFade class will be Deprecated,begin +function DeprecatedClass.CCTransitionFade() + deprecatedTip("CCTransitionFade","cc.TransitionFade") + return cc.TransitionFade +end +_G["CCTransitionFade"] = DeprecatedClass.CCTransitionFade() +--CCTransitionFade class will be Deprecated,end + +--CCTransitionProgressOutIn class will be Deprecated,begin +function DeprecatedClass.CCTransitionProgressOutIn() + deprecatedTip("CCTransitionProgressOutIn","cc.TransitionProgressOutIn") + return cc.TransitionProgressOutIn +end +_G["CCTransitionProgressOutIn"] = DeprecatedClass.CCTransitionProgressOutIn() +--CCTransitionProgressOutIn class will be Deprecated,end + +--CCCatmullRomBy class will be Deprecated,begin +function DeprecatedClass.CCCatmullRomBy() + deprecatedTip("CCCatmullRomBy","cc.CatmullRomBy") + return cc.CatmullRomBy +end +_G["CCCatmullRomBy"] = DeprecatedClass.CCCatmullRomBy() +--CCCatmullRomBy class will be Deprecated,end + +--CCGridAction class will be Deprecated,begin +function DeprecatedClass.CCGridAction() + deprecatedTip("CCGridAction","cc.GridAction") + return cc.GridAction +end +_G["CCGridAction"] = DeprecatedClass.CCGridAction() +--CCGridAction class will be Deprecated,end + +--CCShaky3D class will be Deprecated,begin +function DeprecatedClass.CCShaky3D() + deprecatedTip("CCShaky3D","cc.Shaky3D") + return cc.Shaky3D +end +_G["CCShaky3D"] = DeprecatedClass.CCShaky3D() +--CCShaky3D class will be Deprecated,end + +--CCTransitionEaseScene class will be Deprecated,begin +function DeprecatedClass.CCTransitionEaseScene() + deprecatedTip("CCTransitionEaseScene","cc.TransitionEaseScene") + return cc.TransitionEaseScene +end +_G["CCTransitionEaseScene"] = DeprecatedClass.CCTransitionEaseScene() +--CCTransitionEaseScene class will be Deprecated,end + +--CCSequence class will be Deprecated,begin +function DeprecatedClass.CCSequence() + deprecatedTip("CCSequence","cc.Sequence") + return cc.Sequence +end +_G["CCSequence"] = DeprecatedClass.CCSequence() +--CCSequence class will be Deprecated,end + +--CCTransitionFadeUp class will be Deprecated,begin +function DeprecatedClass.CCTransitionFadeUp() + deprecatedTip("CCTransitionFadeUp","cc.TransitionFadeUp") + return cc.TransitionFadeUp +end +_G["CCTransitionFadeUp"] = DeprecatedClass.CCTransitionFadeUp() +--CCTransitionFadeUp class will be Deprecated,end + +--CCTransitionProgressRadialCW class will be Deprecated,begin +function DeprecatedClass.CCTransitionProgressRadialCW() + deprecatedTip("CCTransitionProgressRadialCW","cc.TransitionProgressRadialCW") + return cc.TransitionProgressRadialCW +end +_G["CCTransitionProgressRadialCW"] = DeprecatedClass.CCTransitionProgressRadialCW() +--CCTransitionProgressRadialCW class will be Deprecated,end + +--CCShuffleTiles class will be Deprecated,begin +function DeprecatedClass.CCShuffleTiles() + deprecatedTip("CCShuffleTiles","cc.ShuffleTiles") + return cc.ShuffleTiles +end +_G["CCShuffleTiles"] = DeprecatedClass.CCShuffleTiles() +--CCShuffleTiles class will be Deprecated,end + +--CCTransitionSlideInR class will be Deprecated,begin +function DeprecatedClass.CCTransitionSlideInR() + deprecatedTip("CCTransitionSlideInR","cc.TransitionSlideInR") + return cc.TransitionSlideInR +end +_G["CCTransitionSlideInR"] = DeprecatedClass.CCTransitionSlideInR() +--CCTransitionSlideInR class will be Deprecated,end + +--CCScene class will be Deprecated,begin +function DeprecatedClass.CCScene() + deprecatedTip("CCScene","cc.Scene") + return cc.Scene +end +_G["CCScene"] = DeprecatedClass.CCScene() +--CCScene class will be Deprecated,end + +--CCParallaxNode class will be Deprecated,begin +function DeprecatedClass.CCParallaxNode() + deprecatedTip("CCParallaxNode","cc.ParallaxNode") + return cc.ParallaxNode +end +_G["CCParallaxNode"] = DeprecatedClass.CCParallaxNode() +--CCParallaxNode class will be Deprecated,end + +--CCTransitionSlideInL class will be Deprecated,begin +function DeprecatedClass.CCTransitionSlideInL() + deprecatedTip("CCTransitionSlideInL","cc.TransitionSlideInL") + return cc.TransitionSlideInL +end +_G["CCTransitionSlideInL"] = DeprecatedClass.CCTransitionSlideInL() +--CCTransitionSlideInL class will be Deprecated,end + +--CCControlSwitch class will be Deprecated,begin +function DeprecatedClass.CCControlSwitch() + deprecatedTip("CCControlSwitch","cc.ControlSwitch") + return cc.ControlSwitch +end +_G["CCControlSwitch"] = DeprecatedClass.CCControlSwitch() +--CCControlSwitch class will be Deprecated,end + +--CCWavesTiles3D class will be Deprecated,begin +function DeprecatedClass.CCWavesTiles3D() + deprecatedTip("CCWavesTiles3D","cc.WavesTiles3D") + return cc.WavesTiles3D +end +_G["CCWavesTiles3D"] = DeprecatedClass.CCWavesTiles3D() +--CCWavesTiles3D class will be Deprecated,end + +--CCTransitionSlideInB class will be Deprecated,begin +function DeprecatedClass.CCTransitionSlideInB() + deprecatedTip("CCTransitionSlideInB","cc.TransitionSlideInB") + return cc.TransitionSlideInB +end +_G["CCTransitionSlideInB"] = DeprecatedClass.CCTransitionSlideInB() +--CCTransitionSlideInB class will be Deprecated,end + +--CCSpeed class will be Deprecated,begin +function DeprecatedClass.CCSpeed() + deprecatedTip("CCSpeed","cc.Speed") + return cc.Speed +end +_G["CCSpeed"] = DeprecatedClass.CCSpeed() +--CCSpeed class will be Deprecated,end + +--CCShatteredTiles3D class will be Deprecated,begin +function DeprecatedClass.CCShatteredTiles3D() + deprecatedTip("CCShatteredTiles3D","cc.ShatteredTiles3D") + return cc.ShatteredTiles3D +end +_G["CCShatteredTiles3D"] = DeprecatedClass.CCShatteredTiles3D() +--CCShatteredTiles3D class will be Deprecated,end + +--CCCallFuncN class will be Deprecated,begin +function DeprecatedClass.CCCallFuncN() + deprecatedTip("CCCallFuncN","cc.CallFunc") + return cc.CallFunc +end +_G["CCCallFuncN"] = DeprecatedClass.CCCallFuncN() +--CCCallFuncN class will be Deprecated,end + +--CCArmature class will be Deprecated,begin +function DeprecatedClass.CCArmature() + deprecatedTip("CCArmature","ccs.Armature") + return ccs.Armature +end +_G["CCArmature"] = DeprecatedClass.CCArmature() +--CCArmature class will be Deprecated,end + +--CCArmatureAnimation class will be Deprecated,begin +function DeprecatedClass.CCArmatureAnimation() + deprecatedTip("CCArmatureAnimation","ccs.ArmatureAnimation") + return ccs.ArmatureAnimation +end +_G["CCArmatureAnimation"] = DeprecatedClass.CCArmatureAnimation() +--CCArmatureAnimation class will be Deprecated,end + +--CCSkin class will be Deprecated,begin +function DeprecatedClass.CCSkin() + deprecatedTip("CCSkin","ccs.Skin") + return ccs.Skin +end +_G["CCSkin"] = DeprecatedClass.CCSkin() +--CCSkin class will be Deprecated,end + +--CCBone class will be Deprecated,begin +function DeprecatedClass.CCBone() + deprecatedTip("CCBone","ccs.Bone") + return ccs.Bone +end +_G["CCBone"] = DeprecatedClass.CCBone() +--CCBone class will be Deprecated,end + +--CCArmatureDataManager class will be Deprecated,begin +function DeprecatedClass.CCArmatureDataManager() + deprecatedTip("CCArmatureDataManager","ccs.ArmatureDataManager") + return ccs.ArmatureDataManager +end +_G["CCArmatureDataManager"] = DeprecatedClass.CCArmatureDataManager() +--CCArmatureDataManager class will be Deprecated,end + +--CCBatchNode class will be Deprecated,begin +function DeprecatedClass.CCBatchNode() + deprecatedTip("CCBatchNode","ccs.BatchNode") + return ccs.BatchNode +end +_G["CCBatchNode"] = DeprecatedClass.CCBatchNode() +--CCBatchNode class will be Deprecated,end + +--CCTween class will be Deprecated,begin +function DeprecatedClass.CCTween() + deprecatedTip("CCTween","ccs.Tween") + return ccs.Tween +end +_G["CCTween"] = DeprecatedClass.CCTween() +--CCTween class will be Deprecated,end + +--CCBaseData class will be Deprecated,begin +function DeprecatedClass.CCBaseData() + deprecatedTip("CCBaseData","ccs.BaseData") + return ccs.BaseData +end +_G["CCBaseData"] = DeprecatedClass.CCBaseData() +--CCBaseData class will be Deprecated,end + +--CCDisplayManager class will be Deprecated,begin +function DeprecatedClass.CCDisplayManager() + deprecatedTip("CCDisplayManager","ccs.DisplayManager") + return ccs.DisplayManager +end +_G["CCDisplayManager"] = DeprecatedClass.CCDisplayManager() +--CCDisplayManager class will be Deprecated,end + +--UIHelper class will be Deprecated,begin +function DeprecatedClass.UIHelper() + deprecatedTip("UIHelper","ccs.UIHelper") + return ccs.UIHelper +end +_G["UIHelper"] = DeprecatedClass.UIHelper() +--UIHelper class will be Deprecated,end + +--UILayout class will be Deprecated,begin +function DeprecatedClass.UILayout() + deprecatedTip("UILayout","ccs.UILayout") + return ccs.UILayout +end +_G["UILayout"] = DeprecatedClass.UILayout() +--UILayout class will be Deprecated,end + +--UIWidget class will be Deprecated,begin +function DeprecatedClass.UIWidget() + deprecatedTip("UIWidget","ccs.UIWidget") + return ccs.UIWidget +end +_G["UIWidget"] = DeprecatedClass.UIWidget() +--UIWidget class will be Deprecated,end + +--UILayer class will be Deprecated,begin +function DeprecatedClass.UILayer() + deprecatedTip("UILayer","ccs.UILayer") + return ccs.UILayer +end +_G["UILayer"] = DeprecatedClass.UILayer() +--UILayer class will be Deprecated,end + +--UIButton class will be Deprecated,begin +function DeprecatedClass.UIButton() + deprecatedTip("UIButton","ccs.UIButton") + return ccs.UIButton +end +_G["UIButton"] = DeprecatedClass.UIButton() +--UIButton class will be Deprecated,end + +--UICheckBox class will be Deprecated,begin +function DeprecatedClass.UICheckBox() + deprecatedTip("UICheckBox","ccs.UICheckBox") + return ccs.UICheckBox +end +_G["UICheckBox"] = DeprecatedClass.UICheckBox() +--UICheckBox class will be Deprecated,end + +--UIImageView class will be Deprecated,begin +function DeprecatedClass.UIImageView() + deprecatedTip("UIImageView","ccs.UIImageView") + return ccs.UIImageView +end +_G["UIImageView"] = DeprecatedClass.UIImageView() +--UIImageView class will be Deprecated,end + +--UILabel class will be Deprecated,begin +function DeprecatedClass.UILabel() + deprecatedTip("UILabel","ccs.UILabel") + return ccs.UILabel +end +_G["UILabel"] = DeprecatedClass.UILabel() +--UILabel class will be Deprecated,end + +--UILabelAtlas class will be Deprecated,begin +function DeprecatedClass.UILabelAtlas() + deprecatedTip("UILabelAtlas","ccs.UILabelAtlas") + return ccs.UILabelAtlas +end +_G["UILabelAtlas"] = DeprecatedClass.UILabelAtlas() +--UILabelAtlas class will be Deprecated,end + +--UILabelBMFont class will be Deprecated,begin +function DeprecatedClass.UILabelBMFont() + deprecatedTip("UILabelBMFont","ccs.UILabelBMFont") + return ccs.UILabelBMFont +end +_G["UILabelBMFont"] = DeprecatedClass.UILabelBMFont() +--UILabelBMFont class will be Deprecated,end + +--UILoadingBar class will be Deprecated,begin +function DeprecatedClass.UILoadingBar() + deprecatedTip("UILoadingBar","ccs.UILoadingBar") + return ccs.UILoadingBar +end +_G["UILoadingBar"] = DeprecatedClass.UILoadingBar() +--UILoadingBar class will be Deprecated,end + +--UISlider class will be Deprecated,begin +function DeprecatedClass.UISlider() + deprecatedTip("UISlider","ccs.UISlider") + return ccs.UISlider +end +_G["UISlider"] = DeprecatedClass.UISlider() +--UISlider class will be Deprecated,end + +--UITextField class will be Deprecated,begin +function DeprecatedClass.UITextField() + deprecatedTip("UITextField","ccs.UITextField") + return ccs.UITextField +end +_G["UITextField"] = DeprecatedClass.UITextField() +--UITextField class will be Deprecated,end + +--UIScrollView class will be Deprecated,begin +function DeprecatedClass.UIScrollView() + deprecatedTip("UIScrollView","ccs.UIScrollView") + return ccs.UIScrollView +end +_G["UIScrollView"] = DeprecatedClass.UIScrollView() +--UIScrollView class will be Deprecated,end + +--UIPageView class will be Deprecated,begin +function DeprecatedClass.UIPageView() + deprecatedTip("UIPageView","ccs.UIPageView") + return ccs.UIPageView +end +_G["UIPageView"] = DeprecatedClass.UIPageView() +--UIPageView class will be Deprecated,end + +--UIListView class will be Deprecated,begin +function DeprecatedClass.UIListView() + deprecatedTip("UIListView","ccs.UIListView") + return ccs.UIListView +end +_G["UIListView"] = DeprecatedClass.UIListView() +--UIListView class will be Deprecated,end + +--UILayoutParameter class will be Deprecated,begin +function DeprecatedClass.UILayoutParameter() + deprecatedTip("UILayoutParameter","ccs.UILayoutParameter") + return ccs.UILayoutParameter +end +_G["UILayoutParameter"] = DeprecatedClass.UILayoutParameter() +--UILayoutParameter class will be Deprecated,end + +--UILinearLayoutParameter class will be Deprecated,begin +function DeprecatedClass.UILinearLayoutParameter() + deprecatedTip("UILinearLayoutParameter","ccs.UILinearLayoutParameter") + return ccs.UILinearLayoutParameter +end +_G["UILinearLayoutParameter"] = DeprecatedClass.UILinearLayoutParameter() +--UILinearLayoutParameter class will be Deprecated,end + +--UIRelativeLayoutParameter class will be Deprecated,begin +function DeprecatedClass.UIRelativeLayoutParameter() + deprecatedTip("UIRelativeLayoutParameter","ccs.UIRelativeLayoutParameter") + return ccs.UIRelativeLayoutParameter +end +_G["UIRelativeLayoutParameter"] = DeprecatedClass.UIRelativeLayoutParameter() +--UIRelativeLayoutParameter class will be Deprecated,end + +--CCComController class will be Deprecated,begin +function DeprecatedClass.CCComController() + deprecatedTip("CCComController","ccs.ComController") + return ccs.CCComController +end +_G["CCComController"] = DeprecatedClass.CCComController() +--CCComController class will be Deprecated,end + +--CCComAudio class will be Deprecated,begin +function DeprecatedClass.CCComAudio() + deprecatedTip("CCComAudio","ccs.ComAudio") + return ccs.ComAudio +end +_G["CCComAudio"] = DeprecatedClass.CCComAudio() +--CCComAudio class will be Deprecated,end + +--CCComAttribute class will be Deprecated,begin +function DeprecatedClass.CCComAttribute() + deprecatedTip("CCComAttribute","ccs.ComAttribute") + return ccs.ComAttribute +end +_G["CCComAttribute"] = DeprecatedClass.CCComAttribute() +--CCComAttribute class will be Deprecated,end + +--CCComRender class will be Deprecated,begin +function DeprecatedClass.CCComRender() + deprecatedTip("CCComRender","ccs.ComRender") + return ccs.ComRender +end +_G["CCComRender"] = DeprecatedClass.CCComRender() +--CCComRender class will be Deprecated,end + +--ActionManager class will be Deprecated,begin +function DeprecatedClass.ActionManager() + deprecatedTip("ActionManager","ccs.ActionManagerEx") + return ccs.ActionManagerEx +end +_G["ActionManager"] = DeprecatedClass.ActionManager() +--CCComRender class will be Deprecated,end + +--SceneReader class will be Deprecated,begin +function DeprecatedClass.SceneReader() + deprecatedTip("SceneReader","ccs.SceneReader") + return ccs.SceneReader +end +_G["SceneReader"] = DeprecatedClass.SceneReader() +--SceneReader class will be Deprecated,end + +--GUIReader class will be Deprecated,begin +function DeprecatedClass.GUIReader() + deprecatedTip("GUIReader","ccs.GUIReader") + return ccs.GUIReader +end +_G["GUIReader"] = DeprecatedClass.GUIReader() +--GUIReader class will be Deprecated,end + +--UIRootWidget class will be Deprecated,begin +function DeprecatedClass.UIRootWidget() + deprecatedTip("UIRootWidget","ccs.UIRootWidget") + return ccs.UIRootWidget +end +_G["UIRootWidget"] = DeprecatedClass.UIRootWidget() +--UIRootWidget class will be Deprecated,end + +--ActionObject class will be Deprecated,begin +function DeprecatedClass.ActionObject() + deprecatedTip("ActionObject","ccs.ActionObject") + return ccs.ActionObject +end +_G["ActionObject"] = DeprecatedClass.ActionObject() +--ActionObject class will be Deprecated,end + +--CCEGLViewProtocol class will be Deprecated,begin +function DeprecatedClass.CCEGLViewProtocol() + deprecatedTip("CCEGLViewProtocol","cc.GLViewProtocol") + return cc.GLViewProtocol +end +_G["CCEGLViewProtocol"] = DeprecatedClass.CCEGLViewProtocol() +--CCEGLViewProtocol class will be Deprecated,end + +--CCEGLView class will be Deprecated,begin +function DeprecatedClass.CCEGLView() + deprecatedTip("CCEGLView","cc.GLView") + return cc.GLView +end + +_G["CCEGLView"] = DeprecatedClass.CCEGLView() +--CCEGLView class will be Deprecated,end + +--CCBProxy class will be Deprecated,begin +function DeprecatedClass.CCBProxy() + deprecatedTip("CCBProxy","cc.CCBProxy") + return cc.CCBProxy +end +_G["CCBProxy"] = DeprecatedClass.CCBProxy() +--CCBProxy class will be Deprecated,end + +--WebSocket class will be Deprecated,begin +function DeprecatedClass.WebSocket() + deprecatedTip("WebSocket","cc.WebSocket") + return cc.WebSocket +end +_G["WebSocket"] = DeprecatedClass.WebSocket() +--WebSocket class will be Deprecated,end + +--XMLHttpRequest class will be Deprecated,begin +function DeprecatedClass.XMLHttpRequest() + deprecatedTip("XMLHttpRequest","cc.XMLHttpRequest") + return cc.XMLHttpRequest +end +_G["XMLHttpRequest"] = DeprecatedClass.XMLHttpRequest() +--XMLHttpRequest class will be Deprecated,end + +--EGLViewProtocol class will be Deprecated,begin +function DeprecatedClass.EGLViewProtocol() + deprecatedTip("cc.EGLViewProtocol","cc.GLViewProtocol") + return cc.GLViewProtocol +end +_G["cc.EGLViewProtocol"] = DeprecatedClass.EGLViewProtocol() +--EGLViewProtocol class will be Deprecated,end + +--EGLView class will be Deprecated,begin +function DeprecatedClass.EGLView() + deprecatedTip("cc.EGLView","cc.GLView") + return cc.GLView +end +_G["cc.EGLView"] = DeprecatedClass.EGLView() +--EGLView class will be Deprecated,end + + diff --git a/cocos/scripting/lua-bindings/script/DeprecatedEnum.lua b/cocos/scripting/lua-bindings/script/DeprecatedEnum.lua new file mode 100644 index 0000000000..96ea41dc97 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/DeprecatedEnum.lua @@ -0,0 +1,487 @@ +require "Cocos2dConstants.lua" +require "OpenglConstants.lua" +require "StudioConstants.lua" +require "GuiConstants.lua" +--Enums will be deprecated,begin +_G.kCCTextAlignmentLeft = cc.TEXT_ALIGNMENT_LEFT +_G.kCCTextAlignmentRight = cc.TEXT_ALIGNMENT_RIGHT +_G.kCCTextAlignmentCenter = cc.TEXT_ALIGNMENT_CENTER +_G.kCCVerticalTextAlignmentTop = cc.VERTICAL_TEXT_ALIGNMENT_TOP +_G.kCCVerticalTextAlignmentCenter = cc.VERTICAL_TEXT_ALIGNMENT_CENTER +_G.kCCVerticalTextAlignmentBottom = cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM +_G.kCCDirectorProjection3D = cc.DIRECTOR_PROJECTION3_D +_G.kCCDirectorProjection2D = cc.DIRECTOR_PROJECTION2_D +_G.kCCDirectorProjectionCustom = cc.DIRECTOR_PROJECTION_CUSTOM +_G.kCCDirectorProjectionDefault = cc.DIRECTOR_PROJECTION_DEFAULT +_G.kCCNodeTagInvalid = cc.NODE_TAG_INVALID +_G.kCCNodeOnEnter = cc.NODE_ON_ENTER +_G.kCCNodeOnExit = cc.NODE_ON_EXIT +_G.kCCTexture2DPixelFormat_RGBA8888 = cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888 +_G.kCCTexture2DPixelFormat_RGB888 = cc.TEXTURE2_D_PIXEL_FORMAT_RG_B888 +_G.kCCTexture2DPixelFormat_RGB565 = cc.TEXTURE2_D_PIXEL_FORMAT_RG_B565 +_G.kCCTexture2DPixelFormat_A8 = cc.TEXTURE2_D_PIXEL_FORMAT_A8 +_G.kCCTexture2DPixelFormat_I8 = cc.TEXTURE2_D_PIXEL_FORMAT_I8 +_G.kCCTexture2DPixelFormat_AI88 = cc.TEXTURE2_D_PIXEL_FORMAT_A_I88 +_G.kCCTexture2DPixelFormat_RGBA4444 = cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444 +_G.kCCTexture2DPixelFormat_RGB5A1 = cc.TEXTURE2_D_PIXEL_FORMAT_RGB5_A1 +_G.kCCTexture2DPixelFormat_PVRTC4 = cc.TEXTURE2_D_PIXEL_FORMAT_PVRTC4 +_G.kCCTexture2DPixelFormat_PVRTC2 = cc.TEXTURE2_D_PIXEL_FORMAT_PVRTC2 +_G.kCCTexture2DPixelFormat_Default = cc.TEXTURE2_D_PIXEL_FORMAT_DEFAULT +_G.kCCImageFormatPNG = cc.IMAGE_FORMAT_PNG +_G.kCCImageFormatJPEG = cc.IMAGE_FORMAT_JPEG +_G.kCCTouchesOneByOne = cc.TOUCHES_ONE_BY_ONE +_G.kCCTouchesAllAtOnce = cc.TOUCHES_ALL_AT_ONCE +_G.kCCTransitionOrientationLeftOver = cc.TRANSITION_ORIENTATION_LEFT_OVER +_G.kCCTransitionOrientationRightOver = cc.TRANSITION_ORIENTATION_RIGHT_OVER +_G.kCCTransitionOrientationUpOver = cc.TRANSITION_ORIENTATION_UP_OVER +_G.kCCTransitionOrientationDownOver = cc.TRANSITION_ORIENTATION_DOWN_OVER +_G.kCCActionTagInvalid = cc.ACTION_TAG_INVALID +_G.kCCLabelAutomaticWidth = cc.LABEL_AUTOMATIC_WIDTH +_G.kCCMenuStateWaiting = cc.MENU_STATE_WAITING +_G.kCCMenuStateTrackingTouch = cc.MENU_STATE_TRACKING_TOUCH +_G.kCCMenuHandlerPriority = cc.MENU_HANDLER_PRIORITY +_G.kCCParticleDurationInfinity = cc.PARTICLE_DURATION_INFINITY +_G.kCCParticleStartSizeEqualToEndSize = cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE +_G.kCCParticleStartRadiusEqualToEndRadius = cc.PARTICLE_START_RADIUS_EQUAL_TO_END_RADIUS +_G.kCCParticleModeGravity = cc.PARTICLE_MODE_GRAVITY +_G.kCCParticleModeRadius = cc.PARTICLE_MODE_RADIUS +_G.kCCPositionTypeFree = cc.POSITION_TYPE_FREE +_G.kCCPositionTypeRelative = cc.POSITION_TYPE_RELATIVE +_G.kCCPositionTypeGrouped = cc.POSITION_TYPE_GROUPED +_G.kCCProgressTimerTypeRadial = cc.PROGRESS_TIMER_TYPE_RADIAL +_G.kCCProgressTimerTypeBar = cc.PROGRESS_TIMER_TYPE_BAR +_G.kCCTMXTileHorizontalFlag = cc.TMX_TILE_HORIZONTAL_FLAG +_G.kCCTMXTileVerticalFlag = cc.TMX_TILE_VERTICAL_FLAG +_G.kCCTMXTileDiagonalFlag = cc.TMX_TILE_DIAGONAL_FLAG +_G.kCCFlipedAll = cc.FLIPED_ALL +_G.kCCFlippedMask = cc.FLIPPED_MASK +_G.kCCControlStepperPartMinus = cc.CONTROL_STEPPER_PART_MINUS +_G.kCCControlStepperPartPlus = cc.CONTROL_STEPPER_PART_PLUS +_G.kCCControlStepperPartNone = cc.CONTROL_STEPPER_PART_NONE + +_G.kLanguageEnglish = cc.LANGUAGE_ENGLISH +_G.kLanguageChinese = cc.LANGUAGE_CHINESE +_G.kLanguageFrench = cc.LANGUAGE_FRENCH +_G.kLanguageItalian = cc.LANGUAGE_ITALIAN +_G.kLanguageGerman = cc.LANGUAGE_GERMAN +_G.kLanguageSpanish = cc.LANGUAGE_SPANISH +_G.kLanguageRussian = cc.LANGUAGE_RUSSIAN +_G.kLanguageKorean = cc.LANGUAGE_KOREAN +_G.kLanguageJapanese = cc.LANGUAGE_JAPANESE +_G.kLanguageHungarian = cc.LANGUAGE_HUNGARIAN +_G.kLanguagePortuguese = cc.LANGUAGE_PORTUGUESE +_G.kLanguageArabic = cc.LANGUAGE_ARABIC +_G.kTargetWindows = cc.PLATFORM_OS_WINDOWS +_G.kTargetLinux = cc.PLATFORM_OS_LINUX +_G.kTargetMacOS = cc.PLATFORM_OS_MAC +_G.kTargetAndroid = cc.PLATFORM_OS_ANDROID +_G.kTargetIphone = cc.PLATFORM_OS_IPHONE +_G.kTargetIpad = cc.PLATFORM_OS_IPAD +_G.kTargetBlackBerry = cc.PLATFORM_OS_BLACKBERRY + +_G.GL_ZERO = gl.ZERO +_G.GL_ONE = gl.ONE +_G.GL_SRC_COLOR = gl.SRC_COLOR +_G.GL_ONE_MINUS_SRC_COLOR = gl.ONE_MINUS_SRC_COLOR +_G.GL_SRC_ALPHA = gl.SRC_ALPHA +_G.GL_ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA +_G.GL_DST_ALPHA = gl.DST_ALPHA +_G.GL_ONE_MINUS_DST_ALPHA = gl.ONE_MINUS_DST_ALPHA +_G.GL_DST_COLOR = gl.DST_COLOR +_G.GL_ONE_MINUS_DST_COLOR = gl.ONE_MINUS_DST_COLOR +_G.GL_RENDERBUFFER_INTERNAL_FORMAT = gl.RENDERBUFFER_INTERNAL_FORMAT +_G.GL_LINE_WIDTH = gl.LINE_WIDTH +_G.GL_CONSTANT_ALPHA = gl.CONSTANT_ALPHA +_G.GL_BLEND_SRC_ALPHA = gl.BLEND_SRC_ALPHA +_G.GL_GREEN_BITS = gl.GREEN_BITS +_G.GL_STENCIL_REF = gl.STENCIL_REF +_G.GL_ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA +_G.GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = gl.FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE +_G.GL_CCW = gl.CCW +_G.GL_MAX_TEXTURE_IMAGE_UNITS = gl.MAX_TEXTURE_IMAGE_UNITS +_G.GL_BACK = gl.BACK +_G.GL_ACTIVE_ATTRIBUTES = gl.ACTIVE_ATTRIBUTES +_G.GL_TEXTURE_CUBE_MAP_POSITIVE_X = gl.TEXTURE_CUBE_MAP_POSITIVE_X +_G.GL_STENCIL_BACK_VALUE_MASK = gl.STENCIL_BACK_VALUE_MASK +_G.GL_TEXTURE_CUBE_MAP_POSITIVE_Z = gl.TEXTURE_CUBE_MAP_POSITIVE_Z +_G.GL_ONE = gl.ONE +_G.GL_TRUE = gl.TRUE +_G.GL_TEXTURE12 = gl.TEXTURE12 +_G.GL_LINK_STATUS = gl.LINK_STATUS +_G.GL_BLEND = gl.BLEND +_G.GL_LESS = gl.LESS +_G.GL_TEXTURE16 = gl.TEXTURE16 +_G.GL_BOOL_VEC2 = gl.BOOL_VEC2 +_G.GL_KEEP = gl.KEEP +_G.GL_DST_COLOR = gl.DST_COLOR +_G.GL_VERTEX_ATTRIB_ARRAY_ENABLED = gl.VERTEX_ATTRIB_ARRAY_ENABLED +_G.GL_EXTENSIONS = gl.EXTENSIONS +_G.GL_FRONT = gl.FRONT +_G.GL_DST_ALPHA = gl.DST_ALPHA +_G.GL_ATTACHED_SHADERS = gl.ATTACHED_SHADERS +_G.GL_STENCIL_BACK_FUNC = gl.STENCIL_BACK_FUNC +_G.GL_ONE_MINUS_DST_COLOR = gl.ONE_MINUS_DST_COLOR +_G.GL_BLEND_EQUATION = gl.BLEND_EQUATION +_G.GL_RENDERBUFFER_DEPTH_SIZE = gl.RENDERBUFFER_DEPTH_SIZE +_G.GL_PACK_ALIGNMENT = gl.PACK_ALIGNMENT +_G.GL_VENDOR = gl.VENDOR +_G.GL_NEAREST_MIPMAP_LINEAR = gl.NEAREST_MIPMAP_LINEAR +_G.GL_TEXTURE_CUBE_MAP_POSITIVE_Y = gl.TEXTURE_CUBE_MAP_POSITIVE_Y +_G.GL_NEAREST = gl.NEAREST +_G.GL_RENDERBUFFER_WIDTH = gl.RENDERBUFFER_WIDTH +_G.GL_ARRAY_BUFFER_BINDING = gl.ARRAY_BUFFER_BINDING +_G.GL_ARRAY_BUFFER = gl.ARRAY_BUFFER +_G.GL_LEQUAL = gl.LEQUAL +_G.GL_VERSION = gl.VERSION +_G.GL_COLOR_CLEAR_VALUE = gl.COLOR_CLEAR_VALUE +_G.GL_RENDERER = gl.RENDERER +_G.GL_STENCIL_BACK_PASS_DEPTH_PASS = gl.STENCIL_BACK_PASS_DEPTH_PASS +_G.GL_STENCIL_BACK_PASS_DEPTH_FAIL = gl.STENCIL_BACK_PASS_DEPTH_FAIL +_G.GL_STENCIL_BACK_WRITEMASK = gl.STENCIL_BACK_WRITEMASK +_G.GL_BOOL = gl.BOOL +_G.GL_VIEWPORT = gl.VIEWPORT +_G.GL_FRAGMENT_SHADER = gl.FRAGMENT_SHADER +_G.GL_LUMINANCE = gl.LUMINANCE +_G.GL_DECR_WRAP = gl.DECR_WRAP +_G.GL_FUNC_ADD = gl.FUNC_ADD +_G.GL_ONE_MINUS_DST_ALPHA = gl.ONE_MINUS_DST_ALPHA +_G.GL_OUT_OF_MEMORY = gl.OUT_OF_MEMORY +_G.GL_BOOL_VEC4 = gl.BOOL_VEC4 +_G.GL_POLYGON_OFFSET_FACTOR = gl.POLYGON_OFFSET_FACTOR +_G.GL_STATIC_DRAW = gl.STATIC_DRAW +_G.GL_DITHER = gl.DITHER +_G.GL_TEXTURE31 = gl.TEXTURE31 +_G.GL_TEXTURE30 = gl.TEXTURE30 +_G.GL_UNSIGNED_BYTE = gl.UNSIGNED_BYTE +_G.GL_DEPTH_COMPONENT16 = gl.DEPTH_COMPONENT16 +_G.GL_TEXTURE23 = gl.TEXTURE23 +_G.GL_DEPTH_TEST = gl.DEPTH_TEST +_G.GL_STENCIL_PASS_DEPTH_FAIL = gl.STENCIL_PASS_DEPTH_FAIL +_G.GL_BOOL_VEC3 = gl.BOOL_VEC3 +_G.GL_POLYGON_OFFSET_UNITS = gl.POLYGON_OFFSET_UNITS +_G.GL_TEXTURE_BINDING_2D = gl.TEXTURE_BINDING_2D +_G.GL_TEXTURE21 = gl.TEXTURE21 +_G.GL_UNPACK_ALIGNMENT = gl.UNPACK_ALIGNMENT +_G.GL_DONT_CARE = gl.DONT_CARE +_G.GL_BUFFER_SIZE = gl.BUFFER_SIZE +_G.GL_FLOAT_MAT3 = gl.FLOAT_MAT3 +_G.GL_UNSIGNED_SHORT_5_6_5 = gl.UNSIGNED_SHORT_5_6_5 +_G.GL_INT_VEC2 = gl.INT_VEC2 +_G.GL_UNSIGNED_SHORT_4_4_4_4 = gl.UNSIGNED_SHORT_4_4_4_4 +_G.GL_NONE = gl.NONE +_G.GL_BLEND_DST_ALPHA = gl.BLEND_DST_ALPHA +_G.GL_VERTEX_ATTRIB_ARRAY_SIZE = gl.VERTEX_ATTRIB_ARRAY_SIZE +_G.GL_SRC_COLOR = gl.SRC_COLOR +_G.GL_COMPRESSED_TEXTURE_FORMATS = gl.COMPRESSED_TEXTURE_FORMATS +_G.GL_STENCIL_ATTACHMENT = gl.STENCIL_ATTACHMENT +_G.GL_MAX_VERTEX_ATTRIBS = gl.MAX_VERTEX_ATTRIBS +_G.GL_NUM_COMPRESSED_TEXTURE_FORMATS = gl.NUM_COMPRESSED_TEXTURE_FORMATS +_G.GL_BLEND_EQUATION_RGB = gl.BLEND_EQUATION_RGB +_G.GL_TEXTURE = gl.TEXTURE +_G.GL_LINEAR_MIPMAP_LINEAR = gl.LINEAR_MIPMAP_LINEAR +_G.GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING = gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING +_G.GL_CURRENT_PROGRAM = gl.CURRENT_PROGRAM +_G.GL_COLOR_BUFFER_BIT = gl.COLOR_BUFFER_BIT +_G.GL_TEXTURE20 = gl.TEXTURE20 +_G.GL_ACTIVE_ATTRIBUTE_MAX_LENGTH = gl.ACTIVE_ATTRIBUTE_MAX_LENGTH +_G.GL_TEXTURE28 = gl.TEXTURE28 +_G.GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE = gl.FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE +_G.GL_TEXTURE22 = gl.TEXTURE22 +_G.GL_ELEMENT_ARRAY_BUFFER_BINDING = gl.ELEMENT_ARRAY_BUFFER_BINDING +_G.GL_STREAM_DRAW = gl.STREAM_DRAW +_G.GL_SCISSOR_BOX = gl.SCISSOR_BOX +_G.GL_TEXTURE26 = gl.TEXTURE26 +_G.GL_TEXTURE27 = gl.TEXTURE27 +_G.GL_TEXTURE24 = gl.TEXTURE24 +_G.GL_TEXTURE25 = gl.TEXTURE25 +_G.GL_NO_ERROR = gl.NO_ERROR +_G.GL_TEXTURE29 = gl.TEXTURE29 +_G.GL_FLOAT_MAT4 = gl.FLOAT_MAT4 +_G.GL_VERTEX_ATTRIB_ARRAY_NORMALIZED = gl.VERTEX_ATTRIB_ARRAY_NORMALIZED +_G.GL_SAMPLE_COVERAGE_INVERT = gl.SAMPLE_COVERAGE_INVERT +_G.GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL = gl.FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL +_G.GL_FLOAT_VEC3 = gl.FLOAT_VEC3 +_G.GL_STENCIL_CLEAR_VALUE = gl.STENCIL_CLEAR_VALUE +_G.GL_UNSIGNED_SHORT_5_5_5_1 = gl.UNSIGNED_SHORT_5_5_5_1 +_G.GL_ACTIVE_UNIFORMS = gl.ACTIVE_UNIFORMS +_G.GL_INVALID_OPERATION = gl.INVALID_OPERATION +_G.GL_DEPTH_ATTACHMENT = gl.DEPTH_ATTACHMENT +_G.GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS = gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS +_G.GL_FRAMEBUFFER_COMPLETE = gl.FRAMEBUFFER_COMPLETE +_G.GL_ONE_MINUS_CONSTANT_COLOR = gl.ONE_MINUS_CONSTANT_COLOR +_G.GL_TEXTURE2 = gl.TEXTURE2 +_G.GL_TEXTURE1 = gl.TEXTURE1 +_G.GL_GEQUAL = gl.GEQUAL +_G.GL_TEXTURE7 = gl.TEXTURE7 +_G.GL_TEXTURE6 = gl.TEXTURE6 +_G.GL_TEXTURE5 = gl.TEXTURE5 +_G.GL_TEXTURE4 = gl.TEXTURE4 +_G.GL_GENERATE_MIPMAP_HINT = gl.GENERATE_MIPMAP_HINT +_G.GL_ONE_MINUS_SRC_COLOR = gl.ONE_MINUS_SRC_COLOR +_G.GL_TEXTURE9 = gl.TEXTURE9 +_G.GL_STENCIL_TEST = gl.STENCIL_TEST +_G.GL_COLOR_WRITEMASK = gl.COLOR_WRITEMASK +_G.GL_DEPTH_COMPONENT = gl.DEPTH_COMPONENT +_G.GL_STENCIL_INDEX8 = gl.STENCIL_INDEX8 +_G.GL_VERTEX_ATTRIB_ARRAY_TYPE = gl.VERTEX_ATTRIB_ARRAY_TYPE +_G.GL_FLOAT_VEC2 = gl.FLOAT_VEC2 +_G.GL_BLUE_BITS = gl.BLUE_BITS +_G.GL_VERTEX_SHADER = gl.VERTEX_SHADER +_G.GL_SUBPIXEL_BITS = gl.SUBPIXEL_BITS +_G.GL_STENCIL_WRITEMASK = gl.STENCIL_WRITEMASK +_G.GL_FLOAT_VEC4 = gl.FLOAT_VEC4 +_G.GL_TEXTURE17 = gl.TEXTURE17 +_G.GL_ONE_MINUS_CONSTANT_ALPHA = gl.ONE_MINUS_CONSTANT_ALPHA +_G.GL_TEXTURE15 = gl.TEXTURE15 +_G.GL_TEXTURE14 = gl.TEXTURE14 +_G.GL_TEXTURE13 = gl.TEXTURE13 +_G.GL_SAMPLES = gl.SAMPLES +_G.GL_TEXTURE11 = gl.TEXTURE11 +_G.GL_TEXTURE10 = gl.TEXTURE10 +_G.GL_FUNC_SUBTRACT = gl.FUNC_SUBTRACT +_G.GL_STENCIL_BUFFER_BIT = gl.STENCIL_BUFFER_BIT +_G.GL_TEXTURE19 = gl.TEXTURE19 +_G.GL_TEXTURE18 = gl.TEXTURE18 +_G.GL_NEAREST_MIPMAP_NEAREST = gl.NEAREST_MIPMAP_NEAREST +_G.GL_SHORT = gl.SHORT +_G.GL_RENDERBUFFER_BINDING = gl.RENDERBUFFER_BINDING +_G.GL_REPEAT = gl.REPEAT +_G.GL_TEXTURE_MIN_FILTER = gl.TEXTURE_MIN_FILTER +_G.GL_RED_BITS = gl.RED_BITS +_G.GL_FRONT_FACE = gl.FRONT_FACE +_G.GL_BLEND_COLOR = gl.BLEND_COLOR +_G.GL_MIRRORED_REPEAT = gl.MIRRORED_REPEAT +_G.GL_INT_VEC4 = gl.INT_VEC4 +_G.GL_MAX_CUBE_MAP_TEXTURE_SIZE = gl.MAX_CUBE_MAP_TEXTURE_SIZE +_G.GL_RENDERBUFFER_BLUE_SIZE = gl.RENDERBUFFER_BLUE_SIZE +_G.GL_SAMPLE_COVERAGE = gl.SAMPLE_COVERAGE +_G.GL_SRC_ALPHA = gl.SRC_ALPHA +_G.GL_FUNC_REVERSE_SUBTRACT = gl.FUNC_REVERSE_SUBTRACT +_G.GL_DEPTH_WRITEMASK = gl.DEPTH_WRITEMASK +_G.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT = gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT +_G.GL_POLYGON_OFFSET_FILL = gl.POLYGON_OFFSET_FILL +_G.GL_STENCIL_FUNC = gl.STENCIL_FUNC +_G.GL_REPLACE = gl.REPLACE +_G.GL_LUMINANCE_ALPHA = gl.LUMINANCE_ALPHA +_G.GL_DEPTH_RANGE = gl.DEPTH_RANGE +_G.GL_FASTEST = gl.FASTEST +_G.GL_STENCIL_FAIL = gl.STENCIL_FAIL +_G.GL_UNSIGNED_SHORT = gl.UNSIGNED_SHORT +_G.GL_RENDERBUFFER_HEIGHT = gl.RENDERBUFFER_HEIGHT +_G.GL_STENCIL_BACK_FAIL = gl.STENCIL_BACK_FAIL +_G.GL_BLEND_SRC_RGB = gl.BLEND_SRC_RGB +_G.GL_TEXTURE3 = gl.TEXTURE3 +_G.GL_RENDERBUFFER = gl.RENDERBUFFER +_G.GL_RGB5_A1 = gl.RGB5_A1 +_G.GL_RENDERBUFFER_ALPHA_SIZE = gl.RENDERBUFFER_ALPHA_SIZE +_G.GL_RENDERBUFFER_STENCIL_SIZE = gl.RENDERBUFFER_STENCIL_SIZE +_G.GL_NOTEQUAL = gl.NOTEQUAL +_G.GL_BLEND_DST_RGB = gl.BLEND_DST_RGB +_G.GL_FRONT_AND_BACK = gl.FRONT_AND_BACK +_G.GL_TEXTURE_BINDING_CUBE_MAP = gl.TEXTURE_BINDING_CUBE_MAP +_G.GL_MAX_RENDERBUFFER_SIZE = gl.MAX_RENDERBUFFER_SIZE +_G.GL_ZERO = gl.ZERO +_G.GL_TEXTURE0 = gl.TEXTURE0 +_G.GL_SAMPLE_ALPHA_TO_COVERAGE = gl.SAMPLE_ALPHA_TO_COVERAGE +_G.GL_BUFFER_USAGE = gl.BUFFER_USAGE +_G.GL_ACTIVE_TEXTURE = gl.ACTIVE_TEXTURE +_G.GL_BYTE = gl.BYTE +_G.GL_CW = gl.CW +_G.GL_DYNAMIC_DRAW = gl.DYNAMIC_DRAW +_G.GL_RENDERBUFFER_RED_SIZE = gl.RENDERBUFFER_RED_SIZE +_G.GL_FALSE = gl.FALSE +_G.GL_GREATER = gl.GREATER +_G.GL_RGBA4 = gl.RGBA4 +_G.GL_VALIDATE_STATUS = gl.VALIDATE_STATUS +_G.GL_STENCIL_BITS = gl.STENCIL_BITS +_G.GL_RGB = gl.RGB +_G.GL_INT = gl.INT +_G.GL_DEPTH_FUNC = gl.DEPTH_FUNC +_G.GL_SAMPLER_2D = gl.SAMPLER_2D +_G.GL_NICEST = gl.NICEST +_G.GL_MAX_VIEWPORT_DIMS = gl.MAX_VIEWPORT_DIMS +_G.GL_CULL_FACE = gl.CULL_FACE +_G.GL_INT_VEC3 = gl.INT_VEC3 +_G.GL_ALIASED_POINT_SIZE_RANGE = gl.ALIASED_POINT_SIZE_RANGE +_G.GL_INVALID_ENUM = gl.INVALID_ENUM +_G.GL_INVERT = gl.INVERT +_G.GL_CULL_FACE_MODE = gl.CULL_FACE_MODE +_G.GL_TEXTURE8 = gl.TEXTURE8 +_G.GL_VERTEX_ATTRIB_ARRAY_POINTER = gl.VERTEX_ATTRIB_ARRAY_POINTER +_G.GL_TEXTURE_WRAP_S = gl.TEXTURE_WRAP_S +_G.GL_VERTEX_ATTRIB_ARRAY_STRIDE = gl.VERTEX_ATTRIB_ARRAY_STRIDE +_G.GL_LINES = gl.LINES +_G.GL_EQUAL = gl.EQUAL +_G.GL_LINE_LOOP = gl.LINE_LOOP +_G.GL_TEXTURE_WRAP_T = gl.TEXTURE_WRAP_T +_G.GL_DEPTH_BUFFER_BIT = gl.DEPTH_BUFFER_BIT +_G.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS +_G.GL_SHADER_TYPE = gl.SHADER_TYPE +_G.GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME = gl.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME +_G.GL_TEXTURE_CUBE_MAP_NEGATIVE_X = gl.TEXTURE_CUBE_MAP_NEGATIVE_X +_G.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y = gl.TEXTURE_CUBE_MAP_NEGATIVE_Y +_G.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z = gl.TEXTURE_CUBE_MAP_NEGATIVE_Z +_G.GL_DECR = gl.DECR +_G.GL_DELETE_STATUS = gl.DELETE_STATUS +_G.GL_DEPTH_BITS = gl.DEPTH_BITS +_G.GL_INCR = gl.INCR +_G.GL_SAMPLE_COVERAGE_VALUE = gl.SAMPLE_COVERAGE_VALUE +_G.GL_ALPHA_BITS = gl.ALPHA_BITS +_G.GL_FLOAT_MAT2 = gl.FLOAT_MAT2 +_G.GL_LINE_STRIP = gl.LINE_STRIP +_G.GL_SHADER_SOURCE_LENGTH = gl.SHADER_SOURCE_LENGTH +_G.GL_INVALID_VALUE = gl.INVALID_VALUE +_G.GL_NEVER = gl.NEVER +_G.GL_INCR_WRAP = gl.INCR_WRAP +_G.GL_BLEND_EQUATION_ALPHA = gl.BLEND_EQUATION_ALPHA +_G.GL_TEXTURE_MAG_FILTER = gl.TEXTURE_MAG_FILTER +_G.GL_POINTS = gl.POINTS +_G.GL_COLOR_ATTACHMENT0 = gl.COLOR_ATTACHMENT0 +_G.GL_RGBA = gl.RGBA +_G.GL_SRC_ALPHA_SATURATE = gl.SRC_ALPHA_SATURATE +_G.GL_SAMPLER_CUBE = gl.SAMPLER_CUBE +_G.GL_FRAMEBUFFER = gl.FRAMEBUFFER +_G.GL_TEXTURE_CUBE_MAP = gl.TEXTURE_CUBE_MAP +_G.GL_SAMPLE_BUFFERS = gl.SAMPLE_BUFFERS +_G.GL_LINEAR = gl.LINEAR +_G.GL_LINEAR_MIPMAP_NEAREST = gl.LINEAR_MIPMAP_NEAREST +_G.GL_ACTIVE_UNIFORM_MAX_LENGTH = gl.ACTIVE_UNIFORM_MAX_LENGTH +_G.GL_STENCIL_BACK_REF = gl.STENCIL_BACK_REF +_G.GL_ELEMENT_ARRAY_BUFFER = gl.ELEMENT_ARRAY_BUFFER +_G.GL_CLAMP_TO_EDGE = gl.CLAMP_TO_EDGE +_G.GL_TRIANGLE_STRIP = gl.TRIANGLE_STRIP +_G.GL_CONSTANT_COLOR = gl.CONSTANT_COLOR +_G.GL_COMPILE_STATUS = gl.COMPILE_STATUS +_G.GL_RENDERBUFFER_GREEN_SIZE = gl.RENDERBUFFER_GREEN_SIZE +_G.GL_UNSIGNED_INT = gl.UNSIGNED_INT +_G.GL_DEPTH_CLEAR_VALUE = gl.DEPTH_CLEAR_VALUE +_G.GL_ALIASED_LINE_WIDTH_RANGE = gl.ALIASED_LINE_WIDTH_RANGE +_G.GL_SHADING_LANGUAGE_VERSION = gl.SHADING_LANGUAGE_VERSION +_G.GL_FRAMEBUFFER_UNSUPPORTED = gl.FRAMEBUFFER_UNSUPPORTED +_G.GL_INFO_LOG_LENGTH = gl.INFO_LOG_LENGTH +_G.GL_STENCIL_PASS_DEPTH_PASS = gl.STENCIL_PASS_DEPTH_PASS +_G.GL_STENCIL_VALUE_MASK = gl.STENCIL_VALUE_MASK +_G.GL_ALWAYS = gl.ALWAYS +_G.GL_MAX_TEXTURE_SIZE = gl.MAX_TEXTURE_SIZE +_G.GL_FLOAT = gl.FLOAT +_G.GL_FRAMEBUFFER_BINDING = gl.FRAMEBUFFER_BINDING +_G.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT +_G.GL_TRIANGLE_FAN = gl.TRIANGLE_FAN +_G.GL_INVALID_FRAMEBUFFER_OPERATION = gl.INVALID_FRAMEBUFFER_OPERATION +_G.GL_TEXTURE_2D = gl.TEXTURE_2D +_G.GL_ALPHA = gl.ALPHA +_G.GL_CURRENT_VERTEX_ATTRIB = gl.CURRENT_VERTEX_ATTRIB +_G.GL_SCISSOR_TEST = gl.SCISSOR_TEST +_G.GL_TRIANGLES = gl.TRIANGLES + +_G.CCControlEventTouchDown = cc.CONTROL_EVENTTYPE_TOUCH_DOWN +_G.CCControlEventTouchDragInside = cc.CONTROL_EVENTTYPE_DRAG_INSIDE +_G.CCControlEventTouchDragOutside = cc.CONTROL_EVENTTYPE_DRAG_OUTSIDE +_G.CCControlEventTouchDragEnter = cc.CONTROL_EVENTTYPE_DRAG_ENTER +_G.CCControlEventTouchDragExit = cc.CONTROL_EVENTTYPE_DRAG_EXIT +_G.CCControlEventTouchUpInside = cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE +_G.CCControlEventTouchUpOutside = cc.CONTROL_EVENTTYPE_TOUCH_UP_OUTSIDE +_G.CCControlEventTouchCancel = cc.CONTROL_EVENTTYPE_TOUCH_CANCEL +_G.CCControlEventValueChanged = cc.CONTROL_EVENTTYPE_VALUE_CHANGED +_G.CCControlStateNormal = cc.CONTROL_STATE_NORMAL +_G.CCControlStateHighlighted = cc.CONTROL_STATE_HIGH_LIGHTED +_G.CCControlStateDisabled = cc.CONTROL_STATE_DISABLED +_G.CCControlStateSelected = cc.CONTROL_STATE_SELECTED + +_G.kWebSocketScriptHandlerOpen = cc.WEBSOCKET_OPEN +_G.kWebSocketScriptHandlerMessage = cc.WEBSOCKET_MESSAGE +_G.kWebSocketScriptHandlerClose = cc.WEBSOCKET_CLOSE +_G.kWebSocketScriptHandlerError = cc.WEBSOCKET_ERROR + +_G.kStateConnecting = cc.WEBSOCKET_STATE_CONNECTING +_G.kStateOpen = cc.WEBSOCKET_STATE_OPEN +_G.kStateClosing = cc.WEBSOCKET_STATE_CLOSING +_G.kStateClosed = cc.WEBSOCKET_STATE_CLOSED + +_G.LAYOUT_COLOR_NONE = ccui.LayoutBackGroundColorType.none +_G.LAYOUT_COLOR_SOLID = ccui.LayoutBackGroundColorType.solid +_G.LAYOUT_COLOR_GRADIENT = ccui.LayoutBackGroundColorType.gradient + +_G.LAYOUT_ABSOLUTE = ccui.LayoutType.absolute +_G.LAYOUT_LINEAR_VERTICAL = ccui.LayoutType.linearVertical +_G.LAYOUT_LINEAR_HORIZONTAL = ccui.LayoutType.linearHorizontal +_G.LAYOUT_RELATIVE = ccui.LayoutType.relative + +_G.BRIGHT_NONE = ccui.BrightStyle.none +_G.BRIGHT_NORMAL = ccui.BrightStyle.normal +_G.BRIGHT_HIGHLIGHT = ccui.BrightStyle.highlight + +_G.WidgetTypeWidget = ccui.WidgetType.widget +_G.WidgetTypeContainer = ccui.WidgetType.container + +_G.UI_TEX_TYPE_LOCAL = ccui.TextureResType.UI_TEX_TYPE_LOCAL +_G.UI_TEX_TYPE_PLIST = ccui.TextureResType.UI_TEX_TYPE_PLIST + +_G.TOUCH_EVENT_BEGAN = ccui.TouchEventType.began +_G.TOUCH_EVENT_MOVED = ccui.TouchEventType.moved +_G.TOUCH_EVENT_ENDED = ccui.TouchEventType.ended +_G.TOUCH_EVENT_CANCELED = ccui.TouchEventType.canceled + +_G.SIZE_ABSOLUTE = ccui.SizeType.absolute +_G.SIZE_PERCENT = ccui.SizeType.percent + +_G.POSITION_ABSOLUTE = ccui.PositionType.absolute +_G.POSITION_PERCENT = ccui.PositionType.percent + +_G.CHECKBOX_STATE_EVENT_SELECTED = ccui.CheckBoxEventType.selected +_G.CHECKBOX_STATE_EVENT_UNSELECTED = ccui.CheckBoxEventType.unselected + +_G.CHECKBOX_STATE_EVENT_SELECTED = ccui.CheckBoxEventType.selected +_G.CHECKBOX_STATE_EVENT_UNSELECTED = ccui.CheckBoxEventType.unselected + +_G.LoadingBarTypeLeft = ccui.LoadingBarType.left +_G.LoadingBarTypeRight = ccui.LoadingBarType.right + +_G.LoadingBarTypeRight = ccui.SliderEventType.percent_changed + +_G.TEXTFIELD_EVENT_ATTACH_WITH_IME = ccui.TextFiledEventType.attach_with_ime +_G.TEXTFIELD_EVENT_DETACH_WITH_IME = ccui.TextFiledEventType.detach_with_ime +_G.TEXTFIELD_EVENT_INSERT_TEXT = ccui.TextFiledEventType.insert_text +_G.TEXTFIELD_EVENT_DELETE_BACKWARD = ccui.TextFiledEventType.delete_backward + +_G.SCROLLVIEW_EVENT_SCROLL_TO_TOP = ccui.ScrollViewDir.none +_G.SCROLLVIEW_DIR_VERTICAL = ccui.ScrollViewDir.vertical +_G.SCROLLVIEW_DIR_HORIZONTAL = ccui.ScrollViewDir.horizontal +_G.SCROLLVIEW_DIR_BOTH = ccui.ScrollViewDir.both + +_G.SCROLLVIEW_EVENT_SCROLL_TO_TOP = ccui.ScrollviewEventType.SCROLL_TO_TOP +_G.SCROLLVIEW_EVENT_SCROLL_TO_BOTTOM = ccui.ScrollviewEventType.SCROLL_TO_BOTTOM +_G.SCROLLVIEW_EVENT_SCROLL_TO_LEFT = ccui.ScrollviewEventType.SCROLL_TO_LEFT +_G.SCROLLVIEW_EVENT_SCROLL_TO_RIGHT = ccui.ScrollviewEventType.SCROLL_TO_RIGHT +_G.SCROLLVIEW_EVENT_SCROLLING = ccui.ScrollviewEventType.SCROLLING +_G.SCROLLVIEW_EVENT_BOUNCE_TOP = ccui.ScrollviewEventType.BOUNCE_TOP +_G.SCROLLVIEW_EVENT_BOUNCE_BOTTOM = ccui.ScrollviewEventType.BOUNCE_BOTTOM +_G.SCROLLVIEW_EVENT_BOUNCE_LEFT = ccui.ScrollviewEventType.BOUNCE_LEFT +_G.SCROLLVIEW_EVENT_BOUNCE_RIGHT = ccui.ScrollviewEventType.BOUNCE_RIGHT + +_G.PAGEVIEW_EVENT_TURNING = ccui.PageViewEventType.turning + +_G.PAGEVIEW_TOUCHLEFT = ccui.PVTouchDir.touch_left +_G.PAGEVIEW_TOUCHRIGHT = ccui.PVTouchDir.touch_right + +_G.LISTVIEW_DIR_NONE = ccui.ListViewDirection.none +_G.LISTVIEW_DIR_VERTICAL = ccui.ListViewDirection.vertical +_G.LISTVIEW_DIR_HORIZONTAL = ccui.ListViewDirection.horizontal + +_G.LISTVIEW_MOVE_DIR_NONE = ccui.ListViewMoveDirection.none +_G.LISTVIEW_MOVE_DIR_UP = ccui.ListViewMoveDirection.up +_G.LISTVIEW_MOVE_DIR_DOWN = ccui.ListViewMoveDirection.down +_G.LISTVIEW_MOVE_DIR_LEFT = ccui.ListViewMoveDirection.left +_G.LISTVIEW_MOVE_DIR_RIGHT = ccui.ListViewMoveDirection.right + +_G.LISTVIEW_EVENT_INIT_CHILD = ccui.ListViewEventType.init_child +_G.LISTVIEW_EVENT_UPDATE_CHILD = ccui.ListViewEventType.update_child + +_G.LAYOUT_PARAMETER_NONE = ccui.LayoutParameterType.none +_G.LAYOUT_PARAMETER_LINEAR = ccui.LayoutParameterType.linear +_G.LAYOUT_PARAMETER_RELATIVE = ccui.LayoutParameterType.relative + +_G.kCCScrollViewDirectionHorizontal = cc.SCROLLVIEW_DIRECTION_HORIZONTAL +_G.kCCScrollViewDirectionVertical = cc.SCROLLVIEW_DIRECTION_VERTICAL +_G.kCCTableViewFillTopDown = cc.TABLEVIEW_FILL_TOPDOWN +_G.kCCTableViewFillBottomUp = cc.TABLEVIEW_FILL_BOTTOMUP diff --git a/cocos/scripting/lua-bindings/script/DeprecatedOpenglEnum.lua b/cocos/scripting/lua-bindings/script/DeprecatedOpenglEnum.lua new file mode 100644 index 0000000000..1235e8dcc3 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/DeprecatedOpenglEnum.lua @@ -0,0 +1,288 @@ +-- This is the DeprecatedEnum + +DeprecatedClass = {} or DeprecatedClass + +_G.GL_RENDERBUFFER_INTERNAL_FORMAT = gl.RENDERBUFFER_INTERNAL_FORMAT +_G.GL_LINE_WIDTH = gl.LINE_WIDTH +_G.GL_CONSTANT_ALPHA = gl.CONSTANT_ALPHA +_G.GL_BLEND_SRC_ALPHA = gl.BLEND_SRC_ALPHA +_G.GL_GREEN_BITS = gl.GREEN_BITS +_G.GL_STENCIL_REF = gl.STENCIL_REF +_G.GL_ONE_MINUS_SRC_ALPHA = gl.ONE_MINUS_SRC_ALPHA +_G.GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = gl.FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE +_G.GL_CCW = gl.CCW +_G.GL_MAX_TEXTURE_IMAGE_UNITS = gl.MAX_TEXTURE_IMAGE_UNITS +_G.GL_BACK = gl.BACK +_G.GL_ACTIVE_ATTRIBUTES = gl.ACTIVE_ATTRIBUTES +_G.GL_TEXTURE_CUBE_MAP_POSITIVE_X = gl.TEXTURE_CUBE_MAP_POSITIVE_X +_G.GL_STENCIL_BACK_VALUE_MASK = gl.STENCIL_BACK_VALUE_MASK +_G.GL_TEXTURE_CUBE_MAP_POSITIVE_Z = gl.TEXTURE_CUBE_MAP_POSITIVE_Z +_G.GL_ONE = gl.ONE +_G.GL_TRUE = gl.TRUE +_G.GL_TEXTURE12 = gl.TEXTURE12 +_G.GL_LINK_STATUS = gl.LINK_STATUS +_G.GL_BLEND = gl.BLEND +_G.GL_LESS = gl.LESS +_G.GL_TEXTURE16 = gl.TEXTURE16 +_G.GL_BOOL_VEC2 = gl.BOOL_VEC2 +_G.GL_KEEP = gl.KEEP +_G.GL_DST_COLOR = gl.DST_COLOR +_G.GL_VERTEX_ATTRIB_ARRAY_ENABLED = gl.VERTEX_ATTRIB_ARRAY_ENABLED +_G.GL_EXTENSIONS = gl.EXTENSIONS +_G.GL_FRONT = gl.FRONT +_G.GL_DST_ALPHA = gl.DST_ALPHA +_G.GL_ATTACHED_SHADERS = gl.ATTACHED_SHADERS +_G.GL_STENCIL_BACK_FUNC = gl.STENCIL_BACK_FUNC +_G.GL_ONE_MINUS_DST_COLOR = gl.ONE_MINUS_DST_COLOR +_G.GL_BLEND_EQUATION = gl.BLEND_EQUATION +_G.GL_RENDERBUFFER_DEPTH_SIZE = gl.RENDERBUFFER_DEPTH_SIZE +_G.GL_PACK_ALIGNMENT = gl.PACK_ALIGNMENT +_G.GL_VENDOR = gl.VENDOR +_G.GL_NEAREST_MIPMAP_LINEAR = gl.NEAREST_MIPMAP_LINEAR +_G.GL_TEXTURE_CUBE_MAP_POSITIVE_Y = gl.TEXTURE_CUBE_MAP_POSITIVE_Y +_G.GL_NEAREST = gl.NEAREST +_G.GL_RENDERBUFFER_WIDTH = gl.RENDERBUFFER_WIDTH +_G.GL_ARRAY_BUFFER_BINDING = gl.ARRAY_BUFFER_BINDING +_G.GL_ARRAY_BUFFER = gl.ARRAY_BUFFER +_G.GL_LEQUAL = gl.LEQUAL +_G.GL_VERSION = gl.VERSION +_G.GL_COLOR_CLEAR_VALUE = gl.COLOR_CLEAR_VALUE +_G.GL_RENDERER = gl.RENDERER +_G.GL_STENCIL_BACK_PASS_DEPTH_PASS = gl.STENCIL_BACK_PASS_DEPTH_PASS +_G.GL_STENCIL_BACK_PASS_DEPTH_FAIL = gl.STENCIL_BACK_PASS_DEPTH_FAIL +_G.GL_STENCIL_BACK_WRITEMASK = gl.STENCIL_BACK_WRITEMASK +_G.GL_BOOL = gl.BOOL +_G.GL_VIEWPORT = gl.VIEWPORT +_G.GL_FRAGMENT_SHADER = gl.FRAGMENT_SHADER +_G.GL_LUMINANCE = gl.LUMINANCE +_G.GL_DECR_WRAP = gl.DECR_WRAP +_G.GL_FUNC_ADD = gl.FUNC_ADD +_G.GL_ONE_MINUS_DST_ALPHA = gl.ONE_MINUS_DST_ALPHA +_G.GL_OUT_OF_MEMORY = gl.OUT_OF_MEMORY +_G.GL_BOOL_VEC4 = gl.BOOL_VEC4 +_G.GL_POLYGON_OFFSET_FACTOR = gl.POLYGON_OFFSET_FACTOR +_G.GL_STATIC_DRAW = gl.STATIC_DRAW +_G.GL_DITHER = gl.DITHER +_G.GL_TEXTURE31 = gl.TEXTURE31 +_G.GL_TEXTURE30 = gl.TEXTURE30 +_G.GL_UNSIGNED_BYTE = gl.UNSIGNED_BYTE +_G.GL_DEPTH_COMPONENT16 = gl.DEPTH_COMPONENT16 +_G.GL_TEXTURE23 = gl.TEXTURE23 +_G.GL_DEPTH_TEST = gl.DEPTH_TEST +_G.GL_STENCIL_PASS_DEPTH_FAIL = gl.STENCIL_PASS_DEPTH_FAIL +_G.GL_BOOL_VEC3 = gl.BOOL_VEC3 +_G.GL_POLYGON_OFFSET_UNITS = gl.POLYGON_OFFSET_UNITS +_G.GL_TEXTURE_BINDING_2D = gl.TEXTURE_BINDING_2D +_G.GL_TEXTURE21 = gl.TEXTURE21 +_G.GL_UNPACK_ALIGNMENT = gl.UNPACK_ALIGNMENT +_G.GL_DONT_CARE = gl.DONT_CARE +_G.GL_BUFFER_SIZE = gl.BUFFER_SIZE +_G.GL_FLOAT_MAT3 = gl.FLOAT_MAT3 +_G.GL_UNSIGNED_SHORT_5_6_5 = gl.UNSIGNED_SHORT_5_6_5 +_G.GL_INT_VEC2 = gl.INT_VEC2 +_G.GL_UNSIGNED_SHORT_4_4_4_4 = gl.UNSIGNED_SHORT_4_4_4_4 +_G.GL_NONE = gl.NONE +_G.GL_BLEND_DST_ALPHA = gl.BLEND_DST_ALPHA +_G.GL_VERTEX_ATTRIB_ARRAY_SIZE = gl.VERTEX_ATTRIB_ARRAY_SIZE +_G.GL_SRC_COLOR = gl.SRC_COLOR +_G.GL_COMPRESSED_TEXTURE_FORMATS = gl.COMPRESSED_TEXTURE_FORMATS +_G.GL_STENCIL_ATTACHMENT = gl.STENCIL_ATTACHMENT +_G.GL_MAX_VERTEX_ATTRIBS = gl.MAX_VERTEX_ATTRIBS +_G.GL_NUM_COMPRESSED_TEXTURE_FORMATS = gl.NUM_COMPRESSED_TEXTURE_FORMATS +_G.GL_BLEND_EQUATION_RGB = gl.BLEND_EQUATION_RGB +_G.GL_TEXTURE = gl.TEXTURE +_G.GL_LINEAR_MIPMAP_LINEAR = gl.LINEAR_MIPMAP_LINEAR +_G.GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING = gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING +_G.GL_CURRENT_PROGRAM = gl.CURRENT_PROGRAM +_G.GL_COLOR_BUFFER_BIT = gl.COLOR_BUFFER_BIT +_G.GL_TEXTURE20 = gl.TEXTURE20 +_G.GL_ACTIVE_ATTRIBUTE_MAX_LENGTH = gl.ACTIVE_ATTRIBUTE_MAX_LENGTH +_G.GL_TEXTURE28 = gl.TEXTURE28 +_G.GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE = gl.FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE +_G.GL_TEXTURE22 = gl.TEXTURE22 +_G.GL_ELEMENT_ARRAY_BUFFER_BINDING = gl.ELEMENT_ARRAY_BUFFER_BINDING +_G.GL_STREAM_DRAW = gl.STREAM_DRAW +_G.GL_SCISSOR_BOX = gl.SCISSOR_BOX +_G.GL_TEXTURE26 = gl.TEXTURE26 +_G.GL_TEXTURE27 = gl.TEXTURE27 +_G.GL_TEXTURE24 = gl.TEXTURE24 +_G.GL_TEXTURE25 = gl.TEXTURE25 +_G.GL_NO_ERROR = gl.NO_ERROR +_G.GL_TEXTURE29 = gl.TEXTURE29 +_G.GL_FLOAT_MAT4 = gl.FLOAT_MAT4 +_G.GL_VERTEX_ATTRIB_ARRAY_NORMALIZED = gl.VERTEX_ATTRIB_ARRAY_NORMALIZED +_G.GL_SAMPLE_COVERAGE_INVERT = gl.SAMPLE_COVERAGE_INVERT +_G.GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL = gl.FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL +_G.GL_FLOAT_VEC3 = gl.FLOAT_VEC3 +_G.GL_STENCIL_CLEAR_VALUE = gl.STENCIL_CLEAR_VALUE +_G.GL_UNSIGNED_SHORT_5_5_5_1 = gl.UNSIGNED_SHORT_5_5_5_1 +_G.GL_ACTIVE_UNIFORMS = gl.ACTIVE_UNIFORMS +_G.GL_INVALID_OPERATION = gl.INVALID_OPERATION +_G.GL_DEPTH_ATTACHMENT = gl.DEPTH_ATTACHMENT +_G.GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS = gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS +_G.GL_FRAMEBUFFER_COMPLETE = gl.FRAMEBUFFER_COMPLETE +_G.GL_ONE_MINUS_CONSTANT_COLOR = gl.ONE_MINUS_CONSTANT_COLOR +_G.GL_TEXTURE2 = gl.TEXTURE2 +_G.GL_TEXTURE1 = gl.TEXTURE1 +_G.GL_GEQUAL = gl.GEQUAL +_G.GL_TEXTURE7 = gl.TEXTURE7 +_G.GL_TEXTURE6 = gl.TEXTURE6 +_G.GL_TEXTURE5 = gl.TEXTURE5 +_G.GL_TEXTURE4 = gl.TEXTURE4 +_G.GL_GENERATE_MIPMAP_HINT = gl.GENERATE_MIPMAP_HINT +_G.GL_ONE_MINUS_SRC_COLOR = gl.ONE_MINUS_SRC_COLOR +_G.GL_TEXTURE9 = gl.TEXTURE9 +_G.GL_STENCIL_TEST = gl.STENCIL_TEST +_G.GL_COLOR_WRITEMASK = gl.COLOR_WRITEMASK +_G.GL_DEPTH_COMPONENT = gl.DEPTH_COMPONENT +_G.GL_STENCIL_INDEX8 = gl.STENCIL_INDEX8 +_G.GL_VERTEX_ATTRIB_ARRAY_TYPE = gl.VERTEX_ATTRIB_ARRAY_TYPE +_G.GL_FLOAT_VEC2 = gl.FLOAT_VEC2 +_G.GL_BLUE_BITS = gl.BLUE_BITS +_G.GL_VERTEX_SHADER = gl.VERTEX_SHADER +_G.GL_SUBPIXEL_BITS = gl.SUBPIXEL_BITS +_G.GL_STENCIL_WRITEMASK = gl.STENCIL_WRITEMASK +_G.GL_FLOAT_VEC4 = gl.FLOAT_VEC4 +_G.GL_TEXTURE17 = gl.TEXTURE17 +_G.GL_ONE_MINUS_CONSTANT_ALPHA = gl.ONE_MINUS_CONSTANT_ALPHA +_G.GL_TEXTURE15 = gl.TEXTURE15 +_G.GL_TEXTURE14 = gl.TEXTURE14 +_G.GL_TEXTURE13 = gl.TEXTURE13 +_G.GL_SAMPLES = gl.SAMPLES +_G.GL_TEXTURE11 = gl.TEXTURE11 +_G.GL_TEXTURE10 = gl.TEXTURE10 +_G.GL_FUNC_SUBTRACT = gl.FUNC_SUBTRACT +_G.GL_STENCIL_BUFFER_BIT = gl.STENCIL_BUFFER_BIT +_G.GL_TEXTURE19 = gl.TEXTURE19 +_G.GL_TEXTURE18 = gl.TEXTURE18 +_G.GL_NEAREST_MIPMAP_NEAREST = gl.NEAREST_MIPMAP_NEAREST +_G.GL_SHORT = gl.SHORT +_G.GL_RENDERBUFFER_BINDING = gl.RENDERBUFFER_BINDING +_G.GL_REPEAT = gl.REPEAT +_G.GL_TEXTURE_MIN_FILTER = gl.TEXTURE_MIN_FILTER +_G.GL_RED_BITS = gl.RED_BITS +_G.GL_FRONT_FACE = gl.FRONT_FACE +_G.GL_BLEND_COLOR = gl.BLEND_COLOR +_G.GL_MIRRORED_REPEAT = gl.MIRRORED_REPEAT +_G.GL_INT_VEC4 = gl.INT_VEC4 +_G.GL_MAX_CUBE_MAP_TEXTURE_SIZE = gl.MAX_CUBE_MAP_TEXTURE_SIZE +_G.GL_RENDERBUFFER_BLUE_SIZE = gl.RENDERBUFFER_BLUE_SIZE +_G.GL_SAMPLE_COVERAGE = gl.SAMPLE_COVERAGE +_G.GL_SRC_ALPHA = gl.SRC_ALPHA +_G.GL_FUNC_REVERSE_SUBTRACT = gl.FUNC_REVERSE_SUBTRACT +_G.GL_DEPTH_WRITEMASK = gl.DEPTH_WRITEMASK +_G.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT = gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT +_G.GL_POLYGON_OFFSET_FILL = gl.POLYGON_OFFSET_FILL +_G.GL_STENCIL_FUNC = gl.STENCIL_FUNC +_G.GL_REPLACE = gl.REPLACE +_G.GL_LUMINANCE_ALPHA = gl.LUMINANCE_ALPHA +_G.GL_DEPTH_RANGE = gl.DEPTH_RANGE +_G.GL_FASTEST = gl.FASTEST +_G.GL_STENCIL_FAIL = gl.STENCIL_FAIL +_G.GL_UNSIGNED_SHORT = gl.UNSIGNED_SHORT +_G.GL_RENDERBUFFER_HEIGHT = gl.RENDERBUFFER_HEIGHT +_G.GL_STENCIL_BACK_FAIL = gl.STENCIL_BACK_FAIL +_G.GL_BLEND_SRC_RGB = gl.BLEND_SRC_RGB +_G.GL_TEXTURE3 = gl.TEXTURE3 +_G.GL_RENDERBUFFER = gl.RENDERBUFFER +_G.GL_RGB5_A1 = gl.RGB5_A1 +_G.GL_RENDERBUFFER_ALPHA_SIZE = gl.RENDERBUFFER_ALPHA_SIZE +_G.GL_RENDERBUFFER_STENCIL_SIZE = gl.RENDERBUFFER_STENCIL_SIZE +_G.GL_NOTEQUAL = gl.NOTEQUAL +_G.GL_BLEND_DST_RGB = gl.BLEND_DST_RGB +_G.GL_FRONT_AND_BACK = gl.FRONT_AND_BACK +_G.GL_TEXTURE_BINDING_CUBE_MAP = gl.TEXTURE_BINDING_CUBE_MAP +_G.GL_MAX_RENDERBUFFER_SIZE = gl.MAX_RENDERBUFFER_SIZE +_G.GL_ZERO = gl.ZERO +_G.GL_TEXTURE0 = gl.TEXTURE0 +_G.GL_SAMPLE_ALPHA_TO_COVERAGE = gl.SAMPLE_ALPHA_TO_COVERAGE +_G.GL_BUFFER_USAGE = gl.BUFFER_USAGE +_G.GL_ACTIVE_TEXTURE = gl.ACTIVE_TEXTURE +_G.GL_BYTE = gl.BYTE +_G.GL_CW = gl.CW +_G.GL_DYNAMIC_DRAW = gl.DYNAMIC_DRAW +_G.GL_RENDERBUFFER_RED_SIZE = gl.RENDERBUFFER_RED_SIZE +_G.GL_FALSE = gl.FALSE +_G.GL_GREATER = gl.GREATER +_G.GL_RGBA4 = gl.RGBA4 +_G.GL_VALIDATE_STATUS = gl.VALIDATE_STATUS +_G.GL_STENCIL_BITS = gl.STENCIL_BITS +_G.GL_RGB = gl.RGB +_G.GL_INT = gl.INT +_G.GL_DEPTH_FUNC = gl.DEPTH_FUNC +_G.GL_SAMPLER_2D = gl.SAMPLER_2D +_G.GL_NICEST = gl.NICEST +_G.GL_MAX_VIEWPORT_DIMS = gl.MAX_VIEWPORT_DIMS +_G.GL_CULL_FACE = gl.CULL_FACE +_G.GL_INT_VEC3 = gl.INT_VEC3 +_G.GL_ALIASED_POINT_SIZE_RANGE = gl.ALIASED_POINT_SIZE_RANGE +_G.GL_INVALID_ENUM = gl.INVALID_ENUM +_G.GL_INVERT = gl.INVERT +_G.GL_CULL_FACE_MODE = gl.CULL_FACE_MODE +_G.GL_TEXTURE8 = gl.TEXTURE8 +_G.GL_VERTEX_ATTRIB_ARRAY_POINTER = gl.VERTEX_ATTRIB_ARRAY_POINTER +_G.GL_TEXTURE_WRAP_S = gl.TEXTURE_WRAP_S +_G.GL_VERTEX_ATTRIB_ARRAY_STRIDE = gl.VERTEX_ATTRIB_ARRAY_STRIDE +_G.GL_LINES = gl.LINES +_G.GL_EQUAL = gl.EQUAL +_G.GL_LINE_LOOP = gl.LINE_LOOP +_G.GL_TEXTURE_WRAP_T = gl.TEXTURE_WRAP_T +_G.GL_DEPTH_BUFFER_BIT = gl.DEPTH_BUFFER_BIT +_G.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS = gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS +_G.GL_SHADER_TYPE = gl.SHADER_TYPE +_G.GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME = gl.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME +_G.GL_TEXTURE_CUBE_MAP_NEGATIVE_X = gl.TEXTURE_CUBE_MAP_NEGATIVE_X +_G.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y = gl.TEXTURE_CUBE_MAP_NEGATIVE_Y +_G.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z = gl.TEXTURE_CUBE_MAP_NEGATIVE_Z +_G.GL_DECR = gl.DECR +_G.GL_DELETE_STATUS = gl.DELETE_STATUS +_G.GL_DEPTH_BITS = gl.DEPTH_BITS +_G.GL_INCR = gl.INCR +_G.GL_SAMPLE_COVERAGE_VALUE = gl.SAMPLE_COVERAGE_VALUE +_G.GL_ALPHA_BITS = gl.ALPHA_BITS +_G.GL_FLOAT_MAT2 = gl.FLOAT_MAT2 +_G.GL_LINE_STRIP = gl.LINE_STRIP +_G.GL_SHADER_SOURCE_LENGTH = gl.SHADER_SOURCE_LENGTH +_G.GL_INVALID_VALUE = gl.INVALID_VALUE +_G.GL_NEVER = gl.NEVER +_G.GL_INCR_WRAP = gl.INCR_WRAP +_G.GL_BLEND_EQUATION_ALPHA = gl.BLEND_EQUATION_ALPHA +_G.GL_TEXTURE_MAG_FILTER = gl.TEXTURE_MAG_FILTER +_G.GL_POINTS = gl.POINTS +_G.GL_COLOR_ATTACHMENT0 = gl.COLOR_ATTACHMENT0 +_G.GL_RGBA = gl.RGBA +_G.GL_SRC_ALPHA_SATURATE = gl.SRC_ALPHA_SATURATE +_G.GL_SAMPLER_CUBE = gl.SAMPLER_CUBE +_G.GL_FRAMEBUFFER = gl.FRAMEBUFFER +_G.GL_TEXTURE_CUBE_MAP = gl.TEXTURE_CUBE_MAP +_G.GL_SAMPLE_BUFFERS = gl.SAMPLE_BUFFERS +_G.GL_LINEAR = gl.LINEAR +_G.GL_LINEAR_MIPMAP_NEAREST = gl.LINEAR_MIPMAP_NEAREST +_G.GL_ACTIVE_UNIFORM_MAX_LENGTH = gl.ACTIVE_UNIFORM_MAX_LENGTH +_G.GL_STENCIL_BACK_REF = gl.STENCIL_BACK_REF +_G.GL_ELEMENT_ARRAY_BUFFER = gl.ELEMENT_ARRAY_BUFFER +_G.GL_CLAMP_TO_EDGE = gl.CLAMP_TO_EDGE +_G.GL_TRIANGLE_STRIP = gl.TRIANGLE_STRIP +_G.GL_CONSTANT_COLOR = gl.CONSTANT_COLOR +_G.GL_COMPILE_STATUS = gl.COMPILE_STATUS +_G.GL_RENDERBUFFER_GREEN_SIZE = gl.RENDERBUFFER_GREEN_SIZE +_G.GL_UNSIGNED_INT = gl.UNSIGNED_INT +_G.GL_DEPTH_CLEAR_VALUE = gl.DEPTH_CLEAR_VALUE +_G.GL_ALIASED_LINE_WIDTH_RANGE = gl.ALIASED_LINE_WIDTH_RANGE +_G.GL_SHADING_LANGUAGE_VERSION = gl.SHADING_LANGUAGE_VERSION +_G.GL_FRAMEBUFFER_UNSUPPORTED = gl.FRAMEBUFFER_UNSUPPORTED +_G.GL_INFO_LOG_LENGTH = gl.INFO_LOG_LENGTH +_G.GL_STENCIL_PASS_DEPTH_PASS = gl.STENCIL_PASS_DEPTH_PASS +_G.GL_STENCIL_VALUE_MASK = gl.STENCIL_VALUE_MASK +_G.GL_ALWAYS = gl.ALWAYS +_G.GL_MAX_TEXTURE_SIZE = gl.MAX_TEXTURE_SIZE +_G.GL_FLOAT = gl.FLOAT +_G.GL_FRAMEBUFFER_BINDING = gl.FRAMEBUFFER_BINDING +_G.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT +_G.GL_TRIANGLE_FAN = gl.TRIANGLE_FAN +_G.GL_INVALID_FRAMEBUFFER_OPERATION = gl.INVALID_FRAMEBUFFER_OPERATION +_G.GL_TEXTURE_2D = gl.TEXTURE_2D +_G.GL_ALPHA = gl.ALPHA +_G.GL_CURRENT_VERTEX_ATTRIB = gl.CURRENT_VERTEX_ATTRIB +_G.GL_SCISSOR_TEST = gl.SCISSOR_TEST +_G.GL_TRIANGLES = gl.TRIANGLES diff --git a/cocos/scripting/lua-bindings/script/DrawPrimitives.lua b/cocos/scripting/lua-bindings/script/DrawPrimitives.lua new file mode 100644 index 0000000000..1171947577 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/DrawPrimitives.lua @@ -0,0 +1,383 @@ +local dp_initialized = false +local dp_shader = nil +local dp_colorLocation = -1 +local dp_color = { 1.0, 1.0, 1.0, 1.0 } +local dp_pointSizeLocation = -1 +local dp_pointSize = 1.0 + +local SHADER_NAME_POSITION_U_COLOR = "ShaderPosition_uColor" + +local targetPlatform = CCApplication:getInstance():getTargetPlatform() + +local function lazy_init() + if not dp_initialized then + dp_shader = CCShaderCache:getInstance():getProgram(SHADER_NAME_POSITION_U_COLOR) + --dp_shader:retain() + if nil ~= dp_shader then + dp_colorLocation = gl.getUniformLocation( dp_shader:getProgram(), "u_color") + dp_pointSizeLocation = gl.getUniformLocation( dp_shader:getProgram(), "u_pointSize") + dp_Initialized = true + end + end + + if nil == dp_shader then + print("Error:dp_shader is nil!") + return false + end + + return true +end + +local function setDrawProperty() + gl.glEnableVertexAttribs( CCConstants.VERTEX_ATTRIB_FLAG_POSITION ) + dp_shader:use() + dp_shader:setUniformsForBuiltins() + dp_shader:setUniformLocationWith4fv(dp_colorLocation, dp_color, 1) +end + +function ccDrawInit() + lazy_init() +end + +function ccDrawFree() + dp_initialized = false +end + +function ccDrawColor4f(r,g,b,a) + dp_color[1] = r + dp_color[2] = g + dp_color[3] = b + dp_color[4] = a +end + +function ccPointSize(pointSize) + dp_pointSize = pointSize * CCDirector:getInstance():getContentScaleFactor() +end + +function ccDrawColor4B(r,g,b,a) + dp_color[1] = r / 255.0 + dp_color[2] = g / 255.0 + dp_color[3] = b / 255.0 + dp_color[4] = a / 255.0 +end + +function ccDrawPoint(point) + if not lazy_init() then + return + end + + local vertexBuffer = { } + + local function initBuffer() + vertexBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + local vertices = { point.x,point.y} + gl.bufferData(gl.ARRAY_BUFFER,2,vertices,gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + initBuffer() + + setDrawProperty() + + dp_shader:setUniformLocationWith1f(dp_pointSizeLocation, dp_pointSize) + + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.POINTS,0,1) + gl.bindBuffer(gl.ARRAY_BUFFER,0) +end + +function ccDrawPoints(points,numOfPoint) + if not lazy_init() then + return + end + + local vertexBuffer = {} + local i = 1 + + local function initBuffer() + vertexBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id) + local vertices = {} + for i = 1, numOfPoint do + vertices[2 * i - 1] = points[i].x + vertices[2 * i] = points[i].y + end + gl.bufferData(gl.ARRAY_BUFFER, numOfPoint * 2, vertices, gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + initBuffer() + + setDrawProperty() + + dp_shader:setUniformLocationWith1f(dp_pointSizeLocation, dp_pointSize) + + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.POINTS,0,numOfPoint) + gl.bindBuffer(gl.ARRAY_BUFFER,0) +end + +function ccDrawLine(origin,destination) + if not lazy_init() then + return + end + + local vertexBuffer = {} + + local function initBuffer() + vertexBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id) + local vertices = { origin.x, origin.y, destination.x, destination.y} + gl.bufferData(gl.ARRAY_BUFFER,4,vertices,gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + initBuffer() + + setDrawProperty() + + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.LINES ,0,2) + gl.bindBuffer(gl.ARRAY_BUFFER,0) +end + +function ccDrawPoly(points,numOfPoints,closePolygon) + if not lazy_init() then + return + end + + local vertexBuffer = {} + local i = 1 + + local function initBuffer() + vertexBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id) + local vertices = {} + for i = 1, numOfPoints do + vertices[2 * i - 1] = points[i].x + vertices[2 * i] = points[i].y + end + gl.bufferData(gl.ARRAY_BUFFER, numOfPoints * 2, vertices, gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + initBuffer() + + setDrawProperty() + + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + if closePolygon then + gl.drawArrays(gl.LINE_LOOP , 0, numOfPoints) + else + gl.drawArrays(gl.LINE_STRIP, 0, numOfPoints) + end + gl.bindBuffer(gl.ARRAY_BUFFER,0) +end + +function ccDrawSolidPoly(points,numOfPoints,color) + if not lazy_init() then + return + end + + local vertexBuffer = {} + local i = 1 + + local function initBuffer() + vertexBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id) + local vertices = {} + for i = 1, numOfPoints do + vertices[2 * i - 1] = points[i].x + vertices[2 * i] = points[i].y + + end + gl.bufferData(gl.ARRAY_BUFFER, numOfPoints * 2, vertices, gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + initBuffer() + + gl.glEnableVertexAttribs( CCConstants.VERTEX_ATTRIB_FLAG_POSITION ) + dp_shader:use() + dp_shader:setUniformsForBuiltins() + dp_shader:setUniformLocationWith4fv(dp_colorLocation, color, 1) + + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.TRIANGLE_FAN , 0, numOfPoints) + gl.bindBuffer(gl.ARRAY_BUFFER,0) +end + +function ccDrawRect(origin,destination) + ccDrawLine(CCPoint:__call(origin.x, origin.y), CCPoint:__call(destination.x, origin.y)) + ccDrawLine(CCPoint:__call(destination.x, origin.y), CCPoint:__call(destination.x, destination.y)) + ccDrawLine(CCPoint:__call(destination.x, destination.y), CCPoint:__call(origin.x, destination.y)) + ccDrawLine(CCPoint:__call(origin.x, destination.y), CCPoint:__call(origin.x, origin.y)) +end + +function ccDrawSolidRect( origin,destination,color ) + local vertices = { origin, CCPoint:__call(destination.x, origin.y) , destination, CCPoint:__call(origin.x, destination.y) } + ccDrawSolidPoly(vertices,4,color) +end + +function ccDrawCircleScale( center, radius, angle, segments,drawLineToCenter,scaleX,scaleY) + if not lazy_init() then + return + end + + local additionalSegment = 1 + if drawLineToCenter then + additionalSegment = additionalSegment + 1 + end + + local vertexBuffer = { } + + local function initBuffer() + local coef = 2.0 * math.pi / segments + local i = 1 + local vertices = {} + vertexBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id) + for i = 1, segments + 1 do + local rads = (i - 1) * coef + local j = radius * math.cos(rads + angle) * scaleX + center.x + local k = radius * math.sin(rads + angle) * scaleY + center.y + vertices[i * 2 - 1] = j + vertices[i * 2] = k + end + vertices[(segments + 2) * 2 - 1] = center.x + vertices[(segments + 2) * 2] = center.y + + gl.bufferData(gl.ARRAY_BUFFER, (segments + 2) * 2, vertices, gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + initBuffer() + + setDrawProperty() + + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.LINE_STRIP , 0, segments + additionalSegment) + gl.bindBuffer(gl.ARRAY_BUFFER,0) +end + +function ccDrawCircle(center, radius, angle, segments, drawLineToCenter) + ccDrawCircleScale(center, radius, angle, segments, drawLineToCenter, 1.0, 1.0) +end + +function ccDrawSolidCircle(center, radius, angle, segments,scaleX,scaleY) + if not lazy_init() then + return + end + + local vertexBuffer = { } + + local function initBuffer() + local coef = 2.0 * math.pi / segments + local i = 1 + local vertices = {} + vertexBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id) + for i = 1, segments + 1 do + local rads = (i - 1) * coef + local j = radius * math.cos(rads + angle) * scaleX + center.x + local k = radius * math.sin(rads + angle) * scaleY + center.y + vertices[i * 2 - 1] = j + vertices[i * 2] = k + end + vertices[(segments + 2) * 2 - 1] = center.x + vertices[(segments + 2) * 2] = center.y + + gl.bufferData(gl.ARRAY_BUFFER, (segments + 2) * 2, vertices, gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + initBuffer() + + setDrawProperty() + + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.TRIANGLE_FAN , 0, segments + 1) + gl.bindBuffer(gl.ARRAY_BUFFER,0) +end + +function ccDrawQuadBezier(origin, control, destination, segments) + if not lazy_init() then + return + end + + local vertexBuffer = { } + + local function initBuffer() + local vertices = { } + local i = 1 + local t = 0.0 + + for i = 1, segments do + vertices[2 * i - 1] = math.pow(1 - t,2) * origin.x + 2.0 * (1 - t) * t * control.x + t * t * destination.x + vertices[2 * i] = math.pow(1 - t,2) * origin.y + 2.0 * (1 - t) * t * control.y + t * t * destination.y + t = t + 1.0 / segments + end + + vertices[2 * (segments + 1) - 1] = destination.x + vertices[2 * (segments + 1)] = destination.y + + vertexBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id) + gl.bufferData(gl.ARRAY_BUFFER, (segments + 1) * 2, vertices, gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + initBuffer() + + setDrawProperty() + + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.LINE_STRIP , 0, segments + 1) + gl.bindBuffer(gl.ARRAY_BUFFER,0) +end + +function ccDrawCubicBezier(origin, control1, control2, destination, segments) + if not lazy_init then + return + end + + local vertexBuffer = { } + + local function initBuffer() + local vertices = { } + local t = 0 + local i = 1 + + for i = 1, segments do + vertices[2 * i - 1] = math.pow(1 - t,3) * origin.x + 3.0 * math.pow(1 - t, 2) * t * control1.x + 3.0 * (1 - t) * t * t * control2.x + t * t * t * destination.x + vertices[2 * i] = math.pow(1 - t,3) * origin.y + 3.0 * math.pow(1 - t, 2) * t * control1.y + 3.0 * (1 - t) * t * t * control2.y + t * t * t * destination.y + t = t + 1.0 / segments + end + + vertices[2 * (segments + 1) - 1] = destination.x + vertices[2 * (segments + 1)] = destination.y + + vertexBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer.buffer_id) + gl.bufferData(gl.ARRAY_BUFFER, (segments + 1) * 2, vertices, gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + initBuffer() + + setDrawProperty() + + gl.bindBuffer(gl.ARRAY_BUFFER,vertexBuffer.buffer_id) + gl.vertexAttribPointer(CCConstants.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.LINE_STRIP , 0, segments + 1) + gl.bindBuffer(gl.ARRAY_BUFFER,0) +end diff --git a/cocos/scripting/lua-bindings/script/GuiConstants.lua b/cocos/scripting/lua-bindings/script/GuiConstants.lua new file mode 100644 index 0000000000..028f8d9778 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/GuiConstants.lua @@ -0,0 +1,179 @@ +ccui = ccui or {} + +ccui.BrightStyle = +{ + none = -1, + normal = 0, + highlight = 1, +} + +ccui.WidgetType = +{ + widget = 0, --control + container = 1, --container +} + +ccui.TextureResType = +{ + localType = 0, + plistType = 1, +} + +ccui.TouchEventType = +{ + began = 0, + moved = 1, + ended = 2, + canceled = 3, +} + +ccui.SizeType = +{ + absolute = 0, + percent = 1, +} + +ccui.PositionType = { + absolute = 0, + percent = 1, +} + +ccui.CheckBoxEventType = +{ + selected = 0, + unselected = 1, +} + +ccui.TextFiledEventType = +{ + attach_with_ime = 0, + detach_with_ime = 1, + insert_text = 2, + delete_backward = 3, +} + +ccui.LayoutBackGroundColorType = +{ + none = 0, + solid = 1, + gradient = 2, +} + +ccui.LayoutType = +{ + absolute = 0, + linearVertical = 1, + linearHorizontal = 2, + relative = 3, +} + +ccui.LayoutParameterType = +{ + none = 0, + linear = 1, + relative = 2, +} + +ccui.LinearGravity = +{ + none = 0, + left = 1, + top = 2, + right = 3, + bottom = 4, + centerVertical = 5, + centerHorizontal = 6, +} + +ccui.RelativeAlign = +{ + alignNone = 0, + alignParentTopLeft = 1, + alignParentTopCenterHorizontal = 2, + alignParentTopRight = 3, + alignParentLeftCenterVertical = 4, + centerInParent = 5, + alignParentRightCenterVertical = 6, + alignParentLeftBottom = 7, + alignParentBottomCenterHorizontal = 8, + alignParentRightBottom = 9, + locationAboveLeftAlign = 10, + locationAboveCenter = 11, + locationAboveRightAlign = 12, + locationLeftOfTopAlign = 13, + locationLeftOfCenter = 14, + locationLeftOfBottomAlign = 15, + locationRightOfTopAlign = 16, + locationRightOfCenter = 17, + locationRightOfBottomAlign = 18, + locationBelowLeftAlign = 19, + locationBelowCenter = 20, + locationBelowRightAlign = 21, +} + +ccui.SliderEventType = {percentChanged = 0} + +ccui.LoadingBarType = { left = 0, right = 1} + +ccui.ScrollViewDir = { + none = 0, + vertical = 1, + horizontal = 2, + both = 3, +} + +ccui.ScrollViewMoveDir = { + none = 0, + up = 1, + down = 2, + left = 3, + right = 4, +} + +ccui.ScrollviewEventType = { + scrollToTop = 0, + scrollToBottom = 1, + scrollToLeft = 2, + scrollToRight = 3, + scrolling = 4, + bounceTop = 5, + bounceBottom = 6, + bounceLeft = 7, + bounceRight = 8, +} + +ccui.ListViewDirection = { + none = 0, + vertical = 1, + horizontal = 2, +} + +ccui.ListViewMoveDirection = { + none = 0, + up = 1, + down = 2, + left = 3, + right = 4, +} + +ccui.ListViewEventType = { + onsSelectedItem = 0, +} + +ccui.PageViewEventType = { + turning = 0, +} + +ccui.PVTouchDir = { + touchLeft = 0, + touchRight = 1, +} + +ccui.ListViewGravity = { + left = 0, + right = 1, + centerHorizontal = 2, + top = 3, + bottom = 4 , + centerVertical = 5, +} diff --git a/cocos/scripting/lua-bindings/script/Opengl.lua b/cocos/scripting/lua-bindings/script/Opengl.lua new file mode 100644 index 0000000000..4f70703093 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/Opengl.lua @@ -0,0 +1,299 @@ +require "OpenglConstants" + +gl = gl or {} + +--Create functions +function gl.createTexture() + local retTable = {} + retTable.texture_id = gl._createTexture() + return retTable +end + +function gl.createBuffer() + local retTable = {} + retTable.buffer_id = gl._createBuffer() + return retTable +end + +function gl.createRenderbuffer() + local retTable = {} + retTable.renderbuffer_id = gl._createRenderuffer() + return retTable +end + +function gl.createFramebuffer( ) + local retTable = {} + retTable.framebuffer_id = gl._createFramebuffer() + return retTable +end + +function gl.createProgram() + local retTable = {} + retTable.program_id = gl._createProgram() + return retTable +end + +function gl.createShader(shaderType) + local retTable = {} + retTable.shader_id = gl._createShader(shaderType) + return retTable +end + +--Delete Fun +function gl.deleteTexture(texture) + local texture_id = 0 + if "number" == type(texture) then + texture_id = texture + elseif "table" == type(texture) then + texture_id = texture.texture_id + end + gl._deleteTexture(texture_id) +end + +function gl.deleteBuffer(buffer) + local buffer_id = 0 + if "number" == type(buffer) then + buffer_id = buffer + elseif "table" == type(buffer) then + buffer_id = buffer.buffer_id + end + gl._deleteBuffer(buffer_id) +end + +function gl.deleteRenderbuffer(buffer) + local renderbuffer_id = 0 + if "number" == type(buffer) then + renderbuffer_id = buffer + elseif "table" == type(buffer) then + renderbuffer_id = buffer.renderbuffer_id + end + gl._deleteRenderbuffer(renderbuffer_id) +end + +function gl.deleteFramebuffer(buffer) + local framebuffer_id = 0 + if "number" == type(buffer) then + framebuffer_id = buffer + elseif "table" == type(buffer) then + framebuffer_id = buffer.framebuffer_id + end + gl._deleteFramebuffer(framebuffer_id) +end + +function gl.deleteProgram( program ) + local program_id = 0 + if "number" == type(buffer) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + gl._deleteProgram(program_id) +end + +function gl.deleteShader(shader) + local shader_id = 0 + if "number" == type(shader) then + shader_id = shader + elseif "table" == type(shader) then + shader_id = shader.shader_id + end + + gl._deleteShader(shader_id) +end + +--Bind Related +function gl.bindTexture(target, texture) + local texture_id = 0 + if "number" == type(texture) then + texture_id = texture + elseif "table" == type(texture) then + texture_id = texture.texture_id + end + + gl._bindTexture(target,texture_id) +end + +function gl.bindBuffer( target,buffer ) + local buffer_id = 0 + if "number" == type(buffer) then + buffer_id = buffer + elseif "table" == type(buffer) then + buffer_id = buffer.buffer_id + end + + gl._bindBuffer(target, buffer_id) +end + +function gl.bindRenderBuffer(target, buffer) + local buffer_id = 0 + + if "number" == type(buffer) then + buffer_id = buffer; + elseif "table" == type(buffer) then + buffer_id = buffer.buffer_id + end + + gl._bindRenderbuffer(target, buffer_id) +end + +function gl.bindFramebuffer(target, buffer) + local buffer_id = 0 + + if "number" == type(buffer) then + buffer_id = buffer + elseif "table" == type(buffer) then + buffer_id = buffer.buffer_id + end + + gl._bindFramebuffer(target, buffer_id) +end + +--Uniform related +function gl.getUniform(program, location) + local program_id = 0 + local location_id = 0 + + if "number" == type(program) then + program_id = program + else + program_id = program.program_id + end + + if "number" == type(location) then + location_id = location + else + location_id = location.location_id + end + + return gl._getUniform(program_id, location_id) +end + +--shader related +function gl.compileShader(shader) + gl._compileShader( shader.shader_id) +end + +function gl.shaderSource(shader, source) + gl._shaderSource(shader.shader_id, source) +end + +function gl.getShaderParameter(shader, e) + return gl._getShaderParameter(shader.shader_id,e) +end + +function gl.getShaderInfoLog( shader ) + return gl._getShaderInfoLog(shader.shader_id) +end + +--program related +function gl.attachShader( program, shader ) + local program_id = 0 + + if "number" == type(program) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + gl._attachShader(program_id, shader.shader_id) +end + +function gl.linkProgram( program ) + local program_id = 0 + + if "number" == type(program) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + gl._linkProgram(program_id) +end + +function gl.getProgramParameter(program, e) + local program_id = 0 + + if "number" == type(program) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + return gl._getProgramParameter(program_id, e) +end + +function gl.useProgram(program) + local program_id = 0 + if "number" == type(program) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + gl._useProgram (program_id) +end + +function gl.getAttribLocation(program, name ) + local program_id = 0 + + if "number" == type(program) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + return gl._getAttribLocation(program_id, name) +end + +function gl.getUniformLocation( program, name ) + local program_id = 0 + + if "number" == type(program) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + return gl._getUniformLocation(program_id,name) +end + +function gl.getActiveAttrib( program, index ) + local program_id = 0 + if "number" == type(program) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + return gl._getActiveAttrib(program_id, index); +end + +function gl.getActiveUniform( program, index ) + local program_id = 0 + + if "number" == type(program) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + return gl._getActiveUniform(program_id, index) +end + +function gl.getAttachedShaders(program) + local program_id = 0 + + if "number" == type(program) then + program_id = program + elseif "table" == type(program) then + program_id = program.program_id + end + + return gl._getAttachedShaders(program_id) +end + +function gl.glNodeCreate() + return cc.GLNode:create() +end + diff --git a/cocos/scripting/lua-bindings/script/OpenglConstants.lua b/cocos/scripting/lua-bindings/script/OpenglConstants.lua new file mode 100644 index 0000000000..858d30a1cf --- /dev/null +++ b/cocos/scripting/lua-bindings/script/OpenglConstants.lua @@ -0,0 +1,824 @@ +--Encapsulate opengl constants. +gl = gl or {} + +gl.GCCSO_SHADER_BINARY_FJ = 0x9260 +gl._3DC_XY_AMD = 0x87fa +gl._3DC_X_AMD = 0x87f9 +gl.ACTIVE_ATTRIBUTES = 0x8b89 +gl.ACTIVE_ATTRIBUTE_MAX_LENGTH = 0x8b8a +gl.ACTIVE_PROGRAM_EXT = 0x8259 +gl.ACTIVE_TEXTURE = 0x84e0 +gl.ACTIVE_UNIFORMS = 0x8b86 +gl.ACTIVE_UNIFORM_MAX_LENGTH = 0x8b87 +gl.ALIASED_LINE_WIDTH_RANGE = 0x846e +gl.ALIASED_POINT_SIZE_RANGE = 0x846d +gl.ALL_COMPLETED_NV = 0x84f2 +gl.ALL_SHADER_BITS_EXT = 0xffffffff +gl.ALPHA = 0x1906 +gl.ALPHA16F_EXT = 0x881c +gl.ALPHA32F_EXT = 0x8816 +gl.ALPHA8_EXT = 0x803c +gl.ALPHA8_OES = 0x803c +gl.ALPHA_BITS = 0xd55 +gl.ALPHA_TEST_FUNC_QCOM = 0xbc1 +gl.ALPHA_TEST_QCOM = 0xbc0 +gl.ALPHA_TEST_REF_QCOM = 0xbc2 +gl.ALREADY_SIGNALED_APPLE = 0x911a +gl.ALWAYS = 0x207 +gl.AMD_compressed_3DC_texture = 0x1 +gl.AMD_compressed_ATC_texture = 0x1 +gl.AMD_performance_monitor = 0x1 +gl.AMD_program_binary_Z400 = 0x1 +gl.ANGLE_depth_texture = 0x1 +gl.ANGLE_framebuffer_blit = 0x1 +gl.ANGLE_framebuffer_multisample = 0x1 +gl.ANGLE_instanced_arrays = 0x1 +gl.ANGLE_pack_reverse_row_order = 0x1 +gl.ANGLE_program_binary = 0x1 +gl.ANGLE_texture_compression_dxt3 = 0x1 +gl.ANGLE_texture_compression_dxt5 = 0x1 +gl.ANGLE_texture_usage = 0x1 +gl.ANGLE_translated_shader_source = 0x1 +gl.ANY_SAMPLES_PASSED_CONSERVATIVE_EXT = 0x8d6a +gl.ANY_SAMPLES_PASSED_EXT = 0x8c2f +gl.APPLE_copy_texture_levels = 0x1 +gl.APPLE_framebuffer_multisample = 0x1 +gl.APPLE_rgb_422 = 0x1 +gl.APPLE_sync = 0x1 +gl.APPLE_texture_format_BGRA8888 = 0x1 +gl.APPLE_texture_max_level = 0x1 +gl.ARM_mali_program_binary = 0x1 +gl.ARM_mali_shader_binary = 0x1 +gl.ARM_rgba8 = 0x1 +gl.ARRAY_BUFFER = 0x8892 +gl.ARRAY_BUFFER_BINDING = 0x8894 +gl.ATC_RGBA_EXPLICIT_ALPHA_AMD = 0x8c93 +gl.ATC_RGBA_INTERPOLATED_ALPHA_AMD = 0x87ee +gl.ATC_RGB_AMD = 0x8c92 +gl.ATTACHED_SHADERS = 0x8b85 +gl.BACK = 0x405 +gl.BGRA8_EXT = 0x93a1 +gl.BGRA_EXT = 0x80e1 +gl.BGRA_IMG = 0x80e1 +gl.BINNING_CONTROL_HINT_QCOM = 0x8fb0 +gl.BLEND = 0xbe2 +gl.BLEND_COLOR = 0x8005 +gl.BLEND_DST_ALPHA = 0x80ca +gl.BLEND_DST_RGB = 0x80c8 +gl.BLEND_EQUATION = 0x8009 +gl.BLEND_EQUATION_ALPHA = 0x883d +gl.BLEND_EQUATION_RGB = 0x8009 +gl.BLEND_SRC_ALPHA = 0x80cb +gl.BLEND_SRC_RGB = 0x80c9 +gl.BLUE_BITS = 0xd54 +gl.BOOL = 0x8b56 +gl.BOOL_VEC2 = 0x8b57 +gl.BOOL_VEC3 = 0x8b58 +gl.BOOL_VEC4 = 0x8b59 +gl.BUFFER = 0x82e0 +gl.BUFFER_ACCESS_OES = 0x88bb +gl.BUFFER_MAPPED_OES = 0x88bc +gl.BUFFER_MAP_POINTER_OES = 0x88bd +gl.BUFFER_OBJECT_EXT = 0x9151 +gl.BUFFER_SIZE = 0x8764 +gl.BUFFER_USAGE = 0x8765 +gl.BYTE = 0x1400 +gl.CCW = 0x901 +gl.CLAMP_TO_BORDER_NV = 0x812d +gl.CLAMP_TO_EDGE = 0x812f +gl.COLOR_ATTACHMENT0 = 0x8ce0 +gl.COLOR_ATTACHMENT0_NV = 0x8ce0 +gl.COLOR_ATTACHMENT10_NV = 0x8cea +gl.COLOR_ATTACHMENT11_NV = 0x8ceb +gl.COLOR_ATTACHMENT12_NV = 0x8cec +gl.COLOR_ATTACHMENT13_NV = 0x8ced +gl.COLOR_ATTACHMENT14_NV = 0x8cee +gl.COLOR_ATTACHMENT15_NV = 0x8cef +gl.COLOR_ATTACHMENT1_NV = 0x8ce1 +gl.COLOR_ATTACHMENT2_NV = 0x8ce2 +gl.COLOR_ATTACHMENT3_NV = 0x8ce3 +gl.COLOR_ATTACHMENT4_NV = 0x8ce4 +gl.COLOR_ATTACHMENT5_NV = 0x8ce5 +gl.COLOR_ATTACHMENT6_NV = 0x8ce6 +gl.COLOR_ATTACHMENT7_NV = 0x8ce7 +gl.COLOR_ATTACHMENT8_NV = 0x8ce8 +gl.COLOR_ATTACHMENT9_NV = 0x8ce9 +gl.COLOR_ATTACHMENT_EXT = 0x90f0 +gl.COLOR_BUFFER_BIT = 0x4000 +gl.COLOR_BUFFER_BIT0_QCOM = 0x1 +gl.COLOR_BUFFER_BIT1_QCOM = 0x2 +gl.COLOR_BUFFER_BIT2_QCOM = 0x4 +gl.COLOR_BUFFER_BIT3_QCOM = 0x8 +gl.COLOR_BUFFER_BIT4_QCOM = 0x10 +gl.COLOR_BUFFER_BIT5_QCOM = 0x20 +gl.COLOR_BUFFER_BIT6_QCOM = 0x40 +gl.COLOR_BUFFER_BIT7_QCOM = 0x80 +gl.COLOR_CLEAR_VALUE = 0xc22 +gl.COLOR_EXT = 0x1800 +gl.COLOR_WRITEMASK = 0xc23 +gl.COMPARE_REF_TO_TEXTURE_EXT = 0x884e +gl.COMPILE_STATUS = 0x8b81 +gl.COMPRESSED_RGBA_ASTC_10x10_KHR = 0x93bb +gl.COMPRESSED_RGBA_ASTC_10x5_KHR = 0x93b8 +gl.COMPRESSED_RGBA_ASTC_10x6_KHR = 0x93b9 +gl.COMPRESSED_RGBA_ASTC_10x8_KHR = 0x93ba +gl.COMPRESSED_RGBA_ASTC_12x10_KHR = 0x93bc +gl.COMPRESSED_RGBA_ASTC_12x12_KHR = 0x93bd +gl.COMPRESSED_RGBA_ASTC_4x4_KHR = 0x93b0 +gl.COMPRESSED_RGBA_ASTC_5x4_KHR = 0x93b1 +gl.COMPRESSED_RGBA_ASTC_5x5_KHR = 0x93b2 +gl.COMPRESSED_RGBA_ASTC_6x5_KHR = 0x93b3 +gl.COMPRESSED_RGBA_ASTC_6x6_KHR = 0x93b4 +gl.COMPRESSED_RGBA_ASTC_8x5_KHR = 0x93b5 +gl.COMPRESSED_RGBA_ASTC_8x6_KHR = 0x93b6 +gl.COMPRESSED_RGBA_ASTC_8x8_KHR = 0x93b7 +gl.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG = 0x8c03 +gl.COMPRESSED_RGBA_PVRTC_2BPPV2_IMG = 0x9137 +gl.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG = 0x8c02 +gl.COMPRESSED_RGBA_PVRTC_4BPPV2_IMG = 0x9138 +gl.COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83f1 +gl.COMPRESSED_RGBA_S3TC_DXT3_ANGLE = 0x83f2 +gl.COMPRESSED_RGBA_S3TC_DXT5_ANGLE = 0x83f3 +gl.COMPRESSED_RGB_PVRTC_2BPPV1_IMG = 0x8c01 +gl.COMPRESSED_RGB_PVRTC_4BPPV1_IMG = 0x8c00 +gl.COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83f0 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR = 0x93db +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR = 0x93d8 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR = 0x93d9 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR = 0x93da +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR = 0x93dc +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR = 0x93dd +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR = 0x93d0 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR = 0x93d1 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR = 0x93d2 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR = 0x93d3 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR = 0x93d4 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR = 0x93d5 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR = 0x93d6 +gl.COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR = 0x93d7 +gl.COMPRESSED_SRGB_ALPHA_S3TC_DXT1_NV = 0x8c4d +gl.COMPRESSED_SRGB_ALPHA_S3TC_DXT3_NV = 0x8c4e +gl.COMPRESSED_SRGB_ALPHA_S3TC_DXT5_NV = 0x8c4f +gl.COMPRESSED_SRGB_S3TC_DXT1_NV = 0x8c4c +gl.COMPRESSED_TEXTURE_FORMATS = 0x86a3 +gl.CONDITION_SATISFIED_APPLE = 0x911c +gl.CONSTANT_ALPHA = 0x8003 +gl.CONSTANT_COLOR = 0x8001 +gl.CONTEXT_FLAG_DEBUG_BIT = 0x2 +gl.CONTEXT_ROBUST_ACCESS_EXT = 0x90f3 +gl.COUNTER_RANGE_AMD = 0x8bc1 +gl.COUNTER_TYPE_AMD = 0x8bc0 +gl.COVERAGE_ALL_FRAGMENTS_NV = 0x8ed5 +gl.COVERAGE_ATTACHMENT_NV = 0x8ed2 +gl.COVERAGE_AUTOMATIC_NV = 0x8ed7 +gl.COVERAGE_BUFFERS_NV = 0x8ed3 +gl.COVERAGE_BUFFER_BIT_NV = 0x8000 +gl.COVERAGE_COMPONENT4_NV = 0x8ed1 +gl.COVERAGE_COMPONENT_NV = 0x8ed0 +gl.COVERAGE_EDGE_FRAGMENTS_NV = 0x8ed6 +gl.COVERAGE_SAMPLES_NV = 0x8ed4 +gl.CPU_OPTIMIZED_QCOM = 0x8fb1 +gl.CULL_FACE = 0xb44 +gl.CULL_FACE_MODE = 0xb45 +gl.CURRENT_PROGRAM = 0x8b8d +gl.CURRENT_QUERY_EXT = 0x8865 +gl.CURRENT_VERTEX_ATTRIB = 0x8626 +gl.CW = 0x900 +gl.DEBUG_CALLBACK_FUNCTION = 0x8244 +gl.DEBUG_CALLBACK_USER_PARAM = 0x8245 +gl.DEBUG_GROUP_STACK_DEPTH = 0x826d +gl.DEBUG_LOGGED_MESSAGES = 0x9145 +gl.DEBUG_NEXT_LOGGED_MESSAGE_LENGTH = 0x8243 +gl.DEBUG_OUTPUT = 0x92e0 +gl.DEBUG_OUTPUT_SYNCHRONOUS = 0x8242 +gl.DEBUG_SEVERITY_HIGH = 0x9146 +gl.DEBUG_SEVERITY_LOW = 0x9148 +gl.DEBUG_SEVERITY_MEDIUM = 0x9147 +gl.DEBUG_SEVERITY_NOTIFICATION = 0x826b +gl.DEBUG_SOURCE_API = 0x8246 +gl.DEBUG_SOURCE_APPLICATION = 0x824a +gl.DEBUG_SOURCE_OTHER = 0x824b +gl.DEBUG_SOURCE_SHADER_COMPILER = 0x8248 +gl.DEBUG_SOURCE_THIRD_PARTY = 0x8249 +gl.DEBUG_SOURCE_WINDOW_SYSTEM = 0x8247 +gl.DEBUG_TYPE_DEPRECATED_BEHAVIOR = 0x824d +gl.DEBUG_TYPE_ERROR = 0x824c +gl.DEBUG_TYPE_MARKER = 0x8268 +gl.DEBUG_TYPE_OTHER = 0x8251 +gl.DEBUG_TYPE_PERFORMANCE = 0x8250 +gl.DEBUG_TYPE_POP_GROUP = 0x826a +gl.DEBUG_TYPE_PORTABILITY = 0x824f +gl.DEBUG_TYPE_PUSH_GROUP = 0x8269 +gl.DEBUG_TYPE_UNDEFINED_BEHAVIOR = 0x824e +gl.DECR = 0x1e03 +gl.DECR_WRAP = 0x8508 +gl.DELETE_STATUS = 0x8b80 +gl.DEPTH24_STENCIL8_OES = 0x88f0 +gl.DEPTH_ATTACHMENT = 0x8d00 +gl.DEPTH_BITS = 0xd56 +gl.DEPTH_BUFFER_BIT = 0x100 +gl.DEPTH_BUFFER_BIT0_QCOM = 0x100 +gl.DEPTH_BUFFER_BIT1_QCOM = 0x200 +gl.DEPTH_BUFFER_BIT2_QCOM = 0x400 +gl.DEPTH_BUFFER_BIT3_QCOM = 0x800 +gl.DEPTH_BUFFER_BIT4_QCOM = 0x1000 +gl.DEPTH_BUFFER_BIT5_QCOM = 0x2000 +gl.DEPTH_BUFFER_BIT6_QCOM = 0x4000 +gl.DEPTH_BUFFER_BIT7_QCOM = 0x8000 +gl.DEPTH_CLEAR_VALUE = 0xb73 +gl.DEPTH_COMPONENT = 0x1902 +gl.DEPTH_COMPONENT16 = 0x81a5 +gl.DEPTH_COMPONENT16_NONLINEAR_NV = 0x8e2c +gl.DEPTH_COMPONENT16_OES = 0x81a5 +gl.DEPTH_COMPONENT24_OES = 0x81a6 +gl.DEPTH_COMPONENT32_OES = 0x81a7 +gl.DEPTH_EXT = 0x1801 +gl.DEPTH_FUNC = 0xb74 +gl.DEPTH_RANGE = 0xb70 +gl.DEPTH_STENCIL_OES = 0x84f9 +gl.DEPTH_TEST = 0xb71 +gl.DEPTH_WRITEMASK = 0xb72 +gl.DITHER = 0xbd0 +gl.DMP_shader_binary = 0x1 +gl.DONT_CARE = 0x1100 +gl.DRAW_BUFFER0_NV = 0x8825 +gl.DRAW_BUFFER10_NV = 0x882f +gl.DRAW_BUFFER11_NV = 0x8830 +gl.DRAW_BUFFER12_NV = 0x8831 +gl.DRAW_BUFFER13_NV = 0x8832 +gl.DRAW_BUFFER14_NV = 0x8833 +gl.DRAW_BUFFER15_NV = 0x8834 +gl.DRAW_BUFFER1_NV = 0x8826 +gl.DRAW_BUFFER2_NV = 0x8827 +gl.DRAW_BUFFER3_NV = 0x8828 +gl.DRAW_BUFFER4_NV = 0x8829 +gl.DRAW_BUFFER5_NV = 0x882a +gl.DRAW_BUFFER6_NV = 0x882b +gl.DRAW_BUFFER7_NV = 0x882c +gl.DRAW_BUFFER8_NV = 0x882d +gl.DRAW_BUFFER9_NV = 0x882e +gl.DRAW_BUFFER_EXT = 0xc01 +gl.DRAW_FRAMEBUFFER_ANGLE = 0x8ca9 +gl.DRAW_FRAMEBUFFER_APPLE = 0x8ca9 +gl.DRAW_FRAMEBUFFER_BINDING_ANGLE = 0x8ca6 +gl.DRAW_FRAMEBUFFER_BINDING_APPLE = 0x8ca6 +gl.DRAW_FRAMEBUFFER_BINDING_NV = 0x8ca6 +gl.DRAW_FRAMEBUFFER_NV = 0x8ca9 +gl.DST_ALPHA = 0x304 +gl.DST_COLOR = 0x306 +gl.DYNAMIC_DRAW = 0x88e8 +gl.ELEMENT_ARRAY_BUFFER = 0x8893 +gl.ELEMENT_ARRAY_BUFFER_BINDING = 0x8895 +gl.EQUAL = 0x202 +gl.ES_VERSION_2_0 = 0x1 +gl.ETC1_RGB8_OES = 0x8d64 +gl.ETC1_SRGB8_NV = 0x88ee +gl.EXTENSIONS = 0x1f03 +gl.EXT_blend_minmax = 0x1 +gl.EXT_color_buffer_half_float = 0x1 +gl.EXT_debug_label = 0x1 +gl.EXT_debug_marker = 0x1 +gl.EXT_discard_framebuffer = 0x1 +gl.EXT_map_buffer_range = 0x1 +gl.EXT_multi_draw_arrays = 0x1 +gl.EXT_multisampled_render_to_texture = 0x1 +gl.EXT_multiview_draw_buffers = 0x1 +gl.EXT_occlusion_query_boolean = 0x1 +gl.EXT_read_format_bgra = 0x1 +gl.EXT_robustness = 0x1 +gl.EXT_sRGB = 0x1 +gl.EXT_separate_shader_objects = 0x1 +gl.EXT_shader_framebuffer_fetch = 0x1 +gl.EXT_shader_texture_lod = 0x1 +gl.EXT_shadow_samplers = 0x1 +gl.EXT_texture_compression_dxt1 = 0x1 +gl.EXT_texture_filter_anisotropic = 0x1 +gl.EXT_texture_format_BGRA8888 = 0x1 +gl.EXT_texture_rg = 0x1 +gl.EXT_texture_storage = 0x1 +gl.EXT_texture_type_2_10_10_10_REV = 0x1 +gl.EXT_unpack_subimage = 0x1 +gl.FALSE = 0x0 +gl.FASTEST = 0x1101 +gl.FENCE_CONDITION_NV = 0x84f4 +gl.FENCE_STATUS_NV = 0x84f3 +gl.FIXED = 0x140c +gl.FJ_shader_binary_GCCSO = 0x1 +gl.FLOAT = 0x1406 +gl.FLOAT_MAT2 = 0x8b5a +gl.FLOAT_MAT3 = 0x8b5b +gl.FLOAT_MAT4 = 0x8b5c +gl.FLOAT_VEC2 = 0x8b50 +gl.FLOAT_VEC3 = 0x8b51 +gl.FLOAT_VEC4 = 0x8b52 +gl.FRAGMENT_SHADER = 0x8b30 +gl.FRAGMENT_SHADER_BIT_EXT = 0x2 +gl.FRAGMENT_SHADER_DERIVATIVE_HINT_OES = 0x8b8b +gl.FRAGMENT_SHADER_DISCARDS_SAMPLES_EXT = 0x8a52 +gl.FRAMEBUFFER = 0x8d40 +gl.FRAMEBUFFER_ATTACHMENT_ANGLE = 0x93a3 +gl.FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT = 0x8210 +gl.FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT = 0x8211 +gl.FRAMEBUFFER_ATTACHMENT_OBJECT_NAME = 0x8cd1 +gl.FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = 0x8cd0 +gl.FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_OES = 0x8cd4 +gl.FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE = 0x8cd3 +gl.FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL = 0x8cd2 +gl.FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT = 0x8d6c +gl.FRAMEBUFFER_BINDING = 0x8ca6 +gl.FRAMEBUFFER_COMPLETE = 0x8cd5 +gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT = 0x8cd6 +gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS = 0x8cd9 +gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = 0x8cd7 +gl.FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_ANGLE = 0x8d56 +gl.FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_APPLE = 0x8d56 +gl.FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT = 0x8d56 +gl.FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_IMG = 0x9134 +gl.FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_NV = 0x8d56 +gl.FRAMEBUFFER_UNDEFINED_OES = 0x8219 +gl.FRAMEBUFFER_UNSUPPORTED = 0x8cdd +gl.FRONT = 0x404 +gl.FRONT_AND_BACK = 0x408 +gl.FRONT_FACE = 0xb46 +gl.FUNC_ADD = 0x8006 +gl.FUNC_REVERSE_SUBTRACT = 0x800b +gl.FUNC_SUBTRACT = 0x800a +gl.GENERATE_MIPMAP_HINT = 0x8192 +gl.GEQUAL = 0x206 +gl.GPU_OPTIMIZED_QCOM = 0x8fb2 +gl.GREATER = 0x204 +gl.GREEN_BITS = 0xd53 +gl.GUILTY_CONTEXT_RESET_EXT = 0x8253 +gl.HALF_FLOAT_OES = 0x8d61 +gl.HIGH_FLOAT = 0x8df2 +gl.HIGH_INT = 0x8df5 +gl.IMG_multisampled_render_to_texture = 0x1 +gl.IMG_program_binary = 0x1 +gl.IMG_read_format = 0x1 +gl.IMG_shader_binary = 0x1 +gl.IMG_texture_compression_pvrtc = 0x1 +gl.IMG_texture_compression_pvrtc2 = 0x1 +gl.IMPLEMENTATION_COLOR_READ_FORMAT = 0x8b9b +gl.IMPLEMENTATION_COLOR_READ_TYPE = 0x8b9a +gl.INCR = 0x1e02 +gl.INCR_WRAP = 0x8507 +gl.INFO_LOG_LENGTH = 0x8b84 +gl.INNOCENT_CONTEXT_RESET_EXT = 0x8254 +gl.INT = 0x1404 +gl.INT_10_10_10_2_OES = 0x8df7 +gl.INT_VEC2 = 0x8b53 +gl.INT_VEC3 = 0x8b54 +gl.INT_VEC4 = 0x8b55 +gl.INVALID_ENUM = 0x500 +gl.INVALID_FRAMEBUFFER_OPERATION = 0x506 +gl.INVALID_OPERATION = 0x502 +gl.INVALID_VALUE = 0x501 +gl.INVERT = 0x150a +gl.KEEP = 0x1e00 +gl.KHR_debug = 0x1 +gl.KHR_texture_compression_astc_ldr = 0x1 +gl.LEQUAL = 0x203 +gl.LESS = 0x201 +gl.LINEAR = 0x2601 +gl.LINEAR_MIPMAP_LINEAR = 0x2703 +gl.LINEAR_MIPMAP_NEAREST = 0x2701 +gl.LINES = 0x1 +gl.LINE_LOOP = 0x2 +gl.LINE_STRIP = 0x3 +gl.LINE_WIDTH = 0xb21 +gl.LINK_STATUS = 0x8b82 +gl.LOSE_CONTEXT_ON_RESET_EXT = 0x8252 +gl.LOW_FLOAT = 0x8df0 +gl.LOW_INT = 0x8df3 +gl.LUMINANCE = 0x1909 +gl.LUMINANCE16F_EXT = 0x881e +gl.LUMINANCE32F_EXT = 0x8818 +gl.LUMINANCE4_ALPHA4_OES = 0x8043 +gl.LUMINANCE8_ALPHA8_EXT = 0x8045 +gl.LUMINANCE8_ALPHA8_OES = 0x8045 +gl.LUMINANCE8_EXT = 0x8040 +gl.LUMINANCE8_OES = 0x8040 +gl.LUMINANCE_ALPHA = 0x190a +gl.LUMINANCE_ALPHA16F_EXT = 0x881f +gl.LUMINANCE_ALPHA32F_EXT = 0x8819 +gl.MALI_PROGRAM_BINARY_ARM = 0x8f61 +gl.MALI_SHADER_BINARY_ARM = 0x8f60 +gl.MAP_FLUSH_EXPLICIT_BIT_EXT = 0x10 +gl.MAP_INVALIDATE_BUFFER_BIT_EXT = 0x8 +gl.MAP_INVALIDATE_RANGE_BIT_EXT = 0x4 +gl.MAP_READ_BIT_EXT = 0x1 +gl.MAP_UNSYNCHRONIZED_BIT_EXT = 0x20 +gl.MAP_WRITE_BIT_EXT = 0x2 +gl.MAX_3D_TEXTURE_SIZE_OES = 0x8073 +gl.MAX_COLOR_ATTACHMENTS_NV = 0x8cdf +gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS = 0x8b4d +gl.MAX_CUBE_MAP_TEXTURE_SIZE = 0x851c +gl.MAX_DEBUG_GROUP_STACK_DEPTH = 0x826c +gl.MAX_DEBUG_LOGGED_MESSAGES = 0x9144 +gl.MAX_DEBUG_MESSAGE_LENGTH = 0x9143 +gl.MAX_DRAW_BUFFERS_NV = 0x8824 +gl.MAX_EXT = 0x8008 +gl.MAX_FRAGMENT_UNIFORM_VECTORS = 0x8dfd +gl.MAX_LABEL_LENGTH = 0x82e8 +gl.MAX_MULTIVIEW_BUFFERS_EXT = 0x90f2 +gl.MAX_RENDERBUFFER_SIZE = 0x84e8 +gl.MAX_SAMPLES_ANGLE = 0x8d57 +gl.MAX_SAMPLES_APPLE = 0x8d57 +gl.MAX_SAMPLES_EXT = 0x8d57 +gl.MAX_SAMPLES_IMG = 0x9135 +gl.MAX_SAMPLES_NV = 0x8d57 +gl.MAX_SERVER_WAIT_TIMEOUT_APPLE = 0x9111 +gl.MAX_TEXTURE_IMAGE_UNITS = 0x8872 +gl.MAX_TEXTURE_MAX_ANISOTROPY_EXT = 0x84ff +gl.MAX_TEXTURE_SIZE = 0xd33 +gl.MAX_VARYING_VECTORS = 0x8dfc +gl.MAX_VERTEX_ATTRIBS = 0x8869 +gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS = 0x8b4c +gl.MAX_VERTEX_UNIFORM_VECTORS = 0x8dfb +gl.MAX_VIEWPORT_DIMS = 0xd3a +gl.MEDIUM_FLOAT = 0x8df1 +gl.MEDIUM_INT = 0x8df4 +gl.MIN_EXT = 0x8007 +gl.MIRRORED_REPEAT = 0x8370 +gl.MULTISAMPLE_BUFFER_BIT0_QCOM = 0x1000000 +gl.MULTISAMPLE_BUFFER_BIT1_QCOM = 0x2000000 +gl.MULTISAMPLE_BUFFER_BIT2_QCOM = 0x4000000 +gl.MULTISAMPLE_BUFFER_BIT3_QCOM = 0x8000000 +gl.MULTISAMPLE_BUFFER_BIT4_QCOM = 0x10000000 +gl.MULTISAMPLE_BUFFER_BIT5_QCOM = 0x20000000 +gl.MULTISAMPLE_BUFFER_BIT6_QCOM = 0x40000000 +gl.MULTISAMPLE_BUFFER_BIT7_QCOM = 0x80000000 +gl.MULTIVIEW_EXT = 0x90f1 +gl.NEAREST = 0x2600 +gl.NEAREST_MIPMAP_LINEAR = 0x2702 +gl.NEAREST_MIPMAP_NEAREST = 0x2700 +gl.NEVER = 0x200 +gl.NICEST = 0x1102 +gl.NONE = 0x0 +gl.NOTEQUAL = 0x205 +gl.NO_ERROR = 0x0 +gl.NO_RESET_NOTIFICATION_EXT = 0x8261 +gl.NUM_COMPRESSED_TEXTURE_FORMATS = 0x86a2 +gl.NUM_PROGRAM_BINARY_FORMATS_OES = 0x87fe +gl.NUM_SHADER_BINARY_FORMATS = 0x8df9 +gl.NV_coverage_sample = 0x1 +gl.NV_depth_nonlinear = 0x1 +gl.NV_draw_buffers = 0x1 +gl.NV_draw_instanced = 0x1 +gl.NV_fbo_color_attachments = 0x1 +gl.NV_fence = 0x1 +gl.NV_framebuffer_blit = 0x1 +gl.NV_framebuffer_multisample = 0x1 +gl.NV_generate_mipmap_sRGB = 0x1 +gl.NV_instanced_arrays = 0x1 +gl.NV_read_buffer = 0x1 +gl.NV_read_buffer_front = 0x1 +gl.NV_read_depth = 0x1 +gl.NV_read_depth_stencil = 0x1 +gl.NV_read_stencil = 0x1 +gl.NV_sRGB_formats = 0x1 +gl.NV_shadow_samplers_array = 0x1 +gl.NV_shadow_samplers_cube = 0x1 +gl.NV_texture_border_clamp = 0x1 +gl.NV_texture_compression_s3tc_update = 0x1 +gl.NV_texture_npot_2D_mipmap = 0x1 +gl.OBJECT_TYPE_APPLE = 0x9112 +gl.OES_EGL_image = 0x1 +gl.OES_EGL_image_external = 0x1 +gl.OES_compressed_ETC1_RGB8_texture = 0x1 +gl.OES_compressed_paletted_texture = 0x1 +gl.OES_depth24 = 0x1 +gl.OES_depth32 = 0x1 +gl.OES_depth_texture = 0x1 +gl.OES_element_index_uint = 0x1 +gl.OES_fbo_render_mipmap = 0x1 +gl.OES_fragment_precision_high = 0x1 +gl.OES_get_program_binary = 0x1 +gl.OES_mapbuffer = 0x1 +gl.OES_packed_depth_stencil = 0x1 +gl.OES_required_internalformat = 0x1 +gl.OES_rgb8_rgba8 = 0x1 +gl.OES_standard_derivatives = 0x1 +gl.OES_stencil1 = 0x1 +gl.OES_stencil4 = 0x1 +gl.OES_surfaceless_context = 0x1 +gl.OES_texture_3D = 0x1 +gl.OES_texture_float = 0x1 +gl.OES_texture_float_linear = 0x1 +gl.OES_texture_half_float = 0x1 +gl.OES_texture_half_float_linear = 0x1 +gl.OES_texture_npot = 0x1 +gl.OES_vertex_array_object = 0x1 +gl.OES_vertex_half_float = 0x1 +gl.OES_vertex_type_10_10_10_2 = 0x1 +gl.ONE = 0x1 +gl.ONE_MINUS_CONSTANT_ALPHA = 0x8004 +gl.ONE_MINUS_CONSTANT_COLOR = 0x8002 +gl.ONE_MINUS_DST_ALPHA = 0x305 +gl.ONE_MINUS_DST_COLOR = 0x307 +gl.ONE_MINUS_SRC_ALPHA = 0x303 +gl.ONE_MINUS_SRC_COLOR = 0x301 +gl.OUT_OF_MEMORY = 0x505 +gl.PACK_ALIGNMENT = 0xd05 +gl.PACK_REVERSE_ROW_ORDER_ANGLE = 0x93a4 +gl.PALETTE4_R5_G6_B5_OES = 0x8b92 +gl.PALETTE4_RGB5_A1_OES = 0x8b94 +gl.PALETTE4_RGB8_OES = 0x8b90 +gl.PALETTE4_RGBA4_OES = 0x8b93 +gl.PALETTE4_RGBA8_OES = 0x8b91 +gl.PALETTE8_R5_G6_B5_OES = 0x8b97 +gl.PALETTE8_RGB5_A1_OES = 0x8b99 +gl.PALETTE8_RGB8_OES = 0x8b95 +gl.PALETTE8_RGBA4_OES = 0x8b98 +gl.PALETTE8_RGBA8_OES = 0x8b96 +gl.PERCENTAGE_AMD = 0x8bc3 +gl.PERFMON_GLOBAL_MODE_QCOM = 0x8fa0 +gl.PERFMON_RESULT_AMD = 0x8bc6 +gl.PERFMON_RESULT_AVAILABLE_AMD = 0x8bc4 +gl.PERFMON_RESULT_SIZE_AMD = 0x8bc5 +gl.POINTS = 0x0 +gl.POLYGON_OFFSET_FACTOR = 0x8038 +gl.POLYGON_OFFSET_FILL = 0x8037 +gl.POLYGON_OFFSET_UNITS = 0x2a00 +gl.PROGRAM = 0x82e2 +gl.PROGRAM_BINARY_ANGLE = 0x93a6 +gl.PROGRAM_BINARY_FORMATS_OES = 0x87ff +gl.PROGRAM_BINARY_LENGTH_OES = 0x8741 +gl.PROGRAM_OBJECT_EXT = 0x8b40 +gl.PROGRAM_PIPELINE_BINDING_EXT = 0x825a +gl.PROGRAM_PIPELINE_OBJECT_EXT = 0x8a4f +gl.PROGRAM_SEPARABLE_EXT = 0x8258 +gl.QCOM_alpha_test = 0x1 +gl.QCOM_binning_control = 0x1 +gl.QCOM_driver_control = 0x1 +gl.QCOM_extended_get = 0x1 +gl.QCOM_extended_get2 = 0x1 +gl.QCOM_perfmon_global_mode = 0x1 +gl.QCOM_tiled_rendering = 0x1 +gl.QCOM_writeonly_rendering = 0x1 +gl.QUERY = 0x82e3 +gl.QUERY_OBJECT_EXT = 0x9153 +gl.QUERY_RESULT_AVAILABLE_EXT = 0x8867 +gl.QUERY_RESULT_EXT = 0x8866 +gl.R16F_EXT = 0x822d +gl.R32F_EXT = 0x822e +gl.R8_EXT = 0x8229 +gl.READ_BUFFER_EXT = 0xc02 +gl.READ_BUFFER_NV = 0xc02 +gl.READ_FRAMEBUFFER_ANGLE = 0x8ca8 +gl.READ_FRAMEBUFFER_APPLE = 0x8ca8 +gl.READ_FRAMEBUFFER_BINDING_ANGLE = 0x8caa +gl.READ_FRAMEBUFFER_BINDING_APPLE = 0x8caa +gl.READ_FRAMEBUFFER_BINDING_NV = 0x8caa +gl.READ_FRAMEBUFFER_NV = 0x8ca8 +gl.RED_BITS = 0xd52 +gl.RED_EXT = 0x1903 +gl.RENDERBUFFER = 0x8d41 +gl.RENDERBUFFER_ALPHA_SIZE = 0x8d53 +gl.RENDERBUFFER_BINDING = 0x8ca7 +gl.RENDERBUFFER_BLUE_SIZE = 0x8d52 +gl.RENDERBUFFER_DEPTH_SIZE = 0x8d54 +gl.RENDERBUFFER_GREEN_SIZE = 0x8d51 +gl.RENDERBUFFER_HEIGHT = 0x8d43 +gl.RENDERBUFFER_INTERNAL_FORMAT = 0x8d44 +gl.RENDERBUFFER_RED_SIZE = 0x8d50 +gl.RENDERBUFFER_SAMPLES_ANGLE = 0x8cab +gl.RENDERBUFFER_SAMPLES_APPLE = 0x8cab +gl.RENDERBUFFER_SAMPLES_EXT = 0x8cab +gl.RENDERBUFFER_SAMPLES_IMG = 0x9133 +gl.RENDERBUFFER_SAMPLES_NV = 0x8cab +gl.RENDERBUFFER_STENCIL_SIZE = 0x8d55 +gl.RENDERBUFFER_WIDTH = 0x8d42 +gl.RENDERER = 0x1f01 +gl.RENDER_DIRECT_TO_FRAMEBUFFER_QCOM = 0x8fb3 +gl.REPEAT = 0x2901 +gl.REPLACE = 0x1e01 +gl.REQUIRED_TEXTURE_IMAGE_UNITS_OES = 0x8d68 +gl.RESET_NOTIFICATION_STRATEGY_EXT = 0x8256 +gl.RG16F_EXT = 0x822f +gl.RG32F_EXT = 0x8230 +gl.RG8_EXT = 0x822b +gl.RGB = 0x1907 +gl.RGB10_A2_EXT = 0x8059 +gl.RGB10_EXT = 0x8052 +gl.RGB16F_EXT = 0x881b +gl.RGB32F_EXT = 0x8815 +gl.RGB565 = 0x8d62 +gl.RGB565_OES = 0x8d62 +gl.RGB5_A1 = 0x8057 +gl.RGB5_A1_OES = 0x8057 +gl.RGB8_OES = 0x8051 +gl.RGBA = 0x1908 +gl.RGBA16F_EXT = 0x881a +gl.RGBA32F_EXT = 0x8814 +gl.RGBA4 = 0x8056 +gl.RGBA4_OES = 0x8056 +gl.RGBA8_OES = 0x8058 +gl.RGB_422_APPLE = 0x8a1f +gl.RG_EXT = 0x8227 +gl.SAMPLER = 0x82e6 +gl.SAMPLER_2D = 0x8b5e +gl.SAMPLER_2D_ARRAY_SHADOW_NV = 0x8dc4 +gl.SAMPLER_2D_SHADOW_EXT = 0x8b62 +gl.SAMPLER_3D_OES = 0x8b5f +gl.SAMPLER_CUBE = 0x8b60 +gl.SAMPLER_CUBE_SHADOW_NV = 0x8dc5 +gl.SAMPLER_EXTERNAL_OES = 0x8d66 +gl.SAMPLES = 0x80a9 +gl.SAMPLE_ALPHA_TO_COVERAGE = 0x809e +gl.SAMPLE_BUFFERS = 0x80a8 +gl.SAMPLE_COVERAGE = 0x80a0 +gl.SAMPLE_COVERAGE_INVERT = 0x80ab +gl.SAMPLE_COVERAGE_VALUE = 0x80aa +gl.SCISSOR_BOX = 0xc10 +gl.SCISSOR_TEST = 0xc11 +gl.SGX_BINARY_IMG = 0x8c0a +gl.SGX_PROGRAM_BINARY_IMG = 0x9130 +gl.SHADER = 0x82e1 +gl.SHADER_BINARY_DMP = 0x9250 +gl.SHADER_BINARY_FORMATS = 0x8df8 +gl.SHADER_BINARY_VIV = 0x8fc4 +gl.SHADER_COMPILER = 0x8dfa +gl.SHADER_OBJECT_EXT = 0x8b48 +gl.SHADER_SOURCE_LENGTH = 0x8b88 +gl.SHADER_TYPE = 0x8b4f +gl.SHADING_LANGUAGE_VERSION = 0x8b8c +gl.SHORT = 0x1402 +gl.SIGNALED_APPLE = 0x9119 +gl.SLUMINANCE8_ALPHA8_NV = 0x8c45 +gl.SLUMINANCE8_NV = 0x8c47 +gl.SLUMINANCE_ALPHA_NV = 0x8c44 +gl.SLUMINANCE_NV = 0x8c46 +gl.SRC_ALPHA = 0x302 +gl.SRC_ALPHA_SATURATE = 0x308 +gl.SRC_COLOR = 0x300 +gl.SRGB8_ALPHA8_EXT = 0x8c43 +gl.SRGB8_NV = 0x8c41 +gl.SRGB_ALPHA_EXT = 0x8c42 +gl.SRGB_EXT = 0x8c40 +gl.STACK_OVERFLOW = 0x503 +gl.STACK_UNDERFLOW = 0x504 +gl.STATE_RESTORE = 0x8bdc +gl.STATIC_DRAW = 0x88e4 +gl.STENCIL_ATTACHMENT = 0x8d20 +gl.STENCIL_BACK_FAIL = 0x8801 +gl.STENCIL_BACK_FUNC = 0x8800 +gl.STENCIL_BACK_PASS_DEPTH_FAIL = 0x8802 +gl.STENCIL_BACK_PASS_DEPTH_PASS = 0x8803 +gl.STENCIL_BACK_REF = 0x8ca3 +gl.STENCIL_BACK_VALUE_MASK = 0x8ca4 +gl.STENCIL_BACK_WRITEMASK = 0x8ca5 +gl.STENCIL_BITS = 0xd57 +gl.STENCIL_BUFFER_BIT = 0x400 +gl.STENCIL_BUFFER_BIT0_QCOM = 0x10000 +gl.STENCIL_BUFFER_BIT1_QCOM = 0x20000 +gl.STENCIL_BUFFER_BIT2_QCOM = 0x40000 +gl.STENCIL_BUFFER_BIT3_QCOM = 0x80000 +gl.STENCIL_BUFFER_BIT4_QCOM = 0x100000 +gl.STENCIL_BUFFER_BIT5_QCOM = 0x200000 +gl.STENCIL_BUFFER_BIT6_QCOM = 0x400000 +gl.STENCIL_BUFFER_BIT7_QCOM = 0x800000 +gl.STENCIL_CLEAR_VALUE = 0xb91 +gl.STENCIL_EXT = 0x1802 +gl.STENCIL_FAIL = 0xb94 +gl.STENCIL_FUNC = 0xb92 +gl.STENCIL_INDEX1_OES = 0x8d46 +gl.STENCIL_INDEX4_OES = 0x8d47 +gl.STENCIL_INDEX8 = 0x8d48 +gl.STENCIL_PASS_DEPTH_FAIL = 0xb95 +gl.STENCIL_PASS_DEPTH_PASS = 0xb96 +gl.STENCIL_REF = 0xb97 +gl.STENCIL_TEST = 0xb90 +gl.STENCIL_VALUE_MASK = 0xb93 +gl.STENCIL_WRITEMASK = 0xb98 +gl.STREAM_DRAW = 0x88e0 +gl.SUBPIXEL_BITS = 0xd50 +gl.SYNC_CONDITION_APPLE = 0x9113 +gl.SYNC_FENCE_APPLE = 0x9116 +gl.SYNC_FLAGS_APPLE = 0x9115 +gl.SYNC_FLUSH_COMMANDS_BIT_APPLE = 0x1 +gl.SYNC_GPU_COMMANDS_COMPLETE_APPLE = 0x9117 +gl.SYNC_OBJECT_APPLE = 0x8a53 +gl.SYNC_STATUS_APPLE = 0x9114 +gl.TEXTURE = 0x1702 +gl.TEXTURE0 = 0x84c0 +gl.TEXTURE1 = 0x84c1 +gl.TEXTURE10 = 0x84ca +gl.TEXTURE11 = 0x84cb +gl.TEXTURE12 = 0x84cc +gl.TEXTURE13 = 0x84cd +gl.TEXTURE14 = 0x84ce +gl.TEXTURE15 = 0x84cf +gl.TEXTURE16 = 0x84d0 +gl.TEXTURE17 = 0x84d1 +gl.TEXTURE18 = 0x84d2 +gl.TEXTURE19 = 0x84d3 +gl.TEXTURE2 = 0x84c2 +gl.TEXTURE20 = 0x84d4 +gl.TEXTURE21 = 0x84d5 +gl.TEXTURE22 = 0x84d6 +gl.TEXTURE23 = 0x84d7 +gl.TEXTURE24 = 0x84d8 +gl.TEXTURE25 = 0x84d9 +gl.TEXTURE26 = 0x84da +gl.TEXTURE27 = 0x84db +gl.TEXTURE28 = 0x84dc +gl.TEXTURE29 = 0x84dd +gl.TEXTURE3 = 0x84c3 +gl.TEXTURE30 = 0x84de +gl.TEXTURE31 = 0x84df +gl.TEXTURE4 = 0x84c4 +gl.TEXTURE5 = 0x84c5 +gl.TEXTURE6 = 0x84c6 +gl.TEXTURE7 = 0x84c7 +gl.TEXTURE8 = 0x84c8 +gl.TEXTURE9 = 0x84c9 +gl.TEXTURE_2D = 0xde1 +gl.TEXTURE_3D_OES = 0x806f +gl.TEXTURE_BINDING_2D = 0x8069 +gl.TEXTURE_BINDING_3D_OES = 0x806a +gl.TEXTURE_BINDING_CUBE_MAP = 0x8514 +gl.TEXTURE_BINDING_EXTERNAL_OES = 0x8d67 +gl.TEXTURE_BORDER_COLOR_NV = 0x1004 +gl.TEXTURE_COMPARE_FUNC_EXT = 0x884d +gl.TEXTURE_COMPARE_MODE_EXT = 0x884c +gl.TEXTURE_CUBE_MAP = 0x8513 +gl.TEXTURE_CUBE_MAP_NEGATIVE_X = 0x8516 +gl.TEXTURE_CUBE_MAP_NEGATIVE_Y = 0x8518 +gl.TEXTURE_CUBE_MAP_NEGATIVE_Z = 0x851a +gl.TEXTURE_CUBE_MAP_POSITIVE_X = 0x8515 +gl.TEXTURE_CUBE_MAP_POSITIVE_Y = 0x8517 +gl.TEXTURE_CUBE_MAP_POSITIVE_Z = 0x8519 +gl.TEXTURE_DEPTH_QCOM = 0x8bd4 +gl.TEXTURE_EXTERNAL_OES = 0x8d65 +gl.TEXTURE_FORMAT_QCOM = 0x8bd6 +gl.TEXTURE_HEIGHT_QCOM = 0x8bd3 +gl.TEXTURE_IMAGE_VALID_QCOM = 0x8bd8 +gl.TEXTURE_IMMUTABLE_FORMAT_EXT = 0x912f +gl.TEXTURE_INTERNAL_FORMAT_QCOM = 0x8bd5 +gl.TEXTURE_MAG_FILTER = 0x2800 +gl.TEXTURE_MAX_ANISOTROPY_EXT = 0x84fe +gl.TEXTURE_MAX_LEVEL_APPLE = 0x813d +gl.TEXTURE_MIN_FILTER = 0x2801 +gl.TEXTURE_NUM_LEVELS_QCOM = 0x8bd9 +gl.TEXTURE_OBJECT_VALID_QCOM = 0x8bdb +gl.TEXTURE_SAMPLES_IMG = 0x9136 +gl.TEXTURE_TARGET_QCOM = 0x8bda +gl.TEXTURE_TYPE_QCOM = 0x8bd7 +gl.TEXTURE_USAGE_ANGLE = 0x93a2 +gl.TEXTURE_WIDTH_QCOM = 0x8bd2 +gl.TEXTURE_WRAP_R_OES = 0x8072 +gl.TEXTURE_WRAP_S = 0x2802 +gl.TEXTURE_WRAP_T = 0x2803 +gl.TIMEOUT_EXPIRED_APPLE = 0x911b +gl.TIMEOUT_IGNORED_APPLE = 0xffffffffffffffff +gl.TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE = 0x93a0 +gl.TRIANGLES = 0x4 +gl.TRIANGLE_FAN = 0x6 +gl.TRIANGLE_STRIP = 0x5 +gl.TRUE = 0x1 +gl.UNKNOWN_CONTEXT_RESET_EXT = 0x8255 +gl.UNPACK_ALIGNMENT = 0xcf5 +gl.UNPACK_ROW_LENGTH = 0xcf2 +gl.UNPACK_SKIP_PIXELS = 0xcf4 +gl.UNPACK_SKIP_ROWS = 0xcf3 +gl.UNSIGNALED_APPLE = 0x9118 +gl.UNSIGNED_BYTE = 0x1401 +gl.UNSIGNED_INT = 0x1405 +gl.UNSIGNED_INT64_AMD = 0x8bc2 +gl.UNSIGNED_INT_10_10_10_2_OES = 0x8df6 +gl.UNSIGNED_INT_24_8_OES = 0x84fa +gl.UNSIGNED_INT_2_10_10_10_REV_EXT = 0x8368 +gl.UNSIGNED_NORMALIZED_EXT = 0x8c17 +gl.UNSIGNED_SHORT = 0x1403 +gl.UNSIGNED_SHORT_1_5_5_5_REV_EXT = 0x8366 +gl.UNSIGNED_SHORT_4_4_4_4 = 0x8033 +gl.UNSIGNED_SHORT_4_4_4_4_REV_EXT = 0x8365 +gl.UNSIGNED_SHORT_4_4_4_4_REV_IMG = 0x8365 +gl.UNSIGNED_SHORT_5_5_5_1 = 0x8034 +gl.UNSIGNED_SHORT_5_6_5 = 0x8363 +gl.UNSIGNED_SHORT_8_8_APPLE = 0x85ba +gl.UNSIGNED_SHORT_8_8_REV_APPLE = 0x85bb +gl.VALIDATE_STATUS = 0x8b83 +gl.VENDOR = 0x1f00 +gl.VERSION = 0x1f02 +gl.VERTEX_ARRAY_BINDING_OES = 0x85b5 +gl.VERTEX_ARRAY_OBJECT_EXT = 0x9154 +gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING = 0x889f +gl.VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE = 0x88fe +gl.VERTEX_ATTRIB_ARRAY_DIVISOR_NV = 0x88fe +gl.VERTEX_ATTRIB_ARRAY_ENABLED = 0x8622 +gl.VERTEX_ATTRIB_ARRAY_NORMALIZED = 0x886a +gl.VERTEX_ATTRIB_ARRAY_POINTER = 0x8645 +gl.VERTEX_ATTRIB_ARRAY_SIZE = 0x8623 +gl.VERTEX_ATTRIB_ARRAY_STRIDE = 0x8624 +gl.VERTEX_ATTRIB_ARRAY_TYPE = 0x8625 +gl.VERTEX_SHADER = 0x8b31 +gl.VERTEX_SHADER_BIT_EXT = 0x1 +gl.VIEWPORT = 0xba2 +gl.VIV_shader_binary = 0x1 +gl.WAIT_FAILED_APPLE = 0x911d +gl.WRITEONLY_RENDERING_QCOM = 0x8823 +gl.WRITE_ONLY_OES = 0x88b9 +gl.Z400_BINARY_AMD = 0x8740 +gl.ZERO = 0x0 diff --git a/cocos/scripting/lua-bindings/script/StudioConstants.lua b/cocos/scripting/lua-bindings/script/StudioConstants.lua new file mode 100644 index 0000000000..5f9bca3036 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/StudioConstants.lua @@ -0,0 +1,8 @@ +ccs = ccs or {} + + +ccs.MovementEventType = { + start = 0, + complete = 1, + loopComplete = 2, +} diff --git a/cocos/scripting/lua-bindings/script/extern.lua b/cocos/scripting/lua-bindings/script/extern.lua new file mode 100644 index 0000000000..61c47e30df --- /dev/null +++ b/cocos/scripting/lua-bindings/script/extern.lua @@ -0,0 +1,93 @@ +function clone(object) + local lookup_table = {} + local function _copy(object) + if type(object) ~= "table" then + return object + elseif lookup_table[object] then + return lookup_table[object] + end + local new_table = {} + lookup_table[object] = new_table + for key, value in pairs(object) do + new_table[_copy(key)] = _copy(value) + end + return setmetatable(new_table, getmetatable(object)) + end + return _copy(object) +end + +--Create an class. +function class(classname, super) + local superType = type(super) + local cls + + if superType ~= "function" and superType ~= "table" then + superType = nil + super = nil + end + + if superType == "function" or (super and super.__ctype == 1) then + -- inherited from native C++ Object + cls = {} + + if superType == "table" then + -- copy fields from super + for k,v in pairs(super) do cls[k] = v end + cls.__create = super.__create + cls.super = super + else + cls.__create = super + end + + cls.ctor = function() end + cls.__cname = classname + cls.__ctype = 1 + + function cls.new(...) + local instance = cls.__create(...) + -- copy fields from class to native object + for k,v in pairs(cls) do instance[k] = v end + instance.class = cls + instance:ctor(...) + return instance + end + + else + -- inherited from Lua Object + if super then + cls = clone(super) + cls.super = super + else + cls = {ctor = function() end} + end + + cls.__cname = classname + cls.__ctype = 2 -- lua + cls.__index = cls + + function cls.new(...) + local instance = setmetatable({}, cls) + instance.class = cls + instance:ctor(...) + return instance + end + end + + return cls +end + +function schedule(node, callback, delay) + local delay = cc.DelayTime:create(delay) + local sequence = cc.Sequence:create(delay, cc.CallFunc:create(callback)) + local action = cc.RepeatForever:create(sequence) + node:runAction(action) + return action +end + +function performWithDelay(node, callback, delay) + local delay = cc.DelayTime:create(delay) + local sequence = cc.Sequence:create(delay, cc.CallFunc:create(callback)) + node:runAction(sequence) + return sequence +end + diff --git a/cocos/scripting/lua-bindings/script/json.lua b/cocos/scripting/lua-bindings/script/json.lua new file mode 100644 index 0000000000..e0b3d17c3e --- /dev/null +++ b/cocos/scripting/lua-bindings/script/json.lua @@ -0,0 +1,376 @@ +----------------------------------------------------------------------------- +-- JSON4Lua: JSON encoding / decoding support for the Lua language. +-- json Module. +-- Author: Craig Mason-Jones +-- Homepage: http://json.luaforge.net/ +-- Version: 0.9.40 +-- This module is released under the MIT License (MIT). +-- Please see LICENCE.txt for details. +-- +-- USAGE: +-- This module exposes two functions: +-- encode(o) +-- Returns the table / string / boolean / number / nil / json.null value as a JSON-encoded string. +-- decode(json_string) +-- Returns a Lua object populated with the data encoded in the JSON string json_string. +-- +-- REQUIREMENTS: +-- compat-5.1 if using Lua 5.0 +-- +-- CHANGELOG +-- 0.9.20 Introduction of local Lua functions for private functions (removed _ function prefix). +-- Fixed Lua 5.1 compatibility issues. +-- Introduced json.null to have null values in associative arrays. +-- encode() performance improvement (more than 50%) through table.concat rather than .. +-- Introduced decode ability to ignore /**/ comments in the JSON string. +-- 0.9.10 Fix to array encoding / decoding to correctly manage nil/null values in arrays. +----------------------------------------------------------------------------- + +----------------------------------------------------------------------------- +-- Imports and dependencies +----------------------------------------------------------------------------- +local math = require('math') +local string = require("string") +local table = require("table") + +local base = _G + +----------------------------------------------------------------------------- +-- Module declaration +----------------------------------------------------------------------------- +module("json") + +-- Public functions + +-- Private functions +local decode_scanArray +local decode_scanComment +local decode_scanConstant +local decode_scanNumber +local decode_scanObject +local decode_scanString +local decode_scanWhitespace +local encodeString +local isArray +local isEncodable + +----------------------------------------------------------------------------- +-- PUBLIC FUNCTIONS +----------------------------------------------------------------------------- +--- Encodes an arbitrary Lua object / variable. +-- @param v The Lua object / variable to be JSON encoded. +-- @return String containing the JSON encoding in internal Lua string format (i.e. not unicode) +function encode (v) + -- Handle nil values + if v==nil then + return "null" + end + + local vtype = base.type(v) + + -- Handle strings + if vtype=='string' then + return '"' .. encodeString(v) .. '"' -- Need to handle encoding in string + end + + -- Handle booleans + if vtype=='number' or vtype=='boolean' then + return base.tostring(v) + end + + -- Handle tables + if vtype=='table' then + local rval = {} + -- Consider arrays separately + local bArray, maxCount = isArray(v) + if bArray then + for i = 1,maxCount do + table.insert(rval, encode(v[i])) + end + else -- An object, not an array + for i,j in base.pairs(v) do + if isEncodable(i) and isEncodable(j) then + table.insert(rval, '"' .. encodeString(i) .. '":' .. encode(j)) + end + end + end + if bArray then + return '[' .. table.concat(rval,',') ..']' + else + return '{' .. table.concat(rval,',') .. '}' + end + end + + -- Handle null values + if vtype=='function' and v==null then + return 'null' + end + + base.assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. base.tostring(v)) +end + + +--- Decodes a JSON string and returns the decoded value as a Lua data structure / value. +-- @param s The string to scan. +-- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1. +-- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil, +-- and the position of the first character after +-- the scanned JSON object. +function decode(s, startPos) + startPos = startPos and startPos or 1 + startPos = decode_scanWhitespace(s,startPos) + base.assert(startPos<=string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']') + local curChar = string.sub(s,startPos,startPos) + -- Object + if curChar=='{' then + return decode_scanObject(s,startPos) + end + -- Array + if curChar=='[' then + return decode_scanArray(s,startPos) + end + -- Number + if string.find("+-0123456789.e", curChar, 1, true) then + return decode_scanNumber(s,startPos) + end + -- String + if curChar==[["]] or curChar==[[']] then + return decode_scanString(s,startPos) + end + if string.sub(s,startPos,startPos+1)=='/*' then + return decode(s, decode_scanComment(s,startPos)) + end + -- Otherwise, it must be a constant + return decode_scanConstant(s,startPos) +end + +--- The null function allows one to specify a null value in an associative array (which is otherwise +-- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null } +function null() + return null -- so json.null() will also return null ;-) +end +----------------------------------------------------------------------------- +-- Internal, PRIVATE functions. +-- Following a Python-like convention, I have prefixed all these 'PRIVATE' +-- functions with an underscore. +----------------------------------------------------------------------------- + +--- Scans an array from JSON into a Lua object +-- startPos begins at the start of the array. +-- Returns the array and the next starting position +-- @param s The string being scanned. +-- @param startPos The starting position for the scan. +-- @return table, int The scanned array as a table, and the position of the next character to scan. +function decode_scanArray(s,startPos) + local array = {} -- The return value + local stringLen = string.len(s) + base.assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s ) + startPos = startPos + 1 + -- Infinite loop for array elements + repeat + startPos = decode_scanWhitespace(s,startPos) + base.assert(startPos<=stringLen,'JSON String ended unexpectedly scanning array.') + local curChar = string.sub(s,startPos,startPos) + if (curChar==']') then + return array, startPos+1 + end + if (curChar==',') then + startPos = decode_scanWhitespace(s,startPos+1) + end + base.assert(startPos<=stringLen, 'JSON String ended unexpectedly scanning array.') + object, startPos = decode(s,startPos) + table.insert(array,object) + until false +end + +--- Scans a comment and discards the comment. +-- Returns the position of the next character following the comment. +-- @param string s The JSON string to scan. +-- @param int startPos The starting position of the comment +function decode_scanComment(s, startPos) + base.assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos) + local endPos = string.find(s,'*/',startPos+2) + base.assert(endPos~=nil, "Unterminated comment in string at " .. startPos) + return endPos+2 +end + +--- Scans for given constants: true, false or null +-- Returns the appropriate Lua type, and the position of the next character to read. +-- @param s The string being scanned. +-- @param startPos The position in the string at which to start scanning. +-- @return object, int The object (true, false or nil) and the position at which the next character should be +-- scanned. +function decode_scanConstant(s, startPos) + local consts = { ["true"] = true, ["false"] = false, ["null"] = nil } + local constNames = {"true","false","null"} + + for i,k in base.pairs(constNames) do + --print ("[" .. string.sub(s,startPos, startPos + string.len(k) -1) .."]", k) + if string.sub(s,startPos, startPos + string.len(k) -1 )==k then + return consts[k], startPos + string.len(k) + end + end + base.assert(nil, 'Failed to scan constant from string ' .. s .. ' at starting position ' .. startPos) +end + +--- Scans a number from the JSON encoded string. +-- (in fact, also is able to scan numeric +- eqns, which is not +-- in the JSON spec.) +-- Returns the number, and the position of the next character +-- after the number. +-- @param s The string being scanned. +-- @param startPos The position at which to start scanning. +-- @return number, int The extracted number and the position of the next character to scan. +function decode_scanNumber(s,startPos) + local endPos = startPos+1 + local stringLen = string.len(s) + local acceptableChars = "+-0123456789.e" + while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true) + and endPos<=stringLen + ) do + endPos = endPos + 1 + end + local stringValue = 'return ' .. string.sub(s,startPos, endPos-1) + local stringEval = base.loadstring(stringValue) + base.assert(stringEval, 'Failed to scan number [ ' .. stringValue .. '] in JSON string at position ' .. startPos .. ' : ' .. endPos) + return stringEval(), endPos +end + +--- Scans a JSON object into a Lua object. +-- startPos begins at the start of the object. +-- Returns the object and the next starting position. +-- @param s The string being scanned. +-- @param startPos The starting position of the scan. +-- @return table, int The scanned object as a table and the position of the next character to scan. +function decode_scanObject(s,startPos) + local object = {} + local stringLen = string.len(s) + local key, value + base.assert(string.sub(s,startPos,startPos)=='{','decode_scanObject called but object does not start at position ' .. startPos .. ' in string:\n' .. s) + startPos = startPos + 1 + repeat + startPos = decode_scanWhitespace(s,startPos) + base.assert(startPos<=stringLen, 'JSON string ended unexpectedly while scanning object.') + local curChar = string.sub(s,startPos,startPos) + if (curChar=='}') then + return object,startPos+1 + end + if (curChar==',') then + startPos = decode_scanWhitespace(s,startPos+1) + end + base.assert(startPos<=stringLen, 'JSON string ended unexpectedly scanning object.') + -- Scan the key + key, startPos = decode(s,startPos) + base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key) + startPos = decode_scanWhitespace(s,startPos) + base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key) + base.assert(string.sub(s,startPos,startPos)==':','JSON object key-value assignment mal-formed at ' .. startPos) + startPos = decode_scanWhitespace(s,startPos+1) + base.assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key) + value, startPos = decode(s,startPos) + object[key]=value + until false -- infinite loop while key-value pairs are found +end + +--- Scans a JSON string from the opening inverted comma or single quote to the +-- end of the string. +-- Returns the string extracted as a Lua string, +-- and the position of the next non-string character +-- (after the closing inverted comma or single quote). +-- @param s The string being scanned. +-- @param startPos The starting position of the scan. +-- @return string, int The extracted string as a Lua string, and the next character to parse. +function decode_scanString(s,startPos) + base.assert(startPos, 'decode_scanString(..) called without start position') + local startChar = string.sub(s,startPos,startPos) + base.assert(startChar==[[']] or startChar==[["]],'decode_scanString called for a non-string') + local escaped = false + local endPos = startPos + 1 + local bEnded = false + local stringLen = string.len(s) + repeat + local curChar = string.sub(s,endPos,endPos) + if not escaped then + if curChar==[[\]] then + escaped = true + else + bEnded = curChar==startChar + end + else + -- If we're escaped, we accept the current character come what may + escaped = false + end + endPos = endPos + 1 + base.assert(endPos <= stringLen+1, "String decoding failed: unterminated string at position " .. endPos) + until bEnded + local stringValue = 'return ' .. string.sub(s, startPos, endPos-1) + local stringEval = base.loadstring(stringValue) + base.assert(stringEval, 'Failed to load string [ ' .. stringValue .. '] in JSON4Lua.decode_scanString at position ' .. startPos .. ' : ' .. endPos) + return stringEval(), endPos +end + +--- Scans a JSON string skipping all whitespace from the current start position. +-- Returns the position of the first non-whitespace character, or nil if the whole end of string is reached. +-- @param s The string being scanned +-- @param startPos The starting position where we should begin removing whitespace. +-- @return int The first position where non-whitespace was encountered, or string.len(s)+1 if the end of string +-- was reached. +function decode_scanWhitespace(s,startPos) + local whitespace=" \n\r\t" + local stringLen = string.len(s) + while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true) and startPos <= stringLen) do + startPos = startPos + 1 + end + return startPos +end + +--- Encodes a string to be JSON-compatible. +-- This just involves back-quoting inverted commas, back-quotes and newlines, I think ;-) +-- @param s The string to return as a JSON encoded (i.e. backquoted string) +-- @return The string appropriately escaped. +function encodeString(s) + s = string.gsub(s,'\\','\\\\') + s = string.gsub(s,'"','\\"') + s = string.gsub(s,"'","\\'") + s = string.gsub(s,'\n','\\n') + s = string.gsub(s,'\t','\\t') + return s +end + +-- Determines whether the given Lua type is an array or a table / dictionary. +-- We consider any table an array if it has indexes 1..n for its n items, and no +-- other data in the table. +-- I think this method is currently a little 'flaky', but can't think of a good way around it yet... +-- @param t The table to evaluate as an array +-- @return boolean, number True if the table can be represented as an array, false otherwise. If true, +-- the second returned value is the maximum +-- number of indexed elements in the array. +function isArray(t) + -- Next we count all the elements, ensuring that any non-indexed elements are not-encodable + -- (with the possible exception of 'n') + local maxIndex = 0 + for k,v in base.pairs(t) do + if (base.type(k)=='number' and math.floor(k)==k and 1<=k) then -- k,v is an indexed pair + if (not isEncodable(v)) then return false end -- All array elements must be encodable + maxIndex = math.max(maxIndex,k) + else + if (k=='n') then + if v ~= table.getn(t) then return false end -- False if n does not hold the number of elements + else -- Else of (k=='n') + if isEncodable(v) then return false end + end -- End of (k~='n') + end -- End of k,v not an indexed pair + end -- End of loop across all pairs + return true, maxIndex +end + +--- Determines whether the given Lua object / table / variable can be JSON encoded. The only +-- types that are JSON encodable are: string, boolean, number, nil, table and json.null. +-- In this implementation, all other types are ignored. +-- @param o The object to examine. +-- @return boolean True if the object should be JSON encoded, false if it should be ignored. +function isEncodable(o) + local t = base.type(o) + return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null) +end + diff --git a/cocos/scripting/lua-bindings/script/luaj.lua b/cocos/scripting/lua-bindings/script/luaj.lua new file mode 100644 index 0000000000..8ec45a3b42 --- /dev/null +++ b/cocos/scripting/lua-bindings/script/luaj.lua @@ -0,0 +1,34 @@ + +local luaj = {} + +local callJavaStaticMethod = LuaJavaBridge.callStaticMethod + +local function checkArguments(args, sig) + if type(args) ~= "table" then args = {} end + if sig then return args, sig end + + sig = {"("} + for i, v in ipairs(args) do + local t = type(v) + if t == "number" then + sig[#sig + 1] = "F" + elseif t == "boolean" then + sig[#sig + 1] = "Z" + elseif t == "function" then + sig[#sig + 1] = "I" + else + sig[#sig + 1] = "Ljava/lang/String;" + end + end + sig[#sig + 1] = ")V" + + return args, table.concat(sig) +end + +function luaj.callStaticMethod(className, methodName, args, sig) + local args, sig = checkArguments(args, sig) + --echoInfo("luaj.callStaticMethod(\"%s\",\n\t\"%s\",\n\targs,\n\t\"%s\"", className, methodName, sig) + return callJavaStaticMethod(className, methodName, args, sig) +end + +return luaj diff --git a/cocos/scripting/lua-bindings/script/luaoc.lua b/cocos/scripting/lua-bindings/script/luaoc.lua new file mode 100644 index 0000000000..ff6d157c3b --- /dev/null +++ b/cocos/scripting/lua-bindings/script/luaoc.lua @@ -0,0 +1,28 @@ + +local luaoc = {} + +local callStaticMethod = LuaObjcBridge.callStaticMethod + +function luaoc.callStaticMethod(className, methodName, args) + local ok, ret = callStaticMethod(className, methodName, args) + if not ok then + local msg = string.format("luaoc.callStaticMethod(\"%s\", \"%s\", \"%s\") - error: [%s] ", + className, methodName, tostring(args), tostring(ret)) + if ret == -1 then + print(msg .. "INVALID PARAMETERS") + elseif ret == -2 then + print(msg .. "CLASS NOT FOUND") + elseif ret == -3 then + print(msg .. "METHOD NOT FOUND") + elseif ret == -4 then + print(msg .. "EXCEPTION OCCURRED") + elseif ret == -5 then + print(msg .. "INVALID METHOD SIGNATURE") + else + print(msg .. "UNKNOWN") + end + end + return ok, ret +end + +return luaoc diff --git a/external/lua/lua/CMakeLists.txt b/external/lua/lua/CMakeLists.txt new file mode 100644 index 0000000000..81febeffb3 --- /dev/null +++ b/external/lua/lua/CMakeLists.txt @@ -0,0 +1,42 @@ +set(LUA_SRC + lapi.c + lauxlib.c + lbaselib.c + lcode.c + ldblib.c + ldebug.c + ldo.c + ldump.c + lfunc.c + lgc.c + linit.c + liolib.c + llex.c + lmathlib.c + lmem.c + loadlib.c + lobject.c + lopcodes.c + loslib.c + lparser.c + lstate.c + lstring.c + lstrlib.c + ltable.c + ltablib.c + ltm.c + lundump.c + lvm.c + lzio.c + print.c +) + +add_library(lua STATIC + ${LUA_SRC} +) + +set_target_properties(lua + PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" +) diff --git a/external/lua/lua/lapi.c b/external/lua/lua/lapi.c new file mode 100644 index 0000000000..5d5145d2eb --- /dev/null +++ b/external/lua/lua/lapi.c @@ -0,0 +1,1087 @@ +/* +** $Id: lapi.c,v 2.55.1.5 2008/07/04 18:41:18 roberto Exp $ +** Lua API +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include + +#define lapi_c +#define LUA_CORE + +#include "lua.h" + +#include "lapi.h" +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" +#include "lundump.h" +#include "lvm.h" + + + +const char lua_ident[] = + "$Lua: " LUA_RELEASE " " LUA_COPYRIGHT " $\n" + "$Authors: " LUA_AUTHORS " $\n" + "$URL: www.lua.org $\n"; + + + +#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) + +#define api_checkvalidindex(L, i) api_check(L, (i) != luaO_nilobject) + +#define api_incr_top(L) {api_check(L, L->top < L->ci->top); L->top++;} + + + +static TValue *index2adr (lua_State *L, int idx) { + if (idx > 0) { + TValue *o = L->base + (idx - 1); + api_check(L, idx <= L->ci->top - L->base); + if (o >= L->top) return cast(TValue *, luaO_nilobject); + else return o; + } + else if (idx > LUA_REGISTRYINDEX) { + api_check(L, idx != 0 && -idx <= L->top - L->base); + return L->top + idx; + } + else switch (idx) { /* pseudo-indices */ + case LUA_REGISTRYINDEX: return registry(L); + case LUA_ENVIRONINDEX: { + Closure *func = curr_func(L); + sethvalue(L, &L->env, func->c.env); + return &L->env; + } + case LUA_GLOBALSINDEX: return gt(L); + default: { + Closure *func = curr_func(L); + idx = LUA_GLOBALSINDEX - idx; + return (idx <= func->c.nupvalues) + ? &func->c.upvalue[idx-1] + : cast(TValue *, luaO_nilobject); + } + } +} + + +static Table *getcurrenv (lua_State *L) { + if (L->ci == L->base_ci) /* no enclosing function? */ + return hvalue(gt(L)); /* use global table as environment */ + else { + Closure *func = curr_func(L); + return func->c.env; + } +} + + +void luaA_pushobject (lua_State *L, const TValue *o) { + setobj2s(L, L->top, o); + api_incr_top(L); +} + + +LUA_API int lua_checkstack (lua_State *L, int size) { + int res = 1; + lua_lock(L); + if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) + res = 0; /* stack overflow */ + else if (size > 0) { + luaD_checkstack(L, size); + if (L->ci->top < L->top + size) + L->ci->top = L->top + size; + } + lua_unlock(L); + return res; +} + + +LUA_API void lua_xmove (lua_State *from, lua_State *to, int n) { + int i; + if (from == to) return; + lua_lock(to); + api_checknelems(from, n); + api_check(from, G(from) == G(to)); + api_check(from, to->ci->top - to->top >= n); + from->top -= n; + for (i = 0; i < n; i++) { + setobj2s(to, to->top++, from->top + i); + } + lua_unlock(to); +} + + +LUA_API void lua_setlevel (lua_State *from, lua_State *to) { + to->nCcalls = from->nCcalls; +} + + +LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) { + lua_CFunction old; + lua_lock(L); + old = G(L)->panic; + G(L)->panic = panicf; + lua_unlock(L); + return old; +} + + +LUA_API lua_State *lua_newthread (lua_State *L) { + lua_State *L1; + lua_lock(L); + luaC_checkGC(L); + L1 = luaE_newthread(L); + setthvalue(L, L->top, L1); + api_incr_top(L); + lua_unlock(L); + luai_userstatethread(L, L1); + return L1; +} + + + +/* +** basic stack manipulation +*/ + + +LUA_API int lua_gettop (lua_State *L) { + return cast_int(L->top - L->base); +} + + +LUA_API void lua_settop (lua_State *L, int idx) { + lua_lock(L); + if (idx >= 0) { + api_check(L, idx <= L->stack_last - L->base); + while (L->top < L->base + idx) + setnilvalue(L->top++); + L->top = L->base + idx; + } + else { + api_check(L, -(idx+1) <= (L->top - L->base)); + L->top += idx+1; /* `subtract' index (index is negative) */ + } + lua_unlock(L); +} + + +LUA_API void lua_remove (lua_State *L, int idx) { + StkId p; + lua_lock(L); + p = index2adr(L, idx); + api_checkvalidindex(L, p); + while (++p < L->top) setobjs2s(L, p-1, p); + L->top--; + lua_unlock(L); +} + + +LUA_API void lua_insert (lua_State *L, int idx) { + StkId p; + StkId q; + lua_lock(L); + p = index2adr(L, idx); + api_checkvalidindex(L, p); + for (q = L->top; q>p; q--) setobjs2s(L, q, q-1); + setobjs2s(L, p, L->top); + lua_unlock(L); +} + + +LUA_API void lua_replace (lua_State *L, int idx) { + StkId o; + lua_lock(L); + /* explicit test for incompatible code */ + if (idx == LUA_ENVIRONINDEX && L->ci == L->base_ci) + luaG_runerror(L, "no calling environment"); + api_checknelems(L, 1); + o = index2adr(L, idx); + api_checkvalidindex(L, o); + if (idx == LUA_ENVIRONINDEX) { + Closure *func = curr_func(L); + api_check(L, ttistable(L->top - 1)); + func->c.env = hvalue(L->top - 1); + luaC_barrier(L, func, L->top - 1); + } + else { + setobj(L, o, L->top - 1); + if (idx < LUA_GLOBALSINDEX) /* function upvalue? */ + luaC_barrier(L, curr_func(L), L->top - 1); + } + L->top--; + lua_unlock(L); +} + + +LUA_API void lua_pushvalue (lua_State *L, int idx) { + lua_lock(L); + setobj2s(L, L->top, index2adr(L, idx)); + api_incr_top(L); + lua_unlock(L); +} + + + +/* +** access functions (stack -> C) +*/ + + +LUA_API int lua_type (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + return (o == luaO_nilobject) ? LUA_TNONE : ttype(o); +} + + +LUA_API const char *lua_typename (lua_State *L, int t) { + UNUSED(L); + return (t == LUA_TNONE) ? "no value" : luaT_typenames[t]; +} + + +LUA_API int lua_iscfunction (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + return iscfunction(o); +} + + +LUA_API int lua_isnumber (lua_State *L, int idx) { + TValue n; + const TValue *o = index2adr(L, idx); + return tonumber(o, &n); +} + + +LUA_API int lua_isstring (lua_State *L, int idx) { + int t = lua_type(L, idx); + return (t == LUA_TSTRING || t == LUA_TNUMBER); +} + + +LUA_API int lua_isuserdata (lua_State *L, int idx) { + const TValue *o = index2adr(L, idx); + return (ttisuserdata(o) || ttislightuserdata(o)); +} + + +LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { + StkId o1 = index2adr(L, index1); + StkId o2 = index2adr(L, index2); + return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 + : luaO_rawequalObj(o1, o2); +} + + +LUA_API int lua_equal (lua_State *L, int index1, int index2) { + StkId o1, o2; + int i; + lua_lock(L); /* may call tag method */ + o1 = index2adr(L, index1); + o2 = index2adr(L, index2); + i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 : equalobj(L, o1, o2); + lua_unlock(L); + return i; +} + + +LUA_API int lua_lessthan (lua_State *L, int index1, int index2) { + StkId o1, o2; + int i; + lua_lock(L); /* may call tag method */ + o1 = index2adr(L, index1); + o2 = index2adr(L, index2); + i = (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0 + : luaV_lessthan(L, o1, o2); + lua_unlock(L); + return i; +} + + + +LUA_API lua_Number lua_tonumber (lua_State *L, int idx) { + TValue n; + const TValue *o = index2adr(L, idx); + if (tonumber(o, &n)) + return nvalue(o); + else + return 0; +} + + +LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { + TValue n; + const TValue *o = index2adr(L, idx); + if (tonumber(o, &n)) { + lua_Integer res; + lua_Number num = nvalue(o); + lua_number2integer(res, num); + return res; + } + else + return 0; +} + + +LUA_API int lua_toboolean (lua_State *L, int idx) { + const TValue *o = index2adr(L, idx); + return !l_isfalse(o); +} + + +LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { + StkId o = index2adr(L, idx); + if (!ttisstring(o)) { + lua_lock(L); /* `luaV_tostring' may create a new string */ + if (!luaV_tostring(L, o)) { /* conversion failed? */ + if (len != NULL) *len = 0; + lua_unlock(L); + return NULL; + } + luaC_checkGC(L); + o = index2adr(L, idx); /* previous call may reallocate the stack */ + lua_unlock(L); + } + if (len != NULL) *len = tsvalue(o)->len; + return svalue(o); +} + + +LUA_API size_t lua_objlen (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + switch (ttype(o)) { + case LUA_TSTRING: return tsvalue(o)->len; + case LUA_TUSERDATA: return uvalue(o)->len; + case LUA_TTABLE: return luaH_getn(hvalue(o)); + case LUA_TNUMBER: { + size_t l; + lua_lock(L); /* `luaV_tostring' may create a new string */ + l = (luaV_tostring(L, o) ? tsvalue(o)->len : 0); + lua_unlock(L); + return l; + } + default: return 0; + } +} + + +LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + return (!iscfunction(o)) ? NULL : clvalue(o)->c.f; +} + + +LUA_API void *lua_touserdata (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + switch (ttype(o)) { + case LUA_TUSERDATA: return (rawuvalue(o) + 1); + case LUA_TLIGHTUSERDATA: return pvalue(o); + default: return NULL; + } +} + + +LUA_API lua_State *lua_tothread (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + return (!ttisthread(o)) ? NULL : thvalue(o); +} + + +LUA_API const void *lua_topointer (lua_State *L, int idx) { + StkId o = index2adr(L, idx); + switch (ttype(o)) { + case LUA_TTABLE: return hvalue(o); + case LUA_TFUNCTION: return clvalue(o); + case LUA_TTHREAD: return thvalue(o); + case LUA_TUSERDATA: + case LUA_TLIGHTUSERDATA: + return lua_touserdata(L, idx); + default: return NULL; + } +} + + + +/* +** push functions (C -> stack) +*/ + + +LUA_API void lua_pushnil (lua_State *L) { + lua_lock(L); + setnilvalue(L->top); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { + lua_lock(L); + setnvalue(L->top, n); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { + lua_lock(L); + setnvalue(L->top, cast_num(n)); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { + lua_lock(L); + luaC_checkGC(L); + setsvalue2s(L, L->top, luaS_newlstr(L, s, len)); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushstring (lua_State *L, const char *s) { + if (s == NULL) + lua_pushnil(L); + else + lua_pushlstring(L, s, strlen(s)); +} + + +LUA_API const char *lua_pushvfstring (lua_State *L, const char *fmt, + va_list argp) { + const char *ret; + lua_lock(L); + luaC_checkGC(L); + ret = luaO_pushvfstring(L, fmt, argp); + lua_unlock(L); + return ret; +} + + +LUA_API const char *lua_pushfstring (lua_State *L, const char *fmt, ...) { + const char *ret; + va_list argp; + lua_lock(L); + luaC_checkGC(L); + va_start(argp, fmt); + ret = luaO_pushvfstring(L, fmt, argp); + va_end(argp); + lua_unlock(L); + return ret; +} + + +LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { + Closure *cl; + lua_lock(L); + luaC_checkGC(L); + api_checknelems(L, n); + cl = luaF_newCclosure(L, n, getcurrenv(L)); + cl->c.f = fn; + L->top -= n; + while (n--) + setobj2n(L, &cl->c.upvalue[n], L->top+n); + setclvalue(L, L->top, cl); + lua_assert(iswhite(obj2gco(cl))); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushboolean (lua_State *L, int b) { + lua_lock(L); + setbvalue(L->top, (b != 0)); /* ensure that true is 1 */ + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_pushlightuserdata (lua_State *L, void *p) { + lua_lock(L); + setpvalue(L->top, p); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API int lua_pushthread (lua_State *L) { + lua_lock(L); + setthvalue(L, L->top, L); + api_incr_top(L); + lua_unlock(L); + return (G(L)->mainthread == L); +} + + + +/* +** get functions (Lua -> stack) +*/ + + +LUA_API void lua_gettable (lua_State *L, int idx) { + StkId t; + lua_lock(L); + t = index2adr(L, idx); + api_checkvalidindex(L, t); + luaV_gettable(L, t, L->top - 1, L->top - 1); + lua_unlock(L); +} + + +LUA_API void lua_getfield (lua_State *L, int idx, const char *k) { + StkId t; + TValue key; + lua_lock(L); + t = index2adr(L, idx); + api_checkvalidindex(L, t); + setsvalue(L, &key, luaS_new(L, k)); + luaV_gettable(L, t, &key, L->top); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_rawget (lua_State *L, int idx) { + StkId t; + lua_lock(L); + t = index2adr(L, idx); + api_check(L, ttistable(t)); + setobj2s(L, L->top - 1, luaH_get(hvalue(t), L->top - 1)); + lua_unlock(L); +} + + +LUA_API void lua_rawgeti (lua_State *L, int idx, int n) { + StkId o; + lua_lock(L); + o = index2adr(L, idx); + api_check(L, ttistable(o)); + setobj2s(L, L->top, luaH_getnum(hvalue(o), n)); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API void lua_createtable (lua_State *L, int narray, int nrec) { + lua_lock(L); + luaC_checkGC(L); + sethvalue(L, L->top, luaH_new(L, narray, nrec)); + api_incr_top(L); + lua_unlock(L); +} + + +LUA_API int lua_getmetatable (lua_State *L, int objindex) { + const TValue *obj; + Table *mt = NULL; + int res; + lua_lock(L); + obj = index2adr(L, objindex); + switch (ttype(obj)) { + case LUA_TTABLE: + mt = hvalue(obj)->metatable; + break; + case LUA_TUSERDATA: + mt = uvalue(obj)->metatable; + break; + default: + mt = G(L)->mt[ttype(obj)]; + break; + } + if (mt == NULL) + res = 0; + else { + sethvalue(L, L->top, mt); + api_incr_top(L); + res = 1; + } + lua_unlock(L); + return res; +} + + +LUA_API void lua_getfenv (lua_State *L, int idx) { + StkId o; + lua_lock(L); + o = index2adr(L, idx); + api_checkvalidindex(L, o); + switch (ttype(o)) { + case LUA_TFUNCTION: + sethvalue(L, L->top, clvalue(o)->c.env); + break; + case LUA_TUSERDATA: + sethvalue(L, L->top, uvalue(o)->env); + break; + case LUA_TTHREAD: + setobj2s(L, L->top, gt(thvalue(o))); + break; + default: + setnilvalue(L->top); + break; + } + api_incr_top(L); + lua_unlock(L); +} + + +/* +** set functions (stack -> Lua) +*/ + + +LUA_API void lua_settable (lua_State *L, int idx) { + StkId t; + lua_lock(L); + api_checknelems(L, 2); + t = index2adr(L, idx); + api_checkvalidindex(L, t); + luaV_settable(L, t, L->top - 2, L->top - 1); + L->top -= 2; /* pop index and value */ + lua_unlock(L); +} + + +LUA_API void lua_setfield (lua_State *L, int idx, const char *k) { + StkId t; + TValue key; + lua_lock(L); + api_checknelems(L, 1); + t = index2adr(L, idx); + api_checkvalidindex(L, t); + setsvalue(L, &key, luaS_new(L, k)); + luaV_settable(L, t, &key, L->top - 1); + L->top--; /* pop value */ + lua_unlock(L); +} + + +LUA_API void lua_rawset (lua_State *L, int idx) { + StkId t; + lua_lock(L); + api_checknelems(L, 2); + t = index2adr(L, idx); + api_check(L, ttistable(t)); + setobj2t(L, luaH_set(L, hvalue(t), L->top-2), L->top-1); + luaC_barriert(L, hvalue(t), L->top-1); + L->top -= 2; + lua_unlock(L); +} + + +LUA_API void lua_rawseti (lua_State *L, int idx, int n) { + StkId o; + lua_lock(L); + api_checknelems(L, 1); + o = index2adr(L, idx); + api_check(L, ttistable(o)); + setobj2t(L, luaH_setnum(L, hvalue(o), n), L->top-1); + luaC_barriert(L, hvalue(o), L->top-1); + L->top--; + lua_unlock(L); +} + + +LUA_API int lua_setmetatable (lua_State *L, int objindex) { + TValue *obj; + Table *mt; + lua_lock(L); + api_checknelems(L, 1); + obj = index2adr(L, objindex); + api_checkvalidindex(L, obj); + if (ttisnil(L->top - 1)) + mt = NULL; + else { + api_check(L, ttistable(L->top - 1)); + mt = hvalue(L->top - 1); + } + switch (ttype(obj)) { + case LUA_TTABLE: { + hvalue(obj)->metatable = mt; + if (mt) + luaC_objbarriert(L, hvalue(obj), mt); + break; + } + case LUA_TUSERDATA: { + uvalue(obj)->metatable = mt; + if (mt) + luaC_objbarrier(L, rawuvalue(obj), mt); + break; + } + default: { + G(L)->mt[ttype(obj)] = mt; + break; + } + } + L->top--; + lua_unlock(L); + return 1; +} + + +LUA_API int lua_setfenv (lua_State *L, int idx) { + StkId o; + int res = 1; + lua_lock(L); + api_checknelems(L, 1); + o = index2adr(L, idx); + api_checkvalidindex(L, o); + api_check(L, ttistable(L->top - 1)); + switch (ttype(o)) { + case LUA_TFUNCTION: + clvalue(o)->c.env = hvalue(L->top - 1); + break; + case LUA_TUSERDATA: + uvalue(o)->env = hvalue(L->top - 1); + break; + case LUA_TTHREAD: + sethvalue(L, gt(thvalue(o)), hvalue(L->top - 1)); + break; + default: + res = 0; + break; + } + if (res) luaC_objbarrier(L, gcvalue(o), hvalue(L->top - 1)); + L->top--; + lua_unlock(L); + return res; +} + + +/* +** `load' and `call' functions (run Lua code) +*/ + + +#define adjustresults(L,nres) \ + { if (nres == LUA_MULTRET && L->top >= L->ci->top) L->ci->top = L->top; } + + +#define checkresults(L,na,nr) \ + api_check(L, (nr) == LUA_MULTRET || (L->ci->top - L->top >= (nr) - (na))) + + +LUA_API void lua_call (lua_State *L, int nargs, int nresults) { + StkId func; + lua_lock(L); + api_checknelems(L, nargs+1); + checkresults(L, nargs, nresults); + func = L->top - (nargs+1); + luaD_call(L, func, nresults); + adjustresults(L, nresults); + lua_unlock(L); +} + + + +/* +** Execute a protected call. +*/ +struct CallS { /* data to `f_call' */ + StkId func; + int nresults; +}; + + +static void f_call (lua_State *L, void *ud) { + struct CallS *c = cast(struct CallS *, ud); + luaD_call(L, c->func, c->nresults); +} + + + +LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) { + struct CallS c; + int status; + ptrdiff_t func; + lua_lock(L); + api_checknelems(L, nargs+1); + checkresults(L, nargs, nresults); + if (errfunc == 0) + func = 0; + else { + StkId o = index2adr(L, errfunc); + api_checkvalidindex(L, o); + func = savestack(L, o); + } + c.func = L->top - (nargs+1); /* function to be called */ + c.nresults = nresults; + status = luaD_pcall(L, f_call, &c, savestack(L, c.func), func); + adjustresults(L, nresults); + lua_unlock(L); + return status; +} + + +/* +** Execute a protected C call. +*/ +struct CCallS { /* data to `f_Ccall' */ + lua_CFunction func; + void *ud; +}; + + +static void f_Ccall (lua_State *L, void *ud) { + struct CCallS *c = cast(struct CCallS *, ud); + Closure *cl; + cl = luaF_newCclosure(L, 0, getcurrenv(L)); + cl->c.f = c->func; + setclvalue(L, L->top, cl); /* push function */ + api_incr_top(L); + setpvalue(L->top, c->ud); /* push only argument */ + api_incr_top(L); + luaD_call(L, L->top - 2, 0); +} + + +LUA_API int lua_cpcall (lua_State *L, lua_CFunction func, void *ud) { + struct CCallS c; + int status; + lua_lock(L); + c.func = func; + c.ud = ud; + status = luaD_pcall(L, f_Ccall, &c, savestack(L, L->top), 0); + lua_unlock(L); + return status; +} + + +LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, + const char *chunkname) { + ZIO z; + int status; + lua_lock(L); + if (!chunkname) chunkname = "?"; + luaZ_init(L, &z, reader, data); + status = luaD_protectedparser(L, &z, chunkname); + lua_unlock(L); + return status; +} + + +LUA_API int lua_dump (lua_State *L, lua_Writer writer, void *data) { + int status; + TValue *o; + lua_lock(L); + api_checknelems(L, 1); + o = L->top - 1; + if (isLfunction(o)) + status = luaU_dump(L, clvalue(o)->l.p, writer, data, 0); + else + status = 1; + lua_unlock(L); + return status; +} + + +LUA_API int lua_status (lua_State *L) { + return L->status; +} + + +/* +** Garbage-collection function +*/ + +LUA_API int lua_gc (lua_State *L, int what, int data) { + int res = 0; + global_State *g; + lua_lock(L); + g = G(L); + switch (what) { + case LUA_GCSTOP: { + g->GCthreshold = MAX_LUMEM; + break; + } + case LUA_GCRESTART: { + g->GCthreshold = g->totalbytes; + break; + } + case LUA_GCCOLLECT: { + luaC_fullgc(L); + break; + } + case LUA_GCCOUNT: { + /* GC values are expressed in Kbytes: #bytes/2^10 */ + res = cast_int(g->totalbytes >> 10); + break; + } + case LUA_GCCOUNTB: { + res = cast_int(g->totalbytes & 0x3ff); + break; + } + case LUA_GCSTEP: { + lu_mem a = (cast(lu_mem, data) << 10); + if (a <= g->totalbytes) + g->GCthreshold = g->totalbytes - a; + else + g->GCthreshold = 0; + while (g->GCthreshold <= g->totalbytes) { + luaC_step(L); + if (g->gcstate == GCSpause) { /* end of cycle? */ + res = 1; /* signal it */ + break; + } + } + break; + } + case LUA_GCSETPAUSE: { + res = g->gcpause; + g->gcpause = data; + break; + } + case LUA_GCSETSTEPMUL: { + res = g->gcstepmul; + g->gcstepmul = data; + break; + } + default: res = -1; /* invalid option */ + } + lua_unlock(L); + return res; +} + + + +/* +** miscellaneous functions +*/ + + +LUA_API int lua_error (lua_State *L) { + lua_lock(L); + api_checknelems(L, 1); + luaG_errormsg(L); + lua_unlock(L); + return 0; /* to avoid warnings */ +} + + +LUA_API int lua_next (lua_State *L, int idx) { + StkId t; + int more; + lua_lock(L); + t = index2adr(L, idx); + api_check(L, ttistable(t)); + more = luaH_next(L, hvalue(t), L->top - 1); + if (more) { + api_incr_top(L); + } + else /* no more elements */ + L->top -= 1; /* remove key */ + lua_unlock(L); + return more; +} + + +LUA_API void lua_concat (lua_State *L, int n) { + lua_lock(L); + api_checknelems(L, n); + if (n >= 2) { + luaC_checkGC(L); + luaV_concat(L, n, cast_int(L->top - L->base) - 1); + L->top -= (n-1); + } + else if (n == 0) { /* push empty string */ + setsvalue2s(L, L->top, luaS_newlstr(L, "", 0)); + api_incr_top(L); + } + /* else n == 1; nothing to do */ + lua_unlock(L); +} + + +LUA_API lua_Alloc lua_getallocf (lua_State *L, void **ud) { + lua_Alloc f; + lua_lock(L); + if (ud) *ud = G(L)->ud; + f = G(L)->frealloc; + lua_unlock(L); + return f; +} + + +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud) { + lua_lock(L); + G(L)->ud = ud; + G(L)->frealloc = f; + lua_unlock(L); +} + + +LUA_API void *lua_newuserdata (lua_State *L, size_t size) { + Udata *u; + lua_lock(L); + luaC_checkGC(L); + u = luaS_newudata(L, size, getcurrenv(L)); + setuvalue(L, L->top, u); + api_incr_top(L); + lua_unlock(L); + return u + 1; +} + + + + +static const char *aux_upvalue (StkId fi, int n, TValue **val) { + Closure *f; + if (!ttisfunction(fi)) return NULL; + f = clvalue(fi); + if (f->c.isC) { + if (!(1 <= n && n <= f->c.nupvalues)) return NULL; + *val = &f->c.upvalue[n-1]; + return ""; + } + else { + Proto *p = f->l.p; + if (!(1 <= n && n <= p->sizeupvalues)) return NULL; + *val = f->l.upvals[n-1]->v; + return getstr(p->upvalues[n-1]); + } +} + + +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n) { + const char *name; + TValue *val; + lua_lock(L); + name = aux_upvalue(index2adr(L, funcindex), n, &val); + if (name) { + setobj2s(L, L->top, val); + api_incr_top(L); + } + lua_unlock(L); + return name; +} + + +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { + const char *name; + TValue *val; + StkId fi; + lua_lock(L); + fi = index2adr(L, funcindex); + api_checknelems(L, 1); + name = aux_upvalue(fi, n, &val); + if (name) { + L->top--; + setobj(L, val, L->top); + luaC_barrier(L, clvalue(fi), L->top); + } + lua_unlock(L); + return name; +} + diff --git a/external/lua/lua/lapi.h b/external/lua/lua/lapi.h new file mode 100644 index 0000000000..2c3fab244e --- /dev/null +++ b/external/lua/lua/lapi.h @@ -0,0 +1,16 @@ +/* +** $Id: lapi.h,v 2.2.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions from Lua API +** See Copyright Notice in lua.h +*/ + +#ifndef lapi_h +#define lapi_h + + +#include "lobject.h" + + +LUAI_FUNC void luaA_pushobject (lua_State *L, const TValue *o); + +#endif diff --git a/external/lua/lua/lauxlib.c b/external/lua/lua/lauxlib.c new file mode 100644 index 0000000000..10f14e2c08 --- /dev/null +++ b/external/lua/lua/lauxlib.c @@ -0,0 +1,652 @@ +/* +** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include +#include +#include + + +/* This file uses only the official API of Lua. +** Any function declared here could be written as an application function. +*/ + +#define lauxlib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" + + +#define FREELIST_REF 0 /* free list of references */ + + +/* convert a stack index to positive */ +#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ + lua_gettop(L) + (i) + 1) + + +/* +** {====================================================== +** Error-report functions +** ======================================================= +*/ + + +LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) { + lua_Debug ar; + if (!lua_getstack(L, 0, &ar)) /* no stack frame? */ + return luaL_error(L, "bad argument #%d (%s)", narg, extramsg); + lua_getinfo(L, "n", &ar); + if (strcmp(ar.namewhat, "method") == 0) { + narg--; /* do not count `self' */ + if (narg == 0) /* error is in the self argument itself? */ + return luaL_error(L, "calling " LUA_QS " on bad self (%s)", + ar.name, extramsg); + } + if (ar.name == NULL) + ar.name = "?"; + return luaL_error(L, "bad argument #%d to " LUA_QS " (%s)", + narg, ar.name, extramsg); +} + + +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { + const char *msg = lua_pushfstring(L, "%s expected, got %s", + tname, luaL_typename(L, narg)); + return luaL_argerror(L, narg, msg); +} + + +static void tag_error (lua_State *L, int narg, int tag) { + luaL_typerror(L, narg, lua_typename(L, tag)); +} + + +LUALIB_API void luaL_where (lua_State *L, int level) { + lua_Debug ar; + if (lua_getstack(L, level, &ar)) { /* check function at level */ + lua_getinfo(L, "Sl", &ar); /* get info about it */ + if (ar.currentline > 0) { /* is there info? */ + lua_pushfstring(L, "%s:%d: ", ar.short_src, ar.currentline); + return; + } + } + lua_pushliteral(L, ""); /* else, no information available... */ +} + + +LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { + va_list argp; + va_start(argp, fmt); + luaL_where(L, 1); + lua_pushvfstring(L, fmt, argp); + va_end(argp); + lua_concat(L, 2); + return lua_error(L); +} + +/* }====================================================== */ + + +LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, + const char *const lst[]) { + const char *name = (def) ? luaL_optstring(L, narg, def) : + luaL_checkstring(L, narg); + int i; + for (i=0; lst[i]; i++) + if (strcmp(lst[i], name) == 0) + return i; + return luaL_argerror(L, narg, + lua_pushfstring(L, "invalid option " LUA_QS, name)); +} + + +LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) { + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get registry.name */ + if (!lua_isnil(L, -1)) /* name already in use? */ + return 0; /* leave previous value on top, but return 0 */ + lua_pop(L, 1); + lua_newtable(L); /* create metatable */ + lua_pushvalue(L, -1); + lua_setfield(L, LUA_REGISTRYINDEX, tname); /* registry.name = metatable */ + return 1; +} + + +LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) { + void *p = lua_touserdata(L, ud); + if (p != NULL) { /* value is a userdata? */ + if (lua_getmetatable(L, ud)) { /* does it have a metatable? */ + lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */ + if (lua_rawequal(L, -1, -2)) { /* does it have the correct mt? */ + lua_pop(L, 2); /* remove both metatables */ + return p; + } + } + } + luaL_typerror(L, ud, tname); /* else error */ + return NULL; /* to avoid warnings */ +} + + +LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) { + if (!lua_checkstack(L, space)) + luaL_error(L, "stack overflow (%s)", mes); +} + + +LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) { + if (lua_type(L, narg) != t) + tag_error(L, narg, t); +} + + +LUALIB_API void luaL_checkany (lua_State *L, int narg) { + if (lua_type(L, narg) == LUA_TNONE) + luaL_argerror(L, narg, "value expected"); +} + + +LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) { + const char *s = lua_tolstring(L, narg, len); + if (!s) tag_error(L, narg, LUA_TSTRING); + return s; +} + + +LUALIB_API const char *luaL_optlstring (lua_State *L, int narg, + const char *def, size_t *len) { + if (lua_isnoneornil(L, narg)) { + if (len) + *len = (def ? strlen(def) : 0); + return def; + } + else return luaL_checklstring(L, narg, len); +} + + +LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) { + lua_Number d = lua_tonumber(L, narg); + if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ + tag_error(L, narg, LUA_TNUMBER); + return d; +} + + +LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) { + return luaL_opt(L, luaL_checknumber, narg, def); +} + + +LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { + lua_Integer d = lua_tointeger(L, narg); + if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ + tag_error(L, narg, LUA_TNUMBER); + return d; +} + + +LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, + lua_Integer def) { + return luaL_opt(L, luaL_checkinteger, narg, def); +} + + +LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) { + if (!lua_getmetatable(L, obj)) /* no metatable? */ + return 0; + lua_pushstring(L, event); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) { + lua_pop(L, 2); /* remove metatable and metafield */ + return 0; + } + else { + lua_remove(L, -2); /* remove only metatable */ + return 1; + } +} + + +LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { + obj = abs_index(L, obj); + if (!luaL_getmetafield(L, obj, event)) /* no metafield? */ + return 0; + lua_pushvalue(L, obj); + lua_call(L, 1, 1); + return 1; +} + + +LUALIB_API void (luaL_register) (lua_State *L, const char *libname, + const luaL_Reg *l) { + luaI_openlib(L, libname, l, 0); +} + + +static int libsize (const luaL_Reg *l) { + int size = 0; + for (; l->name; l++) size++; + return size; +} + + +LUALIB_API void luaI_openlib (lua_State *L, const char *libname, + const luaL_Reg *l, int nup) { + if (libname) { + int size = libsize(l); + /* check whether lib already exists */ + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); + lua_getfield(L, -1, libname); /* get _LOADED[libname] */ + if (!lua_istable(L, -1)) { /* not found? */ + lua_pop(L, 1); /* remove previous result */ + /* try global variable (and create one if it does not exist) */ + if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) + luaL_error(L, "name conflict for module " LUA_QS, libname); + lua_pushvalue(L, -1); + lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ + } + lua_remove(L, -2); /* remove _LOADED table */ + lua_insert(L, -(nup+1)); /* move library table to below upvalues */ + } + for (; l->name; l++) { + int i; + for (i=0; ifunc, nup); + lua_setfield(L, -(nup+2), l->name); + } + lua_pop(L, nup); /* remove upvalues */ +} + + + +/* +** {====================================================== +** getn-setn: size for arrays +** ======================================================= +*/ + +#if defined(LUA_COMPAT_GETN) + +static int checkint (lua_State *L, int topop) { + int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1; + lua_pop(L, topop); + return n; +} + + +static void getsizes (lua_State *L) { + lua_getfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); + if (lua_isnil(L, -1)) { /* no `size' table? */ + lua_pop(L, 1); /* remove nil */ + lua_newtable(L); /* create it */ + lua_pushvalue(L, -1); /* `size' will be its own metatable */ + lua_setmetatable(L, -2); + lua_pushliteral(L, "kv"); + lua_setfield(L, -2, "__mode"); /* metatable(N).__mode = "kv" */ + lua_pushvalue(L, -1); + lua_setfield(L, LUA_REGISTRYINDEX, "LUA_SIZES"); /* store in register */ + } +} + + +LUALIB_API void luaL_setn (lua_State *L, int t, int n) { + t = abs_index(L, t); + lua_pushliteral(L, "n"); + lua_rawget(L, t); + if (checkint(L, 1) >= 0) { /* is there a numeric field `n'? */ + lua_pushliteral(L, "n"); /* use it */ + lua_pushinteger(L, n); + lua_rawset(L, t); + } + else { /* use `sizes' */ + getsizes(L); + lua_pushvalue(L, t); + lua_pushinteger(L, n); + lua_rawset(L, -3); /* sizes[t] = n */ + lua_pop(L, 1); /* remove `sizes' */ + } +} + + +LUALIB_API int luaL_getn (lua_State *L, int t) { + int n; + t = abs_index(L, t); + lua_pushliteral(L, "n"); /* try t.n */ + lua_rawget(L, t); + if ((n = checkint(L, 1)) >= 0) return n; + getsizes(L); /* else try sizes[t] */ + lua_pushvalue(L, t); + lua_rawget(L, -2); + if ((n = checkint(L, 2)) >= 0) return n; + return (int)lua_objlen(L, t); +} + +#endif + +/* }====================================================== */ + + + +LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, + const char *r) { + const char *wild; + size_t l = strlen(p); + luaL_Buffer b; + luaL_buffinit(L, &b); + while ((wild = strstr(s, p)) != NULL) { + luaL_addlstring(&b, s, wild - s); /* push prefix */ + luaL_addstring(&b, r); /* push replacement in place of pattern */ + s = wild + l; /* continue after `p' */ + } + luaL_addstring(&b, s); /* push last suffix */ + luaL_pushresult(&b); + return lua_tostring(L, -1); +} + + +LUALIB_API const char *luaL_findtable (lua_State *L, int idx, + const char *fname, int szhint) { + const char *e; + lua_pushvalue(L, idx); + do { + e = strchr(fname, '.'); + if (e == NULL) e = fname + strlen(fname); + lua_pushlstring(L, fname, e - fname); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) { /* no such field? */ + lua_pop(L, 1); /* remove this nil */ + lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ + lua_pushlstring(L, fname, e - fname); + lua_pushvalue(L, -2); + lua_settable(L, -4); /* set new table into field */ + } + else if (!lua_istable(L, -1)) { /* field has a non-table value? */ + lua_pop(L, 2); /* remove table and value */ + return fname; /* return problematic part of the name */ + } + lua_remove(L, -2); /* remove previous table */ + fname = e + 1; + } while (*e == '.'); + return NULL; +} + + + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + + +#define bufflen(B) ((B)->p - (B)->buffer) +#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) + +#define LIMIT (LUA_MINSTACK/2) + + +static int emptybuffer (luaL_Buffer *B) { + size_t l = bufflen(B); + if (l == 0) return 0; /* put nothing on stack */ + else { + lua_pushlstring(B->L, B->buffer, l); + B->p = B->buffer; + B->lvl++; + return 1; + } +} + + +static void adjuststack (luaL_Buffer *B) { + if (B->lvl > 1) { + lua_State *L = B->L; + int toget = 1; /* number of levels to concat */ + size_t toplen = lua_strlen(L, -1); + do { + size_t l = lua_strlen(L, -(toget+1)); + if (B->lvl - toget + 1 >= LIMIT || toplen > l) { + toplen += l; + toget++; + } + else break; + } while (toget < B->lvl); + lua_concat(L, toget); + B->lvl = B->lvl - toget + 1; + } +} + + +LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) { + if (emptybuffer(B)) + adjuststack(B); + return B->buffer; +} + + +LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { + while (l--) + luaL_addchar(B, *s++); +} + + +LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) { + luaL_addlstring(B, s, strlen(s)); +} + + +LUALIB_API void luaL_pushresult (luaL_Buffer *B) { + emptybuffer(B); + lua_concat(B->L, B->lvl); + B->lvl = 1; +} + + +LUALIB_API void luaL_addvalue (luaL_Buffer *B) { + lua_State *L = B->L; + size_t vl; + const char *s = lua_tolstring(L, -1, &vl); + if (vl <= bufffree(B)) { /* fit into buffer? */ + memcpy(B->p, s, vl); /* put it there */ + B->p += vl; + lua_pop(L, 1); /* remove from stack */ + } + else { + if (emptybuffer(B)) + lua_insert(L, -2); /* put buffer before new value */ + B->lvl++; /* add new value into B stack */ + adjuststack(B); + } +} + + +LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) { + B->L = L; + B->p = B->buffer; + B->lvl = 0; +} + +/* }====================================================== */ + + +LUALIB_API int luaL_ref (lua_State *L, int t) { + int ref; + t = abs_index(L, t); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); /* remove from stack */ + return LUA_REFNIL; /* `nil' has a unique fixed reference */ + } + lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ + ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ + lua_pop(L, 1); /* remove it from stack */ + if (ref != 0) { /* any free element? */ + lua_rawgeti(L, t, ref); /* remove it from list */ + lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ + } + else { /* no free elements */ + ref = (int)lua_objlen(L, t); + ref++; /* create new reference */ + } + lua_rawseti(L, t, ref); + return ref; +} + + +LUALIB_API void luaL_unref (lua_State *L, int t, int ref) { + if (ref >= 0) { + t = abs_index(L, t); + lua_rawgeti(L, t, FREELIST_REF); + lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ + lua_pushinteger(L, ref); + lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ + } +} + + + +/* +** {====================================================== +** Load functions +** ======================================================= +*/ + +typedef struct LoadF { + int extraline; + FILE *f; + char buff[LUAL_BUFFERSIZE]; +} LoadF; + + +static const char *getF (lua_State *L, void *ud, size_t *size) { + LoadF *lf = (LoadF *)ud; + (void)L; + if (lf->extraline) { + lf->extraline = 0; + *size = 1; + return "\n"; + } + if (feof(lf->f)) return NULL; + *size = fread(lf->buff, 1, sizeof(lf->buff), lf->f); + return (*size > 0) ? lf->buff : NULL; +} + + +static int errfile (lua_State *L, const char *what, int fnameindex) { + const char *serr = strerror(errno); + const char *filename = lua_tostring(L, fnameindex) + 1; + lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr); + lua_remove(L, fnameindex); + return LUA_ERRFILE; +} + + +LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { + LoadF lf; + int status, readstatus; + int c; + int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ + lf.extraline = 0; + if (filename == NULL) { + lua_pushliteral(L, "=stdin"); + lf.f = stdin; + } + else { + lua_pushfstring(L, "@%s", filename); + lf.f = fopen(filename, "r"); + if (lf.f == NULL) return errfile(L, "open", fnameindex); + } + c = getc(lf.f); + if (c == '#') { /* Unix exec. file? */ + lf.extraline = 1; + while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ + if (c == '\n') c = getc(lf.f); + } + if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ + lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ + if (lf.f == NULL) return errfile(L, "reopen", fnameindex); + /* skip eventual `#!...' */ + while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; + lf.extraline = 0; + } + ungetc(c, lf.f); + status = lua_load(L, getF, &lf, lua_tostring(L, -1)); + readstatus = ferror(lf.f); + if (filename) fclose(lf.f); /* close file (even in case of errors) */ + if (readstatus) { + lua_settop(L, fnameindex); /* ignore results from `lua_load' */ + return errfile(L, "read", fnameindex); + } + lua_remove(L, fnameindex); + return status; +} + + +typedef struct LoadS { + const char *s; + size_t size; +} LoadS; + + +static const char *getS (lua_State *L, void *ud, size_t *size) { + LoadS *ls = (LoadS *)ud; + (void)L; + if (ls->size == 0) return NULL; + *size = ls->size; + ls->size = 0; + return ls->s; +} + + +LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size, + const char *name) { + LoadS ls; + ls.s = buff; + ls.size = size; + return lua_load(L, getS, &ls, name); +} + + +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) { + return luaL_loadbuffer(L, s, strlen(s), s); +} + + + +/* }====================================================== */ + + +static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { + (void)ud; + (void)osize; + if (nsize == 0) { + free(ptr); + return NULL; + } + else + return realloc(ptr, nsize); +} + + +static int panic (lua_State *L) { + (void)L; /* to avoid warnings */ + fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", + lua_tostring(L, -1)); + return 0; +} + + +LUALIB_API lua_State *luaL_newstate (void) { + lua_State *L = lua_newstate(l_alloc, NULL); + if (L) lua_atpanic(L, &panic); + return L; +} + diff --git a/external/lua/lua/lauxlib.h b/external/lua/lua/lauxlib.h new file mode 100644 index 0000000000..34258235db --- /dev/null +++ b/external/lua/lua/lauxlib.h @@ -0,0 +1,174 @@ +/* +** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lauxlib_h +#define lauxlib_h + + +#include +#include + +#include "lua.h" + + +#if defined(LUA_COMPAT_GETN) +LUALIB_API int (luaL_getn) (lua_State *L, int t); +LUALIB_API void (luaL_setn) (lua_State *L, int t, int n); +#else +#define luaL_getn(L,i) ((int)lua_objlen(L, i)) +#define luaL_setn(L,i,j) ((void)0) /* no op! */ +#endif + +#if defined(LUA_COMPAT_OPENLIB) +#define luaI_openlib luaL_openlib +#endif + + +/* extra error code for `luaL_load' */ +#define LUA_ERRFILE (LUA_ERRERR+1) + + +typedef struct luaL_Reg { + const char *name; + lua_CFunction func; +} luaL_Reg; + + + +LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname, + const luaL_Reg *l, int nup); +LUALIB_API void (luaL_register) (lua_State *L, const char *libname, + const luaL_Reg *l); +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); +LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, + size_t *l); +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, + const char *def, size_t *l); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); + +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, + lua_Integer def); + +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); +LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int narg); + +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); + +LUALIB_API void (luaL_where) (lua_State *L, int lvl); +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); + +LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, + const char *const lst[]); + +LUALIB_API int (luaL_ref) (lua_State *L, int t); +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); + +LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); +LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, + const char *name); +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); + +LUALIB_API lua_State *(luaL_newstate) (void); + + +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, + const char *r); + +LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, + const char *fname, int szhint); + + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define luaL_argcheck(L, cond,numarg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) + +#define luaL_dofile(L, fn) \ + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_dostring(L, s) \ + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) + +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + + + +typedef struct luaL_Buffer { + char *p; /* current position in buffer */ + int lvl; /* number of strings in the stack (level) */ + lua_State *L; + char buffer[LUAL_BUFFERSIZE]; +} luaL_Buffer; + +#define luaL_addchar(B,c) \ + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ + (*(B)->p++ = (char)(c))) + +/* compatibility only */ +#define luaL_putchar(B,c) luaL_addchar(B,c) + +#define luaL_addsize(B,n) ((B)->p += (n)) + +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); +LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); + + +/* }====================================================== */ + + +/* compatibility with ref system */ + +/* pre-defined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ + (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) + +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) + +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) + + +#define luaL_reg luaL_Reg + +#endif + + diff --git a/external/lua/lua/lbaselib.c b/external/lua/lua/lbaselib.c new file mode 100644 index 0000000000..2ab550bd48 --- /dev/null +++ b/external/lua/lua/lbaselib.c @@ -0,0 +1,653 @@ +/* +** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $ +** Basic library +** See Copyright Notice in lua.h +*/ + + + +#include +#include +#include +#include + +#define lbaselib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + + + +/* +** If your system does not support `stdout', you can just remove this function. +** If you need, you can define your own `print' function, following this +** model but changing `fputs' to put the strings at a proper place +** (a console window or a log file, for instance). +*/ +static int luaB_print (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + int i; + lua_getglobal(L, "tostring"); + for (i=1; i<=n; i++) { + const char *s; + lua_pushvalue(L, -1); /* function to be called */ + lua_pushvalue(L, i); /* value to print */ + lua_call(L, 1, 1); + s = lua_tostring(L, -1); /* get result */ + if (s == NULL) + return luaL_error(L, LUA_QL("tostring") " must return a string to " + LUA_QL("print")); + if (i>1) fputs("\t", stdout); + fputs(s, stdout); + lua_pop(L, 1); /* pop result */ + } + fputs("\n", stdout); + return 0; +} + + +static int luaB_tonumber (lua_State *L) { + int base = luaL_optint(L, 2, 10); + if (base == 10) { /* standard conversion */ + luaL_checkany(L, 1); + if (lua_isnumber(L, 1)) { + lua_pushnumber(L, lua_tonumber(L, 1)); + return 1; + } + } + else { + const char *s1 = luaL_checkstring(L, 1); + char *s2; + unsigned long n; + luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); + n = strtoul(s1, &s2, base); + if (s1 != s2) { /* at least one valid digit? */ + while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ + if (*s2 == '\0') { /* no invalid trailing characters? */ + lua_pushnumber(L, (lua_Number)n); + return 1; + } + } + } + lua_pushnil(L); /* else not a number */ + return 1; +} + + +static int luaB_error (lua_State *L) { + int level = luaL_optint(L, 2, 1); + lua_settop(L, 1); + if (lua_isstring(L, 1) && level > 0) { /* add extra information? */ + luaL_where(L, level); + lua_pushvalue(L, 1); + lua_concat(L, 2); + } + return lua_error(L); +} + + +static int luaB_getmetatable (lua_State *L) { + luaL_checkany(L, 1); + if (!lua_getmetatable(L, 1)) { + lua_pushnil(L); + return 1; /* no metatable */ + } + luaL_getmetafield(L, 1, "__metatable"); + return 1; /* returns either __metatable field (if present) or metatable */ +} + + +static int luaB_setmetatable (lua_State *L) { + int t = lua_type(L, 2); + luaL_checktype(L, 1, LUA_TTABLE); + luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, + "nil or table expected"); + if (luaL_getmetafield(L, 1, "__metatable")) + luaL_error(L, "cannot change a protected metatable"); + lua_settop(L, 2); + lua_setmetatable(L, 1); + return 1; +} + + +static void getfunc (lua_State *L, int opt) { + if (lua_isfunction(L, 1)) lua_pushvalue(L, 1); + else { + lua_Debug ar; + int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1); + luaL_argcheck(L, level >= 0, 1, "level must be non-negative"); + if (lua_getstack(L, level, &ar) == 0) + luaL_argerror(L, 1, "invalid level"); + lua_getinfo(L, "f", &ar); + if (lua_isnil(L, -1)) + luaL_error(L, "no function environment for tail call at level %d", + level); + } +} + + +static int luaB_getfenv (lua_State *L) { + getfunc(L, 1); + if (lua_iscfunction(L, -1)) /* is a C function? */ + lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */ + else + lua_getfenv(L, -1); + return 1; +} + + +static int luaB_setfenv (lua_State *L) { + luaL_checktype(L, 2, LUA_TTABLE); + getfunc(L, 0); + lua_pushvalue(L, 2); + if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) { + /* change environment of current thread */ + lua_pushthread(L); + lua_insert(L, -2); + lua_setfenv(L, -2); + return 0; + } + else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0) + luaL_error(L, + LUA_QL("setfenv") " cannot change environment of given object"); + return 1; +} + + +static int luaB_rawequal (lua_State *L) { + luaL_checkany(L, 1); + luaL_checkany(L, 2); + lua_pushboolean(L, lua_rawequal(L, 1, 2)); + return 1; +} + + +static int luaB_rawget (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checkany(L, 2); + lua_settop(L, 2); + lua_rawget(L, 1); + return 1; +} + +static int luaB_rawset (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checkany(L, 2); + luaL_checkany(L, 3); + lua_settop(L, 3); + lua_rawset(L, 1); + return 1; +} + + +static int luaB_gcinfo (lua_State *L) { + lua_pushinteger(L, lua_getgccount(L)); + return 1; +} + + +static int luaB_collectgarbage (lua_State *L) { + static const char *const opts[] = {"stop", "restart", "collect", + "count", "step", "setpause", "setstepmul", NULL}; + static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, + LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; + int o = luaL_checkoption(L, 1, "collect", opts); + int ex = luaL_optint(L, 2, 0); + int res = lua_gc(L, optsnum[o], ex); + switch (optsnum[o]) { + case LUA_GCCOUNT: { + int b = lua_gc(L, LUA_GCCOUNTB, 0); + lua_pushnumber(L, res + ((lua_Number)b/1024)); + return 1; + } + case LUA_GCSTEP: { + lua_pushboolean(L, res); + return 1; + } + default: { + lua_pushnumber(L, res); + return 1; + } + } +} + + +static int luaB_type (lua_State *L) { + luaL_checkany(L, 1); + lua_pushstring(L, luaL_typename(L, 1)); + return 1; +} + + +static int luaB_next (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + lua_settop(L, 2); /* create a 2nd argument if there isn't one */ + if (lua_next(L, 1)) + return 2; + else { + lua_pushnil(L); + return 1; + } +} + + +static int luaB_pairs (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ + lua_pushvalue(L, 1); /* state, */ + lua_pushnil(L); /* and initial value */ + return 3; +} + + +static int ipairsaux (lua_State *L) { + int i = luaL_checkint(L, 2); + luaL_checktype(L, 1, LUA_TTABLE); + i++; /* next value */ + lua_pushinteger(L, i); + lua_rawgeti(L, 1, i); + return (lua_isnil(L, -1)) ? 0 : 2; +} + + +static int luaB_ipairs (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ + lua_pushvalue(L, 1); /* state, */ + lua_pushinteger(L, 0); /* and initial value */ + return 3; +} + + +static int load_aux (lua_State *L, int status) { + if (status == 0) /* OK? */ + return 1; + else { + lua_pushnil(L); + lua_insert(L, -2); /* put before error message */ + return 2; /* return nil plus error message */ + } +} + + +static int luaB_loadstring (lua_State *L) { + size_t l; + const char *s = luaL_checklstring(L, 1, &l); + const char *chunkname = luaL_optstring(L, 2, s); + return load_aux(L, luaL_loadbuffer(L, s, l, chunkname)); +} + + +static int luaB_loadfile (lua_State *L) { + const char *fname = luaL_optstring(L, 1, NULL); + return load_aux(L, luaL_loadfile(L, fname)); +} + + +/* +** Reader for generic `load' function: `lua_load' uses the +** stack for internal stuff, so the reader cannot change the +** stack top. Instead, it keeps its resulting string in a +** reserved slot inside the stack. +*/ +static const char *generic_reader (lua_State *L, void *ud, size_t *size) { + (void)ud; /* to avoid warnings */ + luaL_checkstack(L, 2, "too many nested functions"); + lua_pushvalue(L, 1); /* get function */ + lua_call(L, 0, 1); /* call it */ + if (lua_isnil(L, -1)) { + *size = 0; + return NULL; + } + else if (lua_isstring(L, -1)) { + lua_replace(L, 3); /* save string in a reserved stack slot */ + return lua_tolstring(L, 3, size); + } + else luaL_error(L, "reader function must return a string"); + return NULL; /* to avoid warnings */ +} + + +static int luaB_load (lua_State *L) { + int status; + const char *cname = luaL_optstring(L, 2, "=(load)"); + luaL_checktype(L, 1, LUA_TFUNCTION); + lua_settop(L, 3); /* function, eventual name, plus one reserved slot */ + status = lua_load(L, generic_reader, NULL, cname); + return load_aux(L, status); +} + + +static int luaB_dofile (lua_State *L) { + const char *fname = luaL_optstring(L, 1, NULL); + int n = lua_gettop(L); + if (luaL_loadfile(L, fname) != 0) lua_error(L); + lua_call(L, 0, LUA_MULTRET); + return lua_gettop(L) - n; +} + + +static int luaB_assert (lua_State *L) { + luaL_checkany(L, 1); + if (!lua_toboolean(L, 1)) + return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!")); + return lua_gettop(L); +} + + +static int luaB_unpack (lua_State *L) { + int i, e, n; + luaL_checktype(L, 1, LUA_TTABLE); + i = luaL_optint(L, 2, 1); + e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); + if (i > e) return 0; /* empty range */ + n = e - i + 1; /* number of elements */ + if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */ + return luaL_error(L, "too many results to unpack"); + lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ + while (i++ < e) /* push arg[i + 1...e] */ + lua_rawgeti(L, 1, i); + return n; +} + + +static int luaB_select (lua_State *L) { + int n = lua_gettop(L); + if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') { + lua_pushinteger(L, n-1); + return 1; + } + else { + int i = luaL_checkint(L, 1); + if (i < 0) i = n + i; + else if (i > n) i = n; + luaL_argcheck(L, 1 <= i, 1, "index out of range"); + return n - i; + } +} + + +static int luaB_pcall (lua_State *L) { + int status; + luaL_checkany(L, 1); + status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0); + lua_pushboolean(L, (status == 0)); + lua_insert(L, 1); + return lua_gettop(L); /* return status + all results */ +} + + +static int luaB_xpcall (lua_State *L) { + int status; + luaL_checkany(L, 2); + lua_settop(L, 2); + lua_insert(L, 1); /* put error function under function to be called */ + status = lua_pcall(L, 0, LUA_MULTRET, 1); + lua_pushboolean(L, (status == 0)); + lua_replace(L, 1); + return lua_gettop(L); /* return status + all results */ +} + + +static int luaB_tostring (lua_State *L) { + luaL_checkany(L, 1); + if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ + return 1; /* use its value */ + switch (lua_type(L, 1)) { + case LUA_TNUMBER: + lua_pushstring(L, lua_tostring(L, 1)); + break; + case LUA_TSTRING: + lua_pushvalue(L, 1); + break; + case LUA_TBOOLEAN: + lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); + break; + case LUA_TNIL: + lua_pushliteral(L, "nil"); + break; + default: + lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1)); + break; + } + return 1; +} + + +static int luaB_newproxy (lua_State *L) { + lua_settop(L, 1); + lua_newuserdata(L, 0); /* create proxy */ + if (lua_toboolean(L, 1) == 0) + return 1; /* no metatable */ + else if (lua_isboolean(L, 1)) { + lua_newtable(L); /* create a new metatable `m' ... */ + lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */ + lua_pushboolean(L, 1); + lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */ + } + else { + int validproxy = 0; /* to check if weaktable[metatable(u)] == true */ + if (lua_getmetatable(L, 1)) { + lua_rawget(L, lua_upvalueindex(1)); + validproxy = lua_toboolean(L, -1); + lua_pop(L, 1); /* remove value */ + } + luaL_argcheck(L, validproxy, 1, "boolean or proxy expected"); + lua_getmetatable(L, 1); /* metatable is valid; get it */ + } + lua_setmetatable(L, 2); + return 1; +} + + +static const luaL_Reg base_funcs[] = { + {"assert", luaB_assert}, + {"collectgarbage", luaB_collectgarbage}, + {"dofile", luaB_dofile}, + {"error", luaB_error}, + {"gcinfo", luaB_gcinfo}, + {"getfenv", luaB_getfenv}, + {"getmetatable", luaB_getmetatable}, + {"loadfile", luaB_loadfile}, + {"load", luaB_load}, + {"loadstring", luaB_loadstring}, + {"next", luaB_next}, + {"pcall", luaB_pcall}, + {"print", luaB_print}, + {"rawequal", luaB_rawequal}, + {"rawget", luaB_rawget}, + {"rawset", luaB_rawset}, + {"select", luaB_select}, + {"setfenv", luaB_setfenv}, + {"setmetatable", luaB_setmetatable}, + {"tonumber", luaB_tonumber}, + {"tostring", luaB_tostring}, + {"type", luaB_type}, + {"unpack", luaB_unpack}, + {"xpcall", luaB_xpcall}, + {NULL, NULL} +}; + + +/* +** {====================================================== +** Coroutine library +** ======================================================= +*/ + +#define CO_RUN 0 /* running */ +#define CO_SUS 1 /* suspended */ +#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */ +#define CO_DEAD 3 + +static const char *const statnames[] = + {"running", "suspended", "normal", "dead"}; + +static int costatus (lua_State *L, lua_State *co) { + if (L == co) return CO_RUN; + switch (lua_status(co)) { + case LUA_YIELD: + return CO_SUS; + case 0: { + lua_Debug ar; + if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ + return CO_NOR; /* it is running */ + else if (lua_gettop(co) == 0) + return CO_DEAD; + else + return CO_SUS; /* initial state */ + } + default: /* some error occured */ + return CO_DEAD; + } +} + + +static int luaB_costatus (lua_State *L) { + lua_State *co = lua_tothread(L, 1); + luaL_argcheck(L, co, 1, "coroutine expected"); + lua_pushstring(L, statnames[costatus(L, co)]); + return 1; +} + + +static int auxresume (lua_State *L, lua_State *co, int narg) { + int status = costatus(L, co); + if (!lua_checkstack(co, narg)) + luaL_error(L, "too many arguments to resume"); + if (status != CO_SUS) { + lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]); + return -1; /* error flag */ + } + lua_xmove(L, co, narg); + lua_setlevel(L, co); + status = lua_resume(co, narg); + if (status == 0 || status == LUA_YIELD) { + int nres = lua_gettop(co); + if (!lua_checkstack(L, nres + 1)) + luaL_error(L, "too many results to resume"); + lua_xmove(co, L, nres); /* move yielded values */ + return nres; + } + else { + lua_xmove(co, L, 1); /* move error message */ + return -1; /* error flag */ + } +} + + +static int luaB_coresume (lua_State *L) { + lua_State *co = lua_tothread(L, 1); + int r; + luaL_argcheck(L, co, 1, "coroutine expected"); + r = auxresume(L, co, lua_gettop(L) - 1); + if (r < 0) { + lua_pushboolean(L, 0); + lua_insert(L, -2); + return 2; /* return false + error message */ + } + else { + lua_pushboolean(L, 1); + lua_insert(L, -(r + 1)); + return r + 1; /* return true + `resume' returns */ + } +} + + +static int luaB_auxwrap (lua_State *L) { + lua_State *co = lua_tothread(L, lua_upvalueindex(1)); + int r = auxresume(L, co, lua_gettop(L)); + if (r < 0) { + if (lua_isstring(L, -1)) { /* error object is a string? */ + luaL_where(L, 1); /* add extra info */ + lua_insert(L, -2); + lua_concat(L, 2); + } + lua_error(L); /* propagate error */ + } + return r; +} + + +static int luaB_cocreate (lua_State *L) { + lua_State *NL = lua_newthread(L); + luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, + "Lua function expected"); + lua_pushvalue(L, 1); /* move function to top */ + lua_xmove(L, NL, 1); /* move function from L to NL */ + return 1; +} + + +static int luaB_cowrap (lua_State *L) { + luaB_cocreate(L); + lua_pushcclosure(L, luaB_auxwrap, 1); + return 1; +} + + +static int luaB_yield (lua_State *L) { + return lua_yield(L, lua_gettop(L)); +} + + +static int luaB_corunning (lua_State *L) { + if (lua_pushthread(L)) + lua_pushnil(L); /* main thread is not a coroutine */ + return 1; +} + + +static const luaL_Reg co_funcs[] = { + {"create", luaB_cocreate}, + {"resume", luaB_coresume}, + {"running", luaB_corunning}, + {"status", luaB_costatus}, + {"wrap", luaB_cowrap}, + {"yield", luaB_yield}, + {NULL, NULL} +}; + +/* }====================================================== */ + + +static void auxopen (lua_State *L, const char *name, + lua_CFunction f, lua_CFunction u) { + lua_pushcfunction(L, u); + lua_pushcclosure(L, f, 1); + lua_setfield(L, -2, name); +} + + +static void base_open (lua_State *L) { + /* set global _G */ + lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_setglobal(L, "_G"); + /* open lib into global table */ + luaL_register(L, "_G", base_funcs); + lua_pushliteral(L, LUA_VERSION); + lua_setglobal(L, "_VERSION"); /* set global _VERSION */ + /* `ipairs' and `pairs' need auxiliary functions as upvalues */ + auxopen(L, "ipairs", luaB_ipairs, ipairsaux); + auxopen(L, "pairs", luaB_pairs, luaB_next); + /* `newproxy' needs a weaktable as upvalue */ + lua_createtable(L, 0, 1); /* new table `w' */ + lua_pushvalue(L, -1); /* `w' will be its own metatable */ + lua_setmetatable(L, -2); + lua_pushliteral(L, "kv"); + lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ + lua_pushcclosure(L, luaB_newproxy, 1); + lua_setglobal(L, "newproxy"); /* set global `newproxy' */ +} + + +LUALIB_API int luaopen_base (lua_State *L) { + base_open(L); + luaL_register(L, LUA_COLIBNAME, co_funcs); + return 2; +} + diff --git a/external/lua/lua/lcode.c b/external/lua/lua/lcode.c new file mode 100644 index 0000000000..679cb9cfd9 --- /dev/null +++ b/external/lua/lua/lcode.c @@ -0,0 +1,831 @@ +/* +** $Id: lcode.c,v 2.25.1.5 2011/01/31 14:53:16 roberto Exp $ +** Code generator for Lua +** See Copyright Notice in lua.h +*/ + + +#include + +#define lcode_c +#define LUA_CORE + +#include "lua.h" + +#include "lcode.h" +#include "ldebug.h" +#include "ldo.h" +#include "lgc.h" +#include "llex.h" +#include "lmem.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lparser.h" +#include "ltable.h" + + +#define hasjumps(e) ((e)->t != (e)->f) + + +static int isnumeral(expdesc *e) { + return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); +} + + +void luaK_nil (FuncState *fs, int from, int n) { + Instruction *previous; + if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ + if (fs->pc == 0) { /* function start? */ + if (from >= fs->nactvar) + return; /* positions are already clean */ + } + else { + previous = &fs->f->code[fs->pc-1]; + if (GET_OPCODE(*previous) == OP_LOADNIL) { + int pfrom = GETARG_A(*previous); + int pto = GETARG_B(*previous); + if (pfrom <= from && from <= pto+1) { /* can connect both? */ + if (from+n-1 > pto) + SETARG_B(*previous, from+n-1); + return; + } + } + } + } + luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */ +} + + +int luaK_jump (FuncState *fs) { + int jpc = fs->jpc; /* save list of jumps to here */ + int j; + fs->jpc = NO_JUMP; + j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); + luaK_concat(fs, &j, jpc); /* keep them on hold */ + return j; +} + + +void luaK_ret (FuncState *fs, int first, int nret) { + luaK_codeABC(fs, OP_RETURN, first, nret+1, 0); +} + + +static int condjump (FuncState *fs, OpCode op, int A, int B, int C) { + luaK_codeABC(fs, op, A, B, C); + return luaK_jump(fs); +} + + +static void fixjump (FuncState *fs, int pc, int dest) { + Instruction *jmp = &fs->f->code[pc]; + int offset = dest-(pc+1); + lua_assert(dest != NO_JUMP); + if (abs(offset) > MAXARG_sBx) + luaX_syntaxerror(fs->ls, "control structure too long"); + SETARG_sBx(*jmp, offset); +} + + +/* +** returns current `pc' and marks it as a jump target (to avoid wrong +** optimizations with consecutive instructions not in the same basic block). +*/ +int luaK_getlabel (FuncState *fs) { + fs->lasttarget = fs->pc; + return fs->pc; +} + + +static int getjump (FuncState *fs, int pc) { + int offset = GETARG_sBx(fs->f->code[pc]); + if (offset == NO_JUMP) /* point to itself represents end of list */ + return NO_JUMP; /* end of list */ + else + return (pc+1)+offset; /* turn offset into absolute position */ +} + + +static Instruction *getjumpcontrol (FuncState *fs, int pc) { + Instruction *pi = &fs->f->code[pc]; + if (pc >= 1 && testTMode(GET_OPCODE(*(pi-1)))) + return pi-1; + else + return pi; +} + + +/* +** check whether list has any jump that do not produce a value +** (or produce an inverted value) +*/ +static int need_value (FuncState *fs, int list) { + for (; list != NO_JUMP; list = getjump(fs, list)) { + Instruction i = *getjumpcontrol(fs, list); + if (GET_OPCODE(i) != OP_TESTSET) return 1; + } + return 0; /* not found */ +} + + +static int patchtestreg (FuncState *fs, int node, int reg) { + Instruction *i = getjumpcontrol(fs, node); + if (GET_OPCODE(*i) != OP_TESTSET) + return 0; /* cannot patch other instructions */ + if (reg != NO_REG && reg != GETARG_B(*i)) + SETARG_A(*i, reg); + else /* no register to put value or register already has the value */ + *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); + + return 1; +} + + +static void removevalues (FuncState *fs, int list) { + for (; list != NO_JUMP; list = getjump(fs, list)) + patchtestreg(fs, list, NO_REG); +} + + +static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, + int dtarget) { + while (list != NO_JUMP) { + int next = getjump(fs, list); + if (patchtestreg(fs, list, reg)) + fixjump(fs, list, vtarget); + else + fixjump(fs, list, dtarget); /* jump to default target */ + list = next; + } +} + + +static void dischargejpc (FuncState *fs) { + patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); + fs->jpc = NO_JUMP; +} + + +void luaK_patchlist (FuncState *fs, int list, int target) { + if (target == fs->pc) + luaK_patchtohere(fs, list); + else { + lua_assert(target < fs->pc); + patchlistaux(fs, list, target, NO_REG, target); + } +} + + +void luaK_patchtohere (FuncState *fs, int list) { + luaK_getlabel(fs); + luaK_concat(fs, &fs->jpc, list); +} + + +void luaK_concat (FuncState *fs, int *l1, int l2) { + if (l2 == NO_JUMP) return; + else if (*l1 == NO_JUMP) + *l1 = l2; + else { + int list = *l1; + int next; + while ((next = getjump(fs, list)) != NO_JUMP) /* find last element */ + list = next; + fixjump(fs, list, l2); + } +} + + +void luaK_checkstack (FuncState *fs, int n) { + int newstack = fs->freereg + n; + if (newstack > fs->f->maxstacksize) { + if (newstack >= MAXSTACK) + luaX_syntaxerror(fs->ls, "function or expression too complex"); + fs->f->maxstacksize = cast_byte(newstack); + } +} + + +void luaK_reserveregs (FuncState *fs, int n) { + luaK_checkstack(fs, n); + fs->freereg += n; +} + + +static void freereg (FuncState *fs, int reg) { + if (!ISK(reg) && reg >= fs->nactvar) { + fs->freereg--; + lua_assert(reg == fs->freereg); + } +} + + +static void freeexp (FuncState *fs, expdesc *e) { + if (e->k == VNONRELOC) + freereg(fs, e->u.s.info); +} + + +static int addk (FuncState *fs, TValue *k, TValue *v) { + lua_State *L = fs->L; + TValue *idx = luaH_set(L, fs->h, k); + Proto *f = fs->f; + int oldsize = f->sizek; + if (ttisnumber(idx)) { + lua_assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v)); + return cast_int(nvalue(idx)); + } + else { /* constant not found; create a new entry */ + setnvalue(idx, cast_num(fs->nk)); + luaM_growvector(L, f->k, fs->nk, f->sizek, TValue, + MAXARG_Bx, "constant table overflow"); + while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); + setobj(L, &f->k[fs->nk], v); + luaC_barrier(L, f, v); + return fs->nk++; + } +} + + +int luaK_stringK (FuncState *fs, TString *s) { + TValue o; + setsvalue(fs->L, &o, s); + return addk(fs, &o, &o); +} + + +int luaK_numberK (FuncState *fs, lua_Number r) { + TValue o; + setnvalue(&o, r); + return addk(fs, &o, &o); +} + + +static int boolK (FuncState *fs, int b) { + TValue o; + setbvalue(&o, b); + return addk(fs, &o, &o); +} + + +static int nilK (FuncState *fs) { + TValue k, v; + setnilvalue(&v); + /* cannot use nil as key; instead use table itself to represent nil */ + sethvalue(fs->L, &k, fs->h); + return addk(fs, &k, &v); +} + + +void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) { + if (e->k == VCALL) { /* expression is an open function call? */ + SETARG_C(getcode(fs, e), nresults+1); + } + else if (e->k == VVARARG) { + SETARG_B(getcode(fs, e), nresults+1); + SETARG_A(getcode(fs, e), fs->freereg); + luaK_reserveregs(fs, 1); + } +} + + +void luaK_setoneret (FuncState *fs, expdesc *e) { + if (e->k == VCALL) { /* expression is an open function call? */ + e->k = VNONRELOC; + e->u.s.info = GETARG_A(getcode(fs, e)); + } + else if (e->k == VVARARG) { + SETARG_B(getcode(fs, e), 2); + e->k = VRELOCABLE; /* can relocate its simple result */ + } +} + + +void luaK_dischargevars (FuncState *fs, expdesc *e) { + switch (e->k) { + case VLOCAL: { + e->k = VNONRELOC; + break; + } + case VUPVAL: { + e->u.s.info = luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.s.info, 0); + e->k = VRELOCABLE; + break; + } + case VGLOBAL: { + e->u.s.info = luaK_codeABx(fs, OP_GETGLOBAL, 0, e->u.s.info); + e->k = VRELOCABLE; + break; + } + case VINDEXED: { + freereg(fs, e->u.s.aux); + freereg(fs, e->u.s.info); + e->u.s.info = luaK_codeABC(fs, OP_GETTABLE, 0, e->u.s.info, e->u.s.aux); + e->k = VRELOCABLE; + break; + } + case VVARARG: + case VCALL: { + luaK_setoneret(fs, e); + break; + } + default: break; /* there is one value available (somewhere) */ + } +} + + +static int code_label (FuncState *fs, int A, int b, int jump) { + luaK_getlabel(fs); /* those instructions may be jump targets */ + return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump); +} + + +static void discharge2reg (FuncState *fs, expdesc *e, int reg) { + luaK_dischargevars(fs, e); + switch (e->k) { + case VNIL: { + luaK_nil(fs, reg, 1); + break; + } + case VFALSE: case VTRUE: { + luaK_codeABC(fs, OP_LOADBOOL, reg, e->k == VTRUE, 0); + break; + } + case VK: { + luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info); + break; + } + case VKNUM: { + luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval)); + break; + } + case VRELOCABLE: { + Instruction *pc = &getcode(fs, e); + SETARG_A(*pc, reg); + break; + } + case VNONRELOC: { + if (reg != e->u.s.info) + luaK_codeABC(fs, OP_MOVE, reg, e->u.s.info, 0); + break; + } + default: { + lua_assert(e->k == VVOID || e->k == VJMP); + return; /* nothing to do... */ + } + } + e->u.s.info = reg; + e->k = VNONRELOC; +} + + +static void discharge2anyreg (FuncState *fs, expdesc *e) { + if (e->k != VNONRELOC) { + luaK_reserveregs(fs, 1); + discharge2reg(fs, e, fs->freereg-1); + } +} + + +static void exp2reg (FuncState *fs, expdesc *e, int reg) { + discharge2reg(fs, e, reg); + if (e->k == VJMP) + luaK_concat(fs, &e->t, e->u.s.info); /* put this jump in `t' list */ + if (hasjumps(e)) { + int final; /* position after whole expression */ + int p_f = NO_JUMP; /* position of an eventual LOAD false */ + int p_t = NO_JUMP; /* position of an eventual LOAD true */ + if (need_value(fs, e->t) || need_value(fs, e->f)) { + int fj = (e->k == VJMP) ? NO_JUMP : luaK_jump(fs); + p_f = code_label(fs, reg, 0, 1); + p_t = code_label(fs, reg, 1, 0); + luaK_patchtohere(fs, fj); + } + final = luaK_getlabel(fs); + patchlistaux(fs, e->f, final, reg, p_f); + patchlistaux(fs, e->t, final, reg, p_t); + } + e->f = e->t = NO_JUMP; + e->u.s.info = reg; + e->k = VNONRELOC; +} + + +void luaK_exp2nextreg (FuncState *fs, expdesc *e) { + luaK_dischargevars(fs, e); + freeexp(fs, e); + luaK_reserveregs(fs, 1); + exp2reg(fs, e, fs->freereg - 1); +} + + +int luaK_exp2anyreg (FuncState *fs, expdesc *e) { + luaK_dischargevars(fs, e); + if (e->k == VNONRELOC) { + if (!hasjumps(e)) return e->u.s.info; /* exp is already in a register */ + if (e->u.s.info >= fs->nactvar) { /* reg. is not a local? */ + exp2reg(fs, e, e->u.s.info); /* put value on it */ + return e->u.s.info; + } + } + luaK_exp2nextreg(fs, e); /* default */ + return e->u.s.info; +} + + +void luaK_exp2val (FuncState *fs, expdesc *e) { + if (hasjumps(e)) + luaK_exp2anyreg(fs, e); + else + luaK_dischargevars(fs, e); +} + + +int luaK_exp2RK (FuncState *fs, expdesc *e) { + luaK_exp2val(fs, e); + switch (e->k) { + case VKNUM: + case VTRUE: + case VFALSE: + case VNIL: { + if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */ + e->u.s.info = (e->k == VNIL) ? nilK(fs) : + (e->k == VKNUM) ? luaK_numberK(fs, e->u.nval) : + boolK(fs, (e->k == VTRUE)); + e->k = VK; + return RKASK(e->u.s.info); + } + else break; + } + case VK: { + if (e->u.s.info <= MAXINDEXRK) /* constant fit in argC? */ + return RKASK(e->u.s.info); + else break; + } + default: break; + } + /* not a constant in the right range: put it in a register */ + return luaK_exp2anyreg(fs, e); +} + + +void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { + switch (var->k) { + case VLOCAL: { + freeexp(fs, ex); + exp2reg(fs, ex, var->u.s.info); + return; + } + case VUPVAL: { + int e = luaK_exp2anyreg(fs, ex); + luaK_codeABC(fs, OP_SETUPVAL, e, var->u.s.info, 0); + break; + } + case VGLOBAL: { + int e = luaK_exp2anyreg(fs, ex); + luaK_codeABx(fs, OP_SETGLOBAL, e, var->u.s.info); + break; + } + case VINDEXED: { + int e = luaK_exp2RK(fs, ex); + luaK_codeABC(fs, OP_SETTABLE, var->u.s.info, var->u.s.aux, e); + break; + } + default: { + lua_assert(0); /* invalid var kind to store */ + break; + } + } + freeexp(fs, ex); +} + + +void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { + int func; + luaK_exp2anyreg(fs, e); + freeexp(fs, e); + func = fs->freereg; + luaK_reserveregs(fs, 2); + luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key)); + freeexp(fs, key); + e->u.s.info = func; + e->k = VNONRELOC; +} + + +static void invertjump (FuncState *fs, expdesc *e) { + Instruction *pc = getjumpcontrol(fs, e->u.s.info); + lua_assert(testTMode(GET_OPCODE(*pc)) && GET_OPCODE(*pc) != OP_TESTSET && + GET_OPCODE(*pc) != OP_TEST); + SETARG_A(*pc, !(GETARG_A(*pc))); +} + + +static int jumponcond (FuncState *fs, expdesc *e, int cond) { + if (e->k == VRELOCABLE) { + Instruction ie = getcode(fs, e); + if (GET_OPCODE(ie) == OP_NOT) { + fs->pc--; /* remove previous OP_NOT */ + return condjump(fs, OP_TEST, GETARG_B(ie), 0, !cond); + } + /* else go through */ + } + discharge2anyreg(fs, e); + freeexp(fs, e); + return condjump(fs, OP_TESTSET, NO_REG, e->u.s.info, cond); +} + + +void luaK_goiftrue (FuncState *fs, expdesc *e) { + int pc; /* pc of last jump */ + luaK_dischargevars(fs, e); + switch (e->k) { + case VK: case VKNUM: case VTRUE: { + pc = NO_JUMP; /* always true; do nothing */ + break; + } + case VJMP: { + invertjump(fs, e); + pc = e->u.s.info; + break; + } + default: { + pc = jumponcond(fs, e, 0); + break; + } + } + luaK_concat(fs, &e->f, pc); /* insert last jump in `f' list */ + luaK_patchtohere(fs, e->t); + e->t = NO_JUMP; +} + + +static void luaK_goiffalse (FuncState *fs, expdesc *e) { + int pc; /* pc of last jump */ + luaK_dischargevars(fs, e); + switch (e->k) { + case VNIL: case VFALSE: { + pc = NO_JUMP; /* always false; do nothing */ + break; + } + case VJMP: { + pc = e->u.s.info; + break; + } + default: { + pc = jumponcond(fs, e, 1); + break; + } + } + luaK_concat(fs, &e->t, pc); /* insert last jump in `t' list */ + luaK_patchtohere(fs, e->f); + e->f = NO_JUMP; +} + + +static void codenot (FuncState *fs, expdesc *e) { + luaK_dischargevars(fs, e); + switch (e->k) { + case VNIL: case VFALSE: { + e->k = VTRUE; + break; + } + case VK: case VKNUM: case VTRUE: { + e->k = VFALSE; + break; + } + case VJMP: { + invertjump(fs, e); + break; + } + case VRELOCABLE: + case VNONRELOC: { + discharge2anyreg(fs, e); + freeexp(fs, e); + e->u.s.info = luaK_codeABC(fs, OP_NOT, 0, e->u.s.info, 0); + e->k = VRELOCABLE; + break; + } + default: { + lua_assert(0); /* cannot happen */ + break; + } + } + /* interchange true and false lists */ + { int temp = e->f; e->f = e->t; e->t = temp; } + removevalues(fs, e->f); + removevalues(fs, e->t); +} + + +void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { + t->u.s.aux = luaK_exp2RK(fs, k); + t->k = VINDEXED; +} + + +static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { + lua_Number v1, v2, r; + if (!isnumeral(e1) || !isnumeral(e2)) return 0; + v1 = e1->u.nval; + v2 = e2->u.nval; + switch (op) { + case OP_ADD: r = luai_numadd(v1, v2); break; + case OP_SUB: r = luai_numsub(v1, v2); break; + case OP_MUL: r = luai_nummul(v1, v2); break; + case OP_DIV: + if (v2 == 0) return 0; /* do not attempt to divide by 0 */ + r = luai_numdiv(v1, v2); break; + case OP_MOD: + if (v2 == 0) return 0; /* do not attempt to divide by 0 */ + r = luai_nummod(v1, v2); break; + case OP_POW: r = luai_numpow(v1, v2); break; + case OP_UNM: r = luai_numunm(v1); break; + case OP_LEN: return 0; /* no constant folding for 'len' */ + default: lua_assert(0); r = 0; break; + } + if (luai_numisnan(r)) return 0; /* do not attempt to produce NaN */ + e1->u.nval = r; + return 1; +} + + +static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { + if (constfolding(op, e1, e2)) + return; + else { + int o2 = (op != OP_UNM && op != OP_LEN) ? luaK_exp2RK(fs, e2) : 0; + int o1 = luaK_exp2RK(fs, e1); + if (o1 > o2) { + freeexp(fs, e1); + freeexp(fs, e2); + } + else { + freeexp(fs, e2); + freeexp(fs, e1); + } + e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); + e1->k = VRELOCABLE; + } +} + + +static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, + expdesc *e2) { + int o1 = luaK_exp2RK(fs, e1); + int o2 = luaK_exp2RK(fs, e2); + freeexp(fs, e2); + freeexp(fs, e1); + if (cond == 0 && op != OP_EQ) { + int temp; /* exchange args to replace by `<' or `<=' */ + temp = o1; o1 = o2; o2 = temp; /* o1 <==> o2 */ + cond = 1; + } + e1->u.s.info = condjump(fs, op, cond, o1, o2); + e1->k = VJMP; +} + + +void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { + expdesc e2; + e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; + switch (op) { + case OPR_MINUS: { + if (!isnumeral(e)) + luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */ + codearith(fs, OP_UNM, e, &e2); + break; + } + case OPR_NOT: codenot(fs, e); break; + case OPR_LEN: { + luaK_exp2anyreg(fs, e); /* cannot operate on constants */ + codearith(fs, OP_LEN, e, &e2); + break; + } + default: lua_assert(0); + } +} + + +void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) { + switch (op) { + case OPR_AND: { + luaK_goiftrue(fs, v); + break; + } + case OPR_OR: { + luaK_goiffalse(fs, v); + break; + } + case OPR_CONCAT: { + luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */ + break; + } + case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: + case OPR_MOD: case OPR_POW: { + if (!isnumeral(v)) luaK_exp2RK(fs, v); + break; + } + default: { + luaK_exp2RK(fs, v); + break; + } + } +} + + +void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { + switch (op) { + case OPR_AND: { + lua_assert(e1->t == NO_JUMP); /* list must be closed */ + luaK_dischargevars(fs, e2); + luaK_concat(fs, &e2->f, e1->f); + *e1 = *e2; + break; + } + case OPR_OR: { + lua_assert(e1->f == NO_JUMP); /* list must be closed */ + luaK_dischargevars(fs, e2); + luaK_concat(fs, &e2->t, e1->t); + *e1 = *e2; + break; + } + case OPR_CONCAT: { + luaK_exp2val(fs, e2); + if (e2->k == VRELOCABLE && GET_OPCODE(getcode(fs, e2)) == OP_CONCAT) { + lua_assert(e1->u.s.info == GETARG_B(getcode(fs, e2))-1); + freeexp(fs, e1); + SETARG_B(getcode(fs, e2), e1->u.s.info); + e1->k = VRELOCABLE; e1->u.s.info = e2->u.s.info; + } + else { + luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ + codearith(fs, OP_CONCAT, e1, e2); + } + break; + } + case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break; + case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break; + case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break; + case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break; + case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break; + case OPR_POW: codearith(fs, OP_POW, e1, e2); break; + case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break; + case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break; + case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break; + case OPR_LE: codecomp(fs, OP_LE, 1, e1, e2); break; + case OPR_GT: codecomp(fs, OP_LT, 0, e1, e2); break; + case OPR_GE: codecomp(fs, OP_LE, 0, e1, e2); break; + default: lua_assert(0); + } +} + + +void luaK_fixline (FuncState *fs, int line) { + fs->f->lineinfo[fs->pc - 1] = line; +} + + +static int luaK_code (FuncState *fs, Instruction i, int line) { + Proto *f = fs->f; + dischargejpc(fs); /* `pc' will change */ + /* put new instruction in code array */ + luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction, + MAX_INT, "code size overflow"); + f->code[fs->pc] = i; + /* save corresponding line information */ + luaM_growvector(fs->L, f->lineinfo, fs->pc, f->sizelineinfo, int, + MAX_INT, "code size overflow"); + f->lineinfo[fs->pc] = line; + return fs->pc++; +} + + +int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) { + lua_assert(getOpMode(o) == iABC); + lua_assert(getBMode(o) != OpArgN || b == 0); + lua_assert(getCMode(o) != OpArgN || c == 0); + return luaK_code(fs, CREATE_ABC(o, a, b, c), fs->ls->lastline); +} + + +int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { + lua_assert(getOpMode(o) == iABx || getOpMode(o) == iAsBx); + lua_assert(getCMode(o) == OpArgN); + return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); +} + + +void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { + int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; + int b = (tostore == LUA_MULTRET) ? 0 : tostore; + lua_assert(tostore != 0); + if (c <= MAXARG_C) + luaK_codeABC(fs, OP_SETLIST, base, b, c); + else { + luaK_codeABC(fs, OP_SETLIST, base, b, 0); + luaK_code(fs, cast(Instruction, c), fs->ls->lastline); + } + fs->freereg = base + 1; /* free registers with list values */ +} + diff --git a/external/lua/lua/lcode.h b/external/lua/lua/lcode.h new file mode 100644 index 0000000000..b941c60721 --- /dev/null +++ b/external/lua/lua/lcode.h @@ -0,0 +1,76 @@ +/* +** $Id: lcode.h,v 1.48.1.1 2007/12/27 13:02:25 roberto Exp $ +** Code generator for Lua +** See Copyright Notice in lua.h +*/ + +#ifndef lcode_h +#define lcode_h + +#include "llex.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lparser.h" + + +/* +** Marks the end of a patch list. It is an invalid value both as an absolute +** address, and as a list link (would link an element to itself). +*/ +#define NO_JUMP (-1) + + +/* +** grep "ORDER OPR" if you change these enums +*/ +typedef enum BinOpr { + OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, + OPR_CONCAT, + OPR_NE, OPR_EQ, + OPR_LT, OPR_LE, OPR_GT, OPR_GE, + OPR_AND, OPR_OR, + OPR_NOBINOPR +} BinOpr; + + +typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; + + +#define getcode(fs,e) ((fs)->f->code[(e)->u.s.info]) + +#define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) + +#define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) + +LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); +LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); +LUAI_FUNC void luaK_fixline (FuncState *fs, int line); +LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); +LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); +LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); +LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); +LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); +LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); +LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); +LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); +LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); +LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); +LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); +LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); +LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); +LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); +LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); +LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); +LUAI_FUNC int luaK_jump (FuncState *fs); +LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); +LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); +LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); +LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); +LUAI_FUNC int luaK_getlabel (FuncState *fs); +LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); +LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); +LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); +LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); + + +#endif diff --git a/external/lua/lua/ldblib.c b/external/lua/lua/ldblib.c new file mode 100644 index 0000000000..2027eda598 --- /dev/null +++ b/external/lua/lua/ldblib.c @@ -0,0 +1,398 @@ +/* +** $Id: ldblib.c,v 1.104.1.4 2009/08/04 18:50:18 roberto Exp $ +** Interface from Lua to its debug API +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + +#define ldblib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + + +static int db_getregistry (lua_State *L) { + lua_pushvalue(L, LUA_REGISTRYINDEX); + return 1; +} + + +static int db_getmetatable (lua_State *L) { + luaL_checkany(L, 1); + if (!lua_getmetatable(L, 1)) { + lua_pushnil(L); /* no metatable */ + } + return 1; +} + + +static int db_setmetatable (lua_State *L) { + int t = lua_type(L, 2); + luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, + "nil or table expected"); + lua_settop(L, 2); + lua_pushboolean(L, lua_setmetatable(L, 1)); + return 1; +} + + +static int db_getfenv (lua_State *L) { + luaL_checkany(L, 1); + lua_getfenv(L, 1); + return 1; +} + + +static int db_setfenv (lua_State *L) { + luaL_checktype(L, 2, LUA_TTABLE); + lua_settop(L, 2); + if (lua_setfenv(L, 1) == 0) + luaL_error(L, LUA_QL("setfenv") + " cannot change environment of given object"); + return 1; +} + + +static void settabss (lua_State *L, const char *i, const char *v) { + lua_pushstring(L, v); + lua_setfield(L, -2, i); +} + + +static void settabsi (lua_State *L, const char *i, int v) { + lua_pushinteger(L, v); + lua_setfield(L, -2, i); +} + + +static lua_State *getthread (lua_State *L, int *arg) { + if (lua_isthread(L, 1)) { + *arg = 1; + return lua_tothread(L, 1); + } + else { + *arg = 0; + return L; + } +} + + +static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) { + if (L == L1) { + lua_pushvalue(L, -2); + lua_remove(L, -3); + } + else + lua_xmove(L1, L, 1); + lua_setfield(L, -2, fname); +} + + +static int db_getinfo (lua_State *L) { + lua_Debug ar; + int arg; + lua_State *L1 = getthread(L, &arg); + const char *options = luaL_optstring(L, arg+2, "flnSu"); + if (lua_isnumber(L, arg+1)) { + if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), &ar)) { + lua_pushnil(L); /* level out of range */ + return 1; + } + } + else if (lua_isfunction(L, arg+1)) { + lua_pushfstring(L, ">%s", options); + options = lua_tostring(L, -1); + lua_pushvalue(L, arg+1); + lua_xmove(L, L1, 1); + } + else + return luaL_argerror(L, arg+1, "function or level expected"); + if (!lua_getinfo(L1, options, &ar)) + return luaL_argerror(L, arg+2, "invalid option"); + lua_createtable(L, 0, 2); + if (strchr(options, 'S')) { + settabss(L, "source", ar.source); + settabss(L, "short_src", ar.short_src); + settabsi(L, "linedefined", ar.linedefined); + settabsi(L, "lastlinedefined", ar.lastlinedefined); + settabss(L, "what", ar.what); + } + if (strchr(options, 'l')) + settabsi(L, "currentline", ar.currentline); + if (strchr(options, 'u')) + settabsi(L, "nups", ar.nups); + if (strchr(options, 'n')) { + settabss(L, "name", ar.name); + settabss(L, "namewhat", ar.namewhat); + } + if (strchr(options, 'L')) + treatstackoption(L, L1, "activelines"); + if (strchr(options, 'f')) + treatstackoption(L, L1, "func"); + return 1; /* return table */ +} + + +static int db_getlocal (lua_State *L) { + int arg; + lua_State *L1 = getthread(L, &arg); + lua_Debug ar; + const char *name; + if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ + return luaL_argerror(L, arg+1, "level out of range"); + name = lua_getlocal(L1, &ar, luaL_checkint(L, arg+2)); + if (name) { + lua_xmove(L1, L, 1); + lua_pushstring(L, name); + lua_pushvalue(L, -2); + return 2; + } + else { + lua_pushnil(L); + return 1; + } +} + + +static int db_setlocal (lua_State *L) { + int arg; + lua_State *L1 = getthread(L, &arg); + lua_Debug ar; + if (!lua_getstack(L1, luaL_checkint(L, arg+1), &ar)) /* out of range? */ + return luaL_argerror(L, arg+1, "level out of range"); + luaL_checkany(L, arg+3); + lua_settop(L, arg+3); + lua_xmove(L, L1, 1); + lua_pushstring(L, lua_setlocal(L1, &ar, luaL_checkint(L, arg+2))); + return 1; +} + + +static int auxupvalue (lua_State *L, int get) { + const char *name; + int n = luaL_checkint(L, 2); + luaL_checktype(L, 1, LUA_TFUNCTION); + if (lua_iscfunction(L, 1)) return 0; /* cannot touch C upvalues from Lua */ + name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); + if (name == NULL) return 0; + lua_pushstring(L, name); + lua_insert(L, -(get+1)); + return get + 1; +} + + +static int db_getupvalue (lua_State *L) { + return auxupvalue(L, 1); +} + + +static int db_setupvalue (lua_State *L) { + luaL_checkany(L, 3); + return auxupvalue(L, 0); +} + + + +static const char KEY_HOOK = 'h'; + + +static void hookf (lua_State *L, lua_Debug *ar) { + static const char *const hooknames[] = + {"call", "return", "line", "count", "tail return"}; + lua_pushlightuserdata(L, (void *)&KEY_HOOK); + lua_rawget(L, LUA_REGISTRYINDEX); + lua_pushlightuserdata(L, L); + lua_rawget(L, -2); + if (lua_isfunction(L, -1)) { + lua_pushstring(L, hooknames[(int)ar->event]); + if (ar->currentline >= 0) + lua_pushinteger(L, ar->currentline); + else lua_pushnil(L); + lua_assert(lua_getinfo(L, "lS", ar)); + lua_call(L, 2, 0); + } +} + + +static int makemask (const char *smask, int count) { + int mask = 0; + if (strchr(smask, 'c')) mask |= LUA_MASKCALL; + if (strchr(smask, 'r')) mask |= LUA_MASKRET; + if (strchr(smask, 'l')) mask |= LUA_MASKLINE; + if (count > 0) mask |= LUA_MASKCOUNT; + return mask; +} + + +static char *unmakemask (int mask, char *smask) { + int i = 0; + if (mask & LUA_MASKCALL) smask[i++] = 'c'; + if (mask & LUA_MASKRET) smask[i++] = 'r'; + if (mask & LUA_MASKLINE) smask[i++] = 'l'; + smask[i] = '\0'; + return smask; +} + + +static void gethooktable (lua_State *L) { + lua_pushlightuserdata(L, (void *)&KEY_HOOK); + lua_rawget(L, LUA_REGISTRYINDEX); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + lua_createtable(L, 0, 1); + lua_pushlightuserdata(L, (void *)&KEY_HOOK); + lua_pushvalue(L, -2); + lua_rawset(L, LUA_REGISTRYINDEX); + } +} + + +static int db_sethook (lua_State *L) { + int arg, mask, count; + lua_Hook func; + lua_State *L1 = getthread(L, &arg); + if (lua_isnoneornil(L, arg+1)) { + lua_settop(L, arg+1); + func = NULL; mask = 0; count = 0; /* turn off hooks */ + } + else { + const char *smask = luaL_checkstring(L, arg+2); + luaL_checktype(L, arg+1, LUA_TFUNCTION); + count = luaL_optint(L, arg+3, 0); + func = hookf; mask = makemask(smask, count); + } + gethooktable(L); + lua_pushlightuserdata(L, L1); + lua_pushvalue(L, arg+1); + lua_rawset(L, -3); /* set new hook */ + lua_pop(L, 1); /* remove hook table */ + lua_sethook(L1, func, mask, count); /* set hooks */ + return 0; +} + + +static int db_gethook (lua_State *L) { + int arg; + lua_State *L1 = getthread(L, &arg); + char buff[5]; + int mask = lua_gethookmask(L1); + lua_Hook hook = lua_gethook(L1); + if (hook != NULL && hook != hookf) /* external hook? */ + lua_pushliteral(L, "external hook"); + else { + gethooktable(L); + lua_pushlightuserdata(L, L1); + lua_rawget(L, -2); /* get hook */ + lua_remove(L, -2); /* remove hook table */ + } + lua_pushstring(L, unmakemask(mask, buff)); + lua_pushinteger(L, lua_gethookcount(L1)); + return 3; +} + + +static int db_debug (lua_State *L) { + for (;;) { + char buffer[250]; + fputs("lua_debug> ", stderr); + if (fgets(buffer, sizeof(buffer), stdin) == 0 || + strcmp(buffer, "cont\n") == 0) + return 0; + if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || + lua_pcall(L, 0, 0, 0)) { + fputs(lua_tostring(L, -1), stderr); + fputs("\n", stderr); + } + lua_settop(L, 0); /* remove eventual returns */ + } +} + + +#define LEVELS1 12 /* size of the first part of the stack */ +#define LEVELS2 10 /* size of the second part of the stack */ + +static int db_errorfb (lua_State *L) { + int level; + int firstpart = 1; /* still before eventual `...' */ + int arg; + lua_State *L1 = getthread(L, &arg); + lua_Debug ar; + if (lua_isnumber(L, arg+2)) { + level = (int)lua_tointeger(L, arg+2); + lua_pop(L, 1); + } + else + level = (L == L1) ? 1 : 0; /* level 0 may be this own function */ + if (lua_gettop(L) == arg) + lua_pushliteral(L, ""); + else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */ + else lua_pushliteral(L, "\n"); + lua_pushliteral(L, "stack traceback:"); + while (lua_getstack(L1, level++, &ar)) { + if (level > LEVELS1 && firstpart) { + /* no more than `LEVELS2' more levels? */ + if (!lua_getstack(L1, level+LEVELS2, &ar)) + level--; /* keep going */ + else { + lua_pushliteral(L, "\n\t..."); /* too many levels */ + while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */ + level++; + } + firstpart = 0; + continue; + } + lua_pushliteral(L, "\n\t"); + lua_getinfo(L1, "Snl", &ar); + lua_pushfstring(L, "%s:", ar.short_src); + if (ar.currentline > 0) + lua_pushfstring(L, "%d:", ar.currentline); + if (*ar.namewhat != '\0') /* is there a name? */ + lua_pushfstring(L, " in function " LUA_QS, ar.name); + else { + if (*ar.what == 'm') /* main? */ + lua_pushfstring(L, " in main chunk"); + else if (*ar.what == 'C' || *ar.what == 't') + lua_pushliteral(L, " ?"); /* C function or tail call */ + else + lua_pushfstring(L, " in function <%s:%d>", + ar.short_src, ar.linedefined); + } + lua_concat(L, lua_gettop(L) - arg); + } + lua_concat(L, lua_gettop(L) - arg); + return 1; +} + + +static const luaL_Reg dblib[] = { + {"debug", db_debug}, + {"getfenv", db_getfenv}, + {"gethook", db_gethook}, + {"getinfo", db_getinfo}, + {"getlocal", db_getlocal}, + {"getregistry", db_getregistry}, + {"getmetatable", db_getmetatable}, + {"getupvalue", db_getupvalue}, + {"setfenv", db_setfenv}, + {"sethook", db_sethook}, + {"setlocal", db_setlocal}, + {"setmetatable", db_setmetatable}, + {"setupvalue", db_setupvalue}, + {"traceback", db_errorfb}, + {NULL, NULL} +}; + + +LUALIB_API int luaopen_debug (lua_State *L) { + luaL_register(L, LUA_DBLIBNAME, dblib); + return 1; +} + diff --git a/external/lua/lua/ldebug.c b/external/lua/lua/ldebug.c new file mode 100644 index 0000000000..50ad3d3803 --- /dev/null +++ b/external/lua/lua/ldebug.c @@ -0,0 +1,638 @@ +/* +** $Id: ldebug.c,v 2.29.1.6 2008/05/08 16:56:26 roberto Exp $ +** Debug Interface +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + + +#define ldebug_c +#define LUA_CORE + +#include "lua.h" + +#include "lapi.h" +#include "lcode.h" +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" +#include "lvm.h" + + + +static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); + + +static int currentpc (lua_State *L, CallInfo *ci) { + if (!isLua(ci)) return -1; /* function is not a Lua function? */ + if (ci == L->ci) + ci->savedpc = L->savedpc; + return pcRel(ci->savedpc, ci_func(ci)->l.p); +} + + +static int currentline (lua_State *L, CallInfo *ci) { + int pc = currentpc(L, ci); + if (pc < 0) + return -1; /* only active lua functions have current-line information */ + else + return getline(ci_func(ci)->l.p, pc); +} + + +/* +** this function can be called asynchronous (e.g. during a signal) +*/ +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count) { + if (func == NULL || mask == 0) { /* turn off hooks? */ + mask = 0; + func = NULL; + } + L->hook = func; + L->basehookcount = count; + resethookcount(L); + L->hookmask = cast_byte(mask); + return 1; +} + + +LUA_API lua_Hook lua_gethook (lua_State *L) { + return L->hook; +} + + +LUA_API int lua_gethookmask (lua_State *L) { + return L->hookmask; +} + + +LUA_API int lua_gethookcount (lua_State *L) { + return L->basehookcount; +} + + +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { + int status; + CallInfo *ci; + lua_lock(L); + for (ci = L->ci; level > 0 && ci > L->base_ci; ci--) { + level--; + if (f_isLua(ci)) /* Lua function? */ + level -= ci->tailcalls; /* skip lost tail calls */ + } + if (level == 0 && ci > L->base_ci) { /* level found? */ + status = 1; + ar->i_ci = cast_int(ci - L->base_ci); + } + else if (level < 0) { /* level is of a lost tail call? */ + status = 1; + ar->i_ci = 0; + } + else status = 0; /* no such level */ + lua_unlock(L); + return status; +} + + +static Proto *getluaproto (CallInfo *ci) { + return (isLua(ci) ? ci_func(ci)->l.p : NULL); +} + + +static const char *findlocal (lua_State *L, CallInfo *ci, int n) { + const char *name; + Proto *fp = getluaproto(ci); + if (fp && (name = luaF_getlocalname(fp, n, currentpc(L, ci))) != NULL) + return name; /* is a local variable in a Lua function */ + else { + StkId limit = (ci == L->ci) ? L->top : (ci+1)->func; + if (limit - ci->base >= n && n > 0) /* is 'n' inside 'ci' stack? */ + return "(*temporary)"; + else + return NULL; + } +} + + +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { + CallInfo *ci = L->base_ci + ar->i_ci; + const char *name = findlocal(L, ci, n); + lua_lock(L); + if (name) + luaA_pushobject(L, ci->base + (n - 1)); + lua_unlock(L); + return name; +} + + +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { + CallInfo *ci = L->base_ci + ar->i_ci; + const char *name = findlocal(L, ci, n); + lua_lock(L); + if (name) + setobjs2s(L, ci->base + (n - 1), L->top - 1); + L->top--; /* pop value */ + lua_unlock(L); + return name; +} + + +static void funcinfo (lua_Debug *ar, Closure *cl) { + if (cl->c.isC) { + ar->source = "=[C]"; + ar->linedefined = -1; + ar->lastlinedefined = -1; + ar->what = "C"; + } + else { + ar->source = getstr(cl->l.p->source); + ar->linedefined = cl->l.p->linedefined; + ar->lastlinedefined = cl->l.p->lastlinedefined; + ar->what = (ar->linedefined == 0) ? "main" : "Lua"; + } + luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); +} + + +static void info_tailcall (lua_Debug *ar) { + ar->name = ar->namewhat = ""; + ar->what = "tail"; + ar->lastlinedefined = ar->linedefined = ar->currentline = -1; + ar->source = "=(tail call)"; + luaO_chunkid(ar->short_src, ar->source, LUA_IDSIZE); + ar->nups = 0; +} + + +static void collectvalidlines (lua_State *L, Closure *f) { + if (f == NULL || f->c.isC) { + setnilvalue(L->top); + } + else { + Table *t = luaH_new(L, 0, 0); + int *lineinfo = f->l.p->lineinfo; + int i; + for (i=0; il.p->sizelineinfo; i++) + setbvalue(luaH_setnum(L, t, lineinfo[i]), 1); + sethvalue(L, L->top, t); + } + incr_top(L); +} + + +static int auxgetinfo (lua_State *L, const char *what, lua_Debug *ar, + Closure *f, CallInfo *ci) { + int status = 1; + if (f == NULL) { + info_tailcall(ar); + return status; + } + for (; *what; what++) { + switch (*what) { + case 'S': { + funcinfo(ar, f); + break; + } + case 'l': { + ar->currentline = (ci) ? currentline(L, ci) : -1; + break; + } + case 'u': { + ar->nups = f->c.nupvalues; + break; + } + case 'n': { + ar->namewhat = (ci) ? getfuncname(L, ci, &ar->name) : NULL; + if (ar->namewhat == NULL) { + ar->namewhat = ""; /* not found */ + ar->name = NULL; + } + break; + } + case 'L': + case 'f': /* handled by lua_getinfo */ + break; + default: status = 0; /* invalid option */ + } + } + return status; +} + + +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { + int status; + Closure *f = NULL; + CallInfo *ci = NULL; + lua_lock(L); + if (*what == '>') { + StkId func = L->top - 1; + luai_apicheck(L, ttisfunction(func)); + what++; /* skip the '>' */ + f = clvalue(func); + L->top--; /* pop function */ + } + else if (ar->i_ci != 0) { /* no tail call? */ + ci = L->base_ci + ar->i_ci; + lua_assert(ttisfunction(ci->func)); + f = clvalue(ci->func); + } + status = auxgetinfo(L, what, ar, f, ci); + if (strchr(what, 'f')) { + if (f == NULL) setnilvalue(L->top); + else setclvalue(L, L->top, f); + incr_top(L); + } + if (strchr(what, 'L')) + collectvalidlines(L, f); + lua_unlock(L); + return status; +} + + +/* +** {====================================================== +** Symbolic Execution and code checker +** ======================================================= +*/ + +#define check(x) if (!(x)) return 0; + +#define checkjump(pt,pc) check(0 <= pc && pc < pt->sizecode) + +#define checkreg(pt,reg) check((reg) < (pt)->maxstacksize) + + + +static int precheck (const Proto *pt) { + check(pt->maxstacksize <= MAXSTACK); + check(pt->numparams+(pt->is_vararg & VARARG_HASARG) <= pt->maxstacksize); + check(!(pt->is_vararg & VARARG_NEEDSARG) || + (pt->is_vararg & VARARG_HASARG)); + check(pt->sizeupvalues <= pt->nups); + check(pt->sizelineinfo == pt->sizecode || pt->sizelineinfo == 0); + check(pt->sizecode > 0 && GET_OPCODE(pt->code[pt->sizecode-1]) == OP_RETURN); + return 1; +} + + +#define checkopenop(pt,pc) luaG_checkopenop((pt)->code[(pc)+1]) + +int luaG_checkopenop (Instruction i) { + switch (GET_OPCODE(i)) { + case OP_CALL: + case OP_TAILCALL: + case OP_RETURN: + case OP_SETLIST: { + check(GETARG_B(i) == 0); + return 1; + } + default: return 0; /* invalid instruction after an open call */ + } +} + + +static int checkArgMode (const Proto *pt, int r, enum OpArgMask mode) { + switch (mode) { + case OpArgN: check(r == 0); break; + case OpArgU: break; + case OpArgR: checkreg(pt, r); break; + case OpArgK: + check(ISK(r) ? INDEXK(r) < pt->sizek : r < pt->maxstacksize); + break; + } + return 1; +} + + +static Instruction symbexec (const Proto *pt, int lastpc, int reg) { + int pc; + int last; /* stores position of last instruction that changed `reg' */ + last = pt->sizecode-1; /* points to final return (a `neutral' instruction) */ + check(precheck(pt)); + for (pc = 0; pc < lastpc; pc++) { + Instruction i = pt->code[pc]; + OpCode op = GET_OPCODE(i); + int a = GETARG_A(i); + int b = 0; + int c = 0; + check(op < NUM_OPCODES); + checkreg(pt, a); + switch (getOpMode(op)) { + case iABC: { + b = GETARG_B(i); + c = GETARG_C(i); + check(checkArgMode(pt, b, getBMode(op))); + check(checkArgMode(pt, c, getCMode(op))); + break; + } + case iABx: { + b = GETARG_Bx(i); + if (getBMode(op) == OpArgK) check(b < pt->sizek); + break; + } + case iAsBx: { + b = GETARG_sBx(i); + if (getBMode(op) == OpArgR) { + int dest = pc+1+b; + check(0 <= dest && dest < pt->sizecode); + if (dest > 0) { + int j; + /* check that it does not jump to a setlist count; this + is tricky, because the count from a previous setlist may + have the same value of an invalid setlist; so, we must + go all the way back to the first of them (if any) */ + for (j = 0; j < dest; j++) { + Instruction d = pt->code[dest-1-j]; + if (!(GET_OPCODE(d) == OP_SETLIST && GETARG_C(d) == 0)) break; + } + /* if 'j' is even, previous value is not a setlist (even if + it looks like one) */ + check((j&1) == 0); + } + } + break; + } + } + if (testAMode(op)) { + if (a == reg) last = pc; /* change register `a' */ + } + if (testTMode(op)) { + check(pc+2 < pt->sizecode); /* check skip */ + check(GET_OPCODE(pt->code[pc+1]) == OP_JMP); + } + switch (op) { + case OP_LOADBOOL: { + if (c == 1) { /* does it jump? */ + check(pc+2 < pt->sizecode); /* check its jump */ + check(GET_OPCODE(pt->code[pc+1]) != OP_SETLIST || + GETARG_C(pt->code[pc+1]) != 0); + } + break; + } + case OP_LOADNIL: { + if (a <= reg && reg <= b) + last = pc; /* set registers from `a' to `b' */ + break; + } + case OP_GETUPVAL: + case OP_SETUPVAL: { + check(b < pt->nups); + break; + } + case OP_GETGLOBAL: + case OP_SETGLOBAL: { + check(ttisstring(&pt->k[b])); + break; + } + case OP_SELF: { + checkreg(pt, a+1); + if (reg == a+1) last = pc; + break; + } + case OP_CONCAT: { + check(b < c); /* at least two operands */ + break; + } + case OP_TFORLOOP: { + check(c >= 1); /* at least one result (control variable) */ + checkreg(pt, a+2+c); /* space for results */ + if (reg >= a+2) last = pc; /* affect all regs above its base */ + break; + } + case OP_FORLOOP: + case OP_FORPREP: + checkreg(pt, a+3); + /* go through */ + case OP_JMP: { + int dest = pc+1+b; + /* not full check and jump is forward and do not skip `lastpc'? */ + if (reg != NO_REG && pc < dest && dest <= lastpc) + pc += b; /* do the jump */ + break; + } + case OP_CALL: + case OP_TAILCALL: { + if (b != 0) { + checkreg(pt, a+b-1); + } + c--; /* c = num. returns */ + if (c == LUA_MULTRET) { + check(checkopenop(pt, pc)); + } + else if (c != 0) + checkreg(pt, a+c-1); + if (reg >= a) last = pc; /* affect all registers above base */ + break; + } + case OP_RETURN: { + b--; /* b = num. returns */ + if (b > 0) checkreg(pt, a+b-1); + break; + } + case OP_SETLIST: { + if (b > 0) checkreg(pt, a + b); + if (c == 0) { + pc++; + check(pc < pt->sizecode - 1); + } + break; + } + case OP_CLOSURE: { + int nup, j; + check(b < pt->sizep); + nup = pt->p[b]->nups; + check(pc + nup < pt->sizecode); + for (j = 1; j <= nup; j++) { + OpCode op1 = GET_OPCODE(pt->code[pc + j]); + check(op1 == OP_GETUPVAL || op1 == OP_MOVE); + } + if (reg != NO_REG) /* tracing? */ + pc += nup; /* do not 'execute' these pseudo-instructions */ + break; + } + case OP_VARARG: { + check((pt->is_vararg & VARARG_ISVARARG) && + !(pt->is_vararg & VARARG_NEEDSARG)); + b--; + if (b == LUA_MULTRET) check(checkopenop(pt, pc)); + checkreg(pt, a+b-1); + break; + } + default: break; + } + } + return pt->code[last]; +} + +#undef check +#undef checkjump +#undef checkreg + +/* }====================================================== */ + + +int luaG_checkcode (const Proto *pt) { + return (symbexec(pt, pt->sizecode, NO_REG) != 0); +} + + +static const char *kname (Proto *p, int c) { + if (ISK(c) && ttisstring(&p->k[INDEXK(c)])) + return svalue(&p->k[INDEXK(c)]); + else + return "?"; +} + + +static const char *getobjname (lua_State *L, CallInfo *ci, int stackpos, + const char **name) { + if (isLua(ci)) { /* a Lua function? */ + Proto *p = ci_func(ci)->l.p; + int pc = currentpc(L, ci); + Instruction i; + *name = luaF_getlocalname(p, stackpos+1, pc); + if (*name) /* is a local? */ + return "local"; + i = symbexec(p, pc, stackpos); /* try symbolic execution */ + lua_assert(pc != -1); + switch (GET_OPCODE(i)) { + case OP_GETGLOBAL: { + int g = GETARG_Bx(i); /* global index */ + lua_assert(ttisstring(&p->k[g])); + *name = svalue(&p->k[g]); + return "global"; + } + case OP_MOVE: { + int a = GETARG_A(i); + int b = GETARG_B(i); /* move from `b' to `a' */ + if (b < a) + return getobjname(L, ci, b, name); /* get name for `b' */ + break; + } + case OP_GETTABLE: { + int k = GETARG_C(i); /* key index */ + *name = kname(p, k); + return "field"; + } + case OP_GETUPVAL: { + int u = GETARG_B(i); /* upvalue index */ + *name = p->upvalues ? getstr(p->upvalues[u]) : "?"; + return "upvalue"; + } + case OP_SELF: { + int k = GETARG_C(i); /* key index */ + *name = kname(p, k); + return "method"; + } + default: break; + } + } + return NULL; /* no useful name found */ +} + + +static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { + Instruction i; + if ((isLua(ci) && ci->tailcalls > 0) || !isLua(ci - 1)) + return NULL; /* calling function is not Lua (or is unknown) */ + ci--; /* calling function */ + i = ci_func(ci)->l.p->code[currentpc(L, ci)]; + if (GET_OPCODE(i) == OP_CALL || GET_OPCODE(i) == OP_TAILCALL || + GET_OPCODE(i) == OP_TFORLOOP) + return getobjname(L, ci, GETARG_A(i), name); + else + return NULL; /* no useful name can be found */ +} + + +/* only ANSI way to check whether a pointer points to an array */ +static int isinstack (CallInfo *ci, const TValue *o) { + StkId p; + for (p = ci->base; p < ci->top; p++) + if (o == p) return 1; + return 0; +} + + +void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { + const char *name = NULL; + const char *t = luaT_typenames[ttype(o)]; + const char *kind = (isinstack(L->ci, o)) ? + getobjname(L, L->ci, cast_int(o - L->base), &name) : + NULL; + if (kind) + luaG_runerror(L, "attempt to %s %s " LUA_QS " (a %s value)", + op, kind, name, t); + else + luaG_runerror(L, "attempt to %s a %s value", op, t); +} + + +void luaG_concaterror (lua_State *L, StkId p1, StkId p2) { + if (ttisstring(p1) || ttisnumber(p1)) p1 = p2; + lua_assert(!ttisstring(p1) && !ttisnumber(p1)); + luaG_typeerror(L, p1, "concatenate"); +} + + +void luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { + TValue temp; + if (luaV_tonumber(p1, &temp) == NULL) + p2 = p1; /* first operand is wrong */ + luaG_typeerror(L, p2, "perform arithmetic on"); +} + + +int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) { + const char *t1 = luaT_typenames[ttype(p1)]; + const char *t2 = luaT_typenames[ttype(p2)]; + if (t1[2] == t2[2]) + luaG_runerror(L, "attempt to compare two %s values", t1); + else + luaG_runerror(L, "attempt to compare %s with %s", t1, t2); + return 0; +} + + +static void addinfo (lua_State *L, const char *msg) { + CallInfo *ci = L->ci; + if (isLua(ci)) { /* is Lua code? */ + char buff[LUA_IDSIZE]; /* add file:line information */ + int line = currentline(L, ci); + luaO_chunkid(buff, getstr(getluaproto(ci)->source), LUA_IDSIZE); + luaO_pushfstring(L, "%s:%d: %s", buff, line, msg); + } +} + + +void luaG_errormsg (lua_State *L) { + if (L->errfunc != 0) { /* is there an error handling function? */ + StkId errfunc = restorestack(L, L->errfunc); + if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR); + setobjs2s(L, L->top, L->top - 1); /* move argument */ + setobjs2s(L, L->top - 1, errfunc); /* push function */ + incr_top(L); + luaD_call(L, L->top - 2, 1); /* call it */ + } + luaD_throw(L, LUA_ERRRUN); +} + + +void luaG_runerror (lua_State *L, const char *fmt, ...) { + va_list argp; + va_start(argp, fmt); + addinfo(L, luaO_pushvfstring(L, fmt, argp)); + va_end(argp); + luaG_errormsg(L); +} + diff --git a/external/lua/lua/ldebug.h b/external/lua/lua/ldebug.h new file mode 100644 index 0000000000..ba28a97248 --- /dev/null +++ b/external/lua/lua/ldebug.h @@ -0,0 +1,33 @@ +/* +** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions from Debug Interface module +** See Copyright Notice in lua.h +*/ + +#ifndef ldebug_h +#define ldebug_h + + +#include "lstate.h" + + +#define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) + +#define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) + +#define resethookcount(L) (L->hookcount = L->basehookcount) + + +LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, + const char *opname); +LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); +LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, + const TValue *p2); +LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, + const TValue *p2); +LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); +LUAI_FUNC void luaG_errormsg (lua_State *L); +LUAI_FUNC int luaG_checkcode (const Proto *pt); +LUAI_FUNC int luaG_checkopenop (Instruction i); + +#endif diff --git a/external/lua/lua/ldo.c b/external/lua/lua/ldo.c new file mode 100644 index 0000000000..d1bf786cb7 --- /dev/null +++ b/external/lua/lua/ldo.c @@ -0,0 +1,519 @@ +/* +** $Id: ldo.c,v 2.38.1.4 2012/01/18 02:27:10 roberto Exp $ +** Stack and Call structure of Lua +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + +#define ldo_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lparser.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" +#include "lundump.h" +#include "lvm.h" +#include "lzio.h" + + + + +/* +** {====================================================== +** Error-recovery functions +** ======================================================= +*/ + + +/* chain list of long jump buffers */ +struct lua_longjmp { + struct lua_longjmp *previous; + luai_jmpbuf b; + volatile int status; /* error code */ +}; + + +void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) { + switch (errcode) { + case LUA_ERRMEM: { + setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG)); + break; + } + case LUA_ERRERR: { + setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); + break; + } + case LUA_ERRSYNTAX: + case LUA_ERRRUN: { + setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ + break; + } + } + L->top = oldtop + 1; +} + + +static void restore_stack_limit (lua_State *L) { + lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); + if (L->size_ci > LUAI_MAXCALLS) { /* there was an overflow? */ + int inuse = cast_int(L->ci - L->base_ci); + if (inuse + 1 < LUAI_MAXCALLS) /* can `undo' overflow? */ + luaD_reallocCI(L, LUAI_MAXCALLS); + } +} + + +static void resetstack (lua_State *L, int status) { + L->ci = L->base_ci; + L->base = L->ci->base; + luaF_close(L, L->base); /* close eventual pending closures */ + luaD_seterrorobj(L, status, L->base); + L->nCcalls = L->baseCcalls; + L->allowhook = 1; + restore_stack_limit(L); + L->errfunc = 0; + L->errorJmp = NULL; +} + + +void luaD_throw (lua_State *L, int errcode) { + if (L->errorJmp) { + L->errorJmp->status = errcode; + LUAI_THROW(L, L->errorJmp); + } + else { + L->status = cast_byte(errcode); + if (G(L)->panic) { + resetstack(L, errcode); + lua_unlock(L); + G(L)->panic(L); + } + exit(EXIT_FAILURE); + } +} + + +int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { + struct lua_longjmp lj; + lj.status = 0; + lj.previous = L->errorJmp; /* chain new error handler */ + L->errorJmp = &lj; + LUAI_TRY(L, &lj, + (*f)(L, ud); + ); + L->errorJmp = lj.previous; /* restore old error handler */ + return lj.status; +} + +/* }====================================================== */ + + +static void correctstack (lua_State *L, TValue *oldstack) { + CallInfo *ci; + GCObject *up; + L->top = (L->top - oldstack) + L->stack; + for (up = L->openupval; up != NULL; up = up->gch.next) + gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; + for (ci = L->base_ci; ci <= L->ci; ci++) { + ci->top = (ci->top - oldstack) + L->stack; + ci->base = (ci->base - oldstack) + L->stack; + ci->func = (ci->func - oldstack) + L->stack; + } + L->base = (L->base - oldstack) + L->stack; +} + + +void luaD_reallocstack (lua_State *L, int newsize) { + TValue *oldstack = L->stack; + int realsize = newsize + 1 + EXTRA_STACK; + lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1); + luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue); + L->stacksize = realsize; + L->stack_last = L->stack+newsize; + correctstack(L, oldstack); +} + + +void luaD_reallocCI (lua_State *L, int newsize) { + CallInfo *oldci = L->base_ci; + luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo); + L->size_ci = newsize; + L->ci = (L->ci - oldci) + L->base_ci; + L->end_ci = L->base_ci + L->size_ci - 1; +} + + +void luaD_growstack (lua_State *L, int n) { + if (n <= L->stacksize) /* double size is enough? */ + luaD_reallocstack(L, 2*L->stacksize); + else + luaD_reallocstack(L, L->stacksize + n); +} + + +static CallInfo *growCI (lua_State *L) { + if (L->size_ci > LUAI_MAXCALLS) /* overflow while handling overflow? */ + luaD_throw(L, LUA_ERRERR); + else { + luaD_reallocCI(L, 2*L->size_ci); + if (L->size_ci > LUAI_MAXCALLS) + luaG_runerror(L, "stack overflow"); + } + return ++L->ci; +} + + +void luaD_callhook (lua_State *L, int event, int line) { + lua_Hook hook = L->hook; + if (hook && L->allowhook) { + ptrdiff_t top = savestack(L, L->top); + ptrdiff_t ci_top = savestack(L, L->ci->top); + lua_Debug ar; + ar.event = event; + ar.currentline = line; + if (event == LUA_HOOKTAILRET) + ar.i_ci = 0; /* tail call; no debug information about it */ + else + ar.i_ci = cast_int(L->ci - L->base_ci); + luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ + L->ci->top = L->top + LUA_MINSTACK; + lua_assert(L->ci->top <= L->stack_last); + L->allowhook = 0; /* cannot call hooks inside a hook */ + lua_unlock(L); + (*hook)(L, &ar); + lua_lock(L); + lua_assert(!L->allowhook); + L->allowhook = 1; + L->ci->top = restorestack(L, ci_top); + L->top = restorestack(L, top); + } +} + + +static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { + int i; + int nfixargs = p->numparams; + Table *htab = NULL; + StkId base, fixed; + for (; actual < nfixargs; ++actual) + setnilvalue(L->top++); +#if defined(LUA_COMPAT_VARARG) + if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */ + int nvar = actual - nfixargs; /* number of extra arguments */ + lua_assert(p->is_vararg & VARARG_HASARG); + luaC_checkGC(L); + luaD_checkstack(L, p->maxstacksize); + htab = luaH_new(L, nvar, 1); /* create `arg' table */ + for (i=0; itop - nvar + i); + /* store counter in field `n' */ + setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar)); + } +#endif + /* move fixed parameters to final position */ + fixed = L->top - actual; /* first fixed argument */ + base = L->top; /* final position of first argument */ + for (i=0; itop++, fixed+i); + setnilvalue(fixed+i); + } + /* add `arg' parameter */ + if (htab) { + sethvalue(L, L->top++, htab); + lua_assert(iswhite(obj2gco(htab))); + } + return base; +} + + +static StkId tryfuncTM (lua_State *L, StkId func) { + const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); + StkId p; + ptrdiff_t funcr = savestack(L, func); + if (!ttisfunction(tm)) + luaG_typeerror(L, func, "call"); + /* Open a hole inside the stack at `func' */ + for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); + incr_top(L); + func = restorestack(L, funcr); /* previous call may change stack */ + setobj2s(L, func, tm); /* tag method is the new function to be called */ + return func; +} + + + +#define inc_ci(L) \ + ((L->ci == L->end_ci) ? growCI(L) : \ + (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci)) + + +int luaD_precall (lua_State *L, StkId func, int nresults) { + LClosure *cl; + ptrdiff_t funcr; + if (!ttisfunction(func)) /* `func' is not a function? */ + func = tryfuncTM(L, func); /* check the `function' tag method */ + funcr = savestack(L, func); + cl = &clvalue(func)->l; + L->ci->savedpc = L->savedpc; + if (!cl->isC) { /* Lua function? prepare its call */ + CallInfo *ci; + StkId st, base; + Proto *p = cl->p; + luaD_checkstack(L, p->maxstacksize); + func = restorestack(L, funcr); + if (!p->is_vararg) { /* no varargs? */ + base = func + 1; + if (L->top > base + p->numparams) + L->top = base + p->numparams; + } + else { /* vararg function */ + int nargs = cast_int(L->top - func) - 1; + base = adjust_varargs(L, p, nargs); + func = restorestack(L, funcr); /* previous call may change the stack */ + } + ci = inc_ci(L); /* now `enter' new function */ + ci->func = func; + L->base = ci->base = base; + ci->top = L->base + p->maxstacksize; + lua_assert(ci->top <= L->stack_last); + L->savedpc = p->code; /* starting point */ + ci->tailcalls = 0; + ci->nresults = nresults; + for (st = L->top; st < ci->top; st++) + setnilvalue(st); + L->top = ci->top; + if (L->hookmask & LUA_MASKCALL) { + L->savedpc++; /* hooks assume 'pc' is already incremented */ + luaD_callhook(L, LUA_HOOKCALL, -1); + L->savedpc--; /* correct 'pc' */ + } + return PCRLUA; + } + else { /* if is a C function, call it */ + CallInfo *ci; + int n; + luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ + ci = inc_ci(L); /* now `enter' new function */ + ci->func = restorestack(L, funcr); + L->base = ci->base = ci->func + 1; + ci->top = L->top + LUA_MINSTACK; + lua_assert(ci->top <= L->stack_last); + ci->nresults = nresults; + if (L->hookmask & LUA_MASKCALL) + luaD_callhook(L, LUA_HOOKCALL, -1); + lua_unlock(L); + n = (*curr_func(L)->c.f)(L); /* do the actual call */ + lua_lock(L); + if (n < 0) /* yielding? */ + return PCRYIELD; + else { + luaD_poscall(L, L->top - n); + return PCRC; + } + } +} + + +static StkId callrethooks (lua_State *L, StkId firstResult) { + ptrdiff_t fr = savestack(L, firstResult); /* next call may change stack */ + luaD_callhook(L, LUA_HOOKRET, -1); + if (f_isLua(L->ci)) { /* Lua function? */ + while ((L->hookmask & LUA_MASKRET) && L->ci->tailcalls--) /* tail calls */ + luaD_callhook(L, LUA_HOOKTAILRET, -1); + } + return restorestack(L, fr); +} + + +int luaD_poscall (lua_State *L, StkId firstResult) { + StkId res; + int wanted, i; + CallInfo *ci; + if (L->hookmask & LUA_MASKRET) + firstResult = callrethooks(L, firstResult); + ci = L->ci--; + res = ci->func; /* res == final position of 1st result */ + wanted = ci->nresults; + L->base = (ci - 1)->base; /* restore base */ + L->savedpc = (ci - 1)->savedpc; /* restore savedpc */ + /* move results to correct place */ + for (i = wanted; i != 0 && firstResult < L->top; i--) + setobjs2s(L, res++, firstResult++); + while (i-- > 0) + setnilvalue(res++); + L->top = res; + return (wanted - LUA_MULTRET); /* 0 iff wanted == LUA_MULTRET */ +} + + +/* +** Call a function (C or Lua). The function to be called is at *func. +** The arguments are on the stack, right after the function. +** When returns, all the results are on the stack, starting at the original +** function position. +*/ +void luaD_call (lua_State *L, StkId func, int nResults) { + if (++L->nCcalls >= LUAI_MAXCCALLS) { + if (L->nCcalls == LUAI_MAXCCALLS) + luaG_runerror(L, "C stack overflow"); + else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) + luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ + } + if (luaD_precall(L, func, nResults) == PCRLUA) /* is a Lua function? */ + luaV_execute(L, 1); /* call it */ + L->nCcalls--; + luaC_checkGC(L); +} + + +static void resume (lua_State *L, void *ud) { + StkId firstArg = cast(StkId, ud); + CallInfo *ci = L->ci; + if (L->status == 0) { /* start coroutine? */ + lua_assert(ci == L->base_ci && firstArg > L->base); + if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA) + return; + } + else { /* resuming from previous yield */ + lua_assert(L->status == LUA_YIELD); + L->status = 0; + if (!f_isLua(ci)) { /* `common' yield? */ + /* finish interrupted execution of `OP_CALL' */ + lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL || + GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL); + if (luaD_poscall(L, firstArg)) /* complete it... */ + L->top = L->ci->top; /* and correct top if not multiple results */ + } + else /* yielded inside a hook: just continue its execution */ + L->base = L->ci->base; + } + luaV_execute(L, cast_int(L->ci - L->base_ci)); +} + + +static int resume_error (lua_State *L, const char *msg) { + L->top = L->ci->base; + setsvalue2s(L, L->top, luaS_new(L, msg)); + incr_top(L); + lua_unlock(L); + return LUA_ERRRUN; +} + + +LUA_API int lua_resume (lua_State *L, int nargs) { + int status; + lua_lock(L); + if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci)) + return resume_error(L, "cannot resume non-suspended coroutine"); + if (L->nCcalls >= LUAI_MAXCCALLS) + return resume_error(L, "C stack overflow"); + luai_userstateresume(L, nargs); + lua_assert(L->errfunc == 0); + L->baseCcalls = ++L->nCcalls; + status = luaD_rawrunprotected(L, resume, L->top - nargs); + if (status != 0) { /* error? */ + L->status = cast_byte(status); /* mark thread as `dead' */ + luaD_seterrorobj(L, status, L->top); + L->ci->top = L->top; + } + else { + lua_assert(L->nCcalls == L->baseCcalls); + status = L->status; + } + --L->nCcalls; + lua_unlock(L); + return status; +} + + +LUA_API int lua_yield (lua_State *L, int nresults) { + luai_userstateyield(L, nresults); + lua_lock(L); + if (L->nCcalls > L->baseCcalls) + luaG_runerror(L, "attempt to yield across metamethod/C-call boundary"); + L->base = L->top - nresults; /* protect stack slots below */ + L->status = LUA_YIELD; + lua_unlock(L); + return -1; +} + + +int luaD_pcall (lua_State *L, Pfunc func, void *u, + ptrdiff_t old_top, ptrdiff_t ef) { + int status; + unsigned short oldnCcalls = L->nCcalls; + ptrdiff_t old_ci = saveci(L, L->ci); + lu_byte old_allowhooks = L->allowhook; + ptrdiff_t old_errfunc = L->errfunc; + L->errfunc = ef; + status = luaD_rawrunprotected(L, func, u); + if (status != 0) { /* an error occurred? */ + StkId oldtop = restorestack(L, old_top); + luaF_close(L, oldtop); /* close eventual pending closures */ + luaD_seterrorobj(L, status, oldtop); + L->nCcalls = oldnCcalls; + L->ci = restoreci(L, old_ci); + L->base = L->ci->base; + L->savedpc = L->ci->savedpc; + L->allowhook = old_allowhooks; + restore_stack_limit(L); + } + L->errfunc = old_errfunc; + return status; +} + + + +/* +** Execute a protected parser. +*/ +struct SParser { /* data to `f_parser' */ + ZIO *z; + Mbuffer buff; /* buffer to be used by the scanner */ + const char *name; +}; + +static void f_parser (lua_State *L, void *ud) { + int i; + Proto *tf; + Closure *cl; + struct SParser *p = cast(struct SParser *, ud); + int c = luaZ_lookahead(p->z); + luaC_checkGC(L); + tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z, + &p->buff, p->name); + cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L))); + cl->l.p = tf; + for (i = 0; i < tf->nups; i++) /* initialize eventual upvalues */ + cl->l.upvals[i] = luaF_newupval(L); + setclvalue(L, L->top, cl); + incr_top(L); +} + + +int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) { + struct SParser p; + int status; + p.z = z; p.name = name; + luaZ_initbuffer(L, &p.buff); + status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); + luaZ_freebuffer(L, &p.buff); + return status; +} + + diff --git a/external/lua/lua/ldo.h b/external/lua/lua/ldo.h new file mode 100644 index 0000000000..98fddac59f --- /dev/null +++ b/external/lua/lua/ldo.h @@ -0,0 +1,57 @@ +/* +** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ +** Stack and Call structure of Lua +** See Copyright Notice in lua.h +*/ + +#ifndef ldo_h +#define ldo_h + + +#include "lobject.h" +#include "lstate.h" +#include "lzio.h" + + +#define luaD_checkstack(L,n) \ + if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ + luaD_growstack(L, n); \ + else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); + + +#define incr_top(L) {luaD_checkstack(L,1); L->top++;} + +#define savestack(L,p) ((char *)(p) - (char *)L->stack) +#define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) + +#define saveci(L,p) ((char *)(p) - (char *)L->base_ci) +#define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) + + +/* results from luaD_precall */ +#define PCRLUA 0 /* initiated a call to a Lua function */ +#define PCRC 1 /* did a call to a C function */ +#define PCRYIELD 2 /* C funtion yielded */ + + +/* type of protected functions, to be ran by `runprotected' */ +typedef void (*Pfunc) (lua_State *L, void *ud); + +LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); +LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); +LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); +LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); +LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, + ptrdiff_t oldtop, ptrdiff_t ef); +LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); +LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); +LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); +LUAI_FUNC void luaD_growstack (lua_State *L, int n); + +LUAI_FUNC void luaD_throw (lua_State *L, int errcode); +LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); + +LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); + +#endif + diff --git a/external/lua/lua/ldump.c b/external/lua/lua/ldump.c new file mode 100644 index 0000000000..c9d3d4870f --- /dev/null +++ b/external/lua/lua/ldump.c @@ -0,0 +1,164 @@ +/* +** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ +** save precompiled Lua chunks +** See Copyright Notice in lua.h +*/ + +#include + +#define ldump_c +#define LUA_CORE + +#include "lua.h" + +#include "lobject.h" +#include "lstate.h" +#include "lundump.h" + +typedef struct { + lua_State* L; + lua_Writer writer; + void* data; + int strip; + int status; +} DumpState; + +#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) +#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) + +static void DumpBlock(const void* b, size_t size, DumpState* D) +{ + if (D->status==0) + { + lua_unlock(D->L); + D->status=(*D->writer)(D->L,b,size,D->data); + lua_lock(D->L); + } +} + +static void DumpChar(int y, DumpState* D) +{ + char x=(char)y; + DumpVar(x,D); +} + +static void DumpInt(int x, DumpState* D) +{ + DumpVar(x,D); +} + +static void DumpNumber(lua_Number x, DumpState* D) +{ + DumpVar(x,D); +} + +static void DumpVector(const void* b, int n, size_t size, DumpState* D) +{ + DumpInt(n,D); + DumpMem(b,n,size,D); +} + +static void DumpString(const TString* s, DumpState* D) +{ + if (s==NULL || getstr(s)==NULL) + { + size_t size=0; + DumpVar(size,D); + } + else + { + size_t size=s->tsv.len+1; /* include trailing '\0' */ + DumpVar(size,D); + DumpBlock(getstr(s),size,D); + } +} + +#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) + +static void DumpFunction(const Proto* f, const TString* p, DumpState* D); + +static void DumpConstants(const Proto* f, DumpState* D) +{ + int i,n=f->sizek; + DumpInt(n,D); + for (i=0; ik[i]; + DumpChar(ttype(o),D); + switch (ttype(o)) + { + case LUA_TNIL: + break; + case LUA_TBOOLEAN: + DumpChar(bvalue(o),D); + break; + case LUA_TNUMBER: + DumpNumber(nvalue(o),D); + break; + case LUA_TSTRING: + DumpString(rawtsvalue(o),D); + break; + default: + lua_assert(0); /* cannot happen */ + break; + } + } + n=f->sizep; + DumpInt(n,D); + for (i=0; ip[i],f->source,D); +} + +static void DumpDebug(const Proto* f, DumpState* D) +{ + int i,n; + n= (D->strip) ? 0 : f->sizelineinfo; + DumpVector(f->lineinfo,n,sizeof(int),D); + n= (D->strip) ? 0 : f->sizelocvars; + DumpInt(n,D); + for (i=0; ilocvars[i].varname,D); + DumpInt(f->locvars[i].startpc,D); + DumpInt(f->locvars[i].endpc,D); + } + n= (D->strip) ? 0 : f->sizeupvalues; + DumpInt(n,D); + for (i=0; iupvalues[i],D); +} + +static void DumpFunction(const Proto* f, const TString* p, DumpState* D) +{ + DumpString((f->source==p || D->strip) ? NULL : f->source,D); + DumpInt(f->linedefined,D); + DumpInt(f->lastlinedefined,D); + DumpChar(f->nups,D); + DumpChar(f->numparams,D); + DumpChar(f->is_vararg,D); + DumpChar(f->maxstacksize,D); + DumpCode(f,D); + DumpConstants(f,D); + DumpDebug(f,D); +} + +static void DumpHeader(DumpState* D) +{ + char h[LUAC_HEADERSIZE]; + luaU_header(h); + DumpBlock(h,LUAC_HEADERSIZE,D); +} + +/* +** dump Lua function as precompiled chunk +*/ +int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) +{ + DumpState D; + D.L=L; + D.writer=w; + D.data=data; + D.strip=strip; + D.status=0; + DumpHeader(&D); + DumpFunction(f,NULL,&D); + return D.status; +} diff --git a/external/lua/lua/lfunc.c b/external/lua/lua/lfunc.c new file mode 100644 index 0000000000..813e88f583 --- /dev/null +++ b/external/lua/lua/lfunc.c @@ -0,0 +1,174 @@ +/* +** $Id: lfunc.c,v 2.12.1.2 2007/12/28 14:58:43 roberto Exp $ +** Auxiliary functions to manipulate prototypes and closures +** See Copyright Notice in lua.h +*/ + + +#include + +#define lfunc_c +#define LUA_CORE + +#include "lua.h" + +#include "lfunc.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" + + + +Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { + Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); + luaC_link(L, obj2gco(c), LUA_TFUNCTION); + c->c.isC = 1; + c->c.env = e; + c->c.nupvalues = cast_byte(nelems); + return c; +} + + +Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { + Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); + luaC_link(L, obj2gco(c), LUA_TFUNCTION); + c->l.isC = 0; + c->l.env = e; + c->l.nupvalues = cast_byte(nelems); + while (nelems--) c->l.upvals[nelems] = NULL; + return c; +} + + +UpVal *luaF_newupval (lua_State *L) { + UpVal *uv = luaM_new(L, UpVal); + luaC_link(L, obj2gco(uv), LUA_TUPVAL); + uv->v = &uv->u.value; + setnilvalue(uv->v); + return uv; +} + + +UpVal *luaF_findupval (lua_State *L, StkId level) { + global_State *g = G(L); + GCObject **pp = &L->openupval; + UpVal *p; + UpVal *uv; + while (*pp != NULL && (p = ngcotouv(*pp))->v >= level) { + lua_assert(p->v != &p->u.value); + if (p->v == level) { /* found a corresponding upvalue? */ + if (isdead(g, obj2gco(p))) /* is it dead? */ + changewhite(obj2gco(p)); /* ressurect it */ + return p; + } + pp = &p->next; + } + uv = luaM_new(L, UpVal); /* not found: create a new one */ + uv->tt = LUA_TUPVAL; + uv->marked = luaC_white(g); + uv->v = level; /* current value lives in the stack */ + uv->next = *pp; /* chain it in the proper position */ + *pp = obj2gco(uv); + uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ + uv->u.l.next = g->uvhead.u.l.next; + uv->u.l.next->u.l.prev = uv; + g->uvhead.u.l.next = uv; + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); + return uv; +} + + +static void unlinkupval (UpVal *uv) { + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); + uv->u.l.next->u.l.prev = uv->u.l.prev; /* remove from `uvhead' list */ + uv->u.l.prev->u.l.next = uv->u.l.next; +} + + +void luaF_freeupval (lua_State *L, UpVal *uv) { + if (uv->v != &uv->u.value) /* is it open? */ + unlinkupval(uv); /* remove from open list */ + luaM_free(L, uv); /* free upvalue */ +} + + +void luaF_close (lua_State *L, StkId level) { + UpVal *uv; + global_State *g = G(L); + while (L->openupval != NULL && (uv = ngcotouv(L->openupval))->v >= level) { + GCObject *o = obj2gco(uv); + lua_assert(!isblack(o) && uv->v != &uv->u.value); + L->openupval = uv->next; /* remove from `open' list */ + if (isdead(g, o)) + luaF_freeupval(L, uv); /* free upvalue */ + else { + unlinkupval(uv); + setobj(L, &uv->u.value, uv->v); + uv->v = &uv->u.value; /* now current value lives here */ + luaC_linkupval(L, uv); /* link upvalue into `gcroot' list */ + } + } +} + + +Proto *luaF_newproto (lua_State *L) { + Proto *f = luaM_new(L, Proto); + luaC_link(L, obj2gco(f), LUA_TPROTO); + f->k = NULL; + f->sizek = 0; + f->p = NULL; + f->sizep = 0; + f->code = NULL; + f->sizecode = 0; + f->sizelineinfo = 0; + f->sizeupvalues = 0; + f->nups = 0; + f->upvalues = NULL; + f->numparams = 0; + f->is_vararg = 0; + f->maxstacksize = 0; + f->lineinfo = NULL; + f->sizelocvars = 0; + f->locvars = NULL; + f->linedefined = 0; + f->lastlinedefined = 0; + f->source = NULL; + return f; +} + + +void luaF_freeproto (lua_State *L, Proto *f) { + luaM_freearray(L, f->code, f->sizecode, Instruction); + luaM_freearray(L, f->p, f->sizep, Proto *); + luaM_freearray(L, f->k, f->sizek, TValue); + luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); + luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); + luaM_freearray(L, f->upvalues, f->sizeupvalues, TString *); + luaM_free(L, f); +} + + +void luaF_freeclosure (lua_State *L, Closure *c) { + int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : + sizeLclosure(c->l.nupvalues); + luaM_freemem(L, c, size); +} + + +/* +** Look for n-th local variable at line `line' in function `func'. +** Returns NULL if not found. +*/ +const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { + int i; + for (i = 0; isizelocvars && f->locvars[i].startpc <= pc; i++) { + if (pc < f->locvars[i].endpc) { /* is variable active? */ + local_number--; + if (local_number == 0) + return getstr(f->locvars[i].varname); + } + } + return NULL; /* not found */ +} + diff --git a/external/lua/lua/lfunc.h b/external/lua/lua/lfunc.h new file mode 100644 index 0000000000..a68cf5151c --- /dev/null +++ b/external/lua/lua/lfunc.h @@ -0,0 +1,34 @@ +/* +** $Id: lfunc.h,v 2.4.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions to manipulate prototypes and closures +** See Copyright Notice in lua.h +*/ + +#ifndef lfunc_h +#define lfunc_h + + +#include "lobject.h" + + +#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ + cast(int, sizeof(TValue)*((n)-1))) + +#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ + cast(int, sizeof(TValue *)*((n)-1))) + + +LUAI_FUNC Proto *luaF_newproto (lua_State *L); +LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e); +LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e); +LUAI_FUNC UpVal *luaF_newupval (lua_State *L); +LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); +LUAI_FUNC void luaF_close (lua_State *L, StkId level); +LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); +LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); +LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); +LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, + int pc); + + +#endif diff --git a/external/lua/lua/lgc.c b/external/lua/lua/lgc.c new file mode 100644 index 0000000000..e909c79a96 --- /dev/null +++ b/external/lua/lua/lgc.c @@ -0,0 +1,710 @@ +/* +** $Id: lgc.c,v 2.38.1.2 2011/03/18 18:05:38 roberto Exp $ +** Garbage Collector +** See Copyright Notice in lua.h +*/ + +#include + +#define lgc_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" + + +#define GCSTEPSIZE 1024u +#define GCSWEEPMAX 40 +#define GCSWEEPCOST 10 +#define GCFINALIZECOST 100 + + +#define maskmarks cast_byte(~(bitmask(BLACKBIT)|WHITEBITS)) + +#define makewhite(g,x) \ + ((x)->gch.marked = cast_byte(((x)->gch.marked & maskmarks) | luaC_white(g))) + +#define white2gray(x) reset2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) +#define black2gray(x) resetbit((x)->gch.marked, BLACKBIT) + +#define stringmark(s) reset2bits((s)->tsv.marked, WHITE0BIT, WHITE1BIT) + + +#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT) +#define markfinalized(u) l_setbit((u)->marked, FINALIZEDBIT) + + +#define KEYWEAK bitmask(KEYWEAKBIT) +#define VALUEWEAK bitmask(VALUEWEAKBIT) + + + +#define markvalue(g,o) { checkconsistency(o); \ + if (iscollectable(o) && iswhite(gcvalue(o))) reallymarkobject(g,gcvalue(o)); } + +#define markobject(g,t) { if (iswhite(obj2gco(t))) \ + reallymarkobject(g, obj2gco(t)); } + + +#define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause) + + +static void removeentry (Node *n) { + lua_assert(ttisnil(gval(n))); + if (iscollectable(gkey(n))) + setttype(gkey(n), LUA_TDEADKEY); /* dead key; remove it */ +} + + +static void reallymarkobject (global_State *g, GCObject *o) { + lua_assert(iswhite(o) && !isdead(g, o)); + white2gray(o); + switch (o->gch.tt) { + case LUA_TSTRING: { + return; + } + case LUA_TUSERDATA: { + Table *mt = gco2u(o)->metatable; + gray2black(o); /* udata are never gray */ + if (mt) markobject(g, mt); + markobject(g, gco2u(o)->env); + return; + } + case LUA_TUPVAL: { + UpVal *uv = gco2uv(o); + markvalue(g, uv->v); + if (uv->v == &uv->u.value) /* closed? */ + gray2black(o); /* open upvalues are never black */ + return; + } + case LUA_TFUNCTION: { + gco2cl(o)->c.gclist = g->gray; + g->gray = o; + break; + } + case LUA_TTABLE: { + gco2h(o)->gclist = g->gray; + g->gray = o; + break; + } + case LUA_TTHREAD: { + gco2th(o)->gclist = g->gray; + g->gray = o; + break; + } + case LUA_TPROTO: { + gco2p(o)->gclist = g->gray; + g->gray = o; + break; + } + default: lua_assert(0); + } +} + + +static void marktmu (global_State *g) { + GCObject *u = g->tmudata; + if (u) { + do { + u = u->gch.next; + makewhite(g, u); /* may be marked, if left from previous GC */ + reallymarkobject(g, u); + } while (u != g->tmudata); + } +} + + +/* move `dead' udata that need finalization to list `tmudata' */ +size_t luaC_separateudata (lua_State *L, int all) { + global_State *g = G(L); + size_t deadmem = 0; + GCObject **p = &g->mainthread->next; + GCObject *curr; + while ((curr = *p) != NULL) { + if (!(iswhite(curr) || all) || isfinalized(gco2u(curr))) + p = &curr->gch.next; /* don't bother with them */ + else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) { + markfinalized(gco2u(curr)); /* don't need finalization */ + p = &curr->gch.next; + } + else { /* must call its gc method */ + deadmem += sizeudata(gco2u(curr)); + markfinalized(gco2u(curr)); + *p = curr->gch.next; + /* link `curr' at the end of `tmudata' list */ + if (g->tmudata == NULL) /* list is empty? */ + g->tmudata = curr->gch.next = curr; /* creates a circular list */ + else { + curr->gch.next = g->tmudata->gch.next; + g->tmudata->gch.next = curr; + g->tmudata = curr; + } + } + } + return deadmem; +} + + +static int traversetable (global_State *g, Table *h) { + int i; + int weakkey = 0; + int weakvalue = 0; + const TValue *mode; + if (h->metatable) + markobject(g, h->metatable); + mode = gfasttm(g, h->metatable, TM_MODE); + if (mode && ttisstring(mode)) { /* is there a weak mode? */ + weakkey = (strchr(svalue(mode), 'k') != NULL); + weakvalue = (strchr(svalue(mode), 'v') != NULL); + if (weakkey || weakvalue) { /* is really weak? */ + h->marked &= ~(KEYWEAK | VALUEWEAK); /* clear bits */ + h->marked |= cast_byte((weakkey << KEYWEAKBIT) | + (weakvalue << VALUEWEAKBIT)); + h->gclist = g->weak; /* must be cleared after GC, ... */ + g->weak = obj2gco(h); /* ... so put in the appropriate list */ + } + } + if (weakkey && weakvalue) return 1; + if (!weakvalue) { + i = h->sizearray; + while (i--) + markvalue(g, &h->array[i]); + } + i = sizenode(h); + while (i--) { + Node *n = gnode(h, i); + lua_assert(ttype(gkey(n)) != LUA_TDEADKEY || ttisnil(gval(n))); + if (ttisnil(gval(n))) + removeentry(n); /* remove empty entries */ + else { + lua_assert(!ttisnil(gkey(n))); + if (!weakkey) markvalue(g, gkey(n)); + if (!weakvalue) markvalue(g, gval(n)); + } + } + return weakkey || weakvalue; +} + + +/* +** All marks are conditional because a GC may happen while the +** prototype is still being created +*/ +static void traverseproto (global_State *g, Proto *f) { + int i; + if (f->source) stringmark(f->source); + for (i=0; isizek; i++) /* mark literals */ + markvalue(g, &f->k[i]); + for (i=0; isizeupvalues; i++) { /* mark upvalue names */ + if (f->upvalues[i]) + stringmark(f->upvalues[i]); + } + for (i=0; isizep; i++) { /* mark nested protos */ + if (f->p[i]) + markobject(g, f->p[i]); + } + for (i=0; isizelocvars; i++) { /* mark local-variable names */ + if (f->locvars[i].varname) + stringmark(f->locvars[i].varname); + } +} + + + +static void traverseclosure (global_State *g, Closure *cl) { + markobject(g, cl->c.env); + if (cl->c.isC) { + int i; + for (i=0; ic.nupvalues; i++) /* mark its upvalues */ + markvalue(g, &cl->c.upvalue[i]); + } + else { + int i; + lua_assert(cl->l.nupvalues == cl->l.p->nups); + markobject(g, cl->l.p); + for (i=0; il.nupvalues; i++) /* mark its upvalues */ + markobject(g, cl->l.upvals[i]); + } +} + + +static void checkstacksizes (lua_State *L, StkId max) { + int ci_used = cast_int(L->ci - L->base_ci); /* number of `ci' in use */ + int s_used = cast_int(max - L->stack); /* part of stack in use */ + if (L->size_ci > LUAI_MAXCALLS) /* handling overflow? */ + return; /* do not touch the stacks */ + if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci) + luaD_reallocCI(L, L->size_ci/2); /* still big enough... */ + condhardstacktests(luaD_reallocCI(L, ci_used + 1)); + if (4*s_used < L->stacksize && + 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize) + luaD_reallocstack(L, L->stacksize/2); /* still big enough... */ + condhardstacktests(luaD_reallocstack(L, s_used)); +} + + +static void traversestack (global_State *g, lua_State *l) { + StkId o, lim; + CallInfo *ci; + markvalue(g, gt(l)); + lim = l->top; + for (ci = l->base_ci; ci <= l->ci; ci++) { + lua_assert(ci->top <= l->stack_last); + if (lim < ci->top) lim = ci->top; + } + for (o = l->stack; o < l->top; o++) + markvalue(g, o); + for (; o <= lim; o++) + setnilvalue(o); + checkstacksizes(l, lim); +} + + +/* +** traverse one gray object, turning it to black. +** Returns `quantity' traversed. +*/ +static l_mem propagatemark (global_State *g) { + GCObject *o = g->gray; + lua_assert(isgray(o)); + gray2black(o); + switch (o->gch.tt) { + case LUA_TTABLE: { + Table *h = gco2h(o); + g->gray = h->gclist; + if (traversetable(g, h)) /* table is weak? */ + black2gray(o); /* keep it gray */ + return sizeof(Table) + sizeof(TValue) * h->sizearray + + sizeof(Node) * sizenode(h); + } + case LUA_TFUNCTION: { + Closure *cl = gco2cl(o); + g->gray = cl->c.gclist; + traverseclosure(g, cl); + return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : + sizeLclosure(cl->l.nupvalues); + } + case LUA_TTHREAD: { + lua_State *th = gco2th(o); + g->gray = th->gclist; + th->gclist = g->grayagain; + g->grayagain = o; + black2gray(o); + traversestack(g, th); + return sizeof(lua_State) + sizeof(TValue) * th->stacksize + + sizeof(CallInfo) * th->size_ci; + } + case LUA_TPROTO: { + Proto *p = gco2p(o); + g->gray = p->gclist; + traverseproto(g, p); + return sizeof(Proto) + sizeof(Instruction) * p->sizecode + + sizeof(Proto *) * p->sizep + + sizeof(TValue) * p->sizek + + sizeof(int) * p->sizelineinfo + + sizeof(LocVar) * p->sizelocvars + + sizeof(TString *) * p->sizeupvalues; + } + default: lua_assert(0); return 0; + } +} + + +static size_t propagateall (global_State *g) { + size_t m = 0; + while (g->gray) m += propagatemark(g); + return m; +} + + +/* +** The next function tells whether a key or value can be cleared from +** a weak table. Non-collectable objects are never removed from weak +** tables. Strings behave as `values', so are never removed too. for +** other objects: if really collected, cannot keep them; for userdata +** being finalized, keep them in keys, but not in values +*/ +static int iscleared (const TValue *o, int iskey) { + if (!iscollectable(o)) return 0; + if (ttisstring(o)) { + stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */ + return 0; + } + return iswhite(gcvalue(o)) || + (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o)))); +} + + +/* +** clear collected entries from weaktables +*/ +static void cleartable (GCObject *l) { + while (l) { + Table *h = gco2h(l); + int i = h->sizearray; + lua_assert(testbit(h->marked, VALUEWEAKBIT) || + testbit(h->marked, KEYWEAKBIT)); + if (testbit(h->marked, VALUEWEAKBIT)) { + while (i--) { + TValue *o = &h->array[i]; + if (iscleared(o, 0)) /* value was collected? */ + setnilvalue(o); /* remove value */ + } + } + i = sizenode(h); + while (i--) { + Node *n = gnode(h, i); + if (!ttisnil(gval(n)) && /* non-empty entry? */ + (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { + setnilvalue(gval(n)); /* remove value ... */ + removeentry(n); /* remove entry from table */ + } + } + l = h->gclist; + } +} + + +static void freeobj (lua_State *L, GCObject *o) { + switch (o->gch.tt) { + case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; + case LUA_TFUNCTION: luaF_freeclosure(L, gco2cl(o)); break; + case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; + case LUA_TTABLE: luaH_free(L, gco2h(o)); break; + case LUA_TTHREAD: { + lua_assert(gco2th(o) != L && gco2th(o) != G(L)->mainthread); + luaE_freethread(L, gco2th(o)); + break; + } + case LUA_TSTRING: { + G(L)->strt.nuse--; + luaM_freemem(L, o, sizestring(gco2ts(o))); + break; + } + case LUA_TUSERDATA: { + luaM_freemem(L, o, sizeudata(gco2u(o))); + break; + } + default: lua_assert(0); + } +} + + + +#define sweepwholelist(L,p) sweeplist(L,p,MAX_LUMEM) + + +static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { + GCObject *curr; + global_State *g = G(L); + int deadmask = otherwhite(g); + while ((curr = *p) != NULL && count-- > 0) { + if (curr->gch.tt == LUA_TTHREAD) /* sweep open upvalues of each thread */ + sweepwholelist(L, &gco2th(curr)->openupval); + if ((curr->gch.marked ^ WHITEBITS) & deadmask) { /* not dead? */ + lua_assert(!isdead(g, curr) || testbit(curr->gch.marked, FIXEDBIT)); + makewhite(g, curr); /* make it white (for next cycle) */ + p = &curr->gch.next; + } + else { /* must erase `curr' */ + lua_assert(isdead(g, curr) || deadmask == bitmask(SFIXEDBIT)); + *p = curr->gch.next; + if (curr == g->rootgc) /* is the first element of the list? */ + g->rootgc = curr->gch.next; /* adjust first */ + freeobj(L, curr); + } + } + return p; +} + + +static void checkSizes (lua_State *L) { + global_State *g = G(L); + /* check size of string hash */ + if (g->strt.nuse < cast(lu_int32, g->strt.size/4) && + g->strt.size > MINSTRTABSIZE*2) + luaS_resize(L, g->strt.size/2); /* table is too big */ + /* check size of buffer */ + if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */ + size_t newsize = luaZ_sizebuffer(&g->buff) / 2; + luaZ_resizebuffer(L, &g->buff, newsize); + } +} + + +static void GCTM (lua_State *L) { + global_State *g = G(L); + GCObject *o = g->tmudata->gch.next; /* get first element */ + Udata *udata = rawgco2u(o); + const TValue *tm; + /* remove udata from `tmudata' */ + if (o == g->tmudata) /* last element? */ + g->tmudata = NULL; + else + g->tmudata->gch.next = udata->uv.next; + udata->uv.next = g->mainthread->next; /* return it to `root' list */ + g->mainthread->next = o; + makewhite(g, o); + tm = fasttm(L, udata->uv.metatable, TM_GC); + if (tm != NULL) { + lu_byte oldah = L->allowhook; + lu_mem oldt = g->GCthreshold; + L->allowhook = 0; /* stop debug hooks during GC tag method */ + g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */ + setobj2s(L, L->top, tm); + setuvalue(L, L->top+1, udata); + L->top += 2; + luaD_call(L, L->top - 2, 0); + L->allowhook = oldah; /* restore hooks */ + g->GCthreshold = oldt; /* restore threshold */ + } +} + + +/* +** Call all GC tag methods +*/ +void luaC_callGCTM (lua_State *L) { + while (G(L)->tmudata) + GCTM(L); +} + + +void luaC_freeall (lua_State *L) { + global_State *g = G(L); + int i; + g->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* mask to collect all elements */ + sweepwholelist(L, &g->rootgc); + for (i = 0; i < g->strt.size; i++) /* free all string lists */ + sweepwholelist(L, &g->strt.hash[i]); +} + + +static void markmt (global_State *g) { + int i; + for (i=0; imt[i]) markobject(g, g->mt[i]); +} + + +/* mark root set */ +static void markroot (lua_State *L) { + global_State *g = G(L); + g->gray = NULL; + g->grayagain = NULL; + g->weak = NULL; + markobject(g, g->mainthread); + /* make global table be traversed before main stack */ + markvalue(g, gt(g->mainthread)); + markvalue(g, registry(L)); + markmt(g); + g->gcstate = GCSpropagate; +} + + +static void remarkupvals (global_State *g) { + UpVal *uv; + for (uv = g->uvhead.u.l.next; uv != &g->uvhead; uv = uv->u.l.next) { + lua_assert(uv->u.l.next->u.l.prev == uv && uv->u.l.prev->u.l.next == uv); + if (isgray(obj2gco(uv))) + markvalue(g, uv->v); + } +} + + +static void atomic (lua_State *L) { + global_State *g = G(L); + size_t udsize; /* total size of userdata to be finalized */ + /* remark occasional upvalues of (maybe) dead threads */ + remarkupvals(g); + /* traverse objects cautch by write barrier and by 'remarkupvals' */ + propagateall(g); + /* remark weak tables */ + g->gray = g->weak; + g->weak = NULL; + lua_assert(!iswhite(obj2gco(g->mainthread))); + markobject(g, L); /* mark running thread */ + markmt(g); /* mark basic metatables (again) */ + propagateall(g); + /* remark gray again */ + g->gray = g->grayagain; + g->grayagain = NULL; + propagateall(g); + udsize = luaC_separateudata(L, 0); /* separate userdata to be finalized */ + marktmu(g); /* mark `preserved' userdata */ + udsize += propagateall(g); /* remark, to propagate `preserveness' */ + cleartable(g->weak); /* remove collected objects from weak tables */ + /* flip current white */ + g->currentwhite = cast_byte(otherwhite(g)); + g->sweepstrgc = 0; + g->sweepgc = &g->rootgc; + g->gcstate = GCSsweepstring; + g->estimate = g->totalbytes - udsize; /* first estimate */ +} + + +static l_mem singlestep (lua_State *L) { + global_State *g = G(L); + /*lua_checkmemory(L);*/ + switch (g->gcstate) { + case GCSpause: { + markroot(L); /* start a new collection */ + return 0; + } + case GCSpropagate: { + if (g->gray) + return propagatemark(g); + else { /* no more `gray' objects */ + atomic(L); /* finish mark phase */ + return 0; + } + } + case GCSsweepstring: { + lu_mem old = g->totalbytes; + sweepwholelist(L, &g->strt.hash[g->sweepstrgc++]); + if (g->sweepstrgc >= g->strt.size) /* nothing more to sweep? */ + g->gcstate = GCSsweep; /* end sweep-string phase */ + lua_assert(old >= g->totalbytes); + g->estimate -= old - g->totalbytes; + return GCSWEEPCOST; + } + case GCSsweep: { + lu_mem old = g->totalbytes; + g->sweepgc = sweeplist(L, g->sweepgc, GCSWEEPMAX); + if (*g->sweepgc == NULL) { /* nothing more to sweep? */ + checkSizes(L); + g->gcstate = GCSfinalize; /* end sweep phase */ + } + lua_assert(old >= g->totalbytes); + g->estimate -= old - g->totalbytes; + return GCSWEEPMAX*GCSWEEPCOST; + } + case GCSfinalize: { + if (g->tmudata) { + GCTM(L); + if (g->estimate > GCFINALIZECOST) + g->estimate -= GCFINALIZECOST; + return GCFINALIZECOST; + } + else { + g->gcstate = GCSpause; /* end collection */ + g->gcdept = 0; + return 0; + } + } + default: lua_assert(0); return 0; + } +} + + +void luaC_step (lua_State *L) { + global_State *g = G(L); + l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul; + if (lim == 0) + lim = (MAX_LUMEM-1)/2; /* no limit */ + g->gcdept += g->totalbytes - g->GCthreshold; + do { + lim -= singlestep(L); + if (g->gcstate == GCSpause) + break; + } while (lim > 0); + if (g->gcstate != GCSpause) { + if (g->gcdept < GCSTEPSIZE) + g->GCthreshold = g->totalbytes + GCSTEPSIZE; /* - lim/g->gcstepmul;*/ + else { + g->gcdept -= GCSTEPSIZE; + g->GCthreshold = g->totalbytes; + } + } + else { + setthreshold(g); + } +} + + +void luaC_fullgc (lua_State *L) { + global_State *g = G(L); + if (g->gcstate <= GCSpropagate) { + /* reset sweep marks to sweep all elements (returning them to white) */ + g->sweepstrgc = 0; + g->sweepgc = &g->rootgc; + /* reset other collector lists */ + g->gray = NULL; + g->grayagain = NULL; + g->weak = NULL; + g->gcstate = GCSsweepstring; + } + lua_assert(g->gcstate != GCSpause && g->gcstate != GCSpropagate); + /* finish any pending sweep phase */ + while (g->gcstate != GCSfinalize) { + lua_assert(g->gcstate == GCSsweepstring || g->gcstate == GCSsweep); + singlestep(L); + } + markroot(L); + while (g->gcstate != GCSpause) { + singlestep(L); + } + setthreshold(g); +} + + +void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v) { + global_State *g = G(L); + lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); + lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); + lua_assert(ttype(&o->gch) != LUA_TTABLE); + /* must keep invariant? */ + if (g->gcstate == GCSpropagate) + reallymarkobject(g, v); /* restore invariant */ + else /* don't mind */ + makewhite(g, o); /* mark as white just to avoid other barriers */ +} + + +void luaC_barrierback (lua_State *L, Table *t) { + global_State *g = G(L); + GCObject *o = obj2gco(t); + lua_assert(isblack(o) && !isdead(g, o)); + lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); + black2gray(o); /* make table gray (again) */ + t->gclist = g->grayagain; + g->grayagain = o; +} + + +void luaC_link (lua_State *L, GCObject *o, lu_byte tt) { + global_State *g = G(L); + o->gch.next = g->rootgc; + g->rootgc = o; + o->gch.marked = luaC_white(g); + o->gch.tt = tt; +} + + +void luaC_linkupval (lua_State *L, UpVal *uv) { + global_State *g = G(L); + GCObject *o = obj2gco(uv); + o->gch.next = g->rootgc; /* link upvalue into `rootgc' list */ + g->rootgc = o; + if (isgray(o)) { + if (g->gcstate == GCSpropagate) { + gray2black(o); /* closed upvalues need barrier */ + luaC_barrier(L, uv, uv->v); + } + else { /* sweep phase: sweep it (turning it into white) */ + makewhite(g, o); + lua_assert(g->gcstate != GCSfinalize && g->gcstate != GCSpause); + } + } +} + diff --git a/external/lua/lua/lgc.h b/external/lua/lua/lgc.h new file mode 100644 index 0000000000..5a8dc605b3 --- /dev/null +++ b/external/lua/lua/lgc.h @@ -0,0 +1,110 @@ +/* +** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ +** Garbage Collector +** See Copyright Notice in lua.h +*/ + +#ifndef lgc_h +#define lgc_h + + +#include "lobject.h" + + +/* +** Possible states of the Garbage Collector +*/ +#define GCSpause 0 +#define GCSpropagate 1 +#define GCSsweepstring 2 +#define GCSsweep 3 +#define GCSfinalize 4 + + +/* +** some userful bit tricks +*/ +#define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) +#define setbits(x,m) ((x) |= (m)) +#define testbits(x,m) ((x) & (m)) +#define bitmask(b) (1<<(b)) +#define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) +#define l_setbit(x,b) setbits(x, bitmask(b)) +#define resetbit(x,b) resetbits(x, bitmask(b)) +#define testbit(x,b) testbits(x, bitmask(b)) +#define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) +#define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) +#define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) + + + +/* +** Layout for bit use in `marked' field: +** bit 0 - object is white (type 0) +** bit 1 - object is white (type 1) +** bit 2 - object is black +** bit 3 - for userdata: has been finalized +** bit 3 - for tables: has weak keys +** bit 4 - for tables: has weak values +** bit 5 - object is fixed (should not be collected) +** bit 6 - object is "super" fixed (only the main thread) +*/ + + +#define WHITE0BIT 0 +#define WHITE1BIT 1 +#define BLACKBIT 2 +#define FINALIZEDBIT 3 +#define KEYWEAKBIT 3 +#define VALUEWEAKBIT 4 +#define FIXEDBIT 5 +#define SFIXEDBIT 6 +#define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) + + +#define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) +#define isblack(x) testbit((x)->gch.marked, BLACKBIT) +#define isgray(x) (!isblack(x) && !iswhite(x)) + +#define otherwhite(g) (g->currentwhite ^ WHITEBITS) +#define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) + +#define changewhite(x) ((x)->gch.marked ^= WHITEBITS) +#define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) + +#define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) + +#define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) + + +#define luaC_checkGC(L) { \ + condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ + if (G(L)->totalbytes >= G(L)->GCthreshold) \ + luaC_step(L); } + + +#define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ + luaC_barrierf(L,obj2gco(p),gcvalue(v)); } + +#define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ + luaC_barrierback(L,t); } + +#define luaC_objbarrier(L,p,o) \ + { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ + luaC_barrierf(L,obj2gco(p),obj2gco(o)); } + +#define luaC_objbarriert(L,t,o) \ + { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } + +LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); +LUAI_FUNC void luaC_callGCTM (lua_State *L); +LUAI_FUNC void luaC_freeall (lua_State *L); +LUAI_FUNC void luaC_step (lua_State *L); +LUAI_FUNC void luaC_fullgc (lua_State *L); +LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); +LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); +LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); +LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); + + +#endif diff --git a/external/lua/lua/linit.c b/external/lua/lua/linit.c new file mode 100644 index 0000000000..c1f90dfab7 --- /dev/null +++ b/external/lua/lua/linit.c @@ -0,0 +1,38 @@ +/* +** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $ +** Initialization of libraries for lua.c +** See Copyright Notice in lua.h +*/ + + +#define linit_c +#define LUA_LIB + +#include "lua.h" + +#include "lualib.h" +#include "lauxlib.h" + + +static const luaL_Reg lualibs[] = { + {"", luaopen_base}, + {LUA_LOADLIBNAME, luaopen_package}, + {LUA_TABLIBNAME, luaopen_table}, + {LUA_IOLIBNAME, luaopen_io}, + {LUA_OSLIBNAME, luaopen_os}, + {LUA_STRLIBNAME, luaopen_string}, + {LUA_MATHLIBNAME, luaopen_math}, + {LUA_DBLIBNAME, luaopen_debug}, + {NULL, NULL} +}; + + +LUALIB_API void luaL_openlibs (lua_State *L) { + const luaL_Reg *lib = lualibs; + for (; lib->func; lib++) { + lua_pushcfunction(L, lib->func); + lua_pushstring(L, lib->name); + lua_call(L, 1, 0); + } +} + diff --git a/external/lua/lua/liolib.c b/external/lua/lua/liolib.c new file mode 100644 index 0000000000..e072056ca1 --- /dev/null +++ b/external/lua/lua/liolib.c @@ -0,0 +1,560 @@ +/* +** $Id: liolib.c,v 2.73.1.4 2010/05/14 15:33:51 roberto Exp $ +** Standard I/O (and system) library +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include + +#define liolib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + + +#define IO_INPUT 1 +#define IO_OUTPUT 2 + + +static const char *const fnames[] = {"input", "output"}; + + +static int pushresult (lua_State *L, int i, const char *filename) { + int en = errno; /* calls to Lua API may change this value */ + if (i) { + lua_pushboolean(L, 1); + return 1; + } + else { + lua_pushnil(L); + if (filename) + lua_pushfstring(L, "%s: %s", filename, strerror(en)); + else + lua_pushfstring(L, "%s", strerror(en)); + lua_pushinteger(L, en); + return 3; + } +} + + +static void fileerror (lua_State *L, int arg, const char *filename) { + lua_pushfstring(L, "%s: %s", filename, strerror(errno)); + luaL_argerror(L, arg, lua_tostring(L, -1)); +} + + +#define tofilep(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE)) + + +static int io_type (lua_State *L) { + void *ud; + luaL_checkany(L, 1); + ud = lua_touserdata(L, 1); + lua_getfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); + if (ud == NULL || !lua_getmetatable(L, 1) || !lua_rawequal(L, -2, -1)) + lua_pushnil(L); /* not a file */ + else if (*((FILE **)ud) == NULL) + lua_pushliteral(L, "closed file"); + else + lua_pushliteral(L, "file"); + return 1; +} + + +static FILE *tofile (lua_State *L) { + FILE **f = tofilep(L); + if (*f == NULL) + luaL_error(L, "attempt to use a closed file"); + return *f; +} + + + +/* +** When creating file handles, always creates a `closed' file handle +** before opening the actual file; so, if there is a memory error, the +** file is not left opened. +*/ +static FILE **newfile (lua_State *L) { + FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); + *pf = NULL; /* file handle is currently `closed' */ + luaL_getmetatable(L, LUA_FILEHANDLE); + lua_setmetatable(L, -2); + return pf; +} + + +/* +** function to (not) close the standard files stdin, stdout, and stderr +*/ +static int io_noclose (lua_State *L) { + lua_pushnil(L); + lua_pushliteral(L, "cannot close standard file"); + return 2; +} + + +/* +** function to close 'popen' files +*/ +static int io_pclose (lua_State *L) { + FILE **p = tofilep(L); + int ok = lua_pclose(L, *p); + *p = NULL; + return pushresult(L, ok, NULL); +} + + +/* +** function to close regular files +*/ +static int io_fclose (lua_State *L) { + FILE **p = tofilep(L); + int ok = (fclose(*p) == 0); + *p = NULL; + return pushresult(L, ok, NULL); +} + + +static int aux_close (lua_State *L) { + lua_getfenv(L, 1); + lua_getfield(L, -1, "__close"); + return (lua_tocfunction(L, -1))(L); +} + + +static int io_close (lua_State *L) { + if (lua_isnone(L, 1)) + lua_rawgeti(L, LUA_ENVIRONINDEX, IO_OUTPUT); + tofile(L); /* make sure argument is a file */ + return aux_close(L); +} + + +static int io_gc (lua_State *L) { + FILE *f = *tofilep(L); + /* ignore closed files */ + if (f != NULL) + aux_close(L); + return 0; +} + + +static int io_tostring (lua_State *L) { + FILE *f = *tofilep(L); + if (f == NULL) + lua_pushliteral(L, "file (closed)"); + else + lua_pushfstring(L, "file (%p)", f); + return 1; +} + + +static int io_open (lua_State *L) { + const char *filename = luaL_checkstring(L, 1); + const char *mode = luaL_optstring(L, 2, "r"); + FILE **pf = newfile(L); + *pf = fopen(filename, mode); + return (*pf == NULL) ? pushresult(L, 0, filename) : 1; +} + + +/* +** this function has a separated environment, which defines the +** correct __close for 'popen' files +*/ +static int io_popen (lua_State *L) { + const char *filename = luaL_checkstring(L, 1); + const char *mode = luaL_optstring(L, 2, "r"); + FILE **pf = newfile(L); + *pf = lua_popen(L, filename, mode); + return (*pf == NULL) ? pushresult(L, 0, filename) : 1; +} + + +#ifndef __native_client__ +static int io_tmpfile (lua_State *L) { + FILE **pf = newfile(L); + *pf = tmpfile(); + return (*pf == NULL) ? pushresult(L, 0, NULL) : 1; +} +#endif + + +static FILE *getiofile (lua_State *L, int findex) { + FILE *f; + lua_rawgeti(L, LUA_ENVIRONINDEX, findex); + f = *(FILE **)lua_touserdata(L, -1); + if (f == NULL) + luaL_error(L, "standard %s file is closed", fnames[findex - 1]); + return f; +} + + +static int g_iofile (lua_State *L, int f, const char *mode) { + if (!lua_isnoneornil(L, 1)) { + const char *filename = lua_tostring(L, 1); + if (filename) { + FILE **pf = newfile(L); + *pf = fopen(filename, mode); + if (*pf == NULL) + fileerror(L, 1, filename); + } + else { + tofile(L); /* check that it's a valid file handle */ + lua_pushvalue(L, 1); + } + lua_rawseti(L, LUA_ENVIRONINDEX, f); + } + /* return current value */ + lua_rawgeti(L, LUA_ENVIRONINDEX, f); + return 1; +} + + +static int io_input (lua_State *L) { + return g_iofile(L, IO_INPUT, "r"); +} + + +static int io_output (lua_State *L) { + return g_iofile(L, IO_OUTPUT, "w"); +} + + +static int io_readline (lua_State *L); + + +static void aux_lines (lua_State *L, int idx, int toclose) { + lua_pushvalue(L, idx); + lua_pushboolean(L, toclose); /* close/not close file when finished */ + lua_pushcclosure(L, io_readline, 2); +} + + +static int f_lines (lua_State *L) { + tofile(L); /* check that it's a valid file handle */ + aux_lines(L, 1, 0); + return 1; +} + + +static int io_lines (lua_State *L) { + if (lua_isnoneornil(L, 1)) { /* no arguments? */ + /* will iterate over default input */ + lua_rawgeti(L, LUA_ENVIRONINDEX, IO_INPUT); + return f_lines(L); + } + else { + const char *filename = luaL_checkstring(L, 1); + FILE **pf = newfile(L); + *pf = fopen(filename, "r"); + if (*pf == NULL) + fileerror(L, 1, filename); + aux_lines(L, lua_gettop(L), 1); + return 1; + } +} + + +/* +** {====================================================== +** READ +** ======================================================= +*/ + + +static int read_number (lua_State *L, FILE *f) { + lua_Number d; + if (fscanf(f, LUA_NUMBER_SCAN, &d) == 1) { + lua_pushnumber(L, d); + return 1; + } + else { + lua_pushnil(L); /* "result" to be removed */ + return 0; /* read fails */ + } +} + + +static int test_eof (lua_State *L, FILE *f) { + int c = getc(f); + ungetc(c, f); + lua_pushlstring(L, NULL, 0); + return (c != EOF); +} + + +static int read_line (lua_State *L, FILE *f) { + luaL_Buffer b; + luaL_buffinit(L, &b); + for (;;) { + size_t l; + char *p = luaL_prepbuffer(&b); + if (fgets(p, LUAL_BUFFERSIZE, f) == NULL) { /* eof? */ + luaL_pushresult(&b); /* close buffer */ + return (lua_objlen(L, -1) > 0); /* check whether read something */ + } + l = strlen(p); + if (l == 0 || p[l-1] != '\n') + luaL_addsize(&b, l); + else { + luaL_addsize(&b, l - 1); /* do not include `eol' */ + luaL_pushresult(&b); /* close buffer */ + return 1; /* read at least an `eol' */ + } + } +} + + +static int read_chars (lua_State *L, FILE *f, size_t n) { + size_t rlen; /* how much to read */ + size_t nr; /* number of chars actually read */ + luaL_Buffer b; + luaL_buffinit(L, &b); + rlen = LUAL_BUFFERSIZE; /* try to read that much each time */ + do { + char *p = luaL_prepbuffer(&b); + if (rlen > n) rlen = n; /* cannot read more than asked */ + nr = fread(p, sizeof(char), rlen, f); + luaL_addsize(&b, nr); + n -= nr; /* still have to read `n' chars */ + } while (n > 0 && nr == rlen); /* until end of count or eof */ + luaL_pushresult(&b); /* close buffer */ + return (n == 0 || lua_objlen(L, -1) > 0); +} + + +static int g_read (lua_State *L, FILE *f, int first) { + int nargs = lua_gettop(L) - 1; + int success; + int n; + clearerr(f); + if (nargs == 0) { /* no arguments? */ + success = read_line(L, f); + n = first+1; /* to return 1 result */ + } + else { /* ensure stack space for all results and for auxlib's buffer */ + luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); + success = 1; + for (n = first; nargs-- && success; n++) { + if (lua_type(L, n) == LUA_TNUMBER) { + size_t l = (size_t)lua_tointeger(L, n); + success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l); + } + else { + const char *p = lua_tostring(L, n); + luaL_argcheck(L, p && p[0] == '*', n, "invalid option"); + switch (p[1]) { + case 'n': /* number */ + success = read_number(L, f); + break; + case 'l': /* line */ + success = read_line(L, f); + break; + case 'a': /* file */ + read_chars(L, f, ~((size_t)0)); /* read MAX_SIZE_T chars */ + success = 1; /* always success */ + break; + default: + return luaL_argerror(L, n, "invalid format"); + } + } + } + } + if (ferror(f)) + return pushresult(L, 0, NULL); + if (!success) { + lua_pop(L, 1); /* remove last result */ + lua_pushnil(L); /* push nil instead */ + } + return n - first; +} + + +static int io_read (lua_State *L) { + return g_read(L, getiofile(L, IO_INPUT), 1); +} + + +static int f_read (lua_State *L) { + return g_read(L, tofile(L), 2); +} + + +static int io_readline (lua_State *L) { + FILE *f = *(FILE **)lua_touserdata(L, lua_upvalueindex(1)); + int sucess; + if (f == NULL) /* file is already closed? */ + luaL_error(L, "file is already closed"); + sucess = read_line(L, f); + if (ferror(f)) + return luaL_error(L, "%s", strerror(errno)); + if (sucess) return 1; + else { /* EOF */ + if (lua_toboolean(L, lua_upvalueindex(2))) { /* generator created file? */ + lua_settop(L, 0); + lua_pushvalue(L, lua_upvalueindex(1)); + aux_close(L); /* close it */ + } + return 0; + } +} + +/* }====================================================== */ + + +static int g_write (lua_State *L, FILE *f, int arg) { + int nargs = lua_gettop(L) - 1; + int status = 1; + for (; nargs--; arg++) { + if (lua_type(L, arg) == LUA_TNUMBER) { + /* optimization: could be done exactly as for strings */ + status = status && + fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0; + } + else { + size_t l; + const char *s = luaL_checklstring(L, arg, &l); + status = status && (fwrite(s, sizeof(char), l, f) == l); + } + } + return pushresult(L, status, NULL); +} + + +static int io_write (lua_State *L) { + return g_write(L, getiofile(L, IO_OUTPUT), 1); +} + + +static int f_write (lua_State *L) { + return g_write(L, tofile(L), 2); +} + + +static int f_seek (lua_State *L) { + static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; + static const char *const modenames[] = {"set", "cur", "end", NULL}; + FILE *f = tofile(L); + int op = luaL_checkoption(L, 2, "cur", modenames); + long offset = luaL_optlong(L, 3, 0); + op = fseek(f, offset, mode[op]); + if (op) + return pushresult(L, 0, NULL); /* error */ + else { + lua_pushinteger(L, ftell(f)); + return 1; + } +} + + +static int f_setvbuf (lua_State *L) { + static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; + static const char *const modenames[] = {"no", "full", "line", NULL}; + FILE *f = tofile(L); + int op = luaL_checkoption(L, 2, NULL, modenames); + lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE); + int res = setvbuf(f, NULL, mode[op], sz); + return pushresult(L, res == 0, NULL); +} + + + +static int io_flush (lua_State *L) { + return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL); +} + + +static int f_flush (lua_State *L) { + return pushresult(L, fflush(tofile(L)) == 0, NULL); +} + + +static const luaL_Reg iolib[] = { + {"close", io_close}, + {"flush", io_flush}, + {"input", io_input}, + {"lines", io_lines}, + {"open", io_open}, + {"output", io_output}, + {"popen", io_popen}, + {"read", io_read}, +#ifndef __native_client__ + {"tmpfile", io_tmpfile}, +#endif + {"type", io_type}, + {"write", io_write}, + {NULL, NULL} +}; + + +static const luaL_Reg flib[] = { + {"close", io_close}, + {"flush", f_flush}, + {"lines", f_lines}, + {"read", f_read}, + {"seek", f_seek}, + {"setvbuf", f_setvbuf}, + {"write", f_write}, + {"__gc", io_gc}, + {"__tostring", io_tostring}, + {NULL, NULL} +}; + + +static void createmeta (lua_State *L) { + luaL_newmetatable(L, LUA_FILEHANDLE); /* create metatable for file handles */ + lua_pushvalue(L, -1); /* push metatable */ + lua_setfield(L, -2, "__index"); /* metatable.__index = metatable */ + luaL_register(L, NULL, flib); /* file methods */ +} + + +static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) { + *newfile(L) = f; + if (k > 0) { + lua_pushvalue(L, -1); + lua_rawseti(L, LUA_ENVIRONINDEX, k); + } + lua_pushvalue(L, -2); /* copy environment */ + lua_setfenv(L, -2); /* set it */ + lua_setfield(L, -3, fname); +} + + +static void newfenv (lua_State *L, lua_CFunction cls) { + lua_createtable(L, 0, 1); + lua_pushcfunction(L, cls); + lua_setfield(L, -2, "__close"); +} + + +LUALIB_API int luaopen_io (lua_State *L) { + createmeta(L); + /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ + newfenv(L, io_fclose); + lua_replace(L, LUA_ENVIRONINDEX); + /* open library */ + luaL_register(L, LUA_IOLIBNAME, iolib); + /* create (and set) default files */ + newfenv(L, io_noclose); /* close function for default files */ + createstdfile(L, stdin, IO_INPUT, "stdin"); + createstdfile(L, stdout, IO_OUTPUT, "stdout"); + createstdfile(L, stderr, 0, "stderr"); + lua_pop(L, 1); /* pop environment for default files */ + lua_getfield(L, -1, "popen"); + newfenv(L, io_pclose); /* create environment for 'popen' */ + lua_setfenv(L, -2); /* set fenv for 'popen' */ + lua_pop(L, 1); /* pop 'popen' */ + return 1; +} + diff --git a/external/lua/lua/llex.c b/external/lua/lua/llex.c new file mode 100644 index 0000000000..77afcdedc7 --- /dev/null +++ b/external/lua/lua/llex.c @@ -0,0 +1,463 @@ +/* +** $Id: llex.c,v 2.20.1.2 2009/11/23 14:58:22 roberto Exp $ +** Lexical Analyzer +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + +#define llex_c +#define LUA_CORE + +#include "lua.h" + +#include "ldo.h" +#include "llex.h" +#include "lobject.h" +#include "lparser.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "lzio.h" + + + +#define next(ls) (ls->current = zgetc(ls->z)) + + + + +#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') + + +/* ORDER RESERVED */ +const char *const luaX_tokens [] = { + "and", "break", "do", "else", "elseif", + "end", "false", "for", "function", "if", + "in", "local", "nil", "not", "or", "repeat", + "return", "then", "true", "until", "while", + "..", "...", "==", ">=", "<=", "~=", + "", "", "", "", + NULL +}; + + +#define save_and_next(ls) (save(ls, ls->current), next(ls)) + + +static void save (LexState *ls, int c) { + Mbuffer *b = ls->buff; + if (b->n + 1 > b->buffsize) { + size_t newsize; + if (b->buffsize >= MAX_SIZET/2) + luaX_lexerror(ls, "lexical element too long", 0); + newsize = b->buffsize * 2; + luaZ_resizebuffer(ls->L, b, newsize); + } + b->buffer[b->n++] = cast(char, c); +} + + +void luaX_init (lua_State *L) { + int i; + for (i=0; itsv.reserved = cast_byte(i+1); /* reserved word */ + } +} + + +#define MAXSRC 80 + + +const char *luaX_token2str (LexState *ls, int token) { + if (token < FIRST_RESERVED) { + lua_assert(token == cast(unsigned char, token)); + return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) : + luaO_pushfstring(ls->L, "%c", token); + } + else + return luaX_tokens[token-FIRST_RESERVED]; +} + + +static const char *txtToken (LexState *ls, int token) { + switch (token) { + case TK_NAME: + case TK_STRING: + case TK_NUMBER: + save(ls, '\0'); + return luaZ_buffer(ls->buff); + default: + return luaX_token2str(ls, token); + } +} + + +void luaX_lexerror (LexState *ls, const char *msg, int token) { + char buff[MAXSRC]; + luaO_chunkid(buff, getstr(ls->source), MAXSRC); + msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg); + if (token) + luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token)); + luaD_throw(ls->L, LUA_ERRSYNTAX); +} + + +void luaX_syntaxerror (LexState *ls, const char *msg) { + luaX_lexerror(ls, msg, ls->t.token); +} + + +TString *luaX_newstring (LexState *ls, const char *str, size_t l) { + lua_State *L = ls->L; + TString *ts = luaS_newlstr(L, str, l); + TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */ + if (ttisnil(o)) { + setbvalue(o, 1); /* make sure `str' will not be collected */ + luaC_checkGC(L); + } + return ts; +} + + +static void inclinenumber (LexState *ls) { + int old = ls->current; + lua_assert(currIsNewline(ls)); + next(ls); /* skip `\n' or `\r' */ + if (currIsNewline(ls) && ls->current != old) + next(ls); /* skip `\n\r' or `\r\n' */ + if (++ls->linenumber >= MAX_INT) + luaX_syntaxerror(ls, "chunk has too many lines"); +} + + +void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { + ls->decpoint = '.'; + ls->L = L; + ls->lookahead.token = TK_EOS; /* no look-ahead token */ + ls->z = z; + ls->fs = NULL; + ls->linenumber = 1; + ls->lastline = 1; + ls->source = source; + luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ + next(ls); /* read first char */ +} + + + +/* +** ======================================================= +** LEXICAL ANALYZER +** ======================================================= +*/ + + + +static int check_next (LexState *ls, const char *set) { + if (!strchr(set, ls->current)) + return 0; + save_and_next(ls); + return 1; +} + + +static void buffreplace (LexState *ls, char from, char to) { + size_t n = luaZ_bufflen(ls->buff); + char *p = luaZ_buffer(ls->buff); + while (n--) + if (p[n] == from) p[n] = to; +} + + +static void trydecpoint (LexState *ls, SemInfo *seminfo) { + /* format error: try to update decimal point separator */ + /* struct lconv *cv = localeconv(); removed for android ndk */ + char old = ls->decpoint; + ls->decpoint = '.'; /* (cv ? cv->decimal_point[0] : '.'); removed for android ndk */ + buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */ + if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) { + /* format error with correct decimal point: no more options */ + buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ + luaX_lexerror(ls, "malformed number", TK_NUMBER); + } +} + + +/* LUA_NUMBER */ +static void read_numeral (LexState *ls, SemInfo *seminfo) { + lua_assert(isdigit(ls->current)); + do { + save_and_next(ls); + } while (isdigit(ls->current) || ls->current == '.'); + if (check_next(ls, "Ee")) /* `E'? */ + check_next(ls, "+-"); /* optional exponent sign */ + while (isalnum(ls->current) || ls->current == '_') + save_and_next(ls); + save(ls, '\0'); + buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ + if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */ + trydecpoint(ls, seminfo); /* try to update decimal point separator */ +} + + +static int skip_sep (LexState *ls) { + int count = 0; + int s = ls->current; + lua_assert(s == '[' || s == ']'); + save_and_next(ls); + while (ls->current == '=') { + save_and_next(ls); + count++; + } + return (ls->current == s) ? count : (-count) - 1; +} + + +static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { + int cont = 0; + (void)(cont); /* avoid warnings when `cont' is not used */ + save_and_next(ls); /* skip 2nd `[' */ + if (currIsNewline(ls)) /* string starts with a newline? */ + inclinenumber(ls); /* skip it */ + for (;;) { + switch (ls->current) { + case EOZ: + luaX_lexerror(ls, (seminfo) ? "unfinished long string" : + "unfinished long comment", TK_EOS); + break; /* to avoid warnings */ +#if defined(LUA_COMPAT_LSTR) + case '[': { + if (skip_sep(ls) == sep) { + save_and_next(ls); /* skip 2nd `[' */ + cont++; +#if LUA_COMPAT_LSTR == 1 + if (sep == 0) + luaX_lexerror(ls, "nesting of [[...]] is deprecated", '['); +#endif + } + break; + } +#endif + case ']': { + if (skip_sep(ls) == sep) { + save_and_next(ls); /* skip 2nd `]' */ +#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2 + cont--; + if (sep == 0 && cont >= 0) break; +#endif + goto endloop; + } + break; + } + case '\n': + case '\r': { + save(ls, '\n'); + inclinenumber(ls); + if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ + break; + } + default: { + if (seminfo) save_and_next(ls); + else next(ls); + } + } + } endloop: + if (seminfo) + seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), + luaZ_bufflen(ls->buff) - 2*(2 + sep)); +} + + +static void read_string (LexState *ls, int del, SemInfo *seminfo) { + save_and_next(ls); + while (ls->current != del) { + switch (ls->current) { + case EOZ: + luaX_lexerror(ls, "unfinished string", TK_EOS); + continue; /* to avoid warnings */ + case '\n': + case '\r': + luaX_lexerror(ls, "unfinished string", TK_STRING); + continue; /* to avoid warnings */ + case '\\': { + int c; + next(ls); /* do not save the `\' */ + switch (ls->current) { + case 'a': c = '\a'; break; + case 'b': c = '\b'; break; + case 'f': c = '\f'; break; + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'v': c = '\v'; break; + case '\n': /* go through */ + case '\r': save(ls, '\n'); inclinenumber(ls); continue; + case EOZ: continue; /* will raise an error next loop */ + default: { + if (!isdigit(ls->current)) + save_and_next(ls); /* handles \\, \", \', and \? */ + else { /* \xxx */ + int i = 0; + c = 0; + do { + c = 10*c + (ls->current-'0'); + next(ls); + } while (++i<3 && isdigit(ls->current)); + if (c > UCHAR_MAX) + luaX_lexerror(ls, "escape sequence too large", TK_STRING); + save(ls, c); + } + continue; + } + } + save(ls, c); + next(ls); + continue; + } + default: + save_and_next(ls); + } + } + save_and_next(ls); /* skip delimiter */ + seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, + luaZ_bufflen(ls->buff) - 2); +} + + +static int llex (LexState *ls, SemInfo *seminfo) { + luaZ_resetbuffer(ls->buff); + for (;;) { + switch (ls->current) { + case '\n': + case '\r': { + inclinenumber(ls); + continue; + } + case '-': { + next(ls); + if (ls->current != '-') return '-'; + /* else is a comment */ + next(ls); + if (ls->current == '[') { + int sep = skip_sep(ls); + luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */ + if (sep >= 0) { + read_long_string(ls, NULL, sep); /* long comment */ + luaZ_resetbuffer(ls->buff); + continue; + } + } + /* else short comment */ + while (!currIsNewline(ls) && ls->current != EOZ) + next(ls); + continue; + } + case '[': { + int sep = skip_sep(ls); + if (sep >= 0) { + read_long_string(ls, seminfo, sep); + return TK_STRING; + } + else if (sep == -1) return '['; + else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING); + } + case '=': { + next(ls); + if (ls->current != '=') return '='; + else { next(ls); return TK_EQ; } + } + case '<': { + next(ls); + if (ls->current != '=') return '<'; + else { next(ls); return TK_LE; } + } + case '>': { + next(ls); + if (ls->current != '=') return '>'; + else { next(ls); return TK_GE; } + } + case '~': { + next(ls); + if (ls->current != '=') return '~'; + else { next(ls); return TK_NE; } + } + case '"': + case '\'': { + read_string(ls, ls->current, seminfo); + return TK_STRING; + } + case '.': { + save_and_next(ls); + if (check_next(ls, ".")) { + if (check_next(ls, ".")) + return TK_DOTS; /* ... */ + else return TK_CONCAT; /* .. */ + } + else if (!isdigit(ls->current)) return '.'; + else { + read_numeral(ls, seminfo); + return TK_NUMBER; + } + } + case EOZ: { + return TK_EOS; + } + default: { + if (isspace(ls->current)) { + lua_assert(!currIsNewline(ls)); + next(ls); + continue; + } + else if (isdigit(ls->current)) { + read_numeral(ls, seminfo); + return TK_NUMBER; + } + else if (isalpha(ls->current) || ls->current == '_') { + /* identifier or reserved word */ + TString *ts; + do { + save_and_next(ls); + } while (isalnum(ls->current) || ls->current == '_'); + ts = luaX_newstring(ls, luaZ_buffer(ls->buff), + luaZ_bufflen(ls->buff)); + if (ts->tsv.reserved > 0) /* reserved word? */ + return ts->tsv.reserved - 1 + FIRST_RESERVED; + else { + seminfo->ts = ts; + return TK_NAME; + } + } + else { + int c = ls->current; + next(ls); + return c; /* single-char tokens (+ - / ...) */ + } + } + } + } +} + + +void luaX_next (LexState *ls) { + ls->lastline = ls->linenumber; + if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ + ls->t = ls->lookahead; /* use this one */ + ls->lookahead.token = TK_EOS; /* and discharge it */ + } + else + ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ +} + + +void luaX_lookahead (LexState *ls) { + lua_assert(ls->lookahead.token == TK_EOS); + ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); +} + diff --git a/external/lua/lua/llex.h b/external/lua/lua/llex.h new file mode 100644 index 0000000000..a9201cee48 --- /dev/null +++ b/external/lua/lua/llex.h @@ -0,0 +1,81 @@ +/* +** $Id: llex.h,v 1.58.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lexical Analyzer +** See Copyright Notice in lua.h +*/ + +#ifndef llex_h +#define llex_h + +#include "lobject.h" +#include "lzio.h" + + +#define FIRST_RESERVED 257 + +/* maximum length of a reserved word */ +#define TOKEN_LEN (sizeof("function")/sizeof(char)) + + +/* +* WARNING: if you change the order of this enumeration, +* grep "ORDER RESERVED" +*/ +enum RESERVED { + /* terminal symbols denoted by reserved words */ + TK_AND = FIRST_RESERVED, TK_BREAK, + TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, + TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, + TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, + /* other terminal symbols */ + TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER, + TK_NAME, TK_STRING, TK_EOS +}; + +/* number of reserved words */ +#define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) + + +/* array with token `names' */ +LUAI_DATA const char *const luaX_tokens []; + + +typedef union { + lua_Number r; + TString *ts; +} SemInfo; /* semantics information */ + + +typedef struct Token { + int token; + SemInfo seminfo; +} Token; + + +typedef struct LexState { + int current; /* current character (charint) */ + int linenumber; /* input line counter */ + int lastline; /* line of last token `consumed' */ + Token t; /* current token */ + Token lookahead; /* look ahead token */ + struct FuncState *fs; /* `FuncState' is private to the parser */ + struct lua_State *L; + ZIO *z; /* input stream */ + Mbuffer *buff; /* buffer for tokens */ + TString *source; /* current source name */ + char decpoint; /* locale decimal point */ +} LexState; + + +LUAI_FUNC void luaX_init (lua_State *L); +LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, + TString *source); +LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); +LUAI_FUNC void luaX_next (LexState *ls); +LUAI_FUNC void luaX_lookahead (LexState *ls); +LUAI_FUNC void luaX_lexerror (LexState *ls, const char *msg, int token); +LUAI_FUNC void luaX_syntaxerror (LexState *ls, const char *s); +LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); + + +#endif diff --git a/external/lua/lua/llimits.h b/external/lua/lua/llimits.h new file mode 100644 index 0000000000..ca8dcb7224 --- /dev/null +++ b/external/lua/lua/llimits.h @@ -0,0 +1,128 @@ +/* +** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ +** Limits, basic types, and some other `installation-dependent' definitions +** See Copyright Notice in lua.h +*/ + +#ifndef llimits_h +#define llimits_h + + +#include +#include + + +#include "lua.h" + + +typedef LUAI_UINT32 lu_int32; + +typedef LUAI_UMEM lu_mem; + +typedef LUAI_MEM l_mem; + + + +/* chars used as small naturals (so that `char' is reserved for characters) */ +typedef unsigned char lu_byte; + + +#define MAX_SIZET ((size_t)(~(size_t)0)-2) + +#define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) + + +#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ + +/* +** conversion of pointer to integer +** this is for hashing only; there is no problem if the integer +** cannot hold the whole pointer value +*/ +#define IntPoint(p) ((unsigned int)(lu_mem)(p)) + + + +/* type to ensure maximum alignment */ +typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; + + +/* result of a `usual argument conversion' over lua_Number */ +typedef LUAI_UACNUMBER l_uacNumber; + + +/* internal assertions for in-house debugging */ +#ifdef lua_assert + +#define check_exp(c,e) (lua_assert(c), (e)) +#define api_check(l,e) lua_assert(e) + +#else + +#define lua_assert(c) ((void)0) +#define check_exp(c,e) (e) +#define api_check luai_apicheck + +#endif + + +#ifndef UNUSED +#define UNUSED(x) ((void)(x)) /* to avoid warnings */ +#endif + + +#ifndef cast +#define cast(t, exp) ((t)(exp)) +#endif + +#define cast_byte(i) cast(lu_byte, (i)) +#define cast_num(i) cast(lua_Number, (i)) +#define cast_int(i) cast(int, (i)) + + + +/* +** type for virtual-machine instructions +** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) +*/ +typedef lu_int32 Instruction; + + + +/* maximum stack for a Lua function */ +#define MAXSTACK 250 + + + +/* minimum size for the string table (must be power of 2) */ +#ifndef MINSTRTABSIZE +#define MINSTRTABSIZE 32 +#endif + + +/* minimum size for string buffer */ +#ifndef LUA_MINBUFFER +#define LUA_MINBUFFER 32 +#endif + + +#ifndef lua_lock +#define lua_lock(L) ((void) 0) +#define lua_unlock(L) ((void) 0) +#endif + +#ifndef luai_threadyield +#define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} +#endif + + +/* +** macro to control inclusion of some hard tests on stack reallocation +*/ +#ifndef HARDSTACKTESTS +#define condhardstacktests(x) ((void)0) +#else +#define condhardstacktests(x) x +#endif + +#endif diff --git a/external/lua/lua/lmathlib.c b/external/lua/lua/lmathlib.c new file mode 100644 index 0000000000..441fbf736c --- /dev/null +++ b/external/lua/lua/lmathlib.c @@ -0,0 +1,263 @@ +/* +** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $ +** Standard mathematical library +** See Copyright Notice in lua.h +*/ + + +#include +#include + +#define lmathlib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +#undef PI +#define PI (3.14159265358979323846) +#define RADIANS_PER_DEGREE (PI/180.0) + + + +static int math_abs (lua_State *L) { + lua_pushnumber(L, fabs(luaL_checknumber(L, 1))); + return 1; +} + +static int math_sin (lua_State *L) { + lua_pushnumber(L, sin(luaL_checknumber(L, 1))); + return 1; +} + +static int math_sinh (lua_State *L) { + lua_pushnumber(L, sinh(luaL_checknumber(L, 1))); + return 1; +} + +static int math_cos (lua_State *L) { + lua_pushnumber(L, cos(luaL_checknumber(L, 1))); + return 1; +} + +static int math_cosh (lua_State *L) { + lua_pushnumber(L, cosh(luaL_checknumber(L, 1))); + return 1; +} + +static int math_tan (lua_State *L) { + lua_pushnumber(L, tan(luaL_checknumber(L, 1))); + return 1; +} + +static int math_tanh (lua_State *L) { + lua_pushnumber(L, tanh(luaL_checknumber(L, 1))); + return 1; +} + +static int math_asin (lua_State *L) { + lua_pushnumber(L, asin(luaL_checknumber(L, 1))); + return 1; +} + +static int math_acos (lua_State *L) { + lua_pushnumber(L, acos(luaL_checknumber(L, 1))); + return 1; +} + +static int math_atan (lua_State *L) { + lua_pushnumber(L, atan(luaL_checknumber(L, 1))); + return 1; +} + +static int math_atan2 (lua_State *L) { + lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +static int math_ceil (lua_State *L) { + lua_pushnumber(L, ceil(luaL_checknumber(L, 1))); + return 1; +} + +static int math_floor (lua_State *L) { + lua_pushnumber(L, floor(luaL_checknumber(L, 1))); + return 1; +} + +static int math_fmod (lua_State *L) { + lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +static int math_modf (lua_State *L) { + double ip; + double fp = modf(luaL_checknumber(L, 1), &ip); + lua_pushnumber(L, ip); + lua_pushnumber(L, fp); + return 2; +} + +static int math_sqrt (lua_State *L) { + lua_pushnumber(L, sqrt(luaL_checknumber(L, 1))); + return 1; +} + +static int math_pow (lua_State *L) { + lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); + return 1; +} + +static int math_log (lua_State *L) { + lua_pushnumber(L, log(luaL_checknumber(L, 1))); + return 1; +} + +static int math_log10 (lua_State *L) { + lua_pushnumber(L, log10(luaL_checknumber(L, 1))); + return 1; +} + +static int math_exp (lua_State *L) { + lua_pushnumber(L, exp(luaL_checknumber(L, 1))); + return 1; +} + +static int math_deg (lua_State *L) { + lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE); + return 1; +} + +static int math_rad (lua_State *L) { + lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE); + return 1; +} + +static int math_frexp (lua_State *L) { + int e; + lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e)); + lua_pushinteger(L, e); + return 2; +} + +static int math_ldexp (lua_State *L) { + lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2))); + return 1; +} + + + +static int math_min (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + lua_Number dmin = luaL_checknumber(L, 1); + int i; + for (i=2; i<=n; i++) { + lua_Number d = luaL_checknumber(L, i); + if (d < dmin) + dmin = d; + } + lua_pushnumber(L, dmin); + return 1; +} + + +static int math_max (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + lua_Number dmax = luaL_checknumber(L, 1); + int i; + for (i=2; i<=n; i++) { + lua_Number d = luaL_checknumber(L, i); + if (d > dmax) + dmax = d; + } + lua_pushnumber(L, dmax); + return 1; +} + + +static int math_random (lua_State *L) { + /* the `%' avoids the (rare) case of r==1, and is needed also because on + some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */ + lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX; + switch (lua_gettop(L)) { /* check number of arguments */ + case 0: { /* no arguments */ + lua_pushnumber(L, r); /* Number between 0 and 1 */ + break; + } + case 1: { /* only upper limit */ + int u = luaL_checkint(L, 1); + luaL_argcheck(L, 1<=u, 1, "interval is empty"); + lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */ + break; + } + case 2: { /* lower and upper limits */ + int l = luaL_checkint(L, 1); + int u = luaL_checkint(L, 2); + luaL_argcheck(L, l<=u, 2, "interval is empty"); + lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */ + break; + } + default: return luaL_error(L, "wrong number of arguments"); + } + return 1; +} + + +static int math_randomseed (lua_State *L) { + srand(luaL_checkint(L, 1)); + return 0; +} + + +static const luaL_Reg mathlib[] = { + {"abs", math_abs}, + {"acos", math_acos}, + {"asin", math_asin}, + {"atan2", math_atan2}, + {"atan", math_atan}, + {"ceil", math_ceil}, + {"cosh", math_cosh}, + {"cos", math_cos}, + {"deg", math_deg}, + {"exp", math_exp}, + {"floor", math_floor}, + {"fmod", math_fmod}, + {"frexp", math_frexp}, + {"ldexp", math_ldexp}, + {"log10", math_log10}, + {"log", math_log}, + {"max", math_max}, + {"min", math_min}, + {"modf", math_modf}, + {"pow", math_pow}, + {"rad", math_rad}, + {"random", math_random}, + {"randomseed", math_randomseed}, + {"sinh", math_sinh}, + {"sin", math_sin}, + {"sqrt", math_sqrt}, + {"tanh", math_tanh}, + {"tan", math_tan}, + {NULL, NULL} +}; + + +/* +** Open math library +*/ +LUALIB_API int luaopen_math (lua_State *L) { + luaL_register(L, LUA_MATHLIBNAME, mathlib); + lua_pushnumber(L, PI); + lua_setfield(L, -2, "pi"); + lua_pushnumber(L, HUGE_VAL); + lua_setfield(L, -2, "huge"); +#if defined(LUA_COMPAT_MOD) + lua_getfield(L, -1, "fmod"); + lua_setfield(L, -2, "mod"); +#endif + return 1; +} + diff --git a/external/lua/lua/lmem.c b/external/lua/lua/lmem.c new file mode 100644 index 0000000000..ae7d8c965f --- /dev/null +++ b/external/lua/lua/lmem.c @@ -0,0 +1,86 @@ +/* +** $Id: lmem.c,v 1.70.1.1 2007/12/27 13:02:25 roberto Exp $ +** Interface to Memory Manager +** See Copyright Notice in lua.h +*/ + + +#include + +#define lmem_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" + + + +/* +** About the realloc function: +** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); +** (`osize' is the old size, `nsize' is the new size) +** +** Lua ensures that (ptr == NULL) iff (osize == 0). +** +** * frealloc(ud, NULL, 0, x) creates a new block of size `x' +** +** * frealloc(ud, p, x, 0) frees the block `p' +** (in this specific case, frealloc must return NULL). +** particularly, frealloc(ud, NULL, 0, 0) does nothing +** (which is equivalent to free(NULL) in ANSI C) +** +** frealloc returns NULL if it cannot create or reallocate the area +** (any reallocation to an equal or smaller size cannot fail!) +*/ + + + +#define MINSIZEARRAY 4 + + +void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, + int limit, const char *errormsg) { + void *newblock; + int newsize; + if (*size >= limit/2) { /* cannot double it? */ + if (*size >= limit) /* cannot grow even a little? */ + luaG_runerror(L, errormsg); + newsize = limit; /* still have at least one free place */ + } + else { + newsize = (*size)*2; + if (newsize < MINSIZEARRAY) + newsize = MINSIZEARRAY; /* minimum size */ + } + newblock = luaM_reallocv(L, block, *size, newsize, size_elems); + *size = newsize; /* update only when everything else is OK */ + return newblock; +} + + +void *luaM_toobig (lua_State *L) { + luaG_runerror(L, "memory allocation error: block too big"); + return NULL; /* to avoid warnings */ +} + + + +/* +** generic allocation routine. +*/ +void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { + global_State *g = G(L); + lua_assert((osize == 0) == (block == NULL)); + block = (*g->frealloc)(g->ud, block, osize, nsize); + if (block == NULL && nsize > 0) + luaD_throw(L, LUA_ERRMEM); + lua_assert((nsize == 0) == (block == NULL)); + g->totalbytes = (g->totalbytes - osize) + nsize; + return block; +} + diff --git a/external/lua/lua/lmem.h b/external/lua/lua/lmem.h new file mode 100644 index 0000000000..7c2dcb3220 --- /dev/null +++ b/external/lua/lua/lmem.h @@ -0,0 +1,49 @@ +/* +** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ +** Interface to Memory Manager +** See Copyright Notice in lua.h +*/ + +#ifndef lmem_h +#define lmem_h + + +#include + +#include "llimits.h" +#include "lua.h" + +#define MEMERRMSG "not enough memory" + + +#define luaM_reallocv(L,b,on,n,e) \ + ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ + luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ + luaM_toobig(L)) + +#define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) +#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) +#define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) + +#define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) +#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) +#define luaM_newvector(L,n,t) \ + cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) + +#define luaM_growvector(L,v,nelems,size,t,limit,e) \ + if ((nelems)+1 > (size)) \ + ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) + +#define luaM_reallocvector(L, v,oldn,n,t) \ + ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) + + +LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, + size_t size); +LUAI_FUNC void *luaM_toobig (lua_State *L); +LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, + size_t size_elem, int limit, + const char *errormsg); + +#endif + diff --git a/external/lua/lua/loadlib.c b/external/lua/lua/loadlib.c new file mode 100644 index 0000000000..6158c5353d --- /dev/null +++ b/external/lua/lua/loadlib.c @@ -0,0 +1,666 @@ +/* +** $Id: loadlib.c,v 1.52.1.4 2009/09/09 13:17:16 roberto Exp $ +** Dynamic library loader for Lua +** See Copyright Notice in lua.h +** +** This module contains an implementation of loadlib for Unix systems +** that have dlfcn, an implementation for Darwin (Mac OS X), an +** implementation for Windows, and a stub for other systems. +*/ + + +#include +#include + + +#define loadlib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +/* prefix for open functions in C libraries */ +#define LUA_POF "luaopen_" + +/* separator for open functions in C libraries */ +#define LUA_OFSEP "_" + + +#define LIBPREFIX "LOADLIB: " + +#define POF LUA_POF +#define LIB_FAIL "open" + + +/* error codes for ll_loadfunc */ +#define ERRLIB 1 +#define ERRFUNC 2 + +#define setprogdir(L) ((void)0) + + +static void ll_unloadlib (void *lib); +static void *ll_load (lua_State *L, const char *path); +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym); + + + +#if defined(LUA_DL_DLOPEN) +/* +** {======================================================================== +** This is an implementation of loadlib based on the dlfcn interface. +** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, +** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least +** as an emulation layer on top of native functions. +** ========================================================================= +*/ + +#include + +static void ll_unloadlib (void *lib) { + dlclose(lib); +} + + +static void *ll_load (lua_State *L, const char *path) { + void *lib = dlopen(path, RTLD_NOW); + if (lib == NULL) lua_pushstring(L, dlerror()); + return lib; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + lua_CFunction f = (lua_CFunction)dlsym(lib, sym); + if (f == NULL) lua_pushstring(L, dlerror()); + return f; +} + +/* }====================================================== */ + + + +#elif defined(LUA_DL_DLL) +/* +** {====================================================================== +** This is an implementation of loadlib for Windows using native functions. +** ======================================================================= +*/ + +#include + + +#undef setprogdir + +static void setprogdir (lua_State *L) { + char buff[MAX_PATH + 1]; + char *lb; + DWORD nsize = sizeof(buff)/sizeof(char); + DWORD n = GetModuleFileNameA(NULL, buff, nsize); + if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) + luaL_error(L, "unable to get ModuleFileName"); + else { + *lb = '\0'; + luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); + lua_remove(L, -2); /* remove original string */ + } +} + + +static void pusherror (lua_State *L) { + int error = GetLastError(); + char buffer[128]; + if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, error, 0, buffer, sizeof(buffer), NULL)) + lua_pushstring(L, buffer); + else + lua_pushfstring(L, "system error %d\n", error); +} + +static void ll_unloadlib (void *lib) { + FreeLibrary((HINSTANCE)lib); +} + + +static void *ll_load (lua_State *L, const char *path) { + HINSTANCE lib = LoadLibraryA(path); + if (lib == NULL) pusherror(L); + return lib; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); + if (f == NULL) pusherror(L); + return f; +} + +/* }====================================================== */ + + + +#elif defined(LUA_DL_DYLD) +/* +** {====================================================================== +** Native Mac OS X / Darwin Implementation +** ======================================================================= +*/ + +#include + + +/* Mac appends a `_' before C function names */ +#undef POF +#define POF "_" LUA_POF + + +static void pusherror (lua_State *L) { + const char *err_str; + const char *err_file; + NSLinkEditErrors err; + int err_num; + NSLinkEditError(&err, &err_num, &err_file, &err_str); + lua_pushstring(L, err_str); +} + + +static const char *errorfromcode (NSObjectFileImageReturnCode ret) { + switch (ret) { + case NSObjectFileImageInappropriateFile: + return "file is not a bundle"; + case NSObjectFileImageArch: + return "library is for wrong CPU type"; + case NSObjectFileImageFormat: + return "bad format"; + case NSObjectFileImageAccess: + return "cannot access file"; + case NSObjectFileImageFailure: + default: + return "unable to load library"; + } +} + + +static void ll_unloadlib (void *lib) { + NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_RESET_LAZY_REFERENCES); +} + + +static void *ll_load (lua_State *L, const char *path) { + NSObjectFileImage img; + NSObjectFileImageReturnCode ret; + /* this would be a rare case, but prevents crashing if it happens */ + if(!_dyld_present()) { + lua_pushliteral(L, "dyld not present"); + return NULL; + } + ret = NSCreateObjectFileImageFromFile(path, &img); + if (ret == NSObjectFileImageSuccess) { + NSModule mod = NSLinkModule(img, path, NSLINKMODULE_OPTION_PRIVATE | + NSLINKMODULE_OPTION_RETURN_ON_ERROR); + NSDestroyObjectFileImage(img); + if (mod == NULL) pusherror(L); + return mod; + } + lua_pushstring(L, errorfromcode(ret)); + return NULL; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + NSSymbol nss = NSLookupSymbolInModule((NSModule)lib, sym); + if (nss == NULL) { + lua_pushfstring(L, "symbol " LUA_QS " not found", sym); + return NULL; + } + return (lua_CFunction)NSAddressOfSymbol(nss); +} + +/* }====================================================== */ + + + +#else +/* +** {====================================================== +** Fallback for other systems +** ======================================================= +*/ + +#undef LIB_FAIL +#define LIB_FAIL "absent" + + +#define DLMSG "dynamic libraries not enabled; check your Lua installation" + + +static void ll_unloadlib (void *lib) { + (void)lib; /* to avoid warnings */ +} + + +static void *ll_load (lua_State *L, const char *path) { + (void)path; /* to avoid warnings */ + lua_pushliteral(L, DLMSG); + return NULL; +} + + +static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { + (void)lib; (void)sym; /* to avoid warnings */ + lua_pushliteral(L, DLMSG); + return NULL; +} + +/* }====================================================== */ +#endif + + + +static void **ll_register (lua_State *L, const char *path) { + void **plib; + lua_pushfstring(L, "%s%s", LIBPREFIX, path); + lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ + if (!lua_isnil(L, -1)) /* is there an entry? */ + plib = (void **)lua_touserdata(L, -1); + else { /* no entry yet; create one */ + lua_pop(L, 1); + plib = (void **)lua_newuserdata(L, sizeof(const void *)); + *plib = NULL; + luaL_getmetatable(L, "_LOADLIB"); + lua_setmetatable(L, -2); + lua_pushfstring(L, "%s%s", LIBPREFIX, path); + lua_pushvalue(L, -2); + lua_settable(L, LUA_REGISTRYINDEX); + } + return plib; +} + + +/* +** __gc tag method: calls library's `ll_unloadlib' function with the lib +** handle +*/ +static int gctm (lua_State *L) { + void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); + if (*lib) ll_unloadlib(*lib); + *lib = NULL; /* mark library as closed */ + return 0; +} + + +static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { + void **reg = ll_register(L, path); + if (*reg == NULL) *reg = ll_load(L, path); + if (*reg == NULL) + return ERRLIB; /* unable to load library */ + else { + lua_CFunction f = ll_sym(L, *reg, sym); + if (f == NULL) + return ERRFUNC; /* unable to find function */ + lua_pushcfunction(L, f); + return 0; /* return function */ + } +} + + +static int ll_loadlib (lua_State *L) { + const char *path = luaL_checkstring(L, 1); + const char *init = luaL_checkstring(L, 2); + int stat = ll_loadfunc(L, path, init); + if (stat == 0) /* no errors? */ + return 1; /* return the loaded function */ + else { /* error; error message is on stack top */ + lua_pushnil(L); + lua_insert(L, -2); + lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); + return 3; /* return nil, error message, and where */ + } +} + + + +/* +** {====================================================== +** 'require' function +** ======================================================= +*/ + + +static int readable (const char *filename) { + FILE *f = fopen(filename, "r"); /* try to open file */ + if (f == NULL) return 0; /* open failed */ + fclose(f); + return 1; +} + + +static const char *pushnexttemplate (lua_State *L, const char *path) { + const char *l; + while (*path == *LUA_PATHSEP) path++; /* skip separators */ + if (*path == '\0') return NULL; /* no more templates */ + l = strchr(path, *LUA_PATHSEP); /* find next separator */ + if (l == NULL) l = path + strlen(path); + lua_pushlstring(L, path, l - path); /* template */ + return l; +} + + +static const char *findfile (lua_State *L, const char *name, + const char *pname) { + const char *path; + name = luaL_gsub(L, name, ".", LUA_DIRSEP); + lua_getfield(L, LUA_ENVIRONINDEX, pname); + path = lua_tostring(L, -1); + if (path == NULL) + luaL_error(L, LUA_QL("package.%s") " must be a string", pname); + lua_pushliteral(L, ""); /* error accumulator */ + while ((path = pushnexttemplate(L, path)) != NULL) { + const char *filename; + filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); + lua_remove(L, -2); /* remove path template */ + if (readable(filename)) /* does file exist and is readable? */ + return filename; /* return that file name */ + lua_pushfstring(L, "\n\tno file " LUA_QS, filename); + lua_remove(L, -2); /* remove file name */ + lua_concat(L, 2); /* add entry to possible error message */ + } + return NULL; /* not found */ +} + + +static void loaderror (lua_State *L, const char *filename) { + luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", + lua_tostring(L, 1), filename, lua_tostring(L, -1)); +} + + +static int loader_Lua (lua_State *L) { + const char *filename; + const char *name = luaL_checkstring(L, 1); + filename = findfile(L, name, "path"); + if (filename == NULL) return 1; /* library not found in this path */ + if (luaL_loadfile(L, filename) != 0) + loaderror(L, filename); + return 1; /* library loaded successfully */ +} + + +static const char *mkfuncname (lua_State *L, const char *modname) { + const char *funcname; + const char *mark = strchr(modname, *LUA_IGMARK); + if (mark) modname = mark + 1; + funcname = luaL_gsub(L, modname, ".", LUA_OFSEP); + funcname = lua_pushfstring(L, POF"%s", funcname); + lua_remove(L, -2); /* remove 'gsub' result */ + return funcname; +} + + +static int loader_C (lua_State *L) { + const char *funcname; + const char *name = luaL_checkstring(L, 1); + const char *filename = findfile(L, name, "cpath"); + if (filename == NULL) return 1; /* library not found in this path */ + funcname = mkfuncname(L, name); + if (ll_loadfunc(L, filename, funcname) != 0) + loaderror(L, filename); + return 1; /* library loaded successfully */ +} + + +static int loader_Croot (lua_State *L) { + const char *funcname; + const char *filename; + const char *name = luaL_checkstring(L, 1); + const char *p = strchr(name, '.'); + int stat; + if (p == NULL) return 0; /* is root */ + lua_pushlstring(L, name, p - name); + filename = findfile(L, lua_tostring(L, -1), "cpath"); + if (filename == NULL) return 1; /* root not found */ + funcname = mkfuncname(L, name); + if ((stat = ll_loadfunc(L, filename, funcname)) != 0) { + if (stat != ERRFUNC) loaderror(L, filename); /* real error */ + lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, + name, filename); + return 1; /* function not found */ + } + return 1; +} + + +static int loader_preload (lua_State *L) { + const char *name = luaL_checkstring(L, 1); + lua_getfield(L, LUA_ENVIRONINDEX, "preload"); + if (!lua_istable(L, -1)) + luaL_error(L, LUA_QL("package.preload") " must be a table"); + lua_getfield(L, -1, name); + if (lua_isnil(L, -1)) /* not found? */ + lua_pushfstring(L, "\n\tno field package.preload['%s']", name); + return 1; +} + + +static const int sentinel_ = 0; +#define sentinel ((void *)&sentinel_) + + +static int ll_require (lua_State *L) { + const char *name = luaL_checkstring(L, 1); + int i; + lua_settop(L, 1); /* _LOADED table will be at index 2 */ + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, 2, name); + if (lua_toboolean(L, -1)) { /* is it there? */ + if (lua_touserdata(L, -1) == sentinel) /* check loops */ + luaL_error(L, "loop or previous error loading module " LUA_QS, name); + return 1; /* package is already loaded */ + } + /* else must load it; iterate over available loaders */ + lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); + if (!lua_istable(L, -1)) + luaL_error(L, LUA_QL("package.loaders") " must be a table"); + lua_pushliteral(L, ""); /* error message accumulator */ + for (i=1; ; i++) { + lua_rawgeti(L, -2, i); /* get a loader */ + if (lua_isnil(L, -1)) + luaL_error(L, "module " LUA_QS " not found:%s", + name, lua_tostring(L, -2)); + lua_pushstring(L, name); + lua_call(L, 1, 1); /* call it */ + if (lua_isfunction(L, -1)) /* did it find module? */ + break; /* module loaded successfully */ + else if (lua_isstring(L, -1)) /* loader returned error message? */ + lua_concat(L, 2); /* accumulate it */ + else + lua_pop(L, 1); + } + lua_pushlightuserdata(L, sentinel); + lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ + lua_pushstring(L, name); /* pass name as argument to module */ + lua_call(L, 1, 1); /* run loaded module */ + if (!lua_isnil(L, -1)) /* non-nil return? */ + lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ + lua_getfield(L, 2, name); + if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ + lua_pushboolean(L, 1); /* use true as result */ + lua_pushvalue(L, -1); /* extra copy to be returned */ + lua_setfield(L, 2, name); /* _LOADED[name] = true */ + } + return 1; +} + +/* }====================================================== */ + + + +/* +** {====================================================== +** 'module' function +** ======================================================= +*/ + + +static void setfenv (lua_State *L) { + lua_Debug ar; + if (lua_getstack(L, 1, &ar) == 0 || + lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ + lua_iscfunction(L, -1)) + luaL_error(L, LUA_QL("module") " not called from a Lua function"); + lua_pushvalue(L, -2); + lua_setfenv(L, -2); + lua_pop(L, 1); +} + + +static void dooptions (lua_State *L, int n) { + int i; + for (i = 2; i <= n; i++) { + lua_pushvalue(L, i); /* get option (a function) */ + lua_pushvalue(L, -2); /* module */ + lua_call(L, 1, 0); + } +} + + +static void modinit (lua_State *L, const char *modname) { + const char *dot; + lua_pushvalue(L, -1); + lua_setfield(L, -2, "_M"); /* module._M = module */ + lua_pushstring(L, modname); + lua_setfield(L, -2, "_NAME"); + dot = strrchr(modname, '.'); /* look for last dot in module name */ + if (dot == NULL) dot = modname; + else dot++; + /* set _PACKAGE as package name (full module name minus last part) */ + lua_pushlstring(L, modname, dot - modname); + lua_setfield(L, -2, "_PACKAGE"); +} + + +static int ll_module (lua_State *L) { + const char *modname = luaL_checkstring(L, 1); + int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ + if (!lua_istable(L, -1)) { /* not found? */ + lua_pop(L, 1); /* remove previous result */ + /* try global variable (and create one if it does not exist) */ + if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) + return luaL_error(L, "name conflict for module " LUA_QS, modname); + lua_pushvalue(L, -1); + lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ + } + /* check whether table already has a _NAME field */ + lua_getfield(L, -1, "_NAME"); + if (!lua_isnil(L, -1)) /* is table an initialized module? */ + lua_pop(L, 1); + else { /* no; initialize it */ + lua_pop(L, 1); + modinit(L, modname); + } + lua_pushvalue(L, -1); + setfenv(L); + dooptions(L, loaded - 1); + return 0; +} + + +static int ll_seeall (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + if (!lua_getmetatable(L, 1)) { + lua_createtable(L, 0, 1); /* create new metatable */ + lua_pushvalue(L, -1); + lua_setmetatable(L, 1); + } + lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_setfield(L, -2, "__index"); /* mt.__index = _G */ + return 0; +} + + +/* }====================================================== */ + + + +/* auxiliary mark (for internal use) */ +#define AUXMARK "\1" + +static void setpath (lua_State *L, const char *fieldname, const char *envname, + const char *def) { + const char *path = getenv(envname); + if (path == NULL) /* no environment variable? */ + lua_pushstring(L, def); /* use default */ + else { + /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ + path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, + LUA_PATHSEP AUXMARK LUA_PATHSEP); + luaL_gsub(L, path, AUXMARK, def); + lua_remove(L, -2); + } + setprogdir(L); + lua_setfield(L, -2, fieldname); +} + + +static const luaL_Reg pk_funcs[] = { + {"loadlib", ll_loadlib}, + {"seeall", ll_seeall}, + {NULL, NULL} +}; + + +static const luaL_Reg ll_funcs[] = { + {"module", ll_module}, + {"require", ll_require}, + {NULL, NULL} +}; + + +static const lua_CFunction loaders[] = + {loader_preload, loader_Lua, loader_C, loader_Croot, NULL}; + + +LUALIB_API int luaopen_package (lua_State *L) { + int i; + /* create new type _LOADLIB */ + luaL_newmetatable(L, "_LOADLIB"); + lua_pushcfunction(L, gctm); + lua_setfield(L, -2, "__gc"); + /* create `package' table */ + luaL_register(L, LUA_LOADLIBNAME, pk_funcs); +#if defined(LUA_COMPAT_LOADLIB) + lua_getfield(L, -1, "loadlib"); + lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); +#endif + lua_pushvalue(L, -1); + lua_replace(L, LUA_ENVIRONINDEX); + /* create `loaders' table */ + lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); + /* fill it with pre-defined loaders */ + for (i=0; loaders[i] != NULL; i++) { + lua_pushcfunction(L, loaders[i]); + lua_rawseti(L, -2, i+1); + } + lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ + setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ + setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */ + /* store config information */ + lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" + LUA_EXECDIR "\n" LUA_IGMARK); + lua_setfield(L, -2, "config"); + /* set field `loaded' */ + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); + lua_setfield(L, -2, "loaded"); + /* set field `preload' */ + lua_newtable(L); + lua_setfield(L, -2, "preload"); + lua_pushvalue(L, LUA_GLOBALSINDEX); + luaL_register(L, NULL, ll_funcs); /* open lib into global table */ + lua_pop(L, 1); + return 1; /* return 'package' table */ +} + diff --git a/external/lua/lua/lobject.c b/external/lua/lua/lobject.c new file mode 100644 index 0000000000..4ff50732a4 --- /dev/null +++ b/external/lua/lua/lobject.c @@ -0,0 +1,214 @@ +/* +** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $ +** Some generic functions over Lua objects +** See Copyright Notice in lua.h +*/ + +#include +#include +#include +#include +#include + +#define lobject_c +#define LUA_CORE + +#include "lua.h" + +#include "ldo.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" +#include "lvm.h" + + + +const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL}; + + +/* +** converts an integer to a "floating point byte", represented as +** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if +** eeeee != 0 and (xxx) otherwise. +*/ +int luaO_int2fb (unsigned int x) { + int e = 0; /* expoent */ + while (x >= 16) { + x = (x+1) >> 1; + e++; + } + if (x < 8) return x; + else return ((e+1) << 3) | (cast_int(x) - 8); +} + + +/* converts back */ +int luaO_fb2int (int x) { + int e = (x >> 3) & 31; + if (e == 0) return x; + else return ((x & 7)+8) << (e - 1); +} + + +int luaO_log2 (unsigned int x) { + static const lu_byte log_2[256] = { + 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, + 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 + }; + int l = -1; + while (x >= 256) { l += 8; x >>= 8; } + return l + log_2[x]; + +} + + +int luaO_rawequalObj (const TValue *t1, const TValue *t2) { + if (ttype(t1) != ttype(t2)) return 0; + else switch (ttype(t1)) { + case LUA_TNIL: + return 1; + case LUA_TNUMBER: + return luai_numeq(nvalue(t1), nvalue(t2)); + case LUA_TBOOLEAN: + return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */ + case LUA_TLIGHTUSERDATA: + return pvalue(t1) == pvalue(t2); + default: + lua_assert(iscollectable(t1)); + return gcvalue(t1) == gcvalue(t2); + } +} + + +int luaO_str2d (const char *s, lua_Number *result) { + char *endptr; + *result = lua_str2number(s, &endptr); + if (endptr == s) return 0; /* conversion failed */ + if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ + *result = cast_num(strtoul(s, &endptr, 16)); + if (*endptr == '\0') return 1; /* most common case */ + while (isspace(cast(unsigned char, *endptr))) endptr++; + if (*endptr != '\0') return 0; /* invalid trailing characters? */ + return 1; +} + + + +static void pushstr (lua_State *L, const char *str) { + setsvalue2s(L, L->top, luaS_new(L, str)); + incr_top(L); +} + + +/* this function handles only `%d', `%c', %f, %p, and `%s' formats */ +const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { + int n = 1; + pushstr(L, ""); + for (;;) { + const char *e = strchr(fmt, '%'); + if (e == NULL) break; + setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); + incr_top(L); + switch (*(e+1)) { + case 's': { + const char *s = va_arg(argp, char *); + if (s == NULL) s = "(null)"; + pushstr(L, s); + break; + } + case 'c': { + char buff[2]; + buff[0] = cast(char, va_arg(argp, int)); + buff[1] = '\0'; + pushstr(L, buff); + break; + } + case 'd': { + setnvalue(L->top, cast_num(va_arg(argp, int))); + incr_top(L); + break; + } + case 'f': { + setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); + incr_top(L); + break; + } + case 'p': { + char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ + sprintf(buff, "%p", va_arg(argp, void *)); + pushstr(L, buff); + break; + } + case '%': { + pushstr(L, "%"); + break; + } + default: { + char buff[3]; + buff[0] = '%'; + buff[1] = *(e+1); + buff[2] = '\0'; + pushstr(L, buff); + break; + } + } + n += 2; + fmt = e+2; + } + pushstr(L, fmt); + luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); + L->top -= n; + return svalue(L->top - 1); +} + + +const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { + const char *msg; + va_list argp; + va_start(argp, fmt); + msg = luaO_pushvfstring(L, fmt, argp); + va_end(argp); + return msg; +} + + +void luaO_chunkid (char *out, const char *source, size_t bufflen) { + if (*source == '=') { + strncpy(out, source+1, bufflen); /* remove first char */ + out[bufflen-1] = '\0'; /* ensures null termination */ + } + else { /* out = "source", or "...source" */ + if (*source == '@') { + size_t l; + source++; /* skip the `@' */ + bufflen -= sizeof(" '...' "); + l = strlen(source); + strcpy(out, ""); + if (l > bufflen) { + source += (l-bufflen); /* get last part of file name */ + strcat(out, "..."); + } + strcat(out, source); + } + else { /* out = [string "string"] */ + size_t len = strcspn(source, "\n\r"); /* stop at first newline */ + bufflen -= sizeof(" [string \"...\"] "); + if (len > bufflen) len = bufflen; + strcpy(out, "[string \""); + if (source[len] != '\0') { /* must truncate? */ + strncat(out, source, len); + strcat(out, "..."); + } + else + strcat(out, source); + strcat(out, "\"]"); + } + } +} diff --git a/external/lua/lua/lobject.h b/external/lua/lua/lobject.h new file mode 100644 index 0000000000..f1e447ef3b --- /dev/null +++ b/external/lua/lua/lobject.h @@ -0,0 +1,381 @@ +/* +** $Id: lobject.h,v 2.20.1.2 2008/08/06 13:29:48 roberto Exp $ +** Type definitions for Lua objects +** See Copyright Notice in lua.h +*/ + + +#ifndef lobject_h +#define lobject_h + + +#include + + +#include "llimits.h" +#include "lua.h" + + +/* tags for values visible from Lua */ +#define LAST_TAG LUA_TTHREAD + +#define NUM_TAGS (LAST_TAG+1) + + +/* +** Extra tags for non-values +*/ +#define LUA_TPROTO (LAST_TAG+1) +#define LUA_TUPVAL (LAST_TAG+2) +#define LUA_TDEADKEY (LAST_TAG+3) + + +/* +** Union of all collectable objects +*/ +typedef union GCObject GCObject; + + +/* +** Common Header for all collectable objects (in macro form, to be +** included in other objects) +*/ +#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked + + +/* +** Common header in struct form +*/ +typedef struct GCheader { + CommonHeader; +} GCheader; + + + + +/* +** Union of all Lua values +*/ +typedef union { + GCObject *gc; + void *p; + lua_Number n; + int b; +} Value; + + +/* +** Tagged Values +*/ + +#define TValuefields Value value; int tt + +typedef struct lua_TValue { + TValuefields; +} TValue; + + +/* Macros to test type */ +#define ttisnil(o) (ttype(o) == LUA_TNIL) +#define ttisnumber(o) (ttype(o) == LUA_TNUMBER) +#define ttisstring(o) (ttype(o) == LUA_TSTRING) +#define ttistable(o) (ttype(o) == LUA_TTABLE) +#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION) +#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) +#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) +#define ttisthread(o) (ttype(o) == LUA_TTHREAD) +#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) + +/* Macros to access values */ +#define ttype(o) ((o)->tt) +#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc) +#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) +#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) +#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts) +#define tsvalue(o) (&rawtsvalue(o)->tsv) +#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u) +#define uvalue(o) (&rawuvalue(o)->uv) +#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl) +#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h) +#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) +#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th) + +#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) + +/* +** for internal debug only +*/ +#define checkconsistency(obj) \ + lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt)) + +#define checkliveness(g,obj) \ + lua_assert(!iscollectable(obj) || \ + ((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc))) + + +/* Macros to set values */ +#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) + +#define setnvalue(obj,x) \ + { TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; } + +#define setpvalue(obj,x) \ + { TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; } + +#define setbvalue(obj,x) \ + { TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; } + +#define setsvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \ + checkliveness(G(L),i_o); } + +#define setuvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \ + checkliveness(G(L),i_o); } + +#define setthvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \ + checkliveness(G(L),i_o); } + +#define setclvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \ + checkliveness(G(L),i_o); } + +#define sethvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \ + checkliveness(G(L),i_o); } + +#define setptvalue(L,obj,x) \ + { TValue *i_o=(obj); \ + i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \ + checkliveness(G(L),i_o); } + + + + +#define setobj(L,obj1,obj2) \ + { const TValue *o2=(obj2); TValue *o1=(obj1); \ + o1->value = o2->value; o1->tt=o2->tt; \ + checkliveness(G(L),o1); } + + +/* +** different types of sets, according to destination +*/ + +/* from stack to (same) stack */ +#define setobjs2s setobj +/* to stack (not from same stack) */ +#define setobj2s setobj +#define setsvalue2s setsvalue +#define sethvalue2s sethvalue +#define setptvalue2s setptvalue +/* from table to same table */ +#define setobjt2t setobj +/* to table */ +#define setobj2t setobj +/* to new object */ +#define setobj2n setobj +#define setsvalue2n setsvalue + +#define setttype(obj, tt) (ttype(obj) = (tt)) + + +#define iscollectable(o) (ttype(o) >= LUA_TSTRING) + + + +typedef TValue *StkId; /* index to stack elements */ + + +/* +** String headers for string table +*/ +typedef union TString { + L_Umaxalign dummy; /* ensures maximum alignment for strings */ + struct { + CommonHeader; + lu_byte reserved; + unsigned int hash; + size_t len; + } tsv; +} TString; + + +#define getstr(ts) cast(const char *, (ts) + 1) +#define svalue(o) getstr(rawtsvalue(o)) + + + +typedef union Udata { + L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ + struct { + CommonHeader; + struct Table *metatable; + struct Table *env; + size_t len; + } uv; +} Udata; + + + + +/* +** Function Prototypes +*/ +typedef struct Proto { + CommonHeader; + TValue *k; /* constants used by the function */ + Instruction *code; + struct Proto **p; /* functions defined inside the function */ + int *lineinfo; /* map from opcodes to source lines */ + struct LocVar *locvars; /* information about local variables */ + TString **upvalues; /* upvalue names */ + TString *source; + int sizeupvalues; + int sizek; /* size of `k' */ + int sizecode; + int sizelineinfo; + int sizep; /* size of `p' */ + int sizelocvars; + int linedefined; + int lastlinedefined; + GCObject *gclist; + lu_byte nups; /* number of upvalues */ + lu_byte numparams; + lu_byte is_vararg; + lu_byte maxstacksize; +} Proto; + + +/* masks for new-style vararg */ +#define VARARG_HASARG 1 +#define VARARG_ISVARARG 2 +#define VARARG_NEEDSARG 4 + + +typedef struct LocVar { + TString *varname; + int startpc; /* first point where variable is active */ + int endpc; /* first point where variable is dead */ +} LocVar; + + + +/* +** Upvalues +*/ + +typedef struct UpVal { + CommonHeader; + TValue *v; /* points to stack or to its own value */ + union { + TValue value; /* the value (when closed) */ + struct { /* double linked list (when open) */ + struct UpVal *prev; + struct UpVal *next; + } l; + } u; +} UpVal; + + +/* +** Closures +*/ + +#define ClosureHeader \ + CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \ + struct Table *env + +typedef struct CClosure { + ClosureHeader; + lua_CFunction f; + TValue upvalue[1]; +} CClosure; + + +typedef struct LClosure { + ClosureHeader; + struct Proto *p; + UpVal *upvals[1]; +} LClosure; + + +typedef union Closure { + CClosure c; + LClosure l; +} Closure; + + +#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC) +#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC) + + +/* +** Tables +*/ + +typedef union TKey { + struct { + TValuefields; + struct Node *next; /* for chaining */ + } nk; + TValue tvk; +} TKey; + + +typedef struct Node { + TValue i_val; + TKey i_key; +} Node; + + +typedef struct Table { + CommonHeader; + lu_byte flags; /* 1<

lsizenode)) + + +#define luaO_nilobject (&luaO_nilobject_) + +LUAI_DATA const TValue luaO_nilobject_; + +#define ceillog2(x) (luaO_log2((x)-1) + 1) + +LUAI_FUNC int luaO_log2 (unsigned int x); +LUAI_FUNC int luaO_int2fb (unsigned int x); +LUAI_FUNC int luaO_fb2int (int x); +LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2); +LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result); +LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, + va_list argp); +LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...); +LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len); + + +#endif + diff --git a/external/lua/lua/lopcodes.c b/external/lua/lua/lopcodes.c new file mode 100644 index 0000000000..4cc745230b --- /dev/null +++ b/external/lua/lua/lopcodes.c @@ -0,0 +1,102 @@ +/* +** $Id: lopcodes.c,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ +** See Copyright Notice in lua.h +*/ + + +#define lopcodes_c +#define LUA_CORE + + +#include "lopcodes.h" + + +/* ORDER OP */ + +const char *const luaP_opnames[NUM_OPCODES+1] = { + "MOVE", + "LOADK", + "LOADBOOL", + "LOADNIL", + "GETUPVAL", + "GETGLOBAL", + "GETTABLE", + "SETGLOBAL", + "SETUPVAL", + "SETTABLE", + "NEWTABLE", + "SELF", + "ADD", + "SUB", + "MUL", + "DIV", + "MOD", + "POW", + "UNM", + "NOT", + "LEN", + "CONCAT", + "JMP", + "EQ", + "LT", + "LE", + "TEST", + "TESTSET", + "CALL", + "TAILCALL", + "RETURN", + "FORLOOP", + "FORPREP", + "TFORLOOP", + "SETLIST", + "CLOSE", + "CLOSURE", + "VARARG", + NULL +}; + + +#define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) + +const lu_byte luaP_opmodes[NUM_OPCODES] = { +/* T A B C mode opcode */ + opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ + ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */ + ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ + ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_GETGLOBAL */ + ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ + ,opmode(0, 0, OpArgK, OpArgN, iABx) /* OP_SETGLOBAL */ + ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ + ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ + ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ + ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ + ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ + ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ + ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ + ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ + ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ + ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ + ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */ + ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ + ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ + ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ + ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ + ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ + ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TFORLOOP */ + ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ + ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */ + ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ + ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ +}; + diff --git a/external/lua/lua/lopcodes.h b/external/lua/lua/lopcodes.h new file mode 100644 index 0000000000..41224d6ee1 --- /dev/null +++ b/external/lua/lua/lopcodes.h @@ -0,0 +1,268 @@ +/* +** $Id: lopcodes.h,v 1.125.1.1 2007/12/27 13:02:25 roberto Exp $ +** Opcodes for Lua virtual machine +** See Copyright Notice in lua.h +*/ + +#ifndef lopcodes_h +#define lopcodes_h + +#include "llimits.h" + + +/*=========================================================================== + We assume that instructions are unsigned numbers. + All instructions have an opcode in the first 6 bits. + Instructions can have the following fields: + `A' : 8 bits + `B' : 9 bits + `C' : 9 bits + `Bx' : 18 bits (`B' and `C' together) + `sBx' : signed Bx + + A signed argument is represented in excess K; that is, the number + value is the unsigned value minus K. K is exactly the maximum value + for that argument (so that -max is represented by 0, and +max is + represented by 2*max), which is half the maximum for the corresponding + unsigned argument. +===========================================================================*/ + + +enum OpMode {iABC, iABx, iAsBx}; /* basic instruction format */ + + +/* +** size and position of opcode arguments. +*/ +#define SIZE_C 9 +#define SIZE_B 9 +#define SIZE_Bx (SIZE_C + SIZE_B) +#define SIZE_A 8 + +#define SIZE_OP 6 + +#define POS_OP 0 +#define POS_A (POS_OP + SIZE_OP) +#define POS_C (POS_A + SIZE_A) +#define POS_B (POS_C + SIZE_C) +#define POS_Bx POS_C + + +/* +** limits for opcode arguments. +** we use (signed) int to manipulate most arguments, +** so they must fit in LUAI_BITSINT-1 bits (-1 for sign) +*/ +#if SIZE_Bx < LUAI_BITSINT-1 +#define MAXARG_Bx ((1<>1) /* `sBx' is signed */ +#else +#define MAXARG_Bx MAX_INT +#define MAXARG_sBx MAX_INT +#endif + + +#define MAXARG_A ((1<>POS_OP) & MASK1(SIZE_OP,0))) +#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \ + ((cast(Instruction, o)<>POS_A) & MASK1(SIZE_A,0))) +#define SETARG_A(i,u) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \ + ((cast(Instruction, u)<>POS_B) & MASK1(SIZE_B,0))) +#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \ + ((cast(Instruction, b)<>POS_C) & MASK1(SIZE_C,0))) +#define SETARG_C(i,b) ((i) = (((i)&MASK0(SIZE_C,POS_C)) | \ + ((cast(Instruction, b)<>POS_Bx) & MASK1(SIZE_Bx,0))) +#define SETARG_Bx(i,b) ((i) = (((i)&MASK0(SIZE_Bx,POS_Bx)) | \ + ((cast(Instruction, b)< C) then pc++ */ +OP_TESTSET,/* A B C if (R(B) <=> C) then R(A) := R(B) else pc++ */ + +OP_CALL,/* A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1)) */ +OP_TAILCALL,/* A B C return R(A)(R(A+1), ... ,R(A+B-1)) */ +OP_RETURN,/* A B return R(A), ... ,R(A+B-2) (see note) */ + +OP_FORLOOP,/* A sBx R(A)+=R(A+2); + if R(A) =) R(A)*/ +OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ + +OP_VARARG/* A B R(A), R(A+1), ..., R(A+B-1) = vararg */ +} OpCode; + + +#define NUM_OPCODES (cast(int, OP_VARARG) + 1) + + + +/*=========================================================================== + Notes: + (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1, + and can be 0: OP_CALL then sets `top' to last_result+1, so + next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use `top'. + + (*) In OP_VARARG, if (B == 0) then use actual number of varargs and + set top (like in OP_CALL with C == 0). + + (*) In OP_RETURN, if (B == 0) then return up to `top' + + (*) In OP_SETLIST, if (B == 0) then B = `top'; + if (C == 0) then next `instruction' is real C + + (*) For comparisons, A specifies what condition the test should accept + (true or false). + + (*) All `skips' (pc++) assume that next instruction is a jump +===========================================================================*/ + + +/* +** masks for instruction properties. The format is: +** bits 0-1: op mode +** bits 2-3: C arg mode +** bits 4-5: B arg mode +** bit 6: instruction set register A +** bit 7: operator is a test +*/ + +enum OpArgMask { + OpArgN, /* argument is not used */ + OpArgU, /* argument is used */ + OpArgR, /* argument is a register or a jump offset */ + OpArgK /* argument is a constant or register/constant */ +}; + +LUAI_DATA const lu_byte luaP_opmodes[NUM_OPCODES]; + +#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 3)) +#define getBMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 4) & 3)) +#define getCMode(m) (cast(enum OpArgMask, (luaP_opmodes[m] >> 2) & 3)) +#define testAMode(m) (luaP_opmodes[m] & (1 << 6)) +#define testTMode(m) (luaP_opmodes[m] & (1 << 7)) + + +LUAI_DATA const char *const luaP_opnames[NUM_OPCODES+1]; /* opcode names */ + + +/* number of list items to accumulate before a SETLIST instruction */ +#define LFIELDS_PER_FLUSH 50 + + +#endif diff --git a/external/lua/lua/loslib.c b/external/lua/lua/loslib.c new file mode 100644 index 0000000000..3443907973 --- /dev/null +++ b/external/lua/lua/loslib.c @@ -0,0 +1,247 @@ +/* +** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ +** Standard Operating System library +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include +#include + +#define loslib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +static int os_pushresult (lua_State *L, int i, const char *filename) { + int en = errno; /* calls to Lua API may change this value */ + if (i) { + lua_pushboolean(L, 1); + return 1; + } + else { + lua_pushnil(L); + lua_pushfstring(L, "%s: %s", filename, strerror(en)); + lua_pushinteger(L, en); + return 3; + } +} + + +#ifndef __native_client__ +static int os_execute (lua_State *L) { + lua_pushinteger(L, system(luaL_optstring(L, 1, NULL))); + return 1; +} +#endif + + +static int os_remove (lua_State *L) { + const char *filename = luaL_checkstring(L, 1); + return os_pushresult(L, remove(filename) == 0, filename); +} + + +static int os_rename (lua_State *L) { + const char *fromname = luaL_checkstring(L, 1); + const char *toname = luaL_checkstring(L, 2); + return os_pushresult(L, rename(fromname, toname) == 0, fromname); +} + + +static int os_tmpname (lua_State *L) { + char buff[LUA_TMPNAMBUFSIZE]; + int err; + lua_tmpnam(buff, err); + if (err) + return luaL_error(L, "unable to generate a unique filename"); + lua_pushstring(L, buff); + return 1; +} + + +static int os_getenv (lua_State *L) { + lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ + return 1; +} + + +static int os_clock (lua_State *L) { + lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC); + return 1; +} + + +/* +** {====================================================== +** Time/Date operations +** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, +** wday=%w+1, yday=%j, isdst=? } +** ======================================================= +*/ + +static void setfield (lua_State *L, const char *key, int value) { + lua_pushinteger(L, value); + lua_setfield(L, -2, key); +} + +static void setboolfield (lua_State *L, const char *key, int value) { + if (value < 0) /* undefined? */ + return; /* does not set field */ + lua_pushboolean(L, value); + lua_setfield(L, -2, key); +} + +static int getboolfield (lua_State *L, const char *key) { + int res; + lua_getfield(L, -1, key); + res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); + lua_pop(L, 1); + return res; +} + + +static int getfield (lua_State *L, const char *key, int d) { + int res; + lua_getfield(L, -1, key); + if (lua_isnumber(L, -1)) + res = (int)lua_tointeger(L, -1); + else { + if (d < 0) + return luaL_error(L, "field " LUA_QS " missing in date table", key); + res = d; + } + lua_pop(L, 1); + return res; +} + + +static int os_date (lua_State *L) { + const char *s = luaL_optstring(L, 1, "%c"); + time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); + struct tm *stm; + if (*s == '!') { /* UTC? */ + stm = gmtime(&t); + s++; /* skip `!' */ + } + else + stm = localtime(&t); + if (stm == NULL) /* invalid date? */ + lua_pushnil(L); + else if (strcmp(s, "*t") == 0) { + lua_createtable(L, 0, 9); /* 9 = number of fields */ + setfield(L, "sec", stm->tm_sec); + setfield(L, "min", stm->tm_min); + setfield(L, "hour", stm->tm_hour); + setfield(L, "day", stm->tm_mday); + setfield(L, "month", stm->tm_mon+1); + setfield(L, "year", stm->tm_year+1900); + setfield(L, "wday", stm->tm_wday+1); + setfield(L, "yday", stm->tm_yday+1); + setboolfield(L, "isdst", stm->tm_isdst); + } + else { + char cc[3]; + luaL_Buffer b; + cc[0] = '%'; cc[2] = '\0'; + luaL_buffinit(L, &b); + for (; *s; s++) { + if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ + luaL_addchar(&b, *s); + else { + size_t reslen; + char buff[200]; /* should be big enough for any conversion result */ + cc[1] = *(++s); + reslen = strftime(buff, sizeof(buff), cc, stm); + luaL_addlstring(&b, buff, reslen); + } + } + luaL_pushresult(&b); + } + return 1; +} + + +static int os_time (lua_State *L) { + time_t t; + if (lua_isnoneornil(L, 1)) /* called without args? */ + t = time(NULL); /* get current time */ + else { + struct tm ts; + luaL_checktype(L, 1, LUA_TTABLE); + lua_settop(L, 1); /* make sure table is at the top */ + ts.tm_sec = getfield(L, "sec", 0); + ts.tm_min = getfield(L, "min", 0); + ts.tm_hour = getfield(L, "hour", 12); + ts.tm_mday = getfield(L, "day", -1); + ts.tm_mon = getfield(L, "month", -1) - 1; + ts.tm_year = getfield(L, "year", -1) - 1900; + ts.tm_isdst = getboolfield(L, "isdst"); + t = mktime(&ts); + } + if (t == (time_t)(-1)) + lua_pushnil(L); + else + lua_pushnumber(L, (lua_Number)t); + return 1; +} + + +static int os_difftime (lua_State *L) { + lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), + (time_t)(luaL_optnumber(L, 2, 0)))); + return 1; +} + +/* }====================================================== */ + + +static int os_setlocale (lua_State *L) { + static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, + LC_NUMERIC, LC_TIME}; + static const char *const catnames[] = {"all", "collate", "ctype", "monetary", + "numeric", "time", NULL}; + const char *l = luaL_optstring(L, 1, NULL); + int op = luaL_checkoption(L, 2, "all", catnames); + lua_pushstring(L, setlocale(cat[op], l)); + return 1; +} + + +static int os_exit (lua_State *L) { + exit(luaL_optint(L, 1, EXIT_SUCCESS)); +} + +static const luaL_Reg syslib[] = { + {"clock", os_clock}, + {"date", os_date}, + {"difftime", os_difftime}, +#ifndef __native_client__ + {"execute", os_execute}, +#endif + {"exit", os_exit}, + {"getenv", os_getenv}, + {"remove", os_remove}, + {"rename", os_rename}, + {"setlocale", os_setlocale}, + {"time", os_time}, + {"tmpname", os_tmpname}, + {NULL, NULL} +}; + +/* }====================================================== */ + + + +LUALIB_API int luaopen_os (lua_State *L) { + luaL_register(L, LUA_OSLIBNAME, syslib); + return 1; +} + diff --git a/external/lua/lua/lparser.c b/external/lua/lua/lparser.c new file mode 100644 index 0000000000..dda7488dca --- /dev/null +++ b/external/lua/lua/lparser.c @@ -0,0 +1,1339 @@ +/* +** $Id: lparser.c,v 2.42.1.4 2011/10/21 19:31:42 roberto Exp $ +** Lua Parser +** See Copyright Notice in lua.h +*/ + + +#include + +#define lparser_c +#define LUA_CORE + +#include "lua.h" + +#include "lcode.h" +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "llex.h" +#include "lmem.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lparser.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" + + + +#define hasmultret(k) ((k) == VCALL || (k) == VVARARG) + +#define getlocvar(fs, i) ((fs)->f->locvars[(fs)->actvar[i]]) + +#define luaY_checklimit(fs,v,l,m) if ((v)>(l)) errorlimit(fs,l,m) + + +/* +** nodes for block list (list of active blocks) +*/ +typedef struct BlockCnt { + struct BlockCnt *previous; /* chain */ + int breaklist; /* list of jumps out of this loop */ + lu_byte nactvar; /* # active locals outside the breakable structure */ + lu_byte upval; /* true if some variable in the block is an upvalue */ + lu_byte isbreakable; /* true if `block' is a loop */ +} BlockCnt; + + + +/* +** prototypes for recursive non-terminal functions +*/ +static void chunk (LexState *ls); +static void expr (LexState *ls, expdesc *v); + + +static void anchor_token (LexState *ls) { + if (ls->t.token == TK_NAME || ls->t.token == TK_STRING) { + TString *ts = ls->t.seminfo.ts; + luaX_newstring(ls, getstr(ts), ts->tsv.len); + } +} + + +static void error_expected (LexState *ls, int token) { + luaX_syntaxerror(ls, + luaO_pushfstring(ls->L, LUA_QS " expected", luaX_token2str(ls, token))); +} + + +static void errorlimit (FuncState *fs, int limit, const char *what) { + const char *msg = (fs->f->linedefined == 0) ? + luaO_pushfstring(fs->L, "main function has more than %d %s", limit, what) : + luaO_pushfstring(fs->L, "function at line %d has more than %d %s", + fs->f->linedefined, limit, what); + luaX_lexerror(fs->ls, msg, 0); +} + + +static int testnext (LexState *ls, int c) { + if (ls->t.token == c) { + luaX_next(ls); + return 1; + } + else return 0; +} + + +static void check (LexState *ls, int c) { + if (ls->t.token != c) + error_expected(ls, c); +} + +static void checknext (LexState *ls, int c) { + check(ls, c); + luaX_next(ls); +} + + +#define check_condition(ls,c,msg) { if (!(c)) luaX_syntaxerror(ls, msg); } + + + +static void check_match (LexState *ls, int what, int who, int where) { + if (!testnext(ls, what)) { + if (where == ls->linenumber) + error_expected(ls, what); + else { + luaX_syntaxerror(ls, luaO_pushfstring(ls->L, + LUA_QS " expected (to close " LUA_QS " at line %d)", + luaX_token2str(ls, what), luaX_token2str(ls, who), where)); + } + } +} + + +static TString *str_checkname (LexState *ls) { + TString *ts; + check(ls, TK_NAME); + ts = ls->t.seminfo.ts; + luaX_next(ls); + return ts; +} + + +static void init_exp (expdesc *e, expkind k, int i) { + e->f = e->t = NO_JUMP; + e->k = k; + e->u.s.info = i; +} + + +static void codestring (LexState *ls, expdesc *e, TString *s) { + init_exp(e, VK, luaK_stringK(ls->fs, s)); +} + + +static void checkname(LexState *ls, expdesc *e) { + codestring(ls, e, str_checkname(ls)); +} + + +static int registerlocalvar (LexState *ls, TString *varname) { + FuncState *fs = ls->fs; + Proto *f = fs->f; + int oldsize = f->sizelocvars; + luaM_growvector(ls->L, f->locvars, fs->nlocvars, f->sizelocvars, + LocVar, SHRT_MAX, "too many local variables"); + while (oldsize < f->sizelocvars) f->locvars[oldsize++].varname = NULL; + f->locvars[fs->nlocvars].varname = varname; + luaC_objbarrier(ls->L, f, varname); + return fs->nlocvars++; +} + + +#define new_localvarliteral(ls,v,n) \ + new_localvar(ls, luaX_newstring(ls, "" v, (sizeof(v)/sizeof(char))-1), n) + + +static void new_localvar (LexState *ls, TString *name, int n) { + FuncState *fs = ls->fs; + luaY_checklimit(fs, fs->nactvar+n+1, LUAI_MAXVARS, "local variables"); + fs->actvar[fs->nactvar+n] = cast(unsigned short, registerlocalvar(ls, name)); +} + + +static void adjustlocalvars (LexState *ls, int nvars) { + FuncState *fs = ls->fs; + fs->nactvar = cast_byte(fs->nactvar + nvars); + for (; nvars; nvars--) { + getlocvar(fs, fs->nactvar - nvars).startpc = fs->pc; + } +} + + +static void removevars (LexState *ls, int tolevel) { + FuncState *fs = ls->fs; + while (fs->nactvar > tolevel) + getlocvar(fs, --fs->nactvar).endpc = fs->pc; +} + + +static int indexupvalue (FuncState *fs, TString *name, expdesc *v) { + int i; + Proto *f = fs->f; + int oldsize = f->sizeupvalues; + for (i=0; inups; i++) { + if (fs->upvalues[i].k == v->k && fs->upvalues[i].info == v->u.s.info) { + lua_assert(f->upvalues[i] == name); + return i; + } + } + /* new one */ + luaY_checklimit(fs, f->nups + 1, LUAI_MAXUPVALUES, "upvalues"); + luaM_growvector(fs->L, f->upvalues, f->nups, f->sizeupvalues, + TString *, MAX_INT, ""); + while (oldsize < f->sizeupvalues) f->upvalues[oldsize++] = NULL; + f->upvalues[f->nups] = name; + luaC_objbarrier(fs->L, f, name); + lua_assert(v->k == VLOCAL || v->k == VUPVAL); + fs->upvalues[f->nups].k = cast_byte(v->k); + fs->upvalues[f->nups].info = cast_byte(v->u.s.info); + return f->nups++; +} + + +static int searchvar (FuncState *fs, TString *n) { + int i; + for (i=fs->nactvar-1; i >= 0; i--) { + if (n == getlocvar(fs, i).varname) + return i; + } + return -1; /* not found */ +} + + +static void markupval (FuncState *fs, int level) { + BlockCnt *bl = fs->bl; + while (bl && bl->nactvar > level) bl = bl->previous; + if (bl) bl->upval = 1; +} + + +static int singlevaraux (FuncState *fs, TString *n, expdesc *var, int base) { + if (fs == NULL) { /* no more levels? */ + init_exp(var, VGLOBAL, NO_REG); /* default is global variable */ + return VGLOBAL; + } + else { + int v = searchvar(fs, n); /* look up at current level */ + if (v >= 0) { + init_exp(var, VLOCAL, v); + if (!base) + markupval(fs, v); /* local will be used as an upval */ + return VLOCAL; + } + else { /* not found at current level; try upper one */ + if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL) + return VGLOBAL; + var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */ + var->k = VUPVAL; /* upvalue in this level */ + return VUPVAL; + } + } +} + + +static void singlevar (LexState *ls, expdesc *var) { + TString *varname = str_checkname(ls); + FuncState *fs = ls->fs; + if (singlevaraux(fs, varname, var, 1) == VGLOBAL) + var->u.s.info = luaK_stringK(fs, varname); /* info points to global name */ +} + + +static void adjust_assign (LexState *ls, int nvars, int nexps, expdesc *e) { + FuncState *fs = ls->fs; + int extra = nvars - nexps; + if (hasmultret(e->k)) { + extra++; /* includes call itself */ + if (extra < 0) extra = 0; + luaK_setreturns(fs, e, extra); /* last exp. provides the difference */ + if (extra > 1) luaK_reserveregs(fs, extra-1); + } + else { + if (e->k != VVOID) luaK_exp2nextreg(fs, e); /* close last expression */ + if (extra > 0) { + int reg = fs->freereg; + luaK_reserveregs(fs, extra); + luaK_nil(fs, reg, extra); + } + } +} + + +static void enterlevel (LexState *ls) { + if (++ls->L->nCcalls > LUAI_MAXCCALLS) + luaX_lexerror(ls, "chunk has too many syntax levels", 0); +} + + +#define leavelevel(ls) ((ls)->L->nCcalls--) + + +static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { + bl->breaklist = NO_JUMP; + bl->isbreakable = isbreakable; + bl->nactvar = fs->nactvar; + bl->upval = 0; + bl->previous = fs->bl; + fs->bl = bl; + lua_assert(fs->freereg == fs->nactvar); +} + + +static void leaveblock (FuncState *fs) { + BlockCnt *bl = fs->bl; + fs->bl = bl->previous; + removevars(fs->ls, bl->nactvar); + if (bl->upval) + luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); + /* a block either controls scope or breaks (never both) */ + lua_assert(!bl->isbreakable || !bl->upval); + lua_assert(bl->nactvar == fs->nactvar); + fs->freereg = fs->nactvar; /* free registers */ + luaK_patchtohere(fs, bl->breaklist); +} + + +static void pushclosure (LexState *ls, FuncState *func, expdesc *v) { + FuncState *fs = ls->fs; + Proto *f = fs->f; + int oldsize = f->sizep; + int i; + luaM_growvector(ls->L, f->p, fs->np, f->sizep, Proto *, + MAXARG_Bx, "constant table overflow"); + while (oldsize < f->sizep) f->p[oldsize++] = NULL; + f->p[fs->np++] = func->f; + luaC_objbarrier(ls->L, f, func->f); + init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1)); + for (i=0; if->nups; i++) { + OpCode o = (func->upvalues[i].k == VLOCAL) ? OP_MOVE : OP_GETUPVAL; + luaK_codeABC(fs, o, 0, func->upvalues[i].info, 0); + } +} + + +static void open_func (LexState *ls, FuncState *fs) { + lua_State *L = ls->L; + Proto *f = luaF_newproto(L); + fs->f = f; + fs->prev = ls->fs; /* linked list of funcstates */ + fs->ls = ls; + fs->L = L; + ls->fs = fs; + fs->pc = 0; + fs->lasttarget = -1; + fs->jpc = NO_JUMP; + fs->freereg = 0; + fs->nk = 0; + fs->np = 0; + fs->nlocvars = 0; + fs->nactvar = 0; + fs->bl = NULL; + f->source = ls->source; + f->maxstacksize = 2; /* registers 0/1 are always valid */ + fs->h = luaH_new(L, 0, 0); + /* anchor table of constants and prototype (to avoid being collected) */ + sethvalue2s(L, L->top, fs->h); + incr_top(L); + setptvalue2s(L, L->top, f); + incr_top(L); +} + + +static void close_func (LexState *ls) { + lua_State *L = ls->L; + FuncState *fs = ls->fs; + Proto *f = fs->f; + removevars(ls, 0); + luaK_ret(fs, 0, 0); /* final return */ + luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); + f->sizecode = fs->pc; + luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int); + f->sizelineinfo = fs->pc; + luaM_reallocvector(L, f->k, f->sizek, fs->nk, TValue); + f->sizek = fs->nk; + luaM_reallocvector(L, f->p, f->sizep, fs->np, Proto *); + f->sizep = fs->np; + luaM_reallocvector(L, f->locvars, f->sizelocvars, fs->nlocvars, LocVar); + f->sizelocvars = fs->nlocvars; + luaM_reallocvector(L, f->upvalues, f->sizeupvalues, f->nups, TString *); + f->sizeupvalues = f->nups; + lua_assert(luaG_checkcode(f)); + lua_assert(fs->bl == NULL); + ls->fs = fs->prev; + /* last token read was anchored in defunct function; must reanchor it */ + if (fs) anchor_token(ls); + L->top -= 2; /* remove table and prototype from the stack */ +} + + +Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) { + struct LexState lexstate; + struct FuncState funcstate; + lexstate.buff = buff; + luaX_setinput(L, &lexstate, z, luaS_new(L, name)); + open_func(&lexstate, &funcstate); + funcstate.f->is_vararg = VARARG_ISVARARG; /* main func. is always vararg */ + luaX_next(&lexstate); /* read first token */ + chunk(&lexstate); + check(&lexstate, TK_EOS); + close_func(&lexstate); + lua_assert(funcstate.prev == NULL); + lua_assert(funcstate.f->nups == 0); + lua_assert(lexstate.fs == NULL); + return funcstate.f; +} + + + +/*============================================================*/ +/* GRAMMAR RULES */ +/*============================================================*/ + + +static void field (LexState *ls, expdesc *v) { + /* field -> ['.' | ':'] NAME */ + FuncState *fs = ls->fs; + expdesc key; + luaK_exp2anyreg(fs, v); + luaX_next(ls); /* skip the dot or colon */ + checkname(ls, &key); + luaK_indexed(fs, v, &key); +} + + +static void yindex (LexState *ls, expdesc *v) { + /* index -> '[' expr ']' */ + luaX_next(ls); /* skip the '[' */ + expr(ls, v); + luaK_exp2val(ls->fs, v); + checknext(ls, ']'); +} + + +/* +** {====================================================================== +** Rules for Constructors +** ======================================================================= +*/ + + +struct ConsControl { + expdesc v; /* last list item read */ + expdesc *t; /* table descriptor */ + int nh; /* total number of `record' elements */ + int na; /* total number of array elements */ + int tostore; /* number of array elements pending to be stored */ +}; + + +static void recfield (LexState *ls, struct ConsControl *cc) { + /* recfield -> (NAME | `['exp1`]') = exp1 */ + FuncState *fs = ls->fs; + int reg = ls->fs->freereg; + expdesc key, val; + int rkkey; + if (ls->t.token == TK_NAME) { + luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor"); + checkname(ls, &key); + } + else /* ls->t.token == '[' */ + yindex(ls, &key); + cc->nh++; + checknext(ls, '='); + rkkey = luaK_exp2RK(fs, &key); + expr(ls, &val); + luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val)); + fs->freereg = reg; /* free registers */ +} + + +static void closelistfield (FuncState *fs, struct ConsControl *cc) { + if (cc->v.k == VVOID) return; /* there is no list item */ + luaK_exp2nextreg(fs, &cc->v); + cc->v.k = VVOID; + if (cc->tostore == LFIELDS_PER_FLUSH) { + luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); /* flush */ + cc->tostore = 0; /* no more items pending */ + } +} + + +static void lastlistfield (FuncState *fs, struct ConsControl *cc) { + if (cc->tostore == 0) return; + if (hasmultret(cc->v.k)) { + luaK_setmultret(fs, &cc->v); + luaK_setlist(fs, cc->t->u.s.info, cc->na, LUA_MULTRET); + cc->na--; /* do not count last expression (unknown number of elements) */ + } + else { + if (cc->v.k != VVOID) + luaK_exp2nextreg(fs, &cc->v); + luaK_setlist(fs, cc->t->u.s.info, cc->na, cc->tostore); + } +} + + +static void listfield (LexState *ls, struct ConsControl *cc) { + expr(ls, &cc->v); + luaY_checklimit(ls->fs, cc->na, MAX_INT, "items in a constructor"); + cc->na++; + cc->tostore++; +} + + +static void constructor (LexState *ls, expdesc *t) { + /* constructor -> ?? */ + FuncState *fs = ls->fs; + int line = ls->linenumber; + int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0); + struct ConsControl cc; + cc.na = cc.nh = cc.tostore = 0; + cc.t = t; + init_exp(t, VRELOCABLE, pc); + init_exp(&cc.v, VVOID, 0); /* no value (yet) */ + luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */ + checknext(ls, '{'); + do { + lua_assert(cc.v.k == VVOID || cc.tostore > 0); + if (ls->t.token == '}') break; + closelistfield(fs, &cc); + switch(ls->t.token) { + case TK_NAME: { /* may be listfields or recfields */ + luaX_lookahead(ls); + if (ls->lookahead.token != '=') /* expression? */ + listfield(ls, &cc); + else + recfield(ls, &cc); + break; + } + case '[': { /* constructor_item -> recfield */ + recfield(ls, &cc); + break; + } + default: { /* constructor_part -> listfield */ + listfield(ls, &cc); + break; + } + } + } while (testnext(ls, ',') || testnext(ls, ';')); + check_match(ls, '}', '{', line); + lastlistfield(fs, &cc); + SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ + SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh)); /* set initial table size */ +} + +/* }====================================================================== */ + + + +static void parlist (LexState *ls) { + /* parlist -> [ param { `,' param } ] */ + FuncState *fs = ls->fs; + Proto *f = fs->f; + int nparams = 0; + f->is_vararg = 0; + if (ls->t.token != ')') { /* is `parlist' not empty? */ + do { + switch (ls->t.token) { + case TK_NAME: { /* param -> NAME */ + new_localvar(ls, str_checkname(ls), nparams++); + break; + } + case TK_DOTS: { /* param -> `...' */ + luaX_next(ls); +#if defined(LUA_COMPAT_VARARG) + /* use `arg' as default name */ + new_localvarliteral(ls, "arg", nparams++); + f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG; +#endif + f->is_vararg |= VARARG_ISVARARG; + break; + } + default: luaX_syntaxerror(ls, " or " LUA_QL("...") " expected"); + } + } while (!f->is_vararg && testnext(ls, ',')); + } + adjustlocalvars(ls, nparams); + f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); + luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ +} + + +static void body (LexState *ls, expdesc *e, int needself, int line) { + /* body -> `(' parlist `)' chunk END */ + FuncState new_fs; + open_func(ls, &new_fs); + new_fs.f->linedefined = line; + checknext(ls, '('); + if (needself) { + new_localvarliteral(ls, "self", 0); + adjustlocalvars(ls, 1); + } + parlist(ls); + checknext(ls, ')'); + chunk(ls); + new_fs.f->lastlinedefined = ls->linenumber; + check_match(ls, TK_END, TK_FUNCTION, line); + close_func(ls); + pushclosure(ls, &new_fs, e); +} + + +static int explist1 (LexState *ls, expdesc *v) { + /* explist1 -> expr { `,' expr } */ + int n = 1; /* at least one expression */ + expr(ls, v); + while (testnext(ls, ',')) { + luaK_exp2nextreg(ls->fs, v); + expr(ls, v); + n++; + } + return n; +} + + +static void funcargs (LexState *ls, expdesc *f) { + FuncState *fs = ls->fs; + expdesc args; + int base, nparams; + int line = ls->linenumber; + switch (ls->t.token) { + case '(': { /* funcargs -> `(' [ explist1 ] `)' */ + if (line != ls->lastline) + luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)"); + luaX_next(ls); + if (ls->t.token == ')') /* arg list is empty? */ + args.k = VVOID; + else { + explist1(ls, &args); + luaK_setmultret(fs, &args); + } + check_match(ls, ')', '(', line); + break; + } + case '{': { /* funcargs -> constructor */ + constructor(ls, &args); + break; + } + case TK_STRING: { /* funcargs -> STRING */ + codestring(ls, &args, ls->t.seminfo.ts); + luaX_next(ls); /* must use `seminfo' before `next' */ + break; + } + default: { + luaX_syntaxerror(ls, "function arguments expected"); + return; + } + } + lua_assert(f->k == VNONRELOC); + base = f->u.s.info; /* base register for call */ + if (hasmultret(args.k)) + nparams = LUA_MULTRET; /* open call */ + else { + if (args.k != VVOID) + luaK_exp2nextreg(fs, &args); /* close last argument */ + nparams = fs->freereg - (base+1); + } + init_exp(f, VCALL, luaK_codeABC(fs, OP_CALL, base, nparams+1, 2)); + luaK_fixline(fs, line); + fs->freereg = base+1; /* call remove function and arguments and leaves + (unless changed) one result */ +} + + + + +/* +** {====================================================================== +** Expression parsing +** ======================================================================= +*/ + + +static void prefixexp (LexState *ls, expdesc *v) { + /* prefixexp -> NAME | '(' expr ')' */ + switch (ls->t.token) { + case '(': { + int line = ls->linenumber; + luaX_next(ls); + expr(ls, v); + check_match(ls, ')', '(', line); + luaK_dischargevars(ls->fs, v); + return; + } + case TK_NAME: { + singlevar(ls, v); + return; + } + default: { + luaX_syntaxerror(ls, "unexpected symbol"); + return; + } + } +} + + +static void primaryexp (LexState *ls, expdesc *v) { + /* primaryexp -> + prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */ + FuncState *fs = ls->fs; + prefixexp(ls, v); + for (;;) { + switch (ls->t.token) { + case '.': { /* field */ + field(ls, v); + break; + } + case '[': { /* `[' exp1 `]' */ + expdesc key; + luaK_exp2anyreg(fs, v); + yindex(ls, &key); + luaK_indexed(fs, v, &key); + break; + } + case ':': { /* `:' NAME funcargs */ + expdesc key; + luaX_next(ls); + checkname(ls, &key); + luaK_self(fs, v, &key); + funcargs(ls, v); + break; + } + case '(': case TK_STRING: case '{': { /* funcargs */ + luaK_exp2nextreg(fs, v); + funcargs(ls, v); + break; + } + default: return; + } + } +} + + +static void simpleexp (LexState *ls, expdesc *v) { + /* simpleexp -> NUMBER | STRING | NIL | true | false | ... | + constructor | FUNCTION body | primaryexp */ + switch (ls->t.token) { + case TK_NUMBER: { + init_exp(v, VKNUM, 0); + v->u.nval = ls->t.seminfo.r; + break; + } + case TK_STRING: { + codestring(ls, v, ls->t.seminfo.ts); + break; + } + case TK_NIL: { + init_exp(v, VNIL, 0); + break; + } + case TK_TRUE: { + init_exp(v, VTRUE, 0); + break; + } + case TK_FALSE: { + init_exp(v, VFALSE, 0); + break; + } + case TK_DOTS: { /* vararg */ + FuncState *fs = ls->fs; + check_condition(ls, fs->f->is_vararg, + "cannot use " LUA_QL("...") " outside a vararg function"); + fs->f->is_vararg &= ~VARARG_NEEDSARG; /* don't need 'arg' */ + init_exp(v, VVARARG, luaK_codeABC(fs, OP_VARARG, 0, 1, 0)); + break; + } + case '{': { /* constructor */ + constructor(ls, v); + return; + } + case TK_FUNCTION: { + luaX_next(ls); + body(ls, v, 0, ls->linenumber); + return; + } + default: { + primaryexp(ls, v); + return; + } + } + luaX_next(ls); +} + + +static UnOpr getunopr (int op) { + switch (op) { + case TK_NOT: return OPR_NOT; + case '-': return OPR_MINUS; + case '#': return OPR_LEN; + default: return OPR_NOUNOPR; + } +} + + +static BinOpr getbinopr (int op) { + switch (op) { + case '+': return OPR_ADD; + case '-': return OPR_SUB; + case '*': return OPR_MUL; + case '/': return OPR_DIV; + case '%': return OPR_MOD; + case '^': return OPR_POW; + case TK_CONCAT: return OPR_CONCAT; + case TK_NE: return OPR_NE; + case TK_EQ: return OPR_EQ; + case '<': return OPR_LT; + case TK_LE: return OPR_LE; + case '>': return OPR_GT; + case TK_GE: return OPR_GE; + case TK_AND: return OPR_AND; + case TK_OR: return OPR_OR; + default: return OPR_NOBINOPR; + } +} + + +static const struct { + lu_byte left; /* left priority for each binary operator */ + lu_byte right; /* right priority */ +} priority[] = { /* ORDER OPR */ + {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, /* `+' `-' `/' `%' */ + {10, 9}, {5, 4}, /* power and concat (right associative) */ + {3, 3}, {3, 3}, /* equality and inequality */ + {3, 3}, {3, 3}, {3, 3}, {3, 3}, /* order */ + {2, 2}, {1, 1} /* logical (and/or) */ +}; + +#define UNARY_PRIORITY 8 /* priority for unary operators */ + + +/* +** subexpr -> (simpleexp | unop subexpr) { binop subexpr } +** where `binop' is any binary operator with a priority higher than `limit' +*/ +static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) { + BinOpr op; + UnOpr uop; + enterlevel(ls); + uop = getunopr(ls->t.token); + if (uop != OPR_NOUNOPR) { + luaX_next(ls); + subexpr(ls, v, UNARY_PRIORITY); + luaK_prefix(ls->fs, uop, v); + } + else simpleexp(ls, v); + /* expand while operators have priorities higher than `limit' */ + op = getbinopr(ls->t.token); + while (op != OPR_NOBINOPR && priority[op].left > limit) { + expdesc v2; + BinOpr nextop; + luaX_next(ls); + luaK_infix(ls->fs, op, v); + /* read sub-expression with higher priority */ + nextop = subexpr(ls, &v2, priority[op].right); + luaK_posfix(ls->fs, op, v, &v2); + op = nextop; + } + leavelevel(ls); + return op; /* return first untreated operator */ +} + + +static void expr (LexState *ls, expdesc *v) { + subexpr(ls, v, 0); +} + +/* }==================================================================== */ + + + +/* +** {====================================================================== +** Rules for Statements +** ======================================================================= +*/ + + +static int block_follow (int token) { + switch (token) { + case TK_ELSE: case TK_ELSEIF: case TK_END: + case TK_UNTIL: case TK_EOS: + return 1; + default: return 0; + } +} + + +static void block (LexState *ls) { + /* block -> chunk */ + FuncState *fs = ls->fs; + BlockCnt bl; + enterblock(fs, &bl, 0); + chunk(ls); + lua_assert(bl.breaklist == NO_JUMP); + leaveblock(fs); +} + + +/* +** structure to chain all variables in the left-hand side of an +** assignment +*/ +struct LHS_assign { + struct LHS_assign *prev; + expdesc v; /* variable (global, local, upvalue, or indexed) */ +}; + + +/* +** check whether, in an assignment to a local variable, the local variable +** is needed in a previous assignment (to a table). If so, save original +** local value in a safe place and use this safe copy in the previous +** assignment. +*/ +static void check_conflict (LexState *ls, struct LHS_assign *lh, expdesc *v) { + FuncState *fs = ls->fs; + int extra = fs->freereg; /* eventual position to save local variable */ + int conflict = 0; + for (; lh; lh = lh->prev) { + if (lh->v.k == VINDEXED) { + if (lh->v.u.s.info == v->u.s.info) { /* conflict? */ + conflict = 1; + lh->v.u.s.info = extra; /* previous assignment will use safe copy */ + } + if (lh->v.u.s.aux == v->u.s.info) { /* conflict? */ + conflict = 1; + lh->v.u.s.aux = extra; /* previous assignment will use safe copy */ + } + } + } + if (conflict) { + luaK_codeABC(fs, OP_MOVE, fs->freereg, v->u.s.info, 0); /* make copy */ + luaK_reserveregs(fs, 1); + } +} + + +static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { + expdesc e; + check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, + "syntax error"); + if (testnext(ls, ',')) { /* assignment -> `,' primaryexp assignment */ + struct LHS_assign nv; + nv.prev = lh; + primaryexp(ls, &nv.v); + if (nv.v.k == VLOCAL) + check_conflict(ls, lh, &nv.v); + luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, + "variables in assignment"); + assignment(ls, &nv, nvars+1); + } + else { /* assignment -> `=' explist1 */ + int nexps; + checknext(ls, '='); + nexps = explist1(ls, &e); + if (nexps != nvars) { + adjust_assign(ls, nvars, nexps, &e); + if (nexps > nvars) + ls->fs->freereg -= nexps - nvars; /* remove extra values */ + } + else { + luaK_setoneret(ls->fs, &e); /* close last expression */ + luaK_storevar(ls->fs, &lh->v, &e); + return; /* avoid default */ + } + } + init_exp(&e, VNONRELOC, ls->fs->freereg-1); /* default assignment */ + luaK_storevar(ls->fs, &lh->v, &e); +} + + +static int cond (LexState *ls) { + /* cond -> exp */ + expdesc v; + expr(ls, &v); /* read condition */ + if (v.k == VNIL) v.k = VFALSE; /* `falses' are all equal here */ + luaK_goiftrue(ls->fs, &v); + return v.f; +} + + +static void breakstat (LexState *ls) { + FuncState *fs = ls->fs; + BlockCnt *bl = fs->bl; + int upval = 0; + while (bl && !bl->isbreakable) { + upval |= bl->upval; + bl = bl->previous; + } + if (!bl) + luaX_syntaxerror(ls, "no loop to break"); + if (upval) + luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0); + luaK_concat(fs, &bl->breaklist, luaK_jump(fs)); +} + + +static void whilestat (LexState *ls, int line) { + /* whilestat -> WHILE cond DO block END */ + FuncState *fs = ls->fs; + int whileinit; + int condexit; + BlockCnt bl; + luaX_next(ls); /* skip WHILE */ + whileinit = luaK_getlabel(fs); + condexit = cond(ls); + enterblock(fs, &bl, 1); + checknext(ls, TK_DO); + block(ls); + luaK_patchlist(fs, luaK_jump(fs), whileinit); + check_match(ls, TK_END, TK_WHILE, line); + leaveblock(fs); + luaK_patchtohere(fs, condexit); /* false conditions finish the loop */ +} + + +static void repeatstat (LexState *ls, int line) { + /* repeatstat -> REPEAT block UNTIL cond */ + int condexit; + FuncState *fs = ls->fs; + int repeat_init = luaK_getlabel(fs); + BlockCnt bl1, bl2; + enterblock(fs, &bl1, 1); /* loop block */ + enterblock(fs, &bl2, 0); /* scope block */ + luaX_next(ls); /* skip REPEAT */ + chunk(ls); + check_match(ls, TK_UNTIL, TK_REPEAT, line); + condexit = cond(ls); /* read condition (inside scope block) */ + if (!bl2.upval) { /* no upvalues? */ + leaveblock(fs); /* finish scope */ + luaK_patchlist(ls->fs, condexit, repeat_init); /* close the loop */ + } + else { /* complete semantics when there are upvalues */ + breakstat(ls); /* if condition then break */ + luaK_patchtohere(ls->fs, condexit); /* else... */ + leaveblock(fs); /* finish scope... */ + luaK_patchlist(ls->fs, luaK_jump(fs), repeat_init); /* and repeat */ + } + leaveblock(fs); /* finish loop */ +} + + +static int exp1 (LexState *ls) { + expdesc e; + int k; + expr(ls, &e); + k = e.k; + luaK_exp2nextreg(ls->fs, &e); + return k; +} + + +static void forbody (LexState *ls, int base, int line, int nvars, int isnum) { + /* forbody -> DO block */ + BlockCnt bl; + FuncState *fs = ls->fs; + int prep, endfor; + adjustlocalvars(ls, 3); /* control variables */ + checknext(ls, TK_DO); + prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs); + enterblock(fs, &bl, 0); /* scope for declared variables */ + adjustlocalvars(ls, nvars); + luaK_reserveregs(fs, nvars); + block(ls); + leaveblock(fs); /* end of scope for declared variables */ + luaK_patchtohere(fs, prep); + endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) : + luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars); + luaK_fixline(fs, line); /* pretend that `OP_FOR' starts the loop */ + luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1); +} + + +static void fornum (LexState *ls, TString *varname, int line) { + /* fornum -> NAME = exp1,exp1[,exp1] forbody */ + FuncState *fs = ls->fs; + int base = fs->freereg; + new_localvarliteral(ls, "(for index)", 0); + new_localvarliteral(ls, "(for limit)", 1); + new_localvarliteral(ls, "(for step)", 2); + new_localvar(ls, varname, 3); + checknext(ls, '='); + exp1(ls); /* initial value */ + checknext(ls, ','); + exp1(ls); /* limit */ + if (testnext(ls, ',')) + exp1(ls); /* optional step */ + else { /* default step = 1 */ + luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1)); + luaK_reserveregs(fs, 1); + } + forbody(ls, base, line, 1, 1); +} + + +static void forlist (LexState *ls, TString *indexname) { + /* forlist -> NAME {,NAME} IN explist1 forbody */ + FuncState *fs = ls->fs; + expdesc e; + int nvars = 0; + int line; + int base = fs->freereg; + /* create control variables */ + new_localvarliteral(ls, "(for generator)", nvars++); + new_localvarliteral(ls, "(for state)", nvars++); + new_localvarliteral(ls, "(for control)", nvars++); + /* create declared variables */ + new_localvar(ls, indexname, nvars++); + while (testnext(ls, ',')) + new_localvar(ls, str_checkname(ls), nvars++); + checknext(ls, TK_IN); + line = ls->linenumber; + adjust_assign(ls, 3, explist1(ls, &e), &e); + luaK_checkstack(fs, 3); /* extra space to call generator */ + forbody(ls, base, line, nvars - 3, 0); +} + + +static void forstat (LexState *ls, int line) { + /* forstat -> FOR (fornum | forlist) END */ + FuncState *fs = ls->fs; + TString *varname; + BlockCnt bl; + enterblock(fs, &bl, 1); /* scope for loop and control variables */ + luaX_next(ls); /* skip `for' */ + varname = str_checkname(ls); /* first variable name */ + switch (ls->t.token) { + case '=': fornum(ls, varname, line); break; + case ',': case TK_IN: forlist(ls, varname); break; + default: luaX_syntaxerror(ls, LUA_QL("=") " or " LUA_QL("in") " expected"); + } + check_match(ls, TK_END, TK_FOR, line); + leaveblock(fs); /* loop scope (`break' jumps to this point) */ +} + + +static int test_then_block (LexState *ls) { + /* test_then_block -> [IF | ELSEIF] cond THEN block */ + int condexit; + luaX_next(ls); /* skip IF or ELSEIF */ + condexit = cond(ls); + checknext(ls, TK_THEN); + block(ls); /* `then' part */ + return condexit; +} + + +static void ifstat (LexState *ls, int line) { + /* ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END */ + FuncState *fs = ls->fs; + int flist; + int escapelist = NO_JUMP; + flist = test_then_block(ls); /* IF cond THEN block */ + while (ls->t.token == TK_ELSEIF) { + luaK_concat(fs, &escapelist, luaK_jump(fs)); + luaK_patchtohere(fs, flist); + flist = test_then_block(ls); /* ELSEIF cond THEN block */ + } + if (ls->t.token == TK_ELSE) { + luaK_concat(fs, &escapelist, luaK_jump(fs)); + luaK_patchtohere(fs, flist); + luaX_next(ls); /* skip ELSE (after patch, for correct line info) */ + block(ls); /* `else' part */ + } + else + luaK_concat(fs, &escapelist, flist); + luaK_patchtohere(fs, escapelist); + check_match(ls, TK_END, TK_IF, line); +} + + +static void localfunc (LexState *ls) { + expdesc v, b; + FuncState *fs = ls->fs; + new_localvar(ls, str_checkname(ls), 0); + init_exp(&v, VLOCAL, fs->freereg); + luaK_reserveregs(fs, 1); + adjustlocalvars(ls, 1); + body(ls, &b, 0, ls->linenumber); + luaK_storevar(fs, &v, &b); + /* debug information will only see the variable after this point! */ + getlocvar(fs, fs->nactvar - 1).startpc = fs->pc; +} + + +static void localstat (LexState *ls) { + /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */ + int nvars = 0; + int nexps; + expdesc e; + do { + new_localvar(ls, str_checkname(ls), nvars++); + } while (testnext(ls, ',')); + if (testnext(ls, '=')) + nexps = explist1(ls, &e); + else { + e.k = VVOID; + nexps = 0; + } + adjust_assign(ls, nvars, nexps, &e); + adjustlocalvars(ls, nvars); +} + + +static int funcname (LexState *ls, expdesc *v) { + /* funcname -> NAME {field} [`:' NAME] */ + int needself = 0; + singlevar(ls, v); + while (ls->t.token == '.') + field(ls, v); + if (ls->t.token == ':') { + needself = 1; + field(ls, v); + } + return needself; +} + + +static void funcstat (LexState *ls, int line) { + /* funcstat -> FUNCTION funcname body */ + int needself; + expdesc v, b; + luaX_next(ls); /* skip FUNCTION */ + needself = funcname(ls, &v); + body(ls, &b, needself, line); + luaK_storevar(ls->fs, &v, &b); + luaK_fixline(ls->fs, line); /* definition `happens' in the first line */ +} + + +static void exprstat (LexState *ls) { + /* stat -> func | assignment */ + FuncState *fs = ls->fs; + struct LHS_assign v; + primaryexp(ls, &v.v); + if (v.v.k == VCALL) /* stat -> func */ + SETARG_C(getcode(fs, &v.v), 1); /* call statement uses no results */ + else { /* stat -> assignment */ + v.prev = NULL; + assignment(ls, &v, 1); + } +} + + +static void retstat (LexState *ls) { + /* stat -> RETURN explist */ + FuncState *fs = ls->fs; + expdesc e; + int first, nret; /* registers with returned values */ + luaX_next(ls); /* skip RETURN */ + if (block_follow(ls->t.token) || ls->t.token == ';') + first = nret = 0; /* return no values */ + else { + nret = explist1(ls, &e); /* optional return values */ + if (hasmultret(e.k)) { + luaK_setmultret(fs, &e); + if (e.k == VCALL && nret == 1) { /* tail call? */ + SET_OPCODE(getcode(fs,&e), OP_TAILCALL); + lua_assert(GETARG_A(getcode(fs,&e)) == fs->nactvar); + } + first = fs->nactvar; + nret = LUA_MULTRET; /* return all values */ + } + else { + if (nret == 1) /* only one single value? */ + first = luaK_exp2anyreg(fs, &e); + else { + luaK_exp2nextreg(fs, &e); /* values must go to the `stack' */ + first = fs->nactvar; /* return all `active' values */ + lua_assert(nret == fs->freereg - first); + } + } + } + luaK_ret(fs, first, nret); +} + + +static int statement (LexState *ls) { + int line = ls->linenumber; /* may be needed for error messages */ + switch (ls->t.token) { + case TK_IF: { /* stat -> ifstat */ + ifstat(ls, line); + return 0; + } + case TK_WHILE: { /* stat -> whilestat */ + whilestat(ls, line); + return 0; + } + case TK_DO: { /* stat -> DO block END */ + luaX_next(ls); /* skip DO */ + block(ls); + check_match(ls, TK_END, TK_DO, line); + return 0; + } + case TK_FOR: { /* stat -> forstat */ + forstat(ls, line); + return 0; + } + case TK_REPEAT: { /* stat -> repeatstat */ + repeatstat(ls, line); + return 0; + } + case TK_FUNCTION: { + funcstat(ls, line); /* stat -> funcstat */ + return 0; + } + case TK_LOCAL: { /* stat -> localstat */ + luaX_next(ls); /* skip LOCAL */ + if (testnext(ls, TK_FUNCTION)) /* local function? */ + localfunc(ls); + else + localstat(ls); + return 0; + } + case TK_RETURN: { /* stat -> retstat */ + retstat(ls); + return 1; /* must be last statement */ + } + case TK_BREAK: { /* stat -> breakstat */ + luaX_next(ls); /* skip BREAK */ + breakstat(ls); + return 1; /* must be last statement */ + } + default: { + exprstat(ls); + return 0; /* to avoid warnings */ + } + } +} + + +static void chunk (LexState *ls) { + /* chunk -> { stat [`;'] } */ + int islast = 0; + enterlevel(ls); + while (!islast && !block_follow(ls->t.token)) { + islast = statement(ls); + testnext(ls, ';'); + lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg && + ls->fs->freereg >= ls->fs->nactvar); + ls->fs->freereg = ls->fs->nactvar; /* free registers */ + } + leavelevel(ls); +} + +/* }====================================================================== */ diff --git a/external/lua/lua/lparser.h b/external/lua/lua/lparser.h new file mode 100644 index 0000000000..18836afd1c --- /dev/null +++ b/external/lua/lua/lparser.h @@ -0,0 +1,82 @@ +/* +** $Id: lparser.h,v 1.57.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lua Parser +** See Copyright Notice in lua.h +*/ + +#ifndef lparser_h +#define lparser_h + +#include "llimits.h" +#include "lobject.h" +#include "lzio.h" + + +/* +** Expression descriptor +*/ + +typedef enum { + VVOID, /* no value */ + VNIL, + VTRUE, + VFALSE, + VK, /* info = index of constant in `k' */ + VKNUM, /* nval = numerical value */ + VLOCAL, /* info = local register */ + VUPVAL, /* info = index of upvalue in `upvalues' */ + VGLOBAL, /* info = index of table; aux = index of global name in `k' */ + VINDEXED, /* info = table register; aux = index register (or `k') */ + VJMP, /* info = instruction pc */ + VRELOCABLE, /* info = instruction pc */ + VNONRELOC, /* info = result register */ + VCALL, /* info = instruction pc */ + VVARARG /* info = instruction pc */ +} expkind; + +typedef struct expdesc { + expkind k; + union { + struct { int info, aux; } s; + lua_Number nval; + } u; + int t; /* patch list of `exit when true' */ + int f; /* patch list of `exit when false' */ +} expdesc; + + +typedef struct upvaldesc { + lu_byte k; + lu_byte info; +} upvaldesc; + + +struct BlockCnt; /* defined in lparser.c */ + + +/* state needed to generate code for a given function */ +typedef struct FuncState { + Proto *f; /* current function header */ + Table *h; /* table to find (and reuse) elements in `k' */ + struct FuncState *prev; /* enclosing function */ + struct LexState *ls; /* lexical state */ + struct lua_State *L; /* copy of the Lua state */ + struct BlockCnt *bl; /* chain of current blocks */ + int pc; /* next position to code (equivalent to `ncode') */ + int lasttarget; /* `pc' of last `jump target' */ + int jpc; /* list of pending jumps to `pc' */ + int freereg; /* first free register */ + int nk; /* number of elements in `k' */ + int np; /* number of elements in `p' */ + short nlocvars; /* number of elements in `locvars' */ + lu_byte nactvar; /* number of active local variables */ + upvaldesc upvalues[LUAI_MAXUPVALUES]; /* upvalues */ + unsigned short actvar[LUAI_MAXVARS]; /* declared-variable stack */ +} FuncState; + + +LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, + const char *name); + + +#endif diff --git a/external/lua/lua/lstate.c b/external/lua/lua/lstate.c new file mode 100644 index 0000000000..4313b83a0c --- /dev/null +++ b/external/lua/lua/lstate.c @@ -0,0 +1,214 @@ +/* +** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $ +** Global State +** See Copyright Notice in lua.h +*/ + + +#include + +#define lstate_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "llex.h" +#include "lmem.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" + + +#define state_size(x) (sizeof(x) + LUAI_EXTRASPACE) +#define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE) +#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE)) + + +/* +** Main thread combines a thread state and the global state +*/ +typedef struct LG { + lua_State l; + global_State g; +} LG; + + + +static void stack_init (lua_State *L1, lua_State *L) { + /* initialize CallInfo array */ + L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); + L1->ci = L1->base_ci; + L1->size_ci = BASIC_CI_SIZE; + L1->end_ci = L1->base_ci + L1->size_ci - 1; + /* initialize stack array */ + L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue); + L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; + L1->top = L1->stack; + L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; + /* initialize first ci */ + L1->ci->func = L1->top; + setnilvalue(L1->top++); /* `function' entry for this `ci' */ + L1->base = L1->ci->base = L1->top; + L1->ci->top = L1->top + LUA_MINSTACK; +} + + +static void freestack (lua_State *L, lua_State *L1) { + luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); + luaM_freearray(L, L1->stack, L1->stacksize, TValue); +} + + +/* +** open parts that may cause memory-allocation errors +*/ +static void f_luaopen (lua_State *L, void *ud) { + global_State *g = G(L); + UNUSED(ud); + stack_init(L, L); /* init stack */ + sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */ + sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */ + luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ + luaT_init(L); + luaX_init(L); + luaS_fix(luaS_newliteral(L, MEMERRMSG)); + g->GCthreshold = 4*g->totalbytes; +} + + +static void preinit_state (lua_State *L, global_State *g) { + G(L) = g; + L->stack = NULL; + L->stacksize = 0; + L->errorJmp = NULL; + L->hook = NULL; + L->hookmask = 0; + L->basehookcount = 0; + L->allowhook = 1; + resethookcount(L); + L->openupval = NULL; + L->size_ci = 0; + L->nCcalls = L->baseCcalls = 0; + L->status = 0; + L->base_ci = L->ci = NULL; + L->savedpc = NULL; + L->errfunc = 0; + setnilvalue(gt(L)); +} + + +static void close_state (lua_State *L) { + global_State *g = G(L); + luaF_close(L, L->stack); /* close all upvalues for this thread */ + luaC_freeall(L); /* collect all objects */ + lua_assert(g->rootgc == obj2gco(L)); + lua_assert(g->strt.nuse == 0); + luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *); + luaZ_freebuffer(L, &g->buff); + freestack(L, L); + lua_assert(g->totalbytes == sizeof(LG)); + (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0); +} + + +lua_State *luaE_newthread (lua_State *L) { + lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State))); + luaC_link(L, obj2gco(L1), LUA_TTHREAD); + preinit_state(L1, G(L)); + stack_init(L1, L); /* init stack */ + setobj2n(L, gt(L1), gt(L)); /* share table of globals */ + L1->hookmask = L->hookmask; + L1->basehookcount = L->basehookcount; + L1->hook = L->hook; + resethookcount(L1); + lua_assert(iswhite(obj2gco(L1))); + return L1; +} + + +void luaE_freethread (lua_State *L, lua_State *L1) { + luaF_close(L1, L1->stack); /* close all upvalues for this thread */ + lua_assert(L1->openupval == NULL); + luai_userstatefree(L1); + freestack(L, L1); + luaM_freemem(L, fromstate(L1), state_size(lua_State)); +} + + +LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { + int i; + lua_State *L; + global_State *g; + void *l = (*f)(ud, NULL, 0, state_size(LG)); + if (l == NULL) return NULL; + L = tostate(l); + g = &((LG *)L)->g; + L->next = NULL; + L->tt = LUA_TTHREAD; + g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT); + L->marked = luaC_white(g); + set2bits(L->marked, FIXEDBIT, SFIXEDBIT); + preinit_state(L, g); + g->frealloc = f; + g->ud = ud; + g->mainthread = L; + g->uvhead.u.l.prev = &g->uvhead; + g->uvhead.u.l.next = &g->uvhead; + g->GCthreshold = 0; /* mark it as unfinished state */ + g->strt.size = 0; + g->strt.nuse = 0; + g->strt.hash = NULL; + setnilvalue(registry(L)); + luaZ_initbuffer(L, &g->buff); + g->panic = NULL; + g->gcstate = GCSpause; + g->rootgc = obj2gco(L); + g->sweepstrgc = 0; + g->sweepgc = &g->rootgc; + g->gray = NULL; + g->grayagain = NULL; + g->weak = NULL; + g->tmudata = NULL; + g->totalbytes = sizeof(LG); + g->gcpause = LUAI_GCPAUSE; + g->gcstepmul = LUAI_GCMUL; + g->gcdept = 0; + for (i=0; imt[i] = NULL; + if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { + /* memory allocation error: free partial state */ + close_state(L); + L = NULL; + } + else + luai_userstateopen(L); + return L; +} + + +static void callallgcTM (lua_State *L, void *ud) { + UNUSED(ud); + luaC_callGCTM(L); /* call GC metamethods for all udata */ +} + + +LUA_API void lua_close (lua_State *L) { + L = G(L)->mainthread; /* only the main thread can be closed */ + lua_lock(L); + luaF_close(L, L->stack); /* close all upvalues for this thread */ + luaC_separateudata(L, 1); /* separate udata that have GC metamethods */ + L->errfunc = 0; /* no error function during GC metamethods */ + do { /* repeat until no more errors */ + L->ci = L->base_ci; + L->base = L->top = L->ci->base; + L->nCcalls = L->baseCcalls = 0; + } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); + lua_assert(G(L)->tmudata == NULL); + luai_userstateclose(L); + close_state(L); +} + diff --git a/external/lua/lua/lstate.h b/external/lua/lua/lstate.h new file mode 100644 index 0000000000..3bc575b6bc --- /dev/null +++ b/external/lua/lua/lstate.h @@ -0,0 +1,169 @@ +/* +** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ +** Global State +** See Copyright Notice in lua.h +*/ + +#ifndef lstate_h +#define lstate_h + +#include "lua.h" + +#include "lobject.h" +#include "ltm.h" +#include "lzio.h" + + + +struct lua_longjmp; /* defined in ldo.c */ + + +/* table of globals */ +#define gt(L) (&L->l_gt) + +/* registry */ +#define registry(L) (&G(L)->l_registry) + + +/* extra stack space to handle TM calls and some other extras */ +#define EXTRA_STACK 5 + + +#define BASIC_CI_SIZE 8 + +#define BASIC_STACK_SIZE (2*LUA_MINSTACK) + + + +typedef struct stringtable { + GCObject **hash; + lu_int32 nuse; /* number of elements */ + int size; +} stringtable; + + +/* +** informations about a call +*/ +typedef struct CallInfo { + StkId base; /* base for this function */ + StkId func; /* function index in the stack */ + StkId top; /* top for this function */ + const Instruction *savedpc; + int nresults; /* expected number of results from this function */ + int tailcalls; /* number of tail calls lost under this entry */ +} CallInfo; + + + +#define curr_func(L) (clvalue(L->ci->func)) +#define ci_func(ci) (clvalue((ci)->func)) +#define f_isLua(ci) (!ci_func(ci)->c.isC) +#define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) + + +/* +** `global state', shared by all threads of this state +*/ +typedef struct global_State { + stringtable strt; /* hash table for strings */ + lua_Alloc frealloc; /* function to reallocate memory */ + void *ud; /* auxiliary data to `frealloc' */ + lu_byte currentwhite; + lu_byte gcstate; /* state of garbage collector */ + int sweepstrgc; /* position of sweep in `strt' */ + GCObject *rootgc; /* list of all collectable objects */ + GCObject **sweepgc; /* position of sweep in `rootgc' */ + GCObject *gray; /* list of gray objects */ + GCObject *grayagain; /* list of objects to be traversed atomically */ + GCObject *weak; /* list of weak tables (to be cleared) */ + GCObject *tmudata; /* last element of list of userdata to be GC */ + Mbuffer buff; /* temporary buffer for string concatentation */ + lu_mem GCthreshold; + lu_mem totalbytes; /* number of bytes currently allocated */ + lu_mem estimate; /* an estimate of number of bytes actually in use */ + lu_mem gcdept; /* how much GC is `behind schedule' */ + int gcpause; /* size of pause between successive GCs */ + int gcstepmul; /* GC `granularity' */ + lua_CFunction panic; /* to be called in unprotected errors */ + TValue l_registry; + struct lua_State *mainthread; + UpVal uvhead; /* head of double-linked list of all open upvalues */ + struct Table *mt[NUM_TAGS]; /* metatables for basic types */ + TString *tmname[TM_N]; /* array with tag-method names */ +} global_State; + + +/* +** `per thread' state +*/ +struct lua_State { + CommonHeader; + lu_byte status; + StkId top; /* first free slot in the stack */ + StkId base; /* base of current function */ + global_State *l_G; + CallInfo *ci; /* call info for current function */ + const Instruction *savedpc; /* `savedpc' of current function */ + StkId stack_last; /* last free slot in the stack */ + StkId stack; /* stack base */ + CallInfo *end_ci; /* points after end of ci array*/ + CallInfo *base_ci; /* array of CallInfo's */ + int stacksize; + int size_ci; /* size of array `base_ci' */ + unsigned short nCcalls; /* number of nested C calls */ + unsigned short baseCcalls; /* nested C calls when resuming coroutine */ + lu_byte hookmask; + lu_byte allowhook; + int basehookcount; + int hookcount; + lua_Hook hook; + TValue l_gt; /* table of globals */ + TValue env; /* temporary place for environments */ + GCObject *openupval; /* list of open upvalues in this stack */ + GCObject *gclist; + struct lua_longjmp *errorJmp; /* current error recover point */ + ptrdiff_t errfunc; /* current error handling function (stack index) */ +}; + + +#define G(L) (L->l_G) + + +/* +** Union of all collectable objects +*/ +union GCObject { + GCheader gch; + union TString ts; + union Udata u; + union Closure cl; + struct Table h; + struct Proto p; + struct UpVal uv; + struct lua_State th; /* thread */ +}; + + +/* macros to convert a GCObject into a specific value */ +#define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) +#define gco2ts(o) (&rawgco2ts(o)->tsv) +#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) +#define gco2u(o) (&rawgco2u(o)->uv) +#define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) +#define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) +#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) +#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) +#define ngcotouv(o) \ + check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) +#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) + +/* macro to convert any Lua object into a GCObject */ +#define obj2gco(v) (cast(GCObject *, (v))) + + +LUAI_FUNC lua_State *luaE_newthread (lua_State *L); +LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); + +#endif + diff --git a/external/lua/lua/lstring.c b/external/lua/lua/lstring.c new file mode 100644 index 0000000000..49113151cc --- /dev/null +++ b/external/lua/lua/lstring.c @@ -0,0 +1,111 @@ +/* +** $Id: lstring.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $ +** String table (keeps all strings handled by Lua) +** See Copyright Notice in lua.h +*/ + + +#include + +#define lstring_c +#define LUA_CORE + +#include "lua.h" + +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" + + + +void luaS_resize (lua_State *L, int newsize) { + GCObject **newhash; + stringtable *tb; + int i; + if (G(L)->gcstate == GCSsweepstring) + return; /* cannot resize during GC traverse */ + newhash = luaM_newvector(L, newsize, GCObject *); + tb = &G(L)->strt; + for (i=0; isize; i++) { + GCObject *p = tb->hash[i]; + while (p) { /* for each node in the list */ + GCObject *next = p->gch.next; /* save next */ + unsigned int h = gco2ts(p)->hash; + int h1 = lmod(h, newsize); /* new position */ + lua_assert(cast_int(h%newsize) == lmod(h, newsize)); + p->gch.next = newhash[h1]; /* chain it */ + newhash[h1] = p; + p = next; + } + } + luaM_freearray(L, tb->hash, tb->size, TString *); + tb->size = newsize; + tb->hash = newhash; +} + + +static TString *newlstr (lua_State *L, const char *str, size_t l, + unsigned int h) { + TString *ts; + stringtable *tb; + if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) + luaM_toobig(L); + ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); + ts->tsv.len = l; + ts->tsv.hash = h; + ts->tsv.marked = luaC_white(G(L)); + ts->tsv.tt = LUA_TSTRING; + ts->tsv.reserved = 0; + memcpy(ts+1, str, l*sizeof(char)); + ((char *)(ts+1))[l] = '\0'; /* ending 0 */ + tb = &G(L)->strt; + h = lmod(h, tb->size); + ts->tsv.next = tb->hash[h]; /* chain new entry */ + tb->hash[h] = obj2gco(ts); + tb->nuse++; + if (tb->nuse > cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) + luaS_resize(L, tb->size*2); /* too crowded */ + return ts; +} + + +TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { + GCObject *o; + unsigned int h = cast(unsigned int, l); /* seed */ + size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ + size_t l1; + for (l1=l; l1>=step; l1-=step) /* compute hash */ + h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); + for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; + o != NULL; + o = o->gch.next) { + TString *ts = rawgco2ts(o); + if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) { + /* string may be dead */ + if (isdead(G(L), o)) changewhite(o); + return ts; + } + } + return newlstr(L, str, l, h); /* not found */ +} + + +Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { + Udata *u; + if (s > MAX_SIZET - sizeof(Udata)) + luaM_toobig(L); + u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); + u->uv.marked = luaC_white(G(L)); /* is not finalized */ + u->uv.tt = LUA_TUSERDATA; + u->uv.len = s; + u->uv.metatable = NULL; + u->uv.env = e; + /* chain it on udata list (after main thread) */ + u->uv.next = G(L)->mainthread->next; + G(L)->mainthread->next = obj2gco(u); + return u; +} + diff --git a/external/lua/lua/lstring.h b/external/lua/lua/lstring.h new file mode 100644 index 0000000000..73a2ff8b38 --- /dev/null +++ b/external/lua/lua/lstring.h @@ -0,0 +1,31 @@ +/* +** $Id: lstring.h,v 1.43.1.1 2007/12/27 13:02:25 roberto Exp $ +** String table (keep all strings handled by Lua) +** See Copyright Notice in lua.h +*/ + +#ifndef lstring_h +#define lstring_h + + +#include "lgc.h" +#include "lobject.h" +#include "lstate.h" + + +#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) + +#define sizeudata(u) (sizeof(union Udata)+(u)->len) + +#define luaS_new(L, s) (luaS_newlstr(L, s, strlen(s))) +#define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ + (sizeof(s)/sizeof(char))-1)) + +#define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) + +LUAI_FUNC void luaS_resize (lua_State *L, int newsize); +LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); +LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); + + +#endif diff --git a/external/lua/lua/lstrlib.c b/external/lua/lua/lstrlib.c new file mode 100644 index 0000000000..7a03489beb --- /dev/null +++ b/external/lua/lua/lstrlib.c @@ -0,0 +1,871 @@ +/* +** $Id: lstrlib.c,v 1.132.1.5 2010/05/14 15:34:19 roberto Exp $ +** Standard library for string operations and pattern-matching +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include +#include + +#define lstrlib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +/* macro to `unsign' a character */ +#define uchar(c) ((unsigned char)(c)) + + + +static int str_len (lua_State *L) { + size_t l; + luaL_checklstring(L, 1, &l); + lua_pushinteger(L, l); + return 1; +} + + +static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) { + /* relative string position: negative means back from end */ + if (pos < 0) pos += (ptrdiff_t)len + 1; + return (pos >= 0) ? pos : 0; +} + + +static int str_sub (lua_State *L) { + size_t l; + const char *s = luaL_checklstring(L, 1, &l); + ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l); + ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l); + if (start < 1) start = 1; + if (end > (ptrdiff_t)l) end = (ptrdiff_t)l; + if (start <= end) + lua_pushlstring(L, s+start-1, end-start+1); + else lua_pushliteral(L, ""); + return 1; +} + + +static int str_reverse (lua_State *L) { + size_t l; + luaL_Buffer b; + const char *s = luaL_checklstring(L, 1, &l); + luaL_buffinit(L, &b); + while (l--) luaL_addchar(&b, s[l]); + luaL_pushresult(&b); + return 1; +} + + +static int str_lower (lua_State *L) { + size_t l; + size_t i; + luaL_Buffer b; + const char *s = luaL_checklstring(L, 1, &l); + luaL_buffinit(L, &b); + for (i=0; i 0) + luaL_addlstring(&b, s, l); + luaL_pushresult(&b); + return 1; +} + + +static int str_byte (lua_State *L) { + size_t l; + const char *s = luaL_checklstring(L, 1, &l); + ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l); + ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l); + int n, i; + if (posi <= 0) posi = 1; + if ((size_t)pose > l) pose = l; + if (posi > pose) return 0; /* empty interval; return no values */ + n = (int)(pose - posi + 1); + if (posi + n <= pose) /* overflow? */ + luaL_error(L, "string slice too long"); + luaL_checkstack(L, n, "string slice too long"); + for (i=0; i= ms->level || ms->capture[l].len == CAP_UNFINISHED) + return luaL_error(ms->L, "invalid capture index"); + return l; +} + + +static int capture_to_close (MatchState *ms) { + int level = ms->level; + for (level--; level>=0; level--) + if (ms->capture[level].len == CAP_UNFINISHED) return level; + return luaL_error(ms->L, "invalid pattern capture"); +} + + +static const char *classend (MatchState *ms, const char *p) { + switch (*p++) { + case L_ESC: { + if (*p == '\0') + luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); + return p+1; + } + case '[': { + if (*p == '^') p++; + do { /* look for a `]' */ + if (*p == '\0') + luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); + if (*(p++) == L_ESC && *p != '\0') + p++; /* skip escapes (e.g. `%]') */ + } while (*p != ']'); + return p+1; + } + default: { + return p; + } + } +} + + +static int match_class (int c, int cl) { + int res; + switch (tolower(cl)) { + case 'a' : res = isalpha(c); break; + case 'c' : res = iscntrl(c); break; + case 'd' : res = isdigit(c); break; + case 'l' : res = islower(c); break; + case 'p' : res = ispunct(c); break; + case 's' : res = isspace(c); break; + case 'u' : res = isupper(c); break; + case 'w' : res = isalnum(c); break; + case 'x' : res = isxdigit(c); break; + case 'z' : res = (c == 0); break; + default: return (cl == c); + } + return (islower(cl) ? res : !res); +} + + +static int matchbracketclass (int c, const char *p, const char *ec) { + int sig = 1; + if (*(p+1) == '^') { + sig = 0; + p++; /* skip the `^' */ + } + while (++p < ec) { + if (*p == L_ESC) { + p++; + if (match_class(c, uchar(*p))) + return sig; + } + else if ((*(p+1) == '-') && (p+2 < ec)) { + p+=2; + if (uchar(*(p-2)) <= c && c <= uchar(*p)) + return sig; + } + else if (uchar(*p) == c) return sig; + } + return !sig; +} + + +static int singlematch (int c, const char *p, const char *ep) { + switch (*p) { + case '.': return 1; /* matches any char */ + case L_ESC: return match_class(c, uchar(*(p+1))); + case '[': return matchbracketclass(c, p, ep-1); + default: return (uchar(*p) == c); + } +} + + +static const char *match (MatchState *ms, const char *s, const char *p); + + +static const char *matchbalance (MatchState *ms, const char *s, + const char *p) { + if (*p == 0 || *(p+1) == 0) + luaL_error(ms->L, "unbalanced pattern"); + if (*s != *p) return NULL; + else { + int b = *p; + int e = *(p+1); + int cont = 1; + while (++s < ms->src_end) { + if (*s == e) { + if (--cont == 0) return s+1; + } + else if (*s == b) cont++; + } + } + return NULL; /* string ends out of balance */ +} + + +static const char *max_expand (MatchState *ms, const char *s, + const char *p, const char *ep) { + ptrdiff_t i = 0; /* counts maximum expand for item */ + while ((s+i)src_end && singlematch(uchar(*(s+i)), p, ep)) + i++; + /* keeps trying to match with the maximum repetitions */ + while (i>=0) { + const char *res = match(ms, (s+i), ep+1); + if (res) return res; + i--; /* else didn't match; reduce 1 repetition to try again */ + } + return NULL; +} + + +static const char *min_expand (MatchState *ms, const char *s, + const char *p, const char *ep) { + for (;;) { + const char *res = match(ms, s, ep+1); + if (res != NULL) + return res; + else if (ssrc_end && singlematch(uchar(*s), p, ep)) + s++; /* try with one more repetition */ + else return NULL; + } +} + + +static const char *start_capture (MatchState *ms, const char *s, + const char *p, int what) { + const char *res; + int level = ms->level; + if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); + ms->capture[level].init = s; + ms->capture[level].len = what; + ms->level = level+1; + if ((res=match(ms, s, p)) == NULL) /* match failed? */ + ms->level--; /* undo capture */ + return res; +} + + +static const char *end_capture (MatchState *ms, const char *s, + const char *p) { + int l = capture_to_close(ms); + const char *res; + ms->capture[l].len = s - ms->capture[l].init; /* close capture */ + if ((res = match(ms, s, p)) == NULL) /* match failed? */ + ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ + return res; +} + + +static const char *match_capture (MatchState *ms, const char *s, int l) { + size_t len; + l = check_capture(ms, l); + len = ms->capture[l].len; + if ((size_t)(ms->src_end-s) >= len && + memcmp(ms->capture[l].init, s, len) == 0) + return s+len; + else return NULL; +} + + +static const char *match (MatchState *ms, const char *s, const char *p) { + init: /* using goto's to optimize tail recursion */ + switch (*p) { + case '(': { /* start capture */ + if (*(p+1) == ')') /* position capture? */ + return start_capture(ms, s, p+2, CAP_POSITION); + else + return start_capture(ms, s, p+1, CAP_UNFINISHED); + } + case ')': { /* end capture */ + return end_capture(ms, s, p+1); + } + case L_ESC: { + switch (*(p+1)) { + case 'b': { /* balanced string? */ + s = matchbalance(ms, s, p+2); + if (s == NULL) return NULL; + p+=4; goto init; /* else return match(ms, s, p+4); */ + } + case 'f': { /* frontier? */ + const char *ep; char previous; + p += 2; + if (*p != '[') + luaL_error(ms->L, "missing " LUA_QL("[") " after " + LUA_QL("%%f") " in pattern"); + ep = classend(ms, p); /* points to what is next */ + previous = (s == ms->src_init) ? '\0' : *(s-1); + if (matchbracketclass(uchar(previous), p, ep-1) || + !matchbracketclass(uchar(*s), p, ep-1)) return NULL; + p=ep; goto init; /* else return match(ms, s, ep); */ + } + default: { + if (isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */ + s = match_capture(ms, s, uchar(*(p+1))); + if (s == NULL) return NULL; + p+=2; goto init; /* else return match(ms, s, p+2) */ + } + goto dflt; /* case default */ + } + } + } + case '\0': { /* end of pattern */ + return s; /* match succeeded */ + } + case '$': { + if (*(p+1) == '\0') /* is the `$' the last char in pattern? */ + return (s == ms->src_end) ? s : NULL; /* check end of string */ + else goto dflt; + } + default: dflt: { /* it is a pattern item */ + const char *ep = classend(ms, p); /* points to what is next */ + int m = ssrc_end && singlematch(uchar(*s), p, ep); + switch (*ep) { + case '?': { /* optional */ + const char *res; + if (m && ((res=match(ms, s+1, ep+1)) != NULL)) + return res; + p=ep+1; goto init; /* else return match(ms, s, ep+1); */ + } + case '*': { /* 0 or more repetitions */ + return max_expand(ms, s, p, ep); + } + case '+': { /* 1 or more repetitions */ + return (m ? max_expand(ms, s+1, p, ep) : NULL); + } + case '-': { /* 0 or more repetitions (minimum) */ + return min_expand(ms, s, p, ep); + } + default: { + if (!m) return NULL; + s++; p=ep; goto init; /* else return match(ms, s+1, ep); */ + } + } + } + } +} + + + +static const char *lmemfind (const char *s1, size_t l1, + const char *s2, size_t l2) { + if (l2 == 0) return s1; /* empty strings are everywhere */ + else if (l2 > l1) return NULL; /* avoids a negative `l1' */ + else { + const char *init; /* to search for a `*s2' inside `s1' */ + l2--; /* 1st char will be checked by `memchr' */ + l1 = l1-l2; /* `s2' cannot be found after that */ + while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { + init++; /* 1st char is already checked */ + if (memcmp(init, s2+1, l2) == 0) + return init-1; + else { /* correct `l1' and `s1' to try again */ + l1 -= init-s1; + s1 = init; + } + } + return NULL; /* not found */ + } +} + + +static void push_onecapture (MatchState *ms, int i, const char *s, + const char *e) { + if (i >= ms->level) { + if (i == 0) /* ms->level == 0, too */ + lua_pushlstring(ms->L, s, e - s); /* add whole match */ + else + luaL_error(ms->L, "invalid capture index"); + } + else { + ptrdiff_t l = ms->capture[i].len; + if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); + if (l == CAP_POSITION) + lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); + else + lua_pushlstring(ms->L, ms->capture[i].init, l); + } +} + + +static int push_captures (MatchState *ms, const char *s, const char *e) { + int i; + int nlevels = (ms->level == 0 && s) ? 1 : ms->level; + luaL_checkstack(ms->L, nlevels, "too many captures"); + for (i = 0; i < nlevels; i++) + push_onecapture(ms, i, s, e); + return nlevels; /* number of strings pushed */ +} + + +static int str_find_aux (lua_State *L, int find) { + size_t l1, l2; + const char *s = luaL_checklstring(L, 1, &l1); + const char *p = luaL_checklstring(L, 2, &l2); + ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; + if (init < 0) init = 0; + else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; + if (find && (lua_toboolean(L, 4) || /* explicit request? */ + strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ + /* do a plain search */ + const char *s2 = lmemfind(s+init, l1-init, p, l2); + if (s2) { + lua_pushinteger(L, s2-s+1); + lua_pushinteger(L, s2-s+l2); + return 2; + } + } + else { + MatchState ms; + int anchor = (*p == '^') ? (p++, 1) : 0; + const char *s1=s+init; + ms.L = L; + ms.src_init = s; + ms.src_end = s+l1; + do { + const char *res; + ms.level = 0; + if ((res=match(&ms, s1, p)) != NULL) { + if (find) { + lua_pushinteger(L, s1-s+1); /* start */ + lua_pushinteger(L, res-s); /* end */ + return push_captures(&ms, NULL, 0) + 2; + } + else + return push_captures(&ms, s1, res); + } + } while (s1++ < ms.src_end && !anchor); + } + lua_pushnil(L); /* not found */ + return 1; +} + + +static int str_find (lua_State *L) { + return str_find_aux(L, 1); +} + + +static int str_match (lua_State *L) { + return str_find_aux(L, 0); +} + + +static int gmatch_aux (lua_State *L) { + MatchState ms; + size_t ls; + const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); + const char *p = lua_tostring(L, lua_upvalueindex(2)); + const char *src; + ms.L = L; + ms.src_init = s; + ms.src_end = s+ls; + for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3)); + src <= ms.src_end; + src++) { + const char *e; + ms.level = 0; + if ((e = match(&ms, src, p)) != NULL) { + lua_Integer newstart = e-s; + if (e == src) newstart++; /* empty match? go at least one position */ + lua_pushinteger(L, newstart); + lua_replace(L, lua_upvalueindex(3)); + return push_captures(&ms, src, e); + } + } + return 0; /* not found */ +} + + +static int gmatch (lua_State *L) { + luaL_checkstring(L, 1); + luaL_checkstring(L, 2); + lua_settop(L, 2); + lua_pushinteger(L, 0); + lua_pushcclosure(L, gmatch_aux, 3); + return 1; +} + + +static int gfind_nodef (lua_State *L) { + return luaL_error(L, LUA_QL("string.gfind") " was renamed to " + LUA_QL("string.gmatch")); +} + + +static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, + const char *e) { + size_t l, i; + const char *news = lua_tolstring(ms->L, 3, &l); + for (i = 0; i < l; i++) { + if (news[i] != L_ESC) + luaL_addchar(b, news[i]); + else { + i++; /* skip ESC */ + if (!isdigit(uchar(news[i]))) + luaL_addchar(b, news[i]); + else if (news[i] == '0') + luaL_addlstring(b, s, e - s); + else { + push_onecapture(ms, news[i] - '1', s, e); + luaL_addvalue(b); /* add capture to accumulated result */ + } + } + } +} + + +static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, + const char *e) { + lua_State *L = ms->L; + switch (lua_type(L, 3)) { + case LUA_TNUMBER: + case LUA_TSTRING: { + add_s(ms, b, s, e); + return; + } + case LUA_TFUNCTION: { + int n; + lua_pushvalue(L, 3); + n = push_captures(ms, s, e); + lua_call(L, n, 1); + break; + } + case LUA_TTABLE: { + push_onecapture(ms, 0, s, e); + lua_gettable(L, 3); + break; + } + } + if (!lua_toboolean(L, -1)) { /* nil or false? */ + lua_pop(L, 1); + lua_pushlstring(L, s, e - s); /* keep original text */ + } + else if (!lua_isstring(L, -1)) + luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); + luaL_addvalue(b); /* add result to accumulator */ +} + + +static int str_gsub (lua_State *L) { + size_t srcl; + const char *src = luaL_checklstring(L, 1, &srcl); + const char *p = luaL_checkstring(L, 2); + int tr = lua_type(L, 3); + int max_s = luaL_optint(L, 4, srcl+1); + int anchor = (*p == '^') ? (p++, 1) : 0; + int n = 0; + MatchState ms; + luaL_Buffer b; + luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || + tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, + "string/function/table expected"); + luaL_buffinit(L, &b); + ms.L = L; + ms.src_init = src; + ms.src_end = src+srcl; + while (n < max_s) { + const char *e; + ms.level = 0; + e = match(&ms, src, p); + if (e) { + n++; + add_value(&ms, &b, src, e); + } + if (e && e>src) /* non empty match? */ + src = e; /* skip it */ + else if (src < ms.src_end) + luaL_addchar(&b, *src++); + else break; + if (anchor) break; + } + luaL_addlstring(&b, src, ms.src_end-src); + luaL_pushresult(&b); + lua_pushinteger(L, n); /* number of substitutions */ + return 2; +} + +/* }====================================================== */ + + +/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ +#define MAX_ITEM 512 +/* valid flags in a format specification */ +#define FLAGS "-+ #0" +/* +** maximum size of each format specification (such as '%-099.99d') +** (+10 accounts for %99.99x plus margin of error) +*/ +#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) + + +static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { + size_t l; + const char *s = luaL_checklstring(L, arg, &l); + luaL_addchar(b, '"'); + while (l--) { + switch (*s) { + case '"': case '\\': case '\n': { + luaL_addchar(b, '\\'); + luaL_addchar(b, *s); + break; + } + case '\r': { + luaL_addlstring(b, "\\r", 2); + break; + } + case '\0': { + luaL_addlstring(b, "\\000", 4); + break; + } + default: { + luaL_addchar(b, *s); + break; + } + } + s++; + } + luaL_addchar(b, '"'); +} + +static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { + const char *p = strfrmt; + while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ + if ((size_t)(p - strfrmt) >= sizeof(FLAGS)) + luaL_error(L, "invalid format (repeated flags)"); + if (isdigit(uchar(*p))) p++; /* skip width */ + if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ + if (*p == '.') { + p++; + if (isdigit(uchar(*p))) p++; /* skip precision */ + if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ + } + if (isdigit(uchar(*p))) + luaL_error(L, "invalid format (width or precision too long)"); + *(form++) = '%'; + strncpy(form, strfrmt, p - strfrmt + 1); + form += p - strfrmt + 1; + *form = '\0'; + return p; +} + + +static void addintlen (char *form) { + size_t l = strlen(form); + char spec = form[l - 1]; + strcpy(form + l - 1, LUA_INTFRMLEN); + form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; + form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; +} + + +static int str_format (lua_State *L) { + int top = lua_gettop(L); + int arg = 1; + size_t sfl; + const char *strfrmt = luaL_checklstring(L, arg, &sfl); + const char *strfrmt_end = strfrmt+sfl; + luaL_Buffer b; + luaL_buffinit(L, &b); + while (strfrmt < strfrmt_end) { + if (*strfrmt != L_ESC) + luaL_addchar(&b, *strfrmt++); + else if (*++strfrmt == L_ESC) + luaL_addchar(&b, *strfrmt++); /* %% */ + else { /* format item */ + char form[MAX_FORMAT]; /* to store the format (`%...') */ + char buff[MAX_ITEM]; /* to store the formatted item */ + if (++arg > top) + luaL_argerror(L, arg, "no value"); + strfrmt = scanformat(L, strfrmt, form); + switch (*strfrmt++) { + case 'c': { + sprintf(buff, form, (int)luaL_checknumber(L, arg)); + break; + } + case 'd': case 'i': { + addintlen(form); + sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); + break; + } + case 'o': case 'u': case 'x': case 'X': { + addintlen(form); + sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); + break; + } + case 'e': case 'E': case 'f': + case 'g': case 'G': { + sprintf(buff, form, (double)luaL_checknumber(L, arg)); + break; + } + case 'q': { + addquoted(L, &b, arg); + continue; /* skip the 'addsize' at the end */ + } + case 's': { + size_t l; + const char *s = luaL_checklstring(L, arg, &l); + if (!strchr(form, '.') && l >= 100) { + /* no precision and string is too long to be formatted; + keep original string */ + lua_pushvalue(L, arg); + luaL_addvalue(&b); + continue; /* skip the `addsize' at the end */ + } + else { + sprintf(buff, form, s); + break; + } + } + default: { /* also treat cases `pnLlh' */ + return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " + LUA_QL("format"), *(strfrmt - 1)); + } + } + luaL_addlstring(&b, buff, strlen(buff)); + } + } + luaL_pushresult(&b); + return 1; +} + + +static const luaL_Reg strlib[] = { + {"byte", str_byte}, + {"char", str_char}, + {"dump", str_dump}, + {"find", str_find}, + {"format", str_format}, + {"gfind", gfind_nodef}, + {"gmatch", gmatch}, + {"gsub", str_gsub}, + {"len", str_len}, + {"lower", str_lower}, + {"match", str_match}, + {"rep", str_rep}, + {"reverse", str_reverse}, + {"sub", str_sub}, + {"upper", str_upper}, + {NULL, NULL} +}; + + +static void createmetatable (lua_State *L) { + lua_createtable(L, 0, 1); /* create metatable for strings */ + lua_pushliteral(L, ""); /* dummy string */ + lua_pushvalue(L, -2); + lua_setmetatable(L, -2); /* set string metatable */ + lua_pop(L, 1); /* pop dummy string */ + lua_pushvalue(L, -2); /* string library... */ + lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */ + lua_pop(L, 1); /* pop metatable */ +} + + +/* +** Open string library +*/ +LUALIB_API int luaopen_string (lua_State *L) { + luaL_register(L, LUA_STRLIBNAME, strlib); +#if defined(LUA_COMPAT_GFIND) + lua_getfield(L, -1, "gmatch"); + lua_setfield(L, -2, "gfind"); +#endif + createmetatable(L); + return 1; +} + diff --git a/external/lua/lua/ltable.c b/external/lua/lua/ltable.c new file mode 100644 index 0000000000..ec84f4fabc --- /dev/null +++ b/external/lua/lua/ltable.c @@ -0,0 +1,588 @@ +/* +** $Id: ltable.c,v 2.32.1.2 2007/12/28 15:32:23 roberto Exp $ +** Lua tables (hash) +** See Copyright Notice in lua.h +*/ + + +/* +** Implementation of tables (aka arrays, objects, or hash tables). +** Tables keep its elements in two parts: an array part and a hash part. +** Non-negative integer keys are all candidates to be kept in the array +** part. The actual size of the array is the largest `n' such that at +** least half the slots between 0 and n are in use. +** Hash uses a mix of chained scatter table with Brent's variation. +** A main invariant of these tables is that, if an element is not +** in its main position (i.e. the `original' position that its hash gives +** to it), then the colliding element is in its own main position. +** Hence even when the load factor reaches 100%, performance remains good. +*/ + +#include +#include + +#define ltable_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lgc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstate.h" +#include "ltable.h" + + +/* +** max size of array part is 2^MAXBITS +*/ +#if LUAI_BITSINT > 26 +#define MAXBITS 26 +#else +#define MAXBITS (LUAI_BITSINT-2) +#endif + +#define MAXASIZE (1 << MAXBITS) + + +#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t)))) + +#define hashstr(t,str) hashpow2(t, (str)->tsv.hash) +#define hashboolean(t,p) hashpow2(t, p) + + +/* +** for some types, it is better to avoid modulus by power of 2, as +** they tend to have many 2 factors. +*/ +#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1)))) + + +#define hashpointer(t,p) hashmod(t, IntPoint(p)) + + +/* +** number of ints inside a lua_Number +*/ +#define numints cast_int(sizeof(lua_Number)/sizeof(int)) + + + +#define dummynode (&dummynode_) + +static const Node dummynode_ = { + {{NULL}, LUA_TNIL}, /* value */ + {{{NULL}, LUA_TNIL, NULL}} /* key */ +}; + + +/* +** hash for lua_Numbers +*/ +static Node *hashnum (const Table *t, lua_Number n) { + unsigned int a[numints]; + int i; + if (luai_numeq(n, 0)) /* avoid problems with -0 */ + return gnode(t, 0); + memcpy(a, &n, sizeof(a)); + for (i = 1; i < numints; i++) a[0] += a[i]; + return hashmod(t, a[0]); +} + + + +/* +** returns the `main' position of an element in a table (that is, the index +** of its hash value) +*/ +static Node *mainposition (const Table *t, const TValue *key) { + switch (ttype(key)) { + case LUA_TNUMBER: + return hashnum(t, nvalue(key)); + case LUA_TSTRING: + return hashstr(t, rawtsvalue(key)); + case LUA_TBOOLEAN: + return hashboolean(t, bvalue(key)); + case LUA_TLIGHTUSERDATA: + return hashpointer(t, pvalue(key)); + default: + return hashpointer(t, gcvalue(key)); + } +} + + +/* +** returns the index for `key' if `key' is an appropriate key to live in +** the array part of the table, -1 otherwise. +*/ +static int arrayindex (const TValue *key) { + if (ttisnumber(key)) { + lua_Number n = nvalue(key); + int k; + lua_number2int(k, n); + if (luai_numeq(cast_num(k), n)) + return k; + } + return -1; /* `key' did not match some condition */ +} + + +/* +** returns the index of a `key' for table traversals. First goes all +** elements in the array part, then elements in the hash part. The +** beginning of a traversal is signalled by -1. +*/ +static int findindex (lua_State *L, Table *t, StkId key) { + int i; + if (ttisnil(key)) return -1; /* first iteration */ + i = arrayindex(key); + if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ + return i-1; /* yes; that's the index (corrected to C) */ + else { + Node *n = mainposition(t, key); + do { /* check whether `key' is somewhere in the chain */ + /* key may be dead already, but it is ok to use it in `next' */ + if (luaO_rawequalObj(key2tval(n), key) || + (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) && + gcvalue(gkey(n)) == gcvalue(key))) { + i = cast_int(n - gnode(t, 0)); /* key index in hash table */ + /* hash elements are numbered after array ones */ + return i + t->sizearray; + } + else n = gnext(n); + } while (n); + luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ + return 0; /* to avoid warnings */ + } +} + + +int luaH_next (lua_State *L, Table *t, StkId key) { + int i = findindex(L, t, key); /* find original element */ + for (i++; i < t->sizearray; i++) { /* try first array part */ + if (!ttisnil(&t->array[i])) { /* a non-nil value? */ + setnvalue(key, cast_num(i+1)); + setobj2s(L, key+1, &t->array[i]); + return 1; + } + } + for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ + if (!ttisnil(gval(gnode(t, i)))) { /* a non-nil value? */ + setobj2s(L, key, key2tval(gnode(t, i))); + setobj2s(L, key+1, gval(gnode(t, i))); + return 1; + } + } + return 0; /* no more elements */ +} + + +/* +** {============================================================= +** Rehash +** ============================================================== +*/ + + +static int computesizes (int nums[], int *narray) { + int i; + int twotoi; /* 2^i */ + int a = 0; /* number of elements smaller than 2^i */ + int na = 0; /* number of elements to go to array part */ + int n = 0; /* optimal size for array part */ + for (i = 0, twotoi = 1; twotoi/2 < *narray; i++, twotoi *= 2) { + if (nums[i] > 0) { + a += nums[i]; + if (a > twotoi/2) { /* more than half elements present? */ + n = twotoi; /* optimal size (till now) */ + na = a; /* all elements smaller than n will go to array part */ + } + } + if (a == *narray) break; /* all elements already counted */ + } + *narray = n; + lua_assert(*narray/2 <= na && na <= *narray); + return na; +} + + +static int countint (const TValue *key, int *nums) { + int k = arrayindex(key); + if (0 < k && k <= MAXASIZE) { /* is `key' an appropriate array index? */ + nums[ceillog2(k)]++; /* count as such */ + return 1; + } + else + return 0; +} + + +static int numusearray (const Table *t, int *nums) { + int lg; + int ttlg; /* 2^lg */ + int ause = 0; /* summation of `nums' */ + int i = 1; /* count to traverse all array keys */ + for (lg=0, ttlg=1; lg<=MAXBITS; lg++, ttlg*=2) { /* for each slice */ + int lc = 0; /* counter */ + int lim = ttlg; + if (lim > t->sizearray) { + lim = t->sizearray; /* adjust upper limit */ + if (i > lim) + break; /* no more elements to count */ + } + /* count elements in range (2^(lg-1), 2^lg] */ + for (; i <= lim; i++) { + if (!ttisnil(&t->array[i-1])) + lc++; + } + nums[lg] += lc; + ause += lc; + } + return ause; +} + + +static int numusehash (const Table *t, int *nums, int *pnasize) { + int totaluse = 0; /* total number of elements */ + int ause = 0; /* summation of `nums' */ + int i = sizenode(t); + while (i--) { + Node *n = &t->node[i]; + if (!ttisnil(gval(n))) { + ause += countint(key2tval(n), nums); + totaluse++; + } + } + *pnasize += ause; + return totaluse; +} + + +static void setarrayvector (lua_State *L, Table *t, int size) { + int i; + luaM_reallocvector(L, t->array, t->sizearray, size, TValue); + for (i=t->sizearray; iarray[i]); + t->sizearray = size; +} + + +static void setnodevector (lua_State *L, Table *t, int size) { + int lsize; + if (size == 0) { /* no elements to hash part? */ + t->node = cast(Node *, dummynode); /* use common `dummynode' */ + lsize = 0; + } + else { + int i; + lsize = ceillog2(size); + if (lsize > MAXBITS) + luaG_runerror(L, "table overflow"); + size = twoto(lsize); + t->node = luaM_newvector(L, size, Node); + for (i=0; ilsizenode = cast_byte(lsize); + t->lastfree = gnode(t, size); /* all positions are free */ +} + + +static void resize (lua_State *L, Table *t, int nasize, int nhsize) { + int i; + int oldasize = t->sizearray; + int oldhsize = t->lsizenode; + Node *nold = t->node; /* save old hash ... */ + if (nasize > oldasize) /* array part must grow? */ + setarrayvector(L, t, nasize); + /* create new hash part with appropriate size */ + setnodevector(L, t, nhsize); + if (nasize < oldasize) { /* array part must shrink? */ + t->sizearray = nasize; + /* re-insert elements from vanishing slice */ + for (i=nasize; iarray[i])) + setobjt2t(L, luaH_setnum(L, t, i+1), &t->array[i]); + } + /* shrink array */ + luaM_reallocvector(L, t->array, oldasize, nasize, TValue); + } + /* re-insert elements from hash part */ + for (i = twoto(oldhsize) - 1; i >= 0; i--) { + Node *old = nold+i; + if (!ttisnil(gval(old))) + setobjt2t(L, luaH_set(L, t, key2tval(old)), gval(old)); + } + if (nold != dummynode) + luaM_freearray(L, nold, twoto(oldhsize), Node); /* free old array */ +} + + +void luaH_resizearray (lua_State *L, Table *t, int nasize) { + int nsize = (t->node == dummynode) ? 0 : sizenode(t); + resize(L, t, nasize, nsize); +} + + +static void rehash (lua_State *L, Table *t, const TValue *ek) { + int nasize, na; + int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */ + int i; + int totaluse; + for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */ + nasize = numusearray(t, nums); /* count keys in array part */ + totaluse = nasize; /* all those keys are integer keys */ + totaluse += numusehash(t, nums, &nasize); /* count keys in hash part */ + /* count extra key */ + nasize += countint(ek, nums); + totaluse++; + /* compute new size for array part */ + na = computesizes(nums, &nasize); + /* resize the table to new computed sizes */ + resize(L, t, nasize, totaluse - na); +} + + + +/* +** }============================================================= +*/ + + +Table *luaH_new (lua_State *L, int narray, int nhash) { + Table *t = luaM_new(L, Table); + luaC_link(L, obj2gco(t), LUA_TTABLE); + t->metatable = NULL; + t->flags = cast_byte(~0); + /* temporary values (kept only if some malloc fails) */ + t->array = NULL; + t->sizearray = 0; + t->lsizenode = 0; + t->node = cast(Node *, dummynode); + setarrayvector(L, t, narray); + setnodevector(L, t, nhash); + return t; +} + + +void luaH_free (lua_State *L, Table *t) { + if (t->node != dummynode) + luaM_freearray(L, t->node, sizenode(t), Node); + luaM_freearray(L, t->array, t->sizearray, TValue); + luaM_free(L, t); +} + + +static Node *getfreepos (Table *t) { + while (t->lastfree-- > t->node) { + if (ttisnil(gkey(t->lastfree))) + return t->lastfree; + } + return NULL; /* could not find a free place */ +} + + + +/* +** inserts a new key into a hash table; first, check whether key's main +** position is free. If not, check whether colliding node is in its main +** position or not: if it is not, move colliding node to an empty place and +** put new key in its main position; otherwise (colliding node is in its main +** position), new key goes to an empty position. +*/ +static TValue *newkey (lua_State *L, Table *t, const TValue *key) { + Node *mp = mainposition(t, key); + if (!ttisnil(gval(mp)) || mp == dummynode) { + Node *othern; + Node *n = getfreepos(t); /* get a free place */ + if (n == NULL) { /* cannot find a free place? */ + rehash(L, t, key); /* grow table */ + return luaH_set(L, t, key); /* re-insert key into grown table */ + } + lua_assert(n != dummynode); + othern = mainposition(t, key2tval(mp)); + if (othern != mp) { /* is colliding node out of its main position? */ + /* yes; move colliding node into free position */ + while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ + gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ + *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ + gnext(mp) = NULL; /* now `mp' is free */ + setnilvalue(gval(mp)); + } + else { /* colliding node is in its own main position */ + /* new node will go into free position */ + gnext(n) = gnext(mp); /* chain new position */ + gnext(mp) = n; + mp = n; + } + } + gkey(mp)->value = key->value; gkey(mp)->tt = key->tt; + luaC_barriert(L, t, key); + lua_assert(ttisnil(gval(mp))); + return gval(mp); +} + + +/* +** search function for integers +*/ +const TValue *luaH_getnum (Table *t, int key) { + /* (1 <= key && key <= t->sizearray) */ + if (cast(unsigned int, key-1) < cast(unsigned int, t->sizearray)) + return &t->array[key-1]; + else { + lua_Number nk = cast_num(key); + Node *n = hashnum(t, nk); + do { /* check whether `key' is somewhere in the chain */ + if (ttisnumber(gkey(n)) && luai_numeq(nvalue(gkey(n)), nk)) + return gval(n); /* that's it */ + else n = gnext(n); + } while (n); + return luaO_nilobject; + } +} + + +/* +** search function for strings +*/ +const TValue *luaH_getstr (Table *t, TString *key) { + Node *n = hashstr(t, key); + do { /* check whether `key' is somewhere in the chain */ + if (ttisstring(gkey(n)) && rawtsvalue(gkey(n)) == key) + return gval(n); /* that's it */ + else n = gnext(n); + } while (n); + return luaO_nilobject; +} + + +/* +** main search function +*/ +const TValue *luaH_get (Table *t, const TValue *key) { + switch (ttype(key)) { + case LUA_TNIL: return luaO_nilobject; + case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key)); + case LUA_TNUMBER: { + int k; + lua_Number n = nvalue(key); + lua_number2int(k, n); + if (luai_numeq(cast_num(k), nvalue(key))) /* index is int? */ + return luaH_getnum(t, k); /* use specialized version */ + /* else go through */ + } + default: { + Node *n = mainposition(t, key); + do { /* check whether `key' is somewhere in the chain */ + if (luaO_rawequalObj(key2tval(n), key)) + return gval(n); /* that's it */ + else n = gnext(n); + } while (n); + return luaO_nilobject; + } + } +} + + +TValue *luaH_set (lua_State *L, Table *t, const TValue *key) { + const TValue *p = luaH_get(t, key); + t->flags = 0; + if (p != luaO_nilobject) + return cast(TValue *, p); + else { + if (ttisnil(key)) luaG_runerror(L, "table index is nil"); + else if (ttisnumber(key) && luai_numisnan(nvalue(key))) + luaG_runerror(L, "table index is NaN"); + return newkey(L, t, key); + } +} + + +TValue *luaH_setnum (lua_State *L, Table *t, int key) { + const TValue *p = luaH_getnum(t, key); + if (p != luaO_nilobject) + return cast(TValue *, p); + else { + TValue k; + setnvalue(&k, cast_num(key)); + return newkey(L, t, &k); + } +} + + +TValue *luaH_setstr (lua_State *L, Table *t, TString *key) { + const TValue *p = luaH_getstr(t, key); + if (p != luaO_nilobject) + return cast(TValue *, p); + else { + TValue k; + setsvalue(L, &k, key); + return newkey(L, t, &k); + } +} + + +static int unbound_search (Table *t, unsigned int j) { + unsigned int i = j; /* i is zero or a present index */ + j++; + /* find `i' and `j' such that i is present and j is not */ + while (!ttisnil(luaH_getnum(t, j))) { + i = j; + j *= 2; + if (j > cast(unsigned int, MAX_INT)) { /* overflow? */ + /* table was built with bad purposes: resort to linear search */ + i = 1; + while (!ttisnil(luaH_getnum(t, i))) i++; + return i - 1; + } + } + /* now do a binary search between them */ + while (j - i > 1) { + unsigned int m = (i+j)/2; + if (ttisnil(luaH_getnum(t, m))) j = m; + else i = m; + } + return i; +} + + +/* +** Try to find a boundary in table `t'. A `boundary' is an integer index +** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). +*/ +int luaH_getn (Table *t) { + unsigned int j = t->sizearray; + if (j > 0 && ttisnil(&t->array[j - 1])) { + /* there is a boundary in the array part: (binary) search for it */ + unsigned int i = 0; + while (j - i > 1) { + unsigned int m = (i+j)/2; + if (ttisnil(&t->array[m - 1])) j = m; + else i = m; + } + return i; + } + /* else must find a boundary in hash part */ + else if (t->node == dummynode) /* hash part is empty? */ + return j; /* that is easy... */ + else return unbound_search(t, j); +} + + + +#if defined(LUA_DEBUG) + +Node *luaH_mainposition (const Table *t, const TValue *key) { + return mainposition(t, key); +} + +int luaH_isdummy (Node *n) { return n == dummynode; } + +#endif diff --git a/external/lua/lua/ltable.h b/external/lua/lua/ltable.h new file mode 100644 index 0000000000..f5b9d5ead0 --- /dev/null +++ b/external/lua/lua/ltable.h @@ -0,0 +1,40 @@ +/* +** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lua tables (hash) +** See Copyright Notice in lua.h +*/ + +#ifndef ltable_h +#define ltable_h + +#include "lobject.h" + + +#define gnode(t,i) (&(t)->node[i]) +#define gkey(n) (&(n)->i_key.nk) +#define gval(n) (&(n)->i_val) +#define gnext(n) ((n)->i_key.nk.next) + +#define key2tval(n) (&(n)->i_key.tvk) + + +LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); +LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); +LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); +LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); +LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); +LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); +LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); +LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); +LUAI_FUNC void luaH_free (lua_State *L, Table *t); +LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); +LUAI_FUNC int luaH_getn (Table *t); + + +#if defined(LUA_DEBUG) +LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); +LUAI_FUNC int luaH_isdummy (Node *n); +#endif + + +#endif diff --git a/external/lua/lua/ltablib.c b/external/lua/lua/ltablib.c new file mode 100644 index 0000000000..b6d9cb4ac7 --- /dev/null +++ b/external/lua/lua/ltablib.c @@ -0,0 +1,287 @@ +/* +** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $ +** Library for Table Manipulation +** See Copyright Notice in lua.h +*/ + + +#include + +#define ltablib_c +#define LUA_LIB + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + +#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) + + +static int foreachi (lua_State *L) { + int i; + int n = aux_getn(L, 1); + luaL_checktype(L, 2, LUA_TFUNCTION); + for (i=1; i <= n; i++) { + lua_pushvalue(L, 2); /* function */ + lua_pushinteger(L, i); /* 1st argument */ + lua_rawgeti(L, 1, i); /* 2nd argument */ + lua_call(L, 2, 1); + if (!lua_isnil(L, -1)) + return 1; + lua_pop(L, 1); /* remove nil result */ + } + return 0; +} + + +static int foreach (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); + luaL_checktype(L, 2, LUA_TFUNCTION); + lua_pushnil(L); /* first key */ + while (lua_next(L, 1)) { + lua_pushvalue(L, 2); /* function */ + lua_pushvalue(L, -3); /* key */ + lua_pushvalue(L, -3); /* value */ + lua_call(L, 2, 1); + if (!lua_isnil(L, -1)) + return 1; + lua_pop(L, 2); /* remove value and result */ + } + return 0; +} + + +static int maxn (lua_State *L) { + lua_Number max = 0; + luaL_checktype(L, 1, LUA_TTABLE); + lua_pushnil(L); /* first key */ + while (lua_next(L, 1)) { + lua_pop(L, 1); /* remove value */ + if (lua_type(L, -1) == LUA_TNUMBER) { + lua_Number v = lua_tonumber(L, -1); + if (v > max) max = v; + } + } + lua_pushnumber(L, max); + return 1; +} + + +static int getn (lua_State *L) { + lua_pushinteger(L, aux_getn(L, 1)); + return 1; +} + + +static int setn (lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); +#ifndef luaL_setn + luaL_setn(L, 1, luaL_checkint(L, 2)); +#else + luaL_error(L, LUA_QL("setn") " is obsolete"); +#endif + lua_pushvalue(L, 1); + return 1; +} + + +static int tinsert (lua_State *L) { + int e = aux_getn(L, 1) + 1; /* first empty element */ + int pos; /* where to insert new element */ + switch (lua_gettop(L)) { + case 2: { /* called with only 2 arguments */ + pos = e; /* insert new element at the end */ + break; + } + case 3: { + int i; + pos = luaL_checkint(L, 2); /* 2nd argument is the position */ + if (pos > e) e = pos; /* `grow' array if necessary */ + for (i = e; i > pos; i--) { /* move up elements */ + lua_rawgeti(L, 1, i-1); + lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ + } + break; + } + default: { + return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); + } + } + luaL_setn(L, 1, e); /* new size */ + lua_rawseti(L, 1, pos); /* t[pos] = v */ + return 0; +} + + +static int tremove (lua_State *L) { + int e = aux_getn(L, 1); + int pos = luaL_optint(L, 2, e); + if (!(1 <= pos && pos <= e)) /* position is outside bounds? */ + return 0; /* nothing to remove */ + luaL_setn(L, 1, e - 1); /* t.n = n-1 */ + lua_rawgeti(L, 1, pos); /* result = t[pos] */ + for ( ;pos= P */ + while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { + if (i>u) luaL_error(L, "invalid order function for sorting"); + lua_pop(L, 1); /* remove a[i] */ + } + /* repeat --j until a[j] <= P */ + while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { + if (j + +#define ltm_c +#define LUA_CORE + +#include "lua.h" + +#include "lobject.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" + + + +const char *const luaT_typenames[] = { + "nil", "boolean", "userdata", "number", + "string", "table", "function", "userdata", "thread", + "proto", "upval" +}; + + +void luaT_init (lua_State *L) { + static const char *const luaT_eventname[] = { /* ORDER TM */ + "__index", "__newindex", + "__gc", "__mode", "__eq", + "__add", "__sub", "__mul", "__div", "__mod", + "__pow", "__unm", "__len", "__lt", "__le", + "__concat", "__call" + }; + int i; + for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); + luaS_fix(G(L)->tmname[i]); /* never collect these names */ + } +} + + +/* +** function to be used with macro "fasttm": optimized for absence of +** tag methods +*/ +const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { + const TValue *tm = luaH_getstr(events, ename); + lua_assert(event <= TM_EQ); + if (ttisnil(tm)) { /* no tag method? */ + events->flags |= cast_byte(1u<metatable; + break; + case LUA_TUSERDATA: + mt = uvalue(o)->metatable; + break; + default: + mt = G(L)->mt[ttype(o)]; + } + return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); +} + diff --git a/external/lua/lua/ltm.h b/external/lua/lua/ltm.h new file mode 100644 index 0000000000..64343b781b --- /dev/null +++ b/external/lua/lua/ltm.h @@ -0,0 +1,54 @@ +/* +** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ +** Tag methods +** See Copyright Notice in lua.h +*/ + +#ifndef ltm_h +#define ltm_h + + +#include "lobject.h" + + +/* +* WARNING: if you change the order of this enumeration, +* grep "ORDER TM" +*/ +typedef enum { + TM_INDEX, + TM_NEWINDEX, + TM_GC, + TM_MODE, + TM_EQ, /* last tag method with `fast' access */ + TM_ADD, + TM_SUB, + TM_MUL, + TM_DIV, + TM_MOD, + TM_POW, + TM_UNM, + TM_LEN, + TM_LT, + TM_LE, + TM_CONCAT, + TM_CALL, + TM_N /* number of elements in the enum */ +} TMS; + + + +#define gfasttm(g,et,e) ((et) == NULL ? NULL : \ + ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) + +#define fasttm(l,et,e) gfasttm(G(l), et, e) + +LUAI_DATA const char *const luaT_typenames[]; + + +LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); +LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, + TMS event); +LUAI_FUNC void luaT_init (lua_State *L); + +#endif diff --git a/external/lua/lua/lua.c b/external/lua/lua/lua.c new file mode 100644 index 0000000000..3add3ee1c5 --- /dev/null +++ b/external/lua/lua/lua.c @@ -0,0 +1,338 @@ +/* +** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $ +** Lua stand-alone interpreter +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include +#include + +#define lua_c + +#include "lua.h" + +#include "lauxlib.h" +#include "lualib.h" + + + +static lua_State *globalL = NULL; + +static const char *progname = LUA_PROGNAME; + + + +static void lstop (lua_State *L, lua_Debug *ar) { + (void)ar; /* unused arg. */ + lua_sethook(L, NULL, 0, 0); + luaL_error(L, "interrupted!"); +} + + +static void laction (int i) { + signal(i, SIG_DFL); /* if another SIGINT happens before lstop, + terminate process (default action) */ + lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); +} + + +static void print_usage (void) { + fprintf(stderr, + "usage: %s [options] [script [args]].\n" + "Available options are:\n" + " -e stat execute string " LUA_QL("stat") "\n" + " -l name require library " LUA_QL("name") "\n" + " -i enter interactive mode after executing " LUA_QL("script") "\n" + " -v show version information\n" + " -- stop handling options\n" + " - execute stdin and stop handling options\n" + , + progname); + fflush(stderr); +} + + +static void l_message (const char *pname, const char *msg) { + if (pname) fprintf(stderr, "%s: ", pname); + fprintf(stderr, "%s\n", msg); + fflush(stderr); +} + + +static int report (lua_State *L, int status) { + if (status && !lua_isnil(L, -1)) { + const char *msg = lua_tostring(L, -1); + if (msg == NULL) msg = "(error object is not a string)"; + l_message(progname, msg); + lua_pop(L, 1); + } + return status; +} + + +static int traceback (lua_State *L) { + if (!lua_isstring(L, 1)) /* 'message' not a string? */ + return 1; /* keep it intact */ + lua_getfield(L, LUA_GLOBALSINDEX, "debug"); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + return 1; + } + lua_getfield(L, -1, "traceback"); + if (!lua_isfunction(L, -1)) { + lua_pop(L, 2); + return 1; + } + lua_pushvalue(L, 1); /* pass error message */ + lua_pushinteger(L, 2); /* skip this function and traceback */ + lua_call(L, 2, 1); /* call debug.traceback */ + return 1; +} + + +static int docall (lua_State *L, int narg, int clear) { + int status; + int base = lua_gettop(L) - narg; /* function index */ + lua_pushcfunction(L, traceback); /* push traceback function */ + lua_insert(L, base); /* put it under chunk and args */ + signal(SIGINT, laction); + status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); + signal(SIGINT, SIG_DFL); + lua_remove(L, base); /* remove traceback function */ + /* force a complete garbage collection in case of errors */ + if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); + return status; +} + + +static void print_version (void) { + l_message(NULL, LUA_RELEASE " " LUA_COPYRIGHT); +} + + +static int getargs (lua_State *L, char **argv, int n) { + int narg; + int i; + int argc = 0; + while (argv[argc]) argc++; /* count total number of arguments */ + narg = argc - (n + 1); /* number of arguments to the script */ + luaL_checkstack(L, narg + 3, "too many arguments to script"); + for (i=n+1; i < argc; i++) + lua_pushstring(L, argv[i]); + lua_createtable(L, narg, n + 1); + for (i=0; i < argc; i++) { + lua_pushstring(L, argv[i]); + lua_rawseti(L, -2, i - n); + } + return narg; +} + + +static int dofile (lua_State *L, const char *name) { + int status = luaL_loadfile(L, name) || docall(L, 0, 1); + return report(L, status); +} + + +static int dostring (lua_State *L, const char *s, const char *name) { + int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); + return report(L, status); +} + + +static int dolibrary (lua_State *L, const char *name) { + lua_getglobal(L, "require"); + lua_pushstring(L, name); + return report(L, docall(L, 1, 1)); +} + + +static const char *get_prompt (lua_State *L, int firstline) { + const char *p; + lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2"); + p = lua_tostring(L, -1); + if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2); + lua_pop(L, 1); /* remove global */ + return p; +} + + +static int incomplete (lua_State *L, int status) { + if (status == LUA_ERRSYNTAX) { + size_t lmsg; + const char *msg = lua_tolstring(L, -1, &lmsg); + const char *tp = msg + lmsg - (sizeof(LUA_QL("")) - 1); + if (strstr(msg, LUA_QL("")) == tp) { + lua_pop(L, 1); + return 1; + } + } + return 0; /* else... */ +} + + +static int pushline (lua_State *L, int firstline) { + char buffer[LUA_MAXINPUT]; + char *b = buffer; + size_t l; + const char *prmt = get_prompt(L, firstline); + if (lua_readline(L, b, prmt) == 0) + return 0; /* no input */ + l = strlen(b); + if (l > 0 && b[l-1] == '\n') /* line ends with newline? */ + b[l-1] = '\0'; /* remove it */ + if (firstline && b[0] == '=') /* first line starts with `=' ? */ + lua_pushfstring(L, "return %s", b+1); /* change it to `return' */ + else + lua_pushstring(L, b); + lua_freeline(L, b); + return 1; +} + + +static int loadline (lua_State *L) { + int status; + lua_settop(L, 0); + if (!pushline(L, 1)) + return -1; /* no input */ + for (;;) { /* repeat until gets a complete line */ + status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); + if (!incomplete(L, status)) break; /* cannot try to add lines? */ + if (!pushline(L, 0)) /* no more input? */ + return -1; + lua_pushliteral(L, "\n"); /* add a new line... */ + lua_insert(L, -2); /* ...between the two lines */ + lua_concat(L, 3); /* join them */ + } + lua_saveline(L, 1); + lua_remove(L, 1); /* remove line */ + return status; +} + + +static void dotty (lua_State *L) { + int status; + const char *oldprogname = progname; + progname = NULL; + while ((status = loadline(L)) != -1) { + if (status == 0) status = docall(L, 0, 0); + report(L, status); + if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ + lua_getglobal(L, "print"); + lua_insert(L, 1); + if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) + l_message(progname, lua_pushfstring(L, + "error calling " LUA_QL("print") " (%s)", + lua_tostring(L, -1))); + } + } + lua_settop(L, 0); /* clear stack */ + fputs("\n", stdout); + fflush(stdout); + progname = oldprogname; +} + + +static int handle_script (lua_State *L, char **argv, int n) { + int status; + const char *fname; + int narg = getargs(L, argv, n); /* collect arguments */ + lua_setglobal(L, "arg"); + fname = argv[n]; + if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) + fname = NULL; /* stdin */ + status = luaL_loadfile(L, fname); + lua_insert(L, -(narg+1)); + if (status == 0) + status = docall(L, narg, 0); + else + lua_pop(L, narg); + return report(L, status); +} + + +/* check that argument has no extra characters at the end */ +#define notail(x) {if ((x)[2] != '\0') return -1;} + + +static int collectargs (char **argv, int *pi, int *pv, int *pe) { + int i; + for (i = 1; argv[i] != NULL; i++) { + if (argv[i][0] != '-') /* not an option? */ + return i; + switch (argv[i][1]) { /* option */ + case '-': + notail(argv[i]); + return (argv[i+1] != NULL ? i+1 : 0); + case '\0': + return i; + case 'i': + notail(argv[i]); + *pi = 1; /* go through */ + case 'v': + notail(argv[i]); + *pv = 1; + break; + case 'e': + *pe = 1; /* go through */ + case 'l': + if (argv[i][2] == '\0') { + i++; + if (argv[i] == NULL) return -1; + } + break; + default: return -1; /* invalid option */ + } + } + return 0; +} + + +static int runargs (lua_State *L, char **argv, int n) { + int i; + for (i = 1; i < n; i++) { + if (argv[i] == NULL) continue; + lua_assert(argv[i][0] == '-'); + switch (argv[i][1]) { /* option */ + case 'e': { + const char *chunk = argv[i] + 2; + if (*chunk == '\0') chunk = argv[++i]; + lua_assert(chunk != NULL); + if (dostring(L, chunk, "=(command line)") != 0) + return 1; + break; + } + case 'l': { + const char *filename = argv[i] + 2; + if (*filename == '\0') filename = argv[++i]; + lua_assert(filename != NULL); + if (dolibrary(L, filename)) + return 1; /* stop if file fails */ + break; + } + default: break; + } + } + return 0; +} + + +static int handle_luainit (lua_State *L) { + const char *init = getenv(LUA_INIT); + if (init == NULL) return 0; /* status OK */ + else if (init[0] == '@') + return dofile(L, init+1); + else + return dostring(L, init, "=" LUA_INIT); +} + + +struct Smain { + int argc; + char **argv; + int status; +}; + diff --git a/external/lua/lua/lua.h b/external/lua/lua/lua.h new file mode 100644 index 0000000000..a4b73e743e --- /dev/null +++ b/external/lua/lua/lua.h @@ -0,0 +1,388 @@ +/* +** $Id: lua.h,v 1.218.1.7 2012/01/13 20:36:20 roberto Exp $ +** Lua - An Extensible Extension Language +** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** See Copyright Notice at the end of this file +*/ + + +#ifndef lua_h +#define lua_h + +#include +#include + + +#include "luaconf.h" + + +#define LUA_VERSION "Lua 5.1" +#define LUA_RELEASE "Lua 5.1.5" +#define LUA_VERSION_NUM 501 +#define LUA_COPYRIGHT "Copyright (C) 1994-2012 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" + + +/* mark for precompiled code (`Lua') */ +#define LUA_SIGNATURE "\033Lua" + +/* option for multiple returns in `lua_pcall' and `lua_call' */ +#define LUA_MULTRET (-1) + + +/* +** pseudo-indices +*/ +#define LUA_REGISTRYINDEX (-10000) +#define LUA_ENVIRONINDEX (-10001) +#define LUA_GLOBALSINDEX (-10002) +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) + + +/* thread status; 0 is OK */ +#define LUA_YIELD 1 +#define LUA_ERRRUN 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRERR 5 + + +typedef struct lua_State lua_State; + +typedef int (*lua_CFunction) (lua_State *L); + + +/* +** functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); + + +/* +** prototype for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + + +/* +** basic types +*/ +#define LUA_TNONE (-1) + +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_TFUNCTION 6 +#define LUA_TUSERDATA 7 +#define LUA_TTHREAD 8 + + + +/* minimum Lua stack available to a C function */ +#define LUA_MINSTACK 20 + + +/* +** generic extra include file +*/ +#if defined(LUA_USER_H) +#include LUA_USER_H +#endif + + +/* type of numbers in Lua */ +typedef LUA_NUMBER lua_Number; + + +/* type for integer functions */ +typedef LUA_INTEGER lua_Integer; + + + +/* +** state manipulation +*/ +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API void (lua_close) (lua_State *L); +LUA_API lua_State *(lua_newthread) (lua_State *L); + +LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); + + +/* +** basic stack manipulation +*/ +LUA_API int (lua_gettop) (lua_State *L); +LUA_API void (lua_settop) (lua_State *L, int idx); +LUA_API void (lua_pushvalue) (lua_State *L, int idx); +LUA_API void (lua_remove) (lua_State *L, int idx); +LUA_API void (lua_insert) (lua_State *L, int idx); +LUA_API void (lua_replace) (lua_State *L, int idx); +LUA_API int (lua_checkstack) (lua_State *L, int sz); + +LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); + + +/* +** access functions (stack -> C) +*/ + +LUA_API int (lua_isnumber) (lua_State *L, int idx); +LUA_API int (lua_isstring) (lua_State *L, int idx); +LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isuserdata) (lua_State *L, int idx); +LUA_API int (lua_type) (lua_State *L, int idx); +LUA_API const char *(lua_typename) (lua_State *L, int tp); + +LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); + +LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); +LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); +LUA_API int (lua_toboolean) (lua_State *L, int idx); +LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); +LUA_API size_t (lua_objlen) (lua_State *L, int idx); +LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); +LUA_API void *(lua_touserdata) (lua_State *L, int idx); +LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); +LUA_API const void *(lua_topointer) (lua_State *L, int idx); + + +/* +** push functions (C -> stack) +*/ +LUA_API void (lua_pushnil) (lua_State *L); +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); +LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); +LUA_API void (lua_pushstring) (lua_State *L, const char *s); +LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, + va_list argp); +LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); +LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); +LUA_API void (lua_pushboolean) (lua_State *L, int b); +LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); +LUA_API int (lua_pushthread) (lua_State *L); + + +/* +** get functions (Lua -> stack) +*/ +LUA_API void (lua_gettable) (lua_State *L, int idx); +LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawget) (lua_State *L, int idx); +LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); +LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); +LUA_API int (lua_getmetatable) (lua_State *L, int objindex); +LUA_API void (lua_getfenv) (lua_State *L, int idx); + + +/* +** set functions (stack -> Lua) +*/ +LUA_API void (lua_settable) (lua_State *L, int idx); +LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawset) (lua_State *L, int idx); +LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); +LUA_API int (lua_setmetatable) (lua_State *L, int objindex); +LUA_API int (lua_setfenv) (lua_State *L, int idx); + + +/* +** `load' and `call' functions (load and run Lua code) +*/ +LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); +LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); +LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); +LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname); + +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); + + +/* +** coroutine functions +*/ +LUA_API int (lua_yield) (lua_State *L, int nresults); +LUA_API int (lua_resume) (lua_State *L, int narg); +LUA_API int (lua_status) (lua_State *L); + +/* +** garbage-collection function and options +*/ + +#define LUA_GCSTOP 0 +#define LUA_GCRESTART 1 +#define LUA_GCCOLLECT 2 +#define LUA_GCCOUNT 3 +#define LUA_GCCOUNTB 4 +#define LUA_GCSTEP 5 +#define LUA_GCSETPAUSE 6 +#define LUA_GCSETSTEPMUL 7 + +LUA_API int (lua_gc) (lua_State *L, int what, int data); + + +/* +** miscellaneous functions +*/ + +LUA_API int (lua_error) (lua_State *L); + +LUA_API int (lua_next) (lua_State *L, int idx); + +LUA_API void (lua_concat) (lua_State *L, int n); + +LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define lua_pop(L,n) lua_settop(L, -(n)-1) + +#define lua_newtable(L) lua_createtable(L, 0, 0) + +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) + +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) + +#define lua_strlen(L,i) lua_objlen(L, (i)) + +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) + +#define lua_pushliteral(L, s) \ + lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) + +#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) +#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) + +#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) + + + +/* +** compatibility macros and functions +*/ + +#define lua_open() luaL_newstate() + +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) + +#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) + +#define lua_Chunkreader lua_Reader +#define lua_Chunkwriter lua_Writer + + +/* hack */ +LUA_API void lua_setlevel (lua_State *from, lua_State *to); + + +/* +** {====================================================================== +** Debug API +** ======================================================================= +*/ + + +/* +** Event codes +*/ +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILRET 4 + + +/* +** Event masks +*/ +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +typedef struct lua_Debug lua_Debug; /* activation record */ + + +/* Functions to be called by the debuger in specific events */ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); + + +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); + +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); +LUA_API lua_Hook lua_gethook (lua_State *L); +LUA_API int lua_gethookmask (lua_State *L); +LUA_API int lua_gethookcount (lua_State *L); + + +struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) `global', `local', `field', `method' */ + const char *what; /* (S) `Lua', `C', `main', `tail' */ + const char *source; /* (S) */ + int currentline; /* (l) */ + int nups; /* (u) number of upvalues */ + int linedefined; /* (S) */ + int lastlinedefined; /* (S) */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + int i_ci; /* active function */ +}; + +/* }====================================================================== */ + + +/****************************************************************************** +* Copyright (C) 1994-2012 Lua.org, PUC-Rio. All rights reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + + +#endif diff --git a/external/lua/lua/luaconf.h b/external/lua/lua/luaconf.h new file mode 100644 index 0000000000..e2cb26163a --- /dev/null +++ b/external/lua/lua/luaconf.h @@ -0,0 +1,763 @@ +/* +** $Id: luaconf.h,v 1.82.1.7 2008/02/11 16:25:08 roberto Exp $ +** Configuration file for Lua +** See Copyright Notice in lua.h +*/ + + +#ifndef lconfig_h +#define lconfig_h + +#include +#include + + +/* +** ================================================================== +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + + +/* +@@ LUA_ANSI controls the use of non-ansi features. +** CHANGE it (define it) if you want Lua to avoid the use of any +** non-ansi feature or library. +*/ +#if defined(__STRICT_ANSI__) +#define LUA_ANSI +#endif + + +#if !defined(LUA_ANSI) && defined(_WIN32) +#define LUA_WIN +#endif + +#if defined(LUA_USE_LINUX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#define LUA_USE_READLINE /* needs some extra libraries */ +#endif + +#if defined(LUA_USE_MACOSX) +#define LUA_USE_POSIX +#define LUA_DL_DYLD /* does not need extra library */ +#endif + + + +/* +@@ LUA_USE_POSIX includes all functionallity listed as X/Open System +@* Interfaces Extension (XSI). +** CHANGE it (define it) if your system is XSI compatible. +*/ +#if defined(LUA_USE_POSIX) +#define LUA_USE_MKSTEMP +#define LUA_USE_ISATTY +#define LUA_USE_POPEN +#define LUA_USE_ULONGJMP +#endif + + +/* +@@ LUA_PATH and LUA_CPATH are the names of the environment variables that +@* Lua check to set its paths. +@@ LUA_INIT is the name of the environment variable that Lua +@* checks for initialization code. +** CHANGE them if you want different names. +*/ +#define LUA_PATH "LUA_PATH" +#define LUA_CPATH "LUA_CPATH" +#define LUA_INIT "LUA_INIT" + + +/* +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for +@* Lua libraries. +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for +@* C libraries. +** CHANGE them if your machine has a non-conventional directory +** hierarchy or if you want to install your libraries in +** non-conventional directories. +*/ +#if defined(_WIN32) +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_PATH_DEFAULT \ + ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua" +#define LUA_CPATH_DEFAULT \ + ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" + +#else +#define LUA_ROOT "/usr/local/" +#define LUA_LDIR LUA_ROOT "share/lua/5.1/" +#define LUA_CDIR LUA_ROOT "lib/lua/5.1/" +#define LUA_PATH_DEFAULT \ + "./?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua" +#define LUA_CPATH_DEFAULT \ + "./?.so;" LUA_CDIR"?.so;" LUA_CDIR"loadall.so" +#endif + + +/* +@@ LUA_DIRSEP is the directory separator (for submodules). +** CHANGE it if your machine does not use "/" as the directory separator +** and is not Windows. (On Windows Lua automatically uses "\".) +*/ +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + + +/* +@@ LUA_PATHSEP is the character that separates templates in a path. +@@ LUA_PATH_MARK is the string that marks the substitution points in a +@* template. +@@ LUA_EXECDIR in a Windows path is replaced by the executable's +@* directory. +@@ LUA_IGMARK is a mark to ignore all before it when bulding the +@* luaopen_ function name. +** CHANGE them if for some reason your system cannot use those +** characters. (E.g., if one of those characters is a common character +** in file/directory names.) Probably you do not need to change them. +*/ +#define LUA_PATHSEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXECDIR "!" +#define LUA_IGMARK "-" + + +/* +@@ LUA_INTEGER is the integral type used by lua_pushinteger/lua_tointeger. +** CHANGE that if ptrdiff_t is not adequate on your machine. (On most +** machines, ptrdiff_t gives a good choice between int or long.) +*/ +#define LUA_INTEGER ptrdiff_t + + +/* +@@ LUA_API is a mark for all core API functions. +@@ LUALIB_API is a mark for all standard library functions. +** CHANGE them if you need to define those functions in some special way. +** For instance, if you want to create one Windows DLL with the core and +** the libraries, you may want to use the following definition (define +** LUA_BUILD_AS_DLL to get it). +*/ +#if defined(LUA_BUILD_AS_DLL) + +#if defined(LUA_CORE) || defined(LUA_LIB) +#define LUA_API __declspec(dllexport) +#else +#define LUA_API __declspec(dllimport) +#endif + +#else + +#define LUA_API extern + +#endif + +/* more often than not the libs go together with the core */ +#define LUALIB_API LUA_API + + +/* +@@ LUAI_FUNC is a mark for all extern functions that are not to be +@* exported to outside modules. +@@ LUAI_DATA is a mark for all extern (const) variables that are not to +@* be exported to outside modules. +** CHANGE them if you need to mark them in some special way. Elf/gcc +** (versions 3.2 and later) mark them as "hidden" to optimize access +** when Lua is compiled as a shared library. +*/ +#if defined(luaall_c) +#define LUAI_FUNC static +#define LUAI_DATA /* empty */ + +#elif defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + defined(__ELF__) +#define LUAI_FUNC __attribute__((visibility("hidden"))) extern +#define LUAI_DATA LUAI_FUNC + +#else +#define LUAI_FUNC extern +#define LUAI_DATA extern +#endif + + + +/* +@@ LUA_QL describes how error messages quote program elements. +** CHANGE it if you want a different appearance. +*/ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + + +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@* of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 + + +/* +** {================================================================== +** Stand-alone configuration +** =================================================================== +*/ + +#if defined(lua_c) || defined(luaall_c) + +/* +@@ lua_stdin_is_tty detects whether the standard input is a 'tty' (that +@* is, whether we're running lua interactively). +** CHANGE it if you have a better definition for non-POSIX/non-Windows +** systems. +*/ +#if defined(LUA_USE_ISATTY) +#include +#define lua_stdin_is_tty() isatty(0) +#elif defined(LUA_WIN) +#include +#include +#define lua_stdin_is_tty() _isatty(_fileno(stdin)) +#else +#define lua_stdin_is_tty() 1 /* assume stdin is a tty */ +#endif + + +/* +@@ LUA_PROMPT is the default prompt used by stand-alone Lua. +@@ LUA_PROMPT2 is the default continuation prompt used by stand-alone Lua. +** CHANGE them if you want different prompts. (You can also change the +** prompts dynamically, assigning to globals _PROMPT/_PROMPT2.) +*/ +#define LUA_PROMPT "> " +#define LUA_PROMPT2 ">> " + + +/* +@@ LUA_PROGNAME is the default name for the stand-alone Lua program. +** CHANGE it if your stand-alone interpreter has a different name and +** your system is not able to detect that name automatically. +*/ +#define LUA_PROGNAME "lua" + + +/* +@@ LUA_MAXINPUT is the maximum length for an input line in the +@* stand-alone interpreter. +** CHANGE it if you need longer lines. +*/ +#define LUA_MAXINPUT 512 + + +/* +@@ lua_readline defines how to show a prompt and then read a line from +@* the standard input. +@@ lua_saveline defines how to "save" a read line in a "history". +@@ lua_freeline defines how to free a line read by lua_readline. +** CHANGE them if you want to improve this functionality (e.g., by using +** GNU readline and history facilities). +*/ +#if defined(LUA_USE_READLINE) +#include +#include +#include +#define lua_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) +#define lua_saveline(L,idx) \ + if (lua_strlen(L,idx) > 0) /* non-empty line? */ \ + add_history(lua_tostring(L, idx)); /* add it to history */ +#define lua_freeline(L,b) ((void)L, free(b)) +#else +#define lua_readline(L,b,p) \ + ((void)L, fputs(p, stdout), fflush(stdout), /* show prompt */ \ + fgets(b, LUA_MAXINPUT, stdin) != NULL) /* get line */ +#define lua_saveline(L,idx) { (void)L; (void)idx; } +#define lua_freeline(L,b) { (void)L; (void)b; } +#endif + +#endif + +/* }================================================================== */ + + +/* +@@ LUAI_GCPAUSE defines the default pause between garbage-collector cycles +@* as a percentage. +** CHANGE it if you want the GC to run faster or slower (higher values +** mean larger pauses which mean slower collection.) You can also change +** this value dynamically. +*/ +#define LUAI_GCPAUSE 200 /* 200% (wait memory to double before next GC) */ + + +/* +@@ LUAI_GCMUL defines the default speed of garbage collection relative to +@* memory allocation as a percentage. +** CHANGE it if you want to change the granularity of the garbage +** collection. (Higher values mean coarser collections. 0 represents +** infinity, where each step performs a full collection.) You can also +** change this value dynamically. +*/ +#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */ + + + +/* +@@ LUA_COMPAT_GETN controls compatibility with old getn behavior. +** CHANGE it (define it) if you want exact compatibility with the +** behavior of setn/getn in Lua 5.0. +*/ +#undef LUA_COMPAT_GETN + +/* +@@ LUA_COMPAT_LOADLIB controls compatibility about global loadlib. +** CHANGE it to undefined as soon as you do not need a global 'loadlib' +** function (the function is still available as 'package.loadlib'). +*/ +#undef LUA_COMPAT_LOADLIB + +/* +@@ LUA_COMPAT_VARARG controls compatibility with old vararg feature. +** CHANGE it to undefined as soon as your programs use only '...' to +** access vararg parameters (instead of the old 'arg' table). +*/ +#define LUA_COMPAT_VARARG + +/* +@@ LUA_COMPAT_MOD controls compatibility with old math.mod function. +** CHANGE it to undefined as soon as your programs use 'math.fmod' or +** the new '%' operator instead of 'math.mod'. +*/ +#define LUA_COMPAT_MOD + +/* +@@ LUA_COMPAT_LSTR controls compatibility with old long string nesting +@* facility. +** CHANGE it to 2 if you want the old behaviour, or undefine it to turn +** off the advisory error when nesting [[...]]. +*/ +#define LUA_COMPAT_LSTR 1 + +/* +@@ LUA_COMPAT_GFIND controls compatibility with old 'string.gfind' name. +** CHANGE it to undefined as soon as you rename 'string.gfind' to +** 'string.gmatch'. +*/ +#define LUA_COMPAT_GFIND + +/* +@@ LUA_COMPAT_OPENLIB controls compatibility with old 'luaL_openlib' +@* behavior. +** CHANGE it to undefined as soon as you replace to 'luaL_register' +** your uses of 'luaL_openlib' +*/ +#define LUA_COMPAT_OPENLIB + + + +/* +@@ luai_apicheck is the assert macro used by the Lua-C API. +** CHANGE luai_apicheck if you want Lua to perform some checks in the +** parameters it gets from API calls. This may slow down the interpreter +** a bit, but may be quite useful when debugging C code that interfaces +** with Lua. A useful redefinition is to use assert.h. +*/ +#if defined(LUA_USE_APICHECK) +#include +#define luai_apicheck(L,o) { (void)L; assert(o); } +#else +#define luai_apicheck(L,o) { (void)L; } +#endif + + +/* +@@ LUAI_BITSINT defines the number of bits in an int. +** CHANGE here if Lua cannot automatically detect the number of bits of +** your machine. Probably you do not need to change this. +*/ +/* avoid overflows in comparison */ +#if INT_MAX-20 < 32760 +#define LUAI_BITSINT 16 +#elif INT_MAX > 2147483640L +/* int has at least 32 bits */ +#define LUAI_BITSINT 32 +#else +#error "you must define LUA_BITSINT with number of bits in an integer" +#endif + + +/* +@@ LUAI_UINT32 is an unsigned integer with at least 32 bits. +@@ LUAI_INT32 is an signed integer with at least 32 bits. +@@ LUAI_UMEM is an unsigned integer big enough to count the total +@* memory used by Lua. +@@ LUAI_MEM is a signed integer big enough to count the total memory +@* used by Lua. +** CHANGE here if for some weird reason the default definitions are not +** good enough for your machine. (The definitions in the 'else' +** part always works, but may waste space on machines with 64-bit +** longs.) Probably you do not need to change this. +*/ +#if LUAI_BITSINT >= 32 +#define LUAI_UINT32 unsigned int +#define LUAI_INT32 int +#define LUAI_MAXINT32 INT_MAX +#define LUAI_UMEM size_t +#define LUAI_MEM ptrdiff_t +#else +/* 16-bit ints */ +#define LUAI_UINT32 unsigned long +#define LUAI_INT32 long +#define LUAI_MAXINT32 LONG_MAX +#define LUAI_UMEM unsigned long +#define LUAI_MEM long +#endif + + +/* +@@ LUAI_MAXCALLS limits the number of nested calls. +** CHANGE it if you need really deep recursive calls. This limit is +** arbitrary; its only purpose is to stop infinite recursion before +** exhausting memory. +*/ +#define LUAI_MAXCALLS 20000 + + +/* +@@ LUAI_MAXCSTACK limits the number of Lua stack slots that a C function +@* can use. +** CHANGE it if you need lots of (Lua) stack space for your C +** functions. This limit is arbitrary; its only purpose is to stop C +** functions to consume unlimited stack space. (must be smaller than +** -LUA_REGISTRYINDEX) +*/ +#define LUAI_MAXCSTACK 8000 + + + +/* +** {================================================================== +** CHANGE (to smaller values) the following definitions if your system +** has a small C stack. (Or you may want to change them to larger +** values if your system has a large C stack and these limits are +** too rigid for you.) Some of these constants control the size of +** stack-allocated arrays used by the compiler or the interpreter, while +** others limit the maximum number of recursive calls that the compiler +** or the interpreter can perform. Values too large may cause a C stack +** overflow for some forms of deep constructs. +** =================================================================== +*/ + + +/* +@@ LUAI_MAXCCALLS is the maximum depth for nested C calls (short) and +@* syntactical nested non-terminals in a program. +*/ +#define LUAI_MAXCCALLS 200 + + +/* +@@ LUAI_MAXVARS is the maximum number of local variables per function +@* (must be smaller than 250). +*/ +#define LUAI_MAXVARS 200 + + +/* +@@ LUAI_MAXUPVALUES is the maximum number of upvalues per function +@* (must be smaller than 250). +*/ +#define LUAI_MAXUPVALUES 60 + + +/* +@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +*/ +#define LUAL_BUFFERSIZE BUFSIZ + +/* }================================================================== */ + + + + +/* +** {================================================================== +@@ LUA_NUMBER is the type of numbers in Lua. +** CHANGE the following definitions only if you want to build Lua +** with a number type different from double. You may also need to +** change lua_number2int & lua_number2integer. +** =================================================================== +*/ + +#define LUA_NUMBER_DOUBLE +#define LUA_NUMBER double + +/* +@@ LUAI_UACNUMBER is the result of an 'usual argument conversion' +@* over a number. +*/ +#define LUAI_UACNUMBER double + + +/* +@@ LUA_NUMBER_SCAN is the format for reading numbers. +@@ LUA_NUMBER_FMT is the format for writing numbers. +@@ lua_number2str converts a number to a string. +@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. +@@ lua_str2number converts a string to a number. +*/ +#define LUA_NUMBER_SCAN "%lf" +#define LUA_NUMBER_FMT "%.14g" +#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) +#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ +#define lua_str2number(s,p) strtod((s), (p)) + + +/* +@@ The luai_num* macros define the primitive operations over numbers. +*/ +#if defined(LUA_CORE) +#include +#define luai_numadd(a,b) ((a)+(b)) +#define luai_numsub(a,b) ((a)-(b)) +#define luai_nummul(a,b) ((a)*(b)) +#define luai_numdiv(a,b) ((a)/(b)) +#define luai_nummod(a,b) ((a) - floor((a)/(b))*(b)) +#define luai_numpow(a,b) (pow(a,b)) +#define luai_numunm(a) (-(a)) +#define luai_numeq(a,b) ((a)==(b)) +#define luai_numlt(a,b) ((a)<(b)) +#define luai_numle(a,b) ((a)<=(b)) +#define luai_numisnan(a) (!luai_numeq((a), (a))) +#endif + + +/* +@@ lua_number2int is a macro to convert lua_Number to int. +@@ lua_number2integer is a macro to convert lua_Number to lua_Integer. +** CHANGE them if you know a faster way to convert a lua_Number to +** int (with any rounding method and without throwing errors) in your +** system. In Pentium machines, a naive typecast from double to int +** in C is extremely slow, so any alternative is worth trying. +*/ + +/* On a Pentium, resort to a trick */ +#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && \ + (defined(__i386) || defined (_M_IX86) || defined(__i386__)) + +/* On a Microsoft compiler, use assembler */ +#if defined(_MSC_VER) + +#define lua_number2int(i,d) __asm fld d __asm fistp i +#define lua_number2integer(i,n) lua_number2int(i, n) + +/* the next trick should work on any Pentium, but sometimes clashes + with a DirectX idiosyncrasy */ +#else + +union luai_Cast { double l_d; long l_l; }; +#define lua_number2int(i,d) \ + { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; } +#define lua_number2integer(i,n) lua_number2int(i, n) + +#endif + + +/* this option always works, but may be slow */ +#else +#define lua_number2int(i,d) ((i)=(int)(d)) +#define lua_number2integer(i,d) ((i)=(lua_Integer)(d)) + +#endif + +/* }================================================================== */ + + +/* +@@ LUAI_USER_ALIGNMENT_T is a type that requires maximum alignment. +** CHANGE it if your system requires alignments larger than double. (For +** instance, if your system supports long doubles and they must be +** aligned in 16-byte boundaries, then you should add long double in the +** union.) Probably you do not need to change this. +*/ +#define LUAI_USER_ALIGNMENT_T union { double u; void *s; long l; } + + +/* +@@ LUAI_THROW/LUAI_TRY define how Lua does exception handling. +** CHANGE them if you prefer to use longjmp/setjmp even with C++ +** or if want/don't to use _longjmp/_setjmp instead of regular +** longjmp/setjmp. By default, Lua handles errors with exceptions when +** compiling as C++ code, with _longjmp/_setjmp when asked to use them, +** and with longjmp/setjmp otherwise. +*/ +#if defined(__cplusplus) +/* C++ exceptions */ +#define LUAI_THROW(L,c) throw(c) +#define LUAI_TRY(L,c,a) try { a } catch(...) \ + { if ((c)->status == 0) (c)->status = -1; } +#define luai_jmpbuf int /* dummy variable */ + +#elif defined(LUA_USE_ULONGJMP) +/* in Unix, try _longjmp/_setjmp (more efficient) */ +#define LUAI_THROW(L,c) _longjmp((c)->b, 1) +#define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } +#define luai_jmpbuf jmp_buf + +#else +/* default handling with long jumps */ +#define LUAI_THROW(L,c) longjmp((c)->b, 1) +#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } +#define luai_jmpbuf jmp_buf + +#endif + + +/* +@@ LUA_MAXCAPTURES is the maximum number of captures that a pattern +@* can do during pattern-matching. +** CHANGE it if you need more captures. This limit is arbitrary. +*/ +#define LUA_MAXCAPTURES 32 + + +/* +@@ lua_tmpnam is the function that the OS library uses to create a +@* temporary name. +@@ LUA_TMPNAMBUFSIZE is the maximum size of a name created by lua_tmpnam. +** CHANGE them if you have an alternative to tmpnam (which is considered +** insecure) or if you want the original tmpnam anyway. By default, Lua +** uses tmpnam except when POSIX is available, where it uses mkstemp. +*/ +#if defined(loslib_c) || defined(luaall_c) + +#if defined(LUA_USE_MKSTEMP) +#include +#define LUA_TMPNAMBUFSIZE 32 +#define lua_tmpnam(b,e) { \ + strcpy(b, "/tmp/lua_XXXXXX"); \ + e = mkstemp(b); \ + if (e != -1) close(e); \ + e = (e == -1); } + +#else +#define LUA_TMPNAMBUFSIZE L_tmpnam +#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); } +#endif + +#endif + + +/* +@@ lua_popen spawns a new process connected to the current one through +@* the file streams. +** CHANGE it if you have a way to implement it in your system. +*/ +#if defined(LUA_USE_POPEN) + +#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m)) +#define lua_pclose(L,file) ((void)L, (pclose(file) != -1)) + +#elif defined(LUA_WIN) + +#define lua_popen(L,c,m) ((void)L, _popen(c,m)) +#define lua_pclose(L,file) ((void)L, (_pclose(file) != -1)) + +#else + +#define lua_popen(L,c,m) ((void)((void)c, m), \ + luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0) +#define lua_pclose(L,file) ((void)((void)L, file), 0) + +#endif + +/* +@@ LUA_DL_* define which dynamic-library system Lua should use. +** CHANGE here if Lua has problems choosing the appropriate +** dynamic-library system for your platform (either Windows' DLL, Mac's +** dyld, or Unix's dlopen). If your system is some kind of Unix, there +** is a good chance that it has dlopen, so LUA_DL_DLOPEN will work for +** it. To use dlopen you also need to adapt the src/Makefile (probably +** adding -ldl to the linker options), so Lua does not select it +** automatically. (When you change the makefile to add -ldl, you must +** also add -DLUA_USE_DLOPEN.) +** If you do not want any kind of dynamic library, undefine all these +** options. +** By default, _WIN32 gets LUA_DL_DLL and MAC OS X gets LUA_DL_DYLD. +*/ +#if defined(LUA_USE_DLOPEN) +#define LUA_DL_DLOPEN +#endif + +#if defined(LUA_WIN) +#define LUA_DL_DLL +#endif + + +/* +@@ LUAI_EXTRASPACE allows you to add user-specific data in a lua_State +@* (the data goes just *before* the lua_State pointer). +** CHANGE (define) this if you really need that. This value must be +** a multiple of the maximum alignment required for your machine. +*/ +#define LUAI_EXTRASPACE 0 + + +/* +@@ luai_userstate* allow user-specific actions on threads. +** CHANGE them if you defined LUAI_EXTRASPACE and need to do something +** extra when a thread is created/deleted/resumed/yielded. +*/ +#define luai_userstateopen(L) ((void)L) +#define luai_userstateclose(L) ((void)L) +#define luai_userstatethread(L,L1) ((void)L) +#define luai_userstatefree(L) ((void)L) +#define luai_userstateresume(L,n) ((void)L) +#define luai_userstateyield(L,n) ((void)L) + + +/* +@@ LUA_INTFRMLEN is the length modifier for integer conversions +@* in 'string.format'. +@@ LUA_INTFRM_T is the integer type correspoding to the previous length +@* modifier. +** CHANGE them if your system supports long long or does not support long. +*/ + +#if defined(LUA_USELONGLONG) + +#define LUA_INTFRMLEN "ll" +#define LUA_INTFRM_T long long + +#else + +#define LUA_INTFRMLEN "l" +#define LUA_INTFRM_T long + +#endif + + + +/* =================================================================== */ + +/* +** Local configuration. You can use this space to add your redefinitions +** without modifying the main part of the file. +*/ + + + +#endif + diff --git a/external/lua/lua/lualib.h b/external/lua/lua/lualib.h new file mode 100644 index 0000000000..469417f670 --- /dev/null +++ b/external/lua/lua/lualib.h @@ -0,0 +1,53 @@ +/* +** $Id: lualib.h,v 1.36.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lua standard libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lualib_h +#define lualib_h + +#include "lua.h" + + +/* Key to file-handle type */ +#define LUA_FILEHANDLE "FILE*" + + +#define LUA_COLIBNAME "coroutine" +LUALIB_API int (luaopen_base) (lua_State *L); + +#define LUA_TABLIBNAME "table" +LUALIB_API int (luaopen_table) (lua_State *L); + +#define LUA_IOLIBNAME "io" +LUALIB_API int (luaopen_io) (lua_State *L); + +#define LUA_OSLIBNAME "os" +LUALIB_API int (luaopen_os) (lua_State *L); + +#define LUA_STRLIBNAME "string" +LUALIB_API int (luaopen_string) (lua_State *L); + +#define LUA_MATHLIBNAME "math" +LUALIB_API int (luaopen_math) (lua_State *L); + +#define LUA_DBLIBNAME "debug" +LUALIB_API int (luaopen_debug) (lua_State *L); + +#define LUA_LOADLIBNAME "package" +LUALIB_API int (luaopen_package) (lua_State *L); + + +/* open all previous libraries */ +LUALIB_API void (luaL_openlibs) (lua_State *L); + + + +#ifndef lua_assert +#define lua_assert(x) ((void)0) +#endif + + +#endif diff --git a/external/lua/lua/lundump.c b/external/lua/lua/lundump.c new file mode 100644 index 0000000000..8010a45795 --- /dev/null +++ b/external/lua/lua/lundump.c @@ -0,0 +1,227 @@ +/* +** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ +** load precompiled Lua chunks +** See Copyright Notice in lua.h +*/ + +#include + +#define lundump_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lmem.h" +#include "lobject.h" +#include "lstring.h" +#include "lundump.h" +#include "lzio.h" + +typedef struct { + lua_State* L; + ZIO* Z; + Mbuffer* b; + const char* name; +} LoadState; + +#ifdef LUAC_TRUST_BINARIES +#define IF(c,s) +#define error(S,s) +#else +#define IF(c,s) if (c) error(S,s) + +static void error(LoadState* S, const char* why) +{ + luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); + luaD_throw(S->L,LUA_ERRSYNTAX); +} +#endif + +#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) +#define LoadByte(S) (lu_byte)LoadChar(S) +#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) +#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) + +static void LoadBlock(LoadState* S, void* b, size_t size) +{ + size_t r=luaZ_read(S->Z,b,size); + IF (r!=0, "unexpected end"); +} + +static int LoadChar(LoadState* S) +{ + char x; + LoadVar(S,x); + return x; +} + +static int LoadInt(LoadState* S) +{ + int x; + LoadVar(S,x); + IF (x<0, "bad integer"); + return x; +} + +static lua_Number LoadNumber(LoadState* S) +{ + lua_Number x; + LoadVar(S,x); + return x; +} + +static TString* LoadString(LoadState* S) +{ + size_t size; + LoadVar(S,size); + if (size==0) + return NULL; + else + { + char* s=luaZ_openspace(S->L,S->b,size); + LoadBlock(S,s,size); + return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ + } +} + +static void LoadCode(LoadState* S, Proto* f) +{ + int n=LoadInt(S); + f->code=luaM_newvector(S->L,n,Instruction); + f->sizecode=n; + LoadVector(S,f->code,n,sizeof(Instruction)); +} + +static Proto* LoadFunction(LoadState* S, TString* p); + +static void LoadConstants(LoadState* S, Proto* f) +{ + int i,n; + n=LoadInt(S); + f->k=luaM_newvector(S->L,n,TValue); + f->sizek=n; + for (i=0; ik[i]); + for (i=0; ik[i]; + int t=LoadChar(S); + switch (t) + { + case LUA_TNIL: + setnilvalue(o); + break; + case LUA_TBOOLEAN: + setbvalue(o,LoadChar(S)!=0); + break; + case LUA_TNUMBER: + setnvalue(o,LoadNumber(S)); + break; + case LUA_TSTRING: + setsvalue2n(S->L,o,LoadString(S)); + break; + default: + error(S,"bad constant"); + break; + } + } + n=LoadInt(S); + f->p=luaM_newvector(S->L,n,Proto*); + f->sizep=n; + for (i=0; ip[i]=NULL; + for (i=0; ip[i]=LoadFunction(S,f->source); +} + +static void LoadDebug(LoadState* S, Proto* f) +{ + int i,n; + n=LoadInt(S); + f->lineinfo=luaM_newvector(S->L,n,int); + f->sizelineinfo=n; + LoadVector(S,f->lineinfo,n,sizeof(int)); + n=LoadInt(S); + f->locvars=luaM_newvector(S->L,n,LocVar); + f->sizelocvars=n; + for (i=0; ilocvars[i].varname=NULL; + for (i=0; ilocvars[i].varname=LoadString(S); + f->locvars[i].startpc=LoadInt(S); + f->locvars[i].endpc=LoadInt(S); + } + n=LoadInt(S); + f->upvalues=luaM_newvector(S->L,n,TString*); + f->sizeupvalues=n; + for (i=0; iupvalues[i]=NULL; + for (i=0; iupvalues[i]=LoadString(S); +} + +static Proto* LoadFunction(LoadState* S, TString* p) +{ + Proto* f; + if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); + f=luaF_newproto(S->L); + setptvalue2s(S->L,S->L->top,f); incr_top(S->L); + f->source=LoadString(S); if (f->source==NULL) f->source=p; + f->linedefined=LoadInt(S); + f->lastlinedefined=LoadInt(S); + f->nups=LoadByte(S); + f->numparams=LoadByte(S); + f->is_vararg=LoadByte(S); + f->maxstacksize=LoadByte(S); + LoadCode(S,f); + LoadConstants(S,f); + LoadDebug(S,f); + IF (!luaG_checkcode(f), "bad code"); + S->L->top--; + S->L->nCcalls--; + return f; +} + +static void LoadHeader(LoadState* S) +{ + char h[LUAC_HEADERSIZE]; + char s[LUAC_HEADERSIZE]; + luaU_header(h); + LoadBlock(S,s,LUAC_HEADERSIZE); + IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); +} + +/* +** load precompiled chunk +*/ +Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) +{ + LoadState S; + if (*name=='@' || *name=='=') + S.name=name+1; + else if (*name==LUA_SIGNATURE[0]) + S.name="binary string"; + else + S.name=name; + S.L=L; + S.Z=Z; + S.b=buff; + LoadHeader(&S); + return LoadFunction(&S,luaS_newliteral(L,"=?")); +} + +/* +* make header +*/ +void luaU_header (char* h) +{ + int x=1; + memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); + h+=sizeof(LUA_SIGNATURE)-1; + *h++=(char)LUAC_VERSION; + *h++=(char)LUAC_FORMAT; + *h++=(char)*(char*)&x; /* endianness */ + *h++=(char)sizeof(int); + *h++=(char)sizeof(size_t); + *h++=(char)sizeof(Instruction); + *h++=(char)sizeof(lua_Number); + *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ +} diff --git a/external/lua/lua/lundump.h b/external/lua/lua/lundump.h new file mode 100644 index 0000000000..c80189dbff --- /dev/null +++ b/external/lua/lua/lundump.h @@ -0,0 +1,36 @@ +/* +** $Id: lundump.h,v 1.37.1.1 2007/12/27 13:02:25 roberto Exp $ +** load precompiled Lua chunks +** See Copyright Notice in lua.h +*/ + +#ifndef lundump_h +#define lundump_h + +#include "lobject.h" +#include "lzio.h" + +/* load one chunk; from lundump.c */ +LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); + +/* make header; from lundump.c */ +LUAI_FUNC void luaU_header (char* h); + +/* dump one chunk; from ldump.c */ +LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); + +#ifdef luac_c +/* print one chunk; from print.c */ +LUAI_FUNC void luaU_print (const Proto* f, int full); +#endif + +/* for header of binary files -- this is Lua 5.1 */ +#define LUAC_VERSION 0x51 + +/* for header of binary files -- this is the official format */ +#define LUAC_FORMAT 0 + +/* size of header of binary files */ +#define LUAC_HEADERSIZE 12 + +#endif diff --git a/external/lua/lua/lvm.c b/external/lua/lua/lvm.c new file mode 100644 index 0000000000..e0a0cd8521 --- /dev/null +++ b/external/lua/lua/lvm.c @@ -0,0 +1,767 @@ +/* +** $Id: lvm.c,v 2.63.1.5 2011/08/17 20:43:11 roberto Exp $ +** Lua virtual machine +** See Copyright Notice in lua.h +*/ + + +#include +#include +#include + +#define lvm_c +#define LUA_CORE + +#include "lua.h" + +#include "ldebug.h" +#include "ldo.h" +#include "lfunc.h" +#include "lgc.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lstate.h" +#include "lstring.h" +#include "ltable.h" +#include "ltm.h" +#include "lvm.h" + + + +/* limit for table tag-method chains (to avoid loops) */ +#define MAXTAGLOOP 100 + + +const TValue *luaV_tonumber (const TValue *obj, TValue *n) { + lua_Number num; + if (ttisnumber(obj)) return obj; + if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) { + setnvalue(n, num); + return n; + } + else + return NULL; +} + + +int luaV_tostring (lua_State *L, StkId obj) { + if (!ttisnumber(obj)) + return 0; + else { + char s[LUAI_MAXNUMBER2STR]; + lua_Number n = nvalue(obj); + lua_number2str(s, n); + setsvalue2s(L, obj, luaS_new(L, s)); + return 1; + } +} + + +static void traceexec (lua_State *L, const Instruction *pc) { + lu_byte mask = L->hookmask; + const Instruction *oldpc = L->savedpc; + L->savedpc = pc; + if ((mask & LUA_MASKCOUNT) && L->hookcount == 0) { + resethookcount(L); + luaD_callhook(L, LUA_HOOKCOUNT, -1); + } + if (mask & LUA_MASKLINE) { + Proto *p = ci_func(L->ci)->l.p; + int npc = pcRel(pc, p); + int newline = getline(p, npc); + /* call linehook when enter a new function, when jump back (loop), + or when enter a new line */ + if (npc == 0 || pc <= oldpc || newline != getline(p, pcRel(oldpc, p))) + luaD_callhook(L, LUA_HOOKLINE, newline); + } +} + + +static void callTMres (lua_State *L, StkId res, const TValue *f, + const TValue *p1, const TValue *p2) { + ptrdiff_t result = savestack(L, res); + setobj2s(L, L->top, f); /* push function */ + setobj2s(L, L->top+1, p1); /* 1st argument */ + setobj2s(L, L->top+2, p2); /* 2nd argument */ + luaD_checkstack(L, 3); + L->top += 3; + luaD_call(L, L->top - 3, 1); + res = restorestack(L, result); + L->top--; + setobjs2s(L, res, L->top); +} + + + +static void callTM (lua_State *L, const TValue *f, const TValue *p1, + const TValue *p2, const TValue *p3) { + setobj2s(L, L->top, f); /* push function */ + setobj2s(L, L->top+1, p1); /* 1st argument */ + setobj2s(L, L->top+2, p2); /* 2nd argument */ + setobj2s(L, L->top+3, p3); /* 3th argument */ + luaD_checkstack(L, 4); + L->top += 4; + luaD_call(L, L->top - 4, 0); +} + + +void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { + int loop; + for (loop = 0; loop < MAXTAGLOOP; loop++) { + const TValue *tm; + if (ttistable(t)) { /* `t' is a table? */ + Table *h = hvalue(t); + const TValue *res = luaH_get(h, key); /* do a primitive get */ + if (!ttisnil(res) || /* result is no nil? */ + (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ + setobj2s(L, val, res); + return; + } + /* else will try the tag method */ + } + else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) + luaG_typeerror(L, t, "index"); + if (ttisfunction(tm)) { + callTMres(L, val, tm, t, key); + return; + } + t = tm; /* else repeat with `tm' */ + } + luaG_runerror(L, "loop in gettable"); +} + + +void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { + int loop; + TValue temp; + for (loop = 0; loop < MAXTAGLOOP; loop++) { + const TValue *tm; + if (ttistable(t)) { /* `t' is a table? */ + Table *h = hvalue(t); + TValue *oldval = luaH_set(L, h, key); /* do a primitive set */ + if (!ttisnil(oldval) || /* result is no nil? */ + (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ + setobj2t(L, oldval, val); + h->flags = 0; + luaC_barriert(L, h, val); + return; + } + /* else will try the tag method */ + } + else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) + luaG_typeerror(L, t, "index"); + if (ttisfunction(tm)) { + callTM(L, tm, t, key, val); + return; + } + /* else repeat with `tm' */ + setobj(L, &temp, tm); /* avoid pointing inside table (may rehash) */ + t = &temp; + } + luaG_runerror(L, "loop in settable"); +} + + +static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, + StkId res, TMS event) { + const TValue *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ + if (ttisnil(tm)) + tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ + if (ttisnil(tm)) return 0; + callTMres(L, res, tm, p1, p2); + return 1; +} + + +static const TValue *get_compTM (lua_State *L, Table *mt1, Table *mt2, + TMS event) { + const TValue *tm1 = fasttm(L, mt1, event); + const TValue *tm2; + if (tm1 == NULL) return NULL; /* no metamethod */ + if (mt1 == mt2) return tm1; /* same metatables => same metamethods */ + tm2 = fasttm(L, mt2, event); + if (tm2 == NULL) return NULL; /* no metamethod */ + if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */ + return tm1; + return NULL; +} + + +static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2, + TMS event) { + const TValue *tm1 = luaT_gettmbyobj(L, p1, event); + const TValue *tm2; + if (ttisnil(tm1)) return -1; /* no metamethod? */ + tm2 = luaT_gettmbyobj(L, p2, event); + if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */ + return -1; + callTMres(L, L->top, tm1, p1, p2); + return !l_isfalse(L->top); +} + + +static int l_strcmp (const TString *ls, const TString *rs) { + const char *l = getstr(ls); + size_t ll = ls->tsv.len; + const char *r = getstr(rs); + size_t lr = rs->tsv.len; + for (;;) { + int temp = strcoll(l, r); + if (temp != 0) return temp; + else { /* strings are equal up to a `\0' */ + size_t len = strlen(l); /* index of first `\0' in both strings */ + if (len == lr) /* r is finished? */ + return (len == ll) ? 0 : 1; + else if (len == ll) /* l is finished? */ + return -1; /* l is smaller than r (because r is not finished) */ + /* both strings longer than `len'; go on comparing (after the `\0') */ + len++; + l += len; ll -= len; r += len; lr -= len; + } + } +} + + +int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) { + int res; + if (ttype(l) != ttype(r)) + return luaG_ordererror(L, l, r); + else if (ttisnumber(l)) + return luai_numlt(nvalue(l), nvalue(r)); + else if (ttisstring(l)) + return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0; + else if ((res = call_orderTM(L, l, r, TM_LT)) != -1) + return res; + return luaG_ordererror(L, l, r); +} + + +static int lessequal (lua_State *L, const TValue *l, const TValue *r) { + int res; + if (ttype(l) != ttype(r)) + return luaG_ordererror(L, l, r); + else if (ttisnumber(l)) + return luai_numle(nvalue(l), nvalue(r)); + else if (ttisstring(l)) + return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0; + else if ((res = call_orderTM(L, l, r, TM_LE)) != -1) /* first try `le' */ + return res; + else if ((res = call_orderTM(L, r, l, TM_LT)) != -1) /* else try `lt' */ + return !res; + return luaG_ordererror(L, l, r); +} + + +int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2) { + const TValue *tm; + lua_assert(ttype(t1) == ttype(t2)); + switch (ttype(t1)) { + case LUA_TNIL: return 1; + case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2)); + case LUA_TBOOLEAN: return bvalue(t1) == bvalue(t2); /* true must be 1 !! */ + case LUA_TLIGHTUSERDATA: return pvalue(t1) == pvalue(t2); + case LUA_TUSERDATA: { + if (uvalue(t1) == uvalue(t2)) return 1; + tm = get_compTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, + TM_EQ); + break; /* will try TM */ + } + case LUA_TTABLE: { + if (hvalue(t1) == hvalue(t2)) return 1; + tm = get_compTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ); + break; /* will try TM */ + } + default: return gcvalue(t1) == gcvalue(t2); + } + if (tm == NULL) return 0; /* no TM? */ + callTMres(L, L->top, tm, t1, t2); /* call TM */ + return !l_isfalse(L->top); +} + + +void luaV_concat (lua_State *L, int total, int last) { + do { + StkId top = L->base + last + 1; + int n = 2; /* number of elements handled in this pass (at least 2) */ + if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) { + if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) + luaG_concaterror(L, top-2, top-1); + } else if (tsvalue(top-1)->len == 0) /* second op is empty? */ + (void)tostring(L, top - 2); /* result is first op (as string) */ + else { + /* at least two string values; get as many as possible */ + size_t tl = tsvalue(top-1)->len; + char *buffer; + int i; + /* collect total length */ + for (n = 1; n < total && tostring(L, top-n-1); n++) { + size_t l = tsvalue(top-n-1)->len; + if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); + tl += l; + } + buffer = luaZ_openspace(L, &G(L)->buff, tl); + tl = 0; + for (i=n; i>0; i--) { /* concat all strings */ + size_t l = tsvalue(top-i)->len; + memcpy(buffer+tl, svalue(top-i), l); + tl += l; + } + setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl)); + } + total -= n-1; /* got `n' strings to create 1 new */ + last -= n-1; + } while (total > 1); /* repeat until only 1 result left */ +} + + +static void Arith (lua_State *L, StkId ra, const TValue *rb, + const TValue *rc, TMS op) { + TValue tempb, tempc; + const TValue *b, *c; + if ((b = luaV_tonumber(rb, &tempb)) != NULL && + (c = luaV_tonumber(rc, &tempc)) != NULL) { + lua_Number nb = nvalue(b), nc = nvalue(c); + switch (op) { + case TM_ADD: setnvalue(ra, luai_numadd(nb, nc)); break; + case TM_SUB: setnvalue(ra, luai_numsub(nb, nc)); break; + case TM_MUL: setnvalue(ra, luai_nummul(nb, nc)); break; + case TM_DIV: setnvalue(ra, luai_numdiv(nb, nc)); break; + case TM_MOD: setnvalue(ra, luai_nummod(nb, nc)); break; + case TM_POW: setnvalue(ra, luai_numpow(nb, nc)); break; + case TM_UNM: setnvalue(ra, luai_numunm(nb)); break; + default: lua_assert(0); break; + } + } + else if (!call_binTM(L, rb, rc, ra, op)) + luaG_aritherror(L, rb, rc); +} + + + +/* +** some macros for common tasks in `luaV_execute' +*/ + +#define runtime_check(L, c) { if (!(c)) break; } + +#define RA(i) (base+GETARG_A(i)) +/* to be used after possible stack reallocation */ +#define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i)) +#define RC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgR, base+GETARG_C(i)) +#define RKB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, \ + ISK(GETARG_B(i)) ? k+INDEXK(GETARG_B(i)) : base+GETARG_B(i)) +#define RKC(i) check_exp(getCMode(GET_OPCODE(i)) == OpArgK, \ + ISK(GETARG_C(i)) ? k+INDEXK(GETARG_C(i)) : base+GETARG_C(i)) +#define KBx(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgK, k+GETARG_Bx(i)) + + +#define dojump(L,pc,i) {(pc) += (i); luai_threadyield(L);} + + +#define Protect(x) { L->savedpc = pc; {x;}; base = L->base; } + + +#define arith_op(op,tm) { \ + TValue *rb = RKB(i); \ + TValue *rc = RKC(i); \ + if (ttisnumber(rb) && ttisnumber(rc)) { \ + lua_Number nb = nvalue(rb), nc = nvalue(rc); \ + setnvalue(ra, op(nb, nc)); \ + } \ + else \ + Protect(Arith(L, ra, rb, rc, tm)); \ + } + + + +void luaV_execute (lua_State *L, int nexeccalls) { + LClosure *cl; + StkId base; + TValue *k; + const Instruction *pc; + reentry: /* entry point */ + lua_assert(isLua(L->ci)); + pc = L->savedpc; + cl = &clvalue(L->ci->func)->l; + base = L->base; + k = cl->p->k; + /* main loop of interpreter */ + for (;;) { + const Instruction i = *pc++; + StkId ra; + if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) && + (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) { + traceexec(L, pc); + if (L->status == LUA_YIELD) { /* did hook yield? */ + L->savedpc = pc - 1; + return; + } + base = L->base; + } + /* warning!! several calls may realloc the stack and invalidate `ra' */ + ra = RA(i); + lua_assert(base == L->base && L->base == L->ci->base); + lua_assert(base <= L->top && L->top <= L->stack + L->stacksize); + lua_assert(L->top == L->ci->top || luaG_checkopenop(i)); + switch (GET_OPCODE(i)) { + case OP_MOVE: { + setobjs2s(L, ra, RB(i)); + continue; + } + case OP_LOADK: { + setobj2s(L, ra, KBx(i)); + continue; + } + case OP_LOADBOOL: { + setbvalue(ra, GETARG_B(i)); + if (GETARG_C(i)) pc++; /* skip next instruction (if C) */ + continue; + } + case OP_LOADNIL: { + TValue *rb = RB(i); + do { + setnilvalue(rb--); + } while (rb >= ra); + continue; + } + case OP_GETUPVAL: { + int b = GETARG_B(i); + setobj2s(L, ra, cl->upvals[b]->v); + continue; + } + case OP_GETGLOBAL: { + TValue g; + TValue *rb = KBx(i); + sethvalue(L, &g, cl->env); + lua_assert(ttisstring(rb)); + Protect(luaV_gettable(L, &g, rb, ra)); + continue; + } + case OP_GETTABLE: { + Protect(luaV_gettable(L, RB(i), RKC(i), ra)); + continue; + } + case OP_SETGLOBAL: { + TValue g; + sethvalue(L, &g, cl->env); + lua_assert(ttisstring(KBx(i))); + Protect(luaV_settable(L, &g, KBx(i), ra)); + continue; + } + case OP_SETUPVAL: { + UpVal *uv = cl->upvals[GETARG_B(i)]; + setobj(L, uv->v, ra); + luaC_barrier(L, uv, ra); + continue; + } + case OP_SETTABLE: { + Protect(luaV_settable(L, ra, RKB(i), RKC(i))); + continue; + } + case OP_NEWTABLE: { + int b = GETARG_B(i); + int c = GETARG_C(i); + sethvalue(L, ra, luaH_new(L, luaO_fb2int(b), luaO_fb2int(c))); + Protect(luaC_checkGC(L)); + continue; + } + case OP_SELF: { + StkId rb = RB(i); + setobjs2s(L, ra+1, rb); + Protect(luaV_gettable(L, rb, RKC(i), ra)); + continue; + } + case OP_ADD: { + arith_op(luai_numadd, TM_ADD); + continue; + } + case OP_SUB: { + arith_op(luai_numsub, TM_SUB); + continue; + } + case OP_MUL: { + arith_op(luai_nummul, TM_MUL); + continue; + } + case OP_DIV: { + arith_op(luai_numdiv, TM_DIV); + continue; + } + case OP_MOD: { + arith_op(luai_nummod, TM_MOD); + continue; + } + case OP_POW: { + arith_op(luai_numpow, TM_POW); + continue; + } + case OP_UNM: { + TValue *rb = RB(i); + if (ttisnumber(rb)) { + lua_Number nb = nvalue(rb); + setnvalue(ra, luai_numunm(nb)); + } + else { + Protect(Arith(L, ra, rb, rb, TM_UNM)); + } + continue; + } + case OP_NOT: { + int res = l_isfalse(RB(i)); /* next assignment may change this value */ + setbvalue(ra, res); + continue; + } + case OP_LEN: { + const TValue *rb = RB(i); + switch (ttype(rb)) { + case LUA_TTABLE: { + setnvalue(ra, cast_num(luaH_getn(hvalue(rb)))); + break; + } + case LUA_TSTRING: { + setnvalue(ra, cast_num(tsvalue(rb)->len)); + break; + } + default: { /* try metamethod */ + Protect( + if (!call_binTM(L, rb, luaO_nilobject, ra, TM_LEN)) + luaG_typeerror(L, rb, "get length of"); + ) + } + } + continue; + } + case OP_CONCAT: { + int b = GETARG_B(i); + int c = GETARG_C(i); + Protect(luaV_concat(L, c-b+1, c); luaC_checkGC(L)); + setobjs2s(L, RA(i), base+b); + continue; + } + case OP_JMP: { + dojump(L, pc, GETARG_sBx(i)); + continue; + } + case OP_EQ: { + TValue *rb = RKB(i); + TValue *rc = RKC(i); + Protect( + if (equalobj(L, rb, rc) == GETARG_A(i)) + dojump(L, pc, GETARG_sBx(*pc)); + ) + pc++; + continue; + } + case OP_LT: { + Protect( + if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i)) + dojump(L, pc, GETARG_sBx(*pc)); + ) + pc++; + continue; + } + case OP_LE: { + Protect( + if (lessequal(L, RKB(i), RKC(i)) == GETARG_A(i)) + dojump(L, pc, GETARG_sBx(*pc)); + ) + pc++; + continue; + } + case OP_TEST: { + if (l_isfalse(ra) != GETARG_C(i)) + dojump(L, pc, GETARG_sBx(*pc)); + pc++; + continue; + } + case OP_TESTSET: { + TValue *rb = RB(i); + if (l_isfalse(rb) != GETARG_C(i)) { + setobjs2s(L, ra, rb); + dojump(L, pc, GETARG_sBx(*pc)); + } + pc++; + continue; + } + case OP_CALL: { + int b = GETARG_B(i); + int nresults = GETARG_C(i) - 1; + if (b != 0) L->top = ra+b; /* else previous instruction set top */ + L->savedpc = pc; + switch (luaD_precall(L, ra, nresults)) { + case PCRLUA: { + nexeccalls++; + goto reentry; /* restart luaV_execute over new Lua function */ + } + case PCRC: { + /* it was a C function (`precall' called it); adjust results */ + if (nresults >= 0) L->top = L->ci->top; + base = L->base; + continue; + } + default: { + return; /* yield */ + } + } + } + case OP_TAILCALL: { + int b = GETARG_B(i); + if (b != 0) L->top = ra+b; /* else previous instruction set top */ + L->savedpc = pc; + lua_assert(GETARG_C(i) - 1 == LUA_MULTRET); + switch (luaD_precall(L, ra, LUA_MULTRET)) { + case PCRLUA: { + /* tail call: put new frame in place of previous one */ + CallInfo *ci = L->ci - 1; /* previous frame */ + int aux; + StkId func = ci->func; + StkId pfunc = (ci+1)->func; /* previous function index */ + if (L->openupval) luaF_close(L, ci->base); + L->base = ci->base = ci->func + ((ci+1)->base - pfunc); + for (aux = 0; pfunc+aux < L->top; aux++) /* move frame down */ + setobjs2s(L, func+aux, pfunc+aux); + ci->top = L->top = func+aux; /* correct top */ + lua_assert(L->top == L->base + clvalue(func)->l.p->maxstacksize); + ci->savedpc = L->savedpc; + ci->tailcalls++; /* one more call lost */ + L->ci--; /* remove new frame */ + goto reentry; + } + case PCRC: { /* it was a C function (`precall' called it) */ + base = L->base; + continue; + } + default: { + return; /* yield */ + } + } + } + case OP_RETURN: { + int b = GETARG_B(i); + if (b != 0) L->top = ra+b-1; + if (L->openupval) luaF_close(L, base); + L->savedpc = pc; + b = luaD_poscall(L, ra); + if (--nexeccalls == 0) /* was previous function running `here'? */ + return; /* no: return */ + else { /* yes: continue its execution */ + if (b) L->top = L->ci->top; + lua_assert(isLua(L->ci)); + lua_assert(GET_OPCODE(*((L->ci)->savedpc - 1)) == OP_CALL); + goto reentry; + } + } + case OP_FORLOOP: { + lua_Number step = nvalue(ra+2); + lua_Number idx = luai_numadd(nvalue(ra), step); /* increment index */ + lua_Number limit = nvalue(ra+1); + if (luai_numlt(0, step) ? luai_numle(idx, limit) + : luai_numle(limit, idx)) { + dojump(L, pc, GETARG_sBx(i)); /* jump back */ + setnvalue(ra, idx); /* update internal index... */ + setnvalue(ra+3, idx); /* ...and external index */ + } + continue; + } + case OP_FORPREP: { + const TValue *init = ra; + const TValue *plimit = ra+1; + const TValue *pstep = ra+2; + L->savedpc = pc; /* next steps may throw errors */ + if (!tonumber(init, ra)) + luaG_runerror(L, LUA_QL("for") " initial value must be a number"); + else if (!tonumber(plimit, ra+1)) + luaG_runerror(L, LUA_QL("for") " limit must be a number"); + else if (!tonumber(pstep, ra+2)) + luaG_runerror(L, LUA_QL("for") " step must be a number"); + setnvalue(ra, luai_numsub(nvalue(ra), nvalue(pstep))); + dojump(L, pc, GETARG_sBx(i)); + continue; + } + case OP_TFORLOOP: { + StkId cb = ra + 3; /* call base */ + setobjs2s(L, cb+2, ra+2); + setobjs2s(L, cb+1, ra+1); + setobjs2s(L, cb, ra); + L->top = cb+3; /* func. + 2 args (state and index) */ + Protect(luaD_call(L, cb, GETARG_C(i))); + L->top = L->ci->top; + cb = RA(i) + 3; /* previous call may change the stack */ + if (!ttisnil(cb)) { /* continue loop? */ + setobjs2s(L, cb-1, cb); /* save control variable */ + dojump(L, pc, GETARG_sBx(*pc)); /* jump back */ + } + pc++; + continue; + } + case OP_SETLIST: { + int n = GETARG_B(i); + int c = GETARG_C(i); + int last; + Table *h; + if (n == 0) { + n = cast_int(L->top - ra) - 1; + L->top = L->ci->top; + } + if (c == 0) c = cast_int(*pc++); + runtime_check(L, ttistable(ra)); + h = hvalue(ra); + last = ((c-1)*LFIELDS_PER_FLUSH) + n; + if (last > h->sizearray) /* needs more space? */ + luaH_resizearray(L, h, last); /* pre-alloc it at once */ + for (; n > 0; n--) { + TValue *val = ra+n; + setobj2t(L, luaH_setnum(L, h, last--), val); + luaC_barriert(L, h, val); + } + continue; + } + case OP_CLOSE: { + luaF_close(L, ra); + continue; + } + case OP_CLOSURE: { + Proto *p; + Closure *ncl; + int nup, j; + p = cl->p->p[GETARG_Bx(i)]; + nup = p->nups; + ncl = luaF_newLclosure(L, nup, cl->env); + ncl->l.p = p; + for (j=0; jl.upvals[j] = cl->upvals[GETARG_B(*pc)]; + else { + lua_assert(GET_OPCODE(*pc) == OP_MOVE); + ncl->l.upvals[j] = luaF_findupval(L, base + GETARG_B(*pc)); + } + } + setclvalue(L, ra, ncl); + Protect(luaC_checkGC(L)); + continue; + } + case OP_VARARG: { + int b = GETARG_B(i) - 1; + int j; + CallInfo *ci = L->ci; + int n = cast_int(ci->base - ci->func) - cl->p->numparams - 1; + if (b == LUA_MULTRET) { + Protect(luaD_checkstack(L, n)); + ra = RA(i); /* previous call may change the stack */ + b = n; + L->top = ra + n; + } + for (j = 0; j < b; j++) { + if (j < n) { + setobjs2s(L, ra + j, ci->base - n + j); + } + else { + setnilvalue(ra + j); + } + } + continue; + } + } + } +} + diff --git a/external/lua/lua/lvm.h b/external/lua/lua/lvm.h new file mode 100644 index 0000000000..bfe4f5678d --- /dev/null +++ b/external/lua/lua/lvm.h @@ -0,0 +1,36 @@ +/* +** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $ +** Lua virtual machine +** See Copyright Notice in lua.h +*/ + +#ifndef lvm_h +#define lvm_h + + +#include "ldo.h" +#include "lobject.h" +#include "ltm.h" + + +#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o))) + +#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \ + (((o) = luaV_tonumber(o,n)) != NULL)) + +#define equalobj(L,o1,o2) \ + (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2)) + + +LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); +LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2); +LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); +LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); +LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, + StkId val); +LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, + StkId val); +LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls); +LUAI_FUNC void luaV_concat (lua_State *L, int total, int last); + +#endif diff --git a/external/lua/lua/lzio.c b/external/lua/lua/lzio.c new file mode 100644 index 0000000000..293edd59b0 --- /dev/null +++ b/external/lua/lua/lzio.c @@ -0,0 +1,82 @@ +/* +** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ +** a generic input stream interface +** See Copyright Notice in lua.h +*/ + + +#include + +#define lzio_c +#define LUA_CORE + +#include "lua.h" + +#include "llimits.h" +#include "lmem.h" +#include "lstate.h" +#include "lzio.h" + + +int luaZ_fill (ZIO *z) { + size_t size; + lua_State *L = z->L; + const char *buff; + lua_unlock(L); + buff = z->reader(L, z->data, &size); + lua_lock(L); + if (buff == NULL || size == 0) return EOZ; + z->n = size - 1; + z->p = buff; + return char2int(*(z->p++)); +} + + +int luaZ_lookahead (ZIO *z) { + if (z->n == 0) { + if (luaZ_fill(z) == EOZ) + return EOZ; + else { + z->n++; /* luaZ_fill removed first byte; put back it */ + z->p--; + } + } + return char2int(*z->p); +} + + +void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { + z->L = L; + z->reader = reader; + z->data = data; + z->n = 0; + z->p = NULL; +} + + +/* --------------------------------------------------------------- read --- */ +size_t luaZ_read (ZIO *z, void *b, size_t n) { + while (n) { + size_t m; + if (luaZ_lookahead(z) == EOZ) + return n; /* return number of missing bytes */ + m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ + memcpy(b, z->p, m); + z->n -= m; + z->p += m; + b = (char *)b + m; + n -= m; + } + return 0; +} + +/* ------------------------------------------------------------------------ */ +char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { + if (n > buff->buffsize) { + if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; + luaZ_resizebuffer(L, buff, n); + } + return buff->buffer; +} + + diff --git a/external/lua/lua/lzio.h b/external/lua/lua/lzio.h new file mode 100644 index 0000000000..51d695d8c1 --- /dev/null +++ b/external/lua/lua/lzio.h @@ -0,0 +1,67 @@ +/* +** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ +** Buffered streams +** See Copyright Notice in lua.h +*/ + + +#ifndef lzio_h +#define lzio_h + +#include "lua.h" + +#include "lmem.h" + + +#define EOZ (-1) /* end of stream */ + +typedef struct Zio ZIO; + +#define char2int(c) cast(int, cast(unsigned char, (c))) + +#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) + +typedef struct Mbuffer { + char *buffer; + size_t n; + size_t buffsize; +} Mbuffer; + +#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) + +#define luaZ_buffer(buff) ((buff)->buffer) +#define luaZ_sizebuffer(buff) ((buff)->buffsize) +#define luaZ_bufflen(buff) ((buff)->n) + +#define luaZ_resetbuffer(buff) ((buff)->n = 0) + + +#define luaZ_resizebuffer(L, buff, size) \ + (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ + (buff)->buffsize = size) + +#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) + + +LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); +LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, + void *data); +LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ +LUAI_FUNC int luaZ_lookahead (ZIO *z); + + + +/* --------- Private Part ------------------ */ + +struct Zio { + size_t n; /* bytes still unread */ + const char *p; /* current position in buffer */ + lua_Reader reader; + void* data; /* additional data */ + lua_State *L; /* Lua state (for reader) */ +}; + + +LUAI_FUNC int luaZ_fill (ZIO *z); + +#endif diff --git a/external/lua/lua/print.c b/external/lua/lua/print.c new file mode 100644 index 0000000000..e240cfc3c6 --- /dev/null +++ b/external/lua/lua/print.c @@ -0,0 +1,227 @@ +/* +** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $ +** print bytecodes +** See Copyright Notice in lua.h +*/ + +#include +#include + +#define luac_c +#define LUA_CORE + +#include "ldebug.h" +#include "lobject.h" +#include "lopcodes.h" +#include "lundump.h" + +#define PrintFunction luaU_print + +#define Sizeof(x) ((int)sizeof(x)) +#define VOID(p) ((const void*)(p)) + +static void PrintString(const TString* ts) +{ + const char* s=getstr(ts); + size_t i,n=ts->tsv.len; + putchar('"'); + for (i=0; ik[i]; + switch (ttype(o)) + { + case LUA_TNIL: + printf("nil"); + break; + case LUA_TBOOLEAN: + printf(bvalue(o) ? "true" : "false"); + break; + case LUA_TNUMBER: + printf(LUA_NUMBER_FMT,nvalue(o)); + break; + case LUA_TSTRING: + PrintString(rawtsvalue(o)); + break; + default: /* cannot happen */ + printf("? type=%d",ttype(o)); + break; + } +} + +static void PrintCode(const Proto* f) +{ + const Instruction* code=f->code; + int pc,n=f->sizecode; + for (pc=0; pc0) printf("[%d]\t",line); else printf("[-]\t"); + printf("%-9s\t",luaP_opnames[o]); + switch (getOpMode(o)) + { + case iABC: + printf("%d",a); + if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b); + if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c); + break; + case iABx: + if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx); + break; + case iAsBx: + if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx); + break; + } + switch (o) + { + case OP_LOADK: + printf("\t; "); PrintConstant(f,bx); + break; + case OP_GETUPVAL: + case OP_SETUPVAL: + printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-"); + break; + case OP_GETGLOBAL: + case OP_SETGLOBAL: + printf("\t; %s",svalue(&f->k[bx])); + break; + case OP_GETTABLE: + case OP_SELF: + if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } + break; + case OP_SETTABLE: + case OP_ADD: + case OP_SUB: + case OP_MUL: + case OP_DIV: + case OP_POW: + case OP_EQ: + case OP_LT: + case OP_LE: + if (ISK(b) || ISK(c)) + { + printf("\t; "); + if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); + printf(" "); + if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); + } + break; + case OP_JMP: + case OP_FORLOOP: + case OP_FORPREP: + printf("\t; to %d",sbx+pc+2); + break; + case OP_CLOSURE: + printf("\t; %p",VOID(f->p[bx])); + break; + case OP_SETLIST: + if (c==0) printf("\t; %d",(int)code[++pc]); + else printf("\t; %d",c); + break; + default: + break; + } + printf("\n"); + } +} + +#define SS(x) (x==1)?"":"s" +#define S(x) x,SS(x) + +static void PrintHeader(const Proto* f) +{ + const char* s=getstr(f->source); + if (*s=='@' || *s=='=') + s++; + else if (*s==LUA_SIGNATURE[0]) + s="(bstring)"; + else + s="(string)"; + printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n", + (f->linedefined==0)?"main":"function",s, + f->linedefined,f->lastlinedefined, + S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f)); + printf("%d%s param%s, %d slot%s, %d upvalue%s, ", + f->numparams,f->is_vararg?"+":"",SS(f->numparams), + S(f->maxstacksize),S(f->nups)); + printf("%d local%s, %d constant%s, %d function%s\n", + S(f->sizelocvars),S(f->sizek),S(f->sizep)); +} + +static void PrintConstants(const Proto* f) +{ + int i,n=f->sizek; + printf("constants (%d) for %p:\n",n,VOID(f)); + for (i=0; isizelocvars; + printf("locals (%d) for %p:\n",n,VOID(f)); + for (i=0; ilocvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); + } +} + +static void PrintUpvalues(const Proto* f) +{ + int i,n=f->sizeupvalues; + printf("upvalues (%d) for %p:\n",n,VOID(f)); + if (f->upvalues==NULL) return; + for (i=0; iupvalues[i])); + } +} + +void PrintFunction(const Proto* f, int full) +{ + int i,n=f->sizep; + PrintHeader(f); + PrintCode(f); + if (full) + { + PrintConstants(f); + PrintLocals(f); + PrintUpvalues(f); + } + for (i=0; ip[i],full); +} diff --git a/external/lua/luajit/build_android.sh b/external/lua/luajit/build_android.sh new file mode 100755 index 0000000000..c0551cb92a --- /dev/null +++ b/external/lua/luajit/build_android.sh @@ -0,0 +1,49 @@ +#!/bin/sh +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +host_os=`uname -s | tr "[:upper:]" "[:lower:]"` + +SRCDIR=$DIR/src +cd "$SRCDIR" + +NDK=$NDK_ROOT +NDKABI=8 +NDKVER=$NDK/toolchains/arm-linux-androideabi-4.6 +NDKP=$NDKVER/prebuilt/${host_os}-x86/bin/arm-linux-androideabi- +NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm" + +# Android/ARM, armeabi (ARMv5TE soft-float), Android 2.2+ (Froyo) +DESTDIR=$DIR/prebuilt/android/armeabi +rm "$DESTDIR"/*.a +make clean +make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF" + +if [ -f $SRCDIR/src/libluajit.a ]; then + mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.a +fi; + +# Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.0+ (ICS) +NDKARCH="-march=armv7-a -mfloat-abi=softfp -Wl,--fix-cortex-a8" +DESTDIR=$DIR/prebuilt/android/armeabi-v7a +rm "$DESTDIR"/*.a +make clean +make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF $NDKARCH" + +if [ -f $SRCDIR/src/libluajit.a ]; then + mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.a +fi; + +# Android/x86, x86 (i686 SSE3), Android 4.0+ (ICS) +NDKABI=14 +DESTDIR=$DIR/prebuilt/android/x86 +NDKVER=$NDK/toolchains/x86-4.6 +NDKP=$NDKVER/prebuilt/${host_os}-x86/bin/i686-linux-android- +NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-x86" +rm "$DESTDIR"/*.a +make clean +make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_SYS=Linux TARGET_FLAGS="$NDKF" + +if [ -f $SRCDIR/src/libluajit.a ]; then + mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.a +fi; + +make clean diff --git a/external/lua/luajit/build_ios.sh b/external/lua/luajit/build_ios.sh new file mode 100755 index 0000000000..52ea997371 --- /dev/null +++ b/external/lua/luajit/build_ios.sh @@ -0,0 +1,36 @@ +#!/bin/sh +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +LIPO="xcrun -sdk iphoneos lipo" +STRIP="xcrun -sdk iphoneos strip" + +SRCDIR=$DIR/src +DESTDIR=$DIR/prebuilt/ios +IXCODE=`xcode-select -print-path` +ISDK=$IXCODE/Platforms/iPhoneOS.platform/Developer +ISDKVER=iPhoneOS6.1.sdk +ISDKP=$ISDK/usr/bin/ + +rm "$DESTDIR"/*.a +cd $SRCDIR + +make clean +ISDKF="-arch armv7 -isysroot $ISDK/SDKs/$ISDKVER" +make HOST_CC="gcc -m32 -arch i386" CROSS=$ISDKP TARGET_FLAGS="$ISDKF" TARGET_SYS=iOS +mv "$SRCDIR"/src/libluajit.a "$DESTDIR"/libluajit-armv7.a + +make clean +ISDKF="-arch armv7s -isysroot $ISDK/SDKs/$ISDKVER" +make HOST_CC="gcc -m32 -arch i386" CROSS=$ISDKP TARGET_FLAGS="$ISDKF" TARGET_SYS=iOS +mv "$SRCDIR"/src/libluajit.a "$DESTDIR"/libluajit-armv7s.a + +make clean +make CC="gcc -m32 -arch i386" clean all +mv "$SRCDIR"/src/libluajit.a "$DESTDIR"/libluajit-i386.a + +$LIPO -create "$DESTDIR"/libluajit-*.a -output "$DESTDIR"/libluajit.a +$STRIP -S "$DESTDIR"/libluajit.a +$LIPO -info "$DESTDIR"/libluajit.a + +rm "$DESTDIR"/libluajit-*.a + +make clean diff --git a/external/lua/luajit/build_mac.sh b/external/lua/luajit/build_mac.sh new file mode 100755 index 0000000000..f5f93d3d56 --- /dev/null +++ b/external/lua/luajit/build_mac.sh @@ -0,0 +1,17 @@ +#!/bin/sh +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SRCDIR=$DIR/src +DESTDIR=$DIR/prebuilt/mac +MACOSX_DEPLOYMENT_TARGET="10.6" + +rm "$DESTDIR"/*.a + +cd $SRCDIR +make clean +make CC="gcc -m64 -arch x86_64" clean all + +if [ -f $SRCDIR/src/libluajit.a ]; then + mv $SRCDIR/src/libluajit.a $DESTDIR/libluajit.a +fi; + +make clean diff --git a/external/lua/luajit/include/lauxlib.h b/external/lua/luajit/include/lauxlib.h new file mode 100644 index 0000000000..fed1491b89 --- /dev/null +++ b/external/lua/luajit/include/lauxlib.h @@ -0,0 +1,167 @@ +/* +** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lauxlib_h +#define lauxlib_h + + +#include +#include + +#include "lua.h" + + +#define luaL_getn(L,i) ((int)lua_objlen(L, i)) +#define luaL_setn(L,i,j) ((void)0) /* no op! */ + +/* extra error code for `luaL_load' */ +#define LUA_ERRFILE (LUA_ERRERR+1) + +typedef struct luaL_Reg { + const char *name; + lua_CFunction func; +} luaL_Reg; + +LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, + const luaL_Reg *l, int nup); +LUALIB_API void (luaL_register) (lua_State *L, const char *libname, + const luaL_Reg *l); +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); +LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, + size_t *l); +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, + const char *def, size_t *l); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); + +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, + lua_Integer def); + +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); +LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int narg); + +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); + +LUALIB_API void (luaL_where) (lua_State *L, int lvl); +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); + +LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, + const char *const lst[]); + +LUALIB_API int (luaL_ref) (lua_State *L, int t); +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); + +LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); +LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, + const char *name); +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); + +LUALIB_API lua_State *(luaL_newstate) (void); + + +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, + const char *r); + +LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, + const char *fname, int szhint); + +/* From Lua 5.2. */ +LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname); +LUALIB_API int luaL_execresult(lua_State *L, int stat); +LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, + const char *mode); +LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, + const char *name, const char *mode); +LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, + int level); + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define luaL_argcheck(L, cond,numarg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) + +#define luaL_dofile(L, fn) \ + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_dostring(L, s) \ + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) + +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + + + +typedef struct luaL_Buffer { + char *p; /* current position in buffer */ + int lvl; /* number of strings in the stack (level) */ + lua_State *L; + char buffer[LUAL_BUFFERSIZE]; +} luaL_Buffer; + +#define luaL_addchar(B,c) \ + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ + (*(B)->p++ = (char)(c))) + +/* compatibility only */ +#define luaL_putchar(B,c) luaL_addchar(B,c) + +#define luaL_addsize(B,n) ((B)->p += (n)) + +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); +LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); + + +/* }====================================================== */ + + +/* compatibility with ref system */ + +/* pre-defined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ + (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) + +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) + +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) + + +#define luaL_reg luaL_Reg + +#endif diff --git a/external/lua/luajit/include/lua.h b/external/lua/luajit/include/lua.h new file mode 100644 index 0000000000..c83fd3bbe7 --- /dev/null +++ b/external/lua/luajit/include/lua.h @@ -0,0 +1,393 @@ +/* +** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $ +** Lua - An Extensible Extension Language +** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** See Copyright Notice at the end of this file +*/ + + +#ifndef lua_h +#define lua_h + +#include +#include + + +#include "luaconf.h" + + +#define LUA_VERSION "Lua 5.1" +#define LUA_RELEASE "Lua 5.1.4" +#define LUA_VERSION_NUM 501 +#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" + + +/* mark for precompiled code (`Lua') */ +#define LUA_SIGNATURE "\033Lua" + +/* option for multiple returns in `lua_pcall' and `lua_call' */ +#define LUA_MULTRET (-1) + + +/* +** pseudo-indices +*/ +#define LUA_REGISTRYINDEX (-10000) +#define LUA_ENVIRONINDEX (-10001) +#define LUA_GLOBALSINDEX (-10002) +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) + + +/* thread status; 0 is OK */ +#define LUA_YIELD 1 +#define LUA_ERRRUN 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRERR 5 + + +typedef struct lua_State lua_State; + +typedef int (*lua_CFunction) (lua_State *L); + + +/* +** functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); + + +/* +** prototype for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + + +/* +** basic types +*/ +#define LUA_TNONE (-1) + +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_TFUNCTION 6 +#define LUA_TUSERDATA 7 +#define LUA_TTHREAD 8 + + + +/* minimum Lua stack available to a C function */ +#define LUA_MINSTACK 20 + + +/* +** generic extra include file +*/ +#if defined(LUA_USER_H) +#include LUA_USER_H +#endif + + +/* type of numbers in Lua */ +typedef LUA_NUMBER lua_Number; + + +/* type for integer functions */ +typedef LUA_INTEGER lua_Integer; + + + +/* +** state manipulation +*/ +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API void (lua_close) (lua_State *L); +LUA_API lua_State *(lua_newthread) (lua_State *L); + +LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); + + +/* +** basic stack manipulation +*/ +LUA_API int (lua_gettop) (lua_State *L); +LUA_API void (lua_settop) (lua_State *L, int idx); +LUA_API void (lua_pushvalue) (lua_State *L, int idx); +LUA_API void (lua_remove) (lua_State *L, int idx); +LUA_API void (lua_insert) (lua_State *L, int idx); +LUA_API void (lua_replace) (lua_State *L, int idx); +LUA_API int (lua_checkstack) (lua_State *L, int sz); + +LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); + + +/* +** access functions (stack -> C) +*/ + +LUA_API int (lua_isnumber) (lua_State *L, int idx); +LUA_API int (lua_isstring) (lua_State *L, int idx); +LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isuserdata) (lua_State *L, int idx); +LUA_API int (lua_type) (lua_State *L, int idx); +LUA_API const char *(lua_typename) (lua_State *L, int tp); + +LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); + +LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); +LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); +LUA_API int (lua_toboolean) (lua_State *L, int idx); +LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); +LUA_API size_t (lua_objlen) (lua_State *L, int idx); +LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); +LUA_API void *(lua_touserdata) (lua_State *L, int idx); +LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); +LUA_API const void *(lua_topointer) (lua_State *L, int idx); + + +/* +** push functions (C -> stack) +*/ +LUA_API void (lua_pushnil) (lua_State *L); +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); +LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); +LUA_API void (lua_pushstring) (lua_State *L, const char *s); +LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, + va_list argp); +LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); +LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); +LUA_API void (lua_pushboolean) (lua_State *L, int b); +LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); +LUA_API int (lua_pushthread) (lua_State *L); + + +/* +** get functions (Lua -> stack) +*/ +LUA_API void (lua_gettable) (lua_State *L, int idx); +LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawget) (lua_State *L, int idx); +LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); +LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); +LUA_API int (lua_getmetatable) (lua_State *L, int objindex); +LUA_API void (lua_getfenv) (lua_State *L, int idx); + + +/* +** set functions (stack -> Lua) +*/ +LUA_API void (lua_settable) (lua_State *L, int idx); +LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawset) (lua_State *L, int idx); +LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); +LUA_API int (lua_setmetatable) (lua_State *L, int objindex); +LUA_API int (lua_setfenv) (lua_State *L, int idx); + + +/* +** `load' and `call' functions (load and run Lua code) +*/ +LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); +LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); +LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); +LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname); + +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); + + +/* +** coroutine functions +*/ +LUA_API int (lua_yield) (lua_State *L, int nresults); +LUA_API int (lua_resume) (lua_State *L, int narg); +LUA_API int (lua_status) (lua_State *L); + +/* +** garbage-collection function and options +*/ + +#define LUA_GCSTOP 0 +#define LUA_GCRESTART 1 +#define LUA_GCCOLLECT 2 +#define LUA_GCCOUNT 3 +#define LUA_GCCOUNTB 4 +#define LUA_GCSTEP 5 +#define LUA_GCSETPAUSE 6 +#define LUA_GCSETSTEPMUL 7 + +LUA_API int (lua_gc) (lua_State *L, int what, int data); + + +/* +** miscellaneous functions +*/ + +LUA_API int (lua_error) (lua_State *L); + +LUA_API int (lua_next) (lua_State *L, int idx); + +LUA_API void (lua_concat) (lua_State *L, int n); + +LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define lua_pop(L,n) lua_settop(L, -(n)-1) + +#define lua_newtable(L) lua_createtable(L, 0, 0) + +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) + +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) + +#define lua_strlen(L,i) lua_objlen(L, (i)) + +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) + +#define lua_pushliteral(L, s) \ + lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) + +#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) +#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) + +#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) + + + +/* +** compatibility macros and functions +*/ + +#define lua_open() luaL_newstate() + +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) + +#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) + +#define lua_Chunkreader lua_Reader +#define lua_Chunkwriter lua_Writer + + +/* hack */ +LUA_API void lua_setlevel (lua_State *from, lua_State *to); + + +/* +** {====================================================================== +** Debug API +** ======================================================================= +*/ + + +/* +** Event codes +*/ +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILRET 4 + + +/* +** Event masks +*/ +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +typedef struct lua_Debug lua_Debug; /* activation record */ + + +/* Functions to be called by the debuger in specific events */ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); + + +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); +LUA_API lua_Hook lua_gethook (lua_State *L); +LUA_API int lua_gethookmask (lua_State *L); +LUA_API int lua_gethookcount (lua_State *L); + +/* From Lua 5.2. */ +LUA_API void *lua_upvalueid (lua_State *L, int idx, int n); +LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2); +LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname, const char *mode); + + +struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) `global', `local', `field', `method' */ + const char *what; /* (S) `Lua', `C', `main', `tail' */ + const char *source; /* (S) */ + int currentline; /* (l) */ + int nups; /* (u) number of upvalues */ + int linedefined; /* (S) */ + int lastlinedefined; /* (S) */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + int i_ci; /* active function */ +}; + +/* }====================================================================== */ + + +/****************************************************************************** +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + + +#endif diff --git a/external/lua/luajit/include/luaconf.h b/external/lua/luajit/include/luaconf.h new file mode 100644 index 0000000000..8e3a7aaa0c --- /dev/null +++ b/external/lua/luajit/include/luaconf.h @@ -0,0 +1,139 @@ +/* +** Configuration header. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef luaconf_h +#define luaconf_h + +#include +#include + +/* Default path for loading Lua and C modules with require(). */ +#if defined(_WIN32) +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_PATH_DEFAULT \ + ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" +#define LUA_CPATH_DEFAULT \ + ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" +#else +/* +** Note to distribution maintainers: do NOT patch the following line! +** Please read ../doc/install.html#distro and pass PREFIX=/usr instead. +*/ +#define LUA_ROOT "/usr/local/" +#define LUA_LDIR LUA_ROOT "share/lua/5.1/" +#define LUA_CDIR LUA_ROOT "lib/lua/5.1/" +#ifdef LUA_XROOT +#define LUA_JDIR LUA_XROOT "share/luajit-2.0.1/" +#define LUA_XPATH \ + ";" LUA_XROOT "share/lua/5.1/?.lua;" LUA_XROOT "share/lua/5.1/?/init.lua" +#define LUA_XCPATH LUA_XROOT "lib/lua/5.1/?.so;" +#else +#define LUA_JDIR LUA_ROOT "share/luajit-2.0.1/" +#define LUA_XPATH +#define LUA_XCPATH +#endif +#define LUA_PATH_DEFAULT \ + "./?.lua;" LUA_JDIR"?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua" LUA_XPATH +#define LUA_CPATH_DEFAULT \ + "./?.so;" LUA_CDIR"?.so;" LUA_XCPATH LUA_CDIR"loadall.so" +#endif + +/* Environment variable names for path overrides and initialization code. */ +#define LUA_PATH "LUA_PATH" +#define LUA_CPATH "LUA_CPATH" +#define LUA_INIT "LUA_INIT" + +/* Special file system characters. */ +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif +#define LUA_PATHSEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXECDIR "!" +#define LUA_IGMARK "-" +#define LUA_PATH_CONFIG \ + LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" \ + LUA_EXECDIR "\n" LUA_IGMARK + +/* Quoting in error messages. */ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + +/* Various tunables. */ +#define LUAI_MAXSTACK 65500 /* Max. # of stack slots for a thread (<64K). */ +#define LUAI_MAXCSTACK 8000 /* Max. # of stack slots for a C func (<10K). */ +#define LUAI_GCPAUSE 200 /* Pause GC until memory is at 200%. */ +#define LUAI_GCMUL 200 /* Run GC at 200% of allocation speed. */ +#define LUA_MAXCAPTURES 32 /* Max. pattern captures. */ + +/* Compatibility with older library function names. */ +#define LUA_COMPAT_MOD /* OLD: math.mod, NEW: math.fmod */ +#define LUA_COMPAT_GFIND /* OLD: string.gfind, NEW: string.gmatch */ + +/* Configuration for the frontend (the luajit executable). */ +#if defined(luajit_c) +#define LUA_PROGNAME "luajit" /* Fallback frontend name. */ +#define LUA_PROMPT "> " /* Interactive prompt. */ +#define LUA_PROMPT2 ">> " /* Continuation prompt. */ +#define LUA_MAXINPUT 512 /* Max. input line length. */ +#endif + +/* Note: changing the following defines breaks the Lua 5.1 ABI. */ +#define LUA_INTEGER ptrdiff_t +#define LUA_IDSIZE 60 /* Size of lua_Debug.short_src. */ +/* +** Size of lauxlib and io.* on-stack buffers. Weird workaround to avoid using +** unreasonable amounts of stack space, but still retain ABI compatibility. +** Blame Lua for depending on BUFSIZ in the ABI, blame **** for wrecking it. +*/ +#define LUAL_BUFFERSIZE (BUFSIZ > 16384 ? 8192 : BUFSIZ) + +/* The following defines are here only for compatibility with luaconf.h +** from the standard Lua distribution. They must not be changed for LuaJIT. +*/ +#define LUA_NUMBER_DOUBLE +#define LUA_NUMBER double +#define LUAI_UACNUMBER double +#define LUA_NUMBER_SCAN "%lf" +#define LUA_NUMBER_FMT "%.14g" +#define lua_number2str(s, n) sprintf((s), LUA_NUMBER_FMT, (n)) +#define LUAI_MAXNUMBER2STR 32 +#define LUA_INTFRMLEN "l" +#define LUA_INTFRM_T long + +/* Linkage of public API functions. */ +#if defined(LUA_BUILD_AS_DLL) +#if defined(LUA_CORE) || defined(LUA_LIB) +#define LUA_API __declspec(dllexport) +#else +#define LUA_API __declspec(dllimport) +#endif +#else +#define LUA_API extern +#endif + +#define LUALIB_API LUA_API + +/* Support for internal assertions. */ +#if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK) +#include +#endif +#ifdef LUA_USE_ASSERT +#define lua_assert(x) assert(x) +#endif +#ifdef LUA_USE_APICHECK +#define luai_apicheck(L, o) { (void)L; assert(o); } +#else +#define luai_apicheck(L, o) { (void)L; } +#endif + +#endif diff --git a/external/lua/luajit/include/lualib.h b/external/lua/luajit/include/lualib.h new file mode 100644 index 0000000000..18c6234da7 --- /dev/null +++ b/external/lua/luajit/include/lualib.h @@ -0,0 +1,43 @@ +/* +** Standard library header. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LUALIB_H +#define _LUALIB_H + +#include "lua.h" + +#define LUA_FILEHANDLE "FILE*" + +#define LUA_COLIBNAME "coroutine" +#define LUA_MATHLIBNAME "math" +#define LUA_STRLIBNAME "string" +#define LUA_TABLIBNAME "table" +#define LUA_IOLIBNAME "io" +#define LUA_OSLIBNAME "os" +#define LUA_LOADLIBNAME "package" +#define LUA_DBLIBNAME "debug" +#define LUA_BITLIBNAME "bit" +#define LUA_JITLIBNAME "jit" +#define LUA_FFILIBNAME "ffi" + +LUALIB_API int luaopen_base(lua_State *L); +LUALIB_API int luaopen_math(lua_State *L); +LUALIB_API int luaopen_string(lua_State *L); +LUALIB_API int luaopen_table(lua_State *L); +LUALIB_API int luaopen_io(lua_State *L); +LUALIB_API int luaopen_os(lua_State *L); +LUALIB_API int luaopen_package(lua_State *L); +LUALIB_API int luaopen_debug(lua_State *L); +LUALIB_API int luaopen_bit(lua_State *L); +LUALIB_API int luaopen_jit(lua_State *L); +LUALIB_API int luaopen_ffi(lua_State *L); + +LUALIB_API void luaL_openlibs(lua_State *L); + +#ifndef lua_assert +#define lua_assert(x) ((void)0) +#endif + +#endif diff --git a/external/lua/luajit/prebuilt/android/Android.mk b/external/lua/luajit/prebuilt/android/Android.mk new file mode 100644 index 0000000000..f94fb6c3e4 --- /dev/null +++ b/external/lua/luajit/prebuilt/android/Android.mk @@ -0,0 +1,8 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := luajit_static +LOCAL_MODULE_FILENAME := libluajit +LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libluajit.a +LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include +include $(PREBUILT_STATIC_LIBRARY) diff --git a/external/lua/luajit/prebuilt/android/armeabi-v7a/libluajit.a.REMOVED.git-id b/external/lua/luajit/prebuilt/android/armeabi-v7a/libluajit.a.REMOVED.git-id new file mode 100644 index 0000000000..f72d7d637b --- /dev/null +++ b/external/lua/luajit/prebuilt/android/armeabi-v7a/libluajit.a.REMOVED.git-id @@ -0,0 +1 @@ +bd67268978745730788ae5e927c11e6fe29fa83f \ No newline at end of file diff --git a/external/lua/luajit/prebuilt/android/armeabi/libluajit.a.REMOVED.git-id b/external/lua/luajit/prebuilt/android/armeabi/libluajit.a.REMOVED.git-id new file mode 100644 index 0000000000..40e35b96f1 --- /dev/null +++ b/external/lua/luajit/prebuilt/android/armeabi/libluajit.a.REMOVED.git-id @@ -0,0 +1 @@ +14c2fb2ea24bb1e5f0470aed70340c94352ee6b2 \ No newline at end of file diff --git a/external/lua/luajit/prebuilt/android/x86/libluajit.a.REMOVED.git-id b/external/lua/luajit/prebuilt/android/x86/libluajit.a.REMOVED.git-id new file mode 100644 index 0000000000..6ddb73d658 --- /dev/null +++ b/external/lua/luajit/prebuilt/android/x86/libluajit.a.REMOVED.git-id @@ -0,0 +1 @@ +76bfc22fbc3c421a62d00eafb9b2554cae4840d7 \ No newline at end of file diff --git a/external/lua/luajit/prebuilt/ios/libluajit.a.REMOVED.git-id b/external/lua/luajit/prebuilt/ios/libluajit.a.REMOVED.git-id new file mode 100644 index 0000000000..a62e429694 --- /dev/null +++ b/external/lua/luajit/prebuilt/ios/libluajit.a.REMOVED.git-id @@ -0,0 +1 @@ +c575634b13ad68bafaba45d8141968768687751f \ No newline at end of file diff --git a/external/lua/luajit/prebuilt/mac/libluajit.a.REMOVED.git-id b/external/lua/luajit/prebuilt/mac/libluajit.a.REMOVED.git-id new file mode 100644 index 0000000000..2b297152c9 --- /dev/null +++ b/external/lua/luajit/prebuilt/mac/libluajit.a.REMOVED.git-id @@ -0,0 +1 @@ +c319cbee7c1d56c5e7add0ba9dcd272ccb36366d \ No newline at end of file diff --git a/external/lua/luajit/prebuilt/win32/lua51.dll.REMOVED.git-id b/external/lua/luajit/prebuilt/win32/lua51.dll.REMOVED.git-id new file mode 100644 index 0000000000..26a14d1567 --- /dev/null +++ b/external/lua/luajit/prebuilt/win32/lua51.dll.REMOVED.git-id @@ -0,0 +1 @@ +f111322c4e868041db77722795dd831097f3ad11 \ No newline at end of file diff --git a/external/lua/luajit/src/COPYRIGHT b/external/lua/luajit/src/COPYRIGHT new file mode 100644 index 0000000000..83ce94daee --- /dev/null +++ b/external/lua/luajit/src/COPYRIGHT @@ -0,0 +1,56 @@ +=============================================================================== +LuaJIT -- a Just-In-Time Compiler for Lua. http://luajit.org/ + +Copyright (C) 2005-2013 Mike Pall. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +[ MIT license: http://www.opensource.org/licenses/mit-license.php ] + +=============================================================================== +[ LuaJIT includes code from Lua 5.1/5.2, which has this license statement: ] + +Copyright (C) 1994-2012 Lua.org, PUC-Rio. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +=============================================================================== +[ LuaJIT includes code from dlmalloc, which has this license statement: ] + +This is a version (aka dlmalloc) of malloc/free/realloc written by +Doug Lea and released to the public domain, as explained at +http://creativecommons.org/licenses/publicdomain + +=============================================================================== diff --git a/external/lua/luajit/src/README b/external/lua/luajit/src/README new file mode 100644 index 0000000000..e68604b72c --- /dev/null +++ b/external/lua/luajit/src/README @@ -0,0 +1,16 @@ +README for LuaJIT 2.0.1 +----------------------- + +LuaJIT is a Just-In-Time (JIT) compiler for the Lua programming language. + +Project Homepage: http://luajit.org/ + +LuaJIT is Copyright (C) 2005-2013 Mike Pall. +LuaJIT is free software, released under the MIT license. +See full Copyright Notice in the COPYRIGHT file or in luajit.h. + +Documentation for LuaJIT is available in HTML format. +Please point your favorite browser to: + + doc/luajit.html + diff --git a/external/lua/luajit/src/doc/bluequad-print.css b/external/lua/luajit/src/doc/bluequad-print.css new file mode 100644 index 0000000000..41ae7575b6 --- /dev/null +++ b/external/lua/luajit/src/doc/bluequad-print.css @@ -0,0 +1,166 @@ +/* Copyright (C) 2004-2013 Mike Pall. + * + * You are welcome to use the general ideas of this design for your own sites. + * But please do not steal the stylesheet, the layout or the color scheme. + */ +body { + font-family: serif; + font-size: 11pt; + margin: 0 3em; + padding: 0; + border: none; +} +a:link, a:visited, a:hover, a:active { + text-decoration: none; + background: transparent; + color: #0000ff; +} +h1, h2, h3 { + font-family: sans-serif; + font-weight: bold; + text-align: left; + margin: 0.5em 0; + padding: 0; +} +h1 { + font-size: 200%; +} +h2 { + font-size: 150%; +} +h3 { + font-size: 125%; +} +p { + margin: 0 0 0.5em 0; + padding: 0; +} +ul, ol { + margin: 0.5em 0; + padding: 0 0 0 2em; +} +ul { + list-style: outside square; +} +ol { + list-style: outside decimal; +} +li { + margin: 0; + padding: 0; +} +dl { + margin: 1em 0; + padding: 1em; + border: 1px solid black; +} +dt { + font-weight: bold; + margin: 0; + padding: 0; +} +dt sup { + float: right; + margin-left: 1em; +} +dd { + margin: 0.5em 0 0 2em; + padding: 0; +} +table { + table-layout: fixed; + width: 100%; + margin: 1em 0; + padding: 0; + border: 1px solid black; + border-spacing: 0; + border-collapse: collapse; +} +tr { + margin: 0; + padding: 0; + border: none; +} +td { + text-align: left; + margin: 0; + padding: 0.2em 0.5em; + border-top: 1px solid black; + border-bottom: 1px solid black; +} +tr.separate td { + border-top: double; +} +tt, pre, code, kbd, samp { + font-family: monospace; + font-size: 75%; +} +kbd { + font-weight: bolder; +} +blockquote, pre { + margin: 1em 2em; + padding: 0; +} +img { + border: none; + vertical-align: baseline; + margin: 0; + padding: 0; +} +img.left { + float: left; + margin: 0.5em 1em 0.5em 0; +} +img.right { + float: right; + margin: 0.5em 0 0.5em 1em; +} +.flush { + clear: both; + visibility: hidden; +} +.hide, .noprint, #nav { + display: none !important; +} +.pagebreak { + page-break-before: always; +} +#site { + text-align: right; + font-family: sans-serif; + font-weight: bold; + margin: 0 1em; + border-bottom: 1pt solid black; +} +#site a { + font-size: 1.2em; +} +#site a:link, #site a:visited { + text-decoration: none; + font-weight: bold; + background: transparent; + color: #ffffff; +} +#logo { + color: #ff8000; +} +#head { + clear: both; + margin: 0 1em; +} +#main { + line-height: 1.3; + text-align: justify; + margin: 1em; +} +#foot { + clear: both; + font-size: 80%; + text-align: center; + margin: 0 1.25em; + padding: 0.5em 0 0 0; + border-top: 1pt solid black; + page-break-before: avoid; + page-break-after: avoid; +} diff --git a/external/lua/luajit/src/doc/bluequad.css b/external/lua/luajit/src/doc/bluequad.css new file mode 100644 index 0000000000..5e8d5ce772 --- /dev/null +++ b/external/lua/luajit/src/doc/bluequad.css @@ -0,0 +1,325 @@ +/* Copyright (C) 2004-2013 Mike Pall. + * + * You are welcome to use the general ideas of this design for your own sites. + * But please do not steal the stylesheet, the layout or the color scheme. + */ +/* colorscheme: + * + * site | head #4162bf/white | #6078bf/#e6ecff + * ------+------ ----------------+------------------- + * nav | main #bfcfff | #e6ecff/black + * + * nav: hiback loback #c5d5ff #b9c9f9 + * hiborder loborder #e6ecff #97a7d7 + * link hover #2142bf #ff0000 + * + * link: link visited hover #2142bf #8122bf #ff0000 + * + * main: boxback boxborder #f0f4ff #bfcfff + */ +body { + font-family: Verdana, Arial, Helvetica, sans-serif; + font-size: 10pt; + margin: 0; + padding: 0; + border: none; + background: #e0e0e0; + color: #000000; +} +a:link { + text-decoration: none; + background: transparent; + color: #2142bf; +} +a:visited { + text-decoration: none; + background: transparent; + color: #8122bf; +} +a:hover, a:active { + text-decoration: underline; + background: transparent; + color: #ff0000; +} +h1, h2, h3 { + font-weight: bold; + text-align: left; + margin: 0.5em 0; + padding: 0; + background: transparent; +} +h1 { + font-size: 200%; + line-height: 3em; /* really 6em relative to body, match #site span */ + margin: 0; +} +h2 { + font-size: 150%; + color: #606060; +} +h3 { + font-size: 125%; + color: #404040; +} +p { + max-width: 600px; + margin: 0 0 0.5em 0; + padding: 0; +} +b { + color: #404040; +} +ul, ol { + max-width: 600px; + margin: 0.5em 0; + padding: 0 0 0 2em; +} +ul { + list-style: outside square; +} +ol { + list-style: outside decimal; +} +li { + margin: 0; + padding: 0; +} +dl { + max-width: 600px; + margin: 1em 0; + padding: 1em; + border: 1px solid #bfcfff; + background: #f0f4ff; +} +dt { + font-weight: bold; + margin: 0; + padding: 0; +} +dt sup { + float: right; + margin-left: 1em; + color: #808080; +} +dt a:visited { + text-decoration: none; + color: #2142bf; +} +dt a:hover, dt a:active { + text-decoration: none; + color: #ff0000; +} +dd { + margin: 0.5em 0 0 2em; + padding: 0; +} +div.tablewrap { /* for IE *sigh* */ + max-width: 600px; +} +table { + table-layout: fixed; + border-spacing: 0; + border-collapse: collapse; + max-width: 600px; + width: 100%; + margin: 1em 0; + padding: 0; + border: 1px solid #bfcfff; +} +tr { + margin: 0; + padding: 0; + border: none; +} +tr.odd { + background: #f0f4ff; +} +tr.separate td { + border-top: 1px solid #bfcfff; +} +td { + text-align: left; + margin: 0; + padding: 0.2em 0.5em; + border: none; +} +tt, code, kbd, samp { + font-family: Courier New, Courier, monospace; + line-height: 1.2; + font-size: 110%; +} +kbd { + font-weight: bolder; +} +blockquote, pre { + max-width: 600px; + margin: 1em 2em; + padding: 0; +} +pre { + line-height: 1.1; +} +pre.code { + line-height: 1.4; + margin: 0.5em 0 1em 0.5em; + padding: 0.5em 1em; + border: 1px solid #bfcfff; + background: #f0f4ff; +} +pre.mark { + padding-left: 2em; +} +span.codemark { + position:absolute; + left: 16em; + color: #4040c0; +} +span.mark { + color: #4040c0; + font-family: Courier New, Courier, monospace; + line-height: 1.1; +} +img { + border: none; + vertical-align: baseline; + margin: 0; + padding: 0; +} +img.left { + float: left; + margin: 0.5em 1em 0.5em 0; +} +img.right { + float: right; + margin: 0.5em 0 0.5em 1em; +} +.indent { + padding-left: 1em; +} +.flush { + clear: both; + visibility: hidden; +} +.hide, .noscreen { + display: none !important; +} +.ext { + color: #ff8000; +} +.new { + font-size: 6pt; + vertical-align: middle; + background: #ff8000; + color: #ffffff; +} +#site { + clear: both; + float: left; + width: 13em; + text-align: center; + font-weight: bold; + margin: 0; + padding: 0; + background: transparent; + color: #ffffff; +} +#site a { + font-size: 200%; +} +#site a:link, #site a:visited { + text-decoration: none; + font-weight: bold; + background: transparent; + color: #ffffff; +} +#site span { + line-height: 3em; /* really 6em relative to body, match h1 */ +} +#logo { + color: #ffb380; +} +#head { + margin: 0; + padding: 0 0 0 2em; + border-left: solid 13em #4162bf; + border-right: solid 3em #6078bf; + background: #6078bf; + color: #e6ecff; +} +#nav { + clear: both; + float: left; + overflow: hidden; + text-align: left; + line-height: 1.5; + width: 13em; + padding-top: 1em; + background: transparent; +} +#nav ul { + list-style: none outside; + margin: 0; + padding: 0; +} +#nav li { + margin: 0; + padding: 0; +} +#nav a { + display: block; + text-decoration: none; + font-weight: bold; + margin: 0; + padding: 2px 1em; + border-top: 1px solid transparent; + border-bottom: 1px solid transparent; + background: transparent; + color: #2142bf; +} +#nav a:hover, #nav a:active { + text-decoration: none; + border-top: 1px solid #97a7d7; + border-bottom: 1px solid #e6ecff; + background: #b9c9f9; + color: #ff0000; +} +#nav a.current, #nav a.current:hover, #nav a.current:active { + border-top: 1px solid #e6ecff; + border-bottom: 1px solid #97a7d7; + background: #c5d5ff; + color: #2142bf; +} +#nav ul ul a { + padding: 0 1em 0 1.7em; +} +#nav ul ul ul a { + padding: 0 0.5em 0 2.4em; +} +#main { + line-height: 1.5; + text-align: left; + margin: 0; + padding: 1em 2em; + border-left: solid 13em #bfcfff; + border-right: solid 3em #e6ecff; + background: #e6ecff; +} +#foot { + clear: both; + font-size: 80%; + text-align: center; + margin: 0; + padding: 0.5em; + background: #6078bf; + color: #ffffff; +} +#foot a:link, #foot a:visited { + text-decoration: underline; + background: transparent; + color: #ffffff; +} +#foot a:hover, #foot a:active { + text-decoration: underline; + background: transparent; + color: #bfcfff; +} diff --git a/external/lua/luajit/src/doc/changes.html b/external/lua/luajit/src/doc/changes.html new file mode 100644 index 0000000000..1176fb5be6 --- /dev/null +++ b/external/lua/luajit/src/doc/changes.html @@ -0,0 +1,862 @@ + + + +LuaJIT Change History + + + + + + + + + +

+Lua +
+ + +
+

+This is a list of changes between the released versions of LuaJIT.
+The current stable version is LuaJIT 2.0.1.
+

+

+Please check the +» Online Change History +to see whether newer versions are available. +

+ +
+

LuaJIT 2.0.1 — 2013-02-19

+
    +
  • Don't clear frame for out-of-memory error.
  • +
  • Leave hook when resume catches error thrown from hook.
  • +
  • Add missing GC steps for template table creation.
  • +
  • Fix discharge order of comparisons in Lua parser.
  • +
  • Improve buffer handling for io.read().
  • +
  • OSX: Add support for Mach-O object files to -b option.
  • +
  • Fix PS3 port.
  • +
  • Fix/enable Xbox 360 port.
  • +
  • x86/x64: Always mark ref for shift count as non-weak.
  • +
  • x64: Don't fuse implicitly 32-to-64 extended operands.
  • +
  • ARM: Fix armhf call argument handling.
  • +
  • ARM: Fix code generation for integer math.min/math.max.
  • +
  • PPC/e500: Fix lj_vm_floor() for Inf/NaN.
  • +
  • FFI: Change priority of table initializer variants for structs.
  • +
  • FFI: Fix code generation for bool call result check on x86/x64.
  • +
  • FFI: Load FFI library on-demand for bytecode with cdata literals.
  • +
  • FFI: Fix handling of qualified transparent structs/unions.
  • +
+ +

LuaJIT 2.0.0 — 2012-11-08

+
    +
  • Correctness and completeness: +
      +
    • Fix Android/x86 build.
    • +
    • Fix recording of equality comparisons with __eq metamethods.
    • +
    • Fix detection of immutable upvalues.
    • +
    • Replace error with PANIC for callbacks from JIT-compiled code.
    • +
    • Fix builtin string to number conversion for INT_MIN.
    • +
    • Don't create unneeded array part for template tables.
    • +
    • Fix CONV.num.int sinking.
    • +
    • Don't propagate implicitly widened number to index metamethods.
    • +
    • ARM: Fix ordered comparisons of number vs. non-number.
    • +
    • FFI: Fix code generation for replay of sunk float fields.
    • +
    • FFI: Fix signedness of bool.
    • +
    • FFI: Fix recording of bool call result check on x86/x64.
    • +
    • FFI: Fix stack-adjustment for __thiscall callbacks.
    • +
  • +
+ +

LuaJIT 2.0.0-beta11 — 2012-10-16

+
    +
  • New features: +
      +
    • Use ARM VFP instructions, if available (build-time detection).
    • +
    • Add support for ARM hard-float EABI (armhf).
    • +
    • Add PS3 port.
    • +
    • Add many features from Lua 5.2, e.g. goto/labels. + Refer to this list.
    • +
    • FFI: Add parameterized C types.
    • +
    • FFI: Add support for copy constructors.
    • +
    • FFI: Equality comparisons never raise an error (treat as unequal instead).
    • +
    • FFI: Box all accessed or returned enums.
    • +
    • FFI: Check for __new metamethod when calling a constructor.
    • +
    • FFI: Handle __pairs/__ipairs metamethods for cdata objects.
    • +
    • FFI: Convert io.* file handle to FILE * pointer (but as a void *).
    • +
    • FFI: Detect and support type punning through unions.
    • +
    • FFI: Improve various error messages.
    • +
  • +
  • Build-system reorganization: +
      +
    • Reorganize directory layout:
      + lib/*src/jit/*
      + src/buildvm_*.dascsrc/vm_*.dasc
      + src/buildvm_*.h → removed
      + src/buildvm*src/host/*
    • +
    • Add minified Lua interpreter plus Lua BitOp (minilua) to run DynASM.
    • +
    • Change DynASM bit operations to use Lua BitOp
    • +
    • Translate only vm_*.dasc for detected target architecture.
    • +
    • Improve target detection for msvcbuild.bat.
    • +
    • Fix build issues on Cygwin and MinGW with optional MSys.
    • +
    • Handle cross-compiles with FPU/no-FPU or hard-fp/soft-fp ABI mismatch.
    • +
    • Remove some library functions for no-JIT/no-FFI builds.
    • +
    • Add uninstall target to top-level Makefile.
    • +
  • +
  • Correctness and completeness: +
      +
    • Preserve snapshot #0 PC for all traces.
    • +
    • Fix argument checks for coroutine.create().
    • +
    • Command line prints version and JIT status to stdout, not stderr.
    • +
    • Fix userdata __gc separations at Lua state close.
    • +
    • Fix TDUP to HLOAD forwarding for LJ_DUALNUM builds.
    • +
    • Fix buffer check in bytecode writer.
    • +
    • Make os.date() thread-safe.
    • +
    • Add missing declarations for MSVC intrinsics.
    • +
    • Fix dispatch table modifications for return hooks.
    • +
    • Workaround for MSVC conversion bug (doubleuint32_tint32_t).
    • +
    • Fix FOLD rule (i-j)-i => 0-j.
    • +
    • Never use DWARF unwinder on Windows.
    • +
    • Fix shrinking of direct mapped blocks in builtin allocator.
    • +
    • Limit recursion depth in string.match() et al.
    • +
    • Fix late despecialization of ITERN after loop has been entered.
    • +
    • Fix 'f' and 'L' options for debug.getinfo() and lua_getinfo().
    • +
    • Fix package.searchpath().
    • +
    • OSX: Change dylib names to be consistent with other platforms.
    • +
    • Android: Workaround for broken sprintf("%g", -0.0).
    • +
    • x86: Remove support for ancient CPUs without CMOV (before Pentium Pro).
    • +
    • x86: Fix register allocation for calls returning register pair.
    • +
    • x86/x64: Fix fusion of unsigned byte comparisons with swapped operands.
    • +
    • ARM: Fix tonumber() argument check.
    • +
    • ARM: Fix modulo operator and math.floor()/math.ceil() for inf/nan.
    • +
    • ARM: Invoke SPLIT pass for leftover IR_TOBIT.
    • +
    • ARM: Fix BASE register coalescing.
    • +
    • PPC: Fix interpreter state setup in callbacks.
    • +
    • PPC: Fix string.sub() range check.
    • +
    • MIPS: Support generation of MIPS/MIPSEL bytecode object files.
    • +
    • MIPS: Fix calls to floor()/ceil()/trunc().
    • +
    • ARM/PPC: Detect more target architecture variants.
    • +
    • ARM/PPC/e500/MIPS: Fix tailcalls from fast functions, esp. tostring().
    • +
    • ARM/PPC/MIPS: Fix rematerialization of FP constants.
    • +
    • FFI: Don't call FreeLibrary() on our own EXE/DLL.
    • +
    • FFI: Resolve metamethods for constructors, too.
    • +
    • FFI: Properly disable callbacks on iOS (would require executable memory).
    • +
    • FFI: Fix cdecl string parsing during recording.
    • +
    • FFI: Show address pointed to for tostring(ref), too.
    • +
    • FFI: Fix alignment of C call argument/return structure.
    • +
    • FFI: Initialize all fields of standard types.
    • +
    • FFI: Fix callback handling when new C types are declared in callback.
    • +
    • FFI: Fix recording of constructors for pointers.
    • +
    • FFI: Always resolve metamethods for pointers to structs.
    • +
    • FFI: Correctly propagate alignment when interning nested types.
    • +
  • +
  • Structural and performance enhancements: +
      +
    • Add allocation sinking and store sinking optimization.
    • +
    • Constify immutable upvalues.
    • +
    • Add builtin string to integer or FP number conversion. Improves cross-platform consistency and correctness.
    • +
    • Create string hash slots in template tables for non-const values, too. Avoids later table resizes.
    • +
    • Eliminate HREFK guard for template table references.
    • +
    • Add various new FOLD rules.
    • +
    • Don't use stack unwinding for lua_yield() (slow on x64).
    • +
    • ARM, PPC, MIPS: Improve XLOAD operand fusion and register hinting.
    • +
    • PPC, MIPS: Compile math.sqrt() to sqrt instruction, if available.
    • +
    • FFI: Fold KPTR + constant offset in SPLIT pass.
    • +
    • FFI: Optimize/inline ffi.copy() and ffi.fill().
    • +
    • FFI: Compile and optimize array/struct copies.
    • +
    • FFI: Compile ffi.typeof(cdata|ctype), ffi.sizeof(), ffi.alignof(), ffi.offsetof() and ffi.gc().
    • +
  • +
+ +

LuaJIT 2.0.0-beta10 — 2012-05-09

+
    +
  • New features: +
      +
    • The MIPS of LuaJIT is complete. It requires a CPU conforming to the +MIPS32 R1 architecture with hardware FPU. O32 hard-fp ABI, +little-endian or big-endian.
    • +
    • Auto-detect target arch via cross-compiler. No need for +TARGET=arch anymore.
    • +
    • Make DynASM compatible with Lua 5.2.
    • +
    • From Lua 5.2: Try __tostring metamethod on non-string error +messages..
    • +
  • +
  • Correctness and completeness: +
      +
    • Fix parsing of hex literals with exponents.
    • +
    • Fix bytecode dump for certain number constants.
    • +
    • Fix argument type in error message for relative arguments.
    • +
    • Fix argument error handling on Lua stacks without a frame.
    • +
    • Add missing mcode limit check in assembler backend.
    • +
    • Fix compilation on OpenBSD.
    • +
    • Avoid recursive GC steps after GC-triggered trace exit.
    • +
    • Replace <unwind.h> definitions with our own.
    • +
    • Fix OSX build issues. Bump minimum required OSX version to 10.4.
    • +
    • Fix discharge order of comparisons in Lua parser.
    • +
    • Ensure running __gc of userdata created in __gc +at state close.
    • +
    • Limit number of userdata __gc separations at state close.
    • +
    • Fix bytecode JMP slot range when optimizing +and/or with constant LHS.
    • +
    • Fix DSE of USTORE.
    • +
    • Make lua_concat() work from C hook with partial frame.
    • +
    • Add required PHIs for implicit conversions, e.g. via XREF +forwarding.
    • +
    • Add more comparison variants to Valgrind suppressions file.
    • +
    • Disable loading bytecode with an extra header (BOM or #!).
    • +
    • Fix PHI stack slot syncing.
    • +
    • ARM: Reorder type/value tests to silence Valgrind.
    • +
    • ARM: Fix register allocation for ldrd-optimized +HREFK.
    • +
    • ARM: Fix conditional branch fixup for OBAR.
    • +
    • ARM: Invoke SPLIT pass for double args in FFI call.
    • +
    • ARM: Handle all CALL* ops with double results in +SPLIT pass.
    • +
    • ARM: Fix rejoin of POW in SPLIT pass.
    • +
    • ARM: Fix compilation of math.sinh, math.cosh, +math.tanh.
    • +
    • ARM, PPC: Avoid pointless arg clearing in BC_IFUNCF.
    • +
    • PPC: Fix resume after yield from hook.
    • +
    • PPC: Fix argument checking for rawget().
    • +
    • PPC: Fix fusion of floating-point XLOAD/XSTORE.
    • +
    • PPC: Fix HREFK code generation for huge tables.
    • +
    • PPC: Use builtin D-Cache/I-Cache sync code.
    • +
  • +
  • FFI library: +
      +
    • Ignore empty statements in ffi.cdef().
    • +
    • Ignore number parsing errors while skipping definitions.
    • +
    • Don't touch frame in callbacks with tailcalls to fast functions.
    • +
    • Fix library unloading on POSIX systems.
    • +
    • Finalize cdata before userdata when closing the state.
    • +
    • Change ffi.load() library name resolution for Cygwin.
    • +
    • Fix resolving of function name redirects on Windows/x86.
    • +
    • Fix symbol resolving error messages on Windows.
    • +
    • Fix blacklisting of C functions calling callbacks.
    • +
    • Fix result type of pointer difference.
    • +
    • Use correct PC in FFI metamethod error message.
    • +
    • Allow 'typedef _Bool int BOOL;' for the Windows API.
    • +
    • Don't record test for bool result of call, if ignored.
    • +
  • +
+ +

LuaJIT 2.0.0-beta9 — 2011-12-14

+
    +
  • New features: +
      +
    • PPC port of LuaJIT is complete. Default is the dual-number port +(usually faster). Single-number port selectable via src/Makefile +at build time.
    • +
    • Add FFI callback support.
    • +
    • Extend -b to generate .c, .h or .obj/.o +files with embedded bytecode.
    • +
    • Allow loading embedded bytecode with require().
    • +
    • From Lua 5.2: Change to '\z' escape. Reject undefined escape +sequences.
    • +
  • +
  • Correctness and completeness: +
      +
    • Fix OSX 10.7 build. Fix install_name and versioning on OSX.
    • +
    • Fix iOS build.
    • +
    • Install dis_arm.lua, too.
    • +
    • Mark installed shared library as executable.
    • +
    • Add debug option to msvcbuild.bat and improve error handling.
    • +
    • Fix data-flow analysis for iterators.
    • +
    • Fix forced unwinding triggered by external unwinder.
    • +
    • Record missing for loop slot loads (return to lower frame).
    • +
    • Always use ANSI variants of Windows system functions.
    • +
    • Fix GC barrier for multi-result table constructor (TSETM).
    • +
    • Fix/add various FOLD rules.
    • +
    • Add potential PHI for number conversions due to type instability.
    • +
    • Do not eliminate PHIs only referenced from other PHIs.
    • +
    • Correctly anchor implicit number to string conversions in Lua/C API.
    • +
    • Fix various stack limit checks.
    • +
    • x64: Use thread-safe exceptions for external unwinding (GCC platforms).
    • +
    • x64: Fix result type of cdata index conversions.
    • +
    • x64: Fix math.random() and bit.bswap() code generation.
    • +
    • x64: Fix lightuserdata comparisons.
    • +
    • x64: Always extend stack-passed arguments to pointer size.
    • +
    • ARM: Many fixes to code generation backend.
    • +
    • PPC/e500: Fix dispatch for binop metamethods.
    • +
    • PPC/e500: Save/restore condition registers when entering/leaving the VM.
    • +
    • PPC/e500: Fix write barrier in stores of strings to upvalues.
    • +
  • +
  • FFI library: +
      +
    • Fix C comment parsing.
    • +
    • Fix snapshot optimization for cdata comparisons.
    • +
    • Fix recording of const/enum lookups in namespaces.
    • +
    • Fix call argument and return handling for I8/U8/I16/U16 types.
    • +
    • Fix unfused loads of float fields.
    • +
    • Fix ffi.string() recording.
    • +
    • Save GetLastError() around ffi.load() and symbol +resolving, too.
    • +
    • Improve ld script detection in ffi.load().
    • +
    • Record loads/stores to external variables in namespaces.
    • +
    • Compile calls to stdcall, fastcall and vararg functions.
    • +
    • Treat function ctypes like pointers in comparisons.
    • +
    • Resolve __call metamethod for pointers, too.
    • +
    • Record C function calls with bool return values.
    • +
    • Record ffi.errno().
    • +
    • x86: Fix number to uint32_t conversion rounding.
    • +
    • x86: Fix 64 bit arithmetic in assembler backend.
    • +
    • x64: Fix struct-by-value calling conventions.
    • +
    • ARM: Ensure invocation of SPLIT pass for float conversions.
    • +
  • +
  • Structural and performance enhancements: +
      +
    • Display trace types with -jv and -jdump.
    • +
    • Record isolated calls. But prefer recording loops over calls.
    • +
    • Specialize to prototype for non-monomorphic functions. Solves the +trace-explosion problem for closure-heavy programming styles.
    • +
    • Always generate a portable vmdef.lua. Easier for distros.
    • +
  • +
+ +

LuaJIT 2.0.0-beta8 — 2011-06-23

+
    +
  • New features: +
      +
    • Soft-float ARM port of LuaJIT is complete.
    • +
    • Add support for bytecode loading/saving and -b command line +option.
    • +
    • From Lua 5.2: __len metamethod for tables +(disabled by default).
    • +
  • +
  • Correctness and completeness: +
      +
    • ARM: Misc. fixes for interpreter.
    • +
    • x86/x64: Fix bit.* argument checking in interpreter.
    • +
    • Catch early out-of-memory in memory allocator initialization.
    • +
    • Fix data-flow analysis for paths leading to an upvalue close.
    • +
    • Fix check for missing arguments in string.format().
    • +
    • Fix Solaris/x86 build (note: not a supported target).
    • +
    • Fix recording of loops with instable directions in side traces.
    • +
    • x86/x64: Fix fusion of comparisons with u8/u16 +XLOAD.
    • +
    • x86/x64: Fix register allocation for variable shifts.
    • +
  • +
  • FFI library: +
      +
    • Add ffi.errno(). Save errno/GetLastError() +around allocations etc.
    • +
    • Fix __gc for VLA/VLS cdata objects.
    • +
    • Fix recording of casts from 32 bit cdata pointers to integers.
    • +
    • tonumber(cdata) returns nil for non-numbers.
    • +
    • Show address pointed to for tostring(pointer).
    • +
    • Print NULL pointers as "cdata<... *>: NULL".
    • +
    • Support __tostring metamethod for pointers to structs, too.
    • +
  • +
  • Structural and performance enhancements: +
      +
    • More tuning for loop unrolling heuristics.
    • +
    • Flatten and compress in-memory debug info (saves ~70%).
    • +
  • +
+ +

LuaJIT 2.0.0-beta7 — 2011-05-05

+
    +
  • New features: +
      +
    • ARM port of the LuaJIT interpreter is complete.
    • +
    • FFI library: Add ffi.gc(), ffi.metatype(), +ffi.istype().
    • +
    • FFI library: Resolve ld script redirection in ffi.load().
    • +
    • From Lua 5.2: package.searchpath(), fp:read("*L"), +load(string).
    • +
    • From Lua 5.2, disabled by default: empty statement, +table.unpack(), modified coroutine.running().
    • +
  • +
  • Correctness and completeness: +
      +
    • FFI library: numerous fixes.
    • +
    • Fix type mismatches in store-to-load forwarding.
    • +
    • Fix error handling within metamethods.
    • +
    • Fix table.maxn().
    • +
    • Improve accuracy of x^-k on x64.
    • +
    • Fix code generation for Intel Atom in x64 mode.
    • +
    • Fix narrowing of POW.
    • +
    • Fix recording of retried fast functions.
    • +
    • Fix code generation for bit.bnot() and multiplies.
    • +
    • Fix error location within cpcall frames.
    • +
    • Add workaround for old libgcc unwind bug.
    • +
    • Fix lua_yield() and getmetatable(lightuserdata) on x64.
    • +
    • Misc. fixes for PPC/e500 interpreter.
    • +
    • Fix stack slot updates for down-recursion.
    • +
  • +
  • Structural and performance enhancements: +
      +
    • Add dual-number mode (int/double) for the VM. Enabled for ARM.
    • +
    • Improve narrowing of arithmetic operators and for loops.
    • +
    • Tune loop unrolling heuristics and increase trace recorder limits.
    • +
    • Eliminate dead slots in snapshots using bytecode data-flow analysis.
    • +
    • Avoid phantom stores to proxy tables.
    • +
    • Optimize lookups in empty proxy tables.
    • +
    • Improve bytecode optimization of and/or operators.
    • +
  • +
+ +

LuaJIT 2.0.0-beta6 — 2011-02-11

+
    +
  • New features: +
      +
    • PowerPC/e500v2 port of the LuaJIT interpreter is complete.
    • +
    • Various minor features from Lua 5.2: Hex escapes in literals, +'\*' escape, reversible string.format("%q",s), +"%g" pattern, table.sort checks callbacks, +os.exit(status|true|false[,close]).
    • +
    • Lua 5.2 __pairs and __ipairs metamethods +(disabled by default).
    • +
    • Initial release of the FFI library.
    • +
  • +
  • Correctness and completeness: +
      +
    • Fix string.format() for non-finite numbers.
    • +
    • Fix memory leak when compiled to use the built-in allocator.
    • +
    • x86/x64: Fix unnecessary resize in TSETM bytecode.
    • +
    • Fix various GC issues with traces and jit.flush().
    • +
    • x64: Fix fusion of indexes for array references.
    • +
    • x86/x64: Fix stack overflow handling for coroutine results.
    • +
    • Enable low-2GB memory allocation on FreeBSD/x64.
    • +
    • Fix collectgarbage("count") result if more than 2GB is in use.
    • +
    • Fix parsing of hex floats.
    • +
    • x86/x64: Fix loop branch inversion with trailing +HREF+NE/EQ.
    • +
    • Add jit.os string.
    • +
    • coroutine.create() permits running C functions, too.
    • +
    • Fix OSX build to work with newer ld64 versions.
    • +
    • Fix bytecode optimization of and/or operators.
    • +
  • +
  • Structural and performance enhancements: +
      +
    • Emit specialized bytecode for pairs()/next().
    • +
    • Improve bytecode coalescing of nil constants.
    • +
    • Compile calls to vararg functions.
    • +
    • Compile select().
    • +
    • Improve alias analysis, esp. for loads from allocations.
    • +
    • Tuning of various compiler heuristics.
    • +
    • Refactor and extend IR conversion instructions.
    • +
    • x86/x64: Various backend enhancements related to the FFI.
    • +
    • Add SPLIT pass to split 64 bit IR instructions for 32 bit CPUs.
    • +
  • +
+ +

LuaJIT 2.0.0-beta5 — 2010-08-24

+
    +
  • Correctness and completeness: +
      +
    • Fix trace exit dispatch to function headers.
    • +
    • Fix Windows and OSX builds with LUAJIT_DISABLE_JIT.
    • +
    • Reorganize and fix placement of generated machine code on x64.
    • +
    • Fix TNEW in x64 interpreter.
    • +
    • Do not eliminate PHIs for values only referenced from side exits.
    • +
    • OS-independent canonicalization of strings for non-finite numbers.
    • +
    • Fix string.char() range check on x64.
    • +
    • Fix tostring() resolving within print().
    • +
    • Fix error handling for next().
    • +
    • Fix passing of constant arguments to external calls on x64.
    • +
    • Fix interpreter argument check for two-argument SSE math functions.
    • +
    • Fix C frame chain corruption caused by lua_cpcall().
    • +
    • Fix return from pcall() within active hook.
    • +
  • +
  • Structural and performance enhancements: +
      +
    • Replace on-trace GC frame syncing with interpreter exit.
    • +
    • Improve hash lookup specialization by not removing dead keys during GC.
    • +
    • Turn traces into true GC objects.
    • +
    • Avoid starting a GC cycle immediately after library init.
    • +
    • Add weak guards to improve dead-code elimination.
    • +
    • Speed up string interning.
    • +
  • +
+ +

LuaJIT 2.0.0-beta4 — 2010-03-28

+
    +
  • Correctness and completeness: +
      +
    • Fix precondition for on-trace creation of table keys.
    • +
    • Fix {f()} on x64 when table is resized.
    • +
    • Fix folding of ordered comparisons with same references.
    • +
    • Fix snapshot restores for multi-result bytecodes.
    • +
    • Fix potential hang when recording bytecode with nested closures.
    • +
    • Fix recording of getmetatable(), tonumber() and bad argument types.
    • +
    • Fix SLOAD fusion across returns to lower frames.
    • +
  • +
  • Structural and performance enhancements: +
      +
    • Add array bounds check elimination. -Oabc is enabled by default.
    • +
    • More tuning for x64, e.g. smaller table objects.
    • +
  • +
+ +

LuaJIT 2.0.0-beta3 — 2010-03-07

+
    +
  • LuaJIT x64 port: +
      +
    • Port integrated memory allocator to Linux/x64, Windows/x64 and OSX/x64.
    • +
    • Port interpreter and JIT compiler to x64.
    • +
    • Port DynASM to x64.
    • +
    • Many 32/64 bit cleanups in the VM.
    • +
    • Allow building the interpreter with either x87 or SSE2 arithmetics.
    • +
    • Add external unwinding and C++ exception interop (default on x64).
    • +
  • +
  • Correctness and completeness: +
      +
    • Fix constructor bytecode generation for certain conditional values.
    • +
    • Fix some cases of ordered string comparisons.
    • +
    • Fix lua_tocfunction().
    • +
    • Fix cutoff register in JMP bytecode for some conditional expressions.
    • +
    • Fix PHI marking algorithm for references from variant slots.
    • +
    • Fix package.cpath for non-default PREFIX.
    • +
    • Fix DWARF2 frame unwind information for interpreter on OSX.
    • +
    • Drive the GC forward on string allocations in the parser.
    • +
    • Implement call/return hooks (zero-cost if disabled).
    • +
    • Implement yield from C hooks.
    • +
    • Disable JIT compiler on older non-SSE2 CPUs instead of aborting.
    • +
  • +
  • Structural and performance enhancements: +
      +
    • Compile recursive code (tail-, up- and down-recursion).
    • +
    • Improve heuristics for bytecode penalties and blacklisting.
    • +
    • Split CALL/FUNC recording and clean up fast function call semantics.
    • +
    • Major redesign of internal function call handling.
    • +
    • Improve FOR loop const specialization and integerness checks.
    • +
    • Switch to pre-initialized stacks. Avoid frame-clearing.
    • +
    • Colocation of prototypes and related data: bytecode, constants, debug info.
    • +
    • Cleanup parser and streamline bytecode generation.
    • +
    • Add support for weak IR references to register allocator.
    • +
    • Switch to compressed, extensible snapshots.
    • +
    • Compile returns to frames below the start frame.
    • +
    • Improve alias analysis of upvalues using a disambiguation hash value.
    • +
    • Compile floor/ceil/trunc to SSE2 helper calls or SSE4.1 instructions.
    • +
    • Add generic C call handling to IR and backend.
    • +
    • Improve KNUM fuse vs. load heuristics.
    • +
    • Compile various io.*() functions.
    • +
    • Compile math.sinh(), math.cosh(), math.tanh() +and math.random().
    • +
  • +
+ +

LuaJIT 2.0.0-beta2 — 2009-11-09

+
    +
  • Reorganize build system. Build static+shared library on POSIX.
  • +
  • Allow C++ exception conversion on all platforms +using a wrapper function.
  • +
  • Automatically catch C++ exceptions and rethrow Lua error +(DWARF2 only).
  • +
  • Check for the correct x87 FPU precision at strategic points.
  • +
  • Always use wrappers for libm functions.
  • +
  • Resurrect metamethod name strings before copying them.
  • +
  • Mark current trace, even if compiler is idle.
  • +
  • Ensure FILE metatable is created only once.
  • +
  • Fix type comparisons when different integer types are involved.
  • +
  • Fix getmetatable() recording.
  • +
  • Fix TDUP with dead keys in template table.
  • +
  • jit.flush(tr) returns status. +Prevent manual flush of a trace that's still linked.
  • +
  • Improve register allocation heuristics for invariant references.
  • +
  • Compile the push/pop variants of table.insert() and +table.remove().
  • +
  • Compatibility with MSVC link /debug.
  • +
  • Fix lua_iscfunction().
  • +
  • Fix math.random() when compiled with -fpic (OSX).
  • +
  • Fix table.maxn().
  • +
  • Bump MACOSX_DEPLOYMENT_TARGET to 10.4
  • +
  • luaL_check*() and luaL_opt*() now support +negative arguments, too.
    +This matches the behavior of Lua 5.1, but not the specification.
  • +
+ +

LuaJIT 2.0.0-beta1 — 2009-10-31

+
    +
  • This is the first public release of LuaJIT 2.0.
  • +
  • The whole VM has been rewritten from the ground up, so there's +no point in listing differences over earlier versions.
  • +
+
+ +
+

LuaJIT 1.1.8 — 2012-04-16

+ + +

LuaJIT 1.1.7 — 2011-05-05

+ + +

LuaJIT 1.1.6 — 2010-03-28

+
    +
  • Added fixes for the +» currently known bugs in Lua 5.1.4.
  • +
  • Removed wrong GC check in jit_createstate(). +Thanks to Tim Mensch.
  • +
  • Fixed bad assertions while compiling table.insert() and +table.remove().
  • +
+ +

LuaJIT 1.1.5 — 2008-10-25

+ + +

LuaJIT 1.1.4 — 2008-02-05

+
    +
  • Merged with Lua 5.1.3. Fixes all +» known bugs in Lua 5.1.2.
  • +
  • Fixed possible (but unlikely) stack corruption while compiling +k^x expressions.
  • +
  • Fixed DynASM template for cmpss instruction.
  • +
+ +

LuaJIT 1.1.3 — 2007-05-24

+
    +
  • Merged with Lua 5.1.2. Fixes all +» known bugs in Lua 5.1.1.
  • +
  • Merged pending Lua 5.1.x fixes: "return -nil" bug, spurious count hook call.
  • +
  • Remove a (sometimes) wrong assertion in luaJIT_findpc().
  • +
  • DynASM now allows labels for displacements and .aword.
  • +
  • Fix some compiler warnings for DynASM glue (internal API change).
  • +
  • Correct naming for SSSE3 (temporarily known as SSE4) in DynASM and x86 disassembler.
  • +
  • The loadable debug modules now handle redirection to stdout +(e.g. -j trace=-).
  • +
+ +

LuaJIT 1.1.2 — 2006-06-24

+
    +
  • Fix MSVC inline assembly: use only local variables with +lua_number2int().
  • +
  • Fix "attempt to call a thread value" bug on Mac OS X: +make values of consts used as lightuserdata keys unique +to avoid joining by the compiler/linker.
  • +
+ +

LuaJIT 1.1.1 — 2006-06-20

+
    +
  • Merged with Lua 5.1.1. Fixes all +» known bugs in Lua 5.1.
  • +
  • Enforce (dynamic) linker error for EXE/DLL version mismatches.
  • +
  • Minor changes to DynASM: faster pre-processing, smaller encoding +for some immediates.
  • +
+

+This release is in sync with Coco 1.1.1 (see the +» Coco Change History). +

+ +

LuaJIT 1.1.0 — 2006-03-13

+
    +
  • Merged with Lua 5.1 (final).
  • + +
  • New JIT call frame setup: +
      +
    • The C stack is kept 16 byte aligned (faster). +Mandatory for Mac OS X on Intel, too.
    • +
    • Faster calling conventions for internal C helper functions.
    • +
    • Better instruction scheduling for function prologue, OP_CALL and +OP_RETURN.
    • +
  • + +
  • Miscellaneous optimizations: +
      +
    • Faster loads of FP constants. Remove narrow-to-wide store-to-load +forwarding stalls.
    • +
    • Use (scalar) SSE2 ops (if the CPU supports it) to speed up slot moves +and FP to integer conversions.
    • +
    • Optimized the two-argument form of OP_CONCAT (a..b).
    • +
    • Inlined OP_MOD (a%b). +With better accuracy than the C variant, too.
    • +
    • Inlined OP_POW (a^b). Unroll x^k or +use k^x = 2^(log2(k)*x) or call pow().
    • +
  • + +
  • Changes in the optimizer: +
      +
    • Improved hinting for table keys derived from table values +(t1[t2[x]]).
    • +
    • Lookup hinting now works with arbitrary object types and +supports index chains, too.
    • +
    • Generate type hints for arithmetic and comparison operators, +OP_LEN, OP_CONCAT and OP_FORPREP.
    • +
    • Remove several hint definitions in favour of a generic COMBINE hint.
    • +
    • Complete rewrite of jit.opt_inline module +(ex jit.opt_lib).
    • +
  • + +
  • Use adaptive deoptimization: +
      +
    • If runtime verification of a contract fails, the affected +instruction is recompiled and patched on-the-fly. +Regular programs will trigger deoptimization only occasionally.
    • +
    • This avoids generating code for uncommon fallback cases +most of the time. Generated code is up to 30% smaller compared to +LuaJIT 1.0.3.
    • +
    • Deoptimization is used for many opcodes and contracts: +
        +
      • OP_CALL, OP_TAILCALL: type mismatch for callable.
      • +
      • Inlined calls: closure mismatch, parameter number and type mismatches.
      • +
      • OP_GETTABLE, OP_SETTABLE: table or key type and range mismatches.
      • +
      • All arithmetic and comparison operators, OP_LEN, OP_CONCAT, +OP_FORPREP: operand type and range mismatches.
      • +
    • +
    • Complete redesign of the debug and traceback info +(bytecode ↔ mcode) to support deoptimization. +Much more flexible and needs only 50% of the space.
    • +
    • The modules jit.trace, jit.dumphints and +jit.dump handle deoptimization.
    • +
  • + +
  • Inlined many popular library functions +(for commonly used arguments only): +
      +
    • Most math.* functions (the 18 most used ones) +[2x-10x faster].
    • +
    • string.len, string.sub and string.char +[2x-10x faster].
    • +
    • table.insert, table.remove and table.getn +[3x-5x faster].
    • +
    • coroutine.yield and coroutine.resume +[3x-5x faster].
    • +
    • pairs, ipairs and the corresponding iterators +[8x-15x faster].
    • +
  • + +
  • Changes in the core and loadable modules and the stand-alone executable: +
      +
    • Added jit.version, jit.version_num +and jit.arch.
    • +
    • Reorganized some internal API functions (jit.util.*mcode*).
    • +
    • The -j dump output now shows JSUB names, too.
    • +
    • New x86 disassembler module written in pure Lua. No dependency +on ndisasm anymore. Flexible API, very compact (500 lines) +and complete (x87, MMX, SSE, SSE2, SSE3, SSSE3, privileged instructions).
    • +
    • luajit -v prints the LuaJIT version and copyright +on a separate line.
    • +
  • + +
  • Added SSE, SSE2, SSE3 and SSSE3 support to DynASM.
  • +
  • Miscellaneous doc changes. Added a section about +embedding LuaJIT.
  • +
+

+This release is in sync with Coco 1.1.0 (see the +» Coco Change History). +

+
+ +
+

LuaJIT 1.0.3 — 2005-09-08

+
    +
  • Even more docs.
  • +
  • Unified closure checks in jit.*.
  • +
  • Fixed some range checks in jit.util.*.
  • +
  • Fixed __newindex call originating from jit_settable_str().
  • +
  • Merged with Lua 5.1 alpha (including early bug fixes).
  • +
+

+This is the first public release of LuaJIT. +

+ +

LuaJIT 1.0.2 — 2005-09-02

+
    +
  • Add support for flushing the Valgrind translation cache
    +(MYCFLAGS= -DUSE_VALGRIND).
  • +
  • Add support for freeing executable mcode memory to the mmap()-based +variant for POSIX systems.
  • +
  • Reorganized the C function signature handling in +jit.opt_lib.
  • +
  • Changed to index-based hints for inlining C functions. +Still no support in the backend for inlining.
  • +
  • Hardcode HEAP_CREATE_ENABLE_EXECUTE value if undefined.
  • +
  • Misc. changes to the jit.* modules.
  • +
  • Misc. changes to the Makefiles.
  • +
  • Lots of new docs.
  • +
  • Complete doc reorg.
  • +
+

+Not released because Lua 5.1 alpha came out today. +

+ +

LuaJIT 1.0.1 — 2005-08-31

+
    +
  • Missing GC step in OP_CONCAT.
  • +
  • Fix result handling for C –> JIT calls.
  • +
  • Detect CPU feature bits.
  • +
  • Encode conditional moves (fucomip) only when supported.
  • +
  • Add fallback instructions for FP compares.
  • +
  • Add support for LUA_COMPAT_VARARG. Still disabled by default.
  • +
  • MSVC needs a specific place for the CALLBACK attribute +(David Burgess).
  • +
  • Misc. doc updates.
  • +
+

+Interim non-public release. +Special thanks to Adam D. Moss for reporting most of the bugs. +

+ +

LuaJIT 1.0.0 — 2005-08-29

+

+This is the initial non-public release of LuaJIT. +

+
+
+
+ + + diff --git a/external/lua/luajit/src/doc/contact.html b/external/lua/luajit/src/doc/contact.html new file mode 100644 index 0000000000..4735faf440 --- /dev/null +++ b/external/lua/luajit/src/doc/contact.html @@ -0,0 +1,102 @@ + + + +Contact + + + + + + + + +
+Lua +
+ + +
+

+Please send general questions to the +» LuaJIT mailing list. +You can also send any questions you have directly to me: +

+ + + + + +

Copyright

+

+All documentation is +Copyright © 2005-2013 Mike Pall. +

+ + +
+
+ + + diff --git a/external/lua/luajit/src/doc/ext_c_api.html b/external/lua/luajit/src/doc/ext_c_api.html new file mode 100644 index 0000000000..c6feb8e16c --- /dev/null +++ b/external/lua/luajit/src/doc/ext_c_api.html @@ -0,0 +1,187 @@ + + + +Lua/C API Extensions + + + + + + + + +
+Lua +
+ + +
+

+LuaJIT adds some extensions to the standard Lua/C API. The LuaJIT include +directory must be in the compiler search path (-Ipath) +to be able to include the required header for C code: +

+
+#include "luajit.h"
+
+

+Or for C++ code: +

+
+#include "lua.hpp"
+
+ +

luaJIT_setmode(L, idx, mode) +— Control VM

+

+This is a C API extension to allow control of the VM from C code. The +full prototype of LuaJIT_setmode is: +

+
+LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
+
+

+The returned status is either success (1) or failure (0). +The second argument is either 0 or a stack index (similar to the +other Lua/C API functions). +

+

+The third argument specifies the mode, which is 'or'ed with a flag. +The flag can be LUAJIT_MODE_OFF to turn a feature on, +LUAJIT_MODE_ON to turn a feature off, or +LUAJIT_MODE_FLUSH to flush cached code. +

+

+The following modes are defined: +

+ +

luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|flag)

+

+Turn the whole JIT compiler on or off or flush the whole cache of compiled code. +

+ +

luaJIT_setmode(L, idx, LUAJIT_MODE_FUNC|flag)
+luaJIT_setmode(L, idx, LUAJIT_MODE_ALLFUNC|flag)
+luaJIT_setmode(L, idx, LUAJIT_MODE_ALLSUBFUNC|flag)

+

+This sets the mode for the function at the stack index idx or +the parent of the calling function (idx = 0). It either +enables JIT compilation for a function, disables it and flushes any +already compiled code or only flushes already compiled code. This +applies recursively to all sub-functions of the function with +LUAJIT_MODE_ALLFUNC or only to the sub-functions with +LUAJIT_MODE_ALLSUBFUNC. +

+ +

luaJIT_setmode(L, trace,
+  LUAJIT_MODE_TRACE|LUAJIT_MODE_FLUSH)

+

+Flushes the specified root trace and all of its side traces from the cache. +The code for the trace will be retained as long as there are any other +traces which link to it. +

+ +

luaJIT_setmode(L, idx, LUAJIT_MODE_WRAPCFUNC|flag)

+

+This mode defines a wrapper function for calls to C functions. If +called with LUAJIT_MODE_ON, the stack index at idx +must be a lightuserdata object holding a pointer to the wrapper +function. From now on all C functions are called through the wrapper +function. If called with LUAJIT_MODE_OFF this mode is turned +off and all C functions are directly called. +

+

+The wrapper function can be used for debugging purposes or to catch +and convert foreign exceptions. But please read the section on +C++ exception interoperability +first. Recommended usage can be seen in this C++ code excerpt: +

+
+#include <exception>
+#include "lua.hpp"
+
+// Catch C++ exceptions and convert them to Lua error messages.
+// Customize as needed for your own exception classes.
+static int wrap_exceptions(lua_State *L, lua_CFunction f)
+{
+  try {
+    return f(L);  // Call wrapped function and return result.
+  } catch (const char *s) {  // Catch and convert exceptions.
+    lua_pushstring(L, s);
+  } catch (std::exception& e) {
+    lua_pushstring(L, e.what());
+  } catch (...) {
+    lua_pushliteral(L, "caught (...)");
+  }
+  return lua_error(L);  // Rethrow as a Lua error.
+}
+
+static int myinit(lua_State *L)
+{
+  ...
+  // Define wrapper function and enable it.
+  lua_pushlightuserdata(L, (void *)wrap_exceptions);
+  luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
+  lua_pop(L, 1);
+  ...
+}
+
+

+Note that you can only define a single global wrapper function, +so be careful when using this mechanism from multiple C++ modules. +Also note that this mechanism is not without overhead. +

+
+
+ + + diff --git a/external/lua/luajit/src/doc/ext_ffi.html b/external/lua/luajit/src/doc/ext_ffi.html new file mode 100644 index 0000000000..a146b055d8 --- /dev/null +++ b/external/lua/luajit/src/doc/ext_ffi.html @@ -0,0 +1,330 @@ + + + +FFI Library + + + + + + + + +
+Lua +
+ + +
+

+ +The FFI library allows calling external C functions and +using C data structures from pure Lua code. + +

+

+ +The FFI library largely obviates the need to write tedious manual +Lua/C bindings in C. No need to learn a separate binding language +— it parses plain C declarations! These can be +cut-n-pasted from C header files or reference manuals. It's up to +the task of binding large libraries without the need for dealing with +fragile binding generators. + +

+

+The FFI library is tightly integrated into LuaJIT (it's not available +as a separate module). The code generated by the JIT-compiler for +accesses to C data structures from Lua code is on par with the +code a C compiler would generate. Calls to C functions can +be inlined in JIT-compiled code, unlike calls to functions bound via +the classic Lua/C API. +

+

+This page gives a short introduction to the usage of the FFI library. +Please use the FFI sub-topics in the navigation bar to learn more. +

+ +

Motivating Example: Calling External C Functions

+

+It's really easy to call an external C library function: +

+
+①
+②
+
+
+③local ffi = require("ffi")
+ffi.cdef[[
+int printf(const char *fmt, ...);
+]]
+ffi.C.printf("Hello %s!", "world")
+
+

+So, let's pick that apart: +

+

+ Load the FFI library. +

+

+ Add a C declaration +for the function. The part inside the double-brackets (in green) is +just standard C syntax. +

+

+ Call the named +C function — Yes, it's that simple! +

+

+Actually, what goes on behind the scenes is far from simple: makes use of the standard +C library namespace ffi.C. Indexing this namespace with +a symbol name ("printf") automatically binds it to the +standard C library. The result is a special kind of object which, +when called, runs the printf function. The arguments passed +to this function are automatically converted from Lua objects to the +corresponding C types. +

+

+Ok, so maybe the use of printf() wasn't such a spectacular +example. You could have done that with io.write() and +string.format(), too. But you get the idea ... +

+

+So here's something to pop up a message box on Windows: +

+
+local ffi = require("ffi")
+ffi.cdef[[
+int MessageBoxA(void *w, const char *txt, const char *cap, int type);
+]]
+ffi.C.MessageBoxA(nil, "Hello world!", "Test", 0)
+
+

+Bing! Again, that was far too easy, no? +

+

+Compare this with the effort required to bind that function using the +classic Lua/C API: create an extra C file, add a C function +that retrieves and checks the argument types passed from Lua and calls +the actual C function, add a list of module functions and their +names, add a luaopen_* function and register all module +functions, compile and link it into a shared library (DLL), move it to +the proper path, add Lua code that loads the module aaaand ... finally +call the binding function. Phew! +

+ +

Motivating Example: Using C Data Structures

+

+The FFI library allows you to create and access C data +structures. Of course the main use for this is for interfacing with +C functions. But they can be used stand-alone, too. +

+

+Lua is built upon high-level data types. They are flexible, extensible +and dynamic. That's why we all love Lua so much. Alas, this can be +inefficient for certain tasks, where you'd really want a low-level +data type. E.g. a large array of a fixed structure needs to be +implemented with a big table holding lots of tiny tables. This imposes +both a substantial memory overhead as well as a performance overhead. +

+

+Here's a sketch of a library that operates on color images plus a +simple benchmark. First, the plain Lua version: +

+
+local floor = math.floor
+
+local function image_ramp_green(n)
+  local img = {}
+  local f = 255/(n-1)
+  for i=1,n do
+    img[i] = { red = 0, green = floor((i-1)*f), blue = 0, alpha = 255 }
+  end
+  return img
+end
+
+local function image_to_grey(img, n)
+  for i=1,n do
+    local y = floor(0.3*img[i].red + 0.59*img[i].green + 0.11*img[i].blue)
+    img[i].red = y; img[i].green = y; img[i].blue = y
+  end
+end
+
+local N = 400*400
+local img = image_ramp_green(N)
+for i=1,1000 do
+  image_to_grey(img, N)
+end
+
+

+This creates a table with 160.000 pixels, each of which is a table +holding four number values in the range of 0-255. First an image with +a green ramp is created (1D for simplicity), then the image is +converted to greyscale 1000 times. Yes, that's silly, but I was in +need of a simple example ... +

+

+And here's the FFI version. The modified parts have been marked in +bold: +

+
+①
+
+
+
+
+
+②
+
+③
+④
+
+
+
+
+
+
+③
+⑤local ffi = require("ffi")
+ffi.cdef[[
+typedef struct { uint8_t red, green, blue, alpha; } rgba_pixel;
+]]
+
+local function image_ramp_green(n)
+  local img = ffi.new("rgba_pixel[?]", n)
+  local f = 255/(n-1)
+  for i=0,n-1 do
+    img[i].green = i*f
+    img[i].alpha = 255
+  end
+  return img
+end
+
+local function image_to_grey(img, n)
+  for i=0,n-1 do
+    local y = 0.3*img[i].red + 0.59*img[i].green + 0.11*img[i].blue
+    img[i].red = y; img[i].green = y; img[i].blue = y
+  end
+end
+
+local N = 400*400
+local img = image_ramp_green(N)
+for i=1,1000 do
+  image_to_grey(img, N)
+end
+
+

+Ok, so that wasn't too difficult: +

+

+ First, load the FFI +library and declare the low-level data type. Here we choose a +struct which holds four byte fields, one for each component +of a 4x8 bit RGBA pixel. +

+

+ Creating the data +structure with ffi.new() is straightforward — the +'?' is a placeholder for the number of elements of a +variable-length array. +

+

+ C arrays are +zero-based, so the indexes have to run from 0 to +n-1. One might want to allocate one more element instead to +simplify converting legacy code. +

+

+ Since ffi.new() +zero-fills the array by default, we only need to set the green and the +alpha fields. +

+

+ The calls to +math.floor() can be omitted here, because floating-point +numbers are already truncated towards zero when converting them to an +integer. This happens implicitly when the number is stored in the +fields of each pixel. +

+

+Now let's have a look at the impact of the changes: first, memory +consumption for the image is down from 22 Megabytes to +640 Kilobytes (400*400*4 bytes). That's a factor of 35x less! So, +yes, tables do have a noticeable overhead. BTW: The original program +would consume 40 Megabytes in plain Lua (on x64). +

+

+Next, performance: the pure Lua version runs in 9.57 seconds (52.9 +seconds with the Lua interpreter) and the FFI version runs in 0.48 +seconds on my machine (YMMV). That's a factor of 20x faster (110x +faster than the Lua interpreter). +

+

+The avid reader may notice that converting the pure Lua version over +to use array indexes for the colors ([1] instead of +.red, [2] instead of .green etc.) ought to +be more compact and faster. This is certainly true (by a factor of +~1.7x). Switching to a struct-of-arrays would help, too. +

+

+However the resulting code would be less idiomatic and rather +error-prone. And it still doesn't get even close to the performance of +the FFI version of the code. Also, high-level data structures cannot +be easily passed to other C functions, especially I/O functions, +without undue conversion penalties. +

+
+
+ + + diff --git a/external/lua/luajit/src/doc/ext_ffi_api.html b/external/lua/luajit/src/doc/ext_ffi_api.html new file mode 100644 index 0000000000..8b2555b568 --- /dev/null +++ b/external/lua/luajit/src/doc/ext_ffi_api.html @@ -0,0 +1,566 @@ + + + +ffi.* API Functions + + + + + + + + + +
+Lua +
+ + +
+

+This page describes the API functions provided by the FFI library in +detail. It's recommended to read through the +introduction and the +FFI tutorial first. +

+ +

Glossary

+
    +
  • cdecl — An abstract C type declaration (a Lua +string).
  • +
  • ctype — A C type object. This is a special kind of +cdata returned by ffi.typeof(). It serves as a +cdata constructor when called.
  • +
  • cdata — A C data object. It holds a value of the +corresponding ctype.
  • +
  • ct — A C type specification which can be used for +most of the API functions. Either a cdecl, a ctype or a +cdata serving as a template type.
  • +
  • cb — A callback object. This is a C data object +holding a special function pointer. Calling this function from +C code runs an associated Lua function.
  • +
  • VLA — A variable-length array is declared with a +? instead of the number of elements, e.g. "int[?]". +The number of elements (nelem) must be given when it's +created.
  • +
  • VLS — A variable-length struct is a struct C +type where the last element is a VLA. The same rules for +declaration and creation apply.
  • +
+ +

Declaring and Accessing External Symbols

+

+External symbols must be declared first and can then be accessed by +indexing a C library +namespace, which automatically binds the symbol to a specific +library. +

+ +

ffi.cdef(def)

+

+Adds multiple C declarations for types or external symbols (named +variables or functions). def must be a Lua string. It's +recommended to use the syntactic sugar for string arguments as +follows: +

+
+ffi.cdef[[
+typedef struct foo { int a, b; } foo_t;  // Declare a struct and typedef.
+int dofoo(foo_t *f, int n);  /* Declare an external C function. */
+]]
+
+

+The contents of the string (the part in green above) must be a +sequence of +C declarations, +separated by semicolons. The trailing semicolon for a single +declaration may be omitted. +

+

+Please note that external symbols are only declared, but they +are not bound to any specific address, yet. Binding is +achieved with C library namespaces (see below). +

+

+C declarations are not passed through a C pre-processor, +yet. No pre-processor tokens are allowed, except for +#pragma pack. Replace #define in existing +C header files with enum, static const +or typedef and/or pass the files through an external +C pre-processor (once). Be careful not to include unneeded or +redundant declarations from unrelated header files. +

+ +

ffi.C

+

+This is the default C library namespace — note the +uppercase 'C'. It binds to the default set of symbols or +libraries on the target system. These are more or less the same as a +C compiler would offer by default, without specifying extra link +libraries. +

+

+On POSIX systems, this binds to symbols in the default or global +namespace. This includes all exported symbols from the executable and +any libraries loaded into the global namespace. This includes at least +libc, libm, libdl (on Linux), +libgcc (if compiled with GCC), as well as any exported +symbols from the Lua/C API provided by LuaJIT itself. +

+

+On Windows systems, this binds to symbols exported from the +*.exe, the lua51.dll (i.e. the Lua/C API +provided by LuaJIT itself), the C runtime library LuaJIT was linked +with (msvcrt*.dll), kernel32.dll, +user32.dll and gdi32.dll. +

+ +

clib = ffi.load(name [,global])

+

+This loads the dynamic library given by name and returns +a new C library namespace which binds to its symbols. On POSIX +systems, if global is true, the library symbols are +loaded into the global namespace, too. +

+

+If name is a path, the library is loaded from this path. +Otherwise name is canonicalized in a system-dependent way and +searched in the default search path for dynamic libraries: +

+

+On POSIX systems, if the name contains no dot, the extension +.so is appended. Also, the lib prefix is prepended +if necessary. So ffi.load("z") looks for "libz.so" +in the default shared library search path. +

+

+On Windows systems, if the name contains no dot, the extension +.dll is appended. So ffi.load("ws2_32") looks for +"ws2_32.dll" in the default DLL search path. +

+ +

Creating cdata Objects

+

+The following API functions create cdata objects (type() +returns "cdata"). All created cdata objects are +garbage collected. +

+ +

cdata = ffi.new(ct [,nelem] [,init...])
+cdata = ctype([nelem,] [init...])

+

+Creates a cdata object for the given ct. VLA/VLS types +require the nelem argument. The second syntax uses a ctype as +a constructor and is otherwise fully equivalent. +

+

+The cdata object is initialized according to the +rules for initializers, +using the optional init arguments. Excess initializers cause +an error. +

+

+Performance notice: if you want to create many objects of one kind, +parse the cdecl only once and get its ctype with +ffi.typeof(). Then use the ctype as a constructor repeatedly. +

+

+Please note that an anonymous struct declaration implicitly +creates a new and distinguished ctype every time you use it for +ffi.new(). This is probably not what you want, +especially if you create more than one cdata object. Different anonymous +structs are not considered assignment-compatible by the +C standard, even though they may have the same fields! Also, they +are considered different types by the JIT-compiler, which may cause an +excessive number of traces. It's strongly suggested to either declare +a named struct or typedef with ffi.cdef() +or to create a single ctype object for an anonymous struct +with ffi.typeof(). +

+ +

ctype = ffi.typeof(ct)

+

+Creates a ctype object for the given ct. +

+

+This function is especially useful to parse a cdecl only once and then +use the resulting ctype object as a constructor. +

+ +

cdata = ffi.cast(ct, init)

+

+Creates a scalar cdata object for the given ct. The cdata +object is initialized with init using the "cast" variant of +the C type conversion +rules. +

+

+This functions is mainly useful to override the pointer compatibility +checks or to convert pointers to addresses or vice versa. +

+ +

ctype = ffi.metatype(ct, metatable)

+

+Creates a ctype object for the given ct and associates it with +a metatable. Only struct/union types, complex numbers +and vectors are allowed. Other types may be wrapped in a +struct, if needed. +

+

+The association with a metatable is permanent and cannot be changed +afterwards. Neither the contents of the metatable nor the +contents of an __index table (if any) may be modified +afterwards. The associated metatable automatically applies to all uses +of this type, no matter how the objects are created or where they +originate from. Note that pre-defined operations on types have +precedence (e.g. declared field names cannot be overriden). +

+

+All standard Lua metamethods are implemented. These are called directly, +without shortcuts and on any mix of types. For binary operations, the +left operand is checked first for a valid ctype metamethod. The +__gc metamethod only applies to struct/union +types and performs an implicit ffi.gc() +call during creation of an instance. +

+ +

cdata = ffi.gc(cdata, finalizer)

+

+Associates a finalizer with a pointer or aggregate cdata object. The +cdata object is returned unchanged. +

+

+This function allows safe integration of unmanaged resources into the +automatic memory management of the LuaJIT garbage collector. Typical +usage: +

+
+local p = ffi.gc(ffi.C.malloc(n), ffi.C.free)
+...
+p = nil -- Last reference to p is gone.
+-- GC will eventually run finalizer: ffi.C.free(p)
+
+

+A cdata finalizer works like the __gc metamethod for userdata +objects: when the last reference to a cdata object is gone, the +associated finalizer is called with the cdata object as an argument. The +finalizer can be a Lua function or a cdata function or cdata function +pointer. An existing finalizer can be removed by setting a nil +finalizer, e.g. right before explicitly deleting a resource: +

+
+ffi.C.free(ffi.gc(p, nil)) -- Manually free the memory.
+
+ +

C Type Information

+

+The following API functions return information about C types. +They are most useful for inspecting cdata objects. +

+ +

size = ffi.sizeof(ct [,nelem])

+

+Returns the size of ct in bytes. Returns nil if +the size is not known (e.g. for "void" or function types). +Requires nelem for VLA/VLS types, except for cdata objects. +

+ +

align = ffi.alignof(ct)

+

+Returns the minimum required alignment for ct in bytes. +

+ +

ofs [,bpos,bsize] = ffi.offsetof(ct, field)

+

+Returns the offset (in bytes) of field relative to the start +of ct, which must be a struct. Additionally returns +the position and the field size (in bits) for bit fields. +

+ +

status = ffi.istype(ct, obj)

+

+Returns true if obj has the C type given by +ct. Returns false otherwise. +

+

+C type qualifiers (const etc.) are ignored. Pointers are +checked with the standard pointer compatibility rules, but without any +special treatment for void *. If ct specifies a +struct/union, then a pointer to this type is accepted, +too. Otherwise the types must match exactly. +

+

+Note: this function accepts all kinds of Lua objects for the +obj argument, but always returns false for non-cdata +objects. +

+ +

Utility Functions

+ +

err = ffi.errno([newerr])

+

+Returns the error number set by the last C function call which +indicated an error condition. If the optional newerr argument +is present, the error number is set to the new value and the previous +value is returned. +

+

+This function offers a portable and OS-independent way to get and set the +error number. Note that only some C functions set the error +number. And it's only significant if the function actually indicated an +error condition (e.g. with a return value of -1 or +NULL). Otherwise, it may or may not contain any previously set +value. +

+

+You're advised to call this function only when needed and as close as +possible after the return of the related C function. The +errno value is preserved across hooks, memory allocations, +invocations of the JIT compiler and other internal VM activity. The same +applies to the value returned by GetLastError() on Windows, but +you need to declare and call it yourself. +

+ +

str = ffi.string(ptr [,len])

+

+Creates an interned Lua string from the data pointed to by +ptr. +

+

+If the optional argument len is missing, ptr is +converted to a "char *" and the data is assumed to be +zero-terminated. The length of the string is computed with +strlen(). +

+

+Otherwise ptr is converted to a "void *" and +len gives the length of the data. The data may contain +embedded zeros and need not be byte-oriented (though this may cause +endianess issues). +

+

+This function is mainly useful to convert (temporary) +"const char *" pointers returned by +C functions to Lua strings and store them or pass them to other +functions expecting a Lua string. The Lua string is an (interned) copy +of the data and bears no relation to the original data area anymore. +Lua strings are 8 bit clean and may be used to hold arbitrary, +non-character data. +

+

+Performance notice: it's faster to pass the length of the string, if +it's known. E.g. when the length is returned by a C call like +sprintf(). +

+ +

ffi.copy(dst, src, len)
+ffi.copy(dst, str)

+

+Copies the data pointed to by src to dst. +dst is converted to a "void *" and src +is converted to a "const void *". +

+

+In the first syntax, len gives the number of bytes to copy. +Caveat: if src is a Lua string, then len must not +exceed #src+1. +

+

+In the second syntax, the source of the copy must be a Lua string. All +bytes of the string plus a zero-terminator are copied to +dst (i.e. #src+1 bytes). +

+

+Performance notice: ffi.copy() may be used as a faster +(inlinable) replacement for the C library functions +memcpy(), strcpy() and strncpy(). +

+ +

ffi.fill(dst, len [,c])

+

+Fills the data pointed to by dst with len constant +bytes, given by c. If c is omitted, the data is +zero-filled. +

+

+Performance notice: ffi.fill() may be used as a faster +(inlinable) replacement for the C library function +memset(dst, c, len). Please note the different +order of arguments! +

+ +

Target-specific Information

+ +

status = ffi.abi(param)

+

+Returns true if param (a Lua string) applies for the +target ABI (Application Binary Interface). Returns false +otherwise. The following parameters are currently defined: +

+ + + + + + + + + + + + + + + + + + + + + + + +
ParameterDescription
32bit32 bit architecture
64bit64 bit architecture
leLittle-endian architecture
beBig-endian architecture
fpuTarget has a hardware FPU
softfpsoftfp calling conventions
hardfphardfp calling conventions
eabiEABI variant of the standard ABI
winWindows variant of the standard ABI
+ +

ffi.os

+

+Contains the target OS name. Same contents as +jit.os. +

+ +

ffi.arch

+

+Contains the target architecture name. Same contents as +jit.arch. +

+ +

Methods for Callbacks

+

+The C types for callbacks +have some extra methods: +

+ +

cb:free()

+

+Free the resources associated with a callback. The associated Lua +function is unanchored and may be garbage collected. The callback +function pointer is no longer valid and must not be called anymore +(it may be reused by a subsequently created callback). +

+ +

cb:set(func)

+

+Associate a new Lua function with a callback. The C type of the +callback and the callback function pointer are unchanged. +

+

+This method is useful to dynamically switch the receiver of callbacks +without creating a new callback each time and registering it again (e.g. +with a GUI library). +

+ +

Extended Standard Library Functions

+

+The following standard library functions have been extended to work +with cdata objects: +

+ +

n = tonumber(cdata)

+

+Converts a number cdata object to a double and returns it as +a Lua number. This is particularly useful for boxed 64 bit +integer values. Caveat: this conversion may incur a precision loss. +

+ +

s = tostring(cdata)

+

+Returns a string representation of the value of 64 bit integers +("nnnLL" or "nnnULL") or +complex numbers ("re±imi"). Otherwise +returns a string representation of the C type of a ctype object +("ctype<type>") or a cdata object +("cdata<type>: address"), unless you +override it with a __tostring metamethod (see +ffi.metatype()). +

+ +

iter, obj, start = pairs(cdata)
+iter, obj, start = ipairs(cdata)

+

+Calls the __pairs or __ipairs metamethod of the +corresponding ctype. +

+ +

Extensions to the Lua Parser

+

+The parser for Lua source code treats numeric literals with the +suffixes LL or ULL as signed or unsigned 64 bit +integers. Case doesn't matter, but uppercase is recommended for +readability. It handles both decimal (42LL) and hexadecimal +(0x2aLL) literals. +

+

+The imaginary part of complex numbers can be specified by suffixing +number literals with i or I, e.g. 12.5i. +Caveat: you'll need to use 1i to get an imaginary part with +the value one, since i itself still refers to a variable +named i. +

+
+
+ + + diff --git a/external/lua/luajit/src/doc/ext_ffi_semantics.html b/external/lua/luajit/src/doc/ext_ffi_semantics.html new file mode 100644 index 0000000000..03229012ed --- /dev/null +++ b/external/lua/luajit/src/doc/ext_ffi_semantics.html @@ -0,0 +1,1243 @@ + + + +FFI Semantics + + + + + + + + + +
+Lua +
+ + +
+

+This page describes the detailed semantics underlying the FFI library +and its interaction with both Lua and C code. +

+

+Given that the FFI library is designed to interface with C code +and that declarations can be written in plain C syntax, it +closely follows the C language semantics, wherever possible. +Some minor concessions are needed for smoother interoperation with Lua +language semantics. +

+

+Please don't be overwhelmed by the contents of this page — this +is a reference and you may need to consult it, if in doubt. It doesn't +hurt to skim this page, but most of the semantics "just work" as you'd +expect them to work. It should be straightforward to write +applications using the LuaJIT FFI for developers with a C or C++ +background. +

+ +

C Language Support

+

+The FFI library has a built-in C parser with a minimal memory +footprint. It's used by the ffi.* library +functions to declare C types or external symbols. +

+

+It's only purpose is to parse C declarations, as found e.g. in +C header files. Although it does evaluate constant expressions, +it's not a C compiler. The body of inline +C function definitions is simply ignored. +

+

+Also, this is not a validating C parser. It expects and +accepts correctly formed C declarations, but it may choose to +ignore bad declarations or show rather generic error messages. If in +doubt, please check the input against your favorite C compiler. +

+

+The C parser complies to the C99 language standard plus +the following extensions: +

+
    + +
  • The '\e' escape in character and string literals.
  • + +
  • The C99/C++ boolean type, declared with the keywords bool +or _Bool.
  • + +
  • Complex numbers, declared with the keywords complex or +_Complex.
  • + +
  • Two complex number types: complex (aka +complex double) and complex float.
  • + +
  • Vector types, declared with the GCC mode or +vector_size attribute.
  • + +
  • Unnamed ('transparent') struct/union fields +inside a struct/union.
  • + +
  • Incomplete enum declarations, handled like incomplete +struct declarations.
  • + +
  • Unnamed enum fields inside a +struct/union. This is similar to a scoped C++ +enum, except that declared constants are visible in the +global namespace, too.
  • + +
  • Scoped static const declarations inside a +struct/union (from C++).
  • + +
  • Zero-length arrays ([0]), empty +struct/union, variable-length arrays (VLA, +[?]) and variable-length structs (VLS, with a trailing +VLA).
  • + +
  • C++ reference types (int &x).
  • + +
  • Alternate GCC keywords with '__', e.g. +__const__.
  • + +
  • GCC __attribute__ with the following attributes: +aligned, packed, mode, +vector_size, cdecl, fastcall, +stdcall, thiscall.
  • + +
  • The GCC __extension__ keyword and the GCC +__alignof__ operator.
  • + +
  • GCC __asm__("symname") symbol name redirection for +function declarations.
  • + +
  • MSVC keywords for fixed-length types: __int8, +__int16, __int32 and __int64.
  • + +
  • MSVC __cdecl, __fastcall, __stdcall, +__thiscall, __ptr32, __ptr64, +__declspec(align(n)) and #pragma pack.
  • + +
  • All other GCC/MSVC-specific attributes are ignored.
  • + +
+

+The following C types are pre-defined by the C parser (like +a typedef, except re-declarations will be ignored): +

+
    + +
  • Vararg handling: va_list, __builtin_va_list, +__gnuc_va_list.
  • + +
  • From <stddef.h>: ptrdiff_t, +size_t, wchar_t.
  • + +
  • From <stdint.h>: int8_t, int16_t, +int32_t, int64_t, uint8_t, +uint16_t, uint32_t, uint64_t, +intptr_t, uintptr_t.
  • + +
+

+You're encouraged to use these types in preference to +compiler-specific extensions or target-dependent standard types. +E.g. char differs in signedness and long differs in +size, depending on the target architecture and platform ABI. +

+

+The following C features are not supported: +

+
    + +
  • A declaration must always have a type specifier; it doesn't +default to an int type.
  • + +
  • Old-style empty function declarations (K&R) are not allowed. +All C functions must have a proper prototype declaration. A +function declared without parameters (int foo();) is +treated as a function taking zero arguments, like in C++.
  • + +
  • The long double C type is parsed correctly, but +there's no support for the related conversions, accesses or arithmetic +operations.
  • + +
  • Wide character strings and character literals are not +supported.
  • + +
  • See below for features that are currently +not implemented.
  • + +
+ +

C Type Conversion Rules

+ +

Conversions from C types to Lua objects

+

+These conversion rules apply for read accesses to +C types: indexing pointers, arrays or +struct/union types; reading external variables or +constant values; retrieving return values from C calls: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
InputConversionOutput
int8_t, int16_tsign-ext int32_tdoublenumber
uint8_t, uint16_tzero-ext int32_tdoublenumber
int32_t, uint32_tdoublenumber
int64_t, uint64_tboxed value64 bit int cdata
double, floatdoublenumber
bool0 → false, otherwise trueboolean
enumboxed valueenum cdata
Complex numberboxed valuecomplex cdata
Vectorboxed valuevector cdata
Pointerboxed valuepointer cdata
Arrayboxed referencereference cdata
struct/unionboxed referencereference cdata
+

+Bitfields are treated like their underlying type. +

+

+Reference types are dereferenced before a conversion can take +place — the conversion is applied to the C type pointed to +by the reference. +

+ +

Conversions from Lua objects to C types

+

+These conversion rules apply for write accesses to +C types: indexing pointers, arrays or +struct/union types; initializing cdata objects; +casts to C types; writing to external variables; passing +arguments to C calls: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
InputConversionOutput
numberdouble
booleanfalse → 0, true → 1bool
nilNULL(void *)
lightuserdatalightuserdata address →(void *)
userdatauserdata payload →(void *)
io.* fileget FILE * handle →(void *)
stringmatch against enum constantenum
stringcopy string data + zero-byteint8_t[], uint8_t[]
stringstring data →const char[]
functioncreate callbackC function type
tabletable initializerArray
tabletable initializerstruct/union
cdatacdata payload →C type
+

+If the result type of this conversion doesn't match the +C type of the destination, the +conversion rules between C types +are applied. +

+

+Reference types are immutable after initialization ("no re-seating of +references"). For initialization purposes or when passing values to +reference parameters, they are treated like pointers. Note that unlike +in C++, there's no way to implement automatic reference generation of +variables under the Lua language semantics. If you want to call a +function with a reference parameter, you need to explicitly pass a +one-element array. +

+ +

Conversions between C types

+

+These conversion rules are more or less the same as the standard +C conversion rules. Some rules only apply to casts, or require +pointer or type compatibility: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
InputConversionOutput
Signed integernarrow or sign-extendInteger
Unsigned integernarrow or zero-extendInteger
Integerrounddouble, float
double, floattrunc int32_tnarrow(u)int8_t, (u)int16_t
double, floattrunc(u)int32_t, (u)int64_t
double, floatroundfloat, double
Numbern == 0 → 0, otherwise 1bool
boolfalse → 0, true → 1Number
Complex numberconvert real partNumber
Numberconvert real part, imag = 0Complex number
Complex numberconvert real and imag partComplex number
Numberconvert scalar and replicateVector
Vectorcopy (same size)Vector
struct/uniontake base address (compat)Pointer
Arraytake base address (compat)Pointer
Functiontake function addressFunction pointer
Numberconvert via uintptr_t (cast)Pointer
Pointerconvert address (compat/cast)Pointer
Pointerconvert address (cast)Integer
Arrayconvert base address (cast)Integer
Arraycopy (compat)Array
struct/unioncopy (identical type)struct/union
+

+Bitfields or enum types are treated like their underlying +type. +

+

+Conversions not listed above will raise an error. E.g. it's not +possible to convert a pointer to a complex number or vice versa. +

+ +

Conversions for vararg C function arguments

+

+The following default conversion rules apply when passing Lua objects +to the variable argument part of vararg C functions: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
InputConversionOutput
numberdouble
booleanfalse → 0, true → 1bool
nilNULL(void *)
userdatauserdata payload →(void *)
lightuserdatalightuserdata address →(void *)
stringstring data →const char *
float cdatadouble
Array cdatatake base addressElement pointer
struct/union cdatatake base addressstruct/union pointer
Function cdatatake function addressFunction pointer
Any other cdatano conversionC type
+

+To pass a Lua object, other than a cdata object, as a specific type, +you need to override the conversion rules: create a temporary cdata +object with a constructor or a cast and initialize it with the value +to pass: +

+

+Assuming x is a Lua number, here's how to pass it as an +integer to a vararg function: +

+
+ffi.cdef[[
+int printf(const char *fmt, ...);
+]]
+ffi.C.printf("integer value: %d\n", ffi.new("int", x))
+
+

+If you don't do this, the default Lua number → double +conversion rule applies. A vararg C function expecting an integer +will see a garbled or uninitialized value. +

+ +

Initializers

+

+Creating a cdata object with +ffi.new() or the +equivalent constructor syntax always initializes its contents, too. +Different rules apply, depending on the number of optional +initializers and the C types involved: +

+
    +
  • If no initializers are given, the object is filled with zero bytes.
  • + +
  • Scalar types (numbers and pointers) accept a single initializer. +The Lua object is converted to the scalar +C type.
  • + +
  • Valarrays (complex numbers and vectors) are treated like scalars +when a single initializer is given. Otherwise they are treated like +regular arrays.
  • + +
  • Aggregate types (arrays and structs) accept either a single cdata +initializer of the same type (copy constructor), a single +table initializer, or a flat list of +initializers.
  • + +
  • The elements of an array are initialized, starting at index zero. +If a single initializer is given for an array, it's repeated for all +remaining elements. This doesn't happen if two or more initializers +are given: all remaining uninitialized elements are filled with zero +bytes.
  • + +
  • Byte arrays may also be initialized with a Lua string. This copies +the whole string plus a terminating zero-byte. The copy stops early only +if the array has a known, fixed size.
  • + +
  • The fields of a struct are initialized in the order of +their declaration. Uninitialized fields are filled with zero +bytes.
  • + +
  • Only the first field of a union can be initialized with a +flat initializer.
  • + +
  • Elements or fields which are aggregates themselves are initialized +with a single initializer, but this may be a table +initializer or a compatible aggregate.
  • + +
  • Excess initializers cause an error.
  • + +
+ +

Table Initializers

+

+The following rules apply if a Lua table is used to initialize an +Array or a struct/union: +

+
    + +
  • If the table index [0] is non-nil, then the +table is assumed to be zero-based. Otherwise it's assumed to be +one-based.
  • + +
  • Array elements, starting at index zero, are initialized one-by-one +with the consecutive table elements, starting at either index +[0] or [1]. This process stops at the first +nil table element.
  • + +
  • If exactly one array element was initialized, it's repeated for +all the remaining elements. Otherwise all remaining uninitialized +elements are filled with zero bytes.
  • + +
  • The above logic only applies to arrays with a known fixed size. +A VLA is only initialized with the element(s) given in the table. +Depending on the use case, you may need to explicitly add a +NULL or 0 terminator to a VLA.
  • + +
  • A struct/union can be initialized in the +order of the declaration of its fields. Each field is initialized with +consecutive table elements, starting at either index [0] +or [1]. This process stops at the first nil table +element.
  • + +
  • Otherwise, if neither index [0] nor [1] is present, +a struct/union is initialized by looking up each field +name (as a string key) in the table. Each non-nil value is +used to initialize the corresponding field.
  • + +
  • Uninitialized fields of a struct are filled with zero +bytes, except for the trailing VLA of a VLS.
  • + +
  • Initialization of a union stops after one field has been +initialized. If no field has been initialized, the union is +filled with zero bytes.
  • + +
  • Elements or fields which are aggregates themselves are initialized +with a single initializer, but this may be a nested table +initializer (or a compatible aggregate).
  • + +
  • Excess initializers for an array cause an error. Excess +initializers for a struct/union are ignored. +Unrelated table entries are ignored, too.
  • + +
+

+Example: +

+
+local ffi = require("ffi")
+
+ffi.cdef[[
+struct foo { int a, b; };
+union bar { int i; double d; };
+struct nested { int x; struct foo y; };
+]]
+
+ffi.new("int[3]", {})            --> 0, 0, 0
+ffi.new("int[3]", {1})           --> 1, 1, 1
+ffi.new("int[3]", {1,2})         --> 1, 2, 0
+ffi.new("int[3]", {1,2,3})       --> 1, 2, 3
+ffi.new("int[3]", {[0]=1})       --> 1, 1, 1
+ffi.new("int[3]", {[0]=1,2})     --> 1, 2, 0
+ffi.new("int[3]", {[0]=1,2,3})   --> 1, 2, 3
+ffi.new("int[3]", {[0]=1,2,3,4}) --> error: too many initializers
+
+ffi.new("struct foo", {})            --> a = 0, b = 0
+ffi.new("struct foo", {1})           --> a = 1, b = 0
+ffi.new("struct foo", {1,2})         --> a = 1, b = 2
+ffi.new("struct foo", {[0]=1,2})     --> a = 1, b = 2
+ffi.new("struct foo", {b=2})         --> a = 0, b = 2
+ffi.new("struct foo", {a=1,b=2,c=3}) --> a = 1, b = 2  'c' is ignored
+
+ffi.new("union bar", {})        --> i = 0, d = 0.0
+ffi.new("union bar", {1})       --> i = 1, d = ?
+ffi.new("union bar", {[0]=1,2}) --> i = 1, d = ?    '2' is ignored
+ffi.new("union bar", {d=2})     --> i = ?, d = 2.0
+
+ffi.new("struct nested", {1,{2,3}})     --> x = 1, y.a = 2, y.b = 3
+ffi.new("struct nested", {x=1,y={2,3}}) --> x = 1, y.a = 2, y.b = 3
+
+ +

Operations on cdata Objects

+

+All of the standard Lua operators can be applied to cdata objects or a +mix of a cdata object and another Lua object. The following list shows +the pre-defined operations. +

+

+Reference types are dereferenced before performing each of +the operations below — the operation is applied to the +C type pointed to by the reference. +

+

+The pre-defined operations are always tried first before deferring to a +metamethod or index table (if any) for the corresponding ctype (except +for __new). An error is raised if the metamethod lookup or +index table lookup fails. +

+ +

Indexing a cdata object

+
    + +
  • Indexing a pointer/array: a cdata pointer/array can be +indexed by a cdata number or a Lua number. The element address is +computed as the base address plus the number value multiplied by the +element size in bytes. A read access loads the element value and +converts it to a Lua object. A write +access converts a Lua object to the element +type and stores the converted value to the element. An error is +raised if the element size is undefined or a write access to a +constant element is attempted.
  • + +
  • Dereferencing a struct/union field: a +cdata struct/union or a pointer to a +struct/union can be dereferenced by a string key, +giving the field name. The field address is computed as the base +address plus the relative offset of the field. A read access loads the +field value and converts it to a Lua +object. A write access converts a Lua +object to the field type and stores the converted value to the +field. An error is raised if a write access to a constant +struct/union or a constant field is attempted. +Scoped enum constants or static constants are treated like a constant +field.
  • + +
  • Indexing a complex number: a complex number can be indexed +either by a cdata number or a Lua number with the values 0 or 1, or by +the strings "re" or "im". A read access loads the +real part ([0], .re) or the imaginary part +([1], .im) part of a complex number and +converts it to a Lua number. The +sub-parts of a complex number are immutable — assigning to an +index of a complex number raises an error. Accessing out-of-bound +indexes returns unspecified results, but is guaranteed not to trigger +memory access violations.
  • + +
  • Indexing a vector: a vector is treated like an array for +indexing purposes, except the vector elements are immutable — +assigning to an index of a vector raises an error.
  • + +
+

+A ctype object can be indexed with a string key, too. The only +pre-defined operation is reading scoped constants of +struct/union types. All other accesses defer +to the corresponding metamethods or index tables (if any). +

+

+Note: since there's (deliberately) no address-of operator, a cdata +object holding a value type is effectively immutable after +initialization. The JIT compiler benefits from this fact when applying +certain optimizations. +

+

+As a consequence, the elements of complex numbers and +vectors are immutable. But the elements of an aggregate holding these +types may be modified of course. I.e. you cannot assign to +foo.c.im, but you can assign a (newly created) complex number +to foo.c. +

+

+The JIT compiler implements strict aliasing rules: accesses to different +types do not alias, except for differences in signedness (this +applies even to char pointers, unlike C99). Type punning +through unions is explicitly detected and allowed. +

+ +

Calling a cdata object

+
    + +
  • Constructor: a ctype object can be called and used as a +constructor. This is equivalent +to ffi.new(ct, ...), unless a __new metamethod is +defined. The __new metamethod is called with the ctype object +plus any other arguments passed to the contructor. Note that you have to +use ffi.new inside of it, since calling ct(...) would +cause infinite recursion.
  • + +
  • C function call: a cdata function or cdata function +pointer can be called. The passed arguments are +converted to the C types of the +parameters given by the function declaration. Arguments passed to the +variable argument part of vararg C function use +special conversion rules. This +C function is called and the return value (if any) is +converted to a Lua object.
    +On Windows/x86 systems, __stdcall functions are automatically +detected and a function declared as __cdecl (the default) is +silently fixed up after the first call.
  • + +
+ +

Arithmetic on cdata objects

+
    + +
  • Pointer arithmetic: a cdata pointer/array and a cdata +number or a Lua number can be added or subtracted. The number must be +on the right hand side for a subtraction. The result is a pointer of +the same type with an address plus or minus the number value +multiplied by the element size in bytes. An error is raised if the +element size is undefined.
  • + +
  • Pointer difference: two compatible cdata pointers/arrays +can be subtracted. The result is the difference between their +addresses, divided by the element size in bytes. An error is raised if +the element size is undefined or zero.
  • + +
  • 64 bit integer arithmetic: the standard arithmetic +operators (+ - * / % ^ and unary +minus) can be applied to two cdata numbers, or a cdata number and a +Lua number. If one of them is an uint64_t, the other side is +converted to an uint64_t and an unsigned arithmetic operation +is performed. Otherwise both sides are converted to an +int64_t and a signed arithmetic operation is performed. The +result is a boxed 64 bit cdata object.
    + +If one of the operands is an enum and the other operand is a +string, the string is converted to the value of a matching enum +constant before the above conversion.
    + +These rules ensure that 64 bit integers are "sticky". Any +expression involving at least one 64 bit integer operand results +in another one. The undefined cases for the division, modulo and power +operators return 2LL ^ 63 or +2ULL ^ 63.
    + +You'll have to explicitly convert a 64 bit integer to a Lua +number (e.g. for regular floating-point calculations) with +tonumber(). But note this may incur a precision loss.
  • + +
+ +

Comparisons of cdata objects

+
    + +
  • Pointer comparison: two compatible cdata pointers/arrays +can be compared. The result is the same as an unsigned comparison of +their addresses. nil is treated like a NULL pointer, +which is compatible with any other pointer type.
  • + +
  • 64 bit integer comparison: two cdata numbers, or a +cdata number and a Lua number can be compared with each other. If one +of them is an uint64_t, the other side is converted to an +uint64_t and an unsigned comparison is performed. Otherwise +both sides are converted to an int64_t and a signed +comparison is performed.
    + +If one of the operands is an enum and the other operand is a +string, the string is converted to the value of a matching enum +constant before the above conversion.
    + +
  • Comparisons for equality/inequality never raise an error. +Even incompatible pointers can be compared for equality by address. Any +other incompatible comparison (also with non-cdata objects) treats the +two sides as unequal.
  • + +
+ +

cdata objects as table keys

+

+Lua tables may be indexed by cdata objects, but this doesn't provide +any useful semantics — cdata objects are unsuitable as table +keys! +

+

+A cdata object is treated like any other garbage-collected object and +is hashed and compared by its address for table indexing. Since +there's no interning for cdata value types, the same value may be +boxed in different cdata objects with different addresses. Thus +t[1LL+1LL] and t[2LL] usually do not point to +the same hash slot and they certainly do not point to the same +hash slot as t[2]. +

+

+It would seriously drive up implementation complexity and slow down +the common case, if one were to add extra handling for by-value +hashing and comparisons to Lua tables. Given the ubiquity of their use +inside the VM, this is not acceptable. +

+

+There are three viable alternatives, if you really need to use cdata +objects as keys: +

+
    + +
  • If you can get by with the precision of Lua numbers +(52 bits), then use tonumber() on a cdata number or +combine multiple fields of a cdata aggregate to a Lua number. Then use +the resulting Lua number as a key when indexing tables.
    +One obvious benefit: t[tonumber(2LL)] does point to +the same slot as t[2].
  • + +
  • Otherwise use either tostring() on 64 bit integers +or complex numbers or combine multiple fields of a cdata aggregate to +a Lua string (e.g. with +ffi.string()). Then +use the resulting Lua string as a key when indexing tables.
  • + +
  • Create your own specialized hash table implementation using the +C types provided by the FFI library, just like you would in +C code. Ultimately this may give much better performance than the +other alternatives or what a generic by-value hash table could +possibly provide.
  • + +
+ +

Parameterized Types

+

+To facilitate some abstractions, the two functions +ffi.typeof and +ffi.cdef support +parameterized types in C declarations. Note: none of the other API +functions taking a cdecl allow this. +

+

+Any place you can write a typedef name, an +identifier or a number in a declaration, you can write +$ (the dollar sign) instead. These placeholders are replaced in +order of appearance with the arguments following the cdecl string: +

+
+-- Declare a struct with a parameterized field type and name:
+ffi.cdef([[
+typedef struct { $ $; } foo_t;
+]], type1, name1)
+
+-- Anonymous struct with dynamic names:
+local bar_t = ffi.typeof("struct { int $, $; }", name1, name2)
+-- Derived pointer type:
+local bar_ptr_t = ffi.typeof("$ *", bar_t)
+
+-- Parameterized dimensions work even where a VLA won't work:
+local matrix_t = ffi.typeof("uint8_t[$][$]", width, height)
+
+

+Caveat: this is not simple text substitution! A passed ctype or +cdata object is treated like the underlying type, a passed string is +considered an identifier and a number is considered a number. You must +not mix this up: e.g. passing "int" as a string doesn't work in +place of a type, you'd need to use ffi.typeof("int") instead. +

+

+The main use for parameterized types are libraries implementing abstract +data types +(» example), +similar to what can be achieved with C++ template metaprogramming. +Another use case are derived types of anonymous structs, which avoids +pollution of the global struct namespace. +

+

+Please note that parameterized types are a nice tool and indispensable +for certain use cases. But you'll want to use them sparingly in regular +code, e.g. when all types are actually fixed. +

+ +

Garbage Collection of cdata Objects

+

+All explicitly (ffi.new(), ffi.cast() etc.) or +implicitly (accessors) created cdata objects are garbage collected. +You need to ensure to retain valid references to cdata objects +somewhere on a Lua stack, an upvalue or in a Lua table while they are +still in use. Once the last reference to a cdata object is gone, the +garbage collector will automatically free the memory used by it (at +the end of the next GC cycle). +

+

+Please note that pointers themselves are cdata objects, however they +are not followed by the garbage collector. So e.g. if you +assign a cdata array to a pointer, you must keep the cdata object +holding the array alive as long as the pointer is still in use: +

+
+ffi.cdef[[
+typedef struct { int *a; } foo_t;
+]]
+
+local s = ffi.new("foo_t", ffi.new("int[10]")) -- WRONG!
+
+local a = ffi.new("int[10]") -- OK
+local s = ffi.new("foo_t", a)
+-- Now do something with 's', but keep 'a' alive until you're done.
+
+

+Similar rules apply for Lua strings which are implicitly converted to +"const char *": the string object itself must be +referenced somewhere or it'll be garbage collected eventually. The +pointer will then point to stale data, which may have already been +overwritten. Note that string literals are automatically kept +alive as long as the function containing it (actually its prototype) +is not garbage collected. +

+

+Objects which are passed as an argument to an external C function +are kept alive until the call returns. So it's generally safe to +create temporary cdata objects in argument lists. This is a common +idiom for passing specific C types to +vararg functions. +

+

+Memory areas returned by C functions (e.g. from malloc()) +must be manually managed, of course (or use +ffi.gc()). Pointers to +cdata objects are indistinguishable from pointers returned by C +functions (which is one of the reasons why the GC cannot follow them). +

+ +

Callbacks

+

+The LuaJIT FFI automatically generates special callback functions +whenever a Lua function is converted to a C function pointer. This +associates the generated callback function pointer with the C type +of the function pointer and the Lua function object (closure). +

+

+This can happen implicitly due to the usual conversions, e.g. when +passing a Lua function to a function pointer argument. Or you can use +ffi.cast() to explicitly cast a Lua function to a +C function pointer. +

+

+Currently only certain C function types can be used as callback +functions. Neither C vararg functions nor functions with +pass-by-value aggregate argument or result types are supported. There +are no restrictions for the kind of Lua functions that can be called +from the callback — no checks for the proper number of arguments +are made. The return value of the Lua function will be converted to the +result type and an error will be thrown for invalid conversions. +

+

+It's allowed to throw errors across a callback invocation, but it's not +advisable in general. Do this only if you know the C function, that +called the callback, copes with the forced stack unwinding and doesn't +leak resources. +

+

+One thing that's not allowed, is to let an FFI call into a C function +get JIT-compiled, which in turn calls a callback, calling into Lua again. +Usually this attempt is caught by the interpreter first and the +C function is blacklisted for compilation. +

+

+However, this heuristic may fail under specific circumstances: e.g. a +message polling function might not run Lua callbacks right away and the call +gets JIT-compiled. If it later happens to call back into Lua (e.g. a rarely +invoked error callback), you'll get a VM PANIC with the message +"bad callback". Then you'll need to manually turn off +JIT-compilation with +jit.off() for the +surrounding Lua function that invokes such a message polling function (or +similar). +

+ +

Callback resource handling

+

+Callbacks take up resources — you can only have a limited number +of them at the same time (500 - 1000, depending on the +architecture). The associated Lua functions are anchored to prevent +garbage collection, too. +

+

+Callbacks due to implicit conversions are permanent! There is no +way to guess their lifetime, since the C side might store the +function pointer for later use (typical for GUI toolkits). The associated +resources cannot be reclaimed until termination: +

+
+ffi.cdef[[
+typedef int (__stdcall *WNDENUMPROC)(void *hwnd, intptr_t l);
+int EnumWindows(WNDENUMPROC func, intptr_t l);
+]]
+
+-- Implicit conversion to a callback via function pointer argument.
+local count = 0
+ffi.C.EnumWindows(function(hwnd, l)
+  count = count + 1
+  return true
+end, 0)
+-- The callback is permanent and its resources cannot be reclaimed!
+-- Ok, so this may not be a problem, if you do this only once.
+
+

+Note: this example shows that you must properly declare +__stdcall callbacks on Windows/x86 systems. The calling +convention cannot be automatically detected, unlike for +__stdcall calls to Windows functions. +

+

+For some use cases it's necessary to free up the resources or to +dynamically redirect callbacks. Use an explicit cast to a +C function pointer and keep the resulting cdata object. Then use +the cb:free() +or cb:set() methods +on the cdata object: +

+
+-- Explicitly convert to a callback via cast.
+local count = 0
+local cb = ffi.cast("WNDENUMPROC", function(hwnd, l)
+  count = count + 1
+  return true
+end)
+
+-- Pass it to a C function.
+ffi.C.EnumWindows(cb, 0)
+-- EnumWindows doesn't need the callback after it returns, so free it.
+
+cb:free()
+-- The callback function pointer is no longer valid and its resources
+-- will be reclaimed. The created Lua closure will be garbage collected.
+
+ +

Callback performance

+

+Callbacks are slow! First, the C to Lua transition itself +has an unavoidable cost, similar to a lua_call() or +lua_pcall(). Argument and result marshalling add to that cost. +And finally, neither the C compiler nor LuaJIT can inline or +optimize across the language barrier and hoist repeated computations out +of a callback function. +

+

+Do not use callbacks for performance-sensitive work: e.g. consider a +numerical integration routine which takes a user-defined function to +integrate over. It's a bad idea to call a user-defined Lua function from +C code millions of times. The callback overhead will be absolutely +detrimental for performance. +

+

+It's considerably faster to write the numerical integration routine +itself in Lua — the JIT compiler will be able to inline the +user-defined function and optimize it together with its calling context, +with very competitive performance. +

+

+As a general guideline: use callbacks only when you must, because +of existing C APIs. E.g. callback performance is irrelevant for a +GUI application, which waits for user input most of the time, anyway. +

+

+For new designs avoid push-style APIs: a C function repeatedly +calling a callback for each result. Instead use pull-style APIs: +call a C function repeatedly to get a new result. Calls from Lua +to C via the FFI are much faster than the other way round. Most well-designed +libraries already use pull-style APIs (read/write, get/put). +

+ +

C Library Namespaces

+

+A C library namespace is a special kind of object which allows +access to the symbols contained in shared libraries or the default +symbol namespace. The default +ffi.C namespace is +automatically created when the FFI library is loaded. C library +namespaces for specific shared libraries may be created with the +ffi.load() API +function. +

+

+Indexing a C library namespace object with a symbol name (a Lua +string) automatically binds it to the library. First the symbol type +is resolved — it must have been declared with +ffi.cdef. Then the +symbol address is resolved by searching for the symbol name in the +associated shared libraries or the default symbol namespace. Finally, +the resulting binding between the symbol name, the symbol type and its +address is cached. Missing symbol declarations or nonexistent symbol +names cause an error. +

+

+This is what happens on a read access for the different kinds of +symbols: +

+
    + +
  • External functions: a cdata object with the type of the function +and its address is returned.
  • + +
  • External variables: the symbol address is dereferenced and the +loaded value is converted to a Lua object +and returned.
  • + +
  • Constant values (static const or enum +constants): the constant is converted to a +Lua object and returned.
  • + +
+

+This is what happens on a write access: +

+
    + +
  • External variables: the value to be written is +converted to the C type of the +variable and then stored at the symbol address.
  • + +
  • Writing to constant variables or to any other symbol type causes +an error, like any other attempted write to a constant location.
  • + +
+

+C library namespaces themselves are garbage collected objects. If +the last reference to the namespace object is gone, the garbage +collector will eventually release the shared library reference and +remove all memory associated with the namespace. Since this may +trigger the removal of the shared library from the memory of the +running process, it's generally not safe to use function +cdata objects obtained from a library if the namespace object may be +unreferenced. +

+

+Performance notice: the JIT compiler specializes to the identity of +namespace objects and to the strings used to index it. This +effectively turns function cdata objects into constants. It's not +useful and actually counter-productive to explicitly cache these +function objects, e.g. local strlen = ffi.C.strlen. OTOH it +is useful to cache the namespace itself, e.g. local C = +ffi.C. +

+ +

No Hand-holding!

+

+The FFI library has been designed as a low-level library. The +goal is to interface with C code and C data types with a +minimum of overhead. This means you can do anything you can do +from C: access all memory, overwrite anything in memory, call +machine code at any memory address and so on. +

+

+The FFI library provides no memory safety, unlike regular Lua +code. It will happily allow you to dereference a NULL +pointer, to access arrays out of bounds or to misdeclare +C functions. If you make a mistake, your application might crash, +just like equivalent C code would. +

+

+This behavior is inevitable, since the goal is to provide full +interoperability with C code. Adding extra safety measures, like +bounds checks, would be futile. There's no way to detect +misdeclarations of C functions, since shared libraries only +provide symbol names, but no type information. Likewise there's no way +to infer the valid range of indexes for a returned pointer. +

+

+Again: the FFI library is a low-level library. This implies it needs +to be used with care, but it's flexibility and performance often +outweigh this concern. If you're a C or C++ developer, it'll be easy +to apply your existing knowledge. OTOH writing code for the FFI +library is not for the faint of heart and probably shouldn't be the +first exercise for someone with little experience in Lua, C or C++. +

+

+As a corollary of the above, the FFI library is not safe for use by +untrusted Lua code. If you're sandboxing untrusted Lua code, you +definitely don't want to give this code access to the FFI library or +to any cdata object (except 64 bit integers or complex +numbers). Any properly engineered Lua sandbox needs to provide safety +wrappers for many of the standard Lua library functions — +similar wrappers need to be written for high-level operations on FFI +data types, too. +

+ +

Current Status

+

+The initial release of the FFI library has some limitations and is +missing some features. Most of these will be fixed in future releases. +

+

+C language support is +currently incomplete: +

+
    +
  • C declarations are not passed through a C pre-processor, +yet.
  • +
  • The C parser is able to evaluate most constant expressions +commonly found in C header files. However it doesn't handle the +full range of C expression semantics and may fail for some +obscure constructs.
  • +
  • static const declarations only work for integer types +up to 32 bits. Neither declaring string constants nor +floating-point constants is supported.
  • +
  • Packed struct bitfields that cross container boundaries +are not implemented.
  • +
  • Native vector types may be defined with the GCC mode or +vector_size attribute. But no operations other than loading, +storing and initializing them are supported, yet.
  • +
  • The volatile type qualifier is currently ignored by +compiled code.
  • +
  • ffi.cdef silently +ignores all re-declarations.
  • +
+

+The JIT compiler already handles a large subset of all FFI operations. +It automatically falls back to the interpreter for unimplemented +operations (you can check for this with the +-jv command line option). +The following operations are currently not compiled and may exhibit +suboptimal performance, especially when used in inner loops: +

+
    +
  • Bitfield accesses and initializations.
  • +
  • Vector operations.
  • +
  • Table initializers.
  • +
  • Initialization of nested struct/union types.
  • +
  • Allocations of variable-length arrays or structs.
  • +
  • Allocations of C types with a size > 128 bytes or an +alignment > 8 bytes.
  • +
  • Conversions from lightuserdata to void *.
  • +
  • Pointer differences for element sizes that are not a power of +two.
  • +
  • Calls to C functions with aggregates passed or returned by +value.
  • +
  • Calls to ctype metamethods which are not plain functions.
  • +
  • ctype __newindex tables and non-string lookups in ctype +__index tables.
  • +
  • tostring() for cdata types.
  • +
  • Calls to ffi.cdef(), ffi.load() and +ffi.metatype().
  • +
+

+Other missing features: +

+
    +
  • Bit operations for 64 bit types.
  • +
  • Arithmetic for complex numbers.
  • +
  • Passing structs by value to vararg C functions.
  • +
  • C++ exception interoperability +does not extend to C functions called via the FFI, if the call is +compiled.
  • +
+
+
+ + + diff --git a/external/lua/luajit/src/doc/ext_ffi_tutorial.html b/external/lua/luajit/src/doc/ext_ffi_tutorial.html new file mode 100644 index 0000000000..30213b31b0 --- /dev/null +++ b/external/lua/luajit/src/doc/ext_ffi_tutorial.html @@ -0,0 +1,601 @@ + + + +FFI Tutorial + + + + + + + + + +
+Lua +
+ + +
+

+This page is intended to give you an overview of the features of the FFI +library by presenting a few use cases and guidelines. +

+

+This page makes no attempt to explain all of the FFI library, though. +You'll want to have a look at the ffi.* API +function reference and the FFI +semantics to learn more. +

+ +

Loading the FFI Library

+

+The FFI library is built into LuaJIT by default, but it's not loaded +and initialized by default. The suggested way to use the FFI library +is to add the following to the start of every Lua file that needs one +of its functions: +

+
+local ffi = require("ffi")
+
+

+Please note this doesn't define an ffi variable in the table +of globals — you really need to use the local variable. The +require function ensures the library is only loaded once. +

+

+Note: If you want to experiment with the FFI from the interactive prompt +of the command line executable, omit the local, as it doesn't +preserve local variables across lines. +

+ +

Accessing Standard System Functions

+

+The following code explains how to access standard system functions. +We slowly print two lines of dots by sleeping for 10 milliseconds +after each dot: +

+
+ 
+①
+
+
+
+
+
+②
+③
+④
+
+
+
+⑤
+
+
+
+
+
+⑥local ffi = require("ffi")
+ffi.cdef[[
+void Sleep(int ms);
+int poll(struct pollfd *fds, unsigned long nfds, int timeout);
+]]
+
+local sleep
+if ffi.os == "Windows" then
+  function sleep(s)
+    ffi.C.Sleep(s*1000)
+  end
+else
+  function sleep(s)
+    ffi.C.poll(nil, 0, s*1000)
+  end
+end
+
+for i=1,160 do
+  io.write("."); io.flush()
+  sleep(0.01)
+end
+io.write("\n")
+
+

+Here's the step-by-step explanation: +

+

+ This defines the +C library functions we're going to use. The part inside the +double-brackets (in green) is just standard C syntax. You can +usually get this info from the C header files or the +documentation provided by each C library or C compiler. +

+

+ The difficulty we're +facing here, is that there are different standards to choose from. +Windows has a simple Sleep() function. On other systems there +are a variety of functions available to achieve sub-second sleeps, but +with no clear consensus. Thankfully poll() can be used for +this task, too, and it's present on most non-Windows systems. The +check for ffi.os makes sure we use the Windows-specific +function only on Windows systems. +

+

+ Here we're wrapping the +call to the C function in a Lua function. This isn't strictly +necessary, but it's helpful to deal with system-specific issues only +in one part of the code. The way we're wrapping it ensures the check +for the OS is only done during initialization and not for every call. +

+

+ A more subtle point is +that we defined our sleep() function (for the sake of this +example) as taking the number of seconds, but accepting fractional +seconds. Multiplying this by 1000 gets us milliseconds, but that still +leaves it a Lua number, which is a floating-point value. Alas, the +Sleep() function only accepts an integer value. Luckily for +us, the FFI library automatically performs the conversion when calling +the function (truncating the FP value towards zero, like in C). +

+

+Some readers will notice that Sleep() is part of +KERNEL32.DLL and is also a stdcall function. So how +can this possibly work? The FFI library provides the ffi.C +default C library namespace, which allows calling functions from +the default set of libraries, like a C compiler would. Also, the +FFI library automatically detects stdcall functions, so you +don't need to declare them as such. +

+

+ The poll() +function takes a couple more arguments we're not going to use. You can +simply use nil to pass a NULL pointer and 0 +for the nfds parameter. Please note that the +number 0 does not convert to a pointer value, +unlike in C++. You really have to pass pointers to pointer arguments +and numbers to number arguments. +

+

+The page on FFI semantics has all +of the gory details about +conversions between Lua +objects and C types. For the most part you don't have to deal +with this, as it's performed automatically and it's carefully designed +to bridge the semantic differences between Lua and C. +

+

+ Now that we have defined +our own sleep() function, we can just call it from plain Lua +code. That wasn't so bad, huh? Turning these boring animated dots into +a fascinating best-selling game is left as an exercise for the reader. +:-) +

+ +

Accessing the zlib Compression Library

+

+The following code shows how to access the zlib compression library from Lua code. +We'll define two convenience wrapper functions that take a string and +compress or uncompress it to another string: +

+
+ 
+①
+
+
+
+
+
+
+②
+
+
+③
+
+④
+
+
+⑤
+
+
+⑥
+
+
+
+
+
+
+
+⑦local ffi = require("ffi")
+ffi.cdef[[
+unsigned long compressBound(unsigned long sourceLen);
+int compress2(uint8_t *dest, unsigned long *destLen,
+	      const uint8_t *source, unsigned long sourceLen, int level);
+int uncompress(uint8_t *dest, unsigned long *destLen,
+	       const uint8_t *source, unsigned long sourceLen);
+]]
+local zlib = ffi.load(ffi.os == "Windows" and "zlib1" or "z")
+
+local function compress(txt)
+  local n = zlib.compressBound(#txt)
+  local buf = ffi.new("uint8_t[?]", n)
+  local buflen = ffi.new("unsigned long[1]", n)
+  local res = zlib.compress2(buf, buflen, txt, #txt, 9)
+  assert(res == 0)
+  return ffi.string(buf, buflen[0])
+end
+
+local function uncompress(comp, n)
+  local buf = ffi.new("uint8_t[?]", n)
+  local buflen = ffi.new("unsigned long[1]", n)
+  local res = zlib.uncompress(buf, buflen, comp, #comp)
+  assert(res == 0)
+  return ffi.string(buf, buflen[0])
+end
+
+-- Simple test code.
+local txt = string.rep("abcd", 1000)
+print("Uncompressed size: ", #txt)
+local c = compress(txt)
+print("Compressed size: ", #c)
+local txt2 = uncompress(c, #txt)
+assert(txt2 == txt)
+
+

+Here's the step-by-step explanation: +

+

+ This defines some of the +C functions provided by zlib. For the sake of this example, some +type indirections have been reduced and it uses the pre-defined +fixed-size integer types, while still adhering to the zlib API/ABI. +

+

+ This loads the zlib shared +library. On POSIX systems it's named libz.so and usually +comes pre-installed. Since ffi.load() automatically adds any +missing standard prefixes/suffixes, we can simply load the +"z" library. On Windows it's named zlib1.dll and +you'll have to download it first from the +» zlib site. The check for +ffi.os makes sure we pass the right name to +ffi.load(). +

+

+ First, the maximum size of +the compression buffer is obtained by calling the +zlib.compressBound function with the length of the +uncompressed string. The next line allocates a byte buffer of this +size. The [?] in the type specification indicates a +variable-length array (VLA). The actual number of elements of this +array is given as the 2nd argument to ffi.new(). +

+

+ This may look strange at +first, but have a look at the declaration of the compress2 +function from zlib: the destination length is defined as a pointer! +This is because you pass in the maximum buffer size and get back the +actual length that was used. +

+

+In C you'd pass in the address of a local variable +(&buflen). But since there's no address-of operator in +Lua, we'll just pass in a one-element array. Conveniently it can be +initialized with the maximum buffer size in one step. Calling the +actual zlib.compress2 function is then straightforward. +

+

+ We want to return the +compressed data as a Lua string, so we'll use ffi.string(). +It needs a pointer to the start of the data and the actual length. The +length has been returned in the buflen array, so we'll just +get it from there. +

+

+Note that since the function returns now, the buf and +buflen variables will eventually be garbage collected. This +is fine, because ffi.string() has copied the contents to a +newly created (interned) Lua string. If you plan to call this function +lots of times, consider reusing the buffers and/or handing back the +results in buffers instead of strings. This will reduce the overhead +for garbage collection and string interning. +

+

+ The uncompress +functions does the exact opposite of the compress function. +The compressed data doesn't include the size of the original string, +so this needs to be passed in. Otherwise no surprises here. +

+

+ The code, that makes use +of the functions we just defined, is just plain Lua code. It doesn't +need to know anything about the LuaJIT FFI — the convenience +wrapper functions completely hide it. +

+

+One major advantage of the LuaJIT FFI is that you are now able to +write those wrappers in Lua. And at a fraction of the time it +would cost you to create an extra C module using the Lua/C API. +Many of the simpler C functions can probably be used directly +from your Lua code, without any wrappers. +

+

+Side note: the zlib API uses the long type for passing +lengths and sizes around. But all those zlib functions actually only +deal with 32 bit values. This is an unfortunate choice for a +public API, but may be explained by zlib's history — we'll just +have to deal with it. +

+

+First, you should know that a long is a 64 bit type e.g. +on POSIX/x64 systems, but a 32 bit type on Windows/x64 and on +32 bit systems. Thus a long result can be either a plain +Lua number or a boxed 64 bit integer cdata object, depending on +the target system. +

+

+Ok, so the ffi.* functions generally accept cdata objects +wherever you'd want to use a number. That's why we get a away with +passing n to ffi.string() above. But other Lua +library functions or modules don't know how to deal with this. So for +maximum portability one needs to use tonumber() on returned +long results before passing them on. Otherwise the +application might work on some systems, but would fail in a POSIX/x64 +environment. +

+ +

Defining Metamethods for a C Type

+

+The following code explains how to define metamethods for a C type. +We define a simple point type and add some operations to it: +

+
+ 
+①
+
+
+
+②
+
+③
+
+④
+
+
+
+⑤
+
+⑥local ffi = require("ffi")
+ffi.cdef[[
+typedef struct { double x, y; } point_t;
+]]
+
+local point
+local mt = {
+  __add = function(a, b) return point(a.x+b.x, a.y+b.y) end,
+  __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,
+  __index = {
+    area = function(a) return a.x*a.x + a.y*a.y end,
+  },
+}
+point = ffi.metatype("point_t", mt)
+
+local a = point(3, 4)
+print(a.x, a.y)  --> 3  4
+print(#a)        --> 5
+print(a:area())  --> 25
+local b = a + point(0.5, 8)
+print(#b)        --> 12.5
+
+

+Here's the step-by-step explanation: +

+

+ This defines the C type for a +two-dimensional point object. +

+

+ We have to declare the variable +holding the point constructor first, because it's used inside of a +metamethod. +

+

+ Let's define an __add +metamethod which adds the coordinates of two points and creates a new +point object. For simplicity, this function assumes that both arguments +are points. But it could be any mix of objects, if at least one operand +is of the required type (e.g. adding a point plus a number or vice +versa). Our __len metamethod returns the distance of a point to +the origin. +

+

+ If we run out of operators, we can +define named methods, too. Here the __index table defines an +area function. For custom indexing needs, one might want to +define __index and __newindex functions instead. +

+

+ This associates the metamethods with +our C type. This only needs to be done once. For convenience, a +constructor is returned by +ffi.metatype(). +We're not required to use it, though. The original C type can still +be used e.g. to create an array of points. The metamethods automatically +apply to any and all uses of this type. +

+

+Please note that the association with a metatable is permanent and +the metatable must not be modified afterwards! Ditto for the +__index table. +

+

+ Here are some simple usage examples +for the point type and their expected results. The pre-defined +operations (such as a.x) can be freely mixed with the newly +defined metamethods. Note that area is a method and must be +called with the Lua syntax for methods: a:area(), not +a.area(). +

+

+The C type metamethod mechanism is most useful when used in +conjunction with C libraries that are written in an object-oriented +style. Creators return a pointer to a new instance and methods take an +instance pointer as the first argument. Sometimes you can just point +__index to the library namespace and __gc to the +destructor and you're done. But often enough you'll want to add +convenience wrappers, e.g. to return actual Lua strings or when +returning multiple values. +

+

+Some C libraries only declare instance pointers as an opaque +void * type. In this case you can use a fake type for all +declarations, e.g. a pointer to a named (incomplete) struct will do: +typedef struct foo_type *foo_handle. The C side doesn't +know what you declare with the LuaJIT FFI, but as long as the underlying +types are compatible, everything still works. +

+ +

Translating C Idioms

+

+Here's a list of common C idioms and their translation to the +LuaJIT FFI: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IdiomC codeLua code
Pointer dereference
int *p;
x = *p;
*p = y;
x = p[0]
p[0] = y
Pointer indexing
int i, *p;
x = p[i];
p[i+1] = y;
x = p[i]
p[i+1] = y
Array indexing
int i, a[];
x = a[i];
a[i+1] = y;
x = a[i]
a[i+1] = y
struct/union dereference
struct foo s;
x = s.field;
s.field = y;
x = s.field
s.field = y
struct/union pointer deref.
struct foo *sp;
x = sp->field;
sp->field = y;
x = s.field
s.field = y
Pointer arithmetic
int i, *p;
x = p + i;
y = p - i;
x = p + i
y = p - i
Pointer difference
int *p1, *p2;
x = p1 - p2;x = p1 - p2
Array element pointer
int i, a[];
x = &a[i];x = a+i
Cast pointer to address
int *p;
x = (intptr_t)p;x = tonumber(
 ffi.cast("intptr_t",
          p))
Functions with outargs
void foo(int *inoutlen);
int len = x;
foo(&len);
y = len;
local len =
  ffi.new("int[1]", x)
foo(len)
y = len[0]
Vararg conversions
int printf(char *fmt, ...);
printf("%g", 1.0);
printf("%d", 1);
 
printf("%g", 1);
printf("%d",
  ffi.new("int", 1))
+ +

To Cache or Not to Cache

+

+It's a common Lua idiom to cache library functions in local variables +or upvalues, e.g.: +

+
+local byte, char = string.byte, string.char
+local function foo(x)
+  return char(byte(x)+1)
+end
+
+

+This replaces several hash-table lookups with a (faster) direct use of +a local or an upvalue. This is less important with LuaJIT, since the +JIT compiler optimizes hash-table lookups a lot and is even able to +hoist most of them out of the inner loops. It can't eliminate +all of them, though, and it saves some typing for often-used +functions. So there's still a place for this, even with LuaJIT. +

+

+The situation is a bit different with C function calls via the +FFI library. The JIT compiler has special logic to eliminate all +of the lookup overhead for functions resolved from a +C library namespace! +Thus it's not helpful and actually counter-productive to cache +individual C functions like this: +

+
+local funca, funcb = ffi.C.funcb, ffi.C.funcb -- Not helpful!
+local function foo(x, n)
+  for i=1,n do funcb(funca(x, i), 1) end
+end
+
+

+This turns them into indirect calls and generates bigger and slower +machine code. Instead you'll want to cache the namespace itself and +rely on the JIT compiler to eliminate the lookups: +

+
+local C = ffi.C          -- Instead use this!
+local function foo(x, n)
+  for i=1,n do C.funcb(C.funca(x, i), 1) end
+end
+
+

+This generates both shorter and faster code. So don't cache +C functions, but do cache namespaces! Most often the +namespace is already in a local variable at an outer scope, e.g. from +local lib = ffi.load(...). Note that copying +it to a local variable in the function scope is unnecessary. +

+
+
+ + + diff --git a/external/lua/luajit/src/doc/ext_jit.html b/external/lua/luajit/src/doc/ext_jit.html new file mode 100644 index 0000000000..cc00e72b1b --- /dev/null +++ b/external/lua/luajit/src/doc/ext_jit.html @@ -0,0 +1,199 @@ + + + +jit.* Library + + + + + + + + +
+Lua +
+ + +
+

+The functions in this built-in module control the behavior of the JIT +compiler engine. Note that JIT-compilation is fully automatic — +you probably won't need to use any of the following functions unless +you have special needs. +

+ +

jit.on()
+jit.off()

+

+Turns the whole JIT compiler on (default) or off. +

+

+These functions are typically used with the command line options +-j on or -j off. +

+ +

jit.flush()

+

+Flushes the whole cache of compiled code. +

+ +

jit.on(func|true [,true|false])
+jit.off(func|true [,true|false])
+jit.flush(func|true [,true|false])

+

+jit.on enables JIT compilation for a Lua function (this is +the default). +

+

+jit.off disables JIT compilation for a Lua function and +flushes any already compiled code from the code cache. +

+

+jit.flush flushes the code, but doesn't affect the +enable/disable status. +

+

+The current function, i.e. the Lua function calling this library +function, can also be specified by passing true as the first +argument. +

+

+If the second argument is true, JIT compilation is also +enabled, disabled or flushed recursively for all sub-functions of a +function. With false only the sub-functions are affected. +

+

+The jit.on and jit.off functions only set a flag +which is checked when the function is about to be compiled. They do +not trigger immediate compilation. +

+

+Typical usage is jit.off(true, true) in the main chunk +of a module to turn off JIT compilation for the whole module for +debugging purposes. +

+ +

jit.flush(tr)

+

+Flushes the root trace, specified by its number, and all of its side +traces from the cache. The code for the trace will be retained as long +as there are any other traces which link to it. +

+ +

status, ... = jit.status()

+

+Returns the current status of the JIT compiler. The first result is +either true or false if the JIT compiler is turned +on or off. The remaining results are strings for CPU-specific features +and enabled optimizations. +

+ +

jit.version

+

+Contains the LuaJIT version string. +

+ +

jit.version_num

+

+Contains the version number of the LuaJIT core. Version xx.yy.zz +is represented by the decimal number xxyyzz. +

+ +

jit.os

+

+Contains the target OS name: +"Windows", "Linux", "OSX", "BSD", "POSIX" or "Other". +

+ +

jit.arch

+

+Contains the target architecture name: +"x86", "x64" or "ppcspe". +

+ +

jit.opt.* — JIT compiler optimization control

+

+This sub-module provides the backend for the -O command line +option. +

+

+You can also use it programmatically, e.g.: +

+
+jit.opt.start(2) -- same as -O2
+jit.opt.start("-dce")
+jit.opt.start("hotloop=10", "hotexit=2")
+
+

+Unlike in LuaJIT 1.x, the module is built-in and +optimization is turned on by default! +It's no longer necessary to run require("jit.opt").start(), +which was one of the ways to enable optimization. +

+ +

jit.util.* — JIT compiler introspection

+

+This sub-module holds functions to introspect the bytecode, generated +traces, the IR and the generated machine code. The functionality +provided by this module is still in flux and therefore undocumented. +

+

+The debug modules -jbc, -jv and -jdump make +extensive use of these functions. Please check out their source code, +if you want to know more. +

+
+
+ + + diff --git a/external/lua/luajit/src/doc/extensions.html b/external/lua/luajit/src/doc/extensions.html new file mode 100644 index 0000000000..2bfcc76c39 --- /dev/null +++ b/external/lua/luajit/src/doc/extensions.html @@ -0,0 +1,407 @@ + + + +Extensions + + + + + + + + + +
+Lua +
+ + +
+

+LuaJIT is fully upwards-compatible with Lua 5.1. It supports all +» standard Lua +library functions and the full set of +» Lua/C API +functions. +

+

+LuaJIT is also fully ABI-compatible to Lua 5.1 at the linker/dynamic +loader level. This means you can compile a C module against the +standard Lua headers and load the same shared library from either Lua +or LuaJIT. +

+

+LuaJIT extends the standard Lua VM with new functionality and adds +several extension modules. Please note this page is only about +functional enhancements and not about performance enhancements, +such as the optimized VM, the faster interpreter or the JIT compiler. +

+ +

Extensions Modules

+

+LuaJIT comes with several built-in extension modules: +

+ +

bit.* — Bitwise operations

+

+LuaJIT supports all bitwise operations as defined by +» Lua BitOp: +

+
+bit.tobit  bit.tohex  bit.bnot    bit.band bit.bor  bit.bxor
+bit.lshift bit.rshift bit.arshift bit.rol  bit.ror  bit.bswap
+
+

+This module is a LuaJIT built-in — you don't need to download or +install Lua BitOp. The Lua BitOp site has full documentation for all +» Lua BitOp API functions. +

+

+Please make sure to require the module before using any of +its functions: +

+
+local bit = require("bit")
+
+

+An already installed Lua BitOp module is ignored by LuaJIT. +This way you can use bit operations from both Lua and LuaJIT on a +shared installation. +

+ +

ffi.* — FFI library

+

+The FFI library allows calling external +C functions and the use of C data structures from pure Lua +code. +

+ +

jit.* — JIT compiler control

+

+The functions in this module +control the behavior of the JIT compiler engine. +

+ +

C API extensions

+

+LuaJIT adds some +extra functions to the Lua/C API. +

+ +

Enhanced Standard Library Functions

+ +

xpcall(f, err [,args...]) passes arguments

+

+Unlike the standard implementation in Lua 5.1, xpcall() +passes any arguments after the error function to the function +which is called in a protected context. +

+ +

loadfile() etc. handle UTF-8 source code

+

+Non-ASCII characters are handled transparently by the Lua source code parser. +This allows the use of UTF-8 characters in identifiers and strings. +A UTF-8 BOM is skipped at the start of the source code. +

+ +

tostring() etc. canonicalize NaN and ±Inf

+

+All number-to-string conversions consistently convert non-finite numbers +to the same strings on all platforms. NaN results in "nan", +positive infinity results in "inf" and negative infinity results +in "-inf". +

+ +

tonumber() etc. use builtin string to number conversion

+

+All string-to-number conversions consistently convert integer and +floating-point inputs in decimal and hexadecimal on all platforms. +strtod() is not used anymore, which avoids numerous +problems with poor C library implementations. The builtin conversion +function provides full precision according to the IEEE-754 standard, it +works independently of the current locale and it supports hex floating-point +numbers (e.g. 0x1.5p-3). +

+ +

string.dump(f [,strip]) generates portable bytecode

+

+An extra argument has been added to string.dump(). If set to +true, 'stripped' bytecode without debug information is +generated. This speeds up later bytecode loading and reduces memory +usage. See also the +-b command line option. +

+

+The generated bytecode is portable and can be loaded on any architecture +that LuaJIT supports, independent of word size or endianess. However the +bytecode compatibility versions must match. Bytecode stays compatible +for dot releases (x.y.0 → x.y.1), but may change with major or +minor releases (2.0 → 2.1) or between any beta release. Foreign +bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded. +

+ +

Enhanced PRNG for math.random()

+

+LuaJIT uses a Tausworthe PRNG with period 2^223 to implement +math.random() and math.randomseed(). The quality of +the PRNG results is much superior compared to the standard Lua +implementation which uses the platform-specific ANSI rand(). +

+

+The PRNG generates the same sequences from the same seeds on all +platforms and makes use of all bits in the seed argument. +math.random() without arguments generates 52 pseudo-random bits +for every call. The result is uniformly distributed between 0.0 and 1.0. +It's correctly scaled up and rounded for math.random(n [,m]) to +preserve uniformity. +

+ +

io.* functions handle 64 bit file offsets

+

+The file I/O functions in the standard io.* library handle +64 bit file offsets. In particular this means it's possible +to open files larger than 2 Gigabytes and to reposition or obtain +the current file position for offsets beyond 2 GB +(fp:seek() method). +

+ +

debug.* functions identify metamethods

+

+debug.getinfo() and lua_getinfo() also return information +about invoked metamethods. The namewhat field is set to +"metamethod" and the name field has the name of +the corresponding metamethod (e.g. "__index"). +

+ +

Fully Resumable VM

+

+The LuaJIT VM is fully resumable. This means you can yield from a +coroutine even across contexts, where this would not possible with +the standard Lua 5.1 VM: e.g. you can yield across pcall() +and xpcall(), across iterators and across metamethods. +

+ +

Extensions from Lua 5.2

+

+LuaJIT supports some language and library extensions from Lua 5.2. +Features that are unlikely to break existing code are unconditionally +enabled: +

+
    +
  • goto and ::labels::.
  • +
  • Hex escapes '\x3F' and '\*' escape in strings.
  • +
  • load(string|reader, chunkname [,mode [,env]]). +loadstring() is an alias.
  • +
  • math.log(x [,base]). +
  • string.rep(s, n [,sep]). +
  • string.format(): %q reversible. +%s checks __tostring. +%a and "%A added.
  • +
  • String matching pattern %g added.
  • +
  • io.read("*L").
  • +
  • io.lines() and file:lines() process +io.read() options.
  • +
  • os.exit(status|true|false [,close]).
  • +
  • package.searchpath(name, path [, sep [, rep]]).
  • +
  • package.loadlib(name, "*").
  • +
  • debug.getinfo() returns nparams and isvararg +for option "u".
  • +
  • debug.getlocal() accepts function instead of level.
  • +
  • debug.getlocal() and debug.setlocal() accept negative +indexes for varargs.
  • +
  • debug.getupvalue() and debug.setupvalue() handle +C functions.
  • +
  • debug.upvalueid() and debug.upvaluejoin().
  • +
  • Command line option -E.
  • +
  • Command line checks __tostring for errors.
  • +
+

+Other features are only enabled, if LuaJIT is built with +-DLUAJIT_ENABLE_LUA52COMPAT: +

+
    +
  • goto is a keyword and not a valid variable name anymore.
  • +
  • break can be placed anywhere. Empty statements (;;) +are allowed.
  • +
  • __lt, __le are invoked for mixed types.
  • +
  • __len for tables. rawlen() library function.
  • +
  • pairs() and ipairs() check for __pairs and +__ipairs.
  • +
  • coroutine.running() returns two results.
  • +
  • table.pack() and table.unpack() +(same as unpack()).
  • +
  • io.write() and file:write() return file handle +instead of true.
  • +
  • os.execute() and pipe:close() return detailed +exit status.
  • +
  • debug.setmetatable() returns object.
  • +
  • debug.getuservalue() and debug.setuservalue().
  • +
  • Remove math.mod(), string.gfind(). +
+

+Note: this provides only partial compatibility with Lua 5.2 at the +language and Lua library level. LuaJIT is API+ABI-compatible with +Lua 5.1, which prevents implementing features that would otherwise +break the Lua/C API and ABI (e.g. _ENV). +

+ +

C++ Exception Interoperability

+

+LuaJIT has built-in support for interoperating with C++ exceptions. +The available range of features depends on the target platform and +the toolchain used to compile LuaJIT: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PlatformCompilerInteroperability
POSIX/x64, DWARF2 unwindingGCC 4.3+Full
Other platforms, DWARF2 unwindingGCCLimited
Windows/x64MSVC or WinSDKFull
Windows/x86AnyNo
Other platformsOther compilersNo
+

+Full interoperability means: +

+
    +
  • C++ exceptions can be caught on the Lua side with pcall(), +lua_pcall() etc.
  • +
  • C++ exceptions will be converted to the generic Lua error +"C++ exception", unless you use the +C call wrapper feature.
  • +
  • It's safe to throw C++ exceptions across non-protected Lua frames +on the C stack. The contents of the C++ exception object +pass through unmodified.
  • +
  • Lua errors can be caught on the C++ side with catch(...). +The corresponding Lua error message can be retrieved from the Lua stack.
  • +
  • Throwing Lua errors across C++ frames is safe. C++ destructors +will be called.
  • +
+

+Limited interoperability means: +

+
    +
  • C++ exceptions can be caught on the Lua side with pcall(), +lua_pcall() etc.
  • +
  • C++ exceptions will be converted to the generic Lua error +"C++ exception", unless you use the +C call wrapper feature.
  • +
  • C++ exceptions will be caught by non-protected Lua frames and +are rethrown as a generic Lua error. The C++ exception object will +be destroyed.
  • +
  • Lua errors cannot be caught on the C++ side.
  • +
  • Throwing Lua errors across C++ frames will not call +C++ destructors.
  • +
+ +

+No interoperability means: +

+
    +
  • It's not safe to throw C++ exceptions across Lua frames.
  • +
  • C++ exceptions cannot be caught on the Lua side.
  • +
  • Lua errors cannot be caught on the C++ side.
  • +
  • Throwing Lua errors across C++ frames will not call +C++ destructors.
  • +
  • Additionally, on Windows/x86 with SEH-based C++ exceptions: +it's not safe to throw a Lua error across any frames containing +a C++ function with any try/catch construct or using variables with +(implicit) destructors. This also applies to any functions which may be +inlined in such a function. It doesn't matter whether lua_error() +is called inside or outside of a try/catch or whether any object actually +needs to be destroyed: the SEH chain is corrupted and this will eventually +lead to the termination of the process.
  • +
+
+
+ + + diff --git a/external/lua/luajit/src/doc/faq.html b/external/lua/luajit/src/doc/faq.html new file mode 100644 index 0000000000..c61b8dcf1f --- /dev/null +++ b/external/lua/luajit/src/doc/faq.html @@ -0,0 +1,184 @@ + + + +Frequently Asked Questions (FAQ) + + + + + + + + + +
+Lua +
+ + +
+
+
Q: Where can I learn more about LuaJIT and Lua?
+
+ +
+ +
+
Q: Where can I learn more about the compiler technology used by LuaJIT?
+
+I'm planning to write more documentation about the internals of LuaJIT. +In the meantime, please use the following Google Scholar searches +to find relevant papers:
+Search for: » Trace Compiler
+Search for: » JIT Compiler
+Search for: » Dynamic Language Optimizations
+Search for: » SSA Form
+Search for: » Linear Scan Register Allocation
+Here is a list of the » innovative features in LuaJIT.
+And, you know, reading the source is of course the only way to enlightenment. :-) +
+
+ +
+
Q: Why do I get this error: "attempt to index global 'arg' (a nil value)"?
+Q: My vararg functions fail after switching to LuaJIT!
+
LuaJIT is compatible to the Lua 5.1 language standard. It doesn't +support the implicit arg parameter for old-style vararg +functions from Lua 5.0.
Please convert your code to the +» Lua 5.1 +vararg syntax.
+
+ +
+
Q: Why do I get this error: "bad FPU precision"?
+
Q: I get weird behavior after initializing Direct3D.
+
Q: Some FPU operations crash after I load a Delphi DLL.
+
+
+ +DirectX/Direct3D (up to version 9) sets the x87 FPU to single-precision +mode by default. This violates the Windows ABI and interferes with the +operation of many programs — LuaJIT is affected, too. Please make +sure you always use the D3DCREATE_FPU_PRESERVE flag when +initializing Direct3D.
+ +Direct3D version 10 or higher do not show this behavior anymore. +Consider testing your application with older versions, too.
+ +Similarly, the Borland/Delphi runtime modifies the FPU control word and +enables FP exceptions. Of course this violates the Windows ABI, too. +Please check the Delphi docs for the Set8087CW method. + +
+ +
+
Q: Sometimes Ctrl-C fails to stop my Lua program. Why?
+
The interrupt signal handler sets a Lua debug hook. But this is +currently ignored by compiled code (this will eventually be fixed). If +your program is running in a tight loop and never falls back to the +interpreter, the debug hook never runs and can't throw the +"interrupted!" error.
In the meantime you have to press Ctrl-C +twice to get stop your program. That's similar to when it's stuck +running inside a C function under the Lua interpreter.
+
+ +
+
Q: Why doesn't my favorite power-patch for Lua apply against LuaJIT?
+
Because it's a completely redesigned VM and has very little code +in common with Lua anymore. Also, if the patch introduces changes to +the Lua semantics, these would need to be reflected everywhere in the +VM, from the interpreter up to all stages of the compiler.
Please +use only standard Lua language constructs. For many common needs you +can use source transformations or use wrapper or proxy functions. +The compiler will happily optimize away such indirections.
+
+ +
+
Q: Lua runs everywhere. Why doesn't LuaJIT support my CPU?
+
Because it's a compiler — it needs to generate native +machine code. This means the code generator must be ported to each +architecture. And the fast interpreter is written in assembler and +must be ported, too. This is quite an undertaking.
+The install documentation shows the supported +architectures. Other architectures will follow based on sufficient user +demand and/or sponsoring.
+
+ +
+
Q: When will feature X be added? When will the next version be released?
+
When it's ready.
+C'mon, it's open source — I'm doing it on my own time and you're +getting it for free. You can either contribute a patch or sponsor +the development of certain features, if they are important to you. +
+
+
+
+ + + diff --git a/external/lua/luajit/src/doc/install.html b/external/lua/luajit/src/doc/install.html new file mode 100644 index 0000000000..f3c71d00d3 --- /dev/null +++ b/external/lua/luajit/src/doc/install.html @@ -0,0 +1,613 @@ + + + +Installation + + + + + + + + + +
+Lua +
+ + +
+

+LuaJIT is only distributed as a source package. This page explains +how to build and install LuaJIT with different operating systems +and C compilers. +

+

+For the impatient (on POSIX systems): +

+
+make && sudo make install
+
+

+LuaJIT currently builds out-of-the box on most systems. +Here's the compatibility matrix for the supported combinations of +operating systems, CPUs and compilers: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CPU / OSLinux or
Android
*BSD, OtherOSX 10.4+ or
iOS 3.0+
Windows
XP/Vista/7
x86 (32 bit)GCC 4.x
GCC 3.4
GCC 4.x
GCC 3.4
GCC 4.x
GCC 3.4
MSVC, MSVC/EE
WinSDK
MinGW, Cygwin
x64 (64 bit)GCC 4.x GCC 4.xMSVC + SDK v7.0
WinSDK v7.0
ARMv5+
ARM9E+
GCC 4.2+GCC 4.2+GCC 4.2+ 
PPCGCC 4.3+GCC 4.3+
GCC 4.1 (PS3)
 XEDK (Xbox 360)
PPC/e500v2GCC 4.3+GCC 4.3+  
MIPSGCC 4.3+GCC 4.3+  
+ +

Configuring LuaJIT

+

+The standard configuration should work fine for most installations. +Usually there is no need to tweak the settings. The following files +hold all user-configurable settings: +

+
    +
  • src/luaconf.h sets some configuration variables.
  • +
  • Makefile has settings for installing LuaJIT (POSIX +only).
  • +
  • src/Makefile has settings for compiling LuaJIT +under POSIX, MinGW or Cygwin.
  • +
  • src/msvcbuild.bat has settings for compiling LuaJIT with +MSVC or WinSDK.
  • +
+

+Please read the instructions given in these files, before changing +any settings. +

+ +

POSIX Systems (Linux, OSX, *BSD etc.)

+

Prerequisites

+

+Depending on your distribution, you may need to install a package for +GCC, the development headers and/or a complete SDK. E.g. on a current +Debian/Ubuntu, install libc6-dev with the package manager. +

+

+Download the current source package of LuaJIT (pick the .tar.gz), +if you haven't already done so. Move it to a directory of your choice, +open a terminal window and change to this directory. Now unpack the archive +and change to the newly created directory: +

+
+tar zxf LuaJIT-2.0.1.tar.gz
+cd LuaJIT-2.0.1
+

Building LuaJIT

+

+The supplied Makefiles try to auto-detect the settings needed for your +operating system and your compiler. They need to be run with GNU Make, +which is probably the default on your system, anyway. Simply run: +

+
+make
+
+

+This always builds a native x86, x64 or PPC binary, depending on the host OS +you're running this command on. Check the section on +cross-compilation for more options. +

+

+By default, modules are only searched under the prefix /usr/local. +You can add an extra prefix to the search paths by appending the +PREFIX option, e.g.: +

+
+make PREFIX=/home/myself/lj2
+
+

+Note for OSX: if the MACOSX_DEPLOYMENT_TARGET environment +variable is not set, then it's forced to 10.4. +

+

Installing LuaJIT

+

+The top-level Makefile installs LuaJIT by default under +/usr/local, i.e. the executable ends up in +/usr/local/bin and so on. You need root privileges +to write to this path. So, assuming sudo is installed on your system, +run the following command and enter your sudo password: +

+
+sudo make install
+
+

+Otherwise specify the directory prefix as an absolute path, e.g.: +

+
+make install PREFIX=/home/myself/lj2
+
+

+Obviously the prefixes given during build and installation need to be the same. +

+ +

Windows Systems

+

Prerequisites

+

+Either install one of the open source SDKs +(» MinGW or +» Cygwin), which come with a modified +GCC plus the required development headers. +

+

+Or install Microsoft's Visual C++ (MSVC). The freely downloadable +» Express Edition +works just fine, but only contains an x86 compiler. +

+

+The freely downloadable +» Windows SDK +only comes with command line tools, but this is all you need to build LuaJIT. +It contains x86 and x64 compilers. +

+

+Next, download the source package and unpack it using an archive manager +(e.g. the Windows Explorer) to a directory of your choice. +

+

Building with MSVC

+

+Open a "Visual Studio .NET Command Prompt", cd to the +directory where you've unpacked the sources and run these commands: +

+
+cd src
+msvcbuild
+
+

+Then follow the installation instructions below. +

+

Building with the Windows SDK

+

+Open a "Windows SDK Command Shell" and select the x86 compiler: +

+
+setenv /release /x86
+
+

+Or select the x64 compiler: +

+
+setenv /release /x64
+
+

+Then cd to the directory where you've unpacked the sources +and run these commands: +

+
+cd src
+msvcbuild
+
+

+Then follow the installation instructions below. +

+

Building with MinGW or Cygwin

+

+Open a command prompt window and make sure the MinGW or Cygwin programs +are in your path. Then cd to the directory where +you've unpacked the sources and run this command for MinGW: +

+
+mingw32-make
+
+

+Or this command for Cygwin: +

+
+make
+
+

+Then follow the installation instructions below. +

+

Installing LuaJIT

+

+Copy luajit.exe and lua51.dll (built in the src +directory) to a newly created directory (any location is ok). +Add lua and lua\jit directories below it and copy +all Lua files from the src\jit directory of the distribution +to the latter directory. +

+

+There are no hardcoded +absolute path names — all modules are loaded relative to the +directory where luajit.exe is installed +(see src/luaconf.h). +

+ +

Cross-compiling LuaJIT

+

+The GNU Makefile-based build system allows cross-compiling on any host +for any supported target, as long as both architectures have the same +pointer size. If you want to cross-compile to any 32 bit target on an +x64 OS, you need to install the multilib development package (e.g. +libc6-dev-i386 on Debian/Ubuntu) and build a 32 bit host part +(HOST_CC="gcc -m32"). +

+

+You need to specify TARGET_SYS whenever the host OS and the +target OS differ, or you'll get assembler or linker errors. E.g. if +you're compiling on a Windows or OSX host for embedded Linux or Android, +you need to add TARGET_SYS=Linux to the examples below. For a +minimal target OS, you may need to disable the built-in allocator in +src/Makefile and use TARGET_SYS=Other. The examples +below only show some popular targets — please check the comments +in src/Makefile for more details. +

+
+# Cross-compile to a 32 bit binary on a multilib x64 OS
+make CC="gcc -m32"
+
+# Cross-compile on Debian/Ubuntu for Windows (mingw32 package)
+make HOST_CC="gcc -m32" CROSS=i586-mingw32msvc- TARGET_SYS=Windows
+
+

+The CROSS prefix allows specifying a standard GNU cross-compile +toolchain (Binutils, GCC and a matching libc). The prefix may vary +depending on the --target the toolchain was built for (note the +CROSS prefix has a trailing "-"). The examples below +use the canonical toolchain triplets for Linux. +

+

+Since there's often no easy way to detect CPU features at runtime, it's +important to compile with the proper CPU or architecture settings. You +can specify these when building the toolchain yourself. Or add +-mcpu=... or -march=... to TARGET_CFLAGS. For +ARM it's important to have the correct -mfloat-abi=... setting, +too. Otherwise LuaJIT may not run at the full performance of your target +CPU. +

+
+# ARM soft-float
+make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabi- \
+     TARGET_CFLAGS="-mfloat-abi=soft"
+
+# ARM soft-float ABI with VFP (example for Cortex-A8)
+make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabi- \
+     TARGET_CFLAGS="-mcpu=cortex-a8 -mfloat-abi=softfp"
+
+# ARM hard-float ABI with VFP (armhf, requires recent toolchain)
+make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabihf-
+
+# PPC
+make HOST_CC="gcc -m32" CROSS=powerpc-linux-gnu-
+# PPC/e500v2 (fast interpreter only)
+make HOST_CC="gcc -m32" CROSS=powerpc-e500v2-linux-gnuspe-
+
+# MIPS big-endian
+make HOST_CC="gcc -m32" CROSS=mips-linux-
+# MIPS little-endian
+make HOST_CC="gcc -m32" CROSS=mipsel-linux-
+
+

+You can cross-compile for Android using the » Android NDK. +The environment variables need to match the install locations and the +desired target platform. E.g. Android 4.0 corresponds to ABI level 14. +For details check the folder docs in the NDK directory. +

+

+Only a few common variations for the different CPUs, ABIs and platforms +are listed. Please use your own judgement for which combination you want +to build/deploy or which lowest common denominator you want to pick: +

+
+# Android/ARM, armeabi (ARMv5TE soft-float), Android 2.2+ (Froyo)
+NDK=/opt/android/ndk
+NDKABI=8
+NDKVER=$NDK/toolchains/arm-linux-androideabi-4.6
+NDKP=$NDKVER/prebuilt/linux-x86/bin/arm-linux-androideabi-
+NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
+make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF"
+
+# Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.0+ (ICS)
+NDK=/opt/android/ndk
+NDKABI=14
+NDKVER=$NDK/toolchains/arm-linux-androideabi-4.6
+NDKP=$NDKVER/prebuilt/linux-x86/bin/arm-linux-androideabi-
+NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
+NDKARCH="-march=armv7-a -mfloat-abi=softfp -Wl,--fix-cortex-a8"
+make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF $NDKARCH"
+
+# Android/MIPS, mips (MIPS32R1 hard-float), Android 4.0+ (ICS)
+NDK=/opt/android/ndk
+NDKABI=14
+NDKVER=$NDK/toolchains/mipsel-linux-android-4.6
+NDKP=$NDKVER/prebuilt/linux-x86/bin/mipsel-linux-android-
+NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-mips"
+make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF"
+
+# Android/x86, x86 (i686 SSE3), Android 4.0+ (ICS)
+NDK=/opt/android/ndk
+NDKABI=14
+NDKVER=$NDK/toolchains/x86-4.6
+NDKP=$NDKVER/prebuilt/linux-x86/bin/i686-linux-android-
+NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-x86"
+make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF"
+
+

+You can cross-compile for iOS 3.0+ (iPhone/iPad) using the » iOS SDK. +The environment variables need to match the iOS SDK version: +

+

+Note: the JIT compiler is disabled for iOS, because regular iOS Apps +are not allowed to generate code at runtime. You'll only get the performance +of the LuaJIT interpreter on iOS. This is still faster than plain Lua, but +much slower than the JIT compiler. Please complain to Apple, not me. +Or use Android. :-p +

+
+IXCODE=`xcode-select -print-path`
+ISDK=$IXCODE/Platforms/iPhoneOS.platform/Developer
+ISDKVER=iPhoneOS6.0.sdk
+ISDKP=$ISDK/usr/bin/
+ISDKF="-arch armv7 -isysroot $ISDK/SDKs/$ISDKVER"
+make HOST_CC="gcc -m32 -arch i386" CROSS=$ISDKP TARGET_FLAGS="$ISDKF" \
+     TARGET_SYS=iOS
+
+

+You can cross-compile for PS3 using the PS3 SDK from +a Linux host or a Windows host (requires 32 bit MinGW (GCC) on the host, +too). Due to restrictions on consoles, the JIT compiler is disabled and +only the fast interpreter is built: +

+
+make HOST_CC="gcc -m32" CROSS=ppu-lv2-
+
+

+You can cross-compile for Xbox 360 using the +Xbox 360 SDK (MSVC + XEDK). Due to restrictions on consoles, the +JIT compiler is disabled and only the fast interpreter is built. +

+

+Open a "Visual Studio .NET Command Prompt" (32 bit host compiler), +cd to the directory where you've unpacked the sources and run +the following commands. This builds a static library luajit20.lib, +which can be linked against your game, just like the Lua library. +

+
+cd src
+xedkbuild
+
+ +

Embedding LuaJIT

+

+LuaJIT is API-compatible with Lua 5.1. If you've already embedded Lua +into your application, you probably don't need to do anything to switch +to LuaJIT, except link with a different library: +

+
    +
  • It's strongly suggested to build LuaJIT separately using the supplied +build system. Please do not attempt to integrate the individual +source files into your build tree. You'll most likely get the internal build +dependencies wrong or mess up the compiler flags. Treat LuaJIT like any +other external library and link your application with either the dynamic +or static library, depending on your needs.
  • +
  • If you want to load C modules compiled for plain Lua +with require(), you need to make sure the public symbols +(e.g. lua_pushnumber) are exported, too: +
    • On POSIX systems you can either link to the shared library +or link the static library into your application. In the latter case +you'll need to export all public symbols from your main executable +(e.g. -Wl,-E on Linux) and add the external dependencies +(e.g. -lm -ldl on Linux).
    • +
    • Since Windows symbols are bound to a specific DLL name, you need to +link to the lua51.dll created by the LuaJIT build (do not rename +the DLL). You may link LuaJIT statically on Windows only if you don't +intend to load Lua/C modules at runtime. +
    +
  • +
  • +If you're building a 64 bit application on OSX which links directly or +indirectly against LuaJIT, you need to link your main executable +with these flags: +
    +-pagezero_size 10000 -image_base 100000000
    +
    +Also, it's recommended to rebase all (self-compiled) shared libraries +which are loaded at runtime on OSX/x64 (e.g. C extension modules for Lua). +See: man rebase +
  • +
+

Additional hints for initializing LuaJIT using the C API functions:

+
    +
  • Here's a +» simple example +for embedding Lua or LuaJIT into your application.
  • +
  • Make sure you use luaL_newstate. Avoid using +lua_newstate, since this uses the (slower) default memory +allocator from your system (no support for this on x64).
  • +
  • Make sure you use luaL_openlibs and not the old Lua 5.0 style +of calling luaopen_base etc. directly.
  • +
  • To change or extend the list of standard libraries to load, copy +src/lib_init.c to your project and modify it accordingly. +Make sure the jit library is loaded or the JIT compiler +will not be activated.
  • +
  • The bit.* module for bitwise operations +is already built-in. There's no need to statically link +» Lua BitOp to your application.
  • +
+ +

Hints for Distribution Maintainers

+

+The LuaJIT build system has extra provisions for the needs of most +POSIX-based distributions. If you're a package maintainer for +a distribution, please make use of these features and +avoid patching, subverting, autotoolizing or messing up the build system +in unspeakable ways. +

+

+There should be absolutely no need to patch luaconf.h or any +of the Makefiles. And please do not hand-pick files for your packages — +simply use whatever make install creates. There's a reason +for all of the files and directories it creates. +

+

+The build system uses GNU make and auto-detects most settings based on +the host you're building it on. This should work fine for native builds, +even when sandboxed. You may need to pass some of the following flags to +both the make and the make install command lines +for a regular distribution build: +

+
    +
  • PREFIX overrides the installation path and should usually +be set to /usr. Setting this also changes the module paths and +the -rpath of the shared library.
  • +
  • DESTDIR is an absolute path which allows you to install +to a shadow tree instead of the root tree of the build system.
  • +
  • Have a look at the top-level Makefile and src/Makefile +for additional variables to tweak. The following variables may be +overridden, but it's not recommended, except for special needs +like cross-builds: +BUILDMODE, CC, HOST_CC, STATIC_CC, DYNAMIC_CC, CFLAGS, HOST_CFLAGS, +TARGET_CFLAGS, LDFLAGS, HOST_LDFLAGS, TARGET_LDFLAGS, TARGET_SHLDFLAGS, +TARGET_FLAGS, LIBS, HOST_LIBS, TARGET_LIBS, CROSS, HOST_SYS, TARGET_SYS +
  • +
+

+The build system has a special target for an amalgamated build, i.e. +make amalg. This compiles the LuaJIT core as one huge C file +and allows GCC to generate faster and shorter code. Alas, this requires +lots of memory during the build. This may be a problem for some users, +that's why it's not enabled by default. But it shouldn't be a problem for +most build farms. It's recommended that binary distributions use this +target for their LuaJIT builds. +

+

+The tl;dr version of the above: +

+
+make amalg PREFIX=/usr && \
+make install PREFIX=/usr DESTDIR=/tmp/buildroot
+
+

+Finally, if you encounter any difficulties, please +contact me first, instead of releasing a broken +package onto unsuspecting users. Because they'll usually gonna complain +to me (the upstream) and not you (the package maintainer), anyway. +

+
+
+ + + diff --git a/external/lua/luajit/src/doc/luajit.html b/external/lua/luajit/src/doc/luajit.html new file mode 100644 index 0000000000..e8581d3a03 --- /dev/null +++ b/external/lua/luajit/src/doc/luajit.html @@ -0,0 +1,228 @@ + + + +LuaJIT + + + + + + + + + + +
+Lua +
+ + +
+

+LuaJIT is a Just-In-Time Compiler (JIT) for the +» Lua programming language. +Lua is a powerful, dynamic and light-weight programming language. +It may be embedded or used as a general-purpose, stand-alone language. +

+

+LuaJIT is Copyright © 2005-2013 Mike Pall, released under the +» MIT open source license. +

+

+

+ +

Compatibility

+ + +
WindowsLinuxBSDOSXPOSIX
+ + +
EmbeddedAndroidiOSPS3Xbox 360
+ + +
GCCCLANG
LLVM
MSVC
+ + +
x86x64ARMPPCe500MIPS
+ + +
Lua 5.1
API+ABI
+ JIT+ BitOp+ FFIDrop-in
DLL/.so
+ +

Overview

+ + + + + + + + + +
3x
-  100x
115 KB
VM
90 KB
JIT
63 KLOC
C
24 KLOC
ASM
11 KLOC
Lua
+

+LuaJIT has been successfully used as a scripting middleware in +games, appliances, network and graphics apps, numerical simulations, +trading platforms and many other specialty applications. It scales from +embedded devices, smartphones, desktops up to server farms. It combines +high flexibility with » high performance +and an unmatched low memory footprint. +

+

+LuaJIT has been in continuous development since 2005. It's widely +considered to be one of the fastest dynamic language +implementations. It has outperformed other dynamic languages on many +cross-language benchmarks since its first release — often by a +substantial margin. +

+

+For LuaJIT 2.0, the whole VM has been rewritten from the ground up +and relentlessly optimized for performance. It combines a high-speed +interpreter, written in assembler, with a state-of-the-art JIT +compiler. +

+

+An innovative trace compiler is integrated with advanced, +SSA-based optimizations and highly tuned code generation backends. +A substantial reduction of the overhead associated with dynamic languages +allows it to break into the performance range traditionally reserved for +offline, static language compilers. +

+ +

More ...

+

+Please select a sub-topic in the navigation bar to learn more about LuaJIT. +

+
+
+ + + diff --git a/external/lua/luajit/src/doc/running.html b/external/lua/luajit/src/doc/running.html new file mode 100644 index 0000000000..443021783c --- /dev/null +++ b/external/lua/luajit/src/doc/running.html @@ -0,0 +1,306 @@ + + + +Running LuaJIT + + + + + + + + + +
+Lua +
+ + +
+

+LuaJIT has only a single stand-alone executable, called luajit on +POSIX systems or luajit.exe on Windows. It can be used to run simple +Lua statements or whole Lua applications from the command line. It has an +interactive mode, too. +

+ +

Command Line Options

+

+The luajit stand-alone executable is just a slightly modified +version of the regular lua stand-alone executable. +It supports the same basic options, too. luajit -h +prints a short list of the available options. Please have a look at the +» Lua manual +for details. +

+

+LuaJIT has some additional options: +

+ +

-b[options] input output

+

+This option saves or lists bytecode. The following additional options +are accepted: +

+
    +
  • -l — Only list bytecode.
  • +
  • -s — Strip debug info (this is the default).
  • +
  • -g — Keep debug info.
  • +
  • -n name — Set module name (default: auto-detect from input name)
  • +
  • -t type — Set output file type (default: auto-detect from output name).
  • +
  • -a arch — Override architecture for object files (default: native).
  • +
  • -o os — Override OS for object files (default: native).
  • +
  • -e chunk — Use chunk string as input.
  • +
  • - (a single minus sign) — Use stdin as input and/or stdout as output.
  • +
+

+The output file type is auto-detected from the extension of the output +file name: +

+
    +
  • c — C source file, exported bytecode data.
  • +
  • h — C header file, static bytecode data.
  • +
  • obj or o — Object file, exported bytecode data +(OS- and architecture-specific).
  • +
  • raw or any other extension — Raw bytecode file (portable). +
+

+Notes: +

+
    +
  • See also string.dump() +for information on bytecode portability and compatibility.
  • +
  • A file in raw bytecode format is auto-detected and can be loaded like +any Lua source file. E.g. directly from the command line or with +loadfile(), dofile() etc.
  • +
  • To statically embed the bytecode of a module in your application, +generate an object file and just link it with your application.
  • +
  • On most ELF-based systems (e.g. Linux) you need to explicitly export the +global symbols when linking your application, e.g. with: -Wl,-E
  • +
  • require() tries to load embedded bytecode data from exported +symbols (in *.exe or lua51.dll on Windows) and from +shared libraries in package.cpath.
  • +
+

+Typical usage examples: +

+
+luajit -b test.lua test.out                 # Save bytecode to test.out
+luajit -bg test.lua test.out                # Keep debug info
+luajit -be "print('hello world')" test.out  # Save cmdline script
+
+luajit -bl test.lua                         # List to stdout
+luajit -bl test.lua test.txt                # List to test.txt
+luajit -ble "print('hello world')"          # List cmdline script
+
+luajit -b test.lua test.obj                 # Generate object file
+# Link test.obj with your application and load it with require("test")
+
+ +

-j cmd[=arg[,arg...]]

+

+This option performs a LuaJIT control command or activates one of the +loadable extension modules. The command is first looked up in the +jit.* library. If no matching function is found, a module +named jit.<cmd> is loaded and the start() +function of the module is called with the specified arguments (if +any). The space between -j and cmd is optional. +

+

+Here are the available LuaJIT control commands: +

+
    +
  • -jon — Turns the JIT compiler on (default).
  • +
  • -joff — Turns the JIT compiler off (only use the interpreter).
  • +
  • -jflush — Flushes the whole cache of compiled code.
  • +
  • -jv — Shows verbose information about the progress of the JIT compiler.
  • +
  • -jdump — Dumps the code and structures used in various compiler stages.
  • +
+

+The -jv and -jdump commands are extension modules +written in Lua. They are mainly used for debugging the JIT compiler +itself. For a description of their options and output format, please +read the comment block at the start of their source. +They can be found in the lib directory of the source +distribution or installed under the jit directory. By default +this is /usr/local/share/luajit-2.0.1/jit on POSIX +systems. +

+ +

-O[level]
+-O[+]flag   -O-flag
+-Oparam=value

+

+This options allows fine-tuned control of the optimizations used by +the JIT compiler. This is mainly intended for debugging LuaJIT itself. +Please note that the JIT compiler is extremely fast (we are talking +about the microsecond to millisecond range). Disabling optimizations +doesn't have any visible impact on its overhead, but usually generates +code that runs slower. +

+

+The first form sets an optimization level — this enables a +specific mix of optimization flags. -O0 turns off all +optimizations and higher numbers enable more optimizations. Omitting +the level (i.e. just -O) sets the default optimization level, +which is -O3 in the current version. +

+

+The second form adds or removes individual optimization flags. +The third form sets a parameter for the VM or the JIT compiler +to a specific value. +

+

+You can either use this option multiple times (like -Ocse +-O-dce -Ohotloop=10) or separate several settings with a comma +(like -O+cse,-dce,hotloop=10). The settings are applied from +left to right and later settings override earlier ones. You can freely +mix the three forms, but note that setting an optimization level +overrides all earlier flags. +

+

+Here are the available flags and at what optimization levels they +are enabled: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Flag-O1-O2-O3 
foldConstant Folding, Simplifications and Reassociation
cseCommon-Subexpression Elimination
dceDead-Code Elimination
narrow Narrowing of numbers to integers
loop Loop Optimizations (code hoisting)
fwd  Load Forwarding (L2L) and Store Forwarding (S2L)
dse  Dead-Store Elimination
abc  Array Bounds Check Elimination
sink  Allocation/Store Sinking
fuse  Fusion of operands into instructions
+

+Here are the parameters and their default settings: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ParameterDefault 
maxtrace1000Max. number of traces in the cache
maxrecord4000Max. number of recorded IR instructions
maxirconst500Max. number of IR constants of a trace
maxside100Max. number of side traces of a root trace
maxsnap500Max. number of snapshots for a trace
hotloop56Number of iterations to detect a hot loop or hot call
hotexit10Number of taken exits to start a side trace
tryside4Number of attempts to compile a side trace
instunroll4Max. unroll factor for instable loops
loopunroll15Max. unroll factor for loop ops in side traces
callunroll3Max. unroll factor for pseudo-recursive calls
recunroll2Min. unroll factor for true recursion
sizemcode32Size of each machine code area in KBytes (Windows: 64K)
maxmcode512Max. total size of all machine code areas in KBytes
+
+
+ + + diff --git a/external/lua/luajit/src/doc/status.html b/external/lua/luajit/src/doc/status.html new file mode 100644 index 0000000000..3d148b0a86 --- /dev/null +++ b/external/lua/luajit/src/doc/status.html @@ -0,0 +1,125 @@ + + + +Status & Roadmap + + + + + + + + + +
+Lua +
+ + +
+

+LuaJIT 2.0 is the current +stable branch. This branch is in +feature-freeze — new features will only be added to LuaJIT 2.1. +

+ +

Current Status

+

+LuaJIT ought to run all Lua 5.1-compatible source code just fine. +It's considered a serious bug if the VM crashes or produces unexpected +results — please report this. +

+

+Known incompatibilities and issues in LuaJIT 2.0: +

+
    +
  • +There are some differences in implementation-defined behavior. +These either have a good reason, are arbitrary design choices +or are due to quirks in the VM. The latter cases may get fixed if a +demonstrable need is shown. +
  • +
  • +The Lua debug API is missing a couple of features (return +hooks for non-Lua functions) and shows slightly different behavior +in LuaJIT (no per-coroutine hooks, no tail call counting). +
  • +
  • +Some checks are missing in the JIT-compiled code for obscure situations +with open upvalues aliasing one of the SSA slots later on (or +vice versa). Bonus points, if you can find a real world test case for +this. +
  • +
  • +Currently some out-of-memory errors from on-trace code are not +handled correctly. The error may fall through an on-trace +pcall or it may be passed on to the function set with +lua_atpanic on x64. This issue will be fixed with the new +garbage collector. +
  • +
+ +

Roadmap

+

+Please refer to the +» LuaJIT Roadmap 2012/2013 and an +» update on release planning for details. +

+

+

+
+
+ + + diff --git a/external/lua/luajit/src/dynasm/dasm_arm.h b/external/lua/luajit/src/dynasm/dasm_arm.h new file mode 100644 index 0000000000..691cfdd717 --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_arm.h @@ -0,0 +1,455 @@ +/* +** DynASM ARM encoding engine. +** Copyright (C) 2005-2013 Mike Pall. All rights reserved. +** Released under the MIT license. See dynasm.lua for full copyright notice. +*/ + +#include +#include +#include +#include + +#define DASM_ARCH "arm" + +#ifndef DASM_EXTERN +#define DASM_EXTERN(a,b,c,d) 0 +#endif + +/* Action definitions. */ +enum { + DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, + /* The following actions need a buffer position. */ + DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, + /* The following actions also have an argument. */ + DASM_REL_PC, DASM_LABEL_PC, + DASM_IMM, DASM_IMM12, DASM_IMM16, DASM_IMML8, DASM_IMML12, DASM_IMMV8, + DASM__MAX +}; + +/* Maximum number of section buffer positions for a single dasm_put() call. */ +#define DASM_MAXSECPOS 25 + +/* DynASM encoder status codes. Action list offset or number are or'ed in. */ +#define DASM_S_OK 0x00000000 +#define DASM_S_NOMEM 0x01000000 +#define DASM_S_PHASE 0x02000000 +#define DASM_S_MATCH_SEC 0x03000000 +#define DASM_S_RANGE_I 0x11000000 +#define DASM_S_RANGE_SEC 0x12000000 +#define DASM_S_RANGE_LG 0x13000000 +#define DASM_S_RANGE_PC 0x14000000 +#define DASM_S_RANGE_REL 0x15000000 +#define DASM_S_UNDEF_LG 0x21000000 +#define DASM_S_UNDEF_PC 0x22000000 + +/* Macros to convert positions (8 bit section + 24 bit index). */ +#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) +#define DASM_POS2BIAS(pos) ((pos)&0xff000000) +#define DASM_SEC2POS(sec) ((sec)<<24) +#define DASM_POS2SEC(pos) ((pos)>>24) +#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) + +/* Action list type. */ +typedef const unsigned int *dasm_ActList; + +/* Per-section structure. */ +typedef struct dasm_Section { + int *rbuf; /* Biased buffer pointer (negative section bias). */ + int *buf; /* True buffer pointer. */ + size_t bsize; /* Buffer size in bytes. */ + int pos; /* Biased buffer position. */ + int epos; /* End of biased buffer position - max single put. */ + int ofs; /* Byte offset into section. */ +} dasm_Section; + +/* Core structure holding the DynASM encoding state. */ +struct dasm_State { + size_t psize; /* Allocated size of this structure. */ + dasm_ActList actionlist; /* Current actionlist pointer. */ + int *lglabels; /* Local/global chain/pos ptrs. */ + size_t lgsize; + int *pclabels; /* PC label chains/pos ptrs. */ + size_t pcsize; + void **globals; /* Array of globals (bias -10). */ + dasm_Section *section; /* Pointer to active section. */ + size_t codesize; /* Total size of all code sections. */ + int maxsection; /* 0 <= sectionidx < maxsection. */ + int status; /* Status code. */ + dasm_Section sections[1]; /* All sections. Alloc-extended. */ +}; + +/* The size of the core structure depends on the max. number of sections. */ +#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) + + +/* Initialize DynASM state. */ +void dasm_init(Dst_DECL, int maxsection) +{ + dasm_State *D; + size_t psz = 0; + int i; + Dst_REF = NULL; + DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); + D = Dst_REF; + D->psize = psz; + D->lglabels = NULL; + D->lgsize = 0; + D->pclabels = NULL; + D->pcsize = 0; + D->globals = NULL; + D->maxsection = maxsection; + for (i = 0; i < maxsection; i++) { + D->sections[i].buf = NULL; /* Need this for pass3. */ + D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); + D->sections[i].bsize = 0; + D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ + } +} + +/* Free DynASM state. */ +void dasm_free(Dst_DECL) +{ + dasm_State *D = Dst_REF; + int i; + for (i = 0; i < D->maxsection; i++) + if (D->sections[i].buf) + DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); + if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); + if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); + DASM_M_FREE(Dst, D, D->psize); +} + +/* Setup global label array. Must be called before dasm_setup(). */ +void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) +{ + dasm_State *D = Dst_REF; + D->globals = gl - 10; /* Negative bias to compensate for locals. */ + DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); +} + +/* Grow PC label array. Can be called after dasm_setup(), too. */ +void dasm_growpc(Dst_DECL, unsigned int maxpc) +{ + dasm_State *D = Dst_REF; + size_t osz = D->pcsize; + DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); + memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); +} + +/* Setup encoder. */ +void dasm_setup(Dst_DECL, const void *actionlist) +{ + dasm_State *D = Dst_REF; + int i; + D->actionlist = (dasm_ActList)actionlist; + D->status = DASM_S_OK; + D->section = &D->sections[0]; + memset((void *)D->lglabels, 0, D->lgsize); + if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); + for (i = 0; i < D->maxsection; i++) { + D->sections[i].pos = DASM_SEC2POS(i); + D->sections[i].ofs = 0; + } +} + + +#ifdef DASM_CHECKS +#define CK(x, st) \ + do { if (!(x)) { \ + D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) +#define CKPL(kind, st) \ + do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ + D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) +#else +#define CK(x, st) ((void)0) +#define CKPL(kind, st) ((void)0) +#endif + +static int dasm_imm12(unsigned int n) +{ + int i; + for (i = 0; i < 16; i++, n = (n << 2) | (n >> 30)) + if (n <= 255) return (int)(n + (i << 8)); + return -1; +} + +/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ +void dasm_put(Dst_DECL, int start, ...) +{ + va_list ap; + dasm_State *D = Dst_REF; + dasm_ActList p = D->actionlist + start; + dasm_Section *sec = D->section; + int pos = sec->pos, ofs = sec->ofs; + int *b; + + if (pos >= sec->epos) { + DASM_M_GROW(Dst, int, sec->buf, sec->bsize, + sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); + sec->rbuf = sec->buf - DASM_POS2BIAS(pos); + sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); + } + + b = sec->rbuf; + b[pos++] = start; + + va_start(ap, start); + while (1) { + unsigned int ins = *p++; + unsigned int action = (ins >> 16); + if (action >= DASM__MAX) { + ofs += 4; + } else { + int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; + switch (action) { + case DASM_STOP: goto stop; + case DASM_SECTION: + n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); + D->section = &D->sections[n]; goto stop; + case DASM_ESC: p++; ofs += 4; break; + case DASM_REL_EXT: break; + case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; + case DASM_REL_LG: + n = (ins & 2047) - 10; pl = D->lglabels + n; + if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ + pl += 10; n = *pl; + if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ + goto linkrel; + case DASM_REL_PC: + pl = D->pclabels + n; CKPL(pc, PC); + putrel: + n = *pl; + if (n < 0) { /* Label exists. Get label pos and store it. */ + b[pos] = -n; + } else { + linkrel: + b[pos] = n; /* Else link to rel chain, anchored at label. */ + *pl = pos; + } + pos++; + break; + case DASM_LABEL_LG: + pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; + case DASM_LABEL_PC: + pl = D->pclabels + n; CKPL(pc, PC); + putlabel: + n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ + while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; + } + *pl = -pos; /* Label exists now. */ + b[pos++] = ofs; /* Store pass1 offset estimate. */ + break; + case DASM_IMM: + case DASM_IMM16: +#ifdef DASM_CHECKS + CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); + if ((ins & 0x8000)) + CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); + else + CK((n>>((ins>>5)&31)) == 0, RANGE_I); +#endif + b[pos++] = n; + break; + case DASM_IMMV8: + CK((n & 3) == 0, RANGE_I); + n >>= 2; + case DASM_IMML8: + case DASM_IMML12: + CK(n >= 0 ? ((n>>((ins>>5)&31)) == 0) : + (((-n)>>((ins>>5)&31)) == 0), RANGE_I); + b[pos++] = n; + break; + case DASM_IMM12: + CK(dasm_imm12((unsigned int)n) != -1, RANGE_I); + b[pos++] = n; + break; + } + } + } +stop: + va_end(ap); + sec->pos = pos; + sec->ofs = ofs; +} +#undef CK + +/* Pass 2: Link sections, shrink aligns, fix label offsets. */ +int dasm_link(Dst_DECL, size_t *szp) +{ + dasm_State *D = Dst_REF; + int secnum; + int ofs = 0; + +#ifdef DASM_CHECKS + *szp = 0; + if (D->status != DASM_S_OK) return D->status; + { + int pc; + for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) + if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; + } +#endif + + { /* Handle globals not defined in this translation unit. */ + int idx; + for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { + int n = D->lglabels[idx]; + /* Undefined label: Collapse rel chain and replace with marker (< 0). */ + while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } + } + } + + /* Combine all code sections. No support for data sections (yet). */ + for (secnum = 0; secnum < D->maxsection; secnum++) { + dasm_Section *sec = D->sections + secnum; + int *b = sec->rbuf; + int pos = DASM_SEC2POS(secnum); + int lastpos = sec->pos; + + while (pos != lastpos) { + dasm_ActList p = D->actionlist + b[pos++]; + while (1) { + unsigned int ins = *p++; + unsigned int action = (ins >> 16); + switch (action) { + case DASM_STOP: case DASM_SECTION: goto stop; + case DASM_ESC: p++; break; + case DASM_REL_EXT: break; + case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; + case DASM_REL_LG: case DASM_REL_PC: pos++; break; + case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; + case DASM_IMM: case DASM_IMM12: case DASM_IMM16: + case DASM_IMML8: case DASM_IMML12: case DASM_IMMV8: pos++; break; + } + } + stop: (void)0; + } + ofs += sec->ofs; /* Next section starts right after current section. */ + } + + D->codesize = ofs; /* Total size of all code sections */ + *szp = ofs; + return DASM_S_OK; +} + +#ifdef DASM_CHECKS +#define CK(x, st) \ + do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) +#else +#define CK(x, st) ((void)0) +#endif + +/* Pass 3: Encode sections. */ +int dasm_encode(Dst_DECL, void *buffer) +{ + dasm_State *D = Dst_REF; + char *base = (char *)buffer; + unsigned int *cp = (unsigned int *)buffer; + int secnum; + + /* Encode all code sections. No support for data sections (yet). */ + for (secnum = 0; secnum < D->maxsection; secnum++) { + dasm_Section *sec = D->sections + secnum; + int *b = sec->buf; + int *endb = sec->rbuf + sec->pos; + + while (b != endb) { + dasm_ActList p = D->actionlist + *b++; + while (1) { + unsigned int ins = *p++; + unsigned int action = (ins >> 16); + int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; + switch (action) { + case DASM_STOP: case DASM_SECTION: goto stop; + case DASM_ESC: *cp++ = *p++; break; + case DASM_REL_EXT: + n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048)); + goto patchrel; + case DASM_ALIGN: + ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000; + break; + case DASM_REL_LG: + CK(n >= 0, UNDEF_LG); + case DASM_REL_PC: + CK(n >= 0, UNDEF_PC); + n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) - 4; + patchrel: + if ((ins & 0x800) == 0) { + CK((n & 3) == 0 && ((n+0x02000000) >> 26) == 0, RANGE_REL); + cp[-1] |= ((n >> 2) & 0x00ffffff); + } else if ((ins & 0x1000)) { + CK((n & 3) == 0 && -256 <= n && n <= 256, RANGE_REL); + goto patchimml8; + } else if ((ins & 0x2000) == 0) { + CK((n & 3) == 0 && -4096 <= n && n <= 4096, RANGE_REL); + goto patchimml; + } else { + CK((n & 3) == 0 && -1020 <= n && n <= 1020, RANGE_REL); + n >>= 2; + goto patchimml; + } + break; + case DASM_LABEL_LG: + ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); + break; + case DASM_LABEL_PC: break; + case DASM_IMM: + cp[-1] |= ((n>>((ins>>10)&31)) & ((1<<((ins>>5)&31))-1)) << (ins&31); + break; + case DASM_IMM12: + cp[-1] |= dasm_imm12((unsigned int)n); + break; + case DASM_IMM16: + cp[-1] |= ((n & 0xf000) << 4) | (n & 0x0fff); + break; + case DASM_IMML8: patchimml8: + cp[-1] |= n >= 0 ? (0x00800000 | (n & 0x0f) | ((n & 0xf0) << 4)) : + ((-n & 0x0f) | ((-n & 0xf0) << 4)); + break; + case DASM_IMML12: case DASM_IMMV8: patchimml: + cp[-1] |= n >= 0 ? (0x00800000 | n) : (-n); + break; + default: *cp++ = ins; break; + } + } + stop: (void)0; + } + } + + if (base + D->codesize != (char *)cp) /* Check for phase errors. */ + return DASM_S_PHASE; + return DASM_S_OK; +} +#undef CK + +/* Get PC label offset. */ +int dasm_getpclabel(Dst_DECL, unsigned int pc) +{ + dasm_State *D = Dst_REF; + if (pc*sizeof(int) < D->pcsize) { + int pos = D->pclabels[pc]; + if (pos < 0) return *DASM_POS2PTR(D, -pos); + if (pos > 0) return -1; /* Undefined. */ + } + return -2; /* Unused or out of range. */ +} + +#ifdef DASM_CHECKS +/* Optional sanity checker to call between isolated encoding steps. */ +int dasm_checkstep(Dst_DECL, int secmatch) +{ + dasm_State *D = Dst_REF; + if (D->status == DASM_S_OK) { + int i; + for (i = 1; i <= 9; i++) { + if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } + D->lglabels[i] = 0; + } + } + if (D->status == DASM_S_OK && secmatch >= 0 && + D->section != &D->sections[secmatch]) + D->status = DASM_S_MATCH_SEC|(D->section-D->sections); + return D->status; +} +#endif + diff --git a/external/lua/luajit/src/dynasm/dasm_arm.lua b/external/lua/luajit/src/dynasm/dasm_arm.lua new file mode 100644 index 0000000000..9720e57cd3 --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_arm.lua @@ -0,0 +1,1122 @@ +------------------------------------------------------------------------------ +-- DynASM ARM module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- See dynasm.lua for full copyright notice. +------------------------------------------------------------------------------ + +-- Module information: +local _info = { + arch = "arm", + description = "DynASM ARM module", + version = "1.3.0", + vernum = 10300, + release = "2011-05-05", + author = "Mike Pall", + license = "MIT", +} + +-- Exported glue functions for the arch-specific module. +local _M = { _info = _info } + +-- Cache library functions. +local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs +local assert, setmetatable, rawget = assert, setmetatable, rawget +local _s = string +local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char +local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub +local concat, sort, insert = table.concat, table.sort, table.insert +local bit = bit or require("bit") +local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift +local ror, tohex = bit.ror, bit.tohex + +-- Inherited tables and callbacks. +local g_opt, g_arch +local wline, werror, wfatal, wwarn + +-- Action name list. +-- CHECK: Keep this in sync with the C code! +local action_names = { + "STOP", "SECTION", "ESC", "REL_EXT", + "ALIGN", "REL_LG", "LABEL_LG", + "REL_PC", "LABEL_PC", "IMM", "IMM12", "IMM16", "IMML8", "IMML12", "IMMV8", +} + +-- Maximum number of section buffer positions for dasm_put(). +-- CHECK: Keep this in sync with the C code! +local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. + +-- Action name -> action number. +local map_action = {} +for n,name in ipairs(action_names) do + map_action[name] = n-1 +end + +-- Action list buffer. +local actlist = {} + +-- Argument list for next dasm_put(). Start with offset 0 into action list. +local actargs = { 0 } + +-- Current number of section buffer positions for dasm_put(). +local secpos = 1 + +------------------------------------------------------------------------------ + +-- Dump action names and numbers. +local function dumpactions(out) + out:write("DynASM encoding engine action codes:\n") + for n,name in ipairs(action_names) do + local num = map_action[name] + out:write(format(" %-10s %02X %d\n", name, num, num)) + end + out:write("\n") +end + +-- Write action list buffer as a huge static C array. +local function writeactions(out, name) + local nn = #actlist + if nn == 0 then nn = 1; actlist[0] = map_action.STOP end + out:write("static const unsigned int ", name, "[", nn, "] = {\n") + for i = 1,nn-1 do + assert(out:write("0x", tohex(actlist[i]), ",\n")) + end + assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) +end + +------------------------------------------------------------------------------ + +-- Add word to action list. +local function wputxw(n) + assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") + actlist[#actlist+1] = n +end + +-- Add action to list with optional arg. Advance buffer pos, too. +local function waction(action, val, a, num) + local w = assert(map_action[action], "bad action name `"..action.."'") + wputxw(w * 0x10000 + (val or 0)) + if a then actargs[#actargs+1] = a end + if a or num then secpos = secpos + (num or 1) end +end + +-- Flush action list (intervening C code or buffer pos overflow). +local function wflush(term) + if #actlist == actargs[1] then return end -- Nothing to flush. + if not term then waction("STOP") end -- Terminate action list. + wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) + actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). + secpos = 1 -- The actionlist offset occupies a buffer position, too. +end + +-- Put escaped word. +local function wputw(n) + if n <= 0x000fffff then waction("ESC") end + wputxw(n) +end + +-- Reserve position for word. +local function wpos() + local pos = #actlist+1 + actlist[pos] = "" + return pos +end + +-- Store word to reserved position. +local function wputpos(pos, n) + assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") + if n <= 0x000fffff then + insert(actlist, pos+1, n) + n = map_action.ESC * 0x10000 + end + actlist[pos] = n +end + +------------------------------------------------------------------------------ + +-- Global label name -> global label number. With auto assignment on 1st use. +local next_global = 20 +local map_global = setmetatable({}, { __index = function(t, name) + if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end + local n = next_global + if n > 2047 then werror("too many global labels") end + next_global = n + 1 + t[name] = n + return n +end}) + +-- Dump global labels. +local function dumpglobals(out, lvl) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("Global labels:\n") + for i=20,next_global-1 do + out:write(format(" %s\n", t[i])) + end + out:write("\n") +end + +-- Write global label enum. +local function writeglobals(out, prefix) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("enum {\n") + for i=20,next_global-1 do + out:write(" ", prefix, t[i], ",\n") + end + out:write(" ", prefix, "_MAX\n};\n") +end + +-- Write global label names. +local function writeglobalnames(out, name) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("static const char *const ", name, "[] = {\n") + for i=20,next_global-1 do + out:write(" \"", t[i], "\",\n") + end + out:write(" (const char *)0\n};\n") +end + +------------------------------------------------------------------------------ + +-- Extern label name -> extern label number. With auto assignment on 1st use. +local next_extern = 0 +local map_extern_ = {} +local map_extern = setmetatable({}, { __index = function(t, name) + -- No restrictions on the name for now. + local n = next_extern + if n > 2047 then werror("too many extern labels") end + next_extern = n + 1 + t[name] = n + map_extern_[n] = name + return n +end}) + +-- Dump extern labels. +local function dumpexterns(out, lvl) + out:write("Extern labels:\n") + for i=0,next_extern-1 do + out:write(format(" %s\n", map_extern_[i])) + end + out:write("\n") +end + +-- Write extern label names. +local function writeexternnames(out, name) + out:write("static const char *const ", name, "[] = {\n") + for i=0,next_extern-1 do + out:write(" \"", map_extern_[i], "\",\n") + end + out:write(" (const char *)0\n};\n") +end + +------------------------------------------------------------------------------ + +-- Arch-specific maps. + +-- Ext. register name -> int. name. +local map_archdef = { sp = "r13", lr = "r14", pc = "r15", } + +-- Int. register name -> ext. name. +local map_reg_rev = { r13 = "sp", r14 = "lr", r15 = "pc", } + +local map_type = {} -- Type name -> { ctype, reg } +local ctypenum = 0 -- Type number (for Dt... macros). + +-- Reverse defines for registers. +function _M.revdef(s) + return map_reg_rev[s] or s +end + +local map_shift = { lsl = 0, lsr = 1, asr = 2, ror = 3, } + +local map_cond = { + eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7, + hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14, + hs = 2, lo = 3, +} + +------------------------------------------------------------------------------ + +-- Template strings for ARM instructions. +local map_op = { + -- Basic data processing instructions. + and_3 = "e0000000DNPs", + eor_3 = "e0200000DNPs", + sub_3 = "e0400000DNPs", + rsb_3 = "e0600000DNPs", + add_3 = "e0800000DNPs", + adc_3 = "e0a00000DNPs", + sbc_3 = "e0c00000DNPs", + rsc_3 = "e0e00000DNPs", + tst_2 = "e1100000NP", + teq_2 = "e1300000NP", + cmp_2 = "e1500000NP", + cmn_2 = "e1700000NP", + orr_3 = "e1800000DNPs", + mov_2 = "e1a00000DPs", + bic_3 = "e1c00000DNPs", + mvn_2 = "e1e00000DPs", + + and_4 = "e0000000DNMps", + eor_4 = "e0200000DNMps", + sub_4 = "e0400000DNMps", + rsb_4 = "e0600000DNMps", + add_4 = "e0800000DNMps", + adc_4 = "e0a00000DNMps", + sbc_4 = "e0c00000DNMps", + rsc_4 = "e0e00000DNMps", + tst_3 = "e1100000NMp", + teq_3 = "e1300000NMp", + cmp_3 = "e1500000NMp", + cmn_3 = "e1700000NMp", + orr_4 = "e1800000DNMps", + mov_3 = "e1a00000DMps", + bic_4 = "e1c00000DNMps", + mvn_3 = "e1e00000DMps", + + lsl_3 = "e1a00000DMws", + lsr_3 = "e1a00020DMws", + asr_3 = "e1a00040DMws", + ror_3 = "e1a00060DMws", + rrx_2 = "e1a00060DMs", + + -- Multiply and multiply-accumulate. + mul_3 = "e0000090NMSs", + mla_4 = "e0200090NMSDs", + umaal_4 = "e0400090DNMSs", -- v6 + mls_4 = "e0600090DNMSs", -- v6T2 + umull_4 = "e0800090DNMSs", + umlal_4 = "e0a00090DNMSs", + smull_4 = "e0c00090DNMSs", + smlal_4 = "e0e00090DNMSs", + + -- Halfword multiply and multiply-accumulate. + smlabb_4 = "e1000080NMSD", -- v5TE + smlatb_4 = "e10000a0NMSD", -- v5TE + smlabt_4 = "e10000c0NMSD", -- v5TE + smlatt_4 = "e10000e0NMSD", -- v5TE + smlawb_4 = "e1200080NMSD", -- v5TE + smulwb_3 = "e12000a0NMS", -- v5TE + smlawt_4 = "e12000c0NMSD", -- v5TE + smulwt_3 = "e12000e0NMS", -- v5TE + smlalbb_4 = "e1400080NMSD", -- v5TE + smlaltb_4 = "e14000a0NMSD", -- v5TE + smlalbt_4 = "e14000c0NMSD", -- v5TE + smlaltt_4 = "e14000e0NMSD", -- v5TE + smulbb_3 = "e1600080NMS", -- v5TE + smultb_3 = "e16000a0NMS", -- v5TE + smulbt_3 = "e16000c0NMS", -- v5TE + smultt_3 = "e16000e0NMS", -- v5TE + + -- Miscellaneous data processing instructions. + clz_2 = "e16f0f10DM", -- v5T + rev_2 = "e6bf0f30DM", -- v6 + rev16_2 = "e6bf0fb0DM", -- v6 + revsh_2 = "e6ff0fb0DM", -- v6 + sel_3 = "e6800fb0DNM", -- v6 + usad8_3 = "e780f010NMS", -- v6 + usada8_4 = "e7800010NMSD", -- v6 + rbit_2 = "e6ff0f30DM", -- v6T2 + movw_2 = "e3000000DW", -- v6T2 + movt_2 = "e3400000DW", -- v6T2 + -- Note: the X encodes width-1, not width. + sbfx_4 = "e7a00050DMvX", -- v6T2 + ubfx_4 = "e7e00050DMvX", -- v6T2 + -- Note: the X encodes the msb field, not the width. + bfc_3 = "e7c0001fDvX", -- v6T2 + bfi_4 = "e7c00010DMvX", -- v6T2 + + -- Packing and unpacking instructions. + pkhbt_3 = "e6800010DNM", pkhbt_4 = "e6800010DNMv", -- v6 + pkhtb_3 = "e6800050DNM", pkhtb_4 = "e6800050DNMv", -- v6 + sxtab_3 = "e6a00070DNM", sxtab_4 = "e6a00070DNMv", -- v6 + sxtab16_3 = "e6800070DNM", sxtab16_4 = "e6800070DNMv", -- v6 + sxtah_3 = "e6b00070DNM", sxtah_4 = "e6b00070DNMv", -- v6 + sxtb_2 = "e6af0070DM", sxtb_3 = "e6af0070DMv", -- v6 + sxtb16_2 = "e68f0070DM", sxtb16_3 = "e68f0070DMv", -- v6 + sxth_2 = "e6bf0070DM", sxth_3 = "e6bf0070DMv", -- v6 + uxtab_3 = "e6e00070DNM", uxtab_4 = "e6e00070DNMv", -- v6 + uxtab16_3 = "e6c00070DNM", uxtab16_4 = "e6c00070DNMv", -- v6 + uxtah_3 = "e6f00070DNM", uxtah_4 = "e6f00070DNMv", -- v6 + uxtb_2 = "e6ef0070DM", uxtb_3 = "e6ef0070DMv", -- v6 + uxtb16_2 = "e6cf0070DM", uxtb16_3 = "e6cf0070DMv", -- v6 + uxth_2 = "e6ff0070DM", uxth_3 = "e6ff0070DMv", -- v6 + + -- Saturating instructions. + qadd_3 = "e1000050DMN", -- v5TE + qsub_3 = "e1200050DMN", -- v5TE + qdadd_3 = "e1400050DMN", -- v5TE + qdsub_3 = "e1600050DMN", -- v5TE + -- Note: the X for ssat* encodes sat_imm-1, not sat_imm. + ssat_3 = "e6a00010DXM", ssat_4 = "e6a00010DXMp", -- v6 + usat_3 = "e6e00010DXM", usat_4 = "e6e00010DXMp", -- v6 + ssat16_3 = "e6a00f30DXM", -- v6 + usat16_3 = "e6e00f30DXM", -- v6 + + -- Parallel addition and subtraction. + sadd16_3 = "e6100f10DNM", -- v6 + sasx_3 = "e6100f30DNM", -- v6 + ssax_3 = "e6100f50DNM", -- v6 + ssub16_3 = "e6100f70DNM", -- v6 + sadd8_3 = "e6100f90DNM", -- v6 + ssub8_3 = "e6100ff0DNM", -- v6 + qadd16_3 = "e6200f10DNM", -- v6 + qasx_3 = "e6200f30DNM", -- v6 + qsax_3 = "e6200f50DNM", -- v6 + qsub16_3 = "e6200f70DNM", -- v6 + qadd8_3 = "e6200f90DNM", -- v6 + qsub8_3 = "e6200ff0DNM", -- v6 + shadd16_3 = "e6300f10DNM", -- v6 + shasx_3 = "e6300f30DNM", -- v6 + shsax_3 = "e6300f50DNM", -- v6 + shsub16_3 = "e6300f70DNM", -- v6 + shadd8_3 = "e6300f90DNM", -- v6 + shsub8_3 = "e6300ff0DNM", -- v6 + uadd16_3 = "e6500f10DNM", -- v6 + uasx_3 = "e6500f30DNM", -- v6 + usax_3 = "e6500f50DNM", -- v6 + usub16_3 = "e6500f70DNM", -- v6 + uadd8_3 = "e6500f90DNM", -- v6 + usub8_3 = "e6500ff0DNM", -- v6 + uqadd16_3 = "e6600f10DNM", -- v6 + uqasx_3 = "e6600f30DNM", -- v6 + uqsax_3 = "e6600f50DNM", -- v6 + uqsub16_3 = "e6600f70DNM", -- v6 + uqadd8_3 = "e6600f90DNM", -- v6 + uqsub8_3 = "e6600ff0DNM", -- v6 + uhadd16_3 = "e6700f10DNM", -- v6 + uhasx_3 = "e6700f30DNM", -- v6 + uhsax_3 = "e6700f50DNM", -- v6 + uhsub16_3 = "e6700f70DNM", -- v6 + uhadd8_3 = "e6700f90DNM", -- v6 + uhsub8_3 = "e6700ff0DNM", -- v6 + + -- Load/store instructions. + str_2 = "e4000000DL", str_3 = "e4000000DL", str_4 = "e4000000DL", + strb_2 = "e4400000DL", strb_3 = "e4400000DL", strb_4 = "e4400000DL", + ldr_2 = "e4100000DL", ldr_3 = "e4100000DL", ldr_4 = "e4100000DL", + ldrb_2 = "e4500000DL", ldrb_3 = "e4500000DL", ldrb_4 = "e4500000DL", + strh_2 = "e00000b0DL", strh_3 = "e00000b0DL", + ldrh_2 = "e01000b0DL", ldrh_3 = "e01000b0DL", + ldrd_2 = "e00000d0DL", ldrd_3 = "e00000d0DL", -- v5TE + ldrsb_2 = "e01000d0DL", ldrsb_3 = "e01000d0DL", + strd_2 = "e00000f0DL", strd_3 = "e00000f0DL", -- v5TE + ldrsh_2 = "e01000f0DL", ldrsh_3 = "e01000f0DL", + + ldm_2 = "e8900000oR", ldmia_2 = "e8900000oR", ldmfd_2 = "e8900000oR", + ldmda_2 = "e8100000oR", ldmfa_2 = "e8100000oR", + ldmdb_2 = "e9100000oR", ldmea_2 = "e9100000oR", + ldmib_2 = "e9900000oR", ldmed_2 = "e9900000oR", + stm_2 = "e8800000oR", stmia_2 = "e8800000oR", stmfd_2 = "e8800000oR", + stmda_2 = "e8000000oR", stmfa_2 = "e8000000oR", + stmdb_2 = "e9000000oR", stmea_2 = "e9000000oR", + stmib_2 = "e9800000oR", stmed_2 = "e9800000oR", + pop_1 = "e8bd0000R", push_1 = "e92d0000R", + + -- Branch instructions. + b_1 = "ea000000B", + bl_1 = "eb000000B", + blx_1 = "e12fff30C", + bx_1 = "e12fff10M", + + -- Miscellaneous instructions. + nop_0 = "e1a00000", + mrs_1 = "e10f0000D", + bkpt_1 = "e1200070K", -- v5T + svc_1 = "ef000000T", swi_1 = "ef000000T", + ud_0 = "e7f001f0", + + -- VFP instructions. + ["vadd.f32_3"] = "ee300a00dnm", + ["vadd.f64_3"] = "ee300b00Gdnm", + ["vsub.f32_3"] = "ee300a40dnm", + ["vsub.f64_3"] = "ee300b40Gdnm", + ["vmul.f32_3"] = "ee200a00dnm", + ["vmul.f64_3"] = "ee200b00Gdnm", + ["vnmul.f32_3"] = "ee200a40dnm", + ["vnmul.f64_3"] = "ee200b40Gdnm", + ["vmla.f32_3"] = "ee000a00dnm", + ["vmla.f64_3"] = "ee000b00Gdnm", + ["vmls.f32_3"] = "ee000a40dnm", + ["vmls.f64_3"] = "ee000b40Gdnm", + ["vnmla.f32_3"] = "ee100a40dnm", + ["vnmla.f64_3"] = "ee100b40Gdnm", + ["vnmls.f32_3"] = "ee100a00dnm", + ["vnmls.f64_3"] = "ee100b00Gdnm", + ["vdiv.f32_3"] = "ee800a00dnm", + ["vdiv.f64_3"] = "ee800b00Gdnm", + + ["vabs.f32_2"] = "eeb00ac0dm", + ["vabs.f64_2"] = "eeb00bc0Gdm", + ["vneg.f32_2"] = "eeb10a40dm", + ["vneg.f64_2"] = "eeb10b40Gdm", + ["vsqrt.f32_2"] = "eeb10ac0dm", + ["vsqrt.f64_2"] = "eeb10bc0Gdm", + ["vcmp.f32_2"] = "eeb40a40dm", + ["vcmp.f64_2"] = "eeb40b40Gdm", + ["vcmpe.f32_2"] = "eeb40ac0dm", + ["vcmpe.f64_2"] = "eeb40bc0Gdm", + ["vcmpz.f32_1"] = "eeb50a40d", + ["vcmpz.f64_1"] = "eeb50b40Gd", + ["vcmpze.f32_1"] = "eeb50ac0d", + ["vcmpze.f64_1"] = "eeb50bc0Gd", + + vldr_2 = "ed100a00dl|ed100b00Gdl", + vstr_2 = "ed000a00dl|ed000b00Gdl", + vldm_2 = "ec900a00or", + vldmia_2 = "ec900a00or", + vldmdb_2 = "ed100a00or", + vpop_1 = "ecbd0a00r", + vstm_2 = "ec800a00or", + vstmia_2 = "ec800a00or", + vstmdb_2 = "ed000a00or", + vpush_1 = "ed2d0a00r", + + ["vmov.f32_2"] = "eeb00a40dm|eeb00a00dY", -- #imm is VFPv3 only + ["vmov.f64_2"] = "eeb00b40Gdm|eeb00b00GdY", -- #imm is VFPv3 only + vmov_2 = "ee100a10Dn|ee000a10nD", + vmov_3 = "ec500a10DNm|ec400a10mDN|ec500b10GDNm|ec400b10GmDN", + + vmrs_0 = "eef1fa10", + vmrs_1 = "eef10a10D", + vmsr_1 = "eee10a10D", + + ["vcvt.s32.f32_2"] = "eebd0ac0dm", + ["vcvt.s32.f64_2"] = "eebd0bc0dGm", + ["vcvt.u32.f32_2"] = "eebc0ac0dm", + ["vcvt.u32.f64_2"] = "eebc0bc0dGm", + ["vcvtr.s32.f32_2"] = "eebd0a40dm", + ["vcvtr.s32.f64_2"] = "eebd0b40dGm", + ["vcvtr.u32.f32_2"] = "eebc0a40dm", + ["vcvtr.u32.f64_2"] = "eebc0b40dGm", + ["vcvt.f32.s32_2"] = "eeb80ac0dm", + ["vcvt.f64.s32_2"] = "eeb80bc0GdFm", + ["vcvt.f32.u32_2"] = "eeb80a40dm", + ["vcvt.f64.u32_2"] = "eeb80b40GdFm", + ["vcvt.f32.f64_2"] = "eeb70bc0dGm", + ["vcvt.f64.f32_2"] = "eeb70ac0GdFm", + + -- VFPv4 only: + ["vfma.f32_3"] = "eea00a00dnm", + ["vfma.f64_3"] = "eea00b00Gdnm", + ["vfms.f32_3"] = "eea00a40dnm", + ["vfms.f64_3"] = "eea00b40Gdnm", + ["vfnma.f32_3"] = "ee900a40dnm", + ["vfnma.f64_3"] = "ee900b40Gdnm", + ["vfnms.f32_3"] = "ee900a00dnm", + ["vfnms.f64_3"] = "ee900b00Gdnm", + + -- NYI: Advanced SIMD instructions. + + -- NYI: I have no need for these instructions right now: + -- swp, swpb, strex, ldrex, strexd, ldrexd, strexb, ldrexb, strexh, ldrexh + -- msr, nopv6, yield, wfe, wfi, sev, dbg, bxj, smc, srs, rfe + -- cps, setend, pli, pld, pldw, clrex, dsb, dmb, isb + -- stc, ldc, mcr, mcr2, mrc, mrc2, mcrr, mcrr2, mrrc, mrrc2, cdp, cdp2 +} + +-- Add mnemonics for "s" variants. +do + local t = {} + for k,v in pairs(map_op) do + if sub(v, -1) == "s" then + local v2 = sub(v, 1, 2)..char(byte(v, 3)+1)..sub(v, 4, -2) + t[sub(k, 1, -3).."s"..sub(k, -2)] = v2 + end + end + for k,v in pairs(t) do + map_op[k] = v + end +end + +------------------------------------------------------------------------------ + +local function parse_gpr(expr) + local tname, ovreg = match(expr, "^([%w_]+):(r1?[0-9])$") + local tp = map_type[tname or expr] + if tp then + local reg = ovreg or tp.reg + if not reg then + werror("type `"..(tname or expr).."' needs a register override") + end + expr = reg + end + local r = match(expr, "^r(1?[0-9])$") + if r then + r = tonumber(r) + if r <= 15 then return r, tp end + end + werror("bad register name `"..expr.."'") +end + +local function parse_gpr_pm(expr) + local pm, expr2 = match(expr, "^([+-]?)(.*)$") + return parse_gpr(expr2), (pm == "-") +end + +local function parse_vr(expr, tp) + local t, r = match(expr, "^([sd])([0-9]+)$") + if t == tp then + r = tonumber(r) + if r <= 31 then + if t == "s" then return shr(r, 1), band(r, 1) end + return band(r, 15), shr(r, 4) + end + end + werror("bad register name `"..expr.."'") +end + +local function parse_reglist(reglist) + reglist = match(reglist, "^{%s*([^}]*)}$") + if not reglist then werror("register list expected") end + local rr = 0 + for p in gmatch(reglist..",", "%s*([^,]*),") do + local rbit = shl(1, parse_gpr(gsub(p, "%s+$", ""))) + if band(rr, rbit) ~= 0 then + werror("duplicate register `"..p.."'") + end + rr = rr + rbit + end + return rr +end + +local function parse_vrlist(reglist) + local ta, ra, tb, rb = match(reglist, + "^{%s*([sd])([0-9]+)%s*%-%s*([sd])([0-9]+)%s*}$") + ra, rb = tonumber(ra), tonumber(rb) + if ta and ta == tb and ra and rb and ra <= 31 and rb <= 31 and ra <= rb then + local nr = rb+1 - ra + if ta == "s" then + return shl(shr(ra,1),12)+shl(band(ra,1),22) + nr + else + return shl(band(ra,15),12)+shl(shr(ra,4),22) + nr*2 + 0x100 + end + end + werror("register list expected") +end + +local function parse_imm(imm, bits, shift, scale, signed) + imm = match(imm, "^#(.*)$") + if not imm then werror("expected immediate operand") end + local n = tonumber(imm) + if n then + local m = sar(n, scale) + if shl(m, scale) == n then + if signed then + local s = sar(m, bits-1) + if s == 0 then return shl(m, shift) + elseif s == -1 then return shl(m + shl(1, bits), shift) end + else + if sar(m, bits) == 0 then return shl(m, shift) end + end + end + werror("out of range immediate `"..imm.."'") + else + waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) + return 0 + end +end + +local function parse_imm12(imm) + local n = tonumber(imm) + if n then + local m = band(n) + for i=0,-15,-1 do + if shr(m, 8) == 0 then return m + shl(band(i, 15), 8) end + m = ror(m, 2) + end + werror("out of range immediate `"..imm.."'") + else + waction("IMM12", 0, imm) + return 0 + end +end + +local function parse_imm16(imm) + imm = match(imm, "^#(.*)$") + if not imm then werror("expected immediate operand") end + local n = tonumber(imm) + if n then + if shr(n, 16) == 0 then return band(n, 0x0fff) + shl(band(n, 0xf000), 4) end + werror("out of range immediate `"..imm.."'") + else + waction("IMM16", 32*16, imm) + return 0 + end +end + +local function parse_imm_load(imm, ext) + local n = tonumber(imm) + if n then + if ext then + if n >= -255 and n <= 255 then + local up = 0x00800000 + if n < 0 then n = -n; up = 0 end + return shl(band(n, 0xf0), 4) + band(n, 0x0f) + up + end + else + if n >= -4095 and n <= 4095 then + if n >= 0 then return n+0x00800000 end + return -n + end + end + werror("out of range immediate `"..imm.."'") + else + waction(ext and "IMML8" or "IMML12", 32768 + shl(ext and 8 or 12, 5), imm) + return 0 + end +end + +local function parse_shift(shift, gprok) + if shift == "rrx" then + return 3 * 32 + else + local s, s2 = match(shift, "^(%S+)%s*(.*)$") + s = map_shift[s] + if not s then werror("expected shift operand") end + if sub(s2, 1, 1) == "#" then + return parse_imm(s2, 5, 7, 0, false) + shl(s, 5) + else + if not gprok then werror("expected immediate shift operand") end + return shl(parse_gpr(s2), 8) + shl(s, 5) + 16 + end + end +end + +local function parse_label(label, def) + local prefix = sub(label, 1, 2) + -- =>label (pc label reference) + if prefix == "=>" then + return "PC", 0, sub(label, 3) + end + -- ->name (global label reference) + if prefix == "->" then + return "LG", map_global[sub(label, 3)] + end + if def then + -- [1-9] (local label definition) + if match(label, "^[1-9]$") then + return "LG", 10+tonumber(label) + end + else + -- [<>][1-9] (local label reference) + local dir, lnum = match(label, "^([<>])([1-9])$") + if dir then -- Fwd: 1-9, Bkwd: 11-19. + return "LG", lnum + (dir == ">" and 0 or 10) + end + -- extern label (extern label reference) + local extname = match(label, "^extern%s+(%S+)$") + if extname then + return "EXT", map_extern[extname] + end + end + werror("bad label `"..label.."'") +end + +local function parse_load(params, nparams, n, op) + local oplo = band(op, 255) + local ext, ldrd = (oplo ~= 0), (oplo == 208) + local d + if (ldrd or oplo == 240) then + d = band(shr(op, 12), 15) + if band(d, 1) ~= 0 then werror("odd destination register") end + end + local pn = params[n] + local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$") + local p2 = params[n+1] + if not p1 then + if not p2 then + if match(pn, "^[<>=%-]") or match(pn, "^extern%s+") then + local mode, n, s = parse_label(pn, false) + waction("REL_"..mode, n + (ext and 0x1800 or 0x0800), s, 1) + return op + 15 * 65536 + 0x01000000 + (ext and 0x00400000 or 0) + end + local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$") + if reg and tailr ~= "" then + local d, tp = parse_gpr(reg) + if tp then + waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12), + format(tp.ctypefmt, tailr)) + return op + shl(d, 16) + 0x01000000 + (ext and 0x00400000 or 0) + end + end + end + werror("expected address operand") + end + if wb == "!" then op = op + 0x00200000 end + if p2 then + if wb == "!" then werror("bad use of '!'") end + local p3 = params[n+2] + op = op + shl(parse_gpr(p1), 16) + local imm = match(p2, "^#(.*)$") + if imm then + local m = parse_imm_load(imm, ext) + if p3 then werror("too many parameters") end + op = op + m + (ext and 0x00400000 or 0) + else + local m, neg = parse_gpr_pm(p2) + if ldrd and (m == d or m-1 == d) then werror("register conflict") end + op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) + if p3 then op = op + parse_shift(p3) end + end + else + local p1a, p2 = match(p1, "^([^,%s]*)%s*(.*)$") + op = op + shl(parse_gpr(p1a), 16) + 0x01000000 + if p2 ~= "" then + local imm = match(p2, "^,%s*#(.*)$") + if imm then + local m = parse_imm_load(imm, ext) + op = op + m + (ext and 0x00400000 or 0) + else + local p2a, p3 = match(p2, "^,%s*([^,%s]*)%s*,?%s*(.*)$") + local m, neg = parse_gpr_pm(p2a) + if ldrd and (m == d or m-1 == d) then werror("register conflict") end + op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) + if p3 ~= "" then + if ext then werror("too many parameters") end + op = op + parse_shift(p3) + end + end + else + if wb == "!" then werror("bad use of '!'") end + op = op + (ext and 0x00c00000 or 0x00800000) + end + end + return op +end + +local function parse_vload(q) + local reg, imm = match(q, "^%[%s*([^,%s]*)%s*(.*)%]$") + if reg then + local d = shl(parse_gpr(reg), 16) + if imm == "" then return d end + imm = match(imm, "^,%s*#(.*)$") + if imm then + local n = tonumber(imm) + if n then + if n >= -1020 and n <= 1020 and n%4 == 0 then + return d + (n >= 0 and n/4+0x00800000 or -n/4) + end + werror("out of range immediate `"..imm.."'") + else + waction("IMMV8", 32768 + 32*8, imm) + return d + end + end + else + if match(q, "^[<>=%-]") or match(q, "^extern%s+") then + local mode, n, s = parse_label(q, false) + waction("REL_"..mode, n + 0x2800, s, 1) + return 15 * 65536 + end + local reg, tailr = match(q, "^([%w_:]+)%s*(.*)$") + if reg and tailr ~= "" then + local d, tp = parse_gpr(reg) + if tp then + waction("IMMV8", 32768 + 32*8, format(tp.ctypefmt, tailr)) + return shl(d, 16) + end + end + end + werror("expected address operand") +end + +------------------------------------------------------------------------------ + +-- Handle opcodes defined with template strings. +local function parse_template(params, template, nparams, pos) + local op = tonumber(sub(template, 1, 8), 16) + local n = 1 + local vr = "s" + + -- Process each character. + for p in gmatch(sub(template, 9), ".") do + local q = params[n] + if p == "D" then + op = op + shl(parse_gpr(q), 12); n = n + 1 + elseif p == "N" then + op = op + shl(parse_gpr(q), 16); n = n + 1 + elseif p == "S" then + op = op + shl(parse_gpr(q), 8); n = n + 1 + elseif p == "M" then + op = op + parse_gpr(q); n = n + 1 + elseif p == "d" then + local r,h = parse_vr(q, vr); op = op+shl(r,12)+shl(h,22); n = n + 1 + elseif p == "n" then + local r,h = parse_vr(q, vr); op = op+shl(r,16)+shl(h,7); n = n + 1 + elseif p == "m" then + local r,h = parse_vr(q, vr); op = op+r+shl(h,5); n = n + 1 + elseif p == "P" then + local imm = match(q, "^#(.*)$") + if imm then + op = op + parse_imm12(imm) + 0x02000000 + else + op = op + parse_gpr(q) + end + n = n + 1 + elseif p == "p" then + op = op + parse_shift(q, true); n = n + 1 + elseif p == "L" then + op = parse_load(params, nparams, n, op) + elseif p == "l" then + op = op + parse_vload(q) + elseif p == "B" then + local mode, n, s = parse_label(q, false) + waction("REL_"..mode, n, s, 1) + elseif p == "C" then -- blx gpr vs. blx label. + if match(q, "^([%w_]+):(r1?[0-9])$") or match(q, "^r(1?[0-9])$") then + op = op + parse_gpr(q) + else + if op < 0xe0000000 then werror("unconditional instruction") end + local mode, n, s = parse_label(q, false) + waction("REL_"..mode, n, s, 1) + op = 0xfa000000 + end + elseif p == "F" then + vr = "s" + elseif p == "G" then + vr = "d" + elseif p == "o" then + local r, wb = match(q, "^([^!]*)(!?)$") + op = op + shl(parse_gpr(r), 16) + (wb == "!" and 0x00200000 or 0) + n = n + 1 + elseif p == "R" then + op = op + parse_reglist(q); n = n + 1 + elseif p == "r" then + op = op + parse_vrlist(q); n = n + 1 + elseif p == "W" then + op = op + parse_imm16(q); n = n + 1 + elseif p == "v" then + op = op + parse_imm(q, 5, 7, 0, false); n = n + 1 + elseif p == "w" then + local imm = match(q, "^#(.*)$") + if imm then + op = op + parse_imm(q, 5, 7, 0, false); n = n + 1 + else + op = op + shl(parse_gpr(q), 8) + 16 + end + elseif p == "X" then + op = op + parse_imm(q, 5, 16, 0, false); n = n + 1 + elseif p == "Y" then + local imm = tonumber(match(q, "^#(.*)$")); n = n + 1 + if not imm or shr(imm, 8) ~= 0 then + werror("bad immediate operand") + end + op = op + shl(band(imm, 0xf0), 12) + band(imm, 0x0f) + elseif p == "K" then + local imm = tonumber(match(q, "^#(.*)$")); n = n + 1 + if not imm or shr(imm, 16) ~= 0 then + werror("bad immediate operand") + end + op = op + shl(band(imm, 0xfff0), 4) + band(imm, 0x000f) + elseif p == "T" then + op = op + parse_imm(q, 24, 0, 0, false); n = n + 1 + elseif p == "s" then + -- Ignored. + else + assert(false) + end + end + wputpos(pos, op) +end + +map_op[".template__"] = function(params, template, nparams) + if not params then return sub(template, 9) end + + -- Limit number of section buffer positions used by a single dasm_put(). + -- A single opcode needs a maximum of 3 positions. + if secpos+3 > maxsecpos then wflush() end + local pos = wpos() + local apos, spos = #actargs, secpos + + local ok, err + for t in gmatch(template, "[^|]+") do + ok, err = pcall(parse_template, params, t, nparams, pos) + if ok then return end + secpos = spos + actargs[apos+1] = nil + actargs[apos+2] = nil + actargs[apos+3] = nil + end + error(err, 0) +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcode to mark the position where the action list is to be emitted. +map_op[".actionlist_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeactions(out, name) end) +end + +-- Pseudo-opcode to mark the position where the global enum is to be emitted. +map_op[".globals_1"] = function(params) + if not params then return "prefix" end + local prefix = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeglobals(out, prefix) end) +end + +-- Pseudo-opcode to mark the position where the global names are to be emitted. +map_op[".globalnames_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeglobalnames(out, name) end) +end + +-- Pseudo-opcode to mark the position where the extern names are to be emitted. +map_op[".externnames_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeexternnames(out, name) end) +end + +------------------------------------------------------------------------------ + +-- Label pseudo-opcode (converted from trailing colon form). +map_op[".label_1"] = function(params) + if not params then return "[1-9] | ->global | =>pcexpr" end + if secpos+1 > maxsecpos then wflush() end + local mode, n, s = parse_label(params[1], true) + if mode == "EXT" then werror("bad label definition") end + waction("LABEL_"..mode, n, s, 1) +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcodes for data storage. +map_op[".long_*"] = function(params) + if not params then return "imm..." end + for _,p in ipairs(params) do + local n = tonumber(p) + if not n then werror("bad immediate `"..p.."'") end + if n < 0 then n = n + 2^32 end + wputw(n) + if secpos+2 > maxsecpos then wflush() end + end +end + +-- Alignment pseudo-opcode. +map_op[".align_1"] = function(params) + if not params then return "numpow2" end + if secpos+1 > maxsecpos then wflush() end + local align = tonumber(params[1]) + if align then + local x = align + -- Must be a power of 2 in the range (2 ... 256). + for i=1,8 do + x = x / 2 + if x == 1 then + waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. + return + end + end + end + werror("bad alignment") +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcode for (primitive) type definitions (map to C types). +map_op[".type_3"] = function(params, nparams) + if not params then + return nparams == 2 and "name, ctype" or "name, ctype, reg" + end + local name, ctype, reg = params[1], params[2], params[3] + if not match(name, "^[%a_][%w_]*$") then + werror("bad type name `"..name.."'") + end + local tp = map_type[name] + if tp then + werror("duplicate type `"..name.."'") + end + -- Add #type to defines. A bit unclean to put it in map_archdef. + map_archdef["#"..name] = "sizeof("..ctype..")" + -- Add new type and emit shortcut define. + local num = ctypenum + 1 + map_type[name] = { + ctype = ctype, + ctypefmt = format("Dt%X(%%s)", num), + reg = reg, + } + wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) + ctypenum = num +end +map_op[".type_2"] = map_op[".type_3"] + +-- Dump type definitions. +local function dumptypes(out, lvl) + local t = {} + for name in pairs(map_type) do t[#t+1] = name end + sort(t) + out:write("Type definitions:\n") + for _,name in ipairs(t) do + local tp = map_type[name] + local reg = tp.reg or "" + out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) + end + out:write("\n") +end + +------------------------------------------------------------------------------ + +-- Set the current section. +function _M.section(num) + waction("SECTION", num) + wflush(true) -- SECTION is a terminal action. +end + +------------------------------------------------------------------------------ + +-- Dump architecture description. +function _M.dumparch(out) + out:write(format("DynASM %s version %s, released %s\n\n", + _info.arch, _info.version, _info.release)) + dumpactions(out) +end + +-- Dump all user defined elements. +function _M.dumpdef(out, lvl) + dumptypes(out, lvl) + dumpglobals(out, lvl) + dumpexterns(out, lvl) +end + +------------------------------------------------------------------------------ + +-- Pass callbacks from/to the DynASM core. +function _M.passcb(wl, we, wf, ww) + wline, werror, wfatal, wwarn = wl, we, wf, ww + return wflush +end + +-- Setup the arch-specific module. +function _M.setup(arch, opt) + g_arch, g_opt = arch, opt +end + +-- Merge the core maps and the arch-specific maps. +function _M.mergemaps(map_coreop, map_def) + setmetatable(map_op, { __index = function(t, k) + local v = map_coreop[k] + if v then return v end + local k1, cc, k2 = match(k, "^(.-)(..)([._].*)$") + local cv = map_cond[cc] + if cv then + local v = rawget(t, k1..k2) + if type(v) == "string" then + local scv = format("%x", cv) + return gsub(scv..sub(v, 2), "|e", "|"..scv) + end + end + end }) + setmetatable(map_def, { __index = map_archdef }) + return map_op, map_def +end + +return _M + +------------------------------------------------------------------------------ + diff --git a/external/lua/luajit/src/dynasm/dasm_mips.h b/external/lua/luajit/src/dynasm/dasm_mips.h new file mode 100644 index 0000000000..77a8eb5254 --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_mips.h @@ -0,0 +1,415 @@ +/* +** DynASM MIPS encoding engine. +** Copyright (C) 2005-2013 Mike Pall. All rights reserved. +** Released under the MIT license. See dynasm.lua for full copyright notice. +*/ + +#include +#include +#include +#include + +#define DASM_ARCH "mips" + +#ifndef DASM_EXTERN +#define DASM_EXTERN(a,b,c,d) 0 +#endif + +/* Action definitions. */ +enum { + DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, + /* The following actions need a buffer position. */ + DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, + /* The following actions also have an argument. */ + DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, + DASM__MAX +}; + +/* Maximum number of section buffer positions for a single dasm_put() call. */ +#define DASM_MAXSECPOS 25 + +/* DynASM encoder status codes. Action list offset or number are or'ed in. */ +#define DASM_S_OK 0x00000000 +#define DASM_S_NOMEM 0x01000000 +#define DASM_S_PHASE 0x02000000 +#define DASM_S_MATCH_SEC 0x03000000 +#define DASM_S_RANGE_I 0x11000000 +#define DASM_S_RANGE_SEC 0x12000000 +#define DASM_S_RANGE_LG 0x13000000 +#define DASM_S_RANGE_PC 0x14000000 +#define DASM_S_RANGE_REL 0x15000000 +#define DASM_S_UNDEF_LG 0x21000000 +#define DASM_S_UNDEF_PC 0x22000000 + +/* Macros to convert positions (8 bit section + 24 bit index). */ +#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) +#define DASM_POS2BIAS(pos) ((pos)&0xff000000) +#define DASM_SEC2POS(sec) ((sec)<<24) +#define DASM_POS2SEC(pos) ((pos)>>24) +#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) + +/* Action list type. */ +typedef const unsigned int *dasm_ActList; + +/* Per-section structure. */ +typedef struct dasm_Section { + int *rbuf; /* Biased buffer pointer (negative section bias). */ + int *buf; /* True buffer pointer. */ + size_t bsize; /* Buffer size in bytes. */ + int pos; /* Biased buffer position. */ + int epos; /* End of biased buffer position - max single put. */ + int ofs; /* Byte offset into section. */ +} dasm_Section; + +/* Core structure holding the DynASM encoding state. */ +struct dasm_State { + size_t psize; /* Allocated size of this structure. */ + dasm_ActList actionlist; /* Current actionlist pointer. */ + int *lglabels; /* Local/global chain/pos ptrs. */ + size_t lgsize; + int *pclabels; /* PC label chains/pos ptrs. */ + size_t pcsize; + void **globals; /* Array of globals (bias -10). */ + dasm_Section *section; /* Pointer to active section. */ + size_t codesize; /* Total size of all code sections. */ + int maxsection; /* 0 <= sectionidx < maxsection. */ + int status; /* Status code. */ + dasm_Section sections[1]; /* All sections. Alloc-extended. */ +}; + +/* The size of the core structure depends on the max. number of sections. */ +#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) + + +/* Initialize DynASM state. */ +void dasm_init(Dst_DECL, int maxsection) +{ + dasm_State *D; + size_t psz = 0; + int i; + Dst_REF = NULL; + DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); + D = Dst_REF; + D->psize = psz; + D->lglabels = NULL; + D->lgsize = 0; + D->pclabels = NULL; + D->pcsize = 0; + D->globals = NULL; + D->maxsection = maxsection; + for (i = 0; i < maxsection; i++) { + D->sections[i].buf = NULL; /* Need this for pass3. */ + D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); + D->sections[i].bsize = 0; + D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ + } +} + +/* Free DynASM state. */ +void dasm_free(Dst_DECL) +{ + dasm_State *D = Dst_REF; + int i; + for (i = 0; i < D->maxsection; i++) + if (D->sections[i].buf) + DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); + if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); + if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); + DASM_M_FREE(Dst, D, D->psize); +} + +/* Setup global label array. Must be called before dasm_setup(). */ +void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) +{ + dasm_State *D = Dst_REF; + D->globals = gl - 10; /* Negative bias to compensate for locals. */ + DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); +} + +/* Grow PC label array. Can be called after dasm_setup(), too. */ +void dasm_growpc(Dst_DECL, unsigned int maxpc) +{ + dasm_State *D = Dst_REF; + size_t osz = D->pcsize; + DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); + memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); +} + +/* Setup encoder. */ +void dasm_setup(Dst_DECL, const void *actionlist) +{ + dasm_State *D = Dst_REF; + int i; + D->actionlist = (dasm_ActList)actionlist; + D->status = DASM_S_OK; + D->section = &D->sections[0]; + memset((void *)D->lglabels, 0, D->lgsize); + if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); + for (i = 0; i < D->maxsection; i++) { + D->sections[i].pos = DASM_SEC2POS(i); + D->sections[i].ofs = 0; + } +} + + +#ifdef DASM_CHECKS +#define CK(x, st) \ + do { if (!(x)) { \ + D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) +#define CKPL(kind, st) \ + do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ + D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) +#else +#define CK(x, st) ((void)0) +#define CKPL(kind, st) ((void)0) +#endif + +/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ +void dasm_put(Dst_DECL, int start, ...) +{ + va_list ap; + dasm_State *D = Dst_REF; + dasm_ActList p = D->actionlist + start; + dasm_Section *sec = D->section; + int pos = sec->pos, ofs = sec->ofs; + int *b; + + if (pos >= sec->epos) { + DASM_M_GROW(Dst, int, sec->buf, sec->bsize, + sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); + sec->rbuf = sec->buf - DASM_POS2BIAS(pos); + sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); + } + + b = sec->rbuf; + b[pos++] = start; + + va_start(ap, start); + while (1) { + unsigned int ins = *p++; + unsigned int action = (ins >> 16) - 0xff00; + if (action >= DASM__MAX) { + ofs += 4; + } else { + int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; + switch (action) { + case DASM_STOP: goto stop; + case DASM_SECTION: + n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); + D->section = &D->sections[n]; goto stop; + case DASM_ESC: p++; ofs += 4; break; + case DASM_REL_EXT: break; + case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; + case DASM_REL_LG: + n = (ins & 2047) - 10; pl = D->lglabels + n; + if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ + pl += 10; n = *pl; + if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ + goto linkrel; + case DASM_REL_PC: + pl = D->pclabels + n; CKPL(pc, PC); + putrel: + n = *pl; + if (n < 0) { /* Label exists. Get label pos and store it. */ + b[pos] = -n; + } else { + linkrel: + b[pos] = n; /* Else link to rel chain, anchored at label. */ + *pl = pos; + } + pos++; + break; + case DASM_LABEL_LG: + pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; + case DASM_LABEL_PC: + pl = D->pclabels + n; CKPL(pc, PC); + putlabel: + n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ + while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; + } + *pl = -pos; /* Label exists now. */ + b[pos++] = ofs; /* Store pass1 offset estimate. */ + break; + case DASM_IMM: +#ifdef DASM_CHECKS + CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); +#endif + n >>= ((ins>>10)&31); +#ifdef DASM_CHECKS + if (ins & 0x8000) + CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); + else + CK((n>>((ins>>5)&31)) == 0, RANGE_I); +#endif + b[pos++] = n; + break; + } + } + } +stop: + va_end(ap); + sec->pos = pos; + sec->ofs = ofs; +} +#undef CK + +/* Pass 2: Link sections, shrink aligns, fix label offsets. */ +int dasm_link(Dst_DECL, size_t *szp) +{ + dasm_State *D = Dst_REF; + int secnum; + int ofs = 0; + +#ifdef DASM_CHECKS + *szp = 0; + if (D->status != DASM_S_OK) return D->status; + { + int pc; + for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) + if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; + } +#endif + + { /* Handle globals not defined in this translation unit. */ + int idx; + for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { + int n = D->lglabels[idx]; + /* Undefined label: Collapse rel chain and replace with marker (< 0). */ + while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } + } + } + + /* Combine all code sections. No support for data sections (yet). */ + for (secnum = 0; secnum < D->maxsection; secnum++) { + dasm_Section *sec = D->sections + secnum; + int *b = sec->rbuf; + int pos = DASM_SEC2POS(secnum); + int lastpos = sec->pos; + + while (pos != lastpos) { + dasm_ActList p = D->actionlist + b[pos++]; + while (1) { + unsigned int ins = *p++; + unsigned int action = (ins >> 16) - 0xff00; + switch (action) { + case DASM_STOP: case DASM_SECTION: goto stop; + case DASM_ESC: p++; break; + case DASM_REL_EXT: break; + case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; + case DASM_REL_LG: case DASM_REL_PC: pos++; break; + case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; + case DASM_IMM: pos++; break; + } + } + stop: (void)0; + } + ofs += sec->ofs; /* Next section starts right after current section. */ + } + + D->codesize = ofs; /* Total size of all code sections */ + *szp = ofs; + return DASM_S_OK; +} + +#ifdef DASM_CHECKS +#define CK(x, st) \ + do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) +#else +#define CK(x, st) ((void)0) +#endif + +/* Pass 3: Encode sections. */ +int dasm_encode(Dst_DECL, void *buffer) +{ + dasm_State *D = Dst_REF; + char *base = (char *)buffer; + unsigned int *cp = (unsigned int *)buffer; + int secnum; + + /* Encode all code sections. No support for data sections (yet). */ + for (secnum = 0; secnum < D->maxsection; secnum++) { + dasm_Section *sec = D->sections + secnum; + int *b = sec->buf; + int *endb = sec->rbuf + sec->pos; + + while (b != endb) { + dasm_ActList p = D->actionlist + *b++; + while (1) { + unsigned int ins = *p++; + unsigned int action = (ins >> 16) - 0xff00; + int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; + switch (action) { + case DASM_STOP: case DASM_SECTION: goto stop; + case DASM_ESC: *cp++ = *p++; break; + case DASM_REL_EXT: + n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1); + goto patchrel; + case DASM_ALIGN: + ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; + break; + case DASM_REL_LG: + CK(n >= 0, UNDEF_LG); + case DASM_REL_PC: + CK(n >= 0, UNDEF_PC); + n = *DASM_POS2PTR(D, n); + if (ins & 2048) + n = n - (int)((char *)cp - base); + else + n = (n + (int)base) & 0x0fffffff; + patchrel: + CK((n & 3) == 0 && + ((n + ((ins & 2048) ? 0x00020000 : 0)) >> + ((ins & 2048) ? 18 : 28)) == 0, RANGE_REL); + cp[-1] |= ((n>>2) & ((ins & 2048) ? 0x0000ffff: 0x03ffffff)); + break; + case DASM_LABEL_LG: + ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); + break; + case DASM_LABEL_PC: break; + case DASM_IMM: + cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); + break; + default: *cp++ = ins; break; + } + } + stop: (void)0; + } + } + + if (base + D->codesize != (char *)cp) /* Check for phase errors. */ + return DASM_S_PHASE; + return DASM_S_OK; +} +#undef CK + +/* Get PC label offset. */ +int dasm_getpclabel(Dst_DECL, unsigned int pc) +{ + dasm_State *D = Dst_REF; + if (pc*sizeof(int) < D->pcsize) { + int pos = D->pclabels[pc]; + if (pos < 0) return *DASM_POS2PTR(D, -pos); + if (pos > 0) return -1; /* Undefined. */ + } + return -2; /* Unused or out of range. */ +} + +#ifdef DASM_CHECKS +/* Optional sanity checker to call between isolated encoding steps. */ +int dasm_checkstep(Dst_DECL, int secmatch) +{ + dasm_State *D = Dst_REF; + if (D->status == DASM_S_OK) { + int i; + for (i = 1; i <= 9; i++) { + if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } + D->lglabels[i] = 0; + } + } + if (D->status == DASM_S_OK && secmatch >= 0 && + D->section != &D->sections[secmatch]) + D->status = DASM_S_MATCH_SEC|(D->section-D->sections); + return D->status; +} +#endif + diff --git a/external/lua/luajit/src/dynasm/dasm_mips.lua b/external/lua/luajit/src/dynasm/dasm_mips.lua new file mode 100644 index 0000000000..02ab9d5fe7 --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_mips.lua @@ -0,0 +1,953 @@ +------------------------------------------------------------------------------ +-- DynASM MIPS module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- See dynasm.lua for full copyright notice. +------------------------------------------------------------------------------ + +-- Module information: +local _info = { + arch = "mips", + description = "DynASM MIPS module", + version = "1.3.0", + vernum = 10300, + release = "2012-01-23", + author = "Mike Pall", + license = "MIT", +} + +-- Exported glue functions for the arch-specific module. +local _M = { _info = _info } + +-- Cache library functions. +local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs +local assert, setmetatable = assert, setmetatable +local _s = string +local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char +local match, gmatch = _s.match, _s.gmatch +local concat, sort = table.concat, table.sort +local bit = bit or require("bit") +local band, shl, sar, tohex = bit.band, bit.lshift, bit.arshift, bit.tohex + +-- Inherited tables and callbacks. +local g_opt, g_arch +local wline, werror, wfatal, wwarn + +-- Action name list. +-- CHECK: Keep this in sync with the C code! +local action_names = { + "STOP", "SECTION", "ESC", "REL_EXT", + "ALIGN", "REL_LG", "LABEL_LG", + "REL_PC", "LABEL_PC", "IMM", +} + +-- Maximum number of section buffer positions for dasm_put(). +-- CHECK: Keep this in sync with the C code! +local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. + +-- Action name -> action number. +local map_action = {} +for n,name in ipairs(action_names) do + map_action[name] = n-1 +end + +-- Action list buffer. +local actlist = {} + +-- Argument list for next dasm_put(). Start with offset 0 into action list. +local actargs = { 0 } + +-- Current number of section buffer positions for dasm_put(). +local secpos = 1 + +------------------------------------------------------------------------------ + +-- Dump action names and numbers. +local function dumpactions(out) + out:write("DynASM encoding engine action codes:\n") + for n,name in ipairs(action_names) do + local num = map_action[name] + out:write(format(" %-10s %02X %d\n", name, num, num)) + end + out:write("\n") +end + +-- Write action list buffer as a huge static C array. +local function writeactions(out, name) + local nn = #actlist + if nn == 0 then nn = 1; actlist[0] = map_action.STOP end + out:write("static const unsigned int ", name, "[", nn, "] = {\n") + for i = 1,nn-1 do + assert(out:write("0x", tohex(actlist[i]), ",\n")) + end + assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) +end + +------------------------------------------------------------------------------ + +-- Add word to action list. +local function wputxw(n) + assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") + actlist[#actlist+1] = n +end + +-- Add action to list with optional arg. Advance buffer pos, too. +local function waction(action, val, a, num) + local w = assert(map_action[action], "bad action name `"..action.."'") + wputxw(0xff000000 + w * 0x10000 + (val or 0)) + if a then actargs[#actargs+1] = a end + if a or num then secpos = secpos + (num or 1) end +end + +-- Flush action list (intervening C code or buffer pos overflow). +local function wflush(term) + if #actlist == actargs[1] then return end -- Nothing to flush. + if not term then waction("STOP") end -- Terminate action list. + wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) + actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). + secpos = 1 -- The actionlist offset occupies a buffer position, too. +end + +-- Put escaped word. +local function wputw(n) + if n >= 0xff000000 then waction("ESC") end + wputxw(n) +end + +-- Reserve position for word. +local function wpos() + local pos = #actlist+1 + actlist[pos] = "" + return pos +end + +-- Store word to reserved position. +local function wputpos(pos, n) + assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") + actlist[pos] = n +end + +------------------------------------------------------------------------------ + +-- Global label name -> global label number. With auto assignment on 1st use. +local next_global = 20 +local map_global = setmetatable({}, { __index = function(t, name) + if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end + local n = next_global + if n > 2047 then werror("too many global labels") end + next_global = n + 1 + t[name] = n + return n +end}) + +-- Dump global labels. +local function dumpglobals(out, lvl) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("Global labels:\n") + for i=20,next_global-1 do + out:write(format(" %s\n", t[i])) + end + out:write("\n") +end + +-- Write global label enum. +local function writeglobals(out, prefix) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("enum {\n") + for i=20,next_global-1 do + out:write(" ", prefix, t[i], ",\n") + end + out:write(" ", prefix, "_MAX\n};\n") +end + +-- Write global label names. +local function writeglobalnames(out, name) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("static const char *const ", name, "[] = {\n") + for i=20,next_global-1 do + out:write(" \"", t[i], "\",\n") + end + out:write(" (const char *)0\n};\n") +end + +------------------------------------------------------------------------------ + +-- Extern label name -> extern label number. With auto assignment on 1st use. +local next_extern = 0 +local map_extern_ = {} +local map_extern = setmetatable({}, { __index = function(t, name) + -- No restrictions on the name for now. + local n = next_extern + if n > 2047 then werror("too many extern labels") end + next_extern = n + 1 + t[name] = n + map_extern_[n] = name + return n +end}) + +-- Dump extern labels. +local function dumpexterns(out, lvl) + out:write("Extern labels:\n") + for i=0,next_extern-1 do + out:write(format(" %s\n", map_extern_[i])) + end + out:write("\n") +end + +-- Write extern label names. +local function writeexternnames(out, name) + out:write("static const char *const ", name, "[] = {\n") + for i=0,next_extern-1 do + out:write(" \"", map_extern_[i], "\",\n") + end + out:write(" (const char *)0\n};\n") +end + +------------------------------------------------------------------------------ + +-- Arch-specific maps. +local map_archdef = { sp="r29", ra="r31" } -- Ext. register name -> int. name. + +local map_type = {} -- Type name -> { ctype, reg } +local ctypenum = 0 -- Type number (for Dt... macros). + +-- Reverse defines for registers. +function _M.revdef(s) + if s == "r29" then return "sp" + elseif s == "r31" then return "ra" end + return s +end + +------------------------------------------------------------------------------ + +-- Template strings for MIPS instructions. +local map_op = { + -- First-level opcodes. + j_1 = "08000000J", + jal_1 = "0c000000J", + b_1 = "10000000B", + beqz_2 = "10000000SB", + beq_3 = "10000000STB", + bnez_2 = "14000000SB", + bne_3 = "14000000STB", + blez_2 = "18000000SB", + bgtz_2 = "1c000000SB", + addi_3 = "20000000TSI", + li_2 = "24000000TI", + addiu_3 = "24000000TSI", + slti_3 = "28000000TSI", + sltiu_3 = "2c000000TSI", + andi_3 = "30000000TSU", + lu_2 = "34000000TU", + ori_3 = "34000000TSU", + xori_3 = "38000000TSU", + lui_2 = "3c000000TU", + beqzl_2 = "50000000SB", + beql_3 = "50000000STB", + bnezl_2 = "54000000SB", + bnel_3 = "54000000STB", + blezl_2 = "58000000SB", + bgtzl_2 = "5c000000SB", + lb_2 = "80000000TO", + lh_2 = "84000000TO", + lwl_2 = "88000000TO", + lw_2 = "8c000000TO", + lbu_2 = "90000000TO", + lhu_2 = "94000000TO", + lwr_2 = "98000000TO", + sb_2 = "a0000000TO", + sh_2 = "a4000000TO", + swl_2 = "a8000000TO", + sw_2 = "ac000000TO", + swr_2 = "b8000000TO", + cache_2 = "bc000000NO", + ll_2 = "c0000000TO", + lwc1_2 = "c4000000HO", + pref_2 = "cc000000NO", + ldc1_2 = "d4000000HO", + sc_2 = "e0000000TO", + swc1_2 = "e4000000HO", + sdc1_2 = "f4000000HO", + + -- Opcode SPECIAL. + nop_0 = "00000000", + sll_3 = "00000000DTA", + movf_2 = "00000001DS", + movf_3 = "00000001DSC", + movt_2 = "00010001DS", + movt_3 = "00010001DSC", + srl_3 = "00000002DTA", + rotr_3 = "00200002DTA", + sra_3 = "00000003DTA", + sllv_3 = "00000004DTS", + srlv_3 = "00000006DTS", + rotrv_3 = "00000046DTS", + srav_3 = "00000007DTS", + jr_1 = "00000008S", + jalr_1 = "0000f809S", + jalr_2 = "00000009DS", + movz_3 = "0000000aDST", + movn_3 = "0000000bDST", + syscall_0 = "0000000c", + syscall_1 = "0000000cY", + break_0 = "0000000d", + break_1 = "0000000dY", + sync_0 = "0000000f", + mfhi_1 = "00000010D", + mthi_1 = "00000011S", + mflo_1 = "00000012D", + mtlo_1 = "00000013S", + mult_2 = "00000018ST", + multu_2 = "00000019ST", + div_2 = "0000001aST", + divu_2 = "0000001bST", + add_3 = "00000020DST", + move_2 = "00000021DS", + addu_3 = "00000021DST", + sub_3 = "00000022DST", + negu_2 = "00000023DT", + subu_3 = "00000023DST", + and_3 = "00000024DST", + or_3 = "00000025DST", + xor_3 = "00000026DST", + not_2 = "00000027DS", + nor_3 = "00000027DST", + slt_3 = "0000002aDST", + sltu_3 = "0000002bDST", + tge_2 = "00000030ST", + tge_3 = "00000030STZ", + tgeu_2 = "00000031ST", + tgeu_3 = "00000031STZ", + tlt_2 = "00000032ST", + tlt_3 = "00000032STZ", + tltu_2 = "00000033ST", + tltu_3 = "00000033STZ", + teq_2 = "00000034ST", + teq_3 = "00000034STZ", + tne_2 = "00000036ST", + tne_3 = "00000036STZ", + + -- Opcode REGIMM. + bltz_2 = "04000000SB", + bgez_2 = "04010000SB", + bltzl_2 = "04020000SB", + bgezl_2 = "04030000SB", + tgei_2 = "04080000SI", + tgeiu_2 = "04090000SI", + tlti_2 = "040a0000SI", + tltiu_2 = "040b0000SI", + teqi_2 = "040c0000SI", + tnei_2 = "040e0000SI", + bltzal_2 = "04100000SB", + bal_1 = "04110000B", + bgezal_2 = "04110000SB", + bltzall_2 = "04120000SB", + bgezall_2 = "04130000SB", + synci_1 = "041f0000O", + + -- Opcode SPECIAL2. + madd_2 = "70000000ST", + maddu_2 = "70000001ST", + mul_3 = "70000002DST", + msub_2 = "70000004ST", + msubu_2 = "70000005ST", + clz_2 = "70000020DS=", + clo_2 = "70000021DS=", + sdbbp_0 = "7000003f", + sdbbp_1 = "7000003fY", + + -- Opcode SPECIAL3. + ext_4 = "7c000000TSAM", -- Note: last arg is msbd = size-1 + ins_4 = "7c000004TSAM", -- Note: last arg is msb = pos+size-1 + wsbh_2 = "7c0000a0DT", + seb_2 = "7c000420DT", + seh_2 = "7c000620DT", + rdhwr_2 = "7c00003bTD", + + -- Opcode COP0. + mfc0_2 = "40000000TD", + mfc0_3 = "40000000TDW", + mtc0_2 = "40800000TD", + mtc0_3 = "40800000TDW", + rdpgpr_2 = "41400000DT", + di_0 = "41606000", + di_1 = "41606000T", + ei_0 = "41606020", + ei_1 = "41606020T", + wrpgpr_2 = "41c00000DT", + tlbr_0 = "42000001", + tlbwi_0 = "42000002", + tlbwr_0 = "42000006", + tlbp_0 = "42000008", + eret_0 = "42000018", + deret_0 = "4200001f", + wait_0 = "42000020", + + -- Opcode COP1. + mfc1_2 = "44000000TG", + cfc1_2 = "44400000TG", + mfhc1_2 = "44600000TG", + mtc1_2 = "44800000TG", + ctc1_2 = "44c00000TG", + mthc1_2 = "44e00000TG", + + bc1f_1 = "45000000B", + bc1f_2 = "45000000CB", + bc1t_1 = "45010000B", + bc1t_2 = "45010000CB", + bc1fl_1 = "45020000B", + bc1fl_2 = "45020000CB", + bc1tl_1 = "45030000B", + bc1tl_2 = "45030000CB", + + ["add.s_3"] = "46000000FGH", + ["sub.s_3"] = "46000001FGH", + ["mul.s_3"] = "46000002FGH", + ["div.s_3"] = "46000003FGH", + ["sqrt.s_2"] = "46000004FG", + ["abs.s_2"] = "46000005FG", + ["mov.s_2"] = "46000006FG", + ["neg.s_2"] = "46000007FG", + ["round.l.s_2"] = "46000008FG", + ["trunc.l.s_2"] = "46000009FG", + ["ceil.l.s_2"] = "4600000aFG", + ["floor.l.s_2"] = "4600000bFG", + ["round.w.s_2"] = "4600000cFG", + ["trunc.w.s_2"] = "4600000dFG", + ["ceil.w.s_2"] = "4600000eFG", + ["floor.w.s_2"] = "4600000fFG", + ["movf.s_2"] = "46000011FG", + ["movf.s_3"] = "46000011FGC", + ["movt.s_2"] = "46010011FG", + ["movt.s_3"] = "46010011FGC", + ["movz.s_3"] = "46000012FGT", + ["movn.s_3"] = "46000013FGT", + ["recip.s_2"] = "46000015FG", + ["rsqrt.s_2"] = "46000016FG", + ["cvt.d.s_2"] = "46000021FG", + ["cvt.w.s_2"] = "46000024FG", + ["cvt.l.s_2"] = "46000025FG", + ["cvt.ps.s_3"] = "46000026FGH", + ["c.f.s_2"] = "46000030GH", + ["c.f.s_3"] = "46000030VGH", + ["c.un.s_2"] = "46000031GH", + ["c.un.s_3"] = "46000031VGH", + ["c.eq.s_2"] = "46000032GH", + ["c.eq.s_3"] = "46000032VGH", + ["c.ueq.s_2"] = "46000033GH", + ["c.ueq.s_3"] = "46000033VGH", + ["c.olt.s_2"] = "46000034GH", + ["c.olt.s_3"] = "46000034VGH", + ["c.ult.s_2"] = "46000035GH", + ["c.ult.s_3"] = "46000035VGH", + ["c.ole.s_2"] = "46000036GH", + ["c.ole.s_3"] = "46000036VGH", + ["c.ule.s_2"] = "46000037GH", + ["c.ule.s_3"] = "46000037VGH", + ["c.sf.s_2"] = "46000038GH", + ["c.sf.s_3"] = "46000038VGH", + ["c.ngle.s_2"] = "46000039GH", + ["c.ngle.s_3"] = "46000039VGH", + ["c.seq.s_2"] = "4600003aGH", + ["c.seq.s_3"] = "4600003aVGH", + ["c.ngl.s_2"] = "4600003bGH", + ["c.ngl.s_3"] = "4600003bVGH", + ["c.lt.s_2"] = "4600003cGH", + ["c.lt.s_3"] = "4600003cVGH", + ["c.nge.s_2"] = "4600003dGH", + ["c.nge.s_3"] = "4600003dVGH", + ["c.le.s_2"] = "4600003eGH", + ["c.le.s_3"] = "4600003eVGH", + ["c.ngt.s_2"] = "4600003fGH", + ["c.ngt.s_3"] = "4600003fVGH", + + ["add.d_3"] = "46200000FGH", + ["sub.d_3"] = "46200001FGH", + ["mul.d_3"] = "46200002FGH", + ["div.d_3"] = "46200003FGH", + ["sqrt.d_2"] = "46200004FG", + ["abs.d_2"] = "46200005FG", + ["mov.d_2"] = "46200006FG", + ["neg.d_2"] = "46200007FG", + ["round.l.d_2"] = "46200008FG", + ["trunc.l.d_2"] = "46200009FG", + ["ceil.l.d_2"] = "4620000aFG", + ["floor.l.d_2"] = "4620000bFG", + ["round.w.d_2"] = "4620000cFG", + ["trunc.w.d_2"] = "4620000dFG", + ["ceil.w.d_2"] = "4620000eFG", + ["floor.w.d_2"] = "4620000fFG", + ["movf.d_2"] = "46200011FG", + ["movf.d_3"] = "46200011FGC", + ["movt.d_2"] = "46210011FG", + ["movt.d_3"] = "46210011FGC", + ["movz.d_3"] = "46200012FGT", + ["movn.d_3"] = "46200013FGT", + ["recip.d_2"] = "46200015FG", + ["rsqrt.d_2"] = "46200016FG", + ["cvt.s.d_2"] = "46200020FG", + ["cvt.w.d_2"] = "46200024FG", + ["cvt.l.d_2"] = "46200025FG", + ["c.f.d_2"] = "46200030GH", + ["c.f.d_3"] = "46200030VGH", + ["c.un.d_2"] = "46200031GH", + ["c.un.d_3"] = "46200031VGH", + ["c.eq.d_2"] = "46200032GH", + ["c.eq.d_3"] = "46200032VGH", + ["c.ueq.d_2"] = "46200033GH", + ["c.ueq.d_3"] = "46200033VGH", + ["c.olt.d_2"] = "46200034GH", + ["c.olt.d_3"] = "46200034VGH", + ["c.ult.d_2"] = "46200035GH", + ["c.ult.d_3"] = "46200035VGH", + ["c.ole.d_2"] = "46200036GH", + ["c.ole.d_3"] = "46200036VGH", + ["c.ule.d_2"] = "46200037GH", + ["c.ule.d_3"] = "46200037VGH", + ["c.sf.d_2"] = "46200038GH", + ["c.sf.d_3"] = "46200038VGH", + ["c.ngle.d_2"] = "46200039GH", + ["c.ngle.d_3"] = "46200039VGH", + ["c.seq.d_2"] = "4620003aGH", + ["c.seq.d_3"] = "4620003aVGH", + ["c.ngl.d_2"] = "4620003bGH", + ["c.ngl.d_3"] = "4620003bVGH", + ["c.lt.d_2"] = "4620003cGH", + ["c.lt.d_3"] = "4620003cVGH", + ["c.nge.d_2"] = "4620003dGH", + ["c.nge.d_3"] = "4620003dVGH", + ["c.le.d_2"] = "4620003eGH", + ["c.le.d_3"] = "4620003eVGH", + ["c.ngt.d_2"] = "4620003fGH", + ["c.ngt.d_3"] = "4620003fVGH", + + ["add.ps_3"] = "46c00000FGH", + ["sub.ps_3"] = "46c00001FGH", + ["mul.ps_3"] = "46c00002FGH", + ["abs.ps_2"] = "46c00005FG", + ["mov.ps_2"] = "46c00006FG", + ["neg.ps_2"] = "46c00007FG", + ["movf.ps_2"] = "46c00011FG", + ["movf.ps_3"] = "46c00011FGC", + ["movt.ps_2"] = "46c10011FG", + ["movt.ps_3"] = "46c10011FGC", + ["movz.ps_3"] = "46c00012FGT", + ["movn.ps_3"] = "46c00013FGT", + ["cvt.s.pu_2"] = "46c00020FG", + ["cvt.s.pl_2"] = "46c00028FG", + ["pll.ps_3"] = "46c0002cFGH", + ["plu.ps_3"] = "46c0002dFGH", + ["pul.ps_3"] = "46c0002eFGH", + ["puu.ps_3"] = "46c0002fFGH", + ["c.f.ps_2"] = "46c00030GH", + ["c.f.ps_3"] = "46c00030VGH", + ["c.un.ps_2"] = "46c00031GH", + ["c.un.ps_3"] = "46c00031VGH", + ["c.eq.ps_2"] = "46c00032GH", + ["c.eq.ps_3"] = "46c00032VGH", + ["c.ueq.ps_2"] = "46c00033GH", + ["c.ueq.ps_3"] = "46c00033VGH", + ["c.olt.ps_2"] = "46c00034GH", + ["c.olt.ps_3"] = "46c00034VGH", + ["c.ult.ps_2"] = "46c00035GH", + ["c.ult.ps_3"] = "46c00035VGH", + ["c.ole.ps_2"] = "46c00036GH", + ["c.ole.ps_3"] = "46c00036VGH", + ["c.ule.ps_2"] = "46c00037GH", + ["c.ule.ps_3"] = "46c00037VGH", + ["c.sf.ps_2"] = "46c00038GH", + ["c.sf.ps_3"] = "46c00038VGH", + ["c.ngle.ps_2"] = "46c00039GH", + ["c.ngle.ps_3"] = "46c00039VGH", + ["c.seq.ps_2"] = "46c0003aGH", + ["c.seq.ps_3"] = "46c0003aVGH", + ["c.ngl.ps_2"] = "46c0003bGH", + ["c.ngl.ps_3"] = "46c0003bVGH", + ["c.lt.ps_2"] = "46c0003cGH", + ["c.lt.ps_3"] = "46c0003cVGH", + ["c.nge.ps_2"] = "46c0003dGH", + ["c.nge.ps_3"] = "46c0003dVGH", + ["c.le.ps_2"] = "46c0003eGH", + ["c.le.ps_3"] = "46c0003eVGH", + ["c.ngt.ps_2"] = "46c0003fGH", + ["c.ngt.ps_3"] = "46c0003fVGH", + + ["cvt.s.w_2"] = "46800020FG", + ["cvt.d.w_2"] = "46800021FG", + + ["cvt.s.l_2"] = "46a00020FG", + ["cvt.d.l_2"] = "46a00021FG", + + -- Opcode COP1X. + lwxc1_2 = "4c000000FX", + ldxc1_2 = "4c000001FX", + luxc1_2 = "4c000005FX", + swxc1_2 = "4c000008FX", + sdxc1_2 = "4c000009FX", + suxc1_2 = "4c00000dFX", + prefx_2 = "4c00000fMX", + ["alnv.ps_4"] = "4c00001eFGHS", + ["madd.s_4"] = "4c000020FRGH", + ["madd.d_4"] = "4c000021FRGH", + ["madd.ps_4"] = "4c000026FRGH", + ["msub.s_4"] = "4c000028FRGH", + ["msub.d_4"] = "4c000029FRGH", + ["msub.ps_4"] = "4c00002eFRGH", + ["nmadd.s_4"] = "4c000030FRGH", + ["nmadd.d_4"] = "4c000031FRGH", + ["nmadd.ps_4"] = "4c000036FRGH", + ["nmsub.s_4"] = "4c000038FRGH", + ["nmsub.d_4"] = "4c000039FRGH", + ["nmsub.ps_4"] = "4c00003eFRGH", +} + +------------------------------------------------------------------------------ + +local function parse_gpr(expr) + local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") + local tp = map_type[tname or expr] + if tp then + local reg = ovreg or tp.reg + if not reg then + werror("type `"..(tname or expr).."' needs a register override") + end + expr = reg + end + local r = match(expr, "^r([1-3]?[0-9])$") + if r then + r = tonumber(r) + if r <= 31 then return r, tp end + end + werror("bad register name `"..expr.."'") +end + +local function parse_fpr(expr) + local r = match(expr, "^f([1-3]?[0-9])$") + if r then + r = tonumber(r) + if r <= 31 then return r end + end + werror("bad register name `"..expr.."'") +end + +local function parse_imm(imm, bits, shift, scale, signed) + local n = tonumber(imm) + if n then + local m = sar(n, scale) + if shl(m, scale) == n then + if signed then + local s = sar(m, bits-1) + if s == 0 then return shl(m, shift) + elseif s == -1 then return shl(m + shl(1, bits), shift) end + else + if sar(m, bits) == 0 then return shl(m, shift) end + end + end + werror("out of range immediate `"..imm.."'") + elseif match(imm, "^[rf]([1-3]?[0-9])$") or + match(imm, "^([%w_]+):([rf][1-3]?[0-9])$") then + werror("expected immediate operand, got register") + else + waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) + return 0 + end +end + +local function parse_disp(disp) + local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") + if imm then + local r = shl(parse_gpr(reg), 21) + local extname = match(imm, "^extern%s+(%S+)$") + if extname then + waction("REL_EXT", map_extern[extname], nil, 1) + return r + else + return r + parse_imm(imm, 16, 0, 0, true) + end + end + local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") + if reg and tailr ~= "" then + local r, tp = parse_gpr(reg) + if tp then + waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) + return shl(r, 21) + end + end + werror("bad displacement `"..disp.."'") +end + +local function parse_index(idx) + local rt, rs = match(idx, "^(.*)%(([%w_:]+)%)$") + if rt then + rt = parse_gpr(rt) + rs = parse_gpr(rs) + return shl(rt, 16) + shl(rs, 21) + end + werror("bad index `"..idx.."'") +end + +local function parse_label(label, def) + local prefix = sub(label, 1, 2) + -- =>label (pc label reference) + if prefix == "=>" then + return "PC", 0, sub(label, 3) + end + -- ->name (global label reference) + if prefix == "->" then + return "LG", map_global[sub(label, 3)] + end + if def then + -- [1-9] (local label definition) + if match(label, "^[1-9]$") then + return "LG", 10+tonumber(label) + end + else + -- [<>][1-9] (local label reference) + local dir, lnum = match(label, "^([<>])([1-9])$") + if dir then -- Fwd: 1-9, Bkwd: 11-19. + return "LG", lnum + (dir == ">" and 0 or 10) + end + -- extern label (extern label reference) + local extname = match(label, "^extern%s+(%S+)$") + if extname then + return "EXT", map_extern[extname] + end + end + werror("bad label `"..label.."'") +end + +------------------------------------------------------------------------------ + +-- Handle opcodes defined with template strings. +map_op[".template__"] = function(params, template, nparams) + if not params then return sub(template, 9) end + local op = tonumber(sub(template, 1, 8), 16) + local n = 1 + + -- Limit number of section buffer positions used by a single dasm_put(). + -- A single opcode needs a maximum of 2 positions (ins/ext). + if secpos+2 > maxsecpos then wflush() end + local pos = wpos() + + -- Process each character. + for p in gmatch(sub(template, 9), ".") do + if p == "D" then + op = op + shl(parse_gpr(params[n]), 11); n = n + 1 + elseif p == "T" then + op = op + shl(parse_gpr(params[n]), 16); n = n + 1 + elseif p == "S" then + op = op + shl(parse_gpr(params[n]), 21); n = n + 1 + elseif p == "F" then + op = op + shl(parse_fpr(params[n]), 6); n = n + 1 + elseif p == "G" then + op = op + shl(parse_fpr(params[n]), 11); n = n + 1 + elseif p == "H" then + op = op + shl(parse_fpr(params[n]), 16); n = n + 1 + elseif p == "R" then + op = op + shl(parse_fpr(params[n]), 21); n = n + 1 + elseif p == "I" then + op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 + elseif p == "U" then + op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 + elseif p == "O" then + op = op + parse_disp(params[n]); n = n + 1 + elseif p == "X" then + op = op + parse_index(params[n]); n = n + 1 + elseif p == "B" or p == "J" then + local mode, n, s = parse_label(params[n], false) + if p == "B" then n = n + 2048 end + waction("REL_"..mode, n, s, 1) + n = n + 1 + elseif p == "A" then + op = op + parse_imm(params[n], 5, 6, 0, false); n = n + 1 + elseif p == "M" then + op = op + parse_imm(params[n], 5, 11, 0, false); n = n + 1 + elseif p == "N" then + op = op + parse_imm(params[n], 5, 16, 0, false); n = n + 1 + elseif p == "C" then + op = op + parse_imm(params[n], 3, 18, 0, false); n = n + 1 + elseif p == "V" then + op = op + parse_imm(params[n], 3, 8, 0, false); n = n + 1 + elseif p == "W" then + op = op + parse_imm(params[n], 3, 0, 0, false); n = n + 1 + elseif p == "Y" then + op = op + parse_imm(params[n], 20, 6, 0, false); n = n + 1 + elseif p == "Z" then + op = op + parse_imm(params[n], 10, 6, 0, false); n = n + 1 + elseif p == "=" then + op = op + shl(band(op, 0xf800), 5) -- Copy D to T for clz, clo. + else + assert(false) + end + end + wputpos(pos, op) +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcode to mark the position where the action list is to be emitted. +map_op[".actionlist_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeactions(out, name) end) +end + +-- Pseudo-opcode to mark the position where the global enum is to be emitted. +map_op[".globals_1"] = function(params) + if not params then return "prefix" end + local prefix = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeglobals(out, prefix) end) +end + +-- Pseudo-opcode to mark the position where the global names are to be emitted. +map_op[".globalnames_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeglobalnames(out, name) end) +end + +-- Pseudo-opcode to mark the position where the extern names are to be emitted. +map_op[".externnames_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeexternnames(out, name) end) +end + +------------------------------------------------------------------------------ + +-- Label pseudo-opcode (converted from trailing colon form). +map_op[".label_1"] = function(params) + if not params then return "[1-9] | ->global | =>pcexpr" end + if secpos+1 > maxsecpos then wflush() end + local mode, n, s = parse_label(params[1], true) + if mode == "EXT" then werror("bad label definition") end + waction("LABEL_"..mode, n, s, 1) +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcodes for data storage. +map_op[".long_*"] = function(params) + if not params then return "imm..." end + for _,p in ipairs(params) do + local n = tonumber(p) + if not n then werror("bad immediate `"..p.."'") end + if n < 0 then n = n + 2^32 end + wputw(n) + if secpos+2 > maxsecpos then wflush() end + end +end + +-- Alignment pseudo-opcode. +map_op[".align_1"] = function(params) + if not params then return "numpow2" end + if secpos+1 > maxsecpos then wflush() end + local align = tonumber(params[1]) + if align then + local x = align + -- Must be a power of 2 in the range (2 ... 256). + for i=1,8 do + x = x / 2 + if x == 1 then + waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. + return + end + end + end + werror("bad alignment") +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcode for (primitive) type definitions (map to C types). +map_op[".type_3"] = function(params, nparams) + if not params then + return nparams == 2 and "name, ctype" or "name, ctype, reg" + end + local name, ctype, reg = params[1], params[2], params[3] + if not match(name, "^[%a_][%w_]*$") then + werror("bad type name `"..name.."'") + end + local tp = map_type[name] + if tp then + werror("duplicate type `"..name.."'") + end + -- Add #type to defines. A bit unclean to put it in map_archdef. + map_archdef["#"..name] = "sizeof("..ctype..")" + -- Add new type and emit shortcut define. + local num = ctypenum + 1 + map_type[name] = { + ctype = ctype, + ctypefmt = format("Dt%X(%%s)", num), + reg = reg, + } + wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) + ctypenum = num +end +map_op[".type_2"] = map_op[".type_3"] + +-- Dump type definitions. +local function dumptypes(out, lvl) + local t = {} + for name in pairs(map_type) do t[#t+1] = name end + sort(t) + out:write("Type definitions:\n") + for _,name in ipairs(t) do + local tp = map_type[name] + local reg = tp.reg or "" + out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) + end + out:write("\n") +end + +------------------------------------------------------------------------------ + +-- Set the current section. +function _M.section(num) + waction("SECTION", num) + wflush(true) -- SECTION is a terminal action. +end + +------------------------------------------------------------------------------ + +-- Dump architecture description. +function _M.dumparch(out) + out:write(format("DynASM %s version %s, released %s\n\n", + _info.arch, _info.version, _info.release)) + dumpactions(out) +end + +-- Dump all user defined elements. +function _M.dumpdef(out, lvl) + dumptypes(out, lvl) + dumpglobals(out, lvl) + dumpexterns(out, lvl) +end + +------------------------------------------------------------------------------ + +-- Pass callbacks from/to the DynASM core. +function _M.passcb(wl, we, wf, ww) + wline, werror, wfatal, wwarn = wl, we, wf, ww + return wflush +end + +-- Setup the arch-specific module. +function _M.setup(arch, opt) + g_arch, g_opt = arch, opt +end + +-- Merge the core maps and the arch-specific maps. +function _M.mergemaps(map_coreop, map_def) + setmetatable(map_op, { __index = map_coreop }) + setmetatable(map_def, { __index = map_archdef }) + return map_op, map_def +end + +return _M + +------------------------------------------------------------------------------ + diff --git a/external/lua/luajit/src/dynasm/dasm_ppc.h b/external/lua/luajit/src/dynasm/dasm_ppc.h new file mode 100644 index 0000000000..774ee6edf9 --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_ppc.h @@ -0,0 +1,411 @@ +/* +** DynASM PPC encoding engine. +** Copyright (C) 2005-2013 Mike Pall. All rights reserved. +** Released under the MIT license. See dynasm.lua for full copyright notice. +*/ + +#include +#include +#include +#include + +#define DASM_ARCH "ppc" + +#ifndef DASM_EXTERN +#define DASM_EXTERN(a,b,c,d) 0 +#endif + +/* Action definitions. */ +enum { + DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, + /* The following actions need a buffer position. */ + DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, + /* The following actions also have an argument. */ + DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, + DASM__MAX +}; + +/* Maximum number of section buffer positions for a single dasm_put() call. */ +#define DASM_MAXSECPOS 25 + +/* DynASM encoder status codes. Action list offset or number are or'ed in. */ +#define DASM_S_OK 0x00000000 +#define DASM_S_NOMEM 0x01000000 +#define DASM_S_PHASE 0x02000000 +#define DASM_S_MATCH_SEC 0x03000000 +#define DASM_S_RANGE_I 0x11000000 +#define DASM_S_RANGE_SEC 0x12000000 +#define DASM_S_RANGE_LG 0x13000000 +#define DASM_S_RANGE_PC 0x14000000 +#define DASM_S_RANGE_REL 0x15000000 +#define DASM_S_UNDEF_LG 0x21000000 +#define DASM_S_UNDEF_PC 0x22000000 + +/* Macros to convert positions (8 bit section + 24 bit index). */ +#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) +#define DASM_POS2BIAS(pos) ((pos)&0xff000000) +#define DASM_SEC2POS(sec) ((sec)<<24) +#define DASM_POS2SEC(pos) ((pos)>>24) +#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) + +/* Action list type. */ +typedef const unsigned int *dasm_ActList; + +/* Per-section structure. */ +typedef struct dasm_Section { + int *rbuf; /* Biased buffer pointer (negative section bias). */ + int *buf; /* True buffer pointer. */ + size_t bsize; /* Buffer size in bytes. */ + int pos; /* Biased buffer position. */ + int epos; /* End of biased buffer position - max single put. */ + int ofs; /* Byte offset into section. */ +} dasm_Section; + +/* Core structure holding the DynASM encoding state. */ +struct dasm_State { + size_t psize; /* Allocated size of this structure. */ + dasm_ActList actionlist; /* Current actionlist pointer. */ + int *lglabels; /* Local/global chain/pos ptrs. */ + size_t lgsize; + int *pclabels; /* PC label chains/pos ptrs. */ + size_t pcsize; + void **globals; /* Array of globals (bias -10). */ + dasm_Section *section; /* Pointer to active section. */ + size_t codesize; /* Total size of all code sections. */ + int maxsection; /* 0 <= sectionidx < maxsection. */ + int status; /* Status code. */ + dasm_Section sections[1]; /* All sections. Alloc-extended. */ +}; + +/* The size of the core structure depends on the max. number of sections. */ +#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) + + +/* Initialize DynASM state. */ +void dasm_init(Dst_DECL, int maxsection) +{ + dasm_State *D; + size_t psz = 0; + int i; + Dst_REF = NULL; + DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); + D = Dst_REF; + D->psize = psz; + D->lglabels = NULL; + D->lgsize = 0; + D->pclabels = NULL; + D->pcsize = 0; + D->globals = NULL; + D->maxsection = maxsection; + for (i = 0; i < maxsection; i++) { + D->sections[i].buf = NULL; /* Need this for pass3. */ + D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); + D->sections[i].bsize = 0; + D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ + } +} + +/* Free DynASM state. */ +void dasm_free(Dst_DECL) +{ + dasm_State *D = Dst_REF; + int i; + for (i = 0; i < D->maxsection; i++) + if (D->sections[i].buf) + DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); + if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); + if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); + DASM_M_FREE(Dst, D, D->psize); +} + +/* Setup global label array. Must be called before dasm_setup(). */ +void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) +{ + dasm_State *D = Dst_REF; + D->globals = gl - 10; /* Negative bias to compensate for locals. */ + DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); +} + +/* Grow PC label array. Can be called after dasm_setup(), too. */ +void dasm_growpc(Dst_DECL, unsigned int maxpc) +{ + dasm_State *D = Dst_REF; + size_t osz = D->pcsize; + DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); + memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); +} + +/* Setup encoder. */ +void dasm_setup(Dst_DECL, const void *actionlist) +{ + dasm_State *D = Dst_REF; + int i; + D->actionlist = (dasm_ActList)actionlist; + D->status = DASM_S_OK; + D->section = &D->sections[0]; + memset((void *)D->lglabels, 0, D->lgsize); + if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); + for (i = 0; i < D->maxsection; i++) { + D->sections[i].pos = DASM_SEC2POS(i); + D->sections[i].ofs = 0; + } +} + + +#ifdef DASM_CHECKS +#define CK(x, st) \ + do { if (!(x)) { \ + D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) +#define CKPL(kind, st) \ + do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ + D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) +#else +#define CK(x, st) ((void)0) +#define CKPL(kind, st) ((void)0) +#endif + +/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ +void dasm_put(Dst_DECL, int start, ...) +{ + va_list ap; + dasm_State *D = Dst_REF; + dasm_ActList p = D->actionlist + start; + dasm_Section *sec = D->section; + int pos = sec->pos, ofs = sec->ofs; + int *b; + + if (pos >= sec->epos) { + DASM_M_GROW(Dst, int, sec->buf, sec->bsize, + sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); + sec->rbuf = sec->buf - DASM_POS2BIAS(pos); + sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); + } + + b = sec->rbuf; + b[pos++] = start; + + va_start(ap, start); + while (1) { + unsigned int ins = *p++; + unsigned int action = (ins >> 16); + if (action >= DASM__MAX) { + ofs += 4; + } else { + int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; + switch (action) { + case DASM_STOP: goto stop; + case DASM_SECTION: + n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); + D->section = &D->sections[n]; goto stop; + case DASM_ESC: p++; ofs += 4; break; + case DASM_REL_EXT: break; + case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; + case DASM_REL_LG: + n = (ins & 2047) - 10; pl = D->lglabels + n; + if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ + pl += 10; n = *pl; + if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ + goto linkrel; + case DASM_REL_PC: + pl = D->pclabels + n; CKPL(pc, PC); + putrel: + n = *pl; + if (n < 0) { /* Label exists. Get label pos and store it. */ + b[pos] = -n; + } else { + linkrel: + b[pos] = n; /* Else link to rel chain, anchored at label. */ + *pl = pos; + } + pos++; + break; + case DASM_LABEL_LG: + pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; + case DASM_LABEL_PC: + pl = D->pclabels + n; CKPL(pc, PC); + putlabel: + n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ + while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; + } + *pl = -pos; /* Label exists now. */ + b[pos++] = ofs; /* Store pass1 offset estimate. */ + break; + case DASM_IMM: +#ifdef DASM_CHECKS + CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); +#endif + n >>= ((ins>>10)&31); +#ifdef DASM_CHECKS + if (ins & 0x8000) + CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); + else + CK((n>>((ins>>5)&31)) == 0, RANGE_I); +#endif + b[pos++] = n; + break; + } + } + } +stop: + va_end(ap); + sec->pos = pos; + sec->ofs = ofs; +} +#undef CK + +/* Pass 2: Link sections, shrink aligns, fix label offsets. */ +int dasm_link(Dst_DECL, size_t *szp) +{ + dasm_State *D = Dst_REF; + int secnum; + int ofs = 0; + +#ifdef DASM_CHECKS + *szp = 0; + if (D->status != DASM_S_OK) return D->status; + { + int pc; + for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) + if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; + } +#endif + + { /* Handle globals not defined in this translation unit. */ + int idx; + for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { + int n = D->lglabels[idx]; + /* Undefined label: Collapse rel chain and replace with marker (< 0). */ + while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } + } + } + + /* Combine all code sections. No support for data sections (yet). */ + for (secnum = 0; secnum < D->maxsection; secnum++) { + dasm_Section *sec = D->sections + secnum; + int *b = sec->rbuf; + int pos = DASM_SEC2POS(secnum); + int lastpos = sec->pos; + + while (pos != lastpos) { + dasm_ActList p = D->actionlist + b[pos++]; + while (1) { + unsigned int ins = *p++; + unsigned int action = (ins >> 16); + switch (action) { + case DASM_STOP: case DASM_SECTION: goto stop; + case DASM_ESC: p++; break; + case DASM_REL_EXT: break; + case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; + case DASM_REL_LG: case DASM_REL_PC: pos++; break; + case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; + case DASM_IMM: pos++; break; + } + } + stop: (void)0; + } + ofs += sec->ofs; /* Next section starts right after current section. */ + } + + D->codesize = ofs; /* Total size of all code sections */ + *szp = ofs; + return DASM_S_OK; +} + +#ifdef DASM_CHECKS +#define CK(x, st) \ + do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) +#else +#define CK(x, st) ((void)0) +#endif + +/* Pass 3: Encode sections. */ +int dasm_encode(Dst_DECL, void *buffer) +{ + dasm_State *D = Dst_REF; + char *base = (char *)buffer; + unsigned int *cp = (unsigned int *)buffer; + int secnum; + + /* Encode all code sections. No support for data sections (yet). */ + for (secnum = 0; secnum < D->maxsection; secnum++) { + dasm_Section *sec = D->sections + secnum; + int *b = sec->buf; + int *endb = sec->rbuf + sec->pos; + + while (b != endb) { + dasm_ActList p = D->actionlist + *b++; + while (1) { + unsigned int ins = *p++; + unsigned int action = (ins >> 16); + int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; + switch (action) { + case DASM_STOP: case DASM_SECTION: goto stop; + case DASM_ESC: *cp++ = *p++; break; + case DASM_REL_EXT: + n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1) - 4; + goto patchrel; + case DASM_ALIGN: + ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; + break; + case DASM_REL_LG: + CK(n >= 0, UNDEF_LG); + case DASM_REL_PC: + CK(n >= 0, UNDEF_PC); + n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base); + patchrel: + CK((n & 3) == 0 && + (((n+4) + ((ins & 2048) ? 0x00008000 : 0x02000000)) >> + ((ins & 2048) ? 16 : 26)) == 0, RANGE_REL); + cp[-1] |= ((n+4) & ((ins & 2048) ? 0x0000fffc: 0x03fffffc)); + break; + case DASM_LABEL_LG: + ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); + break; + case DASM_LABEL_PC: break; + case DASM_IMM: + cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); + break; + default: *cp++ = ins; break; + } + } + stop: (void)0; + } + } + + if (base + D->codesize != (char *)cp) /* Check for phase errors. */ + return DASM_S_PHASE; + return DASM_S_OK; +} +#undef CK + +/* Get PC label offset. */ +int dasm_getpclabel(Dst_DECL, unsigned int pc) +{ + dasm_State *D = Dst_REF; + if (pc*sizeof(int) < D->pcsize) { + int pos = D->pclabels[pc]; + if (pos < 0) return *DASM_POS2PTR(D, -pos); + if (pos > 0) return -1; /* Undefined. */ + } + return -2; /* Unused or out of range. */ +} + +#ifdef DASM_CHECKS +/* Optional sanity checker to call between isolated encoding steps. */ +int dasm_checkstep(Dst_DECL, int secmatch) +{ + dasm_State *D = Dst_REF; + if (D->status == DASM_S_OK) { + int i; + for (i = 1; i <= 9; i++) { + if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } + D->lglabels[i] = 0; + } + } + if (D->status == DASM_S_OK && secmatch >= 0 && + D->section != &D->sections[secmatch]) + D->status = DASM_S_MATCH_SEC|(D->section-D->sections); + return D->status; +} +#endif + diff --git a/external/lua/luajit/src/dynasm/dasm_ppc.lua b/external/lua/luajit/src/dynasm/dasm_ppc.lua new file mode 100644 index 0000000000..65da317b8b --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_ppc.lua @@ -0,0 +1,1249 @@ +------------------------------------------------------------------------------ +-- DynASM PPC module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- See dynasm.lua for full copyright notice. +------------------------------------------------------------------------------ + +-- Module information: +local _info = { + arch = "ppc", + description = "DynASM PPC module", + version = "1.3.0", + vernum = 10300, + release = "2011-05-05", + author = "Mike Pall", + license = "MIT", +} + +-- Exported glue functions for the arch-specific module. +local _M = { _info = _info } + +-- Cache library functions. +local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs +local assert, setmetatable = assert, setmetatable +local _s = string +local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char +local match, gmatch = _s.match, _s.gmatch +local concat, sort = table.concat, table.sort +local bit = bit or require("bit") +local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift +local tohex = bit.tohex + +-- Inherited tables and callbacks. +local g_opt, g_arch +local wline, werror, wfatal, wwarn + +-- Action name list. +-- CHECK: Keep this in sync with the C code! +local action_names = { + "STOP", "SECTION", "ESC", "REL_EXT", + "ALIGN", "REL_LG", "LABEL_LG", + "REL_PC", "LABEL_PC", "IMM", +} + +-- Maximum number of section buffer positions for dasm_put(). +-- CHECK: Keep this in sync with the C code! +local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. + +-- Action name -> action number. +local map_action = {} +for n,name in ipairs(action_names) do + map_action[name] = n-1 +end + +-- Action list buffer. +local actlist = {} + +-- Argument list for next dasm_put(). Start with offset 0 into action list. +local actargs = { 0 } + +-- Current number of section buffer positions for dasm_put(). +local secpos = 1 + +------------------------------------------------------------------------------ + +-- Dump action names and numbers. +local function dumpactions(out) + out:write("DynASM encoding engine action codes:\n") + for n,name in ipairs(action_names) do + local num = map_action[name] + out:write(format(" %-10s %02X %d\n", name, num, num)) + end + out:write("\n") +end + +-- Write action list buffer as a huge static C array. +local function writeactions(out, name) + local nn = #actlist + if nn == 0 then nn = 1; actlist[0] = map_action.STOP end + out:write("static const unsigned int ", name, "[", nn, "] = {\n") + for i = 1,nn-1 do + assert(out:write("0x", tohex(actlist[i]), ",\n")) + end + assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) +end + +------------------------------------------------------------------------------ + +-- Add word to action list. +local function wputxw(n) + assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") + actlist[#actlist+1] = n +end + +-- Add action to list with optional arg. Advance buffer pos, too. +local function waction(action, val, a, num) + local w = assert(map_action[action], "bad action name `"..action.."'") + wputxw(w * 0x10000 + (val or 0)) + if a then actargs[#actargs+1] = a end + if a or num then secpos = secpos + (num or 1) end +end + +-- Flush action list (intervening C code or buffer pos overflow). +local function wflush(term) + if #actlist == actargs[1] then return end -- Nothing to flush. + if not term then waction("STOP") end -- Terminate action list. + wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) + actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). + secpos = 1 -- The actionlist offset occupies a buffer position, too. +end + +-- Put escaped word. +local function wputw(n) + if n <= 0xffffff then waction("ESC") end + wputxw(n) +end + +-- Reserve position for word. +local function wpos() + local pos = #actlist+1 + actlist[pos] = "" + return pos +end + +-- Store word to reserved position. +local function wputpos(pos, n) + assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") + actlist[pos] = n +end + +------------------------------------------------------------------------------ + +-- Global label name -> global label number. With auto assignment on 1st use. +local next_global = 20 +local map_global = setmetatable({}, { __index = function(t, name) + if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end + local n = next_global + if n > 2047 then werror("too many global labels") end + next_global = n + 1 + t[name] = n + return n +end}) + +-- Dump global labels. +local function dumpglobals(out, lvl) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("Global labels:\n") + for i=20,next_global-1 do + out:write(format(" %s\n", t[i])) + end + out:write("\n") +end + +-- Write global label enum. +local function writeglobals(out, prefix) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("enum {\n") + for i=20,next_global-1 do + out:write(" ", prefix, t[i], ",\n") + end + out:write(" ", prefix, "_MAX\n};\n") +end + +-- Write global label names. +local function writeglobalnames(out, name) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("static const char *const ", name, "[] = {\n") + for i=20,next_global-1 do + out:write(" \"", t[i], "\",\n") + end + out:write(" (const char *)0\n};\n") +end + +------------------------------------------------------------------------------ + +-- Extern label name -> extern label number. With auto assignment on 1st use. +local next_extern = 0 +local map_extern_ = {} +local map_extern = setmetatable({}, { __index = function(t, name) + -- No restrictions on the name for now. + local n = next_extern + if n > 2047 then werror("too many extern labels") end + next_extern = n + 1 + t[name] = n + map_extern_[n] = name + return n +end}) + +-- Dump extern labels. +local function dumpexterns(out, lvl) + out:write("Extern labels:\n") + for i=0,next_extern-1 do + out:write(format(" %s\n", map_extern_[i])) + end + out:write("\n") +end + +-- Write extern label names. +local function writeexternnames(out, name) + out:write("static const char *const ", name, "[] = {\n") + for i=0,next_extern-1 do + out:write(" \"", map_extern_[i], "\",\n") + end + out:write(" (const char *)0\n};\n") +end + +------------------------------------------------------------------------------ + +-- Arch-specific maps. +local map_archdef = { sp = "r1" } -- Ext. register name -> int. name. + +local map_type = {} -- Type name -> { ctype, reg } +local ctypenum = 0 -- Type number (for Dt... macros). + +-- Reverse defines for registers. +function _M.revdef(s) + if s == "r1" then return "sp" end + return s +end + +local map_cond = { + lt = 0, gt = 1, eq = 2, so = 3, + ge = 4, le = 5, ne = 6, ns = 7, +} + +------------------------------------------------------------------------------ + +-- Template strings for PPC instructions. +local map_op = { + tdi_3 = "08000000ARI", + twi_3 = "0c000000ARI", + mulli_3 = "1c000000RRI", + subfic_3 = "20000000RRI", + cmplwi_3 = "28000000XRU", + cmplwi_2 = "28000000-RU", + cmpldi_3 = "28200000XRU", + cmpldi_2 = "28200000-RU", + cmpwi_3 = "2c000000XRI", + cmpwi_2 = "2c000000-RI", + cmpdi_3 = "2c200000XRI", + cmpdi_2 = "2c200000-RI", + addic_3 = "30000000RRI", + ["addic._3"] = "34000000RRI", + addi_3 = "38000000RR0I", + li_2 = "38000000RI", + la_2 = "38000000RD", + addis_3 = "3c000000RR0I", + lis_2 = "3c000000RI", + lus_2 = "3c000000RU", + bc_3 = "40000000AAK", + bcl_3 = "40000001AAK", + bdnz_1 = "42000000K", + bdz_1 = "42400000K", + sc_0 = "44000000", + b_1 = "48000000J", + bl_1 = "48000001J", + rlwimi_5 = "50000000RR~AAA.", + rlwinm_5 = "54000000RR~AAA.", + rlwnm_5 = "5c000000RR~RAA.", + ori_3 = "60000000RR~U", + nop_0 = "60000000", + oris_3 = "64000000RR~U", + xori_3 = "68000000RR~U", + xoris_3 = "6c000000RR~U", + ["andi._3"] = "70000000RR~U", + ["andis._3"] = "74000000RR~U", + lwz_2 = "80000000RD", + lwzu_2 = "84000000RD", + lbz_2 = "88000000RD", + lbzu_2 = "8c000000RD", + stw_2 = "90000000RD", + stwu_2 = "94000000RD", + stb_2 = "98000000RD", + stbu_2 = "9c000000RD", + lhz_2 = "a0000000RD", + lhzu_2 = "a4000000RD", + lha_2 = "a8000000RD", + lhau_2 = "ac000000RD", + sth_2 = "b0000000RD", + sthu_2 = "b4000000RD", + lmw_2 = "b8000000RD", + stmw_2 = "bc000000RD", + lfs_2 = "c0000000FD", + lfsu_2 = "c4000000FD", + lfd_2 = "c8000000FD", + lfdu_2 = "cc000000FD", + stfs_2 = "d0000000FD", + stfsu_2 = "d4000000FD", + stfd_2 = "d8000000FD", + stfdu_2 = "dc000000FD", + ld_2 = "e8000000RD", -- NYI: displacement must be divisible by 4. + ldu_2 = "e8000001RD", + lwa_2 = "e8000002RD", + std_2 = "f8000000RD", + stdu_2 = "f8000001RD", + + -- Primary opcode 19: + mcrf_2 = "4c000000XX", + isync_0 = "4c00012c", + crnor_3 = "4c000042CCC", + crnot_2 = "4c000042CC=", + crandc_3 = "4c000102CCC", + crxor_3 = "4c000182CCC", + crclr_1 = "4c000182C==", + crnand_3 = "4c0001c2CCC", + crand_3 = "4c000202CCC", + creqv_3 = "4c000242CCC", + crset_1 = "4c000242C==", + crorc_3 = "4c000342CCC", + cror_3 = "4c000382CCC", + crmove_2 = "4c000382CC=", + bclr_2 = "4c000020AA", + bclrl_2 = "4c000021AA", + bcctr_2 = "4c000420AA", + bcctrl_2 = "4c000421AA", + blr_0 = "4e800020", + blrl_0 = "4e800021", + bctr_0 = "4e800420", + bctrl_0 = "4e800421", + + -- Primary opcode 31: + cmpw_3 = "7c000000XRR", + cmpw_2 = "7c000000-RR", + cmpd_3 = "7c200000XRR", + cmpd_2 = "7c200000-RR", + tw_3 = "7c000008ARR", + subfc_3 = "7c000010RRR.", + subc_3 = "7c000010RRR~.", + mulhdu_3 = "7c000012RRR.", + addc_3 = "7c000014RRR.", + mulhwu_3 = "7c000016RRR.", + isel_4 = "7c00001eRRRC", + isellt_3 = "7c00001eRRR", + iselgt_3 = "7c00005eRRR", + iseleq_3 = "7c00009eRRR", + mfcr_1 = "7c000026R", + mfocrf_2 = "7c100026RG", + mtcrf_2 = "7c000120GR", + mtocrf_2 = "7c100120GR", + lwarx_3 = "7c000028RR0R", + ldx_3 = "7c00002aRR0R", + lwzx_3 = "7c00002eRR0R", + slw_3 = "7c000030RR~R.", + cntlzw_2 = "7c000034RR~", + sld_3 = "7c000036RR~R.", + and_3 = "7c000038RR~R.", + cmplw_3 = "7c000040XRR", + cmplw_2 = "7c000040-RR", + cmpld_3 = "7c200040XRR", + cmpld_2 = "7c200040-RR", + subf_3 = "7c000050RRR.", + sub_3 = "7c000050RRR~.", + ldux_3 = "7c00006aRR0R", + dcbst_2 = "7c00006c-RR", + lwzux_3 = "7c00006eRR0R", + cntlzd_2 = "7c000074RR~", + andc_3 = "7c000078RR~R.", + td_3 = "7c000088ARR", + mulhd_3 = "7c000092RRR.", + mulhw_3 = "7c000096RRR.", + ldarx_3 = "7c0000a8RR0R", + dcbf_2 = "7c0000ac-RR", + lbzx_3 = "7c0000aeRR0R", + neg_2 = "7c0000d0RR.", + lbzux_3 = "7c0000eeRR0R", + popcntb_2 = "7c0000f4RR~", + not_2 = "7c0000f8RR~%.", + nor_3 = "7c0000f8RR~R.", + subfe_3 = "7c000110RRR.", + sube_3 = "7c000110RRR~.", + adde_3 = "7c000114RRR.", + stdx_3 = "7c00012aRR0R", + stwcx_3 = "7c00012cRR0R.", + stwx_3 = "7c00012eRR0R", + prtyw_2 = "7c000134RR~", + stdux_3 = "7c00016aRR0R", + stwux_3 = "7c00016eRR0R", + prtyd_2 = "7c000174RR~", + subfze_2 = "7c000190RR.", + addze_2 = "7c000194RR.", + stdcx_3 = "7c0001acRR0R.", + stbx_3 = "7c0001aeRR0R", + subfme_2 = "7c0001d0RR.", + mulld_3 = "7c0001d2RRR.", + addme_2 = "7c0001d4RR.", + mullw_3 = "7c0001d6RRR.", + dcbtst_2 = "7c0001ec-RR", + stbux_3 = "7c0001eeRR0R", + add_3 = "7c000214RRR.", + dcbt_2 = "7c00022c-RR", + lhzx_3 = "7c00022eRR0R", + eqv_3 = "7c000238RR~R.", + eciwx_3 = "7c00026cRR0R", + lhzux_3 = "7c00026eRR0R", + xor_3 = "7c000278RR~R.", + mfspefscr_1 = "7c0082a6R", + mfxer_1 = "7c0102a6R", + mflr_1 = "7c0802a6R", + mfctr_1 = "7c0902a6R", + lwax_3 = "7c0002aaRR0R", + lhax_3 = "7c0002aeRR0R", + mftb_1 = "7c0c42e6R", + mftbu_1 = "7c0d42e6R", + lwaux_3 = "7c0002eaRR0R", + lhaux_3 = "7c0002eeRR0R", + sthx_3 = "7c00032eRR0R", + orc_3 = "7c000338RR~R.", + ecowx_3 = "7c00036cRR0R", + sthux_3 = "7c00036eRR0R", + or_3 = "7c000378RR~R.", + mr_2 = "7c000378RR~%.", + divdu_3 = "7c000392RRR.", + divwu_3 = "7c000396RRR.", + mtspefscr_1 = "7c0083a6R", + mtxer_1 = "7c0103a6R", + mtlr_1 = "7c0803a6R", + mtctr_1 = "7c0903a6R", + dcbi_2 = "7c0003ac-RR", + nand_3 = "7c0003b8RR~R.", + divd_3 = "7c0003d2RRR.", + divw_3 = "7c0003d6RRR.", + cmpb_3 = "7c0003f8RR~R.", + mcrxr_1 = "7c000400X", + subfco_3 = "7c000410RRR.", + subco_3 = "7c000410RRR~.", + addco_3 = "7c000414RRR.", + ldbrx_3 = "7c000428RR0R", + lswx_3 = "7c00042aRR0R", + lwbrx_3 = "7c00042cRR0R", + lfsx_3 = "7c00042eFR0R", + srw_3 = "7c000430RR~R.", + srd_3 = "7c000436RR~R.", + subfo_3 = "7c000450RRR.", + subo_3 = "7c000450RRR~.", + lfsux_3 = "7c00046eFR0R", + lswi_3 = "7c0004aaRR0A", + sync_0 = "7c0004ac", + lwsync_0 = "7c2004ac", + ptesync_0 = "7c4004ac", + lfdx_3 = "7c0004aeFR0R", + nego_2 = "7c0004d0RR.", + lfdux_3 = "7c0004eeFR0R", + subfeo_3 = "7c000510RRR.", + subeo_3 = "7c000510RRR~.", + addeo_3 = "7c000514RRR.", + stdbrx_3 = "7c000528RR0R", + stswx_3 = "7c00052aRR0R", + stwbrx_3 = "7c00052cRR0R", + stfsx_3 = "7c00052eFR0R", + stfsux_3 = "7c00056eFR0R", + subfzeo_2 = "7c000590RR.", + addzeo_2 = "7c000594RR.", + stswi_3 = "7c0005aaRR0A", + stfdx_3 = "7c0005aeFR0R", + subfmeo_2 = "7c0005d0RR.", + mulldo_3 = "7c0005d2RRR.", + addmeo_2 = "7c0005d4RR.", + mullwo_3 = "7c0005d6RRR.", + dcba_2 = "7c0005ec-RR", + stfdux_3 = "7c0005eeFR0R", + addo_3 = "7c000614RRR.", + lhbrx_3 = "7c00062cRR0R", + sraw_3 = "7c000630RR~R.", + srad_3 = "7c000634RR~R.", + srawi_3 = "7c000670RR~A.", + sradi_3 = "7c000674RR~H.", + eieio_0 = "7c0006ac", + lfiwax_3 = "7c0006aeFR0R", + sthbrx_3 = "7c00072cRR0R", + extsh_2 = "7c000734RR~.", + extsb_2 = "7c000774RR~.", + divduo_3 = "7c000792RRR.", + divwou_3 = "7c000796RRR.", + icbi_2 = "7c0007ac-RR", + stfiwx_3 = "7c0007aeFR0R", + extsw_2 = "7c0007b4RR~.", + divdo_3 = "7c0007d2RRR.", + divwo_3 = "7c0007d6RRR.", + dcbz_2 = "7c0007ec-RR", + + -- Primary opcode 30: + rldicl_4 = "78000000RR~HM.", + rldicr_4 = "78000004RR~HM.", + rldic_4 = "78000008RR~HM.", + rldimi_4 = "7800000cRR~HM.", + rldcl_4 = "78000010RR~RM.", + rldcr_4 = "78000012RR~RM.", + + -- Primary opcode 59: + fdivs_3 = "ec000024FFF.", + fsubs_3 = "ec000028FFF.", + fadds_3 = "ec00002aFFF.", + fsqrts_2 = "ec00002cF-F.", + fres_2 = "ec000030F-F.", + fmuls_3 = "ec000032FF-F.", + frsqrtes_2 = "ec000034F-F.", + fmsubs_4 = "ec000038FFFF~.", + fmadds_4 = "ec00003aFFFF~.", + fnmsubs_4 = "ec00003cFFFF~.", + fnmadds_4 = "ec00003eFFFF~.", + + -- Primary opcode 63: + fdiv_3 = "fc000024FFF.", + fsub_3 = "fc000028FFF.", + fadd_3 = "fc00002aFFF.", + fsqrt_2 = "fc00002cF-F.", + fsel_4 = "fc00002eFFFF~.", + fre_2 = "fc000030F-F.", + fmul_3 = "fc000032FF-F.", + frsqrte_2 = "fc000034F-F.", + fmsub_4 = "fc000038FFFF~.", + fmadd_4 = "fc00003aFFFF~.", + fnmsub_4 = "fc00003cFFFF~.", + fnmadd_4 = "fc00003eFFFF~.", + fcmpu_3 = "fc000000XFF", + fcpsgn_3 = "fc000010FFF.", + fcmpo_3 = "fc000040XFF", + mtfsb1_1 = "fc00004cA", + fneg_2 = "fc000050F-F.", + mcrfs_2 = "fc000080XX", + mtfsb0_1 = "fc00008cA", + fmr_2 = "fc000090F-F.", + frsp_2 = "fc000018F-F.", + fctiw_2 = "fc00001cF-F.", + fctiwz_2 = "fc00001eF-F.", + mtfsfi_2 = "fc00010cAA", -- NYI: upshift. + fnabs_2 = "fc000110F-F.", + fabs_2 = "fc000210F-F.", + frin_2 = "fc000310F-F.", + friz_2 = "fc000350F-F.", + frip_2 = "fc000390F-F.", + frim_2 = "fc0003d0F-F.", + mffs_1 = "fc00048eF.", + -- NYI: mtfsf, mtfsb0, mtfsb1. + fctid_2 = "fc00065cF-F.", + fctidz_2 = "fc00065eF-F.", + fcfid_2 = "fc00069cF-F.", + + -- Primary opcode 4, SPE APU extension: + evaddw_3 = "10000200RRR", + evaddiw_3 = "10000202RAR~", + evsubw_3 = "10000204RRR~", + evsubiw_3 = "10000206RAR~", + evabs_2 = "10000208RR", + evneg_2 = "10000209RR", + evextsb_2 = "1000020aRR", + evextsh_2 = "1000020bRR", + evrndw_2 = "1000020cRR", + evcntlzw_2 = "1000020dRR", + evcntlsw_2 = "1000020eRR", + brinc_3 = "1000020fRRR", + evand_3 = "10000211RRR", + evandc_3 = "10000212RRR", + evxor_3 = "10000216RRR", + evor_3 = "10000217RRR", + evmr_2 = "10000217RR=", + evnor_3 = "10000218RRR", + evnot_2 = "10000218RR=", + eveqv_3 = "10000219RRR", + evorc_3 = "1000021bRRR", + evnand_3 = "1000021eRRR", + evsrwu_3 = "10000220RRR", + evsrws_3 = "10000221RRR", + evsrwiu_3 = "10000222RRA", + evsrwis_3 = "10000223RRA", + evslw_3 = "10000224RRR", + evslwi_3 = "10000226RRA", + evrlw_3 = "10000228RRR", + evsplati_2 = "10000229RS", + evrlwi_3 = "1000022aRRA", + evsplatfi_2 = "1000022bRS", + evmergehi_3 = "1000022cRRR", + evmergelo_3 = "1000022dRRR", + evcmpgtu_3 = "10000230XRR", + evcmpgtu_2 = "10000230-RR", + evcmpgts_3 = "10000231XRR", + evcmpgts_2 = "10000231-RR", + evcmpltu_3 = "10000232XRR", + evcmpltu_2 = "10000232-RR", + evcmplts_3 = "10000233XRR", + evcmplts_2 = "10000233-RR", + evcmpeq_3 = "10000234XRR", + evcmpeq_2 = "10000234-RR", + evsel_4 = "10000278RRRW", + evsel_3 = "10000278RRR", + evfsadd_3 = "10000280RRR", + evfssub_3 = "10000281RRR", + evfsabs_2 = "10000284RR", + evfsnabs_2 = "10000285RR", + evfsneg_2 = "10000286RR", + evfsmul_3 = "10000288RRR", + evfsdiv_3 = "10000289RRR", + evfscmpgt_3 = "1000028cXRR", + evfscmpgt_2 = "1000028c-RR", + evfscmplt_3 = "1000028dXRR", + evfscmplt_2 = "1000028d-RR", + evfscmpeq_3 = "1000028eXRR", + evfscmpeq_2 = "1000028e-RR", + evfscfui_2 = "10000290R-R", + evfscfsi_2 = "10000291R-R", + evfscfuf_2 = "10000292R-R", + evfscfsf_2 = "10000293R-R", + evfsctui_2 = "10000294R-R", + evfsctsi_2 = "10000295R-R", + evfsctuf_2 = "10000296R-R", + evfsctsf_2 = "10000297R-R", + evfsctuiz_2 = "10000298R-R", + evfsctsiz_2 = "1000029aR-R", + evfststgt_3 = "1000029cXRR", + evfststgt_2 = "1000029c-RR", + evfststlt_3 = "1000029dXRR", + evfststlt_2 = "1000029d-RR", + evfststeq_3 = "1000029eXRR", + evfststeq_2 = "1000029e-RR", + efsadd_3 = "100002c0RRR", + efssub_3 = "100002c1RRR", + efsabs_2 = "100002c4RR", + efsnabs_2 = "100002c5RR", + efsneg_2 = "100002c6RR", + efsmul_3 = "100002c8RRR", + efsdiv_3 = "100002c9RRR", + efscmpgt_3 = "100002ccXRR", + efscmpgt_2 = "100002cc-RR", + efscmplt_3 = "100002cdXRR", + efscmplt_2 = "100002cd-RR", + efscmpeq_3 = "100002ceXRR", + efscmpeq_2 = "100002ce-RR", + efscfd_2 = "100002cfR-R", + efscfui_2 = "100002d0R-R", + efscfsi_2 = "100002d1R-R", + efscfuf_2 = "100002d2R-R", + efscfsf_2 = "100002d3R-R", + efsctui_2 = "100002d4R-R", + efsctsi_2 = "100002d5R-R", + efsctuf_2 = "100002d6R-R", + efsctsf_2 = "100002d7R-R", + efsctuiz_2 = "100002d8R-R", + efsctsiz_2 = "100002daR-R", + efststgt_3 = "100002dcXRR", + efststgt_2 = "100002dc-RR", + efststlt_3 = "100002ddXRR", + efststlt_2 = "100002dd-RR", + efststeq_3 = "100002deXRR", + efststeq_2 = "100002de-RR", + efdadd_3 = "100002e0RRR", + efdsub_3 = "100002e1RRR", + efdcfuid_2 = "100002e2R-R", + efdcfsid_2 = "100002e3R-R", + efdabs_2 = "100002e4RR", + efdnabs_2 = "100002e5RR", + efdneg_2 = "100002e6RR", + efdmul_3 = "100002e8RRR", + efddiv_3 = "100002e9RRR", + efdctuidz_2 = "100002eaR-R", + efdctsidz_2 = "100002ebR-R", + efdcmpgt_3 = "100002ecXRR", + efdcmpgt_2 = "100002ec-RR", + efdcmplt_3 = "100002edXRR", + efdcmplt_2 = "100002ed-RR", + efdcmpeq_3 = "100002eeXRR", + efdcmpeq_2 = "100002ee-RR", + efdcfs_2 = "100002efR-R", + efdcfui_2 = "100002f0R-R", + efdcfsi_2 = "100002f1R-R", + efdcfuf_2 = "100002f2R-R", + efdcfsf_2 = "100002f3R-R", + efdctui_2 = "100002f4R-R", + efdctsi_2 = "100002f5R-R", + efdctuf_2 = "100002f6R-R", + efdctsf_2 = "100002f7R-R", + efdctuiz_2 = "100002f8R-R", + efdctsiz_2 = "100002faR-R", + efdtstgt_3 = "100002fcXRR", + efdtstgt_2 = "100002fc-RR", + efdtstlt_3 = "100002fdXRR", + efdtstlt_2 = "100002fd-RR", + efdtsteq_3 = "100002feXRR", + efdtsteq_2 = "100002fe-RR", + evlddx_3 = "10000300RR0R", + evldd_2 = "10000301R8", + evldwx_3 = "10000302RR0R", + evldw_2 = "10000303R8", + evldhx_3 = "10000304RR0R", + evldh_2 = "10000305R8", + evlwhex_3 = "10000310RR0R", + evlwhe_2 = "10000311R4", + evlwhoux_3 = "10000314RR0R", + evlwhou_2 = "10000315R4", + evlwhosx_3 = "10000316RR0R", + evlwhos_2 = "10000317R4", + evstddx_3 = "10000320RR0R", + evstdd_2 = "10000321R8", + evstdwx_3 = "10000322RR0R", + evstdw_2 = "10000323R8", + evstdhx_3 = "10000324RR0R", + evstdh_2 = "10000325R8", + evstwhex_3 = "10000330RR0R", + evstwhe_2 = "10000331R4", + evstwhox_3 = "10000334RR0R", + evstwho_2 = "10000335R4", + evstwwex_3 = "10000338RR0R", + evstwwe_2 = "10000339R4", + evstwwox_3 = "1000033cRR0R", + evstwwo_2 = "1000033dR4", + evmhessf_3 = "10000403RRR", + evmhossf_3 = "10000407RRR", + evmheumi_3 = "10000408RRR", + evmhesmi_3 = "10000409RRR", + evmhesmf_3 = "1000040bRRR", + evmhoumi_3 = "1000040cRRR", + evmhosmi_3 = "1000040dRRR", + evmhosmf_3 = "1000040fRRR", + evmhessfa_3 = "10000423RRR", + evmhossfa_3 = "10000427RRR", + evmheumia_3 = "10000428RRR", + evmhesmia_3 = "10000429RRR", + evmhesmfa_3 = "1000042bRRR", + evmhoumia_3 = "1000042cRRR", + evmhosmia_3 = "1000042dRRR", + evmhosmfa_3 = "1000042fRRR", + evmwhssf_3 = "10000447RRR", + evmwlumi_3 = "10000448RRR", + evmwhumi_3 = "1000044cRRR", + evmwhsmi_3 = "1000044dRRR", + evmwhsmf_3 = "1000044fRRR", + evmwssf_3 = "10000453RRR", + evmwumi_3 = "10000458RRR", + evmwsmi_3 = "10000459RRR", + evmwsmf_3 = "1000045bRRR", + evmwhssfa_3 = "10000467RRR", + evmwlumia_3 = "10000468RRR", + evmwhumia_3 = "1000046cRRR", + evmwhsmia_3 = "1000046dRRR", + evmwhsmfa_3 = "1000046fRRR", + evmwssfa_3 = "10000473RRR", + evmwumia_3 = "10000478RRR", + evmwsmia_3 = "10000479RRR", + evmwsmfa_3 = "1000047bRRR", + evmra_2 = "100004c4RR", + evdivws_3 = "100004c6RRR", + evdivwu_3 = "100004c7RRR", + evmwssfaa_3 = "10000553RRR", + evmwumiaa_3 = "10000558RRR", + evmwsmiaa_3 = "10000559RRR", + evmwsmfaa_3 = "1000055bRRR", + evmwssfan_3 = "100005d3RRR", + evmwumian_3 = "100005d8RRR", + evmwsmian_3 = "100005d9RRR", + evmwsmfan_3 = "100005dbRRR", + evmergehilo_3 = "1000022eRRR", + evmergelohi_3 = "1000022fRRR", + evlhhesplatx_3 = "10000308RR0R", + evlhhesplat_2 = "10000309R2", + evlhhousplatx_3 = "1000030cRR0R", + evlhhousplat_2 = "1000030dR2", + evlhhossplatx_3 = "1000030eRR0R", + evlhhossplat_2 = "1000030fR2", + evlwwsplatx_3 = "10000318RR0R", + evlwwsplat_2 = "10000319R4", + evlwhsplatx_3 = "1000031cRR0R", + evlwhsplat_2 = "1000031dR4", + evaddusiaaw_2 = "100004c0RR", + evaddssiaaw_2 = "100004c1RR", + evsubfusiaaw_2 = "100004c2RR", + evsubfssiaaw_2 = "100004c3RR", + evaddumiaaw_2 = "100004c8RR", + evaddsmiaaw_2 = "100004c9RR", + evsubfumiaaw_2 = "100004caRR", + evsubfsmiaaw_2 = "100004cbRR", + evmheusiaaw_3 = "10000500RRR", + evmhessiaaw_3 = "10000501RRR", + evmhessfaaw_3 = "10000503RRR", + evmhousiaaw_3 = "10000504RRR", + evmhossiaaw_3 = "10000505RRR", + evmhossfaaw_3 = "10000507RRR", + evmheumiaaw_3 = "10000508RRR", + evmhesmiaaw_3 = "10000509RRR", + evmhesmfaaw_3 = "1000050bRRR", + evmhoumiaaw_3 = "1000050cRRR", + evmhosmiaaw_3 = "1000050dRRR", + evmhosmfaaw_3 = "1000050fRRR", + evmhegumiaa_3 = "10000528RRR", + evmhegsmiaa_3 = "10000529RRR", + evmhegsmfaa_3 = "1000052bRRR", + evmhogumiaa_3 = "1000052cRRR", + evmhogsmiaa_3 = "1000052dRRR", + evmhogsmfaa_3 = "1000052fRRR", + evmwlusiaaw_3 = "10000540RRR", + evmwlssiaaw_3 = "10000541RRR", + evmwlumiaaw_3 = "10000548RRR", + evmwlsmiaaw_3 = "10000549RRR", + evmheusianw_3 = "10000580RRR", + evmhessianw_3 = "10000581RRR", + evmhessfanw_3 = "10000583RRR", + evmhousianw_3 = "10000584RRR", + evmhossianw_3 = "10000585RRR", + evmhossfanw_3 = "10000587RRR", + evmheumianw_3 = "10000588RRR", + evmhesmianw_3 = "10000589RRR", + evmhesmfanw_3 = "1000058bRRR", + evmhoumianw_3 = "1000058cRRR", + evmhosmianw_3 = "1000058dRRR", + evmhosmfanw_3 = "1000058fRRR", + evmhegumian_3 = "100005a8RRR", + evmhegsmian_3 = "100005a9RRR", + evmhegsmfan_3 = "100005abRRR", + evmhogumian_3 = "100005acRRR", + evmhogsmian_3 = "100005adRRR", + evmhogsmfan_3 = "100005afRRR", + evmwlusianw_3 = "100005c0RRR", + evmwlssianw_3 = "100005c1RRR", + evmwlumianw_3 = "100005c8RRR", + evmwlsmianw_3 = "100005c9RRR", + + -- NYI: Book E instructions. +} + +-- Add mnemonics for "." variants. +do + local t = {} + for k,v in pairs(map_op) do + if sub(v, -1) == "." then + local v2 = sub(v, 1, 7)..char(byte(v, 8)+1)..sub(v, 9, -2) + t[sub(k, 1, -3).."."..sub(k, -2)] = v2 + end + end + for k,v in pairs(t) do + map_op[k] = v + end +end + +-- Add more branch mnemonics. +for cond,c in pairs(map_cond) do + local b1 = "b"..cond + local c1 = shl(band(c, 3), 16) + (c < 4 and 0x01000000 or 0) + -- bX[l] + map_op[b1.."_1"] = tohex(0x40800000 + c1).."K" + map_op[b1.."y_1"] = tohex(0x40a00000 + c1).."K" + map_op[b1.."l_1"] = tohex(0x40800001 + c1).."K" + map_op[b1.."_2"] = tohex(0x40800000 + c1).."-XK" + map_op[b1.."y_2"] = tohex(0x40a00000 + c1).."-XK" + map_op[b1.."l_2"] = tohex(0x40800001 + c1).."-XK" + -- bXlr[l] + map_op[b1.."lr_0"] = tohex(0x4c800020 + c1) + map_op[b1.."lrl_0"] = tohex(0x4c800021 + c1) + map_op[b1.."ctr_0"] = tohex(0x4c800420 + c1) + map_op[b1.."ctrl_0"] = tohex(0x4c800421 + c1) + -- bXctr[l] + map_op[b1.."lr_1"] = tohex(0x4c800020 + c1).."-X" + map_op[b1.."lrl_1"] = tohex(0x4c800021 + c1).."-X" + map_op[b1.."ctr_1"] = tohex(0x4c800420 + c1).."-X" + map_op[b1.."ctrl_1"] = tohex(0x4c800421 + c1).."-X" +end + +------------------------------------------------------------------------------ + +local function parse_gpr(expr) + local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") + local tp = map_type[tname or expr] + if tp then + local reg = ovreg or tp.reg + if not reg then + werror("type `"..(tname or expr).."' needs a register override") + end + expr = reg + end + local r = match(expr, "^r([1-3]?[0-9])$") + if r then + r = tonumber(r) + if r <= 31 then return r, tp end + end + werror("bad register name `"..expr.."'") +end + +local function parse_fpr(expr) + local r = match(expr, "^f([1-3]?[0-9])$") + if r then + r = tonumber(r) + if r <= 31 then return r end + end + werror("bad register name `"..expr.."'") +end + +local function parse_cr(expr) + local r = match(expr, "^cr([0-7])$") + if r then return tonumber(r) end + werror("bad condition register name `"..expr.."'") +end + +local function parse_cond(expr) + local r, cond = match(expr, "^4%*cr([0-7])%+(%w%w)$") + if r then + r = tonumber(r) + local c = map_cond[cond] + if c and c < 4 then return r*4+c end + end + werror("bad condition bit name `"..expr.."'") +end + +local function parse_imm(imm, bits, shift, scale, signed) + local n = tonumber(imm) + if n then + local m = sar(n, scale) + if shl(m, scale) == n then + if signed then + local s = sar(m, bits-1) + if s == 0 then return shl(m, shift) + elseif s == -1 then return shl(m + shl(1, bits), shift) end + else + if sar(m, bits) == 0 then return shl(m, shift) end + end + end + werror("out of range immediate `"..imm.."'") + elseif match(imm, "^r([1-3]?[0-9])$") or + match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then + werror("expected immediate operand, got register") + else + waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) + return 0 + end +end + +local function parse_shiftmask(imm, isshift) + local n = tonumber(imm) + if n then + if shr(n, 6) == 0 then + local lsb = band(imm, 31) + local msb = imm - lsb + return isshift and (shl(lsb, 11)+shr(msb, 4)) or (shl(lsb, 6)+msb) + end + werror("out of range immediate `"..imm.."'") + elseif match(imm, "^r([1-3]?[0-9])$") or + match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then + werror("expected immediate operand, got register") + else + werror("NYI: parameterized 64 bit shift/mask") + end +end + +local function parse_disp(disp) + local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") + if imm then + local r = parse_gpr(reg) + if r == 0 then werror("cannot use r0 in displacement") end + return shl(r, 16) + parse_imm(imm, 16, 0, 0, true) + end + local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") + if reg and tailr ~= "" then + local r, tp = parse_gpr(reg) + if r == 0 then werror("cannot use r0 in displacement") end + if tp then + waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) + return shl(r, 16) + end + end + werror("bad displacement `"..disp.."'") +end + +local function parse_u5disp(disp, scale) + local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") + if imm then + local r = parse_gpr(reg) + if r == 0 then werror("cannot use r0 in displacement") end + return shl(r, 16) + parse_imm(imm, 5, 11, scale, false) + end + local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") + if reg and tailr ~= "" then + local r, tp = parse_gpr(reg) + if r == 0 then werror("cannot use r0 in displacement") end + if tp then + waction("IMM", scale*1024+5*32+11, format(tp.ctypefmt, tailr)) + return shl(r, 16) + end + end + werror("bad displacement `"..disp.."'") +end + +local function parse_label(label, def) + local prefix = sub(label, 1, 2) + -- =>label (pc label reference) + if prefix == "=>" then + return "PC", 0, sub(label, 3) + end + -- ->name (global label reference) + if prefix == "->" then + return "LG", map_global[sub(label, 3)] + end + if def then + -- [1-9] (local label definition) + if match(label, "^[1-9]$") then + return "LG", 10+tonumber(label) + end + else + -- [<>][1-9] (local label reference) + local dir, lnum = match(label, "^([<>])([1-9])$") + if dir then -- Fwd: 1-9, Bkwd: 11-19. + return "LG", lnum + (dir == ">" and 0 or 10) + end + -- extern label (extern label reference) + local extname = match(label, "^extern%s+(%S+)$") + if extname then + return "EXT", map_extern[extname] + end + end + werror("bad label `"..label.."'") +end + +------------------------------------------------------------------------------ + +-- Handle opcodes defined with template strings. +map_op[".template__"] = function(params, template, nparams) + if not params then return sub(template, 9) end + local op = tonumber(sub(template, 1, 8), 16) + local n, rs = 1, 26 + + -- Limit number of section buffer positions used by a single dasm_put(). + -- A single opcode needs a maximum of 3 positions (rlwinm). + if secpos+3 > maxsecpos then wflush() end + local pos = wpos() + + -- Process each character. + for p in gmatch(sub(template, 9), ".") do + if p == "R" then + rs = rs - 5; op = op + shl(parse_gpr(params[n]), rs); n = n + 1 + elseif p == "F" then + rs = rs - 5; op = op + shl(parse_fpr(params[n]), rs); n = n + 1 + elseif p == "A" then + rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1 + elseif p == "S" then + rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, true); n = n + 1 + elseif p == "I" then + op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 + elseif p == "U" then + op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 + elseif p == "D" then + op = op + parse_disp(params[n]); n = n + 1 + elseif p == "2" then + op = op + parse_u5disp(params[n], 1); n = n + 1 + elseif p == "4" then + op = op + parse_u5disp(params[n], 2); n = n + 1 + elseif p == "8" then + op = op + parse_u5disp(params[n], 3); n = n + 1 + elseif p == "C" then + rs = rs - 5; op = op + shl(parse_cond(params[n]), rs); n = n + 1 + elseif p == "X" then + rs = rs - 5; op = op + shl(parse_cr(params[n]), rs+2); n = n + 1 + elseif p == "W" then + op = op + parse_cr(params[n]); n = n + 1 + elseif p == "G" then + op = op + parse_imm(params[n], 8, 12, 0, false); n = n + 1 + elseif p == "H" then + op = op + parse_shiftmask(params[n], true); n = n + 1 + elseif p == "M" then + op = op + parse_shiftmask(params[n], false); n = n + 1 + elseif p == "J" or p == "K" then + local mode, n, s = parse_label(params[n], false) + if p == "K" then n = n + 2048 end + waction("REL_"..mode, n, s, 1) + n = n + 1 + elseif p == "0" then + if band(shr(op, rs), 31) == 0 then werror("cannot use r0") end + elseif p == "=" or p == "%" then + local t = band(shr(op, p == "%" and rs+5 or rs), 31) + rs = rs - 5 + op = op + shl(t, rs) + elseif p == "~" then + local mm = shl(31, rs) + local lo = band(op, mm) + local hi = band(op, shl(mm, 5)) + op = op - lo - hi + shl(lo, 5) + shr(hi, 5) + elseif p == "-" then + rs = rs - 5 + elseif p == "." then + -- Ignored. + else + assert(false) + end + end + wputpos(pos, op) +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcode to mark the position where the action list is to be emitted. +map_op[".actionlist_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeactions(out, name) end) +end + +-- Pseudo-opcode to mark the position where the global enum is to be emitted. +map_op[".globals_1"] = function(params) + if not params then return "prefix" end + local prefix = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeglobals(out, prefix) end) +end + +-- Pseudo-opcode to mark the position where the global names are to be emitted. +map_op[".globalnames_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeglobalnames(out, name) end) +end + +-- Pseudo-opcode to mark the position where the extern names are to be emitted. +map_op[".externnames_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeexternnames(out, name) end) +end + +------------------------------------------------------------------------------ + +-- Label pseudo-opcode (converted from trailing colon form). +map_op[".label_1"] = function(params) + if not params then return "[1-9] | ->global | =>pcexpr" end + if secpos+1 > maxsecpos then wflush() end + local mode, n, s = parse_label(params[1], true) + if mode == "EXT" then werror("bad label definition") end + waction("LABEL_"..mode, n, s, 1) +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcodes for data storage. +map_op[".long_*"] = function(params) + if not params then return "imm..." end + for _,p in ipairs(params) do + local n = tonumber(p) + if not n then werror("bad immediate `"..p.."'") end + if n < 0 then n = n + 2^32 end + wputw(n) + if secpos+2 > maxsecpos then wflush() end + end +end + +-- Alignment pseudo-opcode. +map_op[".align_1"] = function(params) + if not params then return "numpow2" end + if secpos+1 > maxsecpos then wflush() end + local align = tonumber(params[1]) + if align then + local x = align + -- Must be a power of 2 in the range (2 ... 256). + for i=1,8 do + x = x / 2 + if x == 1 then + waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. + return + end + end + end + werror("bad alignment") +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcode for (primitive) type definitions (map to C types). +map_op[".type_3"] = function(params, nparams) + if not params then + return nparams == 2 and "name, ctype" or "name, ctype, reg" + end + local name, ctype, reg = params[1], params[2], params[3] + if not match(name, "^[%a_][%w_]*$") then + werror("bad type name `"..name.."'") + end + local tp = map_type[name] + if tp then + werror("duplicate type `"..name.."'") + end + -- Add #type to defines. A bit unclean to put it in map_archdef. + map_archdef["#"..name] = "sizeof("..ctype..")" + -- Add new type and emit shortcut define. + local num = ctypenum + 1 + map_type[name] = { + ctype = ctype, + ctypefmt = format("Dt%X(%%s)", num), + reg = reg, + } + wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) + ctypenum = num +end +map_op[".type_2"] = map_op[".type_3"] + +-- Dump type definitions. +local function dumptypes(out, lvl) + local t = {} + for name in pairs(map_type) do t[#t+1] = name end + sort(t) + out:write("Type definitions:\n") + for _,name in ipairs(t) do + local tp = map_type[name] + local reg = tp.reg or "" + out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) + end + out:write("\n") +end + +------------------------------------------------------------------------------ + +-- Set the current section. +function _M.section(num) + waction("SECTION", num) + wflush(true) -- SECTION is a terminal action. +end + +------------------------------------------------------------------------------ + +-- Dump architecture description. +function _M.dumparch(out) + out:write(format("DynASM %s version %s, released %s\n\n", + _info.arch, _info.version, _info.release)) + dumpactions(out) +end + +-- Dump all user defined elements. +function _M.dumpdef(out, lvl) + dumptypes(out, lvl) + dumpglobals(out, lvl) + dumpexterns(out, lvl) +end + +------------------------------------------------------------------------------ + +-- Pass callbacks from/to the DynASM core. +function _M.passcb(wl, we, wf, ww) + wline, werror, wfatal, wwarn = wl, we, wf, ww + return wflush +end + +-- Setup the arch-specific module. +function _M.setup(arch, opt) + g_arch, g_opt = arch, opt +end + +-- Merge the core maps and the arch-specific maps. +function _M.mergemaps(map_coreop, map_def) + setmetatable(map_op, { __index = map_coreop }) + setmetatable(map_def, { __index = map_archdef }) + return map_op, map_def +end + +return _M + +------------------------------------------------------------------------------ + diff --git a/external/lua/luajit/src/dynasm/dasm_proto.h b/external/lua/luajit/src/dynasm/dasm_proto.h new file mode 100644 index 0000000000..960ddb72ec --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_proto.h @@ -0,0 +1,83 @@ +/* +** DynASM encoding engine prototypes. +** Copyright (C) 2005-2013 Mike Pall. All rights reserved. +** Released under the MIT license. See dynasm.lua for full copyright notice. +*/ + +#ifndef _DASM_PROTO_H +#define _DASM_PROTO_H + +#include +#include + +#define DASM_IDENT "DynASM 1.3.0" +#define DASM_VERSION 10300 /* 1.3.0 */ + +#ifndef Dst_DECL +#define Dst_DECL dasm_State **Dst +#endif + +#ifndef Dst_REF +#define Dst_REF (*Dst) +#endif + +#ifndef DASM_FDEF +#define DASM_FDEF extern +#endif + +#ifndef DASM_M_GROW +#define DASM_M_GROW(ctx, t, p, sz, need) \ + do { \ + size_t _sz = (sz), _need = (need); \ + if (_sz < _need) { \ + if (_sz < 16) _sz = 16; \ + while (_sz < _need) _sz += _sz; \ + (p) = (t *)realloc((p), _sz); \ + if ((p) == NULL) exit(1); \ + (sz) = _sz; \ + } \ + } while(0) +#endif + +#ifndef DASM_M_FREE +#define DASM_M_FREE(ctx, p, sz) free(p) +#endif + +/* Internal DynASM encoder state. */ +typedef struct dasm_State dasm_State; + + +/* Initialize and free DynASM state. */ +DASM_FDEF void dasm_init(Dst_DECL, int maxsection); +DASM_FDEF void dasm_free(Dst_DECL); + +/* Setup global array. Must be called before dasm_setup(). */ +DASM_FDEF void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl); + +/* Grow PC label array. Can be called after dasm_setup(), too. */ +DASM_FDEF void dasm_growpc(Dst_DECL, unsigned int maxpc); + +/* Setup encoder. */ +DASM_FDEF void dasm_setup(Dst_DECL, const void *actionlist); + +/* Feed encoder with actions. Calls are generated by pre-processor. */ +DASM_FDEF void dasm_put(Dst_DECL, int start, ...); + +/* Link sections and return the resulting size. */ +DASM_FDEF int dasm_link(Dst_DECL, size_t *szp); + +/* Encode sections into buffer. */ +DASM_FDEF int dasm_encode(Dst_DECL, void *buffer); + +/* Get PC label offset. */ +DASM_FDEF int dasm_getpclabel(Dst_DECL, unsigned int pc); + +#ifdef DASM_CHECKS +/* Optional sanity checker to call between isolated encoding steps. */ +DASM_FDEF int dasm_checkstep(Dst_DECL, int secmatch); +#else +#define dasm_checkstep(a, b) 0 +#endif + + +#endif /* _DASM_PROTO_H */ diff --git a/external/lua/luajit/src/dynasm/dasm_x64.lua b/external/lua/luajit/src/dynasm/dasm_x64.lua new file mode 100644 index 0000000000..ca25823c0d --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_x64.lua @@ -0,0 +1,12 @@ +------------------------------------------------------------------------------ +-- DynASM x64 module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- See dynasm.lua for full copyright notice. +------------------------------------------------------------------------------ +-- This module just sets 64 bit mode for the combined x86/x64 module. +-- All the interesting stuff is there. +------------------------------------------------------------------------------ + +x64 = true -- Using a global is an ugly, but effective solution. +return require("dasm_x86") diff --git a/external/lua/luajit/src/dynasm/dasm_x86.h b/external/lua/luajit/src/dynasm/dasm_x86.h new file mode 100644 index 0000000000..eb2bc76840 --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_x86.h @@ -0,0 +1,470 @@ +/* +** DynASM x86 encoding engine. +** Copyright (C) 2005-2013 Mike Pall. All rights reserved. +** Released under the MIT license. See dynasm.lua for full copyright notice. +*/ + +#include +#include +#include +#include + +#define DASM_ARCH "x86" + +#ifndef DASM_EXTERN +#define DASM_EXTERN(a,b,c,d) 0 +#endif + +/* Action definitions. DASM_STOP must be 255. */ +enum { + DASM_DISP = 233, + DASM_IMM_S, DASM_IMM_B, DASM_IMM_W, DASM_IMM_D, DASM_IMM_WB, DASM_IMM_DB, + DASM_VREG, DASM_SPACE, DASM_SETLABEL, DASM_REL_A, DASM_REL_LG, DASM_REL_PC, + DASM_IMM_LG, DASM_IMM_PC, DASM_LABEL_LG, DASM_LABEL_PC, DASM_ALIGN, + DASM_EXTERN, DASM_ESC, DASM_MARK, DASM_SECTION, DASM_STOP +}; + +/* Maximum number of section buffer positions for a single dasm_put() call. */ +#define DASM_MAXSECPOS 25 + +/* DynASM encoder status codes. Action list offset or number are or'ed in. */ +#define DASM_S_OK 0x00000000 +#define DASM_S_NOMEM 0x01000000 +#define DASM_S_PHASE 0x02000000 +#define DASM_S_MATCH_SEC 0x03000000 +#define DASM_S_RANGE_I 0x11000000 +#define DASM_S_RANGE_SEC 0x12000000 +#define DASM_S_RANGE_LG 0x13000000 +#define DASM_S_RANGE_PC 0x14000000 +#define DASM_S_RANGE_VREG 0x15000000 +#define DASM_S_UNDEF_L 0x21000000 +#define DASM_S_UNDEF_PC 0x22000000 + +/* Macros to convert positions (8 bit section + 24 bit index). */ +#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) +#define DASM_POS2BIAS(pos) ((pos)&0xff000000) +#define DASM_SEC2POS(sec) ((sec)<<24) +#define DASM_POS2SEC(pos) ((pos)>>24) +#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) + +/* Action list type. */ +typedef const unsigned char *dasm_ActList; + +/* Per-section structure. */ +typedef struct dasm_Section { + int *rbuf; /* Biased buffer pointer (negative section bias). */ + int *buf; /* True buffer pointer. */ + size_t bsize; /* Buffer size in bytes. */ + int pos; /* Biased buffer position. */ + int epos; /* End of biased buffer position - max single put. */ + int ofs; /* Byte offset into section. */ +} dasm_Section; + +/* Core structure holding the DynASM encoding state. */ +struct dasm_State { + size_t psize; /* Allocated size of this structure. */ + dasm_ActList actionlist; /* Current actionlist pointer. */ + int *lglabels; /* Local/global chain/pos ptrs. */ + size_t lgsize; + int *pclabels; /* PC label chains/pos ptrs. */ + size_t pcsize; + void **globals; /* Array of globals (bias -10). */ + dasm_Section *section; /* Pointer to active section. */ + size_t codesize; /* Total size of all code sections. */ + int maxsection; /* 0 <= sectionidx < maxsection. */ + int status; /* Status code. */ + dasm_Section sections[1]; /* All sections. Alloc-extended. */ +}; + +/* The size of the core structure depends on the max. number of sections. */ +#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) + + +/* Initialize DynASM state. */ +void dasm_init(Dst_DECL, int maxsection) +{ + dasm_State *D; + size_t psz = 0; + int i; + Dst_REF = NULL; + DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); + D = Dst_REF; + D->psize = psz; + D->lglabels = NULL; + D->lgsize = 0; + D->pclabels = NULL; + D->pcsize = 0; + D->globals = NULL; + D->maxsection = maxsection; + for (i = 0; i < maxsection; i++) { + D->sections[i].buf = NULL; /* Need this for pass3. */ + D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); + D->sections[i].bsize = 0; + D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ + } +} + +/* Free DynASM state. */ +void dasm_free(Dst_DECL) +{ + dasm_State *D = Dst_REF; + int i; + for (i = 0; i < D->maxsection; i++) + if (D->sections[i].buf) + DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); + if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); + if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); + DASM_M_FREE(Dst, D, D->psize); +} + +/* Setup global label array. Must be called before dasm_setup(). */ +void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) +{ + dasm_State *D = Dst_REF; + D->globals = gl - 10; /* Negative bias to compensate for locals. */ + DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); +} + +/* Grow PC label array. Can be called after dasm_setup(), too. */ +void dasm_growpc(Dst_DECL, unsigned int maxpc) +{ + dasm_State *D = Dst_REF; + size_t osz = D->pcsize; + DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); + memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); +} + +/* Setup encoder. */ +void dasm_setup(Dst_DECL, const void *actionlist) +{ + dasm_State *D = Dst_REF; + int i; + D->actionlist = (dasm_ActList)actionlist; + D->status = DASM_S_OK; + D->section = &D->sections[0]; + memset((void *)D->lglabels, 0, D->lgsize); + if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); + for (i = 0; i < D->maxsection; i++) { + D->sections[i].pos = DASM_SEC2POS(i); + D->sections[i].ofs = 0; + } +} + + +#ifdef DASM_CHECKS +#define CK(x, st) \ + do { if (!(x)) { \ + D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0) +#define CKPL(kind, st) \ + do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ + D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0) +#else +#define CK(x, st) ((void)0) +#define CKPL(kind, st) ((void)0) +#endif + +/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ +void dasm_put(Dst_DECL, int start, ...) +{ + va_list ap; + dasm_State *D = Dst_REF; + dasm_ActList p = D->actionlist + start; + dasm_Section *sec = D->section; + int pos = sec->pos, ofs = sec->ofs, mrm = 4; + int *b; + + if (pos >= sec->epos) { + DASM_M_GROW(Dst, int, sec->buf, sec->bsize, + sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); + sec->rbuf = sec->buf - DASM_POS2BIAS(pos); + sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); + } + + b = sec->rbuf; + b[pos++] = start; + + va_start(ap, start); + while (1) { + int action = *p++; + if (action < DASM_DISP) { + ofs++; + } else if (action <= DASM_REL_A) { + int n = va_arg(ap, int); + b[pos++] = n; + switch (action) { + case DASM_DISP: + if (n == 0) { if ((mrm&7) == 4) mrm = p[-2]; if ((mrm&7) != 5) break; } + case DASM_IMM_DB: if (((n+128)&-256) == 0) goto ob; + case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */ + case DASM_IMM_D: ofs += 4; break; + case DASM_IMM_S: CK(((n+128)&-256) == 0, RANGE_I); goto ob; + case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break; + case DASM_IMM_WB: if (((n+128)&-256) == 0) goto ob; + case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break; + case DASM_SPACE: p++; ofs += n; break; + case DASM_SETLABEL: b[pos-2] = -0x40000000; break; /* Neg. label ofs. */ + case DASM_VREG: CK((n&-8) == 0 && (n != 4 || (*p&1) == 0), RANGE_VREG); + if (*p++ == 1 && *p == DASM_DISP) mrm = n; continue; + } + mrm = 4; + } else { + int *pl, n; + switch (action) { + case DASM_REL_LG: + case DASM_IMM_LG: + n = *p++; pl = D->lglabels + n; + if (n <= 246) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ + pl -= 246; n = *pl; + if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ + goto linkrel; + case DASM_REL_PC: + case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); + putrel: + n = *pl; + if (n < 0) { /* Label exists. Get label pos and store it. */ + b[pos] = -n; + } else { + linkrel: + b[pos] = n; /* Else link to rel chain, anchored at label. */ + *pl = pos; + } + pos++; + ofs += 4; /* Maximum offset needed. */ + if (action == DASM_REL_LG || action == DASM_REL_PC) + b[pos++] = ofs; /* Store pass1 offset estimate. */ + break; + case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel; + case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); + putlabel: + n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ + while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; } + *pl = -pos; /* Label exists now. */ + b[pos++] = ofs; /* Store pass1 offset estimate. */ + break; + case DASM_ALIGN: + ofs += *p++; /* Maximum alignment needed (arg is 2**n-1). */ + b[pos++] = ofs; /* Store pass1 offset estimate. */ + break; + case DASM_EXTERN: p += 2; ofs += 4; break; + case DASM_ESC: p++; ofs++; break; + case DASM_MARK: mrm = p[-2]; break; + case DASM_SECTION: + n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n]; + case DASM_STOP: goto stop; + } + } + } +stop: + va_end(ap); + sec->pos = pos; + sec->ofs = ofs; +} +#undef CK + +/* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */ +int dasm_link(Dst_DECL, size_t *szp) +{ + dasm_State *D = Dst_REF; + int secnum; + int ofs = 0; + +#ifdef DASM_CHECKS + *szp = 0; + if (D->status != DASM_S_OK) return D->status; + { + int pc; + for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) + if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; + } +#endif + + { /* Handle globals not defined in this translation unit. */ + int idx; + for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) { + int n = D->lglabels[idx]; + /* Undefined label: Collapse rel chain and replace with marker (< 0). */ + while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } + } + } + + /* Combine all code sections. No support for data sections (yet). */ + for (secnum = 0; secnum < D->maxsection; secnum++) { + dasm_Section *sec = D->sections + secnum; + int *b = sec->rbuf; + int pos = DASM_SEC2POS(secnum); + int lastpos = sec->pos; + + while (pos != lastpos) { + dasm_ActList p = D->actionlist + b[pos++]; + while (1) { + int op, action = *p++; + switch (action) { + case DASM_REL_LG: p++; op = p[-3]; goto rel_pc; + case DASM_REL_PC: op = p[-2]; rel_pc: { + int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0); + if (shrink) { /* Shrinkable branch opcode? */ + int lofs, lpos = b[pos]; + if (lpos < 0) goto noshrink; /* Ext global? */ + lofs = *DASM_POS2PTR(D, lpos); + if (lpos > pos) { /* Fwd label: add cumulative section offsets. */ + int i; + for (i = secnum; i < DASM_POS2SEC(lpos); i++) + lofs += D->sections[i].ofs; + } else { + lofs -= ofs; /* Bkwd label: unfix offset. */ + } + lofs -= b[pos+1]; /* Short branch ok? */ + if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink; /* Yes. */ + else { noshrink: shrink = 0; } /* No, cannot shrink op. */ + } + b[pos+1] = shrink; + pos += 2; + break; + } + case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++; + case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W: + case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB: + case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break; + case DASM_LABEL_LG: p++; + case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */ + case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */ + case DASM_EXTERN: p += 2; break; + case DASM_ESC: p++; break; + case DASM_MARK: break; + case DASM_SECTION: case DASM_STOP: goto stop; + } + } + stop: (void)0; + } + ofs += sec->ofs; /* Next section starts right after current section. */ + } + + D->codesize = ofs; /* Total size of all code sections */ + *szp = ofs; + return DASM_S_OK; +} + +#define dasmb(x) *cp++ = (unsigned char)(x) +#ifndef DASM_ALIGNED_WRITES +#define dasmw(x) \ + do { *((unsigned short *)cp) = (unsigned short)(x); cp+=2; } while (0) +#define dasmd(x) \ + do { *((unsigned int *)cp) = (unsigned int)(x); cp+=4; } while (0) +#else +#define dasmw(x) do { dasmb(x); dasmb((x)>>8); } while (0) +#define dasmd(x) do { dasmw(x); dasmw((x)>>16); } while (0) +#endif + +/* Pass 3: Encode sections. */ +int dasm_encode(Dst_DECL, void *buffer) +{ + dasm_State *D = Dst_REF; + unsigned char *base = (unsigned char *)buffer; + unsigned char *cp = base; + int secnum; + + /* Encode all code sections. No support for data sections (yet). */ + for (secnum = 0; secnum < D->maxsection; secnum++) { + dasm_Section *sec = D->sections + secnum; + int *b = sec->buf; + int *endb = sec->rbuf + sec->pos; + + while (b != endb) { + dasm_ActList p = D->actionlist + *b++; + unsigned char *mark = NULL; + while (1) { + int action = *p++; + int n = (action >= DASM_DISP && action <= DASM_ALIGN) ? *b++ : 0; + switch (action) { + case DASM_DISP: if (!mark) mark = cp; { + unsigned char *mm = mark; + if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL; + if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7; + if (mrm != 5) { mm[-1] -= 0x80; break; } } + if (((n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40; + } + case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break; + case DASM_IMM_DB: if (((n+128)&-256) == 0) { + db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb; + } else mark = NULL; + case DASM_IMM_D: wd: dasmd(n); break; + case DASM_IMM_WB: if (((n+128)&-256) == 0) goto db; else mark = NULL; + case DASM_IMM_W: dasmw(n); break; + case DASM_VREG: { int t = *p++; if (t >= 2) n<<=3; cp[-1] |= n; break; } + case DASM_REL_LG: p++; if (n >= 0) goto rel_pc; + b++; n = (int)(ptrdiff_t)D->globals[-n]; + case DASM_REL_A: rel_a: n -= (int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */ + case DASM_REL_PC: rel_pc: { + int shrink = *b++; + int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; } + n = *pb - ((int)(cp-base) + 4-shrink); + if (shrink == 0) goto wd; + if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb; + goto wb; + } + case DASM_IMM_LG: + p++; if (n < 0) { n = (int)(ptrdiff_t)D->globals[-n]; goto wd; } + case DASM_IMM_PC: { + int *pb = DASM_POS2PTR(D, n); + n = *pb < 0 ? pb[1] : (*pb + (int)(ptrdiff_t)base); + goto wd; + } + case DASM_LABEL_LG: { + int idx = *p++; + if (idx >= 10) + D->globals[idx] = (void *)(base + (*p == DASM_SETLABEL ? *b : n)); + break; + } + case DASM_LABEL_PC: case DASM_SETLABEL: break; + case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; } + case DASM_ALIGN: + n = *p++; + while (((cp-base) & n)) *cp++ = 0x90; /* nop */ + break; + case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd; + case DASM_MARK: mark = cp; break; + case DASM_ESC: action = *p++; + default: *cp++ = action; break; + case DASM_SECTION: case DASM_STOP: goto stop; + } + } + stop: (void)0; + } + } + + if (base + D->codesize != cp) /* Check for phase errors. */ + return DASM_S_PHASE; + return DASM_S_OK; +} + +/* Get PC label offset. */ +int dasm_getpclabel(Dst_DECL, unsigned int pc) +{ + dasm_State *D = Dst_REF; + if (pc*sizeof(int) < D->pcsize) { + int pos = D->pclabels[pc]; + if (pos < 0) return *DASM_POS2PTR(D, -pos); + if (pos > 0) return -1; /* Undefined. */ + } + return -2; /* Unused or out of range. */ +} + +#ifdef DASM_CHECKS +/* Optional sanity checker to call between isolated encoding steps. */ +int dasm_checkstep(Dst_DECL, int secmatch) +{ + dasm_State *D = Dst_REF; + if (D->status == DASM_S_OK) { + int i; + for (i = 1; i <= 9; i++) { + if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; } + D->lglabels[i] = 0; + } + } + if (D->status == DASM_S_OK && secmatch >= 0 && + D->section != &D->sections[secmatch]) + D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections); + return D->status; +} +#endif + diff --git a/external/lua/luajit/src/dynasm/dasm_x86.lua b/external/lua/luajit/src/dynasm/dasm_x86.lua new file mode 100644 index 0000000000..1f0981f326 --- /dev/null +++ b/external/lua/luajit/src/dynasm/dasm_x86.lua @@ -0,0 +1,1931 @@ +------------------------------------------------------------------------------ +-- DynASM x86/x64 module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- See dynasm.lua for full copyright notice. +------------------------------------------------------------------------------ + +local x64 = x64 + +-- Module information: +local _info = { + arch = x64 and "x64" or "x86", + description = "DynASM x86/x64 module", + version = "1.3.0", + vernum = 10300, + release = "2011-05-05", + author = "Mike Pall", + license = "MIT", +} + +-- Exported glue functions for the arch-specific module. +local _M = { _info = _info } + +-- Cache library functions. +local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs +local assert, unpack, setmetatable = assert, unpack or table.unpack, setmetatable +local _s = string +local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char +local find, match, gmatch, gsub = _s.find, _s.match, _s.gmatch, _s.gsub +local concat, sort = table.concat, table.sort +local bit = bit or require("bit") +local band, shl, shr = bit.band, bit.lshift, bit.rshift + +-- Inherited tables and callbacks. +local g_opt, g_arch +local wline, werror, wfatal, wwarn + +-- Action name list. +-- CHECK: Keep this in sync with the C code! +local action_names = { + -- int arg, 1 buffer pos: + "DISP", "IMM_S", "IMM_B", "IMM_W", "IMM_D", "IMM_WB", "IMM_DB", + -- action arg (1 byte), int arg, 1 buffer pos (reg/num): + "VREG", "SPACE", -- !x64: VREG support NYI. + -- ptrdiff_t arg, 1 buffer pos (address): !x64 + "SETLABEL", "REL_A", + -- action arg (1 byte) or int arg, 2 buffer pos (link, offset): + "REL_LG", "REL_PC", + -- action arg (1 byte) or int arg, 1 buffer pos (link): + "IMM_LG", "IMM_PC", + -- action arg (1 byte) or int arg, 1 buffer pos (offset): + "LABEL_LG", "LABEL_PC", + -- action arg (1 byte), 1 buffer pos (offset): + "ALIGN", + -- action args (2 bytes), no buffer pos. + "EXTERN", + -- action arg (1 byte), no buffer pos. + "ESC", + -- no action arg, no buffer pos. + "MARK", + -- action arg (1 byte), no buffer pos, terminal action: + "SECTION", + -- no args, no buffer pos, terminal action: + "STOP" +} + +-- Maximum number of section buffer positions for dasm_put(). +-- CHECK: Keep this in sync with the C code! +local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. + +-- Action name -> action number (dynamically generated below). +local map_action = {} +-- First action number. Everything below does not need to be escaped. +local actfirst = 256-#action_names + +-- Action list buffer and string (only used to remove dupes). +local actlist = {} +local actstr = "" + +-- Argument list for next dasm_put(). Start with offset 0 into action list. +local actargs = { 0 } + +-- Current number of section buffer positions for dasm_put(). +local secpos = 1 + +------------------------------------------------------------------------------ + +-- Compute action numbers for action names. +for n,name in ipairs(action_names) do + local num = actfirst + n - 1 + map_action[name] = num +end + +-- Dump action names and numbers. +local function dumpactions(out) + out:write("DynASM encoding engine action codes:\n") + for n,name in ipairs(action_names) do + local num = map_action[name] + out:write(format(" %-10s %02X %d\n", name, num, num)) + end + out:write("\n") +end + +-- Write action list buffer as a huge static C array. +local function writeactions(out, name) + local nn = #actlist + local last = actlist[nn] or 255 + actlist[nn] = nil -- Remove last byte. + if nn == 0 then nn = 1 end + out:write("static const unsigned char ", name, "[", nn, "] = {\n") + local s = " " + for n,b in ipairs(actlist) do + s = s..b.."," + if #s >= 75 then + assert(out:write(s, "\n")) + s = " " + end + end + out:write(s, last, "\n};\n\n") -- Add last byte back. +end + +------------------------------------------------------------------------------ + +-- Add byte to action list. +local function wputxb(n) + assert(n >= 0 and n <= 255 and n % 1 == 0, "byte out of range") + actlist[#actlist+1] = n +end + +-- Add action to list with optional arg. Advance buffer pos, too. +local function waction(action, a, num) + wputxb(assert(map_action[action], "bad action name `"..action.."'")) + if a then actargs[#actargs+1] = a end + if a or num then secpos = secpos + (num or 1) end +end + +-- Add call to embedded DynASM C code. +local function wcall(func, args) + wline(format("dasm_%s(Dst, %s);", func, concat(args, ", ")), true) +end + +-- Delete duplicate action list chunks. A tad slow, but so what. +local function dedupechunk(offset) + local al, as = actlist, actstr + local chunk = char(unpack(al, offset+1, #al)) + local orig = find(as, chunk, 1, true) + if orig then + actargs[1] = orig-1 -- Replace with original offset. + for i=offset+1,#al do al[i] = nil end -- Kill dupe. + else + actstr = as..chunk + end +end + +-- Flush action list (intervening C code or buffer pos overflow). +local function wflush(term) + local offset = actargs[1] + if #actlist == offset then return end -- Nothing to flush. + if not term then waction("STOP") end -- Terminate action list. + dedupechunk(offset) + wcall("put", actargs) -- Add call to dasm_put(). + actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). + secpos = 1 -- The actionlist offset occupies a buffer position, too. +end + +-- Put escaped byte. +local function wputb(n) + if n >= actfirst then waction("ESC") end -- Need to escape byte. + wputxb(n) +end + +------------------------------------------------------------------------------ + +-- Global label name -> global label number. With auto assignment on 1st use. +local next_global = 10 +local map_global = setmetatable({}, { __index = function(t, name) + if not match(name, "^[%a_][%w_@]*$") then werror("bad global label") end + local n = next_global + if n > 246 then werror("too many global labels") end + next_global = n + 1 + t[name] = n + return n +end}) + +-- Dump global labels. +local function dumpglobals(out, lvl) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("Global labels:\n") + for i=10,next_global-1 do + out:write(format(" %s\n", t[i])) + end + out:write("\n") +end + +-- Write global label enum. +local function writeglobals(out, prefix) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("enum {\n") + for i=10,next_global-1 do + out:write(" ", prefix, gsub(t[i], "@.*", ""), ",\n") + end + out:write(" ", prefix, "_MAX\n};\n") +end + +-- Write global label names. +local function writeglobalnames(out, name) + local t = {} + for name, n in pairs(map_global) do t[n] = name end + out:write("static const char *const ", name, "[] = {\n") + for i=10,next_global-1 do + out:write(" \"", t[i], "\",\n") + end + out:write(" (const char *)0\n};\n") +end + +------------------------------------------------------------------------------ + +-- Extern label name -> extern label number. With auto assignment on 1st use. +local next_extern = -1 +local map_extern = setmetatable({}, { __index = function(t, name) + -- No restrictions on the name for now. + local n = next_extern + if n < -256 then werror("too many extern labels") end + next_extern = n - 1 + t[name] = n + return n +end}) + +-- Dump extern labels. +local function dumpexterns(out, lvl) + local t = {} + for name, n in pairs(map_extern) do t[-n] = name end + out:write("Extern labels:\n") + for i=1,-next_extern-1 do + out:write(format(" %s\n", t[i])) + end + out:write("\n") +end + +-- Write extern label names. +local function writeexternnames(out, name) + local t = {} + for name, n in pairs(map_extern) do t[-n] = name end + out:write("static const char *const ", name, "[] = {\n") + for i=1,-next_extern-1 do + out:write(" \"", t[i], "\",\n") + end + out:write(" (const char *)0\n};\n") +end + +------------------------------------------------------------------------------ + +-- Arch-specific maps. +local map_archdef = {} -- Ext. register name -> int. name. +local map_reg_rev = {} -- Int. register name -> ext. name. +local map_reg_num = {} -- Int. register name -> register number. +local map_reg_opsize = {} -- Int. register name -> operand size. +local map_reg_valid_base = {} -- Int. register name -> valid base register? +local map_reg_valid_index = {} -- Int. register name -> valid index register? +local map_reg_needrex = {} -- Int. register name -> need rex vs. no rex. +local reg_list = {} -- Canonical list of int. register names. + +local map_type = {} -- Type name -> { ctype, reg } +local ctypenum = 0 -- Type number (for _PTx macros). + +local addrsize = x64 and "q" or "d" -- Size for address operands. + +-- Helper functions to fill register maps. +local function mkrmap(sz, cl, names) + local cname = format("@%s", sz) + reg_list[#reg_list+1] = cname + map_archdef[cl] = cname + map_reg_rev[cname] = cl + map_reg_num[cname] = -1 + map_reg_opsize[cname] = sz + if sz == addrsize or sz == "d" then + map_reg_valid_base[cname] = true + map_reg_valid_index[cname] = true + end + if names then + for n,name in ipairs(names) do + local iname = format("@%s%x", sz, n-1) + reg_list[#reg_list+1] = iname + map_archdef[name] = iname + map_reg_rev[iname] = name + map_reg_num[iname] = n-1 + map_reg_opsize[iname] = sz + if sz == "b" and n > 4 then map_reg_needrex[iname] = false end + if sz == addrsize or sz == "d" then + map_reg_valid_base[iname] = true + map_reg_valid_index[iname] = true + end + end + end + for i=0,(x64 and sz ~= "f") and 15 or 7 do + local needrex = sz == "b" and i > 3 + local iname = format("@%s%x%s", sz, i, needrex and "R" or "") + if needrex then map_reg_needrex[iname] = true end + local name + if sz == "o" then name = format("xmm%d", i) + elseif sz == "f" then name = format("st%d", i) + else name = format("r%d%s", i, sz == addrsize and "" or sz) end + map_archdef[name] = iname + if not map_reg_rev[iname] then + reg_list[#reg_list+1] = iname + map_reg_rev[iname] = name + map_reg_num[iname] = i + map_reg_opsize[iname] = sz + if sz == addrsize or sz == "d" then + map_reg_valid_base[iname] = true + map_reg_valid_index[iname] = true + end + end + end + reg_list[#reg_list+1] = "" +end + +-- Integer registers (qword, dword, word and byte sized). +if x64 then + mkrmap("q", "Rq", {"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi"}) +end +mkrmap("d", "Rd", {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"}) +mkrmap("w", "Rw", {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"}) +mkrmap("b", "Rb", {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"}) +map_reg_valid_index[map_archdef.esp] = false +if x64 then map_reg_valid_index[map_archdef.rsp] = false end +map_archdef["Ra"] = "@"..addrsize + +-- FP registers (internally tword sized, but use "f" as operand size). +mkrmap("f", "Rf") + +-- SSE registers (oword sized, but qword and dword accessible). +mkrmap("o", "xmm") + +-- Operand size prefixes to codes. +local map_opsize = { + byte = "b", word = "w", dword = "d", qword = "q", oword = "o", tword = "t", + aword = addrsize, +} + +-- Operand size code to number. +local map_opsizenum = { + b = 1, w = 2, d = 4, q = 8, o = 16, t = 10, +} + +-- Operand size code to name. +local map_opsizename = { + b = "byte", w = "word", d = "dword", q = "qword", o = "oword", t = "tword", + f = "fpword", +} + +-- Valid index register scale factors. +local map_xsc = { + ["1"] = 0, ["2"] = 1, ["4"] = 2, ["8"] = 3, +} + +-- Condition codes. +local map_cc = { + o = 0, no = 1, b = 2, nb = 3, e = 4, ne = 5, be = 6, nbe = 7, + s = 8, ns = 9, p = 10, np = 11, l = 12, nl = 13, le = 14, nle = 15, + c = 2, nae = 2, nc = 3, ae = 3, z = 4, nz = 5, na = 6, a = 7, + pe = 10, po = 11, nge = 12, ge = 13, ng = 14, g = 15, +} + + +-- Reverse defines for registers. +function _M.revdef(s) + return gsub(s, "@%w+", map_reg_rev) +end + +-- Dump register names and numbers +local function dumpregs(out) + out:write("Register names, sizes and internal numbers:\n") + for _,reg in ipairs(reg_list) do + if reg == "" then + out:write("\n") + else + local name = map_reg_rev[reg] + local num = map_reg_num[reg] + local opsize = map_opsizename[map_reg_opsize[reg]] + out:write(format(" %-5s %-8s %s\n", name, opsize, + num < 0 and "(variable)" or num)) + end + end +end + +------------------------------------------------------------------------------ + +-- Put action for label arg (IMM_LG, IMM_PC, REL_LG, REL_PC). +local function wputlabel(aprefix, imm, num) + if type(imm) == "number" then + if imm < 0 then + waction("EXTERN") + wputxb(aprefix == "IMM_" and 0 or 1) + imm = -imm-1 + else + waction(aprefix.."LG", nil, num); + end + wputxb(imm) + else + waction(aprefix.."PC", imm, num) + end +end + +-- Put signed byte or arg. +local function wputsbarg(n) + if type(n) == "number" then + if n < -128 or n > 127 then + werror("signed immediate byte out of range") + end + if n < 0 then n = n + 256 end + wputb(n) + else waction("IMM_S", n) end +end + +-- Put unsigned byte or arg. +local function wputbarg(n) + if type(n) == "number" then + if n < 0 or n > 255 then + werror("unsigned immediate byte out of range") + end + wputb(n) + else waction("IMM_B", n) end +end + +-- Put unsigned word or arg. +local function wputwarg(n) + if type(n) == "number" then + if shr(n, 16) ~= 0 then + werror("unsigned immediate word out of range") + end + wputb(band(n, 255)); wputb(shr(n, 8)); + else waction("IMM_W", n) end +end + +-- Put signed or unsigned dword or arg. +local function wputdarg(n) + local tn = type(n) + if tn == "number" then + wputb(band(n, 255)) + wputb(band(shr(n, 8), 255)) + wputb(band(shr(n, 16), 255)) + wputb(shr(n, 24)) + elseif tn == "table" then + wputlabel("IMM_", n[1], 1) + else + waction("IMM_D", n) + end +end + +-- Put operand-size dependent number or arg (defaults to dword). +local function wputszarg(sz, n) + if not sz or sz == "d" or sz == "q" then wputdarg(n) + elseif sz == "w" then wputwarg(n) + elseif sz == "b" then wputbarg(n) + elseif sz == "s" then wputsbarg(n) + else werror("bad operand size") end +end + +-- Put multi-byte opcode with operand-size dependent modifications. +local function wputop(sz, op, rex) + local r + if rex ~= 0 and not x64 then werror("bad operand size") end + if sz == "w" then wputb(102) end + -- Needs >32 bit numbers, but only for crc32 eax, word [ebx] + if op >= 4294967296 then r = op%4294967296 wputb((op-r)/4294967296) op = r end + if op >= 16777216 then wputb(shr(op, 24)); op = band(op, 0xffffff) end + if op >= 65536 then + if rex ~= 0 then + local opc3 = band(op, 0xffff00) + if opc3 == 0x0f3a00 or opc3 == 0x0f3800 then + wputb(64 + band(rex, 15)); rex = 0 + end + end + wputb(shr(op, 16)); op = band(op, 0xffff) + end + if op >= 256 then + local b = shr(op, 8) + if b == 15 and rex ~= 0 then wputb(64 + band(rex, 15)); rex = 0 end + wputb(b) + op = band(op, 255) + end + if rex ~= 0 then wputb(64 + band(rex, 15)) end + if sz == "b" then op = op - 1 end + wputb(op) +end + +-- Put ModRM or SIB formatted byte. +local function wputmodrm(m, s, rm, vs, vrm) + assert(m < 4 and s < 16 and rm < 16, "bad modrm operands") + wputb(shl(m, 6) + shl(band(s, 7), 3) + band(rm, 7)) +end + +-- Put ModRM/SIB plus optional displacement. +local function wputmrmsib(t, imark, s, vsreg) + local vreg, vxreg + local reg, xreg = t.reg, t.xreg + if reg and reg < 0 then reg = 0; vreg = t.vreg end + if xreg and xreg < 0 then xreg = 0; vxreg = t.vxreg end + if s < 0 then s = 0 end + + -- Register mode. + if sub(t.mode, 1, 1) == "r" then + wputmodrm(3, s, reg) + if vsreg then waction("VREG", vsreg); wputxb(2) end + if vreg then waction("VREG", vreg); wputxb(0) end + return + end + + local disp = t.disp + local tdisp = type(disp) + -- No base register? + if not reg then + local riprel = false + if xreg then + -- Indexed mode with index register only. + -- [xreg*xsc+disp] -> (0, s, esp) (xsc, xreg, ebp) + wputmodrm(0, s, 4) + if imark == "I" then waction("MARK") end + if vsreg then waction("VREG", vsreg); wputxb(2) end + wputmodrm(t.xsc, xreg, 5) + if vxreg then waction("VREG", vxreg); wputxb(3) end + else + -- Pure 32 bit displacement. + if x64 and tdisp ~= "table" then + wputmodrm(0, s, 4) -- [disp] -> (0, s, esp) (0, esp, ebp) + if imark == "I" then waction("MARK") end + wputmodrm(0, 4, 5) + else + riprel = x64 + wputmodrm(0, s, 5) -- [disp|rip-label] -> (0, s, ebp) + if imark == "I" then waction("MARK") end + end + if vsreg then waction("VREG", vsreg); wputxb(2) end + end + if riprel then -- Emit rip-relative displacement. + if match("UWSiI", imark) then + werror("NYI: rip-relative displacement followed by immediate") + end + -- The previous byte in the action buffer cannot be 0xe9 or 0x80-0x8f. + wputlabel("REL_", disp[1], 2) + else + wputdarg(disp) + end + return + end + + local m + if tdisp == "number" then -- Check displacement size at assembly time. + if disp == 0 and band(reg, 7) ~= 5 then -- [ebp] -> [ebp+0] (in SIB, too) + if not vreg then m = 0 end -- Force DISP to allow [Rd(5)] -> [ebp+0] + elseif disp >= -128 and disp <= 127 then m = 1 + else m = 2 end + elseif tdisp == "table" then + m = 2 + end + + -- Index register present or esp as base register: need SIB encoding. + if xreg or band(reg, 7) == 4 then + wputmodrm(m or 2, s, 4) -- ModRM. + if m == nil or imark == "I" then waction("MARK") end + if vsreg then waction("VREG", vsreg); wputxb(2) end + wputmodrm(t.xsc or 0, xreg or 4, reg) -- SIB. + if vxreg then waction("VREG", vxreg); wputxb(3) end + if vreg then waction("VREG", vreg); wputxb(1) end + else + wputmodrm(m or 2, s, reg) -- ModRM. + if (imark == "I" and (m == 1 or m == 2)) or + (m == nil and (vsreg or vreg)) then waction("MARK") end + if vsreg then waction("VREG", vsreg); wputxb(2) end + if vreg then waction("VREG", vreg); wputxb(1) end + end + + -- Put displacement. + if m == 1 then wputsbarg(disp) + elseif m == 2 then wputdarg(disp) + elseif m == nil then waction("DISP", disp) end +end + +------------------------------------------------------------------------------ + +-- Return human-readable operand mode string. +local function opmodestr(op, args) + local m = {} + for i=1,#args do + local a = args[i] + m[#m+1] = sub(a.mode, 1, 1)..(a.opsize or "?") + end + return op.." "..concat(m, ",") +end + +-- Convert number to valid integer or nil. +local function toint(expr) + local n = tonumber(expr) + if n then + if n % 1 ~= 0 or n < -2147483648 or n > 4294967295 then + werror("bad integer number `"..expr.."'") + end + return n + end +end + +-- Parse immediate expression. +local function immexpr(expr) + -- &expr (pointer) + if sub(expr, 1, 1) == "&" then + return "iPJ", format("(ptrdiff_t)(%s)", sub(expr,2)) + end + + local prefix = sub(expr, 1, 2) + -- =>expr (pc label reference) + if prefix == "=>" then + return "iJ", sub(expr, 3) + end + -- ->name (global label reference) + if prefix == "->" then + return "iJ", map_global[sub(expr, 3)] + end + + -- [<>][1-9] (local label reference) + local dir, lnum = match(expr, "^([<>])([1-9])$") + if dir then -- Fwd: 247-255, Bkwd: 1-9. + return "iJ", lnum + (dir == ">" and 246 or 0) + end + + local extname = match(expr, "^extern%s+(%S+)$") + if extname then + return "iJ", map_extern[extname] + end + + -- expr (interpreted as immediate) + return "iI", expr +end + +-- Parse displacement expression: +-num, +-expr, +-opsize*num +local function dispexpr(expr) + local disp = expr == "" and 0 or toint(expr) + if disp then return disp end + local c, dispt = match(expr, "^([+-])%s*(.+)$") + if c == "+" then + expr = dispt + elseif not c then + werror("bad displacement expression `"..expr.."'") + end + local opsize, tailops = match(dispt, "^(%w+)%s*%*%s*(.+)$") + local ops, imm = map_opsize[opsize], toint(tailops) + if ops and imm then + if c == "-" then imm = -imm end + return imm*map_opsizenum[ops] + end + local mode, iexpr = immexpr(dispt) + if mode == "iJ" then + if c == "-" then werror("cannot invert label reference") end + return { iexpr } + end + return expr -- Need to return original signed expression. +end + +-- Parse register or type expression. +local function rtexpr(expr) + if not expr then return end + local tname, ovreg = match(expr, "^([%w_]+):(@[%w_]+)$") + local tp = map_type[tname or expr] + if tp then + local reg = ovreg or tp.reg + local rnum = map_reg_num[reg] + if not rnum then + werror("type `"..(tname or expr).."' needs a register override") + end + if not map_reg_valid_base[reg] then + werror("bad base register override `"..(map_reg_rev[reg] or reg).."'") + end + return reg, rnum, tp + end + return expr, map_reg_num[expr] +end + +-- Parse operand and return { mode, opsize, reg, xreg, xsc, disp, imm }. +local function parseoperand(param) + local t = {} + + local expr = param + local opsize, tailops = match(param, "^(%w+)%s*(.+)$") + if opsize then + t.opsize = map_opsize[opsize] + if t.opsize then expr = tailops end + end + + local br = match(expr, "^%[%s*(.-)%s*%]$") + repeat + if br then + t.mode = "xm" + + -- [disp] + t.disp = toint(br) + if t.disp then + t.mode = x64 and "xm" or "xmO" + break + end + + -- [reg...] + local tp + local reg, tailr = match(br, "^([@%w_:]+)%s*(.*)$") + reg, t.reg, tp = rtexpr(reg) + if not t.reg then + -- [expr] + t.mode = x64 and "xm" or "xmO" + t.disp = dispexpr("+"..br) + break + end + + if t.reg == -1 then + t.vreg, tailr = match(tailr, "^(%b())(.*)$") + if not t.vreg then werror("bad variable register expression") end + end + + -- [xreg*xsc] or [xreg*xsc+-disp] or [xreg*xsc+-expr] + local xsc, tailsc = match(tailr, "^%*%s*([1248])%s*(.*)$") + if xsc then + if not map_reg_valid_index[reg] then + werror("bad index register `"..map_reg_rev[reg].."'") + end + t.xsc = map_xsc[xsc] + t.xreg = t.reg + t.vxreg = t.vreg + t.reg = nil + t.vreg = nil + t.disp = dispexpr(tailsc) + break + end + if not map_reg_valid_base[reg] then + werror("bad base register `"..map_reg_rev[reg].."'") + end + + -- [reg] or [reg+-disp] + t.disp = toint(tailr) or (tailr == "" and 0) + if t.disp then break end + + -- [reg+xreg...] + local xreg, tailx = match(tailr, "^+%s*([@%w_:]+)%s*(.*)$") + xreg, t.xreg, tp = rtexpr(xreg) + if not t.xreg then + -- [reg+-expr] + t.disp = dispexpr(tailr) + break + end + if not map_reg_valid_index[xreg] then + werror("bad index register `"..map_reg_rev[xreg].."'") + end + + if t.xreg == -1 then + t.vxreg, tailx = match(tailx, "^(%b())(.*)$") + if not t.vxreg then werror("bad variable register expression") end + end + + -- [reg+xreg*xsc...] + local xsc, tailsc = match(tailx, "^%*%s*([1248])%s*(.*)$") + if xsc then + t.xsc = map_xsc[xsc] + tailx = tailsc + end + + -- [...] or [...+-disp] or [...+-expr] + t.disp = dispexpr(tailx) + else + -- imm or opsize*imm + local imm = toint(expr) + if not imm and sub(expr, 1, 1) == "*" and t.opsize then + imm = toint(sub(expr, 2)) + if imm then + imm = imm * map_opsizenum[t.opsize] + t.opsize = nil + end + end + if imm then + if t.opsize then werror("bad operand size override") end + local m = "i" + if imm == 1 then m = m.."1" end + if imm >= 4294967168 and imm <= 4294967295 then imm = imm-4294967296 end + if imm >= -128 and imm <= 127 then m = m.."S" end + t.imm = imm + t.mode = m + break + end + + local tp + local reg, tailr = match(expr, "^([@%w_:]+)%s*(.*)$") + reg, t.reg, tp = rtexpr(reg) + if t.reg then + if t.reg == -1 then + t.vreg, tailr = match(tailr, "^(%b())(.*)$") + if not t.vreg then werror("bad variable register expression") end + end + -- reg + if tailr == "" then + if t.opsize then werror("bad operand size override") end + t.opsize = map_reg_opsize[reg] + if t.opsize == "f" then + t.mode = t.reg == 0 and "fF" or "f" + else + if reg == "@w4" or (x64 and reg == "@d4") then + wwarn("bad idea, try again with `"..(x64 and "rsp'" or "esp'")) + end + t.mode = t.reg == 0 and "rmR" or (reg == "@b1" and "rmC" or "rm") + end + t.needrex = map_reg_needrex[reg] + break + end + + -- type[idx], type[idx].field, type->field -> [reg+offset_expr] + if not tp then werror("bad operand `"..param.."'") end + t.mode = "xm" + t.disp = format(tp.ctypefmt, tailr) + else + t.mode, t.imm = immexpr(expr) + if sub(t.mode, -1) == "J" then + if t.opsize and t.opsize ~= addrsize then + werror("bad operand size override") + end + t.opsize = addrsize + end + end + end + until true + return t +end + +------------------------------------------------------------------------------ +-- x86 Template String Description +-- =============================== +-- +-- Each template string is a list of [match:]pattern pairs, +-- separated by "|". The first match wins. No match means a +-- bad or unsupported combination of operand modes or sizes. +-- +-- The match part and the ":" is omitted if the operation has +-- no operands. Otherwise the first N characters are matched +-- against the mode strings of each of the N operands. +-- +-- The mode string for each operand type is (see parseoperand()): +-- Integer register: "rm", +"R" for eax, ax, al, +"C" for cl +-- FP register: "f", +"F" for st0 +-- Index operand: "xm", +"O" for [disp] (pure offset) +-- Immediate: "i", +"S" for signed 8 bit, +"1" for 1, +-- +"I" for arg, +"P" for pointer +-- Any: +"J" for valid jump targets +-- +-- So a match character "m" (mixed) matches both an integer register +-- and an index operand (to be encoded with the ModRM/SIB scheme). +-- But "r" matches only a register and "x" only an index operand +-- (e.g. for FP memory access operations). +-- +-- The operand size match string starts right after the mode match +-- characters and ends before the ":". "dwb" or "qdwb" is assumed, if empty. +-- The effective data size of the operation is matched against this list. +-- +-- If only the regular "b", "w", "d", "q", "t" operand sizes are +-- present, then all operands must be the same size. Unspecified sizes +-- are ignored, but at least one operand must have a size or the pattern +-- won't match (use the "byte", "word", "dword", "qword", "tword" +-- operand size overrides. E.g.: mov dword [eax], 1). +-- +-- If the list has a "1" or "2" prefix, the operand size is taken +-- from the respective operand and any other operand sizes are ignored. +-- If the list contains only ".", all operand sizes are ignored. +-- If the list has a "/" prefix, the concatenated (mixed) operand sizes +-- are compared to the match. +-- +-- E.g. "rrdw" matches for either two dword registers or two word +-- registers. "Fx2dq" matches an st0 operand plus an index operand +-- pointing to a dword (float) or qword (double). +-- +-- Every character after the ":" is part of the pattern string: +-- Hex chars are accumulated to form the opcode (left to right). +-- "n" disables the standard opcode mods +-- (otherwise: -1 for "b", o16 prefix for "w", rex.w for "q") +-- "X" Force REX.W. +-- "r"/"R" adds the reg. number from the 1st/2nd operand to the opcode. +-- "m"/"M" generates ModRM/SIB from the 1st/2nd operand. +-- The spare 3 bits are either filled with the last hex digit or +-- the result from a previous "r"/"R". The opcode is restored. +-- +-- All of the following characters force a flush of the opcode: +-- "o"/"O" stores a pure 32 bit disp (offset) from the 1st/2nd operand. +-- "S" stores a signed 8 bit immediate from the last operand. +-- "U" stores an unsigned 8 bit immediate from the last operand. +-- "W" stores an unsigned 16 bit immediate from the last operand. +-- "i" stores an operand sized immediate from the last operand. +-- "I" dito, but generates an action code to optionally modify +-- the opcode (+2) for a signed 8 bit immediate. +-- "J" generates one of the REL action codes from the last operand. +-- +------------------------------------------------------------------------------ + +-- Template strings for x86 instructions. Ordered by first opcode byte. +-- Unimplemented opcodes (deliberate omissions) are marked with *. +local map_op = { + -- 00-05: add... + -- 06: *push es + -- 07: *pop es + -- 08-0D: or... + -- 0E: *push cs + -- 0F: two byte opcode prefix + -- 10-15: adc... + -- 16: *push ss + -- 17: *pop ss + -- 18-1D: sbb... + -- 1E: *push ds + -- 1F: *pop ds + -- 20-25: and... + es_0 = "26", + -- 27: *daa + -- 28-2D: sub... + cs_0 = "2E", + -- 2F: *das + -- 30-35: xor... + ss_0 = "36", + -- 37: *aaa + -- 38-3D: cmp... + ds_0 = "3E", + -- 3F: *aas + inc_1 = x64 and "m:FF0m" or "rdw:40r|m:FF0m", + dec_1 = x64 and "m:FF1m" or "rdw:48r|m:FF1m", + push_1 = (x64 and "rq:n50r|rw:50r|mq:nFF6m|mw:FF6m" or + "rdw:50r|mdw:FF6m").."|S.:6AS|ib:n6Ai|i.:68i", + pop_1 = x64 and "rq:n58r|rw:58r|mq:n8F0m|mw:8F0m" or "rdw:58r|mdw:8F0m", + -- 60: *pusha, *pushad, *pushaw + -- 61: *popa, *popad, *popaw + -- 62: *bound rdw,x + -- 63: x86: *arpl mw,rw + movsxd_2 = x64 and "rm/qd:63rM", + fs_0 = "64", + gs_0 = "65", + o16_0 = "66", + a16_0 = not x64 and "67" or nil, + a32_0 = x64 and "67", + -- 68: push idw + -- 69: imul rdw,mdw,idw + -- 6A: push ib + -- 6B: imul rdw,mdw,S + -- 6C: *insb + -- 6D: *insd, *insw + -- 6E: *outsb + -- 6F: *outsd, *outsw + -- 70-7F: jcc lb + -- 80: add... mb,i + -- 81: add... mdw,i + -- 82: *undefined + -- 83: add... mdw,S + test_2 = "mr:85Rm|rm:85rM|Ri:A9ri|mi:F70mi", + -- 86: xchg rb,mb + -- 87: xchg rdw,mdw + -- 88: mov mb,r + -- 89: mov mdw,r + -- 8A: mov r,mb + -- 8B: mov r,mdw + -- 8C: *mov mdw,seg + lea_2 = "rx1dq:8DrM", + -- 8E: *mov seg,mdw + -- 8F: pop mdw + nop_0 = "90", + xchg_2 = "Rrqdw:90R|rRqdw:90r|rm:87rM|mr:87Rm", + cbw_0 = "6698", + cwde_0 = "98", + cdqe_0 = "4898", + cwd_0 = "6699", + cdq_0 = "99", + cqo_0 = "4899", + -- 9A: *call iw:idw + wait_0 = "9B", + fwait_0 = "9B", + pushf_0 = "9C", + pushfd_0 = not x64 and "9C", + pushfq_0 = x64 and "9C", + popf_0 = "9D", + popfd_0 = not x64 and "9D", + popfq_0 = x64 and "9D", + sahf_0 = "9E", + lahf_0 = "9F", + mov_2 = "OR:A3o|RO:A1O|mr:89Rm|rm:8BrM|rib:nB0ri|ridw:B8ri|mi:C70mi", + movsb_0 = "A4", + movsw_0 = "66A5", + movsd_0 = "A5", + cmpsb_0 = "A6", + cmpsw_0 = "66A7", + cmpsd_0 = "A7", + -- A8: test Rb,i + -- A9: test Rdw,i + stosb_0 = "AA", + stosw_0 = "66AB", + stosd_0 = "AB", + lodsb_0 = "AC", + lodsw_0 = "66AD", + lodsd_0 = "AD", + scasb_0 = "AE", + scasw_0 = "66AF", + scasd_0 = "AF", + -- B0-B7: mov rb,i + -- B8-BF: mov rdw,i + -- C0: rol... mb,i + -- C1: rol... mdw,i + ret_1 = "i.:nC2W", + ret_0 = "C3", + -- C4: *les rdw,mq + -- C5: *lds rdw,mq + -- C6: mov mb,i + -- C7: mov mdw,i + -- C8: *enter iw,ib + leave_0 = "C9", + -- CA: *retf iw + -- CB: *retf + int3_0 = "CC", + int_1 = "i.:nCDU", + into_0 = "CE", + -- CF: *iret + -- D0: rol... mb,1 + -- D1: rol... mdw,1 + -- D2: rol... mb,cl + -- D3: rol... mb,cl + -- D4: *aam ib + -- D5: *aad ib + -- D6: *salc + -- D7: *xlat + -- D8-DF: floating point ops + -- E0: *loopne + -- E1: *loope + -- E2: *loop + -- E3: *jcxz, *jecxz + -- E4: *in Rb,ib + -- E5: *in Rdw,ib + -- E6: *out ib,Rb + -- E7: *out ib,Rdw + call_1 = x64 and "mq:nFF2m|J.:E8nJ" or "md:FF2m|J.:E8J", + jmp_1 = x64 and "mq:nFF4m|J.:E9nJ" or "md:FF4m|J.:E9J", -- short: EB + -- EA: *jmp iw:idw + -- EB: jmp ib + -- EC: *in Rb,dx + -- ED: *in Rdw,dx + -- EE: *out dx,Rb + -- EF: *out dx,Rdw + -- F0: *lock + int1_0 = "F1", + repne_0 = "F2", + repnz_0 = "F2", + rep_0 = "F3", + repe_0 = "F3", + repz_0 = "F3", + -- F4: *hlt + cmc_0 = "F5", + -- F6: test... mb,i; div... mb + -- F7: test... mdw,i; div... mdw + clc_0 = "F8", + stc_0 = "F9", + -- FA: *cli + cld_0 = "FC", + std_0 = "FD", + -- FE: inc... mb + -- FF: inc... mdw + + -- misc ops + not_1 = "m:F72m", + neg_1 = "m:F73m", + mul_1 = "m:F74m", + imul_1 = "m:F75m", + div_1 = "m:F76m", + idiv_1 = "m:F77m", + + imul_2 = "rmqdw:0FAFrM|rIqdw:69rmI|rSqdw:6BrmS|riqdw:69rmi", + imul_3 = "rmIqdw:69rMI|rmSqdw:6BrMS|rmiqdw:69rMi", + + movzx_2 = "rm/db:0FB6rM|rm/qb:|rm/wb:0FB6rM|rm/dw:0FB7rM|rm/qw:", + movsx_2 = "rm/db:0FBErM|rm/qb:|rm/wb:0FBErM|rm/dw:0FBFrM|rm/qw:", + + bswap_1 = "rqd:0FC8r", + bsf_2 = "rmqdw:0FBCrM", + bsr_2 = "rmqdw:0FBDrM", + bt_2 = "mrqdw:0FA3Rm|miqdw:0FBA4mU", + btc_2 = "mrqdw:0FBBRm|miqdw:0FBA7mU", + btr_2 = "mrqdw:0FB3Rm|miqdw:0FBA6mU", + bts_2 = "mrqdw:0FABRm|miqdw:0FBA5mU", + + rdtsc_0 = "0F31", -- P1+ + cpuid_0 = "0FA2", -- P1+ + + -- floating point ops + fst_1 = "ff:DDD0r|xd:D92m|xq:nDD2m", + fstp_1 = "ff:DDD8r|xd:D93m|xq:nDD3m|xt:DB7m", + fld_1 = "ff:D9C0r|xd:D90m|xq:nDD0m|xt:DB5m", + + fpop_0 = "DDD8", -- Alias for fstp st0. + + fist_1 = "xw:nDF2m|xd:DB2m", + fistp_1 = "xw:nDF3m|xd:DB3m|xq:nDF7m", + fild_1 = "xw:nDF0m|xd:DB0m|xq:nDF5m", + + fxch_0 = "D9C9", + fxch_1 = "ff:D9C8r", + fxch_2 = "fFf:D9C8r|Fff:D9C8R", + + fucom_1 = "ff:DDE0r", + fucom_2 = "Fff:DDE0R", + fucomp_1 = "ff:DDE8r", + fucomp_2 = "Fff:DDE8R", + fucomi_1 = "ff:DBE8r", -- P6+ + fucomi_2 = "Fff:DBE8R", -- P6+ + fucomip_1 = "ff:DFE8r", -- P6+ + fucomip_2 = "Fff:DFE8R", -- P6+ + fcomi_1 = "ff:DBF0r", -- P6+ + fcomi_2 = "Fff:DBF0R", -- P6+ + fcomip_1 = "ff:DFF0r", -- P6+ + fcomip_2 = "Fff:DFF0R", -- P6+ + fucompp_0 = "DAE9", + fcompp_0 = "DED9", + + fldcw_1 = "xw:nD95m", + fstcw_1 = "xw:n9BD97m", + fnstcw_1 = "xw:nD97m", + fstsw_1 = "Rw:n9BDFE0|xw:n9BDD7m", + fnstsw_1 = "Rw:nDFE0|xw:nDD7m", + fclex_0 = "9BDBE2", + fnclex_0 = "DBE2", + + fnop_0 = "D9D0", + -- D9D1-D9DF: unassigned + + fchs_0 = "D9E0", + fabs_0 = "D9E1", + -- D9E2: unassigned + -- D9E3: unassigned + ftst_0 = "D9E4", + fxam_0 = "D9E5", + -- D9E6: unassigned + -- D9E7: unassigned + fld1_0 = "D9E8", + fldl2t_0 = "D9E9", + fldl2e_0 = "D9EA", + fldpi_0 = "D9EB", + fldlg2_0 = "D9EC", + fldln2_0 = "D9ED", + fldz_0 = "D9EE", + -- D9EF: unassigned + + f2xm1_0 = "D9F0", + fyl2x_0 = "D9F1", + fptan_0 = "D9F2", + fpatan_0 = "D9F3", + fxtract_0 = "D9F4", + fprem1_0 = "D9F5", + fdecstp_0 = "D9F6", + fincstp_0 = "D9F7", + fprem_0 = "D9F8", + fyl2xp1_0 = "D9F9", + fsqrt_0 = "D9FA", + fsincos_0 = "D9FB", + frndint_0 = "D9FC", + fscale_0 = "D9FD", + fsin_0 = "D9FE", + fcos_0 = "D9FF", + + -- SSE, SSE2 + andnpd_2 = "rmo:660F55rM", + andnps_2 = "rmo:0F55rM", + andpd_2 = "rmo:660F54rM", + andps_2 = "rmo:0F54rM", + clflush_1 = "x.:0FAE7m", + cmppd_3 = "rmio:660FC2rMU", + cmpps_3 = "rmio:0FC2rMU", + cmpsd_3 = "rrio:F20FC2rMU|rxi/oq:", + cmpss_3 = "rrio:F30FC2rMU|rxi/od:", + comisd_2 = "rro:660F2FrM|rx/oq:", + comiss_2 = "rro:0F2FrM|rx/od:", + cvtdq2pd_2 = "rro:F30FE6rM|rx/oq:", + cvtdq2ps_2 = "rmo:0F5BrM", + cvtpd2dq_2 = "rmo:F20FE6rM", + cvtpd2ps_2 = "rmo:660F5ArM", + cvtpi2pd_2 = "rx/oq:660F2ArM", + cvtpi2ps_2 = "rx/oq:0F2ArM", + cvtps2dq_2 = "rmo:660F5BrM", + cvtps2pd_2 = "rro:0F5ArM|rx/oq:", + cvtsd2si_2 = "rr/do:F20F2DrM|rr/qo:|rx/dq:|rxq:", + cvtsd2ss_2 = "rro:F20F5ArM|rx/oq:", + cvtsi2sd_2 = "rm/od:F20F2ArM|rm/oq:F20F2ArXM", + cvtsi2ss_2 = "rm/od:F30F2ArM|rm/oq:F30F2ArXM", + cvtss2sd_2 = "rro:F30F5ArM|rx/od:", + cvtss2si_2 = "rr/do:F20F2CrM|rr/qo:|rxd:|rx/qd:", + cvttpd2dq_2 = "rmo:660FE6rM", + cvttps2dq_2 = "rmo:F30F5BrM", + cvttsd2si_2 = "rr/do:F20F2CrM|rr/qo:|rx/dq:|rxq:", + cvttss2si_2 = "rr/do:F30F2CrM|rr/qo:|rxd:|rx/qd:", + ldmxcsr_1 = "xd:0FAE2m", + lfence_0 = "0FAEE8", + maskmovdqu_2 = "rro:660FF7rM", + mfence_0 = "0FAEF0", + movapd_2 = "rmo:660F28rM|mro:660F29Rm", + movaps_2 = "rmo:0F28rM|mro:0F29Rm", + movd_2 = "rm/od:660F6ErM|rm/oq:660F6ErXM|mr/do:660F7ERm|mr/qo:", + movdqa_2 = "rmo:660F6FrM|mro:660F7FRm", + movdqu_2 = "rmo:F30F6FrM|mro:F30F7FRm", + movhlps_2 = "rro:0F12rM", + movhpd_2 = "rx/oq:660F16rM|xr/qo:n660F17Rm", + movhps_2 = "rx/oq:0F16rM|xr/qo:n0F17Rm", + movlhps_2 = "rro:0F16rM", + movlpd_2 = "rx/oq:660F12rM|xr/qo:n660F13Rm", + movlps_2 = "rx/oq:0F12rM|xr/qo:n0F13Rm", + movmskpd_2 = "rr/do:660F50rM", + movmskps_2 = "rr/do:0F50rM", + movntdq_2 = "xro:660FE7Rm", + movnti_2 = "xrqd:0FC3Rm", + movntpd_2 = "xro:660F2BRm", + movntps_2 = "xro:0F2BRm", + movq_2 = "rro:F30F7ErM|rx/oq:|xr/qo:n660FD6Rm", + movsd_2 = "rro:F20F10rM|rx/oq:|xr/qo:nF20F11Rm", + movss_2 = "rro:F30F10rM|rx/od:|xr/do:F30F11Rm", + movupd_2 = "rmo:660F10rM|mro:660F11Rm", + movups_2 = "rmo:0F10rM|mro:0F11Rm", + orpd_2 = "rmo:660F56rM", + orps_2 = "rmo:0F56rM", + packssdw_2 = "rmo:660F6BrM", + packsswb_2 = "rmo:660F63rM", + packuswb_2 = "rmo:660F67rM", + paddb_2 = "rmo:660FFCrM", + paddd_2 = "rmo:660FFErM", + paddq_2 = "rmo:660FD4rM", + paddsb_2 = "rmo:660FECrM", + paddsw_2 = "rmo:660FEDrM", + paddusb_2 = "rmo:660FDCrM", + paddusw_2 = "rmo:660FDDrM", + paddw_2 = "rmo:660FFDrM", + pand_2 = "rmo:660FDBrM", + pandn_2 = "rmo:660FDFrM", + pause_0 = "F390", + pavgb_2 = "rmo:660FE0rM", + pavgw_2 = "rmo:660FE3rM", + pcmpeqb_2 = "rmo:660F74rM", + pcmpeqd_2 = "rmo:660F76rM", + pcmpeqw_2 = "rmo:660F75rM", + pcmpgtb_2 = "rmo:660F64rM", + pcmpgtd_2 = "rmo:660F66rM", + pcmpgtw_2 = "rmo:660F65rM", + pextrw_3 = "rri/do:660FC5rMU|xri/wo:660F3A15nrMU", -- Mem op: SSE4.1 only. + pinsrw_3 = "rri/od:660FC4rMU|rxi/ow:", + pmaddwd_2 = "rmo:660FF5rM", + pmaxsw_2 = "rmo:660FEErM", + pmaxub_2 = "rmo:660FDErM", + pminsw_2 = "rmo:660FEArM", + pminub_2 = "rmo:660FDArM", + pmovmskb_2 = "rr/do:660FD7rM", + pmulhuw_2 = "rmo:660FE4rM", + pmulhw_2 = "rmo:660FE5rM", + pmullw_2 = "rmo:660FD5rM", + pmuludq_2 = "rmo:660FF4rM", + por_2 = "rmo:660FEBrM", + prefetchnta_1 = "xb:n0F180m", + prefetcht0_1 = "xb:n0F181m", + prefetcht1_1 = "xb:n0F182m", + prefetcht2_1 = "xb:n0F183m", + psadbw_2 = "rmo:660FF6rM", + pshufd_3 = "rmio:660F70rMU", + pshufhw_3 = "rmio:F30F70rMU", + pshuflw_3 = "rmio:F20F70rMU", + pslld_2 = "rmo:660FF2rM|rio:660F726mU", + pslldq_2 = "rio:660F737mU", + psllq_2 = "rmo:660FF3rM|rio:660F736mU", + psllw_2 = "rmo:660FF1rM|rio:660F716mU", + psrad_2 = "rmo:660FE2rM|rio:660F724mU", + psraw_2 = "rmo:660FE1rM|rio:660F714mU", + psrld_2 = "rmo:660FD2rM|rio:660F722mU", + psrldq_2 = "rio:660F733mU", + psrlq_2 = "rmo:660FD3rM|rio:660F732mU", + psrlw_2 = "rmo:660FD1rM|rio:660F712mU", + psubb_2 = "rmo:660FF8rM", + psubd_2 = "rmo:660FFArM", + psubq_2 = "rmo:660FFBrM", + psubsb_2 = "rmo:660FE8rM", + psubsw_2 = "rmo:660FE9rM", + psubusb_2 = "rmo:660FD8rM", + psubusw_2 = "rmo:660FD9rM", + psubw_2 = "rmo:660FF9rM", + punpckhbw_2 = "rmo:660F68rM", + punpckhdq_2 = "rmo:660F6ArM", + punpckhqdq_2 = "rmo:660F6DrM", + punpckhwd_2 = "rmo:660F69rM", + punpcklbw_2 = "rmo:660F60rM", + punpckldq_2 = "rmo:660F62rM", + punpcklqdq_2 = "rmo:660F6CrM", + punpcklwd_2 = "rmo:660F61rM", + pxor_2 = "rmo:660FEFrM", + rcpps_2 = "rmo:0F53rM", + rcpss_2 = "rro:F30F53rM|rx/od:", + rsqrtps_2 = "rmo:0F52rM", + rsqrtss_2 = "rmo:F30F52rM", + sfence_0 = "0FAEF8", + shufpd_3 = "rmio:660FC6rMU", + shufps_3 = "rmio:0FC6rMU", + stmxcsr_1 = "xd:0FAE3m", + ucomisd_2 = "rro:660F2ErM|rx/oq:", + ucomiss_2 = "rro:0F2ErM|rx/od:", + unpckhpd_2 = "rmo:660F15rM", + unpckhps_2 = "rmo:0F15rM", + unpcklpd_2 = "rmo:660F14rM", + unpcklps_2 = "rmo:0F14rM", + xorpd_2 = "rmo:660F57rM", + xorps_2 = "rmo:0F57rM", + + -- SSE3 ops + fisttp_1 = "xw:nDF1m|xd:DB1m|xq:nDD1m", + addsubpd_2 = "rmo:660FD0rM", + addsubps_2 = "rmo:F20FD0rM", + haddpd_2 = "rmo:660F7CrM", + haddps_2 = "rmo:F20F7CrM", + hsubpd_2 = "rmo:660F7DrM", + hsubps_2 = "rmo:F20F7DrM", + lddqu_2 = "rxo:F20FF0rM", + movddup_2 = "rmo:F20F12rM", + movshdup_2 = "rmo:F30F16rM", + movsldup_2 = "rmo:F30F12rM", + + -- SSSE3 ops + pabsb_2 = "rmo:660F381CrM", + pabsd_2 = "rmo:660F381ErM", + pabsw_2 = "rmo:660F381DrM", + palignr_3 = "rmio:660F3A0FrMU", + phaddd_2 = "rmo:660F3802rM", + phaddsw_2 = "rmo:660F3803rM", + phaddw_2 = "rmo:660F3801rM", + phsubd_2 = "rmo:660F3806rM", + phsubsw_2 = "rmo:660F3807rM", + phsubw_2 = "rmo:660F3805rM", + pmaddubsw_2 = "rmo:660F3804rM", + pmulhrsw_2 = "rmo:660F380BrM", + pshufb_2 = "rmo:660F3800rM", + psignb_2 = "rmo:660F3808rM", + psignd_2 = "rmo:660F380ArM", + psignw_2 = "rmo:660F3809rM", + + -- SSE4.1 ops + blendpd_3 = "rmio:660F3A0DrMU", + blendps_3 = "rmio:660F3A0CrMU", + blendvpd_3 = "rmRo:660F3815rM", + blendvps_3 = "rmRo:660F3814rM", + dppd_3 = "rmio:660F3A41rMU", + dpps_3 = "rmio:660F3A40rMU", + extractps_3 = "mri/do:660F3A17RmU|rri/qo:660F3A17RXmU", + insertps_3 = "rrio:660F3A41rMU|rxi/od:", + movntdqa_2 = "rmo:660F382ArM", + mpsadbw_3 = "rmio:660F3A42rMU", + packusdw_2 = "rmo:660F382BrM", + pblendvb_3 = "rmRo:660F3810rM", + pblendw_3 = "rmio:660F3A0ErMU", + pcmpeqq_2 = "rmo:660F3829rM", + pextrb_3 = "rri/do:660F3A14nRmU|rri/qo:|xri/bo:", + pextrd_3 = "mri/do:660F3A16RmU", + pextrq_3 = "mri/qo:660F3A16RmU", + -- pextrw is SSE2, mem operand is SSE4.1 only + phminposuw_2 = "rmo:660F3841rM", + pinsrb_3 = "rri/od:660F3A20nrMU|rxi/ob:", + pinsrd_3 = "rmi/od:660F3A22rMU", + pinsrq_3 = "rmi/oq:660F3A22rXMU", + pmaxsb_2 = "rmo:660F383CrM", + pmaxsd_2 = "rmo:660F383DrM", + pmaxud_2 = "rmo:660F383FrM", + pmaxuw_2 = "rmo:660F383ErM", + pminsb_2 = "rmo:660F3838rM", + pminsd_2 = "rmo:660F3839rM", + pminud_2 = "rmo:660F383BrM", + pminuw_2 = "rmo:660F383ArM", + pmovsxbd_2 = "rro:660F3821rM|rx/od:", + pmovsxbq_2 = "rro:660F3822rM|rx/ow:", + pmovsxbw_2 = "rro:660F3820rM|rx/oq:", + pmovsxdq_2 = "rro:660F3825rM|rx/oq:", + pmovsxwd_2 = "rro:660F3823rM|rx/oq:", + pmovsxwq_2 = "rro:660F3824rM|rx/od:", + pmovzxbd_2 = "rro:660F3831rM|rx/od:", + pmovzxbq_2 = "rro:660F3832rM|rx/ow:", + pmovzxbw_2 = "rro:660F3830rM|rx/oq:", + pmovzxdq_2 = "rro:660F3835rM|rx/oq:", + pmovzxwd_2 = "rro:660F3833rM|rx/oq:", + pmovzxwq_2 = "rro:660F3834rM|rx/od:", + pmuldq_2 = "rmo:660F3828rM", + pmulld_2 = "rmo:660F3840rM", + ptest_2 = "rmo:660F3817rM", + roundpd_3 = "rmio:660F3A09rMU", + roundps_3 = "rmio:660F3A08rMU", + roundsd_3 = "rrio:660F3A0BrMU|rxi/oq:", + roundss_3 = "rrio:660F3A0ArMU|rxi/od:", + + -- SSE4.2 ops + crc32_2 = "rmqd:F20F38F1rM|rm/dw:66F20F38F1rM|rm/db:F20F38F0rM|rm/qb:", + pcmpestri_3 = "rmio:660F3A61rMU", + pcmpestrm_3 = "rmio:660F3A60rMU", + pcmpgtq_2 = "rmo:660F3837rM", + pcmpistri_3 = "rmio:660F3A63rMU", + pcmpistrm_3 = "rmio:660F3A62rMU", + popcnt_2 = "rmqdw:F30FB8rM", + + -- SSE4a + extrq_2 = "rro:660F79rM", + extrq_3 = "riio:660F780mUU", + insertq_2 = "rro:F20F79rM", + insertq_4 = "rriio:F20F78rMUU", + lzcnt_2 = "rmqdw:F30FBDrM", + movntsd_2 = "xr/qo:nF20F2BRm", + movntss_2 = "xr/do:F30F2BRm", + -- popcnt is also in SSE4.2 +} + +------------------------------------------------------------------------------ + +-- Arithmetic ops. +for name,n in pairs{ add = 0, ["or"] = 1, adc = 2, sbb = 3, + ["and"] = 4, sub = 5, xor = 6, cmp = 7 } do + local n8 = shl(n, 3) + map_op[name.."_2"] = format( + "mr:%02XRm|rm:%02XrM|mI1qdw:81%XmI|mS1qdw:83%XmS|Ri1qdwb:%02Xri|mi1qdwb:81%Xmi", + 1+n8, 3+n8, n, n, 5+n8, n) +end + +-- Shift ops. +for name,n in pairs{ rol = 0, ror = 1, rcl = 2, rcr = 3, + shl = 4, shr = 5, sar = 7, sal = 4 } do + map_op[name.."_2"] = format("m1:D1%Xm|mC1qdwb:D3%Xm|mi:C1%XmU", n, n, n) +end + +-- Conditional ops. +for cc,n in pairs(map_cc) do + map_op["j"..cc.."_1"] = format("J.:n0F8%XJ", n) -- short: 7%X + map_op["set"..cc.."_1"] = format("mb:n0F9%X2m", n) + map_op["cmov"..cc.."_2"] = format("rmqdw:0F4%XrM", n) -- P6+ +end + +-- FP arithmetic ops. +for name,n in pairs{ add = 0, mul = 1, com = 2, comp = 3, + sub = 4, subr = 5, div = 6, divr = 7 } do + local nc = 0xc0 + shl(n, 3) + local nr = nc + (n < 4 and 0 or (n % 2 == 0 and 8 or -8)) + local fn = "f"..name + map_op[fn.."_1"] = format("ff:D8%02Xr|xd:D8%Xm|xq:nDC%Xm", nc, n, n) + if n == 2 or n == 3 then + map_op[fn.."_2"] = format("Fff:D8%02XR|Fx2d:D8%XM|Fx2q:nDC%XM", nc, n, n) + else + map_op[fn.."_2"] = format("Fff:D8%02XR|fFf:DC%02Xr|Fx2d:D8%XM|Fx2q:nDC%XM", nc, nr, n, n) + map_op[fn.."p_1"] = format("ff:DE%02Xr", nr) + map_op[fn.."p_2"] = format("fFf:DE%02Xr", nr) + end + map_op["fi"..name.."_1"] = format("xd:DA%Xm|xw:nDE%Xm", n, n) +end + +-- FP conditional moves. +for cc,n in pairs{ b=0, e=1, be=2, u=3, nb=4, ne=5, nbe=6, nu=7 } do + local nc = 0xdac0 + shl(band(n, 3), 3) + shl(band(n, 4), 6) + map_op["fcmov"..cc.."_1"] = format("ff:%04Xr", nc) -- P6+ + map_op["fcmov"..cc.."_2"] = format("Fff:%04XR", nc) -- P6+ +end + +-- SSE FP arithmetic ops. +for name,n in pairs{ sqrt = 1, add = 8, mul = 9, + sub = 12, min = 13, div = 14, max = 15 } do + map_op[name.."ps_2"] = format("rmo:0F5%XrM", n) + map_op[name.."ss_2"] = format("rro:F30F5%XrM|rx/od:", n) + map_op[name.."pd_2"] = format("rmo:660F5%XrM", n) + map_op[name.."sd_2"] = format("rro:F20F5%XrM|rx/oq:", n) +end + +------------------------------------------------------------------------------ + +-- Process pattern string. +local function dopattern(pat, args, sz, op, needrex) + local digit, addin + local opcode = 0 + local szov = sz + local narg = 1 + local rex = 0 + + -- Limit number of section buffer positions used by a single dasm_put(). + -- A single opcode needs a maximum of 5 positions. + if secpos+5 > maxsecpos then wflush() end + + -- Process each character. + for c in gmatch(pat.."|", ".") do + if match(c, "%x") then -- Hex digit. + digit = byte(c) - 48 + if digit > 48 then digit = digit - 39 + elseif digit > 16 then digit = digit - 7 end + opcode = opcode*16 + digit + addin = nil + elseif c == "n" then -- Disable operand size mods for opcode. + szov = nil + elseif c == "X" then -- Force REX.W. + rex = 8 + elseif c == "r" then -- Merge 1st operand regno. into opcode. + addin = args[1]; opcode = opcode + (addin.reg % 8) + if narg < 2 then narg = 2 end + elseif c == "R" then -- Merge 2nd operand regno. into opcode. + addin = args[2]; opcode = opcode + (addin.reg % 8) + narg = 3 + elseif c == "m" or c == "M" then -- Encode ModRM/SIB. + local s + if addin then + s = addin.reg + opcode = opcode - band(s, 7) -- Undo regno opcode merge. + else + s = band(opcode, 15) -- Undo last digit. + opcode = shr(opcode, 4) + end + local nn = c == "m" and 1 or 2 + local t = args[nn] + if narg <= nn then narg = nn + 1 end + if szov == "q" and rex == 0 then rex = rex + 8 end + if t.reg and t.reg > 7 then rex = rex + 1 end + if t.xreg and t.xreg > 7 then rex = rex + 2 end + if s > 7 then rex = rex + 4 end + if needrex then rex = rex + 16 end + wputop(szov, opcode, rex); opcode = nil + local imark = sub(pat, -1) -- Force a mark (ugly). + -- Put ModRM/SIB with regno/last digit as spare. + wputmrmsib(t, imark, s, addin and addin.vreg) + addin = nil + else + if opcode then -- Flush opcode. + if szov == "q" and rex == 0 then rex = rex + 8 end + if needrex then rex = rex + 16 end + if addin and addin.reg == -1 then + wputop(szov, opcode - 7, rex) + waction("VREG", addin.vreg); wputxb(0) + else + if addin and addin.reg > 7 then rex = rex + 1 end + wputop(szov, opcode, rex) + end + opcode = nil + end + if c == "|" then break end + if c == "o" then -- Offset (pure 32 bit displacement). + wputdarg(args[1].disp); if narg < 2 then narg = 2 end + elseif c == "O" then + wputdarg(args[2].disp); narg = 3 + else + -- Anything else is an immediate operand. + local a = args[narg] + narg = narg + 1 + local mode, imm = a.mode, a.imm + if mode == "iJ" and not match("iIJ", c) then + werror("bad operand size for label") + end + if c == "S" then + wputsbarg(imm) + elseif c == "U" then + wputbarg(imm) + elseif c == "W" then + wputwarg(imm) + elseif c == "i" or c == "I" then + if mode == "iJ" then + wputlabel("IMM_", imm, 1) + elseif mode == "iI" and c == "I" then + waction(sz == "w" and "IMM_WB" or "IMM_DB", imm) + else + wputszarg(sz, imm) + end + elseif c == "J" then + if mode == "iPJ" then + waction("REL_A", imm) -- !x64 (secpos) + else + wputlabel("REL_", imm, 2) + end + else + werror("bad char `"..c.."' in pattern `"..pat.."' for `"..op.."'") + end + end + end + end +end + +------------------------------------------------------------------------------ + +-- Mapping of operand modes to short names. Suppress output with '#'. +local map_modename = { + r = "reg", R = "eax", C = "cl", x = "mem", m = "mrm", i = "imm", + f = "stx", F = "st0", J = "lbl", ["1"] = "1", + I = "#", S = "#", O = "#", +} + +-- Return a table/string showing all possible operand modes. +local function templatehelp(template, nparams) + if nparams == 0 then return "" end + local t = {} + for tm in gmatch(template, "[^%|]+") do + local s = map_modename[sub(tm, 1, 1)] + s = s..gsub(sub(tm, 2, nparams), ".", function(c) + return ", "..map_modename[c] + end) + if not match(s, "#") then t[#t+1] = s end + end + return t +end + +-- Match operand modes against mode match part of template. +local function matchtm(tm, args) + for i=1,#args do + if not match(args[i].mode, sub(tm, i, i)) then return end + end + return true +end + +-- Handle opcodes defined with template strings. +map_op[".template__"] = function(params, template, nparams) + if not params then return templatehelp(template, nparams) end + local args = {} + + -- Zero-operand opcodes have no match part. + if #params == 0 then + dopattern(template, args, "d", params.op, nil) + return + end + + -- Determine common operand size (coerce undefined size) or flag as mixed. + local sz, szmix, needrex + for i,p in ipairs(params) do + args[i] = parseoperand(p) + local nsz = args[i].opsize + if nsz then + if sz and sz ~= nsz then szmix = true else sz = nsz end + end + local nrex = args[i].needrex + if nrex ~= nil then + if needrex == nil then + needrex = nrex + elseif needrex ~= nrex then + werror("bad mix of byte-addressable registers") + end + end + end + + -- Try all match:pattern pairs (separated by '|'). + local gotmatch, lastpat + for tm in gmatch(template, "[^%|]+") do + -- Split off size match (starts after mode match) and pattern string. + local szm, pat = match(tm, "^(.-):(.*)$", #args+1) + if pat == "" then pat = lastpat else lastpat = pat end + if matchtm(tm, args) then + local prefix = sub(szm, 1, 1) + if prefix == "/" then -- Match both operand sizes. + if args[1].opsize == sub(szm, 2, 2) and + args[2].opsize == sub(szm, 3, 3) then + dopattern(pat, args, sz, params.op, needrex) -- Process pattern. + return + end + else -- Match common operand size. + local szp = sz + if szm == "" then szm = x64 and "qdwb" or "dwb" end -- Default sizes. + if prefix == "1" then szp = args[1].opsize; szmix = nil + elseif prefix == "2" then szp = args[2].opsize; szmix = nil end + if not szmix and (prefix == "." or match(szm, szp or "#")) then + dopattern(pat, args, szp, params.op, needrex) -- Process pattern. + return + end + end + gotmatch = true + end + end + + local msg = "bad operand mode" + if gotmatch then + if szmix then + msg = "mixed operand size" + else + msg = sz and "bad operand size" or "missing operand size" + end + end + + werror(msg.." in `"..opmodestr(params.op, args).."'") +end + +------------------------------------------------------------------------------ + +-- x64-specific opcode for 64 bit immediates and displacements. +if x64 then + function map_op.mov64_2(params) + if not params then return { "reg, imm", "reg, [disp]", "[disp], reg" } end + if secpos+2 > maxsecpos then wflush() end + local opcode, op64, sz, rex + local op64 = match(params[1], "^%[%s*(.-)%s*%]$") + if op64 then + local a = parseoperand(params[2]) + if a.mode ~= "rmR" then werror("bad operand mode") end + sz = a.opsize + rex = sz == "q" and 8 or 0 + opcode = 0xa3 + else + op64 = match(params[2], "^%[%s*(.-)%s*%]$") + local a = parseoperand(params[1]) + if op64 then + if a.mode ~= "rmR" then werror("bad operand mode") end + sz = a.opsize + rex = sz == "q" and 8 or 0 + opcode = 0xa1 + else + if sub(a.mode, 1, 1) ~= "r" or a.opsize ~= "q" then + werror("bad operand mode") + end + op64 = params[2] + opcode = 0xb8 + band(a.reg, 7) -- !x64: no VREG support. + rex = a.reg > 7 and 9 or 8 + end + end + wputop(sz, opcode, rex) + waction("IMM_D", format("(unsigned int)(%s)", op64)) + waction("IMM_D", format("(unsigned int)((%s)>>32)", op64)) + end +end + +------------------------------------------------------------------------------ + +-- Pseudo-opcodes for data storage. +local function op_data(params) + if not params then return "imm..." end + local sz = sub(params.op, 2, 2) + if sz == "a" then sz = addrsize end + for _,p in ipairs(params) do + local a = parseoperand(p) + if sub(a.mode, 1, 1) ~= "i" or (a.opsize and a.opsize ~= sz) then + werror("bad mode or size in `"..p.."'") + end + if a.mode == "iJ" then + wputlabel("IMM_", a.imm, 1) + else + wputszarg(sz, a.imm) + end + if secpos+2 > maxsecpos then wflush() end + end +end + +map_op[".byte_*"] = op_data +map_op[".sbyte_*"] = op_data +map_op[".word_*"] = op_data +map_op[".dword_*"] = op_data +map_op[".aword_*"] = op_data + +------------------------------------------------------------------------------ + +-- Pseudo-opcode to mark the position where the action list is to be emitted. +map_op[".actionlist_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeactions(out, name) end) +end + +-- Pseudo-opcode to mark the position where the global enum is to be emitted. +map_op[".globals_1"] = function(params) + if not params then return "prefix" end + local prefix = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeglobals(out, prefix) end) +end + +-- Pseudo-opcode to mark the position where the global names are to be emitted. +map_op[".globalnames_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeglobalnames(out, name) end) +end + +-- Pseudo-opcode to mark the position where the extern names are to be emitted. +map_op[".externnames_1"] = function(params) + if not params then return "cvar" end + local name = params[1] -- No syntax check. You get to keep the pieces. + wline(function(out) writeexternnames(out, name) end) +end + +------------------------------------------------------------------------------ + +-- Label pseudo-opcode (converted from trailing colon form). +map_op[".label_2"] = function(params) + if not params then return "[1-9] | ->global | =>pcexpr [, addr]" end + if secpos+2 > maxsecpos then wflush() end + local a = parseoperand(params[1]) + local mode, imm = a.mode, a.imm + if type(imm) == "number" and (mode == "iJ" or (imm >= 1 and imm <= 9)) then + -- Local label (1: ... 9:) or global label (->global:). + waction("LABEL_LG", nil, 1) + wputxb(imm) + elseif mode == "iJ" then + -- PC label (=>pcexpr:). + waction("LABEL_PC", imm) + else + werror("bad label definition") + end + -- SETLABEL must immediately follow LABEL_LG/LABEL_PC. + local addr = params[2] + if addr then + local a = parseoperand(addr) + if a.mode == "iPJ" then + waction("SETLABEL", a.imm) + else + werror("bad label assignment") + end + end +end +map_op[".label_1"] = map_op[".label_2"] + +------------------------------------------------------------------------------ + +-- Alignment pseudo-opcode. +map_op[".align_1"] = function(params) + if not params then return "numpow2" end + if secpos+1 > maxsecpos then wflush() end + local align = tonumber(params[1]) or map_opsizenum[map_opsize[params[1]]] + if align then + local x = align + -- Must be a power of 2 in the range (2 ... 256). + for i=1,8 do + x = x / 2 + if x == 1 then + waction("ALIGN", nil, 1) + wputxb(align-1) -- Action byte is 2**n-1. + return + end + end + end + werror("bad alignment") +end + +-- Spacing pseudo-opcode. +map_op[".space_2"] = function(params) + if not params then return "num [, filler]" end + if secpos+1 > maxsecpos then wflush() end + waction("SPACE", params[1]) + local fill = params[2] + if fill then + fill = tonumber(fill) + if not fill or fill < 0 or fill > 255 then werror("bad filler") end + end + wputxb(fill or 0) +end +map_op[".space_1"] = map_op[".space_2"] + +------------------------------------------------------------------------------ + +-- Pseudo-opcode for (primitive) type definitions (map to C types). +map_op[".type_3"] = function(params, nparams) + if not params then + return nparams == 2 and "name, ctype" or "name, ctype, reg" + end + local name, ctype, reg = params[1], params[2], params[3] + if not match(name, "^[%a_][%w_]*$") then + werror("bad type name `"..name.."'") + end + local tp = map_type[name] + if tp then + werror("duplicate type `"..name.."'") + end + if reg and not map_reg_valid_base[reg] then + werror("bad base register `"..(map_reg_rev[reg] or reg).."'") + end + -- Add #type to defines. A bit unclean to put it in map_archdef. + map_archdef["#"..name] = "sizeof("..ctype..")" + -- Add new type and emit shortcut define. + local num = ctypenum + 1 + map_type[name] = { + ctype = ctype, + ctypefmt = format("Dt%X(%%s)", num), + reg = reg, + } + wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) + ctypenum = num +end +map_op[".type_2"] = map_op[".type_3"] + +-- Dump type definitions. +local function dumptypes(out, lvl) + local t = {} + for name in pairs(map_type) do t[#t+1] = name end + sort(t) + out:write("Type definitions:\n") + for _,name in ipairs(t) do + local tp = map_type[name] + local reg = tp.reg and map_reg_rev[tp.reg] or "" + out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) + end + out:write("\n") +end + +------------------------------------------------------------------------------ + +-- Set the current section. +function _M.section(num) + waction("SECTION") + wputxb(num) + wflush(true) -- SECTION is a terminal action. +end + +------------------------------------------------------------------------------ + +-- Dump architecture description. +function _M.dumparch(out) + out:write(format("DynASM %s version %s, released %s\n\n", + _info.arch, _info.version, _info.release)) + dumpregs(out) + dumpactions(out) +end + +-- Dump all user defined elements. +function _M.dumpdef(out, lvl) + dumptypes(out, lvl) + dumpglobals(out, lvl) + dumpexterns(out, lvl) +end + +------------------------------------------------------------------------------ + +-- Pass callbacks from/to the DynASM core. +function _M.passcb(wl, we, wf, ww) + wline, werror, wfatal, wwarn = wl, we, wf, ww + return wflush +end + +-- Setup the arch-specific module. +function _M.setup(arch, opt) + g_arch, g_opt = arch, opt +end + +-- Merge the core maps and the arch-specific maps. +function _M.mergemaps(map_coreop, map_def) + setmetatable(map_op, { __index = map_coreop }) + setmetatable(map_def, { __index = map_archdef }) + return map_op, map_def +end + +return _M + +------------------------------------------------------------------------------ + diff --git a/external/lua/luajit/src/dynasm/dynasm.lua b/external/lua/luajit/src/dynasm/dynasm.lua new file mode 100644 index 0000000000..d670f8f8b3 --- /dev/null +++ b/external/lua/luajit/src/dynasm/dynasm.lua @@ -0,0 +1,1095 @@ +------------------------------------------------------------------------------ +-- DynASM. A dynamic assembler for code generation engines. +-- Originally designed and implemented for LuaJIT. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- See below for full copyright notice. +------------------------------------------------------------------------------ + +-- Application information. +local _info = { + name = "DynASM", + description = "A dynamic assembler for code generation engines", + version = "1.3.0", + vernum = 10300, + release = "2011-05-05", + author = "Mike Pall", + url = "http://luajit.org/dynasm.html", + license = "MIT", + copyright = [[ +Copyright (C) 2005-2013 Mike Pall. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +[ MIT license: http://www.opensource.org/licenses/mit-license.php ] +]], +} + +-- Cache library functions. +local type, pairs, ipairs = type, pairs, ipairs +local pcall, error, assert = pcall, error, assert +local _s = string +local sub, match, gmatch, gsub = _s.sub, _s.match, _s.gmatch, _s.gsub +local format, rep, upper = _s.format, _s.rep, _s.upper +local _t = table +local insert, remove, concat, sort = _t.insert, _t.remove, _t.concat, _t.sort +local exit = os.exit +local io = io +local stdin, stdout, stderr = io.stdin, io.stdout, io.stderr + +------------------------------------------------------------------------------ + +-- Program options. +local g_opt = {} + +-- Global state for current file. +local g_fname, g_curline, g_indent, g_lineno, g_synclineno, g_arch +local g_errcount = 0 + +-- Write buffer for output file. +local g_wbuffer, g_capbuffer + +------------------------------------------------------------------------------ + +-- Write an output line (or callback function) to the buffer. +local function wline(line, needindent) + local buf = g_capbuffer or g_wbuffer + buf[#buf+1] = needindent and g_indent..line or line + g_synclineno = g_synclineno + 1 +end + +-- Write assembler line as a comment, if requestd. +local function wcomment(aline) + if g_opt.comment then + wline(g_opt.comment..aline..g_opt.endcomment, true) + end +end + +-- Resync CPP line numbers. +local function wsync() + if g_synclineno ~= g_lineno and g_opt.cpp then + wline("# "..g_lineno..' "'..g_fname..'"') + g_synclineno = g_lineno + end +end + +-- Dummy action flush function. Replaced with arch-specific function later. +local function wflush(term) +end + +-- Dump all buffered output lines. +local function wdumplines(out, buf) + for _,line in ipairs(buf) do + if type(line) == "string" then + assert(out:write(line, "\n")) + else + -- Special callback to dynamically insert lines after end of processing. + line(out) + end + end +end + +------------------------------------------------------------------------------ + +-- Emit an error. Processing continues with next statement. +local function werror(msg) + error(format("%s:%s: error: %s:\n%s", g_fname, g_lineno, msg, g_curline), 0) +end + +-- Emit a fatal error. Processing stops. +local function wfatal(msg) + g_errcount = "fatal" + werror(msg) +end + +-- Print a warning. Processing continues. +local function wwarn(msg) + stderr:write(format("%s:%s: warning: %s:\n%s\n", + g_fname, g_lineno, msg, g_curline)) +end + +-- Print caught error message. But suppress excessive errors. +local function wprinterr(...) + if type(g_errcount) == "number" then + -- Regular error. + g_errcount = g_errcount + 1 + if g_errcount < 21 then -- Seems to be a reasonable limit. + stderr:write(...) + elseif g_errcount == 21 then + stderr:write(g_fname, + ":*: warning: too many errors (suppressed further messages).\n") + end + else + -- Fatal error. + stderr:write(...) + return true -- Stop processing. + end +end + +------------------------------------------------------------------------------ + +-- Map holding all option handlers. +local opt_map = {} +local opt_current + +-- Print error and exit with error status. +local function opterror(...) + stderr:write("dynasm.lua: ERROR: ", ...) + stderr:write("\n") + exit(1) +end + +-- Get option parameter. +local function optparam(args) + local argn = args.argn + local p = args[argn] + if not p then + opterror("missing parameter for option `", opt_current, "'.") + end + args.argn = argn + 1 + return p +end + +------------------------------------------------------------------------------ + +-- Core pseudo-opcodes. +local map_coreop = {} +-- Dummy opcode map. Replaced by arch-specific map. +local map_op = {} + +-- Forward declarations. +local dostmt +local readfile + +------------------------------------------------------------------------------ + +-- Map for defines (initially empty, chains to arch-specific map). +local map_def = {} + +-- Pseudo-opcode to define a substitution. +map_coreop[".define_2"] = function(params, nparams) + if not params then return nparams == 1 and "name" or "name, subst" end + local name, def = params[1], params[2] or "1" + if not match(name, "^[%a_][%w_]*$") then werror("bad or duplicate define") end + map_def[name] = def +end +map_coreop[".define_1"] = map_coreop[".define_2"] + +-- Define a substitution on the command line. +function opt_map.D(args) + local namesubst = optparam(args) + local name, subst = match(namesubst, "^([%a_][%w_]*)=(.*)$") + if name then + map_def[name] = subst + elseif match(namesubst, "^[%a_][%w_]*$") then + map_def[namesubst] = "1" + else + opterror("bad define") + end +end + +-- Undefine a substitution on the command line. +function opt_map.U(args) + local name = optparam(args) + if match(name, "^[%a_][%w_]*$") then + map_def[name] = nil + else + opterror("bad define") + end +end + +-- Helper for definesubst. +local gotsubst + +local function definesubst_one(word) + local subst = map_def[word] + if subst then gotsubst = word; return subst else return word end +end + +-- Iteratively substitute defines. +local function definesubst(stmt) + -- Limit number of iterations. + for i=1,100 do + gotsubst = false + stmt = gsub(stmt, "#?[%w_]+", definesubst_one) + if not gotsubst then break end + end + if gotsubst then wfatal("recursive define involving `"..gotsubst.."'") end + return stmt +end + +-- Dump all defines. +local function dumpdefines(out, lvl) + local t = {} + for name in pairs(map_def) do + t[#t+1] = name + end + sort(t) + out:write("Defines:\n") + for _,name in ipairs(t) do + local subst = map_def[name] + if g_arch then subst = g_arch.revdef(subst) end + out:write(format(" %-20s %s\n", name, subst)) + end + out:write("\n") +end + +------------------------------------------------------------------------------ + +-- Support variables for conditional assembly. +local condlevel = 0 +local condstack = {} + +-- Evaluate condition with a Lua expression. Substitutions already performed. +local function cond_eval(cond) + local func, err + if setfenv then + func, err = loadstring("return "..cond, "=expr") + else + -- No globals. All unknown identifiers evaluate to nil. + func, err = load("return "..cond, "=expr", "t", {}) + end + if func then + if setfenv then + setfenv(func, {}) -- No globals. All unknown identifiers evaluate to nil. + end + local ok, res = pcall(func) + if ok then + if res == 0 then return false end -- Oh well. + return not not res + end + err = res + end + wfatal("bad condition: "..err) +end + +-- Skip statements until next conditional pseudo-opcode at the same level. +local function stmtskip() + local dostmt_save = dostmt + local lvl = 0 + dostmt = function(stmt) + local op = match(stmt, "^%s*(%S+)") + if op == ".if" then + lvl = lvl + 1 + elseif lvl ~= 0 then + if op == ".endif" then lvl = lvl - 1 end + elseif op == ".elif" or op == ".else" or op == ".endif" then + dostmt = dostmt_save + dostmt(stmt) + end + end +end + +-- Pseudo-opcodes for conditional assembly. +map_coreop[".if_1"] = function(params) + if not params then return "condition" end + local lvl = condlevel + 1 + local res = cond_eval(params[1]) + condlevel = lvl + condstack[lvl] = res + if not res then stmtskip() end +end + +map_coreop[".elif_1"] = function(params) + if not params then return "condition" end + if condlevel == 0 then wfatal(".elif without .if") end + local lvl = condlevel + local res = condstack[lvl] + if res then + if res == "else" then wfatal(".elif after .else") end + else + res = cond_eval(params[1]) + if res then + condstack[lvl] = res + return + end + end + stmtskip() +end + +map_coreop[".else_0"] = function(params) + if condlevel == 0 then wfatal(".else without .if") end + local lvl = condlevel + local res = condstack[lvl] + condstack[lvl] = "else" + if res then + if res == "else" then wfatal(".else after .else") end + stmtskip() + end +end + +map_coreop[".endif_0"] = function(params) + local lvl = condlevel + if lvl == 0 then wfatal(".endif without .if") end + condlevel = lvl - 1 +end + +-- Check for unfinished conditionals. +local function checkconds() + if g_errcount ~= "fatal" and condlevel ~= 0 then + wprinterr(g_fname, ":*: error: unbalanced conditional\n") + end +end + +------------------------------------------------------------------------------ + +-- Search for a file in the given path and open it for reading. +local function pathopen(path, name) + local dirsep = package and match(package.path, "\\") and "\\" or "/" + for _,p in ipairs(path) do + local fullname = p == "" and name or p..dirsep..name + local fin = io.open(fullname, "r") + if fin then + g_fname = fullname + return fin + end + end +end + +-- Include a file. +map_coreop[".include_1"] = function(params) + if not params then return "filename" end + local name = params[1] + -- Save state. Ugly, I know. but upvalues are fast. + local gf, gl, gcl, gi = g_fname, g_lineno, g_curline, g_indent + -- Read the included file. + local fatal = readfile(pathopen(g_opt.include, name) or + wfatal("include file `"..name.."' not found")) + -- Restore state. + g_synclineno = -1 + g_fname, g_lineno, g_curline, g_indent = gf, gl, gcl, gi + if fatal then wfatal("in include file") end +end + +-- Make .include and conditionals initially available, too. +map_op[".include_1"] = map_coreop[".include_1"] +map_op[".if_1"] = map_coreop[".if_1"] +map_op[".elif_1"] = map_coreop[".elif_1"] +map_op[".else_0"] = map_coreop[".else_0"] +map_op[".endif_0"] = map_coreop[".endif_0"] + +------------------------------------------------------------------------------ + +-- Support variables for macros. +local mac_capture, mac_lineno, mac_name +local mac_active = {} +local mac_list = {} + +-- Pseudo-opcode to define a macro. +map_coreop[".macro_*"] = function(mparams) + if not mparams then return "name [, params...]" end + -- Split off and validate macro name. + local name = remove(mparams, 1) + if not name then werror("missing macro name") end + if not (match(name, "^[%a_][%w_%.]*$") or match(name, "^%.[%w_%.]*$")) then + wfatal("bad macro name `"..name.."'") + end + -- Validate macro parameter names. + local mdup = {} + for _,mp in ipairs(mparams) do + if not match(mp, "^[%a_][%w_]*$") then + wfatal("bad macro parameter name `"..mp.."'") + end + if mdup[mp] then wfatal("duplicate macro parameter name `"..mp.."'") end + mdup[mp] = true + end + -- Check for duplicate or recursive macro definitions. + local opname = name.."_"..#mparams + if map_op[opname] or map_op[name.."_*"] then + wfatal("duplicate macro `"..name.."' ("..#mparams.." parameters)") + end + if mac_capture then wfatal("recursive macro definition") end + + -- Enable statement capture. + local lines = {} + mac_lineno = g_lineno + mac_name = name + mac_capture = function(stmt) -- Statement capture function. + -- Stop macro definition with .endmacro pseudo-opcode. + if not match(stmt, "^%s*.endmacro%s*$") then + lines[#lines+1] = stmt + return + end + mac_capture = nil + mac_lineno = nil + mac_name = nil + mac_list[#mac_list+1] = opname + -- Add macro-op definition. + map_op[opname] = function(params) + if not params then return mparams, lines end + -- Protect against recursive macro invocation. + if mac_active[opname] then wfatal("recursive macro invocation") end + mac_active[opname] = true + -- Setup substitution map. + local subst = {} + for i,mp in ipairs(mparams) do subst[mp] = params[i] end + local mcom + if g_opt.maccomment and g_opt.comment then + mcom = " MACRO "..name.." ("..#mparams..")" + wcomment("{"..mcom) + end + -- Loop through all captured statements + for _,stmt in ipairs(lines) do + -- Substitute macro parameters. + local st = gsub(stmt, "[%w_]+", subst) + st = definesubst(st) + st = gsub(st, "%s*%.%.%s*", "") -- Token paste a..b. + if mcom and sub(st, 1, 1) ~= "|" then wcomment(st) end + -- Emit statement. Use a protected call for better diagnostics. + local ok, err = pcall(dostmt, st) + if not ok then + -- Add the captured statement to the error. + wprinterr(err, "\n", g_indent, "| ", stmt, + "\t[MACRO ", name, " (", #mparams, ")]\n") + end + end + if mcom then wcomment("}"..mcom) end + mac_active[opname] = nil + end + end +end + +-- An .endmacro pseudo-opcode outside of a macro definition is an error. +map_coreop[".endmacro_0"] = function(params) + wfatal(".endmacro without .macro") +end + +-- Dump all macros and their contents (with -PP only). +local function dumpmacros(out, lvl) + sort(mac_list) + out:write("Macros:\n") + for _,opname in ipairs(mac_list) do + local name = sub(opname, 1, -3) + local params, lines = map_op[opname]() + out:write(format(" %-20s %s\n", name, concat(params, ", "))) + if lvl > 1 then + for _,line in ipairs(lines) do + out:write(" |", line, "\n") + end + out:write("\n") + end + end + out:write("\n") +end + +-- Check for unfinished macro definitions. +local function checkmacros() + if mac_capture then + wprinterr(g_fname, ":", mac_lineno, + ": error: unfinished .macro `", mac_name ,"'\n") + end +end + +------------------------------------------------------------------------------ + +-- Support variables for captures. +local cap_lineno, cap_name +local cap_buffers = {} +local cap_used = {} + +-- Start a capture. +map_coreop[".capture_1"] = function(params) + if not params then return "name" end + wflush() + local name = params[1] + if not match(name, "^[%a_][%w_]*$") then + wfatal("bad capture name `"..name.."'") + end + if cap_name then + wfatal("already capturing to `"..cap_name.."' since line "..cap_lineno) + end + cap_name = name + cap_lineno = g_lineno + -- Create or continue a capture buffer and start the output line capture. + local buf = cap_buffers[name] + if not buf then buf = {}; cap_buffers[name] = buf end + g_capbuffer = buf + g_synclineno = 0 +end + +-- Stop a capture. +map_coreop[".endcapture_0"] = function(params) + wflush() + if not cap_name then wfatal(".endcapture without a valid .capture") end + cap_name = nil + cap_lineno = nil + g_capbuffer = nil + g_synclineno = 0 +end + +-- Dump a capture buffer. +map_coreop[".dumpcapture_1"] = function(params) + if not params then return "name" end + wflush() + local name = params[1] + if not match(name, "^[%a_][%w_]*$") then + wfatal("bad capture name `"..name.."'") + end + cap_used[name] = true + wline(function(out) + local buf = cap_buffers[name] + if buf then wdumplines(out, buf) end + end) + g_synclineno = 0 +end + +-- Dump all captures and their buffers (with -PP only). +local function dumpcaptures(out, lvl) + out:write("Captures:\n") + for name,buf in pairs(cap_buffers) do + out:write(format(" %-20s %4s)\n", name, "("..#buf)) + if lvl > 1 then + local bar = rep("=", 76) + out:write(" ", bar, "\n") + for _,line in ipairs(buf) do + out:write(" ", line, "\n") + end + out:write(" ", bar, "\n\n") + end + end + out:write("\n") +end + +-- Check for unfinished or unused captures. +local function checkcaptures() + if cap_name then + wprinterr(g_fname, ":", cap_lineno, + ": error: unfinished .capture `", cap_name,"'\n") + return + end + for name in pairs(cap_buffers) do + if not cap_used[name] then + wprinterr(g_fname, ":*: error: missing .dumpcapture ", name ,"\n") + end + end +end + +------------------------------------------------------------------------------ + +-- Sections names. +local map_sections = {} + +-- Pseudo-opcode to define code sections. +-- TODO: Data sections, BSS sections. Needs extra C code and API. +map_coreop[".section_*"] = function(params) + if not params then return "name..." end + if #map_sections > 0 then werror("duplicate section definition") end + wflush() + for sn,name in ipairs(params) do + local opname = "."..name.."_0" + if not match(name, "^[%a][%w_]*$") or + map_op[opname] or map_op["."..name.."_*"] then + werror("bad section name `"..name.."'") + end + map_sections[#map_sections+1] = name + wline(format("#define DASM_SECTION_%s\t%d", upper(name), sn-1)) + map_op[opname] = function(params) g_arch.section(sn-1) end + end + wline(format("#define DASM_MAXSECTION\t\t%d", #map_sections)) +end + +-- Dump all sections. +local function dumpsections(out, lvl) + out:write("Sections:\n") + for _,name in ipairs(map_sections) do + out:write(format(" %s\n", name)) + end + out:write("\n") +end + +------------------------------------------------------------------------------ + +-- Replacement for customized Lua, which lacks the package library. +local prefix = "" +if not require then + function require(name) + local fp = assert(io.open(prefix..name..".lua")) + local s = fp:read("*a") + assert(fp:close()) + return assert(loadstring(s, "@"..name..".lua"))() + end +end + +-- Load architecture-specific module. +local function loadarch(arch) + if not match(arch, "^[%w_]+$") then return "bad arch name" end + local ok, m_arch = pcall(require, "dasm_"..arch) + if not ok then return "cannot load module: "..m_arch end + g_arch = m_arch + wflush = m_arch.passcb(wline, werror, wfatal, wwarn) + m_arch.setup(arch, g_opt) + map_op, map_def = m_arch.mergemaps(map_coreop, map_def) +end + +-- Dump architecture description. +function opt_map.dumparch(args) + local name = optparam(args) + if not g_arch then + local err = loadarch(name) + if err then opterror(err) end + end + + local t = {} + for name in pairs(map_coreop) do t[#t+1] = name end + for name in pairs(map_op) do t[#t+1] = name end + sort(t) + + local out = stdout + local _arch = g_arch._info + out:write(format("%s version %s, released %s, %s\n", + _info.name, _info.version, _info.release, _info.url)) + g_arch.dumparch(out) + + local pseudo = true + out:write("Pseudo-Opcodes:\n") + for _,sname in ipairs(t) do + local name, nparam = match(sname, "^(.+)_([0-9%*])$") + if name then + if pseudo and sub(name, 1, 1) ~= "." then + out:write("\nOpcodes:\n") + pseudo = false + end + local f = map_op[sname] + local s + if nparam ~= "*" then nparam = nparam + 0 end + if nparam == 0 then + s = "" + elseif type(f) == "string" then + s = map_op[".template__"](nil, f, nparam) + else + s = f(nil, nparam) + end + if type(s) == "table" then + for _,s2 in ipairs(s) do + out:write(format(" %-12s %s\n", name, s2)) + end + else + out:write(format(" %-12s %s\n", name, s)) + end + end + end + out:write("\n") + exit(0) +end + +-- Pseudo-opcode to set the architecture. +-- Only initially available (map_op is replaced when called). +map_op[".arch_1"] = function(params) + if not params then return "name" end + local err = loadarch(params[1]) + if err then wfatal(err) end +end + +-- Dummy .arch pseudo-opcode to improve the error report. +map_coreop[".arch_1"] = function(params) + if not params then return "name" end + wfatal("duplicate .arch statement") +end + +------------------------------------------------------------------------------ + +-- Dummy pseudo-opcode. Don't confuse '.nop' with 'nop'. +map_coreop[".nop_*"] = function(params) + if not params then return "[ignored...]" end +end + +-- Pseudo-opcodes to raise errors. +map_coreop[".error_1"] = function(params) + if not params then return "message" end + werror(params[1]) +end + +map_coreop[".fatal_1"] = function(params) + if not params then return "message" end + wfatal(params[1]) +end + +-- Dump all user defined elements. +local function dumpdef(out) + local lvl = g_opt.dumpdef + if lvl == 0 then return end + dumpsections(out, lvl) + dumpdefines(out, lvl) + if g_arch then g_arch.dumpdef(out, lvl) end + dumpmacros(out, lvl) + dumpcaptures(out, lvl) +end + +------------------------------------------------------------------------------ + +-- Helper for splitstmt. +local splitlvl + +local function splitstmt_one(c) + if c == "(" then + splitlvl = ")"..splitlvl + elseif c == "[" then + splitlvl = "]"..splitlvl + elseif c == "{" then + splitlvl = "}"..splitlvl + elseif c == ")" or c == "]" or c == "}" then + if sub(splitlvl, 1, 1) ~= c then werror("unbalanced (), [] or {}") end + splitlvl = sub(splitlvl, 2) + elseif splitlvl == "" then + return " \0 " + end + return c +end + +-- Split statement into (pseudo-)opcode and params. +local function splitstmt(stmt) + -- Convert label with trailing-colon into .label statement. + local label = match(stmt, "^%s*(.+):%s*$") + if label then return ".label", {label} end + + -- Split at commas and equal signs, but obey parentheses and brackets. + splitlvl = "" + stmt = gsub(stmt, "[,%(%)%[%]{}]", splitstmt_one) + if splitlvl ~= "" then werror("unbalanced () or []") end + + -- Split off opcode. + local op, other = match(stmt, "^%s*([^%s%z]+)%s*(.*)$") + if not op then werror("bad statement syntax") end + + -- Split parameters. + local params = {} + for p in gmatch(other, "%s*(%Z+)%z?") do + params[#params+1] = gsub(p, "%s+$", "") + end + if #params > 16 then werror("too many parameters") end + + params.op = op + return op, params +end + +-- Process a single statement. +dostmt = function(stmt) + -- Ignore empty statements. + if match(stmt, "^%s*$") then return end + + -- Capture macro defs before substitution. + if mac_capture then return mac_capture(stmt) end + stmt = definesubst(stmt) + + -- Emit C code without parsing the line. + if sub(stmt, 1, 1) == "|" then + local tail = sub(stmt, 2) + wflush() + if sub(tail, 1, 2) == "//" then wcomment(tail) else wline(tail, true) end + return + end + + -- Split into (pseudo-)opcode and params. + local op, params = splitstmt(stmt) + + -- Get opcode handler (matching # of parameters or generic handler). + local f = map_op[op.."_"..#params] or map_op[op.."_*"] + if not f then + if not g_arch then wfatal("first statement must be .arch") end + -- Improve error report. + for i=0,9 do + if map_op[op.."_"..i] then + werror("wrong number of parameters for `"..op.."'") + end + end + werror("unknown statement `"..op.."'") + end + + -- Call opcode handler or special handler for template strings. + if type(f) == "string" then + map_op[".template__"](params, f) + else + f(params) + end +end + +-- Process a single line. +local function doline(line) + if g_opt.flushline then wflush() end + + -- Assembler line? + local indent, aline = match(line, "^(%s*)%|(.*)$") + if not aline then + -- No, plain C code line, need to flush first. + wflush() + wsync() + wline(line, false) + return + end + + g_indent = indent -- Remember current line indentation. + + -- Emit C code (even from macros). Avoids echo and line parsing. + if sub(aline, 1, 1) == "|" then + if not mac_capture then + wsync() + elseif g_opt.comment then + wsync() + wcomment(aline) + end + dostmt(aline) + return + end + + -- Echo assembler line as a comment. + if g_opt.comment then + wsync() + wcomment(aline) + end + + -- Strip assembler comments. + aline = gsub(aline, "//.*$", "") + + -- Split line into statements at semicolons. + if match(aline, ";") then + for stmt in gmatch(aline, "[^;]+") do dostmt(stmt) end + else + dostmt(aline) + end +end + +------------------------------------------------------------------------------ + +-- Write DynASM header. +local function dasmhead(out) + out:write(format([[ +/* +** This file has been pre-processed with DynASM. +** %s +** DynASM version %s, DynASM %s version %s +** DO NOT EDIT! The original file is in "%s". +*/ + +#if DASM_VERSION != %d +#error "Version mismatch between DynASM and included encoding engine" +#endif + +]], _info.url, + _info.version, g_arch._info.arch, g_arch._info.version, + g_fname, _info.vernum)) +end + +-- Read input file. +readfile = function(fin) + g_indent = "" + g_lineno = 0 + g_synclineno = -1 + + -- Process all lines. + for line in fin:lines() do + g_lineno = g_lineno + 1 + g_curline = line + local ok, err = pcall(doline, line) + if not ok and wprinterr(err, "\n") then return true end + end + wflush() + + -- Close input file. + assert(fin == stdin or fin:close()) +end + +-- Write output file. +local function writefile(outfile) + local fout + + -- Open output file. + if outfile == nil or outfile == "-" then + fout = stdout + else + fout = assert(io.open(outfile, "w")) + end + + -- Write all buffered lines + wdumplines(fout, g_wbuffer) + + -- Close output file. + assert(fout == stdout or fout:close()) + + -- Optionally dump definitions. + dumpdef(fout == stdout and stderr or stdout) +end + +-- Translate an input file to an output file. +local function translate(infile, outfile) + g_wbuffer = {} + g_indent = "" + g_lineno = 0 + g_synclineno = -1 + + -- Put header. + wline(dasmhead) + + -- Read input file. + local fin + if infile == "-" then + g_fname = "(stdin)" + fin = stdin + else + g_fname = infile + fin = assert(io.open(infile, "r")) + end + readfile(fin) + + -- Check for errors. + if not g_arch then + wprinterr(g_fname, ":*: error: missing .arch directive\n") + end + checkconds() + checkmacros() + checkcaptures() + + if g_errcount ~= 0 then + stderr:write(g_fname, ":*: info: ", g_errcount, " error", + (type(g_errcount) == "number" and g_errcount > 1) and "s" or "", + " in input file -- no output file generated.\n") + dumpdef(stderr) + exit(1) + end + + -- Write output file. + writefile(outfile) +end + +------------------------------------------------------------------------------ + +-- Print help text. +function opt_map.help() + stdout:write("DynASM -- ", _info.description, ".\n") + stdout:write("DynASM ", _info.version, " ", _info.release, " ", _info.url, "\n") + stdout:write[[ + +Usage: dynasm [OPTION]... INFILE.dasc|- + + -h, --help Display this help text. + -V, --version Display version and copyright information. + + -o, --outfile FILE Output file name (default is stdout). + -I, --include DIR Add directory to the include search path. + + -c, --ccomment Use /* */ comments for assembler lines. + -C, --cppcomment Use // comments for assembler lines (default). + -N, --nocomment Suppress assembler lines in output. + -M, --maccomment Show macro expansions as comments (default off). + + -L, --nolineno Suppress CPP line number information in output. + -F, --flushline Flush action list for every line. + + -D NAME[=SUBST] Define a substitution. + -U NAME Undefine a substitution. + + -P, --dumpdef Dump defines, macros, etc. Repeat for more output. + -A, --dumparch ARCH Load architecture ARCH and dump description. +]] + exit(0) +end + +-- Print version information. +function opt_map.version() + stdout:write(format("%s version %s, released %s\n%s\n\n%s", + _info.name, _info.version, _info.release, _info.url, _info.copyright)) + exit(0) +end + +-- Misc. options. +function opt_map.outfile(args) g_opt.outfile = optparam(args) end +function opt_map.include(args) insert(g_opt.include, 1, optparam(args)) end +function opt_map.ccomment() g_opt.comment = "/*|"; g_opt.endcomment = " */" end +function opt_map.cppcomment() g_opt.comment = "//|"; g_opt.endcomment = "" end +function opt_map.nocomment() g_opt.comment = false end +function opt_map.maccomment() g_opt.maccomment = true end +function opt_map.nolineno() g_opt.cpp = false end +function opt_map.flushline() g_opt.flushline = true end +function opt_map.dumpdef() g_opt.dumpdef = g_opt.dumpdef + 1 end + +------------------------------------------------------------------------------ + +-- Short aliases for long options. +local opt_alias = { + h = "help", ["?"] = "help", V = "version", + o = "outfile", I = "include", + c = "ccomment", C = "cppcomment", N = "nocomment", M = "maccomment", + L = "nolineno", F = "flushline", + P = "dumpdef", A = "dumparch", +} + +-- Parse single option. +local function parseopt(opt, args) + opt_current = #opt == 1 and "-"..opt or "--"..opt + local f = opt_map[opt] or opt_map[opt_alias[opt]] + if not f then + opterror("unrecognized option `", opt_current, "'. Try `--help'.\n") + end + f(args) +end + +-- Parse arguments. +local function parseargs(args) + -- Default options. + g_opt.comment = "//|" + g_opt.endcomment = "" + g_opt.cpp = true + g_opt.dumpdef = 0 + g_opt.include = { "" } + + -- Process all option arguments. + args.argn = 1 + repeat + local a = args[args.argn] + if not a then break end + local lopt, opt = match(a, "^%-(%-?)(.+)") + if not opt then break end + args.argn = args.argn + 1 + if lopt == "" then + -- Loop through short options. + for o in gmatch(opt, ".") do parseopt(o, args) end + else + -- Long option. + parseopt(opt, args) + end + until false + + -- Check for proper number of arguments. + local nargs = #args - args.argn + 1 + if nargs ~= 1 then + if nargs == 0 then + if g_opt.dumpdef > 0 then return dumpdef(stdout) end + end + opt_map.help() + end + + -- Translate a single input file to a single output file + -- TODO: Handle multiple files? + translate(args[args.argn], g_opt.outfile) +end + +------------------------------------------------------------------------------ + +-- Add the directory dynasm.lua resides in to the Lua module search path. +local arg = arg +if arg and arg[0] then + prefix = match(arg[0], "^(.*[/\\])") + if package and prefix then package.path = prefix.."?.lua;"..package.path end +end + +-- Start DynASM. +parseargs{...} + +------------------------------------------------------------------------------ + diff --git a/external/lua/luajit/src/etc/luajit.1 b/external/lua/luajit/src/etc/luajit.1 new file mode 100644 index 0000000000..6489bd3b53 --- /dev/null +++ b/external/lua/luajit/src/etc/luajit.1 @@ -0,0 +1,88 @@ +.TH luajit 1 "" "" "LuaJIT documentation" +.SH NAME +luajit \- Just-In-Time Compiler for the Lua Language +\fB +.SH SYNOPSIS +.B luajit +[\fIoptions\fR]... [\fIscript\fR [\fIargs\fR]...] +.SH "WEB SITE" +.IR http://luajit.org +.SH DESCRIPTION +.PP +This is the command-line program to run Lua programs with \fBLuaJIT\fR. +.PP +\fBLuaJIT\fR is a just-in-time (JIT) compiler for the Lua language. +The virtual machine (VM) is based on a fast interpreter combined with +a trace compiler. It can significantly improve the performance of Lua programs. +.PP +\fBLuaJIT\fR is API\- and ABI-compatible with the VM of the standard +Lua\ 5.1 interpreter. When embedding the VM into an application, +the built library can be used as a drop-in replacement. +.SH OPTIONS +.TP +.BI "\-e " chunk +Run the given chunk of Lua code. +.TP +.BI "\-l " library +Load the named library, just like \fBrequire("\fR\fIlibrary\fR\fB")\fR. +.TP +.BI "\-b " ... +Save or list bytecode. Run without arguments to get help on options. +.TP +.BI "\-j " command +Perform LuaJIT control command (optional space after \fB\-j\fR). +.TP +.BI "\-O" [opt] +Control LuaJIT optimizations. +.TP +.B "\-i" +Run in interactive mode. +.TP +.B "\-v" +Show \fBLuaJIT\fR version. +.TP +.B "\-E" +Ignore environment variables. +.TP +.B "\-\-" +Stop processing options. +.TP +.B "\-" +Read script from stdin instead. +.PP +After all options are processed, the given \fIscript\fR is run. +The arguments are passed in the global \fIarg\fR table. +.PP +Interactive mode is only entered, if no \fIscript\fR and no \fB\-e\fR +option is given. Interactive mode can be left with EOF (\fICtrl\-Z\fB). +.SH EXAMPLES +.TP +luajit hello.lua world + +Prints "Hello world", assuming \fIhello.lua\fR contains: +.br + print("Hello", arg[1]) +.TP +luajit \-e "local x=0; for i=1,1e9 do x=x+i end; print(x)" + +Calculates the sum of the numbers from 1 to 1000000000. +.br +And finishes in a reasonable amount of time, too. +.TP +luajit \-jv \-e "for i=1,10 do for j=1,10 do for k=1,100 do end end end" + +Runs some nested loops and shows the resulting traces. +.SH COPYRIGHT +.PP +\fBLuaJIT\fR is Copyright \(co 2005-2013 Mike Pall. +.br +\fBLuaJIT\fR is open source software, released under the MIT license. +.SH SEE ALSO +.PP +More details in the provided HTML docs or at: +.IR http://luajit.org +.br +More about the Lua language can be found at: +.IR http://lua.org/docs.html +.PP +lua(1) diff --git a/external/lua/luajit/src/etc/luajit.pc b/external/lua/luajit/src/etc/luajit.pc new file mode 100644 index 0000000000..2ce6e94cd1 --- /dev/null +++ b/external/lua/luajit/src/etc/luajit.pc @@ -0,0 +1,24 @@ +# Package information for LuaJIT to be used by pkg-config. +majver=2 +minver=0 +relver=1 +version=${majver}.${minver}.${relver} +abiver=5.1 + +prefix=/usr/local +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +libname=luajit-${abiver} +includedir=${prefix}/include/luajit-${majver}.${minver} + +INSTALL_LMOD=${prefix}/share/lua/${abiver} +INSTALL_CMOD=${prefix}/lib/lua/${abiver} + +Name: LuaJIT +Description: Just-in-time compiler for Lua +URL: http://luajit.org +Version: ${version} +Requires: +Libs: -L${libdir} -l${libname} +Libs.private: -Wl,-E -lm -ldl +Cflags: -I${includedir} diff --git a/external/lua/luajit/src/src/Makefile.dep b/external/lua/luajit/src/src/Makefile.dep new file mode 100644 index 0000000000..5d91723a76 --- /dev/null +++ b/external/lua/luajit/src/src/Makefile.dep @@ -0,0 +1,226 @@ +lib_aux.o: lib_aux.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \ + lj_arch.h lj_err.h lj_errmsg.h lj_state.h lj_trace.h lj_jit.h lj_ir.h \ + lj_dispatch.h lj_bc.h lj_traceerr.h lj_lib.h lj_alloc.h +lib_base.o: lib_base.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ + lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h \ + lj_tab.h lj_meta.h lj_state.h lj_ctype.h lj_cconv.h lj_bc.h lj_ff.h \ + lj_ffdef.h lj_dispatch.h lj_jit.h lj_ir.h lj_char.h lj_strscan.h \ + lj_lib.h lj_libdef.h +lib_bit.o: lib_bit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ + lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_lib.h lj_libdef.h +lib_debug.o: lib_debug.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ + lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_lib.h \ + lj_libdef.h +lib_ffi.o: lib_ffi.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ + lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h \ + lj_ctype.h lj_cparse.h lj_cdata.h lj_cconv.h lj_carith.h lj_ccall.h \ + lj_ccallback.h lj_clib.h lj_ff.h lj_ffdef.h lj_lib.h lj_libdef.h +lib_init.o: lib_init.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h +lib_io.o: lib_io.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ + lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_state.h lj_ff.h lj_ffdef.h \ + lj_lib.h lj_libdef.h +lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h \ + lj_obj.h lj_def.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h \ + lj_bc.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_target.h \ + lj_target_*.h lj_dispatch.h lj_vm.h lj_vmevent.h lj_lib.h luajit.h \ + lj_libdef.h +lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ + lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_libdef.h +lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ + lj_arch.h lj_err.h lj_errmsg.h lj_lib.h lj_libdef.h +lib_package.o: lib_package.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ + lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h +lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ + lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h \ + lj_meta.h lj_state.h lj_ff.h lj_ffdef.h lj_bcdump.h lj_lex.h lj_char.h \ + lj_lib.h lj_libdef.h +lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ + lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_lib.h \ + lj_libdef.h +lj_alloc.o: lj_alloc.c lj_def.h lua.h luaconf.h lj_arch.h lj_alloc.h +lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h lj_udata.h \ + lj_meta.h lj_state.h lj_bc.h lj_frame.h lj_trace.h lj_jit.h lj_ir.h \ + lj_dispatch.h lj_traceerr.h lj_vm.h lj_strscan.h +lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h lj_ir.h lj_jit.h \ + lj_ircall.h lj_iropt.h lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h \ + lj_snap.h lj_asm.h lj_vm.h lj_target.h lj_target_*.h lj_emit_*.h \ + lj_asm_*.h +lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \ + lj_bcdef.h +lj_bcread.o: lj_bcread.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_bc.h lj_ctype.h \ + lj_cdata.h lualib.h lj_lex.h lj_bcdump.h lj_state.h +lj_bcwrite.o: lj_bcwrite.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_str.h lj_bc.h lj_ctype.h lj_dispatch.h lj_jit.h lj_ir.h \ + lj_bcdump.h lj_lex.h lj_err.h lj_errmsg.h lj_vm.h +lj_carith.o: lj_carith.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_meta.h lj_ctype.h lj_cconv.h \ + lj_cdata.h lj_carith.h +lj_ccall.o: lj_ccall.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_cconv.h \ + lj_cdata.h lj_ccall.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \ + lj_traceerr.h +lj_ccallback.o: lj_ccallback.c lj_obj.h lua.h luaconf.h lj_def.h \ + lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_state.h lj_frame.h \ + lj_bc.h lj_ctype.h lj_cconv.h lj_ccall.h lj_ccallback.h lj_target.h \ + lj_target_*.h lj_mcode.h lj_jit.h lj_ir.h lj_trace.h lj_dispatch.h \ + lj_traceerr.h lj_vm.h +lj_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_err.h lj_errmsg.h lj_tab.h lj_ctype.h lj_gc.h lj_cdata.h lj_cconv.h \ + lj_ccallback.h +lj_cdata.o: lj_cdata.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_cconv.h \ + lj_cdata.h +lj_char.o: lj_char.c lj_char.h lj_def.h lua.h luaconf.h +lj_clib.o: lj_clib.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_err.h lj_errmsg.h lj_tab.h lj_str.h lj_udata.h lj_ctype.h lj_cconv.h \ + lj_cdata.h lj_clib.h +lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \ + lj_bc.h lj_vm.h lj_char.h lj_strscan.h +lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ctype.h \ + lj_gc.h lj_cdata.h lj_cparse.h lj_cconv.h lj_clib.h lj_ccall.h lj_ff.h \ + lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \ + lj_dispatch.h lj_traceerr.h lj_record.h lj_ffrecord.h lj_snap.h \ + lj_crecord.h +lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_ccallback.h +lj_debug.o: lj_debug.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_state.h lj_frame.h \ + lj_bc.h lj_jit.h lj_ir.h +lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_err.h lj_errmsg.h lj_func.h lj_str.h lj_tab.h lj_meta.h lj_debug.h \ + lj_state.h lj_frame.h lj_bc.h lj_ff.h lj_ffdef.h lj_jit.h lj_ir.h \ + lj_ccallback.h lj_ctype.h lj_gc.h lj_trace.h lj_dispatch.h lj_traceerr.h \ + lj_vm.h luajit.h +lj_err.o: lj_err.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_err.h \ + lj_errmsg.h lj_debug.h lj_str.h lj_func.h lj_state.h lj_frame.h lj_bc.h \ + lj_ff.h lj_ffdef.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h \ + lj_traceerr.h lj_vm.h +lj_ffrecord.o: lj_ffrecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ff.h \ + lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \ + lj_dispatch.h lj_traceerr.h lj_record.h lj_ffrecord.h lj_crecord.h \ + lj_vm.h lj_strscan.h lj_recdef.h +lj_func.o: lj_func.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_func.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \ + lj_traceerr.h lj_vm.h +lj_gc.o: lj_gc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_udata.h lj_meta.h \ + lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_cdata.h lj_trace.h lj_jit.h \ + lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h +lj_gdbjit.o: lj_gdbjit.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_frame.h lj_bc.h lj_jit.h \ + lj_ir.h lj_dispatch.h +lj_ir.o: lj_ir.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_str.h lj_tab.h lj_ir.h lj_jit.h lj_ircall.h lj_iropt.h lj_trace.h \ + lj_dispatch.h lj_bc.h lj_traceerr.h lj_ctype.h lj_cdata.h lj_carith.h \ + lj_vm.h lj_strscan.h lj_lib.h +lj_lex.o: lj_lex.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h lj_cdata.h lualib.h \ + lj_state.h lj_lex.h lj_parse.h lj_char.h lj_strscan.h +lj_lib.o: lj_lib.c lauxlib.h lua.h luaconf.h lj_obj.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_bc.h \ + lj_dispatch.h lj_jit.h lj_ir.h lj_vm.h lj_strscan.h lj_lib.h +lj_load.o: lj_load.c lua.h luaconf.h lauxlib.h lj_obj.h lj_def.h \ + lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_func.h lj_frame.h \ + lj_bc.h lj_vm.h lj_lex.h lj_bcdump.h lj_parse.h +lj_mcode.o: lj_mcode.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_jit.h lj_ir.h lj_mcode.h lj_trace.h lj_dispatch.h lj_bc.h \ + lj_traceerr.h lj_vm.h +lj_meta.o: lj_meta.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \ + lj_vm.h lj_strscan.h +lj_obj.o: lj_obj.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h +lj_opt_dce.o: lj_opt_dce.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_ir.h lj_jit.h lj_iropt.h +lj_opt_fold.o: lj_opt_fold.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_str.h lj_tab.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \ + lj_bc.h lj_traceerr.h lj_ctype.h lj_gc.h lj_carith.h lj_vm.h \ + lj_strscan.h lj_folddef.h +lj_opt_loop.o: lj_opt_loop.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_err.h lj_errmsg.h lj_str.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h \ + lj_dispatch.h lj_bc.h lj_traceerr.h lj_snap.h lj_vm.h +lj_opt_mem.o: lj_opt_mem.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_tab.h lj_ir.h lj_jit.h lj_iropt.h +lj_opt_narrow.o: lj_opt_narrow.c lj_obj.h lua.h luaconf.h lj_def.h \ + lj_arch.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \ + lj_traceerr.h lj_vm.h lj_strscan.h +lj_opt_sink.o: lj_opt_sink.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_ir.h lj_jit.h lj_iropt.h lj_target.h lj_target_*.h +lj_opt_split.o: lj_opt_split.c lj_obj.h lua.h luaconf.h lj_def.h \ + lj_arch.h lj_err.h lj_errmsg.h lj_str.h lj_ir.h lj_jit.h lj_ircall.h \ + lj_iropt.h lj_vm.h +lj_parse.o: lj_parse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h \ + lj_state.h lj_bc.h lj_ctype.h lj_lex.h lj_parse.h lj_vm.h lj_vmevent.h +lj_record.o: lj_record.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_meta.h lj_frame.h lj_bc.h \ + lj_ctype.h lj_gc.h lj_ff.h lj_ffdef.h lj_ir.h lj_jit.h lj_ircall.h \ + lj_iropt.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_record.h \ + lj_ffrecord.h lj_snap.h lj_vm.h +lj_snap.o: lj_snap.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_tab.h lj_state.h lj_frame.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h \ + lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h lj_target.h \ + lj_target_*.h lj_ctype.h lj_cdata.h +lj_state.o: lj_state.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h lj_meta.h \ + lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_trace.h lj_jit.h lj_ir.h \ + lj_dispatch.h lj_traceerr.h lj_vm.h lj_lex.h lj_alloc.h +lj_str.o: lj_str.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_err.h lj_errmsg.h lj_str.h lj_state.h lj_char.h +lj_strscan.o: lj_strscan.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_char.h lj_strscan.h +lj_tab.o: lj_tab.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ + lj_err.h lj_errmsg.h lj_tab.h +lj_trace.o: lj_trace.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_frame.h lj_bc.h \ + lj_state.h lj_ir.h lj_jit.h lj_iropt.h lj_mcode.h lj_trace.h \ + lj_dispatch.h lj_traceerr.h lj_snap.h lj_gdbjit.h lj_record.h lj_asm.h \ + lj_vm.h lj_vmevent.h lj_target.h lj_target_*.h +lj_udata.o: lj_udata.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_gc.h lj_udata.h +lj_vmevent.o: lj_vmevent.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_str.h lj_tab.h lj_state.h lj_dispatch.h lj_bc.h lj_jit.h lj_ir.h \ + lj_vm.h lj_vmevent.h +lj_vmmath.o: lj_vmmath.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ + lj_ir.h lj_vm.h +ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \ + lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h \ + lj_udata.h lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_ctype.h lj_cdata.h \ + lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h lj_err.c \ + lj_debug.h lj_ff.h lj_ffdef.h lj_char.c lj_char.h lj_bc.c lj_bcdef.h \ + lj_obj.c lj_str.c lj_tab.c lj_func.c lj_udata.c lj_meta.c lj_strscan.h \ + lj_debug.c lj_state.c lj_lex.h lj_alloc.h lj_dispatch.c lj_ccallback.h \ + luajit.h lj_vmevent.c lj_vmevent.h lj_vmmath.c lj_strscan.c lj_api.c \ + lj_lex.c lualib.h lj_parse.h lj_parse.c lj_bcread.c lj_bcdump.h \ + lj_bcwrite.c lj_load.c lj_ctype.c lj_cdata.c lj_cconv.h lj_cconv.c \ + lj_ccall.c lj_ccall.h lj_ccallback.c lj_target.h lj_target_*.h \ + lj_mcode.h lj_carith.c lj_carith.h lj_clib.c lj_clib.h lj_cparse.c \ + lj_cparse.h lj_lib.c lj_lib.h lj_ir.c lj_ircall.h lj_iropt.h \ + lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c \ + lj_opt_loop.c lj_snap.h lj_opt_split.c lj_opt_sink.c lj_mcode.c \ + lj_snap.c lj_record.c lj_record.h lj_ffrecord.h lj_crecord.c \ + lj_crecord.h lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h lj_emit_*.h \ + lj_asm_*.h lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c \ + lib_base.c lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c \ + lib_os.c lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_ffi.c \ + lib_init.c +luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h +host/buildvm.o: host/buildvm.c host/buildvm.h lj_def.h lua.h luaconf.h \ + lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_gc.h lj_obj.h lj_bc.h lj_ir.h \ + lj_ircall.h lj_ir.h lj_jit.h lj_frame.h lj_bc.h lj_dispatch.h lj_ctype.h \ + lj_gc.h lj_ccall.h lj_ctype.h luajit.h \ + host/buildvm_arch.h lj_traceerr.h +host/buildvm_asm.o: host/buildvm_asm.c host/buildvm.h lj_def.h lua.h luaconf.h \ + lj_arch.h lj_bc.h lj_def.h lj_arch.h +host/buildvm_fold.o: host/buildvm_fold.c host/buildvm.h lj_def.h lua.h \ + luaconf.h lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_ir.h lj_obj.h +host/buildvm_lib.o: host/buildvm_lib.c host/buildvm.h lj_def.h lua.h luaconf.h \ + lj_arch.h lj_obj.h lj_def.h lj_arch.h lj_lib.h lj_obj.h +host/buildvm_peobj.o: host/buildvm_peobj.c host/buildvm.h lj_def.h lua.h \ + luaconf.h lj_arch.h lj_bc.h lj_def.h lj_arch.h +host/minilua.o: host/minilua.c diff --git a/external/lua/luajit/src/src/host/README b/external/lua/luajit/src/src/host/README new file mode 100644 index 0000000000..abfcdaa76e --- /dev/null +++ b/external/lua/luajit/src/src/host/README @@ -0,0 +1,4 @@ +The files in this directory are only used during the build process of LuaJIT. +For cross-compilation, they must be executed on the host, not on the target. + +These files should NOT be installed! diff --git a/external/lua/luajit/src/src/host/buildvm.c b/external/lua/luajit/src/src/host/buildvm.c new file mode 100644 index 0000000000..ba8fbcba57 --- /dev/null +++ b/external/lua/luajit/src/src/host/buildvm.c @@ -0,0 +1,510 @@ +/* +** LuaJIT VM builder. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** This is a tool to build the hand-tuned assembler code required for +** LuaJIT's bytecode interpreter. It supports a variety of output formats +** to feed different toolchains (see usage() below). +** +** This tool is not particularly optimized because it's only used while +** _building_ LuaJIT. There's no point in distributing or installing it. +** Only the object code generated by this tool is linked into LuaJIT. +** +** Caveat: some memory is not free'd, error handling is lazy. +** It's a one-shot tool -- any effort fixing this would be wasted. +*/ + +#include "buildvm.h" +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_bc.h" +#include "lj_ir.h" +#include "lj_ircall.h" +#include "lj_frame.h" +#include "lj_dispatch.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#include "lj_ccall.h" +#endif +#include "luajit.h" + +#if defined(_WIN32) +#include +#include +#endif + +/* ------------------------------------------------------------------------ */ + +/* DynASM glue definitions. */ +#define Dst ctx +#define Dst_DECL BuildCtx *ctx +#define Dst_REF (ctx->D) +#define DASM_CHECKS 1 + +#include "../dynasm/dasm_proto.h" + +/* Glue macros for DynASM. */ +static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type); + +#define DASM_EXTERN(ctx, addr, idx, type) \ + collect_reloc(ctx, addr, idx, type) + +/* ------------------------------------------------------------------------ */ + +/* Avoid trouble if cross-compiling for an x86 target. Speed doesn't matter. */ +#define DASM_ALIGNED_WRITES 1 + +/* Embed architecture-specific DynASM encoder. */ +#if LJ_TARGET_X86ORX64 +#include "../dynasm/dasm_x86.h" +#elif LJ_TARGET_ARM +#include "../dynasm/dasm_arm.h" +#elif LJ_TARGET_PPC +#include "../dynasm/dasm_ppc.h" +#elif LJ_TARGET_PPCSPE +#include "../dynasm/dasm_ppc.h" +#elif LJ_TARGET_MIPS +#include "../dynasm/dasm_mips.h" +#else +#error "No support for this architecture (yet)" +#endif + +/* Embed generated architecture-specific backend. */ +#include "buildvm_arch.h" + +/* ------------------------------------------------------------------------ */ + +void owrite(BuildCtx *ctx, const void *ptr, size_t sz) +{ + if (fwrite(ptr, 1, sz, ctx->fp) != sz) { + fprintf(stderr, "Error: cannot write to output file: %s\n", + strerror(errno)); + exit(1); + } +} + +/* ------------------------------------------------------------------------ */ + +/* Emit code as raw bytes. Only used for DynASM debugging. */ +static void emit_raw(BuildCtx *ctx) +{ + owrite(ctx, ctx->code, ctx->codesz); +} + +/* -- Build machine code -------------------------------------------------- */ + +static const char *sym_decorate(BuildCtx *ctx, + const char *prefix, const char *suffix) +{ + char name[256]; + char *p; +#if LJ_64 + const char *symprefix = ctx->mode == BUILD_machasm ? "_" : ""; +#elif LJ_TARGET_XBOX360 + const char *symprefix = ""; +#else + const char *symprefix = ctx->mode != BUILD_elfasm ? "_" : ""; +#endif + sprintf(name, "%s%s%s", symprefix, prefix, suffix); + p = strchr(name, '@'); + if (p) { + if (!LJ_64 && (ctx->mode == BUILD_coffasm || ctx->mode == BUILD_peobj)) + name[0] = '@'; + else + *p = '\0'; + } + p = (char *)malloc(strlen(name)+1); /* MSVC doesn't like strdup. */ + strcpy(p, name); + return p; +} + +#define NRELOCSYM (sizeof(extnames)/sizeof(extnames[0])-1) + +static int relocmap[NRELOCSYM]; + +/* Collect external relocations. */ +static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type) +{ + if (ctx->nreloc >= BUILD_MAX_RELOC) { + fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n"); + exit(1); + } + if (relocmap[idx] < 0) { + relocmap[idx] = ctx->nrelocsym; + ctx->relocsym[ctx->nrelocsym] = sym_decorate(ctx, "", extnames[idx]); + ctx->nrelocsym++; + } + ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code); + ctx->reloc[ctx->nreloc].sym = relocmap[idx]; + ctx->reloc[ctx->nreloc].type = type; + ctx->nreloc++; +#if LJ_TARGET_XBOX360 + return (int)(ctx->code - addr) + 4; /* Encode symbol offset of .text. */ +#else + return 0; /* Encode symbol offset of 0. */ +#endif +} + +/* Naive insertion sort. Performance doesn't matter here. */ +static void sym_insert(BuildCtx *ctx, int32_t ofs, + const char *prefix, const char *suffix) +{ + ptrdiff_t i = ctx->nsym++; + while (i > 0) { + if (ctx->sym[i-1].ofs <= ofs) + break; + ctx->sym[i] = ctx->sym[i-1]; + i--; + } + ctx->sym[i].ofs = ofs; + ctx->sym[i].name = sym_decorate(ctx, prefix, suffix); +} + +/* Build the machine code. */ +static int build_code(BuildCtx *ctx) +{ + int status; + int i; + + /* Initialize DynASM structures. */ + ctx->nglob = GLOB__MAX; + ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *)); + memset(ctx->glob, 0, ctx->nglob*sizeof(void *)); + ctx->nreloc = 0; + + ctx->globnames = globnames; + ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *)); + ctx->nrelocsym = 0; + for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1; + + ctx->dasm_ident = DASM_IDENT; + ctx->dasm_arch = DASM_ARCH; + + dasm_init(Dst, DASM_MAXSECTION); + dasm_setupglobal(Dst, ctx->glob, ctx->nglob); + dasm_setup(Dst, build_actionlist); + + /* Call arch-specific backend to emit the code. */ + ctx->npc = build_backend(ctx); + + /* Finalize the code. */ + (void)dasm_checkstep(Dst, -1); + if ((status = dasm_link(Dst, &ctx->codesz))) return status; + ctx->code = (uint8_t *)malloc(ctx->codesz); + if ((status = dasm_encode(Dst, (void *)ctx->code))) return status; + + /* Allocate symbol table and bytecode offsets. */ + ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin"); + ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym)); + ctx->nsym = 0; + ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t)); + + /* Collect the opcodes (PC labels). */ + for (i = 0; i < ctx->npc; i++) { + int32_t ofs = dasm_getpclabel(Dst, i); + if (ofs < 0) return 0x22000000|i; + ctx->bc_ofs[i] = ofs; + if ((LJ_HASJIT || + !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP || + i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) && + (LJ_HASFFI || i != BC_KCDATA)) + sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]); + } + + /* Collect the globals (named labels). */ + for (i = 0; i < ctx->nglob; i++) { + const char *gl = globnames[i]; + int len = (int)strlen(gl); + if (!ctx->glob[i]) { + fprintf(stderr, "Error: undefined global %s\n", gl); + exit(2); + } + /* Skip the _Z symbols. */ + if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z')) + sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code), + LABEL_PREFIX, globnames[i]); + } + + /* Close the address range. */ + sym_insert(ctx, (int32_t)ctx->codesz, "", ""); + ctx->nsym--; + + dasm_free(Dst); + + return 0; +} + +/* -- Generate VM enums --------------------------------------------------- */ + +const char *const bc_names[] = { +#define BCNAME(name, ma, mb, mc, mt) #name, +BCDEF(BCNAME) +#undef BCNAME + NULL +}; + +const char *const ir_names[] = { +#define IRNAME(name, m, m1, m2) #name, +IRDEF(IRNAME) +#undef IRNAME + NULL +}; + +const char *const irt_names[] = { +#define IRTNAME(name, size) #name, +IRTDEF(IRTNAME) +#undef IRTNAME + NULL +}; + +const char *const irfpm_names[] = { +#define FPMNAME(name) #name, +IRFPMDEF(FPMNAME) +#undef FPMNAME + NULL +}; + +const char *const irfield_names[] = { +#define FLNAME(name, ofs) #name, +IRFLDEF(FLNAME) +#undef FLNAME + NULL +}; + +const char *const ircall_names[] = { +#define IRCALLNAME(cond, name, nargs, kind, type, flags) #name, +IRCALLDEF(IRCALLNAME) +#undef IRCALLNAME + NULL +}; + +static const char *const trace_errors[] = { +#define TREDEF(name, msg) msg, +#include "lj_traceerr.h" + NULL +}; + +static const char *lower(char *buf, const char *s) +{ + char *p = buf; + while (*s) { + *p++ = (*s >= 'A' && *s <= 'Z') ? *s+0x20 : *s; + s++; + } + *p = '\0'; + return buf; +} + +/* Emit C source code for bytecode-related definitions. */ +static void emit_bcdef(BuildCtx *ctx) +{ + int i; + fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n"); + fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_ofs[] = {\n"); + for (i = 0; i < ctx->npc; i++) { + if (i != 0) + fprintf(ctx->fp, ",\n"); + fprintf(ctx->fp, "%d", ctx->bc_ofs[i]); + } +} + +/* Emit VM definitions as Lua code for debug modules. */ +static void emit_vmdef(BuildCtx *ctx) +{ + char buf[80]; + int i; + fprintf(ctx->fp, "-- This is a generated file. DO NOT EDIT!\n\n"); + fprintf(ctx->fp, "module(...)\n\n"); + + fprintf(ctx->fp, "bcnames = \""); + for (i = 0; bc_names[i]; i++) fprintf(ctx->fp, "%-6s", bc_names[i]); + fprintf(ctx->fp, "\"\n\n"); + + fprintf(ctx->fp, "irnames = \""); + for (i = 0; ir_names[i]; i++) fprintf(ctx->fp, "%-6s", ir_names[i]); + fprintf(ctx->fp, "\"\n\n"); + + fprintf(ctx->fp, "irfpm = { [0]="); + for (i = 0; irfpm_names[i]; i++) + fprintf(ctx->fp, "\"%s\", ", lower(buf, irfpm_names[i])); + fprintf(ctx->fp, "}\n\n"); + + fprintf(ctx->fp, "irfield = { [0]="); + for (i = 0; irfield_names[i]; i++) { + char *p; + lower(buf, irfield_names[i]); + p = strchr(buf, '_'); + if (p) *p = '.'; + fprintf(ctx->fp, "\"%s\", ", buf); + } + fprintf(ctx->fp, "}\n\n"); + + fprintf(ctx->fp, "ircall = {\n[0]="); + for (i = 0; ircall_names[i]; i++) + fprintf(ctx->fp, "\"%s\",\n", ircall_names[i]); + fprintf(ctx->fp, "}\n\n"); + + fprintf(ctx->fp, "traceerr = {\n[0]="); + for (i = 0; trace_errors[i]; i++) + fprintf(ctx->fp, "\"%s\",\n", trace_errors[i]); + fprintf(ctx->fp, "}\n\n"); +} + +/* -- Argument parsing ---------------------------------------------------- */ + +/* Build mode names. */ +static const char *const modenames[] = { +#define BUILDNAME(name) #name, +BUILDDEF(BUILDNAME) +#undef BUILDNAME + NULL +}; + +/* Print usage information and exit. */ +static void usage(void) +{ + int i; + fprintf(stderr, LUAJIT_VERSION " VM builder.\n"); + fprintf(stderr, LUAJIT_COPYRIGHT ", " LUAJIT_URL "\n"); + fprintf(stderr, "Target architecture: " LJ_ARCH_NAME "\n\n"); + fprintf(stderr, "Usage: buildvm -m mode [-o outfile] [infiles...]\n\n"); + fprintf(stderr, "Available modes:\n"); + for (i = 0; i < BUILD__MAX; i++) + fprintf(stderr, " %s\n", modenames[i]); + exit(1); +} + +/* Parse the output mode name. */ +static BuildMode parsemode(const char *mode) +{ + int i; + for (i = 0; modenames[i]; i++) + if (!strcmp(mode, modenames[i])) + return (BuildMode)i; + usage(); + return (BuildMode)-1; +} + +/* Parse arguments. */ +static void parseargs(BuildCtx *ctx, char **argv) +{ + const char *a; + int i; + ctx->mode = (BuildMode)-1; + ctx->outname = "-"; + for (i = 1; (a = argv[i]) != NULL; i++) { + if (a[0] != '-') + break; + switch (a[1]) { + case '-': + if (a[2]) goto err; + i++; + goto ok; + case '\0': + goto ok; + case 'm': + i++; + if (a[2] || argv[i] == NULL) goto err; + ctx->mode = parsemode(argv[i]); + break; + case 'o': + i++; + if (a[2] || argv[i] == NULL) goto err; + ctx->outname = argv[i]; + break; + default: err: + usage(); + break; + } + } +ok: + ctx->args = argv+i; + if (ctx->mode == (BuildMode)-1) goto err; +} + +int main(int argc, char **argv) +{ + BuildCtx ctx_; + BuildCtx *ctx = &ctx_; + int status, binmode; + + if (sizeof(void *) != 4*LJ_32+8*LJ_64) { + fprintf(stderr,"Error: pointer size mismatch in cross-build.\n"); + fprintf(stderr,"Try: make HOST_CC=\"gcc -m32\" CROSS=...\n\n"); + return 1; + } + + UNUSED(argc); + parseargs(ctx, argv); + + if ((status = build_code(ctx))) { + fprintf(stderr,"Error: DASM error %08x\n", status); + return 1; + } + + switch (ctx->mode) { + case BUILD_peobj: + case BUILD_raw: + binmode = 1; + break; + default: + binmode = 0; + break; + } + + if (ctx->outname[0] == '-' && ctx->outname[1] == '\0') { + ctx->fp = stdout; +#if defined(_WIN32) + if (binmode) + _setmode(_fileno(stdout), _O_BINARY); /* Yuck. */ +#endif + } else if (!(ctx->fp = fopen(ctx->outname, binmode ? "wb" : "w"))) { + fprintf(stderr, "Error: cannot open output file '%s': %s\n", + ctx->outname, strerror(errno)); + exit(1); + } + + switch (ctx->mode) { + case BUILD_elfasm: + case BUILD_coffasm: + case BUILD_machasm: + emit_asm(ctx); + emit_asm_debug(ctx); + break; + case BUILD_peobj: + emit_peobj(ctx); + break; + case BUILD_raw: + emit_raw(ctx); + break; + case BUILD_bcdef: + emit_bcdef(ctx); + emit_lib(ctx); + break; + case BUILD_vmdef: + emit_vmdef(ctx); + emit_lib(ctx); + break; + case BUILD_ffdef: + case BUILD_libdef: + case BUILD_recdef: + emit_lib(ctx); + break; + case BUILD_folddef: + emit_fold(ctx); + break; + default: + break; + } + + fflush(ctx->fp); + if (ferror(ctx->fp)) { + fprintf(stderr, "Error: cannot write to output file: %s\n", + strerror(errno)); + exit(1); + } + fclose(ctx->fp); + + return 0; +} + diff --git a/external/lua/luajit/src/src/host/buildvm.h b/external/lua/luajit/src/src/host/buildvm.h new file mode 100644 index 0000000000..1a037e168f --- /dev/null +++ b/external/lua/luajit/src/src/host/buildvm.h @@ -0,0 +1,104 @@ +/* +** LuaJIT VM builder. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _BUILDVM_H +#define _BUILDVM_H + +#include +#include +#include +#include +#include + +#include "lj_def.h" +#include "lj_arch.h" + +/* Hardcoded limits. Increase as needed. */ +#define BUILD_MAX_RELOC 200 /* Max. number of relocations. */ +#define BUILD_MAX_FOLD 4096 /* Max. number of fold rules. */ + +/* Prefix for scanned library definitions. */ +#define LIBDEF_PREFIX "LJLIB_" + +/* Prefix for scanned fold definitions. */ +#define FOLDDEF_PREFIX "LJFOLD" + +/* Prefixes for generated labels. */ +#define LABEL_PREFIX "lj_" +#define LABEL_PREFIX_BC LABEL_PREFIX "BC_" +#define LABEL_PREFIX_FF LABEL_PREFIX "ff_" +#define LABEL_PREFIX_CF LABEL_PREFIX "cf_" +#define LABEL_PREFIX_FFH LABEL_PREFIX "ffh_" +#define LABEL_PREFIX_LIBCF LABEL_PREFIX "lib_cf_" +#define LABEL_PREFIX_LIBINIT LABEL_PREFIX "lib_init_" + +/* Forward declaration. */ +struct dasm_State; + +/* Build modes. */ +#define BUILDDEF(_) \ + _(elfasm) _(coffasm) _(machasm) _(peobj) _(raw) \ + _(bcdef) _(ffdef) _(libdef) _(recdef) _(vmdef) \ + _(folddef) + +typedef enum { +#define BUILDENUM(name) BUILD_##name, +BUILDDEF(BUILDENUM) +#undef BUILDENUM + BUILD__MAX +} BuildMode; + +/* Code relocation. */ +typedef struct BuildReloc { + int32_t ofs; + int sym; + int type; +} BuildReloc; + +typedef struct BuildSym { + const char *name; + int32_t ofs; +} BuildSym; + +/* Build context structure. */ +typedef struct BuildCtx { + /* DynASM state pointer. Should be first member. */ + struct dasm_State *D; + /* Parsed command line. */ + BuildMode mode; + FILE *fp; + const char *outname; + char **args; + /* Code and symbols generated by DynASM. */ + uint8_t *code; + size_t codesz; + int npc, nglob, nsym, nreloc, nrelocsym; + void **glob; + BuildSym *sym; + const char **relocsym; + int32_t *bc_ofs; + const char *beginsym; + /* Strings generated by DynASM. */ + const char *const *globnames; + const char *dasm_ident; + const char *dasm_arch; + /* Relocations. */ + BuildReloc reloc[BUILD_MAX_RELOC]; +} BuildCtx; + +extern void owrite(BuildCtx *ctx, const void *ptr, size_t sz); +extern void emit_asm(BuildCtx *ctx); +extern void emit_peobj(BuildCtx *ctx); +extern void emit_lib(BuildCtx *ctx); +extern void emit_fold(BuildCtx *ctx); + +extern const char *const bc_names[]; +extern const char *const ir_names[]; +extern const char *const irt_names[]; +extern const char *const irfpm_names[]; +extern const char *const irfield_names[]; +extern const char *const ircall_names[]; + +#endif diff --git a/external/lua/luajit/src/src/host/buildvm_asm.c b/external/lua/luajit/src/src/host/buildvm_asm.c new file mode 100644 index 0000000000..f18d1493fb --- /dev/null +++ b/external/lua/luajit/src/src/host/buildvm_asm.c @@ -0,0 +1,313 @@ +/* +** LuaJIT VM builder: Assembler source code emitter. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "buildvm.h" +#include "lj_bc.h" + +/* ------------------------------------------------------------------------ */ + +#if LJ_TARGET_X86ORX64 +/* Emit bytes piecewise as assembler text. */ +static void emit_asm_bytes(BuildCtx *ctx, uint8_t *p, int n) +{ + int i; + for (i = 0; i < n; i++) { + if ((i & 15) == 0) + fprintf(ctx->fp, "\t.byte %d", p[i]); + else + fprintf(ctx->fp, ",%d", p[i]); + if ((i & 15) == 15) putc('\n', ctx->fp); + } + if ((n & 15) != 0) putc('\n', ctx->fp); +} + +/* Emit relocation */ +static void emit_asm_reloc(BuildCtx *ctx, int type, const char *sym) +{ + switch (ctx->mode) { + case BUILD_elfasm: + if (type) + fprintf(ctx->fp, "\t.long %s-.-4\n", sym); + else + fprintf(ctx->fp, "\t.long %s\n", sym); + break; + case BUILD_coffasm: + fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", sym); + if (type) + fprintf(ctx->fp, "\t.long %s-.-4\n", sym); + else + fprintf(ctx->fp, "\t.long %s\n", sym); + break; + default: /* BUILD_machasm for relative relocations handled below. */ + fprintf(ctx->fp, "\t.long %s\n", sym); + break; + } +} + +static const char *const jccnames[] = { + "jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "ja", + "js", "jns", "jpe", "jpo", "jl", "jge", "jle", "jg" +}; + +/* Emit relocation for the incredibly stupid OSX assembler. */ +static void emit_asm_reloc_mach(BuildCtx *ctx, uint8_t *cp, int n, + const char *sym) +{ + const char *opname = NULL; + if (--n < 0) goto err; + if (cp[n] == 0xe8) { + opname = "call"; + } else if (cp[n] == 0xe9) { + opname = "jmp"; + } else if (cp[n] >= 0x80 && cp[n] <= 0x8f && n > 0 && cp[n-1] == 0x0f) { + opname = jccnames[cp[n]-0x80]; + n--; + } else { +err: + fprintf(stderr, "Error: unsupported opcode for %s symbol relocation.\n", + sym); + exit(1); + } + emit_asm_bytes(ctx, cp, n); + fprintf(ctx->fp, "\t%s %s\n", opname, sym); +} +#else +/* Emit words piecewise as assembler text. */ +static void emit_asm_words(BuildCtx *ctx, uint8_t *p, int n) +{ + int i; + for (i = 0; i < n; i += 4) { + if ((i & 15) == 0) + fprintf(ctx->fp, "\t.long 0x%08x", *(uint32_t *)(p+i)); + else + fprintf(ctx->fp, ",0x%08x", *(uint32_t *)(p+i)); + if ((i & 15) == 12) putc('\n', ctx->fp); + } + if ((n & 15) != 0) putc('\n', ctx->fp); +} + +/* Emit relocation as part of an instruction. */ +static void emit_asm_wordreloc(BuildCtx *ctx, uint8_t *p, int n, + const char *sym) +{ + uint32_t ins; + emit_asm_words(ctx, p, n-4); + ins = *(uint32_t *)(p+n-4); +#if LJ_TARGET_ARM + if ((ins & 0xff000000u) == 0xfa000000u) { + fprintf(ctx->fp, "\tblx %s\n", sym); + } else if ((ins & 0x0e000000u) == 0x0a000000u) { + fprintf(ctx->fp, "\t%s%.2s %s\n", (ins & 0x01000000u) ? "bl" : "b", + "eqnecsccmiplvsvchilsgeltgtle" + 2*(ins >> 28), sym); + } else { + fprintf(stderr, + "Error: unsupported opcode %08x for %s symbol relocation.\n", + ins, sym); + exit(1); + } +#elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE +#if LJ_TARGET_PS3 +#define TOCPREFIX "." +#else +#define TOCPREFIX "" +#endif + if ((ins >> 26) == 16) { + fprintf(ctx->fp, "\t%s %d, %d, " TOCPREFIX "%s\n", + (ins & 1) ? "bcl" : "bc", (ins >> 21) & 31, (ins >> 16) & 31, sym); + } else if ((ins >> 26) == 18) { + fprintf(ctx->fp, "\t%s " TOCPREFIX "%s\n", (ins & 1) ? "bl" : "b", sym); + } else { + fprintf(stderr, + "Error: unsupported opcode %08x for %s symbol relocation.\n", + ins, sym); + exit(1); + } +#elif LJ_TARGET_MIPS + fprintf(stderr, + "Error: unsupported opcode %08x for %s symbol relocation.\n", + ins, sym); + exit(1); +#else +#error "missing relocation support for this architecture" +#endif +} +#endif + +#if LJ_TARGET_ARM +#define ELFASM_PX "%%" +#else +#define ELFASM_PX "@" +#endif + +/* Emit an assembler label. */ +static void emit_asm_label(BuildCtx *ctx, const char *name, int size, int isfunc) +{ + switch (ctx->mode) { + case BUILD_elfasm: +#if LJ_TARGET_PS3 + if (!strncmp(name, "lj_vm_", 6) && + strcmp(name, ctx->beginsym) && + !strstr(name, "hook")) { + fprintf(ctx->fp, + "\n\t.globl %s\n" + "\t.section \".opd\",\"aw\"\n" + "%s:\n" + "\t.long .%s,.TOC.@tocbase32\n" + "\t.size %s,8\n" + "\t.previous\n" + "\t.globl .%s\n" + "\t.hidden .%s\n" + "\t.type .%s, " ELFASM_PX "function\n" + "\t.size .%s, %d\n" + ".%s:\n", + name, name, name, name, name, name, name, name, size, name); + break; + } +#endif + fprintf(ctx->fp, + "\n\t.globl %s\n" + "\t.hidden %s\n" + "\t.type %s, " ELFASM_PX "%s\n" + "\t.size %s, %d\n" + "%s:\n", + name, name, name, isfunc ? "function" : "object", name, size, name); + break; + case BUILD_coffasm: + fprintf(ctx->fp, "\n\t.globl %s\n", name); + if (isfunc) + fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", name); + fprintf(ctx->fp, "%s:\n", name); + break; + case BUILD_machasm: + fprintf(ctx->fp, + "\n\t.private_extern %s\n" + "%s:\n", name, name); + break; + default: + break; + } +} + +/* Emit alignment. */ +static void emit_asm_align(BuildCtx *ctx, int bits) +{ + switch (ctx->mode) { + case BUILD_elfasm: + case BUILD_coffasm: + fprintf(ctx->fp, "\t.p2align %d\n", bits); + break; + case BUILD_machasm: + fprintf(ctx->fp, "\t.align %d\n", bits); + break; + default: + break; + } +} + +/* ------------------------------------------------------------------------ */ + +/* Emit assembler source code. */ +void emit_asm(BuildCtx *ctx) +{ + int i, rel; + + fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch); + fprintf(ctx->fp, "\t.text\n"); + emit_asm_align(ctx, 4); + +#if LJ_TARGET_PS3 + emit_asm_label(ctx, ctx->beginsym, ctx->codesz, 0); +#else + emit_asm_label(ctx, ctx->beginsym, 0, 0); +#endif + if (ctx->mode != BUILD_machasm) + fprintf(ctx->fp, ".Lbegin:\n"); + +#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND + /* This should really be moved into buildvm_arm.dasc. */ + fprintf(ctx->fp, + ".fnstart\n" + ".save {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n" + ".pad #28\n"); +#endif +#if LJ_TARGET_MIPS + fprintf(ctx->fp, ".set nomips16\n.abicalls\n.set noreorder\n.set nomacro\n"); +#endif + + for (i = rel = 0; i < ctx->nsym; i++) { + int32_t ofs = ctx->sym[i].ofs; + int32_t next = ctx->sym[i+1].ofs; +#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND && LJ_HASFFI + if (!strcmp(ctx->sym[i].name, "lj_vm_ffi_call")) + fprintf(ctx->fp, + ".globl lj_err_unwind_arm\n" + ".personality lj_err_unwind_arm\n" + ".fnend\n" + ".fnstart\n" + ".save {r4, r5, r11, lr}\n" + ".setfp r11, sp\n"); +#endif + emit_asm_label(ctx, ctx->sym[i].name, next - ofs, 1); + while (rel < ctx->nreloc && ctx->reloc[rel].ofs <= next) { + BuildReloc *r = &ctx->reloc[rel]; + int n = r->ofs - ofs; +#if LJ_TARGET_X86ORX64 + if (ctx->mode == BUILD_machasm && r->type != 0) { + emit_asm_reloc_mach(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]); + } else { + emit_asm_bytes(ctx, ctx->code+ofs, n); + emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]); + } + ofs += n+4; +#else + emit_asm_wordreloc(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]); + ofs += n; +#endif + rel++; + } +#if LJ_TARGET_X86ORX64 + emit_asm_bytes(ctx, ctx->code+ofs, next-ofs); +#else + emit_asm_words(ctx, ctx->code+ofs, next-ofs); +#endif + } + +#if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND + fprintf(ctx->fp, +#if !LJ_HASFFI + ".globl lj_err_unwind_arm\n" + ".personality lj_err_unwind_arm\n" +#endif + ".fnend\n"); +#endif + + fprintf(ctx->fp, "\n"); + switch (ctx->mode) { + case BUILD_elfasm: +#if !LJ_TARGET_PS3 + fprintf(ctx->fp, "\t.section .note.GNU-stack,\"\"," ELFASM_PX "progbits\n"); +#endif +#if LJ_TARGET_PPCSPE + /* Soft-float ABI + SPE. */ + fprintf(ctx->fp, "\t.gnu_attribute 4, 2\n\t.gnu_attribute 8, 3\n"); +#elif LJ_TARGET_PPC && !LJ_TARGET_PS3 + /* Hard-float ABI. */ + fprintf(ctx->fp, "\t.gnu_attribute 4, 1\n"); +#endif + /* fallthrough */ + case BUILD_coffasm: + fprintf(ctx->fp, "\t.ident \"%s\"\n", ctx->dasm_ident); + break; + case BUILD_machasm: + fprintf(ctx->fp, + "\t.cstring\n" + "\t.ascii \"%s\\0\"\n", ctx->dasm_ident); + break; + default: + break; + } + fprintf(ctx->fp, "\n"); +} + diff --git a/external/lua/luajit/src/src/host/buildvm_fold.c b/external/lua/luajit/src/src/host/buildvm_fold.c new file mode 100644 index 0000000000..085a4c345c --- /dev/null +++ b/external/lua/luajit/src/src/host/buildvm_fold.c @@ -0,0 +1,229 @@ +/* +** LuaJIT VM builder: IR folding hash table generator. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "buildvm.h" +#include "lj_obj.h" +#include "lj_ir.h" + +/* Context for the folding hash table generator. */ +static int lineno; +static int funcidx; +static uint32_t foldkeys[BUILD_MAX_FOLD]; +static uint32_t nkeys; + +/* Try to fill the hash table with keys using the hash parameters. */ +static int tryhash(uint32_t *htab, uint32_t sz, uint32_t r, int dorol) +{ + uint32_t i; + if (dorol && ((r & 31) == 0 || (r>>5) == 0)) + return 0; /* Avoid zero rotates. */ + memset(htab, 0xff, (sz+1)*sizeof(uint32_t)); + for (i = 0; i < nkeys; i++) { + uint32_t key = foldkeys[i]; + uint32_t k = key & 0xffffff; + uint32_t h = (dorol ? lj_rol(lj_rol(k, r>>5) - k, r&31) : + (((k << (r>>5)) - k) << (r&31))) % sz; + if (htab[h] != 0xffffffff) { /* Collision on primary slot. */ + if (htab[h+1] != 0xffffffff) { /* Collision on secondary slot. */ + /* Try to move the colliding key, if possible. */ + if (h < sz-1 && htab[h+2] == 0xffffffff) { + uint32_t k2 = htab[h+1] & 0xffffff; + uint32_t h2 = (dorol ? lj_rol(lj_rol(k2, r>>5) - k2, r&31) : + (((k2 << (r>>5)) - k2) << (r&31))) % sz; + if (h2 != h+1) return 0; /* Cannot resolve collision. */ + htab[h+2] = htab[h+1]; /* Move colliding key to secondary slot. */ + } else { + return 0; /* Collision. */ + } + } + htab[h+1] = key; + } else { + htab[h] = key; + } + } + return 1; /* Success, all keys could be stored. */ +} + +/* Print the generated hash table. */ +static void printhash(BuildCtx *ctx, uint32_t *htab, uint32_t sz) +{ + uint32_t i; + fprintf(ctx->fp, "static const uint32_t fold_hash[%d] = {\n0x%08x", + sz+1, htab[0]); + for (i = 1; i < sz+1; i++) + fprintf(ctx->fp, ",\n0x%08x", htab[i]); + fprintf(ctx->fp, "\n};\n\n"); +} + +/* Exhaustive search for the shortest semi-perfect hash table. */ +static void makehash(BuildCtx *ctx) +{ + uint32_t htab[BUILD_MAX_FOLD*2+1]; + uint32_t sz, r; + /* Search for the smallest hash table with an odd size. */ + for (sz = (nkeys|1); sz < BUILD_MAX_FOLD*2; sz += 2) { + /* First try all shift hash combinations. */ + for (r = 0; r < 32*32; r++) { + if (tryhash(htab, sz, r, 0)) { + printhash(ctx, htab, sz); + fprintf(ctx->fp, + "#define fold_hashkey(k)\t(((((k)<<%u)-(k))<<%u)%%%u)\n\n", + r>>5, r&31, sz); + return; + } + } + /* Then try all rotate hash combinations. */ + for (r = 0; r < 32*32; r++) { + if (tryhash(htab, sz, r, 1)) { + printhash(ctx, htab, sz); + fprintf(ctx->fp, + "#define fold_hashkey(k)\t(lj_rol(lj_rol((k),%u)-(k),%u)%%%u)\n\n", + r>>5, r&31, sz); + return; + } + } + } + fprintf(stderr, "Error: search for perfect hash failed\n"); + exit(1); +} + +/* Parse one token of a fold rule. */ +static uint32_t nexttoken(char **pp, int allowlit, int allowany) +{ + char *p = *pp; + if (p) { + uint32_t i; + char *q = strchr(p, ' '); + if (q) *q++ = '\0'; + *pp = q; + if (allowlit && !strncmp(p, "IRFPM_", 6)) { + for (i = 0; irfpm_names[i]; i++) + if (!strcmp(irfpm_names[i], p+6)) + return i; + } else if (allowlit && !strncmp(p, "IRFL_", 5)) { + for (i = 0; irfield_names[i]; i++) + if (!strcmp(irfield_names[i], p+5)) + return i; + } else if (allowlit && !strncmp(p, "IRCALL_", 7)) { + for (i = 0; ircall_names[i]; i++) + if (!strcmp(ircall_names[i], p+7)) + return i; + } else if (allowlit && !strncmp(p, "IRCONV_", 7)) { + for (i = 0; irt_names[i]; i++) { + const char *r = strchr(p+7, '_'); + if (r && !strncmp(irt_names[i], p+7, r-(p+7))) { + uint32_t j; + for (j = 0; irt_names[j]; j++) + if (!strcmp(irt_names[j], r+1)) + return (i << 5) + j; + } + } + } else if (allowlit && *p >= '0' && *p <= '9') { + for (i = 0; *p >= '0' && *p <= '9'; p++) + i = i*10 + (*p - '0'); + if (*p == '\0') + return i; + } else if (allowany && !strcmp("any", p)) { + return allowany; + } else { + for (i = 0; ir_names[i]; i++) + if (!strcmp(ir_names[i], p)) + return i; + } + fprintf(stderr, "Error: bad fold definition token \"%s\" at line %d\n", p, lineno); + exit(1); + } + return 0; +} + +/* Parse a fold rule. */ +static void foldrule(char *p) +{ + uint32_t op = nexttoken(&p, 0, 0); + uint32_t left = nexttoken(&p, 0, 0x7f); + uint32_t right = nexttoken(&p, 1, 0x3ff); + uint32_t key = (funcidx << 24) | (op << 17) | (left << 10) | right; + uint32_t i; + if (nkeys >= BUILD_MAX_FOLD) { + fprintf(stderr, "Error: too many fold rules, increase BUILD_MAX_FOLD.\n"); + exit(1); + } + /* Simple insertion sort to detect duplicates. */ + for (i = nkeys; i > 0; i--) { + if ((foldkeys[i-1]&0xffffff) < (key & 0xffffff)) + break; + if ((foldkeys[i-1]&0xffffff) == (key & 0xffffff)) { + fprintf(stderr, "Error: duplicate fold definition at line %d\n", lineno); + exit(1); + } + foldkeys[i] = foldkeys[i-1]; + } + foldkeys[i] = key; + nkeys++; +} + +/* Emit C source code for IR folding hash table. */ +void emit_fold(BuildCtx *ctx) +{ + char buf[256]; /* We don't care about analyzing lines longer than that. */ + const char *fname = ctx->args[0]; + FILE *fp; + + if (fname == NULL) { + fprintf(stderr, "Error: missing input filename\n"); + exit(1); + } + + if (fname[0] == '-' && fname[1] == '\0') { + fp = stdin; + } else { + fp = fopen(fname, "r"); + if (!fp) { + fprintf(stderr, "Error: cannot open input file '%s': %s\n", + fname, strerror(errno)); + exit(1); + } + } + + fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n"); + fprintf(ctx->fp, "static const FoldFunc fold_func[] = {\n"); + + lineno = 0; + funcidx = 0; + nkeys = 0; + while (fgets(buf, sizeof(buf), fp) != NULL) { + lineno++; + /* The prefix must be at the start of a line, otherwise it's ignored. */ + if (!strncmp(buf, FOLDDEF_PREFIX, sizeof(FOLDDEF_PREFIX)-1)) { + char *p = buf+sizeof(FOLDDEF_PREFIX)-1; + char *q = strchr(p, ')'); + if (p[0] == '(' && q) { + p++; + *q = '\0'; + foldrule(p); + } else if ((p[0] == 'F' || p[0] == 'X') && p[1] == '(' && q) { + p += 2; + *q = '\0'; + if (funcidx) + fprintf(ctx->fp, ",\n"); + if (p[-2] == 'X') + fprintf(ctx->fp, " %s", p); + else + fprintf(ctx->fp, " fold_%s", p); + funcidx++; + } else { + buf[strlen(buf)-1] = '\0'; + fprintf(stderr, "Error: unknown fold definition tag %s%s at line %d\n", + FOLDDEF_PREFIX, p, lineno); + exit(1); + } + } + } + fclose(fp); + fprintf(ctx->fp, "\n};\n\n"); + + makehash(ctx); +} + diff --git a/external/lua/luajit/src/src/host/buildvm_lib.c b/external/lua/luajit/src/src/host/buildvm_lib.c new file mode 100644 index 0000000000..40141dfbc6 --- /dev/null +++ b/external/lua/luajit/src/src/host/buildvm_lib.c @@ -0,0 +1,398 @@ +/* +** LuaJIT VM builder: library definition compiler. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "buildvm.h" +#include "lj_obj.h" +#include "lj_lib.h" + +/* Context for library definitions. */ +static uint8_t obuf[8192]; +static uint8_t *optr; +static char modname[80]; +static size_t modnamelen; +static char funcname[80]; +static int modstate, regfunc; +static int ffid, recffid, ffasmfunc; + +enum { + REGFUNC_OK, + REGFUNC_NOREG, + REGFUNC_NOREGUV +}; + +static void libdef_name(const char *p, int kind) +{ + size_t n = strlen(p); + if (kind != LIBINIT_STRING) { + if (n > modnamelen && p[modnamelen] == '_' && + !strncmp(p, modname, modnamelen)) { + p += modnamelen+1; + n -= modnamelen+1; + } + } + if (n > LIBINIT_MAXSTR) { + fprintf(stderr, "Error: string too long: '%s'\n", p); + exit(1); + } + if (optr+1+n+2 > obuf+sizeof(obuf)) { /* +2 for caller. */ + fprintf(stderr, "Error: output buffer overflow\n"); + exit(1); + } + *optr++ = (uint8_t)(n | kind); + memcpy(optr, p, n); + optr += n; +} + +static void libdef_endmodule(BuildCtx *ctx) +{ + if (modstate != 0) { + char line[80]; + const uint8_t *p; + int n; + if (modstate == 1) + fprintf(ctx->fp, " (lua_CFunction)0"); + fprintf(ctx->fp, "\n};\n"); + fprintf(ctx->fp, "static const uint8_t %s%s[] = {\n", + LABEL_PREFIX_LIBINIT, modname); + line[0] = '\0'; + for (n = 0, p = obuf; p < optr; p++) { + n += sprintf(line+n, "%d,", *p); + if (n >= 75) { + fprintf(ctx->fp, "%s\n", line); + n = 0; + line[0] = '\0'; + } + } + fprintf(ctx->fp, "%s%d\n};\n#endif\n\n", line, LIBINIT_END); + } +} + +static void libdef_module(BuildCtx *ctx, char *p, int arg) +{ + UNUSED(arg); + if (ctx->mode == BUILD_libdef) { + libdef_endmodule(ctx); + optr = obuf; + *optr++ = (uint8_t)ffid; + *optr++ = (uint8_t)ffasmfunc; + *optr++ = 0; /* Hash table size. */ + modstate = 1; + fprintf(ctx->fp, "#ifdef %sMODULE_%s\n", LIBDEF_PREFIX, p); + fprintf(ctx->fp, "#undef %sMODULE_%s\n", LIBDEF_PREFIX, p); + fprintf(ctx->fp, "static const lua_CFunction %s%s[] = {\n", + LABEL_PREFIX_LIBCF, p); + } + modnamelen = strlen(p); + if (modnamelen > sizeof(modname)-1) { + fprintf(stderr, "Error: module name too long: '%s'\n", p); + exit(1); + } + strcpy(modname, p); +} + +static int find_ffofs(BuildCtx *ctx, const char *name) +{ + int i; + for (i = 0; i < ctx->nglob; i++) { + const char *gl = ctx->globnames[i]; + if (gl[0] == 'f' && gl[1] == 'f' && gl[2] == '_' && !strcmp(gl+3, name)) { + return (int)((uint8_t *)ctx->glob[i] - ctx->code); + } + } + fprintf(stderr, "Error: undefined fast function %s%s\n", + LABEL_PREFIX_FF, name); + exit(1); +} + +static void libdef_func(BuildCtx *ctx, char *p, int arg) +{ + if (arg != LIBINIT_CF) + ffasmfunc++; + if (ctx->mode == BUILD_libdef) { + if (modstate == 0) { + fprintf(stderr, "Error: no module for function definition %s\n", p); + exit(1); + } + if (regfunc == REGFUNC_NOREG) { + if (optr+1 > obuf+sizeof(obuf)) { + fprintf(stderr, "Error: output buffer overflow\n"); + exit(1); + } + *optr++ = LIBINIT_FFID; + } else { + if (arg != LIBINIT_ASM_) { + if (modstate != 1) fprintf(ctx->fp, ",\n"); + modstate = 2; + fprintf(ctx->fp, " %s%s", arg ? LABEL_PREFIX_FFH : LABEL_PREFIX_CF, p); + } + if (regfunc != REGFUNC_NOREGUV) obuf[2]++; /* Bump hash table size. */ + libdef_name(regfunc == REGFUNC_NOREGUV ? "" : p, arg); + } + } else if (ctx->mode == BUILD_ffdef) { + fprintf(ctx->fp, "FFDEF(%s)\n", p); + } else if (ctx->mode == BUILD_recdef) { + if (strlen(p) > sizeof(funcname)-1) { + fprintf(stderr, "Error: function name too long: '%s'\n", p); + exit(1); + } + strcpy(funcname, p); + } else if (ctx->mode == BUILD_vmdef) { + int i; + for (i = 1; p[i] && modname[i-1]; i++) + if (p[i] == '_') p[i] = '.'; + fprintf(ctx->fp, "\"%s\",\n", p); + } else if (ctx->mode == BUILD_bcdef) { + if (arg != LIBINIT_CF) + fprintf(ctx->fp, ",\n%d", find_ffofs(ctx, p)); + } + ffid++; + regfunc = REGFUNC_OK; +} + +static uint32_t find_rec(char *name) +{ + char *p = (char *)obuf; + uint32_t n; + for (n = 2; *p; n++) { + if (strcmp(p, name) == 0) + return n; + p += strlen(p)+1; + } + if (p+strlen(name)+1 >= (char *)obuf+sizeof(obuf)) { + fprintf(stderr, "Error: output buffer overflow\n"); + exit(1); + } + strcpy(p, name); + return n; +} + +static void libdef_rec(BuildCtx *ctx, char *p, int arg) +{ + UNUSED(arg); + if (ctx->mode == BUILD_recdef) { + char *q; + uint32_t n; + for (; recffid+1 < ffid; recffid++) + fprintf(ctx->fp, ",\n0"); + recffid = ffid; + if (*p == '.') p = funcname; + q = strchr(p, ' '); + if (q) *q++ = '\0'; + n = find_rec(p); + if (q) + fprintf(ctx->fp, ",\n0x%02x00+(%s)", n, q); + else + fprintf(ctx->fp, ",\n0x%02x00", n); + } +} + +static void memcpy_endian(void *dst, void *src, size_t n) +{ + union { uint8_t b; uint32_t u; } host_endian; + host_endian.u = 1; + if (host_endian.b == LJ_ENDIAN_SELECT(1, 0)) { + memcpy(dst, src, n); + } else { + size_t i; + for (i = 0; i < n; i++) + ((uint8_t *)dst)[i] = ((uint8_t *)src)[n-i-1]; + } +} + +static void libdef_push(BuildCtx *ctx, char *p, int arg) +{ + UNUSED(arg); + if (ctx->mode == BUILD_libdef) { + int len = (int)strlen(p); + if (*p == '"') { + if (len > 1 && p[len-1] == '"') { + p[len-1] = '\0'; + libdef_name(p+1, LIBINIT_STRING); + return; + } + } else if (*p >= '0' && *p <= '9') { + char *ep; + double d = strtod(p, &ep); + if (*ep == '\0') { + if (optr+1+sizeof(double) > obuf+sizeof(obuf)) { + fprintf(stderr, "Error: output buffer overflow\n"); + exit(1); + } + *optr++ = LIBINIT_NUMBER; + memcpy_endian(optr, &d, sizeof(double)); + optr += sizeof(double); + return; + } + } else if (!strcmp(p, "lastcl")) { + if (optr+1 > obuf+sizeof(obuf)) { + fprintf(stderr, "Error: output buffer overflow\n"); + exit(1); + } + *optr++ = LIBINIT_LASTCL; + return; + } else if (len > 4 && !strncmp(p, "top-", 4)) { + if (optr+2 > obuf+sizeof(obuf)) { + fprintf(stderr, "Error: output buffer overflow\n"); + exit(1); + } + *optr++ = LIBINIT_COPY; + *optr++ = (uint8_t)atoi(p+4); + return; + } + fprintf(stderr, "Error: bad value for %sPUSH(%s)\n", LIBDEF_PREFIX, p); + exit(1); + } +} + +static void libdef_set(BuildCtx *ctx, char *p, int arg) +{ + UNUSED(arg); + if (ctx->mode == BUILD_libdef) { + if (p[0] == '!' && p[1] == '\0') p[0] = '\0'; /* Set env. */ + libdef_name(p, LIBINIT_STRING); + *optr++ = LIBINIT_SET; + obuf[2]++; /* Bump hash table size. */ + } +} + +static void libdef_regfunc(BuildCtx *ctx, char *p, int arg) +{ + UNUSED(ctx); UNUSED(p); + regfunc = arg; +} + +typedef void (*LibDefFunc)(BuildCtx *ctx, char *p, int arg); + +typedef struct LibDefHandler { + const char *suffix; + const char *stop; + const LibDefFunc func; + const int arg; +} LibDefHandler; + +static const LibDefHandler libdef_handlers[] = { + { "MODULE_", " \t\r\n", libdef_module, 0 }, + { "CF(", ")", libdef_func, LIBINIT_CF }, + { "ASM(", ")", libdef_func, LIBINIT_ASM }, + { "ASM_(", ")", libdef_func, LIBINIT_ASM_ }, + { "REC(", ")", libdef_rec, 0 }, + { "PUSH(", ")", libdef_push, 0 }, + { "SET(", ")", libdef_set, 0 }, + { "NOREGUV", NULL, libdef_regfunc, REGFUNC_NOREGUV }, + { "NOREG", NULL, libdef_regfunc, REGFUNC_NOREG }, + { NULL, NULL, (LibDefFunc)0, 0 } +}; + +/* Emit C source code for library function definitions. */ +void emit_lib(BuildCtx *ctx) +{ + const char *fname; + + if (ctx->mode == BUILD_ffdef || ctx->mode == BUILD_libdef || + ctx->mode == BUILD_recdef) + fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n"); + else if (ctx->mode == BUILD_vmdef) + fprintf(ctx->fp, "ffnames = {\n[0]=\"Lua\",\n\"C\",\n"); + if (ctx->mode == BUILD_recdef) + fprintf(ctx->fp, "static const uint16_t recff_idmap[] = {\n0,\n0x0100"); + recffid = ffid = FF_C+1; + ffasmfunc = 0; + + while ((fname = *ctx->args++)) { + char buf[256]; /* We don't care about analyzing lines longer than that. */ + FILE *fp; + if (fname[0] == '-' && fname[1] == '\0') { + fp = stdin; + } else { + fp = fopen(fname, "r"); + if (!fp) { + fprintf(stderr, "Error: cannot open input file '%s': %s\n", + fname, strerror(errno)); + exit(1); + } + } + modstate = 0; + regfunc = REGFUNC_OK; + while (fgets(buf, sizeof(buf), fp) != NULL) { + char *p; + /* Simplistic pre-processor. Only handles top-level #if/#endif. */ + if (buf[0] == '#' && buf[1] == 'i' && buf[2] == 'f') { + int ok = 1; + if (!strcmp(buf, "#if LJ_52\n")) + ok = LJ_52; + else if (!strcmp(buf, "#if LJ_HASJIT\n")) + ok = LJ_HASJIT; + else if (!strcmp(buf, "#if LJ_HASFFI\n")) + ok = LJ_HASFFI; + if (!ok) { + int lvl = 1; + while (fgets(buf, sizeof(buf), fp) != NULL) { + if (buf[0] == '#' && buf[1] == 'e' && buf[2] == 'n') { + if (--lvl == 0) break; + } else if (buf[0] == '#' && buf[1] == 'i' && buf[2] == 'f') { + lvl++; + } + } + continue; + } + } + for (p = buf; (p = strstr(p, LIBDEF_PREFIX)) != NULL; ) { + const LibDefHandler *ldh; + p += sizeof(LIBDEF_PREFIX)-1; + for (ldh = libdef_handlers; ldh->suffix != NULL; ldh++) { + size_t n, len = strlen(ldh->suffix); + if (!strncmp(p, ldh->suffix, len)) { + p += len; + n = ldh->stop ? strcspn(p, ldh->stop) : 0; + if (!p[n]) break; + p[n] = '\0'; + ldh->func(ctx, p, ldh->arg); + p += n+1; + break; + } + } + if (ldh->suffix == NULL) { + buf[strlen(buf)-1] = '\0'; + fprintf(stderr, "Error: unknown library definition tag %s%s\n", + LIBDEF_PREFIX, p); + exit(1); + } + } + } + fclose(fp); + if (ctx->mode == BUILD_libdef) { + libdef_endmodule(ctx); + } + } + + if (ctx->mode == BUILD_ffdef) { + fprintf(ctx->fp, "\n#undef FFDEF\n\n"); + fprintf(ctx->fp, + "#ifndef FF_NUM_ASMFUNC\n#define FF_NUM_ASMFUNC %d\n#endif\n\n", + ffasmfunc); + } else if (ctx->mode == BUILD_vmdef) { + fprintf(ctx->fp, "}\n\n"); + } else if (ctx->mode == BUILD_bcdef) { + int i; + fprintf(ctx->fp, "\n};\n\n"); + fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_mode[] = {\n"); + fprintf(ctx->fp, "BCDEF(BCMODE)\n"); + for (i = ffasmfunc-1; i > 0; i--) + fprintf(ctx->fp, "BCMODE_FF,\n"); + fprintf(ctx->fp, "BCMODE_FF\n};\n\n"); + } else if (ctx->mode == BUILD_recdef) { + char *p = (char *)obuf; + fprintf(ctx->fp, "\n};\n\n"); + fprintf(ctx->fp, "static const RecordFunc recff_func[] = {\n" + "recff_nyi,\n" + "recff_c"); + while (*p) { + fprintf(ctx->fp, ",\nrecff_%s", p); + p += strlen(p)+1; + } + fprintf(ctx->fp, "\n};\n\n"); + } +} + diff --git a/external/lua/luajit/src/src/host/buildvm_peobj.c b/external/lua/luajit/src/src/host/buildvm_peobj.c new file mode 100644 index 0000000000..1249445b02 --- /dev/null +++ b/external/lua/luajit/src/src/host/buildvm_peobj.c @@ -0,0 +1,368 @@ +/* +** LuaJIT VM builder: PE object emitter. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Only used for building on Windows, since we cannot assume the presence +** of a suitable assembler. The host and target byte order must match. +*/ + +#include "buildvm.h" +#include "lj_bc.h" + +#if LJ_TARGET_X86ORX64 || LJ_TARGET_PPC + +/* Context for PE object emitter. */ +static char *strtab; +static size_t strtabofs; + +/* -- PE object definitions ----------------------------------------------- */ + +/* PE header. */ +typedef struct PEheader { + uint16_t arch; + uint16_t nsects; + uint32_t time; + uint32_t symtabofs; + uint32_t nsyms; + uint16_t opthdrsz; + uint16_t flags; +} PEheader; + +/* PE section. */ +typedef struct PEsection { + char name[8]; + uint32_t vsize; + uint32_t vaddr; + uint32_t size; + uint32_t ofs; + uint32_t relocofs; + uint32_t lineofs; + uint16_t nreloc; + uint16_t nline; + uint32_t flags; +} PEsection; + +/* PE relocation. */ +typedef struct PEreloc { + uint32_t vaddr; + uint32_t symidx; + uint16_t type; +} PEreloc; + +/* Cannot use sizeof, because it pads up to the max. alignment. */ +#define PEOBJ_RELOC_SIZE (4+4+2) + +/* PE symbol table entry. */ +typedef struct PEsym { + union { + char name[8]; + uint32_t nameref[2]; + } n; + uint32_t value; + int16_t sect; + uint16_t type; + uint8_t scl; + uint8_t naux; +} PEsym; + +/* PE symbol table auxiliary entry for a section. */ +typedef struct PEsymaux { + uint32_t size; + uint16_t nreloc; + uint16_t nline; + uint32_t cksum; + uint16_t assoc; + uint8_t comdatsel; + uint8_t unused[3]; +} PEsymaux; + +/* Cannot use sizeof, because it pads up to the max. alignment. */ +#define PEOBJ_SYM_SIZE (8+4+2+2+1+1) + +/* PE object CPU specific defines. */ +#if LJ_TARGET_X86 +#define PEOBJ_ARCH_TARGET 0x014c +#define PEOBJ_RELOC_REL32 0x14 /* MS: REL32, GNU: DISP32. */ +#define PEOBJ_RELOC_DIR32 0x06 +#define PEOBJ_RELOC_OFS 0 +#define PEOBJ_TEXT_FLAGS 0x60500020 /* 60=r+x, 50=align16, 20=code. */ +#elif LJ_TARGET_X64 +#define PEOBJ_ARCH_TARGET 0x8664 +#define PEOBJ_RELOC_REL32 0x04 /* MS: REL32, GNU: DISP32. */ +#define PEOBJ_RELOC_DIR32 0x02 +#define PEOBJ_RELOC_ADDR32NB 0x03 +#define PEOBJ_RELOC_OFS 0 +#define PEOBJ_TEXT_FLAGS 0x60500020 /* 60=r+x, 50=align16, 20=code. */ +#elif LJ_TARGET_PPC +#define PEOBJ_ARCH_TARGET 0x01f2 +#define PEOBJ_RELOC_REL32 0x06 +#define PEOBJ_RELOC_DIR32 0x02 +#define PEOBJ_RELOC_OFS (-4) +#define PEOBJ_TEXT_FLAGS 0x60400020 /* 60=r+x, 40=align8, 20=code. */ +#endif + +/* Section numbers (0-based). */ +enum { + PEOBJ_SECT_ABS = -2, + PEOBJ_SECT_UNDEF = -1, + PEOBJ_SECT_TEXT, +#if LJ_TARGET_X64 + PEOBJ_SECT_PDATA, + PEOBJ_SECT_XDATA, +#endif + PEOBJ_SECT_RDATA_Z, + PEOBJ_NSECTIONS +}; + +/* Symbol types. */ +#define PEOBJ_TYPE_NULL 0 +#define PEOBJ_TYPE_FUNC 0x20 + +/* Symbol storage class. */ +#define PEOBJ_SCL_EXTERN 2 +#define PEOBJ_SCL_STATIC 3 + +/* -- PE object emitter --------------------------------------------------- */ + +/* Emit PE object symbol. */ +static void emit_peobj_sym(BuildCtx *ctx, const char *name, uint32_t value, + int sect, int type, int scl) +{ + PEsym sym; + size_t len = strlen(name); + if (!strtab) { /* Pass 1: only calculate string table length. */ + if (len > 8) strtabofs += len+1; + return; + } + if (len <= 8) { + memcpy(sym.n.name, name, len); + memset(sym.n.name+len, 0, 8-len); + } else { + sym.n.nameref[0] = 0; + sym.n.nameref[1] = (uint32_t)strtabofs; + memcpy(strtab + strtabofs, name, len); + strtab[strtabofs+len] = 0; + strtabofs += len+1; + } + sym.value = value; + sym.sect = (int16_t)(sect+1); /* 1-based section number. */ + sym.type = (uint16_t)type; + sym.scl = (uint8_t)scl; + sym.naux = 0; + owrite(ctx, &sym, PEOBJ_SYM_SIZE); +} + +/* Emit PE object section symbol. */ +static void emit_peobj_sym_sect(BuildCtx *ctx, PEsection *pesect, int sect) +{ + PEsym sym; + PEsymaux aux; + if (!strtab) return; /* Pass 1: no output. */ + memcpy(sym.n.name, pesect[sect].name, 8); + sym.value = 0; + sym.sect = (int16_t)(sect+1); /* 1-based section number. */ + sym.type = PEOBJ_TYPE_NULL; + sym.scl = PEOBJ_SCL_STATIC; + sym.naux = 1; + owrite(ctx, &sym, PEOBJ_SYM_SIZE); + memset(&aux, 0, sizeof(PEsymaux)); + aux.size = pesect[sect].size; + aux.nreloc = pesect[sect].nreloc; + owrite(ctx, &aux, PEOBJ_SYM_SIZE); +} + +/* Emit Windows PE object file. */ +void emit_peobj(BuildCtx *ctx) +{ + PEheader pehdr; + PEsection pesect[PEOBJ_NSECTIONS]; + uint32_t sofs; + int i, nrsym; + union { uint8_t b; uint32_t u; } host_endian; + + sofs = sizeof(PEheader) + PEOBJ_NSECTIONS*sizeof(PEsection); + + /* Fill in PE sections. */ + memset(&pesect, 0, PEOBJ_NSECTIONS*sizeof(PEsection)); + memcpy(pesect[PEOBJ_SECT_TEXT].name, ".text", sizeof(".text")-1); + pesect[PEOBJ_SECT_TEXT].ofs = sofs; + sofs += (pesect[PEOBJ_SECT_TEXT].size = (uint32_t)ctx->codesz); + pesect[PEOBJ_SECT_TEXT].relocofs = sofs; + sofs += (pesect[PEOBJ_SECT_TEXT].nreloc = (uint16_t)ctx->nreloc) * PEOBJ_RELOC_SIZE; + /* Flags: 60 = read+execute, 50 = align16, 20 = code. */ + pesect[PEOBJ_SECT_TEXT].flags = PEOBJ_TEXT_FLAGS; + +#if LJ_TARGET_X64 + memcpy(pesect[PEOBJ_SECT_PDATA].name, ".pdata", sizeof(".pdata")-1); + pesect[PEOBJ_SECT_PDATA].ofs = sofs; + sofs += (pesect[PEOBJ_SECT_PDATA].size = 6*4); + pesect[PEOBJ_SECT_PDATA].relocofs = sofs; + sofs += (pesect[PEOBJ_SECT_PDATA].nreloc = 6) * PEOBJ_RELOC_SIZE; + /* Flags: 40 = read, 30 = align4, 40 = initialized data. */ + pesect[PEOBJ_SECT_PDATA].flags = 0x40300040; + + memcpy(pesect[PEOBJ_SECT_XDATA].name, ".xdata", sizeof(".xdata")-1); + pesect[PEOBJ_SECT_XDATA].ofs = sofs; + sofs += (pesect[PEOBJ_SECT_XDATA].size = 8*2+4+6*2); /* See below. */ + pesect[PEOBJ_SECT_XDATA].relocofs = sofs; + sofs += (pesect[PEOBJ_SECT_XDATA].nreloc = 1) * PEOBJ_RELOC_SIZE; + /* Flags: 40 = read, 30 = align4, 40 = initialized data. */ + pesect[PEOBJ_SECT_XDATA].flags = 0x40300040; +#endif + + memcpy(pesect[PEOBJ_SECT_RDATA_Z].name, ".rdata$Z", sizeof(".rdata$Z")-1); + pesect[PEOBJ_SECT_RDATA_Z].ofs = sofs; + sofs += (pesect[PEOBJ_SECT_RDATA_Z].size = (uint32_t)strlen(ctx->dasm_ident)+1); + /* Flags: 40 = read, 30 = align4, 40 = initialized data. */ + pesect[PEOBJ_SECT_RDATA_Z].flags = 0x40300040; + + /* Fill in PE header. */ + pehdr.arch = PEOBJ_ARCH_TARGET; + pehdr.nsects = PEOBJ_NSECTIONS; + pehdr.time = 0; /* Timestamp is optional. */ + pehdr.symtabofs = sofs; + pehdr.opthdrsz = 0; + pehdr.flags = 0; + + /* Compute the size of the symbol table: + ** @feat.00 + nsections*2 + ** + asm_start + nsym + ** + nrsym + */ + nrsym = ctx->nrelocsym; + pehdr.nsyms = 1+PEOBJ_NSECTIONS*2 + 1+ctx->nsym + nrsym; +#if LJ_TARGET_X64 + pehdr.nsyms += 1; /* Symbol for lj_err_unwind_win64. */ +#endif + + /* Write PE object header and all sections. */ + owrite(ctx, &pehdr, sizeof(PEheader)); + owrite(ctx, &pesect, sizeof(PEsection)*PEOBJ_NSECTIONS); + + /* Write .text section. */ + host_endian.u = 1; + if (host_endian.b != LJ_ENDIAN_SELECT(1, 0)) { +#if LJ_TARGET_PPC + uint32_t *p = (uint32_t *)ctx->code; + int n = (int)(ctx->codesz >> 2); + for (i = 0; i < n; i++, p++) + *p = lj_bswap(*p); /* Byteswap .text section. */ +#else + fprintf(stderr, "Error: different byte order for host and target\n"); + exit(1); +#endif + } + owrite(ctx, ctx->code, ctx->codesz); + for (i = 0; i < ctx->nreloc; i++) { + PEreloc reloc; + reloc.vaddr = (uint32_t)ctx->reloc[i].ofs + PEOBJ_RELOC_OFS; + reloc.symidx = 1+2+ctx->reloc[i].sym; /* Reloc syms are after .text sym. */ + reloc.type = ctx->reloc[i].type ? PEOBJ_RELOC_REL32 : PEOBJ_RELOC_DIR32; + owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); + } + +#if LJ_TARGET_X64 + { /* Write .pdata section. */ + uint32_t fcofs = (uint32_t)ctx->sym[ctx->nsym-1].ofs; + uint32_t pdata[3]; /* Start of .text, end of .text and .xdata. */ + PEreloc reloc; + pdata[0] = 0; pdata[1] = fcofs; pdata[2] = 0; + owrite(ctx, &pdata, sizeof(pdata)); + pdata[0] = fcofs; pdata[1] = (uint32_t)ctx->codesz; pdata[2] = 20; + owrite(ctx, &pdata, sizeof(pdata)); + reloc.vaddr = 0; reloc.symidx = 1+2+nrsym+2+2+1; + reloc.type = PEOBJ_RELOC_ADDR32NB; + owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); + reloc.vaddr = 4; reloc.symidx = 1+2+nrsym+2+2+1; + reloc.type = PEOBJ_RELOC_ADDR32NB; + owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); + reloc.vaddr = 8; reloc.symidx = 1+2+nrsym+2; + reloc.type = PEOBJ_RELOC_ADDR32NB; + owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); + reloc.vaddr = 12; reloc.symidx = 1+2+nrsym+2+2+1; + reloc.type = PEOBJ_RELOC_ADDR32NB; + owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); + reloc.vaddr = 16; reloc.symidx = 1+2+nrsym+2+2+1; + reloc.type = PEOBJ_RELOC_ADDR32NB; + owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); + reloc.vaddr = 20; reloc.symidx = 1+2+nrsym+2; + reloc.type = PEOBJ_RELOC_ADDR32NB; + owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); + } + { /* Write .xdata section. */ + uint16_t xdata[8+2+6]; + PEreloc reloc; + xdata[0] = 0x01|0x08|0x10; /* Ver. 1, uhandler/ehandler, prolog size 0. */ + xdata[1] = 0x0005; /* Number of unwind codes, no frame pointer. */ + xdata[2] = 0x4200; /* Stack offset 4*8+8 = aword*5. */ + xdata[3] = 0x3000; /* Push rbx. */ + xdata[4] = 0x6000; /* Push rsi. */ + xdata[5] = 0x7000; /* Push rdi. */ + xdata[6] = 0x5000; /* Push rbp. */ + xdata[7] = 0; /* Alignment. */ + xdata[8] = xdata[9] = 0; /* Relocated address of exception handler. */ + xdata[10] = 0x01; /* Ver. 1, no handler, prolog size 0. */ + xdata[11] = 0x1504; /* Number of unwind codes, fp = rbp, fpofs = 16. */ + xdata[12] = 0x0300; /* set_fpreg. */ + xdata[13] = 0x0200; /* stack offset 0*8+8 = aword*1. */ + xdata[14] = 0x3000; /* Push rbx. */ + xdata[15] = 0x5000; /* Push rbp. */ + owrite(ctx, &xdata, sizeof(xdata)); + reloc.vaddr = 2*8; reloc.symidx = 1+2+nrsym+2+2; + reloc.type = PEOBJ_RELOC_ADDR32NB; + owrite(ctx, &reloc, PEOBJ_RELOC_SIZE); + } +#endif + + /* Write .rdata$Z section. */ + owrite(ctx, ctx->dasm_ident, strlen(ctx->dasm_ident)+1); + + /* Write symbol table. */ + strtab = NULL; /* 1st pass: collect string sizes. */ + for (;;) { + strtabofs = 4; + /* Mark as SafeSEH compliant. */ + emit_peobj_sym(ctx, "@feat.00", 1, + PEOBJ_SECT_ABS, PEOBJ_TYPE_NULL, PEOBJ_SCL_STATIC); + + emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_TEXT); + for (i = 0; i < nrsym; i++) + emit_peobj_sym(ctx, ctx->relocsym[i], 0, + PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN); + +#if LJ_TARGET_X64 + emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_PDATA); + emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_XDATA); + emit_peobj_sym(ctx, "lj_err_unwind_win64", 0, + PEOBJ_SECT_UNDEF, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN); +#endif + + emit_peobj_sym(ctx, ctx->beginsym, 0, + PEOBJ_SECT_TEXT, PEOBJ_TYPE_NULL, PEOBJ_SCL_EXTERN); + for (i = 0; i < ctx->nsym; i++) + emit_peobj_sym(ctx, ctx->sym[i].name, (uint32_t)ctx->sym[i].ofs, + PEOBJ_SECT_TEXT, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN); + + emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_RDATA_Z); + + if (strtab) + break; + /* 2nd pass: alloc strtab, write syms and copy strings. */ + strtab = (char *)malloc(strtabofs); + *(uint32_t *)strtab = (uint32_t)strtabofs; + } + + /* Write string table. */ + owrite(ctx, strtab, strtabofs); +} + +#else + +void emit_peobj(BuildCtx *ctx) +{ + UNUSED(ctx); + fprintf(stderr, "Error: no PE object support for this target\n"); + exit(1); +} + +#endif diff --git a/external/lua/luajit/src/src/host/genminilua.lua b/external/lua/luajit/src/src/host/genminilua.lua new file mode 100644 index 0000000000..e666f088a8 --- /dev/null +++ b/external/lua/luajit/src/src/host/genminilua.lua @@ -0,0 +1,427 @@ +---------------------------------------------------------------------------- +-- Lua script to generate a customized, minified version of Lua. +-- The resulting 'minilua' is used for the build process of LuaJIT. +---------------------------------------------------------------------------- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- + +local sub, match, gsub = string.sub, string.match, string.gsub + +local LUA_VERSION = "5.1.5" +local LUA_SOURCE + +local function usage() + io.stderr:write("Usage: ", arg and arg[0] or "genminilua", + " lua-", LUA_VERSION, "-source-dir\n") + os.exit(1) +end + +local function find_sources() + LUA_SOURCE = arg and arg[1] + if not LUA_SOURCE then usage() end + if sub(LUA_SOURCE, -1) ~= "/" then LUA_SOURCE = LUA_SOURCE.."/" end + local fp = io.open(LUA_SOURCE .. "lua.h") + if not fp then + LUA_SOURCE = LUA_SOURCE.."src/" + fp = io.open(LUA_SOURCE .. "lua.h") + if not fp then usage() end + end + local all = fp:read("*a") + fp:close() + if not match(all, 'LUA_RELEASE%s*"Lua '..LUA_VERSION..'"') then + io.stderr:write("Error: version mismatch\n") + usage() + end +end + +local LUA_FILES = { +"lmem.c", "lobject.c", "ltm.c", "lfunc.c", "ldo.c", "lstring.c", "ltable.c", +"lgc.c", "lstate.c", "ldebug.c", "lzio.c", "lopcodes.c", +"llex.c", "lcode.c", "lparser.c", "lvm.c", "lapi.c", "lauxlib.c", +"lbaselib.c", "ltablib.c", "liolib.c", "loslib.c", "lstrlib.c", "linit.c", +} + +local REMOVE_LIB = {} +gsub([[ +collectgarbage dofile gcinfo getfenv getmetatable load print rawequal rawset +select tostring xpcall +foreach foreachi getn maxn setn +popen tmpfile seek setvbuf __tostring +clock date difftime execute getenv rename setlocale time tmpname +dump gfind len reverse +LUA_LOADLIBNAME LUA_MATHLIBNAME LUA_DBLIBNAME +]], "%S+", function(name) + REMOVE_LIB[name] = true +end) + +local REMOVE_EXTINC = { [""] = true, [""] = true, } + +local CUSTOM_MAIN = [[ +typedef unsigned int UB; +static UB barg(lua_State *L,int idx){ +union{lua_Number n;U64 b;}bn; +bn.n=lua_tonumber(L,idx)+6755399441055744.0; +if (bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number"); +return(UB)bn.b; +} +#define BRET(b) lua_pushnumber(L,(lua_Number)(int)(b));return 1; +static int tobit(lua_State *L){ +BRET(barg(L,1))} +static int bnot(lua_State *L){ +BRET(~barg(L,1))} +static int band(lua_State *L){ +int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)} +static int bor(lua_State *L){ +int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)} +static int bxor(lua_State *L){ +int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)} +static int lshift(lua_State *L){ +UB b=barg(L,1),n=barg(L,2)&31;BRET(b<>n)} +static int arshift(lua_State *L){ +UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)} +static int rol(lua_State *L){ +UB b=barg(L,1),n=barg(L,2)&31;BRET((b<>(32-n)))} +static int ror(lua_State *L){ +UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))} +static int bswap(lua_State *L){ +UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)} +static int tohex(lua_State *L){ +UB b=barg(L,1); +int n=lua_isnone(L,2)?8:(int)barg(L,2); +const char *hexdigits="0123456789abcdef"; +char buf[8]; +int i; +if(n<0){n=-n;hexdigits="0123456789ABCDEF";} +if(n>8)n=8; +for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} +lua_pushlstring(L,buf,(size_t)n); +return 1; +} +static const struct luaL_Reg bitlib[] = { +{"tobit",tobit}, +{"bnot",bnot}, +{"band",band}, +{"bor",bor}, +{"bxor",bxor}, +{"lshift",lshift}, +{"rshift",rshift}, +{"arshift",arshift}, +{"rol",rol}, +{"ror",ror}, +{"bswap",bswap}, +{"tohex",tohex}, +{NULL,NULL} +}; +int main(int argc, char **argv){ + lua_State *L = luaL_newstate(); + int i; + luaL_openlibs(L); + luaL_register(L, "bit", bitlib); + if (argc < 2) return sizeof(void *); + lua_createtable(L, 0, 1); + lua_pushstring(L, argv[1]); + lua_rawseti(L, -2, 0); + lua_setglobal(L, "arg"); + if (luaL_loadfile(L, argv[1])) + goto err; + for (i = 2; i < argc; i++) + lua_pushstring(L, argv[i]); + if (lua_pcall(L, argc - 2, 0, 0)) { + err: + fprintf(stderr, "Error: %s\n", lua_tostring(L, -1)); + return 1; + } + lua_close(L); + return 0; +} +]] + +local function read_sources() + local t = {} + for i, name in ipairs(LUA_FILES) do + local fp = assert(io.open(LUA_SOURCE..name, "r")) + t[i] = fp:read("*a") + assert(fp:close()) + end + t[#t+1] = CUSTOM_MAIN + return table.concat(t) +end + +local includes = {} + +local function merge_includes(src) + return gsub(src, '#include%s*"([^"]*)"%s*\n', function(name) + if includes[name] then return "" end + includes[name] = true + local fp = assert(io.open(LUA_SOURCE..name, "r")) + local src = fp:read("*a") + assert(fp:close()) + src = gsub(src, "#ifndef%s+%w+_h\n#define%s+%w+_h\n", "") + src = gsub(src, "#endif%s*$", "") + return merge_includes(src) + end) +end + +local function get_license(src) + return match(src, "/%*+\n%* Copyright %(.-%*/\n") +end + +local function fold_lines(src) + return gsub(src, "\\\n", " ") +end + +local strings = {} + +local function save_str(str) + local n = #strings+1 + strings[n] = str + return "\1"..n.."\2" +end + +local function save_strings(src) + src = gsub(src, '"[^"\n]*"', save_str) + return gsub(src, "'[^'\n]*'", save_str) +end + +local function restore_strings(src) + return gsub(src, "\1(%d+)\2", function(numstr) + return strings[tonumber(numstr)] + end) +end + +local function def_istrue(def) + return def == "INT_MAX > 2147483640L" or + def == "LUAI_BITSINT >= 32" or + def == "SIZE_Bx < LUAI_BITSINT-1" or + def == "cast" or + def == "defined(LUA_CORE)" or + def == "MINSTRTABSIZE" or + def == "LUA_MINBUFFER" or + def == "HARDSTACKTESTS" or + def == "UNUSED" +end + +local head, defs = {[[ +#ifdef _MSC_VER +typedef unsigned __int64 U64; +#else +typedef unsigned long long U64; +#endif +]]}, {} + +local function preprocess(src) + local t = { match(src, "^(.-)#") } + local lvl, on, oldon = 0, true, {} + for pp, def, txt in string.gmatch(src, "#(%w+) *([^\n]*)\n([^#]*)") do + if pp == "if" or pp == "ifdef" or pp == "ifndef" then + lvl = lvl + 1 + oldon[lvl] = on + on = def_istrue(def) + elseif pp == "else" then + if oldon[lvl] then + if on == false then on = true else on = false end + end + elseif pp == "elif" then + if oldon[lvl] then + on = def_istrue(def) + end + elseif pp == "endif" then + on = oldon[lvl] + lvl = lvl - 1 + elseif on then + if pp == "include" then + if not head[def] and not REMOVE_EXTINC[def] then + head[def] = true + head[#head+1] = "#include "..def.."\n" + end + elseif pp == "define" then + local k, sp, v = match(def, "([%w_]+)(%s*)(.*)") + if k and not (sp == "" and sub(v, 1, 1) == "(") then + defs[k] = gsub(v, "%a[%w_]*", function(tok) + return defs[tok] or tok + end) + else + t[#t+1] = "#define "..def.."\n" + end + elseif pp ~= "undef" then + error("unexpected directive: "..pp.." "..def) + end + end + if on then t[#t+1] = txt end + end + return gsub(table.concat(t), "%a[%w_]*", function(tok) + return defs[tok] or tok + end) +end + +local function merge_header(src, license) + local hdr = string.format([[ +/* This is a heavily customized and minimized copy of Lua %s. */ +/* It's only used to build LuaJIT. It does NOT have all standard functions! */ +]], LUA_VERSION) + return hdr..license..table.concat(head)..src +end + +local function strip_unused1(src) + return gsub(src, '( {"?([%w_]+)"?,%s+%a[%w_]*},\n)', function(line, func) + return REMOVE_LIB[func] and "" or line + end) +end + +local function strip_unused2(src) + return gsub(src, "Symbolic Execution.-}=", "") +end + +local function strip_unused3(src) + src = gsub(src, "extern", "static") + src = gsub(src, "\nstatic([^\n]-)%(([^)]*)%)%(", "\nstatic%1 %2(") + src = gsub(src, "#define lua_assert[^\n]*\n", "") + src = gsub(src, "lua_assert%b();?", "") + src = gsub(src, "default:\n}", "default:;\n}") + src = gsub(src, "lua_lock%b();", "") + src = gsub(src, "lua_unlock%b();", "") + src = gsub(src, "luai_threadyield%b();", "") + src = gsub(src, "luai_userstateopen%b();", "{}") + src = gsub(src, "luai_userstate%w+%b();", "") + src = gsub(src, "%(%(c==.*luaY_parser%)", "luaY_parser") + src = gsub(src, "trydecpoint%(ls,seminfo%)", + "luaX_lexerror(ls,\"malformed number\",TK_NUMBER)") + src = gsub(src, "int c=luaZ_lookahead%b();", "") + src = gsub(src, "luaL_register%(L,[^,]*,co_funcs%);\nreturn 2;", + "return 1;") + src = gsub(src, "getfuncname%b():", "NULL:") + src = gsub(src, "getobjname%b():", "NULL:") + src = gsub(src, "if%([^\n]*hookmask[^\n]*%)\n[^\n]*\n", "") + src = gsub(src, "if%([^\n]*hookmask[^\n]*%)%b{}\n", "") + src = gsub(src, "if%([^\n]*hookmask[^\n]*&&\n[^\n]*%b{}\n", "") + src = gsub(src, "(twoto%b()%()", "%1(size_t)") + src = gsub(src, "i -- BYTECODE -- [...] +-- print(bc.line(foo, 2)) --> 0002 KSTR 1 1 ; "hello" +-- +-- local out = { +-- -- Do something with each line: +-- write = function(t, ...) io.write(...) end, +-- close = function(t) end, +-- flush = function(t) end, +-- } +-- bc.dump(foo, out) +-- +------------------------------------------------------------------------------ + +-- Cache some library functions and objects. +local jit = require("jit") +assert(jit.version_num == 20001, "LuaJIT core/library version mismatch") +local jutil = require("jit.util") +local vmdef = require("jit.vmdef") +local bit = require("bit") +local sub, gsub, format = string.sub, string.gsub, string.format +local byte, band, shr = string.byte, bit.band, bit.rshift +local funcinfo, funcbc, funck = jutil.funcinfo, jutil.funcbc, jutil.funck +local funcuvname = jutil.funcuvname +local bcnames = vmdef.bcnames +local stdout, stderr = io.stdout, io.stderr + +------------------------------------------------------------------------------ + +local function ctlsub(c) + if c == "\n" then return "\\n" + elseif c == "\r" then return "\\r" + elseif c == "\t" then return "\\t" + else return format("\\%03d", byte(c)) + end +end + +-- Return one bytecode line. +local function bcline(func, pc, prefix) + local ins, m = funcbc(func, pc) + if not ins then return end + local ma, mb, mc = band(m, 7), band(m, 15*8), band(m, 15*128) + local a = band(shr(ins, 8), 0xff) + local oidx = 6*band(ins, 0xff) + local op = sub(bcnames, oidx+1, oidx+6) + local s = format("%04d %s %-6s %3s ", + pc, prefix or " ", op, ma == 0 and "" or a) + local d = shr(ins, 16) + if mc == 13*128 then -- BCMjump + return format("%s=> %04d\n", s, pc+d-0x7fff) + end + if mb ~= 0 then + d = band(d, 0xff) + elseif mc == 0 then + return s.."\n" + end + local kc + if mc == 10*128 then -- BCMstr + kc = funck(func, -d-1) + kc = format(#kc > 40 and '"%.40s"~' or '"%s"', gsub(kc, "%c", ctlsub)) + elseif mc == 9*128 then -- BCMnum + kc = funck(func, d) + if op == "TSETM " then kc = kc - 2^52 end + elseif mc == 12*128 then -- BCMfunc + local fi = funcinfo(funck(func, -d-1)) + if fi.ffid then + kc = vmdef.ffnames[fi.ffid] + else + kc = fi.loc + end + elseif mc == 5*128 then -- BCMuv + kc = funcuvname(func, d) + end + if ma == 5 then -- BCMuv + local ka = funcuvname(func, a) + if kc then kc = ka.." ; "..kc else kc = ka end + end + if mb ~= 0 then + local b = shr(ins, 24) + if kc then return format("%s%3d %3d ; %s\n", s, b, d, kc) end + return format("%s%3d %3d\n", s, b, d) + end + if kc then return format("%s%3d ; %s\n", s, d, kc) end + if mc == 7*128 and d > 32767 then d = d - 65536 end -- BCMlits + return format("%s%3d\n", s, d) +end + +-- Collect branch targets of a function. +local function bctargets(func) + local target = {} + for pc=1,1000000000 do + local ins, m = funcbc(func, pc) + if not ins then break end + if band(m, 15*128) == 13*128 then target[pc+shr(ins, 16)-0x7fff] = true end + end + return target +end + +-- Dump bytecode instructions of a function. +local function bcdump(func, out, all) + if not out then out = stdout end + local fi = funcinfo(func) + if all and fi.children then + for n=-1,-1000000000,-1 do + local k = funck(func, n) + if not k then break end + if type(k) == "proto" then bcdump(k, out, true) end + end + end + out:write(format("-- BYTECODE -- %s-%d\n", fi.loc, fi.lastlinedefined)) + local target = bctargets(func) + for pc=1,1000000000 do + local s = bcline(func, pc, target[pc] and "=>") + if not s then break end + out:write(s) + end + out:write("\n") + out:flush() +end + +------------------------------------------------------------------------------ + +-- Active flag and output file handle. +local active, out + +-- List handler. +local function h_list(func) + return bcdump(func, out) +end + +-- Detach list handler. +local function bclistoff() + if active then + active = false + jit.attach(h_list) + if out and out ~= stdout and out ~= stderr then out:close() end + out = nil + end +end + +-- Open the output file and attach list handler. +local function bcliston(outfile) + if active then bclistoff() end + if not outfile then outfile = os.getenv("LUAJIT_LISTFILE") end + if outfile then + out = outfile == "-" and stdout or assert(io.open(outfile, "w")) + else + out = stderr + end + jit.attach(h_list, "bc") + active = true +end + +-- Public module functions. +module(...) + +line = bcline +dump = bcdump +targets = bctargets + +on = bcliston +off = bclistoff +start = bcliston -- For -j command line option. + diff --git a/external/lua/luajit/src/src/jit/bcsave.lua b/external/lua/luajit/src/src/jit/bcsave.lua new file mode 100644 index 0000000000..25bd604259 --- /dev/null +++ b/external/lua/luajit/src/src/jit/bcsave.lua @@ -0,0 +1,659 @@ +---------------------------------------------------------------------------- +-- LuaJIT module to save/list bytecode. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- +-- +-- This module saves or lists the bytecode for an input file. +-- It's run by the -b command line option. +-- +------------------------------------------------------------------------------ + +local jit = require("jit") +assert(jit.version_num == 20001, "LuaJIT core/library version mismatch") +local bit = require("bit") + +-- Symbol name prefix for LuaJIT bytecode. +local LJBC_PREFIX = "luaJIT_BC_" + +------------------------------------------------------------------------------ + +local function usage() + io.stderr:write[[ +Save LuaJIT bytecode: luajit -b[options] input output + -l Only list bytecode. + -s Strip debug info (default). + -g Keep debug info. + -n name Set module name (default: auto-detect from input name). + -t type Set output file type (default: auto-detect from output name). + -a arch Override architecture for object files (default: native). + -o os Override OS for object files (default: native). + -e chunk Use chunk string as input. + -- Stop handling options. + - Use stdin as input and/or stdout as output. + +File types: c h obj o raw (default) +]] + os.exit(1) +end + +local function check(ok, ...) + if ok then return ok, ... end + io.stderr:write("luajit: ", ...) + io.stderr:write("\n") + os.exit(1) +end + +local function readfile(input) + if type(input) == "function" then return input end + if input == "-" then input = nil end + return check(loadfile(input)) +end + +local function savefile(name, mode) + if name == "-" then return io.stdout end + return check(io.open(name, mode)) +end + +------------------------------------------------------------------------------ + +local map_type = { + raw = "raw", c = "c", h = "h", o = "obj", obj = "obj", +} + +local map_arch = { + x86 = true, x64 = true, arm = true, ppc = true, ppcspe = true, + mips = true, mipsel = true, +} + +local map_os = { + linux = true, windows = true, osx = true, freebsd = true, netbsd = true, + openbsd = true, solaris = true, +} + +local function checkarg(str, map, err) + str = string.lower(str) + local s = check(map[str], "unknown ", err) + return s == true and str or s +end + +local function detecttype(str) + local ext = string.match(string.lower(str), "%.(%a+)$") + return map_type[ext] or "raw" +end + +local function checkmodname(str) + check(string.match(str, "^[%w_.%-]+$"), "bad module name") + return string.gsub(str, "[%.%-]", "_") +end + +local function detectmodname(str) + if type(str) == "string" then + local tail = string.match(str, "[^/\\]+$") + if tail then str = tail end + local head = string.match(str, "^(.*)%.[^.]*$") + if head then str = head end + str = string.match(str, "^[%w_.%-]+") + else + str = nil + end + check(str, "cannot derive module name, use -n name") + return string.gsub(str, "[%.%-]", "_") +end + +------------------------------------------------------------------------------ + +local function bcsave_tail(fp, output, s) + local ok, err = fp:write(s) + if ok and output ~= "-" then ok, err = fp:close() end + check(ok, "cannot write ", output, ": ", err) +end + +local function bcsave_raw(output, s) + local fp = savefile(output, "wb") + bcsave_tail(fp, output, s) +end + +local function bcsave_c(ctx, output, s) + local fp = savefile(output, "w") + if ctx.type == "c" then + fp:write(string.format([[ +#ifdef _cplusplus +extern "C" +#endif +#ifdef _WIN32 +__declspec(dllexport) +#endif +const char %s%s[] = { +]], LJBC_PREFIX, ctx.modname)) + else + fp:write(string.format([[ +#define %s%s_SIZE %d +static const char %s%s[] = { +]], LJBC_PREFIX, ctx.modname, #s, LJBC_PREFIX, ctx.modname)) + end + local t, n, m = {}, 0, 0 + for i=1,#s do + local b = tostring(string.byte(s, i)) + m = m + #b + 1 + if m > 78 then + fp:write(table.concat(t, ",", 1, n), ",\n") + n, m = 0, #b + 1 + end + n = n + 1 + t[n] = b + end + bcsave_tail(fp, output, table.concat(t, ",", 1, n).."\n};\n") +end + +local function bcsave_elfobj(ctx, output, s, ffi) + ffi.cdef[[ +typedef struct { + uint8_t emagic[4], eclass, eendian, eversion, eosabi, eabiversion, epad[7]; + uint16_t type, machine; + uint32_t version; + uint32_t entry, phofs, shofs; + uint32_t flags; + uint16_t ehsize, phentsize, phnum, shentsize, shnum, shstridx; +} ELF32header; +typedef struct { + uint8_t emagic[4], eclass, eendian, eversion, eosabi, eabiversion, epad[7]; + uint16_t type, machine; + uint32_t version; + uint64_t entry, phofs, shofs; + uint32_t flags; + uint16_t ehsize, phentsize, phnum, shentsize, shnum, shstridx; +} ELF64header; +typedef struct { + uint32_t name, type, flags, addr, ofs, size, link, info, align, entsize; +} ELF32sectheader; +typedef struct { + uint32_t name, type; + uint64_t flags, addr, ofs, size; + uint32_t link, info; + uint64_t align, entsize; +} ELF64sectheader; +typedef struct { + uint32_t name, value, size; + uint8_t info, other; + uint16_t sectidx; +} ELF32symbol; +typedef struct { + uint32_t name; + uint8_t info, other; + uint16_t sectidx; + uint64_t value, size; +} ELF64symbol; +typedef struct { + ELF32header hdr; + ELF32sectheader sect[6]; + ELF32symbol sym[2]; + uint8_t space[4096]; +} ELF32obj; +typedef struct { + ELF64header hdr; + ELF64sectheader sect[6]; + ELF64symbol sym[2]; + uint8_t space[4096]; +} ELF64obj; +]] + local symname = LJBC_PREFIX..ctx.modname + local is64, isbe = false, false + if ctx.arch == "x64" then + is64 = true + elseif ctx.arch == "ppc" or ctx.arch == "ppcspe" or ctx.arch == "mips" then + isbe = true + end + + -- Handle different host/target endianess. + local function f32(x) return x end + local f16, fofs = f32, f32 + if ffi.abi("be") ~= isbe then + f32 = bit.bswap + function f16(x) return bit.rshift(bit.bswap(x), 16) end + if is64 then + local two32 = ffi.cast("int64_t", 2^32) + function fofs(x) return bit.bswap(x)*two32 end + else + fofs = f32 + end + end + + -- Create ELF object and fill in header. + local o = ffi.new(is64 and "ELF64obj" or "ELF32obj") + local hdr = o.hdr + if ctx.os == "bsd" or ctx.os == "other" then -- Determine native hdr.eosabi. + local bf = assert(io.open("/bin/ls", "rb")) + local bs = bf:read(9) + bf:close() + ffi.copy(o, bs, 9) + check(hdr.emagic[0] == 127, "no support for writing native object files") + else + hdr.emagic = "\127ELF" + hdr.eosabi = ({ freebsd=9, netbsd=2, openbsd=12, solaris=6 })[ctx.os] or 0 + end + hdr.eclass = is64 and 2 or 1 + hdr.eendian = isbe and 2 or 1 + hdr.eversion = 1 + hdr.type = f16(1) + hdr.machine = f16(({ x86=3, x64=62, arm=40, ppc=20, ppcspe=20, mips=8, mipsel=8 })[ctx.arch]) + if ctx.arch == "mips" or ctx.arch == "mipsel" then + hdr.flags = 0x50001006 + end + hdr.version = f32(1) + hdr.shofs = fofs(ffi.offsetof(o, "sect")) + hdr.ehsize = f16(ffi.sizeof(hdr)) + hdr.shentsize = f16(ffi.sizeof(o.sect[0])) + hdr.shnum = f16(6) + hdr.shstridx = f16(2) + + -- Fill in sections and symbols. + local sofs, ofs = ffi.offsetof(o, "space"), 1 + for i,name in ipairs{ + ".symtab", ".shstrtab", ".strtab", ".rodata", ".note.GNU-stack", + } do + local sect = o.sect[i] + sect.align = fofs(1) + sect.name = f32(ofs) + ffi.copy(o.space+ofs, name) + ofs = ofs + #name+1 + end + o.sect[1].type = f32(2) -- .symtab + o.sect[1].link = f32(3) + o.sect[1].info = f32(1) + o.sect[1].align = fofs(8) + o.sect[1].ofs = fofs(ffi.offsetof(o, "sym")) + o.sect[1].entsize = fofs(ffi.sizeof(o.sym[0])) + o.sect[1].size = fofs(ffi.sizeof(o.sym)) + o.sym[1].name = f32(1) + o.sym[1].sectidx = f16(4) + o.sym[1].size = fofs(#s) + o.sym[1].info = 17 + o.sect[2].type = f32(3) -- .shstrtab + o.sect[2].ofs = fofs(sofs) + o.sect[2].size = fofs(ofs) + o.sect[3].type = f32(3) -- .strtab + o.sect[3].ofs = fofs(sofs + ofs) + o.sect[3].size = fofs(#symname+1) + ffi.copy(o.space+ofs+1, symname) + ofs = ofs + #symname + 2 + o.sect[4].type = f32(1) -- .rodata + o.sect[4].flags = fofs(2) + o.sect[4].ofs = fofs(sofs + ofs) + o.sect[4].size = fofs(#s) + o.sect[5].type = f32(1) -- .note.GNU-stack + o.sect[5].ofs = fofs(sofs + ofs + #s) + + -- Write ELF object file. + local fp = savefile(output, "wb") + fp:write(ffi.string(o, ffi.sizeof(o)-4096+ofs)) + bcsave_tail(fp, output, s) +end + +local function bcsave_peobj(ctx, output, s, ffi) + ffi.cdef[[ +typedef struct { + uint16_t arch, nsects; + uint32_t time, symtabofs, nsyms; + uint16_t opthdrsz, flags; +} PEheader; +typedef struct { + char name[8]; + uint32_t vsize, vaddr, size, ofs, relocofs, lineofs; + uint16_t nreloc, nline; + uint32_t flags; +} PEsection; +typedef struct __attribute((packed)) { + union { + char name[8]; + uint32_t nameref[2]; + }; + uint32_t value; + int16_t sect; + uint16_t type; + uint8_t scl, naux; +} PEsym; +typedef struct __attribute((packed)) { + uint32_t size; + uint16_t nreloc, nline; + uint32_t cksum; + uint16_t assoc; + uint8_t comdatsel, unused[3]; +} PEsymaux; +typedef struct { + PEheader hdr; + PEsection sect[2]; + // Must be an even number of symbol structs. + PEsym sym0; + PEsymaux sym0aux; + PEsym sym1; + PEsymaux sym1aux; + PEsym sym2; + PEsym sym3; + uint32_t strtabsize; + uint8_t space[4096]; +} PEobj; +]] + local symname = LJBC_PREFIX..ctx.modname + local is64 = false + if ctx.arch == "x86" then + symname = "_"..symname + elseif ctx.arch == "x64" then + is64 = true + end + local symexport = " /EXPORT:"..symname..",DATA " + + -- The file format is always little-endian. Swap if the host is big-endian. + local function f32(x) return x end + local f16 = f32 + if ffi.abi("be") then + f32 = bit.bswap + function f16(x) return bit.rshift(bit.bswap(x), 16) end + end + + -- Create PE object and fill in header. + local o = ffi.new("PEobj") + local hdr = o.hdr + hdr.arch = f16(({ x86=0x14c, x64=0x8664, arm=0x1c0, ppc=0x1f2, mips=0x366, mipsel=0x366 })[ctx.arch]) + hdr.nsects = f16(2) + hdr.symtabofs = f32(ffi.offsetof(o, "sym0")) + hdr.nsyms = f32(6) + + -- Fill in sections and symbols. + o.sect[0].name = ".drectve" + o.sect[0].size = f32(#symexport) + o.sect[0].flags = f32(0x00100a00) + o.sym0.sect = f16(1) + o.sym0.scl = 3 + o.sym0.name = ".drectve" + o.sym0.naux = 1 + o.sym0aux.size = f32(#symexport) + o.sect[1].name = ".rdata" + o.sect[1].size = f32(#s) + o.sect[1].flags = f32(0x40300040) + o.sym1.sect = f16(2) + o.sym1.scl = 3 + o.sym1.name = ".rdata" + o.sym1.naux = 1 + o.sym1aux.size = f32(#s) + o.sym2.sect = f16(2) + o.sym2.scl = 2 + o.sym2.nameref[1] = f32(4) + o.sym3.sect = f16(-1) + o.sym3.scl = 2 + o.sym3.value = f32(1) + o.sym3.name = "@feat.00" -- Mark as SafeSEH compliant. + ffi.copy(o.space, symname) + local ofs = #symname + 1 + o.strtabsize = f32(ofs + 4) + o.sect[0].ofs = f32(ffi.offsetof(o, "space") + ofs) + ffi.copy(o.space + ofs, symexport) + ofs = ofs + #symexport + o.sect[1].ofs = f32(ffi.offsetof(o, "space") + ofs) + + -- Write PE object file. + local fp = savefile(output, "wb") + fp:write(ffi.string(o, ffi.sizeof(o)-4096+ofs)) + bcsave_tail(fp, output, s) +end + +local function bcsave_machobj(ctx, output, s, ffi) + ffi.cdef[[ +typedef struct +{ + uint32_t magic, cputype, cpusubtype, filetype, ncmds, sizeofcmds, flags; +} mach_header; +typedef struct +{ + mach_header; uint32_t reserved; +} mach_header_64; +typedef struct { + uint32_t cmd, cmdsize; + char segname[16]; + uint32_t vmaddr, vmsize, fileoff, filesize; + uint32_t maxprot, initprot, nsects, flags; +} mach_segment_command; +typedef struct { + uint32_t cmd, cmdsize; + char segname[16]; + uint64_t vmaddr, vmsize, fileoff, filesize; + uint32_t maxprot, initprot, nsects, flags; +} mach_segment_command_64; +typedef struct { + char sectname[16], segname[16]; + uint32_t addr, size; + uint32_t offset, align, reloff, nreloc, flags; + uint32_t reserved1, reserved2; +} mach_section; +typedef struct { + char sectname[16], segname[16]; + uint64_t addr, size; + uint32_t offset, align, reloff, nreloc, flags; + uint32_t reserved1, reserved2, reserved3; +} mach_section_64; +typedef struct { + uint32_t cmd, cmdsize, symoff, nsyms, stroff, strsize; +} mach_symtab_command; +typedef struct { + int32_t strx; + uint8_t type, sect; + int16_t desc; + uint32_t value; +} mach_nlist; +typedef struct { + uint32_t strx; + uint8_t type, sect; + uint16_t desc; + uint64_t value; +} mach_nlist_64; +typedef struct +{ + uint32_t magic, nfat_arch; +} mach_fat_header; +typedef struct +{ + uint32_t cputype, cpusubtype, offset, size, align; +} mach_fat_arch; +typedef struct { + struct { + mach_header hdr; + mach_segment_command seg; + mach_section sec; + mach_symtab_command sym; + } arch[1]; + mach_nlist sym_entry; + uint8_t space[4096]; +} mach_obj; +typedef struct { + struct { + mach_header_64 hdr; + mach_segment_command_64 seg; + mach_section_64 sec; + mach_symtab_command sym; + } arch[1]; + mach_nlist_64 sym_entry; + uint8_t space[4096]; +} mach_obj_64; +typedef struct { + mach_fat_header fat; + mach_fat_arch fat_arch[4]; + struct { + mach_header hdr; + mach_segment_command seg; + mach_section sec; + mach_symtab_command sym; + } arch[4]; + mach_nlist sym_entry; + uint8_t space[4096]; +} mach_fat_obj; +]] + local symname = '_'..LJBC_PREFIX..ctx.modname + local isfat, is64, align, mobj = false, false, 4, "mach_obj" + if ctx.arch == "x64" then + is64, align, mobj = true, 8, "mach_obj_64" + elseif ctx.arch == "arm" then + isfat, mobj = true, "mach_fat_obj" + else + check(ctx.arch == "x86", "unsupported architecture for OSX") + end + local function aligned(v, a) return bit.band(v+a-1, -a) end + local be32 = bit.bswap -- Mach-O FAT is BE, supported archs are LE. + + -- Create Mach-O object and fill in header. + local o = ffi.new(mobj) + local mach_size = aligned(ffi.offsetof(o, "space")+#symname+2, align) + local cputype = ({ x86={7}, x64={0x01000007}, arm={7,12,12,12} })[ctx.arch] + local cpusubtype = ({ x86={3}, x64={3}, arm={3,6,9,11} })[ctx.arch] + if isfat then + o.fat.magic = be32(0xcafebabe) + o.fat.nfat_arch = be32(#cpusubtype) + end + + -- Fill in sections and symbols. + for i=0,#cpusubtype-1 do + local ofs = 0 + if isfat then + local a = o.fat_arch[i] + a.cputype = be32(cputype[i+1]) + a.cpusubtype = be32(cpusubtype[i+1]) + -- Subsequent slices overlap each other to share data. + ofs = ffi.offsetof(o, "arch") + i*ffi.sizeof(o.arch[0]) + a.offset = be32(ofs) + a.size = be32(mach_size-ofs+#s) + end + local a = o.arch[i] + a.hdr.magic = is64 and 0xfeedfacf or 0xfeedface + a.hdr.cputype = cputype[i+1] + a.hdr.cpusubtype = cpusubtype[i+1] + a.hdr.filetype = 1 + a.hdr.ncmds = 2 + a.hdr.sizeofcmds = ffi.sizeof(a.seg)+ffi.sizeof(a.sec)+ffi.sizeof(a.sym) + a.seg.cmd = is64 and 0x19 or 0x1 + a.seg.cmdsize = ffi.sizeof(a.seg)+ffi.sizeof(a.sec) + a.seg.vmsize = #s + a.seg.fileoff = mach_size-ofs + a.seg.filesize = #s + a.seg.maxprot = 1 + a.seg.initprot = 1 + a.seg.nsects = 1 + ffi.copy(a.sec.sectname, "__data") + ffi.copy(a.sec.segname, "__DATA") + a.sec.size = #s + a.sec.offset = mach_size-ofs + a.sym.cmd = 2 + a.sym.cmdsize = ffi.sizeof(a.sym) + a.sym.symoff = ffi.offsetof(o, "sym_entry")-ofs + a.sym.nsyms = 1 + a.sym.stroff = ffi.offsetof(o, "sym_entry")+ffi.sizeof(o.sym_entry)-ofs + a.sym.strsize = aligned(#symname+2, align) + end + o.sym_entry.type = 0xf + o.sym_entry.sect = 1 + o.sym_entry.strx = 1 + ffi.copy(o.space+1, symname) + + -- Write Macho-O object file. + local fp = savefile(output, "wb") + fp:write(ffi.string(o, mach_size)) + bcsave_tail(fp, output, s) +end + +local function bcsave_obj(ctx, output, s) + local ok, ffi = pcall(require, "ffi") + check(ok, "FFI library required to write this file type") + if ctx.os == "windows" then + return bcsave_peobj(ctx, output, s, ffi) + elseif ctx.os == "osx" then + return bcsave_machobj(ctx, output, s, ffi) + else + return bcsave_elfobj(ctx, output, s, ffi) + end +end + +------------------------------------------------------------------------------ + +local function bclist(input, output) + local f = readfile(input) + require("jit.bc").dump(f, savefile(output, "w"), true) +end + +local function bcsave(ctx, input, output) + local f = readfile(input) + local s = string.dump(f, ctx.strip) + local t = ctx.type + if not t then + t = detecttype(output) + ctx.type = t + end + if t == "raw" then + bcsave_raw(output, s) + else + if not ctx.modname then ctx.modname = detectmodname(input) end + if t == "obj" then + bcsave_obj(ctx, output, s) + else + bcsave_c(ctx, output, s) + end + end +end + +local function docmd(...) + local arg = {...} + local n = 1 + local list = false + local ctx = { + strip = true, arch = jit.arch, os = string.lower(jit.os), + type = false, modname = false, + } + while n <= #arg do + local a = arg[n] + if type(a) == "string" and string.sub(a, 1, 1) == "-" and a ~= "-" then + table.remove(arg, n) + if a == "--" then break end + for m=2,#a do + local opt = string.sub(a, m, m) + if opt == "l" then + list = true + elseif opt == "s" then + ctx.strip = true + elseif opt == "g" then + ctx.strip = false + else + if arg[n] == nil or m ~= #a then usage() end + if opt == "e" then + if n ~= 1 then usage() end + arg[1] = check(loadstring(arg[1])) + elseif opt == "n" then + ctx.modname = checkmodname(table.remove(arg, n)) + elseif opt == "t" then + ctx.type = checkarg(table.remove(arg, n), map_type, "file type") + elseif opt == "a" then + ctx.arch = checkarg(table.remove(arg, n), map_arch, "architecture") + elseif opt == "o" then + ctx.os = checkarg(table.remove(arg, n), map_os, "OS name") + else + usage() + end + end + end + else + n = n + 1 + end + end + if list then + if #arg == 0 or #arg > 2 then usage() end + bclist(arg[1], arg[2] or "-") + else + if #arg ~= 2 then usage() end + bcsave(ctx, arg[1], arg[2]) + end +end + +------------------------------------------------------------------------------ + +-- Public module functions. +module(...) + +start = docmd -- Process -b command line option. + diff --git a/external/lua/luajit/src/src/jit/dis_arm.lua b/external/lua/luajit/src/src/jit/dis_arm.lua new file mode 100644 index 0000000000..dc7ca71ff4 --- /dev/null +++ b/external/lua/luajit/src/src/jit/dis_arm.lua @@ -0,0 +1,689 @@ +---------------------------------------------------------------------------- +-- LuaJIT ARM disassembler module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- +-- This is a helper module used by the LuaJIT machine code dumper module. +-- +-- It disassembles most user-mode ARMv7 instructions +-- NYI: Advanced SIMD and VFP instructions. +------------------------------------------------------------------------------ + +local type = type +local sub, byte, format = string.sub, string.byte, string.format +local match, gmatch, gsub = string.match, string.gmatch, string.gsub +local concat = table.concat +local bit = require("bit") +local band, bor, ror, tohex = bit.band, bit.bor, bit.ror, bit.tohex +local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift + +------------------------------------------------------------------------------ +-- Opcode maps +------------------------------------------------------------------------------ + +local map_loadc = { + shift = 8, mask = 15, + [10] = { + shift = 20, mask = 1, + [0] = { + shift = 23, mask = 3, + [0] = "vmovFmDN", "vstmFNdr", + _ = { + shift = 21, mask = 1, + [0] = "vstrFdl", + { shift = 16, mask = 15, [13] = "vpushFdr", _ = "vstmdbFNdr", } + }, + }, + { + shift = 23, mask = 3, + [0] = "vmovFDNm", + { shift = 16, mask = 15, [13] = "vpopFdr", _ = "vldmFNdr", }, + _ = { + shift = 21, mask = 1, + [0] = "vldrFdl", "vldmdbFNdr", + }, + }, + }, + [11] = { + shift = 20, mask = 1, + [0] = { + shift = 23, mask = 3, + [0] = "vmovGmDN", "vstmGNdr", + _ = { + shift = 21, mask = 1, + [0] = "vstrGdl", + { shift = 16, mask = 15, [13] = "vpushGdr", _ = "vstmdbGNdr", } + }, + }, + { + shift = 23, mask = 3, + [0] = "vmovGDNm", + { shift = 16, mask = 15, [13] = "vpopGdr", _ = "vldmGNdr", }, + _ = { + shift = 21, mask = 1, + [0] = "vldrGdl", "vldmdbGNdr", + }, + }, + }, + _ = { + shift = 0, mask = 0 -- NYI ldc, mcrr, mrrc. + }, +} + +local map_vfps = { + shift = 6, mask = 0x2c001, + [0] = "vmlaF.dnm", "vmlsF.dnm", + [0x04000] = "vnmlsF.dnm", [0x04001] = "vnmlaF.dnm", + [0x08000] = "vmulF.dnm", [0x08001] = "vnmulF.dnm", + [0x0c000] = "vaddF.dnm", [0x0c001] = "vsubF.dnm", + [0x20000] = "vdivF.dnm", + [0x24000] = "vfnmsF.dnm", [0x24001] = "vfnmaF.dnm", + [0x28000] = "vfmaF.dnm", [0x28001] = "vfmsF.dnm", + [0x2c000] = "vmovF.dY", + [0x2c001] = { + shift = 7, mask = 0x1e01, + [0] = "vmovF.dm", "vabsF.dm", + [0x0200] = "vnegF.dm", [0x0201] = "vsqrtF.dm", + [0x0800] = "vcmpF.dm", [0x0801] = "vcmpeF.dm", + [0x0a00] = "vcmpzF.d", [0x0a01] = "vcmpzeF.d", + [0x0e01] = "vcvtG.dF.m", + [0x1000] = "vcvt.f32.u32Fdm", [0x1001] = "vcvt.f32.s32Fdm", + [0x1800] = "vcvtr.u32F.dm", [0x1801] = "vcvt.u32F.dm", + [0x1a00] = "vcvtr.s32F.dm", [0x1a01] = "vcvt.s32F.dm", + }, +} + +local map_vfpd = { + shift = 6, mask = 0x2c001, + [0] = "vmlaG.dnm", "vmlsG.dnm", + [0x04000] = "vnmlsG.dnm", [0x04001] = "vnmlaG.dnm", + [0x08000] = "vmulG.dnm", [0x08001] = "vnmulG.dnm", + [0x0c000] = "vaddG.dnm", [0x0c001] = "vsubG.dnm", + [0x20000] = "vdivG.dnm", + [0x24000] = "vfnmsG.dnm", [0x24001] = "vfnmaG.dnm", + [0x28000] = "vfmaG.dnm", [0x28001] = "vfmsG.dnm", + [0x2c000] = "vmovG.dY", + [0x2c001] = { + shift = 7, mask = 0x1e01, + [0] = "vmovG.dm", "vabsG.dm", + [0x0200] = "vnegG.dm", [0x0201] = "vsqrtG.dm", + [0x0800] = "vcmpG.dm", [0x0801] = "vcmpeG.dm", + [0x0a00] = "vcmpzG.d", [0x0a01] = "vcmpzeG.d", + [0x0e01] = "vcvtF.dG.m", + [0x1000] = "vcvt.f64.u32GdFm", [0x1001] = "vcvt.f64.s32GdFm", + [0x1800] = "vcvtr.u32FdG.m", [0x1801] = "vcvt.u32FdG.m", + [0x1a00] = "vcvtr.s32FdG.m", [0x1a01] = "vcvt.s32FdG.m", + }, +} + +local map_datac = { + shift = 24, mask = 1, + [0] = { + shift = 4, mask = 1, + [0] = { + shift = 8, mask = 15, + [10] = map_vfps, + [11] = map_vfpd, + -- NYI cdp, mcr, mrc. + }, + { + shift = 8, mask = 15, + [10] = { + shift = 20, mask = 15, + [0] = "vmovFnD", "vmovFDn", + [14] = "vmsrD", + [15] = { shift = 12, mask = 15, [15] = "vmrs", _ = "vmrsD", }, + }, + }, + }, + "svcT", +} + +local map_loadcu = { + shift = 0, mask = 0, -- NYI unconditional CP load/store. +} + +local map_datacu = { + shift = 0, mask = 0, -- NYI unconditional CP data. +} + +local map_simddata = { + shift = 0, mask = 0, -- NYI SIMD data. +} + +local map_simdload = { + shift = 0, mask = 0, -- NYI SIMD load/store, preload. +} + +local map_preload = { + shift = 0, mask = 0, -- NYI preload. +} + +local map_media = { + shift = 20, mask = 31, + [0] = false, + { --01 + shift = 5, mask = 7, + [0] = "sadd16DNM", "sasxDNM", "ssaxDNM", "ssub16DNM", + "sadd8DNM", false, false, "ssub8DNM", + }, + { --02 + shift = 5, mask = 7, + [0] = "qadd16DNM", "qasxDNM", "qsaxDNM", "qsub16DNM", + "qadd8DNM", false, false, "qsub8DNM", + }, + { --03 + shift = 5, mask = 7, + [0] = "shadd16DNM", "shasxDNM", "shsaxDNM", "shsub16DNM", + "shadd8DNM", false, false, "shsub8DNM", + }, + false, + { --05 + shift = 5, mask = 7, + [0] = "uadd16DNM", "uasxDNM", "usaxDNM", "usub16DNM", + "uadd8DNM", false, false, "usub8DNM", + }, + { --06 + shift = 5, mask = 7, + [0] = "uqadd16DNM", "uqasxDNM", "uqsaxDNM", "uqsub16DNM", + "uqadd8DNM", false, false, "uqsub8DNM", + }, + { --07 + shift = 5, mask = 7, + [0] = "uhadd16DNM", "uhasxDNM", "uhsaxDNM", "uhsub16DNM", + "uhadd8DNM", false, false, "uhsub8DNM", + }, + { --08 + shift = 5, mask = 7, + [0] = "pkhbtDNMU", false, "pkhtbDNMU", + { shift = 16, mask = 15, [15] = "sxtb16DMU", _ = "sxtab16DNMU", }, + "pkhbtDNMU", "selDNM", "pkhtbDNMU", + }, + false, + { --0a + shift = 5, mask = 7, + [0] = "ssatDxMu", "ssat16DxM", "ssatDxMu", + { shift = 16, mask = 15, [15] = "sxtbDMU", _ = "sxtabDNMU", }, + "ssatDxMu", false, "ssatDxMu", + }, + { --0b + shift = 5, mask = 7, + [0] = "ssatDxMu", "revDM", "ssatDxMu", + { shift = 16, mask = 15, [15] = "sxthDMU", _ = "sxtahDNMU", }, + "ssatDxMu", "rev16DM", "ssatDxMu", + }, + { --0c + shift = 5, mask = 7, + [3] = { shift = 16, mask = 15, [15] = "uxtb16DMU", _ = "uxtab16DNMU", }, + }, + false, + { --0e + shift = 5, mask = 7, + [0] = "usatDwMu", "usat16DwM", "usatDwMu", + { shift = 16, mask = 15, [15] = "uxtbDMU", _ = "uxtabDNMU", }, + "usatDwMu", false, "usatDwMu", + }, + { --0f + shift = 5, mask = 7, + [0] = "usatDwMu", "rbitDM", "usatDwMu", + { shift = 16, mask = 15, [15] = "uxthDMU", _ = "uxtahDNMU", }, + "usatDwMu", "revshDM", "usatDwMu", + }, + { --10 + shift = 12, mask = 15, + [15] = { + shift = 5, mask = 7, + "smuadNMS", "smuadxNMS", "smusdNMS", "smusdxNMS", + }, + _ = { + shift = 5, mask = 7, + [0] = "smladNMSD", "smladxNMSD", "smlsdNMSD", "smlsdxNMSD", + }, + }, + false, false, false, + { --14 + shift = 5, mask = 7, + [0] = "smlaldDNMS", "smlaldxDNMS", "smlsldDNMS", "smlsldxDNMS", + }, + { --15 + shift = 5, mask = 7, + [0] = { shift = 12, mask = 15, [15] = "smmulNMS", _ = "smmlaNMSD", }, + { shift = 12, mask = 15, [15] = "smmulrNMS", _ = "smmlarNMSD", }, + false, false, false, false, + "smmlsNMSD", "smmlsrNMSD", + }, + false, false, + { --18 + shift = 5, mask = 7, + [0] = { shift = 12, mask = 15, [15] = "usad8NMS", _ = "usada8NMSD", }, + }, + false, + { --1a + shift = 5, mask = 3, [2] = "sbfxDMvw", + }, + { --1b + shift = 5, mask = 3, [2] = "sbfxDMvw", + }, + { --1c + shift = 5, mask = 3, + [0] = { shift = 0, mask = 15, [15] = "bfcDvX", _ = "bfiDMvX", }, + }, + { --1d + shift = 5, mask = 3, + [0] = { shift = 0, mask = 15, [15] = "bfcDvX", _ = "bfiDMvX", }, + }, + { --1e + shift = 5, mask = 3, [2] = "ubfxDMvw", + }, + { --1f + shift = 5, mask = 3, [2] = "ubfxDMvw", + }, +} + +local map_load = { + shift = 21, mask = 9, + { + shift = 20, mask = 5, + [0] = "strtDL", "ldrtDL", [4] = "strbtDL", [5] = "ldrbtDL", + }, + _ = { + shift = 20, mask = 5, + [0] = "strDL", "ldrDL", [4] = "strbDL", [5] = "ldrbDL", + } +} + +local map_load1 = { + shift = 4, mask = 1, + [0] = map_load, map_media, +} + +local map_loadm = { + shift = 20, mask = 1, + [0] = { + shift = 23, mask = 3, + [0] = "stmdaNR", "stmNR", + { shift = 16, mask = 63, [45] = "pushR", _ = "stmdbNR", }, "stmibNR", + }, + { + shift = 23, mask = 3, + [0] = "ldmdaNR", { shift = 16, mask = 63, [61] = "popR", _ = "ldmNR", }, + "ldmdbNR", "ldmibNR", + }, +} + +local map_data = { + shift = 21, mask = 15, + [0] = "andDNPs", "eorDNPs", "subDNPs", "rsbDNPs", + "addDNPs", "adcDNPs", "sbcDNPs", "rscDNPs", + "tstNP", "teqNP", "cmpNP", "cmnNP", + "orrDNPs", "movDPs", "bicDNPs", "mvnDPs", +} + +local map_mul = { + shift = 21, mask = 7, + [0] = "mulNMSs", "mlaNMSDs", "umaalDNMS", "mlsDNMS", + "umullDNMSs", "umlalDNMSs", "smullDNMSs", "smlalDNMSs", +} + +local map_sync = { + shift = 20, mask = 15, -- NYI: brackets around N. R(D+1) for ldrexd/strexd. + [0] = "swpDMN", false, false, false, + "swpbDMN", false, false, false, + "strexDMN", "ldrexDN", "strexdDN", "ldrexdDN", + "strexbDMN", "ldrexbDN", "strexhDN", "ldrexhDN", +} + +local map_mulh = { + shift = 21, mask = 3, + [0] = { shift = 5, mask = 3, + [0] = "smlabbNMSD", "smlatbNMSD", "smlabtNMSD", "smlattNMSD", }, + { shift = 5, mask = 3, + [0] = "smlawbNMSD", "smulwbNMS", "smlawtNMSD", "smulwtNMS", }, + { shift = 5, mask = 3, + [0] = "smlalbbDNMS", "smlaltbDNMS", "smlalbtDNMS", "smlalttDNMS", }, + { shift = 5, mask = 3, + [0] = "smulbbNMS", "smultbNMS", "smulbtNMS", "smulttNMS", }, +} + +local map_misc = { + shift = 4, mask = 7, + -- NYI: decode PSR bits of msr. + [0] = { shift = 21, mask = 1, [0] = "mrsD", "msrM", }, + { shift = 21, mask = 3, "bxM", false, "clzDM", }, + { shift = 21, mask = 3, "bxjM", }, + { shift = 21, mask = 3, "blxM", }, + false, + { shift = 21, mask = 3, [0] = "qaddDMN", "qsubDMN", "qdaddDMN", "qdsubDMN", }, + false, + { shift = 21, mask = 3, "bkptK", }, +} + +local map_datar = { + shift = 4, mask = 9, + [9] = { + shift = 5, mask = 3, + [0] = { shift = 24, mask = 1, [0] = map_mul, map_sync, }, + { shift = 20, mask = 1, [0] = "strhDL", "ldrhDL", }, + { shift = 20, mask = 1, [0] = "ldrdDL", "ldrsbDL", }, + { shift = 20, mask = 1, [0] = "strdDL", "ldrshDL", }, + }, + _ = { + shift = 20, mask = 25, + [16] = { shift = 7, mask = 1, [0] = map_misc, map_mulh, }, + _ = { + shift = 0, mask = 0xffffffff, + [bor(0xe1a00000)] = "nop", + _ = map_data, + } + }, +} + +local map_datai = { + shift = 20, mask = 31, -- NYI: decode PSR bits of msr. Decode imm12. + [16] = "movwDW", [20] = "movtDW", + [18] = { shift = 0, mask = 0xf00ff, [0] = "nopv6", _ = "msrNW", }, + [22] = "msrNW", + _ = map_data, +} + +local map_branch = { + shift = 24, mask = 1, + [0] = "bB", "blB" +} + +local map_condins = { + [0] = map_datar, map_datai, map_load, map_load1, + map_loadm, map_branch, map_loadc, map_datac +} + +-- NYI: setend. +local map_uncondins = { + [0] = false, map_simddata, map_simdload, map_preload, + false, "blxB", map_loadcu, map_datacu, +} + +------------------------------------------------------------------------------ + +local map_gpr = { + [0] = "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", + "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc", +} + +local map_cond = { + [0] = "eq", "ne", "hs", "lo", "mi", "pl", "vs", "vc", + "hi", "ls", "ge", "lt", "gt", "le", "al", +} + +local map_shift = { [0] = "lsl", "lsr", "asr", "ror", } + +------------------------------------------------------------------------------ + +-- Output a nicely formatted line with an opcode and operands. +local function putop(ctx, text, operands) + local pos = ctx.pos + local extra = "" + if ctx.rel then + local sym = ctx.symtab[ctx.rel] + if sym then + extra = "\t->"..sym + elseif band(ctx.op, 0x0e000000) ~= 0x0a000000 then + extra = "\t; 0x"..tohex(ctx.rel) + end + end + if ctx.hexdump > 0 then + ctx.out(format("%08x %s %-5s %s%s\n", + ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) + else + ctx.out(format("%08x %-5s %s%s\n", + ctx.addr+pos, text, concat(operands, ", "), extra)) + end + ctx.pos = pos + 4 +end + +-- Fallback for unknown opcodes. +local function unknown(ctx) + return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) +end + +-- Format operand 2 of load/store opcodes. +local function fmtload(ctx, op, pos) + local base = map_gpr[band(rshift(op, 16), 15)] + local x, ofs + local ext = (band(op, 0x04000000) == 0) + if not ext and band(op, 0x02000000) == 0 then + ofs = band(op, 4095) + if band(op, 0x00800000) == 0 then ofs = -ofs end + if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end + ofs = "#"..ofs + elseif ext and band(op, 0x00400000) ~= 0 then + ofs = band(op, 15) + band(rshift(op, 4), 0xf0) + if band(op, 0x00800000) == 0 then ofs = -ofs end + if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end + ofs = "#"..ofs + else + ofs = map_gpr[band(op, 15)] + if ext or band(op, 0xfe0) == 0 then + elseif band(op, 0xfe0) == 0x60 then + ofs = format("%s, rrx", ofs) + else + local sh = band(rshift(op, 7), 31) + if sh == 0 then sh = 32 end + ofs = format("%s, %s #%d", ofs, map_shift[band(rshift(op, 5), 3)], sh) + end + if band(op, 0x00800000) == 0 then ofs = "-"..ofs end + end + if ofs == "#0" then + x = format("[%s]", base) + elseif band(op, 0x01000000) == 0 then + x = format("[%s], %s", base, ofs) + else + x = format("[%s, %s]", base, ofs) + end + if band(op, 0x01200000) == 0x01200000 then x = x.."!" end + return x +end + +-- Format operand 2 of vector load/store opcodes. +local function fmtvload(ctx, op, pos) + local base = map_gpr[band(rshift(op, 16), 15)] + local ofs = band(op, 255)*4 + if band(op, 0x00800000) == 0 then ofs = -ofs end + if base == "pc" then ctx.rel = ctx.addr + pos + 8 + ofs end + if ofs == 0 then + return format("[%s]", base) + else + return format("[%s, #%d]", base, ofs) + end +end + +local function fmtvr(op, vr, sh0, sh1) + if vr == "s" then + return format("s%d", 2*band(rshift(op, sh0), 15)+band(rshift(op, sh1), 1)) + else + return format("d%d", band(rshift(op, sh0), 15)+band(rshift(op, sh1-4), 16)) + end +end + +-- Disassemble a single instruction. +local function disass_ins(ctx) + local pos = ctx.pos + local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) + local op = bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) + local operands = {} + local suffix = "" + local last, name, pat + local vr + ctx.op = op + ctx.rel = nil + + local cond = rshift(op, 28) + local opat + if cond == 15 then + opat = map_uncondins[band(rshift(op, 25), 7)] + else + if cond ~= 14 then suffix = map_cond[cond] end + opat = map_condins[band(rshift(op, 25), 7)] + end + while type(opat) ~= "string" do + if not opat then return unknown(ctx) end + opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ + end + name, pat = match(opat, "^([a-z0-9]*)(.*)") + if sub(pat, 1, 1) == "." then + local s2, p2 = match(pat, "^([a-z0-9.]*)(.*)") + suffix = suffix..s2 + pat = p2 + end + + for p in gmatch(pat, ".") do + local x = nil + if p == "D" then + x = map_gpr[band(rshift(op, 12), 15)] + elseif p == "N" then + x = map_gpr[band(rshift(op, 16), 15)] + elseif p == "S" then + x = map_gpr[band(rshift(op, 8), 15)] + elseif p == "M" then + x = map_gpr[band(op, 15)] + elseif p == "d" then + x = fmtvr(op, vr, 12, 22) + elseif p == "n" then + x = fmtvr(op, vr, 16, 7) + elseif p == "m" then + x = fmtvr(op, vr, 0, 5) + elseif p == "P" then + if band(op, 0x02000000) ~= 0 then + x = ror(band(op, 255), 2*band(rshift(op, 8), 15)) + else + x = map_gpr[band(op, 15)] + if band(op, 0xff0) ~= 0 then + operands[#operands+1] = x + local s = map_shift[band(rshift(op, 5), 3)] + local r = nil + if band(op, 0xf90) == 0 then + if s == "ror" then s = "rrx" else r = "#32" end + elseif band(op, 0x10) == 0 then + r = "#"..band(rshift(op, 7), 31) + else + r = map_gpr[band(rshift(op, 8), 15)] + end + if name == "mov" then name = s; x = r + elseif r then x = format("%s %s", s, r) + else x = s end + end + end + elseif p == "L" then + x = fmtload(ctx, op, pos) + elseif p == "l" then + x = fmtvload(ctx, op, pos) + elseif p == "B" then + local addr = ctx.addr + pos + 8 + arshift(lshift(op, 8), 6) + if cond == 15 then addr = addr + band(rshift(op, 23), 2) end + ctx.rel = addr + x = "0x"..tohex(addr) + elseif p == "F" then + vr = "s" + elseif p == "G" then + vr = "d" + elseif p == "." then + suffix = suffix..(vr == "s" and ".f32" or ".f64") + elseif p == "R" then + if band(op, 0x00200000) ~= 0 and #operands == 1 then + operands[1] = operands[1].."!" + end + local t = {} + for i=0,15 do + if band(rshift(op, i), 1) == 1 then t[#t+1] = map_gpr[i] end + end + x = "{"..concat(t, ", ").."}" + elseif p == "r" then + if band(op, 0x00200000) ~= 0 and #operands == 2 then + operands[1] = operands[1].."!" + end + local s = tonumber(sub(last, 2)) + local n = band(op, 255) + if vr == "d" then n = rshift(n, 1) end + operands[#operands] = format("{%s-%s%d}", last, vr, s+n-1) + elseif p == "W" then + x = band(op, 0x0fff) + band(rshift(op, 4), 0xf000) + elseif p == "T" then + x = "#0x"..tohex(band(op, 0x00ffffff), 6) + elseif p == "U" then + x = band(rshift(op, 7), 31) + if x == 0 then x = nil end + elseif p == "u" then + x = band(rshift(op, 7), 31) + if band(op, 0x40) == 0 then + if x == 0 then x = nil else x = "lsl #"..x end + else + if x == 0 then x = "asr #32" else x = "asr #"..x end + end + elseif p == "v" then + x = band(rshift(op, 7), 31) + elseif p == "w" then + x = band(rshift(op, 16), 31) + elseif p == "x" then + x = band(rshift(op, 16), 31) + 1 + elseif p == "X" then + x = band(rshift(op, 16), 31) - last + 1 + elseif p == "Y" then + x = band(rshift(op, 12), 0xf0) + band(op, 0x0f) + elseif p == "K" then + x = "#0x"..tohex(band(rshift(op, 4), 0x0000fff0) + band(op, 15), 4) + elseif p == "s" then + if band(op, 0x00100000) ~= 0 then suffix = "s"..suffix end + else + assert(false) + end + if x then + last = x + if type(x) == "number" then x = "#"..x end + operands[#operands+1] = x + end + end + + return putop(ctx, name..suffix, operands) +end + +------------------------------------------------------------------------------ + +-- Disassemble a block of code. +local function disass_block(ctx, ofs, len) + if not ofs then ofs = 0 end + local stop = len and ofs+len or #ctx.code + ctx.pos = ofs + ctx.rel = nil + while ctx.pos < stop do disass_ins(ctx) end +end + +-- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). +local function create_(code, addr, out) + local ctx = {} + ctx.code = code + ctx.addr = addr or 0 + ctx.out = out or io.write + ctx.symtab = {} + ctx.disass = disass_block + ctx.hexdump = 8 + return ctx +end + +-- Simple API: disassemble code (a string) at address and output via out. +local function disass_(code, addr, out) + create_(code, addr, out):disass() +end + +-- Return register name for RID. +local function regname_(r) + if r < 16 then return map_gpr[r] end + return "d"..(r-16) +end + +-- Public module functions. +module(...) + +create = create_ +disass = disass_ +regname = regname_ + diff --git a/external/lua/luajit/src/src/jit/dis_mips.lua b/external/lua/luajit/src/src/jit/dis_mips.lua new file mode 100644 index 0000000000..830db409b6 --- /dev/null +++ b/external/lua/luajit/src/src/jit/dis_mips.lua @@ -0,0 +1,428 @@ +---------------------------------------------------------------------------- +-- LuaJIT MIPS disassembler module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT/X license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- +-- This is a helper module used by the LuaJIT machine code dumper module. +-- +-- It disassembles all standard MIPS32R1/R2 instructions. +-- Default mode is big-endian, but see: dis_mipsel.lua +------------------------------------------------------------------------------ + +local type = type +local sub, byte, format = string.sub, string.byte, string.format +local match, gmatch, gsub = string.match, string.gmatch, string.gsub +local concat = table.concat +local bit = require("bit") +local band, bor, tohex = bit.band, bit.bor, bit.tohex +local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift + +------------------------------------------------------------------------------ +-- Primary and extended opcode maps +------------------------------------------------------------------------------ + +local map_movci = { shift = 16, mask = 1, [0] = "movfDSC", "movtDSC", } +local map_srl = { shift = 21, mask = 1, [0] = "srlDTA", "rotrDTA", } +local map_srlv = { shift = 6, mask = 1, [0] = "srlvDTS", "rotrvDTS", } + +local map_special = { + shift = 0, mask = 63, + [0] = { shift = 0, mask = -1, [0] = "nop", _ = "sllDTA" }, + map_movci, map_srl, "sraDTA", + "sllvDTS", false, map_srlv, "sravDTS", + "jrS", "jalrD1S", "movzDST", "movnDST", + "syscallY", "breakY", false, "sync", + "mfhiD", "mthiS", "mfloD", "mtloS", + false, false, false, false, + "multST", "multuST", "divST", "divuST", + false, false, false, false, + "addDST", "addu|moveDST0", "subDST", "subu|neguDS0T", + "andDST", "orDST", "xorDST", "nor|notDST0", + false, false, "sltDST", "sltuDST", + false, false, false, false, + "tgeSTZ", "tgeuSTZ", "tltSTZ", "tltuSTZ", + "teqSTZ", false, "tneSTZ", +} + +local map_special2 = { + shift = 0, mask = 63, + [0] = "maddST", "madduST", "mulDST", false, + "msubST", "msubuST", + [32] = "clzDS", [33] = "cloDS", + [63] = "sdbbpY", +} + +local map_bshfl = { + shift = 6, mask = 31, + [2] = "wsbhDT", + [16] = "sebDT", + [24] = "sehDT", +} + +local map_special3 = { + shift = 0, mask = 63, + [0] = "extTSAK", [4] = "insTSAL", + [32] = map_bshfl, + [59] = "rdhwrTD", +} + +local map_regimm = { + shift = 16, mask = 31, + [0] = "bltzSB", "bgezSB", "bltzlSB", "bgezlSB", + false, false, false, false, + "tgeiSI", "tgeiuSI", "tltiSI", "tltiuSI", + "teqiSI", false, "tneiSI", false, + "bltzalSB", "bgezalSB", "bltzallSB", "bgezallSB", + false, false, false, false, + false, false, false, false, + false, false, false, "synciSO", +} + +local map_cop0 = { + shift = 25, mask = 1, + [0] = { + shift = 21, mask = 15, + [0] = "mfc0TDW", [4] = "mtc0TDW", + [10] = "rdpgprDT", + [11] = { shift = 5, mask = 1, [0] = "diT0", "eiT0", }, + [14] = "wrpgprDT", + }, { + shift = 0, mask = 63, + [1] = "tlbr", [2] = "tlbwi", [6] = "tlbwr", [8] = "tlbp", + [24] = "eret", [31] = "deret", + [32] = "wait", + }, +} + +local map_cop1s = { + shift = 0, mask = 63, + [0] = "add.sFGH", "sub.sFGH", "mul.sFGH", "div.sFGH", + "sqrt.sFG", "abs.sFG", "mov.sFG", "neg.sFG", + "round.l.sFG", "trunc.l.sFG", "ceil.l.sFG", "floor.l.sFG", + "round.w.sFG", "trunc.w.sFG", "ceil.w.sFG", "floor.w.sFG", + false, + { shift = 16, mask = 1, [0] = "movf.sFGC", "movt.sFGC" }, + "movz.sFGT", "movn.sFGT", + false, "recip.sFG", "rsqrt.sFG", false, + false, false, false, false, + false, false, false, false, + false, "cvt.d.sFG", false, false, + "cvt.w.sFG", "cvt.l.sFG", "cvt.ps.sFGH", false, + false, false, false, false, + false, false, false, false, + "c.f.sVGH", "c.un.sVGH", "c.eq.sVGH", "c.ueq.sVGH", + "c.olt.sVGH", "c.ult.sVGH", "c.ole.sVGH", "c.ule.sVGH", + "c.sf.sVGH", "c.ngle.sVGH", "c.seq.sVGH", "c.ngl.sVGH", + "c.lt.sVGH", "c.nge.sVGH", "c.le.sVGH", "c.ngt.sVGH", +} + +local map_cop1d = { + shift = 0, mask = 63, + [0] = "add.dFGH", "sub.dFGH", "mul.dFGH", "div.dFGH", + "sqrt.dFG", "abs.dFG", "mov.dFG", "neg.dFG", + "round.l.dFG", "trunc.l.dFG", "ceil.l.dFG", "floor.l.dFG", + "round.w.dFG", "trunc.w.dFG", "ceil.w.dFG", "floor.w.dFG", + false, + { shift = 16, mask = 1, [0] = "movf.dFGC", "movt.dFGC" }, + "movz.dFGT", "movn.dFGT", + false, "recip.dFG", "rsqrt.dFG", false, + false, false, false, false, + false, false, false, false, + "cvt.s.dFG", false, false, false, + "cvt.w.dFG", "cvt.l.dFG", false, false, + false, false, false, false, + false, false, false, false, + "c.f.dVGH", "c.un.dVGH", "c.eq.dVGH", "c.ueq.dVGH", + "c.olt.dVGH", "c.ult.dVGH", "c.ole.dVGH", "c.ule.dVGH", + "c.df.dVGH", "c.ngle.dVGH", "c.deq.dVGH", "c.ngl.dVGH", + "c.lt.dVGH", "c.nge.dVGH", "c.le.dVGH", "c.ngt.dVGH", +} + +local map_cop1ps = { + shift = 0, mask = 63, + [0] = "add.psFGH", "sub.psFGH", "mul.psFGH", false, + false, "abs.psFG", "mov.psFG", "neg.psFG", + false, false, false, false, + false, false, false, false, + false, + { shift = 16, mask = 1, [0] = "movf.psFGC", "movt.psFGC" }, + "movz.psFGT", "movn.psFGT", + false, false, false, false, + false, false, false, false, + false, false, false, false, + "cvt.s.puFG", false, false, false, + false, false, false, false, + "cvt.s.plFG", false, false, false, + "pll.psFGH", "plu.psFGH", "pul.psFGH", "puu.psFGH", + "c.f.psVGH", "c.un.psVGH", "c.eq.psVGH", "c.ueq.psVGH", + "c.olt.psVGH", "c.ult.psVGH", "c.ole.psVGH", "c.ule.psVGH", + "c.psf.psVGH", "c.ngle.psVGH", "c.pseq.psVGH", "c.ngl.psVGH", + "c.lt.psVGH", "c.nge.psVGH", "c.le.psVGH", "c.ngt.psVGH", +} + +local map_cop1w = { + shift = 0, mask = 63, + [32] = "cvt.s.wFG", [33] = "cvt.d.wFG", +} + +local map_cop1l = { + shift = 0, mask = 63, + [32] = "cvt.s.lFG", [33] = "cvt.d.lFG", +} + +local map_cop1bc = { + shift = 16, mask = 3, + [0] = "bc1fCB", "bc1tCB", "bc1flCB", "bc1tlCB", +} + +local map_cop1 = { + shift = 21, mask = 31, + [0] = "mfc1TG", false, "cfc1TG", "mfhc1TG", + "mtc1TG", false, "ctc1TG", "mthc1TG", + map_cop1bc, false, false, false, + false, false, false, false, + map_cop1s, map_cop1d, false, false, + map_cop1w, map_cop1l, map_cop1ps, +} + +local map_cop1x = { + shift = 0, mask = 63, + [0] = "lwxc1FSX", "ldxc1FSX", false, false, + false, "luxc1FSX", false, false, + "swxc1FSX", "sdxc1FSX", false, false, + false, "suxc1FSX", false, "prefxMSX", + false, false, false, false, + false, false, false, false, + false, false, false, false, + false, false, "alnv.psFGHS", false, + "madd.sFRGH", "madd.dFRGH", false, false, + false, false, "madd.psFRGH", false, + "msub.sFRGH", "msub.dFRGH", false, false, + false, false, "msub.psFRGH", false, + "nmadd.sFRGH", "nmadd.dFRGH", false, false, + false, false, "nmadd.psFRGH", false, + "nmsub.sFRGH", "nmsub.dFRGH", false, false, + false, false, "nmsub.psFRGH", false, +} + +local map_pri = { + [0] = map_special, map_regimm, "jJ", "jalJ", + "beq|beqz|bST00B", "bne|bnezST0B", "blezSB", "bgtzSB", + "addiTSI", "addiu|liTS0I", "sltiTSI", "sltiuTSI", + "andiTSU", "ori|liTS0U", "xoriTSU", "luiTU", + map_cop0, map_cop1, false, map_cop1x, + "beql|beqzlST0B", "bnel|bnezlST0B", "blezlSB", "bgtzlSB", + false, false, false, false, + map_special2, false, false, map_special3, + "lbTSO", "lhTSO", "lwlTSO", "lwTSO", + "lbuTSO", "lhuTSO", "lwrTSO", false, + "sbTSO", "shTSO", "swlTSO", "swTSO", + false, false, "swrTSO", "cacheNSO", + "llTSO", "lwc1HSO", "lwc2TSO", "prefNSO", + false, "ldc1HSO", "ldc2TSO", false, + "scTSO", "swc1HSO", "swc2TSO", false, + false, "sdc1HSO", "sdc2TSO", false, +} + +------------------------------------------------------------------------------ + +local map_gpr = { + [0] = "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", + "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", + "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", + "r24", "r25", "r26", "r27", "r28", "sp", "r30", "ra", +} + +------------------------------------------------------------------------------ + +-- Output a nicely formatted line with an opcode and operands. +local function putop(ctx, text, operands) + local pos = ctx.pos + local extra = "" + if ctx.rel then + local sym = ctx.symtab[ctx.rel] + if sym then extra = "\t->"..sym end + end + if ctx.hexdump > 0 then + ctx.out(format("%08x %s %-7s %s%s\n", + ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) + else + ctx.out(format("%08x %-7s %s%s\n", + ctx.addr+pos, text, concat(operands, ", "), extra)) + end + ctx.pos = pos + 4 +end + +-- Fallback for unknown opcodes. +local function unknown(ctx) + return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) +end + +local function get_be(ctx) + local pos = ctx.pos + local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) + return bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) +end + +local function get_le(ctx) + local pos = ctx.pos + local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) + return bor(lshift(b3, 24), lshift(b2, 16), lshift(b1, 8), b0) +end + +-- Disassemble a single instruction. +local function disass_ins(ctx) + local op = ctx:get() + local operands = {} + local last = nil + ctx.op = op + ctx.rel = nil + + local opat = map_pri[rshift(op, 26)] + while type(opat) ~= "string" do + if not opat then return unknown(ctx) end + opat = opat[band(rshift(op, opat.shift), opat.mask)] or opat._ + end + local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") + local altname, pat2 = match(pat, "|([a-z0-9_.|]*)(.*)") + if altname then pat = pat2 end + + for p in gmatch(pat, ".") do + local x = nil + if p == "S" then + x = map_gpr[band(rshift(op, 21), 31)] + elseif p == "T" then + x = map_gpr[band(rshift(op, 16), 31)] + elseif p == "D" then + x = map_gpr[band(rshift(op, 11), 31)] + elseif p == "F" then + x = "f"..band(rshift(op, 6), 31) + elseif p == "G" then + x = "f"..band(rshift(op, 11), 31) + elseif p == "H" then + x = "f"..band(rshift(op, 16), 31) + elseif p == "R" then + x = "f"..band(rshift(op, 21), 31) + elseif p == "A" then + x = band(rshift(op, 6), 31) + elseif p == "M" then + x = band(rshift(op, 11), 31) + elseif p == "N" then + x = band(rshift(op, 16), 31) + elseif p == "C" then + x = band(rshift(op, 18), 7) + if x == 0 then x = nil end + elseif p == "K" then + x = band(rshift(op, 11), 31) + 1 + elseif p == "L" then + x = band(rshift(op, 11), 31) - last + 1 + elseif p == "I" then + x = arshift(lshift(op, 16), 16) + elseif p == "U" then + x = band(op, 0xffff) + elseif p == "O" then + local disp = arshift(lshift(op, 16), 16) + operands[#operands] = format("%d(%s)", disp, last) + elseif p == "X" then + local index = map_gpr[band(rshift(op, 16), 31)] + operands[#operands] = format("%s(%s)", index, last) + elseif p == "B" then + x = ctx.addr + ctx.pos + arshift(lshift(op, 16), 16)*4 + 4 + ctx.rel = x + x = "0x"..tohex(x) + elseif p == "J" then + x = band(ctx.addr + ctx.pos, 0xf0000000) + band(op, 0x03ffffff)*4 + ctx.rel = x + x = "0x"..tohex(x) + elseif p == "V" then + x = band(rshift(op, 8), 7) + if x == 0 then x = nil end + elseif p == "W" then + x = band(op, 7) + if x == 0 then x = nil end + elseif p == "Y" then + x = band(rshift(op, 6), 0x000fffff) + if x == 0 then x = nil end + elseif p == "Z" then + x = band(rshift(op, 6), 1023) + if x == 0 then x = nil end + elseif p == "0" then + if last == "r0" or last == 0 then + local n = #operands + operands[n] = nil + last = operands[n-1] + if altname then + local a1, a2 = match(altname, "([^|]*)|(.*)") + if a1 then name, altname = a1, a2 + else name = altname end + end + end + elseif p == "1" then + if last == "ra" then + operands[#operands] = nil + end + else + assert(false) + end + if x then operands[#operands+1] = x; last = x end + end + + return putop(ctx, name, operands) +end + +------------------------------------------------------------------------------ + +-- Disassemble a block of code. +local function disass_block(ctx, ofs, len) + if not ofs then ofs = 0 end + local stop = len and ofs+len or #ctx.code + stop = stop - stop % 4 + ctx.pos = ofs - ofs % 4 + ctx.rel = nil + while ctx.pos < stop do disass_ins(ctx) end +end + +-- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). +local function create_(code, addr, out) + local ctx = {} + ctx.code = code + ctx.addr = addr or 0 + ctx.out = out or io.write + ctx.symtab = {} + ctx.disass = disass_block + ctx.hexdump = 8 + ctx.get = get_be + return ctx +end + +local function create_el_(code, addr, out) + local ctx = create_(code, addr, out) + ctx.get = get_le + return ctx +end + +-- Simple API: disassemble code (a string) at address and output via out. +local function disass_(code, addr, out) + create_(code, addr, out):disass() +end + +local function disass_el_(code, addr, out) + create_el_(code, addr, out):disass() +end + +-- Return register name for RID. +local function regname_(r) + if r < 32 then return map_gpr[r] end + return "f"..(r-32) +end + +-- Public module functions. +module(...) + +create = create_ +create_el = create_el_ +disass = disass_ +disass_el = disass_el_ +regname = regname_ + diff --git a/external/lua/luajit/src/src/jit/dis_mipsel.lua b/external/lua/luajit/src/src/jit/dis_mipsel.lua new file mode 100644 index 0000000000..8a10c46235 --- /dev/null +++ b/external/lua/luajit/src/src/jit/dis_mipsel.lua @@ -0,0 +1,20 @@ +---------------------------------------------------------------------------- +-- LuaJIT MIPSEL disassembler wrapper module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- +-- This module just exports the little-endian functions from the +-- MIPS disassembler module. All the interesting stuff is there. +------------------------------------------------------------------------------ + +local require = require + +module(...) + +local dis_mips = require(_PACKAGE.."dis_mips") + +create = dis_mips.create_el +disass = dis_mips.disass_el +regname = dis_mips.regname + diff --git a/external/lua/luajit/src/src/jit/dis_ppc.lua b/external/lua/luajit/src/src/jit/dis_ppc.lua new file mode 100644 index 0000000000..169a534c10 --- /dev/null +++ b/external/lua/luajit/src/src/jit/dis_ppc.lua @@ -0,0 +1,591 @@ +---------------------------------------------------------------------------- +-- LuaJIT PPC disassembler module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT/X license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- +-- This is a helper module used by the LuaJIT machine code dumper module. +-- +-- It disassembles all common, non-privileged 32/64 bit PowerPC instructions +-- plus the e500 SPE instructions and some Cell/Xenon extensions. +-- +-- NYI: VMX, VMX128 +------------------------------------------------------------------------------ + +local type = type +local sub, byte, format = string.sub, string.byte, string.format +local match, gmatch, gsub = string.match, string.gmatch, string.gsub +local concat = table.concat +local bit = require("bit") +local band, bor, tohex = bit.band, bit.bor, bit.tohex +local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift + +------------------------------------------------------------------------------ +-- Primary and extended opcode maps +------------------------------------------------------------------------------ + +local map_crops = { + shift = 1, mask = 1023, + [0] = "mcrfXX", + [33] = "crnor|crnotCCC=", [129] = "crandcCCC", + [193] = "crxor|crclrCCC%", [225] = "crnandCCC", + [257] = "crandCCC", [289] = "creqv|crsetCCC%", + [417] = "crorcCCC", [449] = "cror|crmoveCCC=", + [16] = "b_lrKB", [528] = "b_ctrKB", + [150] = "isync", +} + +local map_rlwinm = setmetatable({ + shift = 0, mask = -1, +}, +{ __index = function(t, x) + local rot = band(rshift(x, 11), 31) + local mb = band(rshift(x, 6), 31) + local me = band(rshift(x, 1), 31) + if mb == 0 and me == 31-rot then + return "slwiRR~A." + elseif me == 31 and mb == 32-rot then + return "srwiRR~-A." + else + return "rlwinmRR~AAA." + end + end +}) + +local map_rld = { + shift = 2, mask = 7, + [0] = "rldiclRR~HM.", "rldicrRR~HM.", "rldicRR~HM.", "rldimiRR~HM.", + { + shift = 1, mask = 1, + [0] = "rldclRR~RM.", "rldcrRR~RM.", + }, +} + +local map_ext = setmetatable({ + shift = 1, mask = 1023, + + [0] = "cmp_YLRR", [32] = "cmpl_YLRR", + [4] = "twARR", [68] = "tdARR", + + [8] = "subfcRRR.", [40] = "subfRRR.", + [104] = "negRR.", [136] = "subfeRRR.", + [200] = "subfzeRR.", [232] = "subfmeRR.", + [520] = "subfcoRRR.", [552] = "subfoRRR.", + [616] = "negoRR.", [648] = "subfeoRRR.", + [712] = "subfzeoRR.", [744] = "subfmeoRR.", + + [9] = "mulhduRRR.", [73] = "mulhdRRR.", [233] = "mulldRRR.", + [457] = "divduRRR.", [489] = "divdRRR.", + [745] = "mulldoRRR.", + [969] = "divduoRRR.", [1001] = "divdoRRR.", + + [10] = "addcRRR.", [138] = "addeRRR.", + [202] = "addzeRR.", [234] = "addmeRR.", [266] = "addRRR.", + [522] = "addcoRRR.", [650] = "addeoRRR.", + [714] = "addzeoRR.", [746] = "addmeoRR.", [778] = "addoRRR.", + + [11] = "mulhwuRRR.", [75] = "mulhwRRR.", [235] = "mullwRRR.", + [459] = "divwuRRR.", [491] = "divwRRR.", + [747] = "mullwoRRR.", + [971] = "divwouRRR.", [1003] = "divwoRRR.", + + [15] = "iselltRRR", [47] = "iselgtRRR", [79] = "iseleqRRR", + + [144] = { shift = 20, mask = 1, [0] = "mtcrfRZ~", "mtocrfRZ~", }, + [19] = { shift = 20, mask = 1, [0] = "mfcrR", "mfocrfRZ", }, + [371] = { shift = 11, mask = 1023, [392] = "mftbR", [424] = "mftbuR", }, + [339] = { + shift = 11, mask = 1023, + [32] = "mferR", [256] = "mflrR", [288] = "mfctrR", [16] = "mfspefscrR", + }, + [467] = { + shift = 11, mask = 1023, + [32] = "mtxerR", [256] = "mtlrR", [288] = "mtctrR", [16] = "mtspefscrR", + }, + + [20] = "lwarxRR0R", [84] = "ldarxRR0R", + + [21] = "ldxRR0R", [53] = "lduxRRR", + [149] = "stdxRR0R", [181] = "stduxRRR", + [341] = "lwaxRR0R", [373] = "lwauxRRR", + + [23] = "lwzxRR0R", [55] = "lwzuxRRR", + [87] = "lbzxRR0R", [119] = "lbzuxRRR", + [151] = "stwxRR0R", [183] = "stwuxRRR", + [215] = "stbxRR0R", [247] = "stbuxRRR", + [279] = "lhzxRR0R", [311] = "lhzuxRRR", + [343] = "lhaxRR0R", [375] = "lhauxRRR", + [407] = "sthxRR0R", [439] = "sthuxRRR", + + [54] = "dcbst-R0R", [86] = "dcbf-R0R", + [150] = "stwcxRR0R.", [214] = "stdcxRR0R.", + [246] = "dcbtst-R0R", [278] = "dcbt-R0R", + [310] = "eciwxRR0R", [438] = "ecowxRR0R", + [470] = "dcbi-RR", + + [598] = { + shift = 21, mask = 3, + [0] = "sync", "lwsync", "ptesync", + }, + [758] = "dcba-RR", + [854] = "eieio", [982] = "icbi-R0R", [1014] = "dcbz-R0R", + + [26] = "cntlzwRR~", [58] = "cntlzdRR~", + [122] = "popcntbRR~", + [154] = "prtywRR~", [186] = "prtydRR~", + + [28] = "andRR~R.", [60] = "andcRR~R.", [124] = "nor|notRR~R=.", + [284] = "eqvRR~R.", [316] = "xorRR~R.", + [412] = "orcRR~R.", [444] = "or|mrRR~R=.", [476] = "nandRR~R.", + [508] = "cmpbRR~R", + + [512] = "mcrxrX", + + [532] = "ldbrxRR0R", [660] = "stdbrxRR0R", + + [533] = "lswxRR0R", [597] = "lswiRR0A", + [661] = "stswxRR0R", [725] = "stswiRR0A", + + [534] = "lwbrxRR0R", [662] = "stwbrxRR0R", + [790] = "lhbrxRR0R", [918] = "sthbrxRR0R", + + [535] = "lfsxFR0R", [567] = "lfsuxFRR", + [599] = "lfdxFR0R", [631] = "lfduxFRR", + [663] = "stfsxFR0R", [695] = "stfsuxFRR", + [727] = "stfdxFR0R", [759] = "stfduxFR0R", + [855] = "lfiwaxFR0R", + [983] = "stfiwxFR0R", + + [24] = "slwRR~R.", + + [27] = "sldRR~R.", [536] = "srwRR~R.", + [792] = "srawRR~R.", [824] = "srawiRR~A.", + + [794] = "sradRR~R.", [826] = "sradiRR~H.", [827] = "sradiRR~H.", + [922] = "extshRR~.", [954] = "extsbRR~.", [986] = "extswRR~.", + + [539] = "srdRR~R.", +}, +{ __index = function(t, x) + if band(x, 31) == 15 then return "iselRRRC" end + end +}) + +local map_ld = { + shift = 0, mask = 3, + [0] = "ldRRE", "lduRRE", "lwaRRE", +} + +local map_std = { + shift = 0, mask = 3, + [0] = "stdRRE", "stduRRE", +} + +local map_fps = { + shift = 5, mask = 1, + { + shift = 1, mask = 15, + [0] = false, false, "fdivsFFF.", false, + "fsubsFFF.", "faddsFFF.", "fsqrtsF-F.", false, + "fresF-F.", "fmulsFF-F.", "frsqrtesF-F.", false, + "fmsubsFFFF~.", "fmaddsFFFF~.", "fnmsubsFFFF~.", "fnmaddsFFFF~.", + } +} + +local map_fpd = { + shift = 5, mask = 1, + [0] = { + shift = 1, mask = 1023, + [0] = "fcmpuXFF", [32] = "fcmpoXFF", [64] = "mcrfsXX", + [38] = "mtfsb1A.", [70] = "mtfsb0A.", [134] = "mtfsfiA>>-A>", + [8] = "fcpsgnFFF.", [40] = "fnegF-F.", [72] = "fmrF-F.", + [136] = "fnabsF-F.", [264] = "fabsF-F.", + [12] = "frspF-F.", + [14] = "fctiwF-F.", [15] = "fctiwzF-F.", + [583] = "mffsF.", [711] = "mtfsfZF.", + [392] = "frinF-F.", [424] = "frizF-F.", + [456] = "fripF-F.", [488] = "frimF-F.", + [814] = "fctidF-F.", [815] = "fctidzF-F.", [846] = "fcfidF-F.", + }, + { + shift = 1, mask = 15, + [0] = false, false, "fdivFFF.", false, + "fsubFFF.", "faddFFF.", "fsqrtF-F.", "fselFFFF~.", + "freF-F.", "fmulFF-F.", "frsqrteF-F.", false, + "fmsubFFFF~.", "fmaddFFFF~.", "fnmsubFFFF~.", "fnmaddFFFF~.", + } +} + +local map_spe = { + shift = 0, mask = 2047, + + [512] = "evaddwRRR", [514] = "evaddiwRAR~", + [516] = "evsubwRRR~", [518] = "evsubiwRAR~", + [520] = "evabsRR", [521] = "evnegRR", + [522] = "evextsbRR", [523] = "evextshRR", [524] = "evrndwRR", + [525] = "evcntlzwRR", [526] = "evcntlswRR", + + [527] = "brincRRR", + + [529] = "evandRRR", [530] = "evandcRRR", [534] = "evxorRRR", + [535] = "evor|evmrRRR=", [536] = "evnor|evnotRRR=", + [537] = "eveqvRRR", [539] = "evorcRRR", [542] = "evnandRRR", + + [544] = "evsrwuRRR", [545] = "evsrwsRRR", + [546] = "evsrwiuRRA", [547] = "evsrwisRRA", + [548] = "evslwRRR", [550] = "evslwiRRA", + [552] = "evrlwRRR", [553] = "evsplatiRS", + [554] = "evrlwiRRA", [555] = "evsplatfiRS", + [556] = "evmergehiRRR", [557] = "evmergeloRRR", + [558] = "evmergehiloRRR", [559] = "evmergelohiRRR", + + [560] = "evcmpgtuYRR", [561] = "evcmpgtsYRR", + [562] = "evcmpltuYRR", [563] = "evcmpltsYRR", + [564] = "evcmpeqYRR", + + [632] = "evselRRR", [633] = "evselRRRW", + [634] = "evselRRRW", [635] = "evselRRRW", + [636] = "evselRRRW", [637] = "evselRRRW", + [638] = "evselRRRW", [639] = "evselRRRW", + + [640] = "evfsaddRRR", [641] = "evfssubRRR", + [644] = "evfsabsRR", [645] = "evfsnabsRR", [646] = "evfsnegRR", + [648] = "evfsmulRRR", [649] = "evfsdivRRR", + [652] = "evfscmpgtYRR", [653] = "evfscmpltYRR", [654] = "evfscmpeqYRR", + [656] = "evfscfuiR-R", [657] = "evfscfsiR-R", + [658] = "evfscfufR-R", [659] = "evfscfsfR-R", + [660] = "evfsctuiR-R", [661] = "evfsctsiR-R", + [662] = "evfsctufR-R", [663] = "evfsctsfR-R", + [664] = "evfsctuizR-R", [666] = "evfsctsizR-R", + [668] = "evfststgtYRR", [669] = "evfststltYRR", [670] = "evfststeqYRR", + + [704] = "efsaddRRR", [705] = "efssubRRR", + [708] = "efsabsRR", [709] = "efsnabsRR", [710] = "efsnegRR", + [712] = "efsmulRRR", [713] = "efsdivRRR", + [716] = "efscmpgtYRR", [717] = "efscmpltYRR", [718] = "efscmpeqYRR", + [719] = "efscfdR-R", + [720] = "efscfuiR-R", [721] = "efscfsiR-R", + [722] = "efscfufR-R", [723] = "efscfsfR-R", + [724] = "efsctuiR-R", [725] = "efsctsiR-R", + [726] = "efsctufR-R", [727] = "efsctsfR-R", + [728] = "efsctuizR-R", [730] = "efsctsizR-R", + [732] = "efststgtYRR", [733] = "efststltYRR", [734] = "efststeqYRR", + + [736] = "efdaddRRR", [737] = "efdsubRRR", + [738] = "efdcfuidR-R", [739] = "efdcfsidR-R", + [740] = "efdabsRR", [741] = "efdnabsRR", [742] = "efdnegRR", + [744] = "efdmulRRR", [745] = "efddivRRR", + [746] = "efdctuidzR-R", [747] = "efdctsidzR-R", + [748] = "efdcmpgtYRR", [749] = "efdcmpltYRR", [750] = "efdcmpeqYRR", + [751] = "efdcfsR-R", + [752] = "efdcfuiR-R", [753] = "efdcfsiR-R", + [754] = "efdcfufR-R", [755] = "efdcfsfR-R", + [756] = "efdctuiR-R", [757] = "efdctsiR-R", + [758] = "efdctufR-R", [759] = "efdctsfR-R", + [760] = "efdctuizR-R", [762] = "efdctsizR-R", + [764] = "efdtstgtYRR", [765] = "efdtstltYRR", [766] = "efdtsteqYRR", + + [768] = "evlddxRR0R", [769] = "evlddRR8", + [770] = "evldwxRR0R", [771] = "evldwRR8", + [772] = "evldhxRR0R", [773] = "evldhRR8", + [776] = "evlhhesplatxRR0R", [777] = "evlhhesplatRR2", + [780] = "evlhhousplatxRR0R", [781] = "evlhhousplatRR2", + [782] = "evlhhossplatxRR0R", [783] = "evlhhossplatRR2", + [784] = "evlwhexRR0R", [785] = "evlwheRR4", + [788] = "evlwhouxRR0R", [789] = "evlwhouRR4", + [790] = "evlwhosxRR0R", [791] = "evlwhosRR4", + [792] = "evlwwsplatxRR0R", [793] = "evlwwsplatRR4", + [796] = "evlwhsplatxRR0R", [797] = "evlwhsplatRR4", + + [800] = "evstddxRR0R", [801] = "evstddRR8", + [802] = "evstdwxRR0R", [803] = "evstdwRR8", + [804] = "evstdhxRR0R", [805] = "evstdhRR8", + [816] = "evstwhexRR0R", [817] = "evstwheRR4", + [820] = "evstwhoxRR0R", [821] = "evstwhoRR4", + [824] = "evstwwexRR0R", [825] = "evstwweRR4", + [828] = "evstwwoxRR0R", [829] = "evstwwoRR4", + + [1027] = "evmhessfRRR", [1031] = "evmhossfRRR", [1032] = "evmheumiRRR", + [1033] = "evmhesmiRRR", [1035] = "evmhesmfRRR", [1036] = "evmhoumiRRR", + [1037] = "evmhosmiRRR", [1039] = "evmhosmfRRR", [1059] = "evmhessfaRRR", + [1063] = "evmhossfaRRR", [1064] = "evmheumiaRRR", [1065] = "evmhesmiaRRR", + [1067] = "evmhesmfaRRR", [1068] = "evmhoumiaRRR", [1069] = "evmhosmiaRRR", + [1071] = "evmhosmfaRRR", [1095] = "evmwhssfRRR", [1096] = "evmwlumiRRR", + [1100] = "evmwhumiRRR", [1101] = "evmwhsmiRRR", [1103] = "evmwhsmfRRR", + [1107] = "evmwssfRRR", [1112] = "evmwumiRRR", [1113] = "evmwsmiRRR", + [1115] = "evmwsmfRRR", [1127] = "evmwhssfaRRR", [1128] = "evmwlumiaRRR", + [1132] = "evmwhumiaRRR", [1133] = "evmwhsmiaRRR", [1135] = "evmwhsmfaRRR", + [1139] = "evmwssfaRRR", [1144] = "evmwumiaRRR", [1145] = "evmwsmiaRRR", + [1147] = "evmwsmfaRRR", + + [1216] = "evaddusiaawRR", [1217] = "evaddssiaawRR", + [1218] = "evsubfusiaawRR", [1219] = "evsubfssiaawRR", + [1220] = "evmraRR", + [1222] = "evdivwsRRR", [1223] = "evdivwuRRR", + [1224] = "evaddumiaawRR", [1225] = "evaddsmiaawRR", + [1226] = "evsubfumiaawRR", [1227] = "evsubfsmiaawRR", + + [1280] = "evmheusiaawRRR", [1281] = "evmhessiaawRRR", + [1283] = "evmhessfaawRRR", [1284] = "evmhousiaawRRR", + [1285] = "evmhossiaawRRR", [1287] = "evmhossfaawRRR", + [1288] = "evmheumiaawRRR", [1289] = "evmhesmiaawRRR", + [1291] = "evmhesmfaawRRR", [1292] = "evmhoumiaawRRR", + [1293] = "evmhosmiaawRRR", [1295] = "evmhosmfaawRRR", + [1320] = "evmhegumiaaRRR", [1321] = "evmhegsmiaaRRR", + [1323] = "evmhegsmfaaRRR", [1324] = "evmhogumiaaRRR", + [1325] = "evmhogsmiaaRRR", [1327] = "evmhogsmfaaRRR", + [1344] = "evmwlusiaawRRR", [1345] = "evmwlssiaawRRR", + [1352] = "evmwlumiaawRRR", [1353] = "evmwlsmiaawRRR", + [1363] = "evmwssfaaRRR", [1368] = "evmwumiaaRRR", + [1369] = "evmwsmiaaRRR", [1371] = "evmwsmfaaRRR", + [1408] = "evmheusianwRRR", [1409] = "evmhessianwRRR", + [1411] = "evmhessfanwRRR", [1412] = "evmhousianwRRR", + [1413] = "evmhossianwRRR", [1415] = "evmhossfanwRRR", + [1416] = "evmheumianwRRR", [1417] = "evmhesmianwRRR", + [1419] = "evmhesmfanwRRR", [1420] = "evmhoumianwRRR", + [1421] = "evmhosmianwRRR", [1423] = "evmhosmfanwRRR", + [1448] = "evmhegumianRRR", [1449] = "evmhegsmianRRR", + [1451] = "evmhegsmfanRRR", [1452] = "evmhogumianRRR", + [1453] = "evmhogsmianRRR", [1455] = "evmhogsmfanRRR", + [1472] = "evmwlusianwRRR", [1473] = "evmwlssianwRRR", + [1480] = "evmwlumianwRRR", [1481] = "evmwlsmianwRRR", + [1491] = "evmwssfanRRR", [1496] = "evmwumianRRR", + [1497] = "evmwsmianRRR", [1499] = "evmwsmfanRRR", +} + +local map_pri = { + [0] = false, false, "tdiARI", "twiARI", + map_spe, false, false, "mulliRRI", + "subficRRI", false, "cmpl_iYLRU", "cmp_iYLRI", + "addicRRI", "addic.RRI", "addi|liRR0I", "addis|lisRR0I", + "b_KBJ", "sc", "bKJ", map_crops, + "rlwimiRR~AAA.", map_rlwinm, false, "rlwnmRR~RAA.", + "oriNRR~U", "orisRR~U", "xoriRR~U", "xorisRR~U", + "andi.RR~U", "andis.RR~U", map_rld, map_ext, + "lwzRRD", "lwzuRRD", "lbzRRD", "lbzuRRD", + "stwRRD", "stwuRRD", "stbRRD", "stbuRRD", + "lhzRRD", "lhzuRRD", "lhaRRD", "lhauRRD", + "sthRRD", "sthuRRD", "lmwRRD", "stmwRRD", + "lfsFRD", "lfsuFRD", "lfdFRD", "lfduFRD", + "stfsFRD", "stfsuFRD", "stfdFRD", "stfduFRD", + false, false, map_ld, map_fps, + false, false, map_std, map_fpd, +} + +------------------------------------------------------------------------------ + +local map_gpr = { + [0] = "r0", "sp", "r2", "r3", "r4", "r5", "r6", "r7", + "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", + "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", + "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31", +} + +local map_cond = { [0] = "lt", "gt", "eq", "so", "ge", "le", "ne", "ns", } + +-- Format a condition bit. +local function condfmt(cond) + if cond <= 3 then + return map_cond[band(cond, 3)] + else + return format("4*cr%d+%s", rshift(cond, 2), map_cond[band(cond, 3)]) + end +end + +------------------------------------------------------------------------------ + +-- Output a nicely formatted line with an opcode and operands. +local function putop(ctx, text, operands) + local pos = ctx.pos + local extra = "" + if ctx.rel then + local sym = ctx.symtab[ctx.rel] + if sym then extra = "\t->"..sym end + end + if ctx.hexdump > 0 then + ctx.out(format("%08x %s %-7s %s%s\n", + ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra)) + else + ctx.out(format("%08x %-7s %s%s\n", + ctx.addr+pos, text, concat(operands, ", "), extra)) + end + ctx.pos = pos + 4 +end + +-- Fallback for unknown opcodes. +local function unknown(ctx) + return putop(ctx, ".long", { "0x"..tohex(ctx.op) }) +end + +-- Disassemble a single instruction. +local function disass_ins(ctx) + local pos = ctx.pos + local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4) + local op = bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3) + local operands = {} + local last = nil + local rs = 21 + ctx.op = op + ctx.rel = nil + + local opat = map_pri[rshift(b0, 2)] + while type(opat) ~= "string" do + if not opat then return unknown(ctx) end + opat = opat[band(rshift(op, opat.shift), opat.mask)] + end + local name, pat = match(opat, "^([a-z0-9_.]*)(.*)") + local altname, pat2 = match(pat, "|([a-z0-9_.]*)(.*)") + if altname then pat = pat2 end + + for p in gmatch(pat, ".") do + local x = nil + if p == "R" then + x = map_gpr[band(rshift(op, rs), 31)] + rs = rs - 5 + elseif p == "F" then + x = "f"..band(rshift(op, rs), 31) + rs = rs - 5 + elseif p == "A" then + x = band(rshift(op, rs), 31) + rs = rs - 5 + elseif p == "S" then + x = arshift(lshift(op, 27-rs), 27) + rs = rs - 5 + elseif p == "I" then + x = arshift(lshift(op, 16), 16) + elseif p == "U" then + x = band(op, 0xffff) + elseif p == "D" or p == "E" then + local disp = arshift(lshift(op, 16), 16) + if p == "E" then disp = band(disp, -4) end + if last == "r0" then last = "0" end + operands[#operands] = format("%d(%s)", disp, last) + elseif p >= "2" and p <= "8" then + local disp = band(rshift(op, rs), 31) * p + if last == "r0" then last = "0" end + operands[#operands] = format("%d(%s)", disp, last) + elseif p == "H" then + x = band(rshift(op, rs), 31) + lshift(band(op, 2), 4) + rs = rs - 5 + elseif p == "M" then + x = band(rshift(op, rs), 31) + band(op, 0x20) + elseif p == "C" then + x = condfmt(band(rshift(op, rs), 31)) + rs = rs - 5 + elseif p == "B" then + local bo = rshift(op, 21) + local cond = band(rshift(op, 16), 31) + local cn = "" + rs = rs - 10 + if band(bo, 4) == 0 then + cn = band(bo, 2) == 0 and "dnz" or "dz" + if band(bo, 0x10) == 0 then + cn = cn..(band(bo, 8) == 0 and "f" or "t") + end + if band(bo, 0x10) == 0 then x = condfmt(cond) end + name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+") + elseif band(bo, 0x10) == 0 then + cn = map_cond[band(cond, 3) + (band(bo, 8) == 0 and 4 or 0)] + if cond > 3 then x = "cr"..rshift(cond, 2) end + name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+") + end + name = gsub(name, "_", cn) + elseif p == "J" then + x = arshift(lshift(op, 27-rs), 29-rs)*4 + if band(op, 2) == 0 then x = ctx.addr + pos + x end + ctx.rel = x + x = "0x"..tohex(x) + elseif p == "K" then + if band(op, 1) ~= 0 then name = name.."l" end + if band(op, 2) ~= 0 then name = name.."a" end + elseif p == "X" or p == "Y" then + x = band(rshift(op, rs+2), 7) + if x == 0 and p == "Y" then x = nil else x = "cr"..x end + rs = rs - 5 + elseif p == "W" then + x = "cr"..band(op, 7) + elseif p == "Z" then + x = band(rshift(op, rs-4), 255) + rs = rs - 10 + elseif p == ">" then + operands[#operands] = rshift(operands[#operands], 1) + elseif p == "0" then + if last == "r0" then + operands[#operands] = nil + if altname then name = altname end + end + elseif p == "L" then + name = gsub(name, "_", band(op, 0x00200000) ~= 0 and "d" or "w") + elseif p == "." then + if band(op, 1) == 1 then name = name.."." end + elseif p == "N" then + if op == 0x60000000 then name = "nop"; break end + elseif p == "~" then + local n = #operands + operands[n-1], operands[n] = operands[n], operands[n-1] + elseif p == "=" then + local n = #operands + if last == operands[n-1] then + operands[n] = nil + name = altname + end + elseif p == "%" then + local n = #operands + if last == operands[n-1] and last == operands[n-2] then + operands[n] = nil + operands[n-1] = nil + name = altname + end + elseif p == "-" then + rs = rs - 5 + else + assert(false) + end + if x then operands[#operands+1] = x; last = x end + end + + return putop(ctx, name, operands) +end + +------------------------------------------------------------------------------ + +-- Disassemble a block of code. +local function disass_block(ctx, ofs, len) + if not ofs then ofs = 0 end + local stop = len and ofs+len or #ctx.code + stop = stop - stop % 4 + ctx.pos = ofs - ofs % 4 + ctx.rel = nil + while ctx.pos < stop do disass_ins(ctx) end +end + +-- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). +local function create_(code, addr, out) + local ctx = {} + ctx.code = code + ctx.addr = addr or 0 + ctx.out = out or io.write + ctx.symtab = {} + ctx.disass = disass_block + ctx.hexdump = 8 + return ctx +end + +-- Simple API: disassemble code (a string) at address and output via out. +local function disass_(code, addr, out) + create_(code, addr, out):disass() +end + +-- Return register name for RID. +local function regname_(r) + if r < 32 then return map_gpr[r] end + return "f"..(r-32) +end + +-- Public module functions. +module(...) + +create = create_ +disass = disass_ +regname = regname_ + diff --git a/external/lua/luajit/src/src/jit/dis_x64.lua b/external/lua/luajit/src/src/jit/dis_x64.lua new file mode 100644 index 0000000000..4a1894acea --- /dev/null +++ b/external/lua/luajit/src/src/jit/dis_x64.lua @@ -0,0 +1,20 @@ +---------------------------------------------------------------------------- +-- LuaJIT x64 disassembler wrapper module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- +-- This module just exports the 64 bit functions from the combined +-- x86/x64 disassembler module. All the interesting stuff is there. +------------------------------------------------------------------------------ + +local require = require + +module(...) + +local dis_x86 = require(_PACKAGE.."dis_x86") + +create = dis_x86.create64 +disass = dis_x86.disass64 +regname = dis_x86.regname64 + diff --git a/external/lua/luajit/src/src/jit/dis_x86.lua b/external/lua/luajit/src/src/jit/dis_x86.lua new file mode 100644 index 0000000000..c442a176b1 --- /dev/null +++ b/external/lua/luajit/src/src/jit/dis_x86.lua @@ -0,0 +1,836 @@ +---------------------------------------------------------------------------- +-- LuaJIT x86/x64 disassembler module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- +-- This is a helper module used by the LuaJIT machine code dumper module. +-- +-- Sending small code snippets to an external disassembler and mixing the +-- output with our own stuff was too fragile. So I had to bite the bullet +-- and write yet another x86 disassembler. Oh well ... +-- +-- The output format is very similar to what ndisasm generates. But it has +-- been developed independently by looking at the opcode tables from the +-- Intel and AMD manuals. The supported instruction set is quite extensive +-- and reflects what a current generation Intel or AMD CPU implements in +-- 32 bit and 64 bit mode. Yes, this includes MMX, SSE, SSE2, SSE3, SSSE3, +-- SSE4.1, SSE4.2, SSE4a and even privileged and hypervisor (VMX/SVM) +-- instructions. +-- +-- Notes: +-- * The (useless) a16 prefix, 3DNow and pre-586 opcodes are unsupported. +-- * No attempt at optimization has been made -- it's fast enough for my needs. +-- * The public API may change when more architectures are added. +------------------------------------------------------------------------------ + +local type = type +local sub, byte, format = string.sub, string.byte, string.format +local match, gmatch, gsub = string.match, string.gmatch, string.gsub +local lower, rep = string.lower, string.rep + +-- Map for 1st opcode byte in 32 bit mode. Ugly? Well ... read on. +local map_opc1_32 = { +--0x +[0]="addBmr","addVmr","addBrm","addVrm","addBai","addVai","push es","pop es", +"orBmr","orVmr","orBrm","orVrm","orBai","orVai","push cs","opc2*", +--1x +"adcBmr","adcVmr","adcBrm","adcVrm","adcBai","adcVai","push ss","pop ss", +"sbbBmr","sbbVmr","sbbBrm","sbbVrm","sbbBai","sbbVai","push ds","pop ds", +--2x +"andBmr","andVmr","andBrm","andVrm","andBai","andVai","es:seg","daa", +"subBmr","subVmr","subBrm","subVrm","subBai","subVai","cs:seg","das", +--3x +"xorBmr","xorVmr","xorBrm","xorVrm","xorBai","xorVai","ss:seg","aaa", +"cmpBmr","cmpVmr","cmpBrm","cmpVrm","cmpBai","cmpVai","ds:seg","aas", +--4x +"incVR","incVR","incVR","incVR","incVR","incVR","incVR","incVR", +"decVR","decVR","decVR","decVR","decVR","decVR","decVR","decVR", +--5x +"pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR","pushUR", +"popUR","popUR","popUR","popUR","popUR","popUR","popUR","popUR", +--6x +"sz*pushaw,pusha","sz*popaw,popa","boundVrm","arplWmr", +"fs:seg","gs:seg","o16:","a16", +"pushUi","imulVrmi","pushBs","imulVrms", +"insb","insVS","outsb","outsVS", +--7x +"joBj","jnoBj","jbBj","jnbBj","jzBj","jnzBj","jbeBj","jaBj", +"jsBj","jnsBj","jpeBj","jpoBj","jlBj","jgeBj","jleBj","jgBj", +--8x +"arith!Bmi","arith!Vmi","arith!Bmi","arith!Vms", +"testBmr","testVmr","xchgBrm","xchgVrm", +"movBmr","movVmr","movBrm","movVrm", +"movVmg","leaVrm","movWgm","popUm", +--9x +"nop*xchgVaR|pause|xchgWaR|repne nop","xchgVaR","xchgVaR","xchgVaR", +"xchgVaR","xchgVaR","xchgVaR","xchgVaR", +"sz*cbw,cwde,cdqe","sz*cwd,cdq,cqo","call farViw","wait", +"sz*pushfw,pushf","sz*popfw,popf","sahf","lahf", +--Ax +"movBao","movVao","movBoa","movVoa", +"movsb","movsVS","cmpsb","cmpsVS", +"testBai","testVai","stosb","stosVS", +"lodsb","lodsVS","scasb","scasVS", +--Bx +"movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi","movBRi", +"movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI","movVRI", +--Cx +"shift!Bmu","shift!Vmu","retBw","ret","$lesVrm","$ldsVrm","movBmi","movVmi", +"enterBwu","leave","retfBw","retf","int3","intBu","into","iretVS", +--Dx +"shift!Bm1","shift!Vm1","shift!Bmc","shift!Vmc","aamBu","aadBu","salc","xlatb", +"fp*0","fp*1","fp*2","fp*3","fp*4","fp*5","fp*6","fp*7", +--Ex +"loopneBj","loopeBj","loopBj","sz*jcxzBj,jecxzBj,jrcxzBj", +"inBau","inVau","outBua","outVua", +"callVj","jmpVj","jmp farViw","jmpBj","inBad","inVad","outBda","outVda", +--Fx +"lock:","int1","repne:rep","rep:","hlt","cmc","testb!Bm","testv!Vm", +"clc","stc","cli","sti","cld","std","incb!Bm","incd!Vm", +} +assert(#map_opc1_32 == 255) + +-- Map for 1st opcode byte in 64 bit mode (overrides only). +local map_opc1_64 = setmetatable({ + [0x06]=false, [0x07]=false, [0x0e]=false, + [0x16]=false, [0x17]=false, [0x1e]=false, [0x1f]=false, + [0x27]=false, [0x2f]=false, [0x37]=false, [0x3f]=false, + [0x60]=false, [0x61]=false, [0x62]=false, [0x63]="movsxdVrDmt", [0x67]="a32:", + [0x40]="rex*", [0x41]="rex*b", [0x42]="rex*x", [0x43]="rex*xb", + [0x44]="rex*r", [0x45]="rex*rb", [0x46]="rex*rx", [0x47]="rex*rxb", + [0x48]="rex*w", [0x49]="rex*wb", [0x4a]="rex*wx", [0x4b]="rex*wxb", + [0x4c]="rex*wr", [0x4d]="rex*wrb", [0x4e]="rex*wrx", [0x4f]="rex*wrxb", + [0x82]=false, [0x9a]=false, [0xc4]=false, [0xc5]=false, [0xce]=false, + [0xd4]=false, [0xd5]=false, [0xd6]=false, [0xea]=false, +}, { __index = map_opc1_32 }) + +-- Map for 2nd opcode byte (0F xx). True CISC hell. Hey, I told you. +-- Prefix dependent MMX/SSE opcodes: (none)|rep|o16|repne, -|F3|66|F2 +local map_opc2 = { +--0x +[0]="sldt!Dmp","sgdt!Ump","larVrm","lslVrm",nil,"syscall","clts","sysret", +"invd","wbinvd",nil,"ud1",nil,"$prefetch!Bm","femms","3dnowMrmu", +--1x +"movupsXrm|movssXrm|movupdXrm|movsdXrm", +"movupsXmr|movssXmr|movupdXmr|movsdXmr", +"movhlpsXrm$movlpsXrm|movsldupXrm|movlpdXrm|movddupXrm", +"movlpsXmr||movlpdXmr", +"unpcklpsXrm||unpcklpdXrm", +"unpckhpsXrm||unpckhpdXrm", +"movlhpsXrm$movhpsXrm|movshdupXrm|movhpdXrm", +"movhpsXmr||movhpdXmr", +"$prefetcht!Bm","hintnopVm","hintnopVm","hintnopVm", +"hintnopVm","hintnopVm","hintnopVm","hintnopVm", +--2x +"movUmx$","movUmy$","movUxm$","movUym$","movUmz$",nil,"movUzm$",nil, +"movapsXrm||movapdXrm", +"movapsXmr||movapdXmr", +"cvtpi2psXrMm|cvtsi2ssXrVmt|cvtpi2pdXrMm|cvtsi2sdXrVmt", +"movntpsXmr|movntssXmr|movntpdXmr|movntsdXmr", +"cvttps2piMrXm|cvttss2siVrXm|cvttpd2piMrXm|cvttsd2siVrXm", +"cvtps2piMrXm|cvtss2siVrXm|cvtpd2piMrXm|cvtsd2siVrXm", +"ucomissXrm||ucomisdXrm", +"comissXrm||comisdXrm", +--3x +"wrmsr","rdtsc","rdmsr","rdpmc","sysenter","sysexit",nil,"getsec", +"opc3*38",nil,"opc3*3a",nil,nil,nil,nil,nil, +--4x +"cmovoVrm","cmovnoVrm","cmovbVrm","cmovnbVrm", +"cmovzVrm","cmovnzVrm","cmovbeVrm","cmovaVrm", +"cmovsVrm","cmovnsVrm","cmovpeVrm","cmovpoVrm", +"cmovlVrm","cmovgeVrm","cmovleVrm","cmovgVrm", +--5x +"movmskpsVrXm$||movmskpdVrXm$","sqrtpsXrm|sqrtssXrm|sqrtpdXrm|sqrtsdXrm", +"rsqrtpsXrm|rsqrtssXrm","rcppsXrm|rcpssXrm", +"andpsXrm||andpdXrm","andnpsXrm||andnpdXrm", +"orpsXrm||orpdXrm","xorpsXrm||xorpdXrm", +"addpsXrm|addssXrm|addpdXrm|addsdXrm","mulpsXrm|mulssXrm|mulpdXrm|mulsdXrm", +"cvtps2pdXrm|cvtss2sdXrm|cvtpd2psXrm|cvtsd2ssXrm", +"cvtdq2psXrm|cvttps2dqXrm|cvtps2dqXrm", +"subpsXrm|subssXrm|subpdXrm|subsdXrm","minpsXrm|minssXrm|minpdXrm|minsdXrm", +"divpsXrm|divssXrm|divpdXrm|divsdXrm","maxpsXrm|maxssXrm|maxpdXrm|maxsdXrm", +--6x +"punpcklbwPrm","punpcklwdPrm","punpckldqPrm","packsswbPrm", +"pcmpgtbPrm","pcmpgtwPrm","pcmpgtdPrm","packuswbPrm", +"punpckhbwPrm","punpckhwdPrm","punpckhdqPrm","packssdwPrm", +"||punpcklqdqXrm","||punpckhqdqXrm", +"movPrVSm","movqMrm|movdquXrm|movdqaXrm", +--7x +"pshufwMrmu|pshufhwXrmu|pshufdXrmu|pshuflwXrmu","pshiftw!Pmu", +"pshiftd!Pmu","pshiftq!Mmu||pshiftdq!Xmu", +"pcmpeqbPrm","pcmpeqwPrm","pcmpeqdPrm","emms|", +"vmreadUmr||extrqXmuu$|insertqXrmuu$","vmwriteUrm||extrqXrm$|insertqXrm$", +nil,nil, +"||haddpdXrm|haddpsXrm","||hsubpdXrm|hsubpsXrm", +"movVSmMr|movqXrm|movVSmXr","movqMmr|movdquXmr|movdqaXmr", +--8x +"joVj","jnoVj","jbVj","jnbVj","jzVj","jnzVj","jbeVj","jaVj", +"jsVj","jnsVj","jpeVj","jpoVj","jlVj","jgeVj","jleVj","jgVj", +--9x +"setoBm","setnoBm","setbBm","setnbBm","setzBm","setnzBm","setbeBm","setaBm", +"setsBm","setnsBm","setpeBm","setpoBm","setlBm","setgeBm","setleBm","setgBm", +--Ax +"push fs","pop fs","cpuid","btVmr","shldVmru","shldVmrc",nil,nil, +"push gs","pop gs","rsm","btsVmr","shrdVmru","shrdVmrc","fxsave!Dmp","imulVrm", +--Bx +"cmpxchgBmr","cmpxchgVmr","$lssVrm","btrVmr", +"$lfsVrm","$lgsVrm","movzxVrBmt","movzxVrWmt", +"|popcntVrm","ud2Dp","bt!Vmu","btcVmr", +"bsfVrm","bsrVrm|lzcntVrm|bsrWrm","movsxVrBmt","movsxVrWmt", +--Cx +"xaddBmr","xaddVmr", +"cmppsXrmu|cmpssXrmu|cmppdXrmu|cmpsdXrmu","$movntiVmr|", +"pinsrwPrWmu","pextrwDrPmu", +"shufpsXrmu||shufpdXrmu","$cmpxchg!Qmp", +"bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR","bswapVR", +--Dx +"||addsubpdXrm|addsubpsXrm","psrlwPrm","psrldPrm","psrlqPrm", +"paddqPrm","pmullwPrm", +"|movq2dqXrMm|movqXmr|movdq2qMrXm$","pmovmskbVrMm||pmovmskbVrXm", +"psubusbPrm","psubuswPrm","pminubPrm","pandPrm", +"paddusbPrm","padduswPrm","pmaxubPrm","pandnPrm", +--Ex +"pavgbPrm","psrawPrm","psradPrm","pavgwPrm", +"pmulhuwPrm","pmulhwPrm", +"|cvtdq2pdXrm|cvttpd2dqXrm|cvtpd2dqXrm","$movntqMmr||$movntdqXmr", +"psubsbPrm","psubswPrm","pminswPrm","porPrm", +"paddsbPrm","paddswPrm","pmaxswPrm","pxorPrm", +--Fx +"|||lddquXrm","psllwPrm","pslldPrm","psllqPrm", +"pmuludqPrm","pmaddwdPrm","psadbwPrm","maskmovqMrm||maskmovdquXrm$", +"psubbPrm","psubwPrm","psubdPrm","psubqPrm", +"paddbPrm","paddwPrm","padddPrm","ud", +} +assert(map_opc2[255] == "ud") + +-- Map for three-byte opcodes. Can't wait for their next invention. +local map_opc3 = { +["38"] = { -- [66] 0f 38 xx +--0x +[0]="pshufbPrm","phaddwPrm","phadddPrm","phaddswPrm", +"pmaddubswPrm","phsubwPrm","phsubdPrm","phsubswPrm", +"psignbPrm","psignwPrm","psigndPrm","pmulhrswPrm", +nil,nil,nil,nil, +--1x +"||pblendvbXrma",nil,nil,nil, +"||blendvpsXrma","||blendvpdXrma",nil,"||ptestXrm", +nil,nil,nil,nil, +"pabsbPrm","pabswPrm","pabsdPrm",nil, +--2x +"||pmovsxbwXrm","||pmovsxbdXrm","||pmovsxbqXrm","||pmovsxwdXrm", +"||pmovsxwqXrm","||pmovsxdqXrm",nil,nil, +"||pmuldqXrm","||pcmpeqqXrm","||$movntdqaXrm","||packusdwXrm", +nil,nil,nil,nil, +--3x +"||pmovzxbwXrm","||pmovzxbdXrm","||pmovzxbqXrm","||pmovzxwdXrm", +"||pmovzxwqXrm","||pmovzxdqXrm",nil,"||pcmpgtqXrm", +"||pminsbXrm","||pminsdXrm","||pminuwXrm","||pminudXrm", +"||pmaxsbXrm","||pmaxsdXrm","||pmaxuwXrm","||pmaxudXrm", +--4x +"||pmulddXrm","||phminposuwXrm", +--Fx +[0xf0] = "|||crc32TrBmt",[0xf1] = "|||crc32TrVmt", +}, + +["3a"] = { -- [66] 0f 3a xx +--0x +[0x00]=nil,nil,nil,nil,nil,nil,nil,nil, +"||roundpsXrmu","||roundpdXrmu","||roundssXrmu","||roundsdXrmu", +"||blendpsXrmu","||blendpdXrmu","||pblendwXrmu","palignrPrmu", +--1x +nil,nil,nil,nil, +"||pextrbVmXru","||pextrwVmXru","||pextrVmSXru","||extractpsVmXru", +nil,nil,nil,nil,nil,nil,nil,nil, +--2x +"||pinsrbXrVmu","||insertpsXrmu","||pinsrXrVmuS",nil, +--4x +[0x40] = "||dppsXrmu", +[0x41] = "||dppdXrmu", +[0x42] = "||mpsadbwXrmu", +--6x +[0x60] = "||pcmpestrmXrmu",[0x61] = "||pcmpestriXrmu", +[0x62] = "||pcmpistrmXrmu",[0x63] = "||pcmpistriXrmu", +}, +} + +-- Map for VMX/SVM opcodes 0F 01 C0-FF (sgdt group with register operands). +local map_opcvm = { +[0xc1]="vmcall",[0xc2]="vmlaunch",[0xc3]="vmresume",[0xc4]="vmxoff", +[0xc8]="monitor",[0xc9]="mwait", +[0xd8]="vmrun",[0xd9]="vmmcall",[0xda]="vmload",[0xdb]="vmsave", +[0xdc]="stgi",[0xdd]="clgi",[0xde]="skinit",[0xdf]="invlpga", +[0xf8]="swapgs",[0xf9]="rdtscp", +} + +-- Map for FP opcodes. And you thought stack machines are simple? +local map_opcfp = { +-- D8-DF 00-BF: opcodes with a memory operand. +-- D8 +[0]="faddFm","fmulFm","fcomFm","fcompFm","fsubFm","fsubrFm","fdivFm","fdivrFm", +"fldFm",nil,"fstFm","fstpFm","fldenvVm","fldcwWm","fnstenvVm","fnstcwWm", +-- DA +"fiaddDm","fimulDm","ficomDm","ficompDm", +"fisubDm","fisubrDm","fidivDm","fidivrDm", +-- DB +"fildDm","fisttpDm","fistDm","fistpDm",nil,"fld twordFmp",nil,"fstp twordFmp", +-- DC +"faddGm","fmulGm","fcomGm","fcompGm","fsubGm","fsubrGm","fdivGm","fdivrGm", +-- DD +"fldGm","fisttpQm","fstGm","fstpGm","frstorDmp",nil,"fnsaveDmp","fnstswWm", +-- DE +"fiaddWm","fimulWm","ficomWm","ficompWm", +"fisubWm","fisubrWm","fidivWm","fidivrWm", +-- DF +"fildWm","fisttpWm","fistWm","fistpWm", +"fbld twordFmp","fildQm","fbstp twordFmp","fistpQm", +-- xx C0-FF: opcodes with a pseudo-register operand. +-- D8 +"faddFf","fmulFf","fcomFf","fcompFf","fsubFf","fsubrFf","fdivFf","fdivrFf", +-- D9 +"fldFf","fxchFf",{"fnop"},nil, +{"fchs","fabs",nil,nil,"ftst","fxam"}, +{"fld1","fldl2t","fldl2e","fldpi","fldlg2","fldln2","fldz"}, +{"f2xm1","fyl2x","fptan","fpatan","fxtract","fprem1","fdecstp","fincstp"}, +{"fprem","fyl2xp1","fsqrt","fsincos","frndint","fscale","fsin","fcos"}, +-- DA +"fcmovbFf","fcmoveFf","fcmovbeFf","fcmovuFf",nil,{nil,"fucompp"},nil,nil, +-- DB +"fcmovnbFf","fcmovneFf","fcmovnbeFf","fcmovnuFf", +{nil,nil,"fnclex","fninit"},"fucomiFf","fcomiFf",nil, +-- DC +"fadd toFf","fmul toFf",nil,nil, +"fsub toFf","fsubr toFf","fdivr toFf","fdiv toFf", +-- DD +"ffreeFf",nil,"fstFf","fstpFf","fucomFf","fucompFf",nil,nil, +-- DE +"faddpFf","fmulpFf",nil,{nil,"fcompp"}, +"fsubrpFf","fsubpFf","fdivrpFf","fdivpFf", +-- DF +nil,nil,nil,nil,{"fnstsw ax"},"fucomipFf","fcomipFf",nil, +} +assert(map_opcfp[126] == "fcomipFf") + +-- Map for opcode groups. The subkey is sp from the ModRM byte. +local map_opcgroup = { + arith = { "add", "or", "adc", "sbb", "and", "sub", "xor", "cmp" }, + shift = { "rol", "ror", "rcl", "rcr", "shl", "shr", "sal", "sar" }, + testb = { "testBmi", "testBmi", "not", "neg", "mul", "imul", "div", "idiv" }, + testv = { "testVmi", "testVmi", "not", "neg", "mul", "imul", "div", "idiv" }, + incb = { "inc", "dec" }, + incd = { "inc", "dec", "callUmp", "$call farDmp", + "jmpUmp", "$jmp farDmp", "pushUm" }, + sldt = { "sldt", "str", "lldt", "ltr", "verr", "verw" }, + sgdt = { "vm*$sgdt", "vm*$sidt", "$lgdt", "vm*$lidt", + "smsw", nil, "lmsw", "vm*$invlpg" }, + bt = { nil, nil, nil, nil, "bt", "bts", "btr", "btc" }, + cmpxchg = { nil, "sz*,cmpxchg8bQmp,cmpxchg16bXmp", nil, nil, + nil, nil, "vmptrld|vmxon|vmclear", "vmptrst" }, + pshiftw = { nil, nil, "psrlw", nil, "psraw", nil, "psllw" }, + pshiftd = { nil, nil, "psrld", nil, "psrad", nil, "pslld" }, + pshiftq = { nil, nil, "psrlq", nil, nil, nil, "psllq" }, + pshiftdq = { nil, nil, "psrlq", "psrldq", nil, nil, "psllq", "pslldq" }, + fxsave = { "$fxsave", "$fxrstor", "$ldmxcsr", "$stmxcsr", + nil, "lfenceDp$", "mfenceDp$", "sfenceDp$clflush" }, + prefetch = { "prefetch", "prefetchw" }, + prefetcht = { "prefetchnta", "prefetcht0", "prefetcht1", "prefetcht2" }, +} + +------------------------------------------------------------------------------ + +-- Maps for register names. +local map_regs = { + B = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh", + "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" }, + B64 = { "al", "cl", "dl", "bl", "spl", "bpl", "sil", "dil", + "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b" }, + W = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di", + "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w" }, + D = { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", + "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d" }, + Q = { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", + "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" }, + M = { "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7", + "mm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7" }, -- No x64 ext! + X = { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", + "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15" }, +} +local map_segregs = { "es", "cs", "ss", "ds", "fs", "gs", "segr6", "segr7" } + +-- Maps for size names. +local map_sz2n = { + B = 1, W = 2, D = 4, Q = 8, M = 8, X = 16, +} +local map_sz2prefix = { + B = "byte", W = "word", D = "dword", + Q = "qword", + M = "qword", X = "xword", + F = "dword", G = "qword", -- No need for sizes/register names for these two. +} + +------------------------------------------------------------------------------ + +-- Output a nicely formatted line with an opcode and operands. +local function putop(ctx, text, operands) + local code, pos, hex = ctx.code, ctx.pos, "" + local hmax = ctx.hexdump + if hmax > 0 then + for i=ctx.start,pos-1 do + hex = hex..format("%02X", byte(code, i, i)) + end + if #hex > hmax then hex = sub(hex, 1, hmax)..". " + else hex = hex..rep(" ", hmax-#hex+2) end + end + if operands then text = text.." "..operands end + if ctx.o16 then text = "o16 "..text; ctx.o16 = false end + if ctx.a32 then text = "a32 "..text; ctx.a32 = false end + if ctx.rep then text = ctx.rep.." "..text; ctx.rep = false end + if ctx.rex then + local t = (ctx.rexw and "w" or "")..(ctx.rexr and "r" or "").. + (ctx.rexx and "x" or "")..(ctx.rexb and "b" or "") + if t ~= "" then text = "rex."..t.." "..text end + ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false + ctx.rex = false + end + if ctx.seg then + local text2, n = gsub(text, "%[", "["..ctx.seg..":") + if n == 0 then text = ctx.seg.." "..text else text = text2 end + ctx.seg = false + end + if ctx.lock then text = "lock "..text; ctx.lock = false end + local imm = ctx.imm + if imm then + local sym = ctx.symtab[imm] + if sym then text = text.."\t->"..sym end + end + ctx.out(format("%08x %s%s\n", ctx.addr+ctx.start, hex, text)) + ctx.mrm = false + ctx.start = pos + ctx.imm = nil +end + +-- Clear all prefix flags. +local function clearprefixes(ctx) + ctx.o16 = false; ctx.seg = false; ctx.lock = false; ctx.rep = false + ctx.rexw = false; ctx.rexr = false; ctx.rexx = false; ctx.rexb = false + ctx.rex = false; ctx.a32 = false +end + +-- Fallback for incomplete opcodes at the end. +local function incomplete(ctx) + ctx.pos = ctx.stop+1 + clearprefixes(ctx) + return putop(ctx, "(incomplete)") +end + +-- Fallback for unknown opcodes. +local function unknown(ctx) + clearprefixes(ctx) + return putop(ctx, "(unknown)") +end + +-- Return an immediate of the specified size. +local function getimm(ctx, pos, n) + if pos+n-1 > ctx.stop then return incomplete(ctx) end + local code = ctx.code + if n == 1 then + local b1 = byte(code, pos, pos) + return b1 + elseif n == 2 then + local b1, b2 = byte(code, pos, pos+1) + return b1+b2*256 + else + local b1, b2, b3, b4 = byte(code, pos, pos+3) + local imm = b1+b2*256+b3*65536+b4*16777216 + ctx.imm = imm + return imm + end +end + +-- Process pattern string and generate the operands. +local function putpat(ctx, name, pat) + local operands, regs, sz, mode, sp, rm, sc, rx, sdisp + local code, pos, stop = ctx.code, ctx.pos, ctx.stop + + -- Chars used: 1DFGIMPQRSTUVWXacdfgijmoprstuwxyz + for p in gmatch(pat, ".") do + local x = nil + if p == "V" or p == "U" then + if ctx.rexw then sz = "Q"; ctx.rexw = false + elseif ctx.o16 then sz = "W"; ctx.o16 = false + elseif p == "U" and ctx.x64 then sz = "Q" + else sz = "D" end + regs = map_regs[sz] + elseif p == "T" then + if ctx.rexw then sz = "Q"; ctx.rexw = false else sz = "D" end + regs = map_regs[sz] + elseif p == "B" then + sz = "B" + regs = ctx.rex and map_regs.B64 or map_regs.B + elseif match(p, "[WDQMXFG]") then + sz = p + regs = map_regs[sz] + elseif p == "P" then + sz = ctx.o16 and "X" or "M"; ctx.o16 = false + regs = map_regs[sz] + elseif p == "S" then + name = name..lower(sz) + elseif p == "s" then + local imm = getimm(ctx, pos, 1); if not imm then return end + x = imm <= 127 and format("+0x%02x", imm) + or format("-0x%02x", 256-imm) + pos = pos+1 + elseif p == "u" then + local imm = getimm(ctx, pos, 1); if not imm then return end + x = format("0x%02x", imm) + pos = pos+1 + elseif p == "w" then + local imm = getimm(ctx, pos, 2); if not imm then return end + x = format("0x%x", imm) + pos = pos+2 + elseif p == "o" then -- [offset] + if ctx.x64 then + local imm1 = getimm(ctx, pos, 4); if not imm1 then return end + local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end + x = format("[0x%08x%08x]", imm2, imm1) + pos = pos+8 + else + local imm = getimm(ctx, pos, 4); if not imm then return end + x = format("[0x%08x]", imm) + pos = pos+4 + end + elseif p == "i" or p == "I" then + local n = map_sz2n[sz] + if n == 8 and ctx.x64 and p == "I" then + local imm1 = getimm(ctx, pos, 4); if not imm1 then return end + local imm2 = getimm(ctx, pos+4, 4); if not imm2 then return end + x = format("0x%08x%08x", imm2, imm1) + else + if n == 8 then n = 4 end + local imm = getimm(ctx, pos, n); if not imm then return end + if sz == "Q" and (imm < 0 or imm > 0x7fffffff) then + imm = (0xffffffff+1)-imm + x = format(imm > 65535 and "-0x%08x" or "-0x%x", imm) + else + x = format(imm > 65535 and "0x%08x" or "0x%x", imm) + end + end + pos = pos+n + elseif p == "j" then + local n = map_sz2n[sz] + if n == 8 then n = 4 end + local imm = getimm(ctx, pos, n); if not imm then return end + if sz == "B" and imm > 127 then imm = imm-256 + elseif imm > 2147483647 then imm = imm-4294967296 end + pos = pos+n + imm = imm + pos + ctx.addr + if imm > 4294967295 and not ctx.x64 then imm = imm-4294967296 end + ctx.imm = imm + if sz == "W" then + x = format("word 0x%04x", imm%65536) + elseif ctx.x64 then + local lo = imm % 0x1000000 + x = format("0x%02x%06x", (imm-lo) / 0x1000000, lo) + else + x = format("0x%08x", imm) + end + elseif p == "R" then + local r = byte(code, pos-1, pos-1)%8 + if ctx.rexb then r = r + 8; ctx.rexb = false end + x = regs[r+1] + elseif p == "a" then x = regs[1] + elseif p == "c" then x = "cl" + elseif p == "d" then x = "dx" + elseif p == "1" then x = "1" + else + if not mode then + mode = ctx.mrm + if not mode then + if pos > stop then return incomplete(ctx) end + mode = byte(code, pos, pos) + pos = pos+1 + end + rm = mode%8; mode = (mode-rm)/8 + sp = mode%8; mode = (mode-sp)/8 + sdisp = "" + if mode < 3 then + if rm == 4 then + if pos > stop then return incomplete(ctx) end + sc = byte(code, pos, pos) + pos = pos+1 + rm = sc%8; sc = (sc-rm)/8 + rx = sc%8; sc = (sc-rx)/8 + if ctx.rexx then rx = rx + 8; ctx.rexx = false end + if rx == 4 then rx = nil end + end + if mode > 0 or rm == 5 then + local dsz = mode + if dsz ~= 1 then dsz = 4 end + local disp = getimm(ctx, pos, dsz); if not disp then return end + if mode == 0 then rm = nil end + if rm or rx or (not sc and ctx.x64 and not ctx.a32) then + if dsz == 1 and disp > 127 then + sdisp = format("-0x%x", 256-disp) + elseif disp >= 0 and disp <= 0x7fffffff then + sdisp = format("+0x%x", disp) + else + sdisp = format("-0x%x", (0xffffffff+1)-disp) + end + else + sdisp = format(ctx.x64 and not ctx.a32 and + not (disp >= 0 and disp <= 0x7fffffff) + and "0xffffffff%08x" or "0x%08x", disp) + end + pos = pos+dsz + end + end + if rm and ctx.rexb then rm = rm + 8; ctx.rexb = false end + if ctx.rexr then sp = sp + 8; ctx.rexr = false end + end + if p == "m" then + if mode == 3 then x = regs[rm+1] + else + local aregs = ctx.a32 and map_regs.D or ctx.aregs + local srm, srx = "", "" + if rm then srm = aregs[rm+1] + elseif not sc and ctx.x64 and not ctx.a32 then srm = "rip" end + ctx.a32 = false + if rx then + if rm then srm = srm.."+" end + srx = aregs[rx+1] + if sc > 0 then srx = srx.."*"..(2^sc) end + end + x = format("[%s%s%s]", srm, srx, sdisp) + end + if mode < 3 and + (not match(pat, "[aRrgp]") or match(pat, "t")) then -- Yuck. + x = map_sz2prefix[sz].." "..x + end + elseif p == "r" then x = regs[sp+1] + elseif p == "g" then x = map_segregs[sp+1] + elseif p == "p" then -- Suppress prefix. + elseif p == "f" then x = "st"..rm + elseif p == "x" then + if sp == 0 and ctx.lock and not ctx.x64 then + x = "CR8"; ctx.lock = false + else + x = "CR"..sp + end + elseif p == "y" then x = "DR"..sp + elseif p == "z" then x = "TR"..sp + elseif p == "t" then + else + error("bad pattern `"..pat.."'") + end + end + if x then operands = operands and operands..", "..x or x end + end + ctx.pos = pos + return putop(ctx, name, operands) +end + +-- Forward declaration. +local map_act + +-- Fetch and cache MRM byte. +local function getmrm(ctx) + local mrm = ctx.mrm + if not mrm then + local pos = ctx.pos + if pos > ctx.stop then return nil end + mrm = byte(ctx.code, pos, pos) + ctx.pos = pos+1 + ctx.mrm = mrm + end + return mrm +end + +-- Dispatch to handler depending on pattern. +local function dispatch(ctx, opat, patgrp) + if not opat then return unknown(ctx) end + if match(opat, "%|") then -- MMX/SSE variants depending on prefix. + local p + if ctx.rep then + p = ctx.rep=="rep" and "%|([^%|]*)" or "%|[^%|]*%|[^%|]*%|([^%|]*)" + ctx.rep = false + elseif ctx.o16 then p = "%|[^%|]*%|([^%|]*)"; ctx.o16 = false + else p = "^[^%|]*" end + opat = match(opat, p) + if not opat then return unknown(ctx) end +-- ctx.rep = false; ctx.o16 = false + --XXX fails for 66 f2 0f 38 f1 06 crc32 eax,WORD PTR [esi] + --XXX remove in branches? + end + if match(opat, "%$") then -- reg$mem variants. + local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end + opat = match(opat, mrm >= 192 and "^[^%$]*" or "%$(.*)") + if opat == "" then return unknown(ctx) end + end + if opat == "" then return unknown(ctx) end + local name, pat = match(opat, "^([a-z0-9 ]*)(.*)") + if pat == "" and patgrp then pat = patgrp end + return map_act[sub(pat, 1, 1)](ctx, name, pat) +end + +-- Get a pattern from an opcode map and dispatch to handler. +local function dispatchmap(ctx, opcmap) + local pos = ctx.pos + local opat = opcmap[byte(ctx.code, pos, pos)] + pos = pos + 1 + ctx.pos = pos + return dispatch(ctx, opat) +end + +-- Map for action codes. The key is the first char after the name. +map_act = { + -- Simple opcodes without operands. + [""] = function(ctx, name, pat) + return putop(ctx, name) + end, + + -- Operand size chars fall right through. + B = putpat, W = putpat, D = putpat, Q = putpat, + V = putpat, U = putpat, T = putpat, + M = putpat, X = putpat, P = putpat, + F = putpat, G = putpat, + + -- Collect prefixes. + [":"] = function(ctx, name, pat) + ctx[pat == ":" and name or sub(pat, 2)] = name + if ctx.pos - ctx.start > 5 then return unknown(ctx) end -- Limit #prefixes. + end, + + -- Chain to special handler specified by name. + ["*"] = function(ctx, name, pat) + return map_act[name](ctx, name, sub(pat, 2)) + end, + + -- Use named subtable for opcode group. + ["!"] = function(ctx, name, pat) + local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end + return dispatch(ctx, map_opcgroup[name][((mrm-(mrm%8))/8)%8+1], sub(pat, 2)) + end, + + -- o16,o32[,o64] variants. + sz = function(ctx, name, pat) + if ctx.o16 then ctx.o16 = false + else + pat = match(pat, ",(.*)") + if ctx.rexw then + local p = match(pat, ",(.*)") + if p then pat = p; ctx.rexw = false end + end + end + pat = match(pat, "^[^,]*") + return dispatch(ctx, pat) + end, + + -- Two-byte opcode dispatch. + opc2 = function(ctx, name, pat) + return dispatchmap(ctx, map_opc2) + end, + + -- Three-byte opcode dispatch. + opc3 = function(ctx, name, pat) + return dispatchmap(ctx, map_opc3[pat]) + end, + + -- VMX/SVM dispatch. + vm = function(ctx, name, pat) + return dispatch(ctx, map_opcvm[ctx.mrm]) + end, + + -- Floating point opcode dispatch. + fp = function(ctx, name, pat) + local mrm = getmrm(ctx); if not mrm then return incomplete(ctx) end + local rm = mrm%8 + local idx = pat*8 + ((mrm-rm)/8)%8 + if mrm >= 192 then idx = idx + 64 end + local opat = map_opcfp[idx] + if type(opat) == "table" then opat = opat[rm+1] end + return dispatch(ctx, opat) + end, + + -- REX prefix. + rex = function(ctx, name, pat) + if ctx.rex then return unknown(ctx) end -- Only 1 REX prefix allowed. + for p in gmatch(pat, ".") do ctx["rex"..p] = true end + ctx.rex = true + end, + + -- Special case for nop with REX prefix. + nop = function(ctx, name, pat) + return dispatch(ctx, ctx.rex and pat or "nop") + end, +} + +------------------------------------------------------------------------------ + +-- Disassemble a block of code. +local function disass_block(ctx, ofs, len) + if not ofs then ofs = 0 end + local stop = len and ofs+len or #ctx.code + ofs = ofs + 1 + ctx.start = ofs + ctx.pos = ofs + ctx.stop = stop + ctx.imm = nil + ctx.mrm = false + clearprefixes(ctx) + while ctx.pos <= stop do dispatchmap(ctx, ctx.map1) end + if ctx.pos ~= ctx.start then incomplete(ctx) end +end + +-- Extended API: create a disassembler context. Then call ctx:disass(ofs, len). +local function create_(code, addr, out) + local ctx = {} + ctx.code = code + ctx.addr = (addr or 0) - 1 + ctx.out = out or io.write + ctx.symtab = {} + ctx.disass = disass_block + ctx.hexdump = 16 + ctx.x64 = false + ctx.map1 = map_opc1_32 + ctx.aregs = map_regs.D + return ctx +end + +local function create64_(code, addr, out) + local ctx = create_(code, addr, out) + ctx.x64 = true + ctx.map1 = map_opc1_64 + ctx.aregs = map_regs.Q + return ctx +end + +-- Simple API: disassemble code (a string) at address and output via out. +local function disass_(code, addr, out) + create_(code, addr, out):disass() +end + +local function disass64_(code, addr, out) + create64_(code, addr, out):disass() +end + +-- Return register name for RID. +local function regname_(r) + if r < 8 then return map_regs.D[r+1] end + return map_regs.X[r-7] +end + +local function regname64_(r) + if r < 16 then return map_regs.Q[r+1] end + return map_regs.X[r-15] +end + +-- Public module functions. +module(...) + +create = create_ +create64 = create64_ +disass = disass_ +disass64 = disass64_ +regname = regname_ +regname64 = regname64_ + diff --git a/external/lua/luajit/src/src/jit/dump.lua b/external/lua/luajit/src/src/jit/dump.lua new file mode 100644 index 0000000000..70a59280e1 --- /dev/null +++ b/external/lua/luajit/src/src/jit/dump.lua @@ -0,0 +1,700 @@ +---------------------------------------------------------------------------- +-- LuaJIT compiler dump module. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- +-- +-- This module can be used to debug the JIT compiler itself. It dumps the +-- code representations and structures used in various compiler stages. +-- +-- Example usage: +-- +-- luajit -jdump -e "local x=0; for i=1,1e6 do x=x+i end; print(x)" +-- luajit -jdump=im -e "for i=1,1000 do for j=1,1000 do end end" | less -R +-- luajit -jdump=is myapp.lua | less -R +-- luajit -jdump=-b myapp.lua +-- luajit -jdump=+aH,myapp.html myapp.lua +-- luajit -jdump=ixT,myapp.dump myapp.lua +-- +-- The first argument specifies the dump mode. The second argument gives +-- the output file name. Default output is to stdout, unless the environment +-- variable LUAJIT_DUMPFILE is set. The file is overwritten every time the +-- module is started. +-- +-- Different features can be turned on or off with the dump mode. If the +-- mode starts with a '+', the following features are added to the default +-- set of features; a '-' removes them. Otherwise the features are replaced. +-- +-- The following dump features are available (* marks the default): +-- +-- * t Print a line for each started, ended or aborted trace (see also -jv). +-- * b Dump the traced bytecode. +-- * i Dump the IR (intermediate representation). +-- r Augment the IR with register/stack slots. +-- s Dump the snapshot map. +-- * m Dump the generated machine code. +-- x Print each taken trace exit. +-- X Print each taken trace exit and the contents of all registers. +-- +-- The output format can be set with the following characters: +-- +-- T Plain text output. +-- A ANSI-colored text output +-- H Colorized HTML + CSS output. +-- +-- The default output format is plain text. It's set to ANSI-colored text +-- if the COLORTERM variable is set. Note: this is independent of any output +-- redirection, which is actually considered a feature. +-- +-- You probably want to use less -R to enjoy viewing ANSI-colored text from +-- a pipe or a file. Add this to your ~/.bashrc: export LESS="-R" +-- +------------------------------------------------------------------------------ + +-- Cache some library functions and objects. +local jit = require("jit") +assert(jit.version_num == 20001, "LuaJIT core/library version mismatch") +local jutil = require("jit.util") +local vmdef = require("jit.vmdef") +local funcinfo, funcbc = jutil.funcinfo, jutil.funcbc +local traceinfo, traceir, tracek = jutil.traceinfo, jutil.traceir, jutil.tracek +local tracemc, tracesnap = jutil.tracemc, jutil.tracesnap +local traceexitstub, ircalladdr = jutil.traceexitstub, jutil.ircalladdr +local bit = require("bit") +local band, shl, shr = bit.band, bit.lshift, bit.rshift +local sub, gsub, format = string.sub, string.gsub, string.format +local byte, char, rep = string.byte, string.char, string.rep +local type, tostring = type, tostring +local stdout, stderr = io.stdout, io.stderr + +-- Load other modules on-demand. +local bcline, disass + +-- Active flag, output file handle and dump mode. +local active, out, dumpmode + +------------------------------------------------------------------------------ + +local symtabmt = { __index = false } +local symtab = {} +local nexitsym = 0 + +-- Fill nested symbol table with per-trace exit stub addresses. +local function fillsymtab_tr(tr, nexit) + local t = {} + symtabmt.__index = t + if jit.arch == "mips" or jit.arch == "mipsel" then + t[traceexitstub(tr, 0)] = "exit" + return + end + for i=0,nexit-1 do + local addr = traceexitstub(tr, i) + t[addr] = tostring(i) + end + local addr = traceexitstub(tr, nexit) + if addr then t[addr] = "stack_check" end +end + +-- Fill symbol table with trace exit stub addresses. +local function fillsymtab(tr, nexit) + local t = symtab + if nexitsym == 0 then + local ircall = vmdef.ircall + for i=0,#ircall do + local addr = ircalladdr(i) + if addr ~= 0 then t[addr] = ircall[i] end + end + end + if nexitsym == 1000000 then -- Per-trace exit stubs. + fillsymtab_tr(tr, nexit) + elseif nexit > nexitsym then -- Shared exit stubs. + for i=nexitsym,nexit-1 do + local addr = traceexitstub(i) + if addr == nil then -- Fall back to per-trace exit stubs. + fillsymtab_tr(tr, nexit) + setmetatable(symtab, symtabmt) + nexit = 1000000 + break + end + t[addr] = tostring(i) + end + nexitsym = nexit + end + return t +end + +local function dumpwrite(s) + out:write(s) +end + +-- Disassemble machine code. +local function dump_mcode(tr) + local info = traceinfo(tr) + if not info then return end + local mcode, addr, loop = tracemc(tr) + if not mcode then return end + if not disass then disass = require("jit.dis_"..jit.arch) end + out:write("---- TRACE ", tr, " mcode ", #mcode, "\n") + local ctx = disass.create(mcode, addr, dumpwrite) + ctx.hexdump = 0 + ctx.symtab = fillsymtab(tr, info.nexit) + if loop ~= 0 then + symtab[addr+loop] = "LOOP" + ctx:disass(0, loop) + out:write("->LOOP:\n") + ctx:disass(loop, #mcode-loop) + symtab[addr+loop] = nil + else + ctx:disass(0, #mcode) + end +end + +------------------------------------------------------------------------------ + +local irtype_text = { + [0] = "nil", + "fal", + "tru", + "lud", + "str", + "p32", + "thr", + "pro", + "fun", + "p64", + "cdt", + "tab", + "udt", + "flt", + "num", + "i8 ", + "u8 ", + "i16", + "u16", + "int", + "u32", + "i64", + "u64", + "sfp", +} + +local colortype_ansi = { + [0] = "%s", + "%s", + "%s", + "\027[36m%s\027[m", + "\027[32m%s\027[m", + "%s", + "\027[1m%s\027[m", + "%s", + "\027[1m%s\027[m", + "%s", + "\027[33m%s\027[m", + "\027[31m%s\027[m", + "\027[36m%s\027[m", + "\027[34m%s\027[m", + "\027[34m%s\027[m", + "\027[35m%s\027[m", + "\027[35m%s\027[m", + "\027[35m%s\027[m", + "\027[35m%s\027[m", + "\027[35m%s\027[m", + "\027[35m%s\027[m", + "\027[35m%s\027[m", + "\027[35m%s\027[m", + "\027[35m%s\027[m", +} + +local function colorize_text(s, t) + return s +end + +local function colorize_ansi(s, t) + return format(colortype_ansi[t], s) +end + +local irtype_ansi = setmetatable({}, + { __index = function(tab, t) + local s = colorize_ansi(irtype_text[t], t); tab[t] = s; return s; end }) + +local html_escape = { ["<"] = "<", [">"] = ">", ["&"] = "&", } + +local function colorize_html(s, t) + s = gsub(s, "[<>&]", html_escape) + return format('%s', irtype_text[t], s) +end + +local irtype_html = setmetatable({}, + { __index = function(tab, t) + local s = colorize_html(irtype_text[t], t); tab[t] = s; return s; end }) + +local header_html = [[ + +]] + +local colorize, irtype + +-- Lookup tables to convert some literals into names. +local litname = { + ["SLOAD "] = setmetatable({}, { __index = function(t, mode) + local s = "" + if band(mode, 1) ~= 0 then s = s.."P" end + if band(mode, 2) ~= 0 then s = s.."F" end + if band(mode, 4) ~= 0 then s = s.."T" end + if band(mode, 8) ~= 0 then s = s.."C" end + if band(mode, 16) ~= 0 then s = s.."R" end + if band(mode, 32) ~= 0 then s = s.."I" end + t[mode] = s + return s + end}), + ["XLOAD "] = { [0] = "", "R", "V", "RV", "U", "RU", "VU", "RVU", }, + ["CONV "] = setmetatable({}, { __index = function(t, mode) + local s = irtype[band(mode, 31)] + s = irtype[band(shr(mode, 5), 31)].."."..s + if band(mode, 0x400) ~= 0 then s = s.." trunc" + elseif band(mode, 0x800) ~= 0 then s = s.." sext" end + local c = shr(mode, 14) + if c == 2 then s = s.." index" elseif c == 3 then s = s.." check" end + t[mode] = s + return s + end}), + ["FLOAD "] = vmdef.irfield, + ["FREF "] = vmdef.irfield, + ["FPMATH"] = vmdef.irfpm, +} + +local function ctlsub(c) + if c == "\n" then return "\\n" + elseif c == "\r" then return "\\r" + elseif c == "\t" then return "\\t" + else return format("\\%03d", byte(c)) + end +end + +local function fmtfunc(func, pc) + local fi = funcinfo(func, pc) + if fi.loc then + return fi.loc + elseif fi.ffid then + return vmdef.ffnames[fi.ffid] + elseif fi.addr then + return format("C:%x", fi.addr) + else + return "(?)" + end +end + +local function formatk(tr, idx) + local k, t, slot = tracek(tr, idx) + local tn = type(k) + local s + if tn == "number" then + if k == 2^52+2^51 then + s = "bias" + else + s = format("%+.14g", k) + end + elseif tn == "string" then + s = format(#k > 20 and '"%.20s"~' or '"%s"', gsub(k, "%c", ctlsub)) + elseif tn == "function" then + s = fmtfunc(k) + elseif tn == "table" then + s = format("{%p}", k) + elseif tn == "userdata" then + if t == 12 then + s = format("userdata:%p", k) + else + s = format("[%p]", k) + if s == "[0x00000000]" then s = "NULL" end + end + elseif t == 21 then -- int64_t + s = sub(tostring(k), 1, -3) + if sub(s, 1, 1) ~= "-" then s = "+"..s end + else + s = tostring(k) -- For primitives. + end + s = colorize(format("%-4s", s), t) + if slot then + s = format("%s @%d", s, slot) + end + return s +end + +local function printsnap(tr, snap) + local n = 2 + for s=0,snap[1]-1 do + local sn = snap[n] + if shr(sn, 24) == s then + n = n + 1 + local ref = band(sn, 0xffff) - 0x8000 -- REF_BIAS + if ref < 0 then + out:write(formatk(tr, ref)) + elseif band(sn, 0x80000) ~= 0 then -- SNAP_SOFTFPNUM + out:write(colorize(format("%04d/%04d", ref, ref+1), 14)) + else + local m, ot, op1, op2 = traceir(tr, ref) + out:write(colorize(format("%04d", ref), band(ot, 31))) + end + out:write(band(sn, 0x10000) == 0 and " " or "|") -- SNAP_FRAME + else + out:write("---- ") + end + end + out:write("]\n") +end + +-- Dump snapshots (not interleaved with IR). +local function dump_snap(tr) + out:write("---- TRACE ", tr, " snapshots\n") + for i=0,1000000000 do + local snap = tracesnap(tr, i) + if not snap then break end + out:write(format("#%-3d %04d [ ", i, snap[0])) + printsnap(tr, snap) + end +end + +-- Return a register name or stack slot for a rid/sp location. +local function ridsp_name(ridsp, ins) + if not disass then disass = require("jit.dis_"..jit.arch) end + local rid, slot = band(ridsp, 0xff), shr(ridsp, 8) + if rid == 253 or rid == 254 then + return (slot == 0 or slot == 255) and " {sink" or format(" {%04d", ins-slot) + end + if ridsp > 255 then return format("[%x]", slot*4) end + if rid < 128 then return disass.regname(rid) end + return "" +end + +-- Dump CALL* function ref and return optional ctype. +local function dumpcallfunc(tr, ins) + local ctype + if ins > 0 then + local m, ot, op1, op2 = traceir(tr, ins) + if band(ot, 31) == 0 then -- nil type means CARG(func, ctype). + ins = op1 + ctype = formatk(tr, op2) + end + end + if ins < 0 then + out:write(format("[0x%x](", tonumber((tracek(tr, ins))))) + else + out:write(format("%04d (", ins)) + end + return ctype +end + +-- Recursively gather CALL* args and dump them. +local function dumpcallargs(tr, ins) + if ins < 0 then + out:write(formatk(tr, ins)) + else + local m, ot, op1, op2 = traceir(tr, ins) + local oidx = 6*shr(ot, 8) + local op = sub(vmdef.irnames, oidx+1, oidx+6) + if op == "CARG " then + dumpcallargs(tr, op1) + if op2 < 0 then + out:write(" ", formatk(tr, op2)) + else + out:write(" ", format("%04d", op2)) + end + else + out:write(format("%04d", ins)) + end + end +end + +-- Dump IR and interleaved snapshots. +local function dump_ir(tr, dumpsnap, dumpreg) + local info = traceinfo(tr) + if not info then return end + local nins = info.nins + out:write("---- TRACE ", tr, " IR\n") + local irnames = vmdef.irnames + local snapref = 65536 + local snap, snapno + if dumpsnap then + snap = tracesnap(tr, 0) + snapref = snap[0] + snapno = 0 + end + for ins=1,nins do + if ins >= snapref then + if dumpreg then + out:write(format(".... SNAP #%-3d [ ", snapno)) + else + out:write(format(".... SNAP #%-3d [ ", snapno)) + end + printsnap(tr, snap) + snapno = snapno + 1 + snap = tracesnap(tr, snapno) + snapref = snap and snap[0] or 65536 + end + local m, ot, op1, op2, ridsp = traceir(tr, ins) + local oidx, t = 6*shr(ot, 8), band(ot, 31) + local op = sub(irnames, oidx+1, oidx+6) + if op == "LOOP " then + if dumpreg then + out:write(format("%04d ------------ LOOP ------------\n", ins)) + else + out:write(format("%04d ------ LOOP ------------\n", ins)) + end + elseif op ~= "NOP " and op ~= "CARG " and + (dumpreg or op ~= "RENAME") then + local rid = band(ridsp, 255) + if dumpreg then + out:write(format("%04d %-6s", ins, ridsp_name(ridsp, ins))) + else + out:write(format("%04d ", ins)) + end + out:write(format("%s%s %s %s ", + (rid == 254 or rid == 253) and "}" or + (band(ot, 128) == 0 and " " or ">"), + band(ot, 64) == 0 and " " or "+", + irtype[t], op)) + local m1, m2 = band(m, 3), band(m, 3*4) + if sub(op, 1, 4) == "CALL" then + local ctype + if m2 == 1*4 then -- op2 == IRMlit + out:write(format("%-10s (", vmdef.ircall[op2])) + else + ctype = dumpcallfunc(tr, op2) + end + if op1 ~= -1 then dumpcallargs(tr, op1) end + out:write(")") + if ctype then out:write(" ctype ", ctype) end + elseif op == "CNEW " and op2 == -1 then + out:write(formatk(tr, op1)) + elseif m1 ~= 3 then -- op1 != IRMnone + if op1 < 0 then + out:write(formatk(tr, op1)) + else + out:write(format(m1 == 0 and "%04d" or "#%-3d", op1)) + end + if m2 ~= 3*4 then -- op2 != IRMnone + if m2 == 1*4 then -- op2 == IRMlit + local litn = litname[op] + if litn and litn[op2] then + out:write(" ", litn[op2]) + elseif op == "UREFO " or op == "UREFC " then + out:write(format(" #%-3d", shr(op2, 8))) + else + out:write(format(" #%-3d", op2)) + end + elseif op2 < 0 then + out:write(" ", formatk(tr, op2)) + else + out:write(format(" %04d", op2)) + end + end + end + out:write("\n") + end + end + if snap then + if dumpreg then + out:write(format(".... SNAP #%-3d [ ", snapno)) + else + out:write(format(".... SNAP #%-3d [ ", snapno)) + end + printsnap(tr, snap) + end +end + +------------------------------------------------------------------------------ + +local recprefix = "" +local recdepth = 0 + +-- Format trace error message. +local function fmterr(err, info) + if type(err) == "number" then + if type(info) == "function" then info = fmtfunc(info) end + err = format(vmdef.traceerr[err], info) + end + return err +end + +-- Dump trace states. +local function dump_trace(what, tr, func, pc, otr, oex) + if what == "stop" or (what == "abort" and dumpmode.a) then + if dumpmode.i then dump_ir(tr, dumpmode.s, dumpmode.r and what == "stop") + elseif dumpmode.s then dump_snap(tr) end + if dumpmode.m then dump_mcode(tr) end + end + if what == "start" then + if dumpmode.H then out:write('
\n') end
+    out:write("---- TRACE ", tr, " ", what)
+    if otr then out:write(" ", otr, "/", oex) end
+    out:write(" ", fmtfunc(func, pc), "\n")
+    recprefix = ""
+  elseif what == "stop" or what == "abort" then
+    out:write("---- TRACE ", tr, " ", what)
+    recprefix = nil
+    if what == "abort" then
+      out:write(" ", fmtfunc(func, pc), " -- ", fmterr(otr, oex), "\n")
+    else
+      local info = traceinfo(tr)
+      local link, ltype = info.link, info.linktype
+      if link == tr or link == 0 then
+	out:write(" -> ", ltype, "\n")
+      elseif ltype == "root" then
+	out:write(" -> ", link, "\n")
+      else
+	out:write(" -> ", link, " ", ltype, "\n")
+      end
+    end
+    if dumpmode.H then out:write("
\n\n") else out:write("\n") end + else + out:write("---- TRACE ", what, "\n\n") + end + out:flush() +end + +-- Dump recorded bytecode. +local function dump_record(tr, func, pc, depth, callee) + if depth ~= recdepth then + recdepth = depth + recprefix = rep(" .", depth) + end + local line + if pc >= 0 then + line = bcline(func, pc, recprefix) + if dumpmode.H then line = gsub(line, "[<>&]", html_escape) end + else + line = "0000 "..recprefix.." FUNCC \n" + callee = func + end + if pc <= 0 then + out:write(sub(line, 1, -2), " ; ", fmtfunc(func), "\n") + else + out:write(line) + end + if pc >= 0 and band(funcbc(func, pc), 0xff) < 16 then -- ORDER BC + out:write(bcline(func, pc+1, recprefix)) -- Write JMP for cond. + end +end + +------------------------------------------------------------------------------ + +-- Dump taken trace exits. +local function dump_texit(tr, ex, ngpr, nfpr, ...) + out:write("---- TRACE ", tr, " exit ", ex, "\n") + if dumpmode.X then + local regs = {...} + if jit.arch == "x64" then + for i=1,ngpr do + out:write(format(" %016x", regs[i])) + if i % 4 == 0 then out:write("\n") end + end + else + for i=1,ngpr do + out:write(format(" %08x", regs[i])) + if i % 8 == 0 then out:write("\n") end + end + end + if jit.arch == "mips" or jit.arch == "mipsel" then + for i=1,nfpr,2 do + out:write(format(" %+17.14g", regs[ngpr+i])) + if i % 8 == 7 then out:write("\n") end + end + else + for i=1,nfpr do + out:write(format(" %+17.14g", regs[ngpr+i])) + if i % 4 == 0 then out:write("\n") end + end + end + end +end + +------------------------------------------------------------------------------ + +-- Detach dump handlers. +local function dumpoff() + if active then + active = false + jit.attach(dump_texit) + jit.attach(dump_record) + jit.attach(dump_trace) + if out and out ~= stdout and out ~= stderr then out:close() end + out = nil + end +end + +-- Open the output file and attach dump handlers. +local function dumpon(opt, outfile) + if active then dumpoff() end + + local colormode = os.getenv("COLORTERM") and "A" or "T" + if opt then + opt = gsub(opt, "[TAH]", function(mode) colormode = mode; return ""; end) + end + + local m = { t=true, b=true, i=true, m=true, } + if opt and opt ~= "" then + local o = sub(opt, 1, 1) + if o ~= "+" and o ~= "-" then m = {} end + for i=1,#opt do m[sub(opt, i, i)] = (o ~= "-") end + end + dumpmode = m + + if m.t or m.b or m.i or m.s or m.m then + jit.attach(dump_trace, "trace") + end + if m.b then + jit.attach(dump_record, "record") + if not bcline then bcline = require("jit.bc").line end + end + if m.x or m.X then + jit.attach(dump_texit, "texit") + end + + if not outfile then outfile = os.getenv("LUAJIT_DUMPFILE") end + if outfile then + out = outfile == "-" and stdout or assert(io.open(outfile, "w")) + else + out = stdout + end + + m[colormode] = true + if colormode == "A" then + colorize = colorize_ansi + irtype = irtype_ansi + elseif colormode == "H" then + colorize = colorize_html + irtype = irtype_html + out:write(header_html) + else + colorize = colorize_text + irtype = irtype_text + end + + active = true +end + +-- Public module functions. +module(...) + +on = dumpon +off = dumpoff +start = dumpon -- For -j command line option. + diff --git a/external/lua/luajit/src/src/jit/v.lua b/external/lua/luajit/src/src/jit/v.lua new file mode 100644 index 0000000000..f4a9b054d7 --- /dev/null +++ b/external/lua/luajit/src/src/jit/v.lua @@ -0,0 +1,167 @@ +---------------------------------------------------------------------------- +-- Verbose mode of the LuaJIT compiler. +-- +-- Copyright (C) 2005-2013 Mike Pall. All rights reserved. +-- Released under the MIT license. See Copyright Notice in luajit.h +---------------------------------------------------------------------------- +-- +-- This module shows verbose information about the progress of the +-- JIT compiler. It prints one line for each generated trace. This module +-- is useful to see which code has been compiled or where the compiler +-- punts and falls back to the interpreter. +-- +-- Example usage: +-- +-- luajit -jv -e "for i=1,1000 do for j=1,1000 do end end" +-- luajit -jv=myapp.out myapp.lua +-- +-- Default output is to stderr. To redirect the output to a file, pass a +-- filename as an argument (use '-' for stdout) or set the environment +-- variable LUAJIT_VERBOSEFILE. The file is overwritten every time the +-- module is started. +-- +-- The output from the first example should look like this: +-- +-- [TRACE 1 (command line):1 loop] +-- [TRACE 2 (1/3) (command line):1 -> 1] +-- +-- The first number in each line is the internal trace number. Next are +-- the file name ('(command line)') and the line number (':1') where the +-- trace has started. Side traces also show the parent trace number and +-- the exit number where they are attached to in parentheses ('(1/3)'). +-- An arrow at the end shows where the trace links to ('-> 1'), unless +-- it loops to itself. +-- +-- In this case the inner loop gets hot and is traced first, generating +-- a root trace. Then the last exit from the 1st trace gets hot, too, +-- and triggers generation of the 2nd trace. The side trace follows the +-- path along the outer loop and *around* the inner loop, back to its +-- start, and then links to the 1st trace. Yes, this may seem unusual, +-- if you know how traditional compilers work. Trace compilers are full +-- of surprises like this -- have fun! :-) +-- +-- Aborted traces are shown like this: +-- +-- [TRACE --- foo.lua:44 -- leaving loop in root trace at foo:lua:50] +-- +-- Don't worry -- trace aborts are quite common, even in programs which +-- can be fully compiled. The compiler may retry several times until it +-- finds a suitable trace. +-- +-- Of course this doesn't work with features that are not-yet-implemented +-- (NYI error messages). The VM simply falls back to the interpreter. This +-- may not matter at all if the particular trace is not very high up in +-- the CPU usage profile. Oh, and the interpreter is quite fast, too. +-- +-- Also check out the -jdump module, which prints all the gory details. +-- +------------------------------------------------------------------------------ + +-- Cache some library functions and objects. +local jit = require("jit") +assert(jit.version_num == 20001, "LuaJIT core/library version mismatch") +local jutil = require("jit.util") +local vmdef = require("jit.vmdef") +local funcinfo, traceinfo = jutil.funcinfo, jutil.traceinfo +local type, format = type, string.format +local stdout, stderr = io.stdout, io.stderr + +-- Active flag and output file handle. +local active, out + +------------------------------------------------------------------------------ + +local startloc, startex + +local function fmtfunc(func, pc) + local fi = funcinfo(func, pc) + if fi.loc then + return fi.loc + elseif fi.ffid then + return vmdef.ffnames[fi.ffid] + elseif fi.addr then + return format("C:%x", fi.addr) + else + return "(?)" + end +end + +-- Format trace error message. +local function fmterr(err, info) + if type(err) == "number" then + if type(info) == "function" then info = fmtfunc(info) end + err = format(vmdef.traceerr[err], info) + end + return err +end + +-- Dump trace states. +local function dump_trace(what, tr, func, pc, otr, oex) + if what == "start" then + startloc = fmtfunc(func, pc) + startex = otr and "("..otr.."/"..oex..") " or "" + else + if what == "abort" then + local loc = fmtfunc(func, pc) + if loc ~= startloc then + out:write(format("[TRACE --- %s%s -- %s at %s]\n", + startex, startloc, fmterr(otr, oex), loc)) + else + out:write(format("[TRACE --- %s%s -- %s]\n", + startex, startloc, fmterr(otr, oex))) + end + elseif what == "stop" then + local info = traceinfo(tr) + local link, ltype = info.link, info.linktype + if ltype == "interpreter" then + out:write(format("[TRACE %3s %s%s -- fallback to interpreter]\n", + tr, startex, startloc)) + elseif link == tr or link == 0 then + out:write(format("[TRACE %3s %s%s %s]\n", + tr, startex, startloc, ltype)) + elseif ltype == "root" then + out:write(format("[TRACE %3s %s%s -> %d]\n", + tr, startex, startloc, link)) + else + out:write(format("[TRACE %3s %s%s -> %d %s]\n", + tr, startex, startloc, link, ltype)) + end + else + out:write(format("[TRACE %s]\n", what)) + end + out:flush() + end +end + +------------------------------------------------------------------------------ + +-- Detach dump handlers. +local function dumpoff() + if active then + active = false + jit.attach(dump_trace) + if out and out ~= stdout and out ~= stderr then out:close() end + out = nil + end +end + +-- Open the output file and attach dump handlers. +local function dumpon(outfile) + if active then dumpoff() end + if not outfile then outfile = os.getenv("LUAJIT_VERBOSEFILE") end + if outfile then + out = outfile == "-" and stdout or assert(io.open(outfile, "w")) + else + out = stderr + end + jit.attach(dump_trace, "trace") + active = true +end + +-- Public module functions. +module(...) + +on = dumpon +off = dumpoff +start = dumpon -- For -j command line option. + diff --git a/external/lua/luajit/src/src/lauxlib.h b/external/lua/luajit/src/src/lauxlib.h new file mode 100644 index 0000000000..fed1491b89 --- /dev/null +++ b/external/lua/luajit/src/src/lauxlib.h @@ -0,0 +1,167 @@ +/* +** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $ +** Auxiliary functions for building Lua libraries +** See Copyright Notice in lua.h +*/ + + +#ifndef lauxlib_h +#define lauxlib_h + + +#include +#include + +#include "lua.h" + + +#define luaL_getn(L,i) ((int)lua_objlen(L, i)) +#define luaL_setn(L,i,j) ((void)0) /* no op! */ + +/* extra error code for `luaL_load' */ +#define LUA_ERRFILE (LUA_ERRERR+1) + +typedef struct luaL_Reg { + const char *name; + lua_CFunction func; +} luaL_Reg; + +LUALIB_API void (luaL_openlib) (lua_State *L, const char *libname, + const luaL_Reg *l, int nup); +LUALIB_API void (luaL_register) (lua_State *L, const char *libname, + const luaL_Reg *l); +LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); +LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); +LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); +LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, + size_t *l); +LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg, + const char *def, size_t *l); +LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg); +LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); + +LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); +LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, + lua_Integer def); + +LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); +LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); +LUALIB_API void (luaL_checkany) (lua_State *L, int narg); + +LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname); +LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); + +LUALIB_API void (luaL_where) (lua_State *L, int lvl); +LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); + +LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def, + const char *const lst[]); + +LUALIB_API int (luaL_ref) (lua_State *L, int t); +LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref); + +LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename); +LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz, + const char *name); +LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s); + +LUALIB_API lua_State *(luaL_newstate) (void); + + +LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, + const char *r); + +LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, + const char *fname, int szhint); + +/* From Lua 5.2. */ +LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname); +LUALIB_API int luaL_execresult(lua_State *L, int stat); +LUALIB_API int (luaL_loadfilex) (lua_State *L, const char *filename, + const char *mode); +LUALIB_API int (luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, + const char *name, const char *mode); +LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, + int level); + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define luaL_argcheck(L, cond,numarg,extramsg) \ + ((void)((cond) || luaL_argerror(L, (numarg), (extramsg)))) +#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL)) +#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL)) +#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n))) +#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d))) +#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n))) +#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d))) + +#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i))) + +#define luaL_dofile(L, fn) \ + (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_dostring(L, s) \ + (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) + +#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n))) + +#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) + +/* +** {====================================================== +** Generic Buffer manipulation +** ======================================================= +*/ + + + +typedef struct luaL_Buffer { + char *p; /* current position in buffer */ + int lvl; /* number of strings in the stack (level) */ + lua_State *L; + char buffer[LUAL_BUFFERSIZE]; +} luaL_Buffer; + +#define luaL_addchar(B,c) \ + ((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \ + (*(B)->p++ = (char)(c))) + +/* compatibility only */ +#define luaL_putchar(B,c) luaL_addchar(B,c) + +#define luaL_addsize(B,n) ((B)->p += (n)) + +LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B); +LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B); +LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l); +LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s); +LUALIB_API void (luaL_addvalue) (luaL_Buffer *B); +LUALIB_API void (luaL_pushresult) (luaL_Buffer *B); + + +/* }====================================================== */ + + +/* compatibility with ref system */ + +/* pre-defined references */ +#define LUA_NOREF (-2) +#define LUA_REFNIL (-1) + +#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \ + (lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0)) + +#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref)) + +#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref)) + + +#define luaL_reg luaL_Reg + +#endif diff --git a/external/lua/luajit/src/src/lib_aux.c b/external/lua/luajit/src/src/lib_aux.c new file mode 100644 index 0000000000..05fa6b1018 --- /dev/null +++ b/external/lua/luajit/src/src/lib_aux.c @@ -0,0 +1,356 @@ +/* +** Auxiliary library for the Lua/C API. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major parts taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#include +#include +#include + +#define lib_aux_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" + +#include "lj_obj.h" +#include "lj_err.h" +#include "lj_state.h" +#include "lj_trace.h" +#include "lj_lib.h" + +#if LJ_TARGET_POSIX +#include +#endif + +/* -- I/O error handling -------------------------------------------------- */ + +LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname) +{ + if (stat) { + setboolV(L->top++, 1); + return 1; + } else { + int en = errno; /* Lua API calls may change this value. */ + setnilV(L->top++); + if (fname) + lua_pushfstring(L, "%s: %s", fname, strerror(en)); + else + lua_pushfstring(L, "%s", strerror(en)); + setintV(L->top++, en); + lj_trace_abort(G(L)); + return 3; + } +} + +LUALIB_API int luaL_execresult(lua_State *L, int stat) +{ + if (stat != -1) { +#if LJ_TARGET_POSIX + if (WIFSIGNALED(stat)) { + stat = WTERMSIG(stat); + setnilV(L->top++); + lua_pushliteral(L, "signal"); + } else { + if (WIFEXITED(stat)) + stat = WEXITSTATUS(stat); + if (stat == 0) + setboolV(L->top++, 1); + else + setnilV(L->top++); + lua_pushliteral(L, "exit"); + } +#else + if (stat == 0) + setboolV(L->top++, 1); + else + setnilV(L->top++); + lua_pushliteral(L, "exit"); +#endif + setintV(L->top++, stat); + return 3; + } + return luaL_fileresult(L, 0, NULL); +} + +/* -- Module registration ------------------------------------------------- */ + +LUALIB_API const char *luaL_findtable(lua_State *L, int idx, + const char *fname, int szhint) +{ + const char *e; + lua_pushvalue(L, idx); + do { + e = strchr(fname, '.'); + if (e == NULL) e = fname + strlen(fname); + lua_pushlstring(L, fname, (size_t)(e - fname)); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) { /* no such field? */ + lua_pop(L, 1); /* remove this nil */ + lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ + lua_pushlstring(L, fname, (size_t)(e - fname)); + lua_pushvalue(L, -2); + lua_settable(L, -4); /* set new table into field */ + } else if (!lua_istable(L, -1)) { /* field has a non-table value? */ + lua_pop(L, 2); /* remove table and value */ + return fname; /* return problematic part of the name */ + } + lua_remove(L, -2); /* remove previous table */ + fname = e + 1; + } while (*e == '.'); + return NULL; +} + +static int libsize(const luaL_Reg *l) +{ + int size = 0; + for (; l->name; l++) size++; + return size; +} + +LUALIB_API void luaL_openlib(lua_State *L, const char *libname, + const luaL_Reg *l, int nup) +{ + lj_lib_checkfpu(L); + if (libname) { + int size = libsize(l); + /* check whether lib already exists */ + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); + lua_getfield(L, -1, libname); /* get _LOADED[libname] */ + if (!lua_istable(L, -1)) { /* not found? */ + lua_pop(L, 1); /* remove previous result */ + /* try global variable (and create one if it does not exist) */ + if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) + lj_err_callerv(L, LJ_ERR_BADMODN, libname); + lua_pushvalue(L, -1); + lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ + } + lua_remove(L, -2); /* remove _LOADED table */ + lua_insert(L, -(nup+1)); /* move library table to below upvalues */ + } + for (; l->name; l++) { + int i; + for (i = 0; i < nup; i++) /* copy upvalues to the top */ + lua_pushvalue(L, -nup); + lua_pushcclosure(L, l->func, nup); + lua_setfield(L, -(nup+2), l->name); + } + lua_pop(L, nup); /* remove upvalues */ +} + +LUALIB_API void luaL_register(lua_State *L, const char *libname, + const luaL_Reg *l) +{ + luaL_openlib(L, libname, l, 0); +} + +LUALIB_API const char *luaL_gsub(lua_State *L, const char *s, + const char *p, const char *r) +{ + const char *wild; + size_t l = strlen(p); + luaL_Buffer b; + luaL_buffinit(L, &b); + while ((wild = strstr(s, p)) != NULL) { + luaL_addlstring(&b, s, (size_t)(wild - s)); /* push prefix */ + luaL_addstring(&b, r); /* push replacement in place of pattern */ + s = wild + l; /* continue after `p' */ + } + luaL_addstring(&b, s); /* push last suffix */ + luaL_pushresult(&b); + return lua_tostring(L, -1); +} + +/* -- Buffer handling ----------------------------------------------------- */ + +#define bufflen(B) ((size_t)((B)->p - (B)->buffer)) +#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B))) + +static int emptybuffer(luaL_Buffer *B) +{ + size_t l = bufflen(B); + if (l == 0) + return 0; /* put nothing on stack */ + lua_pushlstring(B->L, B->buffer, l); + B->p = B->buffer; + B->lvl++; + return 1; +} + +static void adjuststack(luaL_Buffer *B) +{ + if (B->lvl > 1) { + lua_State *L = B->L; + int toget = 1; /* number of levels to concat */ + size_t toplen = lua_strlen(L, -1); + do { + size_t l = lua_strlen(L, -(toget+1)); + if (!(B->lvl - toget + 1 >= LUA_MINSTACK/2 || toplen > l)) + break; + toplen += l; + toget++; + } while (toget < B->lvl); + lua_concat(L, toget); + B->lvl = B->lvl - toget + 1; + } +} + +LUALIB_API char *luaL_prepbuffer(luaL_Buffer *B) +{ + if (emptybuffer(B)) + adjuststack(B); + return B->buffer; +} + +LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l) +{ + while (l--) + luaL_addchar(B, *s++); +} + +LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s) +{ + luaL_addlstring(B, s, strlen(s)); +} + +LUALIB_API void luaL_pushresult(luaL_Buffer *B) +{ + emptybuffer(B); + lua_concat(B->L, B->lvl); + B->lvl = 1; +} + +LUALIB_API void luaL_addvalue(luaL_Buffer *B) +{ + lua_State *L = B->L; + size_t vl; + const char *s = lua_tolstring(L, -1, &vl); + if (vl <= bufffree(B)) { /* fit into buffer? */ + memcpy(B->p, s, vl); /* put it there */ + B->p += vl; + lua_pop(L, 1); /* remove from stack */ + } else { + if (emptybuffer(B)) + lua_insert(L, -2); /* put buffer before new value */ + B->lvl++; /* add new value into B stack */ + adjuststack(B); + } +} + +LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B) +{ + B->L = L; + B->p = B->buffer; + B->lvl = 0; +} + +/* -- Reference management ------------------------------------------------ */ + +#define FREELIST_REF 0 + +/* Convert a stack index to an absolute index. */ +#define abs_index(L, i) \ + ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1) + +LUALIB_API int luaL_ref(lua_State *L, int t) +{ + int ref; + t = abs_index(L, t); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); /* remove from stack */ + return LUA_REFNIL; /* `nil' has a unique fixed reference */ + } + lua_rawgeti(L, t, FREELIST_REF); /* get first free element */ + ref = (int)lua_tointeger(L, -1); /* ref = t[FREELIST_REF] */ + lua_pop(L, 1); /* remove it from stack */ + if (ref != 0) { /* any free element? */ + lua_rawgeti(L, t, ref); /* remove it from list */ + lua_rawseti(L, t, FREELIST_REF); /* (t[FREELIST_REF] = t[ref]) */ + } else { /* no free elements */ + ref = (int)lua_objlen(L, t); + ref++; /* create new reference */ + } + lua_rawseti(L, t, ref); + return ref; +} + +LUALIB_API void luaL_unref(lua_State *L, int t, int ref) +{ + if (ref >= 0) { + t = abs_index(L, t); + lua_rawgeti(L, t, FREELIST_REF); + lua_rawseti(L, t, ref); /* t[ref] = t[FREELIST_REF] */ + lua_pushinteger(L, ref); + lua_rawseti(L, t, FREELIST_REF); /* t[FREELIST_REF] = ref */ + } +} + +/* -- Default allocator and panic function -------------------------------- */ + +static int panic(lua_State *L) +{ + const char *s = lua_tostring(L, -1); + fputs("PANIC: unprotected error in call to Lua API (", stderr); + fputs(s ? s : "?", stderr); + fputc(')', stderr); fputc('\n', stderr); + fflush(stderr); + return 0; +} + +#ifdef LUAJIT_USE_SYSMALLOC + +#if LJ_64 +#error "Must use builtin allocator for 64 bit target" +#endif + +static void *mem_alloc(void *ud, void *ptr, size_t osize, size_t nsize) +{ + (void)ud; + (void)osize; + if (nsize == 0) { + free(ptr); + return NULL; + } else { + return realloc(ptr, nsize); + } +} + +LUALIB_API lua_State *luaL_newstate(void) +{ + lua_State *L = lua_newstate(mem_alloc, NULL); + if (L) G(L)->panic = panic; + return L; +} + +#else + +#include "lj_alloc.h" + +LUALIB_API lua_State *luaL_newstate(void) +{ + lua_State *L; + void *ud = lj_alloc_create(); + if (ud == NULL) return NULL; +#if LJ_64 + L = lj_state_newstate(lj_alloc_f, ud); +#else + L = lua_newstate(lj_alloc_f, ud); +#endif + if (L) G(L)->panic = panic; + return L; +} + +#if LJ_64 +LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) +{ + UNUSED(f); UNUSED(ud); + fputs("Must use luaL_newstate() for 64 bit target\n", stderr); + return NULL; +} +#endif + +#endif + diff --git a/external/lua/luajit/src/src/lib_base.c b/external/lua/luajit/src/src/lib_base.c new file mode 100644 index 0000000000..070970ed71 --- /dev/null +++ b/external/lua/luajit/src/src/lib_base.c @@ -0,0 +1,683 @@ +/* +** Base and coroutine library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#include + +#define lib_base_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_debug.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_meta.h" +#include "lj_state.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#include "lj_cconv.h" +#endif +#include "lj_bc.h" +#include "lj_ff.h" +#include "lj_dispatch.h" +#include "lj_char.h" +#include "lj_strscan.h" +#include "lj_lib.h" + +/* -- Base library: checks ------------------------------------------------ */ + +#define LJLIB_MODULE_base + +LJLIB_ASM(assert) LJLIB_REC(.) +{ + GCstr *s; + lj_lib_checkany(L, 1); + s = lj_lib_optstr(L, 2); + if (s) + lj_err_callermsg(L, strdata(s)); + else + lj_err_caller(L, LJ_ERR_ASSERT); + return FFH_UNREACHABLE; +} + +/* ORDER LJ_T */ +LJLIB_PUSH("nil") +LJLIB_PUSH("boolean") +LJLIB_PUSH(top-1) /* boolean */ +LJLIB_PUSH("userdata") +LJLIB_PUSH("string") +LJLIB_PUSH("upval") +LJLIB_PUSH("thread") +LJLIB_PUSH("proto") +LJLIB_PUSH("function") +LJLIB_PUSH("trace") +LJLIB_PUSH("cdata") +LJLIB_PUSH("table") +LJLIB_PUSH(top-9) /* userdata */ +LJLIB_PUSH("number") +LJLIB_ASM_(type) LJLIB_REC(.) +/* Recycle the lj_lib_checkany(L, 1) from assert. */ + +/* -- Base library: iterators --------------------------------------------- */ + +/* This solves a circular dependency problem -- change FF_next_N as needed. */ +LJ_STATIC_ASSERT((int)FF_next == FF_next_N); + +LJLIB_ASM(next) +{ + lj_lib_checktab(L, 1); + return FFH_UNREACHABLE; +} + +#if LJ_52 || LJ_HASFFI +static int ffh_pairs(lua_State *L, MMS mm) +{ + TValue *o = lj_lib_checkany(L, 1); + cTValue *mo = lj_meta_lookup(L, o, mm); + if ((LJ_52 || tviscdata(o)) && !tvisnil(mo)) { + L->top = o+1; /* Only keep one argument. */ + copyTV(L, L->base-1, mo); /* Replace callable. */ + return FFH_TAILCALL; + } else { + if (!tvistab(o)) lj_err_argt(L, 1, LUA_TTABLE); + setfuncV(L, o-1, funcV(lj_lib_upvalue(L, 1))); + if (mm == MM_pairs) setnilV(o+1); else setintV(o+1, 0); + return FFH_RES(3); + } +} +#else +#define ffh_pairs(L, mm) (lj_lib_checktab(L, 1), FFH_UNREACHABLE) +#endif + +LJLIB_PUSH(lastcl) +LJLIB_ASM(pairs) +{ + return ffh_pairs(L, MM_pairs); +} + +LJLIB_NOREGUV LJLIB_ASM(ipairs_aux) LJLIB_REC(.) +{ + lj_lib_checktab(L, 1); + lj_lib_checkint(L, 2); + return FFH_UNREACHABLE; +} + +LJLIB_PUSH(lastcl) +LJLIB_ASM(ipairs) LJLIB_REC(.) +{ + return ffh_pairs(L, MM_ipairs); +} + +/* -- Base library: getters and setters ----------------------------------- */ + +LJLIB_ASM_(getmetatable) LJLIB_REC(.) +/* Recycle the lj_lib_checkany(L, 1) from assert. */ + +LJLIB_ASM(setmetatable) LJLIB_REC(.) +{ + GCtab *t = lj_lib_checktab(L, 1); + GCtab *mt = lj_lib_checktabornil(L, 2); + if (!tvisnil(lj_meta_lookup(L, L->base, MM_metatable))) + lj_err_caller(L, LJ_ERR_PROTMT); + setgcref(t->metatable, obj2gco(mt)); + if (mt) { lj_gc_objbarriert(L, t, mt); } + settabV(L, L->base-1, t); + return FFH_RES(1); +} + +LJLIB_CF(getfenv) +{ + GCfunc *fn; + cTValue *o = L->base; + if (!(o < L->top && tvisfunc(o))) { + int level = lj_lib_optint(L, 1, 1); + o = lj_debug_frame(L, level, &level); + if (o == NULL) + lj_err_arg(L, 1, LJ_ERR_INVLVL); + } + fn = &gcval(o)->fn; + settabV(L, L->top++, isluafunc(fn) ? tabref(fn->l.env) : tabref(L->env)); + return 1; +} + +LJLIB_CF(setfenv) +{ + GCfunc *fn; + GCtab *t = lj_lib_checktab(L, 2); + cTValue *o = L->base; + if (!(o < L->top && tvisfunc(o))) { + int level = lj_lib_checkint(L, 1); + if (level == 0) { + /* NOBARRIER: A thread (i.e. L) is never black. */ + setgcref(L->env, obj2gco(t)); + return 0; + } + o = lj_debug_frame(L, level, &level); + if (o == NULL) + lj_err_arg(L, 1, LJ_ERR_INVLVL); + } + fn = &gcval(o)->fn; + if (!isluafunc(fn)) + lj_err_caller(L, LJ_ERR_SETFENV); + setgcref(fn->l.env, obj2gco(t)); + lj_gc_objbarrier(L, obj2gco(fn), t); + setfuncV(L, L->top++, fn); + return 1; +} + +LJLIB_ASM(rawget) LJLIB_REC(.) +{ + lj_lib_checktab(L, 1); + lj_lib_checkany(L, 2); + return FFH_UNREACHABLE; +} + +LJLIB_CF(rawset) LJLIB_REC(.) +{ + lj_lib_checktab(L, 1); + lj_lib_checkany(L, 2); + L->top = 1+lj_lib_checkany(L, 3); + lua_rawset(L, 1); + return 1; +} + +LJLIB_CF(rawequal) LJLIB_REC(.) +{ + cTValue *o1 = lj_lib_checkany(L, 1); + cTValue *o2 = lj_lib_checkany(L, 2); + setboolV(L->top-1, lj_obj_equal(o1, o2)); + return 1; +} + +#if LJ_52 +LJLIB_CF(rawlen) LJLIB_REC(.) +{ + cTValue *o = L->base; + int32_t len; + if (L->top > o && tvisstr(o)) + len = (int32_t)strV(o)->len; + else + len = (int32_t)lj_tab_len(lj_lib_checktab(L, 1)); + setintV(L->top-1, len); + return 1; +} +#endif + +LJLIB_CF(unpack) +{ + GCtab *t = lj_lib_checktab(L, 1); + int32_t n, i = lj_lib_optint(L, 2, 1); + int32_t e = (L->base+3-1 < L->top && !tvisnil(L->base+3-1)) ? + lj_lib_checkint(L, 3) : (int32_t)lj_tab_len(t); + if (i > e) return 0; + n = e - i + 1; + if (n <= 0 || !lua_checkstack(L, n)) + lj_err_caller(L, LJ_ERR_UNPACK); + do { + cTValue *tv = lj_tab_getint(t, i); + if (tv) { + copyTV(L, L->top++, tv); + } else { + setnilV(L->top++); + } + } while (i++ < e); + return n; +} + +LJLIB_CF(select) LJLIB_REC(.) +{ + int32_t n = (int32_t)(L->top - L->base); + if (n >= 1 && tvisstr(L->base) && *strVdata(L->base) == '#') { + setintV(L->top-1, n-1); + return 1; + } else { + int32_t i = lj_lib_checkint(L, 1); + if (i < 0) i = n + i; else if (i > n) i = n; + if (i < 1) + lj_err_arg(L, 1, LJ_ERR_IDXRNG); + return n - i; + } +} + +/* -- Base library: conversions ------------------------------------------- */ + +LJLIB_ASM(tonumber) LJLIB_REC(.) +{ + int32_t base = lj_lib_optint(L, 2, 10); + if (base == 10) { + TValue *o = lj_lib_checkany(L, 1); + if (lj_strscan_numberobj(o)) { + copyTV(L, L->base-1, o); + return FFH_RES(1); + } +#if LJ_HASFFI + if (tviscdata(o)) { + CTState *cts = ctype_cts(L); + CType *ct = lj_ctype_rawref(cts, cdataV(o)->ctypeid); + if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); + if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) { + if (LJ_DUALNUM && ctype_isinteger_or_bool(ct->info) && + ct->size <= 4 && !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) { + int32_t i; + lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, 0); + setintV(L->base-1, i); + return FFH_RES(1); + } + lj_cconv_ct_tv(cts, ctype_get(cts, CTID_DOUBLE), + (uint8_t *)&(L->base-1)->n, o, 0); + return FFH_RES(1); + } + } +#endif + } else { + const char *p = strdata(lj_lib_checkstr(L, 1)); + char *ep; + unsigned long ul; + if (base < 2 || base > 36) + lj_err_arg(L, 2, LJ_ERR_BASERNG); + ul = strtoul(p, &ep, base); + if (p != ep) { + while (lj_char_isspace((unsigned char)(*ep))) ep++; + if (*ep == '\0') { + if (LJ_DUALNUM && LJ_LIKELY(ul < 0x80000000u)) + setintV(L->base-1, (int32_t)ul); + else + setnumV(L->base-1, (lua_Number)ul); + return FFH_RES(1); + } + } + } + setnilV(L->base-1); + return FFH_RES(1); +} + +LJLIB_PUSH("nil") +LJLIB_PUSH("false") +LJLIB_PUSH("true") +LJLIB_ASM(tostring) LJLIB_REC(.) +{ + TValue *o = lj_lib_checkany(L, 1); + cTValue *mo; + L->top = o+1; /* Only keep one argument. */ + if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) { + copyTV(L, L->base-1, mo); /* Replace callable. */ + return FFH_TAILCALL; + } else { + GCstr *s; + if (tvisnumber(o)) { + s = lj_str_fromnumber(L, o); + } else if (tvispri(o)) { + s = strV(lj_lib_upvalue(L, -(int32_t)itype(o))); + } else { + if (tvisfunc(o) && isffunc(funcV(o))) + lua_pushfstring(L, "function: builtin#%d", funcV(o)->c.ffid); + else + lua_pushfstring(L, "%s: %p", lj_typename(o), lua_topointer(L, 1)); + /* Note: lua_pushfstring calls the GC which may invalidate o. */ + s = strV(L->top-1); + } + setstrV(L, L->base-1, s); + return FFH_RES(1); + } +} + +/* -- Base library: throw and catch errors -------------------------------- */ + +LJLIB_CF(error) +{ + int32_t level = lj_lib_optint(L, 2, 1); + lua_settop(L, 1); + if (lua_isstring(L, 1) && level > 0) { + luaL_where(L, level); + lua_pushvalue(L, 1); + lua_concat(L, 2); + } + return lua_error(L); +} + +LJLIB_ASM(pcall) LJLIB_REC(.) +{ + lj_lib_checkany(L, 1); + lj_lib_checkfunc(L, 2); /* For xpcall only. */ + return FFH_UNREACHABLE; +} +LJLIB_ASM_(xpcall) LJLIB_REC(.) + +/* -- Base library: load Lua code ----------------------------------------- */ + +static int load_aux(lua_State *L, int status, int envarg) +{ + if (status == 0) { + if (tvistab(L->base+envarg-1)) { + GCfunc *fn = funcV(L->top-1); + GCtab *t = tabV(L->base+envarg-1); + setgcref(fn->c.env, obj2gco(t)); + lj_gc_objbarrier(L, fn, t); + } + return 1; + } else { + setnilV(L->top-2); + return 2; + } +} + +LJLIB_CF(loadfile) +{ + GCstr *fname = lj_lib_optstr(L, 1); + GCstr *mode = lj_lib_optstr(L, 2); + int status; + lua_settop(L, 3); /* Ensure env arg exists. */ + status = luaL_loadfilex(L, fname ? strdata(fname) : NULL, + mode ? strdata(mode) : NULL); + return load_aux(L, status, 3); +} + +static const char *reader_func(lua_State *L, void *ud, size_t *size) +{ + UNUSED(ud); + luaL_checkstack(L, 2, "too many nested functions"); + copyTV(L, L->top++, L->base); + lua_call(L, 0, 1); /* Call user-supplied function. */ + L->top--; + if (tvisnil(L->top)) { + *size = 0; + return NULL; + } else if (tvisstr(L->top) || tvisnumber(L->top)) { + copyTV(L, L->base+4, L->top); /* Anchor string in reserved stack slot. */ + return lua_tolstring(L, 5, size); + } else { + lj_err_caller(L, LJ_ERR_RDRSTR); + return NULL; + } +} + +LJLIB_CF(load) +{ + GCstr *name = lj_lib_optstr(L, 2); + GCstr *mode = lj_lib_optstr(L, 3); + int status; + if (L->base < L->top && (tvisstr(L->base) || tvisnumber(L->base))) { + GCstr *s = lj_lib_checkstr(L, 1); + lua_settop(L, 4); /* Ensure env arg exists. */ + status = luaL_loadbufferx(L, strdata(s), s->len, strdata(name ? name : s), + mode ? strdata(mode) : NULL); + } else { + lj_lib_checkfunc(L, 1); + lua_settop(L, 5); /* Reserve a slot for the string from the reader. */ + status = lua_loadx(L, reader_func, NULL, name ? strdata(name) : "=(load)", + mode ? strdata(mode) : NULL); + } + return load_aux(L, status, 4); +} + +LJLIB_CF(loadstring) +{ + return lj_cf_load(L); +} + +LJLIB_CF(dofile) +{ + GCstr *fname = lj_lib_optstr(L, 1); + setnilV(L->top); + L->top = L->base+1; + if (luaL_loadfile(L, fname ? strdata(fname) : NULL) != 0) + lua_error(L); + lua_call(L, 0, LUA_MULTRET); + return (int)(L->top - L->base) - 1; +} + +/* -- Base library: GC control -------------------------------------------- */ + +LJLIB_CF(gcinfo) +{ + setintV(L->top++, (G(L)->gc.total >> 10)); + return 1; +} + +LJLIB_CF(collectgarbage) +{ + int opt = lj_lib_checkopt(L, 1, LUA_GCCOLLECT, /* ORDER LUA_GC* */ + "\4stop\7restart\7collect\5count\1\377\4step\10setpause\12setstepmul"); + int32_t data = lj_lib_optint(L, 2, 0); + if (opt == LUA_GCCOUNT) { + setnumV(L->top, (lua_Number)G(L)->gc.total/1024.0); + } else { + int res = lua_gc(L, opt, data); + if (opt == LUA_GCSTEP) + setboolV(L->top, res); + else + setintV(L->top, res); + } + L->top++; + return 1; +} + +/* -- Base library: miscellaneous functions ------------------------------- */ + +LJLIB_PUSH(top-2) /* Upvalue holds weak table. */ +LJLIB_CF(newproxy) +{ + lua_settop(L, 1); + lua_newuserdata(L, 0); + if (lua_toboolean(L, 1) == 0) { /* newproxy(): without metatable. */ + return 1; + } else if (lua_isboolean(L, 1)) { /* newproxy(true): with metatable. */ + lua_newtable(L); + lua_pushvalue(L, -1); + lua_pushboolean(L, 1); + lua_rawset(L, lua_upvalueindex(1)); /* Remember mt in weak table. */ + } else { /* newproxy(proxy): inherit metatable. */ + int validproxy = 0; + if (lua_getmetatable(L, 1)) { + lua_rawget(L, lua_upvalueindex(1)); + validproxy = lua_toboolean(L, -1); + lua_pop(L, 1); + } + if (!validproxy) + lj_err_arg(L, 1, LJ_ERR_NOPROXY); + lua_getmetatable(L, 1); + } + lua_setmetatable(L, 2); + return 1; +} + +LJLIB_PUSH("tostring") +LJLIB_CF(print) +{ + ptrdiff_t i, nargs = L->top - L->base; + cTValue *tv = lj_tab_getstr(tabref(L->env), strV(lj_lib_upvalue(L, 1))); + int shortcut; + if (tv && !tvisnil(tv)) { + copyTV(L, L->top++, tv); + } else { + setstrV(L, L->top++, strV(lj_lib_upvalue(L, 1))); + lua_gettable(L, LUA_GLOBALSINDEX); + tv = L->top-1; + } + shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring); + for (i = 0; i < nargs; i++) { + const char *str; + size_t size; + cTValue *o = &L->base[i]; + if (shortcut && tvisstr(o)) { + str = strVdata(o); + size = strV(o)->len; + } else if (shortcut && tvisint(o)) { + char buf[LJ_STR_INTBUF]; + char *p = lj_str_bufint(buf, intV(o)); + size = (size_t)(buf+LJ_STR_INTBUF-p); + str = p; + } else if (shortcut && tvisnum(o)) { + char buf[LJ_STR_NUMBUF]; + size = lj_str_bufnum(buf, o); + str = buf; + } else { + copyTV(L, L->top+1, o); + copyTV(L, L->top, L->top-1); + L->top += 2; + lua_call(L, 1, 1); + str = lua_tolstring(L, -1, &size); + if (!str) + lj_err_caller(L, LJ_ERR_PRTOSTR); + L->top--; + } + if (i) + putchar('\t'); + fwrite(str, 1, size, stdout); + } + putchar('\n'); + return 0; +} + +LJLIB_PUSH(top-3) +LJLIB_SET(_VERSION) + +#include "lj_libdef.h" + +/* -- Coroutine library --------------------------------------------------- */ + +#define LJLIB_MODULE_coroutine + +LJLIB_CF(coroutine_status) +{ + const char *s; + lua_State *co; + if (!(L->top > L->base && tvisthread(L->base))) + lj_err_arg(L, 1, LJ_ERR_NOCORO); + co = threadV(L->base); + if (co == L) s = "running"; + else if (co->status == LUA_YIELD) s = "suspended"; + else if (co->status != 0) s = "dead"; + else if (co->base > tvref(co->stack)+1) s = "normal"; + else if (co->top == co->base) s = "dead"; + else s = "suspended"; + lua_pushstring(L, s); + return 1; +} + +LJLIB_CF(coroutine_running) +{ +#if LJ_52 + int ismain = lua_pushthread(L); + setboolV(L->top++, ismain); + return 2; +#else + if (lua_pushthread(L)) + setnilV(L->top++); + return 1; +#endif +} + +LJLIB_CF(coroutine_create) +{ + lua_State *L1; + if (!(L->base < L->top && tvisfunc(L->base))) + lj_err_argt(L, 1, LUA_TFUNCTION); + L1 = lua_newthread(L); + setfuncV(L, L1->top++, funcV(L->base)); + return 1; +} + +LJLIB_ASM(coroutine_yield) +{ + lj_err_caller(L, LJ_ERR_CYIELD); + return FFH_UNREACHABLE; +} + +static int ffh_resume(lua_State *L, lua_State *co, int wrap) +{ + if (co->cframe != NULL || co->status > LUA_YIELD || + (co->status == 0 && co->top == co->base)) { + ErrMsg em = co->cframe ? LJ_ERR_CORUN : LJ_ERR_CODEAD; + if (wrap) lj_err_caller(L, em); + setboolV(L->base-1, 0); + setstrV(L, L->base, lj_err_str(L, em)); + return FFH_RES(2); + } + lj_state_growstack(co, (MSize)(L->top - L->base)); + return FFH_RETRY; +} + +LJLIB_ASM(coroutine_resume) +{ + if (!(L->top > L->base && tvisthread(L->base))) + lj_err_arg(L, 1, LJ_ERR_NOCORO); + return ffh_resume(L, threadV(L->base), 0); +} + +LJLIB_NOREG LJLIB_ASM(coroutine_wrap_aux) +{ + return ffh_resume(L, threadV(lj_lib_upvalue(L, 1)), 1); +} + +/* Inline declarations. */ +LJ_ASMF void lj_ff_coroutine_wrap_aux(void); +#if !(LJ_TARGET_MIPS && defined(ljamalg_c)) +LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, + lua_State *co); +#endif + +/* Error handler, called from assembler VM. */ +void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, lua_State *co) +{ + co->top--; copyTV(L, L->top, co->top); L->top++; + if (tvisstr(L->top-1)) + lj_err_callermsg(L, strVdata(L->top-1)); + else + lj_err_run(L); +} + +/* Forward declaration. */ +static void setpc_wrap_aux(lua_State *L, GCfunc *fn); + +LJLIB_CF(coroutine_wrap) +{ + lj_cf_coroutine_create(L); + lj_lib_pushcc(L, lj_ffh_coroutine_wrap_aux, FF_coroutine_wrap_aux, 1); + setpc_wrap_aux(L, funcV(L->top-1)); + return 1; +} + +#include "lj_libdef.h" + +/* Fix the PC of wrap_aux. Really ugly workaround. */ +static void setpc_wrap_aux(lua_State *L, GCfunc *fn) +{ + setmref(fn->c.pc, &L2GG(L)->bcff[lj_lib_init_coroutine[1]+2]); +} + +/* ------------------------------------------------------------------------ */ + +static void newproxy_weaktable(lua_State *L) +{ + /* NOBARRIER: The table is new (marked white). */ + GCtab *t = lj_tab_new(L, 0, 1); + settabV(L, L->top++, t); + setgcref(t->metatable, obj2gco(t)); + setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")), + lj_str_newlit(L, "kv")); + t->nomm = (uint8_t)(~(1u<env); + settabV(L, lj_tab_setstr(L, env, lj_str_newlit(L, "_G")), env); + lua_pushliteral(L, LUA_VERSION); /* top-3. */ + newproxy_weaktable(L); /* top-2. */ + LJ_LIB_REG(L, "_G", base); + LJ_LIB_REG(L, LUA_COLIBNAME, coroutine); + return 2; +} + diff --git a/external/lua/luajit/src/src/lib_bit.c b/external/lua/luajit/src/src/lib_bit.c new file mode 100644 index 0000000000..93fead9232 --- /dev/null +++ b/external/lua/luajit/src/src/lib_bit.c @@ -0,0 +1,74 @@ +/* +** Bit manipulation library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lib_bit_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_obj.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_lib.h" + +/* ------------------------------------------------------------------------ */ + +#define LJLIB_MODULE_bit + +LJLIB_ASM(bit_tobit) LJLIB_REC(bit_unary IR_TOBIT) +{ + lj_lib_checknumber(L, 1); + return FFH_RETRY; +} +LJLIB_ASM_(bit_bnot) LJLIB_REC(bit_unary IR_BNOT) +LJLIB_ASM_(bit_bswap) LJLIB_REC(bit_unary IR_BSWAP) + +LJLIB_ASM(bit_lshift) LJLIB_REC(bit_shift IR_BSHL) +{ + lj_lib_checknumber(L, 1); + lj_lib_checkbit(L, 2); + return FFH_RETRY; +} +LJLIB_ASM_(bit_rshift) LJLIB_REC(bit_shift IR_BSHR) +LJLIB_ASM_(bit_arshift) LJLIB_REC(bit_shift IR_BSAR) +LJLIB_ASM_(bit_rol) LJLIB_REC(bit_shift IR_BROL) +LJLIB_ASM_(bit_ror) LJLIB_REC(bit_shift IR_BROR) + +LJLIB_ASM(bit_band) LJLIB_REC(bit_nary IR_BAND) +{ + int i = 0; + do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top); + return FFH_RETRY; +} +LJLIB_ASM_(bit_bor) LJLIB_REC(bit_nary IR_BOR) +LJLIB_ASM_(bit_bxor) LJLIB_REC(bit_nary IR_BXOR) + +/* ------------------------------------------------------------------------ */ + +LJLIB_CF(bit_tohex) +{ + uint32_t b = (uint32_t)lj_lib_checkbit(L, 1); + int32_t i, n = L->base+1 >= L->top ? 8 : lj_lib_checkbit(L, 2); + const char *hexdigits = "0123456789abcdef"; + char buf[8]; + if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; } + if (n > 8) n = 8; + for (i = n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; } + lua_pushlstring(L, buf, (size_t)n); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +#include "lj_libdef.h" + +LUALIB_API int luaopen_bit(lua_State *L) +{ + LJ_LIB_REG(L, LUA_BITLIBNAME, bit); + return 1; +} + diff --git a/external/lua/luajit/src/src/lib_debug.c b/external/lua/luajit/src/src/lib_debug.c new file mode 100644 index 0000000000..38e6054367 --- /dev/null +++ b/external/lua/luajit/src/src/lib_debug.c @@ -0,0 +1,405 @@ +/* +** Debug library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lib_debug_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_debug.h" +#include "lj_lib.h" + +/* ------------------------------------------------------------------------ */ + +#define LJLIB_MODULE_debug + +LJLIB_CF(debug_getregistry) +{ + copyTV(L, L->top++, registry(L)); + return 1; +} + +LJLIB_CF(debug_getmetatable) +{ + lj_lib_checkany(L, 1); + if (!lua_getmetatable(L, 1)) { + setnilV(L->top-1); + } + return 1; +} + +LJLIB_CF(debug_setmetatable) +{ + lj_lib_checktabornil(L, 2); + L->top = L->base+2; + lua_setmetatable(L, 1); +#if !LJ_52 + setboolV(L->top-1, 1); +#endif + return 1; +} + +LJLIB_CF(debug_getfenv) +{ + lj_lib_checkany(L, 1); + lua_getfenv(L, 1); + return 1; +} + +LJLIB_CF(debug_setfenv) +{ + lj_lib_checktab(L, 2); + L->top = L->base+2; + if (!lua_setfenv(L, 1)) + lj_err_caller(L, LJ_ERR_SETFENV); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +static void settabss(lua_State *L, const char *i, const char *v) +{ + lua_pushstring(L, v); + lua_setfield(L, -2, i); +} + +static void settabsi(lua_State *L, const char *i, int v) +{ + lua_pushinteger(L, v); + lua_setfield(L, -2, i); +} + +static void settabsb(lua_State *L, const char *i, int v) +{ + lua_pushboolean(L, v); + lua_setfield(L, -2, i); +} + +static lua_State *getthread(lua_State *L, int *arg) +{ + if (L->base < L->top && tvisthread(L->base)) { + *arg = 1; + return threadV(L->base); + } else { + *arg = 0; + return L; + } +} + +static void treatstackoption(lua_State *L, lua_State *L1, const char *fname) +{ + if (L == L1) { + lua_pushvalue(L, -2); + lua_remove(L, -3); + } + else + lua_xmove(L1, L, 1); + lua_setfield(L, -2, fname); +} + +LJLIB_CF(debug_getinfo) +{ + lj_Debug ar; + int arg, opt_f = 0, opt_L = 0; + lua_State *L1 = getthread(L, &arg); + const char *options = luaL_optstring(L, arg+2, "flnSu"); + if (lua_isnumber(L, arg+1)) { + if (!lua_getstack(L1, (int)lua_tointeger(L, arg+1), (lua_Debug *)&ar)) { + setnilV(L->top-1); + return 1; + } + } else if (L->base+arg < L->top && tvisfunc(L->base+arg)) { + options = lua_pushfstring(L, ">%s", options); + setfuncV(L1, L1->top++, funcV(L->base+arg)); + } else { + lj_err_arg(L, arg+1, LJ_ERR_NOFUNCL); + } + if (!lj_debug_getinfo(L1, options, &ar, 1)) + lj_err_arg(L, arg+2, LJ_ERR_INVOPT); + lua_createtable(L, 0, 16); /* Create result table. */ + for (; *options; options++) { + switch (*options) { + case 'S': + settabss(L, "source", ar.source); + settabss(L, "short_src", ar.short_src); + settabsi(L, "linedefined", ar.linedefined); + settabsi(L, "lastlinedefined", ar.lastlinedefined); + settabss(L, "what", ar.what); + break; + case 'l': + settabsi(L, "currentline", ar.currentline); + break; + case 'u': + settabsi(L, "nups", ar.nups); + settabsi(L, "nparams", ar.nparams); + settabsb(L, "isvararg", ar.isvararg); + break; + case 'n': + settabss(L, "name", ar.name); + settabss(L, "namewhat", ar.namewhat); + break; + case 'f': opt_f = 1; break; + case 'L': opt_L = 1; break; + default: break; + } + } + if (opt_L) treatstackoption(L, L1, "activelines"); + if (opt_f) treatstackoption(L, L1, "func"); + return 1; /* Return result table. */ +} + +LJLIB_CF(debug_getlocal) +{ + int arg; + lua_State *L1 = getthread(L, &arg); + lua_Debug ar; + const char *name; + int slot = lj_lib_checkint(L, arg+2); + if (tvisfunc(L->base+arg)) { + L->top = L->base+arg+1; + lua_pushstring(L, lua_getlocal(L, NULL, slot)); + return 1; + } + if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar)) + lj_err_arg(L, arg+1, LJ_ERR_LVLRNG); + name = lua_getlocal(L1, &ar, slot); + if (name) { + lua_xmove(L1, L, 1); + lua_pushstring(L, name); + lua_pushvalue(L, -2); + return 2; + } else { + setnilV(L->top-1); + return 1; + } +} + +LJLIB_CF(debug_setlocal) +{ + int arg; + lua_State *L1 = getthread(L, &arg); + lua_Debug ar; + TValue *tv; + if (!lua_getstack(L1, lj_lib_checkint(L, arg+1), &ar)) + lj_err_arg(L, arg+1, LJ_ERR_LVLRNG); + tv = lj_lib_checkany(L, arg+3); + copyTV(L1, L1->top++, tv); + lua_pushstring(L, lua_setlocal(L1, &ar, lj_lib_checkint(L, arg+2))); + return 1; +} + +static int debug_getupvalue(lua_State *L, int get) +{ + int32_t n = lj_lib_checkint(L, 2); + const char *name; + lj_lib_checkfunc(L, 1); + name = get ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n); + if (name) { + lua_pushstring(L, name); + if (!get) return 1; + copyTV(L, L->top, L->top-2); + L->top++; + return 2; + } + return 0; +} + +LJLIB_CF(debug_getupvalue) +{ + return debug_getupvalue(L, 1); +} + +LJLIB_CF(debug_setupvalue) +{ + lj_lib_checkany(L, 3); + return debug_getupvalue(L, 0); +} + +LJLIB_CF(debug_upvalueid) +{ + GCfunc *fn = lj_lib_checkfunc(L, 1); + int32_t n = lj_lib_checkint(L, 2) - 1; + if ((uint32_t)n >= fn->l.nupvalues) + lj_err_arg(L, 2, LJ_ERR_IDXRNG); + setlightudV(L->top-1, isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) : + (void *)&fn->c.upvalue[n]); + return 1; +} + +LJLIB_CF(debug_upvaluejoin) +{ + GCfunc *fn[2]; + GCRef *p[2]; + int i; + for (i = 0; i < 2; i++) { + int32_t n; + fn[i] = lj_lib_checkfunc(L, 2*i+1); + if (!isluafunc(fn[i])) + lj_err_arg(L, 2*i+1, LJ_ERR_NOLFUNC); + n = lj_lib_checkint(L, 2*i+2) - 1; + if ((uint32_t)n >= fn[i]->l.nupvalues) + lj_err_arg(L, 2*i+2, LJ_ERR_IDXRNG); + p[i] = &fn[i]->l.uvptr[n]; + } + setgcrefr(*p[0], *p[1]); + lj_gc_objbarrier(L, fn[0], gcref(*p[1])); + return 0; +} + +#if LJ_52 +LJLIB_CF(debug_getuservalue) +{ + TValue *o = L->base; + if (o < L->top && tvisudata(o)) + settabV(L, o, tabref(udataV(o)->env)); + else + setnilV(o); + L->top = o+1; + return 1; +} + +LJLIB_CF(debug_setuservalue) +{ + TValue *o = L->base; + if (!(o < L->top && tvisudata(o))) + lj_err_argt(L, 1, LUA_TUSERDATA); + if (!(o+1 < L->top && tvistab(o+1))) + lj_err_argt(L, 2, LUA_TTABLE); + L->top = o+2; + lua_setfenv(L, 1); + return 1; +} +#endif + +/* ------------------------------------------------------------------------ */ + +static const char KEY_HOOK = 'h'; + +static void hookf(lua_State *L, lua_Debug *ar) +{ + static const char *const hooknames[] = + {"call", "return", "line", "count", "tail return"}; + lua_pushlightuserdata(L, (void *)&KEY_HOOK); + lua_rawget(L, LUA_REGISTRYINDEX); + if (lua_isfunction(L, -1)) { + lua_pushstring(L, hooknames[(int)ar->event]); + if (ar->currentline >= 0) + lua_pushinteger(L, ar->currentline); + else lua_pushnil(L); + lua_call(L, 2, 0); + } +} + +static int makemask(const char *smask, int count) +{ + int mask = 0; + if (strchr(smask, 'c')) mask |= LUA_MASKCALL; + if (strchr(smask, 'r')) mask |= LUA_MASKRET; + if (strchr(smask, 'l')) mask |= LUA_MASKLINE; + if (count > 0) mask |= LUA_MASKCOUNT; + return mask; +} + +static char *unmakemask(int mask, char *smask) +{ + int i = 0; + if (mask & LUA_MASKCALL) smask[i++] = 'c'; + if (mask & LUA_MASKRET) smask[i++] = 'r'; + if (mask & LUA_MASKLINE) smask[i++] = 'l'; + smask[i] = '\0'; + return smask; +} + +LJLIB_CF(debug_sethook) +{ + int arg, mask, count; + lua_Hook func; + (void)getthread(L, &arg); + if (lua_isnoneornil(L, arg+1)) { + lua_settop(L, arg+1); + func = NULL; mask = 0; count = 0; /* turn off hooks */ + } else { + const char *smask = luaL_checkstring(L, arg+2); + luaL_checktype(L, arg+1, LUA_TFUNCTION); + count = luaL_optint(L, arg+3, 0); + func = hookf; mask = makemask(smask, count); + } + lua_pushlightuserdata(L, (void *)&KEY_HOOK); + lua_pushvalue(L, arg+1); + lua_rawset(L, LUA_REGISTRYINDEX); + lua_sethook(L, func, mask, count); + return 0; +} + +LJLIB_CF(debug_gethook) +{ + char buff[5]; + int mask = lua_gethookmask(L); + lua_Hook hook = lua_gethook(L); + if (hook != NULL && hook != hookf) { /* external hook? */ + lua_pushliteral(L, "external hook"); + } else { + lua_pushlightuserdata(L, (void *)&KEY_HOOK); + lua_rawget(L, LUA_REGISTRYINDEX); /* get hook */ + } + lua_pushstring(L, unmakemask(mask, buff)); + lua_pushinteger(L, lua_gethookcount(L)); + return 3; +} + +/* ------------------------------------------------------------------------ */ + +LJLIB_CF(debug_debug) +{ + for (;;) { + char buffer[250]; + fputs("lua_debug> ", stderr); + if (fgets(buffer, sizeof(buffer), stdin) == 0 || + strcmp(buffer, "cont\n") == 0) + return 0; + if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") || + lua_pcall(L, 0, 0, 0)) { + fputs(lua_tostring(L, -1), stderr); + fputs("\n", stderr); + } + lua_settop(L, 0); /* remove eventual returns */ + } +} + +/* ------------------------------------------------------------------------ */ + +#define LEVELS1 12 /* size of the first part of the stack */ +#define LEVELS2 10 /* size of the second part of the stack */ + +LJLIB_CF(debug_traceback) +{ + int arg; + lua_State *L1 = getthread(L, &arg); + const char *msg = lua_tostring(L, arg+1); + if (msg == NULL && L->top > L->base+arg) + L->top = L->base+arg+1; + else + luaL_traceback(L, L1, msg, lj_lib_optint(L, arg+2, (L == L1))); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +#include "lj_libdef.h" + +LUALIB_API int luaopen_debug(lua_State *L) +{ + LJ_LIB_REG(L, LUA_DBLIBNAME, debug); + return 1; +} + diff --git a/external/lua/luajit/src/src/lib_ffi.c b/external/lua/luajit/src/src/lib_ffi.c new file mode 100644 index 0000000000..46f27e0135 --- /dev/null +++ b/external/lua/luajit/src/src/lib_ffi.c @@ -0,0 +1,850 @@ +/* +** FFI library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lib_ffi_c +#define LUA_LIB + +#include + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_obj.h" + +#if LJ_HASFFI + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_meta.h" +#include "lj_ctype.h" +#include "lj_cparse.h" +#include "lj_cdata.h" +#include "lj_cconv.h" +#include "lj_carith.h" +#include "lj_ccall.h" +#include "lj_ccallback.h" +#include "lj_clib.h" +#include "lj_ff.h" +#include "lj_lib.h" + +/* -- C type checks ------------------------------------------------------- */ + +/* Check first argument for a C type and returns its ID. */ +static CTypeID ffi_checkctype(lua_State *L, CTState *cts, TValue *param) +{ + TValue *o = L->base; + if (!(o < L->top)) { + err_argtype: + lj_err_argtype(L, 1, "C type"); + } + if (tvisstr(o)) { /* Parse an abstract C type declaration. */ + GCstr *s = strV(o); + CPState cp; + int errcode; + cp.L = L; + cp.cts = cts; + cp.srcname = strdata(s); + cp.p = strdata(s); + cp.param = param; + cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT; + errcode = lj_cparse(&cp); + if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ + return cp.val.id; + } else { + GCcdata *cd; + if (!tviscdata(o)) goto err_argtype; + if (param && param < L->top) lj_err_arg(L, 1, LJ_ERR_FFI_NUMPARAM); + cd = cdataV(o); + return cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : cd->ctypeid; + } +} + +/* Check argument for C data and return it. */ +static GCcdata *ffi_checkcdata(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (!(o < L->top && tviscdata(o))) + lj_err_argt(L, narg, LUA_TCDATA); + return cdataV(o); +} + +/* Convert argument to C pointer. */ +static void *ffi_checkptr(lua_State *L, int narg, CTypeID id) +{ + CTState *cts = ctype_cts(L); + TValue *o = L->base + narg-1; + void *p; + if (o >= L->top) + lj_err_arg(L, narg, LJ_ERR_NOVAL); + lj_cconv_ct_tv(cts, ctype_get(cts, id), (uint8_t *)&p, o, CCF_ARG(narg)); + return p; +} + +/* Convert argument to int32_t. */ +static int32_t ffi_checkint(lua_State *L, int narg) +{ + CTState *cts = ctype_cts(L); + TValue *o = L->base + narg-1; + int32_t i; + if (o >= L->top) + lj_err_arg(L, narg, LJ_ERR_NOVAL); + lj_cconv_ct_tv(cts, ctype_get(cts, CTID_INT32), (uint8_t *)&i, o, + CCF_ARG(narg)); + return i; +} + +/* -- C type metamethods -------------------------------------------------- */ + +#define LJLIB_MODULE_ffi_meta + +/* Handle ctype __index/__newindex metamethods. */ +static int ffi_index_meta(lua_State *L, CTState *cts, CType *ct, MMS mm) +{ + CTypeID id = ctype_typeid(cts, ct); + cTValue *tv = lj_ctype_meta(cts, id, mm); + TValue *base = L->base; + if (!tv) { + const char *s; + err_index: + s = strdata(lj_ctype_repr(L, id, NULL)); + if (tvisstr(L->base+1)) { + lj_err_callerv(L, LJ_ERR_FFI_BADMEMBER, s, strVdata(L->base+1)); + } else { + const char *key = tviscdata(L->base+1) ? + strdata(lj_ctype_repr(L, cdataV(L->base+1)->ctypeid, NULL)) : + lj_typename(L->base+1); + lj_err_callerv(L, LJ_ERR_FFI_BADIDXW, s, key); + } + } + if (!tvisfunc(tv)) { + if (mm == MM_index) { + cTValue *o = lj_meta_tget(L, tv, base+1); + if (o) { + if (tvisnil(o)) goto err_index; + copyTV(L, L->top-1, o); + return 1; + } + } else { + TValue *o = lj_meta_tset(L, tv, base+1); + if (o) { + copyTV(L, o, base+2); + return 0; + } + } + tv = L->top-1; + } + return lj_meta_tailcall(L, tv); +} + +LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0) +{ + CTState *cts = ctype_cts(L); + CTInfo qual = 0; + CType *ct; + uint8_t *p; + TValue *o = L->base; + if (!(o+1 < L->top && tviscdata(o))) /* Also checks for presence of key. */ + lj_err_argt(L, 1, LUA_TCDATA); + ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual); + if ((qual & 1)) + return ffi_index_meta(L, cts, ct, MM_index); + if (lj_cdata_get(cts, ct, L->top-1, p)) + lj_gc_check(L); + return 1; +} + +LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1) +{ + CTState *cts = ctype_cts(L); + CTInfo qual = 0; + CType *ct; + uint8_t *p; + TValue *o = L->base; + if (!(o+2 < L->top && tviscdata(o))) /* Also checks for key and value. */ + lj_err_argt(L, 1, LUA_TCDATA); + ct = lj_cdata_index(cts, cdataV(o), o+1, &p, &qual); + if ((qual & 1)) { + if ((qual & CTF_CONST)) + lj_err_caller(L, LJ_ERR_FFI_WRCONST); + return ffi_index_meta(L, cts, ct, MM_newindex); + } + lj_cdata_set(cts, ct, p, o+2, qual); + return 0; +} + +/* Common handler for cdata arithmetic. */ +static int ffi_arith(lua_State *L) +{ + MMS mm = (MMS)(curr_func(L)->c.ffid - (int)FF_ffi_meta___eq + (int)MM_eq); + return lj_carith_op(L, mm); +} + +/* The following functions must be in contiguous ORDER MM. */ +LJLIB_CF(ffi_meta___eq) LJLIB_REC(cdata_arith MM_eq) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___len) LJLIB_REC(cdata_arith MM_len) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___lt) LJLIB_REC(cdata_arith MM_lt) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___le) LJLIB_REC(cdata_arith MM_le) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___concat) LJLIB_REC(cdata_arith MM_concat) +{ + return ffi_arith(L); +} + +/* Forward declaration. */ +static int lj_cf_ffi_new(lua_State *L); + +LJLIB_CF(ffi_meta___call) LJLIB_REC(cdata_call) +{ + CTState *cts = ctype_cts(L); + GCcdata *cd = ffi_checkcdata(L, 1); + CTypeID id = cd->ctypeid; + CType *ct; + cTValue *tv; + MMS mm = MM_call; + if (cd->ctypeid == CTID_CTYPEID) { + id = *(CTypeID *)cdataptr(cd); + mm = MM_new; + } else { + int ret = lj_ccall_func(L, cd); + if (ret >= 0) + return ret; + } + /* Handle ctype __call/__new metamethod. */ + ct = ctype_raw(cts, id); + if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); + tv = lj_ctype_meta(cts, id, mm); + if (tv) + return lj_meta_tailcall(L, tv); + else if (mm == MM_call) + lj_err_callerv(L, LJ_ERR_FFI_BADCALL, strdata(lj_ctype_repr(L, id, NULL))); + return lj_cf_ffi_new(L); +} + +LJLIB_CF(ffi_meta___add) LJLIB_REC(cdata_arith MM_add) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___sub) LJLIB_REC(cdata_arith MM_sub) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___mul) LJLIB_REC(cdata_arith MM_mul) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___div) LJLIB_REC(cdata_arith MM_div) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___mod) LJLIB_REC(cdata_arith MM_mod) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___pow) LJLIB_REC(cdata_arith MM_pow) +{ + return ffi_arith(L); +} + +LJLIB_CF(ffi_meta___unm) LJLIB_REC(cdata_arith MM_unm) +{ + return ffi_arith(L); +} +/* End of contiguous ORDER MM. */ + +LJLIB_CF(ffi_meta___tostring) +{ + GCcdata *cd = ffi_checkcdata(L, 1); + const char *msg = "cdata<%s>: %p"; + CTypeID id = cd->ctypeid; + void *p = cdataptr(cd); + if (id == CTID_CTYPEID) { + msg = "ctype<%s>"; + id = *(CTypeID *)p; + } else { + CTState *cts = ctype_cts(L); + CType *ct = ctype_raw(cts, id); + if (ctype_isref(ct->info)) { + p = *(void **)p; + ct = ctype_rawchild(cts, ct); + } + if (ctype_iscomplex(ct->info)) { + setstrV(L, L->top-1, lj_ctype_repr_complex(L, cdataptr(cd), ct->size)); + goto checkgc; + } else if (ct->size == 8 && ctype_isinteger(ct->info)) { + setstrV(L, L->top-1, lj_ctype_repr_int64(L, *(uint64_t *)cdataptr(cd), + (ct->info & CTF_UNSIGNED))); + goto checkgc; + } else if (ctype_isfunc(ct->info)) { + p = *(void **)p; + } else if (ctype_isenum(ct->info)) { + msg = "cdata<%s>: %d"; + p = (void *)(uintptr_t)*(uint32_t **)p; + } else { + if (ctype_isptr(ct->info)) { + p = cdata_getptr(p, ct->size); + ct = ctype_rawchild(cts, ct); + } + if (ctype_isstruct(ct->info) || ctype_isvector(ct->info)) { + /* Handle ctype __tostring metamethod. */ + cTValue *tv = lj_ctype_meta(cts, ctype_typeid(cts, ct), MM_tostring); + if (tv) + return lj_meta_tailcall(L, tv); + } + } + } + lj_str_pushf(L, msg, strdata(lj_ctype_repr(L, id, NULL)), p); +checkgc: + lj_gc_check(L); + return 1; +} + +static int ffi_pairs(lua_State *L, MMS mm) +{ + CTState *cts = ctype_cts(L); + CTypeID id = ffi_checkcdata(L, 1)->ctypeid; + CType *ct = ctype_raw(cts, id); + cTValue *tv; + if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); + tv = lj_ctype_meta(cts, id, mm); + if (!tv) + lj_err_callerv(L, LJ_ERR_FFI_BADMM, strdata(lj_ctype_repr(L, id, NULL)), + strdata(mmname_str(G(L), mm))); + return lj_meta_tailcall(L, tv); +} + +LJLIB_CF(ffi_meta___pairs) +{ + return ffi_pairs(L, MM_pairs); +} + +LJLIB_CF(ffi_meta___ipairs) +{ + return ffi_pairs(L, MM_ipairs); +} + +LJLIB_PUSH("ffi") LJLIB_SET(__metatable) + +#include "lj_libdef.h" + +/* -- C library metamethods ----------------------------------------------- */ + +#define LJLIB_MODULE_ffi_clib + +/* Index C library by a name. */ +static TValue *ffi_clib_index(lua_State *L) +{ + TValue *o = L->base; + CLibrary *cl; + if (!(o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB)) + lj_err_argt(L, 1, LUA_TUSERDATA); + cl = (CLibrary *)uddata(udataV(o)); + if (!(o+1 < L->top && tvisstr(o+1))) + lj_err_argt(L, 2, LUA_TSTRING); + return lj_clib_index(L, cl, strV(o+1)); +} + +LJLIB_CF(ffi_clib___index) LJLIB_REC(clib_index 1) +{ + TValue *tv = ffi_clib_index(L); + if (tviscdata(tv)) { + CTState *cts = ctype_cts(L); + GCcdata *cd = cdataV(tv); + CType *s = ctype_get(cts, cd->ctypeid); + if (ctype_isextern(s->info)) { + CTypeID sid = ctype_cid(s->info); + void *sp = *(void **)cdataptr(cd); + CType *ct = ctype_raw(cts, sid); + if (lj_cconv_tv_ct(cts, ct, sid, L->top-1, sp)) + lj_gc_check(L); + return 1; + } + } + copyTV(L, L->top-1, tv); + return 1; +} + +LJLIB_CF(ffi_clib___newindex) LJLIB_REC(clib_index 0) +{ + TValue *tv = ffi_clib_index(L); + TValue *o = L->base+2; + if (o < L->top && tviscdata(tv)) { + CTState *cts = ctype_cts(L); + GCcdata *cd = cdataV(tv); + CType *d = ctype_get(cts, cd->ctypeid); + if (ctype_isextern(d->info)) { + CTInfo qual = 0; + for (;;) { /* Skip attributes and collect qualifiers. */ + d = ctype_child(cts, d); + if (!ctype_isattrib(d->info)) break; + if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size; + } + if (!((d->info|qual) & CTF_CONST)) { + lj_cconv_ct_tv(cts, d, *(void **)cdataptr(cd), o, 0); + return 0; + } + } + } + lj_err_caller(L, LJ_ERR_FFI_WRCONST); + return 0; /* unreachable */ +} + +LJLIB_CF(ffi_clib___gc) +{ + TValue *o = L->base; + if (o < L->top && tvisudata(o) && udataV(o)->udtype == UDTYPE_FFI_CLIB) + lj_clib_unload((CLibrary *)uddata(udataV(o))); + return 0; +} + +#include "lj_libdef.h" + +/* -- Callback function metamethods --------------------------------------- */ + +#define LJLIB_MODULE_ffi_callback + +static int ffi_callback_set(lua_State *L, GCfunc *fn) +{ + GCcdata *cd = ffi_checkcdata(L, 1); + CTState *cts = ctype_cts(L); + CType *ct = ctype_raw(cts, cd->ctypeid); + if (ctype_isptr(ct->info) && (LJ_32 || ct->size == 8)) { + MSize slot = lj_ccallback_ptr2slot(cts, *(void **)cdataptr(cd)); + if (slot < cts->cb.sizeid && cts->cb.cbid[slot] != 0) { + GCtab *t = cts->miscmap; + TValue *tv = lj_tab_setint(L, t, (int32_t)slot); + if (fn) { + setfuncV(L, tv, fn); + lj_gc_anybarriert(L, t); + } else { + setnilV(tv); + cts->cb.cbid[slot] = 0; + cts->cb.topid = slot < cts->cb.topid ? slot : cts->cb.topid; + } + return 0; + } + } + lj_err_caller(L, LJ_ERR_FFI_BADCBACK); + return 0; +} + +LJLIB_CF(ffi_callback_free) +{ + return ffi_callback_set(L, NULL); +} + +LJLIB_CF(ffi_callback_set) +{ + GCfunc *fn = lj_lib_checkfunc(L, 2); + return ffi_callback_set(L, fn); +} + +LJLIB_PUSH(top-1) LJLIB_SET(__index) + +#include "lj_libdef.h" + +/* -- FFI library functions ----------------------------------------------- */ + +#define LJLIB_MODULE_ffi + +LJLIB_CF(ffi_cdef) +{ + GCstr *s = lj_lib_checkstr(L, 1); + CPState cp; + int errcode; + cp.L = L; + cp.cts = ctype_cts(L); + cp.srcname = strdata(s); + cp.p = strdata(s); + cp.param = L->base+1; + cp.mode = CPARSE_MODE_MULTI|CPARSE_MODE_DIRECT; + errcode = lj_cparse(&cp); + if (errcode) lj_err_throw(L, errcode); /* Propagate errors. */ + lj_gc_check(L); + return 0; +} + +LJLIB_CF(ffi_new) LJLIB_REC(.) +{ + CTState *cts = ctype_cts(L); + CTypeID id = ffi_checkctype(L, cts, NULL); + CType *ct = ctype_raw(cts, id); + CTSize sz; + CTInfo info = lj_ctype_info(cts, id, &sz); + TValue *o = L->base+1; + GCcdata *cd; + if ((info & CTF_VLA)) { + o++; + sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2)); + } + if (sz == CTSIZE_INVALID) + lj_err_arg(L, 1, LJ_ERR_FFI_INVSIZE); + if (!(info & CTF_VLA) && ctype_align(info) <= CT_MEMALIGN) + cd = lj_cdata_new(cts, id, sz); + else + cd = lj_cdata_newv(cts, id, sz, ctype_align(info)); + setcdataV(L, o-1, cd); /* Anchor the uninitialized cdata. */ + lj_cconv_ct_init(cts, ct, sz, cdataptr(cd), + o, (MSize)(L->top - o)); /* Initialize cdata. */ + if (ctype_isstruct(ct->info)) { + /* Handle ctype __gc metamethod. Use the fast lookup here. */ + cTValue *tv = lj_tab_getinth(cts->miscmap, -(int32_t)id); + if (tv && tvistab(tv) && (tv = lj_meta_fast(L, tabV(tv), MM_gc))) { + GCtab *t = cts->finalizer; + if (gcref(t->metatable)) { + /* Add to finalizer table, if still enabled. */ + copyTV(L, lj_tab_set(L, t, o-1), tv); + lj_gc_anybarriert(L, t); + cd->marked |= LJ_GC_CDATA_FIN; + } + } + } + L->top = o; /* Only return the cdata itself. */ + lj_gc_check(L); + return 1; +} + +LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new) +{ + CTState *cts = ctype_cts(L); + CTypeID id = ffi_checkctype(L, cts, NULL); + CType *d = ctype_raw(cts, id); + TValue *o = lj_lib_checkany(L, 2); + L->top = o+1; /* Make sure this is the last item on the stack. */ + if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info))) + lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); + if (!(tviscdata(o) && cdataV(o)->ctypeid == id)) { + GCcdata *cd = lj_cdata_new(cts, id, d->size); + lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST); + setcdataV(L, o, cd); + lj_gc_check(L); + } + return 1; +} + +LJLIB_CF(ffi_typeof) LJLIB_REC(.) +{ + CTState *cts = ctype_cts(L); + CTypeID id = ffi_checkctype(L, cts, L->base+1); + GCcdata *cd = lj_cdata_new(cts, CTID_CTYPEID, 4); + *(CTypeID *)cdataptr(cd) = id; + setcdataV(L, L->top-1, cd); + lj_gc_check(L); + return 1; +} + +LJLIB_CF(ffi_istype) LJLIB_REC(.) +{ + CTState *cts = ctype_cts(L); + CTypeID id1 = ffi_checkctype(L, cts, NULL); + TValue *o = lj_lib_checkany(L, 2); + int b = 0; + if (tviscdata(o)) { + GCcdata *cd = cdataV(o); + CTypeID id2 = cd->ctypeid == CTID_CTYPEID ? *(CTypeID *)cdataptr(cd) : + cd->ctypeid; + CType *ct1 = lj_ctype_rawref(cts, id1); + CType *ct2 = lj_ctype_rawref(cts, id2); + if (ct1 == ct2) { + b = 1; + } else if (ctype_type(ct1->info) == ctype_type(ct2->info) && + ct1->size == ct2->size) { + if (ctype_ispointer(ct1->info)) + b = lj_cconv_compatptr(cts, ct1, ct2, CCF_IGNQUAL); + else if (ctype_isnum(ct1->info) || ctype_isvoid(ct1->info)) + b = (((ct1->info ^ ct2->info) & ~CTF_QUAL) == 0); + } else if (ctype_isstruct(ct1->info) && ctype_isptr(ct2->info) && + ct1 == ctype_rawchild(cts, ct2)) { + b = 1; + } + } + setboolV(L->top-1, b); + setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */ + return 1; +} + +LJLIB_CF(ffi_sizeof) LJLIB_REC(ffi_xof FF_ffi_sizeof) +{ + CTState *cts = ctype_cts(L); + CTypeID id = ffi_checkctype(L, cts, NULL); + CTSize sz; + if (LJ_UNLIKELY(tviscdata(L->base) && cdataisv(cdataV(L->base)))) { + sz = cdatavlen(cdataV(L->base)); + } else { + CType *ct = lj_ctype_rawref(cts, id); + if (ctype_isvltype(ct->info)) + sz = lj_ctype_vlsize(cts, ct, (CTSize)ffi_checkint(L, 2)); + else + sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID; + if (LJ_UNLIKELY(sz == CTSIZE_INVALID)) { + setnilV(L->top-1); + return 1; + } + } + setintV(L->top-1, (int32_t)sz); + return 1; +} + +LJLIB_CF(ffi_alignof) LJLIB_REC(ffi_xof FF_ffi_alignof) +{ + CTState *cts = ctype_cts(L); + CTypeID id = ffi_checkctype(L, cts, NULL); + CTSize sz = 0; + CTInfo info = lj_ctype_info(cts, id, &sz); + setintV(L->top-1, 1 << ctype_align(info)); + return 1; +} + +LJLIB_CF(ffi_offsetof) LJLIB_REC(ffi_xof FF_ffi_offsetof) +{ + CTState *cts = ctype_cts(L); + CTypeID id = ffi_checkctype(L, cts, NULL); + GCstr *name = lj_lib_checkstr(L, 2); + CType *ct = lj_ctype_rawref(cts, id); + CTSize ofs; + if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) { + CType *fct = lj_ctype_getfield(cts, ct, name, &ofs); + if (fct) { + setintV(L->top-1, ofs); + if (ctype_isfield(fct->info)) { + return 1; + } else if (ctype_isbitfield(fct->info)) { + setintV(L->top++, ctype_bitpos(fct->info)); + setintV(L->top++, ctype_bitbsz(fct->info)); + return 3; + } + } + } + return 0; +} + +LJLIB_CF(ffi_errno) LJLIB_REC(.) +{ + int err = errno; + if (L->top > L->base) + errno = ffi_checkint(L, 1); + setintV(L->top++, err); + return 1; +} + +LJLIB_CF(ffi_string) LJLIB_REC(.) +{ + CTState *cts = ctype_cts(L); + TValue *o = lj_lib_checkany(L, 1); + const char *p; + size_t len; + if (o+1 < L->top) { + len = (size_t)ffi_checkint(L, 2); + lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CVOID), (uint8_t *)&p, o, + CCF_ARG(1)); + } else { + lj_cconv_ct_tv(cts, ctype_get(cts, CTID_P_CCHAR), (uint8_t *)&p, o, + CCF_ARG(1)); + len = strlen(p); + } + L->top = o+1; /* Make sure this is the last item on the stack. */ + setstrV(L, o, lj_str_new(L, p, len)); + lj_gc_check(L); + return 1; +} + +LJLIB_CF(ffi_copy) LJLIB_REC(.) +{ + void *dp = ffi_checkptr(L, 1, CTID_P_VOID); + void *sp = ffi_checkptr(L, 2, CTID_P_CVOID); + TValue *o = L->base+1; + CTSize len; + if (tvisstr(o) && o+1 >= L->top) + len = strV(o)->len+1; /* Copy Lua string including trailing '\0'. */ + else + len = (CTSize)ffi_checkint(L, 3); + memcpy(dp, sp, len); + return 0; +} + +LJLIB_CF(ffi_fill) LJLIB_REC(.) +{ + void *dp = ffi_checkptr(L, 1, CTID_P_VOID); + CTSize len = (CTSize)ffi_checkint(L, 2); + int32_t fill = 0; + if (L->base+2 < L->top && !tvisnil(L->base+2)) fill = ffi_checkint(L, 3); + memset(dp, fill, len); + return 0; +} + +#define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be) + +/* Test ABI string. */ +LJLIB_CF(ffi_abi) LJLIB_REC(.) +{ + GCstr *s = lj_lib_checkstr(L, 1); + int b = 0; + switch (s->hash) { +#if LJ_64 + case H_(849858eb,ad35fd06): b = 1; break; /* 64bit */ +#else + case H_(662d3c79,d0e22477): b = 1; break; /* 32bit */ +#endif +#if LJ_ARCH_HASFPU + case H_(e33ee463,e33ee463): b = 1; break; /* fpu */ +#endif +#if LJ_ABI_SOFTFP + case H_(61211a23,c2e8c81c): b = 1; break; /* softfp */ +#else + case H_(539417a8,8ce0812f): b = 1; break; /* hardfp */ +#endif +#if LJ_ABI_EABI + case H_(2182df8f,f2ed1152): b = 1; break; /* eabi */ +#endif +#if LJ_ABI_WIN + case H_(4ab624a8,4ab624a8): b = 1; break; /* win */ +#endif + case H_(3af93066,1f001464): b = 1; break; /* le/be */ + default: + break; + } + setboolV(L->top-1, b); + setboolV(&G(L)->tmptv2, b); /* Remember for trace recorder. */ + return 1; +} + +#undef H_ + +LJLIB_PUSH(top-8) LJLIB_SET(!) /* Store reference to miscmap table. */ + +LJLIB_CF(ffi_metatype) +{ + CTState *cts = ctype_cts(L); + CTypeID id = ffi_checkctype(L, cts, NULL); + GCtab *mt = lj_lib_checktab(L, 2); + GCtab *t = cts->miscmap; + CType *ct = ctype_get(cts, id); /* Only allow raw types. */ + TValue *tv; + GCcdata *cd; + if (!(ctype_isstruct(ct->info) || ctype_iscomplex(ct->info) || + ctype_isvector(ct->info))) + lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); + tv = lj_tab_setinth(L, t, -(int32_t)id); + if (!tvisnil(tv)) + lj_err_caller(L, LJ_ERR_PROTMT); + settabV(L, tv, mt); + lj_gc_anybarriert(L, t); + cd = lj_cdata_new(cts, CTID_CTYPEID, 4); + *(CTypeID *)cdataptr(cd) = id; + setcdataV(L, L->top-1, cd); + lj_gc_check(L); + return 1; +} + +LJLIB_PUSH(top-7) LJLIB_SET(!) /* Store reference to finalizer table. */ + +LJLIB_CF(ffi_gc) LJLIB_REC(.) +{ + GCcdata *cd = ffi_checkcdata(L, 1); + TValue *fin = lj_lib_checkany(L, 2); + CTState *cts = ctype_cts(L); + GCtab *t = cts->finalizer; + CType *ct = ctype_raw(cts, cd->ctypeid); + if (!(ctype_isptr(ct->info) || ctype_isstruct(ct->info) || + ctype_isrefarray(ct->info))) + lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); + if (gcref(t->metatable)) { /* Update finalizer table, if still enabled. */ + copyTV(L, lj_tab_set(L, t, L->base), fin); + lj_gc_anybarriert(L, t); + if (!tvisnil(fin)) + cd->marked |= LJ_GC_CDATA_FIN; + else + cd->marked &= ~LJ_GC_CDATA_FIN; + } + L->top = L->base+1; /* Pass through the cdata object. */ + return 1; +} + +LJLIB_PUSH(top-5) LJLIB_SET(!) /* Store clib metatable in func environment. */ + +LJLIB_CF(ffi_load) +{ + GCstr *name = lj_lib_checkstr(L, 1); + int global = (L->base+1 < L->top && tvistruecond(L->base+1)); + lj_clib_load(L, tabref(curr_func(L)->c.env), name, global); + return 1; +} + +LJLIB_PUSH(top-4) LJLIB_SET(C) +LJLIB_PUSH(top-3) LJLIB_SET(os) +LJLIB_PUSH(top-2) LJLIB_SET(arch) + +#include "lj_libdef.h" + +/* ------------------------------------------------------------------------ */ + +/* Create special weak-keyed finalizer table. */ +static GCtab *ffi_finalizer(lua_State *L) +{ + /* NOBARRIER: The table is new (marked white). */ + GCtab *t = lj_tab_new(L, 0, 1); + settabV(L, L->top++, t); + setgcref(t->metatable, obj2gco(t)); + setstrV(L, lj_tab_setstr(L, t, lj_str_newlit(L, "__mode")), + lj_str_newlit(L, "K")); + t->nomm = (uint8_t)(~(1u<top-1); + lj_gc_anybarriert(L, t); + } +} + +LUALIB_API int luaopen_ffi(lua_State *L) +{ + CTState *cts = lj_ctype_init(L); + settabV(L, L->top++, (cts->miscmap = lj_tab_new(L, 0, 1))); + cts->finalizer = ffi_finalizer(L); + LJ_LIB_REG(L, NULL, ffi_meta); + /* NOBARRIER: basemt is a GC root. */ + setgcref(basemt_it(G(L), LJ_TCDATA), obj2gco(tabV(L->top-1))); + LJ_LIB_REG(L, NULL, ffi_clib); + LJ_LIB_REG(L, NULL, ffi_callback); + /* NOBARRIER: the key is new and lj_tab_newkey() handles the barrier. */ + settabV(L, lj_tab_setstr(L, cts->miscmap, &cts->g->strempty), tabV(L->top-1)); + L->top--; + lj_clib_default(L, tabV(L->top-1)); /* Create ffi.C default namespace. */ + lua_pushliteral(L, LJ_OS_NAME); + lua_pushliteral(L, LJ_ARCH_NAME); + LJ_LIB_REG(L, NULL, ffi); /* Note: no global "ffi" created! */ + ffi_register_module(L); + return 1; +} + +#endif diff --git a/external/lua/luajit/src/src/lib_init.c b/external/lua/luajit/src/src/lib_init.c new file mode 100644 index 0000000000..2d6bd591bf --- /dev/null +++ b/external/lua/luajit/src/src/lib_init.c @@ -0,0 +1,55 @@ +/* +** Library initialization. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major parts taken verbatim from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lib_init_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_arch.h" + +static const luaL_Reg lj_lib_load[] = { + { "", luaopen_base }, + { LUA_LOADLIBNAME, luaopen_package }, + { LUA_TABLIBNAME, luaopen_table }, + { LUA_IOLIBNAME, luaopen_io }, + { LUA_OSLIBNAME, luaopen_os }, + { LUA_STRLIBNAME, luaopen_string }, + { LUA_MATHLIBNAME, luaopen_math }, + { LUA_DBLIBNAME, luaopen_debug }, + { LUA_BITLIBNAME, luaopen_bit }, + { LUA_JITLIBNAME, luaopen_jit }, + { NULL, NULL } +}; + +static const luaL_Reg lj_lib_preload[] = { +#if LJ_HASFFI + { LUA_FFILIBNAME, luaopen_ffi }, +#endif + { NULL, NULL } +}; + +LUALIB_API void luaL_openlibs(lua_State *L) +{ + const luaL_Reg *lib; + for (lib = lj_lib_load; lib->func; lib++) { + lua_pushcfunction(L, lib->func); + lua_pushstring(L, lib->name); + lua_call(L, 1, 0); + } + luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", + sizeof(lj_lib_preload)/sizeof(lj_lib_preload[0])-1); + for (lib = lj_lib_preload; lib->func; lib++) { + lua_pushcfunction(L, lib->func); + lua_setfield(L, -2, lib->name); + } + lua_pop(L, 1); +} + diff --git a/external/lua/luajit/src/src/lib_io.c b/external/lua/luajit/src/src/lib_io.c new file mode 100644 index 0000000000..e0c6908fae --- /dev/null +++ b/external/lua/luajit/src/src/lib_io.c @@ -0,0 +1,539 @@ +/* +** I/O library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2011 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#include +#include + +#define lib_io_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_state.h" +#include "lj_ff.h" +#include "lj_lib.h" + +/* Userdata payload for I/O file. */ +typedef struct IOFileUD { + FILE *fp; /* File handle. */ + uint32_t type; /* File type. */ +} IOFileUD; + +#define IOFILE_TYPE_FILE 0 /* Regular file. */ +#define IOFILE_TYPE_PIPE 1 /* Pipe. */ +#define IOFILE_TYPE_STDF 2 /* Standard file handle. */ +#define IOFILE_TYPE_MASK 3 + +#define IOFILE_FLAG_CLOSE 4 /* Close after io.lines() iterator. */ + +#define IOSTDF_UD(L, id) (&gcref(G(L)->gcroot[(id)])->ud) +#define IOSTDF_IOF(L, id) ((IOFileUD *)uddata(IOSTDF_UD(L, (id)))) + +/* -- Open/close helpers -------------------------------------------------- */ + +static IOFileUD *io_tofilep(lua_State *L) +{ + if (!(L->base < L->top && tvisudata(L->base) && + udataV(L->base)->udtype == UDTYPE_IO_FILE)) + lj_err_argtype(L, 1, "FILE*"); + return (IOFileUD *)uddata(udataV(L->base)); +} + +static IOFileUD *io_tofile(lua_State *L) +{ + IOFileUD *iof = io_tofilep(L); + if (iof->fp == NULL) + lj_err_caller(L, LJ_ERR_IOCLFL); + return iof; +} + +static FILE *io_stdfile(lua_State *L, ptrdiff_t id) +{ + IOFileUD *iof = IOSTDF_IOF(L, id); + if (iof->fp == NULL) + lj_err_caller(L, LJ_ERR_IOSTDCL); + return iof->fp; +} + +static IOFileUD *io_file_new(lua_State *L) +{ + IOFileUD *iof = (IOFileUD *)lua_newuserdata(L, sizeof(IOFileUD)); + GCudata *ud = udataV(L->top-1); + ud->udtype = UDTYPE_IO_FILE; + /* NOBARRIER: The GCudata is new (marked white). */ + setgcrefr(ud->metatable, curr_func(L)->c.env); + iof->fp = NULL; + iof->type = IOFILE_TYPE_FILE; + return iof; +} + +static IOFileUD *io_file_open(lua_State *L, const char *mode) +{ + const char *fname = strdata(lj_lib_checkstr(L, 1)); + IOFileUD *iof = io_file_new(L); + iof->fp = fopen(fname, mode); + if (iof->fp == NULL) + luaL_argerror(L, 1, lj_str_pushf(L, "%s: %s", fname, strerror(errno))); + return iof; +} + +static int io_file_close(lua_State *L, IOFileUD *iof) +{ + int ok; + if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_FILE) { + ok = (fclose(iof->fp) == 0); + } else if ((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_PIPE) { + int stat = -1; +#if LJ_TARGET_POSIX + stat = pclose(iof->fp); +#elif LJ_TARGET_WINDOWS + stat = _pclose(iof->fp); +#else + lua_assert(0); + return 0; +#endif +#if LJ_52 + iof->fp = NULL; + return luaL_execresult(L, stat); +#else + ok = (stat != -1); +#endif + } else { + lua_assert((iof->type & IOFILE_TYPE_MASK) == IOFILE_TYPE_STDF); + setnilV(L->top++); + lua_pushliteral(L, "cannot close standard file"); + return 2; + } + iof->fp = NULL; + return luaL_fileresult(L, ok, NULL); +} + +/* -- Read/write helpers -------------------------------------------------- */ + +static int io_file_readnum(lua_State *L, FILE *fp) +{ + lua_Number d; + if (fscanf(fp, LUA_NUMBER_SCAN, &d) == 1) { + if (LJ_DUALNUM) { + int32_t i = lj_num2int(d); + if (d == (lua_Number)i && !tvismzero((cTValue *)&d)) { + setintV(L->top++, i); + return 1; + } + } + setnumV(L->top++, d); + return 1; + } else { + setnilV(L->top++); + return 0; + } +} + +static int io_file_readline(lua_State *L, FILE *fp, MSize chop) +{ + MSize m = LUAL_BUFFERSIZE, n = 0, ok = 0; + char *buf; + for (;;) { + buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); + if (fgets(buf+n, m-n, fp) == NULL) break; + n += (MSize)strlen(buf+n); + ok |= n; + if (n && buf[n-1] == '\n') { n -= chop; break; } + if (n >= m - 64) m += m; + } + setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); + lj_gc_check(L); + return (int)ok; +} + +static void io_file_readall(lua_State *L, FILE *fp) +{ + MSize m, n; + for (m = LUAL_BUFFERSIZE, n = 0; ; m += m) { + char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); + n += (MSize)fread(buf+n, 1, m-n, fp); + if (n != m) { + setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); + lj_gc_check(L); + return; + } + } +} + +static int io_file_readlen(lua_State *L, FILE *fp, MSize m) +{ + if (m) { + char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); + MSize n = (MSize)fread(buf, 1, m, fp); + setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); + lj_gc_check(L); + return (n > 0 || m == 0); + } else { + int c = getc(fp); + ungetc(c, fp); + setstrV(L, L->top++, &G(L)->strempty); + return (c != EOF); + } +} + +static int io_file_read(lua_State *L, FILE *fp, int start) +{ + int ok, n, nargs = (int)(L->top - L->base) - start; + clearerr(fp); + if (nargs == 0) { + ok = io_file_readline(L, fp, 1); + n = start+1; /* Return 1 result. */ + } else { + /* The results plus the buffers go on top of the args. */ + luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments"); + ok = 1; + for (n = start; nargs-- && ok; n++) { + if (tvisstr(L->base+n)) { + const char *p = strVdata(L->base+n); + if (p[0] != '*') + lj_err_arg(L, n+1, LJ_ERR_INVOPT); + if (p[1] == 'n') + ok = io_file_readnum(L, fp); + else if ((p[1] & ~0x20) == 'L') + ok = io_file_readline(L, fp, (p[1] == 'l')); + else if (p[1] == 'a') + io_file_readall(L, fp); + else + lj_err_arg(L, n+1, LJ_ERR_INVFMT); + } else if (tvisnumber(L->base+n)) { + ok = io_file_readlen(L, fp, (MSize)lj_lib_checkint(L, n+1)); + } else { + lj_err_arg(L, n+1, LJ_ERR_INVOPT); + } + } + } + if (ferror(fp)) + return luaL_fileresult(L, 0, NULL); + if (!ok) + setnilV(L->top-1); /* Replace last result with nil. */ + return n - start; +} + +static int io_file_write(lua_State *L, FILE *fp, int start) +{ + cTValue *tv; + int status = 1; + for (tv = L->base+start; tv < L->top; tv++) { + if (tvisstr(tv)) { + MSize len = strV(tv)->len; + status = status && (fwrite(strVdata(tv), 1, len, fp) == len); + } else if (tvisint(tv)) { + char buf[LJ_STR_INTBUF]; + char *p = lj_str_bufint(buf, intV(tv)); + size_t len = (size_t)(buf+LJ_STR_INTBUF-p); + status = status && (fwrite(p, 1, len, fp) == len); + } else if (tvisnum(tv)) { + status = status && (fprintf(fp, LUA_NUMBER_FMT, numV(tv)) > 0); + } else { + lj_err_argt(L, (int)(tv - L->base) + 1, LUA_TSTRING); + } + } + if (LJ_52 && status) { + L->top = L->base+1; + if (start == 0) + setudataV(L, L->base, IOSTDF_UD(L, GCROOT_IO_OUTPUT)); + return 1; + } + return luaL_fileresult(L, status, NULL); +} + +static int io_file_iter(lua_State *L) +{ + GCfunc *fn = curr_func(L); + IOFileUD *iof = uddata(udataV(&fn->c.upvalue[0])); + int n = fn->c.nupvalues - 1; + if (iof->fp == NULL) + lj_err_caller(L, LJ_ERR_IOCLFL); + L->top = L->base; + if (n) { /* Copy upvalues with options to stack. */ + if (n > LUAI_MAXCSTACK) + lj_err_caller(L, LJ_ERR_STKOV); + lj_state_checkstack(L, (MSize)n); + memcpy(L->top, &fn->c.upvalue[1], n*sizeof(TValue)); + L->top += n; + } + n = io_file_read(L, iof->fp, 0); + if (ferror(iof->fp)) + lj_err_callermsg(L, strVdata(L->top-2)); + if (tvisnil(L->base) && (iof->type & IOFILE_FLAG_CLOSE)) { + io_file_close(L, iof); /* Return values are ignored. */ + return 0; + } + return n; +} + +/* -- I/O file methods ---------------------------------------------------- */ + +#define LJLIB_MODULE_io_method + +LJLIB_CF(io_method_close) +{ + IOFileUD *iof = L->base < L->top ? io_tofile(L) : + IOSTDF_IOF(L, GCROOT_IO_OUTPUT); + return io_file_close(L, iof); +} + +LJLIB_CF(io_method_read) +{ + return io_file_read(L, io_tofile(L)->fp, 1); +} + +LJLIB_CF(io_method_write) LJLIB_REC(io_write 0) +{ + return io_file_write(L, io_tofile(L)->fp, 1); +} + +LJLIB_CF(io_method_flush) LJLIB_REC(io_flush 0) +{ + return luaL_fileresult(L, fflush(io_tofile(L)->fp) == 0, NULL); +} + +LJLIB_CF(io_method_seek) +{ + FILE *fp = io_tofile(L)->fp; + int opt = lj_lib_checkopt(L, 2, 1, "\3set\3cur\3end"); + int64_t ofs = 0; + cTValue *o; + int res; + if (opt == 0) opt = SEEK_SET; + else if (opt == 1) opt = SEEK_CUR; + else if (opt == 2) opt = SEEK_END; + o = L->base+2; + if (o < L->top) { + if (tvisint(o)) + ofs = (int64_t)intV(o); + else if (tvisnum(o)) + ofs = (int64_t)numV(o); + else if (!tvisnil(o)) + lj_err_argt(L, 3, LUA_TNUMBER); + } +#if LJ_TARGET_POSIX + res = fseeko(fp, ofs, opt); +#elif _MSC_VER >= 1400 + res = _fseeki64(fp, ofs, opt); +#elif defined(__MINGW32__) + res = fseeko64(fp, ofs, opt); +#else + res = fseek(fp, (long)ofs, opt); +#endif + if (res) + return luaL_fileresult(L, 0, NULL); +#if LJ_TARGET_POSIX + ofs = ftello(fp); +#elif _MSC_VER >= 1400 + ofs = _ftelli64(fp); +#elif defined(__MINGW32__) + ofs = ftello64(fp); +#else + ofs = (int64_t)ftell(fp); +#endif + setint64V(L->top-1, ofs); + return 1; +} + +LJLIB_CF(io_method_setvbuf) +{ + FILE *fp = io_tofile(L)->fp; + int opt = lj_lib_checkopt(L, 2, -1, "\4full\4line\2no"); + size_t sz = (size_t)lj_lib_optint(L, 3, LUAL_BUFFERSIZE); + if (opt == 0) opt = _IOFBF; + else if (opt == 1) opt = _IOLBF; + else if (opt == 2) opt = _IONBF; + return luaL_fileresult(L, setvbuf(fp, NULL, opt, sz) == 0, NULL); +} + +LJLIB_CF(io_method_lines) +{ + io_tofile(L); + lua_pushcclosure(L, io_file_iter, (int)(L->top - L->base)); + return 1; +} + +LJLIB_CF(io_method___gc) +{ + IOFileUD *iof = io_tofilep(L); + if (iof->fp != NULL && (iof->type & IOFILE_TYPE_MASK) != IOFILE_TYPE_STDF) + io_file_close(L, iof); + return 0; +} + +LJLIB_CF(io_method___tostring) +{ + IOFileUD *iof = io_tofilep(L); + if (iof->fp != NULL) + lua_pushfstring(L, "file (%p)", iof->fp); + else + lua_pushliteral(L, "file (closed)"); + return 1; +} + +LJLIB_PUSH(top-1) LJLIB_SET(__index) + +#include "lj_libdef.h" + +/* -- I/O library functions ----------------------------------------------- */ + +#define LJLIB_MODULE_io + +LJLIB_PUSH(top-2) LJLIB_SET(!) /* Set environment. */ + +LJLIB_CF(io_open) +{ + const char *fname = strdata(lj_lib_checkstr(L, 1)); + GCstr *s = lj_lib_optstr(L, 2); + const char *mode = s ? strdata(s) : "r"; + IOFileUD *iof = io_file_new(L); + iof->fp = fopen(fname, mode); + return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname); +} + +LJLIB_CF(io_popen) +{ +#if LJ_TARGET_POSIX || LJ_TARGET_WINDOWS + const char *fname = strdata(lj_lib_checkstr(L, 1)); + GCstr *s = lj_lib_optstr(L, 2); + const char *mode = s ? strdata(s) : "r"; + IOFileUD *iof = io_file_new(L); + iof->type = IOFILE_TYPE_PIPE; +#if LJ_TARGET_POSIX + fflush(NULL); + iof->fp = popen(fname, mode); +#else + iof->fp = _popen(fname, mode); +#endif + return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, fname); +#else + return luaL_error(L, LUA_QL("popen") " not supported"); +#endif +} + +LJLIB_CF(io_tmpfile) +{ + IOFileUD *iof = io_file_new(L); +#if LJ_TARGET_PS3 + iof->fp = NULL; errno = ENOSYS; +#else + iof->fp = tmpfile(); +#endif + return iof->fp != NULL ? 1 : luaL_fileresult(L, 0, NULL); +} + +LJLIB_CF(io_close) +{ + return lj_cf_io_method_close(L); +} + +LJLIB_CF(io_read) +{ + return io_file_read(L, io_stdfile(L, GCROOT_IO_INPUT), 0); +} + +LJLIB_CF(io_write) LJLIB_REC(io_write GCROOT_IO_OUTPUT) +{ + return io_file_write(L, io_stdfile(L, GCROOT_IO_OUTPUT), 0); +} + +LJLIB_CF(io_flush) LJLIB_REC(io_flush GCROOT_IO_OUTPUT) +{ + return luaL_fileresult(L, fflush(io_stdfile(L, GCROOT_IO_OUTPUT)) == 0, NULL); +} + +static int io_std_getset(lua_State *L, ptrdiff_t id, const char *mode) +{ + if (L->base < L->top && !tvisnil(L->base)) { + if (tvisudata(L->base)) { + io_tofile(L); + L->top = L->base+1; + } else { + io_file_open(L, mode); + } + /* NOBARRIER: The standard I/O handles are GC roots. */ + setgcref(G(L)->gcroot[id], gcV(L->top-1)); + } else { + setudataV(L, L->top++, IOSTDF_UD(L, id)); + } + return 1; +} + +LJLIB_CF(io_input) +{ + return io_std_getset(L, GCROOT_IO_INPUT, "r"); +} + +LJLIB_CF(io_output) +{ + return io_std_getset(L, GCROOT_IO_OUTPUT, "w"); +} + +LJLIB_CF(io_lines) +{ + if (L->base == L->top) setnilV(L->top++); + if (!tvisnil(L->base)) { /* io.lines(fname) */ + IOFileUD *iof = io_file_open(L, "r"); + iof->type = IOFILE_TYPE_FILE|IOFILE_FLAG_CLOSE; + L->top--; + setudataV(L, L->base, udataV(L->top)); + } else { /* io.lines() iterates over stdin. */ + setudataV(L, L->base, IOSTDF_UD(L, GCROOT_IO_INPUT)); + } + lua_pushcclosure(L, io_file_iter, (int)(L->top - L->base)); + return 1; +} + +LJLIB_CF(io_type) +{ + cTValue *o = lj_lib_checkany(L, 1); + if (!(tvisudata(o) && udataV(o)->udtype == UDTYPE_IO_FILE)) + setnilV(L->top++); + else if (((IOFileUD *)uddata(udataV(o)))->fp != NULL) + lua_pushliteral(L, "file"); + else + lua_pushliteral(L, "closed file"); + return 1; +} + +#include "lj_libdef.h" + +/* ------------------------------------------------------------------------ */ + +static GCobj *io_std_new(lua_State *L, FILE *fp, const char *name) +{ + IOFileUD *iof = (IOFileUD *)lua_newuserdata(L, sizeof(IOFileUD)); + GCudata *ud = udataV(L->top-1); + ud->udtype = UDTYPE_IO_FILE; + /* NOBARRIER: The GCudata is new (marked white). */ + setgcref(ud->metatable, gcV(L->top-3)); + iof->fp = fp; + iof->type = IOFILE_TYPE_STDF; + lua_setfield(L, -2, name); + return obj2gco(ud); +} + +LUALIB_API int luaopen_io(lua_State *L) +{ + LJ_LIB_REG(L, NULL, io_method); + copyTV(L, L->top, L->top-1); L->top++; + lua_setfield(L, LUA_REGISTRYINDEX, LUA_FILEHANDLE); + LJ_LIB_REG(L, LUA_IOLIBNAME, io); + setgcref(G(L)->gcroot[GCROOT_IO_INPUT], io_std_new(L, stdin, "stdin")); + setgcref(G(L)->gcroot[GCROOT_IO_OUTPUT], io_std_new(L, stdout, "stdout")); + io_std_new(L, stderr, "stderr"); + return 1; +} + diff --git a/external/lua/luajit/src/src/lib_jit.c b/external/lua/luajit/src/src/lib_jit.c new file mode 100644 index 0000000000..82e68258f3 --- /dev/null +++ b/external/lua/luajit/src/src/lib_jit.c @@ -0,0 +1,663 @@ +/* +** JIT library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lib_jit_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_arch.h" +#include "lj_obj.h" +#include "lj_err.h" +#include "lj_debug.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_bc.h" +#if LJ_HASJIT +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_ircall.h" +#include "lj_iropt.h" +#include "lj_target.h" +#endif +#include "lj_dispatch.h" +#include "lj_vm.h" +#include "lj_vmevent.h" +#include "lj_lib.h" + +#include "luajit.h" + +/* -- jit.* functions ----------------------------------------------------- */ + +#define LJLIB_MODULE_jit + +static int setjitmode(lua_State *L, int mode) +{ + int idx = 0; + if (L->base == L->top || tvisnil(L->base)) { /* jit.on/off/flush([nil]) */ + mode |= LUAJIT_MODE_ENGINE; + } else { + /* jit.on/off/flush(func|proto, nil|true|false) */ + if (tvisfunc(L->base) || tvisproto(L->base)) + idx = 1; + else if (!tvistrue(L->base)) /* jit.on/off/flush(true, nil|true|false) */ + goto err; + if (L->base+1 < L->top && tvisbool(L->base+1)) + mode |= boolV(L->base+1) ? LUAJIT_MODE_ALLFUNC : LUAJIT_MODE_ALLSUBFUNC; + else + mode |= LUAJIT_MODE_FUNC; + } + if (luaJIT_setmode(L, idx, mode) != 1) { + if ((mode & LUAJIT_MODE_MASK) == LUAJIT_MODE_ENGINE) + lj_err_caller(L, LJ_ERR_NOJIT); + err: + lj_err_argt(L, 1, LUA_TFUNCTION); + } + return 0; +} + +LJLIB_CF(jit_on) +{ + return setjitmode(L, LUAJIT_MODE_ON); +} + +LJLIB_CF(jit_off) +{ + return setjitmode(L, LUAJIT_MODE_OFF); +} + +LJLIB_CF(jit_flush) +{ +#if LJ_HASJIT + if (L->base < L->top && !tvisnil(L->base)) { + int traceno = lj_lib_checkint(L, 1); + luaJIT_setmode(L, traceno, LUAJIT_MODE_FLUSH|LUAJIT_MODE_TRACE); + return 0; + } +#endif + return setjitmode(L, LUAJIT_MODE_FLUSH); +} + +#if LJ_HASJIT +/* Push a string for every flag bit that is set. */ +static void flagbits_to_strings(lua_State *L, uint32_t flags, uint32_t base, + const char *str) +{ + for (; *str; base <<= 1, str += 1+*str) + if (flags & base) + setstrV(L, L->top++, lj_str_new(L, str+1, *(uint8_t *)str)); +} +#endif + +LJLIB_CF(jit_status) +{ +#if LJ_HASJIT + jit_State *J = L2J(L); + L->top = L->base; + setboolV(L->top++, (J->flags & JIT_F_ON) ? 1 : 0); + flagbits_to_strings(L, J->flags, JIT_F_CPU_FIRST, JIT_F_CPUSTRING); + flagbits_to_strings(L, J->flags, JIT_F_OPT_FIRST, JIT_F_OPTSTRING); + return (int)(L->top - L->base); +#else + setboolV(L->top++, 0); + return 1; +#endif +} + +LJLIB_CF(jit_attach) +{ +#ifdef LUAJIT_DISABLE_VMEVENT + luaL_error(L, "vmevent API disabled"); +#else + GCfunc *fn = lj_lib_checkfunc(L, 1); + GCstr *s = lj_lib_optstr(L, 2); + luaL_findtable(L, LUA_REGISTRYINDEX, LJ_VMEVENTS_REGKEY, LJ_VMEVENTS_HSIZE); + if (s) { /* Attach to given event. */ + const uint8_t *p = (const uint8_t *)strdata(s); + uint32_t h = s->len; + while (*p) h = h ^ (lj_rol(h, 6) + *p++); + lua_pushvalue(L, 1); + lua_rawseti(L, -2, VMEVENT_HASHIDX(h)); + G(L)->vmevmask = VMEVENT_NOCACHE; /* Invalidate cache. */ + } else { /* Detach if no event given. */ + setnilV(L->top++); + while (lua_next(L, -2)) { + L->top--; + if (tvisfunc(L->top) && funcV(L->top) == fn) { + setnilV(lj_tab_set(L, tabV(L->top-2), L->top-1)); + } + } + } +#endif + return 0; +} + +LJLIB_PUSH(top-5) LJLIB_SET(os) +LJLIB_PUSH(top-4) LJLIB_SET(arch) +LJLIB_PUSH(top-3) LJLIB_SET(version_num) +LJLIB_PUSH(top-2) LJLIB_SET(version) + +#include "lj_libdef.h" + +/* -- jit.util.* functions ------------------------------------------------ */ + +#define LJLIB_MODULE_jit_util + +/* -- Reflection API for Lua functions ------------------------------------ */ + +/* Return prototype of first argument (Lua function or prototype object) */ +static GCproto *check_Lproto(lua_State *L, int nolua) +{ + TValue *o = L->base; + if (L->top > o) { + if (tvisproto(o)) { + return protoV(o); + } else if (tvisfunc(o)) { + if (isluafunc(funcV(o))) + return funcproto(funcV(o)); + else if (nolua) + return NULL; + } + } + lj_err_argt(L, 1, LUA_TFUNCTION); + return NULL; /* unreachable */ +} + +static void setintfield(lua_State *L, GCtab *t, const char *name, int32_t val) +{ + setintV(lj_tab_setstr(L, t, lj_str_newz(L, name)), val); +} + +/* local info = jit.util.funcinfo(func [,pc]) */ +LJLIB_CF(jit_util_funcinfo) +{ + GCproto *pt = check_Lproto(L, 1); + if (pt) { + BCPos pc = (BCPos)lj_lib_optint(L, 2, 0); + GCtab *t; + lua_createtable(L, 0, 16); /* Increment hash size if fields are added. */ + t = tabV(L->top-1); + setintfield(L, t, "linedefined", pt->firstline); + setintfield(L, t, "lastlinedefined", pt->firstline + pt->numline); + setintfield(L, t, "stackslots", pt->framesize); + setintfield(L, t, "params", pt->numparams); + setintfield(L, t, "bytecodes", (int32_t)pt->sizebc); + setintfield(L, t, "gcconsts", (int32_t)pt->sizekgc); + setintfield(L, t, "nconsts", (int32_t)pt->sizekn); + setintfield(L, t, "upvalues", (int32_t)pt->sizeuv); + if (pc < pt->sizebc) + setintfield(L, t, "currentline", lj_debug_line(pt, pc)); + lua_pushboolean(L, (pt->flags & PROTO_VARARG)); + lua_setfield(L, -2, "isvararg"); + lua_pushboolean(L, (pt->flags & PROTO_CHILD)); + lua_setfield(L, -2, "children"); + setstrV(L, L->top++, proto_chunkname(pt)); + lua_setfield(L, -2, "source"); + lj_debug_pushloc(L, pt, pc); + lua_setfield(L, -2, "loc"); + } else { + GCfunc *fn = funcV(L->base); + GCtab *t; + lua_createtable(L, 0, 4); /* Increment hash size if fields are added. */ + t = tabV(L->top-1); + if (!iscfunc(fn)) + setintfield(L, t, "ffid", fn->c.ffid); + setintptrV(lj_tab_setstr(L, t, lj_str_newlit(L, "addr")), + (intptr_t)(void *)fn->c.f); + setintfield(L, t, "upvalues", fn->c.nupvalues); + } + return 1; +} + +/* local ins, m = jit.util.funcbc(func, pc) */ +LJLIB_CF(jit_util_funcbc) +{ + GCproto *pt = check_Lproto(L, 0); + BCPos pc = (BCPos)lj_lib_checkint(L, 2); + if (pc < pt->sizebc) { + BCIns ins = proto_bc(pt)[pc]; + BCOp op = bc_op(ins); + lua_assert(op < BC__MAX); + setintV(L->top, ins); + setintV(L->top+1, lj_bc_mode[op]); + L->top += 2; + return 2; + } + return 0; +} + +/* local k = jit.util.funck(func, idx) */ +LJLIB_CF(jit_util_funck) +{ + GCproto *pt = check_Lproto(L, 0); + ptrdiff_t idx = (ptrdiff_t)lj_lib_checkint(L, 2); + if (idx >= 0) { + if (idx < (ptrdiff_t)pt->sizekn) { + copyTV(L, L->top-1, proto_knumtv(pt, idx)); + return 1; + } + } else { + if (~idx < (ptrdiff_t)pt->sizekgc) { + GCobj *gc = proto_kgc(pt, idx); + setgcV(L, L->top-1, gc, ~gc->gch.gct); + return 1; + } + } + return 0; +} + +/* local name = jit.util.funcuvname(func, idx) */ +LJLIB_CF(jit_util_funcuvname) +{ + GCproto *pt = check_Lproto(L, 0); + uint32_t idx = (uint32_t)lj_lib_checkint(L, 2); + if (idx < pt->sizeuv) { + setstrV(L, L->top-1, lj_str_newz(L, lj_debug_uvname(pt, idx))); + return 1; + } + return 0; +} + +/* -- Reflection API for traces ------------------------------------------- */ + +#if LJ_HASJIT + +/* Check trace argument. Must not throw for non-existent trace numbers. */ +static GCtrace *jit_checktrace(lua_State *L) +{ + TraceNo tr = (TraceNo)lj_lib_checkint(L, 1); + jit_State *J = L2J(L); + if (tr > 0 && tr < J->sizetrace) + return traceref(J, tr); + return NULL; +} + +/* Names of link types. ORDER LJ_TRLINK */ +static const char *const jit_trlinkname[] = { + "none", "root", "loop", "tail-recursion", "up-recursion", "down-recursion", + "interpreter", "return" +}; + +/* local info = jit.util.traceinfo(tr) */ +LJLIB_CF(jit_util_traceinfo) +{ + GCtrace *T = jit_checktrace(L); + if (T) { + GCtab *t; + lua_createtable(L, 0, 8); /* Increment hash size if fields are added. */ + t = tabV(L->top-1); + setintfield(L, t, "nins", (int32_t)T->nins - REF_BIAS - 1); + setintfield(L, t, "nk", REF_BIAS - (int32_t)T->nk); + setintfield(L, t, "link", T->link); + setintfield(L, t, "nexit", T->nsnap); + setstrV(L, L->top++, lj_str_newz(L, jit_trlinkname[T->linktype])); + lua_setfield(L, -2, "linktype"); + /* There are many more fields. Add them only when needed. */ + return 1; + } + return 0; +} + +/* local m, ot, op1, op2, prev = jit.util.traceir(tr, idx) */ +LJLIB_CF(jit_util_traceir) +{ + GCtrace *T = jit_checktrace(L); + IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS; + if (T && ref >= REF_BIAS && ref < T->nins) { + IRIns *ir = &T->ir[ref]; + int32_t m = lj_ir_mode[ir->o]; + setintV(L->top-2, m); + setintV(L->top-1, ir->ot); + setintV(L->top++, (int32_t)ir->op1 - (irm_op1(m)==IRMref ? REF_BIAS : 0)); + setintV(L->top++, (int32_t)ir->op2 - (irm_op2(m)==IRMref ? REF_BIAS : 0)); + setintV(L->top++, ir->prev); + return 5; + } + return 0; +} + +/* local k, t [, slot] = jit.util.tracek(tr, idx) */ +LJLIB_CF(jit_util_tracek) +{ + GCtrace *T = jit_checktrace(L); + IRRef ref = (IRRef)lj_lib_checkint(L, 2) + REF_BIAS; + if (T && ref >= T->nk && ref < REF_BIAS) { + IRIns *ir = &T->ir[ref]; + int32_t slot = -1; + if (ir->o == IR_KSLOT) { + slot = ir->op2; + ir = &T->ir[ir->op1]; + } + lj_ir_kvalue(L, L->top-2, ir); + setintV(L->top-1, (int32_t)irt_type(ir->t)); + if (slot == -1) + return 2; + setintV(L->top++, slot); + return 3; + } + return 0; +} + +/* local snap = jit.util.tracesnap(tr, sn) */ +LJLIB_CF(jit_util_tracesnap) +{ + GCtrace *T = jit_checktrace(L); + SnapNo sn = (SnapNo)lj_lib_checkint(L, 2); + if (T && sn < T->nsnap) { + SnapShot *snap = &T->snap[sn]; + SnapEntry *map = &T->snapmap[snap->mapofs]; + MSize n, nent = snap->nent; + GCtab *t; + lua_createtable(L, nent+2, 0); + t = tabV(L->top-1); + setintV(lj_tab_setint(L, t, 0), (int32_t)snap->ref - REF_BIAS); + setintV(lj_tab_setint(L, t, 1), (int32_t)snap->nslots); + for (n = 0; n < nent; n++) + setintV(lj_tab_setint(L, t, (int32_t)(n+2)), (int32_t)map[n]); + setintV(lj_tab_setint(L, t, (int32_t)(nent+2)), (int32_t)SNAP(255, 0, 0)); + return 1; + } + return 0; +} + +/* local mcode, addr, loop = jit.util.tracemc(tr) */ +LJLIB_CF(jit_util_tracemc) +{ + GCtrace *T = jit_checktrace(L); + if (T && T->mcode != NULL) { + setstrV(L, L->top-1, lj_str_new(L, (const char *)T->mcode, T->szmcode)); + setintptrV(L->top++, (intptr_t)(void *)T->mcode); + setintV(L->top++, T->mcloop); + return 3; + } + return 0; +} + +/* local addr = jit.util.traceexitstub([tr,] exitno) */ +LJLIB_CF(jit_util_traceexitstub) +{ +#ifdef EXITSTUBS_PER_GROUP + ExitNo exitno = (ExitNo)lj_lib_checkint(L, 1); + jit_State *J = L2J(L); + if (exitno < EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) { + setintptrV(L->top-1, (intptr_t)(void *)exitstub_addr(J, exitno)); + return 1; + } +#else + if (L->top > L->base+1) { /* Don't throw for one-argument variant. */ + GCtrace *T = jit_checktrace(L); + ExitNo exitno = (ExitNo)lj_lib_checkint(L, 2); + ExitNo maxexit = T->root ? T->nsnap+1 : T->nsnap; + if (T && T->mcode != NULL && exitno < maxexit) { + setintptrV(L->top-1, (intptr_t)(void *)exitstub_trace_addr(T, exitno)); + return 1; + } + } +#endif + return 0; +} + +/* local addr = jit.util.ircalladdr(idx) */ +LJLIB_CF(jit_util_ircalladdr) +{ + uint32_t idx = (uint32_t)lj_lib_checkint(L, 1); + if (idx < IRCALL__MAX) { + setintptrV(L->top-1, (intptr_t)(void *)lj_ir_callinfo[idx].func); + return 1; + } + return 0; +} + +#endif + +#include "lj_libdef.h" + +/* -- jit.opt module ------------------------------------------------------ */ + +#if LJ_HASJIT + +#define LJLIB_MODULE_jit_opt + +/* Parse optimization level. */ +static int jitopt_level(jit_State *J, const char *str) +{ + if (str[0] >= '0' && str[0] <= '9' && str[1] == '\0') { + uint32_t flags; + if (str[0] == '0') flags = JIT_F_OPT_0; + else if (str[0] == '1') flags = JIT_F_OPT_1; + else if (str[0] == '2') flags = JIT_F_OPT_2; + else flags = JIT_F_OPT_3; + J->flags = (J->flags & ~JIT_F_OPT_MASK) | flags; + return 1; /* Ok. */ + } + return 0; /* No match. */ +} + +/* Parse optimization flag. */ +static int jitopt_flag(jit_State *J, const char *str) +{ + const char *lst = JIT_F_OPTSTRING; + uint32_t opt; + int set = 1; + if (str[0] == '+') { + str++; + } else if (str[0] == '-') { + str++; + set = 0; + } else if (str[0] == 'n' && str[1] == 'o') { + str += str[2] == '-' ? 3 : 2; + set = 0; + } + for (opt = JIT_F_OPT_FIRST; ; opt <<= 1) { + size_t len = *(const uint8_t *)lst; + if (len == 0) + break; + if (strncmp(str, lst+1, len) == 0 && str[len] == '\0') { + if (set) J->flags |= opt; else J->flags &= ~opt; + return 1; /* Ok. */ + } + lst += 1+len; + } + return 0; /* No match. */ +} + +/* Parse optimization parameter. */ +static int jitopt_param(jit_State *J, const char *str) +{ + const char *lst = JIT_P_STRING; + int i; + for (i = 0; i < JIT_P__MAX; i++) { + size_t len = *(const uint8_t *)lst; + lua_assert(len != 0); + if (strncmp(str, lst+1, len) == 0 && str[len] == '=') { + int32_t n = 0; + const char *p = &str[len+1]; + while (*p >= '0' && *p <= '9') + n = n*10 + (*p++ - '0'); + if (*p) return 0; /* Malformed number. */ + J->param[i] = n; + if (i == JIT_P_hotloop) + lj_dispatch_init_hotcount(J2G(J)); + return 1; /* Ok. */ + } + lst += 1+len; + } + return 0; /* No match. */ +} + +/* jit.opt.start(flags...) */ +LJLIB_CF(jit_opt_start) +{ + jit_State *J = L2J(L); + int nargs = (int)(L->top - L->base); + if (nargs == 0) { + J->flags = (J->flags & ~JIT_F_OPT_MASK) | JIT_F_OPT_DEFAULT; + } else { + int i; + for (i = 1; i <= nargs; i++) { + const char *str = strdata(lj_lib_checkstr(L, i)); + if (!jitopt_level(J, str) && + !jitopt_flag(J, str) && + !jitopt_param(J, str)) + lj_err_callerv(L, LJ_ERR_JITOPT, str); + } + } + return 0; +} + +#include "lj_libdef.h" + +#endif + +/* -- JIT compiler initialization ----------------------------------------- */ + +#if LJ_HASJIT +/* Default values for JIT parameters. */ +static const int32_t jit_param_default[JIT_P__MAX+1] = { +#define JIT_PARAMINIT(len, name, value) (value), +JIT_PARAMDEF(JIT_PARAMINIT) +#undef JIT_PARAMINIT + 0 +}; +#endif + +#if LJ_TARGET_ARM && LJ_TARGET_LINUX +#include +#endif + +/* Arch-dependent CPU detection. */ +static uint32_t jit_cpudetect(lua_State *L) +{ + uint32_t flags = 0; +#if LJ_TARGET_X86ORX64 + uint32_t vendor[4]; + uint32_t features[4]; + if (lj_vm_cpuid(0, vendor) && lj_vm_cpuid(1, features)) { +#if !LJ_HASJIT +#define JIT_F_CMOV 1 +#define JIT_F_SSE2 2 +#endif + flags |= ((features[3] >> 15)&1) * JIT_F_CMOV; + flags |= ((features[3] >> 26)&1) * JIT_F_SSE2; +#if LJ_HASJIT + flags |= ((features[2] >> 0)&1) * JIT_F_SSE3; + flags |= ((features[2] >> 19)&1) * JIT_F_SSE4_1; + if (vendor[2] == 0x6c65746e) { /* Intel. */ + if ((features[0] & 0x0ff00f00) == 0x00000f00) /* P4. */ + flags |= JIT_F_P4; /* Currently unused. */ + else if ((features[0] & 0x0fff0ff0) == 0x000106c0) /* Atom. */ + flags |= JIT_F_LEA_AGU; + } else if (vendor[2] == 0x444d4163) { /* AMD. */ + uint32_t fam = (features[0] & 0x0ff00f00); + if (fam == 0x00000f00) /* K8. */ + flags |= JIT_F_SPLIT_XMM; + if (fam >= 0x00000f00) /* K8, K10. */ + flags |= JIT_F_PREFER_IMUL; + } +#endif + } + /* Check for required instruction set support on x86 (unnecessary on x64). */ +#if LJ_TARGET_X86 +#if !defined(LUAJIT_CPU_NOCMOV) + if (!(flags & JIT_F_CMOV)) + luaL_error(L, "CPU not supported"); +#endif +#if defined(LUAJIT_CPU_SSE2) + if (!(flags & JIT_F_SSE2)) + luaL_error(L, "CPU does not support SSE2 (recompile without -DLUAJIT_CPU_SSE2)"); +#endif +#endif +#elif LJ_TARGET_ARM +#if LJ_HASJIT + int ver = LJ_ARCH_VERSION; /* Compile-time ARM CPU detection. */ +#if LJ_TARGET_LINUX + if (ver < 70) { /* Runtime ARM CPU detection. */ + struct utsname ut; + uname(&ut); + if (strncmp(ut.machine, "armv", 4) == 0) { + if (ut.machine[4] >= '7') + ver = 70; + else if (ut.machine[4] == '6') + ver = 60; + } + } +#endif + flags |= ver >= 70 ? JIT_F_ARMV7 : + ver >= 61 ? JIT_F_ARMV6T2_ : + ver >= 60 ? JIT_F_ARMV6_ : 0; + flags |= LJ_ARCH_HASFPU == 0 ? 0 : ver >= 70 ? JIT_F_VFPV3 : JIT_F_VFPV2; +#endif +#elif LJ_TARGET_PPC +#if LJ_HASJIT +#if LJ_ARCH_SQRT + flags |= JIT_F_SQRT; +#endif +#if LJ_ARCH_ROUND + flags |= JIT_F_ROUND; +#endif +#endif +#elif LJ_TARGET_PPCSPE + /* Nothing to do. */ +#elif LJ_TARGET_MIPS +#if LJ_HASJIT + /* Compile-time MIPS CPU detection. */ +#if LJ_ARCH_VERSION >= 20 + flags |= JIT_F_MIPS32R2; +#endif + /* Runtime MIPS CPU detection. */ +#if defined(__GNUC__) + if (!(flags & JIT_F_MIPS32R2)) { + int x; + /* On MIPS32R1 rotr is treated as srl. rotr r2,r2,1 -> srl r2,r2,1. */ + __asm__("li $2, 1\n\t.long 0x00221042\n\tmove %0, $2" : "=r"(x) : : "$2"); + if (x) flags |= JIT_F_MIPS32R2; /* Either 0x80000000 (R2) or 0 (R1). */ + } +#endif +#endif +#else +#error "Missing CPU detection for this architecture" +#endif + UNUSED(L); + return flags; +} + +/* Initialize JIT compiler. */ +static void jit_init(lua_State *L) +{ + uint32_t flags = jit_cpudetect(L); +#if LJ_HASJIT + jit_State *J = L2J(L); +#if LJ_TARGET_X86 + /* Silently turn off the JIT compiler on CPUs without SSE2. */ + if ((flags & JIT_F_SSE2)) +#endif + J->flags = flags | JIT_F_ON | JIT_F_OPT_DEFAULT; + memcpy(J->param, jit_param_default, sizeof(J->param)); + lj_dispatch_update(G(L)); +#else + UNUSED(flags); +#endif +} + +LUALIB_API int luaopen_jit(lua_State *L) +{ + lua_pushliteral(L, LJ_OS_NAME); + lua_pushliteral(L, LJ_ARCH_NAME); + lua_pushinteger(L, LUAJIT_VERSION_NUM); + lua_pushliteral(L, LUAJIT_VERSION); + LJ_LIB_REG(L, LUA_JITLIBNAME, jit); +#ifndef LUAJIT_DISABLE_JITUTIL + LJ_LIB_REG(L, "jit.util", jit_util); +#endif +#if LJ_HASJIT + LJ_LIB_REG(L, "jit.opt", jit_opt); +#endif + L->top -= 2; + jit_init(L); + return 1; +} + diff --git a/external/lua/luajit/src/src/lib_math.c b/external/lua/luajit/src/src/lib_math.c new file mode 100644 index 0000000000..b23d9a2dc0 --- /dev/null +++ b/external/lua/luajit/src/src/lib_math.c @@ -0,0 +1,233 @@ +/* +** Math library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include + +#define lib_math_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_obj.h" +#include "lj_lib.h" +#include "lj_vm.h" + +/* ------------------------------------------------------------------------ */ + +#define LJLIB_MODULE_math + +LJLIB_ASM(math_abs) LJLIB_REC(.) +{ + lj_lib_checknumber(L, 1); + return FFH_RETRY; +} +LJLIB_ASM_(math_floor) LJLIB_REC(math_round IRFPM_FLOOR) +LJLIB_ASM_(math_ceil) LJLIB_REC(math_round IRFPM_CEIL) + +LJLIB_ASM(math_sqrt) LJLIB_REC(math_unary IRFPM_SQRT) +{ + lj_lib_checknum(L, 1); + return FFH_RETRY; +} +LJLIB_ASM_(math_log10) LJLIB_REC(math_unary IRFPM_LOG10) +LJLIB_ASM_(math_exp) LJLIB_REC(math_unary IRFPM_EXP) +LJLIB_ASM_(math_sin) LJLIB_REC(math_unary IRFPM_SIN) +LJLIB_ASM_(math_cos) LJLIB_REC(math_unary IRFPM_COS) +LJLIB_ASM_(math_tan) LJLIB_REC(math_unary IRFPM_TAN) +LJLIB_ASM_(math_asin) LJLIB_REC(math_atrig FF_math_asin) +LJLIB_ASM_(math_acos) LJLIB_REC(math_atrig FF_math_acos) +LJLIB_ASM_(math_atan) LJLIB_REC(math_atrig FF_math_atan) +LJLIB_ASM_(math_sinh) LJLIB_REC(math_htrig IRCALL_sinh) +LJLIB_ASM_(math_cosh) LJLIB_REC(math_htrig IRCALL_cosh) +LJLIB_ASM_(math_tanh) LJLIB_REC(math_htrig IRCALL_tanh) +LJLIB_ASM_(math_frexp) +LJLIB_ASM_(math_modf) LJLIB_REC(.) + +LJLIB_ASM(math_log) LJLIB_REC(math_log) +{ + double x = lj_lib_checknum(L, 1); + if (L->base+1 < L->top) { + double y = lj_lib_checknum(L, 2); +#ifdef LUAJIT_NO_LOG2 + x = log(x); y = 1.0 / log(y); +#else + x = lj_vm_log2(x); y = 1.0 / lj_vm_log2(y); +#endif + setnumV(L->base-1, x*y); /* Do NOT join the expression to x / y. */ + return FFH_RES(1); + } + return FFH_RETRY; +} + +LJLIB_PUSH(57.29577951308232) +LJLIB_ASM_(math_deg) LJLIB_REC(math_degrad) + +LJLIB_PUSH(0.017453292519943295) +LJLIB_ASM_(math_rad) LJLIB_REC(math_degrad) + +LJLIB_ASM(math_atan2) LJLIB_REC(.) +{ + lj_lib_checknum(L, 1); + lj_lib_checknum(L, 2); + return FFH_RETRY; +} +LJLIB_ASM_(math_pow) LJLIB_REC(.) +LJLIB_ASM_(math_fmod) + +LJLIB_ASM(math_ldexp) LJLIB_REC(.) +{ + lj_lib_checknum(L, 1); +#if LJ_DUALNUM && !LJ_TARGET_X86ORX64 + lj_lib_checkint(L, 2); +#else + lj_lib_checknum(L, 2); +#endif + return FFH_RETRY; +} + +LJLIB_ASM(math_min) LJLIB_REC(math_minmax IR_MIN) +{ + int i = 0; + do { lj_lib_checknumber(L, ++i); } while (L->base+i < L->top); + return FFH_RETRY; +} +LJLIB_ASM_(math_max) LJLIB_REC(math_minmax IR_MAX) + +LJLIB_PUSH(3.14159265358979323846) LJLIB_SET(pi) +LJLIB_PUSH(1e310) LJLIB_SET(huge) + +/* ------------------------------------------------------------------------ */ + +/* This implements a Tausworthe PRNG with period 2^223. Based on: +** Tables of maximally-equidistributed combined LFSR generators, +** Pierre L'Ecuyer, 1991, table 3, 1st entry. +** Full-period ME-CF generator with L=64, J=4, k=223, N1=49. +*/ + +/* PRNG state. */ +struct RandomState { + uint64_t gen[4]; /* State of the 4 LFSR generators. */ + int valid; /* State is valid. */ +}; + +/* Union needed for bit-pattern conversion between uint64_t and double. */ +typedef union { uint64_t u64; double d; } U64double; + +/* Update generator i and compute a running xor of all states. */ +#define TW223_GEN(i, k, q, s) \ + z = rs->gen[i]; \ + z = (((z<> (k-s)) ^ ((z&((uint64_t)(int64_t)-1 << (64-k)))<gen[i] = z; + +/* PRNG step function. Returns a double in the range 1.0 <= d < 2.0. */ +LJ_NOINLINE uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs) +{ + uint64_t z, r = 0; + TW223_GEN(0, 63, 31, 18) + TW223_GEN(1, 58, 19, 28) + TW223_GEN(2, 55, 24, 7) + TW223_GEN(3, 47, 21, 8) + return (r & U64x(000fffff,ffffffff)) | U64x(3ff00000,00000000); +} + +/* PRNG initialization function. */ +static void random_init(RandomState *rs, double d) +{ + uint32_t r = 0x11090601; /* 64-k[i] as four 8 bit constants. */ + int i; + for (i = 0; i < 4; i++) { + U64double u; + uint32_t m = 1u << (r&255); + r >>= 8; + u.d = d = d * 3.14159265358979323846 + 2.7182818284590452354; + if (u.u64 < m) u.u64 += m; /* Ensure k[i] MSB of gen[i] are non-zero. */ + rs->gen[i] = u.u64; + } + rs->valid = 1; + for (i = 0; i < 10; i++) + lj_math_random_step(rs); +} + +/* PRNG extract function. */ +LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ +LJLIB_CF(math_random) LJLIB_REC(.) +{ + int n = (int)(L->top - L->base); + RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); + U64double u; + double d; + if (LJ_UNLIKELY(!rs->valid)) random_init(rs, 0.0); + u.u64 = lj_math_random_step(rs); + d = u.d - 1.0; + if (n > 0) { +#if LJ_DUALNUM + int isint = 1; + double r1; + lj_lib_checknumber(L, 1); + if (tvisint(L->base)) { + r1 = (lua_Number)intV(L->base); + } else { + isint = 0; + r1 = numV(L->base); + } +#else + double r1 = lj_lib_checknum(L, 1); +#endif + if (n == 1) { + d = lj_vm_floor(d*r1) + 1.0; /* d is an int in range [1, r1] */ + } else { +#if LJ_DUALNUM + double r2; + lj_lib_checknumber(L, 2); + if (tvisint(L->base+1)) { + r2 = (lua_Number)intV(L->base+1); + } else { + isint = 0; + r2 = numV(L->base+1); + } +#else + double r2 = lj_lib_checknum(L, 2); +#endif + d = lj_vm_floor(d*(r2-r1+1.0)) + r1; /* d is an int in range [r1, r2] */ + } +#if LJ_DUALNUM + if (isint) { + setintV(L->top-1, lj_num2int(d)); + return 1; + } +#endif + } /* else: d is a double in range [0, 1] */ + setnumV(L->top++, d); + return 1; +} + +/* PRNG seed function. */ +LJLIB_PUSH(top-2) /* Upvalue holds userdata with RandomState. */ +LJLIB_CF(math_randomseed) +{ + RandomState *rs = (RandomState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); + random_init(rs, lj_lib_checknum(L, 1)); + return 0; +} + +/* ------------------------------------------------------------------------ */ + +#include "lj_libdef.h" + +LUALIB_API int luaopen_math(lua_State *L) +{ + RandomState *rs; + rs = (RandomState *)lua_newuserdata(L, sizeof(RandomState)); + rs->valid = 0; /* Use lazy initialization to save some time on startup. */ + LJ_LIB_REG(L, LUA_MATHLIBNAME, math); +#if defined(LUA_COMPAT_MOD) && !LJ_52 + lua_getfield(L, -1, "fmod"); + lua_setfield(L, -2, "mod"); +#endif + return 1; +} + diff --git a/external/lua/luajit/src/src/lib_os.c b/external/lua/luajit/src/src/lib_os.c new file mode 100644 index 0000000000..0a78412909 --- /dev/null +++ b/external/lua/luajit/src/src/lib_os.c @@ -0,0 +1,280 @@ +/* +** OS library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#include +#include +#include + +#define lib_os_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_obj.h" +#include "lj_err.h" +#include "lj_lib.h" + +#if LJ_TARGET_POSIX +#include +#else +#include +#endif + +/* ------------------------------------------------------------------------ */ + +#define LJLIB_MODULE_os + +LJLIB_CF(os_execute) +{ +#if LJ_TARGET_CONSOLE +#if LJ_52 + errno = ENOSYS; + return luaL_fileresult(L, 0, NULL); +#else + lua_pushinteger(L, -1); + return 1; +#endif +#else + const char *cmd = luaL_optstring(L, 1, NULL); + int stat = system(cmd); +#if LJ_52 + if (cmd) + return luaL_execresult(L, stat); + setboolV(L->top++, 1); +#else + setintV(L->top++, stat); +#endif + return 1; +#endif +} + +LJLIB_CF(os_remove) +{ + const char *filename = luaL_checkstring(L, 1); + return luaL_fileresult(L, remove(filename) == 0, filename); +} + +LJLIB_CF(os_rename) +{ + const char *fromname = luaL_checkstring(L, 1); + const char *toname = luaL_checkstring(L, 2); + return luaL_fileresult(L, rename(fromname, toname) == 0, fromname); +} + +LJLIB_CF(os_tmpname) +{ +#if LJ_TARGET_PS3 + lj_err_caller(L, LJ_ERR_OSUNIQF); + return 0; +#else +#if LJ_TARGET_POSIX + char buf[15+1]; + int fp; + strcpy(buf, "/tmp/lua_XXXXXX"); + fp = mkstemp(buf); + if (fp != -1) + close(fp); + else + lj_err_caller(L, LJ_ERR_OSUNIQF); +#else + char buf[L_tmpnam]; + if (tmpnam(buf) == NULL) + lj_err_caller(L, LJ_ERR_OSUNIQF); +#endif + lua_pushstring(L, buf); + return 1; +#endif +} + +LJLIB_CF(os_getenv) +{ +#if LJ_TARGET_CONSOLE + lua_pushnil(L); +#else + lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */ +#endif + return 1; +} + +LJLIB_CF(os_exit) +{ + int status; + if (L->base < L->top && tvisbool(L->base)) + status = boolV(L->base) ? EXIT_SUCCESS : EXIT_FAILURE; + else + status = lj_lib_optint(L, 1, EXIT_SUCCESS); + if (L->base+1 < L->top && tvistruecond(L->base+1)) + lua_close(L); + exit(status); + return 0; /* Unreachable. */ +} + +LJLIB_CF(os_clock) +{ + setnumV(L->top++, ((lua_Number)clock())*(1.0/(lua_Number)CLOCKS_PER_SEC)); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +static void setfield(lua_State *L, const char *key, int value) +{ + lua_pushinteger(L, value); + lua_setfield(L, -2, key); +} + +static void setboolfield(lua_State *L, const char *key, int value) +{ + if (value < 0) /* undefined? */ + return; /* does not set field */ + lua_pushboolean(L, value); + lua_setfield(L, -2, key); +} + +static int getboolfield(lua_State *L, const char *key) +{ + int res; + lua_getfield(L, -1, key); + res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); + lua_pop(L, 1); + return res; +} + +static int getfield(lua_State *L, const char *key, int d) +{ + int res; + lua_getfield(L, -1, key); + if (lua_isnumber(L, -1)) { + res = (int)lua_tointeger(L, -1); + } else { + if (d < 0) + lj_err_callerv(L, LJ_ERR_OSDATEF, key); + res = d; + } + lua_pop(L, 1); + return res; +} + +LJLIB_CF(os_date) +{ + const char *s = luaL_optstring(L, 1, "%c"); + time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); + struct tm *stm; +#if LJ_TARGET_POSIX + struct tm rtm; +#endif + if (*s == '!') { /* UTC? */ + s++; /* Skip '!' */ +#if LJ_TARGET_POSIX + stm = gmtime_r(&t, &rtm); +#else + stm = gmtime(&t); +#endif + } else { +#if LJ_TARGET_POSIX + stm = localtime_r(&t, &rtm); +#else + stm = localtime(&t); +#endif + } + if (stm == NULL) { /* Invalid date? */ + setnilV(L->top-1); + } else if (strcmp(s, "*t") == 0) { + lua_createtable(L, 0, 9); /* 9 = number of fields */ + setfield(L, "sec", stm->tm_sec); + setfield(L, "min", stm->tm_min); + setfield(L, "hour", stm->tm_hour); + setfield(L, "day", stm->tm_mday); + setfield(L, "month", stm->tm_mon+1); + setfield(L, "year", stm->tm_year+1900); + setfield(L, "wday", stm->tm_wday+1); + setfield(L, "yday", stm->tm_yday+1); + setboolfield(L, "isdst", stm->tm_isdst); + } else { + char cc[3]; + luaL_Buffer b; + cc[0] = '%'; cc[2] = '\0'; + luaL_buffinit(L, &b); + for (; *s; s++) { + if (*s != '%' || *(s + 1) == '\0') { /* No conversion specifier? */ + luaL_addchar(&b, *s); + } else { + size_t reslen; + char buff[200]; /* Should be big enough for any conversion result. */ + cc[1] = *(++s); + reslen = strftime(buff, sizeof(buff), cc, stm); + luaL_addlstring(&b, buff, reslen); + } + } + luaL_pushresult(&b); + } + return 1; +} + +LJLIB_CF(os_time) +{ + time_t t; + if (lua_isnoneornil(L, 1)) { /* called without args? */ + t = time(NULL); /* get current time */ + } else { + struct tm ts; + luaL_checktype(L, 1, LUA_TTABLE); + lua_settop(L, 1); /* make sure table is at the top */ + ts.tm_sec = getfield(L, "sec", 0); + ts.tm_min = getfield(L, "min", 0); + ts.tm_hour = getfield(L, "hour", 12); + ts.tm_mday = getfield(L, "day", -1); + ts.tm_mon = getfield(L, "month", -1) - 1; + ts.tm_year = getfield(L, "year", -1) - 1900; + ts.tm_isdst = getboolfield(L, "isdst"); + t = mktime(&ts); + } + if (t == (time_t)(-1)) + lua_pushnil(L); + else + lua_pushnumber(L, (lua_Number)t); + return 1; +} + +LJLIB_CF(os_difftime) +{ + lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)), + (time_t)(luaL_optnumber(L, 2, (lua_Number)0)))); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +LJLIB_CF(os_setlocale) +{ + GCstr *s = lj_lib_optstr(L, 1); + const char *str = s ? strdata(s) : NULL; + int opt = lj_lib_checkopt(L, 2, 6, + "\5ctype\7numeric\4time\7collate\10monetary\1\377\3all"); + if (opt == 0) opt = LC_CTYPE; + else if (opt == 1) opt = LC_NUMERIC; + else if (opt == 2) opt = LC_TIME; + else if (opt == 3) opt = LC_COLLATE; + else if (opt == 4) opt = LC_MONETARY; + else if (opt == 6) opt = LC_ALL; + lua_pushstring(L, setlocale(opt, str)); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +#include "lj_libdef.h" + +LUALIB_API int luaopen_os(lua_State *L) +{ + LJ_LIB_REG(L, LUA_OSLIBNAME, os); + return 1; +} + diff --git a/external/lua/luajit/src/src/lib_package.c b/external/lua/luajit/src/src/lib_package.c new file mode 100644 index 0000000000..f0e672d21a --- /dev/null +++ b/external/lua/luajit/src/src/lib_package.c @@ -0,0 +1,605 @@ +/* +** Package library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2012 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lib_package_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_obj.h" +#include "lj_err.h" +#include "lj_lib.h" + +/* ------------------------------------------------------------------------ */ + +/* Error codes for ll_loadfunc. */ +#define PACKAGE_ERR_LIB 1 +#define PACKAGE_ERR_FUNC 2 +#define PACKAGE_ERR_LOAD 3 + +/* Redefined in platform specific part. */ +#define PACKAGE_LIB_FAIL "open" +#define setprogdir(L) ((void)0) + +/* Symbol name prefixes. */ +#define SYMPREFIX_CF "luaopen_%s" +#define SYMPREFIX_BC "luaJIT_BC_%s" + +#if LJ_TARGET_DLOPEN + +#include + +static void ll_unloadlib(void *lib) +{ + dlclose(lib); +} + +static void *ll_load(lua_State *L, const char *path, int gl) +{ + void *lib = dlopen(path, RTLD_NOW | (gl ? RTLD_GLOBAL : RTLD_LOCAL)); + if (lib == NULL) lua_pushstring(L, dlerror()); + return lib; +} + +static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) +{ + lua_CFunction f = (lua_CFunction)dlsym(lib, sym); + if (f == NULL) lua_pushstring(L, dlerror()); + return f; +} + +static const char *ll_bcsym(void *lib, const char *sym) +{ +#if defined(RTLD_DEFAULT) + if (lib == NULL) lib = RTLD_DEFAULT; +#elif LJ_TARGET_OSX || LJ_TARGET_BSD + if (lib == NULL) lib = (void *)(intptr_t)-2; +#endif + return (const char *)dlsym(lib, sym); +} + +#elif LJ_TARGET_WINDOWS + +#define WIN32_LEAN_AND_MEAN +#ifndef WINVER +#define WINVER 0x0500 +#endif +#include + +#ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS +#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4 +#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2 +BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*); +#endif + +#undef setprogdir + +static void setprogdir(lua_State *L) +{ + char buff[MAX_PATH + 1]; + char *lb; + DWORD nsize = sizeof(buff); + DWORD n = GetModuleFileNameA(NULL, buff, nsize); + if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) { + luaL_error(L, "unable to get ModuleFileName"); + } else { + *lb = '\0'; + luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); + lua_remove(L, -2); /* remove original string */ + } +} + +static void pusherror(lua_State *L) +{ + DWORD error = GetLastError(); + char buffer[128]; + if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, error, 0, buffer, sizeof(buffer), NULL)) + lua_pushstring(L, buffer); + else + lua_pushfstring(L, "system error %d\n", error); +} + +static void ll_unloadlib(void *lib) +{ + FreeLibrary((HINSTANCE)lib); +} + +static void *ll_load(lua_State *L, const char *path, int gl) +{ + HINSTANCE lib = LoadLibraryA(path); + if (lib == NULL) pusherror(L); + UNUSED(gl); + return lib; +} + +static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) +{ + lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); + if (f == NULL) pusherror(L); + return f; +} + +static const char *ll_bcsym(void *lib, const char *sym) +{ + if (lib) { + return (const char *)GetProcAddress((HINSTANCE)lib, sym); + } else { + HINSTANCE h = GetModuleHandleA(NULL); + const char *p = (const char *)GetProcAddress(h, sym); + if (p == NULL && GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (const char *)ll_bcsym, &h)) + p = (const char *)GetProcAddress(h, sym); + return p; + } +} + +#else + +#undef PACKAGE_LIB_FAIL +#define PACKAGE_LIB_FAIL "absent" + +#define DLMSG "dynamic libraries not enabled; no support for target OS" + +static void ll_unloadlib(void *lib) +{ + UNUSED(lib); +} + +static void *ll_load(lua_State *L, const char *path, int gl) +{ + UNUSED(path); UNUSED(gl); + lua_pushliteral(L, DLMSG); + return NULL; +} + +static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) +{ + UNUSED(lib); UNUSED(sym); + lua_pushliteral(L, DLMSG); + return NULL; +} + +static const char *ll_bcsym(void *lib, const char *sym) +{ + UNUSED(lib); UNUSED(sym); + return NULL; +} + +#endif + +/* ------------------------------------------------------------------------ */ + +static void **ll_register(lua_State *L, const char *path) +{ + void **plib; + lua_pushfstring(L, "LOADLIB: %s", path); + lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ + if (!lua_isnil(L, -1)) { /* is there an entry? */ + plib = (void **)lua_touserdata(L, -1); + } else { /* no entry yet; create one */ + lua_pop(L, 1); + plib = (void **)lua_newuserdata(L, sizeof(void *)); + *plib = NULL; + luaL_getmetatable(L, "_LOADLIB"); + lua_setmetatable(L, -2); + lua_pushfstring(L, "LOADLIB: %s", path); + lua_pushvalue(L, -2); + lua_settable(L, LUA_REGISTRYINDEX); + } + return plib; +} + +static const char *mksymname(lua_State *L, const char *modname, + const char *prefix) +{ + const char *funcname; + const char *mark = strchr(modname, *LUA_IGMARK); + if (mark) modname = mark + 1; + funcname = luaL_gsub(L, modname, ".", "_"); + funcname = lua_pushfstring(L, prefix, funcname); + lua_remove(L, -2); /* remove 'gsub' result */ + return funcname; +} + +static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r) +{ + void **reg = ll_register(L, path); + if (*reg == NULL) *reg = ll_load(L, path, (*name == '*')); + if (*reg == NULL) { + return PACKAGE_ERR_LIB; /* Unable to load library. */ + } else if (*name == '*') { /* Only load library into global namespace. */ + lua_pushboolean(L, 1); + return 0; + } else { + const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF); + lua_CFunction f = ll_sym(L, *reg, sym); + if (f) { + lua_pushcfunction(L, f); + return 0; + } + if (!r) { + const char *bcdata = ll_bcsym(*reg, mksymname(L, name, SYMPREFIX_BC)); + lua_pop(L, 1); + if (bcdata) { + if (luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0) + return PACKAGE_ERR_LOAD; + return 0; + } + } + return PACKAGE_ERR_FUNC; /* Unable to find function. */ + } +} + +static int lj_cf_package_loadlib(lua_State *L) +{ + const char *path = luaL_checkstring(L, 1); + const char *init = luaL_checkstring(L, 2); + int st = ll_loadfunc(L, path, init, 1); + if (st == 0) { /* no errors? */ + return 1; /* return the loaded function */ + } else { /* error; error message is on stack top */ + lua_pushnil(L); + lua_insert(L, -2); + lua_pushstring(L, (st == PACKAGE_ERR_LIB) ? PACKAGE_LIB_FAIL : "init"); + return 3; /* return nil, error message, and where */ + } +} + +static int lj_cf_package_unloadlib(lua_State *L) +{ + void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); + if (*lib) ll_unloadlib(*lib); + *lib = NULL; /* mark library as closed */ + return 0; +} + +/* ------------------------------------------------------------------------ */ + +static int readable(const char *filename) +{ + FILE *f = fopen(filename, "r"); /* try to open file */ + if (f == NULL) return 0; /* open failed */ + fclose(f); + return 1; +} + +static const char *pushnexttemplate(lua_State *L, const char *path) +{ + const char *l; + while (*path == *LUA_PATHSEP) path++; /* skip separators */ + if (*path == '\0') return NULL; /* no more templates */ + l = strchr(path, *LUA_PATHSEP); /* find next separator */ + if (l == NULL) l = path + strlen(path); + lua_pushlstring(L, path, (size_t)(l - path)); /* template */ + return l; +} + +static const char *searchpath (lua_State *L, const char *name, + const char *path, const char *sep, + const char *dirsep) +{ + luaL_Buffer msg; /* to build error message */ + luaL_buffinit(L, &msg); + if (*sep != '\0') /* non-empty separator? */ + name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ + while ((path = pushnexttemplate(L, path)) != NULL) { + const char *filename = luaL_gsub(L, lua_tostring(L, -1), + LUA_PATH_MARK, name); + lua_remove(L, -2); /* remove path template */ + if (readable(filename)) /* does file exist and is readable? */ + return filename; /* return that file name */ + lua_pushfstring(L, "\n\tno file " LUA_QS, filename); + lua_remove(L, -2); /* remove file name */ + luaL_addvalue(&msg); /* concatenate error msg. entry */ + } + luaL_pushresult(&msg); /* create error message */ + return NULL; /* not found */ +} + +static int lj_cf_package_searchpath(lua_State *L) +{ + const char *f = searchpath(L, luaL_checkstring(L, 1), + luaL_checkstring(L, 2), + luaL_optstring(L, 3, "."), + luaL_optstring(L, 4, LUA_DIRSEP)); + if (f != NULL) { + return 1; + } else { /* error message is on top of the stack */ + lua_pushnil(L); + lua_insert(L, -2); + return 2; /* return nil + error message */ + } +} + +static const char *findfile(lua_State *L, const char *name, + const char *pname) +{ + const char *path; + lua_getfield(L, LUA_ENVIRONINDEX, pname); + path = lua_tostring(L, -1); + if (path == NULL) + luaL_error(L, LUA_QL("package.%s") " must be a string", pname); + return searchpath(L, name, path, ".", LUA_DIRSEP); +} + +static void loaderror(lua_State *L, const char *filename) +{ + luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", + lua_tostring(L, 1), filename, lua_tostring(L, -1)); +} + +static int lj_cf_package_loader_lua(lua_State *L) +{ + const char *filename; + const char *name = luaL_checkstring(L, 1); + filename = findfile(L, name, "path"); + if (filename == NULL) return 1; /* library not found in this path */ + if (luaL_loadfile(L, filename) != 0) + loaderror(L, filename); + return 1; /* library loaded successfully */ +} + +static int lj_cf_package_loader_c(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + const char *filename = findfile(L, name, "cpath"); + if (filename == NULL) return 1; /* library not found in this path */ + if (ll_loadfunc(L, filename, name, 0) != 0) + loaderror(L, filename); + return 1; /* library loaded successfully */ +} + +static int lj_cf_package_loader_croot(lua_State *L) +{ + const char *filename; + const char *name = luaL_checkstring(L, 1); + const char *p = strchr(name, '.'); + int st; + if (p == NULL) return 0; /* is root */ + lua_pushlstring(L, name, (size_t)(p - name)); + filename = findfile(L, lua_tostring(L, -1), "cpath"); + if (filename == NULL) return 1; /* root not found */ + if ((st = ll_loadfunc(L, filename, name, 0)) != 0) { + if (st != PACKAGE_ERR_FUNC) loaderror(L, filename); /* real error */ + lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, + name, filename); + return 1; /* function not found */ + } + return 1; +} + +static int lj_cf_package_loader_preload(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + lua_getfield(L, LUA_ENVIRONINDEX, "preload"); + if (!lua_istable(L, -1)) + luaL_error(L, LUA_QL("package.preload") " must be a table"); + lua_getfield(L, -1, name); + if (lua_isnil(L, -1)) { /* Not found? */ + const char *bcname = mksymname(L, name, SYMPREFIX_BC); + const char *bcdata = ll_bcsym(NULL, bcname); + if (bcdata == NULL || luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0) + lua_pushfstring(L, "\n\tno field package.preload['%s']", name); + } + return 1; +} + +/* ------------------------------------------------------------------------ */ + +static const int sentinel_ = 0; +#define sentinel ((void *)&sentinel_) + +static int lj_cf_package_require(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + int i; + lua_settop(L, 1); /* _LOADED table will be at index 2 */ + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, 2, name); + if (lua_toboolean(L, -1)) { /* is it there? */ + if (lua_touserdata(L, -1) == sentinel) /* check loops */ + luaL_error(L, "loop or previous error loading module " LUA_QS, name); + return 1; /* package is already loaded */ + } + /* else must load it; iterate over available loaders */ + lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); + if (!lua_istable(L, -1)) + luaL_error(L, LUA_QL("package.loaders") " must be a table"); + lua_pushliteral(L, ""); /* error message accumulator */ + for (i = 1; ; i++) { + lua_rawgeti(L, -2, i); /* get a loader */ + if (lua_isnil(L, -1)) + luaL_error(L, "module " LUA_QS " not found:%s", + name, lua_tostring(L, -2)); + lua_pushstring(L, name); + lua_call(L, 1, 1); /* call it */ + if (lua_isfunction(L, -1)) /* did it find module? */ + break; /* module loaded successfully */ + else if (lua_isstring(L, -1)) /* loader returned error message? */ + lua_concat(L, 2); /* accumulate it */ + else + lua_pop(L, 1); + } + lua_pushlightuserdata(L, sentinel); + lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ + lua_pushstring(L, name); /* pass name as argument to module */ + lua_call(L, 1, 1); /* run loaded module */ + if (!lua_isnil(L, -1)) /* non-nil return? */ + lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ + lua_getfield(L, 2, name); + if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ + lua_pushboolean(L, 1); /* use true as result */ + lua_pushvalue(L, -1); /* extra copy to be returned */ + lua_setfield(L, 2, name); /* _LOADED[name] = true */ + } + lj_lib_checkfpu(L); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +static void setfenv(lua_State *L) +{ + lua_Debug ar; + if (lua_getstack(L, 1, &ar) == 0 || + lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ + lua_iscfunction(L, -1)) + luaL_error(L, LUA_QL("module") " not called from a Lua function"); + lua_pushvalue(L, -2); + lua_setfenv(L, -2); + lua_pop(L, 1); +} + +static void dooptions(lua_State *L, int n) +{ + int i; + for (i = 2; i <= n; i++) { + lua_pushvalue(L, i); /* get option (a function) */ + lua_pushvalue(L, -2); /* module */ + lua_call(L, 1, 0); + } +} + +static void modinit(lua_State *L, const char *modname) +{ + const char *dot; + lua_pushvalue(L, -1); + lua_setfield(L, -2, "_M"); /* module._M = module */ + lua_pushstring(L, modname); + lua_setfield(L, -2, "_NAME"); + dot = strrchr(modname, '.'); /* look for last dot in module name */ + if (dot == NULL) dot = modname; else dot++; + /* set _PACKAGE as package name (full module name minus last part) */ + lua_pushlstring(L, modname, (size_t)(dot - modname)); + lua_setfield(L, -2, "_PACKAGE"); +} + +static int lj_cf_package_module(lua_State *L) +{ + const char *modname = luaL_checkstring(L, 1); + int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ + if (!lua_istable(L, -1)) { /* not found? */ + lua_pop(L, 1); /* remove previous result */ + /* try global variable (and create one if it does not exist) */ + if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) + lj_err_callerv(L, LJ_ERR_BADMODN, modname); + lua_pushvalue(L, -1); + lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ + } + /* check whether table already has a _NAME field */ + lua_getfield(L, -1, "_NAME"); + if (!lua_isnil(L, -1)) { /* is table an initialized module? */ + lua_pop(L, 1); + } else { /* no; initialize it */ + lua_pop(L, 1); + modinit(L, modname); + } + lua_pushvalue(L, -1); + setfenv(L); + dooptions(L, loaded - 1); + return 0; +} + +static int lj_cf_package_seeall(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TTABLE); + if (!lua_getmetatable(L, 1)) { + lua_createtable(L, 0, 1); /* create new metatable */ + lua_pushvalue(L, -1); + lua_setmetatable(L, 1); + } + lua_pushvalue(L, LUA_GLOBALSINDEX); + lua_setfield(L, -2, "__index"); /* mt.__index = _G */ + return 0; +} + +/* ------------------------------------------------------------------------ */ + +#define AUXMARK "\1" + +static void setpath(lua_State *L, const char *fieldname, const char *envname, + const char *def, int noenv) +{ +#if LJ_TARGET_CONSOLE + const char *path = NULL; + UNUSED(envname); +#else + const char *path = getenv(envname); +#endif + if (path == NULL || noenv) { + lua_pushstring(L, def); + } else { + path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, + LUA_PATHSEP AUXMARK LUA_PATHSEP); + luaL_gsub(L, path, AUXMARK, def); + lua_remove(L, -2); + } + setprogdir(L); + lua_setfield(L, -2, fieldname); +} + +static const luaL_Reg package_lib[] = { + { "loadlib", lj_cf_package_loadlib }, + { "searchpath", lj_cf_package_searchpath }, + { "seeall", lj_cf_package_seeall }, + { NULL, NULL } +}; + +static const luaL_Reg package_global[] = { + { "module", lj_cf_package_module }, + { "require", lj_cf_package_require }, + { NULL, NULL } +}; + +static const lua_CFunction package_loaders[] = +{ + lj_cf_package_loader_preload, + lj_cf_package_loader_lua, + lj_cf_package_loader_c, + lj_cf_package_loader_croot, + NULL +}; + +LUALIB_API int luaopen_package(lua_State *L) +{ + int i; + int noenv; + luaL_newmetatable(L, "_LOADLIB"); + lj_lib_pushcf(L, lj_cf_package_unloadlib, 1); + lua_setfield(L, -2, "__gc"); + luaL_register(L, LUA_LOADLIBNAME, package_lib); + lua_pushvalue(L, -1); + lua_replace(L, LUA_ENVIRONINDEX); + lua_createtable(L, sizeof(package_loaders)/sizeof(package_loaders[0])-1, 0); + for (i = 0; package_loaders[i] != NULL; i++) { + lj_lib_pushcf(L, package_loaders[i], 1); + lua_rawseti(L, -2, i+1); + } + lua_setfield(L, -2, "loaders"); + lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); + noenv = lua_toboolean(L, -1); + lua_pop(L, 1); + setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv); + setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv); + lua_pushliteral(L, LUA_PATH_CONFIG); + lua_setfield(L, -2, "config"); + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); + lua_setfield(L, -2, "loaded"); + luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4); + lua_setfield(L, -2, "preload"); + lua_pushvalue(L, LUA_GLOBALSINDEX); + luaL_register(L, NULL, package_global); + lua_pop(L, 1); + return 1; +} + diff --git a/external/lua/luajit/src/src/lib_string.c b/external/lua/luajit/src/src/lib_string.c new file mode 100644 index 0000000000..9e8ab900fd --- /dev/null +++ b/external/lua/luajit/src/src/lib_string.c @@ -0,0 +1,940 @@ +/* +** String library. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#include + +#define lib_string_c +#define LUA_LIB + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_meta.h" +#include "lj_state.h" +#include "lj_ff.h" +#include "lj_bcdump.h" +#include "lj_char.h" +#include "lj_lib.h" + +/* ------------------------------------------------------------------------ */ + +#define LJLIB_MODULE_string + +LJLIB_ASM(string_len) LJLIB_REC(.) +{ + lj_lib_checkstr(L, 1); + return FFH_RETRY; +} + +LJLIB_ASM(string_byte) LJLIB_REC(string_range 0) +{ + GCstr *s = lj_lib_checkstr(L, 1); + int32_t len = (int32_t)s->len; + int32_t start = lj_lib_optint(L, 2, 1); + int32_t stop = lj_lib_optint(L, 3, start); + int32_t n, i; + const unsigned char *p; + if (stop < 0) stop += len+1; + if (start < 0) start += len+1; + if (start <= 0) start = 1; + if (stop > len) stop = len; + if (start > stop) return FFH_RES(0); /* Empty interval: return no results. */ + start--; + n = stop - start; + if ((uint32_t)n > LUAI_MAXCSTACK) + lj_err_caller(L, LJ_ERR_STRSLC); + lj_state_checkstack(L, (MSize)n); + p = (const unsigned char *)strdata(s) + start; + for (i = 0; i < n; i++) + setintV(L->base + i-1, p[i]); + return FFH_RES(n); +} + +LJLIB_ASM(string_char) +{ + int i, nargs = (int)(L->top - L->base); + char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, (size_t)nargs); + for (i = 1; i <= nargs; i++) { + int32_t k = lj_lib_checkint(L, i); + if (!checku8(k)) + lj_err_arg(L, i, LJ_ERR_BADVAL); + buf[i-1] = (char)k; + } + setstrV(L, L->base-1, lj_str_new(L, buf, (size_t)nargs)); + return FFH_RES(1); +} + +LJLIB_ASM(string_sub) LJLIB_REC(string_range 1) +{ + lj_lib_checkstr(L, 1); + lj_lib_checkint(L, 2); + setintV(L->base+2, lj_lib_optint(L, 3, -1)); + return FFH_RETRY; +} + +LJLIB_ASM(string_rep) +{ + GCstr *s = lj_lib_checkstr(L, 1); + int32_t k = lj_lib_checkint(L, 2); + GCstr *sep = lj_lib_optstr(L, 3); + int32_t len = (int32_t)s->len; + global_State *g = G(L); + int64_t tlen; + const char *src; + char *buf; + if (k <= 0) { + empty: + setstrV(L, L->base-1, &g->strempty); + return FFH_RES(1); + } + if (sep) { + tlen = (int64_t)len + sep->len; + if (tlen > LJ_MAX_STR) + lj_err_caller(L, LJ_ERR_STROV); + tlen *= k; + if (tlen > LJ_MAX_STR) + lj_err_caller(L, LJ_ERR_STROV); + } else { + tlen = (int64_t)k * len; + if (tlen > LJ_MAX_STR) + lj_err_caller(L, LJ_ERR_STROV); + } + if (tlen == 0) goto empty; + buf = lj_str_needbuf(L, &g->tmpbuf, (MSize)tlen); + src = strdata(s); + if (sep) { + tlen -= sep->len; /* Ignore trailing separator. */ + if (k > 1) { /* Paste one string and one separator. */ + int32_t i; + i = 0; while (i < len) *buf++ = src[i++]; + src = strdata(sep); len = sep->len; + i = 0; while (i < len) *buf++ = src[i++]; + src = g->tmpbuf.buf; len += s->len; k--; /* Now copy that k-1 times. */ + } + } + do { + int32_t i = 0; + do { *buf++ = src[i++]; } while (i < len); + } while (--k > 0); + setstrV(L, L->base-1, lj_str_new(L, g->tmpbuf.buf, (size_t)tlen)); + return FFH_RES(1); +} + +LJLIB_ASM(string_reverse) +{ + GCstr *s = lj_lib_checkstr(L, 1); + lj_str_needbuf(L, &G(L)->tmpbuf, s->len); + return FFH_RETRY; +} +LJLIB_ASM_(string_lower) +LJLIB_ASM_(string_upper) + +/* ------------------------------------------------------------------------ */ + +static int writer_buf(lua_State *L, const void *p, size_t size, void *b) +{ + luaL_addlstring((luaL_Buffer *)b, (const char *)p, size); + UNUSED(L); + return 0; +} + +LJLIB_CF(string_dump) +{ + GCfunc *fn = lj_lib_checkfunc(L, 1); + int strip = L->base+1 < L->top && tvistruecond(L->base+1); + luaL_Buffer b; + L->top = L->base+1; + luaL_buffinit(L, &b); + if (!isluafunc(fn) || lj_bcwrite(L, funcproto(fn), writer_buf, &b, strip)) + lj_err_caller(L, LJ_ERR_STRDUMP); + luaL_pushresult(&b); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +/* macro to `unsign' a character */ +#define uchar(c) ((unsigned char)(c)) + +#define CAP_UNFINISHED (-1) +#define CAP_POSITION (-2) + +typedef struct MatchState { + const char *src_init; /* init of source string */ + const char *src_end; /* end (`\0') of source string */ + lua_State *L; + int level; /* total number of captures (finished or unfinished) */ + int depth; + struct { + const char *init; + ptrdiff_t len; + } capture[LUA_MAXCAPTURES]; +} MatchState; + +#define L_ESC '%' +#define SPECIALS "^$*+?.([%-" + +static int check_capture(MatchState *ms, int l) +{ + l -= '1'; + if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) + lj_err_caller(ms->L, LJ_ERR_STRCAPI); + return l; +} + +static int capture_to_close(MatchState *ms) +{ + int level = ms->level; + for (level--; level>=0; level--) + if (ms->capture[level].len == CAP_UNFINISHED) return level; + lj_err_caller(ms->L, LJ_ERR_STRPATC); + return 0; /* unreachable */ +} + +static const char *classend(MatchState *ms, const char *p) +{ + switch (*p++) { + case L_ESC: + if (*p == '\0') + lj_err_caller(ms->L, LJ_ERR_STRPATE); + return p+1; + case '[': + if (*p == '^') p++; + do { /* look for a `]' */ + if (*p == '\0') + lj_err_caller(ms->L, LJ_ERR_STRPATM); + if (*(p++) == L_ESC && *p != '\0') + p++; /* skip escapes (e.g. `%]') */ + } while (*p != ']'); + return p+1; + default: + return p; + } +} + +static const unsigned char match_class_map[32] = { + 0,LJ_CHAR_ALPHA,0,LJ_CHAR_CNTRL,LJ_CHAR_DIGIT,0,0,LJ_CHAR_GRAPH,0,0,0,0, + LJ_CHAR_LOWER,0,0,0,LJ_CHAR_PUNCT,0,0,LJ_CHAR_SPACE,0, + LJ_CHAR_UPPER,0,LJ_CHAR_ALNUM,LJ_CHAR_XDIGIT,0,0,0,0,0,0,0 +}; + +static int match_class(int c, int cl) +{ + if ((cl & 0xc0) == 0x40) { + int t = match_class_map[(cl&0x1f)]; + if (t) { + t = lj_char_isa(c, t); + return (cl & 0x20) ? t : !t; + } + if (cl == 'z') return c == 0; + if (cl == 'Z') return c != 0; + } + return (cl == c); +} + +static int matchbracketclass(int c, const char *p, const char *ec) +{ + int sig = 1; + if (*(p+1) == '^') { + sig = 0; + p++; /* skip the `^' */ + } + while (++p < ec) { + if (*p == L_ESC) { + p++; + if (match_class(c, uchar(*p))) + return sig; + } + else if ((*(p+1) == '-') && (p+2 < ec)) { + p+=2; + if (uchar(*(p-2)) <= c && c <= uchar(*p)) + return sig; + } + else if (uchar(*p) == c) return sig; + } + return !sig; +} + +static int singlematch(int c, const char *p, const char *ep) +{ + switch (*p) { + case '.': return 1; /* matches any char */ + case L_ESC: return match_class(c, uchar(*(p+1))); + case '[': return matchbracketclass(c, p, ep-1); + default: return (uchar(*p) == c); + } +} + +static const char *match(MatchState *ms, const char *s, const char *p); + +static const char *matchbalance(MatchState *ms, const char *s, const char *p) +{ + if (*p == 0 || *(p+1) == 0) + lj_err_caller(ms->L, LJ_ERR_STRPATU); + if (*s != *p) { + return NULL; + } else { + int b = *p; + int e = *(p+1); + int cont = 1; + while (++s < ms->src_end) { + if (*s == e) { + if (--cont == 0) return s+1; + } else if (*s == b) { + cont++; + } + } + } + return NULL; /* string ends out of balance */ +} + +static const char *max_expand(MatchState *ms, const char *s, + const char *p, const char *ep) +{ + ptrdiff_t i = 0; /* counts maximum expand for item */ + while ((s+i)src_end && singlematch(uchar(*(s+i)), p, ep)) + i++; + /* keeps trying to match with the maximum repetitions */ + while (i>=0) { + const char *res = match(ms, (s+i), ep+1); + if (res) return res; + i--; /* else didn't match; reduce 1 repetition to try again */ + } + return NULL; +} + +static const char *min_expand(MatchState *ms, const char *s, + const char *p, const char *ep) +{ + for (;;) { + const char *res = match(ms, s, ep+1); + if (res != NULL) + return res; + else if (ssrc_end && singlematch(uchar(*s), p, ep)) + s++; /* try with one more repetition */ + else + return NULL; + } +} + +static const char *start_capture(MatchState *ms, const char *s, + const char *p, int what) +{ + const char *res; + int level = ms->level; + if (level >= LUA_MAXCAPTURES) lj_err_caller(ms->L, LJ_ERR_STRCAPN); + ms->capture[level].init = s; + ms->capture[level].len = what; + ms->level = level+1; + if ((res=match(ms, s, p)) == NULL) /* match failed? */ + ms->level--; /* undo capture */ + return res; +} + +static const char *end_capture(MatchState *ms, const char *s, + const char *p) +{ + int l = capture_to_close(ms); + const char *res; + ms->capture[l].len = s - ms->capture[l].init; /* close capture */ + if ((res = match(ms, s, p)) == NULL) /* match failed? */ + ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ + return res; +} + +static const char *match_capture(MatchState *ms, const char *s, int l) +{ + size_t len; + l = check_capture(ms, l); + len = (size_t)ms->capture[l].len; + if ((size_t)(ms->src_end-s) >= len && + memcmp(ms->capture[l].init, s, len) == 0) + return s+len; + else + return NULL; +} + +static const char *match(MatchState *ms, const char *s, const char *p) +{ + if (++ms->depth > LJ_MAX_XLEVEL) + lj_err_caller(ms->L, LJ_ERR_STRPATX); + init: /* using goto's to optimize tail recursion */ + switch (*p) { + case '(': /* start capture */ + if (*(p+1) == ')') /* position capture? */ + s = start_capture(ms, s, p+2, CAP_POSITION); + else + s = start_capture(ms, s, p+1, CAP_UNFINISHED); + break; + case ')': /* end capture */ + s = end_capture(ms, s, p+1); + break; + case L_ESC: + switch (*(p+1)) { + case 'b': /* balanced string? */ + s = matchbalance(ms, s, p+2); + if (s == NULL) break; + p+=4; + goto init; /* else s = match(ms, s, p+4); */ + case 'f': { /* frontier? */ + const char *ep; char previous; + p += 2; + if (*p != '[') + lj_err_caller(ms->L, LJ_ERR_STRPATB); + ep = classend(ms, p); /* points to what is next */ + previous = (s == ms->src_init) ? '\0' : *(s-1); + if (matchbracketclass(uchar(previous), p, ep-1) || + !matchbracketclass(uchar(*s), p, ep-1)) { s = NULL; break; } + p=ep; + goto init; /* else s = match(ms, s, ep); */ + } + default: + if (lj_char_isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */ + s = match_capture(ms, s, uchar(*(p+1))); + if (s == NULL) break; + p+=2; + goto init; /* else s = match(ms, s, p+2) */ + } + goto dflt; /* case default */ + } + break; + case '\0': /* end of pattern */ + break; /* match succeeded */ + case '$': + /* is the `$' the last char in pattern? */ + if (*(p+1) != '\0') goto dflt; + if (s != ms->src_end) s = NULL; /* check end of string */ + break; + default: dflt: { /* it is a pattern item */ + const char *ep = classend(ms, p); /* points to what is next */ + int m = ssrc_end && singlematch(uchar(*s), p, ep); + switch (*ep) { + case '?': { /* optional */ + const char *res; + if (m && ((res=match(ms, s+1, ep+1)) != NULL)) { + s = res; + break; + } + p=ep+1; + goto init; /* else s = match(ms, s, ep+1); */ + } + case '*': /* 0 or more repetitions */ + s = max_expand(ms, s, p, ep); + break; + case '+': /* 1 or more repetitions */ + s = (m ? max_expand(ms, s+1, p, ep) : NULL); + break; + case '-': /* 0 or more repetitions (minimum) */ + s = min_expand(ms, s, p, ep); + break; + default: + if (m) { s++; p=ep; goto init; } /* else s = match(ms, s+1, ep); */ + s = NULL; + break; + } + break; + } + } + ms->depth--; + return s; +} + +static const char *lmemfind(const char *s1, size_t l1, + const char *s2, size_t l2) +{ + if (l2 == 0) { + return s1; /* empty strings are everywhere */ + } else if (l2 > l1) { + return NULL; /* avoids a negative `l1' */ + } else { + const char *init; /* to search for a `*s2' inside `s1' */ + l2--; /* 1st char will be checked by `memchr' */ + l1 = l1-l2; /* `s2' cannot be found after that */ + while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) { + init++; /* 1st char is already checked */ + if (memcmp(init, s2+1, l2) == 0) { + return init-1; + } else { /* correct `l1' and `s1' to try again */ + l1 -= (size_t)(init-s1); + s1 = init; + } + } + return NULL; /* not found */ + } +} + +static void push_onecapture(MatchState *ms, int i, const char *s, const char *e) +{ + if (i >= ms->level) { + if (i == 0) /* ms->level == 0, too */ + lua_pushlstring(ms->L, s, (size_t)(e - s)); /* add whole match */ + else + lj_err_caller(ms->L, LJ_ERR_STRCAPI); + } else { + ptrdiff_t l = ms->capture[i].len; + if (l == CAP_UNFINISHED) lj_err_caller(ms->L, LJ_ERR_STRCAPU); + if (l == CAP_POSITION) + lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); + else + lua_pushlstring(ms->L, ms->capture[i].init, (size_t)l); + } +} + +static int push_captures(MatchState *ms, const char *s, const char *e) +{ + int i; + int nlevels = (ms->level == 0 && s) ? 1 : ms->level; + luaL_checkstack(ms->L, nlevels, "too many captures"); + for (i = 0; i < nlevels; i++) + push_onecapture(ms, i, s, e); + return nlevels; /* number of strings pushed */ +} + +static ptrdiff_t posrelat(ptrdiff_t pos, size_t len) +{ + /* relative string position: negative means back from end */ + if (pos < 0) pos += (ptrdiff_t)len + 1; + return (pos >= 0) ? pos : 0; +} + +static int str_find_aux(lua_State *L, int find) +{ + size_t l1, l2; + const char *s = luaL_checklstring(L, 1, &l1); + const char *p = luaL_checklstring(L, 2, &l2); + ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; + if (init < 0) { + init = 0; + } else if ((size_t)(init) > l1) { +#if LJ_52 + setnilV(L->top-1); + return 1; +#else + init = (ptrdiff_t)l1; +#endif + } + if (find && (lua_toboolean(L, 4) || /* explicit request? */ + strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ + /* do a plain search */ + const char *s2 = lmemfind(s+init, l1-(size_t)init, p, l2); + if (s2) { + lua_pushinteger(L, s2-s+1); + lua_pushinteger(L, s2-s+(ptrdiff_t)l2); + return 2; + } + } else { + MatchState ms; + int anchor = (*p == '^') ? (p++, 1) : 0; + const char *s1=s+init; + ms.L = L; + ms.src_init = s; + ms.src_end = s+l1; + do { + const char *res; + ms.level = ms.depth = 0; + if ((res=match(&ms, s1, p)) != NULL) { + if (find) { + lua_pushinteger(L, s1-s+1); /* start */ + lua_pushinteger(L, res-s); /* end */ + return push_captures(&ms, NULL, 0) + 2; + } else { + return push_captures(&ms, s1, res); + } + } + } while (s1++ < ms.src_end && !anchor); + } + lua_pushnil(L); /* not found */ + return 1; +} + +LJLIB_CF(string_find) +{ + return str_find_aux(L, 1); +} + +LJLIB_CF(string_match) +{ + return str_find_aux(L, 0); +} + +LJLIB_NOREG LJLIB_CF(string_gmatch_aux) +{ + const char *p = strVdata(lj_lib_upvalue(L, 2)); + GCstr *str = strV(lj_lib_upvalue(L, 1)); + const char *s = strdata(str); + TValue *tvpos = lj_lib_upvalue(L, 3); + const char *src = s + tvpos->u32.lo; + MatchState ms; + ms.L = L; + ms.src_init = s; + ms.src_end = s + str->len; + for (; src <= ms.src_end; src++) { + const char *e; + ms.level = ms.depth = 0; + if ((e = match(&ms, src, p)) != NULL) { + int32_t pos = (int32_t)(e - s); + if (e == src) pos++; /* Ensure progress for empty match. */ + tvpos->u32.lo = (uint32_t)pos; + return push_captures(&ms, src, e); + } + } + return 0; /* not found */ +} + +LJLIB_CF(string_gmatch) +{ + lj_lib_checkstr(L, 1); + lj_lib_checkstr(L, 2); + L->top = L->base+3; + (L->top-1)->u64 = 0; + lj_lib_pushcc(L, lj_cf_string_gmatch_aux, FF_string_gmatch_aux, 3); + return 1; +} + +static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e) +{ + size_t l, i; + const char *news = lua_tolstring(ms->L, 3, &l); + for (i = 0; i < l; i++) { + if (news[i] != L_ESC) { + luaL_addchar(b, news[i]); + } else { + i++; /* skip ESC */ + if (!lj_char_isdigit(uchar(news[i]))) { + luaL_addchar(b, news[i]); + } else if (news[i] == '0') { + luaL_addlstring(b, s, (size_t)(e - s)); + } else { + push_onecapture(ms, news[i] - '1', s, e); + luaL_addvalue(b); /* add capture to accumulated result */ + } + } + } +} + +static void add_value(MatchState *ms, luaL_Buffer *b, + const char *s, const char *e) +{ + lua_State *L = ms->L; + switch (lua_type(L, 3)) { + case LUA_TNUMBER: + case LUA_TSTRING: { + add_s(ms, b, s, e); + return; + } + case LUA_TFUNCTION: { + int n; + lua_pushvalue(L, 3); + n = push_captures(ms, s, e); + lua_call(L, n, 1); + break; + } + case LUA_TTABLE: { + push_onecapture(ms, 0, s, e); + lua_gettable(L, 3); + break; + } + } + if (!lua_toboolean(L, -1)) { /* nil or false? */ + lua_pop(L, 1); + lua_pushlstring(L, s, (size_t)(e - s)); /* keep original text */ + } else if (!lua_isstring(L, -1)) { + lj_err_callerv(L, LJ_ERR_STRGSRV, luaL_typename(L, -1)); + } + luaL_addvalue(b); /* add result to accumulator */ +} + +LJLIB_CF(string_gsub) +{ + size_t srcl; + const char *src = luaL_checklstring(L, 1, &srcl); + const char *p = luaL_checkstring(L, 2); + int tr = lua_type(L, 3); + int max_s = luaL_optint(L, 4, (int)(srcl+1)); + int anchor = (*p == '^') ? (p++, 1) : 0; + int n = 0; + MatchState ms; + luaL_Buffer b; + if (!(tr == LUA_TNUMBER || tr == LUA_TSTRING || + tr == LUA_TFUNCTION || tr == LUA_TTABLE)) + lj_err_arg(L, 3, LJ_ERR_NOSFT); + luaL_buffinit(L, &b); + ms.L = L; + ms.src_init = src; + ms.src_end = src+srcl; + while (n < max_s) { + const char *e; + ms.level = ms.depth = 0; + e = match(&ms, src, p); + if (e) { + n++; + add_value(&ms, &b, src, e); + } + if (e && e>src) /* non empty match? */ + src = e; /* skip it */ + else if (src < ms.src_end) + luaL_addchar(&b, *src++); + else + break; + if (anchor) + break; + } + luaL_addlstring(&b, src, (size_t)(ms.src_end-src)); + luaL_pushresult(&b); + lua_pushinteger(L, n); /* number of substitutions */ + return 2; +} + +/* ------------------------------------------------------------------------ */ + +/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ +#define MAX_FMTITEM 512 +/* valid flags in a format specification */ +#define FMT_FLAGS "-+ #0" +/* +** maximum size of each format specification (such as '%-099.99d') +** (+10 accounts for %99.99x plus margin of error) +*/ +#define MAX_FMTSPEC (sizeof(FMT_FLAGS) + sizeof(LUA_INTFRMLEN) + 10) + +static void addquoted(lua_State *L, luaL_Buffer *b, int arg) +{ + GCstr *str = lj_lib_checkstr(L, arg); + int32_t len = (int32_t)str->len; + const char *s = strdata(str); + luaL_addchar(b, '"'); + while (len--) { + uint32_t c = uchar(*s); + if (c == '"' || c == '\\' || c == '\n') { + luaL_addchar(b, '\\'); + } else if (lj_char_iscntrl(c)) { /* This can only be 0-31 or 127. */ + uint32_t d; + luaL_addchar(b, '\\'); + if (c >= 100 || lj_char_isdigit(uchar(s[1]))) { + luaL_addchar(b, '0'+(c >= 100)); if (c >= 100) c -= 100; + goto tens; + } else if (c >= 10) { + tens: + d = (c * 205) >> 11; c -= d * 10; luaL_addchar(b, '0'+d); + } + c += '0'; + } + luaL_addchar(b, c); + s++; + } + luaL_addchar(b, '"'); +} + +static const char *scanformat(lua_State *L, const char *strfrmt, char *form) +{ + const char *p = strfrmt; + while (*p != '\0' && strchr(FMT_FLAGS, *p) != NULL) p++; /* skip flags */ + if ((size_t)(p - strfrmt) >= sizeof(FMT_FLAGS)) + lj_err_caller(L, LJ_ERR_STRFMTR); + if (lj_char_isdigit(uchar(*p))) p++; /* skip width */ + if (lj_char_isdigit(uchar(*p))) p++; /* (2 digits at most) */ + if (*p == '.') { + p++; + if (lj_char_isdigit(uchar(*p))) p++; /* skip precision */ + if (lj_char_isdigit(uchar(*p))) p++; /* (2 digits at most) */ + } + if (lj_char_isdigit(uchar(*p))) + lj_err_caller(L, LJ_ERR_STRFMTW); + *(form++) = '%'; + strncpy(form, strfrmt, (size_t)(p - strfrmt + 1)); + form += p - strfrmt + 1; + *form = '\0'; + return p; +} + +static void addintlen(char *form) +{ + size_t l = strlen(form); + char spec = form[l - 1]; + strcpy(form + l - 1, LUA_INTFRMLEN); + form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; + form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; +} + +static unsigned LUA_INTFRM_T num2intfrm(lua_State *L, int arg) +{ + if (sizeof(LUA_INTFRM_T) == 4) { + return (LUA_INTFRM_T)lj_lib_checkbit(L, arg); + } else { + cTValue *o; + lj_lib_checknumber(L, arg); + o = L->base+arg-1; + if (tvisint(o)) + return (LUA_INTFRM_T)intV(o); + else + return (LUA_INTFRM_T)numV(o); + } +} + +static unsigned LUA_INTFRM_T num2uintfrm(lua_State *L, int arg) +{ + if (sizeof(LUA_INTFRM_T) == 4) { + return (unsigned LUA_INTFRM_T)lj_lib_checkbit(L, arg); + } else { + cTValue *o; + lj_lib_checknumber(L, arg); + o = L->base+arg-1; + if (tvisint(o)) + return (unsigned LUA_INTFRM_T)intV(o); + else if ((int32_t)o->u32.hi < 0) + return (unsigned LUA_INTFRM_T)(LUA_INTFRM_T)numV(o); + else + return (unsigned LUA_INTFRM_T)numV(o); + } +} + +static GCstr *meta_tostring(lua_State *L, int arg) +{ + TValue *o = L->base+arg-1; + cTValue *mo; + lua_assert(o < L->top); /* Caller already checks for existence. */ + if (LJ_LIKELY(tvisstr(o))) + return strV(o); + if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) { + copyTV(L, L->top++, mo); + copyTV(L, L->top++, o); + lua_call(L, 1, 1); + L->top--; + if (tvisstr(L->top)) + return strV(L->top); + o = L->base+arg-1; + copyTV(L, o, L->top); + } + if (tvisnumber(o)) { + return lj_str_fromnumber(L, o); + } else if (tvisnil(o)) { + return lj_str_newlit(L, "nil"); + } else if (tvisfalse(o)) { + return lj_str_newlit(L, "false"); + } else if (tvistrue(o)) { + return lj_str_newlit(L, "true"); + } else { + if (tvisfunc(o) && isffunc(funcV(o))) + lj_str_pushf(L, "function: builtin#%d", funcV(o)->c.ffid); + else + lj_str_pushf(L, "%s: %p", lj_typename(o), lua_topointer(L, arg)); + L->top--; + return strV(L->top); + } +} + +LJLIB_CF(string_format) +{ + int arg = 1, top = (int)(L->top - L->base); + GCstr *fmt = lj_lib_checkstr(L, arg); + const char *strfrmt = strdata(fmt); + const char *strfrmt_end = strfrmt + fmt->len; + luaL_Buffer b; + luaL_buffinit(L, &b); + while (strfrmt < strfrmt_end) { + if (*strfrmt != L_ESC) { + luaL_addchar(&b, *strfrmt++); + } else if (*++strfrmt == L_ESC) { + luaL_addchar(&b, *strfrmt++); /* %% */ + } else { /* format item */ + char form[MAX_FMTSPEC]; /* to store the format (`%...') */ + char buff[MAX_FMTITEM]; /* to store the formatted item */ + if (++arg > top) + luaL_argerror(L, arg, lj_obj_typename[0]); + strfrmt = scanformat(L, strfrmt, form); + switch (*strfrmt++) { + case 'c': + sprintf(buff, form, lj_lib_checkint(L, arg)); + break; + case 'd': case 'i': + addintlen(form); + sprintf(buff, form, num2intfrm(L, arg)); + break; + case 'o': case 'u': case 'x': case 'X': + addintlen(form); + sprintf(buff, form, num2uintfrm(L, arg)); + break; + case 'e': case 'E': case 'f': case 'g': case 'G': case 'a': case 'A': { + TValue tv; + tv.n = lj_lib_checknum(L, arg); + if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) { + /* Canonicalize output of non-finite values. */ + char *p, nbuf[LJ_STR_NUMBUF]; + size_t len = lj_str_bufnum(nbuf, &tv); + if (strfrmt[-1] < 'a') { + nbuf[len-3] = nbuf[len-3] - 0x20; + nbuf[len-2] = nbuf[len-2] - 0x20; + nbuf[len-1] = nbuf[len-1] - 0x20; + } + nbuf[len] = '\0'; + for (p = form; *p < 'A' && *p != '.'; p++) ; + *p++ = 's'; *p = '\0'; + sprintf(buff, form, nbuf); + break; + } + sprintf(buff, form, (double)tv.n); + break; + } + case 'q': + addquoted(L, &b, arg); + continue; + case 'p': + lj_str_pushf(L, "%p", lua_topointer(L, arg)); + luaL_addvalue(&b); + continue; + case 's': { + GCstr *str = meta_tostring(L, arg); + if (!strchr(form, '.') && str->len >= 100) { + /* no precision and string is too long to be formatted; + keep original string */ + setstrV(L, L->top++, str); + luaL_addvalue(&b); + continue; + } + sprintf(buff, form, strdata(str)); + break; + } + default: + lj_err_callerv(L, LJ_ERR_STRFMTO, *(strfrmt -1)); + break; + } + luaL_addlstring(&b, buff, strlen(buff)); + } + } + luaL_pushresult(&b); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +#include "lj_libdef.h" + +LUALIB_API int luaopen_string(lua_State *L) +{ + GCtab *mt; + global_State *g; + LJ_LIB_REG(L, LUA_STRLIBNAME, string); +#if defined(LUA_COMPAT_GFIND) && !LJ_52 + lua_getfield(L, -1, "gmatch"); + lua_setfield(L, -2, "gfind"); +#endif + mt = lj_tab_new(L, 0, 1); + /* NOBARRIER: basemt is a GC root. */ + g = G(L); + setgcref(basemt_it(g, LJ_TSTR), obj2gco(mt)); + settabV(L, lj_tab_setstr(L, mt, mmname_str(g, MM_index)), tabV(L->top-1)); + mt->nomm = (uint8_t)(~(1u<top, func); + setintV(L->top+1, i); + val = lj_tab_getint(t, (int32_t)i); + if (val) { copyTV(L, L->top+2, val); } else { setnilV(L->top+2); } + L->top += 3; + lua_call(L, 2, 1); + if (!tvisnil(L->top-1)) + return 1; + L->top--; + } + return 0; +} + +LJLIB_CF(table_foreach) +{ + GCtab *t = lj_lib_checktab(L, 1); + GCfunc *func = lj_lib_checkfunc(L, 2); + L->top = L->base+3; + setnilV(L->top-1); + while (lj_tab_next(L, t, L->top-1)) { + copyTV(L, L->top+2, L->top); + copyTV(L, L->top+1, L->top-1); + setfuncV(L, L->top, func); + L->top += 3; + lua_call(L, 2, 1); + if (!tvisnil(L->top-1)) + return 1; + L->top--; + } + return 0; +} + +LJLIB_ASM(table_getn) LJLIB_REC(.) +{ + lj_lib_checktab(L, 1); + return FFH_UNREACHABLE; +} + +LJLIB_CF(table_maxn) +{ + GCtab *t = lj_lib_checktab(L, 1); + TValue *array = tvref(t->array); + Node *node; + lua_Number m = 0; + ptrdiff_t i; + for (i = (ptrdiff_t)t->asize - 1; i >= 0; i--) + if (!tvisnil(&array[i])) { + m = (lua_Number)(int32_t)i; + break; + } + node = noderef(t->node); + for (i = (ptrdiff_t)t->hmask; i >= 0; i--) + if (!tvisnil(&node[i].val) && tvisnumber(&node[i].key)) { + lua_Number n = numberVnum(&node[i].key); + if (n > m) m = n; + } + setnumV(L->top-1, m); + return 1; +} + +LJLIB_CF(table_insert) LJLIB_REC(.) +{ + GCtab *t = lj_lib_checktab(L, 1); + int32_t n, i = (int32_t)lj_tab_len(t) + 1; + int nargs = (int)((char *)L->top - (char *)L->base); + if (nargs != 2*sizeof(TValue)) { + if (nargs != 3*sizeof(TValue)) + lj_err_caller(L, LJ_ERR_TABINS); + /* NOBARRIER: This just moves existing elements around. */ + for (n = lj_lib_checkint(L, 2); i > n; i--) { + /* The set may invalidate the get pointer, so need to do it first! */ + TValue *dst = lj_tab_setint(L, t, i); + cTValue *src = lj_tab_getint(t, i-1); + if (src) { + copyTV(L, dst, src); + } else { + setnilV(dst); + } + } + i = n; + } + { + TValue *dst = lj_tab_setint(L, t, i); + copyTV(L, dst, L->top-1); /* Set new value. */ + lj_gc_barriert(L, t, dst); + } + return 0; +} + +LJLIB_CF(table_remove) LJLIB_REC(.) +{ + GCtab *t = lj_lib_checktab(L, 1); + int32_t e = (int32_t)lj_tab_len(t); + int32_t pos = lj_lib_optint(L, 2, e); + if (!(1 <= pos && pos <= e)) /* Nothing to remove? */ + return 0; + lua_rawgeti(L, 1, pos); /* Get previous value. */ + /* NOBARRIER: This just moves existing elements around. */ + for (; pos < e; pos++) { + cTValue *src = lj_tab_getint(t, pos+1); + TValue *dst = lj_tab_setint(L, t, pos); + if (src) { + copyTV(L, dst, src); + } else { + setnilV(dst); + } + } + setnilV(lj_tab_setint(L, t, e)); /* Remove (last) value. */ + return 1; /* Return previous value. */ +} + +LJLIB_CF(table_concat) +{ + luaL_Buffer b; + GCtab *t = lj_lib_checktab(L, 1); + GCstr *sep = lj_lib_optstr(L, 2); + MSize seplen = sep ? sep->len : 0; + int32_t i = lj_lib_optint(L, 3, 1); + int32_t e = L->base+3 < L->top ? lj_lib_checkint(L, 4) : + (int32_t)lj_tab_len(t); + luaL_buffinit(L, &b); + if (i <= e) { + for (;;) { + cTValue *o; + lua_rawgeti(L, 1, i); + o = L->top-1; + if (!(tvisstr(o) || tvisnumber(o))) + lj_err_callerv(L, LJ_ERR_TABCAT, lj_typename(o), i); + luaL_addvalue(&b); + if (i++ == e) break; + if (seplen) + luaL_addlstring(&b, strdata(sep), seplen); + } + } + luaL_pushresult(&b); + return 1; +} + +/* ------------------------------------------------------------------------ */ + +static void set2(lua_State *L, int i, int j) +{ + lua_rawseti(L, 1, i); + lua_rawseti(L, 1, j); +} + +static int sort_comp(lua_State *L, int a, int b) +{ + if (!lua_isnil(L, 2)) { /* function? */ + int res; + lua_pushvalue(L, 2); + lua_pushvalue(L, a-1); /* -1 to compensate function */ + lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ + lua_call(L, 2, 1); + res = lua_toboolean(L, -1); + lua_pop(L, 1); + return res; + } else { /* a < b? */ + return lua_lessthan(L, a, b); + } +} + +static void auxsort(lua_State *L, int l, int u) +{ + while (l < u) { /* for tail recursion */ + int i, j; + /* sort elements a[l], a[(l+u)/2] and a[u] */ + lua_rawgeti(L, 1, l); + lua_rawgeti(L, 1, u); + if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ + set2(L, l, u); /* swap a[l] - a[u] */ + else + lua_pop(L, 2); + if (u-l == 1) break; /* only 2 elements */ + i = (l+u)/2; + lua_rawgeti(L, 1, i); + lua_rawgeti(L, 1, l); + if (sort_comp(L, -2, -1)) { /* a[i]= P */ + while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { + if (i>=u) lj_err_caller(L, LJ_ERR_TABSORT); + lua_pop(L, 1); /* remove a[i] */ + } + /* repeat --j until a[j] <= P */ + while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { + if (j<=l) lj_err_caller(L, LJ_ERR_TABSORT); + lua_pop(L, 1); /* remove a[j] */ + } + if (jbase+1)) + lj_lib_checkfunc(L, 2); + auxsort(L, 1, n); + return 0; +} + +#if LJ_52 +LJLIB_PUSH("n") +LJLIB_CF(table_pack) +{ + TValue *array, *base = L->base; + MSize i, n = (uint32_t)(L->top - base); + GCtab *t = lj_tab_new(L, n ? n+1 : 0, 1); + /* NOBARRIER: The table is new (marked white). */ + setintV(lj_tab_setstr(L, t, strV(lj_lib_upvalue(L, 1))), (int32_t)n); + for (array = tvref(t->array) + 1, i = 0; i < n; i++) + copyTV(L, &array[i], &base[i]); + settabV(L, base, t); + L->top = base+1; + lj_gc_check(L); + return 1; +} +#endif + +/* ------------------------------------------------------------------------ */ + +#include "lj_libdef.h" + +LUALIB_API int luaopen_table(lua_State *L) +{ + LJ_LIB_REG(L, LUA_TABLIBNAME, table); +#if LJ_52 + lua_getglobal(L, "unpack"); + lua_setfield(L, -2, "unpack"); +#endif + return 1; +} + diff --git a/external/lua/luajit/src/src/lj.supp b/external/lua/luajit/src/src/lj.supp new file mode 100644 index 0000000000..411f261700 --- /dev/null +++ b/external/lua/luajit/src/src/lj.supp @@ -0,0 +1,26 @@ +# Valgrind suppression file for LuaJIT 2.0. +{ + Optimized string compare + Memcheck:Addr4 + fun:lj_str_cmp +} +{ + Optimized string compare + Memcheck:Addr1 + fun:lj_str_cmp +} +{ + Optimized string compare + Memcheck:Addr4 + fun:lj_str_new +} +{ + Optimized string compare + Memcheck:Addr1 + fun:lj_str_new +} +{ + Optimized string compare + Memcheck:Cond + fun:lj_str_new +} diff --git a/external/lua/luajit/src/src/lj_alloc.c b/external/lua/luajit/src/src/lj_alloc.c new file mode 100644 index 0000000000..82b4e5b165 --- /dev/null +++ b/external/lua/luajit/src/src/lj_alloc.c @@ -0,0 +1,1381 @@ +/* +** Bundled memory allocator. +** +** Beware: this is a HEAVILY CUSTOMIZED version of dlmalloc. +** The original bears the following remark: +** +** This is a version (aka dlmalloc) of malloc/free/realloc written by +** Doug Lea and released to the public domain, as explained at +** http://creativecommons.org/licenses/publicdomain. +** +** * Version pre-2.8.4 Wed Mar 29 19:46:29 2006 (dl at gee) +** +** No additional copyright is claimed over the customizations. +** Please do NOT bother the original author about this version here! +** +** If you want to use dlmalloc in another project, you should get +** the original from: ftp://gee.cs.oswego.edu/pub/misc/ +** For thread-safe derivatives, take a look at: +** - ptmalloc: http://www.malloc.de/ +** - nedmalloc: http://www.nedprod.com/programs/portable/nedmalloc/ +*/ + +#define lj_alloc_c +#define LUA_CORE + +/* To get the mremap prototype. Must be defined before any system includes. */ +#if defined(__linux__) && !defined(_GNU_SOURCE) +#define _GNU_SOURCE +#endif + +#include "lj_def.h" +#include "lj_arch.h" +#include "lj_alloc.h" + +#ifndef LUAJIT_USE_SYSMALLOC + +#define MAX_SIZE_T (~(size_t)0) +#define MALLOC_ALIGNMENT ((size_t)8U) + +#define DEFAULT_GRANULARITY ((size_t)128U * (size_t)1024U) +#define DEFAULT_TRIM_THRESHOLD ((size_t)2U * (size_t)1024U * (size_t)1024U) +#define DEFAULT_MMAP_THRESHOLD ((size_t)128U * (size_t)1024U) +#define MAX_RELEASE_CHECK_RATE 255 + +/* ------------------- size_t and alignment properties -------------------- */ + +/* The byte and bit size of a size_t */ +#define SIZE_T_SIZE (sizeof(size_t)) +#define SIZE_T_BITSIZE (sizeof(size_t) << 3) + +/* Some constants coerced to size_t */ +/* Annoying but necessary to avoid errors on some platforms */ +#define SIZE_T_ZERO ((size_t)0) +#define SIZE_T_ONE ((size_t)1) +#define SIZE_T_TWO ((size_t)2) +#define TWO_SIZE_T_SIZES (SIZE_T_SIZE<<1) +#define FOUR_SIZE_T_SIZES (SIZE_T_SIZE<<2) +#define SIX_SIZE_T_SIZES (FOUR_SIZE_T_SIZES+TWO_SIZE_T_SIZES) + +/* The bit mask value corresponding to MALLOC_ALIGNMENT */ +#define CHUNK_ALIGN_MASK (MALLOC_ALIGNMENT - SIZE_T_ONE) + +/* the number of bytes to offset an address to align it */ +#define align_offset(A)\ + ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\ + ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK)) + +/* -------------------------- MMAP support ------------------------------- */ + +#define MFAIL ((void *)(MAX_SIZE_T)) +#define CMFAIL ((char *)(MFAIL)) /* defined for convenience */ + +#define IS_DIRECT_BIT (SIZE_T_ONE) + +#if LJ_TARGET_WINDOWS + +#define WIN32_LEAN_AND_MEAN +#include + +#if LJ_64 + +/* Undocumented, but hey, that's what we all love so much about Windows. */ +typedef long (*PNTAVM)(HANDLE handle, void **addr, ULONG zbits, + size_t *size, ULONG alloctype, ULONG prot); +static PNTAVM ntavm; + +/* Number of top bits of the lower 32 bits of an address that must be zero. +** Apparently 0 gives us full 64 bit addresses and 1 gives us the lower 2GB. +*/ +#define NTAVM_ZEROBITS 1 + +static void INIT_MMAP(void) +{ + ntavm = (PNTAVM)GetProcAddress(GetModuleHandleA("ntdll.dll"), + "NtAllocateVirtualMemory"); +} + +/* Win64 32 bit MMAP via NtAllocateVirtualMemory. */ +static LJ_AINLINE void *CALL_MMAP(size_t size) +{ + DWORD olderr = GetLastError(); + void *ptr = NULL; + long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size, + MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); + SetLastError(olderr); + return st == 0 ? ptr : MFAIL; +} + +/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */ +static LJ_AINLINE void *DIRECT_MMAP(size_t size) +{ + DWORD olderr = GetLastError(); + void *ptr = NULL; + long st = ntavm(INVALID_HANDLE_VALUE, &ptr, NTAVM_ZEROBITS, &size, + MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, PAGE_READWRITE); + SetLastError(olderr); + return st == 0 ? ptr : MFAIL; +} + +#else + +#define INIT_MMAP() ((void)0) + +/* Win32 MMAP via VirtualAlloc */ +static LJ_AINLINE void *CALL_MMAP(size_t size) +{ + DWORD olderr = GetLastError(); + void *ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); + SetLastError(olderr); + return ptr ? ptr : MFAIL; +} + +/* For direct MMAP, use MEM_TOP_DOWN to minimize interference */ +static LJ_AINLINE void *DIRECT_MMAP(size_t size) +{ + DWORD olderr = GetLastError(); + void *ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, + PAGE_READWRITE); + SetLastError(olderr); + return ptr ? ptr : MFAIL; +} + +#endif + +/* This function supports releasing coalesed segments */ +static LJ_AINLINE int CALL_MUNMAP(void *ptr, size_t size) +{ + DWORD olderr = GetLastError(); + MEMORY_BASIC_INFORMATION minfo; + char *cptr = (char *)ptr; + while (size) { + if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0) + return -1; + if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr || + minfo.State != MEM_COMMIT || minfo.RegionSize > size) + return -1; + if (VirtualFree(cptr, 0, MEM_RELEASE) == 0) + return -1; + cptr += minfo.RegionSize; + size -= minfo.RegionSize; + } + SetLastError(olderr); + return 0; +} + +#else + +#include +#include + +#define MMAP_PROT (PROT_READ|PROT_WRITE) +#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) +#define MAP_ANONYMOUS MAP_ANON +#endif +#define MMAP_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS) + +#if LJ_64 +/* 64 bit mode needs special support for allocating memory in the lower 2GB. */ + +#if LJ_TARGET_LINUX + +/* Actually this only gives us max. 1GB in current Linux kernels. */ +static LJ_AINLINE void *CALL_MMAP(size_t size) +{ + int olderr = errno; + void *ptr = mmap(NULL, size, MMAP_PROT, MAP_32BIT|MMAP_FLAGS, -1, 0); + errno = olderr; + return ptr; +} + +#elif LJ_TARGET_OSX || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) + +/* OSX and FreeBSD mmap() use a naive first-fit linear search. +** That's perfect for us. Except that -pagezero_size must be set for OSX, +** otherwise the lower 4GB are blocked. And the 32GB RLIMIT_DATA needs +** to be reduced to 250MB on FreeBSD. +*/ +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) +#include +#define MMAP_REGION_START ((uintptr_t)0x10000000) +#else +#define MMAP_REGION_START ((uintptr_t)0x10000) +#endif +#define MMAP_REGION_END ((uintptr_t)0x80000000) + +static LJ_AINLINE void *CALL_MMAP(size_t size) +{ + int olderr = errno; + /* Hint for next allocation. Doesn't need to be thread-safe. */ + static uintptr_t alloc_hint = MMAP_REGION_START; + int retry = 0; +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) + static int rlimit_modified = 0; + if (LJ_UNLIKELY(rlimit_modified == 0)) { + struct rlimit rlim; + rlim.rlim_cur = rlim.rlim_max = MMAP_REGION_START; + setrlimit(RLIMIT_DATA, &rlim); /* Ignore result. May fail below. */ + rlimit_modified = 1; + } +#endif + for (;;) { + void *p = mmap((void *)alloc_hint, size, MMAP_PROT, MMAP_FLAGS, -1, 0); + if ((uintptr_t)p >= MMAP_REGION_START && + (uintptr_t)p + size < MMAP_REGION_END) { + alloc_hint = (uintptr_t)p + size; + errno = olderr; + return p; + } + if (p != CMFAIL) munmap(p, size); + if (retry) break; + retry = 1; + alloc_hint = MMAP_REGION_START; + } + errno = olderr; + return CMFAIL; +} + +#else + +#error "NYI: need an equivalent of MAP_32BIT for this 64 bit OS" + +#endif + +#else + +/* 32 bit mode is easy. */ +static LJ_AINLINE void *CALL_MMAP(size_t size) +{ + int olderr = errno; + void *ptr = mmap(NULL, size, MMAP_PROT, MMAP_FLAGS, -1, 0); + errno = olderr; + return ptr; +} + +#endif + +#define INIT_MMAP() ((void)0) +#define DIRECT_MMAP(s) CALL_MMAP(s) + +static LJ_AINLINE int CALL_MUNMAP(void *ptr, size_t size) +{ + int olderr = errno; + int ret = munmap(ptr, size); + errno = olderr; + return ret; +} + +#if LJ_TARGET_LINUX +/* Need to define _GNU_SOURCE to get the mremap prototype. */ +static LJ_AINLINE void *CALL_MREMAP_(void *ptr, size_t osz, size_t nsz, + int flags) +{ + int olderr = errno; + ptr = mremap(ptr, osz, nsz, flags); + errno = olderr; + return ptr; +} + +#define CALL_MREMAP(addr, osz, nsz, mv) CALL_MREMAP_((addr), (osz), (nsz), (mv)) +#define CALL_MREMAP_NOMOVE 0 +#define CALL_MREMAP_MAYMOVE 1 +#if LJ_64 +#define CALL_MREMAP_MV CALL_MREMAP_NOMOVE +#else +#define CALL_MREMAP_MV CALL_MREMAP_MAYMOVE +#endif +#endif + +#endif + +#ifndef CALL_MREMAP +#define CALL_MREMAP(addr, osz, nsz, mv) ((void)osz, MFAIL) +#endif + +/* ----------------------- Chunk representations ------------------------ */ + +struct malloc_chunk { + size_t prev_foot; /* Size of previous chunk (if free). */ + size_t head; /* Size and inuse bits. */ + struct malloc_chunk *fd; /* double links -- used only if free. */ + struct malloc_chunk *bk; +}; + +typedef struct malloc_chunk mchunk; +typedef struct malloc_chunk *mchunkptr; +typedef struct malloc_chunk *sbinptr; /* The type of bins of chunks */ +typedef size_t bindex_t; /* Described below */ +typedef unsigned int binmap_t; /* Described below */ +typedef unsigned int flag_t; /* The type of various bit flag sets */ + +/* ------------------- Chunks sizes and alignments ----------------------- */ + +#define MCHUNK_SIZE (sizeof(mchunk)) + +#define CHUNK_OVERHEAD (SIZE_T_SIZE) + +/* Direct chunks need a second word of overhead ... */ +#define DIRECT_CHUNK_OVERHEAD (TWO_SIZE_T_SIZES) +/* ... and additional padding for fake next-chunk at foot */ +#define DIRECT_FOOT_PAD (FOUR_SIZE_T_SIZES) + +/* The smallest size we can malloc is an aligned minimal chunk */ +#define MIN_CHUNK_SIZE\ + ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) + +/* conversion from malloc headers to user pointers, and back */ +#define chunk2mem(p) ((void *)((char *)(p) + TWO_SIZE_T_SIZES)) +#define mem2chunk(mem) ((mchunkptr)((char *)(mem) - TWO_SIZE_T_SIZES)) +/* chunk associated with aligned address A */ +#define align_as_chunk(A) (mchunkptr)((A) + align_offset(chunk2mem(A))) + +/* Bounds on request (not chunk) sizes. */ +#define MAX_REQUEST ((~MIN_CHUNK_SIZE+1) << 2) +#define MIN_REQUEST (MIN_CHUNK_SIZE - CHUNK_OVERHEAD - SIZE_T_ONE) + +/* pad request bytes into a usable size */ +#define pad_request(req) \ + (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) + +/* pad request, checking for minimum (but not maximum) */ +#define request2size(req) \ + (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req)) + +/* ------------------ Operations on head and foot fields ----------------- */ + +#define PINUSE_BIT (SIZE_T_ONE) +#define CINUSE_BIT (SIZE_T_TWO) +#define INUSE_BITS (PINUSE_BIT|CINUSE_BIT) + +/* Head value for fenceposts */ +#define FENCEPOST_HEAD (INUSE_BITS|SIZE_T_SIZE) + +/* extraction of fields from head words */ +#define cinuse(p) ((p)->head & CINUSE_BIT) +#define pinuse(p) ((p)->head & PINUSE_BIT) +#define chunksize(p) ((p)->head & ~(INUSE_BITS)) + +#define clear_pinuse(p) ((p)->head &= ~PINUSE_BIT) +#define clear_cinuse(p) ((p)->head &= ~CINUSE_BIT) + +/* Treat space at ptr +/- offset as a chunk */ +#define chunk_plus_offset(p, s) ((mchunkptr)(((char *)(p)) + (s))) +#define chunk_minus_offset(p, s) ((mchunkptr)(((char *)(p)) - (s))) + +/* Ptr to next or previous physical malloc_chunk. */ +#define next_chunk(p) ((mchunkptr)(((char *)(p)) + ((p)->head & ~INUSE_BITS))) +#define prev_chunk(p) ((mchunkptr)(((char *)(p)) - ((p)->prev_foot) )) + +/* extract next chunk's pinuse bit */ +#define next_pinuse(p) ((next_chunk(p)->head) & PINUSE_BIT) + +/* Get/set size at footer */ +#define get_foot(p, s) (((mchunkptr)((char *)(p) + (s)))->prev_foot) +#define set_foot(p, s) (((mchunkptr)((char *)(p) + (s)))->prev_foot = (s)) + +/* Set size, pinuse bit, and foot */ +#define set_size_and_pinuse_of_free_chunk(p, s)\ + ((p)->head = (s|PINUSE_BIT), set_foot(p, s)) + +/* Set size, pinuse bit, foot, and clear next pinuse */ +#define set_free_with_pinuse(p, s, n)\ + (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s)) + +#define is_direct(p)\ + (!((p)->head & PINUSE_BIT) && ((p)->prev_foot & IS_DIRECT_BIT)) + +/* Get the internal overhead associated with chunk p */ +#define overhead_for(p)\ + (is_direct(p)? DIRECT_CHUNK_OVERHEAD : CHUNK_OVERHEAD) + +/* ---------------------- Overlaid data structures ----------------------- */ + +struct malloc_tree_chunk { + /* The first four fields must be compatible with malloc_chunk */ + size_t prev_foot; + size_t head; + struct malloc_tree_chunk *fd; + struct malloc_tree_chunk *bk; + + struct malloc_tree_chunk *child[2]; + struct malloc_tree_chunk *parent; + bindex_t index; +}; + +typedef struct malloc_tree_chunk tchunk; +typedef struct malloc_tree_chunk *tchunkptr; +typedef struct malloc_tree_chunk *tbinptr; /* The type of bins of trees */ + +/* A little helper macro for trees */ +#define leftmost_child(t) ((t)->child[0] != 0? (t)->child[0] : (t)->child[1]) + +/* ----------------------------- Segments -------------------------------- */ + +struct malloc_segment { + char *base; /* base address */ + size_t size; /* allocated size */ + struct malloc_segment *next; /* ptr to next segment */ +}; + +typedef struct malloc_segment msegment; +typedef struct malloc_segment *msegmentptr; + +/* ---------------------------- malloc_state ----------------------------- */ + +/* Bin types, widths and sizes */ +#define NSMALLBINS (32U) +#define NTREEBINS (32U) +#define SMALLBIN_SHIFT (3U) +#define SMALLBIN_WIDTH (SIZE_T_ONE << SMALLBIN_SHIFT) +#define TREEBIN_SHIFT (8U) +#define MIN_LARGE_SIZE (SIZE_T_ONE << TREEBIN_SHIFT) +#define MAX_SMALL_SIZE (MIN_LARGE_SIZE - SIZE_T_ONE) +#define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD) + +struct malloc_state { + binmap_t smallmap; + binmap_t treemap; + size_t dvsize; + size_t topsize; + mchunkptr dv; + mchunkptr top; + size_t trim_check; + size_t release_checks; + mchunkptr smallbins[(NSMALLBINS+1)*2]; + tbinptr treebins[NTREEBINS]; + msegment seg; +}; + +typedef struct malloc_state *mstate; + +#define is_initialized(M) ((M)->top != 0) + +/* -------------------------- system alloc setup ------------------------- */ + +/* page-align a size */ +#define page_align(S)\ + (((S) + (LJ_PAGESIZE - SIZE_T_ONE)) & ~(LJ_PAGESIZE - SIZE_T_ONE)) + +/* granularity-align a size */ +#define granularity_align(S)\ + (((S) + (DEFAULT_GRANULARITY - SIZE_T_ONE))\ + & ~(DEFAULT_GRANULARITY - SIZE_T_ONE)) + +#if LJ_TARGET_WINDOWS +#define mmap_align(S) granularity_align(S) +#else +#define mmap_align(S) page_align(S) +#endif + +/* True if segment S holds address A */ +#define segment_holds(S, A)\ + ((char *)(A) >= S->base && (char *)(A) < S->base + S->size) + +/* Return segment holding given address */ +static msegmentptr segment_holding(mstate m, char *addr) +{ + msegmentptr sp = &m->seg; + for (;;) { + if (addr >= sp->base && addr < sp->base + sp->size) + return sp; + if ((sp = sp->next) == 0) + return 0; + } +} + +/* Return true if segment contains a segment link */ +static int has_segment_link(mstate m, msegmentptr ss) +{ + msegmentptr sp = &m->seg; + for (;;) { + if ((char *)sp >= ss->base && (char *)sp < ss->base + ss->size) + return 1; + if ((sp = sp->next) == 0) + return 0; + } +} + +/* + TOP_FOOT_SIZE is padding at the end of a segment, including space + that may be needed to place segment records and fenceposts when new + noncontiguous segments are added. +*/ +#define TOP_FOOT_SIZE\ + (align_offset(chunk2mem(0))+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE) + +/* ---------------------------- Indexing Bins ---------------------------- */ + +#define is_small(s) (((s) >> SMALLBIN_SHIFT) < NSMALLBINS) +#define small_index(s) ((s) >> SMALLBIN_SHIFT) +#define small_index2size(i) ((i) << SMALLBIN_SHIFT) +#define MIN_SMALL_INDEX (small_index(MIN_CHUNK_SIZE)) + +/* addressing by index. See above about smallbin repositioning */ +#define smallbin_at(M, i) ((sbinptr)((char *)&((M)->smallbins[(i)<<1]))) +#define treebin_at(M,i) (&((M)->treebins[i])) + +/* assign tree index for size S to variable I */ +#define compute_tree_index(S, I)\ +{\ + unsigned int X = (unsigned int)(S >> TREEBIN_SHIFT);\ + if (X == 0) {\ + I = 0;\ + } else if (X > 0xFFFF) {\ + I = NTREEBINS-1;\ + } else {\ + unsigned int K = lj_fls(X);\ + I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ + }\ +} + +/* Bit representing maximum resolved size in a treebin at i */ +#define bit_for_tree_index(i) \ + (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2) + +/* Shift placing maximum resolved bit in a treebin at i as sign bit */ +#define leftshift_for_tree_index(i) \ + ((i == NTREEBINS-1)? 0 : \ + ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2))) + +/* The size of the smallest chunk held in bin with index i */ +#define minsize_for_tree_index(i) \ + ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) | \ + (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1))) + +/* ------------------------ Operations on bin maps ----------------------- */ + +/* bit corresponding to given index */ +#define idx2bit(i) ((binmap_t)(1) << (i)) + +/* Mark/Clear bits with given index */ +#define mark_smallmap(M,i) ((M)->smallmap |= idx2bit(i)) +#define clear_smallmap(M,i) ((M)->smallmap &= ~idx2bit(i)) +#define smallmap_is_marked(M,i) ((M)->smallmap & idx2bit(i)) + +#define mark_treemap(M,i) ((M)->treemap |= idx2bit(i)) +#define clear_treemap(M,i) ((M)->treemap &= ~idx2bit(i)) +#define treemap_is_marked(M,i) ((M)->treemap & idx2bit(i)) + +/* mask with all bits to left of least bit of x on */ +#define left_bits(x) ((x<<1) | (~(x<<1)+1)) + +/* Set cinuse bit and pinuse bit of next chunk */ +#define set_inuse(M,p,s)\ + ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ + ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT) + +/* Set cinuse and pinuse of this chunk and pinuse of next chunk */ +#define set_inuse_and_pinuse(M,p,s)\ + ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ + ((mchunkptr)(((char *)(p)) + (s)))->head |= PINUSE_BIT) + +/* Set size, cinuse and pinuse bit of this chunk */ +#define set_size_and_pinuse_of_inuse_chunk(M, p, s)\ + ((p)->head = (s|PINUSE_BIT|CINUSE_BIT)) + +/* ----------------------- Operations on smallbins ----------------------- */ + +/* Link a free chunk into a smallbin */ +#define insert_small_chunk(M, P, S) {\ + bindex_t I = small_index(S);\ + mchunkptr B = smallbin_at(M, I);\ + mchunkptr F = B;\ + if (!smallmap_is_marked(M, I))\ + mark_smallmap(M, I);\ + else\ + F = B->fd;\ + B->fd = P;\ + F->bk = P;\ + P->fd = F;\ + P->bk = B;\ +} + +/* Unlink a chunk from a smallbin */ +#define unlink_small_chunk(M, P, S) {\ + mchunkptr F = P->fd;\ + mchunkptr B = P->bk;\ + bindex_t I = small_index(S);\ + if (F == B) {\ + clear_smallmap(M, I);\ + } else {\ + F->bk = B;\ + B->fd = F;\ + }\ +} + +/* Unlink the first chunk from a smallbin */ +#define unlink_first_small_chunk(M, B, P, I) {\ + mchunkptr F = P->fd;\ + if (B == F) {\ + clear_smallmap(M, I);\ + } else {\ + B->fd = F;\ + F->bk = B;\ + }\ +} + +/* Replace dv node, binning the old one */ +/* Used only when dvsize known to be small */ +#define replace_dv(M, P, S) {\ + size_t DVS = M->dvsize;\ + if (DVS != 0) {\ + mchunkptr DV = M->dv;\ + insert_small_chunk(M, DV, DVS);\ + }\ + M->dvsize = S;\ + M->dv = P;\ +} + +/* ------------------------- Operations on trees ------------------------- */ + +/* Insert chunk into tree */ +#define insert_large_chunk(M, X, S) {\ + tbinptr *H;\ + bindex_t I;\ + compute_tree_index(S, I);\ + H = treebin_at(M, I);\ + X->index = I;\ + X->child[0] = X->child[1] = 0;\ + if (!treemap_is_marked(M, I)) {\ + mark_treemap(M, I);\ + *H = X;\ + X->parent = (tchunkptr)H;\ + X->fd = X->bk = X;\ + } else {\ + tchunkptr T = *H;\ + size_t K = S << leftshift_for_tree_index(I);\ + for (;;) {\ + if (chunksize(T) != S) {\ + tchunkptr *C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\ + K <<= 1;\ + if (*C != 0) {\ + T = *C;\ + } else {\ + *C = X;\ + X->parent = T;\ + X->fd = X->bk = X;\ + break;\ + }\ + } else {\ + tchunkptr F = T->fd;\ + T->fd = F->bk = X;\ + X->fd = F;\ + X->bk = T;\ + X->parent = 0;\ + break;\ + }\ + }\ + }\ +} + +#define unlink_large_chunk(M, X) {\ + tchunkptr XP = X->parent;\ + tchunkptr R;\ + if (X->bk != X) {\ + tchunkptr F = X->fd;\ + R = X->bk;\ + F->bk = R;\ + R->fd = F;\ + } else {\ + tchunkptr *RP;\ + if (((R = *(RP = &(X->child[1]))) != 0) ||\ + ((R = *(RP = &(X->child[0]))) != 0)) {\ + tchunkptr *CP;\ + while ((*(CP = &(R->child[1])) != 0) ||\ + (*(CP = &(R->child[0])) != 0)) {\ + R = *(RP = CP);\ + }\ + *RP = 0;\ + }\ + }\ + if (XP != 0) {\ + tbinptr *H = treebin_at(M, X->index);\ + if (X == *H) {\ + if ((*H = R) == 0) \ + clear_treemap(M, X->index);\ + } else {\ + if (XP->child[0] == X) \ + XP->child[0] = R;\ + else \ + XP->child[1] = R;\ + }\ + if (R != 0) {\ + tchunkptr C0, C1;\ + R->parent = XP;\ + if ((C0 = X->child[0]) != 0) {\ + R->child[0] = C0;\ + C0->parent = R;\ + }\ + if ((C1 = X->child[1]) != 0) {\ + R->child[1] = C1;\ + C1->parent = R;\ + }\ + }\ + }\ +} + +/* Relays to large vs small bin operations */ + +#define insert_chunk(M, P, S)\ + if (is_small(S)) { insert_small_chunk(M, P, S)\ + } else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); } + +#define unlink_chunk(M, P, S)\ + if (is_small(S)) { unlink_small_chunk(M, P, S)\ + } else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); } + +/* ----------------------- Direct-mmapping chunks ----------------------- */ + +static void *direct_alloc(size_t nb) +{ + size_t mmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); + if (LJ_LIKELY(mmsize > nb)) { /* Check for wrap around 0 */ + char *mm = (char *)(DIRECT_MMAP(mmsize)); + if (mm != CMFAIL) { + size_t offset = align_offset(chunk2mem(mm)); + size_t psize = mmsize - offset - DIRECT_FOOT_PAD; + mchunkptr p = (mchunkptr)(mm + offset); + p->prev_foot = offset | IS_DIRECT_BIT; + p->head = psize|CINUSE_BIT; + chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD; + chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0; + return chunk2mem(p); + } + } + return NULL; +} + +static mchunkptr direct_resize(mchunkptr oldp, size_t nb) +{ + size_t oldsize = chunksize(oldp); + if (is_small(nb)) /* Can't shrink direct regions below small size */ + return NULL; + /* Keep old chunk if big enough but not too big */ + if (oldsize >= nb + SIZE_T_SIZE && + (oldsize - nb) <= (DEFAULT_GRANULARITY >> 1)) { + return oldp; + } else { + size_t offset = oldp->prev_foot & ~IS_DIRECT_BIT; + size_t oldmmsize = oldsize + offset + DIRECT_FOOT_PAD; + size_t newmmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); + char *cp = (char *)CALL_MREMAP((char *)oldp - offset, + oldmmsize, newmmsize, CALL_MREMAP_MV); + if (cp != CMFAIL) { + mchunkptr newp = (mchunkptr)(cp + offset); + size_t psize = newmmsize - offset - DIRECT_FOOT_PAD; + newp->head = psize|CINUSE_BIT; + chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD; + chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0; + return newp; + } + } + return NULL; +} + +/* -------------------------- mspace management -------------------------- */ + +/* Initialize top chunk and its size */ +static void init_top(mstate m, mchunkptr p, size_t psize) +{ + /* Ensure alignment */ + size_t offset = align_offset(chunk2mem(p)); + p = (mchunkptr)((char *)p + offset); + psize -= offset; + + m->top = p; + m->topsize = psize; + p->head = psize | PINUSE_BIT; + /* set size of fake trailing chunk holding overhead space only once */ + chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE; + m->trim_check = DEFAULT_TRIM_THRESHOLD; /* reset on each update */ +} + +/* Initialize bins for a new mstate that is otherwise zeroed out */ +static void init_bins(mstate m) +{ + /* Establish circular links for smallbins */ + bindex_t i; + for (i = 0; i < NSMALLBINS; i++) { + sbinptr bin = smallbin_at(m,i); + bin->fd = bin->bk = bin; + } +} + +/* Allocate chunk and prepend remainder with chunk in successor base. */ +static void *prepend_alloc(mstate m, char *newbase, char *oldbase, size_t nb) +{ + mchunkptr p = align_as_chunk(newbase); + mchunkptr oldfirst = align_as_chunk(oldbase); + size_t psize = (size_t)((char *)oldfirst - (char *)p); + mchunkptr q = chunk_plus_offset(p, nb); + size_t qsize = psize - nb; + set_size_and_pinuse_of_inuse_chunk(m, p, nb); + + /* consolidate remainder with first chunk of old base */ + if (oldfirst == m->top) { + size_t tsize = m->topsize += qsize; + m->top = q; + q->head = tsize | PINUSE_BIT; + } else if (oldfirst == m->dv) { + size_t dsize = m->dvsize += qsize; + m->dv = q; + set_size_and_pinuse_of_free_chunk(q, dsize); + } else { + if (!cinuse(oldfirst)) { + size_t nsize = chunksize(oldfirst); + unlink_chunk(m, oldfirst, nsize); + oldfirst = chunk_plus_offset(oldfirst, nsize); + qsize += nsize; + } + set_free_with_pinuse(q, qsize, oldfirst); + insert_chunk(m, q, qsize); + } + + return chunk2mem(p); +} + +/* Add a segment to hold a new noncontiguous region */ +static void add_segment(mstate m, char *tbase, size_t tsize) +{ + /* Determine locations and sizes of segment, fenceposts, old top */ + char *old_top = (char *)m->top; + msegmentptr oldsp = segment_holding(m, old_top); + char *old_end = oldsp->base + oldsp->size; + size_t ssize = pad_request(sizeof(struct malloc_segment)); + char *rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK); + size_t offset = align_offset(chunk2mem(rawsp)); + char *asp = rawsp + offset; + char *csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp; + mchunkptr sp = (mchunkptr)csp; + msegmentptr ss = (msegmentptr)(chunk2mem(sp)); + mchunkptr tnext = chunk_plus_offset(sp, ssize); + mchunkptr p = tnext; + + /* reset top to new space */ + init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); + + /* Set up segment record */ + set_size_and_pinuse_of_inuse_chunk(m, sp, ssize); + *ss = m->seg; /* Push current record */ + m->seg.base = tbase; + m->seg.size = tsize; + m->seg.next = ss; + + /* Insert trailing fenceposts */ + for (;;) { + mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE); + p->head = FENCEPOST_HEAD; + if ((char *)(&(nextp->head)) < old_end) + p = nextp; + else + break; + } + + /* Insert the rest of old top into a bin as an ordinary free chunk */ + if (csp != old_top) { + mchunkptr q = (mchunkptr)old_top; + size_t psize = (size_t)(csp - old_top); + mchunkptr tn = chunk_plus_offset(q, psize); + set_free_with_pinuse(q, psize, tn); + insert_chunk(m, q, psize); + } +} + +/* -------------------------- System allocation -------------------------- */ + +static void *alloc_sys(mstate m, size_t nb) +{ + char *tbase = CMFAIL; + size_t tsize = 0; + + /* Directly map large chunks */ + if (LJ_UNLIKELY(nb >= DEFAULT_MMAP_THRESHOLD)) { + void *mem = direct_alloc(nb); + if (mem != 0) + return mem; + } + + { + size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE; + size_t rsize = granularity_align(req); + if (LJ_LIKELY(rsize > nb)) { /* Fail if wraps around zero */ + char *mp = (char *)(CALL_MMAP(rsize)); + if (mp != CMFAIL) { + tbase = mp; + tsize = rsize; + } + } + } + + if (tbase != CMFAIL) { + msegmentptr sp = &m->seg; + /* Try to merge with an existing segment */ + while (sp != 0 && tbase != sp->base + sp->size) + sp = sp->next; + if (sp != 0 && segment_holds(sp, m->top)) { /* append */ + sp->size += tsize; + init_top(m, m->top, m->topsize + tsize); + } else { + sp = &m->seg; + while (sp != 0 && sp->base != tbase + tsize) + sp = sp->next; + if (sp != 0) { + char *oldbase = sp->base; + sp->base = tbase; + sp->size += tsize; + return prepend_alloc(m, tbase, oldbase, nb); + } else { + add_segment(m, tbase, tsize); + } + } + + if (nb < m->topsize) { /* Allocate from new or extended top space */ + size_t rsize = m->topsize -= nb; + mchunkptr p = m->top; + mchunkptr r = m->top = chunk_plus_offset(p, nb); + r->head = rsize | PINUSE_BIT; + set_size_and_pinuse_of_inuse_chunk(m, p, nb); + return chunk2mem(p); + } + } + + return NULL; +} + +/* ----------------------- system deallocation -------------------------- */ + +/* Unmap and unlink any mmapped segments that don't contain used chunks */ +static size_t release_unused_segments(mstate m) +{ + size_t released = 0; + size_t nsegs = 0; + msegmentptr pred = &m->seg; + msegmentptr sp = pred->next; + while (sp != 0) { + char *base = sp->base; + size_t size = sp->size; + msegmentptr next = sp->next; + nsegs++; + { + mchunkptr p = align_as_chunk(base); + size_t psize = chunksize(p); + /* Can unmap if first chunk holds entire segment and not pinned */ + if (!cinuse(p) && (char *)p + psize >= base + size - TOP_FOOT_SIZE) { + tchunkptr tp = (tchunkptr)p; + if (p == m->dv) { + m->dv = 0; + m->dvsize = 0; + } else { + unlink_large_chunk(m, tp); + } + if (CALL_MUNMAP(base, size) == 0) { + released += size; + /* unlink obsoleted record */ + sp = pred; + sp->next = next; + } else { /* back out if cannot unmap */ + insert_large_chunk(m, tp, psize); + } + } + } + pred = sp; + sp = next; + } + /* Reset check counter */ + m->release_checks = nsegs > MAX_RELEASE_CHECK_RATE ? + nsegs : MAX_RELEASE_CHECK_RATE; + return released; +} + +static int alloc_trim(mstate m, size_t pad) +{ + size_t released = 0; + if (pad < MAX_REQUEST && is_initialized(m)) { + pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */ + + if (m->topsize > pad) { + /* Shrink top space in granularity-size units, keeping at least one */ + size_t unit = DEFAULT_GRANULARITY; + size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - + SIZE_T_ONE) * unit; + msegmentptr sp = segment_holding(m, (char *)m->top); + + if (sp->size >= extra && + !has_segment_link(m, sp)) { /* can't shrink if pinned */ + size_t newsize = sp->size - extra; + /* Prefer mremap, fall back to munmap */ + if ((CALL_MREMAP(sp->base, sp->size, newsize, CALL_MREMAP_NOMOVE) != MFAIL) || + (CALL_MUNMAP(sp->base + newsize, extra) == 0)) { + released = extra; + } + } + + if (released != 0) { + sp->size -= released; + init_top(m, m->top, m->topsize - released); + } + } + + /* Unmap any unused mmapped segments */ + released += release_unused_segments(m); + + /* On failure, disable autotrim to avoid repeated failed future calls */ + if (released == 0 && m->topsize > m->trim_check) + m->trim_check = MAX_SIZE_T; + } + + return (released != 0)? 1 : 0; +} + +/* ---------------------------- malloc support --------------------------- */ + +/* allocate a large request from the best fitting chunk in a treebin */ +static void *tmalloc_large(mstate m, size_t nb) +{ + tchunkptr v = 0; + size_t rsize = ~nb+1; /* Unsigned negation */ + tchunkptr t; + bindex_t idx; + compute_tree_index(nb, idx); + + if ((t = *treebin_at(m, idx)) != 0) { + /* Traverse tree for this bin looking for node with size == nb */ + size_t sizebits = nb << leftshift_for_tree_index(idx); + tchunkptr rst = 0; /* The deepest untaken right subtree */ + for (;;) { + tchunkptr rt; + size_t trem = chunksize(t) - nb; + if (trem < rsize) { + v = t; + if ((rsize = trem) == 0) + break; + } + rt = t->child[1]; + t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]; + if (rt != 0 && rt != t) + rst = rt; + if (t == 0) { + t = rst; /* set t to least subtree holding sizes > nb */ + break; + } + sizebits <<= 1; + } + } + + if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */ + binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap; + if (leftbits != 0) + t = *treebin_at(m, lj_ffs(leftbits)); + } + + while (t != 0) { /* find smallest of tree or subtree */ + size_t trem = chunksize(t) - nb; + if (trem < rsize) { + rsize = trem; + v = t; + } + t = leftmost_child(t); + } + + /* If dv is a better fit, return NULL so malloc will use it */ + if (v != 0 && rsize < (size_t)(m->dvsize - nb)) { + mchunkptr r = chunk_plus_offset(v, nb); + unlink_large_chunk(m, v); + if (rsize < MIN_CHUNK_SIZE) { + set_inuse_and_pinuse(m, v, (rsize + nb)); + } else { + set_size_and_pinuse_of_inuse_chunk(m, v, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + insert_chunk(m, r, rsize); + } + return chunk2mem(v); + } + return NULL; +} + +/* allocate a small request from the best fitting chunk in a treebin */ +static void *tmalloc_small(mstate m, size_t nb) +{ + tchunkptr t, v; + mchunkptr r; + size_t rsize; + bindex_t i = lj_ffs(m->treemap); + + v = t = *treebin_at(m, i); + rsize = chunksize(t) - nb; + + while ((t = leftmost_child(t)) != 0) { + size_t trem = chunksize(t) - nb; + if (trem < rsize) { + rsize = trem; + v = t; + } + } + + r = chunk_plus_offset(v, nb); + unlink_large_chunk(m, v); + if (rsize < MIN_CHUNK_SIZE) { + set_inuse_and_pinuse(m, v, (rsize + nb)); + } else { + set_size_and_pinuse_of_inuse_chunk(m, v, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + replace_dv(m, r, rsize); + } + return chunk2mem(v); +} + +/* ----------------------------------------------------------------------- */ + +void *lj_alloc_create(void) +{ + size_t tsize = DEFAULT_GRANULARITY; + char *tbase; + INIT_MMAP(); + tbase = (char *)(CALL_MMAP(tsize)); + if (tbase != CMFAIL) { + size_t msize = pad_request(sizeof(struct malloc_state)); + mchunkptr mn; + mchunkptr msp = align_as_chunk(tbase); + mstate m = (mstate)(chunk2mem(msp)); + memset(m, 0, msize); + msp->head = (msize|PINUSE_BIT|CINUSE_BIT); + m->seg.base = tbase; + m->seg.size = tsize; + m->release_checks = MAX_RELEASE_CHECK_RATE; + init_bins(m); + mn = next_chunk(mem2chunk(m)); + init_top(m, mn, (size_t)((tbase + tsize) - (char *)mn) - TOP_FOOT_SIZE); + return m; + } + return NULL; +} + +void lj_alloc_destroy(void *msp) +{ + mstate ms = (mstate)msp; + msegmentptr sp = &ms->seg; + while (sp != 0) { + char *base = sp->base; + size_t size = sp->size; + sp = sp->next; + CALL_MUNMAP(base, size); + } +} + +static LJ_NOINLINE void *lj_alloc_malloc(void *msp, size_t nsize) +{ + mstate ms = (mstate)msp; + void *mem; + size_t nb; + if (nsize <= MAX_SMALL_REQUEST) { + bindex_t idx; + binmap_t smallbits; + nb = (nsize < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(nsize); + idx = small_index(nb); + smallbits = ms->smallmap >> idx; + + if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ + mchunkptr b, p; + idx += ~smallbits & 1; /* Uses next bin if idx empty */ + b = smallbin_at(ms, idx); + p = b->fd; + unlink_first_small_chunk(ms, b, p, idx); + set_inuse_and_pinuse(ms, p, small_index2size(idx)); + mem = chunk2mem(p); + return mem; + } else if (nb > ms->dvsize) { + if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ + mchunkptr b, p, r; + size_t rsize; + binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); + bindex_t i = lj_ffs(leftbits); + b = smallbin_at(ms, i); + p = b->fd; + unlink_first_small_chunk(ms, b, p, i); + rsize = small_index2size(i) - nb; + /* Fit here cannot be remainderless if 4byte sizes */ + if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) { + set_inuse_and_pinuse(ms, p, small_index2size(i)); + } else { + set_size_and_pinuse_of_inuse_chunk(ms, p, nb); + r = chunk_plus_offset(p, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + replace_dv(ms, r, rsize); + } + mem = chunk2mem(p); + return mem; + } else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) { + return mem; + } + } + } else if (nsize >= MAX_REQUEST) { + nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ + } else { + nb = pad_request(nsize); + if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) { + return mem; + } + } + + if (nb <= ms->dvsize) { + size_t rsize = ms->dvsize - nb; + mchunkptr p = ms->dv; + if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ + mchunkptr r = ms->dv = chunk_plus_offset(p, nb); + ms->dvsize = rsize; + set_size_and_pinuse_of_free_chunk(r, rsize); + set_size_and_pinuse_of_inuse_chunk(ms, p, nb); + } else { /* exhaust dv */ + size_t dvs = ms->dvsize; + ms->dvsize = 0; + ms->dv = 0; + set_inuse_and_pinuse(ms, p, dvs); + } + mem = chunk2mem(p); + return mem; + } else if (nb < ms->topsize) { /* Split top */ + size_t rsize = ms->topsize -= nb; + mchunkptr p = ms->top; + mchunkptr r = ms->top = chunk_plus_offset(p, nb); + r->head = rsize | PINUSE_BIT; + set_size_and_pinuse_of_inuse_chunk(ms, p, nb); + mem = chunk2mem(p); + return mem; + } + return alloc_sys(ms, nb); +} + +static LJ_NOINLINE void *lj_alloc_free(void *msp, void *ptr) +{ + if (ptr != 0) { + mchunkptr p = mem2chunk(ptr); + mstate fm = (mstate)msp; + size_t psize = chunksize(p); + mchunkptr next = chunk_plus_offset(p, psize); + if (!pinuse(p)) { + size_t prevsize = p->prev_foot; + if ((prevsize & IS_DIRECT_BIT) != 0) { + prevsize &= ~IS_DIRECT_BIT; + psize += prevsize + DIRECT_FOOT_PAD; + CALL_MUNMAP((char *)p - prevsize, psize); + return NULL; + } else { + mchunkptr prev = chunk_minus_offset(p, prevsize); + psize += prevsize; + p = prev; + /* consolidate backward */ + if (p != fm->dv) { + unlink_chunk(fm, p, prevsize); + } else if ((next->head & INUSE_BITS) == INUSE_BITS) { + fm->dvsize = psize; + set_free_with_pinuse(p, psize, next); + return NULL; + } + } + } + if (!cinuse(next)) { /* consolidate forward */ + if (next == fm->top) { + size_t tsize = fm->topsize += psize; + fm->top = p; + p->head = tsize | PINUSE_BIT; + if (p == fm->dv) { + fm->dv = 0; + fm->dvsize = 0; + } + if (tsize > fm->trim_check) + alloc_trim(fm, 0); + return NULL; + } else if (next == fm->dv) { + size_t dsize = fm->dvsize += psize; + fm->dv = p; + set_size_and_pinuse_of_free_chunk(p, dsize); + return NULL; + } else { + size_t nsize = chunksize(next); + psize += nsize; + unlink_chunk(fm, next, nsize); + set_size_and_pinuse_of_free_chunk(p, psize); + if (p == fm->dv) { + fm->dvsize = psize; + return NULL; + } + } + } else { + set_free_with_pinuse(p, psize, next); + } + + if (is_small(psize)) { + insert_small_chunk(fm, p, psize); + } else { + tchunkptr tp = (tchunkptr)p; + insert_large_chunk(fm, tp, psize); + if (--fm->release_checks == 0) + release_unused_segments(fm); + } + } + return NULL; +} + +static LJ_NOINLINE void *lj_alloc_realloc(void *msp, void *ptr, size_t nsize) +{ + if (nsize >= MAX_REQUEST) { + return NULL; + } else { + mstate m = (mstate)msp; + mchunkptr oldp = mem2chunk(ptr); + size_t oldsize = chunksize(oldp); + mchunkptr next = chunk_plus_offset(oldp, oldsize); + mchunkptr newp = 0; + size_t nb = request2size(nsize); + + /* Try to either shrink or extend into top. Else malloc-copy-free */ + if (is_direct(oldp)) { + newp = direct_resize(oldp, nb); /* this may return NULL. */ + } else if (oldsize >= nb) { /* already big enough */ + size_t rsize = oldsize - nb; + newp = oldp; + if (rsize >= MIN_CHUNK_SIZE) { + mchunkptr rem = chunk_plus_offset(newp, nb); + set_inuse(m, newp, nb); + set_inuse(m, rem, rsize); + lj_alloc_free(m, chunk2mem(rem)); + } + } else if (next == m->top && oldsize + m->topsize > nb) { + /* Expand into top */ + size_t newsize = oldsize + m->topsize; + size_t newtopsize = newsize - nb; + mchunkptr newtop = chunk_plus_offset(oldp, nb); + set_inuse(m, oldp, nb); + newtop->head = newtopsize |PINUSE_BIT; + m->top = newtop; + m->topsize = newtopsize; + newp = oldp; + } + + if (newp != 0) { + return chunk2mem(newp); + } else { + void *newmem = lj_alloc_malloc(m, nsize); + if (newmem != 0) { + size_t oc = oldsize - overhead_for(oldp); + memcpy(newmem, ptr, oc < nsize ? oc : nsize); + lj_alloc_free(m, ptr); + } + return newmem; + } + } +} + +void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize) +{ + (void)osize; + if (nsize == 0) { + return lj_alloc_free(msp, ptr); + } else if (ptr == NULL) { + return lj_alloc_malloc(msp, nsize); + } else { + return lj_alloc_realloc(msp, ptr, nsize); + } +} + +#endif diff --git a/external/lua/luajit/src/src/lj_alloc.h b/external/lua/luajit/src/src/lj_alloc.h new file mode 100644 index 0000000000..f87a7cf342 --- /dev/null +++ b/external/lua/luajit/src/src/lj_alloc.h @@ -0,0 +1,17 @@ +/* +** Bundled memory allocator. +** Donated to the public domain. +*/ + +#ifndef _LJ_ALLOC_H +#define _LJ_ALLOC_H + +#include "lj_def.h" + +#ifndef LUAJIT_USE_SYSMALLOC +LJ_FUNC void *lj_alloc_create(void); +LJ_FUNC void lj_alloc_destroy(void *msp); +LJ_FUNC void *lj_alloc_f(void *msp, void *ptr, size_t osize, size_t nsize); +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_api.c b/external/lua/luajit/src/src/lj_api.c new file mode 100644 index 0000000000..edb2d620da --- /dev/null +++ b/external/lua/luajit/src/src/lj_api.c @@ -0,0 +1,1200 @@ +/* +** Public Lua/C API. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lj_api_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_debug.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_func.h" +#include "lj_udata.h" +#include "lj_meta.h" +#include "lj_state.h" +#include "lj_bc.h" +#include "lj_frame.h" +#include "lj_trace.h" +#include "lj_vm.h" +#include "lj_strscan.h" + +/* -- Common helper functions --------------------------------------------- */ + +#define api_checknelems(L, n) api_check(L, (n) <= (L->top - L->base)) +#define api_checkvalidindex(L, i) api_check(L, (i) != niltv(L)) + +static TValue *index2adr(lua_State *L, int idx) +{ + if (idx > 0) { + TValue *o = L->base + (idx - 1); + return o < L->top ? o : niltv(L); + } else if (idx > LUA_REGISTRYINDEX) { + api_check(L, idx != 0 && -idx <= L->top - L->base); + return L->top + idx; + } else if (idx == LUA_GLOBALSINDEX) { + TValue *o = &G(L)->tmptv; + settabV(L, o, tabref(L->env)); + return o; + } else if (idx == LUA_REGISTRYINDEX) { + return registry(L); + } else { + GCfunc *fn = curr_func(L); + api_check(L, fn->c.gct == ~LJ_TFUNC && !isluafunc(fn)); + if (idx == LUA_ENVIRONINDEX) { + TValue *o = &G(L)->tmptv; + settabV(L, o, tabref(fn->c.env)); + return o; + } else { + idx = LUA_GLOBALSINDEX - idx; + return idx <= fn->c.nupvalues ? &fn->c.upvalue[idx-1] : niltv(L); + } + } +} + +static TValue *stkindex2adr(lua_State *L, int idx) +{ + if (idx > 0) { + TValue *o = L->base + (idx - 1); + return o < L->top ? o : niltv(L); + } else { + api_check(L, idx != 0 && -idx <= L->top - L->base); + return L->top + idx; + } +} + +static GCtab *getcurrenv(lua_State *L) +{ + GCfunc *fn = curr_func(L); + return fn->c.gct == ~LJ_TFUNC ? tabref(fn->c.env) : tabref(L->env); +} + +/* -- Miscellaneous API functions ----------------------------------------- */ + +LUA_API int lua_status(lua_State *L) +{ + return L->status; +} + +LUA_API int lua_checkstack(lua_State *L, int size) +{ + if (size > LUAI_MAXCSTACK || (L->top - L->base + size) > LUAI_MAXCSTACK) { + return 0; /* Stack overflow. */ + } else if (size > 0) { + lj_state_checkstack(L, (MSize)size); + } + return 1; +} + +LUALIB_API void luaL_checkstack(lua_State *L, int size, const char *msg) +{ + if (!lua_checkstack(L, size)) + lj_err_callerv(L, LJ_ERR_STKOVM, msg); +} + +LUA_API void lua_xmove(lua_State *from, lua_State *to, int n) +{ + TValue *f, *t; + if (from == to) return; + api_checknelems(from, n); + api_check(from, G(from) == G(to)); + lj_state_checkstack(to, (MSize)n); + f = from->top; + t = to->top = to->top + n; + while (--n >= 0) copyTV(to, --t, --f); + from->top = f; +} + +/* -- Stack manipulation -------------------------------------------------- */ + +LUA_API int lua_gettop(lua_State *L) +{ + return (int)(L->top - L->base); +} + +LUA_API void lua_settop(lua_State *L, int idx) +{ + if (idx >= 0) { + api_check(L, idx <= tvref(L->maxstack) - L->base); + if (L->base + idx > L->top) { + if (L->base + idx >= tvref(L->maxstack)) + lj_state_growstack(L, (MSize)idx - (MSize)(L->top - L->base)); + do { setnilV(L->top++); } while (L->top < L->base + idx); + } else { + L->top = L->base + idx; + } + } else { + api_check(L, -(idx+1) <= (L->top - L->base)); + L->top += idx+1; /* Shrinks top (idx < 0). */ + } +} + +LUA_API void lua_remove(lua_State *L, int idx) +{ + TValue *p = stkindex2adr(L, idx); + api_checkvalidindex(L, p); + while (++p < L->top) copyTV(L, p-1, p); + L->top--; +} + +LUA_API void lua_insert(lua_State *L, int idx) +{ + TValue *q, *p = stkindex2adr(L, idx); + api_checkvalidindex(L, p); + for (q = L->top; q > p; q--) copyTV(L, q, q-1); + copyTV(L, p, L->top); +} + +LUA_API void lua_replace(lua_State *L, int idx) +{ + api_checknelems(L, 1); + if (idx == LUA_GLOBALSINDEX) { + api_check(L, tvistab(L->top-1)); + /* NOBARRIER: A thread (i.e. L) is never black. */ + setgcref(L->env, obj2gco(tabV(L->top-1))); + } else if (idx == LUA_ENVIRONINDEX) { + GCfunc *fn = curr_func(L); + if (fn->c.gct != ~LJ_TFUNC) + lj_err_msg(L, LJ_ERR_NOENV); + api_check(L, tvistab(L->top-1)); + setgcref(fn->c.env, obj2gco(tabV(L->top-1))); + lj_gc_barrier(L, fn, L->top-1); + } else { + TValue *o = index2adr(L, idx); + api_checkvalidindex(L, o); + copyTV(L, o, L->top-1); + if (idx < LUA_GLOBALSINDEX) /* Need a barrier for upvalues. */ + lj_gc_barrier(L, curr_func(L), L->top-1); + } + L->top--; +} + +LUA_API void lua_pushvalue(lua_State *L, int idx) +{ + copyTV(L, L->top, index2adr(L, idx)); + incr_top(L); +} + +/* -- Stack getters ------------------------------------------------------- */ + +LUA_API int lua_type(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + if (tvisnumber(o)) { + return LUA_TNUMBER; +#if LJ_64 + } else if (tvislightud(o)) { + return LUA_TLIGHTUSERDATA; +#endif + } else if (o == niltv(L)) { + return LUA_TNONE; + } else { /* Magic internal/external tag conversion. ORDER LJ_T */ + uint32_t t = ~itype(o); +#if LJ_64 + int tt = (int)((U64x(75a06,98042110) >> 4*t) & 15u); +#else + int tt = (int)(((t < 8 ? 0x98042110u : 0x75a06u) >> 4*(t&7)) & 15u); +#endif + lua_assert(tt != LUA_TNIL || tvisnil(o)); + return tt; + } +} + +LUALIB_API void luaL_checktype(lua_State *L, int idx, int tt) +{ + if (lua_type(L, idx) != tt) + lj_err_argt(L, idx, tt); +} + +LUALIB_API void luaL_checkany(lua_State *L, int idx) +{ + if (index2adr(L, idx) == niltv(L)) + lj_err_arg(L, idx, LJ_ERR_NOVAL); +} + +LUA_API const char *lua_typename(lua_State *L, int t) +{ + UNUSED(L); + return lj_obj_typename[t+1]; +} + +LUA_API int lua_iscfunction(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + return tvisfunc(o) && !isluafunc(funcV(o)); +} + +LUA_API int lua_isnumber(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + TValue tmp; + return (tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), &tmp))); +} + +LUA_API int lua_isstring(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + return (tvisstr(o) || tvisnumber(o)); +} + +LUA_API int lua_isuserdata(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + return (tvisudata(o) || tvislightud(o)); +} + +LUA_API int lua_rawequal(lua_State *L, int idx1, int idx2) +{ + cTValue *o1 = index2adr(L, idx1); + cTValue *o2 = index2adr(L, idx2); + return (o1 == niltv(L) || o2 == niltv(L)) ? 0 : lj_obj_equal(o1, o2); +} + +LUA_API int lua_equal(lua_State *L, int idx1, int idx2) +{ + cTValue *o1 = index2adr(L, idx1); + cTValue *o2 = index2adr(L, idx2); + if (tvisint(o1) && tvisint(o2)) { + return intV(o1) == intV(o2); + } else if (tvisnumber(o1) && tvisnumber(o2)) { + return numberVnum(o1) == numberVnum(o2); + } else if (itype(o1) != itype(o2)) { + return 0; + } else if (tvispri(o1)) { + return o1 != niltv(L) && o2 != niltv(L); +#if LJ_64 + } else if (tvislightud(o1)) { + return o1->u64 == o2->u64; +#endif + } else if (gcrefeq(o1->gcr, o2->gcr)) { + return 1; + } else if (!tvistabud(o1)) { + return 0; + } else { + TValue *base = lj_meta_equal(L, gcV(o1), gcV(o2), 0); + if ((uintptr_t)base <= 1) { + return (int)(uintptr_t)base; + } else { + L->top = base+2; + lj_vm_call(L, base, 1+1); + L->top -= 2; + return tvistruecond(L->top+1); + } + } +} + +LUA_API int lua_lessthan(lua_State *L, int idx1, int idx2) +{ + cTValue *o1 = index2adr(L, idx1); + cTValue *o2 = index2adr(L, idx2); + if (o1 == niltv(L) || o2 == niltv(L)) { + return 0; + } else if (tvisint(o1) && tvisint(o2)) { + return intV(o1) < intV(o2); + } else if (tvisnumber(o1) && tvisnumber(o2)) { + return numberVnum(o1) < numberVnum(o2); + } else { + TValue *base = lj_meta_comp(L, o1, o2, 0); + if ((uintptr_t)base <= 1) { + return (int)(uintptr_t)base; + } else { + L->top = base+2; + lj_vm_call(L, base, 1+1); + L->top -= 2; + return tvistruecond(L->top+1); + } + } +} + +LUA_API lua_Number lua_tonumber(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + TValue tmp; + if (LJ_LIKELY(tvisnumber(o))) + return numberVnum(o); + else if (tvisstr(o) && lj_strscan_num(strV(o), &tmp)) + return numV(&tmp); + else + return 0; +} + +LUALIB_API lua_Number luaL_checknumber(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + TValue tmp; + if (LJ_LIKELY(tvisnumber(o))) + return numberVnum(o); + else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp))) + lj_err_argt(L, idx, LUA_TNUMBER); + return numV(&tmp); +} + +LUALIB_API lua_Number luaL_optnumber(lua_State *L, int idx, lua_Number def) +{ + cTValue *o = index2adr(L, idx); + TValue tmp; + if (LJ_LIKELY(tvisnumber(o))) + return numberVnum(o); + else if (tvisnil(o)) + return def; + else if (!(tvisstr(o) && lj_strscan_num(strV(o), &tmp))) + lj_err_argt(L, idx, LUA_TNUMBER); + return numV(&tmp); +} + +LUA_API lua_Integer lua_tointeger(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + TValue tmp; + lua_Number n; + if (LJ_LIKELY(tvisint(o))) { + return intV(o); + } else if (LJ_LIKELY(tvisnum(o))) { + n = numV(o); + } else { + if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) + return 0; + if (tvisint(&tmp)) + return (lua_Integer)intV(&tmp); + n = numV(&tmp); + } +#if LJ_64 + return (lua_Integer)n; +#else + return lj_num2int(n); +#endif +} + +LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + TValue tmp; + lua_Number n; + if (LJ_LIKELY(tvisint(o))) { + return intV(o); + } else if (LJ_LIKELY(tvisnum(o))) { + n = numV(o); + } else { + if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) + lj_err_argt(L, idx, LUA_TNUMBER); + if (tvisint(&tmp)) + return (lua_Integer)intV(&tmp); + n = numV(&tmp); + } +#if LJ_64 + return (lua_Integer)n; +#else + return lj_num2int(n); +#endif +} + +LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int idx, lua_Integer def) +{ + cTValue *o = index2adr(L, idx); + TValue tmp; + lua_Number n; + if (LJ_LIKELY(tvisint(o))) { + return intV(o); + } else if (LJ_LIKELY(tvisnum(o))) { + n = numV(o); + } else if (tvisnil(o)) { + return def; + } else { + if (!(tvisstr(o) && lj_strscan_number(strV(o), &tmp))) + lj_err_argt(L, idx, LUA_TNUMBER); + if (tvisint(&tmp)) + return (lua_Integer)intV(&tmp); + n = numV(&tmp); + } +#if LJ_64 + return (lua_Integer)n; +#else + return lj_num2int(n); +#endif +} + +LUA_API int lua_toboolean(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + return tvistruecond(o); +} + +LUA_API const char *lua_tolstring(lua_State *L, int idx, size_t *len) +{ + TValue *o = index2adr(L, idx); + GCstr *s; + if (LJ_LIKELY(tvisstr(o))) { + s = strV(o); + } else if (tvisnumber(o)) { + lj_gc_check(L); + o = index2adr(L, idx); /* GC may move the stack. */ + s = lj_str_fromnumber(L, o); + setstrV(L, o, s); + } else { + if (len != NULL) *len = 0; + return NULL; + } + if (len != NULL) *len = s->len; + return strdata(s); +} + +LUALIB_API const char *luaL_checklstring(lua_State *L, int idx, size_t *len) +{ + TValue *o = index2adr(L, idx); + GCstr *s; + if (LJ_LIKELY(tvisstr(o))) { + s = strV(o); + } else if (tvisnumber(o)) { + lj_gc_check(L); + o = index2adr(L, idx); /* GC may move the stack. */ + s = lj_str_fromnumber(L, o); + setstrV(L, o, s); + } else { + lj_err_argt(L, idx, LUA_TSTRING); + } + if (len != NULL) *len = s->len; + return strdata(s); +} + +LUALIB_API const char *luaL_optlstring(lua_State *L, int idx, + const char *def, size_t *len) +{ + TValue *o = index2adr(L, idx); + GCstr *s; + if (LJ_LIKELY(tvisstr(o))) { + s = strV(o); + } else if (tvisnil(o)) { + if (len != NULL) *len = def ? strlen(def) : 0; + return def; + } else if (tvisnumber(o)) { + lj_gc_check(L); + o = index2adr(L, idx); /* GC may move the stack. */ + s = lj_str_fromnumber(L, o); + setstrV(L, o, s); + } else { + lj_err_argt(L, idx, LUA_TSTRING); + } + if (len != NULL) *len = s->len; + return strdata(s); +} + +LUALIB_API int luaL_checkoption(lua_State *L, int idx, const char *def, + const char *const lst[]) +{ + ptrdiff_t i; + const char *s = lua_tolstring(L, idx, NULL); + if (s == NULL && (s = def) == NULL) + lj_err_argt(L, idx, LUA_TSTRING); + for (i = 0; lst[i]; i++) + if (strcmp(lst[i], s) == 0) + return (int)i; + lj_err_argv(L, idx, LJ_ERR_INVOPTM, s); +} + +LUA_API size_t lua_objlen(lua_State *L, int idx) +{ + TValue *o = index2adr(L, idx); + if (tvisstr(o)) { + return strV(o)->len; + } else if (tvistab(o)) { + return (size_t)lj_tab_len(tabV(o)); + } else if (tvisudata(o)) { + return udataV(o)->len; + } else if (tvisnumber(o)) { + GCstr *s = lj_str_fromnumber(L, o); + setstrV(L, o, s); + return s->len; + } else { + return 0; + } +} + +LUA_API lua_CFunction lua_tocfunction(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + if (tvisfunc(o)) { + BCOp op = bc_op(*mref(funcV(o)->c.pc, BCIns)); + if (op == BC_FUNCC || op == BC_FUNCCW) + return funcV(o)->c.f; + } + return NULL; +} + +LUA_API void *lua_touserdata(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + if (tvisudata(o)) + return uddata(udataV(o)); + else if (tvislightud(o)) + return lightudV(o); + else + return NULL; +} + +LUA_API lua_State *lua_tothread(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + return (!tvisthread(o)) ? NULL : threadV(o); +} + +LUA_API const void *lua_topointer(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + if (tvisudata(o)) + return uddata(udataV(o)); + else if (tvislightud(o)) + return lightudV(o); + else if (tviscdata(o)) + return cdataptr(cdataV(o)); + else if (tvisgcv(o)) + return gcV(o); + else + return NULL; +} + +/* -- Stack setters (object creation) ------------------------------------- */ + +LUA_API void lua_pushnil(lua_State *L) +{ + setnilV(L->top); + incr_top(L); +} + +LUA_API void lua_pushnumber(lua_State *L, lua_Number n) +{ + setnumV(L->top, n); + if (LJ_UNLIKELY(tvisnan(L->top))) + setnanV(L->top); /* Canonicalize injected NaNs. */ + incr_top(L); +} + +LUA_API void lua_pushinteger(lua_State *L, lua_Integer n) +{ + setintptrV(L->top, n); + incr_top(L); +} + +LUA_API void lua_pushlstring(lua_State *L, const char *str, size_t len) +{ + GCstr *s; + lj_gc_check(L); + s = lj_str_new(L, str, len); + setstrV(L, L->top, s); + incr_top(L); +} + +LUA_API void lua_pushstring(lua_State *L, const char *str) +{ + if (str == NULL) { + setnilV(L->top); + } else { + GCstr *s; + lj_gc_check(L); + s = lj_str_newz(L, str); + setstrV(L, L->top, s); + } + incr_top(L); +} + +LUA_API const char *lua_pushvfstring(lua_State *L, const char *fmt, + va_list argp) +{ + lj_gc_check(L); + return lj_str_pushvf(L, fmt, argp); +} + +LUA_API const char *lua_pushfstring(lua_State *L, const char *fmt, ...) +{ + const char *ret; + va_list argp; + lj_gc_check(L); + va_start(argp, fmt); + ret = lj_str_pushvf(L, fmt, argp); + va_end(argp); + return ret; +} + +LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction f, int n) +{ + GCfunc *fn; + lj_gc_check(L); + api_checknelems(L, n); + fn = lj_func_newC(L, (MSize)n, getcurrenv(L)); + fn->c.f = f; + L->top -= n; + while (n--) + copyTV(L, &fn->c.upvalue[n], L->top+n); + setfuncV(L, L->top, fn); + lua_assert(iswhite(obj2gco(fn))); + incr_top(L); +} + +LUA_API void lua_pushboolean(lua_State *L, int b) +{ + setboolV(L->top, (b != 0)); + incr_top(L); +} + +LUA_API void lua_pushlightuserdata(lua_State *L, void *p) +{ + setlightudV(L->top, checklightudptr(L, p)); + incr_top(L); +} + +LUA_API void lua_createtable(lua_State *L, int narray, int nrec) +{ + GCtab *t; + lj_gc_check(L); + t = lj_tab_new(L, (uint32_t)(narray > 0 ? narray+1 : 0), hsize2hbits(nrec)); + settabV(L, L->top, t); + incr_top(L); +} + +LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname) +{ + GCtab *regt = tabV(registry(L)); + TValue *tv = lj_tab_setstr(L, regt, lj_str_newz(L, tname)); + if (tvisnil(tv)) { + GCtab *mt = lj_tab_new(L, 0, 1); + settabV(L, tv, mt); + settabV(L, L->top++, mt); + lj_gc_anybarriert(L, regt); + return 1; + } else { + copyTV(L, L->top++, tv); + return 0; + } +} + +LUA_API int lua_pushthread(lua_State *L) +{ + setthreadV(L, L->top, L); + incr_top(L); + return (mainthread(G(L)) == L); +} + +LUA_API lua_State *lua_newthread(lua_State *L) +{ + lua_State *L1; + lj_gc_check(L); + L1 = lj_state_new(L); + setthreadV(L, L->top, L1); + incr_top(L); + return L1; +} + +LUA_API void *lua_newuserdata(lua_State *L, size_t size) +{ + GCudata *ud; + lj_gc_check(L); + if (size > LJ_MAX_UDATA) + lj_err_msg(L, LJ_ERR_UDATAOV); + ud = lj_udata_new(L, (MSize)size, getcurrenv(L)); + setudataV(L, L->top, ud); + incr_top(L); + return uddata(ud); +} + +LUA_API void lua_concat(lua_State *L, int n) +{ + api_checknelems(L, n); + if (n >= 2) { + n--; + do { + TValue *top = lj_meta_cat(L, L->top-1, -n); + if (top == NULL) { + L->top -= n; + break; + } + n -= (int)(L->top - top); + L->top = top+2; + lj_vm_call(L, top, 1+1); + L->top--; + copyTV(L, L->top-1, L->top); + } while (--n > 0); + } else if (n == 0) { /* Push empty string. */ + setstrV(L, L->top, &G(L)->strempty); + incr_top(L); + } + /* else n == 1: nothing to do. */ +} + +/* -- Object getters ------------------------------------------------------ */ + +LUA_API void lua_gettable(lua_State *L, int idx) +{ + cTValue *v, *t = index2adr(L, idx); + api_checkvalidindex(L, t); + v = lj_meta_tget(L, t, L->top-1); + if (v == NULL) { + L->top += 2; + lj_vm_call(L, L->top-2, 1+1); + L->top -= 2; + v = L->top+1; + } + copyTV(L, L->top-1, v); +} + +LUA_API void lua_getfield(lua_State *L, int idx, const char *k) +{ + cTValue *v, *t = index2adr(L, idx); + TValue key; + api_checkvalidindex(L, t); + setstrV(L, &key, lj_str_newz(L, k)); + v = lj_meta_tget(L, t, &key); + if (v == NULL) { + L->top += 2; + lj_vm_call(L, L->top-2, 1+1); + L->top -= 2; + v = L->top+1; + } + copyTV(L, L->top, v); + incr_top(L); +} + +LUA_API void lua_rawget(lua_State *L, int idx) +{ + cTValue *t = index2adr(L, idx); + api_check(L, tvistab(t)); + copyTV(L, L->top-1, lj_tab_get(L, tabV(t), L->top-1)); +} + +LUA_API void lua_rawgeti(lua_State *L, int idx, int n) +{ + cTValue *v, *t = index2adr(L, idx); + api_check(L, tvistab(t)); + v = lj_tab_getint(tabV(t), n); + if (v) { + copyTV(L, L->top, v); + } else { + setnilV(L->top); + } + incr_top(L); +} + +LUA_API int lua_getmetatable(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + GCtab *mt = NULL; + if (tvistab(o)) + mt = tabref(tabV(o)->metatable); + else if (tvisudata(o)) + mt = tabref(udataV(o)->metatable); + else + mt = tabref(basemt_obj(G(L), o)); + if (mt == NULL) + return 0; + settabV(L, L->top, mt); + incr_top(L); + return 1; +} + +LUALIB_API int luaL_getmetafield(lua_State *L, int idx, const char *field) +{ + if (lua_getmetatable(L, idx)) { + cTValue *tv = lj_tab_getstr(tabV(L->top-1), lj_str_newz(L, field)); + if (tv && !tvisnil(tv)) { + copyTV(L, L->top-1, tv); + return 1; + } + L->top--; + } + return 0; +} + +LUA_API void lua_getfenv(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + api_checkvalidindex(L, o); + if (tvisfunc(o)) { + settabV(L, L->top, tabref(funcV(o)->c.env)); + } else if (tvisudata(o)) { + settabV(L, L->top, tabref(udataV(o)->env)); + } else if (tvisthread(o)) { + settabV(L, L->top, tabref(threadV(o)->env)); + } else { + setnilV(L->top); + } + incr_top(L); +} + +LUA_API int lua_next(lua_State *L, int idx) +{ + cTValue *t = index2adr(L, idx); + int more; + api_check(L, tvistab(t)); + more = lj_tab_next(L, tabV(t), L->top-1); + if (more) { + incr_top(L); /* Return new key and value slot. */ + } else { /* End of traversal. */ + L->top--; /* Remove key slot. */ + } + return more; +} + +LUA_API const char *lua_getupvalue(lua_State *L, int idx, int n) +{ + TValue *val; + const char *name = lj_debug_uvnamev(index2adr(L, idx), (uint32_t)(n-1), &val); + if (name) { + copyTV(L, L->top, val); + incr_top(L); + } + return name; +} + +LUA_API void *lua_upvalueid(lua_State *L, int idx, int n) +{ + GCfunc *fn = funcV(index2adr(L, idx)); + n--; + api_check(L, (uint32_t)n < fn->l.nupvalues); + return isluafunc(fn) ? (void *)gcref(fn->l.uvptr[n]) : + (void *)&fn->c.upvalue[n]; +} + +LUA_API void lua_upvaluejoin(lua_State *L, int idx1, int n1, int idx2, int n2) +{ + GCfunc *fn1 = funcV(index2adr(L, idx1)); + GCfunc *fn2 = funcV(index2adr(L, idx2)); + n1--; n2--; + api_check(L, isluafunc(fn1) && (uint32_t)n1 < fn1->l.nupvalues); + api_check(L, isluafunc(fn2) && (uint32_t)n2 < fn2->l.nupvalues); + setgcrefr(fn1->l.uvptr[n1], fn2->l.uvptr[n2]); + lj_gc_objbarrier(L, fn1, gcref(fn1->l.uvptr[n1])); +} + +LUALIB_API void *luaL_checkudata(lua_State *L, int idx, const char *tname) +{ + cTValue *o = index2adr(L, idx); + if (tvisudata(o)) { + GCudata *ud = udataV(o); + cTValue *tv = lj_tab_getstr(tabV(registry(L)), lj_str_newz(L, tname)); + if (tv && tvistab(tv) && tabV(tv) == tabref(ud->metatable)) + return uddata(ud); + } + lj_err_argtype(L, idx, tname); + return NULL; /* unreachable */ +} + +/* -- Object setters ------------------------------------------------------ */ + +LUA_API void lua_settable(lua_State *L, int idx) +{ + TValue *o; + cTValue *t = index2adr(L, idx); + api_checknelems(L, 2); + api_checkvalidindex(L, t); + o = lj_meta_tset(L, t, L->top-2); + if (o) { + /* NOBARRIER: lj_meta_tset ensures the table is not black. */ + copyTV(L, o, L->top-1); + L->top -= 2; + } else { + L->top += 3; + copyTV(L, L->top-1, L->top-6); + lj_vm_call(L, L->top-3, 0+1); + L->top -= 3; + } +} + +LUA_API void lua_setfield(lua_State *L, int idx, const char *k) +{ + TValue *o; + TValue key; + cTValue *t = index2adr(L, idx); + api_checknelems(L, 1); + api_checkvalidindex(L, t); + setstrV(L, &key, lj_str_newz(L, k)); + o = lj_meta_tset(L, t, &key); + if (o) { + L->top--; + /* NOBARRIER: lj_meta_tset ensures the table is not black. */ + copyTV(L, o, L->top); + } else { + L->top += 3; + copyTV(L, L->top-1, L->top-6); + lj_vm_call(L, L->top-3, 0+1); + L->top -= 2; + } +} + +LUA_API void lua_rawset(lua_State *L, int idx) +{ + GCtab *t = tabV(index2adr(L, idx)); + TValue *dst, *key; + api_checknelems(L, 2); + key = L->top-2; + dst = lj_tab_set(L, t, key); + copyTV(L, dst, key+1); + lj_gc_anybarriert(L, t); + L->top = key; +} + +LUA_API void lua_rawseti(lua_State *L, int idx, int n) +{ + GCtab *t = tabV(index2adr(L, idx)); + TValue *dst, *src; + api_checknelems(L, 1); + dst = lj_tab_setint(L, t, n); + src = L->top-1; + copyTV(L, dst, src); + lj_gc_barriert(L, t, dst); + L->top = src; +} + +LUA_API int lua_setmetatable(lua_State *L, int idx) +{ + global_State *g; + GCtab *mt; + cTValue *o = index2adr(L, idx); + api_checknelems(L, 1); + api_checkvalidindex(L, o); + if (tvisnil(L->top-1)) { + mt = NULL; + } else { + api_check(L, tvistab(L->top-1)); + mt = tabV(L->top-1); + } + g = G(L); + if (tvistab(o)) { + setgcref(tabV(o)->metatable, obj2gco(mt)); + if (mt) + lj_gc_objbarriert(L, tabV(o), mt); + } else if (tvisudata(o)) { + setgcref(udataV(o)->metatable, obj2gco(mt)); + if (mt) + lj_gc_objbarrier(L, udataV(o), mt); + } else { + /* Flush cache, since traces specialize to basemt. But not during __gc. */ + if (lj_trace_flushall(L)) + lj_err_caller(L, LJ_ERR_NOGCMM); + if (tvisbool(o)) { + /* NOBARRIER: basemt is a GC root. */ + setgcref(basemt_it(g, LJ_TTRUE), obj2gco(mt)); + setgcref(basemt_it(g, LJ_TFALSE), obj2gco(mt)); + } else { + /* NOBARRIER: basemt is a GC root. */ + setgcref(basemt_obj(g, o), obj2gco(mt)); + } + } + L->top--; + return 1; +} + +LUA_API int lua_setfenv(lua_State *L, int idx) +{ + cTValue *o = index2adr(L, idx); + GCtab *t; + api_checknelems(L, 1); + api_checkvalidindex(L, o); + api_check(L, tvistab(L->top-1)); + t = tabV(L->top-1); + if (tvisfunc(o)) { + setgcref(funcV(o)->c.env, obj2gco(t)); + } else if (tvisudata(o)) { + setgcref(udataV(o)->env, obj2gco(t)); + } else if (tvisthread(o)) { + setgcref(threadV(o)->env, obj2gco(t)); + } else { + L->top--; + return 0; + } + lj_gc_objbarrier(L, gcV(o), t); + L->top--; + return 1; +} + +LUA_API const char *lua_setupvalue(lua_State *L, int idx, int n) +{ + cTValue *f = index2adr(L, idx); + TValue *val; + const char *name; + api_checknelems(L, 1); + name = lj_debug_uvnamev(f, (uint32_t)(n-1), &val); + if (name) { + L->top--; + copyTV(L, val, L->top); + lj_gc_barrier(L, funcV(f), L->top); + } + return name; +} + +/* -- Calls --------------------------------------------------------------- */ + +LUA_API void lua_call(lua_State *L, int nargs, int nresults) +{ + api_check(L, L->status == 0 || L->status == LUA_ERRERR); + api_checknelems(L, nargs+1); + lj_vm_call(L, L->top - nargs, nresults+1); +} + +LUA_API int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc) +{ + global_State *g = G(L); + uint8_t oldh = hook_save(g); + ptrdiff_t ef; + int status; + api_check(L, L->status == 0 || L->status == LUA_ERRERR); + api_checknelems(L, nargs+1); + if (errfunc == 0) { + ef = 0; + } else { + cTValue *o = stkindex2adr(L, errfunc); + api_checkvalidindex(L, o); + ef = savestack(L, o); + } + status = lj_vm_pcall(L, L->top - nargs, nresults+1, ef); + if (status) hook_restore(g, oldh); + return status; +} + +static TValue *cpcall(lua_State *L, lua_CFunction func, void *ud) +{ + GCfunc *fn = lj_func_newC(L, 0, getcurrenv(L)); + fn->c.f = func; + setfuncV(L, L->top, fn); + setlightudV(L->top+1, checklightudptr(L, ud)); + cframe_nres(L->cframe) = 1+0; /* Zero results. */ + L->top += 2; + return L->top-1; /* Now call the newly allocated C function. */ +} + +LUA_API int lua_cpcall(lua_State *L, lua_CFunction func, void *ud) +{ + global_State *g = G(L); + uint8_t oldh = hook_save(g); + int status; + api_check(L, L->status == 0 || L->status == LUA_ERRERR); + status = lj_vm_cpcall(L, func, ud, cpcall); + if (status) hook_restore(g, oldh); + return status; +} + +LUALIB_API int luaL_callmeta(lua_State *L, int idx, const char *field) +{ + if (luaL_getmetafield(L, idx, field)) { + TValue *base = L->top--; + copyTV(L, base, index2adr(L, idx)); + L->top = base+1; + lj_vm_call(L, base, 1+1); + return 1; + } + return 0; +} + +/* -- Coroutine yield and resume ------------------------------------------ */ + +LUA_API int lua_yield(lua_State *L, int nresults) +{ + void *cf = L->cframe; + global_State *g = G(L); + if (cframe_canyield(cf)) { + cf = cframe_raw(cf); + if (!hook_active(g)) { /* Regular yield: move results down if needed. */ + cTValue *f = L->top - nresults; + if (f > L->base) { + TValue *t = L->base; + while (--nresults >= 0) copyTV(L, t++, f++); + L->top = t; + } + L->cframe = NULL; + L->status = LUA_YIELD; + return -1; + } else { /* Yield from hook: add a pseudo-frame. */ + TValue *top = L->top; + hook_leave(g); + top->u64 = cframe_multres(cf); + setcont(top+1, lj_cont_hook); + setframe_pc(top+1, cframe_pc(cf)-1); + setframe_gc(top+2, obj2gco(L)); + setframe_ftsz(top+2, (int)((char *)(top+3)-(char *)L->base)+FRAME_CONT); + L->top = L->base = top+3; +#if LJ_TARGET_X64 + lj_err_throw(L, LUA_YIELD); +#else + L->cframe = NULL; + L->status = LUA_YIELD; + lj_vm_unwind_c(cf, LUA_YIELD); +#endif + } + } + lj_err_msg(L, LJ_ERR_CYIELD); + return 0; /* unreachable */ +} + +LUA_API int lua_resume(lua_State *L, int nargs) +{ + if (L->cframe == NULL && L->status <= LUA_YIELD) + return lj_vm_resume(L, L->top - nargs, 0, 0); + L->top = L->base; + setstrV(L, L->top, lj_err_str(L, LJ_ERR_COSUSP)); + incr_top(L); + return LUA_ERRRUN; +} + +/* -- GC and memory management -------------------------------------------- */ + +LUA_API int lua_gc(lua_State *L, int what, int data) +{ + global_State *g = G(L); + int res = 0; + switch (what) { + case LUA_GCSTOP: + g->gc.threshold = LJ_MAX_MEM; + break; + case LUA_GCRESTART: + g->gc.threshold = data == -1 ? (g->gc.total/100)*g->gc.pause : g->gc.total; + break; + case LUA_GCCOLLECT: + lj_gc_fullgc(L); + break; + case LUA_GCCOUNT: + res = (int)(g->gc.total >> 10); + break; + case LUA_GCCOUNTB: + res = (int)(g->gc.total & 0x3ff); + break; + case LUA_GCSTEP: { + MSize a = (MSize)data << 10; + g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0; + while (g->gc.total >= g->gc.threshold) + if (lj_gc_step(L)) { + res = 1; + break; + } + break; + } + case LUA_GCSETPAUSE: + res = (int)(g->gc.pause); + g->gc.pause = (MSize)data; + break; + case LUA_GCSETSTEPMUL: + res = (int)(g->gc.stepmul); + g->gc.stepmul = (MSize)data; + break; + default: + res = -1; /* Invalid option. */ + } + return res; +} + +LUA_API lua_Alloc lua_getallocf(lua_State *L, void **ud) +{ + global_State *g = G(L); + if (ud) *ud = g->allocd; + return g->allocf; +} + +LUA_API void lua_setallocf(lua_State *L, lua_Alloc f, void *ud) +{ + global_State *g = G(L); + g->allocd = ud; + g->allocf = f; +} + diff --git a/external/lua/luajit/src/src/lj_arch.h b/external/lua/luajit/src/src/lj_arch.h new file mode 100644 index 0000000000..9ea10d0f5e --- /dev/null +++ b/external/lua/luajit/src/src/lj_arch.h @@ -0,0 +1,419 @@ +/* +** Target architecture selection. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_ARCH_H +#define _LJ_ARCH_H + +#include "lua.h" + +/* Target endianess. */ +#define LUAJIT_LE 0 +#define LUAJIT_BE 1 + +/* Target architectures. */ +#define LUAJIT_ARCH_X86 1 +#define LUAJIT_ARCH_x86 1 +#define LUAJIT_ARCH_X64 2 +#define LUAJIT_ARCH_x64 2 +#define LUAJIT_ARCH_ARM 3 +#define LUAJIT_ARCH_arm 3 +#define LUAJIT_ARCH_PPC 4 +#define LUAJIT_ARCH_ppc 4 +#define LUAJIT_ARCH_PPCSPE 5 +#define LUAJIT_ARCH_ppcspe 5 +#define LUAJIT_ARCH_MIPS 6 +#define LUAJIT_ARCH_mips 6 + +/* Target OS. */ +#define LUAJIT_OS_OTHER 0 +#define LUAJIT_OS_WINDOWS 1 +#define LUAJIT_OS_LINUX 2 +#define LUAJIT_OS_OSX 3 +#define LUAJIT_OS_BSD 4 +#define LUAJIT_OS_POSIX 5 + +/* Select native target if no target defined. */ +#ifndef LUAJIT_TARGET + +#if defined(__i386) || defined(__i386__) || defined(_M_IX86) +#define LUAJIT_TARGET LUAJIT_ARCH_X86 +#elif defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) +#define LUAJIT_TARGET LUAJIT_ARCH_X64 +#elif defined(__arm__) || defined(__arm) || defined(__ARM__) || defined(__ARM) +#define LUAJIT_TARGET LUAJIT_ARCH_ARM +#elif defined(__ppc__) || defined(__ppc) || defined(__PPC__) || defined(__PPC) || defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__POWERPC) || defined(_M_PPC) +#ifdef __NO_FPRS__ +#define LUAJIT_TARGET LUAJIT_ARCH_PPCSPE +#else +#define LUAJIT_TARGET LUAJIT_ARCH_PPC +#endif +#elif defined(__mips__) || defined(__mips) || defined(__MIPS__) || defined(__MIPS) +#define LUAJIT_TARGET LUAJIT_ARCH_MIPS +#else +#error "No support for this architecture (yet)" +#endif + +#endif + +/* Select native OS if no target OS defined. */ +#ifndef LUAJIT_OS + +#if defined(_WIN32) && !defined(_XBOX_VER) +#define LUAJIT_OS LUAJIT_OS_WINDOWS +#elif defined(__linux__) +#define LUAJIT_OS LUAJIT_OS_LINUX +#elif defined(__MACH__) && defined(__APPLE__) +#define LUAJIT_OS LUAJIT_OS_OSX +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ + defined(__NetBSD__) || defined(__OpenBSD__) +#define LUAJIT_OS LUAJIT_OS_BSD +#elif (defined(__sun__) && defined(__svr4__)) || defined(__CYGWIN__) +#define LUAJIT_OS LUAJIT_OS_POSIX +#else +#define LUAJIT_OS LUAJIT_OS_OTHER +#endif + +#endif + +/* Set target OS properties. */ +#if LUAJIT_OS == LUAJIT_OS_WINDOWS +#define LJ_OS_NAME "Windows" +#elif LUAJIT_OS == LUAJIT_OS_LINUX +#define LJ_OS_NAME "Linux" +#elif LUAJIT_OS == LUAJIT_OS_OSX +#define LJ_OS_NAME "OSX" +#elif LUAJIT_OS == LUAJIT_OS_BSD +#define LJ_OS_NAME "BSD" +#elif LUAJIT_OS == LUAJIT_OS_POSIX +#define LJ_OS_NAME "POSIX" +#else +#define LJ_OS_NAME "Other" +#endif + +#define LJ_TARGET_WINDOWS (LUAJIT_OS == LUAJIT_OS_WINDOWS) +#define LJ_TARGET_LINUX (LUAJIT_OS == LUAJIT_OS_LINUX) +#define LJ_TARGET_OSX (LUAJIT_OS == LUAJIT_OS_OSX) +#define LJ_TARGET_IOS (LJ_TARGET_OSX && LUAJIT_TARGET == LUAJIT_ARCH_ARM) +#define LJ_TARGET_POSIX (LUAJIT_OS > LUAJIT_OS_WINDOWS) +#define LJ_TARGET_DLOPEN LJ_TARGET_POSIX + +#ifdef __CELLOS_LV2__ +#define LJ_TARGET_PS3 1 +#define LJ_TARGET_CONSOLE 1 +#endif + +#if _XBOX_VER >= 200 +#define LJ_TARGET_XBOX360 1 +#define LJ_TARGET_CONSOLE 1 +#endif + +#define LJ_NUMMODE_SINGLE 0 /* Single-number mode only. */ +#define LJ_NUMMODE_SINGLE_DUAL 1 /* Default to single-number mode. */ +#define LJ_NUMMODE_DUAL 2 /* Dual-number mode only. */ +#define LJ_NUMMODE_DUAL_SINGLE 3 /* Default to dual-number mode. */ + +/* Set target architecture properties. */ +#if LUAJIT_TARGET == LUAJIT_ARCH_X86 + +#define LJ_ARCH_NAME "x86" +#define LJ_ARCH_BITS 32 +#define LJ_ARCH_ENDIAN LUAJIT_LE +#if LJ_TARGET_WINDOWS || __CYGWIN__ +#define LJ_ABI_WIN 1 +#else +#define LJ_ABI_WIN 0 +#endif +#define LJ_TARGET_X86 1 +#define LJ_TARGET_X86ORX64 1 +#define LJ_TARGET_EHRETREG 0 +#define LJ_TARGET_MASKSHIFT 1 +#define LJ_TARGET_MASKROT 1 +#define LJ_TARGET_UNALIGNED 1 +#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL + +#elif LUAJIT_TARGET == LUAJIT_ARCH_X64 + +#define LJ_ARCH_NAME "x64" +#define LJ_ARCH_BITS 64 +#define LJ_ARCH_ENDIAN LUAJIT_LE +#define LJ_ABI_WIN LJ_TARGET_WINDOWS +#define LJ_TARGET_X64 1 +#define LJ_TARGET_X86ORX64 1 +#define LJ_TARGET_EHRETREG 0 +#define LJ_TARGET_JUMPRANGE 31 /* +-2^31 = +-2GB */ +#define LJ_TARGET_MASKSHIFT 1 +#define LJ_TARGET_MASKROT 1 +#define LJ_TARGET_UNALIGNED 1 +#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE_DUAL + +#elif LUAJIT_TARGET == LUAJIT_ARCH_ARM + +#define LJ_ARCH_NAME "arm" +#define LJ_ARCH_BITS 32 +#define LJ_ARCH_ENDIAN LUAJIT_LE +#if !defined(LJ_ARCH_HASFPU) && __SOFTFP__ +#define LJ_ARCH_HASFPU 0 +#endif +#if !defined(LJ_ABI_SOFTFP) && !__ARM_PCS_VFP +#define LJ_ABI_SOFTFP 1 +#endif +#define LJ_ABI_EABI 1 +#define LJ_TARGET_ARM 1 +#define LJ_TARGET_EHRETREG 0 +#define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ +#define LJ_TARGET_MASKSHIFT 0 +#define LJ_TARGET_MASKROT 1 +#define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */ +#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL + +#if __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ || __ARM_ARCH_7S__ +#define LJ_ARCH_VERSION 70 +#elif __ARM_ARCH_6T2__ +#define LJ_ARCH_VERSION 61 +#elif __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6K__ || __ARM_ARCH_6Z__ || __ARM_ARCH_6ZK__ +#define LJ_ARCH_VERSION 60 +#else +#define LJ_ARCH_VERSION 50 +#endif + +#elif LUAJIT_TARGET == LUAJIT_ARCH_PPC + +#define LJ_ARCH_NAME "ppc" +#if _LP64 +#define LJ_ARCH_BITS 64 +#else +#define LJ_ARCH_BITS 32 +#endif +#define LJ_ARCH_ENDIAN LUAJIT_BE +#define LJ_TARGET_PPC 1 +#define LJ_TARGET_EHRETREG 3 +#define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ +#define LJ_TARGET_MASKSHIFT 0 +#define LJ_TARGET_MASKROT 1 +#define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */ +#define LJ_ARCH_NUMMODE LJ_NUMMODE_DUAL_SINGLE + +#if _ARCH_PWR7 +#define LJ_ARCH_VERSION 70 +#elif _ARCH_PWR6 +#define LJ_ARCH_VERSION 60 +#elif _ARCH_PWR5X +#define LJ_ARCH_VERSION 51 +#elif _ARCH_PWR5 +#define LJ_ARCH_VERSION 50 +#elif _ARCH_PWR4 +#define LJ_ARCH_VERSION 40 +#else +#define LJ_ARCH_VERSION 0 +#endif +#if __PPC64__ || __powerpc64__ || LJ_TARGET_CONSOLE +#define LJ_ARCH_PPC64 1 +#define LJ_ARCH_NOFFI 1 +#endif +#if _ARCH_PPCSQ +#define LJ_ARCH_SQRT 1 +#endif +#if _ARCH_PWR5X +#define LJ_ARCH_ROUND 1 +#endif +#if __PPU__ +#define LJ_ARCH_CELL 1 +#endif +#if LJ_TARGET_XBOX360 +#define LJ_ARCH_XENON 1 +#endif + +#elif LUAJIT_TARGET == LUAJIT_ARCH_PPCSPE + +#define LJ_ARCH_NAME "ppcspe" +#define LJ_ARCH_BITS 32 +#define LJ_ARCH_ENDIAN LUAJIT_BE +#ifndef LJ_ABI_SOFTFP +#define LJ_ABI_SOFTFP 1 +#endif +#define LJ_ABI_EABI 1 +#define LJ_TARGET_PPCSPE 1 +#define LJ_TARGET_EHRETREG 3 +#define LJ_TARGET_JUMPRANGE 25 /* +-2^25 = +-32MB */ +#define LJ_TARGET_MASKSHIFT 0 +#define LJ_TARGET_MASKROT 1 +#define LJ_TARGET_UNIFYROT 1 /* Want only IR_BROL. */ +#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE +#define LJ_ARCH_NOFFI 1 /* NYI: comparisons, calls. */ +#define LJ_ARCH_NOJIT 1 + +#elif LUAJIT_TARGET == LUAJIT_ARCH_MIPS + +#if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) +#define LJ_ARCH_NAME "mipsel" +#define LJ_ARCH_ENDIAN LUAJIT_LE +#else +#define LJ_ARCH_NAME "mips" +#define LJ_ARCH_ENDIAN LUAJIT_BE +#endif +#define LJ_ARCH_BITS 32 +#define LJ_TARGET_MIPS 1 +#define LJ_TARGET_EHRETREG 4 +#define LJ_TARGET_JUMPRANGE 27 /* 2*2^27 = 256MB-aligned region */ +#define LJ_TARGET_MASKSHIFT 1 +#define LJ_TARGET_MASKROT 1 +#define LJ_TARGET_UNIFYROT 2 /* Want only IR_BROR. */ +#define LJ_ARCH_NUMMODE LJ_NUMMODE_SINGLE + +#if _MIPS_ARCH_MIPS32R2 +#define LJ_ARCH_VERSION 20 +#else +#define LJ_ARCH_VERSION 10 +#endif + +#else +#error "No target architecture defined" +#endif + +#ifndef LJ_PAGESIZE +#define LJ_PAGESIZE 4096 +#endif + +/* Check for minimum required compiler versions. */ +#if defined(__GNUC__) +#if LJ_TARGET_X86 +#if (__GNUC__ < 3) || ((__GNUC__ == 3) && __GNUC_MINOR__ < 4) +#error "Need at least GCC 3.4 or newer" +#endif +#elif LJ_TARGET_X64 +#if __GNUC__ < 4 +#error "Need at least GCC 4.0 or newer" +#endif +#elif LJ_TARGET_ARM +#if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 2) +#error "Need at least GCC 4.2 or newer" +#endif +#elif !LJ_TARGET_PS3 +#if (__GNUC__ < 4) || ((__GNUC__ == 4) && __GNUC_MINOR__ < 3) +#error "Need at least GCC 4.3 or newer" +#endif +#endif +#endif + +/* Check target-specific constraints. */ +#ifndef _BUILDVM_H +#if LJ_TARGET_X64 +#if __USING_SJLJ_EXCEPTIONS__ +#error "Need a C compiler with native exception handling on x64" +#endif +#elif LJ_TARGET_ARM +#if defined(__ARMEB__) +#error "No support for big-endian ARM" +#endif +#if __ARM_ARCH_6M__ || __ARM_ARCH_7M__ || __ARM_ARCH_7EM__ +#error "No support for Cortex-M CPUs" +#endif +#if !(__ARM_EABI__ || LJ_TARGET_IOS) +#error "Only ARM EABI or iOS 3.0+ ABI is supported" +#endif +#elif LJ_TARGET_PPC || LJ_TARGET_PPCSPE +#if defined(_SOFT_FLOAT) || defined(_SOFT_DOUBLE) +#error "No support for PowerPC CPUs without double-precision FPU" +#endif +#if defined(_LITTLE_ENDIAN) +#error "No support for little-endian PowerPC" +#endif +#if defined(_LP64) +#error "No support for PowerPC 64 bit mode" +#endif +#elif LJ_TARGET_MIPS +#if defined(__mips_soft_float) +#error "No support for MIPS CPUs without FPU" +#endif +#endif +#endif + +/* Enable or disable the dual-number mode for the VM. */ +#if (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE && LUAJIT_NUMMODE == 2) || \ + (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL && LUAJIT_NUMMODE == 1) +#error "No support for this number mode on this architecture" +#endif +#if LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL || \ + (LJ_ARCH_NUMMODE == LJ_NUMMODE_DUAL_SINGLE && LUAJIT_NUMMODE != 1) || \ + (LJ_ARCH_NUMMODE == LJ_NUMMODE_SINGLE_DUAL && LUAJIT_NUMMODE == 2) +#define LJ_DUALNUM 1 +#else +#define LJ_DUALNUM 0 +#endif + +#if LJ_TARGET_IOS || LJ_TARGET_CONSOLE +/* Runtime code generation is restricted on iOS. Complain to Apple, not me. */ +/* Ditto for the consoles. Complain to Sony or MS, not me. */ +#ifndef LUAJIT_ENABLE_JIT +#define LJ_OS_NOJIT 1 +#endif +#endif + +/* Disable or enable the JIT compiler. */ +#if defined(LUAJIT_DISABLE_JIT) || defined(LJ_ARCH_NOJIT) || defined(LJ_OS_NOJIT) +#define LJ_HASJIT 0 +#else +#define LJ_HASJIT 1 +#endif + +/* Disable or enable the FFI extension. */ +#if defined(LUAJIT_DISABLE_FFI) || defined(LJ_ARCH_NOFFI) +#define LJ_HASFFI 0 +#else +#define LJ_HASFFI 1 +#endif + +#ifndef LJ_ARCH_HASFPU +#define LJ_ARCH_HASFPU 1 +#endif +#ifndef LJ_ABI_SOFTFP +#define LJ_ABI_SOFTFP 0 +#endif +#define LJ_SOFTFP (!LJ_ARCH_HASFPU) + +#if LJ_ARCH_ENDIAN == LUAJIT_BE +#define LJ_LE 0 +#define LJ_BE 1 +#define LJ_ENDIAN_SELECT(le, be) be +#define LJ_ENDIAN_LOHI(lo, hi) hi lo +#else +#define LJ_LE 1 +#define LJ_BE 0 +#define LJ_ENDIAN_SELECT(le, be) le +#define LJ_ENDIAN_LOHI(lo, hi) lo hi +#endif + +#if LJ_ARCH_BITS == 32 +#define LJ_32 1 +#define LJ_64 0 +#else +#define LJ_32 0 +#define LJ_64 1 +#endif + +#ifndef LJ_TARGET_UNALIGNED +#define LJ_TARGET_UNALIGNED 0 +#endif + +/* Various workarounds for embedded operating systems. */ +#if (defined(__ANDROID__) && !defined(LJ_TARGET_X86ORX64)) || defined(__symbian__) || LJ_TARGET_XBOX360 +#define LUAJIT_NO_LOG2 +#endif +#if defined(__symbian__) +#define LUAJIT_NO_EXP2 +#endif + +#if defined(LUAJIT_NO_UNWIND) || defined(__symbian__) || LJ_TARGET_IOS || LJ_TARGET_PS3 +#define LJ_NO_UNWIND 1 +#endif + +/* Compatibility with Lua 5.1 vs. 5.2. */ +#ifdef LUAJIT_ENABLE_LUA52COMPAT +#define LJ_52 1 +#else +#define LJ_52 0 +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_asm.c b/external/lua/luajit/src/src/lj_asm.c new file mode 100644 index 0000000000..c73654041e --- /dev/null +++ b/external/lua/luajit/src/src/lj_asm.c @@ -0,0 +1,1886 @@ +/* +** IR assembler (SSA IR -> machine code). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_asm_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_gc.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_frame.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#endif +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_ircall.h" +#include "lj_iropt.h" +#include "lj_mcode.h" +#include "lj_iropt.h" +#include "lj_trace.h" +#include "lj_snap.h" +#include "lj_asm.h" +#include "lj_dispatch.h" +#include "lj_vm.h" +#include "lj_target.h" + +/* -- Assembler state and common macros ----------------------------------- */ + +/* Assembler state. */ +typedef struct ASMState { + RegCost cost[RID_MAX]; /* Reference and blended allocation cost for regs. */ + + MCode *mcp; /* Current MCode pointer (grows down). */ + MCode *mclim; /* Lower limit for MCode memory + red zone. */ + + IRIns *ir; /* Copy of pointer to IR instructions/constants. */ + jit_State *J; /* JIT compiler state. */ + +#if LJ_TARGET_X86ORX64 + x86ModRM mrm; /* Fused x86 address operand. */ +#endif + + RegSet freeset; /* Set of free registers. */ + RegSet modset; /* Set of registers modified inside the loop. */ + RegSet weakset; /* Set of weakly referenced registers. */ + RegSet phiset; /* Set of PHI registers. */ + + uint32_t flags; /* Copy of JIT compiler flags. */ + int loopinv; /* Loop branch inversion (0:no, 1:yes, 2:yes+CC_P). */ + + int32_t evenspill; /* Next even spill slot. */ + int32_t oddspill; /* Next odd spill slot (or 0). */ + + IRRef curins; /* Reference of current instruction. */ + IRRef stopins; /* Stop assembly before hitting this instruction. */ + IRRef orignins; /* Original T->nins. */ + + IRRef snapref; /* Current snapshot is active after this reference. */ + IRRef snaprename; /* Rename highwater mark for snapshot check. */ + SnapNo snapno; /* Current snapshot number. */ + SnapNo loopsnapno; /* Loop snapshot number. */ + + IRRef fuseref; /* Fusion limit (loopref, 0 or FUSE_DISABLED). */ + IRRef sectref; /* Section base reference (loopref or 0). */ + IRRef loopref; /* Reference of LOOP instruction (or 0). */ + + BCReg topslot; /* Number of slots for stack check (unless 0). */ + int32_t gcsteps; /* Accumulated number of GC steps (per section). */ + + GCtrace *T; /* Trace to assemble. */ + GCtrace *parent; /* Parent trace (or NULL). */ + + MCode *mcbot; /* Bottom of reserved MCode. */ + MCode *mctop; /* Top of generated MCode. */ + MCode *mcloop; /* Pointer to loop MCode (or NULL). */ + MCode *invmcp; /* Points to invertible loop branch (or NULL). */ + MCode *flagmcp; /* Pending opportunity to merge flag setting ins. */ + MCode *realign; /* Realign loop if not NULL. */ + +#ifdef RID_NUM_KREF + int32_t krefk[RID_NUM_KREF]; +#endif + IRRef1 phireg[RID_MAX]; /* PHI register references. */ + uint16_t parentmap[LJ_MAX_JSLOTS]; /* Parent instruction to RegSP map. */ +} ASMState; + +#define IR(ref) (&as->ir[(ref)]) + +#define ASMREF_TMP1 REF_TRUE /* Temp. register. */ +#define ASMREF_TMP2 REF_FALSE /* Temp. register. */ +#define ASMREF_L REF_NIL /* Stores register for L. */ + +/* Check for variant to invariant references. */ +#define iscrossref(as, ref) ((ref) < as->sectref) + +/* Inhibit memory op fusion from variant to invariant references. */ +#define FUSE_DISABLED (~(IRRef)0) +#define mayfuse(as, ref) ((ref) > as->fuseref) +#define neverfuse(as) (as->fuseref == FUSE_DISABLED) +#define canfuse(as, ir) (!neverfuse(as) && !irt_isphi((ir)->t)) +#define opisfusableload(o) \ + ((o) == IR_ALOAD || (o) == IR_HLOAD || (o) == IR_ULOAD || \ + (o) == IR_FLOAD || (o) == IR_XLOAD || (o) == IR_SLOAD || (o) == IR_VLOAD) + +/* Sparse limit checks using a red zone before the actual limit. */ +#define MCLIM_REDZONE 64 +#define checkmclim(as) \ + if (LJ_UNLIKELY(as->mcp < as->mclim)) asm_mclimit(as) + +static LJ_NORET LJ_NOINLINE void asm_mclimit(ASMState *as) +{ + lj_mcode_limiterr(as->J, (size_t)(as->mctop - as->mcp + 4*MCLIM_REDZONE)); +} + +#ifdef RID_NUM_KREF +#define ra_iskref(ref) ((ref) < RID_NUM_KREF) +#define ra_krefreg(ref) ((Reg)(RID_MIN_KREF + (Reg)(ref))) +#define ra_krefk(as, ref) (as->krefk[(ref)]) + +static LJ_AINLINE void ra_setkref(ASMState *as, Reg r, int32_t k) +{ + IRRef ref = (IRRef)(r - RID_MIN_KREF); + as->krefk[ref] = k; + as->cost[r] = REGCOST(ref, ref); +} + +#else +#define ra_iskref(ref) 0 +#define ra_krefreg(ref) RID_MIN_GPR +#define ra_krefk(as, ref) 0 +#endif + +/* Arch-specific field offsets. */ +static const uint8_t field_ofs[IRFL__MAX+1] = { +#define FLOFS(name, ofs) (uint8_t)(ofs), +IRFLDEF(FLOFS) +#undef FLOFS + 0 +}; + +/* -- Target-specific instruction emitter --------------------------------- */ + +#if LJ_TARGET_X86ORX64 +#include "lj_emit_x86.h" +#elif LJ_TARGET_ARM +#include "lj_emit_arm.h" +#elif LJ_TARGET_PPC +#include "lj_emit_ppc.h" +#elif LJ_TARGET_MIPS +#include "lj_emit_mips.h" +#else +#error "Missing instruction emitter for target CPU" +#endif + +/* -- Register allocator debugging ---------------------------------------- */ + +/* #define LUAJIT_DEBUG_RA */ + +#ifdef LUAJIT_DEBUG_RA + +#include +#include + +#define RIDNAME(name) #name, +static const char *const ra_regname[] = { + GPRDEF(RIDNAME) + FPRDEF(RIDNAME) + VRIDDEF(RIDNAME) + NULL +}; +#undef RIDNAME + +static char ra_dbg_buf[65536]; +static char *ra_dbg_p; +static char *ra_dbg_merge; +static MCode *ra_dbg_mcp; + +static void ra_dstart(void) +{ + ra_dbg_p = ra_dbg_buf; + ra_dbg_merge = NULL; + ra_dbg_mcp = NULL; +} + +static void ra_dflush(void) +{ + fwrite(ra_dbg_buf, 1, (size_t)(ra_dbg_p-ra_dbg_buf), stdout); + ra_dstart(); +} + +static void ra_dprintf(ASMState *as, const char *fmt, ...) +{ + char *p; + va_list argp; + va_start(argp, fmt); + p = ra_dbg_mcp == as->mcp ? ra_dbg_merge : ra_dbg_p; + ra_dbg_mcp = NULL; + p += sprintf(p, "%08x \e[36m%04d ", (uintptr_t)as->mcp, as->curins-REF_BIAS); + for (;;) { + const char *e = strchr(fmt, '$'); + if (e == NULL) break; + memcpy(p, fmt, (size_t)(e-fmt)); + p += e-fmt; + if (e[1] == 'r') { + Reg r = va_arg(argp, Reg) & RID_MASK; + if (r <= RID_MAX) { + const char *q; + for (q = ra_regname[r]; *q; q++) + *p++ = *q >= 'A' && *q <= 'Z' ? *q + 0x20 : *q; + } else { + *p++ = '?'; + lua_assert(0); + } + } else if (e[1] == 'f' || e[1] == 'i') { + IRRef ref; + if (e[1] == 'f') + ref = va_arg(argp, IRRef); + else + ref = va_arg(argp, IRIns *) - as->ir; + if (ref >= REF_BIAS) + p += sprintf(p, "%04d", ref - REF_BIAS); + else + p += sprintf(p, "K%03d", REF_BIAS - ref); + } else if (e[1] == 's') { + uint32_t slot = va_arg(argp, uint32_t); + p += sprintf(p, "[sp+0x%x]", sps_scale(slot)); + } else if (e[1] == 'x') { + p += sprintf(p, "%08x", va_arg(argp, int32_t)); + } else { + lua_assert(0); + } + fmt = e+2; + } + va_end(argp); + while (*fmt) + *p++ = *fmt++; + *p++ = '\e'; *p++ = '['; *p++ = 'm'; *p++ = '\n'; + if (p > ra_dbg_buf+sizeof(ra_dbg_buf)-256) { + fwrite(ra_dbg_buf, 1, (size_t)(p-ra_dbg_buf), stdout); + p = ra_dbg_buf; + } + ra_dbg_p = p; +} + +#define RA_DBG_START() ra_dstart() +#define RA_DBG_FLUSH() ra_dflush() +#define RA_DBG_REF() \ + do { char *_p = ra_dbg_p; ra_dprintf(as, ""); \ + ra_dbg_merge = _p; ra_dbg_mcp = as->mcp; } while (0) +#define RA_DBGX(x) ra_dprintf x + +#else +#define RA_DBG_START() ((void)0) +#define RA_DBG_FLUSH() ((void)0) +#define RA_DBG_REF() ((void)0) +#define RA_DBGX(x) ((void)0) +#endif + +/* -- Register allocator -------------------------------------------------- */ + +#define ra_free(as, r) rset_set(as->freeset, (r)) +#define ra_modified(as, r) rset_set(as->modset, (r)) +#define ra_weak(as, r) rset_set(as->weakset, (r)) +#define ra_noweak(as, r) rset_clear(as->weakset, (r)) + +#define ra_used(ir) (ra_hasreg((ir)->r) || ra_hasspill((ir)->s)) + +/* Setup register allocator. */ +static void ra_setup(ASMState *as) +{ + Reg r; + /* Initially all regs (except the stack pointer) are free for use. */ + as->freeset = RSET_INIT; + as->modset = RSET_EMPTY; + as->weakset = RSET_EMPTY; + as->phiset = RSET_EMPTY; + memset(as->phireg, 0, sizeof(as->phireg)); + for (r = RID_MIN_GPR; r < RID_MAX; r++) + as->cost[r] = REGCOST(~0u, 0u); +} + +/* Rematerialize constants. */ +static Reg ra_rematk(ASMState *as, IRRef ref) +{ + IRIns *ir; + Reg r; + if (ra_iskref(ref)) { + r = ra_krefreg(ref); + lua_assert(!rset_test(as->freeset, r)); + ra_free(as, r); + ra_modified(as, r); + emit_loadi(as, r, ra_krefk(as, ref)); + return r; + } + ir = IR(ref); + r = ir->r; + lua_assert(ra_hasreg(r) && !ra_hasspill(ir->s)); + ra_free(as, r); + ra_modified(as, r); + ir->r = RID_INIT; /* Do not keep any hint. */ + RA_DBGX((as, "remat $i $r", ir, r)); +#if !LJ_SOFTFP + if (ir->o == IR_KNUM) { + emit_loadn(as, r, ir_knum(ir)); + } else +#endif + if (emit_canremat(REF_BASE) && ir->o == IR_BASE) { + ra_sethint(ir->r, RID_BASE); /* Restore BASE register hint. */ + emit_getgl(as, r, jit_base); + } else if (emit_canremat(ASMREF_L) && ir->o == IR_KPRI) { + lua_assert(irt_isnil(ir->t)); /* REF_NIL stores ASMREF_L register. */ + emit_getgl(as, r, jit_L); +#if LJ_64 + } else if (ir->o == IR_KINT64) { + emit_loadu64(as, r, ir_kint64(ir)->u64); +#endif + } else { + lua_assert(ir->o == IR_KINT || ir->o == IR_KGC || + ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL); + emit_loadi(as, r, ir->i); + } + return r; +} + +/* Force a spill. Allocate a new spill slot if needed. */ +static int32_t ra_spill(ASMState *as, IRIns *ir) +{ + int32_t slot = ir->s; + if (!ra_hasspill(slot)) { + if (irt_is64(ir->t)) { + slot = as->evenspill; + as->evenspill += 2; + } else if (as->oddspill) { + slot = as->oddspill; + as->oddspill = 0; + } else { + slot = as->evenspill; + as->oddspill = slot+1; + as->evenspill += 2; + } + if (as->evenspill > 256) + lj_trace_err(as->J, LJ_TRERR_SPILLOV); + ir->s = (uint8_t)slot; + } + return sps_scale(slot); +} + +/* Release the temporarily allocated register in ASMREF_TMP1/ASMREF_TMP2. */ +static Reg ra_releasetmp(ASMState *as, IRRef ref) +{ + IRIns *ir = IR(ref); + Reg r = ir->r; + lua_assert(ra_hasreg(r) && !ra_hasspill(ir->s)); + ra_free(as, r); + ra_modified(as, r); + ir->r = RID_INIT; + return r; +} + +/* Restore a register (marked as free). Rematerialize or force a spill. */ +static Reg ra_restore(ASMState *as, IRRef ref) +{ + if (emit_canremat(ref)) { + return ra_rematk(as, ref); + } else { + IRIns *ir = IR(ref); + int32_t ofs = ra_spill(as, ir); /* Force a spill slot. */ + Reg r = ir->r; + lua_assert(ra_hasreg(r)); + ra_sethint(ir->r, r); /* Keep hint. */ + ra_free(as, r); + if (!rset_test(as->weakset, r)) { /* Only restore non-weak references. */ + ra_modified(as, r); + RA_DBGX((as, "restore $i $r", ir, r)); + emit_spload(as, ir, r, ofs); + } + return r; + } +} + +/* Save a register to a spill slot. */ +static void ra_save(ASMState *as, IRIns *ir, Reg r) +{ + RA_DBGX((as, "save $i $r", ir, r)); + emit_spstore(as, ir, r, sps_scale(ir->s)); +} + +#define MINCOST(name) \ + if (rset_test(RSET_ALL, RID_##name) && \ + LJ_LIKELY(allow&RID2RSET(RID_##name)) && as->cost[RID_##name] < cost) \ + cost = as->cost[RID_##name]; + +/* Evict the register with the lowest cost, forcing a restore. */ +static Reg ra_evict(ASMState *as, RegSet allow) +{ + IRRef ref; + RegCost cost = ~(RegCost)0; + lua_assert(allow != RSET_EMPTY); + if (RID_NUM_FPR == 0 || allow < RID2RSET(RID_MAX_GPR)) { + GPRDEF(MINCOST) + } else { + FPRDEF(MINCOST) + } + ref = regcost_ref(cost); + lua_assert(ra_iskref(ref) || (ref >= as->T->nk && ref < as->T->nins)); + /* Preferably pick any weak ref instead of a non-weak, non-const ref. */ + if (!irref_isk(ref) && (as->weakset & allow)) { + IRIns *ir = IR(ref); + if (!rset_test(as->weakset, ir->r)) + ref = regcost_ref(as->cost[rset_pickbot((as->weakset & allow))]); + } + return ra_restore(as, ref); +} + +/* Pick any register (marked as free). Evict on-demand. */ +static Reg ra_pick(ASMState *as, RegSet allow) +{ + RegSet pick = as->freeset & allow; + if (!pick) + return ra_evict(as, allow); + else + return rset_picktop(pick); +} + +/* Get a scratch register (marked as free). */ +static Reg ra_scratch(ASMState *as, RegSet allow) +{ + Reg r = ra_pick(as, allow); + ra_modified(as, r); + RA_DBGX((as, "scratch $r", r)); + return r; +} + +/* Evict all registers from a set (if not free). */ +static void ra_evictset(ASMState *as, RegSet drop) +{ + RegSet work; + as->modset |= drop; +#if !LJ_SOFTFP + work = (drop & ~as->freeset) & RSET_FPR; + while (work) { + Reg r = rset_pickbot(work); + ra_restore(as, regcost_ref(as->cost[r])); + rset_clear(work, r); + checkmclim(as); + } +#endif + work = (drop & ~as->freeset); + while (work) { + Reg r = rset_pickbot(work); + ra_restore(as, regcost_ref(as->cost[r])); + rset_clear(work, r); + checkmclim(as); + } +} + +/* Evict (rematerialize) all registers allocated to constants. */ +static void ra_evictk(ASMState *as) +{ + RegSet work; +#if !LJ_SOFTFP + work = ~as->freeset & RSET_FPR; + while (work) { + Reg r = rset_pickbot(work); + IRRef ref = regcost_ref(as->cost[r]); + if (emit_canremat(ref) && irref_isk(ref)) { + ra_rematk(as, ref); + checkmclim(as); + } + rset_clear(work, r); + } +#endif + work = ~as->freeset & RSET_GPR; + while (work) { + Reg r = rset_pickbot(work); + IRRef ref = regcost_ref(as->cost[r]); + if (emit_canremat(ref) && irref_isk(ref)) { + ra_rematk(as, ref); + checkmclim(as); + } + rset_clear(work, r); + } +} + +#ifdef RID_NUM_KREF +/* Allocate a register for a constant. */ +static Reg ra_allock(ASMState *as, int32_t k, RegSet allow) +{ + /* First try to find a register which already holds the same constant. */ + RegSet pick, work = ~as->freeset & RSET_GPR; + Reg r; + while (work) { + IRRef ref; + r = rset_pickbot(work); + ref = regcost_ref(as->cost[r]); + if (ref < ASMREF_L && + k == (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i)) + return r; + rset_clear(work, r); + } + pick = as->freeset & allow; + if (pick) { + /* Constants should preferably get unmodified registers. */ + if ((pick & ~as->modset)) + pick &= ~as->modset; + r = rset_pickbot(pick); /* Reduce conflicts with inverse allocation. */ + } else { + r = ra_evict(as, allow); + } + RA_DBGX((as, "allock $x $r", k, r)); + ra_setkref(as, r, k); + rset_clear(as->freeset, r); + ra_noweak(as, r); + return r; +} + +/* Allocate a specific register for a constant. */ +static void ra_allockreg(ASMState *as, int32_t k, Reg r) +{ + Reg kr = ra_allock(as, k, RID2RSET(r)); + if (kr != r) { + IRIns irdummy; + irdummy.t.irt = IRT_INT; + ra_scratch(as, RID2RSET(r)); + emit_movrr(as, &irdummy, r, kr); + } +} +#else +#define ra_allockreg(as, k, r) emit_loadi(as, (r), (k)) +#endif + +/* Allocate a register for ref from the allowed set of registers. +** Note: this function assumes the ref does NOT have a register yet! +** Picks an optimal register, sets the cost and marks the register as non-free. +*/ +static Reg ra_allocref(ASMState *as, IRRef ref, RegSet allow) +{ + IRIns *ir = IR(ref); + RegSet pick = as->freeset & allow; + Reg r; + lua_assert(ra_noreg(ir->r)); + if (pick) { + /* First check register hint from propagation or PHI. */ + if (ra_hashint(ir->r)) { + r = ra_gethint(ir->r); + if (rset_test(pick, r)) /* Use hint register if possible. */ + goto found; + /* Rematerialization is cheaper than missing a hint. */ + if (rset_test(allow, r) && emit_canremat(regcost_ref(as->cost[r]))) { + ra_rematk(as, regcost_ref(as->cost[r])); + goto found; + } + RA_DBGX((as, "hintmiss $f $r", ref, r)); + } + /* Invariants should preferably get unmodified registers. */ + if (ref < as->loopref && !irt_isphi(ir->t)) { + if ((pick & ~as->modset)) + pick &= ~as->modset; + r = rset_pickbot(pick); /* Reduce conflicts with inverse allocation. */ + } else { + /* We've got plenty of regs, so get callee-save regs if possible. */ + if (RID_NUM_GPR > 8 && (pick & ~RSET_SCRATCH)) + pick &= ~RSET_SCRATCH; + r = rset_picktop(pick); + } + } else { + r = ra_evict(as, allow); + } +found: + RA_DBGX((as, "alloc $f $r", ref, r)); + ir->r = (uint8_t)r; + rset_clear(as->freeset, r); + ra_noweak(as, r); + as->cost[r] = REGCOST_REF_T(ref, irt_t(ir->t)); + return r; +} + +/* Allocate a register on-demand. */ +static Reg ra_alloc1(ASMState *as, IRRef ref, RegSet allow) +{ + Reg r = IR(ref)->r; + /* Note: allow is ignored if the register is already allocated. */ + if (ra_noreg(r)) r = ra_allocref(as, ref, allow); + ra_noweak(as, r); + return r; +} + +/* Rename register allocation and emit move. */ +static void ra_rename(ASMState *as, Reg down, Reg up) +{ + IRRef ren, ref = regcost_ref(as->cost[up] = as->cost[down]); + IRIns *ir = IR(ref); + ir->r = (uint8_t)up; + as->cost[down] = 0; + lua_assert((down < RID_MAX_GPR) == (up < RID_MAX_GPR)); + lua_assert(!rset_test(as->freeset, down) && rset_test(as->freeset, up)); + ra_free(as, down); /* 'down' is free ... */ + ra_modified(as, down); + rset_clear(as->freeset, up); /* ... and 'up' is now allocated. */ + ra_noweak(as, up); + RA_DBGX((as, "rename $f $r $r", regcost_ref(as->cost[up]), down, up)); + emit_movrr(as, ir, down, up); /* Backwards codegen needs inverse move. */ + if (!ra_hasspill(IR(ref)->s)) { /* Add the rename to the IR. */ + lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), ref, as->snapno); + ren = tref_ref(lj_ir_emit(as->J)); + as->ir = as->T->ir; /* The IR may have been reallocated. */ + IR(ren)->r = (uint8_t)down; + IR(ren)->s = SPS_NONE; + } +} + +/* Pick a destination register (marked as free). +** Caveat: allow is ignored if there's already a destination register. +** Use ra_destreg() to get a specific register. +*/ +static Reg ra_dest(ASMState *as, IRIns *ir, RegSet allow) +{ + Reg dest = ir->r; + if (ra_hasreg(dest)) { + ra_free(as, dest); + ra_modified(as, dest); + } else { + if (ra_hashint(dest) && rset_test((as->freeset&allow), ra_gethint(dest))) { + dest = ra_gethint(dest); + ra_modified(as, dest); + RA_DBGX((as, "dest $r", dest)); + } else { + dest = ra_scratch(as, allow); + } + ir->r = dest; + } + if (LJ_UNLIKELY(ra_hasspill(ir->s))) ra_save(as, ir, dest); + return dest; +} + +/* Force a specific destination register (marked as free). */ +static void ra_destreg(ASMState *as, IRIns *ir, Reg r) +{ + Reg dest = ra_dest(as, ir, RID2RSET(r)); + if (dest != r) { + lua_assert(rset_test(as->freeset, r)); + ra_modified(as, r); + emit_movrr(as, ir, dest, r); + } +} + +#if LJ_TARGET_X86ORX64 +/* Propagate dest register to left reference. Emit moves as needed. +** This is a required fixup step for all 2-operand machine instructions. +*/ +static void ra_left(ASMState *as, Reg dest, IRRef lref) +{ + IRIns *ir = IR(lref); + Reg left = ir->r; + if (ra_noreg(left)) { + if (irref_isk(lref)) { + if (ir->o == IR_KNUM) { + cTValue *tv = ir_knum(ir); + /* FP remat needs a load except for +0. Still better than eviction. */ + if (tvispzero(tv) || !(as->freeset & RSET_FPR)) { + emit_loadn(as, dest, tv); + return; + } +#if LJ_64 + } else if (ir->o == IR_KINT64) { + emit_loadu64(as, dest, ir_kint64(ir)->u64); + return; +#endif + } else { + lua_assert(ir->o == IR_KINT || ir->o == IR_KGC || + ir->o == IR_KPTR || ir->o == IR_KKPTR || ir->o == IR_KNULL); + emit_loadi(as, dest, ir->i); + return; + } + } + if (!ra_hashint(left) && !iscrossref(as, lref)) + ra_sethint(ir->r, dest); /* Propagate register hint. */ + left = ra_allocref(as, lref, dest < RID_MAX_GPR ? RSET_GPR : RSET_FPR); + } + ra_noweak(as, left); + /* Move needed for true 3-operand instruction: y=a+b ==> y=a; y+=b. */ + if (dest != left) { + /* Use register renaming if dest is the PHI reg. */ + if (irt_isphi(ir->t) && as->phireg[dest] == lref) { + ra_modified(as, left); + ra_rename(as, left, dest); + } else { + emit_movrr(as, ir, dest, left); + } + } +} +#else +/* Similar to ra_left, except we override any hints. */ +static void ra_leftov(ASMState *as, Reg dest, IRRef lref) +{ + IRIns *ir = IR(lref); + Reg left = ir->r; + if (ra_noreg(left)) { + ra_sethint(ir->r, dest); /* Propagate register hint. */ + left = ra_allocref(as, lref, + (LJ_SOFTFP || dest < RID_MAX_GPR) ? RSET_GPR : RSET_FPR); + } + ra_noweak(as, left); + if (dest != left) { + /* Use register renaming if dest is the PHI reg. */ + if (irt_isphi(ir->t) && as->phireg[dest] == lref) { + ra_modified(as, left); + ra_rename(as, left, dest); + } else { + emit_movrr(as, ir, dest, left); + } + } +} +#endif + +#if !LJ_64 +/* Force a RID_RETLO/RID_RETHI destination register pair (marked as free). */ +static void ra_destpair(ASMState *as, IRIns *ir) +{ + Reg destlo = ir->r, desthi = (ir+1)->r; + /* First spill unrelated refs blocking the destination registers. */ + if (!rset_test(as->freeset, RID_RETLO) && + destlo != RID_RETLO && desthi != RID_RETLO) + ra_restore(as, regcost_ref(as->cost[RID_RETLO])); + if (!rset_test(as->freeset, RID_RETHI) && + destlo != RID_RETHI && desthi != RID_RETHI) + ra_restore(as, regcost_ref(as->cost[RID_RETHI])); + /* Next free the destination registers (if any). */ + if (ra_hasreg(destlo)) { + ra_free(as, destlo); + ra_modified(as, destlo); + } else { + destlo = RID_RETLO; + } + if (ra_hasreg(desthi)) { + ra_free(as, desthi); + ra_modified(as, desthi); + } else { + desthi = RID_RETHI; + } + /* Check for conflicts and shuffle the registers as needed. */ + if (destlo == RID_RETHI) { + if (desthi == RID_RETLO) { +#if LJ_TARGET_X86 + *--as->mcp = XI_XCHGa + RID_RETHI; +#else + emit_movrr(as, ir, RID_RETHI, RID_TMP); + emit_movrr(as, ir, RID_RETLO, RID_RETHI); + emit_movrr(as, ir, RID_TMP, RID_RETLO); +#endif + } else { + emit_movrr(as, ir, RID_RETHI, RID_RETLO); + if (desthi != RID_RETHI) emit_movrr(as, ir, desthi, RID_RETHI); + } + } else if (desthi == RID_RETLO) { + emit_movrr(as, ir, RID_RETLO, RID_RETHI); + if (destlo != RID_RETLO) emit_movrr(as, ir, destlo, RID_RETLO); + } else { + if (desthi != RID_RETHI) emit_movrr(as, ir, desthi, RID_RETHI); + if (destlo != RID_RETLO) emit_movrr(as, ir, destlo, RID_RETLO); + } + /* Restore spill slots (if any). */ + if (ra_hasspill((ir+1)->s)) ra_save(as, ir+1, RID_RETHI); + if (ra_hasspill(ir->s)) ra_save(as, ir, RID_RETLO); +} +#endif + +/* -- Snapshot handling --------- ----------------------------------------- */ + +/* Can we rematerialize a KNUM instead of forcing a spill? */ +static int asm_snap_canremat(ASMState *as) +{ + Reg r; + for (r = RID_MIN_FPR; r < RID_MAX_FPR; r++) + if (irref_isk(regcost_ref(as->cost[r]))) + return 1; + return 0; +} + +/* Check whether a sunk store corresponds to an allocation. */ +static int asm_sunk_store(ASMState *as, IRIns *ira, IRIns *irs) +{ + if (irs->s == 255) { + if (irs->o == IR_ASTORE || irs->o == IR_HSTORE || + irs->o == IR_FSTORE || irs->o == IR_XSTORE) { + IRIns *irk = IR(irs->op1); + if (irk->o == IR_AREF || irk->o == IR_HREFK) + irk = IR(irk->op1); + return (IR(irk->op1) == ira); + } + return 0; + } else { + return (ira + irs->s == irs); /* Quick check. */ + } +} + +/* Allocate register or spill slot for a ref that escapes to a snapshot. */ +static void asm_snap_alloc1(ASMState *as, IRRef ref) +{ + IRIns *ir = IR(ref); + if (!irref_isk(ref) && (!(ra_used(ir) || ir->r == RID_SUNK))) { + if (ir->r == RID_SINK) { + ir->r = RID_SUNK; +#if LJ_HASFFI + if (ir->o == IR_CNEWI) { /* Allocate CNEWI value. */ + asm_snap_alloc1(as, ir->op2); + if (LJ_32 && (ir+1)->o == IR_HIOP) + asm_snap_alloc1(as, (ir+1)->op2); + } else +#endif + { /* Allocate stored values for TNEW, TDUP and CNEW. */ + IRIns *irs; + lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW); + for (irs = IR(as->snapref-1); irs > ir; irs--) + if (irs->r == RID_SINK && asm_sunk_store(as, ir, irs)) { + lua_assert(irs->o == IR_ASTORE || irs->o == IR_HSTORE || + irs->o == IR_FSTORE || irs->o == IR_XSTORE); + asm_snap_alloc1(as, irs->op2); + if (LJ_32 && (irs+1)->o == IR_HIOP) + asm_snap_alloc1(as, (irs+1)->op2); + } + } + } else { + RegSet allow; + if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT) { + IRIns *irc; + for (irc = IR(as->curins); irc > ir; irc--) + if ((irc->op1 == ref || irc->op2 == ref) && + !(irc->r == RID_SINK || irc->r == RID_SUNK)) + goto nosink; /* Don't sink conversion if result is used. */ + asm_snap_alloc1(as, ir->op1); + return; + } + nosink: + allow = (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR; + if ((as->freeset & allow) || + (allow == RSET_FPR && asm_snap_canremat(as))) { + /* Get a weak register if we have a free one or can rematerialize. */ + Reg r = ra_allocref(as, ref, allow); /* Allocate a register. */ + if (!irt_isphi(ir->t)) + ra_weak(as, r); /* But mark it as weakly referenced. */ + checkmclim(as); + RA_DBGX((as, "snapreg $f $r", ref, ir->r)); + } else { + ra_spill(as, ir); /* Otherwise force a spill slot. */ + RA_DBGX((as, "snapspill $f $s", ref, ir->s)); + } + } + } +} + +/* Allocate refs escaping to a snapshot. */ +static void asm_snap_alloc(ASMState *as) +{ + SnapShot *snap = &as->T->snap[as->snapno]; + SnapEntry *map = &as->T->snapmap[snap->mapofs]; + MSize n, nent = snap->nent; + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + IRRef ref = snap_ref(sn); + if (!irref_isk(ref)) { + asm_snap_alloc1(as, ref); + if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) { + lua_assert(irt_type(IR(ref+1)->t) == IRT_SOFTFP); + asm_snap_alloc1(as, ref+1); + } + } + } +} + +/* All guards for a snapshot use the same exitno. This is currently the +** same as the snapshot number. Since the exact origin of the exit cannot +** be determined, all guards for the same snapshot must exit with the same +** RegSP mapping. +** A renamed ref which has been used in a prior guard for the same snapshot +** would cause an inconsistency. The easy way out is to force a spill slot. +*/ +static int asm_snap_checkrename(ASMState *as, IRRef ren) +{ + SnapShot *snap = &as->T->snap[as->snapno]; + SnapEntry *map = &as->T->snapmap[snap->mapofs]; + MSize n, nent = snap->nent; + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + IRRef ref = snap_ref(sn); + if (ref == ren || (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && ++ref == ren)) { + IRIns *ir = IR(ref); + ra_spill(as, ir); /* Register renamed, so force a spill slot. */ + RA_DBGX((as, "snaprensp $f $s", ref, ir->s)); + return 1; /* Found. */ + } + } + return 0; /* Not found. */ +} + +/* Prepare snapshot for next guard instruction. */ +static void asm_snap_prep(ASMState *as) +{ + if (as->curins < as->snapref) { + do { + if (as->snapno == 0) return; /* Called by sunk stores before snap #0. */ + as->snapno--; + as->snapref = as->T->snap[as->snapno].ref; + } while (as->curins < as->snapref); + asm_snap_alloc(as); + as->snaprename = as->T->nins; + } else { + /* Process any renames above the highwater mark. */ + for (; as->snaprename < as->T->nins; as->snaprename++) { + IRIns *ir = IR(as->snaprename); + if (asm_snap_checkrename(as, ir->op1)) + ir->op2 = REF_BIAS-1; /* Kill rename. */ + } + } +} + +/* -- Miscellaneous helpers ----------------------------------------------- */ + +/* Collect arguments from CALL* and CARG instructions. */ +static void asm_collectargs(ASMState *as, IRIns *ir, + const CCallInfo *ci, IRRef *args) +{ + uint32_t n = CCI_NARGS(ci); + lua_assert(n <= CCI_NARGS_MAX); + if ((ci->flags & CCI_L)) { *args++ = ASMREF_L; n--; } + while (n-- > 1) { + ir = IR(ir->op1); + lua_assert(ir->o == IR_CARG); + args[n] = ir->op2 == REF_NIL ? 0 : ir->op2; + } + args[0] = ir->op1 == REF_NIL ? 0 : ir->op1; + lua_assert(IR(ir->op1)->o != IR_CARG); +} + +/* Reconstruct CCallInfo flags for CALLX*. */ +static uint32_t asm_callx_flags(ASMState *as, IRIns *ir) +{ + uint32_t nargs = 0; + if (ir->op1 != REF_NIL) { /* Count number of arguments first. */ + IRIns *ira = IR(ir->op1); + nargs++; + while (ira->o == IR_CARG) { nargs++; ira = IR(ira->op1); } + } +#if LJ_HASFFI + if (IR(ir->op2)->o == IR_CARG) { /* Copy calling convention info. */ + CTypeID id = (CTypeID)IR(IR(ir->op2)->op2)->i; + CType *ct = ctype_get(ctype_ctsG(J2G(as->J)), id); + nargs |= ((ct->info & CTF_VARARG) ? CCI_VARARG : 0); +#if LJ_TARGET_X86 + nargs |= (ctype_cconv(ct->info) << CCI_CC_SHIFT); +#endif + } +#endif + return (nargs | (ir->t.irt << CCI_OTSHIFT)); +} + +/* Calculate stack adjustment. */ +static int32_t asm_stack_adjust(ASMState *as) +{ + if (as->evenspill <= SPS_FIXED) + return 0; + return sps_scale(sps_align(as->evenspill)); +} + +/* Must match with hash*() in lj_tab.c. */ +static uint32_t ir_khash(IRIns *ir) +{ + uint32_t lo, hi; + if (irt_isstr(ir->t)) { + return ir_kstr(ir)->hash; + } else if (irt_isnum(ir->t)) { + lo = ir_knum(ir)->u32.lo; + hi = ir_knum(ir)->u32.hi << 1; + } else if (irt_ispri(ir->t)) { + lua_assert(!irt_isnil(ir->t)); + return irt_type(ir->t)-IRT_FALSE; + } else { + lua_assert(irt_isgcv(ir->t)); + lo = u32ptr(ir_kgc(ir)); + hi = lo + HASH_BIAS; + } + return hashrot(lo, hi); +} + +/* -- Allocations --------------------------------------------------------- */ + +static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args); +static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci); + +static void asm_snew(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_new]; + IRRef args[3]; + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ir->op1; /* const char *str */ + args[2] = ir->op2; /* size_t len */ + as->gcsteps++; + asm_setupresult(as, ir, ci); /* GCstr * */ + asm_gencall(as, ci, args); +} + +static void asm_tnew(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_new1]; + IRRef args[2]; + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ASMREF_TMP1; /* uint32_t ahsize */ + as->gcsteps++; + asm_setupresult(as, ir, ci); /* GCtab * */ + asm_gencall(as, ci, args); + ra_allockreg(as, ir->op1 | (ir->op2 << 24), ra_releasetmp(as, ASMREF_TMP1)); +} + +static void asm_tdup(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_dup]; + IRRef args[2]; + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ir->op1; /* const GCtab *kt */ + as->gcsteps++; + asm_setupresult(as, ir, ci); /* GCtab * */ + asm_gencall(as, ci, args); +} + +static void asm_gc_check(ASMState *as); + +/* Explicit GC step. */ +static void asm_gcstep(ASMState *as, IRIns *ir) +{ + IRIns *ira; + for (ira = IR(as->stopins+1); ira < ir; ira++) + if ((ira->o == IR_TNEW || ira->o == IR_TDUP || + (LJ_HASFFI && (ira->o == IR_CNEW || ira->o == IR_CNEWI))) && + ra_used(ira)) + as->gcsteps++; + if (as->gcsteps) + asm_gc_check(as); + as->gcsteps = 0x80000000; /* Prevent implicit GC check further up. */ +} + +/* -- PHI and loop handling ----------------------------------------------- */ + +/* Break a PHI cycle by renaming to a free register (evict if needed). */ +static void asm_phi_break(ASMState *as, RegSet blocked, RegSet blockedby, + RegSet allow) +{ + RegSet candidates = blocked & allow; + if (candidates) { /* If this register file has candidates. */ + /* Note: the set for ra_pick cannot be empty, since each register file + ** has some registers never allocated to PHIs. + */ + Reg down, up = ra_pick(as, ~blocked & allow); /* Get a free register. */ + if (candidates & ~blockedby) /* Optimize shifts, else it's a cycle. */ + candidates = candidates & ~blockedby; + down = rset_picktop(candidates); /* Pick candidate PHI register. */ + ra_rename(as, down, up); /* And rename it to the free register. */ + } +} + +/* PHI register shuffling. +** +** The allocator tries hard to preserve PHI register assignments across +** the loop body. Most of the time this loop does nothing, since there +** are no register mismatches. +** +** If a register mismatch is detected and ... +** - the register is currently free: rename it. +** - the register is blocked by an invariant: restore/remat and rename it. +** - Otherwise the register is used by another PHI, so mark it as blocked. +** +** The renames are order-sensitive, so just retry the loop if a register +** is marked as blocked, but has been freed in the meantime. A cycle is +** detected if all of the blocked registers are allocated. To break the +** cycle rename one of them to a free register and retry. +** +** Note that PHI spill slots are kept in sync and don't need to be shuffled. +*/ +static void asm_phi_shuffle(ASMState *as) +{ + RegSet work; + + /* Find and resolve PHI register mismatches. */ + for (;;) { + RegSet blocked = RSET_EMPTY; + RegSet blockedby = RSET_EMPTY; + RegSet phiset = as->phiset; + while (phiset) { /* Check all left PHI operand registers. */ + Reg r = rset_pickbot(phiset); + IRIns *irl = IR(as->phireg[r]); + Reg left = irl->r; + if (r != left) { /* Mismatch? */ + if (!rset_test(as->freeset, r)) { /* PHI register blocked? */ + IRRef ref = regcost_ref(as->cost[r]); + /* Blocked by other PHI (w/reg)? */ + if (!ra_iskref(ref) && irt_ismarked(IR(ref)->t)) { + rset_set(blocked, r); + if (ra_hasreg(left)) + rset_set(blockedby, left); + left = RID_NONE; + } else { /* Otherwise grab register from invariant. */ + ra_restore(as, ref); + checkmclim(as); + } + } + if (ra_hasreg(left)) { + ra_rename(as, left, r); + checkmclim(as); + } + } + rset_clear(phiset, r); + } + if (!blocked) break; /* Finished. */ + if (!(as->freeset & blocked)) { /* Break cycles if none are free. */ + asm_phi_break(as, blocked, blockedby, RSET_GPR); + if (!LJ_SOFTFP) asm_phi_break(as, blocked, blockedby, RSET_FPR); + checkmclim(as); + } /* Else retry some more renames. */ + } + + /* Restore/remat invariants whose registers are modified inside the loop. */ +#if !LJ_SOFTFP + work = as->modset & ~(as->freeset | as->phiset) & RSET_FPR; + while (work) { + Reg r = rset_pickbot(work); + ra_restore(as, regcost_ref(as->cost[r])); + rset_clear(work, r); + checkmclim(as); + } +#endif + work = as->modset & ~(as->freeset | as->phiset); + while (work) { + Reg r = rset_pickbot(work); + ra_restore(as, regcost_ref(as->cost[r])); + rset_clear(work, r); + checkmclim(as); + } + + /* Allocate and save all unsaved PHI regs and clear marks. */ + work = as->phiset; + while (work) { + Reg r = rset_picktop(work); + IRRef lref = as->phireg[r]; + IRIns *ir = IR(lref); + if (ra_hasspill(ir->s)) { /* Left PHI gained a spill slot? */ + irt_clearmark(ir->t); /* Handled here, so clear marker now. */ + ra_alloc1(as, lref, RID2RSET(r)); + ra_save(as, ir, r); /* Save to spill slot inside the loop. */ + checkmclim(as); + } + rset_clear(work, r); + } +} + +/* Copy unsynced left/right PHI spill slots. Rarely needed. */ +static void asm_phi_copyspill(ASMState *as) +{ + int need = 0; + IRIns *ir; + for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) + if (ra_hasspill(ir->s) && ra_hasspill(IR(ir->op1)->s)) + need |= irt_isfp(ir->t) ? 2 : 1; /* Unsynced spill slot? */ + if ((need & 1)) { /* Copy integer spill slots. */ +#if !LJ_TARGET_X86ORX64 + Reg r = RID_TMP; +#else + Reg r = RID_RET; + if ((as->freeset & RSET_GPR)) + r = rset_pickbot((as->freeset & RSET_GPR)); + else + emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); +#endif + for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) { + if (ra_hasspill(ir->s)) { + IRIns *irl = IR(ir->op1); + if (ra_hasspill(irl->s) && !irt_isfp(ir->t)) { + emit_spstore(as, irl, r, sps_scale(irl->s)); + emit_spload(as, ir, r, sps_scale(ir->s)); + } + } + } +#if LJ_TARGET_X86ORX64 + if (!rset_test(as->freeset, r)) + emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); +#endif + } +#if !LJ_SOFTFP + if ((need & 2)) { /* Copy FP spill slots. */ +#if LJ_TARGET_X86 + Reg r = RID_XMM0; +#else + Reg r = RID_FPRET; +#endif + if ((as->freeset & RSET_FPR)) + r = rset_pickbot((as->freeset & RSET_FPR)); + if (!rset_test(as->freeset, r)) + emit_spload(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); + for (ir = IR(as->orignins-1); ir->o == IR_PHI; ir--) { + if (ra_hasspill(ir->s)) { + IRIns *irl = IR(ir->op1); + if (ra_hasspill(irl->s) && irt_isfp(ir->t)) { + emit_spstore(as, irl, r, sps_scale(irl->s)); + emit_spload(as, ir, r, sps_scale(ir->s)); + } + } + } + if (!rset_test(as->freeset, r)) + emit_spstore(as, IR(regcost_ref(as->cost[r])), r, SPOFS_TMP); + } +#endif +} + +/* Emit renames for left PHIs which are only spilled outside the loop. */ +static void asm_phi_fixup(ASMState *as) +{ + RegSet work = as->phiset; + while (work) { + Reg r = rset_picktop(work); + IRRef lref = as->phireg[r]; + IRIns *ir = IR(lref); + /* Left PHI gained a spill slot before the loop? */ + if (irt_ismarked(ir->t) && ra_hasspill(ir->s)) { + IRRef ren; + lj_ir_set(as->J, IRT(IR_RENAME, IRT_NIL), lref, as->loopsnapno); + ren = tref_ref(lj_ir_emit(as->J)); + as->ir = as->T->ir; /* The IR may have been reallocated. */ + IR(ren)->r = (uint8_t)r; + IR(ren)->s = SPS_NONE; + } + irt_clearmark(ir->t); /* Always clear marker. */ + rset_clear(work, r); + } +} + +/* Setup right PHI reference. */ +static void asm_phi(ASMState *as, IRIns *ir) +{ + RegSet allow = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) & + ~as->phiset; + RegSet afree = (as->freeset & allow); + IRIns *irl = IR(ir->op1); + IRIns *irr = IR(ir->op2); + if (ir->r == RID_SINK) /* Sink PHI. */ + return; + /* Spill slot shuffling is not implemented yet (but rarely needed). */ + if (ra_hasspill(irl->s) || ra_hasspill(irr->s)) + lj_trace_err(as->J, LJ_TRERR_NYIPHI); + /* Leave at least one register free for non-PHIs (and PHI cycle breaking). */ + if ((afree & (afree-1))) { /* Two or more free registers? */ + Reg r; + if (ra_noreg(irr->r)) { /* Get a register for the right PHI. */ + r = ra_allocref(as, ir->op2, allow); + } else { /* Duplicate right PHI, need a copy (rare). */ + r = ra_scratch(as, allow); + emit_movrr(as, irr, r, irr->r); + } + ir->r = (uint8_t)r; + rset_set(as->phiset, r); + as->phireg[r] = (IRRef1)ir->op1; + irt_setmark(irl->t); /* Marks left PHIs _with_ register. */ + if (ra_noreg(irl->r)) + ra_sethint(irl->r, r); /* Set register hint for left PHI. */ + } else { /* Otherwise allocate a spill slot. */ + /* This is overly restrictive, but it triggers only on synthetic code. */ + if (ra_hasreg(irl->r) || ra_hasreg(irr->r)) + lj_trace_err(as->J, LJ_TRERR_NYIPHI); + ra_spill(as, ir); + irr->s = ir->s; /* Set right PHI spill slot. Sync left slot later. */ + } +} + +static void asm_loop_fixup(ASMState *as); + +/* Middle part of a loop. */ +static void asm_loop(ASMState *as) +{ + MCode *mcspill; + /* LOOP is a guard, so the snapno is up to date. */ + as->loopsnapno = as->snapno; + if (as->gcsteps) + asm_gc_check(as); + /* LOOP marks the transition from the variant to the invariant part. */ + as->flagmcp = as->invmcp = NULL; + as->sectref = 0; + if (!neverfuse(as)) as->fuseref = 0; + asm_phi_shuffle(as); + mcspill = as->mcp; + asm_phi_copyspill(as); + asm_loop_fixup(as); + as->mcloop = as->mcp; + RA_DBGX((as, "===== LOOP =====")); + if (!as->realign) RA_DBG_FLUSH(); + if (as->mcp != mcspill) + emit_jmp(as, mcspill); +} + +/* -- Target-specific assembler ------------------------------------------- */ + +#if LJ_TARGET_X86ORX64 +#include "lj_asm_x86.h" +#elif LJ_TARGET_ARM +#include "lj_asm_arm.h" +#elif LJ_TARGET_PPC +#include "lj_asm_ppc.h" +#elif LJ_TARGET_MIPS +#include "lj_asm_mips.h" +#else +#error "Missing assembler for target CPU" +#endif + +/* -- Head of trace ------------------------------------------------------- */ + +/* Head of a root trace. */ +static void asm_head_root(ASMState *as) +{ + int32_t spadj; + asm_head_root_base(as); + emit_setvmstate(as, (int32_t)as->T->traceno); + spadj = asm_stack_adjust(as); + as->T->spadjust = (uint16_t)spadj; + emit_spsub(as, spadj); + /* Root traces assume a checked stack for the starting proto. */ + as->T->topslot = gcref(as->T->startpt)->pt.framesize; +} + +/* Head of a side trace. +** +** The current simplistic algorithm requires that all slots inherited +** from the parent are live in a register between pass 2 and pass 3. This +** avoids the complexity of stack slot shuffling. But of course this may +** overflow the register set in some cases and cause the dreaded error: +** "NYI: register coalescing too complex". A refined algorithm is needed. +*/ +static void asm_head_side(ASMState *as) +{ + IRRef1 sloadins[RID_MAX]; + RegSet allow = RSET_ALL; /* Inverse of all coalesced registers. */ + RegSet live = RSET_EMPTY; /* Live parent registers. */ + IRIns *irp = &as->parent->ir[REF_BASE]; /* Parent base. */ + int32_t spadj, spdelta; + int pass2 = 0; + int pass3 = 0; + IRRef i; + + allow = asm_head_side_base(as, irp, allow); + + /* Scan all parent SLOADs and collect register dependencies. */ + for (i = as->stopins; i > REF_BASE; i--) { + IRIns *ir = IR(i); + RegSP rs; + lua_assert((ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_PARENT)) || + (LJ_SOFTFP && ir->o == IR_HIOP) || ir->o == IR_PVAL); + rs = as->parentmap[i - REF_FIRST]; + if (ra_hasreg(ir->r)) { + rset_clear(allow, ir->r); + if (ra_hasspill(ir->s)) { + ra_save(as, ir, ir->r); + checkmclim(as); + } + } else if (ra_hasspill(ir->s)) { + irt_setmark(ir->t); + pass2 = 1; + } + if (ir->r == rs) { /* Coalesce matching registers right now. */ + ra_free(as, ir->r); + } else if (ra_hasspill(regsp_spill(rs))) { + if (ra_hasreg(ir->r)) + pass3 = 1; + } else if (ra_used(ir)) { + sloadins[rs] = (IRRef1)i; + rset_set(live, rs); /* Block live parent register. */ + } + } + + /* Calculate stack frame adjustment. */ + spadj = asm_stack_adjust(as); + spdelta = spadj - (int32_t)as->parent->spadjust; + if (spdelta < 0) { /* Don't shrink the stack frame. */ + spadj = (int32_t)as->parent->spadjust; + spdelta = 0; + } + as->T->spadjust = (uint16_t)spadj; + + /* Reload spilled target registers. */ + if (pass2) { + for (i = as->stopins; i > REF_BASE; i--) { + IRIns *ir = IR(i); + if (irt_ismarked(ir->t)) { + RegSet mask; + Reg r; + RegSP rs; + irt_clearmark(ir->t); + rs = as->parentmap[i - REF_FIRST]; + if (!ra_hasspill(regsp_spill(rs))) + ra_sethint(ir->r, rs); /* Hint may be gone, set it again. */ + else if (sps_scale(regsp_spill(rs))+spdelta == sps_scale(ir->s)) + continue; /* Same spill slot, do nothing. */ + mask = ((!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR) & allow; + if (mask == RSET_EMPTY) + lj_trace_err(as->J, LJ_TRERR_NYICOAL); + r = ra_allocref(as, i, mask); + ra_save(as, ir, r); + rset_clear(allow, r); + if (r == rs) { /* Coalesce matching registers right now. */ + ra_free(as, r); + rset_clear(live, r); + } else if (ra_hasspill(regsp_spill(rs))) { + pass3 = 1; + } + checkmclim(as); + } + } + } + + /* Store trace number and adjust stack frame relative to the parent. */ + emit_setvmstate(as, (int32_t)as->T->traceno); + emit_spsub(as, spdelta); + +#if !LJ_TARGET_X86ORX64 + /* Restore BASE register from parent spill slot. */ + if (ra_hasspill(irp->s)) + emit_spload(as, IR(REF_BASE), IR(REF_BASE)->r, sps_scale(irp->s)); +#endif + + /* Restore target registers from parent spill slots. */ + if (pass3) { + RegSet work = ~as->freeset & RSET_ALL; + while (work) { + Reg r = rset_pickbot(work); + IRRef ref = regcost_ref(as->cost[r]); + RegSP rs = as->parentmap[ref - REF_FIRST]; + rset_clear(work, r); + if (ra_hasspill(regsp_spill(rs))) { + int32_t ofs = sps_scale(regsp_spill(rs)); + ra_free(as, r); + emit_spload(as, IR(ref), r, ofs); + checkmclim(as); + } + } + } + + /* Shuffle registers to match up target regs with parent regs. */ + for (;;) { + RegSet work; + + /* Repeatedly coalesce free live registers by moving to their target. */ + while ((work = as->freeset & live) != RSET_EMPTY) { + Reg rp = rset_pickbot(work); + IRIns *ir = IR(sloadins[rp]); + rset_clear(live, rp); + rset_clear(allow, rp); + ra_free(as, ir->r); + emit_movrr(as, ir, ir->r, rp); + checkmclim(as); + } + + /* We're done if no live registers remain. */ + if (live == RSET_EMPTY) + break; + + /* Break cycles by renaming one target to a temp. register. */ + if (live & RSET_GPR) { + RegSet tmpset = as->freeset & ~live & allow & RSET_GPR; + if (tmpset == RSET_EMPTY) + lj_trace_err(as->J, LJ_TRERR_NYICOAL); + ra_rename(as, rset_pickbot(live & RSET_GPR), rset_pickbot(tmpset)); + } + if (!LJ_SOFTFP && (live & RSET_FPR)) { + RegSet tmpset = as->freeset & ~live & allow & RSET_FPR; + if (tmpset == RSET_EMPTY) + lj_trace_err(as->J, LJ_TRERR_NYICOAL); + ra_rename(as, rset_pickbot(live & RSET_FPR), rset_pickbot(tmpset)); + } + checkmclim(as); + /* Continue with coalescing to fix up the broken cycle(s). */ + } + + /* Inherit top stack slot already checked by parent trace. */ + as->T->topslot = as->parent->topslot; + if (as->topslot > as->T->topslot) { /* Need to check for higher slot? */ +#ifdef EXITSTATE_CHECKEXIT + /* Highest exit + 1 indicates stack check. */ + ExitNo exitno = as->T->nsnap; +#else + /* Reuse the parent exit in the context of the parent trace. */ + ExitNo exitno = as->J->exitno; +#endif + as->T->topslot = (uint8_t)as->topslot; /* Remember for child traces. */ + asm_stack_check(as, as->topslot, irp, allow & RSET_GPR, exitno); + } +} + +/* -- Tail of trace ------------------------------------------------------- */ + +/* Get base slot for a snapshot. */ +static BCReg asm_baseslot(ASMState *as, SnapShot *snap, int *gotframe) +{ + SnapEntry *map = &as->T->snapmap[snap->mapofs]; + MSize n; + for (n = snap->nent; n > 0; n--) { + SnapEntry sn = map[n-1]; + if ((sn & SNAP_FRAME)) { + *gotframe = 1; + return snap_slot(sn); + } + } + return 0; +} + +/* Link to another trace. */ +static void asm_tail_link(ASMState *as) +{ + SnapNo snapno = as->T->nsnap-1; /* Last snapshot. */ + SnapShot *snap = &as->T->snap[snapno]; + int gotframe = 0; + BCReg baseslot = asm_baseslot(as, snap, &gotframe); + + as->topslot = snap->topslot; + checkmclim(as); + ra_allocref(as, REF_BASE, RID2RSET(RID_BASE)); + + if (as->T->link == 0) { + /* Setup fixed registers for exit to interpreter. */ + const BCIns *pc = snap_pc(as->T->snapmap[snap->mapofs + snap->nent]); + int32_t mres; + if (bc_op(*pc) == BC_JLOOP) { /* NYI: find a better way to do this. */ + BCIns *retpc = &traceref(as->J, bc_d(*pc))->startins; + if (bc_isret(bc_op(*retpc))) + pc = retpc; + } + ra_allockreg(as, i32ptr(J2GG(as->J)->dispatch), RID_DISPATCH); + ra_allockreg(as, i32ptr(pc), RID_LPC); + mres = (int32_t)(snap->nslots - baseslot); + switch (bc_op(*pc)) { + case BC_CALLM: case BC_CALLMT: + mres -= (int32_t)(1 + bc_a(*pc) + bc_c(*pc)); break; + case BC_RETM: mres -= (int32_t)(bc_a(*pc) + bc_d(*pc)); break; + case BC_TSETM: mres -= (int32_t)bc_a(*pc); break; + default: if (bc_op(*pc) < BC_FUNCF) mres = 0; break; + } + ra_allockreg(as, mres, RID_RET); /* Return MULTRES or 0. */ + } else if (baseslot) { + /* Save modified BASE for linking to trace with higher start frame. */ + emit_setgl(as, RID_BASE, jit_base); + } + emit_addptr(as, RID_BASE, 8*(int32_t)baseslot); + + /* Sync the interpreter state with the on-trace state. */ + asm_stack_restore(as, snap); + + /* Root traces that add frames need to check the stack at the end. */ + if (!as->parent && gotframe) + asm_stack_check(as, as->topslot, NULL, as->freeset & RSET_GPR, snapno); +} + +/* -- Trace setup --------------------------------------------------------- */ + +/* Clear reg/sp for all instructions and add register hints. */ +static void asm_setup_regsp(ASMState *as) +{ + GCtrace *T = as->T; + int sink = T->sinktags; + IRRef nins = T->nins; + IRIns *ir, *lastir; + int inloop; +#if LJ_TARGET_ARM + uint32_t rload = 0xa6402a64; +#endif + + ra_setup(as); + + /* Clear reg/sp for constants. */ + for (ir = IR(T->nk), lastir = IR(REF_BASE); ir < lastir; ir++) + ir->prev = REGSP_INIT; + + /* REF_BASE is used for implicit references to the BASE register. */ + lastir->prev = REGSP_HINT(RID_BASE); + + ir = IR(nins-1); + if (ir->o == IR_RENAME) { + do { ir--; nins--; } while (ir->o == IR_RENAME); + T->nins = nins; /* Remove any renames left over from ASM restart. */ + } + as->snaprename = nins; + as->snapref = nins; + as->snapno = T->nsnap; + + as->stopins = REF_BASE; + as->orignins = nins; + as->curins = nins; + + /* Setup register hints for parent link instructions. */ + ir = IR(REF_FIRST); + if (as->parent) { + uint16_t *p; + lastir = lj_snap_regspmap(as->parent, as->J->exitno, ir); + if (lastir - ir > LJ_MAX_JSLOTS) + lj_trace_err(as->J, LJ_TRERR_NYICOAL); + as->stopins = (IRRef)((lastir-1) - as->ir); + for (p = as->parentmap; ir < lastir; ir++) { + RegSP rs = ir->prev; + *p++ = (uint16_t)rs; /* Copy original parent RegSP to parentmap. */ + if (!ra_hasspill(regsp_spill(rs))) + ir->prev = (uint16_t)REGSP_HINT(regsp_reg(rs)); + else + ir->prev = REGSP_INIT; + } + } + + inloop = 0; + as->evenspill = SPS_FIRST; + for (lastir = IR(nins); ir < lastir; ir++) { + if (sink) { + if (ir->r == RID_SINK) + continue; + if (ir->r == RID_SUNK) { /* Revert after ASM restart. */ + ir->r = RID_SINK; + continue; + } + } + switch (ir->o) { + case IR_LOOP: + inloop = 1; + break; +#if LJ_TARGET_ARM + case IR_SLOAD: + if (!((ir->op2 & IRSLOAD_TYPECHECK) || (ir+1)->o == IR_HIOP)) + break; + /* fallthrough */ + case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: + if (!LJ_SOFTFP && irt_isnum(ir->t)) break; + ir->prev = (uint16_t)REGSP_HINT((rload & 15)); + rload = lj_ror(rload, 4); + continue; +#endif + case IR_CALLXS: { + CCallInfo ci; + ci.flags = asm_callx_flags(as, ir); + ir->prev = asm_setup_call_slots(as, ir, &ci); + if (inloop) + as->modset |= RSET_SCRATCH; + continue; + } + case IR_CALLN: case IR_CALLL: case IR_CALLS: { + const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; + ir->prev = asm_setup_call_slots(as, ir, ci); + if (inloop) + as->modset |= (ci->flags & CCI_NOFPRCLOBBER) ? + (RSET_SCRATCH & ~RSET_FPR) : RSET_SCRATCH; + continue; + } +#if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) + case IR_HIOP: + switch ((ir-1)->o) { +#if LJ_SOFTFP && LJ_TARGET_ARM + case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: + if (ra_hashint((ir-1)->r)) { + ir->prev = (ir-1)->prev + 1; + continue; + } + break; +#endif +#if !LJ_SOFTFP && LJ_NEED_FP64 + case IR_CONV: + if (irt_isfp((ir-1)->t)) { + ir->prev = REGSP_HINT(RID_FPRET); + continue; + } + /* fallthrough */ +#endif + case IR_CALLN: case IR_CALLXS: +#if LJ_SOFTFP + case IR_MIN: case IR_MAX: +#endif + (ir-1)->prev = REGSP_HINT(RID_RETLO); + ir->prev = REGSP_HINT(RID_RETHI); + continue; + default: + break; + } + break; +#endif +#if LJ_SOFTFP + case IR_MIN: case IR_MAX: + if ((ir+1)->o != IR_HIOP) break; + /* fallthrough */ +#endif + /* C calls evict all scratch regs and return results in RID_RET. */ + case IR_SNEW: case IR_XSNEW: case IR_NEWREF: + if (REGARG_NUMGPR < 3 && as->evenspill < 3) + as->evenspill = 3; /* lj_str_new and lj_tab_newkey need 3 args. */ + case IR_TNEW: case IR_TDUP: case IR_CNEW: case IR_CNEWI: case IR_TOSTR: + ir->prev = REGSP_HINT(RID_RET); + if (inloop) + as->modset = RSET_SCRATCH; + continue; + case IR_STRTO: case IR_OBAR: + if (inloop) + as->modset = RSET_SCRATCH; + break; +#if !LJ_TARGET_X86ORX64 && !LJ_SOFTFP + case IR_ATAN2: case IR_LDEXP: +#endif + case IR_POW: + if (!LJ_SOFTFP && irt_isnum(ir->t)) { +#if LJ_TARGET_X86ORX64 + ir->prev = REGSP_HINT(RID_XMM0); + if (inloop) + as->modset |= RSET_RANGE(RID_XMM0, RID_XMM1+1)|RID2RSET(RID_EAX); +#else + ir->prev = REGSP_HINT(RID_FPRET); + if (inloop) + as->modset |= RSET_SCRATCH; +#endif + continue; + } + /* fallthrough for integer POW */ + case IR_DIV: case IR_MOD: + if (!irt_isnum(ir->t)) { + ir->prev = REGSP_HINT(RID_RET); + if (inloop) + as->modset |= (RSET_SCRATCH & RSET_GPR); + continue; + } + break; + case IR_FPMATH: +#if LJ_TARGET_X86ORX64 + if (ir->op2 == IRFPM_EXP2) { /* May be joined to lj_vm_pow_sse. */ + ir->prev = REGSP_HINT(RID_XMM0); +#if !LJ_64 + if (as->evenspill < 4) /* Leave room for 16 byte scratch area. */ + as->evenspill = 4; +#endif + if (inloop) + as->modset |= RSET_RANGE(RID_XMM0, RID_XMM2+1)|RID2RSET(RID_EAX); + continue; + } else if (ir->op2 <= IRFPM_TRUNC && !(as->flags & JIT_F_SSE4_1)) { + ir->prev = REGSP_HINT(RID_XMM0); + if (inloop) + as->modset |= RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX); + continue; + } + break; +#else + ir->prev = REGSP_HINT(RID_FPRET); + if (inloop) + as->modset |= RSET_SCRATCH; + continue; +#endif +#if LJ_TARGET_X86ORX64 + /* Non-constant shift counts need to be in RID_ECX on x86/x64. */ + case IR_BSHL: case IR_BSHR: case IR_BSAR: case IR_BROL: case IR_BROR: + if (!irref_isk(ir->op2) && !ra_hashint(IR(ir->op2)->r)) { + IR(ir->op2)->r = REGSP_HINT(RID_ECX); + if (inloop) + rset_set(as->modset, RID_ECX); + } + break; +#endif + /* Do not propagate hints across type conversions or loads. */ + case IR_TOBIT: + case IR_XLOAD: +#if !LJ_TARGET_ARM + case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: +#endif + break; + case IR_CONV: + if (irt_isfp(ir->t) || (ir->op2 & IRCONV_SRCMASK) == IRT_NUM || + (ir->op2 & IRCONV_SRCMASK) == IRT_FLOAT) + break; + /* fallthrough */ + default: + /* Propagate hints across likely 'op reg, imm' or 'op reg'. */ + if (irref_isk(ir->op2) && !irref_isk(ir->op1) && + ra_hashint(regsp_reg(IR(ir->op1)->prev))) { + ir->prev = IR(ir->op1)->prev; + continue; + } + break; + } + ir->prev = REGSP_INIT; + } + if ((as->evenspill & 1)) + as->oddspill = as->evenspill++; + else + as->oddspill = 0; +} + +/* -- Assembler core ------------------------------------------------------ */ + +/* Assemble a trace. */ +void lj_asm_trace(jit_State *J, GCtrace *T) +{ + ASMState as_; + ASMState *as = &as_; + MCode *origtop; + + /* Ensure an initialized instruction beyond the last one for HIOP checks. */ + J->cur.nins = lj_ir_nextins(J); + J->cur.ir[J->cur.nins].o = IR_NOP; + + /* Setup initial state. Copy some fields to reduce indirections. */ + as->J = J; + as->T = T; + as->ir = T->ir; + as->flags = J->flags; + as->loopref = J->loopref; + as->realign = NULL; + as->loopinv = 0; + as->parent = J->parent ? traceref(J, J->parent) : NULL; + + /* Reserve MCode memory. */ + as->mctop = origtop = lj_mcode_reserve(J, &as->mcbot); + as->mcp = as->mctop; + as->mclim = as->mcbot + MCLIM_REDZONE; + asm_setup_target(as); + + do { + as->mcp = as->mctop; + as->curins = T->nins; + RA_DBG_START(); + RA_DBGX((as, "===== STOP =====")); + + /* General trace setup. Emit tail of trace. */ + asm_tail_prep(as); + as->mcloop = NULL; + as->flagmcp = NULL; + as->topslot = 0; + as->gcsteps = 0; + as->sectref = as->loopref; + as->fuseref = (as->flags & JIT_F_OPT_FUSE) ? as->loopref : FUSE_DISABLED; + asm_setup_regsp(as); + if (!as->loopref) + asm_tail_link(as); + + /* Assemble a trace in linear backwards order. */ + for (as->curins--; as->curins > as->stopins; as->curins--) { + IRIns *ir = IR(as->curins); + lua_assert(!(LJ_32 && irt_isint64(ir->t))); /* Handled by SPLIT. */ + if (!ra_used(ir) && !ir_sideeff(ir) && (as->flags & JIT_F_OPT_DCE)) + continue; /* Dead-code elimination can be soooo easy. */ + if (irt_isguard(ir->t)) + asm_snap_prep(as); + RA_DBG_REF(); + checkmclim(as); + asm_ir(as, ir); + } + } while (as->realign); /* Retry in case the MCode needs to be realigned. */ + + /* Emit head of trace. */ + RA_DBG_REF(); + checkmclim(as); + if (as->gcsteps > 0) { + as->curins = as->T->snap[0].ref; + asm_snap_prep(as); /* The GC check is a guard. */ + asm_gc_check(as); + } + ra_evictk(as); + if (as->parent) + asm_head_side(as); + else + asm_head_root(as); + asm_phi_fixup(as); + + RA_DBGX((as, "===== START ====")); + RA_DBG_FLUSH(); + if (as->freeset != RSET_ALL) + lj_trace_err(as->J, LJ_TRERR_BADRA); /* Ouch! Should never happen. */ + + /* Set trace entry point before fixing up tail to allow link to self. */ + T->mcode = as->mcp; + T->mcloop = as->mcloop ? (MSize)((char *)as->mcloop - (char *)as->mcp) : 0; + if (!as->loopref) + asm_tail_fixup(as, T->link); /* Note: this may change as->mctop! */ + T->szmcode = (MSize)((char *)as->mctop - (char *)as->mcp); + lj_mcode_sync(T->mcode, origtop); +} + +#undef IR + +#endif diff --git a/external/lua/luajit/src/src/lj_asm.h b/external/lua/luajit/src/src/lj_asm.h new file mode 100644 index 0000000000..a88e7da3c6 --- /dev/null +++ b/external/lua/luajit/src/src/lj_asm.h @@ -0,0 +1,17 @@ +/* +** IR assembler (SSA IR -> machine code). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_ASM_H +#define _LJ_ASM_H + +#include "lj_jit.h" + +#if LJ_HASJIT +LJ_FUNC void lj_asm_trace(jit_State *J, GCtrace *T); +LJ_FUNC void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, + MCode *target); +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_asm_arm.h b/external/lua/luajit/src/src/lj_asm_arm.h new file mode 100644 index 0000000000..196f797e99 --- /dev/null +++ b/external/lua/luajit/src/src/lj_asm_arm.h @@ -0,0 +1,2353 @@ +/* +** ARM IR assembler (SSA IR -> machine code). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* -- Register allocator extensions --------------------------------------- */ + +/* Allocate a register with a hint. */ +static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) +{ + Reg r = IR(ref)->r; + if (ra_noreg(r)) { + if (!ra_hashint(r) && !iscrossref(as, ref)) + ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ + r = ra_allocref(as, ref, allow); + } + ra_noweak(as, r); + return r; +} + +/* Allocate a scratch register pair. */ +static Reg ra_scratchpair(ASMState *as, RegSet allow) +{ + RegSet pick1 = as->freeset & allow; + RegSet pick2 = pick1 & (pick1 >> 1) & RSET_GPREVEN; + Reg r; + if (pick2) { + r = rset_picktop(pick2); + } else { + RegSet pick = pick1 & (allow >> 1) & RSET_GPREVEN; + if (pick) { + r = rset_picktop(pick); + ra_restore(as, regcost_ref(as->cost[r+1])); + } else { + pick = pick1 & (allow << 1) & RSET_GPRODD; + if (pick) { + r = ra_restore(as, regcost_ref(as->cost[rset_picktop(pick)-1])); + } else { + r = ra_evict(as, allow & (allow >> 1) & RSET_GPREVEN); + ra_restore(as, regcost_ref(as->cost[r+1])); + } + } + } + lua_assert(rset_test(RSET_GPREVEN, r)); + ra_modified(as, r); + ra_modified(as, r+1); + RA_DBGX((as, "scratchpair $r $r", r, r+1)); + return r; +} + +#if !LJ_SOFTFP +/* Allocate two source registers for three-operand instructions. */ +static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) +{ + IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); + Reg left = irl->r, right = irr->r; + if (ra_hasreg(left)) { + ra_noweak(as, left); + if (ra_noreg(right)) + right = ra_allocref(as, ir->op2, rset_exclude(allow, left)); + else + ra_noweak(as, right); + } else if (ra_hasreg(right)) { + ra_noweak(as, right); + left = ra_allocref(as, ir->op1, rset_exclude(allow, right)); + } else if (ra_hashint(right)) { + right = ra_allocref(as, ir->op2, allow); + left = ra_alloc1(as, ir->op1, rset_exclude(allow, right)); + } else { + left = ra_allocref(as, ir->op1, allow); + right = ra_alloc1(as, ir->op2, rset_exclude(allow, left)); + } + return left | (right << 8); +} +#endif + +/* -- Guard handling ------------------------------------------------------ */ + +/* Generate an exit stub group at the bottom of the reserved MCode memory. */ +static MCode *asm_exitstub_gen(ASMState *as, ExitNo group) +{ + MCode *mxp = as->mcbot; + int i; + if (mxp + 4*4+4*EXITSTUBS_PER_GROUP >= as->mctop) + asm_mclimit(as); + /* str lr, [sp]; bl ->vm_exit_handler; .long DISPATCH_address, group. */ + *mxp++ = ARMI_STR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_LR)|ARMF_N(RID_SP); + *mxp = ARMI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)-2)&0x00ffffffu); + mxp++; + *mxp++ = (MCode)i32ptr(J2GG(as->J)->dispatch); /* DISPATCH address */ + *mxp++ = group*EXITSTUBS_PER_GROUP; + for (i = 0; i < EXITSTUBS_PER_GROUP; i++) + *mxp++ = ARMI_B|((-6-i)&0x00ffffffu); + lj_mcode_sync(as->mcbot, mxp); + lj_mcode_commitbot(as->J, mxp); + as->mcbot = mxp; + as->mclim = as->mcbot + MCLIM_REDZONE; + return mxp - EXITSTUBS_PER_GROUP; +} + +/* Setup all needed exit stubs. */ +static void asm_exitstub_setup(ASMState *as, ExitNo nexits) +{ + ExitNo i; + if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) + lj_trace_err(as->J, LJ_TRERR_SNAPOV); + for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++) + if (as->J->exitstubgroup[i] == NULL) + as->J->exitstubgroup[i] = asm_exitstub_gen(as, i); +} + +/* Emit conditional branch to exit for guard. */ +static void asm_guardcc(ASMState *as, ARMCC cc) +{ + MCode *target = exitstub_addr(as->J, as->snapno); + MCode *p = as->mcp; + if (LJ_UNLIKELY(p == as->invmcp)) { + as->loopinv = 1; + *p = ARMI_BL | ((target-p-2) & 0x00ffffffu); + emit_branch(as, ARMF_CC(ARMI_B, cc^1), p+1); + return; + } + emit_branch(as, ARMF_CC(ARMI_BL, cc), target); +} + +/* -- Operand fusion ------------------------------------------------------ */ + +/* Limit linear search to this distance. Avoids O(n^2) behavior. */ +#define CONFLICT_SEARCH_LIM 31 + +/* Check if there's no conflicting instruction between curins and ref. */ +static int noconflict(ASMState *as, IRRef ref, IROp conflict) +{ + IRIns *ir = as->ir; + IRRef i = as->curins; + if (i > ref + CONFLICT_SEARCH_LIM) + return 0; /* Give up, ref is too far away. */ + while (--i > ref) + if (ir[i].o == conflict) + return 0; /* Conflict found. */ + return 1; /* Ok, no conflict. */ +} + +/* Fuse the array base of colocated arrays. */ +static int32_t asm_fuseabase(ASMState *as, IRRef ref) +{ + IRIns *ir = IR(ref); + if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && + !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) + return (int32_t)sizeof(GCtab); + return 0; +} + +/* Fuse array/hash/upvalue reference into register+offset operand. */ +static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow, + int lim) +{ + IRIns *ir = IR(ref); + if (ra_noreg(ir->r)) { + if (ir->o == IR_AREF) { + if (mayfuse(as, ref)) { + if (irref_isk(ir->op2)) { + IRRef tab = IR(ir->op1)->op1; + int32_t ofs = asm_fuseabase(as, tab); + IRRef refa = ofs ? tab : ir->op1; + ofs += 8*IR(ir->op2)->i; + if (ofs > -lim && ofs < lim) { + *ofsp = ofs; + return ra_alloc1(as, refa, allow); + } + } + } + } else if (ir->o == IR_HREFK) { + if (mayfuse(as, ref)) { + int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); + if (ofs < lim) { + *ofsp = ofs; + return ra_alloc1(as, ir->op1, allow); + } + } + } else if (ir->o == IR_UREFC) { + if (irref_isk(ir->op1)) { + GCfunc *fn = ir_kfunc(IR(ir->op1)); + int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); + *ofsp = (ofs & 255); /* Mask out less bits to allow LDRD. */ + return ra_allock(as, (ofs & ~255), allow); + } + } + } + *ofsp = 0; + return ra_alloc1(as, ref, allow); +} + +/* Fuse m operand into arithmetic/logic instructions. */ +static uint32_t asm_fuseopm(ASMState *as, ARMIns ai, IRRef ref, RegSet allow) +{ + IRIns *ir = IR(ref); + if (ra_hasreg(ir->r)) { + ra_noweak(as, ir->r); + return ARMF_M(ir->r); + } else if (irref_isk(ref)) { + uint32_t k = emit_isk12(ai, ir->i); + if (k) + return k; + } else if (mayfuse(as, ref)) { + if (ir->o >= IR_BSHL && ir->o <= IR_BROR) { + Reg m = ra_alloc1(as, ir->op1, allow); + ARMShift sh = ir->o == IR_BSHL ? ARMSH_LSL : + ir->o == IR_BSHR ? ARMSH_LSR : + ir->o == IR_BSAR ? ARMSH_ASR : ARMSH_ROR; + if (irref_isk(ir->op2)) { + return m | ARMF_SH(sh, (IR(ir->op2)->i & 31)); + } else { + Reg s = ra_alloc1(as, ir->op2, rset_exclude(allow, m)); + return m | ARMF_RSH(sh, s); + } + } else if (ir->o == IR_ADD && ir->op1 == ir->op2) { + Reg m = ra_alloc1(as, ir->op1, allow); + return m | ARMF_SH(ARMSH_LSL, 1); + } + } + return ra_allocref(as, ref, allow); +} + +/* Fuse shifts into loads/stores. Only bother with BSHL 2 => lsl #2. */ +static IRRef asm_fuselsl2(ASMState *as, IRRef ref) +{ + IRIns *ir = IR(ref); + if (ra_noreg(ir->r) && mayfuse(as, ref) && ir->o == IR_BSHL && + irref_isk(ir->op2) && IR(ir->op2)->i == 2) + return ir->op1; + return 0; /* No fusion. */ +} + +/* Fuse XLOAD/XSTORE reference into load/store operand. */ +static void asm_fusexref(ASMState *as, ARMIns ai, Reg rd, IRRef ref, + RegSet allow, int32_t ofs) +{ + IRIns *ir = IR(ref); + Reg base; + if (ra_noreg(ir->r) && canfuse(as, ir)) { + int32_t lim = (!LJ_SOFTFP && (ai & 0x08000000)) ? 1024 : + (ai & 0x04000000) ? 4096 : 256; + if (ir->o == IR_ADD) { + int32_t ofs2; + if (irref_isk(ir->op2) && + (ofs2 = ofs + IR(ir->op2)->i) > -lim && ofs2 < lim && + (!(!LJ_SOFTFP && (ai & 0x08000000)) || !(ofs2 & 3))) { + ofs = ofs2; + ref = ir->op1; + } else if (ofs == 0 && !(!LJ_SOFTFP && (ai & 0x08000000))) { + IRRef lref = ir->op1, rref = ir->op2; + Reg rn, rm; + if ((ai & 0x04000000)) { + IRRef sref = asm_fuselsl2(as, rref); + if (sref) { + rref = sref; + ai |= ARMF_SH(ARMSH_LSL, 2); + } else if ((sref = asm_fuselsl2(as, lref)) != 0) { + lref = rref; + rref = sref; + ai |= ARMF_SH(ARMSH_LSL, 2); + } + } + rn = ra_alloc1(as, lref, allow); + rm = ra_alloc1(as, rref, rset_exclude(allow, rn)); + if ((ai & 0x04000000)) ai |= ARMI_LS_R; + emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm); + return; + } + } else if (ir->o == IR_STRREF && !(!LJ_SOFTFP && (ai & 0x08000000))) { + lua_assert(ofs == 0); + ofs = (int32_t)sizeof(GCstr); + if (irref_isk(ir->op2)) { + ofs += IR(ir->op2)->i; + ref = ir->op1; + } else if (irref_isk(ir->op1)) { + ofs += IR(ir->op1)->i; + ref = ir->op2; + } else { + /* NYI: Fuse ADD with constant. */ + Reg rn = ra_alloc1(as, ir->op1, allow); + uint32_t m = asm_fuseopm(as, 0, ir->op2, rset_exclude(allow, rn)); + if ((ai & 0x04000000)) + emit_lso(as, ai, rd, rd, ofs); + else + emit_lsox(as, ai, rd, rd, ofs); + emit_dn(as, ARMI_ADD^m, rd, rn); + return; + } + if (ofs <= -lim || ofs >= lim) { + Reg rn = ra_alloc1(as, ref, allow); + Reg rm = ra_allock(as, ofs, rset_exclude(allow, rn)); + if ((ai & 0x04000000)) ai |= ARMI_LS_R; + emit_dnm(as, ai|ARMI_LS_P|ARMI_LS_U, rd, rn, rm); + return; + } + } + } + base = ra_alloc1(as, ref, allow); +#if !LJ_SOFTFP + if ((ai & 0x08000000)) + emit_vlso(as, ai, rd, base, ofs); + else +#endif + if ((ai & 0x04000000)) + emit_lso(as, ai, rd, base, ofs); + else + emit_lsox(as, ai, rd, base, ofs); +} + +#if !LJ_SOFTFP +/* Fuse to multiply-add/sub instruction. */ +static int asm_fusemadd(ASMState *as, IRIns *ir, ARMIns ai, ARMIns air) +{ + IRRef lref = ir->op1, rref = ir->op2; + IRIns *irm; + if (lref != rref && + ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) && + ra_noreg(irm->r)) || + (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) && + (rref = lref, ai = air, ra_noreg(irm->r))))) { + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg add = ra_hintalloc(as, rref, dest, RSET_FPR); + Reg right, left = ra_alloc2(as, irm, + rset_exclude(rset_exclude(RSET_FPR, dest), add)); + right = (left >> 8); left &= 255; + emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15)); + if (dest != add) emit_dm(as, ARMI_VMOV_D, (dest & 15), (add & 15)); + return 1; + } + return 0; +} +#endif + +/* -- Calls --------------------------------------------------------------- */ + +/* Generate a call to a C function. */ +static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) +{ + uint32_t n, nargs = CCI_NARGS(ci); + int32_t ofs = 0; +#if LJ_SOFTFP + Reg gpr = REGARG_FIRSTGPR; +#else + Reg gpr, fpr = REGARG_FIRSTFPR, fprodd = 0; +#endif + if ((void *)ci->func) + emit_call(as, (void *)ci->func); +#if !LJ_SOFTFP + for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++) + as->cost[gpr] = REGCOST(~0u, ASMREF_L); + gpr = REGARG_FIRSTGPR; +#endif + for (n = 0; n < nargs; n++) { /* Setup args. */ + IRRef ref = args[n]; + IRIns *ir = IR(ref); +#if !LJ_SOFTFP + if (ref && irt_isfp(ir->t)) { + RegSet of = as->freeset; + Reg src; + if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) { + if (irt_isnum(ir->t)) { + if (fpr <= REGARG_LASTFPR) { + ra_leftov(as, fpr, ref); + fpr++; + continue; + } + } else if (fprodd) { /* Ick. */ + src = ra_alloc1(as, ref, RSET_FPR); + emit_dm(as, ARMI_VMOV_S, (fprodd & 15), (src & 15) | 0x00400000); + fprodd = 0; + continue; + } else if (fpr <= REGARG_LASTFPR) { + ra_leftov(as, fpr, ref); + fprodd = fpr++; + continue; + } + /* Workaround to protect argument GPRs from being used for remat. */ + as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1); + src = ra_alloc1(as, ref, RSET_FPR); /* May alloc GPR to remat FPR. */ + as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); + fprodd = 0; + goto stackfp; + } + /* Workaround to protect argument GPRs from being used for remat. */ + as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1); + src = ra_alloc1(as, ref, RSET_FPR); /* May alloc GPR to remat FPR. */ + as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); + if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1u; + if (gpr <= REGARG_LASTGPR) { + lua_assert(rset_test(as->freeset, gpr)); /* Must have been evicted. */ + if (irt_isnum(ir->t)) { + lua_assert(rset_test(as->freeset, gpr+1)); /* Ditto. */ + emit_dnm(as, ARMI_VMOV_RR_D, gpr, gpr+1, (src & 15)); + gpr += 2; + } else { + emit_dn(as, ARMI_VMOV_R_S, gpr, (src & 15)); + gpr++; + } + } else { + stackfp: + if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; + emit_spstore(as, ir, src, ofs); + ofs += irt_isnum(ir->t) ? 8 : 4; + } + } else +#endif + { + if (gpr <= REGARG_LASTGPR) { + lua_assert(rset_test(as->freeset, gpr)); /* Must have been evicted. */ + if (ref) ra_leftov(as, gpr, ref); + gpr++; + } else { + if (ref) { + Reg r = ra_alloc1(as, ref, RSET_GPR); + emit_spstore(as, ir, r, ofs); + } + ofs += 4; + } + } + } +} + +/* Setup result reg/sp for call. Evict scratch regs. */ +static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) +{ + RegSet drop = RSET_SCRATCH; + int hiop = ((ir+1)->o == IR_HIOP); + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + if (hiop && ra_hasreg((ir+1)->r)) + rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ + ra_evictset(as, drop); /* Evictions must be performed first. */ + if (ra_used(ir)) { + lua_assert(!irt_ispri(ir->t)); + if (!LJ_SOFTFP && irt_isfp(ir->t)) { + if (LJ_ABI_SOFTFP || (ci->flags & (CCI_CASTU64|CCI_VARARG))) { + Reg dest = (ra_dest(as, ir, RSET_FPR) & 15); + if (irt_isnum(ir->t)) + emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, dest); + else + emit_dn(as, ARMI_VMOV_S_R, RID_RET, dest); + } else { + ra_destreg(as, ir, RID_FPRET); + } + } else if (hiop) { + ra_destpair(as, ir); + } else { + ra_destreg(as, ir, RID_RET); + } + } + UNUSED(ci); +} + +static void asm_call(ASMState *as, IRIns *ir) +{ + IRRef args[CCI_NARGS_MAX]; + const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; + asm_collectargs(as, ir, ci, args); + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} + +static void asm_callx(ASMState *as, IRIns *ir) +{ + IRRef args[CCI_NARGS_MAX]; + CCallInfo ci; + IRRef func; + IRIns *irf; + ci.flags = asm_callx_flags(as, ir); + asm_collectargs(as, ir, &ci, args); + asm_setupresult(as, ir, &ci); + func = ir->op2; irf = IR(func); + if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } + if (irref_isk(func)) { /* Call to constant address. */ + ci.func = (ASMFunction)(void *)(irf->i); + } else { /* Need a non-argument register for indirect calls. */ + Reg freg = ra_alloc1(as, func, RSET_RANGE(RID_R4, RID_R12+1)); + emit_m(as, ARMI_BLXr, freg); + ci.func = (ASMFunction)(void *)0; + } + asm_gencall(as, &ci, args); +} + +/* -- Returns ------------------------------------------------------------- */ + +/* Return to lower frame. Guard that it goes to the right spot. */ +static void asm_retf(ASMState *as, IRIns *ir) +{ + Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); + void *pc = ir_kptr(IR(ir->op2)); + int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); + as->topslot -= (BCReg)delta; + if ((int32_t)as->topslot < 0) as->topslot = 0; + /* Need to force a spill on REF_BASE now to update the stack slot. */ + emit_lso(as, ARMI_STR, base, RID_SP, ra_spill(as, IR(REF_BASE))); + emit_setgl(as, base, jit_base); + emit_addptr(as, base, -8*delta); + asm_guardcc(as, CC_NE); + emit_nm(as, ARMI_CMP, RID_TMP, + ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); + emit_lso(as, ARMI_LDR, RID_TMP, base, -4); +} + +/* -- Type conversions ---------------------------------------------------- */ + +#if !LJ_SOFTFP +static void asm_tointg(ASMState *as, IRIns *ir, Reg left) +{ + Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); + Reg dest = ra_dest(as, ir, RSET_GPR); + asm_guardcc(as, CC_NE); + emit_d(as, ARMI_VMRS, 0); + emit_dm(as, ARMI_VCMP_D, (tmp & 15), (left & 15)); + emit_dm(as, ARMI_VCVT_F64_S32, (tmp & 15), (tmp & 15)); + emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); + emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (left & 15)); +} + +static void asm_tobit(ASMState *as, IRIns *ir) +{ + RegSet allow = RSET_FPR; + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, allow); + Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); + Reg tmp = ra_scratch(as, rset_clear(allow, right)); + emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); + emit_dnm(as, ARMI_VADD_D, (tmp & 15), (left & 15), (right & 15)); +} +#endif + +static void asm_conv(ASMState *as, IRIns *ir) +{ + IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); +#if !LJ_SOFTFP + int stfp = (st == IRT_NUM || st == IRT_FLOAT); +#endif + IRRef lref = ir->op1; + /* 64 bit integer conversions are handled by SPLIT. */ + lua_assert(!irt_isint64(ir->t) && !(st == IRT_I64 || st == IRT_U64)); +#if LJ_SOFTFP + /* FP conversions are handled by SPLIT. */ + lua_assert(!irt_isfp(ir->t) && !(st == IRT_NUM || st == IRT_FLOAT)); + /* Can't check for same types: SPLIT uses CONV int.int + BXOR for sfp NEG. */ +#else + lua_assert(irt_type(ir->t) != st); + if (irt_isfp(ir->t)) { + Reg dest = ra_dest(as, ir, RSET_FPR); + if (stfp) { /* FP to FP conversion. */ + emit_dm(as, st == IRT_NUM ? ARMI_VCVT_F32_F64 : ARMI_VCVT_F64_F32, + (dest & 15), (ra_alloc1(as, lref, RSET_FPR) & 15)); + } else { /* Integer to FP conversion. */ + Reg left = ra_alloc1(as, lref, RSET_GPR); + ARMIns ai = irt_isfloat(ir->t) ? + (st == IRT_INT ? ARMI_VCVT_F32_S32 : ARMI_VCVT_F32_U32) : + (st == IRT_INT ? ARMI_VCVT_F64_S32 : ARMI_VCVT_F64_U32); + emit_dm(as, ai, (dest & 15), (dest & 15)); + emit_dn(as, ARMI_VMOV_S_R, left, (dest & 15)); + } + } else if (stfp) { /* FP to integer conversion. */ + if (irt_isguard(ir->t)) { + /* Checked conversions are only supported from number to int. */ + lua_assert(irt_isint(ir->t) && st == IRT_NUM); + asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, lref, RSET_FPR); + Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); + ARMIns ai; + emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); + ai = irt_isint(ir->t) ? + (st == IRT_NUM ? ARMI_VCVT_S32_F64 : ARMI_VCVT_S32_F32) : + (st == IRT_NUM ? ARMI_VCVT_U32_F64 : ARMI_VCVT_U32_F32); + emit_dm(as, ai, (tmp & 15), (left & 15)); + } + } else +#endif + { + Reg dest = ra_dest(as, ir, RSET_GPR); + if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ + Reg left = ra_alloc1(as, lref, RSET_GPR); + lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); + if ((as->flags & JIT_F_ARMV6)) { + ARMIns ai = st == IRT_I8 ? ARMI_SXTB : + st == IRT_U8 ? ARMI_UXTB : + st == IRT_I16 ? ARMI_SXTH : ARMI_UXTH; + emit_dm(as, ai, dest, left); + } else if (st == IRT_U8) { + emit_dn(as, ARMI_AND|ARMI_K12|255, dest, left); + } else { + uint32_t shift = st == IRT_I8 ? 24 : 16; + ARMShift sh = st == IRT_U16 ? ARMSH_LSR : ARMSH_ASR; + emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, RID_TMP); + emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_LSL, shift), RID_TMP, left); + } + } else { /* Handle 32/32 bit no-op (cast). */ + ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ + } + } +} + +#if !LJ_SOFTFP && LJ_HASFFI +static void asm_conv64(ASMState *as, IRIns *ir) +{ + IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); + IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); + IRCallID id; + CCallInfo ci; + IRRef args[2]; + args[0] = (ir-1)->op1; + args[1] = ir->op1; + if (st == IRT_NUM || st == IRT_FLOAT) { + id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); + ir--; + } else { + id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); + } + ci = lj_ir_callinfo[id]; +#if !LJ_ABI_SOFTFP + ci.flags |= CCI_VARARG; /* These calls don't use the hard-float ABI! */ +#endif + asm_setupresult(as, ir, &ci); + asm_gencall(as, &ci, args); +} +#endif + +static void asm_strto(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; + IRRef args[2]; + Reg rlo = 0, rhi = 0, tmp; + int destused = ra_used(ir); + int32_t ofs = 0; + ra_evictset(as, RSET_SCRATCH); +#if LJ_SOFTFP + if (destused) { + if (ra_hasspill(ir->s) && ra_hasspill((ir+1)->s) && + (ir->s & 1) == 0 && ir->s + 1 == (ir+1)->s) { + int i; + for (i = 0; i < 2; i++) { + Reg r = (ir+i)->r; + if (ra_hasreg(r)) { + ra_free(as, r); + ra_modified(as, r); + emit_spload(as, ir+i, r, sps_scale((ir+i)->s)); + } + } + ofs = sps_scale(ir->s); + destused = 0; + } else { + rhi = ra_dest(as, ir+1, RSET_GPR); + rlo = ra_dest(as, ir, rset_exclude(RSET_GPR, rhi)); + } + } + asm_guardcc(as, CC_EQ); + if (destused) { + emit_lso(as, ARMI_LDR, rhi, RID_SP, 4); + emit_lso(as, ARMI_LDR, rlo, RID_SP, 0); + } +#else + UNUSED(rhi); + if (destused) { + if (ra_hasspill(ir->s)) { + ofs = sps_scale(ir->s); + destused = 0; + if (ra_hasreg(ir->r)) { + ra_free(as, ir->r); + ra_modified(as, ir->r); + emit_spload(as, ir, ir->r, ofs); + } + } else { + rlo = ra_dest(as, ir, RSET_FPR); + } + } + asm_guardcc(as, CC_EQ); + if (destused) + emit_vlso(as, ARMI_VLDR_D, rlo, RID_SP, 0); +#endif + emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET); /* Test return status. */ + args[0] = ir->op1; /* GCstr *str */ + args[1] = ASMREF_TMP1; /* TValue *n */ + asm_gencall(as, ci, args); + tmp = ra_releasetmp(as, ASMREF_TMP1); + if (ofs == 0) + emit_dm(as, ARMI_MOV, tmp, RID_SP); + else + emit_opk(as, ARMI_ADD, tmp, RID_SP, ofs, RSET_GPR); +} + +/* Get pointer to TValue. */ +static void asm_tvptr(ASMState *as, Reg dest, IRRef ref) +{ + IRIns *ir = IR(ref); + if (irt_isnum(ir->t)) { + if (irref_isk(ref)) { + /* Use the number constant itself as a TValue. */ + ra_allockreg(as, i32ptr(ir_knum(ir)), dest); + } else { +#if LJ_SOFTFP + lua_assert(0); +#else + /* Otherwise force a spill and use the spill slot. */ + emit_opk(as, ARMI_ADD, dest, RID_SP, ra_spill(as, ir), RSET_GPR); +#endif + } + } else { + /* Otherwise use [sp] and [sp+4] to hold the TValue. */ + RegSet allow = rset_exclude(RSET_GPR, dest); + Reg type; + emit_dm(as, ARMI_MOV, dest, RID_SP); + if (!irt_ispri(ir->t)) { + Reg src = ra_alloc1(as, ref, allow); + emit_lso(as, ARMI_STR, src, RID_SP, 0); + } + if ((ir+1)->o == IR_HIOP) + type = ra_alloc1(as, ref+1, allow); + else + type = ra_allock(as, irt_toitype(ir->t), allow); + emit_lso(as, ARMI_STR, type, RID_SP, 4); + } +} + +static void asm_tostr(ASMState *as, IRIns *ir) +{ + IRRef args[2]; + args[0] = ASMREF_L; + as->gcsteps++; + if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; + args[1] = ASMREF_TMP1; /* const lua_Number * */ + asm_setupresult(as, ir, ci); /* GCstr * */ + asm_gencall(as, ci, args); + asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1); + } else { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; + args[1] = ir->op1; /* int32_t k */ + asm_setupresult(as, ir, ci); /* GCstr * */ + asm_gencall(as, ci, args); + } +} + +/* -- Memory references --------------------------------------------------- */ + +static void asm_aref(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg idx, base; + if (irref_isk(ir->op2)) { + IRRef tab = IR(ir->op1)->op1; + int32_t ofs = asm_fuseabase(as, tab); + IRRef refa = ofs ? tab : ir->op1; + uint32_t k = emit_isk12(ARMI_ADD, ofs + 8*IR(ir->op2)->i); + if (k) { + base = ra_alloc1(as, refa, RSET_GPR); + emit_dn(as, ARMI_ADD^k, dest, base); + return; + } + } + base = ra_alloc1(as, ir->op1, RSET_GPR); + idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); + emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, base, idx); +} + +/* Inlined hash lookup. Specialized for key type and for const keys. +** The equivalent C code is: +** Node *n = hashkey(t, key); +** do { +** if (lj_obj_equal(&n->key, key)) return &n->val; +** } while ((n = nextnode(n))); +** return niltv(L); +*/ +static void asm_href(ASMState *as, IRIns *ir, IROp merge) +{ + RegSet allow = RSET_GPR; + int destused = ra_used(ir); + Reg dest = ra_dest(as, ir, allow); + Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); + Reg key = 0, keyhi = 0, keynumhi = RID_NONE, tmp = RID_TMP; + IRRef refkey = ir->op2; + IRIns *irkey = IR(refkey); + IRType1 kt = irkey->t; + int32_t k = 0, khi = emit_isk12(ARMI_CMP, irt_toitype(kt)); + uint32_t khash; + MCLabel l_end, l_loop; + rset_clear(allow, tab); + if (!irref_isk(refkey) || irt_isstr(kt)) { +#if LJ_SOFTFP + key = ra_alloc1(as, refkey, allow); + rset_clear(allow, key); + if (irkey[1].o == IR_HIOP) { + if (ra_hasreg((irkey+1)->r)) { + keynumhi = (irkey+1)->r; + keyhi = RID_TMP; + ra_noweak(as, keynumhi); + } else { + keyhi = keynumhi = ra_allocref(as, refkey+1, allow); + } + rset_clear(allow, keynumhi); + khi = 0; + } +#else + if (irt_isnum(kt)) { + key = ra_scratch(as, allow); + rset_clear(allow, key); + keyhi = keynumhi = ra_scratch(as, allow); + rset_clear(allow, keyhi); + khi = 0; + } else { + key = ra_alloc1(as, refkey, allow); + rset_clear(allow, key); + } +#endif + } else if (irt_isnum(kt)) { + int32_t val = (int32_t)ir_knum(irkey)->u32.lo; + k = emit_isk12(ARMI_CMP, val); + if (!k) { + key = ra_allock(as, val, allow); + rset_clear(allow, key); + } + val = (int32_t)ir_knum(irkey)->u32.hi; + khi = emit_isk12(ARMI_CMP, val); + if (!khi) { + keyhi = ra_allock(as, val, allow); + rset_clear(allow, keyhi); + } + } else if (!irt_ispri(kt)) { + k = emit_isk12(ARMI_CMP, irkey->i); + if (!k) { + key = ra_alloc1(as, refkey, allow); + rset_clear(allow, key); + } + } + if (!irt_ispri(kt)) + tmp = ra_scratchpair(as, allow); + + /* Key not found in chain: jump to exit (if merged) or load niltv. */ + l_end = emit_label(as); + as->invmcp = NULL; + if (merge == IR_NE) + asm_guardcc(as, CC_AL); + else if (destused) + emit_loada(as, dest, niltvg(J2G(as->J))); + + /* Follow hash chain until the end. */ + l_loop = --as->mcp; + emit_n(as, ARMI_CMP|ARMI_K12|0, dest); + emit_lso(as, ARMI_LDR, dest, dest, (int32_t)offsetof(Node, next)); + + /* Type and value comparison. */ + if (merge == IR_EQ) + asm_guardcc(as, CC_EQ); + else + emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); + if (!irt_ispri(kt)) { + emit_nm(as, ARMF_CC(ARMI_CMP, CC_EQ)^k, tmp, key); + emit_nm(as, ARMI_CMP^khi, tmp+1, keyhi); + emit_lsox(as, ARMI_LDRD, tmp, dest, (int32_t)offsetof(Node, key)); + } else { + emit_n(as, ARMI_CMP^khi, tmp); + emit_lso(as, ARMI_LDR, tmp, dest, (int32_t)offsetof(Node, key.it)); + } + *l_loop = ARMF_CC(ARMI_B, CC_NE) | ((as->mcp-l_loop-2) & 0x00ffffffu); + + /* Load main position relative to tab->node into dest. */ + khash = irref_isk(refkey) ? ir_khash(irkey) : 1; + if (khash == 0) { + emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); + } else { + emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 3), dest, dest, tmp); + emit_dnm(as, ARMI_ADD|ARMF_SH(ARMSH_LSL, 1), tmp, tmp, tmp); + if (irt_isstr(kt)) { /* Fetch of str->hash is cheaper than ra_allock. */ + emit_dnm(as, ARMI_AND, tmp, tmp+1, RID_TMP); + emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); + emit_lso(as, ARMI_LDR, tmp+1, key, (int32_t)offsetof(GCstr, hash)); + emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); + } else if (irref_isk(refkey)) { + emit_opk(as, ARMI_AND, tmp, RID_TMP, (int32_t)khash, + rset_exclude(rset_exclude(RSET_GPR, tab), dest)); + emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); + emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); + } else { /* Must match with hash*() in lj_tab.c. */ + if (ra_hasreg(keynumhi)) { /* Canonicalize +-0.0 to 0.0. */ + if (keyhi == RID_TMP) + emit_dm(as, ARMF_CC(ARMI_MOV, CC_NE), keyhi, keynumhi); + emit_d(as, ARMF_CC(ARMI_MOV, CC_EQ)|ARMI_K12|0, keyhi); + } + emit_dnm(as, ARMI_AND, tmp, tmp, RID_TMP); + emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT3), tmp, tmp, tmp+1); + emit_lso(as, ARMI_LDR, dest, tab, (int32_t)offsetof(GCtab, node)); + emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 32-((HASH_ROT2+HASH_ROT1)&31)), + tmp, tmp+1, tmp); + emit_lso(as, ARMI_LDR, RID_TMP, tab, (int32_t)offsetof(GCtab, hmask)); + emit_dnm(as, ARMI_SUB|ARMF_SH(ARMSH_ROR, 32-HASH_ROT1), tmp+1, tmp+1, tmp); + if (ra_hasreg(keynumhi)) { + emit_dnm(as, ARMI_EOR, tmp+1, tmp, key); + emit_dnm(as, ARMI_ORR|ARMI_S, RID_TMP, tmp, key); /* Test for +-0.0. */ + emit_dnm(as, ARMI_ADD, tmp, keynumhi, keynumhi); +#if !LJ_SOFTFP + emit_dnm(as, ARMI_VMOV_RR_D, key, keynumhi, + (ra_alloc1(as, refkey, RSET_FPR) & 15)); +#endif + } else { + emit_dnm(as, ARMI_EOR, tmp+1, tmp, key); + emit_opk(as, ARMI_ADD, tmp, key, (int32_t)HASH_BIAS, + rset_exclude(rset_exclude(RSET_GPR, tab), key)); + } + } + } +} + +static void asm_hrefk(ASMState *as, IRIns *ir) +{ + IRIns *kslot = IR(ir->op2); + IRIns *irkey = IR(kslot->op1); + int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); + int32_t kofs = ofs + (int32_t)offsetof(Node, key); + Reg dest = (ra_used(ir) || ofs > 4095) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; + Reg node = ra_alloc1(as, ir->op1, RSET_GPR); + Reg key = RID_NONE, type = RID_TMP, idx = node; + RegSet allow = rset_exclude(RSET_GPR, node); + lua_assert(ofs % sizeof(Node) == 0); + if (ofs > 4095) { + idx = dest; + rset_clear(allow, dest); + kofs = (int32_t)offsetof(Node, key); + } else if (ra_hasreg(dest)) { + emit_opk(as, ARMI_ADD, dest, node, ofs, allow); + } + asm_guardcc(as, CC_NE); + if (!irt_ispri(irkey->t)) { + RegSet even = (as->freeset & allow); + even = even & (even >> 1) & RSET_GPREVEN; + if (even) { + key = ra_scratch(as, even); + if (rset_test(as->freeset, key+1)) { + type = key+1; + ra_modified(as, type); + } + } else { + key = ra_scratch(as, allow); + } + rset_clear(allow, key); + } + rset_clear(allow, type); + if (irt_isnum(irkey->t)) { + emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, type, + (int32_t)ir_knum(irkey)->u32.hi, allow); + emit_opk(as, ARMI_CMP, 0, key, + (int32_t)ir_knum(irkey)->u32.lo, allow); + } else { + if (ra_hasreg(key)) + emit_opk(as, ARMF_CC(ARMI_CMP, CC_EQ), 0, key, irkey->i, allow); + emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype(irkey->t), type); + } + emit_lso(as, ARMI_LDR, type, idx, kofs+4); + if (ra_hasreg(key)) emit_lso(as, ARMI_LDR, key, idx, kofs); + if (ofs > 4095) + emit_opk(as, ARMI_ADD, dest, node, ofs, RSET_GPR); +} + +static void asm_newref(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; + IRRef args[3]; + if (ir->r == RID_SINK) + return; + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ir->op1; /* GCtab *t */ + args[2] = ASMREF_TMP1; /* cTValue *key */ + asm_setupresult(as, ir, ci); /* TValue * */ + asm_gencall(as, ci, args); + asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2); +} + +static void asm_uref(ASMState *as, IRIns *ir) +{ + /* NYI: Check that UREFO is still open and not aliasing a slot. */ + Reg dest = ra_dest(as, ir, RSET_GPR); + if (irref_isk(ir->op1)) { + GCfunc *fn = ir_kfunc(IR(ir->op1)); + MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; + emit_lsptr(as, ARMI_LDR, dest, v); + } else { + Reg uv = ra_scratch(as, RSET_GPR); + Reg func = ra_alloc1(as, ir->op1, RSET_GPR); + if (ir->o == IR_UREFC) { + asm_guardcc(as, CC_NE); + emit_n(as, ARMI_CMP|ARMI_K12|1, RID_TMP); + emit_opk(as, ARMI_ADD, dest, uv, + (int32_t)offsetof(GCupval, tv), RSET_GPR); + emit_lso(as, ARMI_LDRB, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); + } else { + emit_lso(as, ARMI_LDR, dest, uv, (int32_t)offsetof(GCupval, v)); + } + emit_lso(as, ARMI_LDR, uv, func, + (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); + } +} + +static void asm_fref(ASMState *as, IRIns *ir) +{ + UNUSED(as); UNUSED(ir); + lua_assert(!ra_used(ir)); +} + +static void asm_strref(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + IRRef ref = ir->op2, refk = ir->op1; + Reg r; + if (irref_isk(ref)) { + IRRef tmp = refk; refk = ref; ref = tmp; + } else if (!irref_isk(refk)) { + uint32_t k, m = ARMI_K12|sizeof(GCstr); + Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); + IRIns *irr = IR(ir->op2); + if (ra_hasreg(irr->r)) { + ra_noweak(as, irr->r); + right = irr->r; + } else if (mayfuse(as, irr->op2) && + irr->o == IR_ADD && irref_isk(irr->op2) && + (k = emit_isk12(ARMI_ADD, + (int32_t)sizeof(GCstr) + IR(irr->op2)->i))) { + m = k; + right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); + } else { + right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); + } + emit_dn(as, ARMI_ADD^m, dest, dest); + emit_dnm(as, ARMI_ADD, dest, left, right); + return; + } + r = ra_alloc1(as, ref, RSET_GPR); + emit_opk(as, ARMI_ADD, dest, r, + sizeof(GCstr) + IR(refk)->i, rset_exclude(RSET_GPR, r)); +} + +/* -- Loads and stores ---------------------------------------------------- */ + +static ARMIns asm_fxloadins(IRIns *ir) +{ + switch (irt_type(ir->t)) { + case IRT_I8: return ARMI_LDRSB; + case IRT_U8: return ARMI_LDRB; + case IRT_I16: return ARMI_LDRSH; + case IRT_U16: return ARMI_LDRH; + case IRT_NUM: lua_assert(!LJ_SOFTFP); return ARMI_VLDR_D; + case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VLDR_S; + default: return ARMI_LDR; + } +} + +static ARMIns asm_fxstoreins(IRIns *ir) +{ + switch (irt_type(ir->t)) { + case IRT_I8: case IRT_U8: return ARMI_STRB; + case IRT_I16: case IRT_U16: return ARMI_STRH; + case IRT_NUM: lua_assert(!LJ_SOFTFP); return ARMI_VSTR_D; + case IRT_FLOAT: if (!LJ_SOFTFP) return ARMI_VSTR_S; + default: return ARMI_STR; + } +} + +static void asm_fload(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg idx = ra_alloc1(as, ir->op1, RSET_GPR); + ARMIns ai = asm_fxloadins(ir); + int32_t ofs; + if (ir->op2 == IRFL_TAB_ARRAY) { + ofs = asm_fuseabase(as, ir->op1); + if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ + emit_dn(as, ARMI_ADD|ARMI_K12|ofs, dest, idx); + return; + } + } + ofs = field_ofs[ir->op2]; + if ((ai & 0x04000000)) + emit_lso(as, ai, dest, idx, ofs); + else + emit_lsox(as, ai, dest, idx, ofs); +} + +static void asm_fstore(ASMState *as, IRIns *ir) +{ + if (ir->r != RID_SINK) { + Reg src = ra_alloc1(as, ir->op2, RSET_GPR); + IRIns *irf = IR(ir->op1); + Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); + int32_t ofs = field_ofs[irf->op2]; + ARMIns ai = asm_fxstoreins(ir); + if ((ai & 0x04000000)) + emit_lso(as, ai, src, idx, ofs); + else + emit_lsox(as, ai, src, idx, ofs); + } +} + +static void asm_xload(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, + (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); + lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED)); + asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0); +} + +static void asm_xstore(ASMState *as, IRIns *ir, int32_t ofs) +{ + if (ir->r != RID_SINK) { + Reg src = ra_alloc1(as, ir->op2, + (!LJ_SOFTFP && irt_isfp(ir->t)) ? RSET_FPR : RSET_GPR); + asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1, + rset_exclude(RSET_GPR, src), ofs); + } +} + +static void asm_ahuvload(ASMState *as, IRIns *ir) +{ + int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); + IRType t = hiop ? IRT_NUM : irt_type(ir->t); + Reg dest = RID_NONE, type = RID_NONE, idx; + RegSet allow = RSET_GPR; + int32_t ofs = 0; + if (hiop && ra_used(ir+1)) { + type = ra_dest(as, ir+1, allow); + rset_clear(allow, type); + } + if (ra_used(ir)) { + lua_assert((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) || + irt_isint(ir->t) || irt_isaddr(ir->t)); + dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow); + rset_clear(allow, dest); + } + idx = asm_fuseahuref(as, ir->op1, &ofs, allow, + (!LJ_SOFTFP && t == IRT_NUM) ? 1024 : 4096); + if (!hiop || type == RID_NONE) { + rset_clear(allow, idx); + if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 && + rset_test((as->freeset & allow), dest+1)) { + type = dest+1; + ra_modified(as, type); + } else { + type = RID_TMP; + } + } + asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE); + emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type); + if (ra_hasreg(dest)) { +#if !LJ_SOFTFP + if (t == IRT_NUM) + emit_vlso(as, ARMI_VLDR_D, dest, idx, ofs); + else +#endif + emit_lso(as, ARMI_LDR, dest, idx, ofs); + } + emit_lso(as, ARMI_LDR, type, idx, ofs+4); +} + +static void asm_ahustore(ASMState *as, IRIns *ir) +{ + if (ir->r != RID_SINK) { + RegSet allow = RSET_GPR; + Reg idx, src = RID_NONE, type = RID_NONE; + int32_t ofs = 0; +#if !LJ_SOFTFP + if (irt_isnum(ir->t)) { + src = ra_alloc1(as, ir->op2, RSET_FPR); + idx = asm_fuseahuref(as, ir->op1, &ofs, allow, 1024); + emit_vlso(as, ARMI_VSTR_D, src, idx, ofs); + } else +#endif + { + int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); + if (!irt_ispri(ir->t)) { + src = ra_alloc1(as, ir->op2, allow); + rset_clear(allow, src); + } + if (hiop) + type = ra_alloc1(as, (ir+1)->op2, allow); + else + type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); + idx = asm_fuseahuref(as, ir->op1, &ofs, rset_exclude(allow, type), 4096); + if (ra_hasreg(src)) emit_lso(as, ARMI_STR, src, idx, ofs); + emit_lso(as, ARMI_STR, type, idx, ofs+4); + } + } +} + +static void asm_sload(ASMState *as, IRIns *ir) +{ + int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0); + int hiop = (LJ_SOFTFP && (ir+1)->o == IR_HIOP); + IRType t = hiop ? IRT_NUM : irt_type(ir->t); + Reg dest = RID_NONE, type = RID_NONE, base; + RegSet allow = RSET_GPR; + lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ + lua_assert(irt_isguard(ir->t) || !(ir->op2 & IRSLOAD_TYPECHECK)); +#if LJ_SOFTFP + lua_assert(!(ir->op2 & IRSLOAD_CONVERT)); /* Handled by LJ_SOFTFP SPLIT. */ + if (hiop && ra_used(ir+1)) { + type = ra_dest(as, ir+1, allow); + rset_clear(allow, type); + } +#else + if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(ir->t) && t == IRT_INT) { + dest = ra_scratch(as, RSET_FPR); + asm_tointg(as, ir, dest); + t = IRT_NUM; /* Continue with a regular number type check. */ + } else +#endif + if (ra_used(ir)) { + lua_assert((LJ_SOFTFP ? 0 : irt_isnum(ir->t)) || + irt_isint(ir->t) || irt_isaddr(ir->t)); + dest = ra_dest(as, ir, (!LJ_SOFTFP && t == IRT_NUM) ? RSET_FPR : allow); + rset_clear(allow, dest); + base = ra_alloc1(as, REF_BASE, allow); + if ((ir->op2 & IRSLOAD_CONVERT)) { + if (t == IRT_INT) { + Reg tmp = ra_scratch(as, RSET_FPR); + emit_dn(as, ARMI_VMOV_R_S, dest, (tmp & 15)); + emit_dm(as, ARMI_VCVT_S32_F64, (tmp & 15), (tmp & 15)); + dest = tmp; + t = IRT_NUM; /* Check for original type. */ + } else { + Reg tmp = ra_scratch(as, RSET_GPR); + emit_dm(as, ARMI_VCVT_F64_S32, (dest & 15), (dest & 15)); + emit_dn(as, ARMI_VMOV_S_R, tmp, (dest & 15)); + dest = tmp; + t = IRT_INT; /* Check for original type. */ + } + } + goto dotypecheck; + } + base = ra_alloc1(as, REF_BASE, allow); +dotypecheck: + rset_clear(allow, base); + if ((ir->op2 & IRSLOAD_TYPECHECK)) { + if (ra_noreg(type)) { + if (ofs < 256 && ra_hasreg(dest) && (dest & 1) == 0 && + rset_test((as->freeset & allow), dest+1)) { + type = dest+1; + ra_modified(as, type); + } else { + type = RID_TMP; + } + } + asm_guardcc(as, t == IRT_NUM ? CC_HS : CC_NE); + emit_n(as, ARMI_CMN|ARMI_K12|-irt_toitype_(t), type); + } + if (ra_hasreg(dest)) { +#if !LJ_SOFTFP + if (t == IRT_NUM) { + if (ofs < 1024) { + emit_vlso(as, ARMI_VLDR_D, dest, base, ofs); + } else { + if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4); + emit_vlso(as, ARMI_VLDR_D, dest, RID_TMP, 0); + emit_opk(as, ARMI_ADD, RID_TMP, base, ofs, allow); + return; + } + } else +#endif + emit_lso(as, ARMI_LDR, dest, base, ofs); + } + if (ra_hasreg(type)) emit_lso(as, ARMI_LDR, type, base, ofs+4); +} + +/* -- Allocations --------------------------------------------------------- */ + +#if LJ_HASFFI +static void asm_cnew(ASMState *as, IRIns *ir) +{ + CTState *cts = ctype_ctsG(J2G(as->J)); + CTypeID ctypeid = (CTypeID)IR(ir->op1)->i; + CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? + lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i; + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; + IRRef args[2]; + RegSet allow = (RSET_GPR & ~RSET_SCRATCH); + RegSet drop = RSET_SCRATCH; + lua_assert(sz != CTSIZE_INVALID); + + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ASMREF_TMP1; /* MSize size */ + as->gcsteps++; + + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + ra_evictset(as, drop); + if (ra_used(ir)) + ra_destreg(as, ir, RID_RET); /* GCcdata * */ + + /* Initialize immutable cdata object. */ + if (ir->o == IR_CNEWI) { + int32_t ofs = sizeof(GCcdata); + lua_assert(sz == 4 || sz == 8); + if (sz == 8) { + ofs += 4; ir++; + lua_assert(ir->o == IR_HIOP); + } + for (;;) { + Reg r = ra_alloc1(as, ir->op2, allow); + emit_lso(as, ARMI_STR, r, RID_RET, ofs); + rset_clear(allow, r); + if (ofs == sizeof(GCcdata)) break; + ofs -= 4; ir--; + } + } + /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ + { + uint32_t k = emit_isk12(ARMI_MOV, ctypeid); + Reg r = k ? RID_R1 : ra_allock(as, ctypeid, allow); + emit_lso(as, ARMI_STRB, RID_TMP, RID_RET, offsetof(GCcdata, gct)); + emit_lsox(as, ARMI_STRH, r, RID_RET, offsetof(GCcdata, ctypeid)); + emit_d(as, ARMI_MOV|ARMI_K12|~LJ_TCDATA, RID_TMP); + if (k) emit_d(as, ARMI_MOV^k, RID_R1); + } + asm_gencall(as, ci, args); + ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), + ra_releasetmp(as, ASMREF_TMP1)); +} +#else +#define asm_cnew(as, ir) ((void)0) +#endif + +/* -- Write barriers ------------------------------------------------------ */ + +static void asm_tbar(ASMState *as, IRIns *ir) +{ + Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); + Reg link = ra_scratch(as, rset_exclude(RSET_GPR, tab)); + Reg gr = ra_allock(as, i32ptr(J2G(as->J)), + rset_exclude(rset_exclude(RSET_GPR, tab), link)); + Reg mark = RID_TMP; + MCLabel l_end = emit_label(as); + emit_lso(as, ARMI_STR, link, tab, (int32_t)offsetof(GCtab, gclist)); + emit_lso(as, ARMI_STRB, mark, tab, (int32_t)offsetof(GCtab, marked)); + emit_lso(as, ARMI_STR, tab, gr, + (int32_t)offsetof(global_State, gc.grayagain)); + emit_dn(as, ARMI_BIC|ARMI_K12|LJ_GC_BLACK, mark, mark); + emit_lso(as, ARMI_LDR, link, gr, + (int32_t)offsetof(global_State, gc.grayagain)); + emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); + emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_BLACK, mark); + emit_lso(as, ARMI_LDRB, mark, tab, (int32_t)offsetof(GCtab, marked)); +} + +static void asm_obar(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; + IRRef args[2]; + MCLabel l_end; + Reg obj, val, tmp; + /* No need for other object barriers (yet). */ + lua_assert(IR(ir->op1)->o == IR_UREFC); + ra_evictset(as, RSET_SCRATCH); + l_end = emit_label(as); + args[0] = ASMREF_TMP1; /* global_State *g */ + args[1] = ir->op1; /* TValue *tv */ + asm_gencall(as, ci, args); + if ((l_end[-1] >> 28) == CC_AL) + l_end[-1] = ARMF_CC(l_end[-1], CC_NE); + else + emit_branch(as, ARMF_CC(ARMI_B, CC_EQ), l_end); + ra_allockreg(as, i32ptr(J2G(as->J)), ra_releasetmp(as, ASMREF_TMP1)); + obj = IR(ir->op1)->r; + tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); + emit_n(as, ARMF_CC(ARMI_TST, CC_NE)|ARMI_K12|LJ_GC_BLACK, tmp); + emit_n(as, ARMI_TST|ARMI_K12|LJ_GC_WHITES, RID_TMP); + val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); + emit_lso(as, ARMI_LDRB, tmp, obj, + (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); + emit_lso(as, ARMI_LDRB, RID_TMP, val, (int32_t)offsetof(GChead, marked)); +} + +/* -- Arithmetic and logic operations ------------------------------------- */ + +#if !LJ_SOFTFP +static void asm_fparith(ASMState *as, IRIns *ir, ARMIns ai) +{ + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg right, left = ra_alloc2(as, ir, RSET_FPR); + right = (left >> 8); left &= 255; + emit_dnm(as, ai, (dest & 15), (left & 15), (right & 15)); +} + +static void asm_fpunary(ASMState *as, IRIns *ir, ARMIns ai) +{ + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); + emit_dm(as, ai, (dest & 15), (left & 15)); +} + +static int asm_fpjoin_pow(ASMState *as, IRIns *ir) +{ + IRIns *irp = IR(ir->op1); + if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { + IRIns *irpp = IR(irp->op1); + if (irpp == ir-2 && irpp->o == IR_FPMATH && + irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow]; + IRRef args[2]; + args[0] = irpp->op1; + args[1] = irp->op2; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); + return 1; + } + } + return 0; +} +#endif + +static int asm_swapops(ASMState *as, IRRef lref, IRRef rref) +{ + IRIns *ir; + if (irref_isk(rref)) + return 0; /* Don't swap constants to the left. */ + if (irref_isk(lref)) + return 1; /* But swap constants to the right. */ + ir = IR(rref); + if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) || + (ir->o == IR_ADD && ir->op1 == ir->op2)) + return 0; /* Don't swap fusable operands to the left. */ + ir = IR(lref); + if ((ir->o >= IR_BSHL && ir->o <= IR_BROR) || + (ir->o == IR_ADD && ir->op1 == ir->op2)) + return 1; /* But swap fusable operands to the right. */ + return 0; /* Otherwise don't swap. */ +} + +static void asm_intop(ASMState *as, IRIns *ir, ARMIns ai) +{ + IRRef lref = ir->op1, rref = ir->op2; + Reg left, dest = ra_dest(as, ir, RSET_GPR); + uint32_t m; + if (asm_swapops(as, lref, rref)) { + IRRef tmp = lref; lref = rref; rref = tmp; + if ((ai & ~ARMI_S) == ARMI_SUB || (ai & ~ARMI_S) == ARMI_SBC) + ai ^= (ARMI_SUB^ARMI_RSB); + } + left = ra_hintalloc(as, lref, dest, RSET_GPR); + m = asm_fuseopm(as, ai, rref, rset_exclude(RSET_GPR, left)); + if (irt_isguard(ir->t)) { /* For IR_ADDOV etc. */ + asm_guardcc(as, CC_VS); + ai |= ARMI_S; + } + emit_dn(as, ai^m, dest, left); +} + +static void asm_intop_s(ASMState *as, IRIns *ir, ARMIns ai) +{ + if (as->flagmcp == as->mcp) { /* Drop cmp r, #0. */ + as->flagmcp = NULL; + as->mcp++; + ai |= ARMI_S; + } + asm_intop(as, ir, ai); +} + +static void asm_bitop(ASMState *as, IRIns *ir, ARMIns ai) +{ + if (as->flagmcp == as->mcp) { /* Try to drop cmp r, #0. */ + uint32_t cc = (as->mcp[1] >> 28); + as->flagmcp = NULL; + if (cc <= CC_NE) { + as->mcp++; + ai |= ARMI_S; + } else if (cc == CC_GE) { + *++as->mcp ^= ((CC_GE^CC_PL) << 28); + ai |= ARMI_S; + } else if (cc == CC_LT) { + *++as->mcp ^= ((CC_LT^CC_MI) << 28); + ai |= ARMI_S; + } /* else: other conds don't work with bit ops. */ + } + if (ir->op2 == 0) { + Reg dest = ra_dest(as, ir, RSET_GPR); + uint32_t m = asm_fuseopm(as, ai, ir->op1, RSET_GPR); + emit_d(as, ai^m, dest); + } else { + /* NYI: Turn BAND !k12 into uxtb, uxth or bfc or shl+shr. */ + asm_intop(as, ir, ai); + } +} + +static void asm_intneg(ASMState *as, IRIns *ir, ARMIns ai) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + emit_dn(as, ai|ARMI_K12|0, dest, left); +} + +/* NYI: use add/shift for MUL(OV) with constants. FOLD only does 2^k. */ +static void asm_intmul(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, dest)); + Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + Reg tmp = RID_NONE; + /* ARMv5 restriction: dest != left and dest_hi != left. */ + if (dest == left && left != right) { left = right; right = dest; } + if (irt_isguard(ir->t)) { /* IR_MULOV */ + if (!(as->flags & JIT_F_ARMV6) && dest == left) + tmp = left = ra_scratch(as, rset_exclude(RSET_FPR, left)); + asm_guardcc(as, CC_NE); + emit_nm(as, ARMI_TEQ|ARMF_SH(ARMSH_ASR, 31), RID_TMP, dest); + emit_dnm(as, ARMI_SMULL|ARMF_S(right), dest, RID_TMP, left); + } else { + if (!(as->flags & JIT_F_ARMV6) && dest == left) tmp = left = RID_TMP; + emit_nm(as, ARMI_MUL|ARMF_S(right), dest, left); + } + /* Only need this for the dest == left == right case. */ + if (ra_hasreg(tmp)) emit_dm(as, ARMI_MOV, tmp, right); +} + +static void asm_add(ASMState *as, IRIns *ir) +{ +#if !LJ_SOFTFP + if (irt_isnum(ir->t)) { + if (!asm_fusemadd(as, ir, ARMI_VMLA_D, ARMI_VMLA_D)) + asm_fparith(as, ir, ARMI_VADD_D); + return; + } +#endif + asm_intop_s(as, ir, ARMI_ADD); +} + +static void asm_sub(ASMState *as, IRIns *ir) +{ +#if !LJ_SOFTFP + if (irt_isnum(ir->t)) { + if (!asm_fusemadd(as, ir, ARMI_VNMLS_D, ARMI_VMLS_D)) + asm_fparith(as, ir, ARMI_VSUB_D); + return; + } +#endif + asm_intop_s(as, ir, ARMI_SUB); +} + +static void asm_mul(ASMState *as, IRIns *ir) +{ +#if !LJ_SOFTFP + if (irt_isnum(ir->t)) { + asm_fparith(as, ir, ARMI_VMUL_D); + return; + } +#endif + asm_intmul(as, ir); +} + +static void asm_neg(ASMState *as, IRIns *ir) +{ +#if !LJ_SOFTFP + if (irt_isnum(ir->t)) { + asm_fpunary(as, ir, ARMI_VNEG_D); + return; + } +#endif + asm_intneg(as, ir, ARMI_RSB); +} + +static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) +{ + const CCallInfo *ci = &lj_ir_callinfo[id]; + IRRef args[2]; + args[0] = ir->op1; + args[1] = ir->op2; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} + +#if !LJ_SOFTFP +static void asm_callround(ASMState *as, IRIns *ir, int id) +{ + /* The modified regs must match with the *.dasc implementation. */ + RegSet drop = RID2RSET(RID_R0)|RID2RSET(RID_R1)|RID2RSET(RID_R2)| + RID2RSET(RID_R3)|RID2RSET(RID_R12); + RegSet of; + Reg dest, src; + ra_evictset(as, drop); + dest = ra_dest(as, ir, RSET_FPR); + emit_dnm(as, ARMI_VMOV_D_RR, RID_RETLO, RID_RETHI, (dest & 15)); + emit_call(as, id == IRFPM_FLOOR ? (void *)lj_vm_floor_sf : + id == IRFPM_CEIL ? (void *)lj_vm_ceil_sf : + (void *)lj_vm_trunc_sf); + /* Workaround to protect argument GPRs from being used for remat. */ + of = as->freeset; + as->freeset &= ~RSET_RANGE(RID_R0, RID_R1+1); + as->cost[RID_R0] = as->cost[RID_R1] = REGCOST(~0u, ASMREF_L); + src = ra_alloc1(as, ir->op1, RSET_FPR); /* May alloc GPR to remat FPR. */ + as->freeset |= (of & RSET_RANGE(RID_R0, RID_R1+1)); + emit_dnm(as, ARMI_VMOV_RR_D, RID_R0, RID_R1, (src & 15)); +} +#endif + +static void asm_bitswap(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, RSET_GPR); + if ((as->flags & JIT_F_ARMV6)) { + emit_dm(as, ARMI_REV, dest, left); + } else { + Reg tmp2 = dest; + if (tmp2 == left) + tmp2 = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, dest), left)); + emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_LSR, 8), dest, tmp2, RID_TMP); + emit_dm(as, ARMI_MOV|ARMF_SH(ARMSH_ROR, 8), tmp2, left); + emit_dn(as, ARMI_BIC|ARMI_K12|256*8|255, RID_TMP, RID_TMP); + emit_dnm(as, ARMI_EOR|ARMF_SH(ARMSH_ROR, 16), RID_TMP, left, left); + } +} + +static void asm_bitshift(ASMState *as, IRIns *ir, ARMShift sh) +{ + if (irref_isk(ir->op2)) { /* Constant shifts. */ + /* NYI: Turn SHL+SHR or BAND+SHR into uxtb, uxth or ubfx. */ + /* NYI: Turn SHL+ASR into sxtb, sxth or sbfx. */ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, RSET_GPR); + int32_t shift = (IR(ir->op2)->i & 31); + emit_dm(as, ARMI_MOV|ARMF_SH(sh, shift), dest, left); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, RSET_GPR); + Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_dm(as, ARMI_MOV|ARMF_RSH(sh, right), dest, left); + } +} + +static void asm_intmin_max(ASMState *as, IRIns *ir, int cc) +{ + uint32_t kcmp = 0, kmov = 0; + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + Reg right = 0; + if (irref_isk(ir->op2)) { + kcmp = emit_isk12(ARMI_CMP, IR(ir->op2)->i); + if (kcmp) kmov = emit_isk12(ARMI_MOV, IR(ir->op2)->i); + } + if (!kmov) { + kcmp = 0; + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + } + if (kmov || dest != right) { + emit_dm(as, ARMF_CC(ARMI_MOV, cc)^kmov, dest, right); + cc ^= 1; /* Must use opposite conditions for paired moves. */ + } else { + cc ^= (CC_LT^CC_GT); /* Otherwise may swap CC_LT <-> CC_GT. */ + } + if (dest != left) emit_dm(as, ARMF_CC(ARMI_MOV, cc), dest, left); + emit_nm(as, ARMI_CMP^kcmp, left, right); +} + +#if LJ_SOFTFP +static void asm_sfpmin_max(ASMState *as, IRIns *ir, int cc) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; + RegSet drop = RSET_SCRATCH; + Reg r; + IRRef args[4]; + args[0] = ir->op1; args[1] = (ir+1)->op1; + args[2] = ir->op2; args[3] = (ir+1)->op2; + /* __aeabi_cdcmple preserves r0-r3. */ + if (ra_hasreg(ir->r)) rset_clear(drop, ir->r); + if (ra_hasreg((ir+1)->r)) rset_clear(drop, (ir+1)->r); + if (!rset_test(as->freeset, RID_R2) && + regcost_ref(as->cost[RID_R2]) == args[2]) rset_clear(drop, RID_R2); + if (!rset_test(as->freeset, RID_R3) && + regcost_ref(as->cost[RID_R3]) == args[3]) rset_clear(drop, RID_R3); + ra_evictset(as, drop); + ra_destpair(as, ir); + emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETHI, RID_R3); + emit_dm(as, ARMF_CC(ARMI_MOV, cc), RID_RETLO, RID_R2); + emit_call(as, (void *)ci->func); + for (r = RID_R0; r <= RID_R3; r++) + ra_leftov(as, r, args[r-RID_R0]); +} +#else +static void asm_fpmin_max(ASMState *as, IRIns *ir, int cc) +{ + Reg dest = (ra_dest(as, ir, RSET_FPR) & 15); + Reg right, left = ra_alloc2(as, ir, RSET_FPR); + right = ((left >> 8) & 15); left &= 15; + if (dest != left) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc^1), dest, left); + if (dest != right) emit_dm(as, ARMF_CC(ARMI_VMOV_D, cc), dest, right); + emit_d(as, ARMI_VMRS, 0); + emit_dm(as, ARMI_VCMP_D, left, right); +} +#endif + +static void asm_min_max(ASMState *as, IRIns *ir, int cc, int fcc) +{ +#if LJ_SOFTFP + UNUSED(fcc); +#else + if (irt_isnum(ir->t)) + asm_fpmin_max(as, ir, fcc); + else +#endif + asm_intmin_max(as, ir, cc); +} + +/* -- Comparisons --------------------------------------------------------- */ + +/* Map of comparisons to flags. ORDER IR. */ +static const uint8_t asm_compmap[IR_ABC+1] = { + /* op FP swp int cc FP cc */ + /* LT */ CC_GE + (CC_HS << 4), + /* GE x */ CC_LT + (CC_HI << 4), + /* LE */ CC_GT + (CC_HI << 4), + /* GT x */ CC_LE + (CC_HS << 4), + /* ULT x */ CC_HS + (CC_LS << 4), + /* UGE */ CC_LO + (CC_LO << 4), + /* ULE x */ CC_HI + (CC_LO << 4), + /* UGT */ CC_LS + (CC_LS << 4), + /* EQ */ CC_NE + (CC_NE << 4), + /* NE */ CC_EQ + (CC_EQ << 4), + /* ABC */ CC_LS + (CC_LS << 4) /* Same as UGT. */ +}; + +#if LJ_SOFTFP +/* FP comparisons. */ +static void asm_sfpcomp(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_softfp_cmp]; + RegSet drop = RSET_SCRATCH; + Reg r; + IRRef args[4]; + int swp = (((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1) << 1); + args[swp^0] = ir->op1; args[swp^1] = (ir+1)->op1; + args[swp^2] = ir->op2; args[swp^3] = (ir+1)->op2; + /* __aeabi_cdcmple preserves r0-r3. This helps to reduce spills. */ + for (r = RID_R0; r <= RID_R3; r++) + if (!rset_test(as->freeset, r) && + regcost_ref(as->cost[r]) == args[r-RID_R0]) rset_clear(drop, r); + ra_evictset(as, drop); + asm_guardcc(as, (asm_compmap[ir->o] >> 4)); + emit_call(as, (void *)ci->func); + for (r = RID_R0; r <= RID_R3; r++) + ra_leftov(as, r, args[r-RID_R0]); +} +#else +/* FP comparisons. */ +static void asm_fpcomp(ASMState *as, IRIns *ir) +{ + Reg left, right; + ARMIns ai; + int swp = ((ir->o ^ (ir->o >> 2)) & ~(ir->o >> 3) & 1); + if (!swp && irref_isk(ir->op2) && ir_knum(IR(ir->op2))->u64 == 0) { + left = (ra_alloc1(as, ir->op1, RSET_FPR) & 15); + right = 0; + ai = ARMI_VCMPZ_D; + } else { + left = ra_alloc2(as, ir, RSET_FPR); + if (swp) { + right = (left & 15); left = ((left >> 8) & 15); + } else { + right = ((left >> 8) & 15); left &= 15; + } + ai = ARMI_VCMP_D; + } + asm_guardcc(as, (asm_compmap[ir->o] >> 4)); + emit_d(as, ARMI_VMRS, 0); + emit_dm(as, ai, left, right); +} +#endif + +/* Integer comparisons. */ +static void asm_intcomp(ASMState *as, IRIns *ir) +{ + ARMCC cc = (asm_compmap[ir->o] & 15); + IRRef lref = ir->op1, rref = ir->op2; + Reg left; + uint32_t m; + int cmpprev0 = 0; + lua_assert(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)); + if (asm_swapops(as, lref, rref)) { + Reg tmp = lref; lref = rref; rref = tmp; + if (cc >= CC_GE) cc ^= 7; /* LT <-> GT, LE <-> GE */ + else if (cc > CC_NE) cc ^= 11; /* LO <-> HI, LS <-> HS */ + } + if (irref_isk(rref) && IR(rref)->i == 0) { + IRIns *irl = IR(lref); + cmpprev0 = (irl+1 == ir); + /* Combine comp(BAND(left, right), 0) into tst left, right. */ + if (cmpprev0 && irl->o == IR_BAND && !ra_used(irl)) { + IRRef blref = irl->op1, brref = irl->op2; + uint32_t m2 = 0; + Reg bleft; + if (asm_swapops(as, blref, brref)) { + Reg tmp = blref; blref = brref; brref = tmp; + } + if (irref_isk(brref)) { + m2 = emit_isk12(ARMI_AND, IR(brref)->i); + if ((m2 & (ARMI_AND^ARMI_BIC))) + goto notst; /* Not beneficial if we miss a constant operand. */ + } + if (cc == CC_GE) cc = CC_PL; + else if (cc == CC_LT) cc = CC_MI; + else if (cc > CC_NE) goto notst; /* Other conds don't work with tst. */ + bleft = ra_alloc1(as, blref, RSET_GPR); + if (!m2) m2 = asm_fuseopm(as, 0, brref, rset_exclude(RSET_GPR, bleft)); + asm_guardcc(as, cc); + emit_n(as, ARMI_TST^m2, bleft); + return; + } + } +notst: + left = ra_alloc1(as, lref, RSET_GPR); + m = asm_fuseopm(as, ARMI_CMP, rref, rset_exclude(RSET_GPR, left)); + asm_guardcc(as, cc); + emit_n(as, ARMI_CMP^m, left); + /* Signed comparison with zero and referencing previous ins? */ + if (cmpprev0 && (cc <= CC_NE || cc >= CC_GE)) + as->flagmcp = as->mcp; /* Allow elimination of the compare. */ +} + +/* 64 bit integer comparisons. */ +static void asm_int64comp(ASMState *as, IRIns *ir) +{ + int signedcomp = (ir->o <= IR_GT); + ARMCC cclo, cchi; + Reg leftlo, lefthi; + uint32_t mlo, mhi; + RegSet allow = RSET_GPR, oldfree; + + /* Always use unsigned comparison for loword. */ + cclo = asm_compmap[ir->o + (signedcomp ? 4 : 0)] & 15; + leftlo = ra_alloc1(as, ir->op1, allow); + oldfree = as->freeset; + mlo = asm_fuseopm(as, ARMI_CMP, ir->op2, rset_clear(allow, leftlo)); + allow &= ~(oldfree & ~as->freeset); /* Update for allocs of asm_fuseopm. */ + + /* Use signed or unsigned comparison for hiword. */ + cchi = asm_compmap[ir->o] & 15; + lefthi = ra_alloc1(as, (ir+1)->op1, allow); + mhi = asm_fuseopm(as, ARMI_CMP, (ir+1)->op2, rset_clear(allow, lefthi)); + + /* All register allocations must be performed _before_ this point. */ + if (signedcomp) { + MCLabel l_around = emit_label(as); + asm_guardcc(as, cclo); + emit_n(as, ARMI_CMP^mlo, leftlo); + emit_branch(as, ARMF_CC(ARMI_B, CC_NE), l_around); + if (cchi == CC_GE || cchi == CC_LE) cchi ^= 6; /* GE -> GT, LE -> LT */ + asm_guardcc(as, cchi); + } else { + asm_guardcc(as, cclo); + emit_n(as, ARMF_CC(ARMI_CMP, CC_EQ)^mlo, leftlo); + } + emit_n(as, ARMI_CMP^mhi, lefthi); +} + +/* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ + +/* Hiword op of a split 64 bit op. Previous op must be the loword op. */ +static void asm_hiop(ASMState *as, IRIns *ir) +{ +#if LJ_HASFFI || LJ_SOFTFP + /* HIOP is marked as a store because it needs its own DCE logic. */ + int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ + if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; + if ((ir-1)->o <= IR_NE) { /* 64 bit integer or FP comparisons. ORDER IR. */ + as->curins--; /* Always skip the loword comparison. */ +#if LJ_SOFTFP + if (!irt_isint(ir->t)) + asm_sfpcomp(as, ir-1); + else +#endif + asm_int64comp(as, ir-1); + return; +#if LJ_SOFTFP + } else if ((ir-1)->o == IR_MIN || (ir-1)->o == IR_MAX) { + as->curins--; /* Always skip the loword min/max. */ + if (uselo || usehi) + asm_sfpmin_max(as, ir-1, (ir-1)->o == IR_MIN ? CC_HI : CC_LO); + return; +#elif LJ_HASFFI + } else if ((ir-1)->o == IR_CONV) { + as->curins--; /* Always skip the CONV. */ + if (usehi || uselo) + asm_conv64(as, ir); + return; +#endif + } else if ((ir-1)->o == IR_XSTORE) { + if ((ir-1)->r != RID_SINK) + asm_xstore(as, ir, 4); + return; + } + if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ + switch ((ir-1)->o) { +#if LJ_HASFFI + case IR_ADD: + as->curins--; + asm_intop(as, ir, ARMI_ADC); + asm_intop(as, ir-1, ARMI_ADD|ARMI_S); + break; + case IR_SUB: + as->curins--; + asm_intop(as, ir, ARMI_SBC); + asm_intop(as, ir-1, ARMI_SUB|ARMI_S); + break; + case IR_NEG: + as->curins--; + asm_intneg(as, ir, ARMI_RSC); + asm_intneg(as, ir-1, ARMI_RSB|ARMI_S); + break; +#endif +#if LJ_SOFTFP + case IR_SLOAD: case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: + case IR_STRTO: + if (!uselo) + ra_allocref(as, ir->op1, RSET_GPR); /* Mark lo op as used. */ + break; +#endif + case IR_CALLN: + case IR_CALLS: + case IR_CALLXS: + if (!uselo) + ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ + break; +#if LJ_SOFTFP + case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_TOSTR: +#endif + case IR_CNEWI: + /* Nothing to do here. Handled by lo op itself. */ + break; + default: lua_assert(0); break; + } +#else + UNUSED(as); UNUSED(ir); lua_assert(0); +#endif +} + +/* -- Stack handling ------------------------------------------------------ */ + +/* Check Lua stack size for overflow. Use exit handler as fallback. */ +static void asm_stack_check(ASMState *as, BCReg topslot, + IRIns *irp, RegSet allow, ExitNo exitno) +{ + Reg pbase; + uint32_t k; + if (irp) { + if (!ra_hasspill(irp->s)) { + pbase = irp->r; + lua_assert(ra_hasreg(pbase)); + } else if (allow) { + pbase = rset_pickbot(allow); + } else { + pbase = RID_RET; + emit_lso(as, ARMI_LDR, RID_RET, RID_SP, 0); /* Restore temp. register. */ + } + } else { + pbase = RID_BASE; + } + emit_branch(as, ARMF_CC(ARMI_BL, CC_LS), exitstub_addr(as->J, exitno)); + k = emit_isk12(0, (int32_t)(8*topslot)); + lua_assert(k); + emit_n(as, ARMI_CMP^k, RID_TMP); + emit_dnm(as, ARMI_SUB, RID_TMP, RID_TMP, pbase); + emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, + (int32_t)offsetof(lua_State, maxstack)); + if (irp) { /* Must not spill arbitrary registers in head of side trace. */ + int32_t i = i32ptr(&J2G(as->J)->jit_L); + if (ra_hasspill(irp->s)) + emit_lso(as, ARMI_LDR, pbase, RID_SP, sps_scale(irp->s)); + emit_lso(as, ARMI_LDR, RID_TMP, RID_TMP, (i & 4095)); + if (ra_hasspill(irp->s) && !allow) + emit_lso(as, ARMI_STR, RID_RET, RID_SP, 0); /* Save temp. register. */ + emit_loadi(as, RID_TMP, (i & ~4095)); + } else { + emit_getgl(as, RID_TMP, jit_L); + } +} + +/* Restore Lua stack from on-trace state. */ +static void asm_stack_restore(ASMState *as, SnapShot *snap) +{ + SnapEntry *map = &as->T->snapmap[snap->mapofs]; + SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; + MSize n, nent = snap->nent; + /* Store the value of all modified slots to the Lua stack. */ + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + BCReg s = snap_slot(sn); + int32_t ofs = 8*((int32_t)s-1); + IRRef ref = snap_ref(sn); + IRIns *ir = IR(ref); + if ((sn & SNAP_NORESTORE)) + continue; + if (irt_isnum(ir->t)) { +#if LJ_SOFTFP + RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE); + Reg tmp; + lua_assert(irref_isk(ref)); /* LJ_SOFTFP: must be a number constant. */ + tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.lo, + rset_exclude(RSET_GPREVEN, RID_BASE)); + emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs); + if (rset_test(as->freeset, tmp+1)) odd = RID2RSET(tmp+1); + tmp = ra_allock(as, (int32_t)ir_knum(ir)->u32.hi, odd); + emit_lso(as, ARMI_STR, tmp, RID_BASE, ofs+4); +#else + Reg src = ra_alloc1(as, ref, RSET_FPR); + emit_vlso(as, ARMI_VSTR_D, src, RID_BASE, ofs); +#endif + } else { + RegSet odd = rset_exclude(RSET_GPRODD, RID_BASE); + Reg type; + lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t)); + if (!irt_ispri(ir->t)) { + Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPREVEN, RID_BASE)); + emit_lso(as, ARMI_STR, src, RID_BASE, ofs); + if (rset_test(as->freeset, src+1)) odd = RID2RSET(src+1); + } + if ((sn & (SNAP_CONT|SNAP_FRAME))) { + if (s == 0) continue; /* Do not overwrite link to previous frame. */ + type = ra_allock(as, (int32_t)(*flinks--), odd); +#if LJ_SOFTFP + } else if ((sn & SNAP_SOFTFPNUM)) { + type = ra_alloc1(as, ref+1, rset_exclude(RSET_GPRODD, RID_BASE)); +#endif + } else { + type = ra_allock(as, (int32_t)irt_toitype(ir->t), odd); + } + emit_lso(as, ARMI_STR, type, RID_BASE, ofs+4); + } + checkmclim(as); + } + lua_assert(map + nent == flinks); +} + +/* -- GC handling --------------------------------------------------------- */ + +/* Check GC threshold and do one or more GC steps. */ +static void asm_gc_check(ASMState *as) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; + IRRef args[2]; + MCLabel l_end; + Reg tmp1, tmp2; + ra_evictset(as, RSET_SCRATCH); + l_end = emit_label(as); + /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ + asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ + emit_n(as, ARMI_CMP|ARMI_K12|0, RID_RET); + args[0] = ASMREF_TMP1; /* global_State *g */ + args[1] = ASMREF_TMP2; /* MSize steps */ + asm_gencall(as, ci, args); + tmp1 = ra_releasetmp(as, ASMREF_TMP1); + tmp2 = ra_releasetmp(as, ASMREF_TMP2); + emit_loadi(as, tmp2, as->gcsteps); + /* Jump around GC step if GC total < GC threshold. */ + emit_branch(as, ARMF_CC(ARMI_B, CC_LS), l_end); + emit_nm(as, ARMI_CMP, RID_TMP, tmp2); + emit_lso(as, ARMI_LDR, tmp2, tmp1, + (int32_t)offsetof(global_State, gc.threshold)); + emit_lso(as, ARMI_LDR, RID_TMP, tmp1, + (int32_t)offsetof(global_State, gc.total)); + ra_allockreg(as, i32ptr(J2G(as->J)), tmp1); + as->gcsteps = 0; + checkmclim(as); +} + +/* -- Loop handling ------------------------------------------------------- */ + +/* Fixup the loop branch. */ +static void asm_loop_fixup(ASMState *as) +{ + MCode *p = as->mctop; + MCode *target = as->mcp; + if (as->loopinv) { /* Inverted loop branch? */ + /* asm_guardcc already inverted the bcc and patched the final bl. */ + p[-2] |= ((uint32_t)(target-p) & 0x00ffffffu); + } else { + p[-1] = ARMI_B | ((uint32_t)((target-p)-1) & 0x00ffffffu); + } +} + +/* -- Head of trace ------------------------------------------------------- */ + +/* Reload L register from g->jit_L. */ +static void asm_head_lreg(ASMState *as) +{ + IRIns *ir = IR(ASMREF_L); + if (ra_used(ir)) { + Reg r = ra_dest(as, ir, RSET_GPR); + emit_getgl(as, r, jit_L); + ra_evictk(as); + } +} + +/* Coalesce BASE register for a root trace. */ +static void asm_head_root_base(ASMState *as) +{ + IRIns *ir; + asm_head_lreg(as); + ir = IR(REF_BASE); + if (ra_hasreg(ir->r) && rset_test(as->modset, ir->r)) ra_spill(as, ir); + ra_destreg(as, ir, RID_BASE); +} + +/* Coalesce BASE register for a side trace. */ +static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) +{ + IRIns *ir; + asm_head_lreg(as); + ir = IR(REF_BASE); + if (ra_hasreg(ir->r) && rset_test(as->modset, ir->r)) ra_spill(as, ir); + if (ra_hasspill(irp->s)) { + rset_clear(allow, ra_dest(as, ir, allow)); + } else { + Reg r = irp->r; + lua_assert(ra_hasreg(r)); + rset_clear(allow, r); + if (r != ir->r && !rset_test(as->freeset, r)) + ra_restore(as, regcost_ref(as->cost[r])); + ra_destreg(as, ir, r); + } + return allow; +} + +/* -- Tail of trace ------------------------------------------------------- */ + +/* Fixup the tail code. */ +static void asm_tail_fixup(ASMState *as, TraceNo lnk) +{ + MCode *p = as->mctop; + MCode *target; + int32_t spadj = as->T->spadjust; + if (spadj == 0) { + as->mctop = --p; + } else { + /* Patch stack adjustment. */ + uint32_t k = emit_isk12(ARMI_ADD, spadj); + lua_assert(k); + p[-2] = (ARMI_ADD^k) | ARMF_D(RID_SP) | ARMF_N(RID_SP); + } + /* Patch exit branch. */ + target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; + p[-1] = ARMI_B|(((target-p)-1)&0x00ffffffu); +} + +/* Prepare tail of code. */ +static void asm_tail_prep(ASMState *as) +{ + MCode *p = as->mctop - 1; /* Leave room for exit branch. */ + if (as->loopref) { + as->invmcp = as->mcp = p; + } else { + as->mcp = p-1; /* Leave room for stack pointer adjustment. */ + as->invmcp = NULL; + } + *p = 0; /* Prevent load/store merging. */ +} + +/* -- Instruction dispatch ------------------------------------------------ */ + +/* Assemble a single instruction. */ +static void asm_ir(ASMState *as, IRIns *ir) +{ + switch ((IROp)ir->o) { + /* Miscellaneous ops. */ + case IR_LOOP: asm_loop(as); break; + case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; + case IR_USE: + ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; + case IR_PHI: asm_phi(as, ir); break; + case IR_HIOP: asm_hiop(as, ir); break; + case IR_GCSTEP: asm_gcstep(as, ir); break; + + /* Guarded assertions. */ + case IR_EQ: case IR_NE: + if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) { + as->curins--; + asm_href(as, ir-1, (IROp)ir->o); + break; + } + /* fallthrough */ + case IR_LT: case IR_GE: case IR_LE: case IR_GT: + case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: + case IR_ABC: +#if !LJ_SOFTFP + if (irt_isnum(ir->t)) { asm_fpcomp(as, ir); break; } +#endif + asm_intcomp(as, ir); + break; + + case IR_RETF: asm_retf(as, ir); break; + + /* Bit ops. */ + case IR_BNOT: asm_bitop(as, ir, ARMI_MVN); break; + case IR_BSWAP: asm_bitswap(as, ir); break; + + case IR_BAND: asm_bitop(as, ir, ARMI_AND); break; + case IR_BOR: asm_bitop(as, ir, ARMI_ORR); break; + case IR_BXOR: asm_bitop(as, ir, ARMI_EOR); break; + + case IR_BSHL: asm_bitshift(as, ir, ARMSH_LSL); break; + case IR_BSHR: asm_bitshift(as, ir, ARMSH_LSR); break; + case IR_BSAR: asm_bitshift(as, ir, ARMSH_ASR); break; + case IR_BROR: asm_bitshift(as, ir, ARMSH_ROR); break; + case IR_BROL: lua_assert(0); break; + + /* Arithmetic ops. */ + case IR_ADD: case IR_ADDOV: asm_add(as, ir); break; + case IR_SUB: case IR_SUBOV: asm_sub(as, ir); break; + case IR_MUL: case IR_MULOV: asm_mul(as, ir); break; + case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break; + case IR_NEG: asm_neg(as, ir); break; + +#if LJ_SOFTFP + case IR_DIV: case IR_POW: case IR_ABS: + case IR_ATAN2: case IR_LDEXP: case IR_FPMATH: case IR_TOBIT: + lua_assert(0); /* Unused for LJ_SOFTFP. */ + break; +#else + case IR_DIV: asm_fparith(as, ir, ARMI_VDIV_D); break; + case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break; + case IR_ABS: asm_fpunary(as, ir, ARMI_VABS_D); break; + case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break; + case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break; + case IR_FPMATH: + if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir)) + break; + if (ir->op2 <= IRFPM_TRUNC) + asm_callround(as, ir, ir->op2); + else if (ir->op2 == IRFPM_SQRT) + asm_fpunary(as, ir, ARMI_VSQRT_D); + else + asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); + break; + case IR_TOBIT: asm_tobit(as, ir); break; +#endif + + case IR_MIN: asm_min_max(as, ir, CC_GT, CC_HI); break; + case IR_MAX: asm_min_max(as, ir, CC_LT, CC_LO); break; + + /* Memory references. */ + case IR_AREF: asm_aref(as, ir); break; + case IR_HREF: asm_href(as, ir, 0); break; + case IR_HREFK: asm_hrefk(as, ir); break; + case IR_NEWREF: asm_newref(as, ir); break; + case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; + case IR_FREF: asm_fref(as, ir); break; + case IR_STRREF: asm_strref(as, ir); break; + + /* Loads and stores. */ + case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: + asm_ahuvload(as, ir); + break; + case IR_FLOAD: asm_fload(as, ir); break; + case IR_XLOAD: asm_xload(as, ir); break; + case IR_SLOAD: asm_sload(as, ir); break; + + case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; + case IR_FSTORE: asm_fstore(as, ir); break; + case IR_XSTORE: asm_xstore(as, ir, 0); break; + + /* Allocations. */ + case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; + case IR_TNEW: asm_tnew(as, ir); break; + case IR_TDUP: asm_tdup(as, ir); break; + case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; + + /* Write barriers. */ + case IR_TBAR: asm_tbar(as, ir); break; + case IR_OBAR: asm_obar(as, ir); break; + + /* Type conversions. */ + case IR_CONV: asm_conv(as, ir); break; + case IR_TOSTR: asm_tostr(as, ir); break; + case IR_STRTO: asm_strto(as, ir); break; + + /* Calls. */ + case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; + case IR_CALLXS: asm_callx(as, ir); break; + case IR_CARG: break; + + default: + setintV(&as->J->errinfo, ir->o); + lj_trace_err_info(as->J, LJ_TRERR_NYIIR); + break; + } +} + +/* -- Trace setup --------------------------------------------------------- */ + +/* Ensure there are enough stack slots for call arguments. */ +static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) +{ + IRRef args[CCI_NARGS_MAX]; + uint32_t i, nargs = (int)CCI_NARGS(ci); + int nslots = 0, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR, fprodd = 0; + asm_collectargs(as, ir, ci, args); + for (i = 0; i < nargs; i++) { + if (!LJ_SOFTFP && args[i] && irt_isfp(IR(args[i])->t)) { + if (!LJ_ABI_SOFTFP && !(ci->flags & CCI_VARARG)) { + if (irt_isnum(IR(args[i])->t)) { + if (nfpr > 0) nfpr--; + else fprodd = 0, nslots = (nslots + 3) & ~1; + } else { + if (fprodd) fprodd--; + else if (nfpr > 0) fprodd = 1, nfpr--; + else nslots++; + } + } else if (irt_isnum(IR(args[i])->t)) { + ngpr &= ~1; + if (ngpr > 0) ngpr -= 2; else nslots += 2; + } else { + if (ngpr > 0) ngpr--; else nslots++; + } + } else { + if (ngpr > 0) ngpr--; else nslots++; + } + } + if (nslots > as->evenspill) /* Leave room for args in stack slots. */ + as->evenspill = nslots; + return REGSP_HINT(RID_RET); +} + +static void asm_setup_target(ASMState *as) +{ + /* May need extra exit for asm_stack_check on side traces. */ + asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0)); +} + +/* -- Trace patching ------------------------------------------------------ */ + +/* Patch exit jumps of existing machine code to a new target. */ +void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) +{ + MCode *p = T->mcode; + MCode *pe = (MCode *)((char *)p + T->szmcode); + MCode *cstart = NULL, *cend = p; + MCode *mcarea = lj_mcode_patch(J, p, 0); + MCode *px = exitstub_addr(J, exitno) - 2; + for (; p < pe; p++) { + /* Look for bl_cc exitstub, replace with b_cc target. */ + uint32_t ins = *p; + if ((ins & 0x0f000000u) == 0x0b000000u && ins < 0xf0000000u && + ((ins ^ (px-p)) & 0x00ffffffu) == 0) { + *p = (ins & 0xfe000000u) | (((target-p)-2) & 0x00ffffffu); + cend = p+1; + if (!cstart) cstart = p; + } + } + lua_assert(cstart != NULL); + lj_mcode_sync(cstart, cend); + lj_mcode_patch(J, mcarea, 1); +} + diff --git a/external/lua/luajit/src/src/lj_asm_mips.h b/external/lua/luajit/src/src/lj_asm_mips.h new file mode 100644 index 0000000000..e80f758239 --- /dev/null +++ b/external/lua/luajit/src/src/lj_asm_mips.h @@ -0,0 +1,1975 @@ +/* +** MIPS IR assembler (SSA IR -> machine code). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* -- Register allocator extensions --------------------------------------- */ + +/* Allocate a register with a hint. */ +static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) +{ + Reg r = IR(ref)->r; + if (ra_noreg(r)) { + if (!ra_hashint(r) && !iscrossref(as, ref)) + ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ + r = ra_allocref(as, ref, allow); + } + ra_noweak(as, r); + return r; +} + +/* Allocate a register or RID_ZERO. */ +static Reg ra_alloc1z(ASMState *as, IRRef ref, RegSet allow) +{ + Reg r = IR(ref)->r; + if (ra_noreg(r)) { + if (!(allow & RSET_FPR) && irref_isk(ref) && IR(ref)->i == 0) + return RID_ZERO; + r = ra_allocref(as, ref, allow); + } else { + ra_noweak(as, r); + } + return r; +} + +/* Allocate two source registers for three-operand instructions. */ +static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) +{ + IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); + Reg left = irl->r, right = irr->r; + if (ra_hasreg(left)) { + ra_noweak(as, left); + if (ra_noreg(right)) + right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left)); + else + ra_noweak(as, right); + } else if (ra_hasreg(right)) { + ra_noweak(as, right); + left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right)); + } else if (ra_hashint(right)) { + right = ra_alloc1z(as, ir->op2, allow); + left = ra_alloc1z(as, ir->op1, rset_exclude(allow, right)); + } else { + left = ra_alloc1z(as, ir->op1, allow); + right = ra_alloc1z(as, ir->op2, rset_exclude(allow, left)); + } + return left | (right << 8); +} + +/* -- Guard handling ------------------------------------------------------ */ + +/* Need some spare long-range jump slots, for out-of-range branches. */ +#define MIPS_SPAREJUMP 4 + +/* Setup spare long-range jump slots per mcarea. */ +static void asm_sparejump_setup(ASMState *as) +{ + MCode *mxp = as->mcbot; + /* Assumes sizeof(MCLink) == 8. */ + if (((uintptr_t)mxp & (LJ_PAGESIZE-1)) == 8) { + lua_assert(MIPSI_NOP == 0); + memset(mxp+2, 0, MIPS_SPAREJUMP*8); + mxp += MIPS_SPAREJUMP*2; + lua_assert(mxp < as->mctop); + lj_mcode_sync(as->mcbot, mxp); + lj_mcode_commitbot(as->J, mxp); + as->mcbot = mxp; + as->mclim = as->mcbot + MCLIM_REDZONE; + } +} + +/* Setup exit stub after the end of each trace. */ +static void asm_exitstub_setup(ASMState *as) +{ + MCode *mxp = as->mctop; + /* sw TMP, 0(sp); j ->vm_exit_handler; li TMP, traceno */ + *--mxp = MIPSI_LI|MIPSF_T(RID_TMP)|as->T->traceno; + *--mxp = MIPSI_J|((((uintptr_t)(void *)lj_vm_exit_handler)>>2)&0x03ffffffu); + lua_assert(((uintptr_t)mxp ^ (uintptr_t)(void *)lj_vm_exit_handler)>>28 == 0); + *--mxp = MIPSI_SW|MIPSF_T(RID_TMP)|MIPSF_S(RID_SP)|0; + as->mctop = mxp; +} + +/* Keep this in-sync with exitstub_trace_addr(). */ +#define asm_exitstub_addr(as) ((as)->mctop) + +/* Emit conditional branch to exit for guard. */ +static void asm_guard(ASMState *as, MIPSIns mi, Reg rs, Reg rt) +{ + MCode *target = asm_exitstub_addr(as); + MCode *p = as->mcp; + if (LJ_UNLIKELY(p == as->invmcp)) { + as->invmcp = NULL; + as->loopinv = 1; + as->mcp = p+1; + mi = mi ^ ((mi>>28) == 1 ? 0x04000000u : 0x00010000u); /* Invert cond. */ + target = p; /* Patch target later in asm_loop_fixup. */ + } + emit_ti(as, MIPSI_LI, RID_TMP, as->snapno); + emit_branch(as, mi, rs, rt, target); +} + +/* -- Operand fusion ------------------------------------------------------ */ + +/* Limit linear search to this distance. Avoids O(n^2) behavior. */ +#define CONFLICT_SEARCH_LIM 31 + +/* Check if there's no conflicting instruction between curins and ref. */ +static int noconflict(ASMState *as, IRRef ref, IROp conflict) +{ + IRIns *ir = as->ir; + IRRef i = as->curins; + if (i > ref + CONFLICT_SEARCH_LIM) + return 0; /* Give up, ref is too far away. */ + while (--i > ref) + if (ir[i].o == conflict) + return 0; /* Conflict found. */ + return 1; /* Ok, no conflict. */ +} + +/* Fuse the array base of colocated arrays. */ +static int32_t asm_fuseabase(ASMState *as, IRRef ref) +{ + IRIns *ir = IR(ref); + if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && + !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) + return (int32_t)sizeof(GCtab); + return 0; +} + +/* Fuse array/hash/upvalue reference into register+offset operand. */ +static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow) +{ + IRIns *ir = IR(ref); + if (ra_noreg(ir->r)) { + if (ir->o == IR_AREF) { + if (mayfuse(as, ref)) { + if (irref_isk(ir->op2)) { + IRRef tab = IR(ir->op1)->op1; + int32_t ofs = asm_fuseabase(as, tab); + IRRef refa = ofs ? tab : ir->op1; + ofs += 8*IR(ir->op2)->i; + if (checki16(ofs)) { + *ofsp = ofs; + return ra_alloc1(as, refa, allow); + } + } + } + } else if (ir->o == IR_HREFK) { + if (mayfuse(as, ref)) { + int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); + if (checki16(ofs)) { + *ofsp = ofs; + return ra_alloc1(as, ir->op1, allow); + } + } + } else if (ir->o == IR_UREFC) { + if (irref_isk(ir->op1)) { + GCfunc *fn = ir_kfunc(IR(ir->op1)); + int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); + int32_t jgl = (intptr_t)J2G(as->J); + if ((uint32_t)(ofs-jgl) < 65536) { + *ofsp = ofs-jgl-32768; + return RID_JGL; + } else { + *ofsp = (int16_t)ofs; + return ra_allock(as, ofs-(int16_t)ofs, allow); + } + } + } + } + *ofsp = 0; + return ra_alloc1(as, ref, allow); +} + +/* Fuse XLOAD/XSTORE reference into load/store operand. */ +static void asm_fusexref(ASMState *as, MIPSIns mi, Reg rt, IRRef ref, + RegSet allow, int32_t ofs) +{ + IRIns *ir = IR(ref); + Reg base; + if (ra_noreg(ir->r) && canfuse(as, ir)) { + if (ir->o == IR_ADD) { + int32_t ofs2; + if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) { + ref = ir->op1; + ofs = ofs2; + } + } else if (ir->o == IR_STRREF) { + int32_t ofs2 = 65536; + lua_assert(ofs == 0); + ofs = (int32_t)sizeof(GCstr); + if (irref_isk(ir->op2)) { + ofs2 = ofs + IR(ir->op2)->i; + ref = ir->op1; + } else if (irref_isk(ir->op1)) { + ofs2 = ofs + IR(ir->op1)->i; + ref = ir->op2; + } + if (!checki16(ofs2)) { + /* NYI: Fuse ADD with constant. */ + Reg right, left = ra_alloc2(as, ir, allow); + right = (left >> 8); left &= 255; + emit_hsi(as, mi, rt, RID_TMP, ofs); + emit_dst(as, MIPSI_ADDU, RID_TMP, left, right); + return; + } + ofs = ofs2; + } + } + base = ra_alloc1(as, ref, allow); + emit_hsi(as, mi, rt, base, ofs); +} + +/* -- Calls --------------------------------------------------------------- */ + +/* Generate a call to a C function. */ +static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) +{ + uint32_t n, nargs = CCI_NARGS(ci); + int32_t ofs = 16; + Reg gpr, fpr = REGARG_FIRSTFPR; + if ((void *)ci->func) + emit_call(as, (void *)ci->func); + for (gpr = REGARG_FIRSTGPR; gpr <= REGARG_LASTGPR; gpr++) + as->cost[gpr] = REGCOST(~0u, ASMREF_L); + gpr = REGARG_FIRSTGPR; + for (n = 0; n < nargs; n++) { /* Setup args. */ + IRRef ref = args[n]; + if (ref) { + IRIns *ir = IR(ref); + if (irt_isfp(ir->t) && fpr <= REGARG_LASTFPR && + !(ci->flags & CCI_VARARG)) { + lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */ + ra_leftov(as, fpr, ref); + fpr += 2; + gpr += irt_isnum(ir->t) ? 2 : 1; + } else { + fpr = REGARG_LASTFPR+1; + if (irt_isnum(ir->t)) gpr = (gpr+1) & ~1; + if (gpr <= REGARG_LASTGPR) { + lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */ + if (irt_isfp(ir->t)) { + RegSet of = as->freeset; + Reg r; + /* Workaround to protect argument GPRs from being used for remat. */ + as->freeset &= ~RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1); + r = ra_alloc1(as, ref, RSET_FPR); + as->freeset |= (of & RSET_RANGE(REGARG_FIRSTGPR, REGARG_LASTGPR+1)); + if (irt_isnum(ir->t)) { + emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?0:1), r+1); + emit_tg(as, MIPSI_MFC1, gpr+(LJ_BE?1:0), r); + lua_assert(rset_test(as->freeset, gpr+1)); /* Already evicted. */ + gpr += 2; + } else if (irt_isfloat(ir->t)) { + emit_tg(as, MIPSI_MFC1, gpr, r); + gpr++; + } + } else { + ra_leftov(as, gpr, ref); + gpr++; + } + } else { + Reg r = ra_alloc1z(as, ref, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); + if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; + emit_spstore(as, ir, r, ofs); + ofs += irt_isnum(ir->t) ? 8 : 4; + } + } + } else { + fpr = REGARG_LASTFPR+1; + if (gpr <= REGARG_LASTGPR) + gpr++; + else + ofs += 4; + } + } +} + +/* Setup result reg/sp for call. Evict scratch regs. */ +static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) +{ + RegSet drop = RSET_SCRATCH; + int hiop = ((ir+1)->o == IR_HIOP); + if ((ci->flags & CCI_NOFPRCLOBBER)) + drop &= ~RSET_FPR; + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + if (hiop && ra_hasreg((ir+1)->r)) + rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ + ra_evictset(as, drop); /* Evictions must be performed first. */ + if (ra_used(ir)) { + lua_assert(!irt_ispri(ir->t)); + if (irt_isfp(ir->t)) { + if ((ci->flags & CCI_CASTU64)) { + int32_t ofs = sps_scale(ir->s); + Reg dest = ir->r; + if (ra_hasreg(dest)) { + ra_free(as, dest); + ra_modified(as, dest); + emit_tg(as, MIPSI_MTC1, RID_RETHI, dest+1); + emit_tg(as, MIPSI_MTC1, RID_RETLO, dest); + } + if (ofs) { + emit_tsi(as, MIPSI_SW, RID_RETLO, RID_SP, ofs+(LJ_BE?4:0)); + emit_tsi(as, MIPSI_SW, RID_RETHI, RID_SP, ofs+(LJ_BE?0:4)); + } + } else { + ra_destreg(as, ir, RID_FPRET); + } + } else if (hiop) { + ra_destpair(as, ir); + } else { + ra_destreg(as, ir, RID_RET); + } + } +} + +static void asm_call(ASMState *as, IRIns *ir) +{ + IRRef args[CCI_NARGS_MAX]; + const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; + asm_collectargs(as, ir, ci, args); + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} + +static void asm_callx(ASMState *as, IRIns *ir) +{ + IRRef args[CCI_NARGS_MAX]; + CCallInfo ci; + IRRef func; + IRIns *irf; + ci.flags = asm_callx_flags(as, ir); + asm_collectargs(as, ir, &ci, args); + asm_setupresult(as, ir, &ci); + func = ir->op2; irf = IR(func); + if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } + if (irref_isk(func)) { /* Call to constant address. */ + ci.func = (ASMFunction)(void *)(irf->i); + } else { /* Need specific register for indirect calls. */ + Reg r = ra_alloc1(as, func, RID2RSET(RID_CFUNCADDR)); + MCode *p = as->mcp; + if (r == RID_CFUNCADDR) + *--p = MIPSI_NOP; + else + *--p = MIPSI_MOVE | MIPSF_D(RID_CFUNCADDR) | MIPSF_S(r); + *--p = MIPSI_JALR | MIPSF_S(r); + as->mcp = p; + ci.func = (ASMFunction)(void *)0; + } + asm_gencall(as, &ci, args); +} + +static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) +{ + const CCallInfo *ci = &lj_ir_callinfo[id]; + IRRef args[2]; + args[0] = ir->op1; + args[1] = ir->op2; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} + +static void asm_callround(ASMState *as, IRIns *ir, IRCallID id) +{ + /* The modified regs must match with the *.dasc implementation. */ + RegSet drop = RID2RSET(RID_R1)|RID2RSET(RID_R12)|RID2RSET(RID_FPRET)| + RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(REGARG_FIRSTFPR); + if (ra_hasreg(ir->r)) rset_clear(drop, ir->r); + ra_evictset(as, drop); + ra_destreg(as, ir, RID_FPRET); + emit_call(as, (void *)lj_ir_callinfo[id].func); + ra_leftov(as, REGARG_FIRSTFPR, ir->op1); +} + +/* -- Returns ------------------------------------------------------------- */ + +/* Return to lower frame. Guard that it goes to the right spot. */ +static void asm_retf(ASMState *as, IRIns *ir) +{ + Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); + void *pc = ir_kptr(IR(ir->op2)); + int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); + as->topslot -= (BCReg)delta; + if ((int32_t)as->topslot < 0) as->topslot = 0; + emit_setgl(as, base, jit_base); + emit_addptr(as, base, -8*delta); + asm_guard(as, MIPSI_BNE, RID_TMP, + ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); + emit_tsi(as, MIPSI_LW, RID_TMP, base, -8); +} + +/* -- Type conversions ---------------------------------------------------- */ + +static void asm_tointg(ASMState *as, IRIns *ir, Reg left) +{ + Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); + Reg dest = ra_dest(as, ir, RSET_GPR); + asm_guard(as, MIPSI_BC1F, 0, 0); + emit_fgh(as, MIPSI_C_EQ_D, 0, tmp, left); + emit_fg(as, MIPSI_CVT_D_W, tmp, tmp); + emit_tg(as, MIPSI_MFC1, dest, tmp); + emit_fg(as, MIPSI_CVT_W_D, tmp, left); +} + +static void asm_tobit(ASMState *as, IRIns *ir) +{ + RegSet allow = RSET_FPR; + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, allow); + Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); + Reg tmp = ra_scratch(as, rset_clear(allow, right)); + emit_tg(as, MIPSI_MFC1, dest, tmp); + emit_fgh(as, MIPSI_ADD_D, tmp, left, right); +} + +static void asm_conv(ASMState *as, IRIns *ir) +{ + IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); + int stfp = (st == IRT_NUM || st == IRT_FLOAT); + IRRef lref = ir->op1; + lua_assert(irt_type(ir->t) != st); + lua_assert(!(irt_isint64(ir->t) || + (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */ + if (irt_isfp(ir->t)) { + Reg dest = ra_dest(as, ir, RSET_FPR); + if (stfp) { /* FP to FP conversion. */ + emit_fg(as, st == IRT_NUM ? MIPSI_CVT_S_D : MIPSI_CVT_D_S, + dest, ra_alloc1(as, lref, RSET_FPR)); + } else if (st == IRT_U32) { /* U32 to FP conversion. */ + /* y = (x ^ 0x8000000) + 2147483648.0 */ + Reg left = ra_alloc1(as, lref, RSET_GPR); + Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, dest)); + emit_fgh(as, irt_isfloat(ir->t) ? MIPSI_ADD_S : MIPSI_ADD_D, + dest, dest, tmp); + emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W, + dest, dest); + if (irt_isfloat(ir->t)) + emit_lsptr(as, MIPSI_LWC1, (tmp & 31), + (void *)lj_ir_k64_find(as->J, U64x(4f000000,4f000000)), + RSET_GPR); + else + emit_lsptr(as, MIPSI_LDC1, (tmp & 31), + (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)), + RSET_GPR); + emit_tg(as, MIPSI_MTC1, RID_TMP, dest); + emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, left); + emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000); + } else { /* Integer to FP conversion. */ + Reg left = ra_alloc1(as, lref, RSET_GPR); + emit_fg(as, irt_isfloat(ir->t) ? MIPSI_CVT_S_W : MIPSI_CVT_D_W, + dest, dest); + emit_tg(as, MIPSI_MTC1, left, dest); + } + } else if (stfp) { /* FP to integer conversion. */ + if (irt_isguard(ir->t)) { + /* Checked conversions are only supported from number to int. */ + lua_assert(irt_isint(ir->t) && st == IRT_NUM); + asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, lref, RSET_FPR); + Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); + if (irt_isu32(ir->t)) { + /* y = (int)floor(x - 2147483648.0) ^ 0x80000000 */ + emit_dst(as, MIPSI_XOR, dest, dest, RID_TMP); + emit_ti(as, MIPSI_LUI, RID_TMP, 0x8000); + emit_tg(as, MIPSI_MFC1, dest, tmp); + emit_fg(as, st == IRT_FLOAT ? MIPSI_FLOOR_W_S : MIPSI_FLOOR_W_D, + tmp, tmp); + emit_fgh(as, st == IRT_FLOAT ? MIPSI_SUB_S : MIPSI_SUB_D, + tmp, left, tmp); + if (st == IRT_FLOAT) + emit_lsptr(as, MIPSI_LWC1, (tmp & 31), + (void *)lj_ir_k64_find(as->J, U64x(4f000000,4f000000)), + RSET_GPR); + else + emit_lsptr(as, MIPSI_LDC1, (tmp & 31), + (void *)lj_ir_k64_find(as->J, U64x(41e00000,00000000)), + RSET_GPR); + } else { + emit_tg(as, MIPSI_MFC1, dest, tmp); + emit_fg(as, st == IRT_FLOAT ? MIPSI_TRUNC_W_S : MIPSI_TRUNC_W_D, + tmp, left); + } + } + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ + Reg left = ra_alloc1(as, ir->op1, RSET_GPR); + lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); + if ((ir->op2 & IRCONV_SEXT)) { + if ((as->flags & JIT_F_MIPS32R2)) { + emit_dst(as, st == IRT_I8 ? MIPSI_SEB : MIPSI_SEH, dest, 0, left); + } else { + uint32_t shift = st == IRT_I8 ? 24 : 16; + emit_dta(as, MIPSI_SRA, dest, dest, shift); + emit_dta(as, MIPSI_SLL, dest, left, shift); + } + } else { + emit_tsi(as, MIPSI_ANDI, dest, left, + (int32_t)(st == IRT_U8 ? 0xff : 0xffff)); + } + } else { /* 32/64 bit integer conversions. */ + /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */ + ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ + } + } +} + +#if LJ_HASFFI +static void asm_conv64(ASMState *as, IRIns *ir) +{ + IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); + IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); + IRCallID id; + const CCallInfo *ci; + IRRef args[2]; + args[LJ_BE?0:1] = ir->op1; + args[LJ_BE?1:0] = (ir-1)->op1; + if (st == IRT_NUM || st == IRT_FLOAT) { + id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); + ir--; + } else { + id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); + } + ci = &lj_ir_callinfo[id]; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} +#endif + +static void asm_strto(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; + IRRef args[2]; + RegSet drop = RSET_SCRATCH; + if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */ + ra_evictset(as, drop); + asm_guard(as, MIPSI_BEQ, RID_RET, RID_ZERO); /* Test return status. */ + args[0] = ir->op1; /* GCstr *str */ + args[1] = ASMREF_TMP1; /* TValue *n */ + asm_gencall(as, ci, args); + /* Store the result to the spill slot or temp slots. */ + emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), + RID_SP, sps_scale(ir->s)); +} + +/* Get pointer to TValue. */ +static void asm_tvptr(ASMState *as, Reg dest, IRRef ref) +{ + IRIns *ir = IR(ref); + if (irt_isnum(ir->t)) { + if (irref_isk(ref)) /* Use the number constant itself as a TValue. */ + ra_allockreg(as, i32ptr(ir_knum(ir)), dest); + else /* Otherwise force a spill and use the spill slot. */ + emit_tsi(as, MIPSI_ADDIU, dest, RID_SP, ra_spill(as, ir)); + } else { + /* Otherwise use g->tmptv to hold the TValue. */ + RegSet allow = rset_exclude(RSET_GPR, dest); + Reg type; + emit_tsi(as, MIPSI_ADDIU, dest, RID_JGL, offsetof(global_State, tmptv)-32768); + if (!irt_ispri(ir->t)) { + Reg src = ra_alloc1(as, ref, allow); + emit_setgl(as, src, tmptv.gcr); + } + type = ra_allock(as, irt_toitype(ir->t), allow); + emit_setgl(as, type, tmptv.it); + } +} + +static void asm_tostr(ASMState *as, IRIns *ir) +{ + IRRef args[2]; + args[0] = ASMREF_L; + as->gcsteps++; + if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; + args[1] = ASMREF_TMP1; /* const lua_Number * */ + asm_setupresult(as, ir, ci); /* GCstr * */ + asm_gencall(as, ci, args); + asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1); + } else { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; + args[1] = ir->op1; /* int32_t k */ + asm_setupresult(as, ir, ci); /* GCstr * */ + asm_gencall(as, ci, args); + } +} + +/* -- Memory references --------------------------------------------------- */ + +static void asm_aref(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg idx, base; + if (irref_isk(ir->op2)) { + IRRef tab = IR(ir->op1)->op1; + int32_t ofs = asm_fuseabase(as, tab); + IRRef refa = ofs ? tab : ir->op1; + ofs += 8*IR(ir->op2)->i; + if (checki16(ofs)) { + base = ra_alloc1(as, refa, RSET_GPR); + emit_tsi(as, MIPSI_ADDIU, dest, base, ofs); + return; + } + } + base = ra_alloc1(as, ir->op1, RSET_GPR); + idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); + emit_dst(as, MIPSI_ADDU, dest, RID_TMP, base); + emit_dta(as, MIPSI_SLL, RID_TMP, idx, 3); +} + +/* Inlined hash lookup. Specialized for key type and for const keys. +** The equivalent C code is: +** Node *n = hashkey(t, key); +** do { +** if (lj_obj_equal(&n->key, key)) return &n->val; +** } while ((n = nextnode(n))); +** return niltv(L); +*/ +static void asm_href(ASMState *as, IRIns *ir) +{ + RegSet allow = RSET_GPR; + int destused = ra_used(ir); + Reg dest = ra_dest(as, ir, allow); + Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); + Reg key = RID_NONE, type = RID_NONE, tmpnum = RID_NONE, tmp1 = RID_TMP, tmp2; + IRRef refkey = ir->op2; + IRIns *irkey = IR(refkey); + IRType1 kt = irkey->t; + uint32_t khash; + MCLabel l_end, l_loop, l_next; + + rset_clear(allow, tab); + if (irt_isnum(kt)) { + key = ra_alloc1(as, refkey, RSET_FPR); + tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key)); + } else if (!irt_ispri(kt)) { + key = ra_alloc1(as, refkey, allow); + rset_clear(allow, key); + type = ra_allock(as, irt_toitype(irkey->t), allow); + rset_clear(allow, type); + } + tmp2 = ra_scratch(as, allow); + rset_clear(allow, tmp2); + + /* Key not found in chain: load niltv. */ + l_end = emit_label(as); + if (destused) + emit_loada(as, dest, niltvg(J2G(as->J))); + else + *--as->mcp = MIPSI_NOP; + /* Follow hash chain until the end. */ + emit_move(as, dest, tmp1); + l_loop = --as->mcp; + emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, next)); + l_next = emit_label(as); + + /* Type and value comparison. */ + if (irt_isnum(kt)) { + emit_branch(as, MIPSI_BC1T, 0, 0, l_end); + emit_fgh(as, MIPSI_C_EQ_D, 0, tmpnum, key); + emit_tg(as, MIPSI_MFC1, tmp1, key+1); + emit_branch(as, MIPSI_BEQ, tmp1, RID_ZERO, l_next); + emit_tsi(as, MIPSI_SLTIU, tmp1, tmp1, (int32_t)LJ_TISNUM); + emit_hsi(as, MIPSI_LDC1, tmpnum, dest, (int32_t)offsetof(Node, key.n)); + } else { + if (irt_ispri(kt)) { + emit_branch(as, MIPSI_BEQ, tmp1, type, l_end); + } else { + emit_branch(as, MIPSI_BEQ, tmp2, key, l_end); + emit_tsi(as, MIPSI_LW, tmp2, dest, (int32_t)offsetof(Node, key.gcr)); + emit_branch(as, MIPSI_BNE, tmp1, type, l_next); + } + } + emit_tsi(as, MIPSI_LW, tmp1, dest, (int32_t)offsetof(Node, key.it)); + *l_loop = MIPSI_BNE | MIPSF_S(tmp1) | ((as->mcp-l_loop-1) & 0xffffu); + + /* Load main position relative to tab->node into dest. */ + khash = irref_isk(refkey) ? ir_khash(irkey) : 1; + if (khash == 0) { + emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node)); + } else { + Reg tmphash = tmp1; + if (irref_isk(refkey)) + tmphash = ra_allock(as, khash, allow); + emit_dst(as, MIPSI_ADDU, dest, dest, tmp1); + lua_assert(sizeof(Node) == 24); + emit_dst(as, MIPSI_SUBU, tmp1, tmp2, tmp1); + emit_dta(as, MIPSI_SLL, tmp1, tmp1, 3); + emit_dta(as, MIPSI_SLL, tmp2, tmp1, 5); + emit_dst(as, MIPSI_AND, tmp1, tmp2, tmphash); + emit_tsi(as, MIPSI_LW, dest, tab, (int32_t)offsetof(GCtab, node)); + emit_tsi(as, MIPSI_LW, tmp2, tab, (int32_t)offsetof(GCtab, hmask)); + if (irref_isk(refkey)) { + /* Nothing to do. */ + } else if (irt_isstr(kt)) { + emit_tsi(as, MIPSI_LW, tmp1, key, (int32_t)offsetof(GCstr, hash)); + } else { /* Must match with hash*() in lj_tab.c. */ + emit_dst(as, MIPSI_SUBU, tmp1, tmp1, tmp2); + emit_rotr(as, tmp2, tmp2, dest, (-HASH_ROT3)&31); + emit_dst(as, MIPSI_XOR, tmp1, tmp1, tmp2); + emit_rotr(as, tmp1, tmp1, dest, (-HASH_ROT2-HASH_ROT1)&31); + emit_dst(as, MIPSI_SUBU, tmp2, tmp2, dest); + if (irt_isnum(kt)) { + emit_dst(as, MIPSI_XOR, tmp2, tmp2, tmp1); + if ((as->flags & JIT_F_MIPS32R2)) { + emit_dta(as, MIPSI_ROTR, dest, tmp1, (-HASH_ROT1)&31); + } else { + emit_dst(as, MIPSI_OR, dest, dest, tmp1); + emit_dta(as, MIPSI_SLL, tmp1, tmp1, HASH_ROT1); + emit_dta(as, MIPSI_SRL, dest, tmp1, (-HASH_ROT1)&31); + } + emit_dst(as, MIPSI_ADDU, tmp1, tmp1, tmp1); + emit_tg(as, MIPSI_MFC1, tmp2, key); + emit_tg(as, MIPSI_MFC1, tmp1, key+1); + } else { + emit_dst(as, MIPSI_XOR, tmp2, key, tmp1); + emit_rotr(as, dest, tmp1, tmp2, (-HASH_ROT1)&31); + emit_dst(as, MIPSI_ADDU, tmp1, key, ra_allock(as, HASH_BIAS, allow)); + } + } + } +} + +static void asm_hrefk(ASMState *as, IRIns *ir) +{ + IRIns *kslot = IR(ir->op2); + IRIns *irkey = IR(kslot->op1); + int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); + int32_t kofs = ofs + (int32_t)offsetof(Node, key); + Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; + Reg node = ra_alloc1(as, ir->op1, RSET_GPR); + Reg key = RID_NONE, type = RID_TMP, idx = node; + RegSet allow = rset_exclude(RSET_GPR, node); + int32_t lo, hi; + lua_assert(ofs % sizeof(Node) == 0); + if (ofs > 32736) { + idx = dest; + rset_clear(allow, dest); + kofs = (int32_t)offsetof(Node, key); + } else if (ra_hasreg(dest)) { + emit_tsi(as, MIPSI_ADDIU, dest, node, ofs); + } + if (!irt_ispri(irkey->t)) { + key = ra_scratch(as, allow); + rset_clear(allow, key); + } + if (irt_isnum(irkey->t)) { + lo = (int32_t)ir_knum(irkey)->u32.lo; + hi = (int32_t)ir_knum(irkey)->u32.hi; + } else { + lo = irkey->i; + hi = irt_toitype(irkey->t); + if (!ra_hasreg(key)) + goto nolo; + } + asm_guard(as, MIPSI_BNE, key, lo ? ra_allock(as, lo, allow) : RID_ZERO); +nolo: + asm_guard(as, MIPSI_BNE, type, hi ? ra_allock(as, hi, allow) : RID_ZERO); + if (ra_hasreg(key)) emit_tsi(as, MIPSI_LW, key, idx, kofs+(LJ_BE?4:0)); + emit_tsi(as, MIPSI_LW, type, idx, kofs+(LJ_BE?0:4)); + if (ofs > 32736) + emit_tsi(as, MIPSI_ADDU, dest, node, ra_allock(as, ofs, allow)); +} + +static void asm_newref(ASMState *as, IRIns *ir) +{ + if (ir->r != RID_SINK) { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; + IRRef args[3]; + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ir->op1; /* GCtab *t */ + args[2] = ASMREF_TMP1; /* cTValue *key */ + asm_setupresult(as, ir, ci); /* TValue * */ + asm_gencall(as, ci, args); + asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2); + } +} + +static void asm_uref(ASMState *as, IRIns *ir) +{ + /* NYI: Check that UREFO is still open and not aliasing a slot. */ + Reg dest = ra_dest(as, ir, RSET_GPR); + if (irref_isk(ir->op1)) { + GCfunc *fn = ir_kfunc(IR(ir->op1)); + MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; + emit_lsptr(as, MIPSI_LW, dest, v, RSET_GPR); + } else { + Reg uv = ra_scratch(as, RSET_GPR); + Reg func = ra_alloc1(as, ir->op1, RSET_GPR); + if (ir->o == IR_UREFC) { + asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); + emit_tsi(as, MIPSI_ADDIU, dest, uv, (int32_t)offsetof(GCupval, tv)); + emit_tsi(as, MIPSI_LBU, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); + } else { + emit_tsi(as, MIPSI_LW, dest, uv, (int32_t)offsetof(GCupval, v)); + } + emit_tsi(as, MIPSI_LW, uv, func, + (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); + } +} + +static void asm_fref(ASMState *as, IRIns *ir) +{ + UNUSED(as); UNUSED(ir); + lua_assert(!ra_used(ir)); +} + +static void asm_strref(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + IRRef ref = ir->op2, refk = ir->op1; + int32_t ofs = (int32_t)sizeof(GCstr); + Reg r; + if (irref_isk(ref)) { + IRRef tmp = refk; refk = ref; ref = tmp; + } else if (!irref_isk(refk)) { + Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); + IRIns *irr = IR(ir->op2); + if (ra_hasreg(irr->r)) { + ra_noweak(as, irr->r); + right = irr->r; + } else if (mayfuse(as, irr->op2) && + irr->o == IR_ADD && irref_isk(irr->op2) && + checki16(ofs + IR(irr->op2)->i)) { + ofs += IR(irr->op2)->i; + right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); + } else { + right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); + } + emit_tsi(as, MIPSI_ADDIU, dest, dest, ofs); + emit_dst(as, MIPSI_ADDU, dest, left, right); + return; + } + r = ra_alloc1(as, ref, RSET_GPR); + ofs += IR(refk)->i; + if (checki16(ofs)) + emit_tsi(as, MIPSI_ADDIU, dest, r, ofs); + else + emit_dst(as, MIPSI_ADDU, dest, r, + ra_allock(as, ofs, rset_exclude(RSET_GPR, r))); +} + +/* -- Loads and stores ---------------------------------------------------- */ + +static MIPSIns asm_fxloadins(IRIns *ir) +{ + switch (irt_type(ir->t)) { + case IRT_I8: return MIPSI_LB; + case IRT_U8: return MIPSI_LBU; + case IRT_I16: return MIPSI_LH; + case IRT_U16: return MIPSI_LHU; + case IRT_NUM: return MIPSI_LDC1; + case IRT_FLOAT: return MIPSI_LWC1; + default: return MIPSI_LW; + } +} + +static MIPSIns asm_fxstoreins(IRIns *ir) +{ + switch (irt_type(ir->t)) { + case IRT_I8: case IRT_U8: return MIPSI_SB; + case IRT_I16: case IRT_U16: return MIPSI_SH; + case IRT_NUM: return MIPSI_SDC1; + case IRT_FLOAT: return MIPSI_SWC1; + default: return MIPSI_SW; + } +} + +static void asm_fload(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg idx = ra_alloc1(as, ir->op1, RSET_GPR); + MIPSIns mi = asm_fxloadins(ir); + int32_t ofs; + if (ir->op2 == IRFL_TAB_ARRAY) { + ofs = asm_fuseabase(as, ir->op1); + if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ + emit_tsi(as, MIPSI_ADDIU, dest, idx, ofs); + return; + } + } + ofs = field_ofs[ir->op2]; + lua_assert(!irt_isfp(ir->t)); + emit_tsi(as, mi, dest, idx, ofs); +} + +static void asm_fstore(ASMState *as, IRIns *ir) +{ + if (ir->r != RID_SINK) { + Reg src = ra_alloc1z(as, ir->op2, RSET_GPR); + IRIns *irf = IR(ir->op1); + Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); + int32_t ofs = field_ofs[irf->op2]; + MIPSIns mi = asm_fxstoreins(ir); + lua_assert(!irt_isfp(ir->t)); + emit_tsi(as, mi, src, idx, ofs); + } +} + +static void asm_xload(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); + lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED)); + asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0); +} + +static void asm_xstore(ASMState *as, IRIns *ir, int32_t ofs) +{ + if (ir->r != RID_SINK) { + Reg src = ra_alloc1z(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); + asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1, + rset_exclude(RSET_GPR, src), ofs); + } +} + +static void asm_ahuvload(ASMState *as, IRIns *ir) +{ + IRType1 t = ir->t; + Reg dest = RID_NONE, type = RID_TMP, idx; + RegSet allow = RSET_GPR; + int32_t ofs = 0; + if (ra_used(ir)) { + lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); + dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); + rset_clear(allow, dest); + } + idx = asm_fuseahuref(as, ir->op1, &ofs, allow); + rset_clear(allow, idx); + if (irt_isnum(t)) { + asm_guard(as, MIPSI_BEQ, type, RID_ZERO); + emit_tsi(as, MIPSI_SLTIU, type, type, (int32_t)LJ_TISNUM); + if (ra_hasreg(dest)) + emit_hsi(as, MIPSI_LDC1, dest, idx, ofs); + } else { + asm_guard(as, MIPSI_BNE, type, ra_allock(as, irt_toitype(t), allow)); + if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, idx, ofs+(LJ_BE?4:0)); + } + emit_tsi(as, MIPSI_LW, type, idx, ofs+(LJ_BE?0:4)); +} + +static void asm_ahustore(ASMState *as, IRIns *ir) +{ + RegSet allow = RSET_GPR; + Reg idx, src = RID_NONE, type = RID_NONE; + int32_t ofs = 0; + if (ir->r == RID_SINK) + return; + if (irt_isnum(ir->t)) { + src = ra_alloc1(as, ir->op2, RSET_FPR); + } else { + if (!irt_ispri(ir->t)) { + src = ra_alloc1(as, ir->op2, allow); + rset_clear(allow, src); + } + type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); + rset_clear(allow, type); + } + idx = asm_fuseahuref(as, ir->op1, &ofs, allow); + if (irt_isnum(ir->t)) { + emit_hsi(as, MIPSI_SDC1, src, idx, ofs); + } else { + if (ra_hasreg(src)) + emit_tsi(as, MIPSI_SW, src, idx, ofs+(LJ_BE?4:0)); + emit_tsi(as, MIPSI_SW, type, idx, ofs+(LJ_BE?0:4)); + } +} + +static void asm_sload(ASMState *as, IRIns *ir) +{ + int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0); + IRType1 t = ir->t; + Reg dest = RID_NONE, type = RID_NONE, base; + RegSet allow = RSET_GPR; + lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ + lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK)); + lua_assert(!irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME))); + if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { + dest = ra_scratch(as, RSET_FPR); + asm_tointg(as, ir, dest); + t.irt = IRT_NUM; /* Continue with a regular number type check. */ + } else if (ra_used(ir)) { + lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); + dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); + rset_clear(allow, dest); + base = ra_alloc1(as, REF_BASE, allow); + rset_clear(allow, base); + if ((ir->op2 & IRSLOAD_CONVERT)) { + if (irt_isint(t)) { + Reg tmp = ra_scratch(as, RSET_FPR); + emit_tg(as, MIPSI_MFC1, dest, tmp); + emit_fg(as, MIPSI_CVT_W_D, tmp, tmp); + dest = tmp; + t.irt = IRT_NUM; /* Check for original type. */ + } else { + Reg tmp = ra_scratch(as, RSET_GPR); + emit_fg(as, MIPSI_CVT_D_W, dest, dest); + emit_tg(as, MIPSI_MTC1, tmp, dest); + dest = tmp; + t.irt = IRT_INT; /* Check for original type. */ + } + } + goto dotypecheck; + } + base = ra_alloc1(as, REF_BASE, allow); + rset_clear(allow, base); +dotypecheck: + if (irt_isnum(t)) { + if ((ir->op2 & IRSLOAD_TYPECHECK)) { + asm_guard(as, MIPSI_BEQ, RID_TMP, RID_ZERO); + emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)LJ_TISNUM); + type = RID_TMP; + } + if (ra_hasreg(dest)) emit_hsi(as, MIPSI_LDC1, dest, base, ofs); + } else { + if ((ir->op2 & IRSLOAD_TYPECHECK)) { + Reg ktype = ra_allock(as, irt_toitype(t), allow); + asm_guard(as, MIPSI_BNE, RID_TMP, ktype); + type = RID_TMP; + } + if (ra_hasreg(dest)) emit_tsi(as, MIPSI_LW, dest, base, ofs ^ (LJ_BE?4:0)); + } + if (ra_hasreg(type)) emit_tsi(as, MIPSI_LW, type, base, ofs ^ (LJ_BE?0:4)); +} + +/* -- Allocations --------------------------------------------------------- */ + +#if LJ_HASFFI +static void asm_cnew(ASMState *as, IRIns *ir) +{ + CTState *cts = ctype_ctsG(J2G(as->J)); + CTypeID ctypeid = (CTypeID)IR(ir->op1)->i; + CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? + lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i; + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; + IRRef args[2]; + RegSet allow = (RSET_GPR & ~RSET_SCRATCH); + RegSet drop = RSET_SCRATCH; + lua_assert(sz != CTSIZE_INVALID); + + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ASMREF_TMP1; /* MSize size */ + as->gcsteps++; + + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + ra_evictset(as, drop); + if (ra_used(ir)) + ra_destreg(as, ir, RID_RET); /* GCcdata * */ + + /* Initialize immutable cdata object. */ + if (ir->o == IR_CNEWI) { + int32_t ofs = sizeof(GCcdata); + lua_assert(sz == 4 || sz == 8); + if (sz == 8) { + ofs += 4; + lua_assert((ir+1)->o == IR_HIOP); + if (LJ_LE) ir++; + } + for (;;) { + Reg r = ra_alloc1z(as, ir->op2, allow); + emit_tsi(as, MIPSI_SW, r, RID_RET, ofs); + rset_clear(allow, r); + if (ofs == sizeof(GCcdata)) break; + ofs -= 4; if (LJ_BE) ir++; else ir--; + } + } + /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ + emit_tsi(as, MIPSI_SB, RID_RET+1, RID_RET, offsetof(GCcdata, gct)); + emit_tsi(as, MIPSI_SH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid)); + emit_ti(as, MIPSI_LI, RID_RET+1, ~LJ_TCDATA); + emit_ti(as, MIPSI_LI, RID_TMP, ctypeid); /* Lower 16 bit used. Sign-ext ok. */ + asm_gencall(as, ci, args); + ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), + ra_releasetmp(as, ASMREF_TMP1)); +} +#else +#define asm_cnew(as, ir) ((void)0) +#endif + +/* -- Write barriers ------------------------------------------------------ */ + +static void asm_tbar(ASMState *as, IRIns *ir) +{ + Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); + Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab)); + Reg link = RID_TMP; + MCLabel l_end = emit_label(as); + emit_tsi(as, MIPSI_SW, link, tab, (int32_t)offsetof(GCtab, gclist)); + emit_tsi(as, MIPSI_SB, mark, tab, (int32_t)offsetof(GCtab, marked)); + emit_setgl(as, tab, gc.grayagain); + emit_getgl(as, link, gc.grayagain); + emit_dst(as, MIPSI_XOR, mark, mark, RID_TMP); /* Clear black bit. */ + emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); + emit_tsi(as, MIPSI_ANDI, RID_TMP, mark, LJ_GC_BLACK); + emit_tsi(as, MIPSI_LBU, mark, tab, (int32_t)offsetof(GCtab, marked)); +} + +static void asm_obar(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; + IRRef args[2]; + MCLabel l_end; + Reg obj, val, tmp; + /* No need for other object barriers (yet). */ + lua_assert(IR(ir->op1)->o == IR_UREFC); + ra_evictset(as, RSET_SCRATCH); + l_end = emit_label(as); + args[0] = ASMREF_TMP1; /* global_State *g */ + args[1] = ir->op1; /* TValue *tv */ + asm_gencall(as, ci, args); + emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); + obj = IR(ir->op1)->r; + tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); + emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); + emit_tsi(as, MIPSI_ANDI, tmp, tmp, LJ_GC_BLACK); + emit_branch(as, MIPSI_BEQ, RID_TMP, RID_ZERO, l_end); + emit_tsi(as, MIPSI_ANDI, RID_TMP, RID_TMP, LJ_GC_WHITES); + val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); + emit_tsi(as, MIPSI_LBU, tmp, obj, + (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); + emit_tsi(as, MIPSI_LBU, RID_TMP, val, (int32_t)offsetof(GChead, marked)); +} + +/* -- Arithmetic and logic operations ------------------------------------- */ + +static void asm_fparith(ASMState *as, IRIns *ir, MIPSIns mi) +{ + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg right, left = ra_alloc2(as, ir, RSET_FPR); + right = (left >> 8); left &= 255; + emit_fgh(as, mi, dest, left, right); +} + +static void asm_fpunary(ASMState *as, IRIns *ir, MIPSIns mi) +{ + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); + emit_fg(as, mi, dest, left); +} + +static int asm_fpjoin_pow(ASMState *as, IRIns *ir) +{ + IRIns *irp = IR(ir->op1); + if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { + IRIns *irpp = IR(irp->op1); + if (irpp == ir-2 && irpp->o == IR_FPMATH && + irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow]; + IRRef args[2]; + args[0] = irpp->op1; + args[1] = irp->op2; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); + return 1; + } + } + return 0; +} + +static void asm_add(ASMState *as, IRIns *ir) +{ + if (irt_isnum(ir->t)) { + asm_fparith(as, ir, MIPSI_ADD_D); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (checki16(k)) { + emit_tsi(as, MIPSI_ADDIU, dest, left, k); + return; + } + } + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_dst(as, MIPSI_ADDU, dest, left, right); + } +} + +static void asm_sub(ASMState *as, IRIns *ir) +{ + if (irt_isnum(ir->t)) { + asm_fparith(as, ir, MIPSI_SUB_D); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + emit_dst(as, MIPSI_SUBU, dest, left, right); + } +} + +static void asm_mul(ASMState *as, IRIns *ir) +{ + if (irt_isnum(ir->t)) { + asm_fparith(as, ir, MIPSI_MUL_D); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + emit_dst(as, MIPSI_MUL, dest, left, right); + } +} + +static void asm_neg(ASMState *as, IRIns *ir) +{ + if (irt_isnum(ir->t)) { + asm_fpunary(as, ir, MIPSI_NEG_D); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left); + } +} + +static void asm_arithov(ASMState *as, IRIns *ir) +{ + Reg right, left, tmp, dest = ra_dest(as, ir, RSET_GPR); + if (irref_isk(ir->op2)) { + int k = IR(ir->op2)->i; + if (ir->o == IR_SUBOV) k = -k; + if (checki16(k)) { /* (dest < left) == (k >= 0 ? 1 : 0) */ + left = ra_alloc1(as, ir->op1, RSET_GPR); + asm_guard(as, k >= 0 ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); + emit_dst(as, MIPSI_SLT, RID_TMP, dest, dest == left ? RID_TMP : left); + emit_tsi(as, MIPSI_ADDIU, dest, left, k); + if (dest == left) emit_move(as, RID_TMP, left); + return; + } + } + left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, left), + right), dest)); + asm_guard(as, MIPSI_BLTZ, RID_TMP, 0); + emit_dst(as, MIPSI_AND, RID_TMP, RID_TMP, tmp); + if (ir->o == IR_ADDOV) { /* ((dest^left) & (dest^right)) < 0 */ + emit_dst(as, MIPSI_XOR, RID_TMP, dest, dest == right ? RID_TMP : right); + } else { /* ((dest^left) & (dest^~right)) < 0 */ + emit_dst(as, MIPSI_XOR, RID_TMP, RID_TMP, dest); + emit_dst(as, MIPSI_NOR, RID_TMP, dest == right ? RID_TMP : right, RID_ZERO); + } + emit_dst(as, MIPSI_XOR, tmp, dest, dest == left ? RID_TMP : left); + emit_dst(as, ir->o == IR_ADDOV ? MIPSI_ADDU : MIPSI_SUBU, dest, left, right); + if (dest == left || dest == right) + emit_move(as, RID_TMP, dest == left ? left : right); +} + +static void asm_mulov(ASMState *as, IRIns *ir) +{ +#if LJ_DUALNUM +#error "NYI: MULOV" +#else + UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused in single-number mode. */ +#endif +} + +#if LJ_HASFFI +static void asm_add64(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (k == 0) { + emit_dst(as, MIPSI_ADDU, dest, left, RID_TMP); + goto loarith; + } else if (checki16(k)) { + emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP); + emit_tsi(as, MIPSI_ADDIU, dest, left, k); + goto loarith; + } + } + emit_dst(as, MIPSI_ADDU, dest, dest, RID_TMP); + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_dst(as, MIPSI_ADDU, dest, left, right); +loarith: + ir--; + dest = ra_dest(as, ir, RSET_GPR); + left = ra_alloc1(as, ir->op1, RSET_GPR); + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (k == 0) { + if (dest != left) + emit_move(as, dest, left); + return; + } else if (checki16(k)) { + if (dest == left) { + Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, left)); + emit_move(as, dest, tmp); + dest = tmp; + } + emit_dst(as, MIPSI_SLTU, RID_TMP, dest, left); + emit_tsi(as, MIPSI_ADDIU, dest, left, k); + return; + } + } + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + if (dest == left && dest == right) { + Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right)); + emit_move(as, dest, tmp); + dest = tmp; + } + emit_dst(as, MIPSI_SLTU, RID_TMP, dest, dest == left ? right : left); + emit_dst(as, MIPSI_ADDU, dest, left, right); +} + +static void asm_sub64(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP); + emit_dst(as, MIPSI_SUBU, dest, left, right); + ir--; + dest = ra_dest(as, ir, RSET_GPR); + left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + if (dest == left) { + Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right)); + emit_move(as, dest, tmp); + dest = tmp; + } + emit_dst(as, MIPSI_SLTU, RID_TMP, left, dest); + emit_dst(as, MIPSI_SUBU, dest, left, right); +} + +static void asm_neg64(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, RSET_GPR); + emit_dst(as, MIPSI_SUBU, dest, dest, RID_TMP); + emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left); + ir--; + dest = ra_dest(as, ir, RSET_GPR); + left = ra_alloc1(as, ir->op1, RSET_GPR); + emit_dst(as, MIPSI_SLTU, RID_TMP, RID_ZERO, dest); + emit_dst(as, MIPSI_SUBU, dest, RID_ZERO, left); +} +#endif + +static void asm_bitnot(ASMState *as, IRIns *ir) +{ + Reg left, right, dest = ra_dest(as, ir, RSET_GPR); + IRIns *irl = IR(ir->op1); + if (mayfuse(as, ir->op1) && irl->o == IR_BOR) { + left = ra_alloc2(as, irl, RSET_GPR); + right = (left >> 8); left &= 255; + } else { + left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + right = RID_ZERO; + } + emit_dst(as, MIPSI_NOR, dest, left, right); +} + +static void asm_bitswap(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, RSET_GPR); + if ((as->flags & JIT_F_MIPS32R2)) { + emit_dta(as, MIPSI_ROTR, dest, RID_TMP, 16); + emit_dst(as, MIPSI_WSBH, RID_TMP, 0, left); + } else { + Reg tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), dest)); + emit_dst(as, MIPSI_OR, dest, dest, tmp); + emit_dst(as, MIPSI_OR, dest, dest, RID_TMP); + emit_tsi(as, MIPSI_ANDI, dest, dest, 0xff00); + emit_dta(as, MIPSI_SLL, RID_TMP, RID_TMP, 8); + emit_dta(as, MIPSI_SRL, dest, left, 8); + emit_tsi(as, MIPSI_ANDI, RID_TMP, left, 0xff00); + emit_dst(as, MIPSI_OR, tmp, tmp, RID_TMP); + emit_dta(as, MIPSI_SRL, tmp, left, 24); + emit_dta(as, MIPSI_SLL, RID_TMP, left, 24); + } +} + +static void asm_bitop(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (checku16(k)) { + emit_tsi(as, mik, dest, left, k); + return; + } + } + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_dst(as, mi, dest, left, right); +} + +static void asm_bitshift(ASMState *as, IRIns *ir, MIPSIns mi, MIPSIns mik) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + if (irref_isk(ir->op2)) { /* Constant shifts. */ + uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31); + emit_dta(as, mik, dest, ra_hintalloc(as, ir->op1, dest, RSET_GPR), shift); + } else { + Reg right, left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + emit_dst(as, mi, dest, right, left); /* Shift amount is in rs. */ + } +} + +static void asm_bitror(ASMState *as, IRIns *ir) +{ + if ((as->flags & JIT_F_MIPS32R2)) { + asm_bitshift(as, ir, MIPSI_ROTRV, MIPSI_ROTR); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + if (irref_isk(ir->op2)) { /* Constant shifts. */ + uint32_t shift = (uint32_t)(IR(ir->op2)->i & 31); + Reg left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + emit_rotr(as, dest, left, RID_TMP, shift); + } else { + Reg right, left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + emit_dst(as, MIPSI_OR, dest, dest, RID_TMP); + emit_dst(as, MIPSI_SRLV, dest, right, left); + emit_dst(as, MIPSI_SLLV, RID_TMP, RID_TMP, left); + emit_dst(as, MIPSI_SUBU, RID_TMP, ra_allock(as, 32, RSET_GPR), right); + } + } +} + +static void asm_min_max(ASMState *as, IRIns *ir, int ismax) +{ + if (irt_isnum(ir->t)) { + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg right, left = ra_alloc2(as, ir, RSET_FPR); + right = (left >> 8); left &= 255; + if (dest == left) { + emit_fg(as, MIPSI_MOVT_D, dest, right); + } else { + emit_fg(as, MIPSI_MOVF_D, dest, left); + if (dest != right) emit_fg(as, MIPSI_MOV_D, dest, right); + } + emit_fgh(as, MIPSI_C_OLT_D, 0, ismax ? left : right, ismax ? right : left); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + if (dest == left) { + emit_dst(as, MIPSI_MOVN, dest, right, RID_TMP); + } else { + emit_dst(as, MIPSI_MOVZ, dest, left, RID_TMP); + if (dest != right) emit_move(as, dest, right); + } + emit_dst(as, MIPSI_SLT, RID_TMP, + ismax ? left : right, ismax ? right : left); + } +} + +/* -- Comparisons --------------------------------------------------------- */ + +static void asm_comp(ASMState *as, IRIns *ir) +{ + /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */ + IROp op = ir->o; + if (irt_isnum(ir->t)) { + Reg right, left = ra_alloc2(as, ir, RSET_FPR); + right = (left >> 8); left &= 255; + asm_guard(as, (op&1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0); + emit_fgh(as, MIPSI_C_OLT_D + ((op&3) ^ ((op>>2)&1)), 0, left, right); + } else { + Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); + if (op == IR_ABC) op = IR_UGT; + if ((op&4) == 0 && irref_isk(ir->op2) && IR(ir->op2)->i == 0) { + MIPSIns mi = (op&2) ? ((op&1) ? MIPSI_BLEZ : MIPSI_BGTZ) : + ((op&1) ? MIPSI_BLTZ : MIPSI_BGEZ); + asm_guard(as, mi, left, 0); + } else { + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if ((op&2)) k++; + if (checki16(k)) { + asm_guard(as, (op&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); + emit_tsi(as, (op&4) ? MIPSI_SLTIU : MIPSI_SLTI, + RID_TMP, left, k); + return; + } + } + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); + emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT, + RID_TMP, (op&2) ? right : left, (op&2) ? left : right); + } + } +} + +static void asm_compeq(ASMState *as, IRIns *ir) +{ + Reg right, left = ra_alloc2(as, ir, irt_isnum(ir->t) ? RSET_FPR : RSET_GPR); + right = (left >> 8); left &= 255; + if (irt_isnum(ir->t)) { + asm_guard(as, (ir->o & 1) ? MIPSI_BC1T : MIPSI_BC1F, 0, 0); + emit_fgh(as, MIPSI_C_EQ_D, 0, left, right); + } else { + asm_guard(as, (ir->o & 1) ? MIPSI_BEQ : MIPSI_BNE, left, right); + } +} + +#if LJ_HASFFI +/* 64 bit integer comparisons. */ +static void asm_comp64(ASMState *as, IRIns *ir) +{ + /* ORDER IR: LT GE LE GT ULT UGE ULE UGT. */ + IROp op = (ir-1)->o; + MCLabel l_end; + Reg rightlo, leftlo, righthi, lefthi = ra_alloc2(as, ir, RSET_GPR); + righthi = (lefthi >> 8); lefthi &= 255; + leftlo = ra_alloc2(as, ir-1, + rset_exclude(rset_exclude(RSET_GPR, lefthi), righthi)); + rightlo = (leftlo >> 8); leftlo &= 255; + asm_guard(as, ((op^(op>>1))&1) ? MIPSI_BNE : MIPSI_BEQ, RID_TMP, RID_ZERO); + l_end = emit_label(as); + if (lefthi != righthi) + emit_dst(as, (op&4) ? MIPSI_SLTU : MIPSI_SLT, RID_TMP, + (op&2) ? righthi : lefthi, (op&2) ? lefthi : righthi); + emit_dst(as, MIPSI_SLTU, RID_TMP, + (op&2) ? rightlo : leftlo, (op&2) ? leftlo : rightlo); + if (lefthi != righthi) + emit_branch(as, MIPSI_BEQ, lefthi, righthi, l_end); +} + +static void asm_comp64eq(ASMState *as, IRIns *ir) +{ + Reg tmp, right, left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + asm_guard(as, ((ir-1)->o & 1) ? MIPSI_BEQ : MIPSI_BNE, RID_TMP, RID_ZERO); + tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, left), right)); + emit_dst(as, MIPSI_OR, RID_TMP, RID_TMP, tmp); + emit_dst(as, MIPSI_XOR, tmp, left, right); + left = ra_alloc2(as, ir-1, RSET_GPR); + right = (left >> 8); left &= 255; + emit_dst(as, MIPSI_XOR, RID_TMP, left, right); +} +#endif + +/* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ + +/* Hiword op of a split 64 bit op. Previous op must be the loword op. */ +static void asm_hiop(ASMState *as, IRIns *ir) +{ +#if LJ_HASFFI + /* HIOP is marked as a store because it needs its own DCE logic. */ + int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ + if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; + if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ + as->curins--; /* Always skip the CONV. */ + if (usehi || uselo) + asm_conv64(as, ir); + return; + } else if ((ir-1)->o < IR_EQ) { /* 64 bit integer comparisons. ORDER IR. */ + as->curins--; /* Always skip the loword comparison. */ + asm_comp64(as, ir); + return; + } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ + as->curins--; /* Always skip the loword comparison. */ + asm_comp64eq(as, ir); + return; + } else if ((ir-1)->o == IR_XSTORE) { + as->curins--; /* Handle both stores here. */ + if ((ir-1)->r != RID_SINK) { + asm_xstore(as, ir, LJ_LE ? 4 : 0); + asm_xstore(as, ir-1, LJ_LE ? 0 : 4); + } + return; + } + if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ + switch ((ir-1)->o) { + case IR_ADD: as->curins--; asm_add64(as, ir); break; + case IR_SUB: as->curins--; asm_sub64(as, ir); break; + case IR_NEG: as->curins--; asm_neg64(as, ir); break; + case IR_CALLN: + case IR_CALLXS: + if (!uselo) + ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ + break; + case IR_CNEWI: + /* Nothing to do here. Handled by lo op itself. */ + break; + default: lua_assert(0); break; + } +#else + UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused without FFI. */ +#endif +} + +/* -- Stack handling ------------------------------------------------------ */ + +/* Check Lua stack size for overflow. Use exit handler as fallback. */ +static void asm_stack_check(ASMState *as, BCReg topslot, + IRIns *irp, RegSet allow, ExitNo exitno) +{ + /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */ + Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE; + ExitNo oldsnap = as->snapno; + rset_clear(allow, pbase); + tmp = allow ? rset_pickbot(allow) : + (pbase == RID_RETHI ? RID_RETLO : RID_RETHI); + as->snapno = exitno; + asm_guard(as, MIPSI_BNE, RID_TMP, RID_ZERO); + as->snapno = oldsnap; + if (allow == RSET_EMPTY) /* Restore temp. register. */ + emit_tsi(as, MIPSI_LW, tmp, RID_SP, 0); + else + ra_modified(as, tmp); + emit_tsi(as, MIPSI_SLTIU, RID_TMP, RID_TMP, (int32_t)(8*topslot)); + emit_dst(as, MIPSI_SUBU, RID_TMP, tmp, pbase); + emit_tsi(as, MIPSI_LW, tmp, tmp, offsetof(lua_State, maxstack)); + if (pbase == RID_TMP) + emit_getgl(as, RID_TMP, jit_base); + emit_getgl(as, tmp, jit_L); + if (allow == RSET_EMPTY) /* Spill temp. register. */ + emit_tsi(as, MIPSI_SW, tmp, RID_SP, 0); +} + +/* Restore Lua stack from on-trace state. */ +static void asm_stack_restore(ASMState *as, SnapShot *snap) +{ + SnapEntry *map = &as->T->snapmap[snap->mapofs]; + SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; + MSize n, nent = snap->nent; + /* Store the value of all modified slots to the Lua stack. */ + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + BCReg s = snap_slot(sn); + int32_t ofs = 8*((int32_t)s-1); + IRRef ref = snap_ref(sn); + IRIns *ir = IR(ref); + if ((sn & SNAP_NORESTORE)) + continue; + if (irt_isnum(ir->t)) { + Reg src = ra_alloc1(as, ref, RSET_FPR); + emit_hsi(as, MIPSI_SDC1, src, RID_BASE, ofs); + } else { + Reg type; + RegSet allow = rset_exclude(RSET_GPR, RID_BASE); + lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t)); + if (!irt_ispri(ir->t)) { + Reg src = ra_alloc1(as, ref, allow); + rset_clear(allow, src); + emit_tsi(as, MIPSI_SW, src, RID_BASE, ofs+(LJ_BE?4:0)); + } + if ((sn & (SNAP_CONT|SNAP_FRAME))) { + if (s == 0) continue; /* Do not overwrite link to previous frame. */ + type = ra_allock(as, (int32_t)(*flinks--), allow); + } else { + type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); + } + emit_tsi(as, MIPSI_SW, type, RID_BASE, ofs+(LJ_BE?0:4)); + } + checkmclim(as); + } + lua_assert(map + nent == flinks); +} + +/* -- GC handling --------------------------------------------------------- */ + +/* Check GC threshold and do one or more GC steps. */ +static void asm_gc_check(ASMState *as) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; + IRRef args[2]; + MCLabel l_end; + Reg tmp; + ra_evictset(as, RSET_SCRATCH); + l_end = emit_label(as); + /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ + /* Assumes asm_snap_prep() already done. */ + asm_guard(as, MIPSI_BNE, RID_RET, RID_ZERO); + args[0] = ASMREF_TMP1; /* global_State *g */ + args[1] = ASMREF_TMP2; /* MSize steps */ + asm_gencall(as, ci, args); + emit_tsi(as, MIPSI_ADDIU, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); + tmp = ra_releasetmp(as, ASMREF_TMP2); + emit_loadi(as, tmp, as->gcsteps); + /* Jump around GC step if GC total < GC threshold. */ + emit_branch(as, MIPSI_BNE, RID_TMP, RID_ZERO, l_end); + emit_dst(as, MIPSI_SLTU, RID_TMP, RID_TMP, tmp); + emit_getgl(as, tmp, gc.threshold); + emit_getgl(as, RID_TMP, gc.total); + as->gcsteps = 0; + checkmclim(as); +} + +/* -- Loop handling ------------------------------------------------------- */ + +/* Fixup the loop branch. */ +static void asm_loop_fixup(ASMState *as) +{ + MCode *p = as->mctop; + MCode *target = as->mcp; + p[-1] = MIPSI_NOP; + if (as->loopinv) { /* Inverted loop branch? */ + /* asm_guard already inverted the cond branch. Only patch the target. */ + p[-3] |= ((target-p+2) & 0x0000ffffu); + } else { + p[-2] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); + } +} + +/* -- Head of trace ------------------------------------------------------- */ + +/* Coalesce BASE register for a root trace. */ +static void asm_head_root_base(ASMState *as) +{ + IRIns *ir = IR(REF_BASE); + Reg r = ir->r; + if (as->loopinv) as->mctop--; + if (ra_hasreg(r)) { + ra_free(as, r); + if (rset_test(as->modset, r)) + ir->r = RID_INIT; /* No inheritance for modified BASE register. */ + if (r != RID_BASE) + emit_move(as, r, RID_BASE); + } +} + +/* Coalesce BASE register for a side trace. */ +static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) +{ + IRIns *ir = IR(REF_BASE); + Reg r = ir->r; + if (as->loopinv) as->mctop--; + if (ra_hasreg(r)) { + ra_free(as, r); + if (rset_test(as->modset, r)) + ir->r = RID_INIT; /* No inheritance for modified BASE register. */ + if (irp->r == r) { + rset_clear(allow, r); /* Mark same BASE register as coalesced. */ + } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { + rset_clear(allow, irp->r); + emit_move(as, r, irp->r); /* Move from coalesced parent reg. */ + } else { + emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ + } + } + return allow; +} + +/* -- Tail of trace ------------------------------------------------------- */ + +/* Fixup the tail code. */ +static void asm_tail_fixup(ASMState *as, TraceNo lnk) +{ + MCode *target = lnk ? traceref(as->J,lnk)->mcode : (MCode *)lj_vm_exit_interp; + int32_t spadj = as->T->spadjust; + MCode *p = as->mctop-1; + *p = spadj ? (MIPSI_ADDIU|MIPSF_T(RID_SP)|MIPSF_S(RID_SP)|spadj) : MIPSI_NOP; + p[-1] = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); +} + +/* Prepare tail of code. */ +static void asm_tail_prep(ASMState *as) +{ + as->mcp = as->mctop-2; /* Leave room for branch plus nop or stack adj. */ + as->invmcp = as->loopref ? as->mcp : NULL; +} + +/* -- Instruction dispatch ------------------------------------------------ */ + +/* Assemble a single instruction. */ +static void asm_ir(ASMState *as, IRIns *ir) +{ + switch ((IROp)ir->o) { + /* Miscellaneous ops. */ + case IR_LOOP: asm_loop(as); break; + case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; + case IR_USE: + ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; + case IR_PHI: asm_phi(as, ir); break; + case IR_HIOP: asm_hiop(as, ir); break; + case IR_GCSTEP: asm_gcstep(as, ir); break; + + /* Guarded assertions. */ + case IR_EQ: case IR_NE: asm_compeq(as, ir); break; + case IR_LT: case IR_GE: case IR_LE: case IR_GT: + case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: + case IR_ABC: + asm_comp(as, ir); + break; + + case IR_RETF: asm_retf(as, ir); break; + + /* Bit ops. */ + case IR_BNOT: asm_bitnot(as, ir); break; + case IR_BSWAP: asm_bitswap(as, ir); break; + + case IR_BAND: asm_bitop(as, ir, MIPSI_AND, MIPSI_ANDI); break; + case IR_BOR: asm_bitop(as, ir, MIPSI_OR, MIPSI_ORI); break; + case IR_BXOR: asm_bitop(as, ir, MIPSI_XOR, MIPSI_XORI); break; + + case IR_BSHL: asm_bitshift(as, ir, MIPSI_SLLV, MIPSI_SLL); break; + case IR_BSHR: asm_bitshift(as, ir, MIPSI_SRLV, MIPSI_SRL); break; + case IR_BSAR: asm_bitshift(as, ir, MIPSI_SRAV, MIPSI_SRA); break; + case IR_BROL: lua_assert(0); break; + case IR_BROR: asm_bitror(as, ir); break; + + /* Arithmetic ops. */ + case IR_ADD: asm_add(as, ir); break; + case IR_SUB: asm_sub(as, ir); break; + case IR_MUL: asm_mul(as, ir); break; + case IR_DIV: asm_fparith(as, ir, MIPSI_DIV_D); break; + case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break; + case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break; + case IR_NEG: asm_neg(as, ir); break; + + case IR_ABS: asm_fpunary(as, ir, MIPSI_ABS_D); break; + case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break; + case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break; + case IR_MIN: asm_min_max(as, ir, 0); break; + case IR_MAX: asm_min_max(as, ir, 1); break; + case IR_FPMATH: + if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir)) + break; + if (ir->op2 <= IRFPM_TRUNC) + asm_callround(as, ir, IRCALL_lj_vm_floor + ir->op2); + else if (ir->op2 == IRFPM_SQRT) + asm_fpunary(as, ir, MIPSI_SQRT_D); + else + asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); + break; + + /* Overflow-checking arithmetic ops. */ + case IR_ADDOV: asm_arithov(as, ir); break; + case IR_SUBOV: asm_arithov(as, ir); break; + case IR_MULOV: asm_mulov(as, ir); break; + + /* Memory references. */ + case IR_AREF: asm_aref(as, ir); break; + case IR_HREF: asm_href(as, ir); break; + case IR_HREFK: asm_hrefk(as, ir); break; + case IR_NEWREF: asm_newref(as, ir); break; + case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; + case IR_FREF: asm_fref(as, ir); break; + case IR_STRREF: asm_strref(as, ir); break; + + /* Loads and stores. */ + case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: + asm_ahuvload(as, ir); + break; + case IR_FLOAD: asm_fload(as, ir); break; + case IR_XLOAD: asm_xload(as, ir); break; + case IR_SLOAD: asm_sload(as, ir); break; + + case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; + case IR_FSTORE: asm_fstore(as, ir); break; + case IR_XSTORE: asm_xstore(as, ir, 0); break; + + /* Allocations. */ + case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; + case IR_TNEW: asm_tnew(as, ir); break; + case IR_TDUP: asm_tdup(as, ir); break; + case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; + + /* Write barriers. */ + case IR_TBAR: asm_tbar(as, ir); break; + case IR_OBAR: asm_obar(as, ir); break; + + /* Type conversions. */ + case IR_CONV: asm_conv(as, ir); break; + case IR_TOBIT: asm_tobit(as, ir); break; + case IR_TOSTR: asm_tostr(as, ir); break; + case IR_STRTO: asm_strto(as, ir); break; + + /* Calls. */ + case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; + case IR_CALLXS: asm_callx(as, ir); break; + case IR_CARG: break; + + default: + setintV(&as->J->errinfo, ir->o); + lj_trace_err_info(as->J, LJ_TRERR_NYIIR); + break; + } +} + +/* -- Trace setup --------------------------------------------------------- */ + +/* Ensure there are enough stack slots for call arguments. */ +static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) +{ + IRRef args[CCI_NARGS_MAX]; + uint32_t i, nargs = (int)CCI_NARGS(ci); + int nslots = 4, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; + asm_collectargs(as, ir, ci, args); + for (i = 0; i < nargs; i++) { + if (args[i] && irt_isfp(IR(args[i])->t) && + nfpr > 0 && !(ci->flags & CCI_VARARG)) { + nfpr--; + ngpr -= irt_isnum(IR(args[i])->t) ? 2 : 1; + } else if (args[i] && irt_isnum(IR(args[i])->t)) { + nfpr = 0; + ngpr = ngpr & ~1; + if (ngpr > 0) ngpr -= 2; else nslots = (nslots+3) & ~1; + } else { + nfpr = 0; + if (ngpr > 0) ngpr--; else nslots++; + } + } + if (nslots > as->evenspill) /* Leave room for args in stack slots. */ + as->evenspill = nslots; + return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET); +} + +static void asm_setup_target(ASMState *as) +{ + asm_sparejump_setup(as); + asm_exitstub_setup(as); +} + +/* -- Trace patching ------------------------------------------------------ */ + +/* Patch exit jumps of existing machine code to a new target. */ +void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) +{ + MCode *p = T->mcode; + MCode *pe = (MCode *)((char *)p + T->szmcode); + MCode *px = exitstub_trace_addr(T, exitno); + MCode *cstart = NULL, *cstop = NULL; + MCode *mcarea = lj_mcode_patch(J, p, 0); + MCode exitload = MIPSI_LI | MIPSF_T(RID_TMP) | exitno; + MCode tjump = MIPSI_J|(((uintptr_t)target>>2)&0x03ffffffu); + for (p++; p < pe; p++) { + if (*p == exitload) { /* Look for load of exit number. */ + if (((p[-1] ^ (px-p)) & 0xffffu) == 0) { /* Look for exitstub branch. */ + ptrdiff_t delta = target - p; + if (((delta + 0x8000) >> 16) == 0) { /* Patch in-range branch. */ + patchbranch: + p[-1] = (p[-1] & 0xffff0000u) | (delta & 0xffffu); + *p = MIPSI_NOP; /* Replace the load of the exit number. */ + cstop = p; + if (!cstart) cstart = p-1; + } else { /* Branch out of range. Use spare jump slot in mcarea. */ + int i; + for (i = 2; i < 2+MIPS_SPAREJUMP*2; i += 2) { + if (mcarea[i] == tjump) { + delta = mcarea+i - p; + goto patchbranch; + } else if (mcarea[i] == MIPSI_NOP) { + mcarea[i] = tjump; + cstart = mcarea+i; + delta = mcarea+i - p; + goto patchbranch; + } + } + /* Ignore jump slot overflow. Child trace is simply not attached. */ + } + } else if (p+1 == pe) { + /* Patch NOP after code for inverted loop branch. Use of J is ok. */ + lua_assert(p[1] == MIPSI_NOP); + p[1] = tjump; + *p = MIPSI_NOP; /* Replace the load of the exit number. */ + cstop = p+2; + if (!cstart) cstart = p+1; + } + } + } + if (cstart) lj_mcode_sync(cstart, cstop); + lj_mcode_patch(J, mcarea, 1); +} + diff --git a/external/lua/luajit/src/src/lj_asm_ppc.h b/external/lua/luajit/src/src/lj_asm_ppc.h new file mode 100644 index 0000000000..7637f6a6a7 --- /dev/null +++ b/external/lua/luajit/src/src/lj_asm_ppc.h @@ -0,0 +1,2165 @@ +/* +** PPC IR assembler (SSA IR -> machine code). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* -- Register allocator extensions --------------------------------------- */ + +/* Allocate a register with a hint. */ +static Reg ra_hintalloc(ASMState *as, IRRef ref, Reg hint, RegSet allow) +{ + Reg r = IR(ref)->r; + if (ra_noreg(r)) { + if (!ra_hashint(r) && !iscrossref(as, ref)) + ra_sethint(IR(ref)->r, hint); /* Propagate register hint. */ + r = ra_allocref(as, ref, allow); + } + ra_noweak(as, r); + return r; +} + +/* Allocate two source registers for three-operand instructions. */ +static Reg ra_alloc2(ASMState *as, IRIns *ir, RegSet allow) +{ + IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); + Reg left = irl->r, right = irr->r; + if (ra_hasreg(left)) { + ra_noweak(as, left); + if (ra_noreg(right)) + right = ra_allocref(as, ir->op2, rset_exclude(allow, left)); + else + ra_noweak(as, right); + } else if (ra_hasreg(right)) { + ra_noweak(as, right); + left = ra_allocref(as, ir->op1, rset_exclude(allow, right)); + } else if (ra_hashint(right)) { + right = ra_allocref(as, ir->op2, allow); + left = ra_alloc1(as, ir->op1, rset_exclude(allow, right)); + } else { + left = ra_allocref(as, ir->op1, allow); + right = ra_alloc1(as, ir->op2, rset_exclude(allow, left)); + } + return left | (right << 8); +} + +/* -- Guard handling ------------------------------------------------------ */ + +/* Setup exit stubs after the end of each trace. */ +static void asm_exitstub_setup(ASMState *as, ExitNo nexits) +{ + ExitNo i; + MCode *mxp = as->mctop; + /* 1: mflr r0; bl ->vm_exit_handler; li r0, traceno; bl <1; bl <1; ... */ + for (i = nexits-1; (int32_t)i >= 0; i--) + *--mxp = PPCI_BL|(((-3-i)&0x00ffffffu)<<2); + *--mxp = PPCI_LI|PPCF_T(RID_TMP)|as->T->traceno; /* Read by exit handler. */ + mxp--; + *mxp = PPCI_BL|((((MCode *)(void *)lj_vm_exit_handler-mxp)&0x00ffffffu)<<2); + *--mxp = PPCI_MFLR|PPCF_T(RID_TMP); + as->mctop = mxp; +} + +static MCode *asm_exitstub_addr(ASMState *as, ExitNo exitno) +{ + /* Keep this in-sync with exitstub_trace_addr(). */ + return as->mctop + exitno + 3; +} + +/* Emit conditional branch to exit for guard. */ +static void asm_guardcc(ASMState *as, PPCCC cc) +{ + MCode *target = asm_exitstub_addr(as, as->snapno); + MCode *p = as->mcp; + if (LJ_UNLIKELY(p == as->invmcp)) { + as->loopinv = 1; + *p = PPCI_B | (((target-p) & 0x00ffffffu) << 2); + emit_condbranch(as, PPCI_BC, cc^4, p); + return; + } + emit_condbranch(as, PPCI_BC, cc, target); +} + +/* -- Operand fusion ------------------------------------------------------ */ + +/* Limit linear search to this distance. Avoids O(n^2) behavior. */ +#define CONFLICT_SEARCH_LIM 31 + +/* Check if there's no conflicting instruction between curins and ref. */ +static int noconflict(ASMState *as, IRRef ref, IROp conflict) +{ + IRIns *ir = as->ir; + IRRef i = as->curins; + if (i > ref + CONFLICT_SEARCH_LIM) + return 0; /* Give up, ref is too far away. */ + while (--i > ref) + if (ir[i].o == conflict) + return 0; /* Conflict found. */ + return 1; /* Ok, no conflict. */ +} + +/* Fuse the array base of colocated arrays. */ +static int32_t asm_fuseabase(ASMState *as, IRRef ref) +{ + IRIns *ir = IR(ref); + if (ir->o == IR_TNEW && ir->op1 <= LJ_MAX_COLOSIZE && + !neverfuse(as) && noconflict(as, ref, IR_NEWREF)) + return (int32_t)sizeof(GCtab); + return 0; +} + +/* Indicates load/store indexed is ok. */ +#define AHUREF_LSX ((int32_t)0x80000000) + +/* Fuse array/hash/upvalue reference into register+offset operand. */ +static Reg asm_fuseahuref(ASMState *as, IRRef ref, int32_t *ofsp, RegSet allow) +{ + IRIns *ir = IR(ref); + if (ra_noreg(ir->r)) { + if (ir->o == IR_AREF) { + if (mayfuse(as, ref)) { + if (irref_isk(ir->op2)) { + IRRef tab = IR(ir->op1)->op1; + int32_t ofs = asm_fuseabase(as, tab); + IRRef refa = ofs ? tab : ir->op1; + ofs += 8*IR(ir->op2)->i; + if (checki16(ofs)) { + *ofsp = ofs; + return ra_alloc1(as, refa, allow); + } + } + if (*ofsp == AHUREF_LSX) { + Reg base = ra_alloc1(as, ir->op1, allow); + Reg idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); + return base | (idx << 8); + } + } + } else if (ir->o == IR_HREFK) { + if (mayfuse(as, ref)) { + int32_t ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); + if (checki16(ofs)) { + *ofsp = ofs; + return ra_alloc1(as, ir->op1, allow); + } + } + } else if (ir->o == IR_UREFC) { + if (irref_isk(ir->op1)) { + GCfunc *fn = ir_kfunc(IR(ir->op1)); + int32_t ofs = i32ptr(&gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.tv); + int32_t jgl = (intptr_t)J2G(as->J); + if ((uint32_t)(ofs-jgl) < 65536) { + *ofsp = ofs-jgl-32768; + return RID_JGL; + } else { + *ofsp = (int16_t)ofs; + return ra_allock(as, ofs-(int16_t)ofs, allow); + } + } + } + } + *ofsp = 0; + return ra_alloc1(as, ref, allow); +} + +/* Fuse XLOAD/XSTORE reference into load/store operand. */ +static void asm_fusexref(ASMState *as, PPCIns pi, Reg rt, IRRef ref, + RegSet allow, int32_t ofs) +{ + IRIns *ir = IR(ref); + Reg base; + if (ra_noreg(ir->r) && canfuse(as, ir)) { + if (ir->o == IR_ADD) { + int32_t ofs2; + if (irref_isk(ir->op2) && (ofs2 = ofs + IR(ir->op2)->i, checki16(ofs2))) { + ofs = ofs2; + ref = ir->op1; + } else if (ofs == 0) { + Reg right, left = ra_alloc2(as, ir, allow); + right = (left >> 8); left &= 255; + emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right); + return; + } + } else if (ir->o == IR_STRREF) { + lua_assert(ofs == 0); + ofs = (int32_t)sizeof(GCstr); + if (irref_isk(ir->op2)) { + ofs += IR(ir->op2)->i; + ref = ir->op1; + } else if (irref_isk(ir->op1)) { + ofs += IR(ir->op1)->i; + ref = ir->op2; + } else { + /* NYI: Fuse ADD with constant. */ + Reg tmp, right, left = ra_alloc2(as, ir, allow); + right = (left >> 8); left &= 255; + tmp = ra_scratch(as, rset_exclude(rset_exclude(allow, left), right)); + emit_fai(as, pi, rt, tmp, ofs); + emit_tab(as, PPCI_ADD, tmp, left, right); + return; + } + if (!checki16(ofs)) { + Reg left = ra_alloc1(as, ref, allow); + Reg right = ra_allock(as, ofs, rset_exclude(allow, left)); + emit_fab(as, PPCI_LWZX | ((pi >> 20) & 0x780), rt, left, right); + return; + } + } + } + base = ra_alloc1(as, ref, allow); + emit_fai(as, pi, rt, base, ofs); +} + +/* Fuse XLOAD/XSTORE reference into indexed-only load/store operand. */ +static void asm_fusexrefx(ASMState *as, PPCIns pi, Reg rt, IRRef ref, + RegSet allow) +{ + IRIns *ira = IR(ref); + Reg right, left; + if (canfuse(as, ira) && ira->o == IR_ADD && ra_noreg(ira->r)) { + left = ra_alloc2(as, ira, allow); + right = (left >> 8); left &= 255; + } else { + right = ra_alloc1(as, ref, allow); + left = RID_R0; + } + emit_tab(as, pi, rt, left, right); +} + +/* Fuse to multiply-add/sub instruction. */ +static int asm_fusemadd(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pir) +{ + IRRef lref = ir->op1, rref = ir->op2; + IRIns *irm; + if (lref != rref && + ((mayfuse(as, lref) && (irm = IR(lref), irm->o == IR_MUL) && + ra_noreg(irm->r)) || + (mayfuse(as, rref) && (irm = IR(rref), irm->o == IR_MUL) && + (rref = lref, pi = pir, ra_noreg(irm->r))))) { + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg add = ra_alloc1(as, rref, RSET_FPR); + Reg right, left = ra_alloc2(as, irm, rset_exclude(RSET_FPR, add)); + right = (left >> 8); left &= 255; + emit_facb(as, pi, dest, left, right, add); + return 1; + } + return 0; +} + +/* -- Calls --------------------------------------------------------------- */ + +/* Generate a call to a C function. */ +static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) +{ + uint32_t n, nargs = CCI_NARGS(ci); + int32_t ofs = 8; + Reg gpr = REGARG_FIRSTGPR, fpr = REGARG_FIRSTFPR; + if ((void *)ci->func) + emit_call(as, (void *)ci->func); + for (n = 0; n < nargs; n++) { /* Setup args. */ + IRRef ref = args[n]; + if (ref) { + IRIns *ir = IR(ref); + if (irt_isfp(ir->t)) { + if (fpr <= REGARG_LASTFPR) { + lua_assert(rset_test(as->freeset, fpr)); /* Already evicted. */ + ra_leftov(as, fpr, ref); + fpr++; + } else { + Reg r = ra_alloc1(as, ref, RSET_FPR); + if (irt_isnum(ir->t)) ofs = (ofs + 4) & ~4; + emit_spstore(as, ir, r, ofs); + ofs += irt_isnum(ir->t) ? 8 : 4; + } + } else { + if (gpr <= REGARG_LASTGPR) { + lua_assert(rset_test(as->freeset, gpr)); /* Already evicted. */ + ra_leftov(as, gpr, ref); + gpr++; + } else { + Reg r = ra_alloc1(as, ref, RSET_GPR); + emit_spstore(as, ir, r, ofs); + ofs += 4; + } + } + } else { + if (gpr <= REGARG_LASTGPR) + gpr++; + else + ofs += 4; + } + } + if ((ci->flags & CCI_VARARG)) /* Vararg calls need to know about FPR use. */ + emit_tab(as, fpr == REGARG_FIRSTFPR ? PPCI_CRXOR : PPCI_CREQV, 6, 6, 6); +} + +/* Setup result reg/sp for call. Evict scratch regs. */ +static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) +{ + RegSet drop = RSET_SCRATCH; + int hiop = ((ir+1)->o == IR_HIOP); + if ((ci->flags & CCI_NOFPRCLOBBER)) + drop &= ~RSET_FPR; + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + if (hiop && ra_hasreg((ir+1)->r)) + rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ + ra_evictset(as, drop); /* Evictions must be performed first. */ + if (ra_used(ir)) { + lua_assert(!irt_ispri(ir->t)); + if (irt_isfp(ir->t)) { + if ((ci->flags & CCI_CASTU64)) { + /* Use spill slot or temp slots. */ + int32_t ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP; + Reg dest = ir->r; + if (ra_hasreg(dest)) { + ra_free(as, dest); + ra_modified(as, dest); + emit_fai(as, PPCI_LFD, dest, RID_SP, ofs); + } + emit_tai(as, PPCI_STW, RID_RETHI, RID_SP, ofs); + emit_tai(as, PPCI_STW, RID_RETLO, RID_SP, ofs+4); + } else { + ra_destreg(as, ir, RID_FPRET); + } + } else if (hiop) { + ra_destpair(as, ir); + } else { + ra_destreg(as, ir, RID_RET); + } + } +} + +static void asm_call(ASMState *as, IRIns *ir) +{ + IRRef args[CCI_NARGS_MAX]; + const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; + asm_collectargs(as, ir, ci, args); + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} + +static void asm_callx(ASMState *as, IRIns *ir) +{ + IRRef args[CCI_NARGS_MAX]; + CCallInfo ci; + IRRef func; + IRIns *irf; + ci.flags = asm_callx_flags(as, ir); + asm_collectargs(as, ir, &ci, args); + asm_setupresult(as, ir, &ci); + func = ir->op2; irf = IR(func); + if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } + if (irref_isk(func)) { /* Call to constant address. */ + ci.func = (ASMFunction)(void *)(irf->i); + } else { /* Need a non-argument register for indirect calls. */ + RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1); + Reg freg = ra_alloc1(as, func, allow); + *--as->mcp = PPCI_BCTRL; + *--as->mcp = PPCI_MTCTR | PPCF_T(freg); + ci.func = (ASMFunction)(void *)0; + } + asm_gencall(as, &ci, args); +} + +static void asm_callid(ASMState *as, IRIns *ir, IRCallID id) +{ + const CCallInfo *ci = &lj_ir_callinfo[id]; + IRRef args[2]; + args[0] = ir->op1; + args[1] = ir->op2; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} + +/* -- Returns ------------------------------------------------------------- */ + +/* Return to lower frame. Guard that it goes to the right spot. */ +static void asm_retf(ASMState *as, IRIns *ir) +{ + Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); + void *pc = ir_kptr(IR(ir->op2)); + int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); + as->topslot -= (BCReg)delta; + if ((int32_t)as->topslot < 0) as->topslot = 0; + emit_setgl(as, base, jit_base); + emit_addptr(as, base, -8*delta); + asm_guardcc(as, CC_NE); + emit_ab(as, PPCI_CMPW, RID_TMP, + ra_allock(as, i32ptr(pc), rset_exclude(RSET_GPR, base))); + emit_tai(as, PPCI_LWZ, RID_TMP, base, -8); +} + +/* -- Type conversions ---------------------------------------------------- */ + +static void asm_tointg(ASMState *as, IRIns *ir, Reg left) +{ + RegSet allow = RSET_FPR; + Reg tmp = ra_scratch(as, rset_clear(allow, left)); + Reg fbias = ra_scratch(as, rset_clear(allow, tmp)); + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg hibias = ra_allock(as, 0x43300000, rset_exclude(RSET_GPR, dest)); + asm_guardcc(as, CC_NE); + emit_fab(as, PPCI_FCMPU, 0, tmp, left); + emit_fab(as, PPCI_FSUB, tmp, tmp, fbias); + emit_fai(as, PPCI_LFD, tmp, RID_SP, SPOFS_TMP); + emit_tai(as, PPCI_STW, RID_TMP, RID_SP, SPOFS_TMPLO); + emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); + emit_asi(as, PPCI_XORIS, RID_TMP, dest, 0x8000); + emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); + emit_lsptr(as, PPCI_LFS, (fbias & 31), + (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)), + RSET_GPR); + emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); + emit_fb(as, PPCI_FCTIWZ, tmp, left); +} + +static void asm_tobit(ASMState *as, IRIns *ir) +{ + RegSet allow = RSET_FPR; + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, allow); + Reg right = ra_alloc1(as, ir->op2, rset_clear(allow, left)); + Reg tmp = ra_scratch(as, rset_clear(allow, right)); + emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); + emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); + emit_fab(as, PPCI_FADD, tmp, left, right); +} + +static void asm_conv(ASMState *as, IRIns *ir) +{ + IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); + int stfp = (st == IRT_NUM || st == IRT_FLOAT); + IRRef lref = ir->op1; + lua_assert(irt_type(ir->t) != st); + lua_assert(!(irt_isint64(ir->t) || + (st == IRT_I64 || st == IRT_U64))); /* Handled by SPLIT. */ + if (irt_isfp(ir->t)) { + Reg dest = ra_dest(as, ir, RSET_FPR); + if (stfp) { /* FP to FP conversion. */ + if (st == IRT_NUM) /* double -> float conversion. */ + emit_fb(as, PPCI_FRSP, dest, ra_alloc1(as, lref, RSET_FPR)); + else /* float -> double conversion is a no-op on PPC. */ + ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ + } else { /* Integer to FP conversion. */ + /* IRT_INT: Flip hibit, bias with 2^52, subtract 2^52+2^31. */ + /* IRT_U32: Bias with 2^52, subtract 2^52. */ + RegSet allow = RSET_GPR; + Reg left = ra_alloc1(as, lref, allow); + Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, left)); + Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); + const float *kbias; + if (irt_isfloat(ir->t)) emit_fb(as, PPCI_FRSP, dest, dest); + emit_fab(as, PPCI_FSUB, dest, dest, fbias); + emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP); + kbias = (const float *)lj_ir_k64_find(as->J, U64x(59800004,59800000)); + if (st == IRT_U32) kbias++; + emit_lsptr(as, PPCI_LFS, (fbias & 31), (void *)kbias, + rset_clear(allow, hibias)); + emit_tai(as, PPCI_STW, st == IRT_U32 ? left : RID_TMP, + RID_SP, SPOFS_TMPLO); + emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); + if (st != IRT_U32) emit_asi(as, PPCI_XORIS, RID_TMP, left, 0x8000); + } + } else if (stfp) { /* FP to integer conversion. */ + if (irt_isguard(ir->t)) { + /* Checked conversions are only supported from number to int. */ + lua_assert(irt_isint(ir->t) && st == IRT_NUM); + asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, lref, RSET_FPR); + Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); + if (irt_isu32(ir->t)) { + /* Convert both x and x-2^31 to int and merge results. */ + Reg tmpi = ra_scratch(as, rset_exclude(RSET_GPR, dest)); + emit_asb(as, PPCI_OR, dest, dest, tmpi); /* Select with mask idiom. */ + emit_asb(as, PPCI_AND, tmpi, tmpi, RID_TMP); + emit_asb(as, PPCI_ANDC, dest, dest, RID_TMP); + emit_tai(as, PPCI_LWZ, tmpi, RID_SP, SPOFS_TMPLO); /* tmp = (int)(x) */ + emit_tai(as, PPCI_ADDIS, dest, dest, 0x8000); /* dest += 2^31 */ + emit_asb(as, PPCI_SRAWI, RID_TMP, dest, 31); /* mask = -(dest < 0) */ + emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); + emit_tai(as, PPCI_LWZ, dest, + RID_SP, SPOFS_TMPLO); /* dest = (int)(x-2^31) */ + emit_fb(as, PPCI_FCTIWZ, tmp, left); + emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); + emit_fb(as, PPCI_FCTIWZ, tmp, tmp); + emit_fab(as, PPCI_FSUB, tmp, left, tmp); + emit_lsptr(as, PPCI_LFS, (tmp & 31), + (void *)lj_ir_k64_find(as->J, U64x(4f000000,00000000)), + RSET_GPR); + } else { + emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); + emit_fai(as, PPCI_STFD, tmp, RID_SP, SPOFS_TMP); + emit_fb(as, PPCI_FCTIWZ, tmp, left); + } + } + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ + Reg left = ra_alloc1(as, ir->op1, RSET_GPR); + lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); + if ((ir->op2 & IRCONV_SEXT)) + emit_as(as, st == IRT_I8 ? PPCI_EXTSB : PPCI_EXTSH, dest, left); + else + emit_rot(as, PPCI_RLWINM, dest, left, 0, st == IRT_U8 ? 24 : 16, 31); + } else { /* 32/64 bit integer conversions. */ + /* Only need to handle 32/32 bit no-op (cast) on 32 bit archs. */ + ra_leftov(as, dest, lref); /* Do nothing, but may need to move regs. */ + } + } +} + +#if LJ_HASFFI +static void asm_conv64(ASMState *as, IRIns *ir) +{ + IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); + IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); + IRCallID id; + const CCallInfo *ci; + IRRef args[2]; + args[0] = ir->op1; + args[1] = (ir-1)->op1; + if (st == IRT_NUM || st == IRT_FLOAT) { + id = IRCALL_fp64_d2l + ((st == IRT_FLOAT) ? 2 : 0) + (dt - IRT_I64); + ir--; + } else { + id = IRCALL_fp64_l2d + ((dt == IRT_FLOAT) ? 2 : 0) + (st - IRT_I64); + } + ci = &lj_ir_callinfo[id]; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} +#endif + +static void asm_strto(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; + IRRef args[2]; + int32_t ofs; + RegSet drop = RSET_SCRATCH; + if (ra_hasreg(ir->r)) rset_set(drop, ir->r); /* Spill dest reg (if any). */ + ra_evictset(as, drop); + asm_guardcc(as, CC_EQ); + emit_ai(as, PPCI_CMPWI, RID_RET, 0); /* Test return status. */ + args[0] = ir->op1; /* GCstr *str */ + args[1] = ASMREF_TMP1; /* TValue *n */ + asm_gencall(as, ci, args); + /* Store the result to the spill slot or temp slots. */ + ofs = ir->s ? sps_scale(ir->s) : SPOFS_TMP; + emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_SP, ofs); +} + +/* Get pointer to TValue. */ +static void asm_tvptr(ASMState *as, Reg dest, IRRef ref) +{ + IRIns *ir = IR(ref); + if (irt_isnum(ir->t)) { + if (irref_isk(ref)) /* Use the number constant itself as a TValue. */ + ra_allockreg(as, i32ptr(ir_knum(ir)), dest); + else /* Otherwise force a spill and use the spill slot. */ + emit_tai(as, PPCI_ADDI, dest, RID_SP, ra_spill(as, ir)); + } else { + /* Otherwise use g->tmptv to hold the TValue. */ + RegSet allow = rset_exclude(RSET_GPR, dest); + Reg type; + emit_tai(as, PPCI_ADDI, dest, RID_JGL, offsetof(global_State, tmptv)-32768); + if (!irt_ispri(ir->t)) { + Reg src = ra_alloc1(as, ref, allow); + emit_setgl(as, src, tmptv.gcr); + } + type = ra_allock(as, irt_toitype(ir->t), allow); + emit_setgl(as, type, tmptv.it); + } +} + +static void asm_tostr(ASMState *as, IRIns *ir) +{ + IRRef args[2]; + args[0] = ASMREF_L; + as->gcsteps++; + if (irt_isnum(IR(ir->op1)->t) || (ir+1)->o == IR_HIOP) { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; + args[1] = ASMREF_TMP1; /* const lua_Number * */ + asm_setupresult(as, ir, ci); /* GCstr * */ + asm_gencall(as, ci, args); + asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op1); + } else { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; + args[1] = ir->op1; /* int32_t k */ + asm_setupresult(as, ir, ci); /* GCstr * */ + asm_gencall(as, ci, args); + } +} + +/* -- Memory references --------------------------------------------------- */ + +static void asm_aref(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg idx, base; + if (irref_isk(ir->op2)) { + IRRef tab = IR(ir->op1)->op1; + int32_t ofs = asm_fuseabase(as, tab); + IRRef refa = ofs ? tab : ir->op1; + ofs += 8*IR(ir->op2)->i; + if (checki16(ofs)) { + base = ra_alloc1(as, refa, RSET_GPR); + emit_tai(as, PPCI_ADDI, dest, base, ofs); + return; + } + } + base = ra_alloc1(as, ir->op1, RSET_GPR); + idx = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, base)); + emit_tab(as, PPCI_ADD, dest, RID_TMP, base); + emit_slwi(as, RID_TMP, idx, 3); +} + +/* Inlined hash lookup. Specialized for key type and for const keys. +** The equivalent C code is: +** Node *n = hashkey(t, key); +** do { +** if (lj_obj_equal(&n->key, key)) return &n->val; +** } while ((n = nextnode(n))); +** return niltv(L); +*/ +static void asm_href(ASMState *as, IRIns *ir, IROp merge) +{ + RegSet allow = RSET_GPR; + int destused = ra_used(ir); + Reg dest = ra_dest(as, ir, allow); + Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); + Reg key = RID_NONE, tmp1 = RID_TMP, tmp2; + Reg tisnum = RID_NONE, tmpnum = RID_NONE; + IRRef refkey = ir->op2; + IRIns *irkey = IR(refkey); + IRType1 kt = irkey->t; + uint32_t khash; + MCLabel l_end, l_loop, l_next; + + rset_clear(allow, tab); + if (irt_isnum(kt)) { + key = ra_alloc1(as, refkey, RSET_FPR); + tmpnum = ra_scratch(as, rset_exclude(RSET_FPR, key)); + tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow); + rset_clear(allow, tisnum); + } else if (!irt_ispri(kt)) { + key = ra_alloc1(as, refkey, allow); + rset_clear(allow, key); + } + tmp2 = ra_scratch(as, allow); + rset_clear(allow, tmp2); + + /* Key not found in chain: jump to exit (if merged) or load niltv. */ + l_end = emit_label(as); + as->invmcp = NULL; + if (merge == IR_NE) + asm_guardcc(as, CC_EQ); + else if (destused) + emit_loada(as, dest, niltvg(J2G(as->J))); + + /* Follow hash chain until the end. */ + l_loop = --as->mcp; + emit_ai(as, PPCI_CMPWI, dest, 0); + emit_tai(as, PPCI_LWZ, dest, dest, (int32_t)offsetof(Node, next)); + l_next = emit_label(as); + + /* Type and value comparison. */ + if (merge == IR_EQ) + asm_guardcc(as, CC_EQ); + else + emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); + if (irt_isnum(kt)) { + emit_fab(as, PPCI_FCMPU, 0, tmpnum, key); + emit_condbranch(as, PPCI_BC, CC_GE, l_next); + emit_ab(as, PPCI_CMPLW, tmp1, tisnum); + emit_fai(as, PPCI_LFD, tmpnum, dest, (int32_t)offsetof(Node, key.n)); + } else { + if (!irt_ispri(kt)) { + emit_ab(as, PPCI_CMPW, tmp2, key); + emit_condbranch(as, PPCI_BC, CC_NE, l_next); + } + emit_ai(as, PPCI_CMPWI, tmp1, irt_toitype(irkey->t)); + if (!irt_ispri(kt)) + emit_tai(as, PPCI_LWZ, tmp2, dest, (int32_t)offsetof(Node, key.gcr)); + } + emit_tai(as, PPCI_LWZ, tmp1, dest, (int32_t)offsetof(Node, key.it)); + *l_loop = PPCI_BC | PPCF_Y | PPCF_CC(CC_NE) | + (((char *)as->mcp-(char *)l_loop) & 0xffffu); + + /* Load main position relative to tab->node into dest. */ + khash = irref_isk(refkey) ? ir_khash(irkey) : 1; + if (khash == 0) { + emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node)); + } else { + Reg tmphash = tmp1; + if (irref_isk(refkey)) + tmphash = ra_allock(as, khash, allow); + emit_tab(as, PPCI_ADD, dest, dest, tmp1); + emit_tai(as, PPCI_MULLI, tmp1, tmp1, sizeof(Node)); + emit_asb(as, PPCI_AND, tmp1, tmp2, tmphash); + emit_tai(as, PPCI_LWZ, dest, tab, (int32_t)offsetof(GCtab, node)); + emit_tai(as, PPCI_LWZ, tmp2, tab, (int32_t)offsetof(GCtab, hmask)); + if (irref_isk(refkey)) { + /* Nothing to do. */ + } else if (irt_isstr(kt)) { + emit_tai(as, PPCI_LWZ, tmp1, key, (int32_t)offsetof(GCstr, hash)); + } else { /* Must match with hash*() in lj_tab.c. */ + emit_tab(as, PPCI_SUBF, tmp1, tmp2, tmp1); + emit_rotlwi(as, tmp2, tmp2, HASH_ROT3); + emit_asb(as, PPCI_XOR, tmp1, tmp1, tmp2); + emit_rotlwi(as, tmp1, tmp1, (HASH_ROT2+HASH_ROT1)&31); + emit_tab(as, PPCI_SUBF, tmp2, dest, tmp2); + if (irt_isnum(kt)) { + int32_t ofs = ra_spill(as, irkey); + emit_asb(as, PPCI_XOR, tmp2, tmp2, tmp1); + emit_rotlwi(as, dest, tmp1, HASH_ROT1); + emit_tab(as, PPCI_ADD, tmp1, tmp1, tmp1); + emit_tai(as, PPCI_LWZ, tmp2, RID_SP, ofs+4); + emit_tai(as, PPCI_LWZ, tmp1, RID_SP, ofs); + } else { + emit_asb(as, PPCI_XOR, tmp2, key, tmp1); + emit_rotlwi(as, dest, tmp1, HASH_ROT1); + emit_tai(as, PPCI_ADDI, tmp1, tmp2, HASH_BIAS); + emit_tai(as, PPCI_ADDIS, tmp2, key, (HASH_BIAS + 32768)>>16); + } + } + } +} + +static void asm_hrefk(ASMState *as, IRIns *ir) +{ + IRIns *kslot = IR(ir->op2); + IRIns *irkey = IR(kslot->op1); + int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); + int32_t kofs = ofs + (int32_t)offsetof(Node, key); + Reg dest = (ra_used(ir)||ofs > 32736) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; + Reg node = ra_alloc1(as, ir->op1, RSET_GPR); + Reg key = RID_NONE, type = RID_TMP, idx = node; + RegSet allow = rset_exclude(RSET_GPR, node); + lua_assert(ofs % sizeof(Node) == 0); + if (ofs > 32736) { + idx = dest; + rset_clear(allow, dest); + kofs = (int32_t)offsetof(Node, key); + } else if (ra_hasreg(dest)) { + emit_tai(as, PPCI_ADDI, dest, node, ofs); + } + asm_guardcc(as, CC_NE); + if (!irt_ispri(irkey->t)) { + key = ra_scratch(as, allow); + rset_clear(allow, key); + } + rset_clear(allow, type); + if (irt_isnum(irkey->t)) { + emit_cmpi(as, key, (int32_t)ir_knum(irkey)->u32.lo); + asm_guardcc(as, CC_NE); + emit_cmpi(as, type, (int32_t)ir_knum(irkey)->u32.hi); + } else { + if (ra_hasreg(key)) { + emit_cmpi(as, key, irkey->i); /* May use RID_TMP, i.e. type. */ + asm_guardcc(as, CC_NE); + } + emit_ai(as, PPCI_CMPWI, type, irt_toitype(irkey->t)); + } + if (ra_hasreg(key)) emit_tai(as, PPCI_LWZ, key, idx, kofs+4); + emit_tai(as, PPCI_LWZ, type, idx, kofs); + if (ofs > 32736) { + emit_tai(as, PPCI_ADDIS, dest, dest, (ofs + 32768) >> 16); + emit_tai(as, PPCI_ADDI, dest, node, ofs); + } +} + +static void asm_newref(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; + IRRef args[3]; + if (ir->r == RID_SINK) + return; + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ir->op1; /* GCtab *t */ + args[2] = ASMREF_TMP1; /* cTValue *key */ + asm_setupresult(as, ir, ci); /* TValue * */ + asm_gencall(as, ci, args); + asm_tvptr(as, ra_releasetmp(as, ASMREF_TMP1), ir->op2); +} + +static void asm_uref(ASMState *as, IRIns *ir) +{ + /* NYI: Check that UREFO is still open and not aliasing a slot. */ + Reg dest = ra_dest(as, ir, RSET_GPR); + if (irref_isk(ir->op1)) { + GCfunc *fn = ir_kfunc(IR(ir->op1)); + MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; + emit_lsptr(as, PPCI_LWZ, dest, v, RSET_GPR); + } else { + Reg uv = ra_scratch(as, RSET_GPR); + Reg func = ra_alloc1(as, ir->op1, RSET_GPR); + if (ir->o == IR_UREFC) { + asm_guardcc(as, CC_NE); + emit_ai(as, PPCI_CMPWI, RID_TMP, 1); + emit_tai(as, PPCI_ADDI, dest, uv, (int32_t)offsetof(GCupval, tv)); + emit_tai(as, PPCI_LBZ, RID_TMP, uv, (int32_t)offsetof(GCupval, closed)); + } else { + emit_tai(as, PPCI_LWZ, dest, uv, (int32_t)offsetof(GCupval, v)); + } + emit_tai(as, PPCI_LWZ, uv, func, + (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); + } +} + +static void asm_fref(ASMState *as, IRIns *ir) +{ + UNUSED(as); UNUSED(ir); + lua_assert(!ra_used(ir)); +} + +static void asm_strref(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + IRRef ref = ir->op2, refk = ir->op1; + int32_t ofs = (int32_t)sizeof(GCstr); + Reg r; + if (irref_isk(ref)) { + IRRef tmp = refk; refk = ref; ref = tmp; + } else if (!irref_isk(refk)) { + Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); + IRIns *irr = IR(ir->op2); + if (ra_hasreg(irr->r)) { + ra_noweak(as, irr->r); + right = irr->r; + } else if (mayfuse(as, irr->op2) && + irr->o == IR_ADD && irref_isk(irr->op2) && + checki16(ofs + IR(irr->op2)->i)) { + ofs += IR(irr->op2)->i; + right = ra_alloc1(as, irr->op1, rset_exclude(RSET_GPR, left)); + } else { + right = ra_allocref(as, ir->op2, rset_exclude(RSET_GPR, left)); + } + emit_tai(as, PPCI_ADDI, dest, dest, ofs); + emit_tab(as, PPCI_ADD, dest, left, right); + return; + } + r = ra_alloc1(as, ref, RSET_GPR); + ofs += IR(refk)->i; + if (checki16(ofs)) + emit_tai(as, PPCI_ADDI, dest, r, ofs); + else + emit_tab(as, PPCI_ADD, dest, r, + ra_allock(as, ofs, rset_exclude(RSET_GPR, r))); +} + +/* -- Loads and stores ---------------------------------------------------- */ + +static PPCIns asm_fxloadins(IRIns *ir) +{ + switch (irt_type(ir->t)) { + case IRT_I8: return PPCI_LBZ; /* Needs sign-extension. */ + case IRT_U8: return PPCI_LBZ; + case IRT_I16: return PPCI_LHA; + case IRT_U16: return PPCI_LHZ; + case IRT_NUM: return PPCI_LFD; + case IRT_FLOAT: return PPCI_LFS; + default: return PPCI_LWZ; + } +} + +static PPCIns asm_fxstoreins(IRIns *ir) +{ + switch (irt_type(ir->t)) { + case IRT_I8: case IRT_U8: return PPCI_STB; + case IRT_I16: case IRT_U16: return PPCI_STH; + case IRT_NUM: return PPCI_STFD; + case IRT_FLOAT: return PPCI_STFS; + default: return PPCI_STW; + } +} + +static void asm_fload(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg idx = ra_alloc1(as, ir->op1, RSET_GPR); + PPCIns pi = asm_fxloadins(ir); + int32_t ofs; + if (ir->op2 == IRFL_TAB_ARRAY) { + ofs = asm_fuseabase(as, ir->op1); + if (ofs) { /* Turn the t->array load into an add for colocated arrays. */ + emit_tai(as, PPCI_ADDI, dest, idx, ofs); + return; + } + } + ofs = field_ofs[ir->op2]; + lua_assert(!irt_isi8(ir->t)); + emit_tai(as, pi, dest, idx, ofs); +} + +static void asm_fstore(ASMState *as, IRIns *ir) +{ + if (ir->r != RID_SINK) { + Reg src = ra_alloc1(as, ir->op2, RSET_GPR); + IRIns *irf = IR(ir->op1); + Reg idx = ra_alloc1(as, irf->op1, rset_exclude(RSET_GPR, src)); + int32_t ofs = field_ofs[irf->op2]; + PPCIns pi = asm_fxstoreins(ir); + emit_tai(as, pi, src, idx, ofs); + } +} + +static void asm_xload(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); + lua_assert(!(ir->op2 & IRXLOAD_UNALIGNED)); + if (irt_isi8(ir->t)) + emit_as(as, PPCI_EXTSB, dest, dest); + asm_fusexref(as, asm_fxloadins(ir), dest, ir->op1, RSET_GPR, 0); +} + +static void asm_xstore(ASMState *as, IRIns *ir, int32_t ofs) +{ + IRIns *irb; + if (ir->r == RID_SINK) + return; + if (ofs == 0 && mayfuse(as, ir->op2) && (irb = IR(ir->op2))->o == IR_BSWAP && + ra_noreg(irb->r) && (irt_isint(ir->t) || irt_isu32(ir->t))) { + /* Fuse BSWAP with XSTORE to stwbrx. */ + Reg src = ra_alloc1(as, irb->op1, RSET_GPR); + asm_fusexrefx(as, PPCI_STWBRX, src, ir->op1, rset_exclude(RSET_GPR, src)); + } else { + Reg src = ra_alloc1(as, ir->op2, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); + asm_fusexref(as, asm_fxstoreins(ir), src, ir->op1, + rset_exclude(RSET_GPR, src), ofs); + } +} + +static void asm_ahuvload(ASMState *as, IRIns *ir) +{ + IRType1 t = ir->t; + Reg dest = RID_NONE, type = RID_TMP, tmp = RID_TMP, idx; + RegSet allow = RSET_GPR; + int32_t ofs = AHUREF_LSX; + if (ra_used(ir)) { + lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); + if (!irt_isnum(t)) ofs = 0; + dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); + rset_clear(allow, dest); + } + idx = asm_fuseahuref(as, ir->op1, &ofs, allow); + if (irt_isnum(t)) { + Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, rset_exclude(allow, idx)); + asm_guardcc(as, CC_GE); + emit_ab(as, PPCI_CMPLW, type, tisnum); + if (ra_hasreg(dest)) { + if (ofs == AHUREF_LSX) { + tmp = ra_scratch(as, rset_exclude(rset_exclude(RSET_GPR, + (idx&255)), (idx>>8))); + emit_fab(as, PPCI_LFDX, dest, (idx&255), tmp); + } else { + emit_fai(as, PPCI_LFD, dest, idx, ofs); + } + } + } else { + asm_guardcc(as, CC_NE); + emit_ai(as, PPCI_CMPWI, type, irt_toitype(t)); + if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, idx, ofs+4); + } + if (ofs == AHUREF_LSX) { + emit_tab(as, PPCI_LWZX, type, (idx&255), tmp); + emit_slwi(as, tmp, (idx>>8), 3); + } else { + emit_tai(as, PPCI_LWZ, type, idx, ofs); + } +} + +static void asm_ahustore(ASMState *as, IRIns *ir) +{ + RegSet allow = RSET_GPR; + Reg idx, src = RID_NONE, type = RID_NONE; + int32_t ofs = AHUREF_LSX; + if (ir->r == RID_SINK) + return; + if (irt_isnum(ir->t)) { + src = ra_alloc1(as, ir->op2, RSET_FPR); + } else { + if (!irt_ispri(ir->t)) { + src = ra_alloc1(as, ir->op2, allow); + rset_clear(allow, src); + ofs = 0; + } + type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); + rset_clear(allow, type); + } + idx = asm_fuseahuref(as, ir->op1, &ofs, allow); + if (irt_isnum(ir->t)) { + if (ofs == AHUREF_LSX) { + emit_fab(as, PPCI_STFDX, src, (idx&255), RID_TMP); + emit_slwi(as, RID_TMP, (idx>>8), 3); + } else { + emit_fai(as, PPCI_STFD, src, idx, ofs); + } + } else { + if (ra_hasreg(src)) + emit_tai(as, PPCI_STW, src, idx, ofs+4); + if (ofs == AHUREF_LSX) { + emit_tab(as, PPCI_STWX, type, (idx&255), RID_TMP); + emit_slwi(as, RID_TMP, (idx>>8), 3); + } else { + emit_tai(as, PPCI_STW, type, idx, ofs); + } + } +} + +static void asm_sload(ASMState *as, IRIns *ir) +{ + int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 0 : 4); + IRType1 t = ir->t; + Reg dest = RID_NONE, type = RID_NONE, base; + RegSet allow = RSET_GPR; + lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ + lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK)); + lua_assert(LJ_DUALNUM || + !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME))); + if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { + dest = ra_scratch(as, RSET_FPR); + asm_tointg(as, ir, dest); + t.irt = IRT_NUM; /* Continue with a regular number type check. */ + } else if (ra_used(ir)) { + lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); + dest = ra_dest(as, ir, irt_isnum(t) ? RSET_FPR : RSET_GPR); + rset_clear(allow, dest); + base = ra_alloc1(as, REF_BASE, allow); + rset_clear(allow, base); + if ((ir->op2 & IRSLOAD_CONVERT)) { + if (irt_isint(t)) { + emit_tai(as, PPCI_LWZ, dest, RID_SP, SPOFS_TMPLO); + dest = ra_scratch(as, RSET_FPR); + emit_fai(as, PPCI_STFD, dest, RID_SP, SPOFS_TMP); + emit_fb(as, PPCI_FCTIWZ, dest, dest); + t.irt = IRT_NUM; /* Check for original type. */ + } else { + Reg tmp = ra_scratch(as, allow); + Reg hibias = ra_allock(as, 0x43300000, rset_clear(allow, tmp)); + Reg fbias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); + emit_fab(as, PPCI_FSUB, dest, dest, fbias); + emit_fai(as, PPCI_LFD, dest, RID_SP, SPOFS_TMP); + emit_lsptr(as, PPCI_LFS, (fbias & 31), + (void *)lj_ir_k64_find(as->J, U64x(59800004,59800000)), + rset_clear(allow, hibias)); + emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPLO); + emit_tai(as, PPCI_STW, hibias, RID_SP, SPOFS_TMPHI); + emit_asi(as, PPCI_XORIS, tmp, tmp, 0x8000); + dest = tmp; + t.irt = IRT_INT; /* Check for original type. */ + } + } + goto dotypecheck; + } + base = ra_alloc1(as, REF_BASE, allow); + rset_clear(allow, base); +dotypecheck: + if (irt_isnum(t)) { + if ((ir->op2 & IRSLOAD_TYPECHECK)) { + Reg tisnum = ra_allock(as, (int32_t)LJ_TISNUM, allow); + asm_guardcc(as, CC_GE); + emit_ab(as, PPCI_CMPLW, RID_TMP, tisnum); + type = RID_TMP; + } + if (ra_hasreg(dest)) emit_fai(as, PPCI_LFD, dest, base, ofs-4); + } else { + if ((ir->op2 & IRSLOAD_TYPECHECK)) { + asm_guardcc(as, CC_NE); + emit_ai(as, PPCI_CMPWI, RID_TMP, irt_toitype(t)); + type = RID_TMP; + } + if (ra_hasreg(dest)) emit_tai(as, PPCI_LWZ, dest, base, ofs); + } + if (ra_hasreg(type)) emit_tai(as, PPCI_LWZ, type, base, ofs-4); +} + +/* -- Allocations --------------------------------------------------------- */ + +#if LJ_HASFFI +static void asm_cnew(ASMState *as, IRIns *ir) +{ + CTState *cts = ctype_ctsG(J2G(as->J)); + CTypeID ctypeid = (CTypeID)IR(ir->op1)->i; + CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? + lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i; + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; + IRRef args[2]; + RegSet allow = (RSET_GPR & ~RSET_SCRATCH); + RegSet drop = RSET_SCRATCH; + lua_assert(sz != CTSIZE_INVALID); + + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ASMREF_TMP1; /* MSize size */ + as->gcsteps++; + + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + ra_evictset(as, drop); + if (ra_used(ir)) + ra_destreg(as, ir, RID_RET); /* GCcdata * */ + + /* Initialize immutable cdata object. */ + if (ir->o == IR_CNEWI) { + int32_t ofs = sizeof(GCcdata); + lua_assert(sz == 4 || sz == 8); + if (sz == 8) { + ofs += 4; + lua_assert((ir+1)->o == IR_HIOP); + } + for (;;) { + Reg r = ra_alloc1(as, ir->op2, allow); + emit_tai(as, PPCI_STW, r, RID_RET, ofs); + rset_clear(allow, r); + if (ofs == sizeof(GCcdata)) break; + ofs -= 4; ir++; + } + } + /* Initialize gct and ctypeid. lj_mem_newgco() already sets marked. */ + emit_tai(as, PPCI_STB, RID_RET+1, RID_RET, offsetof(GCcdata, gct)); + emit_tai(as, PPCI_STH, RID_TMP, RID_RET, offsetof(GCcdata, ctypeid)); + emit_ti(as, PPCI_LI, RID_RET+1, ~LJ_TCDATA); + emit_ti(as, PPCI_LI, RID_TMP, ctypeid); /* Lower 16 bit used. Sign-ext ok. */ + asm_gencall(as, ci, args); + ra_allockreg(as, (int32_t)(sz+sizeof(GCcdata)), + ra_releasetmp(as, ASMREF_TMP1)); +} +#else +#define asm_cnew(as, ir) ((void)0) +#endif + +/* -- Write barriers ------------------------------------------------------ */ + +static void asm_tbar(ASMState *as, IRIns *ir) +{ + Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); + Reg mark = ra_scratch(as, rset_exclude(RSET_GPR, tab)); + Reg link = RID_TMP; + MCLabel l_end = emit_label(as); + emit_tai(as, PPCI_STW, link, tab, (int32_t)offsetof(GCtab, gclist)); + emit_tai(as, PPCI_STB, mark, tab, (int32_t)offsetof(GCtab, marked)); + emit_setgl(as, tab, gc.grayagain); + lua_assert(LJ_GC_BLACK == 0x04); + emit_rot(as, PPCI_RLWINM, mark, mark, 0, 30, 28); /* Clear black bit. */ + emit_getgl(as, link, gc.grayagain); + emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); + emit_asi(as, PPCI_ANDIDOT, RID_TMP, mark, LJ_GC_BLACK); + emit_tai(as, PPCI_LBZ, mark, tab, (int32_t)offsetof(GCtab, marked)); +} + +static void asm_obar(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; + IRRef args[2]; + MCLabel l_end; + Reg obj, val, tmp; + /* No need for other object barriers (yet). */ + lua_assert(IR(ir->op1)->o == IR_UREFC); + ra_evictset(as, RSET_SCRATCH); + l_end = emit_label(as); + args[0] = ASMREF_TMP1; /* global_State *g */ + args[1] = ir->op1; /* TValue *tv */ + asm_gencall(as, ci, args); + emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); + obj = IR(ir->op1)->r; + tmp = ra_scratch(as, rset_exclude(RSET_GPR, obj)); + emit_condbranch(as, PPCI_BC|PPCF_Y, CC_EQ, l_end); + emit_asi(as, PPCI_ANDIDOT, tmp, tmp, LJ_GC_BLACK); + emit_condbranch(as, PPCI_BC, CC_EQ, l_end); + emit_asi(as, PPCI_ANDIDOT, RID_TMP, RID_TMP, LJ_GC_WHITES); + val = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, obj)); + emit_tai(as, PPCI_LBZ, tmp, obj, + (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); + emit_tai(as, PPCI_LBZ, RID_TMP, val, (int32_t)offsetof(GChead, marked)); +} + +/* -- Arithmetic and logic operations ------------------------------------- */ + +static void asm_fparith(ASMState *as, IRIns *ir, PPCIns pi) +{ + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg right, left = ra_alloc2(as, ir, RSET_FPR); + right = (left >> 8); left &= 255; + if (pi == PPCI_FMUL) + emit_fac(as, pi, dest, left, right); + else + emit_fab(as, pi, dest, left, right); +} + +static void asm_fpunary(ASMState *as, IRIns *ir, PPCIns pi) +{ + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg left = ra_hintalloc(as, ir->op1, dest, RSET_FPR); + emit_fb(as, pi, dest, left); +} + +static int asm_fpjoin_pow(ASMState *as, IRIns *ir) +{ + IRIns *irp = IR(ir->op1); + if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { + IRIns *irpp = IR(irp->op1); + if (irpp == ir-2 && irpp->o == IR_FPMATH && + irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_pow]; + IRRef args[2]; + args[0] = irpp->op1; + args[1] = irp->op2; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); + return 1; + } + } + return 0; +} + +static void asm_add(ASMState *as, IRIns *ir) +{ + if (irt_isnum(ir->t)) { + if (!asm_fusemadd(as, ir, PPCI_FMADD, PPCI_FMADD)) + asm_fparith(as, ir, PPCI_FADD); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + PPCIns pi; + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (checki16(k)) { + pi = PPCI_ADDI; + /* May fail due to spills/restores above, but simplifies the logic. */ + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + pi = PPCI_ADDICDOT; + } + emit_tai(as, pi, dest, left, k); + return; + } else if ((k & 0xffff) == 0) { + emit_tai(as, PPCI_ADDIS, dest, left, (k >> 16)); + return; + } else if (!as->sectref) { + emit_tai(as, PPCI_ADDIS, dest, dest, (k + 32768) >> 16); + emit_tai(as, PPCI_ADDI, dest, left, k); + return; + } + } + pi = PPCI_ADD; + /* May fail due to spills/restores above, but simplifies the logic. */ + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + pi |= PPCF_DOT; + } + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_tab(as, pi, dest, left, right); + } +} + +static void asm_sub(ASMState *as, IRIns *ir) +{ + if (irt_isnum(ir->t)) { + if (!asm_fusemadd(as, ir, PPCI_FMSUB, PPCI_FNMSUB)) + asm_fparith(as, ir, PPCI_FSUB); + } else { + PPCIns pi = PPCI_SUBF; + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left, right; + if (irref_isk(ir->op1)) { + int32_t k = IR(ir->op1)->i; + if (checki16(k)) { + right = ra_alloc1(as, ir->op2, RSET_GPR); + emit_tai(as, PPCI_SUBFIC, dest, right, k); + return; + } + } + /* May fail due to spills/restores above, but simplifies the logic. */ + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + pi |= PPCF_DOT; + } + left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */ + } +} + +static void asm_mul(ASMState *as, IRIns *ir) +{ + if (irt_isnum(ir->t)) { + asm_fparith(as, ir, PPCI_FMUL); + } else { + PPCIns pi = PPCI_MULLW; + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (checki16(k)) { + emit_tai(as, PPCI_MULLI, dest, left, k); + return; + } + } + /* May fail due to spills/restores above, but simplifies the logic. */ + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + pi |= PPCF_DOT; + } + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_tab(as, pi, dest, left, right); + } +} + +static void asm_neg(ASMState *as, IRIns *ir) +{ + if (irt_isnum(ir->t)) { + asm_fpunary(as, ir, PPCI_FNEG); + } else { + Reg dest, left; + PPCIns pi = PPCI_NEG; + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + pi |= PPCF_DOT; + } + dest = ra_dest(as, ir, RSET_GPR); + left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + emit_tab(as, pi, dest, left, 0); + } +} + +static void asm_arithov(ASMState *as, IRIns *ir, PPCIns pi) +{ + Reg dest, left, right; + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + } + asm_guardcc(as, CC_SO); + dest = ra_dest(as, ir, RSET_GPR); + left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + if (pi == PPCI_SUBFO) { Reg tmp = left; left = right; right = tmp; } + emit_tab(as, pi|PPCF_DOT, dest, left, right); +} + +#if LJ_HASFFI +static void asm_add64(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_alloc1(as, ir->op1, RSET_GPR); + PPCIns pi = PPCI_ADDE; + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (k == 0) + pi = PPCI_ADDZE; + else if (k == -1) + pi = PPCI_ADDME; + else + goto needright; + right = 0; + } else { + needright: + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + } + emit_tab(as, pi, dest, left, right); + ir--; + dest = ra_dest(as, ir, RSET_GPR); + left = ra_alloc1(as, ir->op1, RSET_GPR); + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (checki16(k)) { + emit_tai(as, PPCI_ADDIC, dest, left, k); + return; + } + } + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_tab(as, PPCI_ADDC, dest, left, right); +} + +static void asm_sub64(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left, right = ra_alloc1(as, ir->op2, RSET_GPR); + PPCIns pi = PPCI_SUBFE; + if (irref_isk(ir->op1)) { + int32_t k = IR(ir->op1)->i; + if (k == 0) + pi = PPCI_SUBFZE; + else if (k == -1) + pi = PPCI_SUBFME; + else + goto needleft; + left = 0; + } else { + needleft: + left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right)); + } + emit_tab(as, pi, dest, right, left); /* Subtract right _from_ left. */ + ir--; + dest = ra_dest(as, ir, RSET_GPR); + right = ra_alloc1(as, ir->op2, RSET_GPR); + if (irref_isk(ir->op1)) { + int32_t k = IR(ir->op1)->i; + if (checki16(k)) { + emit_tai(as, PPCI_SUBFIC, dest, right, k); + return; + } + } + left = ra_alloc1(as, ir->op1, rset_exclude(RSET_GPR, right)); + emit_tab(as, PPCI_SUBFC, dest, right, left); +} + +static void asm_neg64(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg left = ra_alloc1(as, ir->op1, RSET_GPR); + emit_tab(as, PPCI_SUBFZE, dest, left, 0); + ir--; + dest = ra_dest(as, ir, RSET_GPR); + left = ra_alloc1(as, ir->op1, RSET_GPR); + emit_tai(as, PPCI_SUBFIC, dest, left, 0); +} +#endif + +static void asm_bitnot(ASMState *as, IRIns *ir) +{ + Reg dest, left, right; + PPCIns pi = PPCI_NOR; + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + pi |= PPCF_DOT; + } + dest = ra_dest(as, ir, RSET_GPR); + if (mayfuse(as, ir->op1)) { + IRIns *irl = IR(ir->op1); + if (irl->o == IR_BAND) + pi ^= (PPCI_NOR ^ PPCI_NAND); + else if (irl->o == IR_BXOR) + pi ^= (PPCI_NOR ^ PPCI_EQV); + else if (irl->o != IR_BOR) + goto nofuse; + left = ra_hintalloc(as, irl->op1, dest, RSET_GPR); + right = ra_alloc1(as, irl->op2, rset_exclude(RSET_GPR, left)); + } else { +nofuse: + left = right = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + } + emit_asb(as, pi, dest, left, right); +} + +static void asm_bitswap(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + IRIns *irx; + if (mayfuse(as, ir->op1) && (irx = IR(ir->op1))->o == IR_XLOAD && + ra_noreg(irx->r) && (irt_isint(irx->t) || irt_isu32(irx->t))) { + /* Fuse BSWAP with XLOAD to lwbrx. */ + asm_fusexrefx(as, PPCI_LWBRX, dest, irx->op1, RSET_GPR); + } else { + Reg left = ra_alloc1(as, ir->op1, RSET_GPR); + Reg tmp = dest; + if (tmp == left) { + tmp = RID_TMP; + emit_mr(as, dest, RID_TMP); + } + emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 16, 23); + emit_rot(as, PPCI_RLWIMI, tmp, left, 24, 0, 7); + emit_rotlwi(as, tmp, left, 8); + } +} + +static void asm_bitop(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg right, left = ra_hintalloc(as, ir->op1, dest, RSET_GPR); + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + Reg tmp = left; + if ((checku16(k) || (k & 0xffff) == 0) || (tmp = dest, !as->sectref)) { + if (!checku16(k)) { + emit_asi(as, pik ^ (PPCI_ORI ^ PPCI_ORIS), dest, tmp, (k >> 16)); + if ((k & 0xffff) == 0) return; + } + emit_asi(as, pik, dest, left, k); + return; + } + } + /* May fail due to spills/restores above, but simplifies the logic. */ + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + pi |= PPCF_DOT; + } + right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_asb(as, pi, dest, left, right); +} + +/* Fuse BAND with contiguous bitmask and a shift to rlwinm. */ +static void asm_fuseandsh(ASMState *as, PPCIns pi, int32_t mask, IRRef ref) +{ + IRIns *ir; + Reg left; + if (mayfuse(as, ref) && (ir = IR(ref), ra_noreg(ir->r)) && + irref_isk(ir->op2) && ir->o >= IR_BSHL && ir->o <= IR_BROR) { + int32_t sh = (IR(ir->op2)->i & 31); + switch (ir->o) { + case IR_BSHL: + if ((mask & ((1u<>sh))) goto nofuse; + sh = ((32-sh)&31); + break; + case IR_BROL: + break; + default: + goto nofuse; + } + left = ra_alloc1(as, ir->op1, RSET_GPR); + *--as->mcp = pi | PPCF_T(left) | PPCF_B(sh); + return; + } +nofuse: + left = ra_alloc1(as, ref, RSET_GPR); + *--as->mcp = pi | PPCF_T(left); +} + +static void asm_bitand(ASMState *as, IRIns *ir) +{ + Reg dest, left, right; + IRRef lref = ir->op1; + PPCIns dot = 0; + IRRef op2; + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + dot = PPCF_DOT; + } + dest = ra_dest(as, ir, RSET_GPR); + if (irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (k) { + /* First check for a contiguous bitmask as used by rlwinm. */ + uint32_t s1 = lj_ffs((uint32_t)k); + uint32_t k1 = ((uint32_t)k >> s1); + if ((k1 & (k1+1)) == 0) { + asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) | + PPCF_MB(31-lj_fls((uint32_t)k)) | PPCF_ME(31-s1), + k, lref); + return; + } + if (~(uint32_t)k) { + uint32_t s2 = lj_ffs(~(uint32_t)k); + uint32_t k2 = (~(uint32_t)k >> s2); + if ((k2 & (k2+1)) == 0) { + asm_fuseandsh(as, PPCI_RLWINM|dot | PPCF_A(dest) | + PPCF_MB(32-s2) | PPCF_ME(30-lj_fls(~(uint32_t)k)), + k, lref); + return; + } + } + } + if (checku16(k)) { + left = ra_alloc1(as, lref, RSET_GPR); + emit_asi(as, PPCI_ANDIDOT, dest, left, k); + return; + } else if ((k & 0xffff) == 0) { + left = ra_alloc1(as, lref, RSET_GPR); + emit_asi(as, PPCI_ANDISDOT, dest, left, (k >> 16)); + return; + } + } + op2 = ir->op2; + if (mayfuse(as, op2) && IR(op2)->o == IR_BNOT && ra_noreg(IR(op2)->r)) { + dot ^= (PPCI_AND ^ PPCI_ANDC); + op2 = IR(op2)->op1; + } + left = ra_hintalloc(as, lref, dest, RSET_GPR); + right = ra_alloc1(as, op2, rset_exclude(RSET_GPR, left)); + emit_asb(as, PPCI_AND ^ dot, dest, left, right); +} + +static void asm_bitshift(ASMState *as, IRIns *ir, PPCIns pi, PPCIns pik) +{ + Reg dest, left; + Reg dot = 0; + if (as->flagmcp == as->mcp) { + as->flagmcp = NULL; + as->mcp++; + dot = PPCF_DOT; + } + dest = ra_dest(as, ir, RSET_GPR); + left = ra_alloc1(as, ir->op1, RSET_GPR); + if (irref_isk(ir->op2)) { /* Constant shifts. */ + int32_t shift = (IR(ir->op2)->i & 31); + if (pik == 0) /* SLWI */ + emit_rot(as, PPCI_RLWINM|dot, dest, left, shift, 0, 31-shift); + else if (pik == 1) /* SRWI */ + emit_rot(as, PPCI_RLWINM|dot, dest, left, (32-shift)&31, shift, 31); + else + emit_asb(as, pik|dot, dest, left, shift); + } else { + Reg right = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, left)); + emit_asb(as, pi|dot, dest, left, right); + } +} + +static void asm_min_max(ASMState *as, IRIns *ir, int ismax) +{ + if (irt_isnum(ir->t)) { + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg tmp = dest; + Reg right, left = ra_alloc2(as, ir, RSET_FPR); + right = (left >> 8); left &= 255; + if (tmp == left || tmp == right) + tmp = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_FPR, + dest), left), right)); + emit_facb(as, PPCI_FSEL, dest, tmp, + ismax ? left : right, ismax ? right : left); + emit_fab(as, PPCI_FSUB, tmp, left, right); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg tmp1 = RID_TMP, tmp2 = dest; + Reg right, left = ra_alloc2(as, ir, RSET_GPR); + right = (left >> 8); left &= 255; + if (tmp2 == left || tmp2 == right) + tmp2 = ra_scratch(as, rset_exclude(rset_exclude(rset_exclude(RSET_GPR, + dest), left), right)); + emit_tab(as, PPCI_ADD, dest, tmp2, right); + emit_asb(as, ismax ? PPCI_ANDC : PPCI_AND, tmp2, tmp2, tmp1); + emit_tab(as, PPCI_SUBFE, tmp1, tmp1, tmp1); + emit_tab(as, PPCI_SUBFC, tmp2, tmp2, tmp1); + emit_asi(as, PPCI_XORIS, tmp2, right, 0x8000); + emit_asi(as, PPCI_XORIS, tmp1, left, 0x8000); + } +} + +/* -- Comparisons --------------------------------------------------------- */ + +#define CC_UNSIGNED 0x08 /* Unsigned integer comparison. */ +#define CC_TWO 0x80 /* Check two flags for FP comparison. */ + +/* Map of comparisons to flags. ORDER IR. */ +static const uint8_t asm_compmap[IR_ABC+1] = { + /* op int cc FP cc */ + /* LT */ CC_GE + (CC_GE<<4), + /* GE */ CC_LT + (CC_LE<<4) + CC_TWO, + /* LE */ CC_GT + (CC_GE<<4) + CC_TWO, + /* GT */ CC_LE + (CC_LE<<4), + /* ULT */ CC_GE + CC_UNSIGNED + (CC_GT<<4) + CC_TWO, + /* UGE */ CC_LT + CC_UNSIGNED + (CC_LT<<4), + /* ULE */ CC_GT + CC_UNSIGNED + (CC_GT<<4), + /* UGT */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO, + /* EQ */ CC_NE + (CC_NE<<4), + /* NE */ CC_EQ + (CC_EQ<<4), + /* ABC */ CC_LE + CC_UNSIGNED + (CC_LT<<4) + CC_TWO /* Same as UGT. */ +}; + +static void asm_intcomp_(ASMState *as, IRRef lref, IRRef rref, Reg cr, PPCCC cc) +{ + Reg right, left = ra_alloc1(as, lref, RSET_GPR); + if (irref_isk(rref)) { + int32_t k = IR(rref)->i; + if ((cc & CC_UNSIGNED) == 0) { /* Signed comparison with constant. */ + if (checki16(k)) { + emit_tai(as, PPCI_CMPWI, cr, left, k); + /* Signed comparison with zero and referencing previous ins? */ + if (k == 0 && lref == as->curins-1) + as->flagmcp = as->mcp; /* Allow elimination of the compare. */ + return; + } else if ((cc & 3) == (CC_EQ & 3)) { /* Use CMPLWI for EQ or NE. */ + if (checku16(k)) { + emit_tai(as, PPCI_CMPLWI, cr, left, k); + return; + } else if (!as->sectref && ra_noreg(IR(rref)->r)) { + emit_tai(as, PPCI_CMPLWI, cr, RID_TMP, k); + emit_asi(as, PPCI_XORIS, RID_TMP, left, (k >> 16)); + return; + } + } + } else { /* Unsigned comparison with constant. */ + if (checku16(k)) { + emit_tai(as, PPCI_CMPLWI, cr, left, k); + return; + } + } + } + right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, left)); + emit_tab(as, (cc & CC_UNSIGNED) ? PPCI_CMPLW : PPCI_CMPW, cr, left, right); +} + +static void asm_comp(ASMState *as, IRIns *ir) +{ + PPCCC cc = asm_compmap[ir->o]; + if (irt_isnum(ir->t)) { + Reg right, left = ra_alloc2(as, ir, RSET_FPR); + right = (left >> 8); left &= 255; + asm_guardcc(as, (cc >> 4)); + if ((cc & CC_TWO)) + emit_tab(as, PPCI_CROR, ((cc>>4)&3), ((cc>>4)&3), (CC_EQ&3)); + emit_fab(as, PPCI_FCMPU, 0, left, right); + } else { + IRRef lref = ir->op1, rref = ir->op2; + if (irref_isk(lref) && !irref_isk(rref)) { + /* Swap constants to the right (only for ABC). */ + IRRef tmp = lref; lref = rref; rref = tmp; + if ((cc & 2) == 0) cc ^= 1; /* LT <-> GT, LE <-> GE */ + } + asm_guardcc(as, cc); + asm_intcomp_(as, lref, rref, 0, cc); + } +} + +#if LJ_HASFFI +/* 64 bit integer comparisons. */ +static void asm_comp64(ASMState *as, IRIns *ir) +{ + PPCCC cc = asm_compmap[(ir-1)->o]; + if ((cc&3) == (CC_EQ&3)) { + asm_guardcc(as, cc); + emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CROR, + (CC_EQ&3), (CC_EQ&3), 4+(CC_EQ&3)); + } else { + asm_guardcc(as, CC_EQ); + emit_tab(as, PPCI_CROR, (CC_EQ&3), (CC_EQ&3), ((cc^~(cc>>2))&1)); + emit_tab(as, (cc&4) ? PPCI_CRAND : PPCI_CRANDC, + (CC_EQ&3), (CC_EQ&3), 4+(cc&3)); + } + /* Loword comparison sets cr1 and is unsigned, except for equality. */ + asm_intcomp_(as, (ir-1)->op1, (ir-1)->op2, 4, + cc | ((cc&3) == (CC_EQ&3) ? 0 : CC_UNSIGNED)); + /* Hiword comparison sets cr0. */ + asm_intcomp_(as, ir->op1, ir->op2, 0, cc); + as->flagmcp = NULL; /* Doesn't work here. */ +} +#endif + +/* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ + +/* Hiword op of a split 64 bit op. Previous op must be the loword op. */ +static void asm_hiop(ASMState *as, IRIns *ir) +{ +#if LJ_HASFFI + /* HIOP is marked as a store because it needs its own DCE logic. */ + int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ + if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; + if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ + as->curins--; /* Always skip the CONV. */ + if (usehi || uselo) + asm_conv64(as, ir); + return; + } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ + as->curins--; /* Always skip the loword comparison. */ + asm_comp64(as, ir); + return; + } else if ((ir-1)->o == IR_XSTORE) { + as->curins--; /* Handle both stores here. */ + if ((ir-1)->r != RID_SINK) { + asm_xstore(as, ir, 0); + asm_xstore(as, ir-1, 4); + } + return; + } + if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ + switch ((ir-1)->o) { + case IR_ADD: as->curins--; asm_add64(as, ir); break; + case IR_SUB: as->curins--; asm_sub64(as, ir); break; + case IR_NEG: as->curins--; asm_neg64(as, ir); break; + case IR_CALLN: + case IR_CALLXS: + if (!uselo) + ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ + break; + case IR_CNEWI: + /* Nothing to do here. Handled by lo op itself. */ + break; + default: lua_assert(0); break; + } +#else + UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused without FFI. */ +#endif +} + +/* -- Stack handling ------------------------------------------------------ */ + +/* Check Lua stack size for overflow. Use exit handler as fallback. */ +static void asm_stack_check(ASMState *as, BCReg topslot, + IRIns *irp, RegSet allow, ExitNo exitno) +{ + /* Try to get an unused temp. register, otherwise spill/restore RID_RET*. */ + Reg tmp, pbase = irp ? (ra_hasreg(irp->r) ? irp->r : RID_TMP) : RID_BASE; + rset_clear(allow, pbase); + tmp = allow ? rset_pickbot(allow) : + (pbase == RID_RETHI ? RID_RETLO : RID_RETHI); + emit_condbranch(as, PPCI_BC, CC_LT, asm_exitstub_addr(as, exitno)); + if (allow == RSET_EMPTY) /* Restore temp. register. */ + emit_tai(as, PPCI_LWZ, tmp, RID_SP, SPOFS_TMPW); + else + ra_modified(as, tmp); + emit_ai(as, PPCI_CMPLWI, RID_TMP, (int32_t)(8*topslot)); + emit_tab(as, PPCI_SUBF, RID_TMP, pbase, tmp); + emit_tai(as, PPCI_LWZ, tmp, tmp, offsetof(lua_State, maxstack)); + if (pbase == RID_TMP) + emit_getgl(as, RID_TMP, jit_base); + emit_getgl(as, tmp, jit_L); + if (allow == RSET_EMPTY) /* Spill temp. register. */ + emit_tai(as, PPCI_STW, tmp, RID_SP, SPOFS_TMPW); +} + +/* Restore Lua stack from on-trace state. */ +static void asm_stack_restore(ASMState *as, SnapShot *snap) +{ + SnapEntry *map = &as->T->snapmap[snap->mapofs]; + SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; + MSize n, nent = snap->nent; + /* Store the value of all modified slots to the Lua stack. */ + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + BCReg s = snap_slot(sn); + int32_t ofs = 8*((int32_t)s-1); + IRRef ref = snap_ref(sn); + IRIns *ir = IR(ref); + if ((sn & SNAP_NORESTORE)) + continue; + if (irt_isnum(ir->t)) { + Reg src = ra_alloc1(as, ref, RSET_FPR); + emit_fai(as, PPCI_STFD, src, RID_BASE, ofs); + } else { + Reg type; + RegSet allow = rset_exclude(RSET_GPR, RID_BASE); + lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || irt_isinteger(ir->t)); + if (!irt_ispri(ir->t)) { + Reg src = ra_alloc1(as, ref, allow); + rset_clear(allow, src); + emit_tai(as, PPCI_STW, src, RID_BASE, ofs+4); + } + if ((sn & (SNAP_CONT|SNAP_FRAME))) { + if (s == 0) continue; /* Do not overwrite link to previous frame. */ + type = ra_allock(as, (int32_t)(*flinks--), allow); + } else { + type = ra_allock(as, (int32_t)irt_toitype(ir->t), allow); + } + emit_tai(as, PPCI_STW, type, RID_BASE, ofs); + } + checkmclim(as); + } + lua_assert(map + nent == flinks); +} + +/* -- GC handling --------------------------------------------------------- */ + +/* Check GC threshold and do one or more GC steps. */ +static void asm_gc_check(ASMState *as) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; + IRRef args[2]; + MCLabel l_end; + Reg tmp; + ra_evictset(as, RSET_SCRATCH); + l_end = emit_label(as); + /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ + asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ + emit_ai(as, PPCI_CMPWI, RID_RET, 0); + args[0] = ASMREF_TMP1; /* global_State *g */ + args[1] = ASMREF_TMP2; /* MSize steps */ + asm_gencall(as, ci, args); + emit_tai(as, PPCI_ADDI, ra_releasetmp(as, ASMREF_TMP1), RID_JGL, -32768); + tmp = ra_releasetmp(as, ASMREF_TMP2); + emit_loadi(as, tmp, as->gcsteps); + /* Jump around GC step if GC total < GC threshold. */ + emit_condbranch(as, PPCI_BC|PPCF_Y, CC_LT, l_end); + emit_ab(as, PPCI_CMPLW, RID_TMP, tmp); + emit_getgl(as, tmp, gc.threshold); + emit_getgl(as, RID_TMP, gc.total); + as->gcsteps = 0; + checkmclim(as); +} + +/* -- Loop handling ------------------------------------------------------- */ + +/* Fixup the loop branch. */ +static void asm_loop_fixup(ASMState *as) +{ + MCode *p = as->mctop; + MCode *target = as->mcp; + if (as->loopinv) { /* Inverted loop branch? */ + /* asm_guardcc already inverted the cond branch and patched the final b. */ + p[-2] = (p[-2] & (0xffff0000u & ~PPCF_Y)) | (((target-p+2) & 0x3fffu) << 2); + } else { + p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2); + } +} + +/* -- Head of trace ------------------------------------------------------- */ + +/* Coalesce BASE register for a root trace. */ +static void asm_head_root_base(ASMState *as) +{ + IRIns *ir = IR(REF_BASE); + Reg r = ir->r; + if (ra_hasreg(r)) { + ra_free(as, r); + if (rset_test(as->modset, r)) + ir->r = RID_INIT; /* No inheritance for modified BASE register. */ + if (r != RID_BASE) + emit_mr(as, r, RID_BASE); + } +} + +/* Coalesce BASE register for a side trace. */ +static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) +{ + IRIns *ir = IR(REF_BASE); + Reg r = ir->r; + if (ra_hasreg(r)) { + ra_free(as, r); + if (rset_test(as->modset, r)) + ir->r = RID_INIT; /* No inheritance for modified BASE register. */ + if (irp->r == r) { + rset_clear(allow, r); /* Mark same BASE register as coalesced. */ + } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { + rset_clear(allow, irp->r); + emit_mr(as, r, irp->r); /* Move from coalesced parent reg. */ + } else { + emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ + } + } + return allow; +} + +/* -- Tail of trace ------------------------------------------------------- */ + +/* Fixup the tail code. */ +static void asm_tail_fixup(ASMState *as, TraceNo lnk) +{ + MCode *p = as->mctop; + MCode *target; + int32_t spadj = as->T->spadjust; + if (spadj == 0) { + *--p = PPCI_NOP; + *--p = PPCI_NOP; + as->mctop = p; + } else { + /* Patch stack adjustment. */ + lua_assert(checki16(CFRAME_SIZE+spadj)); + p[-3] = PPCI_ADDI | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | (CFRAME_SIZE+spadj); + p[-2] = PPCI_STWU | PPCF_T(RID_TMP) | PPCF_A(RID_SP) | spadj; + } + /* Patch exit branch. */ + target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; + p[-1] = PPCI_B|(((target-p+1)&0x00ffffffu)<<2); +} + +/* Prepare tail of code. */ +static void asm_tail_prep(ASMState *as) +{ + MCode *p = as->mctop - 1; /* Leave room for exit branch. */ + if (as->loopref) { + as->invmcp = as->mcp = p; + } else { + as->mcp = p-2; /* Leave room for stack pointer adjustment. */ + as->invmcp = NULL; + } +} + +/* -- Instruction dispatch ------------------------------------------------ */ + +/* Assemble a single instruction. */ +static void asm_ir(ASMState *as, IRIns *ir) +{ + switch ((IROp)ir->o) { + /* Miscellaneous ops. */ + case IR_LOOP: asm_loop(as); break; + case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; + case IR_USE: + ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; + case IR_PHI: asm_phi(as, ir); break; + case IR_HIOP: asm_hiop(as, ir); break; + case IR_GCSTEP: asm_gcstep(as, ir); break; + + /* Guarded assertions. */ + case IR_EQ: case IR_NE: + if ((ir-1)->o == IR_HREF && ir->op1 == as->curins-1) { + as->curins--; + asm_href(as, ir-1, (IROp)ir->o); + break; + } + /* fallthrough */ + case IR_LT: case IR_GE: case IR_LE: case IR_GT: + case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: + case IR_ABC: + asm_comp(as, ir); + break; + + case IR_RETF: asm_retf(as, ir); break; + + /* Bit ops. */ + case IR_BNOT: asm_bitnot(as, ir); break; + case IR_BSWAP: asm_bitswap(as, ir); break; + + case IR_BAND: asm_bitand(as, ir); break; + case IR_BOR: asm_bitop(as, ir, PPCI_OR, PPCI_ORI); break; + case IR_BXOR: asm_bitop(as, ir, PPCI_XOR, PPCI_XORI); break; + + case IR_BSHL: asm_bitshift(as, ir, PPCI_SLW, 0); break; + case IR_BSHR: asm_bitshift(as, ir, PPCI_SRW, 1); break; + case IR_BSAR: asm_bitshift(as, ir, PPCI_SRAW, PPCI_SRAWI); break; + case IR_BROL: asm_bitshift(as, ir, PPCI_RLWNM|PPCF_MB(0)|PPCF_ME(31), + PPCI_RLWINM|PPCF_MB(0)|PPCF_ME(31)); break; + case IR_BROR: lua_assert(0); break; + + /* Arithmetic ops. */ + case IR_ADD: asm_add(as, ir); break; + case IR_SUB: asm_sub(as, ir); break; + case IR_MUL: asm_mul(as, ir); break; + case IR_DIV: asm_fparith(as, ir, PPCI_FDIV); break; + case IR_MOD: asm_callid(as, ir, IRCALL_lj_vm_modi); break; + case IR_POW: asm_callid(as, ir, IRCALL_lj_vm_powi); break; + case IR_NEG: asm_neg(as, ir); break; + + case IR_ABS: asm_fpunary(as, ir, PPCI_FABS); break; + case IR_ATAN2: asm_callid(as, ir, IRCALL_atan2); break; + case IR_LDEXP: asm_callid(as, ir, IRCALL_ldexp); break; + case IR_MIN: asm_min_max(as, ir, 0); break; + case IR_MAX: asm_min_max(as, ir, 1); break; + case IR_FPMATH: + if (ir->op2 == IRFPM_EXP2 && asm_fpjoin_pow(as, ir)) + break; + if (ir->op2 == IRFPM_SQRT && (as->flags & JIT_F_SQRT)) + asm_fpunary(as, ir, PPCI_FSQRT); + else + asm_callid(as, ir, IRCALL_lj_vm_floor + ir->op2); + break; + + /* Overflow-checking arithmetic ops. */ + case IR_ADDOV: asm_arithov(as, ir, PPCI_ADDO); break; + case IR_SUBOV: asm_arithov(as, ir, PPCI_SUBFO); break; + case IR_MULOV: asm_arithov(as, ir, PPCI_MULLWO); break; + + /* Memory references. */ + case IR_AREF: asm_aref(as, ir); break; + case IR_HREF: asm_href(as, ir, 0); break; + case IR_HREFK: asm_hrefk(as, ir); break; + case IR_NEWREF: asm_newref(as, ir); break; + case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; + case IR_FREF: asm_fref(as, ir); break; + case IR_STRREF: asm_strref(as, ir); break; + + /* Loads and stores. */ + case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: + asm_ahuvload(as, ir); + break; + case IR_FLOAD: asm_fload(as, ir); break; + case IR_XLOAD: asm_xload(as, ir); break; + case IR_SLOAD: asm_sload(as, ir); break; + + case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; + case IR_FSTORE: asm_fstore(as, ir); break; + case IR_XSTORE: asm_xstore(as, ir, 0); break; + + /* Allocations. */ + case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; + case IR_TNEW: asm_tnew(as, ir); break; + case IR_TDUP: asm_tdup(as, ir); break; + case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; + + /* Write barriers. */ + case IR_TBAR: asm_tbar(as, ir); break; + case IR_OBAR: asm_obar(as, ir); break; + + /* Type conversions. */ + case IR_CONV: asm_conv(as, ir); break; + case IR_TOBIT: asm_tobit(as, ir); break; + case IR_TOSTR: asm_tostr(as, ir); break; + case IR_STRTO: asm_strto(as, ir); break; + + /* Calls. */ + case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; + case IR_CALLXS: asm_callx(as, ir); break; + case IR_CARG: break; + + default: + setintV(&as->J->errinfo, ir->o); + lj_trace_err_info(as->J, LJ_TRERR_NYIIR); + break; + } +} + +/* -- Trace setup --------------------------------------------------------- */ + +/* Ensure there are enough stack slots for call arguments. */ +static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) +{ + IRRef args[CCI_NARGS_MAX]; + uint32_t i, nargs = (int)CCI_NARGS(ci); + int nslots = 2, ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; + asm_collectargs(as, ir, ci, args); + for (i = 0; i < nargs; i++) + if (args[i] && irt_isfp(IR(args[i])->t)) { + if (nfpr > 0) nfpr--; else nslots = (nslots+3) & ~1; + } else { + if (ngpr > 0) ngpr--; else nslots++; + } + if (nslots > as->evenspill) /* Leave room for args in stack slots. */ + as->evenspill = nslots; + return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET); +} + +static void asm_setup_target(ASMState *as) +{ + asm_exitstub_setup(as, as->T->nsnap + (as->parent ? 1 : 0)); +} + +/* -- Trace patching ------------------------------------------------------ */ + +/* Patch exit jumps of existing machine code to a new target. */ +void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) +{ + MCode *p = T->mcode; + MCode *pe = (MCode *)((char *)p + T->szmcode); + MCode *px = exitstub_trace_addr(T, exitno); + MCode *cstart = NULL; + MCode *mcarea = lj_mcode_patch(J, p, 0); + int clearso = 0; + for (; p < pe; p++) { + /* Look for exitstub branch, try to replace with branch to target. */ + uint32_t ins = *p; + if ((ins & 0xfc000000u) == 0x40000000u && + ((ins ^ ((char *)px-(char *)p)) & 0xffffu) == 0) { + ptrdiff_t delta = (char *)target - (char *)p; + if (((ins >> 16) & 3) == (CC_SO&3)) { + clearso = sizeof(MCode); + delta -= sizeof(MCode); + } + /* Many, but not all short-range branches can be patched directly. */ + if (((delta + 0x8000) >> 16) == 0) { + *p = (ins & 0xffdf0000u) | ((uint32_t)delta & 0xffffu) | + ((delta & 0x8000) * (PPCF_Y/0x8000)); + if (!cstart) cstart = p; + } + } else if ((ins & 0xfc000000u) == PPCI_B && + ((ins ^ ((char *)px-(char *)p)) & 0x03ffffffu) == 0) { + ptrdiff_t delta = (char *)target - (char *)p; + lua_assert(((delta + 0x02000000) >> 26) == 0); + *p = PPCI_B | ((uint32_t)delta & 0x03ffffffu); + if (!cstart) cstart = p; + } + } + { /* Always patch long-range branch in exit stub itself. */ + ptrdiff_t delta = (char *)target - (char *)px - clearso; + lua_assert(((delta + 0x02000000) >> 26) == 0); + *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu); + } + if (!cstart) cstart = px; + lj_mcode_sync(cstart, px+1); + if (clearso) { /* Extend the current trace. Ugly workaround. */ + MCode *pp = J->cur.mcode; + J->cur.szmcode += sizeof(MCode); + *--pp = PPCI_MCRXR; /* Clear SO flag. */ + J->cur.mcode = pp; + lj_mcode_sync(pp, pp+1); + } + lj_mcode_patch(J, mcarea, 1); +} + diff --git a/external/lua/luajit/src/src/lj_asm_x86.h b/external/lua/luajit/src/src/lj_asm_x86.h new file mode 100644 index 0000000000..e9c53a0926 --- /dev/null +++ b/external/lua/luajit/src/src/lj_asm_x86.h @@ -0,0 +1,2796 @@ +/* +** x86/x64 IR assembler (SSA IR -> machine code). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* -- Guard handling ------------------------------------------------------ */ + +/* Generate an exit stub group at the bottom of the reserved MCode memory. */ +static MCode *asm_exitstub_gen(ASMState *as, ExitNo group) +{ + ExitNo i, groupofs = (group*EXITSTUBS_PER_GROUP) & 0xff; + MCode *mxp = as->mcbot; + MCode *mxpstart = mxp; + if (mxp + (2+2)*EXITSTUBS_PER_GROUP+8+5 >= as->mctop) + asm_mclimit(as); + /* Push low byte of exitno for each exit stub. */ + *mxp++ = XI_PUSHi8; *mxp++ = (MCode)groupofs; + for (i = 1; i < EXITSTUBS_PER_GROUP; i++) { + *mxp++ = XI_JMPs; *mxp++ = (MCode)((2+2)*(EXITSTUBS_PER_GROUP - i) - 2); + *mxp++ = XI_PUSHi8; *mxp++ = (MCode)(groupofs + i); + } + /* Push the high byte of the exitno for each exit stub group. */ + *mxp++ = XI_PUSHi8; *mxp++ = (MCode)((group*EXITSTUBS_PER_GROUP)>>8); + /* Store DISPATCH at original stack slot 0. Account for the two push ops. */ + *mxp++ = XI_MOVmi; + *mxp++ = MODRM(XM_OFS8, 0, RID_ESP); + *mxp++ = MODRM(XM_SCALE1, RID_ESP, RID_ESP); + *mxp++ = 2*sizeof(void *); + *(int32_t *)mxp = ptr2addr(J2GG(as->J)->dispatch); mxp += 4; + /* Jump to exit handler which fills in the ExitState. */ + *mxp++ = XI_JMP; mxp += 4; + *((int32_t *)(mxp-4)) = jmprel(mxp, (MCode *)(void *)lj_vm_exit_handler); + /* Commit the code for this group (even if assembly fails later on). */ + lj_mcode_commitbot(as->J, mxp); + as->mcbot = mxp; + as->mclim = as->mcbot + MCLIM_REDZONE; + return mxpstart; +} + +/* Setup all needed exit stubs. */ +static void asm_exitstub_setup(ASMState *as, ExitNo nexits) +{ + ExitNo i; + if (nexits >= EXITSTUBS_PER_GROUP*LJ_MAX_EXITSTUBGR) + lj_trace_err(as->J, LJ_TRERR_SNAPOV); + for (i = 0; i < (nexits+EXITSTUBS_PER_GROUP-1)/EXITSTUBS_PER_GROUP; i++) + if (as->J->exitstubgroup[i] == NULL) + as->J->exitstubgroup[i] = asm_exitstub_gen(as, i); +} + +/* Emit conditional branch to exit for guard. +** It's important to emit this *after* all registers have been allocated, +** because rematerializations may invalidate the flags. +*/ +static void asm_guardcc(ASMState *as, int cc) +{ + MCode *target = exitstub_addr(as->J, as->snapno); + MCode *p = as->mcp; + if (LJ_UNLIKELY(p == as->invmcp)) { + as->loopinv = 1; + *(int32_t *)(p+1) = jmprel(p+5, target); + target = p; + cc ^= 1; + if (as->realign) { + emit_sjcc(as, cc, target); + return; + } + } + emit_jcc(as, cc, target); +} + +/* -- Memory operand fusion ----------------------------------------------- */ + +/* Limit linear search to this distance. Avoids O(n^2) behavior. */ +#define CONFLICT_SEARCH_LIM 31 + +/* Check if a reference is a signed 32 bit constant. */ +static int asm_isk32(ASMState *as, IRRef ref, int32_t *k) +{ + if (irref_isk(ref)) { + IRIns *ir = IR(ref); + if (ir->o != IR_KINT64) { + *k = ir->i; + return 1; + } else if (checki32((int64_t)ir_kint64(ir)->u64)) { + *k = (int32_t)ir_kint64(ir)->u64; + return 1; + } + } + return 0; +} + +/* Check if there's no conflicting instruction between curins and ref. +** Also avoid fusing loads if there are multiple references. +*/ +static int noconflict(ASMState *as, IRRef ref, IROp conflict, int noload) +{ + IRIns *ir = as->ir; + IRRef i = as->curins; + if (i > ref + CONFLICT_SEARCH_LIM) + return 0; /* Give up, ref is too far away. */ + while (--i > ref) { + if (ir[i].o == conflict) + return 0; /* Conflict found. */ + else if (!noload && (ir[i].op1 == ref || ir[i].op2 == ref)) + return 0; + } + return 1; /* Ok, no conflict. */ +} + +/* Fuse array base into memory operand. */ +static IRRef asm_fuseabase(ASMState *as, IRRef ref) +{ + IRIns *irb = IR(ref); + as->mrm.ofs = 0; + if (irb->o == IR_FLOAD) { + IRIns *ira = IR(irb->op1); + lua_assert(irb->op2 == IRFL_TAB_ARRAY); + /* We can avoid the FLOAD of t->array for colocated arrays. */ + if (ira->o == IR_TNEW && ira->op1 <= LJ_MAX_COLOSIZE && + !neverfuse(as) && noconflict(as, irb->op1, IR_NEWREF, 1)) { + as->mrm.ofs = (int32_t)sizeof(GCtab); /* Ofs to colocated array. */ + return irb->op1; /* Table obj. */ + } + } else if (irb->o == IR_ADD && irref_isk(irb->op2)) { + /* Fuse base offset (vararg load). */ + as->mrm.ofs = IR(irb->op2)->i; + return irb->op1; + } + return ref; /* Otherwise use the given array base. */ +} + +/* Fuse array reference into memory operand. */ +static void asm_fusearef(ASMState *as, IRIns *ir, RegSet allow) +{ + IRIns *irx; + lua_assert(ir->o == IR_AREF); + as->mrm.base = (uint8_t)ra_alloc1(as, asm_fuseabase(as, ir->op1), allow); + irx = IR(ir->op2); + if (irref_isk(ir->op2)) { + as->mrm.ofs += 8*irx->i; + as->mrm.idx = RID_NONE; + } else { + rset_clear(allow, as->mrm.base); + as->mrm.scale = XM_SCALE8; + /* Fuse a constant ADD (e.g. t[i+1]) into the offset. + ** Doesn't help much without ABCelim, but reduces register pressure. + */ + if (!LJ_64 && /* Has bad effects with negative index on x64. */ + mayfuse(as, ir->op2) && ra_noreg(irx->r) && + irx->o == IR_ADD && irref_isk(irx->op2)) { + as->mrm.ofs += 8*IR(irx->op2)->i; + as->mrm.idx = (uint8_t)ra_alloc1(as, irx->op1, allow); + } else { + as->mrm.idx = (uint8_t)ra_alloc1(as, ir->op2, allow); + } + } +} + +/* Fuse array/hash/upvalue reference into memory operand. +** Caveat: this may allocate GPRs for the base/idx registers. Be sure to +** pass the final allow mask, excluding any GPRs used for other inputs. +** In particular: 2-operand GPR instructions need to call ra_dest() first! +*/ +static void asm_fuseahuref(ASMState *as, IRRef ref, RegSet allow) +{ + IRIns *ir = IR(ref); + if (ra_noreg(ir->r)) { + switch ((IROp)ir->o) { + case IR_AREF: + if (mayfuse(as, ref)) { + asm_fusearef(as, ir, allow); + return; + } + break; + case IR_HREFK: + if (mayfuse(as, ref)) { + as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow); + as->mrm.ofs = (int32_t)(IR(ir->op2)->op2 * sizeof(Node)); + as->mrm.idx = RID_NONE; + return; + } + break; + case IR_UREFC: + if (irref_isk(ir->op1)) { + GCfunc *fn = ir_kfunc(IR(ir->op1)); + GCupval *uv = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv; + as->mrm.ofs = ptr2addr(&uv->tv); + as->mrm.base = as->mrm.idx = RID_NONE; + return; + } + break; + default: + lua_assert(ir->o == IR_HREF || ir->o == IR_NEWREF || ir->o == IR_UREFO || + ir->o == IR_KKPTR); + break; + } + } + as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow); + as->mrm.ofs = 0; + as->mrm.idx = RID_NONE; +} + +/* Fuse FLOAD/FREF reference into memory operand. */ +static void asm_fusefref(ASMState *as, IRIns *ir, RegSet allow) +{ + lua_assert(ir->o == IR_FLOAD || ir->o == IR_FREF); + as->mrm.ofs = field_ofs[ir->op2]; + as->mrm.idx = RID_NONE; + if (irref_isk(ir->op1)) { + as->mrm.ofs += IR(ir->op1)->i; + as->mrm.base = RID_NONE; + } else { + as->mrm.base = (uint8_t)ra_alloc1(as, ir->op1, allow); + } +} + +/* Fuse string reference into memory operand. */ +static void asm_fusestrref(ASMState *as, IRIns *ir, RegSet allow) +{ + IRIns *irr; + lua_assert(ir->o == IR_STRREF); + as->mrm.base = as->mrm.idx = RID_NONE; + as->mrm.scale = XM_SCALE1; + as->mrm.ofs = sizeof(GCstr); + if (irref_isk(ir->op1)) { + as->mrm.ofs += IR(ir->op1)->i; + } else { + Reg r = ra_alloc1(as, ir->op1, allow); + rset_clear(allow, r); + as->mrm.base = (uint8_t)r; + } + irr = IR(ir->op2); + if (irref_isk(ir->op2)) { + as->mrm.ofs += irr->i; + } else { + Reg r; + /* Fuse a constant add into the offset, e.g. string.sub(s, i+10). */ + if (!LJ_64 && /* Has bad effects with negative index on x64. */ + mayfuse(as, ir->op2) && irr->o == IR_ADD && irref_isk(irr->op2)) { + as->mrm.ofs += IR(irr->op2)->i; + r = ra_alloc1(as, irr->op1, allow); + } else { + r = ra_alloc1(as, ir->op2, allow); + } + if (as->mrm.base == RID_NONE) + as->mrm.base = (uint8_t)r; + else + as->mrm.idx = (uint8_t)r; + } +} + +static void asm_fusexref(ASMState *as, IRRef ref, RegSet allow) +{ + IRIns *ir = IR(ref); + as->mrm.idx = RID_NONE; + if (ir->o == IR_KPTR || ir->o == IR_KKPTR) { + as->mrm.ofs = ir->i; + as->mrm.base = RID_NONE; + } else if (ir->o == IR_STRREF) { + asm_fusestrref(as, ir, allow); + } else { + as->mrm.ofs = 0; + if (canfuse(as, ir) && ir->o == IR_ADD && ra_noreg(ir->r)) { + /* Gather (base+idx*sz)+ofs as emitted by cdata ptr/array indexing. */ + IRIns *irx; + IRRef idx; + Reg r; + if (asm_isk32(as, ir->op2, &as->mrm.ofs)) { /* Recognize x+ofs. */ + ref = ir->op1; + ir = IR(ref); + if (!(ir->o == IR_ADD && canfuse(as, ir) && ra_noreg(ir->r))) + goto noadd; + } + as->mrm.scale = XM_SCALE1; + idx = ir->op1; + ref = ir->op2; + irx = IR(idx); + if (!(irx->o == IR_BSHL || irx->o == IR_ADD)) { /* Try other operand. */ + idx = ir->op2; + ref = ir->op1; + irx = IR(idx); + } + if (canfuse(as, irx) && ra_noreg(irx->r)) { + if (irx->o == IR_BSHL && irref_isk(irx->op2) && IR(irx->op2)->i <= 3) { + /* Recognize idx<op1; + as->mrm.scale = (uint8_t)(IR(irx->op2)->i << 6); + } else if (irx->o == IR_ADD && irx->op1 == irx->op2) { + /* FOLD does idx*2 ==> idx<<1 ==> idx+idx. */ + idx = irx->op1; + as->mrm.scale = XM_SCALE2; + } + } + r = ra_alloc1(as, idx, allow); + rset_clear(allow, r); + as->mrm.idx = (uint8_t)r; + } + noadd: + as->mrm.base = (uint8_t)ra_alloc1(as, ref, allow); + } +} + +/* Fuse load into memory operand. */ +static Reg asm_fuseload(ASMState *as, IRRef ref, RegSet allow) +{ + IRIns *ir = IR(ref); + if (ra_hasreg(ir->r)) { + if (allow != RSET_EMPTY) { /* Fast path. */ + ra_noweak(as, ir->r); + return ir->r; + } + fusespill: + /* Force a spill if only memory operands are allowed (asm_x87load). */ + as->mrm.base = RID_ESP; + as->mrm.ofs = ra_spill(as, ir); + as->mrm.idx = RID_NONE; + return RID_MRM; + } + if (ir->o == IR_KNUM) { + RegSet avail = as->freeset & ~as->modset & RSET_FPR; + lua_assert(allow != RSET_EMPTY); + if (!(avail & (avail-1))) { /* Fuse if less than two regs available. */ + as->mrm.ofs = ptr2addr(ir_knum(ir)); + as->mrm.base = as->mrm.idx = RID_NONE; + return RID_MRM; + } + } else if (mayfuse(as, ref)) { + RegSet xallow = (allow & RSET_GPR) ? allow : RSET_GPR; + if (ir->o == IR_SLOAD) { + if (!(ir->op2 & (IRSLOAD_PARENT|IRSLOAD_CONVERT)) && + noconflict(as, ref, IR_RETF, 0)) { + as->mrm.base = (uint8_t)ra_alloc1(as, REF_BASE, xallow); + as->mrm.ofs = 8*((int32_t)ir->op1-1) + ((ir->op2&IRSLOAD_FRAME)?4:0); + as->mrm.idx = RID_NONE; + return RID_MRM; + } + } else if (ir->o == IR_FLOAD) { + /* Generic fusion is only ok for 32 bit operand (but see asm_comp). */ + if ((irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)) && + noconflict(as, ref, IR_FSTORE, 0)) { + asm_fusefref(as, ir, xallow); + return RID_MRM; + } + } else if (ir->o == IR_ALOAD || ir->o == IR_HLOAD || ir->o == IR_ULOAD) { + if (noconflict(as, ref, ir->o + IRDELTA_L2S, 0)) { + asm_fuseahuref(as, ir->op1, xallow); + return RID_MRM; + } + } else if (ir->o == IR_XLOAD) { + /* Generic fusion is not ok for 8/16 bit operands (but see asm_comp). + ** Fusing unaligned memory operands is ok on x86 (except for SIMD types). + */ + if ((!irt_typerange(ir->t, IRT_I8, IRT_U16)) && + noconflict(as, ref, IR_XSTORE, 0)) { + asm_fusexref(as, ir->op1, xallow); + return RID_MRM; + } + } else if (ir->o == IR_VLOAD) { + asm_fuseahuref(as, ir->op1, xallow); + return RID_MRM; + } + } + if (!(as->freeset & allow) && + (allow == RSET_EMPTY || ra_hasspill(ir->s) || iscrossref(as, ref))) + goto fusespill; + return ra_allocref(as, ref, allow); +} + +#if LJ_64 +/* Don't fuse a 32 bit load into a 64 bit operation. */ +static Reg asm_fuseloadm(ASMState *as, IRRef ref, RegSet allow, int is64) +{ + if (is64 && !irt_is64(IR(ref)->t)) + return ra_alloc1(as, ref, allow); + return asm_fuseload(as, ref, allow); +} +#else +#define asm_fuseloadm(as, ref, allow, is64) asm_fuseload(as, (ref), (allow)) +#endif + +/* -- Calls --------------------------------------------------------------- */ + +/* Count the required number of stack slots for a call. */ +static int asm_count_call_slots(ASMState *as, const CCallInfo *ci, IRRef *args) +{ + uint32_t i, nargs = CCI_NARGS(ci); + int nslots = 0; +#if LJ_64 + if (LJ_ABI_WIN) { + nslots = (int)(nargs*2); /* Only matters for more than four args. */ + } else { + int ngpr = REGARG_NUMGPR, nfpr = REGARG_NUMFPR; + for (i = 0; i < nargs; i++) + if (args[i] && irt_isfp(IR(args[i])->t)) { + if (nfpr > 0) nfpr--; else nslots += 2; + } else { + if (ngpr > 0) ngpr--; else nslots += 2; + } + } +#else + int ngpr = 0; + if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL) + ngpr = 2; + else if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL) + ngpr = 1; + for (i = 0; i < nargs; i++) + if (args[i] && irt_isfp(IR(args[i])->t)) { + nslots += irt_isnum(IR(args[i])->t) ? 2 : 1; + } else { + if (ngpr > 0) ngpr--; else nslots++; + } +#endif + return nslots; +} + +/* Generate a call to a C function. */ +static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args) +{ + uint32_t n, nargs = CCI_NARGS(ci); + int32_t ofs = STACKARG_OFS; +#if LJ_64 + uint32_t gprs = REGARG_GPRS; + Reg fpr = REGARG_FIRSTFPR; +#if !LJ_ABI_WIN + MCode *patchnfpr = NULL; +#endif +#else + uint32_t gprs = 0; + if ((ci->flags & CCI_CC_MASK) != CCI_CC_CDECL) { + if ((ci->flags & CCI_CC_MASK) == CCI_CC_THISCALL) + gprs = (REGARG_GPRS & 31); + else if ((ci->flags & CCI_CC_MASK) == CCI_CC_FASTCALL) + gprs = REGARG_GPRS; + } +#endif + if ((void *)ci->func) + emit_call(as, ci->func); +#if LJ_64 + if ((ci->flags & CCI_VARARG)) { /* Special handling for vararg calls. */ +#if LJ_ABI_WIN + for (n = 0; n < 4 && n < nargs; n++) { + IRIns *ir = IR(args[n]); + if (irt_isfp(ir->t)) /* Duplicate FPRs in GPRs. */ + emit_rr(as, XO_MOVDto, (irt_isnum(ir->t) ? REX_64 : 0) | (fpr+n), + ((gprs >> (n*5)) & 31)); /* Either MOVD or MOVQ. */ + } +#else + patchnfpr = --as->mcp; /* Indicate number of used FPRs in register al. */ + *--as->mcp = XI_MOVrib | RID_EAX; +#endif + } +#endif + for (n = 0; n < nargs; n++) { /* Setup args. */ + IRRef ref = args[n]; + IRIns *ir = IR(ref); + Reg r; +#if LJ_64 && LJ_ABI_WIN + /* Windows/x64 argument registers are strictly positional. */ + r = irt_isfp(ir->t) ? (fpr <= REGARG_LASTFPR ? fpr : 0) : (gprs & 31); + fpr++; gprs >>= 5; +#elif LJ_64 + /* POSIX/x64 argument registers are used in order of appearance. */ + if (irt_isfp(ir->t)) { + r = fpr <= REGARG_LASTFPR ? fpr++ : 0; + } else { + r = gprs & 31; gprs >>= 5; + } +#else + if (ref && irt_isfp(ir->t)) { + r = 0; + } else { + r = gprs & 31; gprs >>= 5; + if (!ref) continue; + } +#endif + if (r) { /* Argument is in a register. */ + if (r < RID_MAX_GPR && ref < ASMREF_TMP1) { +#if LJ_64 + if (ir->o == IR_KINT64) + emit_loadu64(as, r, ir_kint64(ir)->u64); + else +#endif + emit_loadi(as, r, ir->i); + } else { + lua_assert(rset_test(as->freeset, r)); /* Must have been evicted. */ + if (ra_hasreg(ir->r)) { + ra_noweak(as, ir->r); + emit_movrr(as, ir, r, ir->r); + } else { + ra_allocref(as, ref, RID2RSET(r)); + } + } + } else if (irt_isfp(ir->t)) { /* FP argument is on stack. */ + lua_assert(!(irt_isfloat(ir->t) && irref_isk(ref))); /* No float k. */ + if (LJ_32 && (ofs & 4) && irref_isk(ref)) { + /* Split stores for unaligned FP consts. */ + emit_movmroi(as, RID_ESP, ofs, (int32_t)ir_knum(ir)->u32.lo); + emit_movmroi(as, RID_ESP, ofs+4, (int32_t)ir_knum(ir)->u32.hi); + } else { + r = ra_alloc1(as, ref, RSET_FPR); + emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto, + r, RID_ESP, ofs); + } + ofs += (LJ_32 && irt_isfloat(ir->t)) ? 4 : 8; + } else { /* Non-FP argument is on stack. */ + if (LJ_32 && ref < ASMREF_TMP1) { + emit_movmroi(as, RID_ESP, ofs, ir->i); + } else { + r = ra_alloc1(as, ref, RSET_GPR); + emit_movtomro(as, REX_64 + r, RID_ESP, ofs); + } + ofs += sizeof(intptr_t); + } + } +#if LJ_64 && !LJ_ABI_WIN + if (patchnfpr) *patchnfpr = fpr - REGARG_FIRSTFPR; +#endif +} + +/* Setup result reg/sp for call. Evict scratch regs. */ +static void asm_setupresult(ASMState *as, IRIns *ir, const CCallInfo *ci) +{ + RegSet drop = RSET_SCRATCH; + int hiop = (LJ_32 && (ir+1)->o == IR_HIOP); + if ((ci->flags & CCI_NOFPRCLOBBER)) + drop &= ~RSET_FPR; + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + if (hiop && ra_hasreg((ir+1)->r)) + rset_clear(drop, (ir+1)->r); /* Dest reg handled below. */ + ra_evictset(as, drop); /* Evictions must be performed first. */ + if (ra_used(ir)) { + if (irt_isfp(ir->t)) { + int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ +#if LJ_64 + if ((ci->flags & CCI_CASTU64)) { + Reg dest = ir->r; + if (ra_hasreg(dest)) { + ra_free(as, dest); + ra_modified(as, dest); + emit_rr(as, XO_MOVD, dest|REX_64, RID_RET); /* Really MOVQ. */ + } + if (ofs) emit_movtomro(as, RID_RET|REX_64, RID_ESP, ofs); + } else { + ra_destreg(as, ir, RID_FPRET); + } +#else + /* Number result is in x87 st0 for x86 calling convention. */ + Reg dest = ir->r; + if (ra_hasreg(dest)) { + ra_free(as, dest); + ra_modified(as, dest); + emit_rmro(as, irt_isnum(ir->t) ? XMM_MOVRM(as) : XO_MOVSS, + dest, RID_ESP, ofs); + } + if ((ci->flags & CCI_CASTU64)) { + emit_movtomro(as, RID_RETLO, RID_ESP, ofs); + emit_movtomro(as, RID_RETHI, RID_ESP, ofs+4); + } else { + emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd, + irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs); + } +#endif +#if LJ_32 + } else if (hiop) { + ra_destpair(as, ir); +#endif + } else { + lua_assert(!irt_ispri(ir->t)); + ra_destreg(as, ir, RID_RET); + } + } else if (LJ_32 && irt_isfp(ir->t)) { + emit_x87op(as, XI_FPOP); /* Pop unused result from x87 st0. */ + } +} + +static void asm_call(ASMState *as, IRIns *ir) +{ + IRRef args[CCI_NARGS_MAX]; + const CCallInfo *ci = &lj_ir_callinfo[ir->op2]; + asm_collectargs(as, ir, ci, args); + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} + +/* Return a constant function pointer or NULL for indirect calls. */ +static void *asm_callx_func(ASMState *as, IRIns *irf, IRRef func) +{ +#if LJ_32 + UNUSED(as); + if (irref_isk(func)) + return (void *)irf->i; +#else + if (irref_isk(func)) { + MCode *p; + if (irf->o == IR_KINT64) + p = (MCode *)(void *)ir_k64(irf)->u64; + else + p = (MCode *)(void *)(uintptr_t)(uint32_t)irf->i; + if (p - as->mcp == (int32_t)(p - as->mcp)) + return p; /* Call target is still in +-2GB range. */ + /* Avoid the indirect case of emit_call(). Try to hoist func addr. */ + } +#endif + return NULL; +} + +static void asm_callx(ASMState *as, IRIns *ir) +{ + IRRef args[CCI_NARGS_MAX]; + CCallInfo ci; + IRRef func; + IRIns *irf; + int32_t spadj = 0; + ci.flags = asm_callx_flags(as, ir); + asm_collectargs(as, ir, &ci, args); + asm_setupresult(as, ir, &ci); +#if LJ_32 + /* Have to readjust stack after non-cdecl calls due to callee cleanup. */ + if ((ci.flags & CCI_CC_MASK) != CCI_CC_CDECL) + spadj = 4 * asm_count_call_slots(as, &ci, args); +#endif + func = ir->op2; irf = IR(func); + if (irf->o == IR_CARG) { func = irf->op1; irf = IR(func); } + ci.func = (ASMFunction)asm_callx_func(as, irf, func); + if (!(void *)ci.func) { + /* Use a (hoistable) non-scratch register for indirect calls. */ + RegSet allow = (RSET_GPR & ~RSET_SCRATCH); + Reg r = ra_alloc1(as, func, allow); + if (LJ_32) emit_spsub(as, spadj); /* Above code may cause restores! */ + emit_rr(as, XO_GROUP5, XOg_CALL, r); + } else if (LJ_32) { + emit_spsub(as, spadj); + } + asm_gencall(as, &ci, args); +} + +/* -- Returns ------------------------------------------------------------- */ + +/* Return to lower frame. Guard that it goes to the right spot. */ +static void asm_retf(ASMState *as, IRIns *ir) +{ + Reg base = ra_alloc1(as, REF_BASE, RSET_GPR); + void *pc = ir_kptr(IR(ir->op2)); + int32_t delta = 1+bc_a(*((const BCIns *)pc - 1)); + as->topslot -= (BCReg)delta; + if ((int32_t)as->topslot < 0) as->topslot = 0; + emit_setgl(as, base, jit_base); + emit_addptr(as, base, -8*delta); + asm_guardcc(as, CC_NE); + emit_gmroi(as, XG_ARITHi(XOg_CMP), base, -4, ptr2addr(pc)); +} + +/* -- Type conversions ---------------------------------------------------- */ + +static void asm_tointg(ASMState *as, IRIns *ir, Reg left) +{ + Reg tmp = ra_scratch(as, rset_exclude(RSET_FPR, left)); + Reg dest = ra_dest(as, ir, RSET_GPR); + asm_guardcc(as, CC_P); + asm_guardcc(as, CC_NE); + emit_rr(as, XO_UCOMISD, left, tmp); + emit_rr(as, XO_CVTSI2SD, tmp, dest); + if (!(as->flags & JIT_F_SPLIT_XMM)) + emit_rr(as, XO_XORPS, tmp, tmp); /* Avoid partial register stall. */ + emit_rr(as, XO_CVTTSD2SI, dest, left); + /* Can't fuse since left is needed twice. */ +} + +static void asm_tobit(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + Reg tmp = ra_noreg(IR(ir->op1)->r) ? + ra_alloc1(as, ir->op1, RSET_FPR) : + ra_scratch(as, RSET_FPR); + Reg right = asm_fuseload(as, ir->op2, rset_exclude(RSET_FPR, tmp)); + emit_rr(as, XO_MOVDto, tmp, dest); + emit_mrm(as, XO_ADDSD, tmp, right); + ra_left(as, tmp, ir->op1); +} + +static void asm_conv(ASMState *as, IRIns *ir) +{ + IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); + int st64 = (st == IRT_I64 || st == IRT_U64 || (LJ_64 && st == IRT_P64)); + int stfp = (st == IRT_NUM || st == IRT_FLOAT); + IRRef lref = ir->op1; + lua_assert(irt_type(ir->t) != st); + lua_assert(!(LJ_32 && (irt_isint64(ir->t) || st64))); /* Handled by SPLIT. */ + if (irt_isfp(ir->t)) { + Reg dest = ra_dest(as, ir, RSET_FPR); + if (stfp) { /* FP to FP conversion. */ + Reg left = asm_fuseload(as, lref, RSET_FPR); + emit_mrm(as, st == IRT_NUM ? XO_CVTSD2SS : XO_CVTSS2SD, dest, left); + if (left == dest) return; /* Avoid the XO_XORPS. */ + } else if (LJ_32 && st == IRT_U32) { /* U32 to FP conversion on x86. */ + /* number = (2^52+2^51 .. u32) - (2^52+2^51) */ + cTValue *k = lj_ir_k64_find(as->J, U64x(43380000,00000000)); + Reg bias = ra_scratch(as, rset_exclude(RSET_FPR, dest)); + if (irt_isfloat(ir->t)) + emit_rr(as, XO_CVTSD2SS, dest, dest); + emit_rr(as, XO_SUBSD, dest, bias); /* Subtract 2^52+2^51 bias. */ + emit_rr(as, XO_XORPS, dest, bias); /* Merge bias and integer. */ + emit_loadn(as, bias, k); + emit_mrm(as, XO_MOVD, dest, asm_fuseload(as, lref, RSET_GPR)); + return; + } else { /* Integer to FP conversion. */ + Reg left = (LJ_64 && (st == IRT_U32 || st == IRT_U64)) ? + ra_alloc1(as, lref, RSET_GPR) : + asm_fuseloadm(as, lref, RSET_GPR, st64); + if (LJ_64 && st == IRT_U64) { + MCLabel l_end = emit_label(as); + const void *k = lj_ir_k64_find(as->J, U64x(43f00000,00000000)); + emit_rma(as, XO_ADDSD, dest, k); /* Add 2^64 to compensate. */ + emit_sjcc(as, CC_NS, l_end); + emit_rr(as, XO_TEST, left|REX_64, left); /* Check if u64 >= 2^63. */ + } + emit_mrm(as, irt_isnum(ir->t) ? XO_CVTSI2SD : XO_CVTSI2SS, + dest|((LJ_64 && (st64 || st == IRT_U32)) ? REX_64 : 0), left); + } + if (!(as->flags & JIT_F_SPLIT_XMM)) + emit_rr(as, XO_XORPS, dest, dest); /* Avoid partial register stall. */ + } else if (stfp) { /* FP to integer conversion. */ + if (irt_isguard(ir->t)) { + /* Checked conversions are only supported from number to int. */ + lua_assert(irt_isint(ir->t) && st == IRT_NUM); + asm_tointg(as, ir, ra_alloc1(as, lref, RSET_FPR)); + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + x86Op op = st == IRT_NUM ? + ((ir->op2 & IRCONV_TRUNC) ? XO_CVTTSD2SI : XO_CVTSD2SI) : + ((ir->op2 & IRCONV_TRUNC) ? XO_CVTTSS2SI : XO_CVTSS2SI); + if (LJ_64 ? irt_isu64(ir->t) : irt_isu32(ir->t)) { + /* LJ_64: For inputs >= 2^63 add -2^64, convert again. */ + /* LJ_32: For inputs >= 2^31 add -2^31, convert again and add 2^31. */ + Reg tmp = ra_noreg(IR(lref)->r) ? ra_alloc1(as, lref, RSET_FPR) : + ra_scratch(as, RSET_FPR); + MCLabel l_end = emit_label(as); + if (LJ_32) + emit_gri(as, XG_ARITHi(XOg_ADD), dest, (int32_t)0x80000000); + emit_rr(as, op, dest|REX_64, tmp); + if (st == IRT_NUM) + emit_rma(as, XO_ADDSD, tmp, lj_ir_k64_find(as->J, + LJ_64 ? U64x(c3f00000,00000000) : U64x(c1e00000,00000000))); + else + emit_rma(as, XO_ADDSS, tmp, lj_ir_k64_find(as->J, + LJ_64 ? U64x(00000000,df800000) : U64x(00000000,cf000000))); + emit_sjcc(as, CC_NS, l_end); + emit_rr(as, XO_TEST, dest|REX_64, dest); /* Check if dest negative. */ + emit_rr(as, op, dest|REX_64, tmp); + ra_left(as, tmp, lref); + } else { + Reg left = asm_fuseload(as, lref, RSET_FPR); + if (LJ_64 && irt_isu32(ir->t)) + emit_rr(as, XO_MOV, dest, dest); /* Zero hiword. */ + emit_mrm(as, op, + dest|((LJ_64 && + (irt_is64(ir->t) || irt_isu32(ir->t))) ? REX_64 : 0), + left); + } + } + } else if (st >= IRT_I8 && st <= IRT_U16) { /* Extend to 32 bit integer. */ + Reg left, dest = ra_dest(as, ir, RSET_GPR); + RegSet allow = RSET_GPR; + x86Op op; + lua_assert(irt_isint(ir->t) || irt_isu32(ir->t)); + if (st == IRT_I8) { + op = XO_MOVSXb; allow = RSET_GPR8; dest |= FORCE_REX; + } else if (st == IRT_U8) { + op = XO_MOVZXb; allow = RSET_GPR8; dest |= FORCE_REX; + } else if (st == IRT_I16) { + op = XO_MOVSXw; + } else { + op = XO_MOVZXw; + } + left = asm_fuseload(as, lref, allow); + /* Add extra MOV if source is already in wrong register. */ + if (!LJ_64 && left != RID_MRM && !rset_test(allow, left)) { + Reg tmp = ra_scratch(as, allow); + emit_rr(as, op, dest, tmp); + emit_rr(as, XO_MOV, tmp, left); + } else { + emit_mrm(as, op, dest, left); + } + } else { /* 32/64 bit integer conversions. */ + if (LJ_32) { /* Only need to handle 32/32 bit no-op (cast) on x86. */ + Reg dest = ra_dest(as, ir, RSET_GPR); + ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */ + } else if (irt_is64(ir->t)) { + Reg dest = ra_dest(as, ir, RSET_GPR); + if (st64 || !(ir->op2 & IRCONV_SEXT)) { + /* 64/64 bit no-op (cast) or 32 to 64 bit zero extension. */ + ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */ + } else { /* 32 to 64 bit sign extension. */ + Reg left = asm_fuseload(as, lref, RSET_GPR); + emit_mrm(as, XO_MOVSXd, dest|REX_64, left); + } + } else { + Reg dest = ra_dest(as, ir, RSET_GPR); + if (st64) { + Reg left = asm_fuseload(as, lref, RSET_GPR); + /* This is either a 32 bit reg/reg mov which zeroes the hiword + ** or a load of the loword from a 64 bit address. + */ + emit_mrm(as, XO_MOV, dest, left); + } else { /* 32/32 bit no-op (cast). */ + ra_left(as, dest, lref); /* Do nothing, but may need to move regs. */ + } + } + } +} + +#if LJ_32 && LJ_HASFFI +/* No SSE conversions to/from 64 bit on x86, so resort to ugly x87 code. */ + +/* 64 bit integer to FP conversion in 32 bit mode. */ +static void asm_conv_fp_int64(ASMState *as, IRIns *ir) +{ + Reg hi = ra_alloc1(as, ir->op1, RSET_GPR); + Reg lo = ra_alloc1(as, (ir-1)->op1, rset_exclude(RSET_GPR, hi)); + int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ + Reg dest = ir->r; + if (ra_hasreg(dest)) { + ra_free(as, dest); + ra_modified(as, dest); + emit_rmro(as, irt_isnum(ir->t) ? XMM_MOVRM(as) : XO_MOVSS, + dest, RID_ESP, ofs); + } + emit_rmro(as, irt_isnum(ir->t) ? XO_FSTPq : XO_FSTPd, + irt_isnum(ir->t) ? XOg_FSTPq : XOg_FSTPd, RID_ESP, ofs); + if (((ir-1)->op2 & IRCONV_SRCMASK) == IRT_U64) { + /* For inputs in [2^63,2^64-1] add 2^64 to compensate. */ + MCLabel l_end = emit_label(as); + emit_rma(as, XO_FADDq, XOg_FADDq, + lj_ir_k64_find(as->J, U64x(43f00000,00000000))); + emit_sjcc(as, CC_NS, l_end); + emit_rr(as, XO_TEST, hi, hi); /* Check if u64 >= 2^63. */ + } else { + lua_assert(((ir-1)->op2 & IRCONV_SRCMASK) == IRT_I64); + } + emit_rmro(as, XO_FILDq, XOg_FILDq, RID_ESP, 0); + /* NYI: Avoid narrow-to-wide store-to-load forwarding stall. */ + emit_rmro(as, XO_MOVto, hi, RID_ESP, 4); + emit_rmro(as, XO_MOVto, lo, RID_ESP, 0); +} + +/* FP to 64 bit integer conversion in 32 bit mode. */ +static void asm_conv_int64_fp(ASMState *as, IRIns *ir) +{ + IRType st = (IRType)((ir-1)->op2 & IRCONV_SRCMASK); + IRType dt = (((ir-1)->op2 & IRCONV_DSTMASK) >> IRCONV_DSH); + Reg lo, hi; + lua_assert(st == IRT_NUM || st == IRT_FLOAT); + lua_assert(dt == IRT_I64 || dt == IRT_U64); + lua_assert(((ir-1)->op2 & IRCONV_TRUNC)); + hi = ra_dest(as, ir, RSET_GPR); + lo = ra_dest(as, ir-1, rset_exclude(RSET_GPR, hi)); + if (ra_used(ir-1)) emit_rmro(as, XO_MOV, lo, RID_ESP, 0); + /* NYI: Avoid wide-to-narrow store-to-load forwarding stall. */ + if (!(as->flags & JIT_F_SSE3)) { /* Set FPU rounding mode to default. */ + emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 4); + emit_rmro(as, XO_MOVto, lo, RID_ESP, 4); + emit_gri(as, XG_ARITHi(XOg_AND), lo, 0xf3ff); + } + if (dt == IRT_U64) { + /* For inputs in [2^63,2^64-1] add -2^64 and convert again. */ + MCLabel l_pop, l_end = emit_label(as); + emit_x87op(as, XI_FPOP); + l_pop = emit_label(as); + emit_sjmp(as, l_end); + emit_rmro(as, XO_MOV, hi, RID_ESP, 4); + if ((as->flags & JIT_F_SSE3)) + emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0); + else + emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0); + emit_rma(as, XO_FADDq, XOg_FADDq, + lj_ir_k64_find(as->J, U64x(c3f00000,00000000))); + emit_sjcc(as, CC_NS, l_pop); + emit_rr(as, XO_TEST, hi, hi); /* Check if out-of-range (2^63). */ + } + emit_rmro(as, XO_MOV, hi, RID_ESP, 4); + if ((as->flags & JIT_F_SSE3)) { /* Truncation is easy with SSE3. */ + emit_rmro(as, XO_FISTTPq, XOg_FISTTPq, RID_ESP, 0); + } else { /* Otherwise set FPU rounding mode to truncate before the store. */ + emit_rmro(as, XO_FISTPq, XOg_FISTPq, RID_ESP, 0); + emit_rmro(as, XO_FLDCW, XOg_FLDCW, RID_ESP, 0); + emit_rmro(as, XO_MOVtow, lo, RID_ESP, 0); + emit_rmro(as, XO_ARITHw(XOg_OR), lo, RID_ESP, 0); + emit_loadi(as, lo, 0xc00); + emit_rmro(as, XO_FNSTCW, XOg_FNSTCW, RID_ESP, 0); + } + if (dt == IRT_U64) + emit_x87op(as, XI_FDUP); + emit_mrm(as, st == IRT_NUM ? XO_FLDq : XO_FLDd, + st == IRT_NUM ? XOg_FLDq: XOg_FLDd, + asm_fuseload(as, ir->op1, RSET_EMPTY)); +} +#endif + +static void asm_strto(ASMState *as, IRIns *ir) +{ + /* Force a spill slot for the destination register (if any). */ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_strscan_num]; + IRRef args[2]; + RegSet drop = RSET_SCRATCH; + if ((drop & RSET_FPR) != RSET_FPR && ra_hasreg(ir->r)) + rset_set(drop, ir->r); /* WIN64 doesn't spill all FPRs. */ + ra_evictset(as, drop); + asm_guardcc(as, CC_E); + emit_rr(as, XO_TEST, RID_RET, RID_RET); /* Test return status. */ + args[0] = ir->op1; /* GCstr *str */ + args[1] = ASMREF_TMP1; /* TValue *n */ + asm_gencall(as, ci, args); + /* Store the result to the spill slot or temp slots. */ + emit_rmro(as, XO_LEA, ra_releasetmp(as, ASMREF_TMP1)|REX_64, + RID_ESP, sps_scale(ir->s)); +} + +static void asm_tostr(ASMState *as, IRIns *ir) +{ + IRIns *irl = IR(ir->op1); + IRRef args[2]; + args[0] = ASMREF_L; + as->gcsteps++; + if (irt_isnum(irl->t)) { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromnum]; + args[1] = ASMREF_TMP1; /* const lua_Number * */ + asm_setupresult(as, ir, ci); /* GCstr * */ + asm_gencall(as, ci, args); + emit_rmro(as, XO_LEA, ra_releasetmp(as, ASMREF_TMP1)|REX_64, + RID_ESP, ra_spill(as, irl)); + } else { + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_str_fromint]; + args[1] = ir->op1; /* int32_t k */ + asm_setupresult(as, ir, ci); /* GCstr * */ + asm_gencall(as, ci, args); + } +} + +/* -- Memory references --------------------------------------------------- */ + +static void asm_aref(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + asm_fusearef(as, ir, RSET_GPR); + if (!(as->mrm.idx == RID_NONE && as->mrm.ofs == 0)) + emit_mrm(as, XO_LEA, dest, RID_MRM); + else if (as->mrm.base != dest) + emit_rr(as, XO_MOV, dest, as->mrm.base); +} + +/* Merge NE(HREF, niltv) check. */ +static MCode *merge_href_niltv(ASMState *as, IRIns *ir) +{ + /* Assumes nothing else generates NE of HREF. */ + if ((ir[1].o == IR_NE || ir[1].o == IR_EQ) && ir[1].op1 == as->curins && + ra_hasreg(ir->r)) { + MCode *p = as->mcp; + p += (LJ_64 && *p != XI_ARITHi) ? 7+6 : 6+6; + /* Ensure no loop branch inversion happened. */ + if (p[-6] == 0x0f && p[-5] == XI_JCCn+(CC_NE^(ir[1].o & 1))) { + as->mcp = p; /* Kill cmp reg, imm32 + jz exit. */ + return p + *(int32_t *)(p-4); /* Return exit address. */ + } + } + return NULL; +} + +/* Inlined hash lookup. Specialized for key type and for const keys. +** The equivalent C code is: +** Node *n = hashkey(t, key); +** do { +** if (lj_obj_equal(&n->key, key)) return &n->val; +** } while ((n = nextnode(n))); +** return niltv(L); +*/ +static void asm_href(ASMState *as, IRIns *ir) +{ + MCode *nilexit = merge_href_niltv(as, ir); /* Do this before any restores. */ + RegSet allow = RSET_GPR; + Reg dest = ra_dest(as, ir, allow); + Reg tab = ra_alloc1(as, ir->op1, rset_clear(allow, dest)); + Reg key = RID_NONE, tmp = RID_NONE; + IRIns *irkey = IR(ir->op2); + int isk = irref_isk(ir->op2); + IRType1 kt = irkey->t; + uint32_t khash; + MCLabel l_end, l_loop, l_next; + + if (!isk) { + rset_clear(allow, tab); + key = ra_alloc1(as, ir->op2, irt_isnum(kt) ? RSET_FPR : allow); + if (!irt_isstr(kt)) + tmp = ra_scratch(as, rset_exclude(allow, key)); + } + + /* Key not found in chain: jump to exit (if merged with NE) or load niltv. */ + l_end = emit_label(as); + if (nilexit && ir[1].o == IR_NE) { + emit_jcc(as, CC_E, nilexit); /* XI_JMP is not found by lj_asm_patchexit. */ + nilexit = NULL; + } else { + emit_loada(as, dest, niltvg(J2G(as->J))); + } + + /* Follow hash chain until the end. */ + l_loop = emit_sjcc_label(as, CC_NZ); + emit_rr(as, XO_TEST, dest, dest); + emit_rmro(as, XO_MOV, dest, dest, offsetof(Node, next)); + l_next = emit_label(as); + + /* Type and value comparison. */ + if (nilexit) + emit_jcc(as, CC_E, nilexit); + else + emit_sjcc(as, CC_E, l_end); + if (irt_isnum(kt)) { + if (isk) { + /* Assumes -0.0 is already canonicalized to +0.0. */ + emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.lo), + (int32_t)ir_knum(irkey)->u32.lo); + emit_sjcc(as, CC_NE, l_next); + emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.u32.hi), + (int32_t)ir_knum(irkey)->u32.hi); + } else { + emit_sjcc(as, CC_P, l_next); + emit_rmro(as, XO_UCOMISD, key, dest, offsetof(Node, key.n)); + emit_sjcc(as, CC_AE, l_next); + /* The type check avoids NaN penalties and complaints from Valgrind. */ +#if LJ_64 + emit_u32(as, LJ_TISNUM); + emit_rmro(as, XO_ARITHi, XOg_CMP, dest, offsetof(Node, key.it)); +#else + emit_i8(as, LJ_TISNUM); + emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it)); +#endif + } +#if LJ_64 + } else if (irt_islightud(kt)) { + emit_rmro(as, XO_CMP, key|REX_64, dest, offsetof(Node, key.u64)); +#endif + } else { + if (!irt_ispri(kt)) { + lua_assert(irt_isaddr(kt)); + if (isk) + emit_gmroi(as, XG_ARITHi(XOg_CMP), dest, offsetof(Node, key.gcr), + ptr2addr(ir_kgc(irkey))); + else + emit_rmro(as, XO_CMP, key, dest, offsetof(Node, key.gcr)); + emit_sjcc(as, CC_NE, l_next); + } + lua_assert(!irt_isnil(kt)); + emit_i8(as, irt_toitype(kt)); + emit_rmro(as, XO_ARITHi8, XOg_CMP, dest, offsetof(Node, key.it)); + } + emit_sfixup(as, l_loop); + checkmclim(as); + + /* Load main position relative to tab->node into dest. */ + khash = isk ? ir_khash(irkey) : 1; + if (khash == 0) { + emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, node)); + } else { + emit_rmro(as, XO_ARITH(XOg_ADD), dest, tab, offsetof(GCtab, node)); + if ((as->flags & JIT_F_PREFER_IMUL)) { + emit_i8(as, sizeof(Node)); + emit_rr(as, XO_IMULi8, dest, dest); + } else { + emit_shifti(as, XOg_SHL, dest, 3); + emit_rmrxo(as, XO_LEA, dest, dest, dest, XM_SCALE2, 0); + } + if (isk) { + emit_gri(as, XG_ARITHi(XOg_AND), dest, (int32_t)khash); + emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask)); + } else if (irt_isstr(kt)) { + emit_rmro(as, XO_ARITH(XOg_AND), dest, key, offsetof(GCstr, hash)); + emit_rmro(as, XO_MOV, dest, tab, offsetof(GCtab, hmask)); + } else { /* Must match with hashrot() in lj_tab.c. */ + emit_rmro(as, XO_ARITH(XOg_AND), dest, tab, offsetof(GCtab, hmask)); + emit_rr(as, XO_ARITH(XOg_SUB), dest, tmp); + emit_shifti(as, XOg_ROL, tmp, HASH_ROT3); + emit_rr(as, XO_ARITH(XOg_XOR), dest, tmp); + emit_shifti(as, XOg_ROL, dest, HASH_ROT2); + emit_rr(as, XO_ARITH(XOg_SUB), tmp, dest); + emit_shifti(as, XOg_ROL, dest, HASH_ROT1); + emit_rr(as, XO_ARITH(XOg_XOR), tmp, dest); + if (irt_isnum(kt)) { + emit_rr(as, XO_ARITH(XOg_ADD), dest, dest); +#if LJ_64 + emit_shifti(as, XOg_SHR|REX_64, dest, 32); + emit_rr(as, XO_MOV, tmp, dest); + emit_rr(as, XO_MOVDto, key|REX_64, dest); +#else + emit_rmro(as, XO_MOV, dest, RID_ESP, ra_spill(as, irkey)+4); + emit_rr(as, XO_MOVDto, key, tmp); +#endif + } else { + emit_rr(as, XO_MOV, tmp, key); + emit_rmro(as, XO_LEA, dest, key, HASH_BIAS); + } + } + } +} + +static void asm_hrefk(ASMState *as, IRIns *ir) +{ + IRIns *kslot = IR(ir->op2); + IRIns *irkey = IR(kslot->op1); + int32_t ofs = (int32_t)(kslot->op2 * sizeof(Node)); + Reg dest = ra_used(ir) ? ra_dest(as, ir, RSET_GPR) : RID_NONE; + Reg node = ra_alloc1(as, ir->op1, RSET_GPR); +#if !LJ_64 + MCLabel l_exit; +#endif + lua_assert(ofs % sizeof(Node) == 0); + if (ra_hasreg(dest)) { + if (ofs != 0) { + if (dest == node && !(as->flags & JIT_F_LEA_AGU)) + emit_gri(as, XG_ARITHi(XOg_ADD), dest, ofs); + else + emit_rmro(as, XO_LEA, dest, node, ofs); + } else if (dest != node) { + emit_rr(as, XO_MOV, dest, node); + } + } + asm_guardcc(as, CC_NE); +#if LJ_64 + if (!irt_ispri(irkey->t)) { + Reg key = ra_scratch(as, rset_exclude(RSET_GPR, node)); + emit_rmro(as, XO_CMP, key|REX_64, node, + ofs + (int32_t)offsetof(Node, key.u64)); + lua_assert(irt_isnum(irkey->t) || irt_isgcv(irkey->t)); + /* Assumes -0.0 is already canonicalized to +0.0. */ + emit_loadu64(as, key, irt_isnum(irkey->t) ? ir_knum(irkey)->u64 : + ((uint64_t)irt_toitype(irkey->t) << 32) | + (uint64_t)(uint32_t)ptr2addr(ir_kgc(irkey))); + } else { + lua_assert(!irt_isnil(irkey->t)); + emit_i8(as, irt_toitype(irkey->t)); + emit_rmro(as, XO_ARITHi8, XOg_CMP, node, + ofs + (int32_t)offsetof(Node, key.it)); + } +#else + l_exit = emit_label(as); + if (irt_isnum(irkey->t)) { + /* Assumes -0.0 is already canonicalized to +0.0. */ + emit_gmroi(as, XG_ARITHi(XOg_CMP), node, + ofs + (int32_t)offsetof(Node, key.u32.lo), + (int32_t)ir_knum(irkey)->u32.lo); + emit_sjcc(as, CC_NE, l_exit); + emit_gmroi(as, XG_ARITHi(XOg_CMP), node, + ofs + (int32_t)offsetof(Node, key.u32.hi), + (int32_t)ir_knum(irkey)->u32.hi); + } else { + if (!irt_ispri(irkey->t)) { + lua_assert(irt_isgcv(irkey->t)); + emit_gmroi(as, XG_ARITHi(XOg_CMP), node, + ofs + (int32_t)offsetof(Node, key.gcr), + ptr2addr(ir_kgc(irkey))); + emit_sjcc(as, CC_NE, l_exit); + } + lua_assert(!irt_isnil(irkey->t)); + emit_i8(as, irt_toitype(irkey->t)); + emit_rmro(as, XO_ARITHi8, XOg_CMP, node, + ofs + (int32_t)offsetof(Node, key.it)); + } +#endif +} + +static void asm_newref(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_tab_newkey]; + IRRef args[3]; + IRIns *irkey; + Reg tmp; + if (ir->r == RID_SINK) + return; + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ir->op1; /* GCtab *t */ + args[2] = ASMREF_TMP1; /* cTValue *key */ + asm_setupresult(as, ir, ci); /* TValue * */ + asm_gencall(as, ci, args); + tmp = ra_releasetmp(as, ASMREF_TMP1); + irkey = IR(ir->op2); + if (irt_isnum(irkey->t)) { + /* For numbers use the constant itself or a spill slot as a TValue. */ + if (irref_isk(ir->op2)) + emit_loada(as, tmp, ir_knum(irkey)); + else + emit_rmro(as, XO_LEA, tmp|REX_64, RID_ESP, ra_spill(as, irkey)); + } else { + /* Otherwise use g->tmptv to hold the TValue. */ + if (!irref_isk(ir->op2)) { + Reg src = ra_alloc1(as, ir->op2, rset_exclude(RSET_GPR, tmp)); + emit_movtomro(as, REX_64IR(irkey, src), tmp, 0); + } else if (!irt_ispri(irkey->t)) { + emit_movmroi(as, tmp, 0, irkey->i); + } + if (!(LJ_64 && irt_islightud(irkey->t))) + emit_movmroi(as, tmp, 4, irt_toitype(irkey->t)); + emit_loada(as, tmp, &J2G(as->J)->tmptv); + } +} + +static void asm_uref(ASMState *as, IRIns *ir) +{ + /* NYI: Check that UREFO is still open and not aliasing a slot. */ + Reg dest = ra_dest(as, ir, RSET_GPR); + if (irref_isk(ir->op1)) { + GCfunc *fn = ir_kfunc(IR(ir->op1)); + MRef *v = &gcref(fn->l.uvptr[(ir->op2 >> 8)])->uv.v; + emit_rma(as, XO_MOV, dest, v); + } else { + Reg uv = ra_scratch(as, RSET_GPR); + Reg func = ra_alloc1(as, ir->op1, RSET_GPR); + if (ir->o == IR_UREFC) { + emit_rmro(as, XO_LEA, dest, uv, offsetof(GCupval, tv)); + asm_guardcc(as, CC_NE); + emit_i8(as, 1); + emit_rmro(as, XO_ARITHib, XOg_CMP, uv, offsetof(GCupval, closed)); + } else { + emit_rmro(as, XO_MOV, dest, uv, offsetof(GCupval, v)); + } + emit_rmro(as, XO_MOV, uv, func, + (int32_t)offsetof(GCfuncL, uvptr) + 4*(int32_t)(ir->op2 >> 8)); + } +} + +static void asm_fref(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + asm_fusefref(as, ir, RSET_GPR); + emit_mrm(as, XO_LEA, dest, RID_MRM); +} + +static void asm_strref(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + asm_fusestrref(as, ir, RSET_GPR); + if (as->mrm.base == RID_NONE) + emit_loadi(as, dest, as->mrm.ofs); + else if (as->mrm.base == dest && as->mrm.idx == RID_NONE) + emit_gri(as, XG_ARITHi(XOg_ADD), dest, as->mrm.ofs); + else + emit_mrm(as, XO_LEA, dest, RID_MRM); +} + +/* -- Loads and stores ---------------------------------------------------- */ + +static void asm_fxload(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); + x86Op xo; + if (ir->o == IR_FLOAD) + asm_fusefref(as, ir, RSET_GPR); + else + asm_fusexref(as, ir->op1, RSET_GPR); + /* ir->op2 is ignored -- unaligned loads are ok on x86. */ + switch (irt_type(ir->t)) { + case IRT_I8: xo = XO_MOVSXb; break; + case IRT_U8: xo = XO_MOVZXb; break; + case IRT_I16: xo = XO_MOVSXw; break; + case IRT_U16: xo = XO_MOVZXw; break; + case IRT_NUM: xo = XMM_MOVRM(as); break; + case IRT_FLOAT: xo = XO_MOVSS; break; + default: + if (LJ_64 && irt_is64(ir->t)) + dest |= REX_64; + else + lua_assert(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)); + xo = XO_MOV; + break; + } + emit_mrm(as, xo, dest, RID_MRM); +} + +static void asm_fxstore(ASMState *as, IRIns *ir) +{ + RegSet allow = RSET_GPR; + Reg src = RID_NONE, osrc = RID_NONE; + int32_t k = 0; + if (ir->r == RID_SINK) + return; + /* The IRT_I16/IRT_U16 stores should never be simplified for constant + ** values since mov word [mem], imm16 has a length-changing prefix. + */ + if (irt_isi16(ir->t) || irt_isu16(ir->t) || irt_isfp(ir->t) || + !asm_isk32(as, ir->op2, &k)) { + RegSet allow8 = irt_isfp(ir->t) ? RSET_FPR : + (irt_isi8(ir->t) || irt_isu8(ir->t)) ? RSET_GPR8 : RSET_GPR; + src = osrc = ra_alloc1(as, ir->op2, allow8); + if (!LJ_64 && !rset_test(allow8, src)) { /* Already in wrong register. */ + rset_clear(allow, osrc); + src = ra_scratch(as, allow8); + } + rset_clear(allow, src); + } + if (ir->o == IR_FSTORE) { + asm_fusefref(as, IR(ir->op1), allow); + } else { + asm_fusexref(as, ir->op1, allow); + if (LJ_32 && ir->o == IR_HIOP) as->mrm.ofs += 4; + } + if (ra_hasreg(src)) { + x86Op xo; + switch (irt_type(ir->t)) { + case IRT_I8: case IRT_U8: xo = XO_MOVtob; src |= FORCE_REX; break; + case IRT_I16: case IRT_U16: xo = XO_MOVtow; break; + case IRT_NUM: xo = XO_MOVSDto; break; + case IRT_FLOAT: xo = XO_MOVSSto; break; +#if LJ_64 + case IRT_LIGHTUD: lua_assert(0); /* NYI: mask 64 bit lightuserdata. */ +#endif + default: + if (LJ_64 && irt_is64(ir->t)) + src |= REX_64; + else + lua_assert(irt_isint(ir->t) || irt_isu32(ir->t) || irt_isaddr(ir->t)); + xo = XO_MOVto; + break; + } + emit_mrm(as, xo, src, RID_MRM); + if (!LJ_64 && src != osrc) { + ra_noweak(as, osrc); + emit_rr(as, XO_MOV, src, osrc); + } + } else { + if (irt_isi8(ir->t) || irt_isu8(ir->t)) { + emit_i8(as, k); + emit_mrm(as, XO_MOVmib, 0, RID_MRM); + } else { + lua_assert(irt_is64(ir->t) || irt_isint(ir->t) || irt_isu32(ir->t) || + irt_isaddr(ir->t)); + emit_i32(as, k); + emit_mrm(as, XO_MOVmi, REX_64IR(ir, 0), RID_MRM); + } + } +} + +#if LJ_64 +static Reg asm_load_lightud64(ASMState *as, IRIns *ir, int typecheck) +{ + if (ra_used(ir) || typecheck) { + Reg dest = ra_dest(as, ir, RSET_GPR); + if (typecheck) { + Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, dest)); + asm_guardcc(as, CC_NE); + emit_i8(as, -2); + emit_rr(as, XO_ARITHi8, XOg_CMP, tmp); + emit_shifti(as, XOg_SAR|REX_64, tmp, 47); + emit_rr(as, XO_MOV, tmp|REX_64, dest); + } + return dest; + } else { + return RID_NONE; + } +} +#endif + +static void asm_ahuvload(ASMState *as, IRIns *ir) +{ + lua_assert(irt_isnum(ir->t) || irt_ispri(ir->t) || irt_isaddr(ir->t) || + (LJ_DUALNUM && irt_isint(ir->t))); +#if LJ_64 + if (irt_islightud(ir->t)) { + Reg dest = asm_load_lightud64(as, ir, 1); + if (ra_hasreg(dest)) { + asm_fuseahuref(as, ir->op1, RSET_GPR); + emit_mrm(as, XO_MOV, dest|REX_64, RID_MRM); + } + return; + } else +#endif + if (ra_used(ir)) { + RegSet allow = irt_isnum(ir->t) ? RSET_FPR : RSET_GPR; + Reg dest = ra_dest(as, ir, allow); + asm_fuseahuref(as, ir->op1, RSET_GPR); + emit_mrm(as, dest < RID_MAX_GPR ? XO_MOV : XMM_MOVRM(as), dest, RID_MRM); + } else { + asm_fuseahuref(as, ir->op1, RSET_GPR); + } + /* Always do the type check, even if the load result is unused. */ + as->mrm.ofs += 4; + asm_guardcc(as, irt_isnum(ir->t) ? CC_AE : CC_NE); + if (LJ_64 && irt_type(ir->t) >= IRT_NUM) { + lua_assert(irt_isinteger(ir->t) || irt_isnum(ir->t)); + emit_u32(as, LJ_TISNUM); + emit_mrm(as, XO_ARITHi, XOg_CMP, RID_MRM); + } else { + emit_i8(as, irt_toitype(ir->t)); + emit_mrm(as, XO_ARITHi8, XOg_CMP, RID_MRM); + } +} + +static void asm_ahustore(ASMState *as, IRIns *ir) +{ + if (ir->r == RID_SINK) + return; + if (irt_isnum(ir->t)) { + Reg src = ra_alloc1(as, ir->op2, RSET_FPR); + asm_fuseahuref(as, ir->op1, RSET_GPR); + emit_mrm(as, XO_MOVSDto, src, RID_MRM); +#if LJ_64 + } else if (irt_islightud(ir->t)) { + Reg src = ra_alloc1(as, ir->op2, RSET_GPR); + asm_fuseahuref(as, ir->op1, rset_exclude(RSET_GPR, src)); + emit_mrm(as, XO_MOVto, src|REX_64, RID_MRM); +#endif + } else { + IRIns *irr = IR(ir->op2); + RegSet allow = RSET_GPR; + Reg src = RID_NONE; + if (!irref_isk(ir->op2)) { + src = ra_alloc1(as, ir->op2, allow); + rset_clear(allow, src); + } + asm_fuseahuref(as, ir->op1, allow); + if (ra_hasreg(src)) { + emit_mrm(as, XO_MOVto, src, RID_MRM); + } else if (!irt_ispri(irr->t)) { + lua_assert(irt_isaddr(ir->t) || (LJ_DUALNUM && irt_isinteger(ir->t))); + emit_i32(as, irr->i); + emit_mrm(as, XO_MOVmi, 0, RID_MRM); + } + as->mrm.ofs += 4; + emit_i32(as, (int32_t)irt_toitype(ir->t)); + emit_mrm(as, XO_MOVmi, 0, RID_MRM); + } +} + +static void asm_sload(ASMState *as, IRIns *ir) +{ + int32_t ofs = 8*((int32_t)ir->op1-1) + ((ir->op2 & IRSLOAD_FRAME) ? 4 : 0); + IRType1 t = ir->t; + Reg base; + lua_assert(!(ir->op2 & IRSLOAD_PARENT)); /* Handled by asm_head_side(). */ + lua_assert(irt_isguard(t) || !(ir->op2 & IRSLOAD_TYPECHECK)); + lua_assert(LJ_DUALNUM || + !irt_isint(t) || (ir->op2 & (IRSLOAD_CONVERT|IRSLOAD_FRAME))); + if ((ir->op2 & IRSLOAD_CONVERT) && irt_isguard(t) && irt_isint(t)) { + Reg left = ra_scratch(as, RSET_FPR); + asm_tointg(as, ir, left); /* Frees dest reg. Do this before base alloc. */ + base = ra_alloc1(as, REF_BASE, RSET_GPR); + emit_rmro(as, XMM_MOVRM(as), left, base, ofs); + t.irt = IRT_NUM; /* Continue with a regular number type check. */ +#if LJ_64 + } else if (irt_islightud(t)) { + Reg dest = asm_load_lightud64(as, ir, (ir->op2 & IRSLOAD_TYPECHECK)); + if (ra_hasreg(dest)) { + base = ra_alloc1(as, REF_BASE, RSET_GPR); + emit_rmro(as, XO_MOV, dest|REX_64, base, ofs); + } + return; +#endif + } else if (ra_used(ir)) { + RegSet allow = irt_isnum(t) ? RSET_FPR : RSET_GPR; + Reg dest = ra_dest(as, ir, allow); + base = ra_alloc1(as, REF_BASE, RSET_GPR); + lua_assert(irt_isnum(t) || irt_isint(t) || irt_isaddr(t)); + if ((ir->op2 & IRSLOAD_CONVERT)) { + t.irt = irt_isint(t) ? IRT_NUM : IRT_INT; /* Check for original type. */ + emit_rmro(as, irt_isint(t) ? XO_CVTSI2SD : XO_CVTSD2SI, dest, base, ofs); + } else if (irt_isnum(t)) { + emit_rmro(as, XMM_MOVRM(as), dest, base, ofs); + } else { + emit_rmro(as, XO_MOV, dest, base, ofs); + } + } else { + if (!(ir->op2 & IRSLOAD_TYPECHECK)) + return; /* No type check: avoid base alloc. */ + base = ra_alloc1(as, REF_BASE, RSET_GPR); + } + if ((ir->op2 & IRSLOAD_TYPECHECK)) { + /* Need type check, even if the load result is unused. */ + asm_guardcc(as, irt_isnum(t) ? CC_AE : CC_NE); + if (LJ_64 && irt_type(t) >= IRT_NUM) { + lua_assert(irt_isinteger(t) || irt_isnum(t)); + emit_u32(as, LJ_TISNUM); + emit_rmro(as, XO_ARITHi, XOg_CMP, base, ofs+4); + } else { + emit_i8(as, irt_toitype(t)); + emit_rmro(as, XO_ARITHi8, XOg_CMP, base, ofs+4); + } + } +} + +/* -- Allocations --------------------------------------------------------- */ + +#if LJ_HASFFI +static void asm_cnew(ASMState *as, IRIns *ir) +{ + CTState *cts = ctype_ctsG(J2G(as->J)); + CTypeID ctypeid = (CTypeID)IR(ir->op1)->i; + CTSize sz = (ir->o == IR_CNEWI || ir->op2 == REF_NIL) ? + lj_ctype_size(cts, ctypeid) : (CTSize)IR(ir->op2)->i; + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_mem_newgco]; + IRRef args[2]; + lua_assert(sz != CTSIZE_INVALID); + + args[0] = ASMREF_L; /* lua_State *L */ + args[1] = ASMREF_TMP1; /* MSize size */ + as->gcsteps++; + asm_setupresult(as, ir, ci); /* GCcdata * */ + + /* Initialize immutable cdata object. */ + if (ir->o == IR_CNEWI) { + RegSet allow = (RSET_GPR & ~RSET_SCRATCH); +#if LJ_64 + Reg r64 = sz == 8 ? REX_64 : 0; + if (irref_isk(ir->op2)) { + IRIns *irk = IR(ir->op2); + uint64_t k = irk->o == IR_KINT64 ? ir_k64(irk)->u64 : + (uint64_t)(uint32_t)irk->i; + if (sz == 4 || checki32((int64_t)k)) { + emit_i32(as, (int32_t)k); + emit_rmro(as, XO_MOVmi, r64, RID_RET, sizeof(GCcdata)); + } else { + emit_movtomro(as, RID_ECX + r64, RID_RET, sizeof(GCcdata)); + emit_loadu64(as, RID_ECX, k); + } + } else { + Reg r = ra_alloc1(as, ir->op2, allow); + emit_movtomro(as, r + r64, RID_RET, sizeof(GCcdata)); + } +#else + int32_t ofs = sizeof(GCcdata); + if (sz == 8) { + ofs += 4; ir++; + lua_assert(ir->o == IR_HIOP); + } + do { + if (irref_isk(ir->op2)) { + emit_movmroi(as, RID_RET, ofs, IR(ir->op2)->i); + } else { + Reg r = ra_alloc1(as, ir->op2, allow); + emit_movtomro(as, r, RID_RET, ofs); + rset_clear(allow, r); + } + if (ofs == sizeof(GCcdata)) break; + ofs -= 4; ir--; + } while (1); +#endif + lua_assert(sz == 4 || sz == 8); + } + + /* Combine initialization of marked, gct and ctypeid. */ + emit_movtomro(as, RID_ECX, RID_RET, offsetof(GCcdata, marked)); + emit_gri(as, XG_ARITHi(XOg_OR), RID_ECX, + (int32_t)((~LJ_TCDATA<<8)+(ctypeid<<16))); + emit_gri(as, XG_ARITHi(XOg_AND), RID_ECX, LJ_GC_WHITES); + emit_opgl(as, XO_MOVZXb, RID_ECX, gc.currentwhite); + + asm_gencall(as, ci, args); + emit_loadi(as, ra_releasetmp(as, ASMREF_TMP1), (int32_t)(sz+sizeof(GCcdata))); +} +#else +#define asm_cnew(as, ir) ((void)0) +#endif + +/* -- Write barriers ------------------------------------------------------ */ + +static void asm_tbar(ASMState *as, IRIns *ir) +{ + Reg tab = ra_alloc1(as, ir->op1, RSET_GPR); + Reg tmp = ra_scratch(as, rset_exclude(RSET_GPR, tab)); + MCLabel l_end = emit_label(as); + emit_movtomro(as, tmp, tab, offsetof(GCtab, gclist)); + emit_setgl(as, tab, gc.grayagain); + emit_getgl(as, tmp, gc.grayagain); + emit_i8(as, ~LJ_GC_BLACK); + emit_rmro(as, XO_ARITHib, XOg_AND, tab, offsetof(GCtab, marked)); + emit_sjcc(as, CC_Z, l_end); + emit_i8(as, LJ_GC_BLACK); + emit_rmro(as, XO_GROUP3b, XOg_TEST, tab, offsetof(GCtab, marked)); +} + +static void asm_obar(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_barrieruv]; + IRRef args[2]; + MCLabel l_end; + Reg obj; + /* No need for other object barriers (yet). */ + lua_assert(IR(ir->op1)->o == IR_UREFC); + ra_evictset(as, RSET_SCRATCH); + l_end = emit_label(as); + args[0] = ASMREF_TMP1; /* global_State *g */ + args[1] = ir->op1; /* TValue *tv */ + asm_gencall(as, ci, args); + emit_loada(as, ra_releasetmp(as, ASMREF_TMP1), J2G(as->J)); + obj = IR(ir->op1)->r; + emit_sjcc(as, CC_Z, l_end); + emit_i8(as, LJ_GC_WHITES); + if (irref_isk(ir->op2)) { + GCobj *vp = ir_kgc(IR(ir->op2)); + emit_rma(as, XO_GROUP3b, XOg_TEST, &vp->gch.marked); + } else { + Reg val = ra_alloc1(as, ir->op2, rset_exclude(RSET_SCRATCH&RSET_GPR, obj)); + emit_rmro(as, XO_GROUP3b, XOg_TEST, val, (int32_t)offsetof(GChead, marked)); + } + emit_sjcc(as, CC_Z, l_end); + emit_i8(as, LJ_GC_BLACK); + emit_rmro(as, XO_GROUP3b, XOg_TEST, obj, + (int32_t)offsetof(GCupval, marked)-(int32_t)offsetof(GCupval, tv)); +} + +/* -- FP/int arithmetic and logic operations ------------------------------ */ + +/* Load reference onto x87 stack. Force a spill to memory if needed. */ +static void asm_x87load(ASMState *as, IRRef ref) +{ + IRIns *ir = IR(ref); + if (ir->o == IR_KNUM) { + cTValue *tv = ir_knum(ir); + if (tvispzero(tv)) /* Use fldz only for +0. */ + emit_x87op(as, XI_FLDZ); + else if (tvispone(tv)) + emit_x87op(as, XI_FLD1); + else + emit_rma(as, XO_FLDq, XOg_FLDq, tv); + } else if (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT && !ra_used(ir) && + !irref_isk(ir->op1) && mayfuse(as, ir->op1)) { + IRIns *iri = IR(ir->op1); + emit_rmro(as, XO_FILDd, XOg_FILDd, RID_ESP, ra_spill(as, iri)); + } else { + emit_mrm(as, XO_FLDq, XOg_FLDq, asm_fuseload(as, ref, RSET_EMPTY)); + } +} + +/* Try to rejoin pow from EXP2, MUL and LOG2 (if still unsplit). */ +static int fpmjoin_pow(ASMState *as, IRIns *ir) +{ + IRIns *irp = IR(ir->op1); + if (irp == ir-1 && irp->o == IR_MUL && !ra_used(irp)) { + IRIns *irpp = IR(irp->op1); + if (irpp == ir-2 && irpp->o == IR_FPMATH && + irpp->op2 == IRFPM_LOG2 && !ra_used(irpp)) { + /* The modified regs must match with the *.dasc implementation. */ + RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM2+1)|RID2RSET(RID_EAX); + IRIns *irx; + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + ra_evictset(as, drop); + ra_destreg(as, ir, RID_XMM0); + emit_call(as, lj_vm_pow_sse); + irx = IR(irpp->op1); + if (ra_noreg(irx->r) && ra_gethint(irx->r) == RID_XMM1) + irx->r = RID_INIT; /* Avoid allocating xmm1 for x. */ + ra_left(as, RID_XMM0, irpp->op1); + ra_left(as, RID_XMM1, irp->op2); + return 1; + } + } + return 0; +} + +static void asm_fpmath(ASMState *as, IRIns *ir) +{ + IRFPMathOp fpm = ir->o == IR_FPMATH ? (IRFPMathOp)ir->op2 : IRFPM_OTHER; + if (fpm == IRFPM_SQRT) { + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg left = asm_fuseload(as, ir->op1, RSET_FPR); + emit_mrm(as, XO_SQRTSD, dest, left); + } else if (fpm <= IRFPM_TRUNC) { + if (as->flags & JIT_F_SSE4_1) { /* SSE4.1 has a rounding instruction. */ + Reg dest = ra_dest(as, ir, RSET_FPR); + Reg left = asm_fuseload(as, ir->op1, RSET_FPR); + /* ROUNDSD has a 4-byte opcode which doesn't fit in x86Op. + ** Let's pretend it's a 3-byte opcode, and compensate afterwards. + ** This is atrocious, but the alternatives are much worse. + */ + /* Round down/up/trunc == 1001/1010/1011. */ + emit_i8(as, 0x09 + fpm); + emit_mrm(as, XO_ROUNDSD, dest, left); + if (LJ_64 && as->mcp[1] != (MCode)(XO_ROUNDSD >> 16)) { + as->mcp[0] = as->mcp[1]; as->mcp[1] = 0x0f; /* Swap 0F and REX. */ + } + *--as->mcp = 0x66; /* 1st byte of ROUNDSD opcode. */ + } else { /* Call helper functions for SSE2 variant. */ + /* The modified regs must match with the *.dasc implementation. */ + RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM3+1)|RID2RSET(RID_EAX); + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + ra_evictset(as, drop); + ra_destreg(as, ir, RID_XMM0); + emit_call(as, fpm == IRFPM_FLOOR ? lj_vm_floor_sse : + fpm == IRFPM_CEIL ? lj_vm_ceil_sse : lj_vm_trunc_sse); + ra_left(as, RID_XMM0, ir->op1); + } + } else if (fpm == IRFPM_EXP2 && fpmjoin_pow(as, ir)) { + /* Rejoined to pow(). */ + } else { /* Handle x87 ops. */ + int32_t ofs = sps_scale(ir->s); /* Use spill slot or temp slots. */ + Reg dest = ir->r; + if (ra_hasreg(dest)) { + ra_free(as, dest); + ra_modified(as, dest); + emit_rmro(as, XMM_MOVRM(as), dest, RID_ESP, ofs); + } + emit_rmro(as, XO_FSTPq, XOg_FSTPq, RID_ESP, ofs); + switch (fpm) { /* st0 = lj_vm_*(st0) */ + case IRFPM_EXP: emit_call(as, lj_vm_exp_x87); break; + case IRFPM_EXP2: emit_call(as, lj_vm_exp2_x87); break; + case IRFPM_SIN: emit_x87op(as, XI_FSIN); break; + case IRFPM_COS: emit_x87op(as, XI_FCOS); break; + case IRFPM_TAN: emit_x87op(as, XI_FPOP); emit_x87op(as, XI_FPTAN); break; + case IRFPM_LOG: case IRFPM_LOG2: case IRFPM_LOG10: + /* Note: the use of fyl2xp1 would be pointless here. When computing + ** log(1.0+eps) the precision is already lost after 1.0 is added. + ** Subtracting 1.0 won't recover it. OTOH math.log1p would make sense. + */ + emit_x87op(as, XI_FYL2X); break; + case IRFPM_OTHER: + switch (ir->o) { + case IR_ATAN2: + emit_x87op(as, XI_FPATAN); asm_x87load(as, ir->op2); break; + case IR_LDEXP: + emit_x87op(as, XI_FPOP1); emit_x87op(as, XI_FSCALE); break; + default: lua_assert(0); break; + } + break; + default: lua_assert(0); break; + } + asm_x87load(as, ir->op1); + switch (fpm) { + case IRFPM_LOG: emit_x87op(as, XI_FLDLN2); break; + case IRFPM_LOG2: emit_x87op(as, XI_FLD1); break; + case IRFPM_LOG10: emit_x87op(as, XI_FLDLG2); break; + case IRFPM_OTHER: + if (ir->o == IR_LDEXP) asm_x87load(as, ir->op2); + break; + default: break; + } + } +} + +static void asm_fppowi(ASMState *as, IRIns *ir) +{ + /* The modified regs must match with the *.dasc implementation. */ + RegSet drop = RSET_RANGE(RID_XMM0, RID_XMM1+1)|RID2RSET(RID_EAX); + if (ra_hasreg(ir->r)) + rset_clear(drop, ir->r); /* Dest reg handled below. */ + ra_evictset(as, drop); + ra_destreg(as, ir, RID_XMM0); + emit_call(as, lj_vm_powi_sse); + ra_left(as, RID_XMM0, ir->op1); + ra_left(as, RID_EAX, ir->op2); +} + +#if LJ_64 && LJ_HASFFI +static void asm_arith64(ASMState *as, IRIns *ir, IRCallID id) +{ + const CCallInfo *ci = &lj_ir_callinfo[id]; + IRRef args[2]; + args[0] = ir->op1; + args[1] = ir->op2; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} +#endif + +static void asm_intmod(ASMState *as, IRIns *ir) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_vm_modi]; + IRRef args[2]; + args[0] = ir->op1; + args[1] = ir->op2; + asm_setupresult(as, ir, ci); + asm_gencall(as, ci, args); +} + +static int asm_swapops(ASMState *as, IRIns *ir) +{ + IRIns *irl = IR(ir->op1); + IRIns *irr = IR(ir->op2); + lua_assert(ra_noreg(irr->r)); + if (!irm_iscomm(lj_ir_mode[ir->o])) + return 0; /* Can't swap non-commutative operations. */ + if (irref_isk(ir->op2)) + return 0; /* Don't swap constants to the left. */ + if (ra_hasreg(irl->r)) + return 1; /* Swap if left already has a register. */ + if (ra_samehint(ir->r, irr->r)) + return 1; /* Swap if dest and right have matching hints. */ + if (as->curins > as->loopref) { /* In variant part? */ + if (ir->op2 < as->loopref && !irt_isphi(irr->t)) + return 0; /* Keep invariants on the right. */ + if (ir->op1 < as->loopref && !irt_isphi(irl->t)) + return 1; /* Swap invariants to the right. */ + } + if (opisfusableload(irl->o)) + return 1; /* Swap fusable loads to the right. */ + return 0; /* Otherwise don't swap. */ +} + +static void asm_fparith(ASMState *as, IRIns *ir, x86Op xo) +{ + IRRef lref = ir->op1; + IRRef rref = ir->op2; + RegSet allow = RSET_FPR; + Reg dest; + Reg right = IR(rref)->r; + if (ra_hasreg(right)) { + rset_clear(allow, right); + ra_noweak(as, right); + } + dest = ra_dest(as, ir, allow); + if (lref == rref) { + right = dest; + } else if (ra_noreg(right)) { + if (asm_swapops(as, ir)) { + IRRef tmp = lref; lref = rref; rref = tmp; + } + right = asm_fuseload(as, rref, rset_clear(allow, dest)); + } + emit_mrm(as, xo, dest, right); + ra_left(as, dest, lref); +} + +static void asm_intarith(ASMState *as, IRIns *ir, x86Arith xa) +{ + IRRef lref = ir->op1; + IRRef rref = ir->op2; + RegSet allow = RSET_GPR; + Reg dest, right; + int32_t k = 0; + if (as->flagmcp == as->mcp) { /* Drop test r,r instruction. */ + as->flagmcp = NULL; + as->mcp += (LJ_64 && *as->mcp < XI_TESTb) ? 3 : 2; + } + right = IR(rref)->r; + if (ra_hasreg(right)) { + rset_clear(allow, right); + ra_noweak(as, right); + } + dest = ra_dest(as, ir, allow); + if (lref == rref) { + right = dest; + } else if (ra_noreg(right) && !asm_isk32(as, rref, &k)) { + if (asm_swapops(as, ir)) { + IRRef tmp = lref; lref = rref; rref = tmp; + } + right = asm_fuseloadm(as, rref, rset_clear(allow, dest), irt_is64(ir->t)); + } + if (irt_isguard(ir->t)) /* For IR_ADDOV etc. */ + asm_guardcc(as, CC_O); + if (xa != XOg_X_IMUL) { + if (ra_hasreg(right)) + emit_mrm(as, XO_ARITH(xa), REX_64IR(ir, dest), right); + else + emit_gri(as, XG_ARITHi(xa), REX_64IR(ir, dest), k); + } else if (ra_hasreg(right)) { /* IMUL r, mrm. */ + emit_mrm(as, XO_IMUL, REX_64IR(ir, dest), right); + } else { /* IMUL r, r, k. */ + /* NYI: use lea/shl/add/sub (FOLD only does 2^k) depending on CPU. */ + Reg left = asm_fuseloadm(as, lref, RSET_GPR, irt_is64(ir->t)); + x86Op xo; + if (checki8(k)) { emit_i8(as, k); xo = XO_IMULi8; + } else { emit_i32(as, k); xo = XO_IMULi; } + emit_mrm(as, xo, REX_64IR(ir, dest), left); + return; + } + ra_left(as, dest, lref); +} + +/* LEA is really a 4-operand ADD with an independent destination register, +** up to two source registers and an immediate. One register can be scaled +** by 1, 2, 4 or 8. This can be used to avoid moves or to fuse several +** instructions. +** +** Currently only a few common cases are supported: +** - 3-operand ADD: y = a+b; y = a+k with a and b already allocated +** - Left ADD fusion: y = (a+b)+k; y = (a+k)+b +** - Right ADD fusion: y = a+(b+k) +** The ommited variants have already been reduced by FOLD. +** +** There are more fusion opportunities, like gathering shifts or joining +** common references. But these are probably not worth the trouble, since +** array indexing is not decomposed and already makes use of all fields +** of the ModRM operand. +*/ +static int asm_lea(ASMState *as, IRIns *ir) +{ + IRIns *irl = IR(ir->op1); + IRIns *irr = IR(ir->op2); + RegSet allow = RSET_GPR; + Reg dest; + as->mrm.base = as->mrm.idx = RID_NONE; + as->mrm.scale = XM_SCALE1; + as->mrm.ofs = 0; + if (ra_hasreg(irl->r)) { + rset_clear(allow, irl->r); + ra_noweak(as, irl->r); + as->mrm.base = irl->r; + if (irref_isk(ir->op2) || ra_hasreg(irr->r)) { + /* The PHI renaming logic does a better job in some cases. */ + if (ra_hasreg(ir->r) && + ((irt_isphi(irl->t) && as->phireg[ir->r] == ir->op1) || + (irt_isphi(irr->t) && as->phireg[ir->r] == ir->op2))) + return 0; + if (irref_isk(ir->op2)) { + as->mrm.ofs = irr->i; + } else { + rset_clear(allow, irr->r); + ra_noweak(as, irr->r); + as->mrm.idx = irr->r; + } + } else if (irr->o == IR_ADD && mayfuse(as, ir->op2) && + irref_isk(irr->op2)) { + Reg idx = ra_alloc1(as, irr->op1, allow); + rset_clear(allow, idx); + as->mrm.idx = (uint8_t)idx; + as->mrm.ofs = IR(irr->op2)->i; + } else { + return 0; + } + } else if (ir->op1 != ir->op2 && irl->o == IR_ADD && mayfuse(as, ir->op1) && + (irref_isk(ir->op2) || irref_isk(irl->op2))) { + Reg idx, base = ra_alloc1(as, irl->op1, allow); + rset_clear(allow, base); + as->mrm.base = (uint8_t)base; + if (irref_isk(ir->op2)) { + as->mrm.ofs = irr->i; + idx = ra_alloc1(as, irl->op2, allow); + } else { + as->mrm.ofs = IR(irl->op2)->i; + idx = ra_alloc1(as, ir->op2, allow); + } + rset_clear(allow, idx); + as->mrm.idx = (uint8_t)idx; + } else { + return 0; + } + dest = ra_dest(as, ir, allow); + emit_mrm(as, XO_LEA, dest, RID_MRM); + return 1; /* Success. */ +} + +static void asm_add(ASMState *as, IRIns *ir) +{ + if (irt_isnum(ir->t)) + asm_fparith(as, ir, XO_ADDSD); + else if ((as->flags & JIT_F_LEA_AGU) || as->flagmcp == as->mcp || + irt_is64(ir->t) || !asm_lea(as, ir)) + asm_intarith(as, ir, XOg_ADD); +} + +static void asm_neg_not(ASMState *as, IRIns *ir, x86Group3 xg) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + emit_rr(as, XO_GROUP3, REX_64IR(ir, xg), dest); + ra_left(as, dest, ir->op1); +} + +static void asm_min_max(ASMState *as, IRIns *ir, int cc) +{ + Reg right, dest = ra_dest(as, ir, RSET_GPR); + IRRef lref = ir->op1, rref = ir->op2; + if (irref_isk(rref)) { lref = rref; rref = ir->op1; } + right = ra_alloc1(as, rref, rset_exclude(RSET_GPR, dest)); + emit_rr(as, XO_CMOV + (cc<<24), REX_64IR(ir, dest), right); + emit_rr(as, XO_CMP, REX_64IR(ir, dest), right); + ra_left(as, dest, lref); +} + +static void asm_bitswap(ASMState *as, IRIns *ir) +{ + Reg dest = ra_dest(as, ir, RSET_GPR); + as->mcp = emit_op(XO_BSWAP + ((dest&7) << 24), + REX_64IR(ir, 0), dest, 0, as->mcp, 1); + ra_left(as, dest, ir->op1); +} + +static void asm_bitshift(ASMState *as, IRIns *ir, x86Shift xs) +{ + IRRef rref = ir->op2; + IRIns *irr = IR(rref); + Reg dest; + if (irref_isk(rref)) { /* Constant shifts. */ + int shift; + dest = ra_dest(as, ir, RSET_GPR); + shift = irr->i & (irt_is64(ir->t) ? 63 : 31); + switch (shift) { + case 0: break; + case 1: emit_rr(as, XO_SHIFT1, REX_64IR(ir, xs), dest); break; + default: emit_shifti(as, REX_64IR(ir, xs), dest, shift); break; + } + } else { /* Variable shifts implicitly use register cl (i.e. ecx). */ + Reg right; + dest = ra_dest(as, ir, rset_exclude(RSET_GPR, RID_ECX)); + if (dest == RID_ECX) { + dest = ra_scratch(as, rset_exclude(RSET_GPR, RID_ECX)); + emit_rr(as, XO_MOV, RID_ECX, dest); + } + right = irr->r; + if (ra_noreg(right)) + right = ra_allocref(as, rref, RID2RSET(RID_ECX)); + else if (right != RID_ECX) + ra_scratch(as, RID2RSET(RID_ECX)); + emit_rr(as, XO_SHIFTcl, REX_64IR(ir, xs), dest); + ra_noweak(as, right); + if (right != RID_ECX) + emit_rr(as, XO_MOV, RID_ECX, right); + } + ra_left(as, dest, ir->op1); + /* + ** Note: avoid using the flags resulting from a shift or rotate! + ** All of them cause a partial flag stall, except for r,1 shifts + ** (but not rotates). And a shift count of 0 leaves the flags unmodified. + */ +} + +/* -- Comparisons --------------------------------------------------------- */ + +/* Virtual flags for unordered FP comparisons. */ +#define VCC_U 0x1000 /* Unordered. */ +#define VCC_P 0x2000 /* Needs extra CC_P branch. */ +#define VCC_S 0x4000 /* Swap avoids CC_P branch. */ +#define VCC_PS (VCC_P|VCC_S) + +/* Map of comparisons to flags. ORDER IR. */ +#define COMPFLAGS(ci, cin, cu, cf) ((ci)+((cu)<<4)+((cin)<<8)+(cf)) +static const uint16_t asm_compmap[IR_ABC+1] = { + /* signed non-eq unsigned flags */ + /* LT */ COMPFLAGS(CC_GE, CC_G, CC_AE, VCC_PS), + /* GE */ COMPFLAGS(CC_L, CC_L, CC_B, 0), + /* LE */ COMPFLAGS(CC_G, CC_G, CC_A, VCC_PS), + /* GT */ COMPFLAGS(CC_LE, CC_L, CC_BE, 0), + /* ULT */ COMPFLAGS(CC_AE, CC_A, CC_AE, VCC_U), + /* UGE */ COMPFLAGS(CC_B, CC_B, CC_B, VCC_U|VCC_PS), + /* ULE */ COMPFLAGS(CC_A, CC_A, CC_A, VCC_U), + /* UGT */ COMPFLAGS(CC_BE, CC_B, CC_BE, VCC_U|VCC_PS), + /* EQ */ COMPFLAGS(CC_NE, CC_NE, CC_NE, VCC_P), + /* NE */ COMPFLAGS(CC_E, CC_E, CC_E, VCC_U|VCC_P), + /* ABC */ COMPFLAGS(CC_BE, CC_B, CC_BE, VCC_U|VCC_PS) /* Same as UGT. */ +}; + +/* FP and integer comparisons. */ +static void asm_comp(ASMState *as, IRIns *ir, uint32_t cc) +{ + if (irt_isnum(ir->t)) { + IRRef lref = ir->op1; + IRRef rref = ir->op2; + Reg left, right; + MCLabel l_around; + /* + ** An extra CC_P branch is required to preserve ordered/unordered + ** semantics for FP comparisons. This can be avoided by swapping + ** the operands and inverting the condition (except for EQ and UNE). + ** So always try to swap if possible. + ** + ** Another option would be to swap operands to achieve better memory + ** operand fusion. But it's unlikely that this outweighs the cost + ** of the extra branches. + */ + if (cc & VCC_S) { /* Swap? */ + IRRef tmp = lref; lref = rref; rref = tmp; + cc ^= (VCC_PS|(5<<4)); /* A <-> B, AE <-> BE, PS <-> none */ + } + left = ra_alloc1(as, lref, RSET_FPR); + right = asm_fuseload(as, rref, rset_exclude(RSET_FPR, left)); + l_around = emit_label(as); + asm_guardcc(as, cc >> 4); + if (cc & VCC_P) { /* Extra CC_P branch required? */ + if (!(cc & VCC_U)) { + asm_guardcc(as, CC_P); /* Branch to exit for ordered comparisons. */ + } else if (l_around != as->invmcp) { + emit_sjcc(as, CC_P, l_around); /* Branch around for unordered. */ + } else { + /* Patched to mcloop by asm_loop_fixup. */ + as->loopinv = 2; + if (as->realign) + emit_sjcc(as, CC_P, as->mcp); + else + emit_jcc(as, CC_P, as->mcp); + } + } + emit_mrm(as, XO_UCOMISD, left, right); + } else { + IRRef lref = ir->op1, rref = ir->op2; + IROp leftop = (IROp)(IR(lref)->o); + Reg r64 = REX_64IR(ir, 0); + int32_t imm = 0; + lua_assert(irt_is64(ir->t) || irt_isint(ir->t) || + irt_isu32(ir->t) || irt_isaddr(ir->t) || irt_isu8(ir->t)); + /* Swap constants (only for ABC) and fusable loads to the right. */ + if (irref_isk(lref) || (!irref_isk(rref) && opisfusableload(leftop))) { + if ((cc & 0xc) == 0xc) cc ^= 0x53; /* L <-> G, LE <-> GE */ + else if ((cc & 0xa) == 0x2) cc ^= 0x55; /* A <-> B, AE <-> BE */ + lref = ir->op2; rref = ir->op1; + } + if (asm_isk32(as, rref, &imm)) { + IRIns *irl = IR(lref); + /* Check wether we can use test ins. Not for unsigned, since CF=0. */ + int usetest = (imm == 0 && (cc & 0xa) != 0x2); + if (usetest && irl->o == IR_BAND && irl+1 == ir && !ra_used(irl)) { + /* Combine comp(BAND(ref, r/imm), 0) into test mrm, r/imm. */ + Reg right, left = RID_NONE; + RegSet allow = RSET_GPR; + if (!asm_isk32(as, irl->op2, &imm)) { + left = ra_alloc1(as, irl->op2, allow); + rset_clear(allow, left); + } else { /* Try to Fuse IRT_I8/IRT_U8 loads, too. See below. */ + IRIns *irll = IR(irl->op1); + if (opisfusableload((IROp)irll->o) && + (irt_isi8(irll->t) || irt_isu8(irll->t))) { + IRType1 origt = irll->t; /* Temporarily flip types. */ + irll->t.irt = (irll->t.irt & ~IRT_TYPE) | IRT_INT; + as->curins--; /* Skip to BAND to avoid failing in noconflict(). */ + right = asm_fuseload(as, irl->op1, RSET_GPR); + as->curins++; + irll->t = origt; + if (right != RID_MRM) goto test_nofuse; + /* Fusion succeeded, emit test byte mrm, imm8. */ + asm_guardcc(as, cc); + emit_i8(as, (imm & 0xff)); + emit_mrm(as, XO_GROUP3b, XOg_TEST, RID_MRM); + return; + } + } + as->curins--; /* Skip to BAND to avoid failing in noconflict(). */ + right = asm_fuseloadm(as, irl->op1, allow, r64); + as->curins++; /* Undo the above. */ + test_nofuse: + asm_guardcc(as, cc); + if (ra_noreg(left)) { + emit_i32(as, imm); + emit_mrm(as, XO_GROUP3, r64 + XOg_TEST, right); + } else { + emit_mrm(as, XO_TEST, r64 + left, right); + } + } else { + Reg left; + if (opisfusableload((IROp)irl->o) && + ((irt_isu8(irl->t) && checku8(imm)) || + ((irt_isi8(irl->t) || irt_isi16(irl->t)) && checki8(imm)) || + (irt_isu16(irl->t) && checku16(imm) && checki8((int16_t)imm)))) { + /* Only the IRT_INT case is fused by asm_fuseload. + ** The IRT_I8/IRT_U8 loads and some IRT_I16/IRT_U16 loads + ** are handled here. + ** Note that cmp word [mem], imm16 should not be generated, + ** since it has a length-changing prefix. Compares of a word + ** against a sign-extended imm8 are ok, however. + */ + IRType1 origt = irl->t; /* Temporarily flip types. */ + irl->t.irt = (irl->t.irt & ~IRT_TYPE) | IRT_INT; + left = asm_fuseload(as, lref, RSET_GPR); + irl->t = origt; + if (left == RID_MRM) { /* Fusion succeeded? */ + if (irt_isu8(irl->t) || irt_isu16(irl->t)) + cc >>= 4; /* Need unsigned compare. */ + asm_guardcc(as, cc); + emit_i8(as, imm); + emit_mrm(as, (irt_isi8(origt) || irt_isu8(origt)) ? + XO_ARITHib : XO_ARITHiw8, r64 + XOg_CMP, RID_MRM); + return; + } /* Otherwise handle register case as usual. */ + } else { + left = asm_fuseloadm(as, lref, + irt_isu8(ir->t) ? RSET_GPR8 : RSET_GPR, r64); + } + asm_guardcc(as, cc); + if (usetest && left != RID_MRM) { + /* Use test r,r instead of cmp r,0. */ + x86Op xo = XO_TEST; + if (irt_isu8(ir->t)) { + lua_assert(ir->o == IR_EQ || ir->o == IR_NE); + xo = XO_TESTb; + if (!rset_test(RSET_RANGE(RID_EAX, RID_EBX+1), left)) { + if (LJ_64) { + left |= FORCE_REX; + } else { + emit_i32(as, 0xff); + emit_mrm(as, XO_GROUP3, XOg_TEST, left); + return; + } + } + } + emit_rr(as, xo, r64 + left, left); + if (irl+1 == ir) /* Referencing previous ins? */ + as->flagmcp = as->mcp; /* Set flag to drop test r,r if possible. */ + } else { + emit_gmrmi(as, XG_ARITHi(XOg_CMP), r64 + left, imm); + } + } + } else { + Reg left = ra_alloc1(as, lref, RSET_GPR); + Reg right = asm_fuseloadm(as, rref, rset_exclude(RSET_GPR, left), r64); + asm_guardcc(as, cc); + emit_mrm(as, XO_CMP, r64 + left, right); + } + } +} + +#if LJ_32 && LJ_HASFFI +/* 64 bit integer comparisons in 32 bit mode. */ +static void asm_comp_int64(ASMState *as, IRIns *ir) +{ + uint32_t cc = asm_compmap[(ir-1)->o]; + RegSet allow = RSET_GPR; + Reg lefthi = RID_NONE, leftlo = RID_NONE; + Reg righthi = RID_NONE, rightlo = RID_NONE; + MCLabel l_around; + x86ModRM mrm; + + as->curins--; /* Skip loword ins. Avoids failing in noconflict(), too. */ + + /* Allocate/fuse hiword operands. */ + if (irref_isk(ir->op2)) { + lefthi = asm_fuseload(as, ir->op1, allow); + } else { + lefthi = ra_alloc1(as, ir->op1, allow); + righthi = asm_fuseload(as, ir->op2, allow); + if (righthi == RID_MRM) { + if (as->mrm.base != RID_NONE) rset_clear(allow, as->mrm.base); + if (as->mrm.idx != RID_NONE) rset_clear(allow, as->mrm.idx); + } else { + rset_clear(allow, righthi); + } + } + mrm = as->mrm; /* Save state for hiword instruction. */ + + /* Allocate/fuse loword operands. */ + if (irref_isk((ir-1)->op2)) { + leftlo = asm_fuseload(as, (ir-1)->op1, allow); + } else { + leftlo = ra_alloc1(as, (ir-1)->op1, allow); + rightlo = asm_fuseload(as, (ir-1)->op2, allow); + if (rightlo == RID_MRM) { + if (as->mrm.base != RID_NONE) rset_clear(allow, as->mrm.base); + if (as->mrm.idx != RID_NONE) rset_clear(allow, as->mrm.idx); + } else { + rset_clear(allow, rightlo); + } + } + + /* All register allocations must be performed _before_ this point. */ + l_around = emit_label(as); + as->invmcp = as->flagmcp = NULL; /* Cannot use these optimizations. */ + + /* Loword comparison and branch. */ + asm_guardcc(as, cc >> 4); /* Always use unsigned compare for loword. */ + if (ra_noreg(rightlo)) { + int32_t imm = IR((ir-1)->op2)->i; + if (imm == 0 && ((cc >> 4) & 0xa) != 0x2 && leftlo != RID_MRM) + emit_rr(as, XO_TEST, leftlo, leftlo); + else + emit_gmrmi(as, XG_ARITHi(XOg_CMP), leftlo, imm); + } else { + emit_mrm(as, XO_CMP, leftlo, rightlo); + } + + /* Hiword comparison and branches. */ + if ((cc & 15) != CC_NE) + emit_sjcc(as, CC_NE, l_around); /* Hiword unequal: skip loword compare. */ + if ((cc & 15) != CC_E) + asm_guardcc(as, cc >> 8); /* Hiword compare without equality check. */ + as->mrm = mrm; /* Restore state. */ + if (ra_noreg(righthi)) { + int32_t imm = IR(ir->op2)->i; + if (imm == 0 && (cc & 0xa) != 0x2 && lefthi != RID_MRM) + emit_rr(as, XO_TEST, lefthi, lefthi); + else + emit_gmrmi(as, XG_ARITHi(XOg_CMP), lefthi, imm); + } else { + emit_mrm(as, XO_CMP, lefthi, righthi); + } +} +#endif + +/* -- Support for 64 bit ops in 32 bit mode ------------------------------- */ + +/* Hiword op of a split 64 bit op. Previous op must be the loword op. */ +static void asm_hiop(ASMState *as, IRIns *ir) +{ +#if LJ_32 && LJ_HASFFI + /* HIOP is marked as a store because it needs its own DCE logic. */ + int uselo = ra_used(ir-1), usehi = ra_used(ir); /* Loword/hiword used? */ + if (LJ_UNLIKELY(!(as->flags & JIT_F_OPT_DCE))) uselo = usehi = 1; + if ((ir-1)->o == IR_CONV) { /* Conversions to/from 64 bit. */ + if (usehi || uselo) { + if (irt_isfp(ir->t)) + asm_conv_fp_int64(as, ir); + else + asm_conv_int64_fp(as, ir); + } + as->curins--; /* Always skip the CONV. */ + return; + } else if ((ir-1)->o <= IR_NE) { /* 64 bit integer comparisons. ORDER IR. */ + asm_comp_int64(as, ir); + return; + } else if ((ir-1)->o == IR_XSTORE) { + if ((ir-1)->r != RID_SINK) + asm_fxstore(as, ir); + return; + } + if (!usehi) return; /* Skip unused hiword op for all remaining ops. */ + switch ((ir-1)->o) { + case IR_ADD: + as->flagmcp = NULL; + as->curins--; + asm_intarith(as, ir, XOg_ADC); + asm_intarith(as, ir-1, XOg_ADD); + break; + case IR_SUB: + as->flagmcp = NULL; + as->curins--; + asm_intarith(as, ir, XOg_SBB); + asm_intarith(as, ir-1, XOg_SUB); + break; + case IR_NEG: { + Reg dest = ra_dest(as, ir, RSET_GPR); + emit_rr(as, XO_GROUP3, XOg_NEG, dest); + emit_i8(as, 0); + emit_rr(as, XO_ARITHi8, XOg_ADC, dest); + ra_left(as, dest, ir->op1); + as->curins--; + asm_neg_not(as, ir-1, XOg_NEG); + break; + } + case IR_CALLN: + case IR_CALLXS: + if (!uselo) + ra_allocref(as, ir->op1, RID2RSET(RID_RETLO)); /* Mark lo op as used. */ + break; + case IR_CNEWI: + /* Nothing to do here. Handled by CNEWI itself. */ + break; + default: lua_assert(0); break; + } +#else + UNUSED(as); UNUSED(ir); lua_assert(0); /* Unused on x64 or without FFI. */ +#endif +} + +/* -- Stack handling ------------------------------------------------------ */ + +/* Check Lua stack size for overflow. Use exit handler as fallback. */ +static void asm_stack_check(ASMState *as, BCReg topslot, + IRIns *irp, RegSet allow, ExitNo exitno) +{ + /* Try to get an unused temp. register, otherwise spill/restore eax. */ + Reg pbase = irp ? irp->r : RID_BASE; + Reg r = allow ? rset_pickbot(allow) : RID_EAX; + emit_jcc(as, CC_B, exitstub_addr(as->J, exitno)); + if (allow == RSET_EMPTY) /* Restore temp. register. */ + emit_rmro(as, XO_MOV, r|REX_64, RID_ESP, 0); + else + ra_modified(as, r); + emit_gri(as, XG_ARITHi(XOg_CMP), r, (int32_t)(8*topslot)); + if (ra_hasreg(pbase) && pbase != r) + emit_rr(as, XO_ARITH(XOg_SUB), r, pbase); + else + emit_rmro(as, XO_ARITH(XOg_SUB), r, RID_NONE, + ptr2addr(&J2G(as->J)->jit_base)); + emit_rmro(as, XO_MOV, r, r, offsetof(lua_State, maxstack)); + emit_getgl(as, r, jit_L); + if (allow == RSET_EMPTY) /* Spill temp. register. */ + emit_rmro(as, XO_MOVto, r|REX_64, RID_ESP, 0); +} + +/* Restore Lua stack from on-trace state. */ +static void asm_stack_restore(ASMState *as, SnapShot *snap) +{ + SnapEntry *map = &as->T->snapmap[snap->mapofs]; + SnapEntry *flinks = &as->T->snapmap[snap_nextofs(as->T, snap)-1]; + MSize n, nent = snap->nent; + /* Store the value of all modified slots to the Lua stack. */ + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + BCReg s = snap_slot(sn); + int32_t ofs = 8*((int32_t)s-1); + IRRef ref = snap_ref(sn); + IRIns *ir = IR(ref); + if ((sn & SNAP_NORESTORE)) + continue; + if (irt_isnum(ir->t)) { + Reg src = ra_alloc1(as, ref, RSET_FPR); + emit_rmro(as, XO_MOVSDto, src, RID_BASE, ofs); + } else { + lua_assert(irt_ispri(ir->t) || irt_isaddr(ir->t) || + (LJ_DUALNUM && irt_isinteger(ir->t))); + if (!irref_isk(ref)) { + Reg src = ra_alloc1(as, ref, rset_exclude(RSET_GPR, RID_BASE)); + emit_movtomro(as, REX_64IR(ir, src), RID_BASE, ofs); + } else if (!irt_ispri(ir->t)) { + emit_movmroi(as, RID_BASE, ofs, ir->i); + } + if ((sn & (SNAP_CONT|SNAP_FRAME))) { + if (s != 0) /* Do not overwrite link to previous frame. */ + emit_movmroi(as, RID_BASE, ofs+4, (int32_t)(*flinks--)); + } else { + if (!(LJ_64 && irt_islightud(ir->t))) + emit_movmroi(as, RID_BASE, ofs+4, irt_toitype(ir->t)); + } + } + checkmclim(as); + } + lua_assert(map + nent == flinks); +} + +/* -- GC handling --------------------------------------------------------- */ + +/* Check GC threshold and do one or more GC steps. */ +static void asm_gc_check(ASMState *as) +{ + const CCallInfo *ci = &lj_ir_callinfo[IRCALL_lj_gc_step_jit]; + IRRef args[2]; + MCLabel l_end; + Reg tmp; + ra_evictset(as, RSET_SCRATCH); + l_end = emit_label(as); + /* Exit trace if in GCSatomic or GCSfinalize. Avoids syncing GC objects. */ + asm_guardcc(as, CC_NE); /* Assumes asm_snap_prep() already done. */ + emit_rr(as, XO_TEST, RID_RET, RID_RET); + args[0] = ASMREF_TMP1; /* global_State *g */ + args[1] = ASMREF_TMP2; /* MSize steps */ + asm_gencall(as, ci, args); + tmp = ra_releasetmp(as, ASMREF_TMP1); + emit_loada(as, tmp, J2G(as->J)); + emit_loadi(as, ra_releasetmp(as, ASMREF_TMP2), as->gcsteps); + /* Jump around GC step if GC total < GC threshold. */ + emit_sjcc(as, CC_B, l_end); + emit_opgl(as, XO_ARITH(XOg_CMP), tmp, gc.threshold); + emit_getgl(as, tmp, gc.total); + as->gcsteps = 0; + checkmclim(as); +} + +/* -- Loop handling ------------------------------------------------------- */ + +/* Fixup the loop branch. */ +static void asm_loop_fixup(ASMState *as) +{ + MCode *p = as->mctop; + MCode *target = as->mcp; + if (as->realign) { /* Realigned loops use short jumps. */ + as->realign = NULL; /* Stop another retry. */ + lua_assert(((intptr_t)target & 15) == 0); + if (as->loopinv) { /* Inverted loop branch? */ + p -= 5; + p[0] = XI_JMP; + lua_assert(target - p >= -128); + p[-1] = (MCode)(target - p); /* Patch sjcc. */ + if (as->loopinv == 2) + p[-3] = (MCode)(target - p + 2); /* Patch opt. short jp. */ + } else { + lua_assert(target - p >= -128); + p[-1] = (MCode)(int8_t)(target - p); /* Patch short jmp. */ + p[-2] = XI_JMPs; + } + } else { + MCode *newloop; + p[-5] = XI_JMP; + if (as->loopinv) { /* Inverted loop branch? */ + /* asm_guardcc already inverted the jcc and patched the jmp. */ + p -= 5; + newloop = target+4; + *(int32_t *)(p-4) = (int32_t)(target - p); /* Patch jcc. */ + if (as->loopinv == 2) { + *(int32_t *)(p-10) = (int32_t)(target - p + 6); /* Patch opt. jp. */ + newloop = target+8; + } + } else { /* Otherwise just patch jmp. */ + *(int32_t *)(p-4) = (int32_t)(target - p); + newloop = target+3; + } + /* Realign small loops and shorten the loop branch. */ + if (newloop >= p - 128) { + as->realign = newloop; /* Force a retry and remember alignment. */ + as->curins = as->stopins; /* Abort asm_trace now. */ + as->T->nins = as->orignins; /* Remove any added renames. */ + } + } +} + +/* -- Head of trace ------------------------------------------------------- */ + +/* Coalesce BASE register for a root trace. */ +static void asm_head_root_base(ASMState *as) +{ + IRIns *ir = IR(REF_BASE); + Reg r = ir->r; + if (ra_hasreg(r)) { + ra_free(as, r); + if (rset_test(as->modset, r)) + ir->r = RID_INIT; /* No inheritance for modified BASE register. */ + if (r != RID_BASE) + emit_rr(as, XO_MOV, r, RID_BASE); + } +} + +/* Coalesce or reload BASE register for a side trace. */ +static RegSet asm_head_side_base(ASMState *as, IRIns *irp, RegSet allow) +{ + IRIns *ir = IR(REF_BASE); + Reg r = ir->r; + if (ra_hasreg(r)) { + ra_free(as, r); + if (rset_test(as->modset, r)) + ir->r = RID_INIT; /* No inheritance for modified BASE register. */ + if (irp->r == r) { + rset_clear(allow, r); /* Mark same BASE register as coalesced. */ + } else if (ra_hasreg(irp->r) && rset_test(as->freeset, irp->r)) { + rset_clear(allow, irp->r); + emit_rr(as, XO_MOV, r, irp->r); /* Move from coalesced parent reg. */ + } else { + emit_getgl(as, r, jit_base); /* Otherwise reload BASE. */ + } + } + return allow; +} + +/* -- Tail of trace ------------------------------------------------------- */ + +/* Fixup the tail code. */ +static void asm_tail_fixup(ASMState *as, TraceNo lnk) +{ + /* Note: don't use as->mcp swap + emit_*: emit_op overwrites more bytes. */ + MCode *p = as->mctop; + MCode *target, *q; + int32_t spadj = as->T->spadjust; + if (spadj == 0) { + p -= ((as->flags & JIT_F_LEA_AGU) ? 7 : 6) + (LJ_64 ? 1 : 0); + } else { + MCode *p1; + /* Patch stack adjustment. */ + if (checki8(spadj)) { + p -= 3; + p1 = p-6; + *p1 = (MCode)spadj; + } else { + p1 = p-9; + *(int32_t *)p1 = spadj; + } + if ((as->flags & JIT_F_LEA_AGU)) { +#if LJ_64 + p1[-4] = 0x48; +#endif + p1[-3] = (MCode)XI_LEA; + p1[-2] = MODRM(checki8(spadj) ? XM_OFS8 : XM_OFS32, RID_ESP, RID_ESP); + p1[-1] = MODRM(XM_SCALE1, RID_ESP, RID_ESP); + } else { +#if LJ_64 + p1[-3] = 0x48; +#endif + p1[-2] = (MCode)(checki8(spadj) ? XI_ARITHi8 : XI_ARITHi); + p1[-1] = MODRM(XM_REG, XOg_ADD, RID_ESP); + } + } + /* Patch exit branch. */ + target = lnk ? traceref(as->J, lnk)->mcode : (MCode *)lj_vm_exit_interp; + *(int32_t *)(p-4) = jmprel(p, target); + p[-5] = XI_JMP; + /* Drop unused mcode tail. Fill with NOPs to make the prefetcher happy. */ + for (q = as->mctop-1; q >= p; q--) + *q = XI_NOP; + as->mctop = p; +} + +/* Prepare tail of code. */ +static void asm_tail_prep(ASMState *as) +{ + MCode *p = as->mctop; + /* Realign and leave room for backwards loop branch or exit branch. */ + if (as->realign) { + int i = ((int)(intptr_t)as->realign) & 15; + /* Fill unused mcode tail with NOPs to make the prefetcher happy. */ + while (i-- > 0) + *--p = XI_NOP; + as->mctop = p; + p -= (as->loopinv ? 5 : 2); /* Space for short/near jmp. */ + } else { + p -= 5; /* Space for exit branch (near jmp). */ + } + if (as->loopref) { + as->invmcp = as->mcp = p; + } else { + /* Leave room for ESP adjustment: add esp, imm or lea esp, [esp+imm] */ + as->mcp = p - (((as->flags & JIT_F_LEA_AGU) ? 7 : 6) + (LJ_64 ? 1 : 0)); + as->invmcp = NULL; + } +} + +/* -- Instruction dispatch ------------------------------------------------ */ + +/* Assemble a single instruction. */ +static void asm_ir(ASMState *as, IRIns *ir) +{ + switch ((IROp)ir->o) { + /* Miscellaneous ops. */ + case IR_LOOP: asm_loop(as); break; + case IR_NOP: case IR_XBAR: lua_assert(!ra_used(ir)); break; + case IR_USE: + ra_alloc1(as, ir->op1, irt_isfp(ir->t) ? RSET_FPR : RSET_GPR); break; + case IR_PHI: asm_phi(as, ir); break; + case IR_HIOP: asm_hiop(as, ir); break; + case IR_GCSTEP: asm_gcstep(as, ir); break; + + /* Guarded assertions. */ + case IR_LT: case IR_GE: case IR_LE: case IR_GT: + case IR_ULT: case IR_UGE: case IR_ULE: case IR_UGT: + case IR_EQ: case IR_NE: case IR_ABC: + asm_comp(as, ir, asm_compmap[ir->o]); + break; + + case IR_RETF: asm_retf(as, ir); break; + + /* Bit ops. */ + case IR_BNOT: asm_neg_not(as, ir, XOg_NOT); break; + case IR_BSWAP: asm_bitswap(as, ir); break; + + case IR_BAND: asm_intarith(as, ir, XOg_AND); break; + case IR_BOR: asm_intarith(as, ir, XOg_OR); break; + case IR_BXOR: asm_intarith(as, ir, XOg_XOR); break; + + case IR_BSHL: asm_bitshift(as, ir, XOg_SHL); break; + case IR_BSHR: asm_bitshift(as, ir, XOg_SHR); break; + case IR_BSAR: asm_bitshift(as, ir, XOg_SAR); break; + case IR_BROL: asm_bitshift(as, ir, XOg_ROL); break; + case IR_BROR: asm_bitshift(as, ir, XOg_ROR); break; + + /* Arithmetic ops. */ + case IR_ADD: asm_add(as, ir); break; + case IR_SUB: + if (irt_isnum(ir->t)) + asm_fparith(as, ir, XO_SUBSD); + else /* Note: no need for LEA trick here. i-k is encoded as i+(-k). */ + asm_intarith(as, ir, XOg_SUB); + break; + case IR_MUL: + if (irt_isnum(ir->t)) + asm_fparith(as, ir, XO_MULSD); + else + asm_intarith(as, ir, XOg_X_IMUL); + break; + case IR_DIV: +#if LJ_64 && LJ_HASFFI + if (!irt_isnum(ir->t)) + asm_arith64(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 : + IRCALL_lj_carith_divu64); + else +#endif + asm_fparith(as, ir, XO_DIVSD); + break; + case IR_MOD: +#if LJ_64 && LJ_HASFFI + if (!irt_isint(ir->t)) + asm_arith64(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 : + IRCALL_lj_carith_modu64); + else +#endif + asm_intmod(as, ir); + break; + + case IR_NEG: + if (irt_isnum(ir->t)) + asm_fparith(as, ir, XO_XORPS); + else + asm_neg_not(as, ir, XOg_NEG); + break; + case IR_ABS: asm_fparith(as, ir, XO_ANDPS); break; + + case IR_MIN: + if (irt_isnum(ir->t)) + asm_fparith(as, ir, XO_MINSD); + else + asm_min_max(as, ir, CC_G); + break; + case IR_MAX: + if (irt_isnum(ir->t)) + asm_fparith(as, ir, XO_MAXSD); + else + asm_min_max(as, ir, CC_L); + break; + + case IR_FPMATH: case IR_ATAN2: case IR_LDEXP: + asm_fpmath(as, ir); + break; + case IR_POW: +#if LJ_64 && LJ_HASFFI + if (!irt_isnum(ir->t)) + asm_arith64(as, ir, irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : + IRCALL_lj_carith_powu64); + else +#endif + asm_fppowi(as, ir); + break; + + /* Overflow-checking arithmetic ops. Note: don't use LEA here! */ + case IR_ADDOV: asm_intarith(as, ir, XOg_ADD); break; + case IR_SUBOV: asm_intarith(as, ir, XOg_SUB); break; + case IR_MULOV: asm_intarith(as, ir, XOg_X_IMUL); break; + + /* Memory references. */ + case IR_AREF: asm_aref(as, ir); break; + case IR_HREF: asm_href(as, ir); break; + case IR_HREFK: asm_hrefk(as, ir); break; + case IR_NEWREF: asm_newref(as, ir); break; + case IR_UREFO: case IR_UREFC: asm_uref(as, ir); break; + case IR_FREF: asm_fref(as, ir); break; + case IR_STRREF: asm_strref(as, ir); break; + + /* Loads and stores. */ + case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: + asm_ahuvload(as, ir); + break; + case IR_FLOAD: case IR_XLOAD: asm_fxload(as, ir); break; + case IR_SLOAD: asm_sload(as, ir); break; + + case IR_ASTORE: case IR_HSTORE: case IR_USTORE: asm_ahustore(as, ir); break; + case IR_FSTORE: case IR_XSTORE: asm_fxstore(as, ir); break; + + /* Allocations. */ + case IR_SNEW: case IR_XSNEW: asm_snew(as, ir); break; + case IR_TNEW: asm_tnew(as, ir); break; + case IR_TDUP: asm_tdup(as, ir); break; + case IR_CNEW: case IR_CNEWI: asm_cnew(as, ir); break; + + /* Write barriers. */ + case IR_TBAR: asm_tbar(as, ir); break; + case IR_OBAR: asm_obar(as, ir); break; + + /* Type conversions. */ + case IR_TOBIT: asm_tobit(as, ir); break; + case IR_CONV: asm_conv(as, ir); break; + case IR_TOSTR: asm_tostr(as, ir); break; + case IR_STRTO: asm_strto(as, ir); break; + + /* Calls. */ + case IR_CALLN: case IR_CALLL: case IR_CALLS: asm_call(as, ir); break; + case IR_CALLXS: asm_callx(as, ir); break; + case IR_CARG: break; + + default: + setintV(&as->J->errinfo, ir->o); + lj_trace_err_info(as->J, LJ_TRERR_NYIIR); + break; + } +} + +/* -- Trace setup --------------------------------------------------------- */ + +/* Ensure there are enough stack slots for call arguments. */ +static Reg asm_setup_call_slots(ASMState *as, IRIns *ir, const CCallInfo *ci) +{ + IRRef args[CCI_NARGS_MAX]; + int nslots; + asm_collectargs(as, ir, ci, args); + nslots = asm_count_call_slots(as, ci, args); + if (nslots > as->evenspill) /* Leave room for args in stack slots. */ + as->evenspill = nslots; +#if LJ_64 + return irt_isfp(ir->t) ? REGSP_HINT(RID_FPRET) : REGSP_HINT(RID_RET); +#else + return irt_isfp(ir->t) ? REGSP_INIT : REGSP_HINT(RID_RET); +#endif +} + +/* Target-specific setup. */ +static void asm_setup_target(ASMState *as) +{ + asm_exitstub_setup(as, as->T->nsnap); +} + +/* -- Trace patching ------------------------------------------------------ */ + +/* Patch exit jumps of existing machine code to a new target. */ +void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target) +{ + MCode *p = T->mcode; + MCode *mcarea = lj_mcode_patch(J, p, 0); + MSize len = T->szmcode; + MCode *px = exitstub_addr(J, exitno) - 6; + MCode *pe = p+len-6; + uint32_t stateaddr = u32ptr(&J2G(J)->vmstate); + if (len > 5 && p[len-5] == XI_JMP && p+len-6 + *(int32_t *)(p+len-4) == px) + *(int32_t *)(p+len-4) = jmprel(p+len, target); + /* Do not patch parent exit for a stack check. Skip beyond vmstate update. */ + for (; p < pe; p++) + if (*(uint32_t *)(p+(LJ_64 ? 3 : 2)) == stateaddr && p[0] == XI_MOVmi) { + p += LJ_64 ? 11 : 10; + break; + } + lua_assert(p < pe); + for (; p < pe; p++) { + if ((*(uint16_t *)p & 0xf0ff) == 0x800f && p + *(int32_t *)(p+2) == px) { + *(int32_t *)(p+2) = jmprel(p+6, target); + p += 5; + } + } + lj_mcode_sync(T->mcode, T->mcode + T->szmcode); + lj_mcode_patch(J, mcarea, 1); +} + diff --git a/external/lua/luajit/src/src/lj_bc.c b/external/lua/luajit/src/src/lj_bc.c new file mode 100644 index 0000000000..1e5869e6e1 --- /dev/null +++ b/external/lua/luajit/src/src/lj_bc.c @@ -0,0 +1,14 @@ +/* +** Bytecode instruction modes. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_bc_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_bc.h" + +/* Bytecode offsets and bytecode instruction modes. */ +#include "lj_bcdef.h" + diff --git a/external/lua/luajit/src/src/lj_bc.h b/external/lua/luajit/src/src/lj_bc.h new file mode 100644 index 0000000000..56e71dd9dc --- /dev/null +++ b/external/lua/luajit/src/src/lj_bc.h @@ -0,0 +1,261 @@ +/* +** Bytecode instruction format. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_BC_H +#define _LJ_BC_H + +#include "lj_def.h" +#include "lj_arch.h" + +/* Bytecode instruction format, 32 bit wide, fields of 8 or 16 bit: +** +** +----+----+----+----+ +** | B | C | A | OP | Format ABC +** +----+----+----+----+ +** | D | A | OP | Format AD +** +-------------------- +** MSB LSB +** +** In-memory instructions are always stored in host byte order. +*/ + +/* Operand ranges and related constants. */ +#define BCMAX_A 0xff +#define BCMAX_B 0xff +#define BCMAX_C 0xff +#define BCMAX_D 0xffff +#define BCBIAS_J 0x8000 +#define NO_REG BCMAX_A +#define NO_JMP (~(BCPos)0) + +/* Macros to get instruction fields. */ +#define bc_op(i) ((BCOp)((i)&0xff)) +#define bc_a(i) ((BCReg)(((i)>>8)&0xff)) +#define bc_b(i) ((BCReg)((i)>>24)) +#define bc_c(i) ((BCReg)(((i)>>16)&0xff)) +#define bc_d(i) ((BCReg)((i)>>16)) +#define bc_j(i) ((ptrdiff_t)bc_d(i)-BCBIAS_J) + +/* Macros to set instruction fields. */ +#define setbc_byte(p, x, ofs) \ + ((uint8_t *)(p))[LJ_ENDIAN_SELECT(ofs, 3-ofs)] = (uint8_t)(x) +#define setbc_op(p, x) setbc_byte(p, (x), 0) +#define setbc_a(p, x) setbc_byte(p, (x), 1) +#define setbc_b(p, x) setbc_byte(p, (x), 3) +#define setbc_c(p, x) setbc_byte(p, (x), 2) +#define setbc_d(p, x) \ + ((uint16_t *)(p))[LJ_ENDIAN_SELECT(1, 0)] = (uint16_t)(x) +#define setbc_j(p, x) setbc_d(p, (BCPos)((int32_t)(x)+BCBIAS_J)) + +/* Macros to compose instructions. */ +#define BCINS_ABC(o, a, b, c) \ + (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(b)<<24)|((BCIns)(c)<<16)) +#define BCINS_AD(o, a, d) \ + (((BCIns)(o))|((BCIns)(a)<<8)|((BCIns)(d)<<16)) +#define BCINS_AJ(o, a, j) BCINS_AD(o, a, (BCPos)((int32_t)(j)+BCBIAS_J)) + +/* Bytecode instruction definition. Order matters, see below. +** +** (name, filler, Amode, Bmode, Cmode or Dmode, metamethod) +** +** The opcode name suffixes specify the type for RB/RC or RD: +** V = variable slot +** S = string const +** N = number const +** P = primitive type (~itype) +** B = unsigned byte literal +** M = multiple args/results +*/ +#define BCDEF(_) \ + /* Comparison ops. ORDER OPR. */ \ + _(ISLT, var, ___, var, lt) \ + _(ISGE, var, ___, var, lt) \ + _(ISLE, var, ___, var, le) \ + _(ISGT, var, ___, var, le) \ + \ + _(ISEQV, var, ___, var, eq) \ + _(ISNEV, var, ___, var, eq) \ + _(ISEQS, var, ___, str, eq) \ + _(ISNES, var, ___, str, eq) \ + _(ISEQN, var, ___, num, eq) \ + _(ISNEN, var, ___, num, eq) \ + _(ISEQP, var, ___, pri, eq) \ + _(ISNEP, var, ___, pri, eq) \ + \ + /* Unary test and copy ops. */ \ + _(ISTC, dst, ___, var, ___) \ + _(ISFC, dst, ___, var, ___) \ + _(IST, ___, ___, var, ___) \ + _(ISF, ___, ___, var, ___) \ + \ + /* Unary ops. */ \ + _(MOV, dst, ___, var, ___) \ + _(NOT, dst, ___, var, ___) \ + _(UNM, dst, ___, var, unm) \ + _(LEN, dst, ___, var, len) \ + \ + /* Binary ops. ORDER OPR. VV last, POW must be next. */ \ + _(ADDVN, dst, var, num, add) \ + _(SUBVN, dst, var, num, sub) \ + _(MULVN, dst, var, num, mul) \ + _(DIVVN, dst, var, num, div) \ + _(MODVN, dst, var, num, mod) \ + \ + _(ADDNV, dst, var, num, add) \ + _(SUBNV, dst, var, num, sub) \ + _(MULNV, dst, var, num, mul) \ + _(DIVNV, dst, var, num, div) \ + _(MODNV, dst, var, num, mod) \ + \ + _(ADDVV, dst, var, var, add) \ + _(SUBVV, dst, var, var, sub) \ + _(MULVV, dst, var, var, mul) \ + _(DIVVV, dst, var, var, div) \ + _(MODVV, dst, var, var, mod) \ + \ + _(POW, dst, var, var, pow) \ + _(CAT, dst, rbase, rbase, concat) \ + \ + /* Constant ops. */ \ + _(KSTR, dst, ___, str, ___) \ + _(KCDATA, dst, ___, cdata, ___) \ + _(KSHORT, dst, ___, lits, ___) \ + _(KNUM, dst, ___, num, ___) \ + _(KPRI, dst, ___, pri, ___) \ + _(KNIL, base, ___, base, ___) \ + \ + /* Upvalue and function ops. */ \ + _(UGET, dst, ___, uv, ___) \ + _(USETV, uv, ___, var, ___) \ + _(USETS, uv, ___, str, ___) \ + _(USETN, uv, ___, num, ___) \ + _(USETP, uv, ___, pri, ___) \ + _(UCLO, rbase, ___, jump, ___) \ + _(FNEW, dst, ___, func, gc) \ + \ + /* Table ops. */ \ + _(TNEW, dst, ___, lit, gc) \ + _(TDUP, dst, ___, tab, gc) \ + _(GGET, dst, ___, str, index) \ + _(GSET, var, ___, str, newindex) \ + _(TGETV, dst, var, var, index) \ + _(TGETS, dst, var, str, index) \ + _(TGETB, dst, var, lit, index) \ + _(TSETV, var, var, var, newindex) \ + _(TSETS, var, var, str, newindex) \ + _(TSETB, var, var, lit, newindex) \ + _(TSETM, base, ___, num, newindex) \ + \ + /* Calls and vararg handling. T = tail call. */ \ + _(CALLM, base, lit, lit, call) \ + _(CALL, base, lit, lit, call) \ + _(CALLMT, base, ___, lit, call) \ + _(CALLT, base, ___, lit, call) \ + _(ITERC, base, lit, lit, call) \ + _(ITERN, base, lit, lit, call) \ + _(VARG, base, lit, lit, ___) \ + _(ISNEXT, base, ___, jump, ___) \ + \ + /* Returns. */ \ + _(RETM, base, ___, lit, ___) \ + _(RET, rbase, ___, lit, ___) \ + _(RET0, rbase, ___, lit, ___) \ + _(RET1, rbase, ___, lit, ___) \ + \ + /* Loops and branches. I/J = interp/JIT, I/C/L = init/call/loop. */ \ + _(FORI, base, ___, jump, ___) \ + _(JFORI, base, ___, jump, ___) \ + \ + _(FORL, base, ___, jump, ___) \ + _(IFORL, base, ___, jump, ___) \ + _(JFORL, base, ___, lit, ___) \ + \ + _(ITERL, base, ___, jump, ___) \ + _(IITERL, base, ___, jump, ___) \ + _(JITERL, base, ___, lit, ___) \ + \ + _(LOOP, rbase, ___, jump, ___) \ + _(ILOOP, rbase, ___, jump, ___) \ + _(JLOOP, rbase, ___, lit, ___) \ + \ + _(JMP, rbase, ___, jump, ___) \ + \ + /* Function headers. I/J = interp/JIT, F/V/C = fixarg/vararg/C func. */ \ + _(FUNCF, rbase, ___, ___, ___) \ + _(IFUNCF, rbase, ___, ___, ___) \ + _(JFUNCF, rbase, ___, lit, ___) \ + _(FUNCV, rbase, ___, ___, ___) \ + _(IFUNCV, rbase, ___, ___, ___) \ + _(JFUNCV, rbase, ___, lit, ___) \ + _(FUNCC, rbase, ___, ___, ___) \ + _(FUNCCW, rbase, ___, ___, ___) + +/* Bytecode opcode numbers. */ +typedef enum { +#define BCENUM(name, ma, mb, mc, mt) BC_##name, +BCDEF(BCENUM) +#undef BCENUM + BC__MAX +} BCOp; + +LJ_STATIC_ASSERT((int)BC_ISEQV+1 == (int)BC_ISNEV); +LJ_STATIC_ASSERT(((int)BC_ISEQV^1) == (int)BC_ISNEV); +LJ_STATIC_ASSERT(((int)BC_ISEQS^1) == (int)BC_ISNES); +LJ_STATIC_ASSERT(((int)BC_ISEQN^1) == (int)BC_ISNEN); +LJ_STATIC_ASSERT(((int)BC_ISEQP^1) == (int)BC_ISNEP); +LJ_STATIC_ASSERT(((int)BC_ISLT^1) == (int)BC_ISGE); +LJ_STATIC_ASSERT(((int)BC_ISLE^1) == (int)BC_ISGT); +LJ_STATIC_ASSERT(((int)BC_ISLT^3) == (int)BC_ISGT); +LJ_STATIC_ASSERT((int)BC_IST-(int)BC_ISTC == (int)BC_ISF-(int)BC_ISFC); +LJ_STATIC_ASSERT((int)BC_CALLT-(int)BC_CALL == (int)BC_CALLMT-(int)BC_CALLM); +LJ_STATIC_ASSERT((int)BC_CALLMT + 1 == (int)BC_CALLT); +LJ_STATIC_ASSERT((int)BC_RETM + 1 == (int)BC_RET); +LJ_STATIC_ASSERT((int)BC_FORL + 1 == (int)BC_IFORL); +LJ_STATIC_ASSERT((int)BC_FORL + 2 == (int)BC_JFORL); +LJ_STATIC_ASSERT((int)BC_ITERL + 1 == (int)BC_IITERL); +LJ_STATIC_ASSERT((int)BC_ITERL + 2 == (int)BC_JITERL); +LJ_STATIC_ASSERT((int)BC_LOOP + 1 == (int)BC_ILOOP); +LJ_STATIC_ASSERT((int)BC_LOOP + 2 == (int)BC_JLOOP); +LJ_STATIC_ASSERT((int)BC_FUNCF + 1 == (int)BC_IFUNCF); +LJ_STATIC_ASSERT((int)BC_FUNCF + 2 == (int)BC_JFUNCF); +LJ_STATIC_ASSERT((int)BC_FUNCV + 1 == (int)BC_IFUNCV); +LJ_STATIC_ASSERT((int)BC_FUNCV + 2 == (int)BC_JFUNCV); + +/* This solves a circular dependency problem, change as needed. */ +#define FF_next_N 4 + +/* Stack slots used by FORI/FORL, relative to operand A. */ +enum { + FORL_IDX, FORL_STOP, FORL_STEP, FORL_EXT +}; + +/* Bytecode operand modes. ORDER BCMode */ +typedef enum { + BCMnone, BCMdst, BCMbase, BCMvar, BCMrbase, BCMuv, /* Mode A must be <= 7 */ + BCMlit, BCMlits, BCMpri, BCMnum, BCMstr, BCMtab, BCMfunc, BCMjump, BCMcdata, + BCM_max +} BCMode; +#define BCM___ BCMnone + +#define bcmode_a(op) ((BCMode)(lj_bc_mode[op] & 7)) +#define bcmode_b(op) ((BCMode)((lj_bc_mode[op]>>3) & 15)) +#define bcmode_c(op) ((BCMode)((lj_bc_mode[op]>>7) & 15)) +#define bcmode_d(op) bcmode_c(op) +#define bcmode_hasd(op) ((lj_bc_mode[op] & (15<<3)) == (BCMnone<<3)) +#define bcmode_mm(op) ((MMS)(lj_bc_mode[op]>>11)) + +#define BCMODE(name, ma, mb, mc, mm) \ + (BCM##ma|(BCM##mb<<3)|(BCM##mc<<7)|(MM_##mm<<11)), +#define BCMODE_FF 0 + +static LJ_AINLINE int bc_isret(BCOp op) +{ + return (op == BC_RETM || op == BC_RET || op == BC_RET0 || op == BC_RET1); +} + +LJ_DATA const uint16_t lj_bc_mode[]; +LJ_DATA const uint16_t lj_bc_ofs[]; + +#endif diff --git a/external/lua/luajit/src/src/lj_bcdump.h b/external/lua/luajit/src/src/lj_bcdump.h new file mode 100644 index 0000000000..e660156d68 --- /dev/null +++ b/external/lua/luajit/src/src/lj_bcdump.h @@ -0,0 +1,66 @@ +/* +** Bytecode dump definitions. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_BCDUMP_H +#define _LJ_BCDUMP_H + +#include "lj_obj.h" +#include "lj_lex.h" + +/* -- Bytecode dump format ------------------------------------------------ */ + +/* +** dump = header proto+ 0U +** header = ESC 'L' 'J' versionB flagsU [namelenU nameB*] +** proto = lengthU pdata +** pdata = phead bcinsW* uvdataH* kgc* knum* [debugB*] +** phead = flagsB numparamsB framesizeB numuvB numkgcU numknU numbcU +** [debuglenU [firstlineU numlineU]] +** kgc = kgctypeU { ktab | (loU hiU) | (rloU rhiU iloU ihiU) | strB* } +** knum = intU0 | (loU1 hiU) +** ktab = narrayU nhashU karray* khash* +** karray = ktabk +** khash = ktabk ktabk +** ktabk = ktabtypeU { intU | (loU hiU) | strB* } +** +** B = 8 bit, H = 16 bit, W = 32 bit, U = ULEB128 of W, U0/U1 = ULEB128 of W+1 +*/ + +/* Bytecode dump header. */ +#define BCDUMP_HEAD1 0x1b +#define BCDUMP_HEAD2 0x4c +#define BCDUMP_HEAD3 0x4a + +/* If you perform *any* kind of private modifications to the bytecode itself +** or to the dump format, you *must* set BCDUMP_VERSION to 0x80 or higher. +*/ +#define BCDUMP_VERSION 1 + +/* Compatibility flags. */ +#define BCDUMP_F_BE 0x01 +#define BCDUMP_F_STRIP 0x02 +#define BCDUMP_F_FFI 0x04 + +#define BCDUMP_F_KNOWN (BCDUMP_F_FFI*2-1) + +/* Type codes for the GC constants of a prototype. Plus length for strings. */ +enum { + BCDUMP_KGC_CHILD, BCDUMP_KGC_TAB, BCDUMP_KGC_I64, BCDUMP_KGC_U64, + BCDUMP_KGC_COMPLEX, BCDUMP_KGC_STR +}; + +/* Type codes for the keys/values of a constant table. */ +enum { + BCDUMP_KTAB_NIL, BCDUMP_KTAB_FALSE, BCDUMP_KTAB_TRUE, + BCDUMP_KTAB_INT, BCDUMP_KTAB_NUM, BCDUMP_KTAB_STR +}; + +/* -- Bytecode reader/writer ---------------------------------------------- */ + +LJ_FUNC int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, + void *data, int strip); +LJ_FUNC GCproto *lj_bcread(LexState *ls); + +#endif diff --git a/external/lua/luajit/src/src/lj_bcread.c b/external/lua/luajit/src/src/lj_bcread.c new file mode 100644 index 0000000000..2b5ba85533 --- /dev/null +++ b/external/lua/luajit/src/src/lj_bcread.c @@ -0,0 +1,476 @@ +/* +** Bytecode reader. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_bcread_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_bc.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#include "lj_cdata.h" +#include "lualib.h" +#endif +#include "lj_lex.h" +#include "lj_bcdump.h" +#include "lj_state.h" + +/* Reuse some lexer fields for our own purposes. */ +#define bcread_flags(ls) ls->level +#define bcread_swap(ls) \ + ((bcread_flags(ls) & BCDUMP_F_BE) != LJ_BE*BCDUMP_F_BE) +#define bcread_oldtop(L, ls) restorestack(L, ls->lastline) +#define bcread_savetop(L, ls, top) \ + ls->lastline = (BCLine)savestack(L, (top)) + +/* -- Input buffer handling ----------------------------------------------- */ + +/* Throw reader error. */ +static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em) +{ + lua_State *L = ls->L; + const char *name = ls->chunkarg; + if (*name == BCDUMP_HEAD1) name = "(binary)"; + else if (*name == '@' || *name == '=') name++; + lj_str_pushf(L, "%s: %s", name, err2msg(em)); + lj_err_throw(L, LUA_ERRSYNTAX); +} + +/* Resize input buffer. */ +static void bcread_resize(LexState *ls, MSize len) +{ + if (ls->sb.sz < len) { + MSize sz = ls->sb.sz * 2; + while (len > sz) sz = sz * 2; + lj_str_resizebuf(ls->L, &ls->sb, sz); + /* Caveat: this may change ls->sb.buf which may affect ls->p. */ + } +} + +/* Refill buffer if needed. */ +static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need) +{ + lua_assert(len != 0); + if (len > LJ_MAX_MEM || ls->current < 0) + bcread_error(ls, LJ_ERR_BCBAD); + do { + const char *buf; + size_t size; + if (ls->n) { /* Copy remainder to buffer. */ + if (ls->sb.n) { /* Move down in buffer. */ + lua_assert(ls->p + ls->n == ls->sb.buf + ls->sb.n); + if (ls->n != ls->sb.n) + memmove(ls->sb.buf, ls->p, ls->n); + } else { /* Copy from buffer provided by reader. */ + bcread_resize(ls, len); + memcpy(ls->sb.buf, ls->p, ls->n); + } + ls->p = ls->sb.buf; + } + ls->sb.n = ls->n; + buf = ls->rfunc(ls->L, ls->rdata, &size); /* Get more data from reader. */ + if (buf == NULL || size == 0) { /* EOF? */ + if (need) bcread_error(ls, LJ_ERR_BCBAD); + ls->current = -1; /* Only bad if we get called again. */ + break; + } + if (ls->sb.n) { /* Append to buffer. */ + MSize n = ls->sb.n + (MSize)size; + bcread_resize(ls, n < len ? len : n); + memcpy(ls->sb.buf + ls->sb.n, buf, size); + ls->n = ls->sb.n = n; + ls->p = ls->sb.buf; + } else { /* Return buffer provided by reader. */ + ls->n = (MSize)size; + ls->p = buf; + } + } while (ls->n < len); +} + +/* Need a certain number of bytes. */ +static LJ_AINLINE void bcread_need(LexState *ls, MSize len) +{ + if (LJ_UNLIKELY(ls->n < len)) + bcread_fill(ls, len, 1); +} + +/* Want to read up to a certain number of bytes, but may need less. */ +static LJ_AINLINE void bcread_want(LexState *ls, MSize len) +{ + if (LJ_UNLIKELY(ls->n < len)) + bcread_fill(ls, len, 0); +} + +#define bcread_dec(ls) check_exp(ls->n > 0, ls->n--) +#define bcread_consume(ls, len) check_exp(ls->n >= (len), ls->n -= (len)) + +/* Return memory block from buffer. */ +static uint8_t *bcread_mem(LexState *ls, MSize len) +{ + uint8_t *p = (uint8_t *)ls->p; + bcread_consume(ls, len); + ls->p = (char *)p + len; + return p; +} + +/* Copy memory block from buffer. */ +static void bcread_block(LexState *ls, void *q, MSize len) +{ + memcpy(q, bcread_mem(ls, len), len); +} + +/* Read byte from buffer. */ +static LJ_AINLINE uint32_t bcread_byte(LexState *ls) +{ + bcread_dec(ls); + return (uint32_t)(uint8_t)*ls->p++; +} + +/* Read ULEB128 value from buffer. */ +static uint32_t bcread_uleb128(LexState *ls) +{ + const uint8_t *p = (const uint8_t *)ls->p; + uint32_t v = *p++; + if (LJ_UNLIKELY(v >= 0x80)) { + int sh = 0; + v &= 0x7f; + do { + v |= ((*p & 0x7f) << (sh += 7)); + bcread_dec(ls); + } while (*p++ >= 0x80); + } + bcread_dec(ls); + ls->p = (char *)p; + return v; +} + +/* Read top 32 bits of 33 bit ULEB128 value from buffer. */ +static uint32_t bcread_uleb128_33(LexState *ls) +{ + const uint8_t *p = (const uint8_t *)ls->p; + uint32_t v = (*p++ >> 1); + if (LJ_UNLIKELY(v >= 0x40)) { + int sh = -1; + v &= 0x3f; + do { + v |= ((*p & 0x7f) << (sh += 7)); + bcread_dec(ls); + } while (*p++ >= 0x80); + } + bcread_dec(ls); + ls->p = (char *)p; + return v; +} + +/* -- Bytecode reader ----------------------------------------------------- */ + +/* Read debug info of a prototype. */ +static void bcread_dbg(LexState *ls, GCproto *pt, MSize sizedbg) +{ + void *lineinfo = (void *)proto_lineinfo(pt); + bcread_block(ls, lineinfo, sizedbg); + /* Swap lineinfo if the endianess differs. */ + if (bcread_swap(ls) && pt->numline >= 256) { + MSize i, n = pt->sizebc-1; + if (pt->numline < 65536) { + uint16_t *p = (uint16_t *)lineinfo; + for (i = 0; i < n; i++) p[i] = (uint16_t)((p[i] >> 8)|(p[i] << 8)); + } else { + uint32_t *p = (uint32_t *)lineinfo; + for (i = 0; i < n; i++) p[i] = lj_bswap(p[i]); + } + } +} + +/* Find pointer to varinfo. */ +static const void *bcread_varinfo(GCproto *pt) +{ + const uint8_t *p = proto_uvinfo(pt); + MSize n = pt->sizeuv; + if (n) while (*p++ || --n) ; + return p; +} + +/* Read a single constant key/value of a template table. */ +static void bcread_ktabk(LexState *ls, TValue *o) +{ + MSize tp = bcread_uleb128(ls); + if (tp >= BCDUMP_KTAB_STR) { + MSize len = tp - BCDUMP_KTAB_STR; + const char *p = (const char *)bcread_mem(ls, len); + setstrV(ls->L, o, lj_str_new(ls->L, p, len)); + } else if (tp == BCDUMP_KTAB_INT) { + setintV(o, (int32_t)bcread_uleb128(ls)); + } else if (tp == BCDUMP_KTAB_NUM) { + o->u32.lo = bcread_uleb128(ls); + o->u32.hi = bcread_uleb128(ls); + } else { + lua_assert(tp <= BCDUMP_KTAB_TRUE); + setitype(o, ~tp); + } +} + +/* Read a template table. */ +static GCtab *bcread_ktab(LexState *ls) +{ + MSize narray = bcread_uleb128(ls); + MSize nhash = bcread_uleb128(ls); + GCtab *t = lj_tab_new(ls->L, narray, hsize2hbits(nhash)); + if (narray) { /* Read array entries. */ + MSize i; + TValue *o = tvref(t->array); + for (i = 0; i < narray; i++, o++) + bcread_ktabk(ls, o); + } + if (nhash) { /* Read hash entries. */ + MSize i; + for (i = 0; i < nhash; i++) { + TValue key; + bcread_ktabk(ls, &key); + lua_assert(!tvisnil(&key)); + bcread_ktabk(ls, lj_tab_set(ls->L, t, &key)); + } + } + return t; +} + +/* Read GC constants of a prototype. */ +static void bcread_kgc(LexState *ls, GCproto *pt, MSize sizekgc) +{ + MSize i; + GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc; + for (i = 0; i < sizekgc; i++, kr++) { + MSize tp = bcread_uleb128(ls); + if (tp >= BCDUMP_KGC_STR) { + MSize len = tp - BCDUMP_KGC_STR; + const char *p = (const char *)bcread_mem(ls, len); + setgcref(*kr, obj2gco(lj_str_new(ls->L, p, len))); + } else if (tp == BCDUMP_KGC_TAB) { + setgcref(*kr, obj2gco(bcread_ktab(ls))); +#if LJ_HASFFI + } else if (tp != BCDUMP_KGC_CHILD) { + CTypeID id = tp == BCDUMP_KGC_COMPLEX ? CTID_COMPLEX_DOUBLE : + tp == BCDUMP_KGC_I64 ? CTID_INT64 : CTID_UINT64; + CTSize sz = tp == BCDUMP_KGC_COMPLEX ? 16 : 8; + GCcdata *cd = lj_cdata_new_(ls->L, id, sz); + TValue *p = (TValue *)cdataptr(cd); + setgcref(*kr, obj2gco(cd)); + p[0].u32.lo = bcread_uleb128(ls); + p[0].u32.hi = bcread_uleb128(ls); + if (tp == BCDUMP_KGC_COMPLEX) { + p[1].u32.lo = bcread_uleb128(ls); + p[1].u32.hi = bcread_uleb128(ls); + } +#endif + } else { + lua_State *L = ls->L; + lua_assert(tp == BCDUMP_KGC_CHILD); + if (L->top <= bcread_oldtop(L, ls)) /* Stack underflow? */ + bcread_error(ls, LJ_ERR_BCBAD); + L->top--; + setgcref(*kr, obj2gco(protoV(L->top))); + } + } +} + +/* Read number constants of a prototype. */ +static void bcread_knum(LexState *ls, GCproto *pt, MSize sizekn) +{ + MSize i; + TValue *o = mref(pt->k, TValue); + for (i = 0; i < sizekn; i++, o++) { + int isnum = (ls->p[0] & 1); + uint32_t lo = bcread_uleb128_33(ls); + if (isnum) { + o->u32.lo = lo; + o->u32.hi = bcread_uleb128(ls); + } else { + setintV(o, lo); + } + } +} + +/* Read bytecode instructions. */ +static void bcread_bytecode(LexState *ls, GCproto *pt, MSize sizebc) +{ + BCIns *bc = proto_bc(pt); + bc[0] = BCINS_AD((pt->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF, + pt->framesize, 0); + bcread_block(ls, bc+1, (sizebc-1)*(MSize)sizeof(BCIns)); + /* Swap bytecode instructions if the endianess differs. */ + if (bcread_swap(ls)) { + MSize i; + for (i = 1; i < sizebc; i++) bc[i] = lj_bswap(bc[i]); + } +} + +/* Read upvalue refs. */ +static void bcread_uv(LexState *ls, GCproto *pt, MSize sizeuv) +{ + if (sizeuv) { + uint16_t *uv = proto_uv(pt); + bcread_block(ls, uv, sizeuv*2); + /* Swap upvalue refs if the endianess differs. */ + if (bcread_swap(ls)) { + MSize i; + for (i = 0; i < sizeuv; i++) + uv[i] = (uint16_t)((uv[i] >> 8)|(uv[i] << 8)); + } + } +} + +/* Read a prototype. */ +static GCproto *bcread_proto(LexState *ls) +{ + GCproto *pt; + MSize framesize, numparams, flags, sizeuv, sizekgc, sizekn, sizebc, sizept; + MSize ofsk, ofsuv, ofsdbg; + MSize sizedbg = 0; + BCLine firstline = 0, numline = 0; + MSize len, startn; + + /* Read length. */ + if (ls->n > 0 && ls->p[0] == 0) { /* Shortcut EOF. */ + ls->n--; ls->p++; + return NULL; + } + bcread_want(ls, 5); + len = bcread_uleb128(ls); + if (!len) return NULL; /* EOF */ + bcread_need(ls, len); + startn = ls->n; + + /* Read prototype header. */ + flags = bcread_byte(ls); + numparams = bcread_byte(ls); + framesize = bcread_byte(ls); + sizeuv = bcread_byte(ls); + sizekgc = bcread_uleb128(ls); + sizekn = bcread_uleb128(ls); + sizebc = bcread_uleb128(ls) + 1; + if (!(bcread_flags(ls) & BCDUMP_F_STRIP)) { + sizedbg = bcread_uleb128(ls); + if (sizedbg) { + firstline = bcread_uleb128(ls); + numline = bcread_uleb128(ls); + } + } + + /* Calculate total size of prototype including all colocated arrays. */ + sizept = (MSize)sizeof(GCproto) + + sizebc*(MSize)sizeof(BCIns) + + sizekgc*(MSize)sizeof(GCRef); + sizept = (sizept + (MSize)sizeof(TValue)-1) & ~((MSize)sizeof(TValue)-1); + ofsk = sizept; sizept += sizekn*(MSize)sizeof(TValue); + ofsuv = sizept; sizept += ((sizeuv+1)&~1)*2; + ofsdbg = sizept; sizept += sizedbg; + + /* Allocate prototype object and initialize its fields. */ + pt = (GCproto *)lj_mem_newgco(ls->L, (MSize)sizept); + pt->gct = ~LJ_TPROTO; + pt->numparams = (uint8_t)numparams; + pt->framesize = (uint8_t)framesize; + pt->sizebc = sizebc; + setmref(pt->k, (char *)pt + ofsk); + setmref(pt->uv, (char *)pt + ofsuv); + pt->sizekgc = 0; /* Set to zero until fully initialized. */ + pt->sizekn = sizekn; + pt->sizept = sizept; + pt->sizeuv = (uint8_t)sizeuv; + pt->flags = (uint8_t)flags; + pt->trace = 0; + setgcref(pt->chunkname, obj2gco(ls->chunkname)); + + /* Close potentially uninitialized gap between bc and kgc. */ + *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(sizekgc+1)) = 0; + + /* Read bytecode instructions and upvalue refs. */ + bcread_bytecode(ls, pt, sizebc); + bcread_uv(ls, pt, sizeuv); + + /* Read constants. */ + bcread_kgc(ls, pt, sizekgc); + pt->sizekgc = sizekgc; + bcread_knum(ls, pt, sizekn); + + /* Read and initialize debug info. */ + pt->firstline = firstline; + pt->numline = numline; + if (sizedbg) { + MSize sizeli = (sizebc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2); + setmref(pt->lineinfo, (char *)pt + ofsdbg); + setmref(pt->uvinfo, (char *)pt + ofsdbg + sizeli); + bcread_dbg(ls, pt, sizedbg); + setmref(pt->varinfo, bcread_varinfo(pt)); + } else { + setmref(pt->lineinfo, NULL); + setmref(pt->uvinfo, NULL); + setmref(pt->varinfo, NULL); + } + + if (len != startn - ls->n) + bcread_error(ls, LJ_ERR_BCBAD); + return pt; +} + +/* Read and check header of bytecode dump. */ +static int bcread_header(LexState *ls) +{ + uint32_t flags; + bcread_want(ls, 3+5+5); + if (bcread_byte(ls) != BCDUMP_HEAD2 || + bcread_byte(ls) != BCDUMP_HEAD3 || + bcread_byte(ls) != BCDUMP_VERSION) return 0; + bcread_flags(ls) = flags = bcread_uleb128(ls); + if ((flags & ~(BCDUMP_F_KNOWN)) != 0) return 0; + if ((flags & BCDUMP_F_FFI)) { +#if LJ_HASFFI + lua_State *L = ls->L; + if (!ctype_ctsG(G(L))) { + ptrdiff_t oldtop = savestack(L, L->top); + luaopen_ffi(L); /* Load FFI library on-demand. */ + L->top = restorestack(L, oldtop); + } +#else + return 0; +#endif + } + if ((flags & BCDUMP_F_STRIP)) { + ls->chunkname = lj_str_newz(ls->L, ls->chunkarg); + } else { + MSize len = bcread_uleb128(ls); + bcread_need(ls, len); + ls->chunkname = lj_str_new(ls->L, (const char *)bcread_mem(ls, len), len); + } + return 1; /* Ok. */ +} + +/* Read a bytecode dump. */ +GCproto *lj_bcread(LexState *ls) +{ + lua_State *L = ls->L; + lua_assert(ls->current == BCDUMP_HEAD1); + bcread_savetop(L, ls, L->top); + lj_str_resetbuf(&ls->sb); + /* Check for a valid bytecode dump header. */ + if (!bcread_header(ls)) + bcread_error(ls, LJ_ERR_BCFMT); + for (;;) { /* Process all prototypes in the bytecode dump. */ + GCproto *pt = bcread_proto(ls); + if (!pt) break; + setprotoV(L, L->top, pt); + incr_top(L); + } + if ((int32_t)ls->n > 0 || L->top-1 != bcread_oldtop(L, ls)) + bcread_error(ls, LJ_ERR_BCBAD); + /* Pop off last prototype. */ + L->top--; + return protoV(L->top); +} + diff --git a/external/lua/luajit/src/src/lj_bcwrite.c b/external/lua/luajit/src/src/lj_bcwrite.c new file mode 100644 index 0000000000..4805d515f3 --- /dev/null +++ b/external/lua/luajit/src/src/lj_bcwrite.c @@ -0,0 +1,396 @@ +/* +** Bytecode writer. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_bcwrite_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_str.h" +#include "lj_bc.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#endif +#if LJ_HASJIT +#include "lj_dispatch.h" +#include "lj_jit.h" +#endif +#include "lj_bcdump.h" +#include "lj_vm.h" + +/* Context for bytecode writer. */ +typedef struct BCWriteCtx { + SBuf sb; /* Output buffer. */ + lua_State *L; /* Lua state. */ + GCproto *pt; /* Root prototype. */ + lua_Writer wfunc; /* Writer callback. */ + void *wdata; /* Writer callback data. */ + int strip; /* Strip debug info. */ + int status; /* Status from writer callback. */ +} BCWriteCtx; + +/* -- Output buffer handling ---------------------------------------------- */ + +/* Resize buffer if needed. */ +static LJ_NOINLINE void bcwrite_resize(BCWriteCtx *ctx, MSize len) +{ + MSize sz = ctx->sb.sz * 2; + while (ctx->sb.n + len > sz) sz = sz * 2; + lj_str_resizebuf(ctx->L, &ctx->sb, sz); +} + +/* Need a certain amount of buffer space. */ +static LJ_AINLINE void bcwrite_need(BCWriteCtx *ctx, MSize len) +{ + if (LJ_UNLIKELY(ctx->sb.n + len > ctx->sb.sz)) + bcwrite_resize(ctx, len); +} + +/* Add memory block to buffer. */ +static void bcwrite_block(BCWriteCtx *ctx, const void *p, MSize len) +{ + uint8_t *q = (uint8_t *)(ctx->sb.buf + ctx->sb.n); + MSize i; + ctx->sb.n += len; + for (i = 0; i < len; i++) q[i] = ((uint8_t *)p)[i]; +} + +/* Add byte to buffer. */ +static LJ_AINLINE void bcwrite_byte(BCWriteCtx *ctx, uint8_t b) +{ + ctx->sb.buf[ctx->sb.n++] = b; +} + +/* Add ULEB128 value to buffer. */ +static void bcwrite_uleb128(BCWriteCtx *ctx, uint32_t v) +{ + MSize n = ctx->sb.n; + uint8_t *p = (uint8_t *)ctx->sb.buf; + for (; v >= 0x80; v >>= 7) + p[n++] = (uint8_t)((v & 0x7f) | 0x80); + p[n++] = (uint8_t)v; + ctx->sb.n = n; +} + +/* -- Bytecode writer ----------------------------------------------------- */ + +/* Write a single constant key/value of a template table. */ +static void bcwrite_ktabk(BCWriteCtx *ctx, cTValue *o, int narrow) +{ + bcwrite_need(ctx, 1+10); + if (tvisstr(o)) { + const GCstr *str = strV(o); + MSize len = str->len; + bcwrite_need(ctx, 5+len); + bcwrite_uleb128(ctx, BCDUMP_KTAB_STR+len); + bcwrite_block(ctx, strdata(str), len); + } else if (tvisint(o)) { + bcwrite_byte(ctx, BCDUMP_KTAB_INT); + bcwrite_uleb128(ctx, intV(o)); + } else if (tvisnum(o)) { + if (!LJ_DUALNUM && narrow) { /* Narrow number constants to integers. */ + lua_Number num = numV(o); + int32_t k = lj_num2int(num); + if (num == (lua_Number)k) { /* -0 is never a constant. */ + bcwrite_byte(ctx, BCDUMP_KTAB_INT); + bcwrite_uleb128(ctx, k); + return; + } + } + bcwrite_byte(ctx, BCDUMP_KTAB_NUM); + bcwrite_uleb128(ctx, o->u32.lo); + bcwrite_uleb128(ctx, o->u32.hi); + } else { + lua_assert(tvispri(o)); + bcwrite_byte(ctx, BCDUMP_KTAB_NIL+~itype(o)); + } +} + +/* Write a template table. */ +static void bcwrite_ktab(BCWriteCtx *ctx, const GCtab *t) +{ + MSize narray = 0, nhash = 0; + if (t->asize > 0) { /* Determine max. length of array part. */ + ptrdiff_t i; + TValue *array = tvref(t->array); + for (i = (ptrdiff_t)t->asize-1; i >= 0; i--) + if (!tvisnil(&array[i])) + break; + narray = (MSize)(i+1); + } + if (t->hmask > 0) { /* Count number of used hash slots. */ + MSize i, hmask = t->hmask; + Node *node = noderef(t->node); + for (i = 0; i <= hmask; i++) + nhash += !tvisnil(&node[i].val); + } + /* Write number of array slots and hash slots. */ + bcwrite_uleb128(ctx, narray); + bcwrite_uleb128(ctx, nhash); + if (narray) { /* Write array entries (may contain nil). */ + MSize i; + TValue *o = tvref(t->array); + for (i = 0; i < narray; i++, o++) + bcwrite_ktabk(ctx, o, 1); + } + if (nhash) { /* Write hash entries. */ + MSize i = nhash; + Node *node = noderef(t->node) + t->hmask; + for (;; node--) + if (!tvisnil(&node->val)) { + bcwrite_ktabk(ctx, &node->key, 0); + bcwrite_ktabk(ctx, &node->val, 1); + if (--i == 0) break; + } + } +} + +/* Write GC constants of a prototype. */ +static void bcwrite_kgc(BCWriteCtx *ctx, GCproto *pt) +{ + MSize i, sizekgc = pt->sizekgc; + GCRef *kr = mref(pt->k, GCRef) - (ptrdiff_t)sizekgc; + for (i = 0; i < sizekgc; i++, kr++) { + GCobj *o = gcref(*kr); + MSize tp, need = 1; + /* Determine constant type and needed size. */ + if (o->gch.gct == ~LJ_TSTR) { + tp = BCDUMP_KGC_STR + gco2str(o)->len; + need = 5+gco2str(o)->len; + } else if (o->gch.gct == ~LJ_TPROTO) { + lua_assert((pt->flags & PROTO_CHILD)); + tp = BCDUMP_KGC_CHILD; +#if LJ_HASFFI + } else if (o->gch.gct == ~LJ_TCDATA) { + CTypeID id = gco2cd(o)->ctypeid; + need = 1+4*5; + if (id == CTID_INT64) { + tp = BCDUMP_KGC_I64; + } else if (id == CTID_UINT64) { + tp = BCDUMP_KGC_U64; + } else { + lua_assert(id == CTID_COMPLEX_DOUBLE); + tp = BCDUMP_KGC_COMPLEX; + } +#endif + } else { + lua_assert(o->gch.gct == ~LJ_TTAB); + tp = BCDUMP_KGC_TAB; + need = 1+2*5; + } + /* Write constant type. */ + bcwrite_need(ctx, need); + bcwrite_uleb128(ctx, tp); + /* Write constant data (if any). */ + if (tp >= BCDUMP_KGC_STR) { + bcwrite_block(ctx, strdata(gco2str(o)), gco2str(o)->len); + } else if (tp == BCDUMP_KGC_TAB) { + bcwrite_ktab(ctx, gco2tab(o)); +#if LJ_HASFFI + } else if (tp != BCDUMP_KGC_CHILD) { + cTValue *p = (TValue *)cdataptr(gco2cd(o)); + bcwrite_uleb128(ctx, p[0].u32.lo); + bcwrite_uleb128(ctx, p[0].u32.hi); + if (tp == BCDUMP_KGC_COMPLEX) { + bcwrite_uleb128(ctx, p[1].u32.lo); + bcwrite_uleb128(ctx, p[1].u32.hi); + } +#endif + } + } +} + +/* Write number constants of a prototype. */ +static void bcwrite_knum(BCWriteCtx *ctx, GCproto *pt) +{ + MSize i, sizekn = pt->sizekn; + cTValue *o = mref(pt->k, TValue); + bcwrite_need(ctx, 10*sizekn); + for (i = 0; i < sizekn; i++, o++) { + int32_t k; + if (tvisint(o)) { + k = intV(o); + goto save_int; + } else { + /* Write a 33 bit ULEB128 for the int (lsb=0) or loword (lsb=1). */ + if (!LJ_DUALNUM) { /* Narrow number constants to integers. */ + lua_Number num = numV(o); + k = lj_num2int(num); + if (num == (lua_Number)k) { /* -0 is never a constant. */ + save_int: + bcwrite_uleb128(ctx, 2*(uint32_t)k | ((uint32_t)k & 0x80000000u)); + if (k < 0) { + char *p = &ctx->sb.buf[ctx->sb.n-1]; + *p = (*p & 7) | ((k>>27) & 0x18); + } + continue; + } + } + bcwrite_uleb128(ctx, 1+(2*o->u32.lo | (o->u32.lo & 0x80000000u))); + if (o->u32.lo >= 0x80000000u) { + char *p = &ctx->sb.buf[ctx->sb.n-1]; + *p = (*p & 7) | ((o->u32.lo>>27) & 0x18); + } + bcwrite_uleb128(ctx, o->u32.hi); + } + } +} + +/* Write bytecode instructions. */ +static void bcwrite_bytecode(BCWriteCtx *ctx, GCproto *pt) +{ + MSize nbc = pt->sizebc-1; /* Omit the [JI]FUNC* header. */ +#if LJ_HASJIT + uint8_t *p = (uint8_t *)&ctx->sb.buf[ctx->sb.n]; +#endif + bcwrite_block(ctx, proto_bc(pt)+1, nbc*(MSize)sizeof(BCIns)); +#if LJ_HASJIT + /* Unpatch modified bytecode containing ILOOP/JLOOP etc. */ + if ((pt->flags & PROTO_ILOOP) || pt->trace) { + jit_State *J = L2J(ctx->L); + MSize i; + for (i = 0; i < nbc; i++, p += sizeof(BCIns)) { + BCOp op = (BCOp)p[LJ_ENDIAN_SELECT(0, 3)]; + if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP || + op == BC_JFORI) { + p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_IFORL+BC_FORL); + } else if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) { + BCReg rd = p[LJ_ENDIAN_SELECT(2, 1)] + (p[LJ_ENDIAN_SELECT(3, 0)] << 8); + BCIns ins = traceref(J, rd)->startins; + p[LJ_ENDIAN_SELECT(0, 3)] = (uint8_t)(op-BC_JFORL+BC_FORL); + p[LJ_ENDIAN_SELECT(2, 1)] = bc_c(ins); + p[LJ_ENDIAN_SELECT(3, 0)] = bc_b(ins); + } + } + } +#endif +} + +/* Write prototype. */ +static void bcwrite_proto(BCWriteCtx *ctx, GCproto *pt) +{ + MSize sizedbg = 0; + + /* Recursively write children of prototype. */ + if ((pt->flags & PROTO_CHILD)) { + ptrdiff_t i, n = pt->sizekgc; + GCRef *kr = mref(pt->k, GCRef) - 1; + for (i = 0; i < n; i++, kr--) { + GCobj *o = gcref(*kr); + if (o->gch.gct == ~LJ_TPROTO) + bcwrite_proto(ctx, gco2pt(o)); + } + } + + /* Start writing the prototype info to a buffer. */ + lj_str_resetbuf(&ctx->sb); + ctx->sb.n = 5; /* Leave room for final size. */ + bcwrite_need(ctx, 4+6*5+(pt->sizebc-1)*(MSize)sizeof(BCIns)+pt->sizeuv*2); + + /* Write prototype header. */ + bcwrite_byte(ctx, (pt->flags & (PROTO_CHILD|PROTO_VARARG|PROTO_FFI))); + bcwrite_byte(ctx, pt->numparams); + bcwrite_byte(ctx, pt->framesize); + bcwrite_byte(ctx, pt->sizeuv); + bcwrite_uleb128(ctx, pt->sizekgc); + bcwrite_uleb128(ctx, pt->sizekn); + bcwrite_uleb128(ctx, pt->sizebc-1); + if (!ctx->strip) { + if (proto_lineinfo(pt)) + sizedbg = pt->sizept - (MSize)((char *)proto_lineinfo(pt) - (char *)pt); + bcwrite_uleb128(ctx, sizedbg); + if (sizedbg) { + bcwrite_uleb128(ctx, pt->firstline); + bcwrite_uleb128(ctx, pt->numline); + } + } + + /* Write bytecode instructions and upvalue refs. */ + bcwrite_bytecode(ctx, pt); + bcwrite_block(ctx, proto_uv(pt), pt->sizeuv*2); + + /* Write constants. */ + bcwrite_kgc(ctx, pt); + bcwrite_knum(ctx, pt); + + /* Write debug info, if not stripped. */ + if (sizedbg) { + bcwrite_need(ctx, sizedbg); + bcwrite_block(ctx, proto_lineinfo(pt), sizedbg); + } + + /* Pass buffer to writer function. */ + if (ctx->status == 0) { + MSize n = ctx->sb.n - 5; + MSize nn = (lj_fls(n)+8)*9 >> 6; + ctx->sb.n = 5 - nn; + bcwrite_uleb128(ctx, n); /* Fill in final size. */ + lua_assert(ctx->sb.n == 5); + ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf+5-nn, nn+n, ctx->wdata); + } +} + +/* Write header of bytecode dump. */ +static void bcwrite_header(BCWriteCtx *ctx) +{ + GCstr *chunkname = proto_chunkname(ctx->pt); + const char *name = strdata(chunkname); + MSize len = chunkname->len; + lj_str_resetbuf(&ctx->sb); + bcwrite_need(ctx, 5+5+len); + bcwrite_byte(ctx, BCDUMP_HEAD1); + bcwrite_byte(ctx, BCDUMP_HEAD2); + bcwrite_byte(ctx, BCDUMP_HEAD3); + bcwrite_byte(ctx, BCDUMP_VERSION); + bcwrite_byte(ctx, (ctx->strip ? BCDUMP_F_STRIP : 0) + + (LJ_BE ? BCDUMP_F_BE : 0) + + ((ctx->pt->flags & PROTO_FFI) ? BCDUMP_F_FFI : 0)); + if (!ctx->strip) { + bcwrite_uleb128(ctx, len); + bcwrite_block(ctx, name, len); + } + ctx->status = ctx->wfunc(ctx->L, ctx->sb.buf, ctx->sb.n, ctx->wdata); +} + +/* Write footer of bytecode dump. */ +static void bcwrite_footer(BCWriteCtx *ctx) +{ + if (ctx->status == 0) { + uint8_t zero = 0; + ctx->status = ctx->wfunc(ctx->L, &zero, 1, ctx->wdata); + } +} + +/* Protected callback for bytecode writer. */ +static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud) +{ + BCWriteCtx *ctx = (BCWriteCtx *)ud; + UNUSED(dummy); + lj_str_resizebuf(L, &ctx->sb, 1024); /* Avoids resize for most prototypes. */ + bcwrite_header(ctx); + bcwrite_proto(ctx, ctx->pt); + bcwrite_footer(ctx); + return NULL; +} + +/* Write bytecode for a prototype. */ +int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data, + int strip) +{ + BCWriteCtx ctx; + int status; + ctx.L = L; + ctx.pt = pt; + ctx.wfunc = writer; + ctx.wdata = data; + ctx.strip = strip; + ctx.status = 0; + lj_str_initbuf(&ctx.sb); + status = lj_vm_cpcall(L, NULL, &ctx, cpwriter); + if (status == 0) status = ctx.status; + lj_str_freebuf(G(ctx.L), &ctx.sb); + return status; +} + diff --git a/external/lua/luajit/src/src/lj_carith.c b/external/lua/luajit/src/src/lj_carith.c new file mode 100644 index 0000000000..18708d668a --- /dev/null +++ b/external/lua/luajit/src/src/lj_carith.c @@ -0,0 +1,351 @@ +/* +** C data arithmetic. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "lj_obj.h" + +#if LJ_HASFFI + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_tab.h" +#include "lj_meta.h" +#include "lj_ctype.h" +#include "lj_cconv.h" +#include "lj_cdata.h" +#include "lj_carith.h" + +/* -- C data arithmetic --------------------------------------------------- */ + +/* Binary operands of an operator converted to ctypes. */ +typedef struct CDArith { + uint8_t *p[2]; + CType *ct[2]; +} CDArith; + +/* Check arguments for arithmetic metamethods. */ +static int carith_checkarg(lua_State *L, CTState *cts, CDArith *ca) +{ + TValue *o = L->base; + int ok = 1; + MSize i; + if (o+1 >= L->top) + lj_err_argt(L, 1, LUA_TCDATA); + for (i = 0; i < 2; i++, o++) { + if (tviscdata(o)) { + GCcdata *cd = cdataV(o); + CTypeID id = (CTypeID)cd->ctypeid; + CType *ct = ctype_raw(cts, id); + uint8_t *p = (uint8_t *)cdataptr(cd); + if (ctype_isptr(ct->info)) { + p = (uint8_t *)cdata_getptr(p, ct->size); + if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct); + } else if (ctype_isfunc(ct->info)) { + p = (uint8_t *)*(void **)p; + ct = ctype_get(cts, + lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR)); + } + if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); + ca->ct[i] = ct; + ca->p[i] = p; + } else if (tvisint(o)) { + ca->ct[i] = ctype_get(cts, CTID_INT32); + ca->p[i] = (uint8_t *)&o->i; + } else if (tvisnum(o)) { + ca->ct[i] = ctype_get(cts, CTID_DOUBLE); + ca->p[i] = (uint8_t *)&o->n; + } else if (tvisnil(o)) { + ca->ct[i] = ctype_get(cts, CTID_P_VOID); + ca->p[i] = (uint8_t *)0; + } else if (tvisstr(o)) { + TValue *o2 = i == 0 ? o+1 : o-1; + CType *ct = ctype_raw(cts, cdataV(o2)->ctypeid); + ca->ct[i] = NULL; + ca->p[i] = NULL; + ok = 0; + if (ctype_isenum(ct->info)) { + CTSize ofs; + CType *cct = lj_ctype_getfield(cts, ct, strV(o), &ofs); + if (cct && ctype_isconstval(cct->info)) { + ca->ct[i] = ctype_child(cts, cct); + ca->p[i] = (uint8_t *)&cct->size; /* Assumes ct does not grow. */ + ok = 1; + } else { + ca->ct[1-i] = ct; /* Use enum to improve error message. */ + ca->p[1-i] = NULL; + break; + } + } + } else { + ca->ct[i] = NULL; + ca->p[i] = NULL; + ok = 0; + } + } + return ok; +} + +/* Pointer arithmetic. */ +static int carith_ptr(lua_State *L, CTState *cts, CDArith *ca, MMS mm) +{ + CType *ctp = ca->ct[0]; + uint8_t *pp = ca->p[0]; + ptrdiff_t idx; + CTSize sz; + CTypeID id; + GCcdata *cd; + if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) { + if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) && + (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) { + uint8_t *pp2 = ca->p[1]; + if (mm == MM_eq) { /* Pointer equality. Incompatible pointers are ok. */ + setboolV(L->top-1, (pp == pp2)); + return 1; + } + if (!lj_cconv_compatptr(cts, ctp, ca->ct[1], CCF_IGNQUAL)) + return 0; + if (mm == MM_sub) { /* Pointer difference. */ + intptr_t diff; + sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ + if (sz == 0 || sz == CTSIZE_INVALID) + return 0; + diff = ((intptr_t)pp - (intptr_t)pp2) / (int32_t)sz; + /* All valid pointer differences on x64 are in (-2^47, +2^47), + ** which fits into a double without loss of precision. + */ + setintptrV(L->top-1, (int32_t)diff); + return 1; + } else if (mm == MM_lt) { /* Pointer comparison (unsigned). */ + setboolV(L->top-1, ((uintptr_t)pp < (uintptr_t)pp2)); + return 1; + } else { + lua_assert(mm == MM_le); + setboolV(L->top-1, ((uintptr_t)pp <= (uintptr_t)pp2)); + return 1; + } + } + if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(ca->ct[1]->info))) + return 0; + lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[1], + (uint8_t *)&idx, ca->p[1], 0); + if (mm == MM_sub) idx = -idx; + } else if (mm == MM_add && ctype_isnum(ctp->info) && + (ctype_isptr(ca->ct[1]->info) || ctype_isrefarray(ca->ct[1]->info))) { + /* Swap pointer and index. */ + ctp = ca->ct[1]; pp = ca->p[1]; + lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ca->ct[0], + (uint8_t *)&idx, ca->p[0], 0); + } else { + return 0; + } + sz = lj_ctype_size(cts, ctype_cid(ctp->info)); /* Element size. */ + if (sz == CTSIZE_INVALID) + return 0; + pp += idx*(int32_t)sz; /* Compute pointer + index. */ + id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)), + CTSIZE_PTR); + cd = lj_cdata_new(cts, id, CTSIZE_PTR); + *(uint8_t **)cdataptr(cd) = pp; + setcdataV(L, L->top-1, cd); + lj_gc_check(L); + return 1; +} + +/* 64 bit integer arithmetic. */ +static int carith_int64(lua_State *L, CTState *cts, CDArith *ca, MMS mm) +{ + if (ctype_isnum(ca->ct[0]->info) && ca->ct[0]->size <= 8 && + ctype_isnum(ca->ct[1]->info) && ca->ct[1]->size <= 8) { + CTypeID id = (((ca->ct[0]->info & CTF_UNSIGNED) && ca->ct[0]->size == 8) || + ((ca->ct[1]->info & CTF_UNSIGNED) && ca->ct[1]->size == 8)) ? + CTID_UINT64 : CTID_INT64; + CType *ct = ctype_get(cts, id); + GCcdata *cd; + uint64_t u0, u1, *up; + lj_cconv_ct_ct(cts, ct, ca->ct[0], (uint8_t *)&u0, ca->p[0], 0); + if (mm != MM_unm) + lj_cconv_ct_ct(cts, ct, ca->ct[1], (uint8_t *)&u1, ca->p[1], 0); + switch (mm) { + case MM_eq: + setboolV(L->top-1, (u0 == u1)); + return 1; + case MM_lt: + setboolV(L->top-1, + id == CTID_INT64 ? ((int64_t)u0 < (int64_t)u1) : (u0 < u1)); + return 1; + case MM_le: + setboolV(L->top-1, + id == CTID_INT64 ? ((int64_t)u0 <= (int64_t)u1) : (u0 <= u1)); + return 1; + default: break; + } + cd = lj_cdata_new(cts, id, 8); + up = (uint64_t *)cdataptr(cd); + setcdataV(L, L->top-1, cd); + switch (mm) { + case MM_add: *up = u0 + u1; break; + case MM_sub: *up = u0 - u1; break; + case MM_mul: *up = u0 * u1; break; + case MM_div: + if (id == CTID_INT64) + *up = (uint64_t)lj_carith_divi64((int64_t)u0, (int64_t)u1); + else + *up = lj_carith_divu64(u0, u1); + break; + case MM_mod: + if (id == CTID_INT64) + *up = (uint64_t)lj_carith_modi64((int64_t)u0, (int64_t)u1); + else + *up = lj_carith_modu64(u0, u1); + break; + case MM_pow: + if (id == CTID_INT64) + *up = (uint64_t)lj_carith_powi64((int64_t)u0, (int64_t)u1); + else + *up = lj_carith_powu64(u0, u1); + break; + case MM_unm: *up = (uint64_t)-(int64_t)u0; break; + default: lua_assert(0); break; + } + lj_gc_check(L); + return 1; + } + return 0; +} + +/* Handle ctype arithmetic metamethods. */ +static int lj_carith_meta(lua_State *L, CTState *cts, CDArith *ca, MMS mm) +{ + cTValue *tv = NULL; + if (tviscdata(L->base)) { + CTypeID id = cdataV(L->base)->ctypeid; + CType *ct = ctype_raw(cts, id); + if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); + tv = lj_ctype_meta(cts, id, mm); + } + if (!tv && L->base+1 < L->top && tviscdata(L->base+1)) { + CTypeID id = cdataV(L->base+1)->ctypeid; + CType *ct = ctype_raw(cts, id); + if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); + tv = lj_ctype_meta(cts, id, mm); + } + if (!tv) { + const char *repr[2]; + int i, isenum = -1, isstr = -1; + if (mm == MM_eq) { /* Equality checks never raise an error. */ + setboolV(L->top-1, 0); + return 1; + } + for (i = 0; i < 2; i++) { + if (ca->ct[i]) { + if (ctype_isenum(ca->ct[i]->info)) isenum = i; + repr[i] = strdata(lj_ctype_repr(L, ctype_typeid(cts, ca->ct[i]), NULL)); + } else { + if (tvisstr(&L->base[i])) isstr = i; + repr[i] = lj_typename(&L->base[i]); + } + } + if ((isenum ^ isstr) == 1) + lj_err_callerv(L, LJ_ERR_FFI_BADCONV, repr[isstr], repr[isenum]); + lj_err_callerv(L, mm == MM_len ? LJ_ERR_FFI_BADLEN : + mm == MM_concat ? LJ_ERR_FFI_BADCONCAT : + mm < MM_add ? LJ_ERR_FFI_BADCOMP : LJ_ERR_FFI_BADARITH, + repr[0], repr[1]); + } + return lj_meta_tailcall(L, tv); +} + +/* Arithmetic operators for cdata. */ +int lj_carith_op(lua_State *L, MMS mm) +{ + CTState *cts = ctype_cts(L); + CDArith ca; + if (carith_checkarg(L, cts, &ca)) { + if (carith_int64(L, cts, &ca, mm) || carith_ptr(L, cts, &ca, mm)) { + copyTV(L, &G(L)->tmptv2, L->top-1); /* Remember for trace recorder. */ + return 1; + } + } + return lj_carith_meta(L, cts, &ca, mm); +} + +/* -- 64 bit integer arithmetic helpers ----------------------------------- */ + +#if LJ_32 && LJ_HASJIT +/* Signed/unsigned 64 bit multiplication. */ +int64_t lj_carith_mul64(int64_t a, int64_t b) +{ + return a * b; +} +#endif + +/* Unsigned 64 bit division. */ +uint64_t lj_carith_divu64(uint64_t a, uint64_t b) +{ + if (b == 0) return U64x(80000000,00000000); + return a / b; +} + +/* Signed 64 bit division. */ +int64_t lj_carith_divi64(int64_t a, int64_t b) +{ + if (b == 0 || (a == (int64_t)U64x(80000000,00000000) && b == -1)) + return U64x(80000000,00000000); + return a / b; +} + +/* Unsigned 64 bit modulo. */ +uint64_t lj_carith_modu64(uint64_t a, uint64_t b) +{ + if (b == 0) return U64x(80000000,00000000); + return a % b; +} + +/* Signed 64 bit modulo. */ +int64_t lj_carith_modi64(int64_t a, int64_t b) +{ + if (b == 0) return U64x(80000000,00000000); + if (a == (int64_t)U64x(80000000,00000000) && b == -1) return 0; + return a % b; +} + +/* Unsigned 64 bit x^k. */ +uint64_t lj_carith_powu64(uint64_t x, uint64_t k) +{ + uint64_t y; + if (k == 0) + return 1; + for (; (k & 1) == 0; k >>= 1) x *= x; + y = x; + if ((k >>= 1) != 0) { + for (;;) { + x *= x; + if (k == 1) break; + if (k & 1) y *= x; + k >>= 1; + } + y *= x; + } + return y; +} + +/* Signed 64 bit x^k. */ +int64_t lj_carith_powi64(int64_t x, int64_t k) +{ + if (k == 0) + return 1; + if (k < 0) { + if (x == 0) + return U64x(7fffffff,ffffffff); + else if (x == 1) + return 1; + else if (x == -1) + return (k & 1) ? -1 : 1; + else + return 0; + } + return (int64_t)lj_carith_powu64((uint64_t)x, (uint64_t)k); +} + +#endif diff --git a/external/lua/luajit/src/src/lj_carith.h b/external/lua/luajit/src/src/lj_carith.h new file mode 100644 index 0000000000..ae17df0068 --- /dev/null +++ b/external/lua/luajit/src/src/lj_carith.h @@ -0,0 +1,27 @@ +/* +** C data arithmetic. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_CARITH_H +#define _LJ_CARITH_H + +#include "lj_obj.h" + +#if LJ_HASFFI + +LJ_FUNC int lj_carith_op(lua_State *L, MMS mm); + +#if LJ_32 && LJ_HASJIT +LJ_FUNC int64_t lj_carith_mul64(int64_t x, int64_t k); +#endif +LJ_FUNC uint64_t lj_carith_divu64(uint64_t a, uint64_t b); +LJ_FUNC int64_t lj_carith_divi64(int64_t a, int64_t b); +LJ_FUNC uint64_t lj_carith_modu64(uint64_t a, uint64_t b); +LJ_FUNC int64_t lj_carith_modi64(int64_t a, int64_t b); +LJ_FUNC uint64_t lj_carith_powu64(uint64_t x, uint64_t k); +LJ_FUNC int64_t lj_carith_powi64(int64_t x, int64_t k); + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_ccall.c b/external/lua/luajit/src/src/lj_ccall.c new file mode 100644 index 0000000000..6196364a9a --- /dev/null +++ b/external/lua/luajit/src/src/lj_ccall.c @@ -0,0 +1,837 @@ +/* +** FFI C call handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "lj_obj.h" + +#if LJ_HASFFI + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_ctype.h" +#include "lj_cconv.h" +#include "lj_cdata.h" +#include "lj_ccall.h" +#include "lj_trace.h" + +/* Target-specific handling of register arguments. */ +#if LJ_TARGET_X86 +/* -- x86 calling conventions --------------------------------------------- */ + +#if LJ_ABI_WIN + +#define CCALL_HANDLE_STRUCTRET \ + /* Return structs bigger than 8 by reference (on stack only). */ \ + cc->retref = (sz > 8); \ + if (cc->retref) cc->stack[nsp++] = (GPRArg)dp; + +#define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET + +#else + +#define CCALL_HANDLE_STRUCTRET \ + cc->retref = 1; /* Return all structs by reference (in reg or on stack). */ \ + if (ngpr < maxgpr) \ + cc->gpr[ngpr++] = (GPRArg)dp; \ + else \ + cc->stack[nsp++] = (GPRArg)dp; + +#define CCALL_HANDLE_COMPLEXRET \ + /* Return complex float in GPRs and complex double by reference. */ \ + cc->retref = (sz > 8); \ + if (cc->retref) { \ + if (ngpr < maxgpr) \ + cc->gpr[ngpr++] = (GPRArg)dp; \ + else \ + cc->stack[nsp++] = (GPRArg)dp; \ + } + +#endif + +#define CCALL_HANDLE_COMPLEXRET2 \ + if (!cc->retref) \ + *(int64_t *)dp = *(int64_t *)sp; /* Copy complex float from GPRs. */ + +#define CCALL_HANDLE_STRUCTARG \ + ngpr = maxgpr; /* Pass all structs by value on the stack. */ + +#define CCALL_HANDLE_COMPLEXARG \ + isfp = 1; /* Pass complex by value on stack. */ + +#define CCALL_HANDLE_REGARG \ + if (!isfp) { /* Only non-FP values may be passed in registers. */ \ + if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \ + if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \ + } else if (ngpr + 1 <= maxgpr) { \ + dp = &cc->gpr[ngpr]; \ + ngpr += n; \ + goto done; \ + } \ + } + +#elif LJ_TARGET_X64 && LJ_ABI_WIN +/* -- Windows/x64 calling conventions ------------------------------------- */ + +#define CCALL_HANDLE_STRUCTRET \ + /* Return structs of size 1, 2, 4 or 8 in a GPR. */ \ + cc->retref = !(sz == 1 || sz == 2 || sz == 4 || sz == 8); \ + if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; + +#define CCALL_HANDLE_COMPLEXRET CCALL_HANDLE_STRUCTRET + +#define CCALL_HANDLE_COMPLEXRET2 \ + if (!cc->retref) \ + *(int64_t *)dp = *(int64_t *)sp; /* Copy complex float from GPRs. */ + +#define CCALL_HANDLE_STRUCTARG \ + /* Pass structs of size 1, 2, 4 or 8 in a GPR by value. */ \ + if (!(sz == 1 || sz == 2 || sz == 4 || sz == 8)) { \ + rp = cdataptr(lj_cdata_new(cts, did, sz)); \ + sz = CTSIZE_PTR; /* Pass all other structs by reference. */ \ + } + +#define CCALL_HANDLE_COMPLEXARG \ + /* Pass complex float in a GPR and complex double by reference. */ \ + if (sz != 2*sizeof(float)) { \ + rp = cdataptr(lj_cdata_new(cts, did, sz)); \ + sz = CTSIZE_PTR; \ + } + +/* Windows/x64 argument registers are strictly positional (use ngpr). */ +#define CCALL_HANDLE_REGARG \ + if (isfp) { \ + if (ngpr < 4) { dp = &cc->fpr[ngpr++]; nfpr = ngpr; goto done; } \ + } else { \ + if (ngpr < 4) { dp = &cc->gpr[ngpr++]; goto done; } \ + } + +#elif LJ_TARGET_X64 +/* -- POSIX/x64 calling conventions --------------------------------------- */ + +#define CCALL_HANDLE_STRUCTRET \ + int rcl[2]; rcl[0] = rcl[1] = 0; \ + if (ccall_classify_struct(cts, ctr, rcl, 0)) { \ + cc->retref = 1; /* Return struct by reference. */ \ + cc->gpr[ngpr++] = (GPRArg)dp; \ + } else { \ + cc->retref = 0; /* Return small structs in registers. */ \ + } + +#define CCALL_HANDLE_STRUCTRET2 \ + int rcl[2]; rcl[0] = rcl[1] = 0; \ + ccall_classify_struct(cts, ctr, rcl, 0); \ + ccall_struct_ret(cc, rcl, dp, ctr->size); + +#define CCALL_HANDLE_COMPLEXRET \ + /* Complex values are returned in one or two FPRs. */ \ + cc->retref = 0; + +#define CCALL_HANDLE_COMPLEXRET2 \ + if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPR. */ \ + *(int64_t *)dp = cc->fpr[0].l[0]; \ + } else { /* Copy non-contiguous complex double from FPRs. */ \ + ((int64_t *)dp)[0] = cc->fpr[0].l[0]; \ + ((int64_t *)dp)[1] = cc->fpr[1].l[0]; \ + } + +#define CCALL_HANDLE_STRUCTARG \ + int rcl[2]; rcl[0] = rcl[1] = 0; \ + if (!ccall_classify_struct(cts, d, rcl, 0)) { \ + cc->nsp = nsp; cc->ngpr = ngpr; cc->nfpr = nfpr; \ + if (ccall_struct_arg(cc, cts, d, rcl, o, narg)) goto err_nyi; \ + nsp = cc->nsp; ngpr = cc->ngpr; nfpr = cc->nfpr; \ + continue; \ + } /* Pass all other structs by value on stack. */ + +#define CCALL_HANDLE_COMPLEXARG \ + isfp = 2; /* Pass complex in FPRs or on stack. Needs postprocessing. */ + +#define CCALL_HANDLE_REGARG \ + if (isfp) { /* Try to pass argument in FPRs. */ \ + if (nfpr + n <= CCALL_NARG_FPR) { \ + dp = &cc->fpr[nfpr]; \ + nfpr += n; \ + goto done; \ + } \ + } else { /* Try to pass argument in GPRs. */ \ + /* Note that reordering is explicitly allowed in the x64 ABI. */ \ + if (n <= 2 && ngpr + n <= maxgpr) { \ + dp = &cc->gpr[ngpr]; \ + ngpr += n; \ + goto done; \ + } \ + } + +#elif LJ_TARGET_ARM +/* -- ARM calling conventions --------------------------------------------- */ + +#if LJ_ABI_SOFTFP + +#define CCALL_HANDLE_STRUCTRET \ + /* Return structs of size <= 4 in a GPR. */ \ + cc->retref = !(sz <= 4); \ + if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; + +#define CCALL_HANDLE_COMPLEXRET \ + cc->retref = 1; /* Return all complex values by reference. */ \ + cc->gpr[ngpr++] = (GPRArg)dp; + +#define CCALL_HANDLE_COMPLEXRET2 \ + UNUSED(dp); /* Nothing to do. */ + +#define CCALL_HANDLE_STRUCTARG \ + /* Pass all structs by value in registers and/or on the stack. */ + +#define CCALL_HANDLE_COMPLEXARG \ + /* Pass complex by value in 2 or 4 GPRs. */ + +#define CCALL_HANDLE_REGARG_FP1 +#define CCALL_HANDLE_REGARG_FP2 + +#else + +#define CCALL_HANDLE_STRUCTRET \ + cc->retref = !ccall_classify_struct(cts, ctr, ct); \ + if (cc->retref) cc->gpr[ngpr++] = (GPRArg)dp; + +#define CCALL_HANDLE_STRUCTRET2 \ + if (ccall_classify_struct(cts, ctr, ct) > 1) sp = (uint8_t *)&cc->fpr[0]; \ + memcpy(dp, sp, ctr->size); + +#define CCALL_HANDLE_COMPLEXRET \ + if (!(ct->info & CTF_VARARG)) cc->retref = 0; /* Return complex in FPRs. */ + +#define CCALL_HANDLE_COMPLEXRET2 \ + if (!(ct->info & CTF_VARARG)) memcpy(dp, &cc->fpr[0], ctr->size); + +#define CCALL_HANDLE_STRUCTARG \ + isfp = (ccall_classify_struct(cts, d, ct) > 1); + /* Pass all structs by value in registers and/or on the stack. */ + +#define CCALL_HANDLE_COMPLEXARG \ + isfp = 1; /* Pass complex by value in FPRs or on stack. */ + +#define CCALL_HANDLE_REGARG_FP1 \ + if (isfp && !(ct->info & CTF_VARARG)) { \ + if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \ + if (nfpr + (n >> 1) <= CCALL_NARG_FPR) { \ + dp = &cc->fpr[nfpr]; \ + nfpr += (n >> 1); \ + goto done; \ + } \ + } else { \ + if (sz > 1 && fprodd != nfpr) fprodd = 0; \ + if (fprodd) { \ + if (2*nfpr+n <= 2*CCALL_NARG_FPR+1) { \ + dp = (void *)&cc->fpr[fprodd-1].f[1]; \ + nfpr += (n >> 1); \ + if ((n & 1)) fprodd = 0; else fprodd = nfpr-1; \ + goto done; \ + } \ + } else { \ + if (2*nfpr+n <= 2*CCALL_NARG_FPR) { \ + dp = (void *)&cc->fpr[nfpr]; \ + nfpr += (n >> 1); \ + if ((n & 1)) fprodd = ++nfpr; else fprodd = 0; \ + goto done; \ + } \ + } \ + } \ + fprodd = 0; /* No reordering after the first FP value is on stack. */ \ + } else { + +#define CCALL_HANDLE_REGARG_FP2 } + +#endif + +#define CCALL_HANDLE_REGARG \ + CCALL_HANDLE_REGARG_FP1 \ + if ((d->info & CTF_ALIGN) > CTALIGN_PTR) { \ + if (ngpr < maxgpr) \ + ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ + } \ + if (ngpr < maxgpr) { \ + dp = &cc->gpr[ngpr]; \ + if (ngpr + n > maxgpr) { \ + nsp += ngpr + n - maxgpr; /* Assumes contiguous gpr/stack fields. */ \ + if (nsp > CCALL_MAXSTACK) goto err_nyi; /* Too many arguments. */ \ + ngpr = maxgpr; \ + } else { \ + ngpr += n; \ + } \ + goto done; \ + } CCALL_HANDLE_REGARG_FP2 + +#define CCALL_HANDLE_RET \ + if ((ct->info & CTF_VARARG)) sp = (uint8_t *)&cc->gpr[0]; + +#elif LJ_TARGET_PPC +/* -- PPC calling conventions --------------------------------------------- */ + +#define CCALL_HANDLE_STRUCTRET \ + cc->retref = 1; /* Return all structs by reference. */ \ + cc->gpr[ngpr++] = (GPRArg)dp; + +#define CCALL_HANDLE_COMPLEXRET \ + /* Complex values are returned in 2 or 4 GPRs. */ \ + cc->retref = 0; + +#define CCALL_HANDLE_COMPLEXRET2 \ + memcpy(dp, sp, ctr->size); /* Copy complex from GPRs. */ + +#define CCALL_HANDLE_STRUCTARG \ + rp = cdataptr(lj_cdata_new(cts, did, sz)); \ + sz = CTSIZE_PTR; /* Pass all structs by reference. */ + +#define CCALL_HANDLE_COMPLEXARG \ + /* Pass complex by value in 2 or 4 GPRs. */ + +#define CCALL_HANDLE_REGARG \ + if (isfp) { /* Try to pass argument in FPRs. */ \ + if (nfpr + 1 <= CCALL_NARG_FPR) { \ + dp = &cc->fpr[nfpr]; \ + nfpr += 1; \ + d = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \ + goto done; \ + } \ + } else { /* Try to pass argument in GPRs. */ \ + if (n > 1) { \ + lua_assert(n == 2 || n == 4); /* int64_t or complex (float). */ \ + if (ctype_isinteger(d->info)) \ + ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \ + else if (ngpr + n > maxgpr) \ + ngpr = maxgpr; /* Prevent reordering. */ \ + } \ + if (ngpr + n <= maxgpr) { \ + dp = &cc->gpr[ngpr]; \ + ngpr += n; \ + goto done; \ + } \ + } + +#define CCALL_HANDLE_RET \ + if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ + ctr = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ + +#elif LJ_TARGET_PPCSPE +/* -- PPC/SPE calling conventions ----------------------------------------- */ + +#define CCALL_HANDLE_STRUCTRET \ + cc->retref = 1; /* Return all structs by reference. */ \ + cc->gpr[ngpr++] = (GPRArg)dp; + +#define CCALL_HANDLE_COMPLEXRET \ + /* Complex values are returned in 2 or 4 GPRs. */ \ + cc->retref = 0; + +#define CCALL_HANDLE_COMPLEXRET2 \ + memcpy(dp, sp, ctr->size); /* Copy complex from GPRs. */ + +#define CCALL_HANDLE_STRUCTARG \ + rp = cdataptr(lj_cdata_new(cts, did, sz)); \ + sz = CTSIZE_PTR; /* Pass all structs by reference. */ + +#define CCALL_HANDLE_COMPLEXARG \ + /* Pass complex by value in 2 or 4 GPRs. */ + +/* PPC/SPE has a softfp ABI. */ +#define CCALL_HANDLE_REGARG \ + if (n > 1) { /* Doesn't fit in a single GPR? */ \ + lua_assert(n == 2 || n == 4); /* int64_t, double or complex (float). */ \ + if (n == 2) \ + ngpr = (ngpr + 1u) & ~1u; /* Only align 64 bit value to regpair. */ \ + else if (ngpr + n > maxgpr) \ + ngpr = maxgpr; /* Prevent reordering. */ \ + } \ + if (ngpr + n <= maxgpr) { \ + dp = &cc->gpr[ngpr]; \ + ngpr += n; \ + goto done; \ + } + +#elif LJ_TARGET_MIPS +/* -- MIPS calling conventions -------------------------------------------- */ + +#define CCALL_HANDLE_STRUCTRET \ + cc->retref = 1; /* Return all structs by reference. */ \ + cc->gpr[ngpr++] = (GPRArg)dp; + +#define CCALL_HANDLE_COMPLEXRET \ + /* Complex values are returned in 1 or 2 FPRs. */ \ + cc->retref = 0; + +#define CCALL_HANDLE_COMPLEXRET2 \ + if (ctr->size == 2*sizeof(float)) { /* Copy complex float from FPRs. */ \ + ((float *)dp)[0] = cc->fpr[0].f; \ + ((float *)dp)[1] = cc->fpr[1].f; \ + } else { /* Copy complex double from FPRs. */ \ + ((double *)dp)[0] = cc->fpr[0].d; \ + ((double *)dp)[1] = cc->fpr[1].d; \ + } + +#define CCALL_HANDLE_STRUCTARG \ + /* Pass all structs by value in registers and/or on the stack. */ + +#define CCALL_HANDLE_COMPLEXARG \ + /* Pass complex by value in 2 or 4 GPRs. */ + +#define CCALL_HANDLE_REGARG \ + if (isfp && nfpr < CCALL_NARG_FPR && !(ct->info & CTF_VARARG)) { \ + /* Try to pass argument in FPRs. */ \ + dp = n == 1 ? (void *)&cc->fpr[nfpr].f : (void *)&cc->fpr[nfpr].d; \ + nfpr++; ngpr += n; \ + goto done; \ + } else { /* Try to pass argument in GPRs. */ \ + nfpr = CCALL_NARG_FPR; \ + if ((d->info & CTF_ALIGN) > CTALIGN_PTR) \ + ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ + if (ngpr < maxgpr) { \ + dp = &cc->gpr[ngpr]; \ + if (ngpr + n > maxgpr) { \ + nsp += ngpr + n - maxgpr; /* Assumes contiguous gpr/stack fields. */ \ + if (nsp > CCALL_MAXSTACK) goto err_nyi; /* Too many arguments. */ \ + ngpr = maxgpr; \ + } else { \ + ngpr += n; \ + } \ + goto done; \ + } \ + } + +#define CCALL_HANDLE_RET \ + if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ + sp = (uint8_t *)&cc->fpr[0].f; + +#else +#error "Missing calling convention definitions for this architecture" +#endif + +#ifndef CCALL_HANDLE_STRUCTRET2 +#define CCALL_HANDLE_STRUCTRET2 \ + memcpy(dp, sp, ctr->size); /* Copy struct return value from GPRs. */ +#endif + +/* -- x64 struct classification ------------------------------------------- */ + +#if LJ_TARGET_X64 && !LJ_ABI_WIN + +/* Register classes for x64 struct classification. */ +#define CCALL_RCL_INT 1 +#define CCALL_RCL_SSE 2 +#define CCALL_RCL_MEM 4 +/* NYI: classify vectors. */ + +static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs); + +/* Classify a C type. */ +static void ccall_classify_ct(CTState *cts, CType *ct, int *rcl, CTSize ofs) +{ + if (ctype_isarray(ct->info)) { + CType *cct = ctype_rawchild(cts, ct); + CTSize eofs, esz = cct->size, asz = ct->size; + for (eofs = 0; eofs < asz; eofs += esz) + ccall_classify_ct(cts, cct, rcl, ofs+eofs); + } else if (ctype_isstruct(ct->info)) { + ccall_classify_struct(cts, ct, rcl, ofs); + } else { + int cl = ctype_isfp(ct->info) ? CCALL_RCL_SSE : CCALL_RCL_INT; + lua_assert(ctype_hassize(ct->info)); + if ((ofs & (ct->size-1))) cl = CCALL_RCL_MEM; /* Unaligned. */ + rcl[(ofs >= 8)] |= cl; + } +} + +/* Recursively classify a struct based on its fields. */ +static int ccall_classify_struct(CTState *cts, CType *ct, int *rcl, CTSize ofs) +{ + if (ct->size > 16) return CCALL_RCL_MEM; /* Too big, gets memory class. */ + while (ct->sib) { + CTSize fofs; + ct = ctype_get(cts, ct->sib); + fofs = ofs+ct->size; + if (ctype_isfield(ct->info)) + ccall_classify_ct(cts, ctype_rawchild(cts, ct), rcl, fofs); + else if (ctype_isbitfield(ct->info)) + rcl[(fofs >= 8)] |= CCALL_RCL_INT; /* NYI: unaligned bitfields? */ + else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) + ccall_classify_struct(cts, ctype_rawchild(cts, ct), rcl, fofs); + } + return ((rcl[0]|rcl[1]) & CCALL_RCL_MEM); /* Memory class? */ +} + +/* Try to split up a small struct into registers. */ +static int ccall_struct_reg(CCallState *cc, GPRArg *dp, int *rcl) +{ + MSize ngpr = cc->ngpr, nfpr = cc->nfpr; + uint32_t i; + for (i = 0; i < 2; i++) { + lua_assert(!(rcl[i] & CCALL_RCL_MEM)); + if ((rcl[i] & CCALL_RCL_INT)) { /* Integer class takes precedence. */ + if (ngpr >= CCALL_NARG_GPR) return 1; /* Register overflow. */ + cc->gpr[ngpr++] = dp[i]; + } else if ((rcl[i] & CCALL_RCL_SSE)) { + if (nfpr >= CCALL_NARG_FPR) return 1; /* Register overflow. */ + cc->fpr[nfpr++].l[0] = dp[i]; + } + } + cc->ngpr = ngpr; cc->nfpr = nfpr; + return 0; /* Ok. */ +} + +/* Pass a small struct argument. */ +static int ccall_struct_arg(CCallState *cc, CTState *cts, CType *d, int *rcl, + TValue *o, int narg) +{ + GPRArg dp[2]; + dp[0] = dp[1] = 0; + /* Convert to temp. struct. */ + lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg)); + if (ccall_struct_reg(cc, dp, rcl)) { /* Register overflow? Pass on stack. */ + MSize nsp = cc->nsp, n = rcl[1] ? 2 : 1; + if (nsp + n > CCALL_MAXSTACK) return 1; /* Too many arguments. */ + cc->nsp = nsp + n; + memcpy(&cc->stack[nsp], dp, n*CTSIZE_PTR); + } + return 0; /* Ok. */ +} + +/* Combine returned small struct. */ +static void ccall_struct_ret(CCallState *cc, int *rcl, uint8_t *dp, CTSize sz) +{ + GPRArg sp[2]; + MSize ngpr = 0, nfpr = 0; + uint32_t i; + for (i = 0; i < 2; i++) { + if ((rcl[i] & CCALL_RCL_INT)) { /* Integer class takes precedence. */ + sp[i] = cc->gpr[ngpr++]; + } else if ((rcl[i] & CCALL_RCL_SSE)) { + sp[i] = cc->fpr[nfpr++].l[0]; + } + } + memcpy(dp, sp, sz); +} +#endif + +/* -- ARM hard-float ABI struct classification ---------------------------- */ + +#if LJ_TARGET_ARM && !LJ_ABI_SOFTFP + +/* Classify a struct based on its fields. */ +static unsigned int ccall_classify_struct(CTState *cts, CType *ct, CType *ctf) +{ + CTSize sz = ct->size; + unsigned int r = 0, n = 0, isu = (ct->info & CTF_UNION); + if ((ctf->info & CTF_VARARG)) goto noth; + while (ct->sib) { + ct = ctype_get(cts, ct->sib); + if (ctype_isfield(ct->info)) { + CType *sct = ctype_rawchild(cts, ct); + if (ctype_isfp(sct->info)) { + r |= sct->size; + if (!isu) n++; else if (n == 0) n = 1; + } else if (ctype_iscomplex(sct->info)) { + r |= (sct->size >> 1); + if (!isu) n += 2; else if (n < 2) n = 2; + } else { + goto noth; + } + } else if (ctype_isbitfield(ct->info)) { + goto noth; + } else if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { + CType *sct = ctype_rawchild(cts, ct); + if (sct->size > 0) { + unsigned int s = ccall_classify_struct(cts, sct, ctf); + if (s <= 1) goto noth; + r |= (s & 255); + if (!isu) n += (s >> 8); else if (n < (s >>8)) n = (s >> 8); + } + } + } + if ((r == 4 || r == 8) && n <= 4) + return r + (n << 8); +noth: /* Not a homogeneous float/double aggregate. */ + return (sz <= 4); /* Return structs of size <= 4 in a GPR. */ +} + +#endif + +/* -- Common C call handling ---------------------------------------------- */ + +/* Infer the destination CTypeID for a vararg argument. */ +CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o) +{ + if (tvisnumber(o)) { + return CTID_DOUBLE; + } else if (tviscdata(o)) { + CTypeID id = cdataV(o)->ctypeid; + CType *s = ctype_get(cts, id); + if (ctype_isrefarray(s->info)) { + return lj_ctype_intern(cts, + CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(s->info)), CTSIZE_PTR); + } else if (ctype_isstruct(s->info) || ctype_isfunc(s->info)) { + /* NYI: how to pass a struct by value in a vararg argument? */ + return lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR); + } else if (ctype_isfp(s->info) && s->size == sizeof(float)) { + return CTID_DOUBLE; + } else { + return id; + } + } else if (tvisstr(o)) { + return CTID_P_CCHAR; + } else if (tvisbool(o)) { + return CTID_BOOL; + } else { + return CTID_P_VOID; + } +} + +/* Setup arguments for C call. */ +static int ccall_set_args(lua_State *L, CTState *cts, CType *ct, + CCallState *cc) +{ + int gcsteps = 0; + TValue *o, *top = L->top; + CTypeID fid; + CType *ctr; + MSize maxgpr, ngpr = 0, nsp = 0, narg; +#if CCALL_NARG_FPR + MSize nfpr = 0; +#if LJ_TARGET_ARM + MSize fprodd = 0; +#endif +#endif + + /* Clear unused regs to get some determinism in case of misdeclaration. */ + memset(cc->gpr, 0, sizeof(cc->gpr)); +#if CCALL_NUM_FPR + memset(cc->fpr, 0, sizeof(cc->fpr)); +#endif + +#if LJ_TARGET_X86 + /* x86 has several different calling conventions. */ + cc->resx87 = 0; + switch (ctype_cconv(ct->info)) { + case CTCC_FASTCALL: maxgpr = 2; break; + case CTCC_THISCALL: maxgpr = 1; break; + default: maxgpr = 0; break; + } +#else + maxgpr = CCALL_NARG_GPR; +#endif + + /* Perform required setup for some result types. */ + ctr = ctype_rawchild(cts, ct); + if (ctype_isvector(ctr->info)) { + if (!(CCALL_VECTOR_REG && (ctr->size == 8 || ctr->size == 16))) + goto err_nyi; + } else if (ctype_iscomplex(ctr->info) || ctype_isstruct(ctr->info)) { + /* Preallocate cdata object and anchor it after arguments. */ + CTSize sz = ctr->size; + GCcdata *cd = lj_cdata_new(cts, ctype_cid(ct->info), sz); + void *dp = cdataptr(cd); + setcdataV(L, L->top++, cd); + if (ctype_isstruct(ctr->info)) { + CCALL_HANDLE_STRUCTRET + } else { + CCALL_HANDLE_COMPLEXRET + } +#if LJ_TARGET_X86 + } else if (ctype_isfp(ctr->info)) { + cc->resx87 = ctr->size == sizeof(float) ? 1 : 2; +#endif + } + + /* Skip initial attributes. */ + fid = ct->sib; + while (fid) { + CType *ctf = ctype_get(cts, fid); + if (!ctype_isattrib(ctf->info)) break; + fid = ctf->sib; + } + + /* Walk through all passed arguments. */ + for (o = L->base+1, narg = 1; o < top; o++, narg++) { + CTypeID did; + CType *d; + CTSize sz; + MSize n, isfp = 0, isva = 0; + void *dp, *rp = NULL; + + if (fid) { /* Get argument type from field. */ + CType *ctf = ctype_get(cts, fid); + fid = ctf->sib; + lua_assert(ctype_isfield(ctf->info)); + did = ctype_cid(ctf->info); + } else { + if (!(ct->info & CTF_VARARG)) + lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too many arguments. */ + did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */ + isva = 1; + } + d = ctype_raw(cts, did); + sz = d->size; + + /* Find out how (by value/ref) and where (GPR/FPR) to pass an argument. */ + if (ctype_isnum(d->info)) { + if (sz > 8) goto err_nyi; + if ((d->info & CTF_FP)) + isfp = 1; + } else if (ctype_isvector(d->info)) { + if (CCALL_VECTOR_REG && (sz == 8 || sz == 16)) + isfp = 1; + else + goto err_nyi; + } else if (ctype_isstruct(d->info)) { + CCALL_HANDLE_STRUCTARG + } else if (ctype_iscomplex(d->info)) { + CCALL_HANDLE_COMPLEXARG + } else { + sz = CTSIZE_PTR; + } + sz = (sz + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1); + n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */ + + CCALL_HANDLE_REGARG /* Handle register arguments. */ + + /* Otherwise pass argument on stack. */ + if (CCALL_ALIGN_STACKARG && !rp && (d->info & CTF_ALIGN) > CTALIGN_PTR) { + MSize align = (1u << ctype_align(d->info-CTALIGN_PTR)) -1; + nsp = (nsp + align) & ~align; /* Align argument on stack. */ + } + if (nsp + n > CCALL_MAXSTACK) { /* Too many arguments. */ + err_nyi: + lj_err_caller(L, LJ_ERR_FFI_NYICALL); + } + dp = &cc->stack[nsp]; + nsp += n; + isva = 0; + + done: + if (rp) { /* Pass by reference. */ + gcsteps++; + *(void **)dp = rp; + dp = rp; + } + lj_cconv_ct_tv(cts, d, (uint8_t *)dp, o, CCF_ARG(narg)); + /* Extend passed integers to 32 bits at least. */ + if (ctype_isinteger_or_bool(d->info) && d->size < 4) { + if (d->info & CTF_UNSIGNED) + *(uint32_t *)dp = d->size == 1 ? (uint32_t)*(uint8_t *)dp : + (uint32_t)*(uint16_t *)dp; + else + *(int32_t *)dp = d->size == 1 ? (int32_t)*(int8_t *)dp : + (int32_t)*(int16_t *)dp; + } +#if LJ_TARGET_X64 && LJ_ABI_WIN + if (isva) { /* Windows/x64 mirrors varargs in both register sets. */ + if (nfpr == ngpr) + cc->gpr[ngpr-1] = cc->fpr[ngpr-1].l[0]; + else + cc->fpr[ngpr-1].l[0] = cc->gpr[ngpr-1]; + } +#else + UNUSED(isva); +#endif +#if LJ_TARGET_X64 && !LJ_ABI_WIN + if (isfp == 2 && n == 2 && (uint8_t *)dp == (uint8_t *)&cc->fpr[nfpr-2]) { + cc->fpr[nfpr-1].d[0] = cc->fpr[nfpr-2].d[1]; /* Split complex double. */ + cc->fpr[nfpr-2].d[1] = 0; + } +#else + UNUSED(isfp); +#endif + } + if (fid) lj_err_caller(L, LJ_ERR_FFI_NUMARG); /* Too few arguments. */ + +#if LJ_TARGET_X64 || LJ_TARGET_PPC + cc->nfpr = nfpr; /* Required for vararg functions. */ +#endif + cc->nsp = nsp; + cc->spadj = (CCALL_SPS_FREE + CCALL_SPS_EXTRA)*CTSIZE_PTR; + if (nsp > CCALL_SPS_FREE) + cc->spadj += (((nsp-CCALL_SPS_FREE)*CTSIZE_PTR + 15u) & ~15u); + return gcsteps; +} + +/* Get results from C call. */ +static int ccall_get_results(lua_State *L, CTState *cts, CType *ct, + CCallState *cc, int *ret) +{ + CType *ctr = ctype_rawchild(cts, ct); + uint8_t *sp = (uint8_t *)&cc->gpr[0]; + if (ctype_isvoid(ctr->info)) { + *ret = 0; /* Zero results. */ + return 0; /* No additional GC step. */ + } + *ret = 1; /* One result. */ + if (ctype_isstruct(ctr->info)) { + /* Return cdata object which is already on top of stack. */ + if (!cc->retref) { + void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */ + CCALL_HANDLE_STRUCTRET2 + } + return 1; /* One GC step. */ + } + if (ctype_iscomplex(ctr->info)) { + /* Return cdata object which is already on top of stack. */ + void *dp = cdataptr(cdataV(L->top-1)); /* Use preallocated object. */ + CCALL_HANDLE_COMPLEXRET2 + return 1; /* One GC step. */ + } + if (LJ_BE && ctype_isinteger_or_bool(ctr->info) && ctr->size < CTSIZE_PTR) + sp += (CTSIZE_PTR - ctr->size); +#if CCALL_NUM_FPR + if (ctype_isfp(ctr->info) || ctype_isvector(ctr->info)) + sp = (uint8_t *)&cc->fpr[0]; +#endif +#ifdef CCALL_HANDLE_RET + CCALL_HANDLE_RET +#endif + /* No reference types end up here, so there's no need for the CTypeID. */ + lua_assert(!(ctype_isrefarray(ctr->info) || ctype_isstruct(ctr->info))); + return lj_cconv_tv_ct(cts, ctr, 0, L->top-1, sp); +} + +/* Call C function. */ +int lj_ccall_func(lua_State *L, GCcdata *cd) +{ + CTState *cts = ctype_cts(L); + CType *ct = ctype_raw(cts, cd->ctypeid); + CTSize sz = CTSIZE_PTR; + if (ctype_isptr(ct->info)) { + sz = ct->size; + ct = ctype_rawchild(cts, ct); + } + if (ctype_isfunc(ct->info)) { + CCallState cc; + int gcsteps, ret; + cc.func = (void (*)(void))cdata_getptr(cdataptr(cd), sz); + gcsteps = ccall_set_args(L, cts, ct, &cc); + ct = (CType *)((intptr_t)ct-(intptr_t)cts->tab); + cts->cb.slot = ~0u; + lj_vm_ffi_call(&cc); + if (cts->cb.slot != ~0u) { /* Blacklist function that called a callback. */ + TValue tv; + setlightudV(&tv, (void *)cc.func); + setboolV(lj_tab_set(L, cts->miscmap, &tv), 1); + } + ct = (CType *)((intptr_t)ct+(intptr_t)cts->tab); /* May be reallocated. */ + gcsteps += ccall_get_results(L, cts, ct, &cc, &ret); +#if LJ_TARGET_X86 && LJ_ABI_WIN + /* Automatically detect __stdcall and fix up C function declaration. */ + if (cc.spadj && ctype_cconv(ct->info) == CTCC_CDECL) { + CTF_INSERT(ct->info, CCONV, CTCC_STDCALL); + lj_trace_abort(G(L)); + } +#endif + while (gcsteps-- > 0) + lj_gc_check(L); + return ret; + } + return -1; /* Not a function. */ +} + +#endif diff --git a/external/lua/luajit/src/src/lj_ccall.h b/external/lua/luajit/src/src/lj_ccall.h new file mode 100644 index 0000000000..1afeed75b2 --- /dev/null +++ b/external/lua/luajit/src/src/lj_ccall.h @@ -0,0 +1,171 @@ +/* +** FFI C call handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_CCALL_H +#define _LJ_CCALL_H + +#include "lj_obj.h" +#include "lj_ctype.h" + +#if LJ_HASFFI + +/* -- C calling conventions ----------------------------------------------- */ + +#if LJ_TARGET_X86ORX64 + +#if LJ_TARGET_X86 +#define CCALL_NARG_GPR 2 /* For fastcall arguments. */ +#define CCALL_NARG_FPR 0 +#define CCALL_NRET_GPR 2 +#define CCALL_NRET_FPR 1 /* For FP results on x87 stack. */ +#define CCALL_ALIGN_STACKARG 0 /* Don't align argument on stack. */ +#elif LJ_ABI_WIN +#define CCALL_NARG_GPR 4 +#define CCALL_NARG_FPR 4 +#define CCALL_NRET_GPR 1 +#define CCALL_NRET_FPR 1 +#define CCALL_SPS_EXTRA 4 +#else +#define CCALL_NARG_GPR 6 +#define CCALL_NARG_FPR 8 +#define CCALL_NRET_GPR 2 +#define CCALL_NRET_FPR 2 +#define CCALL_VECTOR_REG 1 /* Pass vectors in registers. */ +#endif + +#define CCALL_SPS_FREE 1 +#define CCALL_ALIGN_CALLSTATE 16 + +typedef LJ_ALIGN(16) union FPRArg { + double d[2]; + float f[4]; + uint8_t b[16]; + uint16_t s[8]; + int i[4]; + int64_t l[2]; +} FPRArg; + +typedef intptr_t GPRArg; + +#elif LJ_TARGET_ARM + +#define CCALL_NARG_GPR 4 +#define CCALL_NRET_GPR 2 /* For softfp double. */ +#if LJ_ABI_SOFTFP +#define CCALL_NARG_FPR 0 +#define CCALL_NRET_FPR 0 +#else +#define CCALL_NARG_FPR 8 +#define CCALL_NRET_FPR 4 +#endif +#define CCALL_SPS_FREE 0 + +typedef intptr_t GPRArg; +typedef union FPRArg { + double d; + float f[2]; +} FPRArg; + +#elif LJ_TARGET_PPC + +#define CCALL_NARG_GPR 8 +#define CCALL_NARG_FPR 8 +#define CCALL_NRET_GPR 4 /* For complex double. */ +#define CCALL_NRET_FPR 1 +#define CCALL_SPS_EXTRA 4 +#define CCALL_SPS_FREE 0 + +typedef intptr_t GPRArg; +typedef double FPRArg; + +#elif LJ_TARGET_PPCSPE + +#define CCALL_NARG_GPR 8 +#define CCALL_NARG_FPR 0 +#define CCALL_NRET_GPR 4 /* For softfp complex double. */ +#define CCALL_NRET_FPR 0 +#define CCALL_SPS_FREE 0 /* NYI */ + +typedef intptr_t GPRArg; + +#elif LJ_TARGET_MIPS + +#define CCALL_NARG_GPR 4 +#define CCALL_NARG_FPR 2 +#define CCALL_NRET_GPR 2 +#define CCALL_NRET_FPR 2 +#define CCALL_SPS_EXTRA 7 +#define CCALL_SPS_FREE 1 + +typedef intptr_t GPRArg; +typedef union FPRArg { + double d; + struct { LJ_ENDIAN_LOHI(float f; , float g;) }; +} FPRArg; + +#else +#error "Missing calling convention definitions for this architecture" +#endif + +#ifndef CCALL_SPS_EXTRA +#define CCALL_SPS_EXTRA 0 +#endif +#ifndef CCALL_VECTOR_REG +#define CCALL_VECTOR_REG 0 +#endif +#ifndef CCALL_ALIGN_STACKARG +#define CCALL_ALIGN_STACKARG 1 +#endif +#ifndef CCALL_ALIGN_CALLSTATE +#define CCALL_ALIGN_CALLSTATE 8 +#endif + +#define CCALL_NUM_GPR \ + (CCALL_NARG_GPR > CCALL_NRET_GPR ? CCALL_NARG_GPR : CCALL_NRET_GPR) +#define CCALL_NUM_FPR \ + (CCALL_NARG_FPR > CCALL_NRET_FPR ? CCALL_NARG_FPR : CCALL_NRET_FPR) + +/* Check against constants in lj_ctype.h. */ +LJ_STATIC_ASSERT(CCALL_NUM_GPR <= CCALL_MAX_GPR); +LJ_STATIC_ASSERT(CCALL_NUM_FPR <= CCALL_MAX_FPR); + +#define CCALL_MAXSTACK 32 + +/* -- C call state -------------------------------------------------------- */ + +typedef LJ_ALIGN(CCALL_ALIGN_CALLSTATE) struct CCallState { + void (*func)(void); /* Pointer to called function. */ + uint32_t spadj; /* Stack pointer adjustment. */ + uint8_t nsp; /* Number of stack slots. */ + uint8_t retref; /* Return value by reference. */ +#if LJ_TARGET_X64 + uint8_t ngpr; /* Number of arguments in GPRs. */ + uint8_t nfpr; /* Number of arguments in FPRs. */ +#elif LJ_TARGET_X86 + uint8_t resx87; /* Result on x87 stack: 1:float, 2:double. */ +#elif LJ_TARGET_PPC + uint8_t nfpr; /* Number of arguments in FPRs. */ +#endif +#if LJ_32 + int32_t align1; +#endif +#if CCALL_NUM_FPR + FPRArg fpr[CCALL_NUM_FPR]; /* Arguments/results in FPRs. */ +#endif + GPRArg gpr[CCALL_NUM_GPR]; /* Arguments/results in GPRs. */ + GPRArg stack[CCALL_MAXSTACK]; /* Stack slots. */ +} CCallState; + +/* -- C call handling ----------------------------------------------------- */ + +/* Really belongs to lj_vm.h. */ +LJ_ASMF void LJ_FASTCALL lj_vm_ffi_call(CCallState *cc); + +LJ_FUNC CTypeID lj_ccall_ctid_vararg(CTState *cts, cTValue *o); +LJ_FUNC int lj_ccall_func(lua_State *L, GCcdata *cd); + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_ccallback.c b/external/lua/luajit/src/src/lj_ccallback.c new file mode 100644 index 0000000000..a3a0d7985a --- /dev/null +++ b/external/lua/luajit/src/src/lj_ccallback.c @@ -0,0 +1,641 @@ +/* +** FFI C callback handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "lj_obj.h" + +#if LJ_HASFFI + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_tab.h" +#include "lj_state.h" +#include "lj_frame.h" +#include "lj_ctype.h" +#include "lj_cconv.h" +#include "lj_ccall.h" +#include "lj_ccallback.h" +#include "lj_target.h" +#include "lj_mcode.h" +#include "lj_trace.h" +#include "lj_vm.h" + +/* -- Target-specific handling of callback slots -------------------------- */ + +#define CALLBACK_MCODE_SIZE (LJ_PAGESIZE * LJ_NUM_CBPAGE) + +#if LJ_OS_NOJIT + +/* Disabled callback support. */ +#define CALLBACK_SLOT2OFS(slot) (0*(slot)) +#define CALLBACK_OFS2SLOT(ofs) (0*(ofs)) +#define CALLBACK_MAX_SLOT 0 + +#elif LJ_TARGET_X86ORX64 + +#define CALLBACK_MCODE_HEAD (LJ_64 ? 8 : 0) +#define CALLBACK_MCODE_GROUP (-2+1+2+5+(LJ_64 ? 6 : 5)) + +#define CALLBACK_SLOT2OFS(slot) \ + (CALLBACK_MCODE_HEAD + CALLBACK_MCODE_GROUP*((slot)/32) + 4*(slot)) + +static MSize CALLBACK_OFS2SLOT(MSize ofs) +{ + MSize group; + ofs -= CALLBACK_MCODE_HEAD; + group = ofs / (32*4 + CALLBACK_MCODE_GROUP); + return (ofs % (32*4 + CALLBACK_MCODE_GROUP))/4 + group*32; +} + +#define CALLBACK_MAX_SLOT \ + (((CALLBACK_MCODE_SIZE-CALLBACK_MCODE_HEAD)/(CALLBACK_MCODE_GROUP+4*32))*32) + +#elif LJ_TARGET_ARM + +#define CALLBACK_MCODE_HEAD 32 +#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot)) +#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8) +#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE)) + +#elif LJ_TARGET_PPC + +#define CALLBACK_MCODE_HEAD 24 +#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot)) +#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8) +#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE)) + +#elif LJ_TARGET_MIPS + +#define CALLBACK_MCODE_HEAD 24 +#define CALLBACK_SLOT2OFS(slot) (CALLBACK_MCODE_HEAD + 8*(slot)) +#define CALLBACK_OFS2SLOT(ofs) (((ofs)-CALLBACK_MCODE_HEAD)/8) +#define CALLBACK_MAX_SLOT (CALLBACK_OFS2SLOT(CALLBACK_MCODE_SIZE)) + +#else + +/* Missing support for this architecture. */ +#define CALLBACK_SLOT2OFS(slot) (0*(slot)) +#define CALLBACK_OFS2SLOT(ofs) (0*(ofs)) +#define CALLBACK_MAX_SLOT 0 + +#endif + +/* Convert callback slot number to callback function pointer. */ +static void *callback_slot2ptr(CTState *cts, MSize slot) +{ + return (uint8_t *)cts->cb.mcode + CALLBACK_SLOT2OFS(slot); +} + +/* Convert callback function pointer to slot number. */ +MSize lj_ccallback_ptr2slot(CTState *cts, void *p) +{ + uintptr_t ofs = (uintptr_t)((uint8_t *)p -(uint8_t *)cts->cb.mcode); + if (ofs < CALLBACK_MCODE_SIZE) { + MSize slot = CALLBACK_OFS2SLOT((MSize)ofs); + if (CALLBACK_SLOT2OFS(slot) == (MSize)ofs) + return slot; + } + return ~0u; /* Not a known callback function pointer. */ +} + +/* Initialize machine code for callback function pointers. */ +#if LJ_OS_NOJIT +/* Disabled callback support. */ +#define callback_mcode_init(g, p) UNUSED(p) +#elif LJ_TARGET_X86ORX64 +static void callback_mcode_init(global_State *g, uint8_t *page) +{ + uint8_t *p = page; + uint8_t *target = (uint8_t *)(void *)lj_vm_ffi_callback; + MSize slot; +#if LJ_64 + *(void **)p = target; p += 8; +#endif + for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { + /* mov al, slot; jmp group */ + *p++ = XI_MOVrib | RID_EAX; *p++ = (uint8_t)slot; + if ((slot & 31) == 31 || slot == CALLBACK_MAX_SLOT-1) { + /* push ebp/rbp; mov ah, slot>>8; mov ebp, &g. */ + *p++ = XI_PUSH + RID_EBP; + *p++ = XI_MOVrib | (RID_EAX+4); *p++ = (uint8_t)(slot >> 8); + *p++ = XI_MOVri | RID_EBP; + *(int32_t *)p = i32ptr(g); p += 4; +#if LJ_64 + /* jmp [rip-pageofs] where lj_vm_ffi_callback is stored. */ + *p++ = XI_GROUP5; *p++ = XM_OFS0 + (XOg_JMP<<3) + RID_EBP; + *(int32_t *)p = (int32_t)(page-(p+4)); p += 4; +#else + /* jmp lj_vm_ffi_callback. */ + *p++ = XI_JMP; *(int32_t *)p = target-(p+4); p += 4; +#endif + } else { + *p++ = XI_JMPs; *p++ = (uint8_t)((2+2)*(31-(slot&31)) - 2); + } + } + lua_assert(p - page <= CALLBACK_MCODE_SIZE); +} +#elif LJ_TARGET_ARM +static void callback_mcode_init(global_State *g, uint32_t *page) +{ + uint32_t *p = page; + void *target = (void *)lj_vm_ffi_callback; + MSize slot; + /* This must match with the saveregs macro in buildvm_arm.dasc. */ + *p++ = ARMI_SUB|ARMF_D(RID_R12)|ARMF_N(RID_R12)|ARMF_M(RID_PC); + *p++ = ARMI_PUSH|ARMF_N(RID_SP)|RSET_RANGE(RID_R4,RID_R11+1)|RID2RSET(RID_LR); + *p++ = ARMI_SUB|ARMI_K12|ARMF_D(RID_R12)|ARMF_N(RID_R12)|CALLBACK_MCODE_HEAD; + *p++ = ARMI_STR|ARMI_LS_P|ARMI_LS_W|ARMF_D(RID_R12)|ARMF_N(RID_SP)|(CFRAME_SIZE-4*9); + *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_R12)|ARMF_N(RID_PC); + *p++ = ARMI_LDR|ARMI_LS_P|ARMI_LS_U|ARMF_D(RID_PC)|ARMF_N(RID_PC); + *p++ = u32ptr(g); + *p++ = u32ptr(target); + for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { + *p++ = ARMI_MOV|ARMF_D(RID_R12)|ARMF_M(RID_PC); + *p = ARMI_B | ((page-p-2) & 0x00ffffffu); + p++; + } + lua_assert(p - page <= CALLBACK_MCODE_SIZE); +} +#elif LJ_TARGET_PPC +static void callback_mcode_init(global_State *g, uint32_t *page) +{ + uint32_t *p = page; + void *target = (void *)lj_vm_ffi_callback; + MSize slot; + *p++ = PPCI_LIS | PPCF_T(RID_TMP) | (u32ptr(target) >> 16); + *p++ = PPCI_LIS | PPCF_T(RID_R12) | (u32ptr(g) >> 16); + *p++ = PPCI_ORI | PPCF_A(RID_TMP)|PPCF_T(RID_TMP) | (u32ptr(target) & 0xffff); + *p++ = PPCI_ORI | PPCF_A(RID_R12)|PPCF_T(RID_R12) | (u32ptr(g) & 0xffff); + *p++ = PPCI_MTCTR | PPCF_T(RID_TMP); + *p++ = PPCI_BCTR; + for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { + *p++ = PPCI_LI | PPCF_T(RID_R11) | slot; + *p = PPCI_B | (((page-p) & 0x00ffffffu) << 2); + p++; + } + lua_assert(p - page <= CALLBACK_MCODE_SIZE); +} +#elif LJ_TARGET_MIPS +static void callback_mcode_init(global_State *g, uint32_t *page) +{ + uint32_t *p = page; + void *target = (void *)lj_vm_ffi_callback; + MSize slot; + *p++ = MIPSI_SW | MIPSF_T(RID_R1)|MIPSF_S(RID_SP) | 0; + *p++ = MIPSI_LUI | MIPSF_T(RID_R3) | (u32ptr(target) >> 16); + *p++ = MIPSI_LUI | MIPSF_T(RID_R2) | (u32ptr(g) >> 16); + *p++ = MIPSI_ORI | MIPSF_T(RID_R3)|MIPSF_S(RID_R3) |(u32ptr(target)&0xffff); + *p++ = MIPSI_JR | MIPSF_S(RID_R3); + *p++ = MIPSI_ORI | MIPSF_T(RID_R2)|MIPSF_S(RID_R2) | (u32ptr(g)&0xffff); + for (slot = 0; slot < CALLBACK_MAX_SLOT; slot++) { + *p = MIPSI_B | ((page-p-1) & 0x0000ffffu); + p++; + *p++ = MIPSI_LI | MIPSF_T(RID_R1) | slot; + } + lua_assert(p - page <= CALLBACK_MCODE_SIZE); +} +#else +/* Missing support for this architecture. */ +#define callback_mcode_init(g, p) UNUSED(p) +#endif + +/* -- Machine code management --------------------------------------------- */ + +#if LJ_TARGET_WINDOWS + +#define WIN32_LEAN_AND_MEAN +#include + +#elif LJ_TARGET_POSIX + +#include +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON +#endif + +#endif + +/* Allocate and initialize area for callback function pointers. */ +static void callback_mcode_new(CTState *cts) +{ + size_t sz = (size_t)CALLBACK_MCODE_SIZE; + void *p; + if (CALLBACK_MAX_SLOT == 0) + lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); +#if LJ_TARGET_WINDOWS + p = VirtualAlloc(NULL, sz, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); + if (!p) + lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); +#elif LJ_TARGET_POSIX + p = mmap(NULL, sz, (PROT_READ|PROT_WRITE), MAP_PRIVATE|MAP_ANONYMOUS, + -1, 0); + if (p == MAP_FAILED) + lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); +#else + /* Fallback allocator. Fails if memory is not executable by default. */ + p = lj_mem_new(cts->L, sz); +#endif + cts->cb.mcode = p; + callback_mcode_init(cts->g, p); + lj_mcode_sync(p, (char *)p + sz); +#if LJ_TARGET_WINDOWS + { + DWORD oprot; + VirtualProtect(p, sz, PAGE_EXECUTE_READ, &oprot); + } +#elif LJ_TARGET_POSIX + mprotect(p, sz, (PROT_READ|PROT_EXEC)); +#endif +} + +/* Free area for callback function pointers. */ +void lj_ccallback_mcode_free(CTState *cts) +{ + size_t sz = (size_t)CALLBACK_MCODE_SIZE; + void *p = cts->cb.mcode; + if (p == NULL) return; +#if LJ_TARGET_WINDOWS + VirtualFree(p, 0, MEM_RELEASE); + UNUSED(sz); +#elif LJ_TARGET_POSIX + munmap(p, sz); +#else + lj_mem_free(cts->g, p, sz); +#endif +} + +/* -- C callback entry ---------------------------------------------------- */ + +/* Target-specific handling of register arguments. Similar to lj_ccall.c. */ +#if LJ_TARGET_X86 + +#define CALLBACK_HANDLE_REGARG \ + if (!isfp) { /* Only non-FP values may be passed in registers. */ \ + if (n > 1) { /* Anything > 32 bit is passed on the stack. */ \ + if (!LJ_ABI_WIN) ngpr = maxgpr; /* Prevent reordering. */ \ + } else if (ngpr + 1 <= maxgpr) { \ + sp = &cts->cb.gpr[ngpr]; \ + ngpr += n; \ + goto done; \ + } \ + } + +#elif LJ_TARGET_X64 && LJ_ABI_WIN + +/* Windows/x64 argument registers are strictly positional (use ngpr). */ +#define CALLBACK_HANDLE_REGARG \ + if (isfp) { \ + if (ngpr < 4) { sp = &cts->cb.fpr[ngpr++]; nfpr = ngpr; goto done; } \ + } else { \ + if (ngpr < 4) { sp = &cts->cb.gpr[ngpr++]; goto done; } \ + } + +#elif LJ_TARGET_X64 + +#define CALLBACK_HANDLE_REGARG \ + if (isfp) { \ + if (nfpr + n <= CCALL_NARG_FPR) { \ + sp = &cts->cb.fpr[nfpr]; \ + nfpr += n; \ + goto done; \ + } \ + } else { \ + if (ngpr + n <= maxgpr) { \ + sp = &cts->cb.gpr[ngpr]; \ + ngpr += n; \ + goto done; \ + } \ + } + +#elif LJ_TARGET_ARM + +#if LJ_ABI_SOFTFP + +#define CALLBACK_HANDLE_REGARG_FP1 UNUSED(isfp); +#define CALLBACK_HANDLE_REGARG_FP2 + +#else + +#define CALLBACK_HANDLE_REGARG_FP1 \ + if (isfp) { \ + if (n == 1) { \ + if (fprodd) { \ + sp = &cts->cb.fpr[fprodd-1]; \ + fprodd = 0; \ + goto done; \ + } else if (nfpr + 1 <= CCALL_NARG_FPR) { \ + sp = &cts->cb.fpr[nfpr++]; \ + fprodd = nfpr; \ + goto done; \ + } \ + } else { \ + if (nfpr + 1 <= CCALL_NARG_FPR) { \ + sp = &cts->cb.fpr[nfpr++]; \ + goto done; \ + } \ + } \ + fprodd = 0; /* No reordering after the first FP value is on stack. */ \ + } else { + +#define CALLBACK_HANDLE_REGARG_FP2 } + +#endif + +#define CALLBACK_HANDLE_REGARG \ + CALLBACK_HANDLE_REGARG_FP1 \ + if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ + if (ngpr + n <= maxgpr) { \ + sp = &cts->cb.gpr[ngpr]; \ + ngpr += n; \ + goto done; \ + } CALLBACK_HANDLE_REGARG_FP2 + +#elif LJ_TARGET_PPC + +#define CALLBACK_HANDLE_REGARG \ + if (isfp) { \ + if (nfpr + 1 <= CCALL_NARG_FPR) { \ + sp = &cts->cb.fpr[nfpr++]; \ + cta = ctype_get(cts, CTID_DOUBLE); /* FPRs always hold doubles. */ \ + goto done; \ + } \ + } else { /* Try to pass argument in GPRs. */ \ + if (n > 1) { \ + lua_assert(ctype_isinteger(cta->info) && n == 2); /* int64_t. */ \ + ngpr = (ngpr + 1u) & ~1u; /* Align int64_t to regpair. */ \ + } \ + if (ngpr + n <= maxgpr) { \ + sp = &cts->cb.gpr[ngpr]; \ + ngpr += n; \ + goto done; \ + } \ + } + +#define CALLBACK_HANDLE_RET \ + if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ + *(double *)dp = *(float *)dp; /* FPRs always hold doubles. */ + +#elif LJ_TARGET_MIPS + +#define CALLBACK_HANDLE_REGARG \ + if (isfp && nfpr < CCALL_NARG_FPR) { /* Try to pass argument in FPRs. */ \ + sp = (void *)((uint8_t *)&cts->cb.fpr[nfpr] + ((LJ_BE && n==1) ? 4 : 0)); \ + nfpr++; ngpr += n; \ + goto done; \ + } else { /* Try to pass argument in GPRs. */ \ + nfpr = CCALL_NARG_FPR; \ + if (n > 1) ngpr = (ngpr + 1u) & ~1u; /* Align to regpair. */ \ + if (ngpr + n <= maxgpr) { \ + sp = &cts->cb.gpr[ngpr]; \ + ngpr += n; \ + goto done; \ + } \ + } + +#define CALLBACK_HANDLE_RET \ + if (ctype_isfp(ctr->info) && ctr->size == sizeof(float)) \ + ((float *)dp)[1] = *(float *)dp; + +#else +#error "Missing calling convention definitions for this architecture" +#endif + +/* Convert and push callback arguments to Lua stack. */ +static void callback_conv_args(CTState *cts, lua_State *L) +{ + TValue *o = L->top; + intptr_t *stack = cts->cb.stack; + MSize slot = cts->cb.slot; + CTypeID id = 0, rid, fid; + CType *ct; + GCfunc *fn; + MSize ngpr = 0, nsp = 0, maxgpr = CCALL_NARG_GPR; +#if CCALL_NARG_FPR + MSize nfpr = 0; +#if LJ_TARGET_ARM + MSize fprodd = 0; +#endif +#endif + + if (slot < cts->cb.sizeid && (id = cts->cb.cbid[slot]) != 0) { + ct = ctype_get(cts, id); + rid = ctype_cid(ct->info); + fn = funcV(lj_tab_getint(cts->miscmap, (int32_t)slot)); + } else { /* Must set up frame first, before throwing the error. */ + ct = NULL; + rid = 0; + fn = (GCfunc *)L; + } + o->u32.lo = LJ_CONT_FFI_CALLBACK; /* Continuation returns from callback. */ + o->u32.hi = rid; /* Return type. x86: +(spadj<<16). */ + o++; + setframe_gc(o, obj2gco(fn)); + setframe_ftsz(o, (int)((char *)(o+1) - (char *)L->base) + FRAME_CONT); + L->top = L->base = ++o; + if (!ct) + lj_err_caller(cts->L, LJ_ERR_FFI_BADCBACK); + if (isluafunc(fn)) + setcframe_pc(L->cframe, proto_bc(funcproto(fn))+1); + lj_state_checkstack(L, LUA_MINSTACK); /* May throw. */ + o = L->base; /* Might have been reallocated. */ + +#if LJ_TARGET_X86 + /* x86 has several different calling conventions. */ + switch (ctype_cconv(ct->info)) { + case CTCC_FASTCALL: maxgpr = 2; break; + case CTCC_THISCALL: maxgpr = 1; break; + default: maxgpr = 0; break; + } +#endif + + fid = ct->sib; + while (fid) { + CType *ctf = ctype_get(cts, fid); + if (!ctype_isattrib(ctf->info)) { + CType *cta; + void *sp; + CTSize sz; + int isfp; + MSize n; + lua_assert(ctype_isfield(ctf->info)); + cta = ctype_rawchild(cts, ctf); + isfp = ctype_isfp(cta->info); + sz = (cta->size + CTSIZE_PTR-1) & ~(CTSIZE_PTR-1); + n = sz / CTSIZE_PTR; /* Number of GPRs or stack slots needed. */ + + CALLBACK_HANDLE_REGARG /* Handle register arguments. */ + + /* Otherwise pass argument on stack. */ + if (CCALL_ALIGN_STACKARG && LJ_32 && sz == 8) + nsp = (nsp + 1) & ~1u; /* Align 64 bit argument on stack. */ + sp = &stack[nsp]; + nsp += n; + + done: + if (LJ_BE && cta->size < CTSIZE_PTR) + sp = (void *)((uint8_t *)sp + CTSIZE_PTR-cta->size); + lj_cconv_tv_ct(cts, cta, 0, o++, sp); + } + fid = ctf->sib; + } + L->top = o; +#if LJ_TARGET_X86 + /* Store stack adjustment for returns from non-cdecl callbacks. */ + if (ctype_cconv(ct->info) != CTCC_CDECL) + (L->base-2)->u32.hi |= (nsp << (16+2)); +#endif +} + +/* Convert Lua object to callback result. */ +static void callback_conv_result(CTState *cts, lua_State *L, TValue *o) +{ + CType *ctr = ctype_raw(cts, (uint16_t)(L->base-2)->u32.hi); +#if LJ_TARGET_X86 + cts->cb.gpr[2] = 0; +#endif + if (!ctype_isvoid(ctr->info)) { + uint8_t *dp = (uint8_t *)&cts->cb.gpr[0]; +#if CCALL_NUM_FPR + if (ctype_isfp(ctr->info)) + dp = (uint8_t *)&cts->cb.fpr[0]; +#endif + lj_cconv_ct_tv(cts, ctr, dp, o, 0); +#ifdef CALLBACK_HANDLE_RET + CALLBACK_HANDLE_RET +#endif + /* Extend returned integers to (at least) 32 bits. */ + if (ctype_isinteger_or_bool(ctr->info) && ctr->size < 4) { + if (ctr->info & CTF_UNSIGNED) + *(uint32_t *)dp = ctr->size == 1 ? (uint32_t)*(uint8_t *)dp : + (uint32_t)*(uint16_t *)dp; + else + *(int32_t *)dp = ctr->size == 1 ? (int32_t)*(int8_t *)dp : + (int32_t)*(int16_t *)dp; + } +#if LJ_TARGET_X86 + if (ctype_isfp(ctr->info)) + cts->cb.gpr[2] = ctr->size == sizeof(float) ? 1 : 2; +#endif + } +} + +/* Enter callback. */ +lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf) +{ + lua_State *L = cts->L; + global_State *g = cts->g; + lua_assert(L != NULL); + if (gcref(g->jit_L)) { + setstrV(L, L->top++, lj_err_str(L, LJ_ERR_FFI_BADCBACK)); + if (g->panic) g->panic(L); + exit(EXIT_FAILURE); + } + lj_trace_abort(g); /* Never record across callback. */ + /* Setup C frame. */ + cframe_prev(cf) = L->cframe; + setcframe_L(cf, L); + cframe_errfunc(cf) = -1; + cframe_nres(cf) = 0; + L->cframe = cf; + callback_conv_args(cts, L); + return L; /* Now call the function on this stack. */ +} + +/* Leave callback. */ +void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o) +{ + lua_State *L = cts->L; + GCfunc *fn; + TValue *obase = L->base; + L->base = L->top; /* Keep continuation frame for throwing errors. */ + if (o >= L->base) { + /* PC of RET* is lost. Point to last line for result conv. errors. */ + fn = curr_func(L); + if (isluafunc(fn)) { + GCproto *pt = funcproto(fn); + setcframe_pc(L->cframe, proto_bc(pt)+pt->sizebc+1); + } + } + callback_conv_result(cts, L, o); + /* Finally drop C frame and continuation frame. */ + L->cframe = cframe_prev(L->cframe); + L->top -= 2; + L->base = obase; + cts->cb.slot = 0; /* Blacklist C function that called the callback. */ +} + +/* -- C callback management ----------------------------------------------- */ + +/* Get an unused slot in the callback slot table. */ +static MSize callback_slot_new(CTState *cts, CType *ct) +{ + CTypeID id = ctype_typeid(cts, ct); + CTypeID1 *cbid = cts->cb.cbid; + MSize top; + for (top = cts->cb.topid; top < cts->cb.sizeid; top++) + if (LJ_LIKELY(cbid[top] == 0)) + goto found; +#if CALLBACK_MAX_SLOT + if (top >= CALLBACK_MAX_SLOT) +#endif + lj_err_caller(cts->L, LJ_ERR_FFI_CBACKOV); + if (!cts->cb.mcode) + callback_mcode_new(cts); + lj_mem_growvec(cts->L, cbid, cts->cb.sizeid, CALLBACK_MAX_SLOT, CTypeID1); + cts->cb.cbid = cbid; + memset(cbid+top, 0, (cts->cb.sizeid-top)*sizeof(CTypeID1)); +found: + cbid[top] = id; + cts->cb.topid = top+1; + return top; +} + +/* Check for function pointer and supported argument/result types. */ +static CType *callback_checkfunc(CTState *cts, CType *ct) +{ + int narg = 0; + if (!ctype_isptr(ct->info) || (LJ_64 && ct->size != CTSIZE_PTR)) + return NULL; + ct = ctype_rawchild(cts, ct); + if (ctype_isfunc(ct->info)) { + CType *ctr = ctype_rawchild(cts, ct); + CTypeID fid = ct->sib; + if (!(ctype_isvoid(ctr->info) || ctype_isenum(ctr->info) || + ctype_isptr(ctr->info) || (ctype_isnum(ctr->info) && ctr->size <= 8))) + return NULL; + if ((ct->info & CTF_VARARG)) + return NULL; + while (fid) { + CType *ctf = ctype_get(cts, fid); + if (!ctype_isattrib(ctf->info)) { + CType *cta; + lua_assert(ctype_isfield(ctf->info)); + cta = ctype_rawchild(cts, ctf); + if (!(ctype_isenum(cta->info) || ctype_isptr(cta->info) || + (ctype_isnum(cta->info) && cta->size <= 8)) || + ++narg >= LUA_MINSTACK-3) + return NULL; + } + fid = ctf->sib; + } + return ct; + } + return NULL; +} + +/* Create a new callback and return the callback function pointer. */ +void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn) +{ + ct = callback_checkfunc(cts, ct); + if (ct) { + MSize slot = callback_slot_new(cts, ct); + GCtab *t = cts->miscmap; + setfuncV(cts->L, lj_tab_setint(cts->L, t, (int32_t)slot), fn); + lj_gc_anybarriert(cts->L, t); + return callback_slot2ptr(cts, slot); + } + return NULL; /* Bad conversion. */ +} + +#endif diff --git a/external/lua/luajit/src/src/lj_ccallback.h b/external/lua/luajit/src/src/lj_ccallback.h new file mode 100644 index 0000000000..ac11d7b66d --- /dev/null +++ b/external/lua/luajit/src/src/lj_ccallback.h @@ -0,0 +1,25 @@ +/* +** FFI C callback handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_CCALLBACK_H +#define _LJ_CCALLBACK_H + +#include "lj_obj.h" +#include "lj_ctype.h" + +#if LJ_HASFFI + +/* Really belongs to lj_vm.h. */ +LJ_ASMF void lj_vm_ffi_callback(void); + +LJ_FUNC MSize lj_ccallback_ptr2slot(CTState *cts, void *p); +LJ_FUNCA lua_State * LJ_FASTCALL lj_ccallback_enter(CTState *cts, void *cf); +LJ_FUNCA void LJ_FASTCALL lj_ccallback_leave(CTState *cts, TValue *o); +LJ_FUNC void *lj_ccallback_new(CTState *cts, CType *ct, GCfunc *fn); +LJ_FUNC void lj_ccallback_mcode_free(CTState *cts); + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_cconv.c b/external/lua/luajit/src/src/lj_cconv.c new file mode 100644 index 0000000000..337382f5f2 --- /dev/null +++ b/external/lua/luajit/src/src/lj_cconv.c @@ -0,0 +1,751 @@ +/* +** C type conversions. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "lj_obj.h" + +#if LJ_HASFFI + +#include "lj_err.h" +#include "lj_tab.h" +#include "lj_ctype.h" +#include "lj_cdata.h" +#include "lj_cconv.h" +#include "lj_ccallback.h" + +/* -- Conversion errors --------------------------------------------------- */ + +/* Bad conversion. */ +LJ_NORET static void cconv_err_conv(CTState *cts, CType *d, CType *s, + CTInfo flags) +{ + const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL)); + const char *src; + if ((flags & CCF_FROMTV)) + src = lj_obj_typename[1+(ctype_isnum(s->info) ? LUA_TNUMBER : + ctype_isarray(s->info) ? LUA_TSTRING : LUA_TNIL)]; + else + src = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, s), NULL)); + if (CCF_GETARG(flags)) + lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst); + else + lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst); +} + +/* Bad conversion from TValue. */ +LJ_NORET static void cconv_err_convtv(CTState *cts, CType *d, TValue *o, + CTInfo flags) +{ + const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL)); + const char *src = lj_typename(o); + if (CCF_GETARG(flags)) + lj_err_argv(cts->L, CCF_GETARG(flags), LJ_ERR_FFI_BADCONV, src, dst); + else + lj_err_callerv(cts->L, LJ_ERR_FFI_BADCONV, src, dst); +} + +/* Initializer overflow. */ +LJ_NORET static void cconv_err_initov(CTState *cts, CType *d) +{ + const char *dst = strdata(lj_ctype_repr(cts->L, ctype_typeid(cts, d), NULL)); + lj_err_callerv(cts->L, LJ_ERR_FFI_INITOV, dst); +} + +/* -- C type compatibility checks ----------------------------------------- */ + +/* Get raw type and qualifiers for a child type. Resolves enums, too. */ +static CType *cconv_childqual(CTState *cts, CType *ct, CTInfo *qual) +{ + ct = ctype_child(cts, ct); + for (;;) { + if (ctype_isattrib(ct->info)) { + if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size; + } else if (!ctype_isenum(ct->info)) { + break; + } + ct = ctype_child(cts, ct); + } + *qual |= (ct->info & CTF_QUAL); + return ct; +} + +/* Check for compatible types when converting to a pointer. +** Note: these checks are more relaxed than what C99 mandates. +*/ +int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags) +{ + if (!((flags & CCF_CAST) || d == s)) { + CTInfo dqual = 0, squal = 0; + d = cconv_childqual(cts, d, &dqual); + if (!ctype_isstruct(s->info)) + s = cconv_childqual(cts, s, &squal); + if ((flags & CCF_SAME)) { + if (dqual != squal) + return 0; /* Different qualifiers. */ + } else if (!(flags & CCF_IGNQUAL)) { + if ((dqual & squal) != squal) + return 0; /* Discarded qualifiers. */ + if (ctype_isvoid(d->info) || ctype_isvoid(s->info)) + return 1; /* Converting to/from void * is always ok. */ + } + if (ctype_type(d->info) != ctype_type(s->info) || + d->size != s->size) + return 0; /* Different type or different size. */ + if (ctype_isnum(d->info)) { + if (((d->info ^ s->info) & (CTF_BOOL|CTF_FP))) + return 0; /* Different numeric types. */ + } else if (ctype_ispointer(d->info)) { + /* Check child types for compatibility. */ + return lj_cconv_compatptr(cts, d, s, flags|CCF_SAME); + } else if (ctype_isstruct(d->info)) { + if (d != s) + return 0; /* Must be exact same type for struct/union. */ + } else if (ctype_isfunc(d->info)) { + /* NYI: structural equality of functions. */ + } + } + return 1; /* Types are compatible. */ +} + +/* -- C type to C type conversion ----------------------------------------- */ + +/* Convert C type to C type. Caveat: expects to get the raw CType! +** +** Note: This is only used by the interpreter and not optimized at all. +** The JIT compiler will do a much better job specializing for each case. +*/ +void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s, + uint8_t *dp, uint8_t *sp, CTInfo flags) +{ + CTSize dsize = d->size, ssize = s->size; + CTInfo dinfo = d->info, sinfo = s->info; + void *tmpptr; + + lua_assert(!ctype_isenum(dinfo) && !ctype_isenum(sinfo)); + lua_assert(!ctype_isattrib(dinfo) && !ctype_isattrib(sinfo)); + + if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT) + goto err_conv; + + /* Some basic sanity checks. */ + lua_assert(!ctype_isnum(dinfo) || dsize > 0); + lua_assert(!ctype_isnum(sinfo) || ssize > 0); + lua_assert(!ctype_isbool(dinfo) || dsize == 1 || dsize == 4); + lua_assert(!ctype_isbool(sinfo) || ssize == 1 || ssize == 4); + lua_assert(!ctype_isinteger(dinfo) || (1u< ssize) { /* Zero-extend or sign-extend LSB. */ +#if LJ_LE + uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[ssize-1]&0x80)) ? 0xff : 0; + memcpy(dp, sp, ssize); + memset(dp + ssize, fill, dsize-ssize); +#else + uint8_t fill = (!(sinfo & CTF_UNSIGNED) && (sp[0]&0x80)) ? 0xff : 0; + memset(dp, fill, dsize-ssize); + memcpy(dp + (dsize-ssize), sp, ssize); +#endif + } else { /* Copy LSB. */ +#if LJ_LE + memcpy(dp, sp, dsize); +#else + memcpy(dp, sp + (ssize-dsize), dsize); +#endif + } + break; + case CCX(I, F): { + double n; /* Always convert via double. */ + conv_I_F: + /* Convert source to double. */ + if (ssize == sizeof(double)) n = *(double *)sp; + else if (ssize == sizeof(float)) n = (double)*(float *)sp; + else goto err_conv; /* NYI: long double. */ + /* Then convert double to integer. */ + /* The conversion must exactly match the semantics of JIT-compiled code! */ + if (dsize < 4 || (dsize == 4 && !(dinfo & CTF_UNSIGNED))) { + int32_t i = (int32_t)n; + if (dsize == 4) *(int32_t *)dp = i; + else if (dsize == 2) *(int16_t *)dp = (int16_t)i; + else *(int8_t *)dp = (int8_t)i; + } else if (dsize == 4) { + *(uint32_t *)dp = (uint32_t)n; + } else if (dsize == 8) { + if (!(dinfo & CTF_UNSIGNED)) + *(int64_t *)dp = (int64_t)n; + else + *(uint64_t *)dp = lj_num2u64(n); + } else { + goto err_conv; /* NYI: conversion to >64 bit integers. */ + } + break; + } + case CCX(I, C): + s = ctype_child(cts, s); + sinfo = s->info; + ssize = s->size; + goto conv_I_F; /* Just convert re. */ + case CCX(I, P): + if (!(flags & CCF_CAST)) goto err_conv; + sinfo = CTINFO(CT_NUM, CTF_UNSIGNED); + goto conv_I_I; + case CCX(I, A): + if (!(flags & CCF_CAST)) goto err_conv; + sinfo = CTINFO(CT_NUM, CTF_UNSIGNED); + ssize = CTSIZE_PTR; + tmpptr = sp; + sp = (uint8_t *)&tmpptr; + goto conv_I_I; + + /* Destination is a floating-point number. */ + case CCX(F, B): + case CCX(F, I): { + double n; /* Always convert via double. */ + conv_F_I: + /* First convert source to double. */ + /* The conversion must exactly match the semantics of JIT-compiled code! */ + if (ssize < 4 || (ssize == 4 && !(sinfo & CTF_UNSIGNED))) { + int32_t i; + if (ssize == 4) { + i = *(int32_t *)sp; + } else if (!(sinfo & CTF_UNSIGNED)) { + if (ssize == 2) i = *(int16_t *)sp; + else i = *(int8_t *)sp; + } else { + if (ssize == 2) i = *(uint16_t *)sp; + else i = *(uint8_t *)sp; + } + n = (double)i; + } else if (ssize == 4) { + n = (double)*(uint32_t *)sp; + } else if (ssize == 8) { + if (!(sinfo & CTF_UNSIGNED)) n = (double)*(int64_t *)sp; + else n = (double)*(uint64_t *)sp; + } else { + goto err_conv; /* NYI: conversion from >64 bit integers. */ + } + /* Convert double to destination. */ + if (dsize == sizeof(double)) *(double *)dp = n; + else if (dsize == sizeof(float)) *(float *)dp = (float)n; + else goto err_conv; /* NYI: long double. */ + break; + } + case CCX(F, F): { + double n; /* Always convert via double. */ + conv_F_F: + if (ssize == dsize) goto copyval; + /* Convert source to double. */ + if (ssize == sizeof(double)) n = *(double *)sp; + else if (ssize == sizeof(float)) n = (double)*(float *)sp; + else goto err_conv; /* NYI: long double. */ + /* Convert double to destination. */ + if (dsize == sizeof(double)) *(double *)dp = n; + else if (dsize == sizeof(float)) *(float *)dp = (float)n; + else goto err_conv; /* NYI: long double. */ + break; + } + case CCX(F, C): + s = ctype_child(cts, s); + sinfo = s->info; + ssize = s->size; + goto conv_F_F; /* Ignore im, and convert from re. */ + + /* Destination is a complex number. */ + case CCX(C, I): + d = ctype_child(cts, d); + dinfo = d->info; + dsize = d->size; + memset(dp + dsize, 0, dsize); /* Clear im. */ + goto conv_F_I; /* Convert to re. */ + case CCX(C, F): + d = ctype_child(cts, d); + dinfo = d->info; + dsize = d->size; + memset(dp + dsize, 0, dsize); /* Clear im. */ + goto conv_F_F; /* Convert to re. */ + + case CCX(C, C): + if (dsize != ssize) { /* Different types: convert re/im separately. */ + CType *dc = ctype_child(cts, d); + CType *sc = ctype_child(cts, s); + lj_cconv_ct_ct(cts, dc, sc, dp, sp, flags); + lj_cconv_ct_ct(cts, dc, sc, dp + dc->size, sp + sc->size, flags); + return; + } + goto copyval; /* Otherwise this is easy. */ + + /* Destination is a vector. */ + case CCX(V, I): + case CCX(V, F): + case CCX(V, C): { + CType *dc = ctype_child(cts, d); + CTSize esize; + /* First convert the scalar to the first element. */ + lj_cconv_ct_ct(cts, dc, s, dp, sp, flags); + /* Then replicate it to the other elements (splat). */ + for (sp = dp, esize = dc->size; dsize > esize; dsize -= esize) { + dp += esize; + memcpy(dp, sp, esize); + } + break; + } + + case CCX(V, V): + /* Copy same-sized vectors, even for different lengths/element-types. */ + if (dsize != ssize) goto err_conv; + goto copyval; + + /* Destination is a pointer. */ + case CCX(P, I): + if (!(flags & CCF_CAST)) goto err_conv; + dinfo = CTINFO(CT_NUM, CTF_UNSIGNED); + goto conv_I_I; + + case CCX(P, F): + if (!(flags & CCF_CAST) || !(flags & CCF_FROMTV)) goto err_conv; + /* The signed conversion is cheaper. x64 really has 47 bit pointers. */ + dinfo = CTINFO(CT_NUM, (LJ_64 && dsize == 8) ? 0 : CTF_UNSIGNED); + goto conv_I_F; + + case CCX(P, P): + if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv; + cdata_setptr(dp, dsize, cdata_getptr(sp, ssize)); + break; + + case CCX(P, A): + case CCX(P, S): + if (!lj_cconv_compatptr(cts, d, s, flags)) goto err_conv; + cdata_setptr(dp, dsize, sp); + break; + + /* Destination is an array. */ + case CCX(A, A): + if ((flags & CCF_CAST) || (d->info & CTF_VLA) || dsize != ssize || + d->size == CTSIZE_INVALID || !lj_cconv_compatptr(cts, d, s, flags)) + goto err_conv; + goto copyval; + + /* Destination is a struct/union. */ + case CCX(S, S): + if ((flags & CCF_CAST) || (d->info & CTF_VLA) || d != s) + goto err_conv; /* Must be exact same type. */ +copyval: /* Copy value. */ + lua_assert(dsize == ssize); + memcpy(dp, sp, dsize); + break; + + default: + err_conv: + cconv_err_conv(cts, d, s, flags); + } +} + +/* -- C type to TValue conversion ----------------------------------------- */ + +/* Convert C type to TValue. Caveat: expects to get the raw CType! */ +int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid, + TValue *o, uint8_t *sp) +{ + CTInfo sinfo = s->info; + if (ctype_isnum(sinfo)) { + if (!ctype_isbool(sinfo)) { + if (ctype_isinteger(sinfo) && s->size > 4) goto copyval; + if (LJ_DUALNUM && ctype_isinteger(sinfo)) { + int32_t i; + lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT32), s, + (uint8_t *)&i, sp, 0); + if ((sinfo & CTF_UNSIGNED) && i < 0) + setnumV(o, (lua_Number)(uint32_t)i); + else + setintV(o, i); + } else { + lj_cconv_ct_ct(cts, ctype_get(cts, CTID_DOUBLE), s, + (uint8_t *)&o->n, sp, 0); + /* Numbers are NOT canonicalized here! Beware of uninitialized data. */ + lua_assert(tvisnum(o)); + } + } else { + uint32_t b = s->size == 1 ? (*sp != 0) : (*(int *)sp != 0); + setboolV(o, b); + setboolV(&cts->g->tmptv2, b); /* Remember for trace recorder. */ + } + return 0; + } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) { + /* Create reference. */ + setcdataV(cts->L, o, lj_cdata_newref(cts, sp, sid)); + return 1; /* Need GC step. */ + } else { + GCcdata *cd; + CTSize sz; + copyval: /* Copy value. */ + sz = s->size; + lua_assert(sz != CTSIZE_INVALID); + /* Attributes are stripped, qualifiers are kept (but mostly ignored). */ + cd = lj_cdata_new(cts, ctype_typeid(cts, s), sz); + setcdataV(cts->L, o, cd); + memcpy(cdataptr(cd), sp, sz); + return 1; /* Need GC step. */ + } +} + +/* Convert bitfield to TValue. */ +int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp) +{ + CTInfo info = s->info; + CTSize pos, bsz; + uint32_t val; + lua_assert(ctype_isbitfield(info)); + /* NYI: packed bitfields may cause misaligned reads. */ + switch (ctype_bitcsz(info)) { + case 4: val = *(uint32_t *)sp; break; + case 2: val = *(uint16_t *)sp; break; + case 1: val = *(uint8_t *)sp; break; + default: lua_assert(0); val = 0; break; + } + /* Check if a packed bitfield crosses a container boundary. */ + pos = ctype_bitpos(info); + bsz = ctype_bitbsz(info); + lua_assert(pos < 8*ctype_bitcsz(info)); + lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info)); + if (pos + bsz > 8*ctype_bitcsz(info)) + lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT); + if (!(info & CTF_BOOL)) { + CTSize shift = 32 - bsz; + if (!(info & CTF_UNSIGNED)) { + setintV(o, (int32_t)(val << (shift-pos)) >> shift); + } else { + val = (val << (shift-pos)) >> shift; + if (!LJ_DUALNUM || (int32_t)val < 0) + setnumV(o, (lua_Number)(uint32_t)val); + else + setintV(o, (int32_t)val); + } + } else { + lua_assert(bsz == 1); + setboolV(o, (val >> pos) & 1); + } + return 0; /* No GC step needed. */ +} + +/* -- TValue to C type conversion ----------------------------------------- */ + +/* Convert table to array. */ +static void cconv_array_tab(CTState *cts, CType *d, + uint8_t *dp, GCtab *t, CTInfo flags) +{ + int32_t i; + CType *dc = ctype_rawchild(cts, d); /* Array element type. */ + CTSize size = d->size, esize = dc->size, ofs = 0; + for (i = 0; ; i++) { + TValue *tv = (TValue *)lj_tab_getint(t, i); + if (!tv || tvisnil(tv)) { + if (i == 0) continue; /* Try again for 1-based tables. */ + break; /* Stop at first nil. */ + } + if (ofs >= size) + cconv_err_initov(cts, d); + lj_cconv_ct_tv(cts, dc, dp + ofs, tv, flags); + ofs += esize; + } + if (size != CTSIZE_INVALID) { /* Only fill up arrays with known size. */ + if (ofs == esize) { /* Replicate a single element. */ + for (; ofs < size; ofs += esize) memcpy(dp + ofs, dp, esize); + } else { /* Otherwise fill the remainder with zero. */ + memset(dp + ofs, 0, size - ofs); + } + } +} + +/* Convert table to sub-struct/union. */ +static void cconv_substruct_tab(CTState *cts, CType *d, uint8_t *dp, + GCtab *t, int32_t *ip, CTInfo flags) +{ + CTypeID id = d->sib; + while (id) { + CType *df = ctype_get(cts, id); + id = df->sib; + if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) { + TValue *tv; + int32_t i = *ip, iz = i; + if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ + if (i >= 0) { + retry: + tv = (TValue *)lj_tab_getint(t, i); + if (!tv || tvisnil(tv)) { + if (i == 0) { i = 1; goto retry; } /* 1-based tables. */ + if (iz == 0) { *ip = i = -1; goto tryname; } /* Init named fields. */ + break; /* Stop at first nil. */ + } + *ip = i + 1; + } else { + tryname: + tv = (TValue *)lj_tab_getstr(t, gco2str(gcref(df->name))); + if (!tv || tvisnil(tv)) continue; + } + if (ctype_isfield(df->info)) + lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, tv, flags); + else + lj_cconv_bf_tv(cts, df, dp+df->size, tv); + if ((d->info & CTF_UNION)) break; + } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) { + cconv_substruct_tab(cts, ctype_rawchild(cts, df), + dp+df->size, t, ip, flags); + } /* Ignore all other entries in the chain. */ + } +} + +/* Convert table to struct/union. */ +static void cconv_struct_tab(CTState *cts, CType *d, + uint8_t *dp, GCtab *t, CTInfo flags) +{ + int32_t i = 0; + memset(dp, 0, d->size); /* Much simpler to clear the struct first. */ + cconv_substruct_tab(cts, d, dp, t, &i, flags); +} + +/* Convert TValue to C type. Caveat: expects to get the raw CType! */ +void lj_cconv_ct_tv(CTState *cts, CType *d, + uint8_t *dp, TValue *o, CTInfo flags) +{ + CTypeID sid = CTID_P_VOID; + CType *s; + void *tmpptr; + uint8_t tmpbool, *sp = (uint8_t *)&tmpptr; + if (LJ_LIKELY(tvisint(o))) { + sp = (uint8_t *)&o->i; + sid = CTID_INT32; + flags |= CCF_FROMTV; + } else if (LJ_LIKELY(tvisnum(o))) { + sp = (uint8_t *)&o->n; + sid = CTID_DOUBLE; + flags |= CCF_FROMTV; + } else if (tviscdata(o)) { + sp = cdataptr(cdataV(o)); + sid = cdataV(o)->ctypeid; + s = ctype_get(cts, sid); + if (ctype_isref(s->info)) { /* Resolve reference for value. */ + lua_assert(s->size == CTSIZE_PTR); + sp = *(void **)sp; + sid = ctype_cid(s->info); + } + s = ctype_raw(cts, sid); + if (ctype_isfunc(s->info)) { + sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR); + } else { + if (ctype_isenum(s->info)) s = ctype_child(cts, s); + goto doconv; + } + } else if (tvisstr(o)) { + GCstr *str = strV(o); + if (ctype_isenum(d->info)) { /* Match string against enum constant. */ + CTSize ofs; + CType *cct = lj_ctype_getfield(cts, d, str, &ofs); + if (!cct || !ctype_isconstval(cct->info)) + goto err_conv; + lua_assert(d->size == 4); + sp = (uint8_t *)&cct->size; + sid = ctype_cid(cct->info); + } else if (ctype_isrefarray(d->info)) { /* Copy string to array. */ + CType *dc = ctype_rawchild(cts, d); + CTSize sz = str->len+1; + if (!ctype_isinteger(dc->info) || dc->size != 1) + goto err_conv; + if (d->size != 0 && d->size < sz) + sz = d->size; + memcpy(dp, strdata(str), sz); + return; + } else { /* Otherwise pass it as a const char[]. */ + sp = (uint8_t *)strdata(str); + sid = CTID_A_CCHAR; + flags |= CCF_FROMTV; + } + } else if (tvistab(o)) { + if (ctype_isarray(d->info)) { + cconv_array_tab(cts, d, dp, tabV(o), flags); + return; + } else if (ctype_isstruct(d->info)) { + cconv_struct_tab(cts, d, dp, tabV(o), flags); + return; + } else { + goto err_conv; + } + } else if (tvisbool(o)) { + tmpbool = boolV(o); + sp = &tmpbool; + sid = CTID_BOOL; + } else if (tvisnil(o)) { + tmpptr = (void *)0; + flags |= CCF_FROMTV; + } else if (tvisudata(o)) { + GCudata *ud = udataV(o); + tmpptr = uddata(ud); + if (ud->udtype == UDTYPE_IO_FILE) + tmpptr = *(void **)tmpptr; + } else if (tvislightud(o)) { + tmpptr = lightudV(o); + } else if (tvisfunc(o)) { + void *p = lj_ccallback_new(cts, d, funcV(o)); + if (p) { + *(void **)dp = p; + return; + } + goto err_conv; + } else { + err_conv: + cconv_err_convtv(cts, d, o, flags); + } + s = ctype_get(cts, sid); +doconv: + if (ctype_isenum(d->info)) d = ctype_child(cts, d); + lj_cconv_ct_ct(cts, d, s, dp, sp, flags); +} + +/* Convert TValue to bitfield. */ +void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o) +{ + CTInfo info = d->info; + CTSize pos, bsz; + uint32_t val, mask; + lua_assert(ctype_isbitfield(info)); + if ((info & CTF_BOOL)) { + uint8_t tmpbool; + lua_assert(ctype_bitbsz(info) == 1); + lj_cconv_ct_tv(cts, ctype_get(cts, CTID_BOOL), &tmpbool, o, 0); + val = tmpbool; + } else { + CTypeID did = (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32; + lj_cconv_ct_tv(cts, ctype_get(cts, did), (uint8_t *)&val, o, 0); + } + pos = ctype_bitpos(info); + bsz = ctype_bitbsz(info); + lua_assert(pos < 8*ctype_bitcsz(info)); + lua_assert(bsz > 0 && bsz <= 8*ctype_bitcsz(info)); + /* Check if a packed bitfield crosses a container boundary. */ + if (pos + bsz > 8*ctype_bitcsz(info)) + lj_err_caller(cts->L, LJ_ERR_FFI_NYIPACKBIT); + mask = ((1u << bsz) - 1u) << pos; + val = (val << pos) & mask; + /* NYI: packed bitfields may cause misaligned reads/writes. */ + switch (ctype_bitcsz(info)) { + case 4: *(uint32_t *)dp = (*(uint32_t *)dp & ~mask) | (uint32_t)val; break; + case 2: *(uint16_t *)dp = (*(uint16_t *)dp & ~mask) | (uint16_t)val; break; + case 1: *(uint8_t *)dp = (*(uint8_t *)dp & ~mask) | (uint8_t)val; break; + default: lua_assert(0); break; + } +} + +/* -- Initialize C type with TValues -------------------------------------- */ + +/* Initialize an array with TValues. */ +static void cconv_array_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp, + TValue *o, MSize len) +{ + CType *dc = ctype_rawchild(cts, d); /* Array element type. */ + CTSize ofs, esize = dc->size; + MSize i; + if (len*esize > sz) + cconv_err_initov(cts, d); + for (i = 0, ofs = 0; i < len; i++, ofs += esize) + lj_cconv_ct_tv(cts, dc, dp + ofs, o + i, 0); + if (ofs == esize) { /* Replicate a single element. */ + for (; ofs < sz; ofs += esize) memcpy(dp + ofs, dp, esize); + } else { /* Otherwise fill the remainder with zero. */ + memset(dp + ofs, 0, sz - ofs); + } +} + +/* Initialize a sub-struct/union with TValues. */ +static void cconv_substruct_init(CTState *cts, CType *d, uint8_t *dp, + TValue *o, MSize len, MSize *ip) +{ + CTypeID id = d->sib; + while (id) { + CType *df = ctype_get(cts, id); + id = df->sib; + if (ctype_isfield(df->info) || ctype_isbitfield(df->info)) { + MSize i = *ip; + if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ + if (i >= len) break; + *ip = i + 1; + if (ctype_isfield(df->info)) + lj_cconv_ct_tv(cts, ctype_rawchild(cts, df), dp+df->size, o + i, 0); + else + lj_cconv_bf_tv(cts, df, dp+df->size, o + i); + if ((d->info & CTF_UNION)) break; + } else if (ctype_isxattrib(df->info, CTA_SUBTYPE)) { + cconv_substruct_init(cts, ctype_rawchild(cts, df), + dp+df->size, o, len, ip); + } /* Ignore all other entries in the chain. */ + } +} + +/* Initialize a struct/union with TValues. */ +static void cconv_struct_init(CTState *cts, CType *d, CTSize sz, uint8_t *dp, + TValue *o, MSize len) +{ + MSize i = 0; + memset(dp, 0, sz); /* Much simpler to clear the struct first. */ + cconv_substruct_init(cts, d, dp, o, len, &i); + if (i < len) + cconv_err_initov(cts, d); +} + +/* Check whether to use a multi-value initializer. +** This is true if an aggregate is to be initialized with a value. +** Valarrays are treated as values here so ct_tv handles (V|C, I|F). +*/ +int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o) +{ + if (!(ctype_isrefarray(d->info) || ctype_isstruct(d->info))) + return 0; /* Destination is not an aggregate. */ + if (tvistab(o) || (tvisstr(o) && !ctype_isstruct(d->info))) + return 0; /* Initializer is not a value. */ + if (tviscdata(o) && lj_ctype_rawref(cts, cdataV(o)->ctypeid) == d) + return 0; /* Source and destination are identical aggregates. */ + return 1; /* Otherwise the initializer is a value. */ +} + +/* Initialize C type with TValues. Caveat: expects to get the raw CType! */ +void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz, + uint8_t *dp, TValue *o, MSize len) +{ + if (len == 0) + memset(dp, 0, sz); + else if (len == 1 && !lj_cconv_multi_init(cts, d, o)) + lj_cconv_ct_tv(cts, d, dp, o, 0); + else if (ctype_isarray(d->info)) /* Also handles valarray init with len>1. */ + cconv_array_init(cts, d, sz, dp, o, len); + else if (ctype_isstruct(d->info)) + cconv_struct_init(cts, d, sz, dp, o, len); + else + cconv_err_initov(cts, d); +} + +#endif diff --git a/external/lua/luajit/src/src/lj_cconv.h b/external/lua/luajit/src/src/lj_cconv.h new file mode 100644 index 0000000000..5e95d2e930 --- /dev/null +++ b/external/lua/luajit/src/src/lj_cconv.h @@ -0,0 +1,70 @@ +/* +** C type conversions. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_CCONV_H +#define _LJ_CCONV_H + +#include "lj_obj.h" +#include "lj_ctype.h" + +#if LJ_HASFFI + +/* Compressed C type index. ORDER CCX. */ +enum { + CCX_B, /* Bool. */ + CCX_I, /* Integer. */ + CCX_F, /* Floating-point number. */ + CCX_C, /* Complex. */ + CCX_V, /* Vector. */ + CCX_P, /* Pointer. */ + CCX_A, /* Refarray. */ + CCX_S /* Struct/union. */ +}; + +/* Convert C type info to compressed C type index. ORDER CT. ORDER CCX. */ +static LJ_AINLINE uint32_t cconv_idx(CTInfo info) +{ + uint32_t idx = ((info >> 26) & 15u); /* Dispatch bits. */ + lua_assert(ctype_type(info) <= CT_MAYCONVERT); +#if LJ_64 + idx = ((U64x(f436fff5,fff7f021) >> 4*idx) & 15u); +#else + idx = (((idx < 8 ? 0xfff7f021u : 0xf436fff5) >> 4*(idx & 7u)) & 15u); +#endif + lua_assert(idx < 8); + return idx; +} + +#define cconv_idx2(dinfo, sinfo) \ + ((cconv_idx((dinfo)) << 3) + cconv_idx((sinfo))) + +#define CCX(dst, src) ((CCX_##dst << 3) + CCX_##src) + +/* Conversion flags. */ +#define CCF_CAST 0x00000001u +#define CCF_FROMTV 0x00000002u +#define CCF_SAME 0x00000004u +#define CCF_IGNQUAL 0x00000008u + +#define CCF_ARG_SHIFT 8 +#define CCF_ARG(n) ((n) << CCF_ARG_SHIFT) +#define CCF_GETARG(f) ((f) >> CCF_ARG_SHIFT) + +LJ_FUNC int lj_cconv_compatptr(CTState *cts, CType *d, CType *s, CTInfo flags); +LJ_FUNC void lj_cconv_ct_ct(CTState *cts, CType *d, CType *s, + uint8_t *dp, uint8_t *sp, CTInfo flags); +LJ_FUNC int lj_cconv_tv_ct(CTState *cts, CType *s, CTypeID sid, + TValue *o, uint8_t *sp); +LJ_FUNC int lj_cconv_tv_bf(CTState *cts, CType *s, TValue *o, uint8_t *sp); +LJ_FUNC void lj_cconv_ct_tv(CTState *cts, CType *d, + uint8_t *dp, TValue *o, CTInfo flags); +LJ_FUNC void lj_cconv_bf_tv(CTState *cts, CType *d, uint8_t *dp, TValue *o); +LJ_FUNC int lj_cconv_multi_init(CTState *cts, CType *d, TValue *o); +LJ_FUNC void lj_cconv_ct_init(CTState *cts, CType *d, CTSize sz, + uint8_t *dp, TValue *o, MSize len); + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_cdata.c b/external/lua/luajit/src/src/lj_cdata.c new file mode 100644 index 0000000000..10f4809c11 --- /dev/null +++ b/external/lua/luajit/src/src/lj_cdata.c @@ -0,0 +1,285 @@ +/* +** C data management. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "lj_obj.h" + +#if LJ_HASFFI + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_ctype.h" +#include "lj_cconv.h" +#include "lj_cdata.h" + +/* -- C data allocation --------------------------------------------------- */ + +/* Allocate a new C data object holding a reference to another object. */ +GCcdata *lj_cdata_newref(CTState *cts, const void *p, CTypeID id) +{ + CTypeID refid = lj_ctype_intern(cts, CTINFO_REF(id), CTSIZE_PTR); + GCcdata *cd = lj_cdata_new(cts, refid, CTSIZE_PTR); + *(const void **)cdataptr(cd) = p; + return cd; +} + +/* Allocate variable-sized or specially aligned C data object. */ +GCcdata *lj_cdata_newv(CTState *cts, CTypeID id, CTSize sz, CTSize align) +{ + global_State *g; + MSize extra = sizeof(GCcdataVar) + sizeof(GCcdata) + + (align > CT_MEMALIGN ? (1u<L, extra + sz, char); + uintptr_t adata = (uintptr_t)p + sizeof(GCcdataVar) + sizeof(GCcdata); + uintptr_t almask = (1u << align) - 1u; + GCcdata *cd = (GCcdata *)(((adata + almask) & ~almask) - sizeof(GCcdata)); + lua_assert((char *)cd - p < 65536); + cdatav(cd)->offset = (uint16_t)((char *)cd - p); + cdatav(cd)->extra = extra; + cdatav(cd)->len = sz; + g = cts->g; + setgcrefr(cd->nextgc, g->gc.root); + setgcref(g->gc.root, obj2gco(cd)); + newwhite(g, obj2gco(cd)); + cd->marked |= 0x80; + cd->gct = ~LJ_TCDATA; + cd->ctypeid = id; + return cd; +} + +/* Free a C data object. */ +void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd) +{ + if (LJ_UNLIKELY(cd->marked & LJ_GC_CDATA_FIN)) { + GCobj *root; + makewhite(g, obj2gco(cd)); + markfinalized(obj2gco(cd)); + if ((root = gcref(g->gc.mmudata)) != NULL) { + setgcrefr(cd->nextgc, root->gch.nextgc); + setgcref(root->gch.nextgc, obj2gco(cd)); + setgcref(g->gc.mmudata, obj2gco(cd)); + } else { + setgcref(cd->nextgc, obj2gco(cd)); + setgcref(g->gc.mmudata, obj2gco(cd)); + } + } else if (LJ_LIKELY(!cdataisv(cd))) { + CType *ct = ctype_raw(ctype_ctsG(g), cd->ctypeid); + CTSize sz = ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR; + lua_assert(ctype_hassize(ct->info) || ctype_isfunc(ct->info) || + ctype_isextern(ct->info)); + lj_mem_free(g, cd, sizeof(GCcdata) + sz); + } else { + lj_mem_free(g, memcdatav(cd), sizecdatav(cd)); + } +} + +TValue * LJ_FASTCALL lj_cdata_setfin(lua_State *L, GCcdata *cd) +{ + global_State *g = G(L); + GCtab *t = ctype_ctsG(g)->finalizer; + if (gcref(t->metatable)) { + /* Add cdata to finalizer table, if still enabled. */ + TValue *tv, tmp; + setcdataV(L, &tmp, cd); + lj_gc_anybarriert(L, t); + tv = lj_tab_set(L, t, &tmp); + cd->marked |= LJ_GC_CDATA_FIN; + return tv; + } else { + /* Otherwise return dummy TValue. */ + return &g->tmptv; + } +} + +/* -- C data indexing ----------------------------------------------------- */ + +/* Index C data by a TValue. Return CType and pointer. */ +CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp, + CTInfo *qual) +{ + uint8_t *p = (uint8_t *)cdataptr(cd); + CType *ct = ctype_get(cts, cd->ctypeid); + ptrdiff_t idx; + + /* Resolve reference for cdata object. */ + if (ctype_isref(ct->info)) { + lua_assert(ct->size == CTSIZE_PTR); + p = *(uint8_t **)p; + ct = ctype_child(cts, ct); + } + +collect_attrib: + /* Skip attributes and collect qualifiers. */ + while (ctype_isattrib(ct->info)) { + if (ctype_attrib(ct->info) == CTA_QUAL) *qual |= ct->size; + ct = ctype_child(cts, ct); + } + lua_assert(!ctype_isref(ct->info)); /* Interning rejects refs to refs. */ + + if (tvisint(key)) { + idx = (ptrdiff_t)intV(key); + goto integer_key; + } else if (tvisnum(key)) { /* Numeric key. */ + idx = LJ_64 ? (ptrdiff_t)numV(key) : (ptrdiff_t)lj_num2int(numV(key)); + integer_key: + if (ctype_ispointer(ct->info)) { + CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */ + if (sz != CTSIZE_INVALID) { + if (ctype_isptr(ct->info)) { + p = (uint8_t *)cdata_getptr(p, ct->size); + } else if ((ct->info & (CTF_VECTOR|CTF_COMPLEX))) { + if ((ct->info & CTF_COMPLEX)) idx &= 1; + *qual |= CTF_CONST; /* Valarray elements are constant. */ + } + *pp = p + idx*(int32_t)sz; + return ct; + } + } + } else if (tviscdata(key)) { /* Integer cdata key. */ + GCcdata *cdk = cdataV(key); + CType *ctk = ctype_raw(cts, cdk->ctypeid); + if (ctype_isenum(ctk->info)) ctk = ctype_child(cts, ctk); + if (ctype_isinteger(ctk->info)) { + lj_cconv_ct_ct(cts, ctype_get(cts, CTID_INT_PSZ), ctk, + (uint8_t *)&idx, cdataptr(cdk), 0); + goto integer_key; + } + } else if (tvisstr(key)) { /* String key. */ + GCstr *name = strV(key); + if (ctype_isstruct(ct->info)) { + CTSize ofs; + CType *fct = lj_ctype_getfieldq(cts, ct, name, &ofs, qual); + if (fct) { + *pp = p + ofs; + return fct; + } + } else if (ctype_iscomplex(ct->info)) { + if (name->len == 2) { + *qual |= CTF_CONST; /* Complex fields are constant. */ + if (strdata(name)[0] == 'r' && strdata(name)[1] == 'e') { + *pp = p; + return ct; + } else if (strdata(name)[0] == 'i' && strdata(name)[1] == 'm') { + *pp = p + (ct->size >> 1); + return ct; + } + } + } else if (cd->ctypeid == CTID_CTYPEID) { + /* Allow indexing a (pointer to) struct constructor to get constants. */ + CType *sct = ctype_raw(cts, *(CTypeID *)p); + if (ctype_isptr(sct->info)) + sct = ctype_rawchild(cts, sct); + if (ctype_isstruct(sct->info)) { + CTSize ofs; + CType *fct = lj_ctype_getfield(cts, sct, name, &ofs); + if (fct && ctype_isconstval(fct->info)) + return fct; + } + ct = sct; /* Allow resolving metamethods for constructors, too. */ + } + } + if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */ + if (ctype_isstruct(ctype_rawchild(cts, ct)->info)) { + p = (uint8_t *)cdata_getptr(p, ct->size); + ct = ctype_child(cts, ct); + goto collect_attrib; + } + } + *qual |= 1; /* Lookup failed. */ + return ct; /* But return the resolved raw type. */ +} + +/* -- C data getters ------------------------------------------------------ */ + +/* Get constant value and convert to TValue. */ +static void cdata_getconst(CTState *cts, TValue *o, CType *ct) +{ + CType *ctt = ctype_child(cts, ct); + lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4); + /* Constants are already zero-extended/sign-extended to 32 bits. */ + if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0) + setnumV(o, (lua_Number)(uint32_t)ct->size); + else + setintV(o, (int32_t)ct->size); +} + +/* Get C data value and convert to TValue. */ +int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp) +{ + CTypeID sid; + + if (ctype_isconstval(s->info)) { + cdata_getconst(cts, o, s); + return 0; /* No GC step needed. */ + } else if (ctype_isbitfield(s->info)) { + return lj_cconv_tv_bf(cts, s, o, sp); + } + + /* Get child type of pointer/array/field. */ + lua_assert(ctype_ispointer(s->info) || ctype_isfield(s->info)); + sid = ctype_cid(s->info); + s = ctype_get(cts, sid); + + /* Resolve reference for field. */ + if (ctype_isref(s->info)) { + lua_assert(s->size == CTSIZE_PTR); + sp = *(uint8_t **)sp; + sid = ctype_cid(s->info); + s = ctype_get(cts, sid); + } + + /* Skip attributes. */ + while (ctype_isattrib(s->info)) + s = ctype_child(cts, s); + + return lj_cconv_tv_ct(cts, s, sid, o, sp); +} + +/* -- C data setters ------------------------------------------------------ */ + +/* Convert TValue and set C data value. */ +void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, CTInfo qual) +{ + if (ctype_isconstval(d->info)) { + goto err_const; + } else if (ctype_isbitfield(d->info)) { + if (((d->info|qual) & CTF_CONST)) goto err_const; + lj_cconv_bf_tv(cts, d, dp, o); + return; + } + + /* Get child type of pointer/array/field. */ + lua_assert(ctype_ispointer(d->info) || ctype_isfield(d->info)); + d = ctype_child(cts, d); + + /* Resolve reference for field. */ + if (ctype_isref(d->info)) { + lua_assert(d->size == CTSIZE_PTR); + dp = *(uint8_t **)dp; + d = ctype_child(cts, d); + } + + /* Skip attributes and collect qualifiers. */ + for (;;) { + if (ctype_isattrib(d->info)) { + if (ctype_attrib(d->info) == CTA_QUAL) qual |= d->size; + } else { + break; + } + d = ctype_child(cts, d); + } + + lua_assert(ctype_hassize(d->info) && !ctype_isvoid(d->info)); + + if (((d->info|qual) & CTF_CONST)) { + err_const: + lj_err_caller(cts->L, LJ_ERR_FFI_WRCONST); + } + + lj_cconv_ct_tv(cts, d, dp, o, 0); +} + +#endif diff --git a/external/lua/luajit/src/src/lj_cdata.h b/external/lua/luajit/src/src/lj_cdata.h new file mode 100644 index 0000000000..0c81b02b7f --- /dev/null +++ b/external/lua/luajit/src/src/lj_cdata.h @@ -0,0 +1,75 @@ +/* +** C data management. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_CDATA_H +#define _LJ_CDATA_H + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_ctype.h" + +#if LJ_HASFFI + +/* Get C data pointer. */ +static LJ_AINLINE void *cdata_getptr(void *p, CTSize sz) +{ + if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */ + return ((void *)(uintptr_t)*(uint32_t *)p); + } else { + lua_assert(sz == CTSIZE_PTR); + return *(void **)p; + } +} + +/* Set C data pointer. */ +static LJ_AINLINE void cdata_setptr(void *p, CTSize sz, const void *v) +{ + if (LJ_64 && sz == 4) { /* Support 32 bit pointers on 64 bit targets. */ + *(uint32_t *)p = (uint32_t)(uintptr_t)v; + } else { + lua_assert(sz == CTSIZE_PTR); + *(void **)p = (void *)v; + } +} + +/* Allocate fixed-size C data object. */ +static LJ_AINLINE GCcdata *lj_cdata_new(CTState *cts, CTypeID id, CTSize sz) +{ + GCcdata *cd; +#ifdef LUA_USE_ASSERT + CType *ct = ctype_raw(cts, id); + lua_assert((ctype_hassize(ct->info) ? ct->size : CTSIZE_PTR) == sz); +#endif + cd = (GCcdata *)lj_mem_newgco(cts->L, sizeof(GCcdata) + sz); + cd->gct = ~LJ_TCDATA; + cd->ctypeid = ctype_check(cts, id); + return cd; +} + +/* Variant which works without a valid CTState. */ +static LJ_AINLINE GCcdata *lj_cdata_new_(lua_State *L, CTypeID id, CTSize sz) +{ + GCcdata *cd = (GCcdata *)lj_mem_newgco(L, sizeof(GCcdata) + sz); + cd->gct = ~LJ_TCDATA; + cd->ctypeid = id; + return cd; +} + +LJ_FUNC GCcdata *lj_cdata_newref(CTState *cts, const void *pp, CTypeID id); +LJ_FUNC GCcdata *lj_cdata_newv(CTState *cts, CTypeID id, CTSize sz, + CTSize align); + +LJ_FUNC void LJ_FASTCALL lj_cdata_free(global_State *g, GCcdata *cd); +LJ_FUNCA TValue * LJ_FASTCALL lj_cdata_setfin(lua_State *L, GCcdata *cd); + +LJ_FUNC CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, + uint8_t **pp, CTInfo *qual); +LJ_FUNC int lj_cdata_get(CTState *cts, CType *s, TValue *o, uint8_t *sp); +LJ_FUNC void lj_cdata_set(CTState *cts, CType *d, uint8_t *dp, TValue *o, + CTInfo qual); + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_char.c b/external/lua/luajit/src/src/lj_char.c new file mode 100644 index 0000000000..11f23efe44 --- /dev/null +++ b/external/lua/luajit/src/src/lj_char.c @@ -0,0 +1,43 @@ +/* +** Character types. +** Donated to the public domain. +** +** This is intended to replace the problematic libc single-byte NLS functions. +** These just don't make sense anymore with UTF-8 locales becoming the norm +** on POSIX systems. It never worked too well on Windows systems since hardly +** anyone bothered to call setlocale(). +** +** This table is hardcoded for ASCII. Identifiers include the characters +** 128-255, too. This allows for the use of all non-ASCII chars as identifiers +** in the lexer. This is a broad definition, but works well in practice +** for both UTF-8 locales and most single-byte locales (such as ISO-8859-*). +** +** If you really need proper character types for UTF-8 strings, please use +** an add-on library such as slnunicode: http://luaforge.net/projects/sln/ +*/ + +#define lj_char_c +#define LUA_CORE + +#include "lj_char.h" + +LJ_DATADEF const uint8_t lj_char_bits[257] = { + 0, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 152,152,152,152,152,152,152,152,152,152, 4, 4, 4, 4, 4, 4, + 4,176,176,176,176,176,176,160,160,160,160,160,160,160,160,160, + 160,160,160,160,160,160,160,160,160,160,160, 4, 4, 4, 4,132, + 4,208,208,208,208,208,208,192,192,192,192,192,192,192,192,192, + 192,192,192,192,192,192,192,192,192,192,192, 4, 4, 4, 4, 1, + 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, + 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, + 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, + 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, + 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, + 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, + 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128, + 128,128,128,128,128,128,128,128,128,128,128,128,128,128,128,128 +}; + diff --git a/external/lua/luajit/src/src/lj_char.h b/external/lua/luajit/src/src/lj_char.h new file mode 100644 index 0000000000..c3c86d34b1 --- /dev/null +++ b/external/lua/luajit/src/src/lj_char.h @@ -0,0 +1,42 @@ +/* +** Character types. +** Donated to the public domain. +*/ + +#ifndef _LJ_CHAR_H +#define _LJ_CHAR_H + +#include "lj_def.h" + +#define LJ_CHAR_CNTRL 0x01 +#define LJ_CHAR_SPACE 0x02 +#define LJ_CHAR_PUNCT 0x04 +#define LJ_CHAR_DIGIT 0x08 +#define LJ_CHAR_XDIGIT 0x10 +#define LJ_CHAR_UPPER 0x20 +#define LJ_CHAR_LOWER 0x40 +#define LJ_CHAR_IDENT 0x80 +#define LJ_CHAR_ALPHA (LJ_CHAR_LOWER|LJ_CHAR_UPPER) +#define LJ_CHAR_ALNUM (LJ_CHAR_ALPHA|LJ_CHAR_DIGIT) +#define LJ_CHAR_GRAPH (LJ_CHAR_ALNUM|LJ_CHAR_PUNCT) + +/* Only pass -1 or 0..255 to these macros. Never pass a signed char! */ +#define lj_char_isa(c, t) ((lj_char_bits+1)[(c)] & t) +#define lj_char_iscntrl(c) lj_char_isa((c), LJ_CHAR_CNTRL) +#define lj_char_isspace(c) lj_char_isa((c), LJ_CHAR_SPACE) +#define lj_char_ispunct(c) lj_char_isa((c), LJ_CHAR_PUNCT) +#define lj_char_isdigit(c) lj_char_isa((c), LJ_CHAR_DIGIT) +#define lj_char_isxdigit(c) lj_char_isa((c), LJ_CHAR_XDIGIT) +#define lj_char_isupper(c) lj_char_isa((c), LJ_CHAR_UPPER) +#define lj_char_islower(c) lj_char_isa((c), LJ_CHAR_LOWER) +#define lj_char_isident(c) lj_char_isa((c), LJ_CHAR_IDENT) +#define lj_char_isalpha(c) lj_char_isa((c), LJ_CHAR_ALPHA) +#define lj_char_isalnum(c) lj_char_isa((c), LJ_CHAR_ALNUM) +#define lj_char_isgraph(c) lj_char_isa((c), LJ_CHAR_GRAPH) + +#define lj_char_toupper(c) ((c) - (lj_char_islower(c) >> 1)) +#define lj_char_tolower(c) ((c) + lj_char_isupper(c)) + +LJ_DATA const uint8_t lj_char_bits[257]; + +#endif diff --git a/external/lua/luajit/src/src/lj_clib.c b/external/lua/luajit/src/src/lj_clib.c new file mode 100644 index 0000000000..23d1f18224 --- /dev/null +++ b/external/lua/luajit/src/src/lj_clib.c @@ -0,0 +1,412 @@ +/* +** FFI C library loader. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "lj_obj.h" + +#if LJ_HASFFI + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_tab.h" +#include "lj_str.h" +#include "lj_udata.h" +#include "lj_ctype.h" +#include "lj_cconv.h" +#include "lj_cdata.h" +#include "lj_clib.h" + +/* -- OS-specific functions ----------------------------------------------- */ + +#if LJ_TARGET_DLOPEN + +#include +#include + +#if defined(RTLD_DEFAULT) +#define CLIB_DEFHANDLE RTLD_DEFAULT +#elif LJ_TARGET_OSX || LJ_TARGET_BSD +#define CLIB_DEFHANDLE ((void *)(intptr_t)-2) +#else +#define CLIB_DEFHANDLE NULL +#endif + +LJ_NORET LJ_NOINLINE static void clib_error_(lua_State *L) +{ + lj_err_callermsg(L, dlerror()); +} + +#define clib_error(L, fmt, name) clib_error_(L) + +#if defined(__CYGWIN__) +#define CLIB_SOPREFIX "cyg" +#else +#define CLIB_SOPREFIX "lib" +#endif + +#if LJ_TARGET_OSX +#define CLIB_SOEXT "%s.dylib" +#elif defined(__CYGWIN__) +#define CLIB_SOEXT "%s.dll" +#else +#define CLIB_SOEXT "%s.so" +#endif + +static const char *clib_extname(lua_State *L, const char *name) +{ + if (!strchr(name, '/') +#ifdef __CYGWIN__ + && !strchr(name, '\\') +#endif + ) { + if (!strchr(name, '.')) { + name = lj_str_pushf(L, CLIB_SOEXT, name); + L->top--; +#ifdef __CYGWIN__ + } else { + return name; +#endif + } + if (!(name[0] == CLIB_SOPREFIX[0] && name[1] == CLIB_SOPREFIX[1] && + name[2] == CLIB_SOPREFIX[2])) { + name = lj_str_pushf(L, CLIB_SOPREFIX "%s", name); + L->top--; + } + } + return name; +} + +/* Check for a recognized ld script line. */ +static const char *clib_check_lds(lua_State *L, const char *buf) +{ + char *p, *e; + if ((!strncmp(buf, "GROUP", 5) || !strncmp(buf, "INPUT", 5)) && + (p = strchr(buf, '('))) { + while (*++p == ' ') ; + for (e = p; *e && *e != ' ' && *e != ')'; e++) ; + return strdata(lj_str_new(L, p, e-p)); + } + return NULL; +} + +/* Quick and dirty solution to resolve shared library name from ld script. */ +static const char *clib_resolve_lds(lua_State *L, const char *name) +{ + FILE *fp = fopen(name, "r"); + const char *p = NULL; + if (fp) { + char buf[256]; + if (fgets(buf, sizeof(buf), fp)) { + if (!strncmp(buf, "/* GNU ld script", 16)) { /* ld script magic? */ + while (fgets(buf, sizeof(buf), fp)) { /* Check all lines. */ + p = clib_check_lds(L, buf); + if (p) break; + } + } else { /* Otherwise check only the first line. */ + p = clib_check_lds(L, buf); + } + } + fclose(fp); + } + return p; +} + +static void *clib_loadlib(lua_State *L, const char *name, int global) +{ + void *h = dlopen(clib_extname(L, name), + RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL)); + if (!h) { + const char *e, *err = dlerror(); + if (*err == '/' && (e = strchr(err, ':')) && + (name = clib_resolve_lds(L, strdata(lj_str_new(L, err, e-err))))) { + h = dlopen(name, RTLD_LAZY | (global?RTLD_GLOBAL:RTLD_LOCAL)); + if (h) return h; + err = dlerror(); + } + lj_err_callermsg(L, err); + } + return h; +} + +static void clib_unloadlib(CLibrary *cl) +{ + if (cl->handle && cl->handle != CLIB_DEFHANDLE) + dlclose(cl->handle); +} + +static void *clib_getsym(CLibrary *cl, const char *name) +{ + void *p = dlsym(cl->handle, name); + return p; +} + +#elif LJ_TARGET_WINDOWS + +#define WIN32_LEAN_AND_MEAN +#ifndef WINVER +#define WINVER 0x0500 +#endif +#include + +#ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS +#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4 +#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2 +BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*); +#endif + +#define CLIB_DEFHANDLE ((void *)-1) + +/* Default libraries. */ +enum { + CLIB_HANDLE_EXE, + CLIB_HANDLE_DLL, + CLIB_HANDLE_CRT, + CLIB_HANDLE_KERNEL32, + CLIB_HANDLE_USER32, + CLIB_HANDLE_GDI32, + CLIB_HANDLE_MAX +}; + +static void *clib_def_handle[CLIB_HANDLE_MAX]; + +LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt, + const char *name) +{ + DWORD err = GetLastError(); + char buf[128]; + if (!FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM, + NULL, err, 0, buf, sizeof(buf), NULL)) + buf[0] = '\0'; + lj_err_callermsg(L, lj_str_pushf(L, fmt, name, buf)); +} + +static int clib_needext(const char *s) +{ + while (*s) { + if (*s == '/' || *s == '\\' || *s == '.') return 0; + s++; + } + return 1; +} + +static const char *clib_extname(lua_State *L, const char *name) +{ + if (clib_needext(name)) { + name = lj_str_pushf(L, "%s.dll", name); + L->top--; + } + return name; +} + +static void *clib_loadlib(lua_State *L, const char *name, int global) +{ + DWORD oldwerr = GetLastError(); + void *h = (void *)LoadLibraryA(clib_extname(L, name)); + if (!h) clib_error(L, "cannot load module " LUA_QS ": %s", name); + SetLastError(oldwerr); + UNUSED(global); + return h; +} + +static void clib_unloadlib(CLibrary *cl) +{ + if (cl->handle == CLIB_DEFHANDLE) { + MSize i; + for (i = CLIB_HANDLE_KERNEL32; i < CLIB_HANDLE_MAX; i++) { + void *h = clib_def_handle[i]; + if (h) { + clib_def_handle[i] = NULL; + FreeLibrary((HINSTANCE)h); + } + } + } else if (!cl->handle) { + FreeLibrary((HINSTANCE)cl->handle); + } +} + +static void *clib_getsym(CLibrary *cl, const char *name) +{ + void *p = NULL; + if (cl->handle == CLIB_DEFHANDLE) { /* Search default libraries. */ + MSize i; + for (i = 0; i < CLIB_HANDLE_MAX; i++) { + HINSTANCE h = (HINSTANCE)clib_def_handle[i]; + if (!(void *)h) { /* Resolve default library handles (once). */ + switch (i) { + case CLIB_HANDLE_EXE: GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, &h); break; + case CLIB_HANDLE_DLL: + GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (const char *)clib_def_handle, &h); + break; + case CLIB_HANDLE_CRT: + GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (const char *)&_fmode, &h); + break; + case CLIB_HANDLE_KERNEL32: h = LoadLibraryA("kernel32.dll"); break; + case CLIB_HANDLE_USER32: h = LoadLibraryA("user32.dll"); break; + case CLIB_HANDLE_GDI32: h = LoadLibraryA("gdi32.dll"); break; + } + if (!h) continue; + clib_def_handle[i] = (void *)h; + } + p = (void *)GetProcAddress(h, name); + if (p) break; + } + } else { + p = (void *)GetProcAddress((HINSTANCE)cl->handle, name); + } + return p; +} + +#else + +#define CLIB_DEFHANDLE NULL + +LJ_NORET LJ_NOINLINE static void clib_error(lua_State *L, const char *fmt, + const char *name) +{ + lj_err_callermsg(L, lj_str_pushf(L, fmt, name, "no support for this OS")); +} + +static void *clib_loadlib(lua_State *L, const char *name, int global) +{ + lj_err_callermsg(L, "no support for loading dynamic libraries for this OS"); + UNUSED(name); UNUSED(global); + return NULL; +} + +static void clib_unloadlib(CLibrary *cl) +{ + UNUSED(cl); +} + +static void *clib_getsym(CLibrary *cl, const char *name) +{ + UNUSED(cl); UNUSED(name); + return NULL; +} + +#endif + +/* -- C library indexing -------------------------------------------------- */ + +#if LJ_TARGET_X86 && LJ_ABI_WIN +/* Compute argument size for fastcall/stdcall functions. */ +static CTSize clib_func_argsize(CTState *cts, CType *ct) +{ + CTSize n = 0; + while (ct->sib) { + CType *d; + ct = ctype_get(cts, ct->sib); + if (ctype_isfield(ct->info)) { + d = ctype_rawchild(cts, ct); + n += ((d->size + 3) & ~3); + } + } + return n; +} +#endif + +/* Get redirected or mangled external symbol. */ +static const char *clib_extsym(CTState *cts, CType *ct, GCstr *name) +{ + if (ct->sib) { + CType *ctf = ctype_get(cts, ct->sib); + if (ctype_isxattrib(ctf->info, CTA_REDIR)) + return strdata(gco2str(gcref(ctf->name))); + } + return strdata(name); +} + +/* Index a C library by name. */ +TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name) +{ + TValue *tv = lj_tab_setstr(L, cl->cache, name); + if (LJ_UNLIKELY(tvisnil(tv))) { + CTState *cts = ctype_cts(L); + CType *ct; + CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX); + if (!id) + lj_err_callerv(L, LJ_ERR_FFI_NODECL, strdata(name)); + if (ctype_isconstval(ct->info)) { + CType *ctt = ctype_child(cts, ct); + lua_assert(ctype_isinteger(ctt->info) && ctt->size <= 4); + if ((ctt->info & CTF_UNSIGNED) && (int32_t)ct->size < 0) + setnumV(tv, (lua_Number)(uint32_t)ct->size); + else + setintV(tv, (int32_t)ct->size); + } else { + const char *sym = clib_extsym(cts, ct, name); +#if LJ_TARGET_WINDOWS + DWORD oldwerr = GetLastError(); +#endif + void *p = clib_getsym(cl, sym); + GCcdata *cd; + lua_assert(ctype_isfunc(ct->info) || ctype_isextern(ct->info)); +#if LJ_TARGET_X86 && LJ_ABI_WIN + /* Retry with decorated name for fastcall/stdcall functions. */ + if (!p && ctype_isfunc(ct->info)) { + CTInfo cconv = ctype_cconv(ct->info); + if (cconv == CTCC_FASTCALL || cconv == CTCC_STDCALL) { + CTSize sz = clib_func_argsize(cts, ct); + const char *symd = lj_str_pushf(L, + cconv == CTCC_FASTCALL ? "@%s@%d" : "_%s@%d", + sym, sz); + L->top--; + p = clib_getsym(cl, symd); + } + } +#endif + if (!p) + clib_error(L, "cannot resolve symbol " LUA_QS ": %s", sym); +#if LJ_TARGET_WINDOWS + SetLastError(oldwerr); +#endif + cd = lj_cdata_new(cts, id, CTSIZE_PTR); + *(void **)cdataptr(cd) = p; + setcdataV(L, tv, cd); + } + } + return tv; +} + +/* -- C library management ------------------------------------------------ */ + +/* Create a new CLibrary object and push it on the stack. */ +static CLibrary *clib_new(lua_State *L, GCtab *mt) +{ + GCtab *t = lj_tab_new(L, 0, 0); + GCudata *ud = lj_udata_new(L, sizeof(CLibrary), t); + CLibrary *cl = (CLibrary *)uddata(ud); + cl->cache = t; + ud->udtype = UDTYPE_FFI_CLIB; + /* NOBARRIER: The GCudata is new (marked white). */ + setgcref(ud->metatable, obj2gco(mt)); + setudataV(L, L->top++, ud); + return cl; +} + +/* Load a C library. */ +void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global) +{ + void *handle = clib_loadlib(L, strdata(name), global); + CLibrary *cl = clib_new(L, mt); + cl->handle = handle; +} + +/* Unload a C library. */ +void lj_clib_unload(CLibrary *cl) +{ + clib_unloadlib(cl); + cl->handle = NULL; +} + +/* Create the default C library object. */ +void lj_clib_default(lua_State *L, GCtab *mt) +{ + CLibrary *cl = clib_new(L, mt); + cl->handle = CLIB_DEFHANDLE; +} + +#endif diff --git a/external/lua/luajit/src/src/lj_clib.h b/external/lua/luajit/src/src/lj_clib.h new file mode 100644 index 0000000000..421388adeb --- /dev/null +++ b/external/lua/luajit/src/src/lj_clib.h @@ -0,0 +1,29 @@ +/* +** FFI C library loader. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_CLIB_H +#define _LJ_CLIB_H + +#include "lj_obj.h" + +#if LJ_HASFFI + +/* Namespace for C library indexing. */ +#define CLNS_INDEX ((1u<env. */ +} CLibrary; + +LJ_FUNC TValue *lj_clib_index(lua_State *L, CLibrary *cl, GCstr *name); +LJ_FUNC void lj_clib_load(lua_State *L, GCtab *mt, GCstr *name, int global); +LJ_FUNC void lj_clib_unload(CLibrary *cl); +LJ_FUNC void lj_clib_default(lua_State *L, GCtab *mt); + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_cparse.c b/external/lua/luajit/src/src/lj_cparse.c new file mode 100644 index 0000000000..107c0381d4 --- /dev/null +++ b/external/lua/luajit/src/src/lj_cparse.c @@ -0,0 +1,1872 @@ +/* +** C declaration parser. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "lj_obj.h" + +#if LJ_HASFFI + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_ctype.h" +#include "lj_cparse.h" +#include "lj_frame.h" +#include "lj_vm.h" +#include "lj_char.h" +#include "lj_strscan.h" + +/* +** Important note: this is NOT a validating C parser! This is a minimal +** C declaration parser, solely for use by the LuaJIT FFI. +** +** It ought to return correct results for properly formed C declarations, +** but it may accept some invalid declarations, too (and return nonsense). +** Also, it shows rather generic error messages to avoid unnecessary bloat. +** If in doubt, please check the input against your favorite C compiler. +*/ + +/* -- C lexer ------------------------------------------------------------- */ + +/* C lexer token names. */ +static const char *const ctoknames[] = { +#define CTOKSTR(name, str) str, +CTOKDEF(CTOKSTR) +#undef CTOKSTR + NULL +}; + +/* Forward declaration. */ +LJ_NORET static void cp_err(CPState *cp, ErrMsg em); + +static const char *cp_tok2str(CPState *cp, CPToken tok) +{ + lua_assert(tok < CTOK_FIRSTDECL); + if (tok > CTOK_OFS) + return ctoknames[tok-CTOK_OFS-1]; + else if (!lj_char_iscntrl(tok)) + return lj_str_pushf(cp->L, "%c", tok); + else + return lj_str_pushf(cp->L, "char(%d)", tok); +} + +/* End-of-line? */ +static LJ_AINLINE int cp_iseol(CPChar c) +{ + return (c == '\n' || c == '\r'); +} + +static LJ_AINLINE CPChar cp_get(CPState *cp); + +/* Peek next raw character. */ +static LJ_AINLINE CPChar cp_rawpeek(CPState *cp) +{ + return (CPChar)(uint8_t)(*cp->p); +} + +/* Transparently skip backslash-escaped line breaks. */ +static LJ_NOINLINE CPChar cp_get_bs(CPState *cp) +{ + CPChar c2, c = cp_rawpeek(cp); + if (!cp_iseol(c)) return cp->c; + cp->p++; + c2 = cp_rawpeek(cp); + if (cp_iseol(c2) && c2 != c) cp->p++; + cp->linenumber++; + return cp_get(cp); +} + +/* Get next character. */ +static LJ_AINLINE CPChar cp_get(CPState *cp) +{ + cp->c = (CPChar)(uint8_t)(*cp->p++); + if (LJ_LIKELY(cp->c != '\\')) return cp->c; + return cp_get_bs(cp); +} + +/* Grow save buffer. */ +static LJ_NOINLINE void cp_save_grow(CPState *cp, CPChar c) +{ + MSize newsize; + if (cp->sb.sz >= CPARSE_MAX_BUF/2) + cp_err(cp, LJ_ERR_XELEM); + newsize = cp->sb.sz * 2; + lj_str_resizebuf(cp->L, &cp->sb, newsize); + cp->sb.buf[cp->sb.n++] = (char)c; +} + +/* Save character in buffer. */ +static LJ_AINLINE void cp_save(CPState *cp, CPChar c) +{ + if (LJ_UNLIKELY(cp->sb.n + 1 > cp->sb.sz)) + cp_save_grow(cp, c); + else + cp->sb.buf[cp->sb.n++] = (char)c; +} + +/* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */ +static void cp_newline(CPState *cp) +{ + CPChar c = cp_rawpeek(cp); + if (cp_iseol(c) && c != cp->c) cp->p++; + cp->linenumber++; +} + +LJ_NORET static void cp_errmsg(CPState *cp, CPToken tok, ErrMsg em, ...) +{ + const char *msg, *tokstr; + lua_State *L; + va_list argp; + if (tok == 0) { + tokstr = NULL; + } else if (tok == CTOK_IDENT || tok == CTOK_INTEGER || tok == CTOK_STRING || + tok >= CTOK_FIRSTDECL) { + if (cp->sb.n == 0) cp_save(cp, '$'); + cp_save(cp, '\0'); + tokstr = cp->sb.buf; + } else { + tokstr = cp_tok2str(cp, tok); + } + L = cp->L; + va_start(argp, em); + msg = lj_str_pushvf(L, err2msg(em), argp); + va_end(argp); + if (tokstr) + msg = lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tokstr); + if (cp->linenumber > 1) + msg = lj_str_pushf(L, "%s at line %d", msg, cp->linenumber); + lj_err_callermsg(L, msg); +} + +LJ_NORET LJ_NOINLINE static void cp_err_token(CPState *cp, CPToken tok) +{ + cp_errmsg(cp, cp->tok, LJ_ERR_XTOKEN, cp_tok2str(cp, tok)); +} + +LJ_NORET LJ_NOINLINE static void cp_err_badidx(CPState *cp, CType *ct) +{ + GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL); + cp_errmsg(cp, 0, LJ_ERR_FFI_BADIDX, strdata(s)); +} + +LJ_NORET LJ_NOINLINE static void cp_err(CPState *cp, ErrMsg em) +{ + cp_errmsg(cp, 0, em); +} + +/* -- Main lexical scanner ------------------------------------------------ */ + +/* Parse number literal. Only handles int32_t/uint32_t right now. */ +static CPToken cp_number(CPState *cp) +{ + StrScanFmt fmt; + TValue o; + do { cp_save(cp, cp->c); } while (lj_char_isident(cp_get(cp))); + cp_save(cp, '\0'); + fmt = lj_strscan_scan((const uint8_t *)cp->sb.buf, &o, STRSCAN_OPT_C); + if (fmt == STRSCAN_INT) cp->val.id = CTID_INT32; + else if (fmt == STRSCAN_U32) cp->val.id = CTID_UINT32; + else if (!(cp->mode & CPARSE_MODE_SKIP)) + cp_errmsg(cp, CTOK_INTEGER, LJ_ERR_XNUMBER); + cp->val.u32 = (uint32_t)o.i; + return CTOK_INTEGER; +} + +/* Parse identifier or keyword. */ +static CPToken cp_ident(CPState *cp) +{ + do { cp_save(cp, cp->c); } while (lj_char_isident(cp_get(cp))); + cp->str = lj_str_new(cp->L, cp->sb.buf, cp->sb.n); + cp->val.id = lj_ctype_getname(cp->cts, &cp->ct, cp->str, cp->tmask); + if (ctype_type(cp->ct->info) == CT_KW) + return ctype_cid(cp->ct->info); + return CTOK_IDENT; +} + +/* Parse parameter. */ +static CPToken cp_param(CPState *cp) +{ + CPChar c = cp_get(cp); + TValue *o = cp->param; + if (lj_char_isident(c) || c == '$') /* Reserve $xyz for future extensions. */ + cp_errmsg(cp, c, LJ_ERR_XSYNTAX); + if (!o || o >= cp->L->top) + cp_err(cp, LJ_ERR_FFI_NUMPARAM); + cp->param = o+1; + if (tvisstr(o)) { + cp->str = strV(o); + cp->val.id = 0; + cp->ct = &cp->cts->tab[0]; + return CTOK_IDENT; + } else if (tvisnumber(o)) { + cp->val.i32 = numberVint(o); + cp->val.id = CTID_INT32; + return CTOK_INTEGER; + } else { + GCcdata *cd; + if (!tviscdata(o)) + lj_err_argtype(cp->L, (int)(o-cp->L->base)+1, "type parameter"); + cd = cdataV(o); + if (cd->ctypeid == CTID_CTYPEID) + cp->val.id = *(CTypeID *)cdataptr(cd); + else + cp->val.id = cd->ctypeid; + return '$'; + } +} + +/* Parse string or character constant. */ +static CPToken cp_string(CPState *cp) +{ + CPChar delim = cp->c; + cp_get(cp); + while (cp->c != delim) { + CPChar c = cp->c; + if (c == '\0') cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR); + if (c == '\\') { + c = cp_get(cp); + switch (c) { + case '\0': cp_errmsg(cp, CTOK_EOF, LJ_ERR_XSTR); break; + case 'a': c = '\a'; break; + case 'b': c = '\b'; break; + case 'f': c = '\f'; break; + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'v': c = '\v'; break; + case 'e': c = 27; break; + case 'x': + c = 0; + while (lj_char_isxdigit(cp_get(cp))) + c = (c<<4) + (lj_char_isdigit(cp->c) ? cp->c-'0' : (cp->c&15)+9); + cp_save(cp, (c & 0xff)); + continue; + default: + if (lj_char_isdigit(c)) { + c -= '0'; + if (lj_char_isdigit(cp_get(cp))) { + c = c*8 + (cp->c - '0'); + if (lj_char_isdigit(cp_get(cp))) { + c = c*8 + (cp->c - '0'); + cp_get(cp); + } + } + cp_save(cp, (c & 0xff)); + continue; + } + break; + } + } + cp_save(cp, c); + cp_get(cp); + } + cp_get(cp); + if (delim == '"') { + cp->str = lj_str_new(cp->L, cp->sb.buf, cp->sb.n); + return CTOK_STRING; + } else { + if (cp->sb.n != 1) cp_err_token(cp, '\''); + cp->val.i32 = (int32_t)(char)cp->sb.buf[0]; + cp->val.id = CTID_INT32; + return CTOK_INTEGER; + } +} + +/* Skip C comment. */ +static void cp_comment_c(CPState *cp) +{ + do { + if (cp_get(cp) == '*') { + do { + if (cp_get(cp) == '/') { cp_get(cp); return; } + } while (cp->c == '*'); + } + if (cp_iseol(cp->c)) cp_newline(cp); + } while (cp->c != '\0'); +} + +/* Skip C++ comment. */ +static void cp_comment_cpp(CPState *cp) +{ + while (!cp_iseol(cp_get(cp)) && cp->c != '\0') + ; +} + +/* Lexical scanner for C. Only a minimal subset is implemented. */ +static CPToken cp_next_(CPState *cp) +{ + lj_str_resetbuf(&cp->sb); + for (;;) { + if (lj_char_isident(cp->c)) + return lj_char_isdigit(cp->c) ? cp_number(cp) : cp_ident(cp); + switch (cp->c) { + case '\n': case '\r': cp_newline(cp); /* fallthrough. */ + case ' ': case '\t': case '\v': case '\f': cp_get(cp); break; + case '"': case '\'': return cp_string(cp); + case '/': + if (cp_get(cp) == '*') cp_comment_c(cp); + else if (cp->c == '/') cp_comment_cpp(cp); + else return '/'; + break; + case '|': + if (cp_get(cp) != '|') return '|'; cp_get(cp); return CTOK_OROR; + case '&': + if (cp_get(cp) != '&') return '&'; cp_get(cp); return CTOK_ANDAND; + case '=': + if (cp_get(cp) != '=') return '='; cp_get(cp); return CTOK_EQ; + case '!': + if (cp_get(cp) != '=') return '!'; cp_get(cp); return CTOK_NE; + case '<': + if (cp_get(cp) == '=') { cp_get(cp); return CTOK_LE; } + else if (cp->c == '<') { cp_get(cp); return CTOK_SHL; } + return '<'; + case '>': + if (cp_get(cp) == '=') { cp_get(cp); return CTOK_GE; } + else if (cp->c == '>') { cp_get(cp); return CTOK_SHR; } + return '>'; + case '-': + if (cp_get(cp) != '>') return '-'; cp_get(cp); return CTOK_DEREF; + case '$': + return cp_param(cp); + case '\0': return CTOK_EOF; + default: { CPToken c = cp->c; cp_get(cp); return c; } + } + } +} + +static LJ_NOINLINE CPToken cp_next(CPState *cp) +{ + return (cp->tok = cp_next_(cp)); +} + +/* -- C parser ------------------------------------------------------------ */ + +/* Namespaces for resolving identifiers. */ +#define CPNS_DEFAULT \ + ((1u<linenumber = 1; + cp->depth = 0; + cp->curpack = 0; + cp->packstack[0] = 255; + lj_str_initbuf(&cp->sb); + lj_str_resizebuf(cp->L, &cp->sb, LJ_MIN_SBUF); + lua_assert(cp->p != NULL); + cp_get(cp); /* Read-ahead first char. */ + cp->tok = 0; + cp->tmask = CPNS_DEFAULT; + cp_next(cp); /* Read-ahead first token. */ +} + +/* Cleanup C parser state. */ +static void cp_cleanup(CPState *cp) +{ + global_State *g = G(cp->L); + lj_str_freebuf(g, &cp->sb); +} + +/* Check and consume optional token. */ +static int cp_opt(CPState *cp, CPToken tok) +{ + if (cp->tok == tok) { cp_next(cp); return 1; } + return 0; +} + +/* Check and consume token. */ +static void cp_check(CPState *cp, CPToken tok) +{ + if (cp->tok != tok) cp_err_token(cp, tok); + cp_next(cp); +} + +/* Check if the next token may start a type declaration. */ +static int cp_istypedecl(CPState *cp) +{ + if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECL) return 1; + if (cp->tok == CTOK_IDENT && ctype_istypedef(cp->ct->info)) return 1; + if (cp->tok == '$') return 1; + return 0; +} + +/* -- Constant expression evaluator --------------------------------------- */ + +/* Forward declarations. */ +static void cp_expr_unary(CPState *cp, CPValue *k); +static void cp_expr_sub(CPState *cp, CPValue *k, int pri); + +/* Please note that type handling is very weak here. Most ops simply +** assume integer operands. Accessors are only needed to compute types and +** return synthetic values. The only purpose of the expression evaluator +** is to compute the values of constant expressions one would typically +** find in C header files. And again: this is NOT a validating C parser! +*/ + +/* Parse comma separated expression and return last result. */ +static void cp_expr_comma(CPState *cp, CPValue *k) +{ + do { cp_expr_sub(cp, k, 0); } while (cp_opt(cp, ',')); +} + +/* Parse sizeof/alignof operator. */ +static void cp_expr_sizeof(CPState *cp, CPValue *k, int wantsz) +{ + CTSize sz; + CTInfo info; + if (cp_opt(cp, '(')) { + if (cp_istypedecl(cp)) + k->id = cp_decl_abstract(cp); + else + cp_expr_comma(cp, k); + cp_check(cp, ')'); + } else { + cp_expr_unary(cp, k); + } + info = lj_ctype_info(cp->cts, k->id, &sz); + if (wantsz) { + if (sz != CTSIZE_INVALID) + k->u32 = sz; + else if (k->id != CTID_A_CCHAR) /* Special case for sizeof("string"). */ + cp_err(cp, LJ_ERR_FFI_INVSIZE); + } else { + k->u32 = 1u << ctype_align(info); + } + k->id = CTID_UINT32; /* Really size_t. */ +} + +/* Parse prefix operators. */ +static void cp_expr_prefix(CPState *cp, CPValue *k) +{ + if (cp->tok == CTOK_INTEGER) { + *k = cp->val; cp_next(cp); + } else if (cp_opt(cp, '+')) { + cp_expr_unary(cp, k); /* Nothing to do (well, integer promotion). */ + } else if (cp_opt(cp, '-')) { + cp_expr_unary(cp, k); k->i32 = -k->i32; + } else if (cp_opt(cp, '~')) { + cp_expr_unary(cp, k); k->i32 = ~k->i32; + } else if (cp_opt(cp, '!')) { + cp_expr_unary(cp, k); k->i32 = !k->i32; k->id = CTID_INT32; + } else if (cp_opt(cp, '(')) { + if (cp_istypedecl(cp)) { /* Cast operator. */ + CTypeID id = cp_decl_abstract(cp); + cp_check(cp, ')'); + cp_expr_unary(cp, k); + k->id = id; /* No conversion performed. */ + } else { /* Sub-expression. */ + cp_expr_comma(cp, k); + cp_check(cp, ')'); + } + } else if (cp_opt(cp, '*')) { /* Indirection. */ + CType *ct; + cp_expr_unary(cp, k); + ct = lj_ctype_rawref(cp->cts, k->id); + if (!ctype_ispointer(ct->info)) + cp_err_badidx(cp, ct); + k->u32 = 0; k->id = ctype_cid(ct->info); + } else if (cp_opt(cp, '&')) { /* Address operator. */ + cp_expr_unary(cp, k); + k->id = lj_ctype_intern(cp->cts, CTINFO(CT_PTR, CTALIGN_PTR+k->id), + CTSIZE_PTR); + } else if (cp_opt(cp, CTOK_SIZEOF)) { + cp_expr_sizeof(cp, k, 1); + } else if (cp_opt(cp, CTOK_ALIGNOF)) { + cp_expr_sizeof(cp, k, 0); + } else if (cp->tok == CTOK_IDENT) { + if (ctype_type(cp->ct->info) == CT_CONSTVAL) { + k->u32 = cp->ct->size; k->id = ctype_cid(cp->ct->info); + } else if (ctype_type(cp->ct->info) == CT_EXTERN) { + k->u32 = cp->val.id; k->id = ctype_cid(cp->ct->info); + } else if (ctype_type(cp->ct->info) == CT_FUNC) { + k->u32 = cp->val.id; k->id = cp->val.id; + } else { + goto err_expr; + } + cp_next(cp); + } else if (cp->tok == CTOK_STRING) { + CTSize sz = cp->str->len; + while (cp_next(cp) == CTOK_STRING) + sz += cp->str->len; + k->u32 = sz + 1; + k->id = CTID_A_CCHAR; + } else { + err_expr: + cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); + } +} + +/* Parse postfix operators. */ +static void cp_expr_postfix(CPState *cp, CPValue *k) +{ + for (;;) { + CType *ct; + if (cp_opt(cp, '[')) { /* Array/pointer index. */ + CPValue k2; + cp_expr_comma(cp, &k2); + ct = lj_ctype_rawref(cp->cts, k->id); + if (!ctype_ispointer(ct->info)) { + ct = lj_ctype_rawref(cp->cts, k2.id); + if (!ctype_ispointer(ct->info)) + cp_err_badidx(cp, ct); + } + cp_check(cp, ']'); + k->u32 = 0; + } else if (cp->tok == '.' || cp->tok == CTOK_DEREF) { /* Struct deref. */ + CTSize ofs; + CType *fct; + ct = lj_ctype_rawref(cp->cts, k->id); + if (cp->tok == CTOK_DEREF) { + if (!ctype_ispointer(ct->info)) + cp_err_badidx(cp, ct); + ct = lj_ctype_rawref(cp->cts, ctype_cid(ct->info)); + } + cp_next(cp); + if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); + if (!ctype_isstruct(ct->info) || ct->size == CTSIZE_INVALID || + !(fct = lj_ctype_getfield(cp->cts, ct, cp->str, &ofs)) || + ctype_isbitfield(fct->info)) { + GCstr *s = lj_ctype_repr(cp->cts->L, ctype_typeid(cp->cts, ct), NULL); + cp_errmsg(cp, 0, LJ_ERR_FFI_BADMEMBER, strdata(s), strdata(cp->str)); + } + ct = fct; + k->u32 = ctype_isconstval(ct->info) ? ct->size : 0; + cp_next(cp); + } else { + return; + } + k->id = ctype_cid(ct->info); + } +} + +/* Parse infix operators. */ +static void cp_expr_infix(CPState *cp, CPValue *k, int pri) +{ + CPValue k2; + k2.u32 = 0; k2.id = 0; /* Silence the compiler. */ + for (;;) { + switch (pri) { + case 0: + if (cp_opt(cp, '?')) { + CPValue k3; + cp_expr_comma(cp, &k2); /* Right-associative. */ + cp_check(cp, ':'); + cp_expr_sub(cp, &k3, 0); + k->u32 = k->u32 ? k2.u32 : k3.u32; + k->id = k2.id > k3.id ? k2.id : k3.id; + continue; + } + case 1: + if (cp_opt(cp, CTOK_OROR)) { + cp_expr_sub(cp, &k2, 2); k->i32 = k->u32 || k2.u32; k->id = CTID_INT32; + continue; + } + case 2: + if (cp_opt(cp, CTOK_ANDAND)) { + cp_expr_sub(cp, &k2, 3); k->i32 = k->u32 && k2.u32; k->id = CTID_INT32; + continue; + } + case 3: + if (cp_opt(cp, '|')) { + cp_expr_sub(cp, &k2, 4); k->u32 = k->u32 | k2.u32; goto arith_result; + } + case 4: + if (cp_opt(cp, '^')) { + cp_expr_sub(cp, &k2, 5); k->u32 = k->u32 ^ k2.u32; goto arith_result; + } + case 5: + if (cp_opt(cp, '&')) { + cp_expr_sub(cp, &k2, 6); k->u32 = k->u32 & k2.u32; goto arith_result; + } + case 6: + if (cp_opt(cp, CTOK_EQ)) { + cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 == k2.u32; k->id = CTID_INT32; + continue; + } else if (cp_opt(cp, CTOK_NE)) { + cp_expr_sub(cp, &k2, 7); k->i32 = k->u32 != k2.u32; k->id = CTID_INT32; + continue; + } + case 7: + if (cp_opt(cp, '<')) { + cp_expr_sub(cp, &k2, 8); + if (k->id == CTID_INT32 && k2.id == CTID_INT32) + k->i32 = k->i32 < k2.i32; + else + k->i32 = k->u32 < k2.u32; + k->id = CTID_INT32; + continue; + } else if (cp_opt(cp, '>')) { + cp_expr_sub(cp, &k2, 8); + if (k->id == CTID_INT32 && k2.id == CTID_INT32) + k->i32 = k->i32 > k2.i32; + else + k->i32 = k->u32 > k2.u32; + k->id = CTID_INT32; + continue; + } else if (cp_opt(cp, CTOK_LE)) { + cp_expr_sub(cp, &k2, 8); + if (k->id == CTID_INT32 && k2.id == CTID_INT32) + k->i32 = k->i32 <= k2.i32; + else + k->i32 = k->u32 <= k2.u32; + k->id = CTID_INT32; + continue; + } else if (cp_opt(cp, CTOK_GE)) { + cp_expr_sub(cp, &k2, 8); + if (k->id == CTID_INT32 && k2.id == CTID_INT32) + k->i32 = k->i32 >= k2.i32; + else + k->i32 = k->u32 >= k2.u32; + k->id = CTID_INT32; + continue; + } + case 8: + if (cp_opt(cp, CTOK_SHL)) { + cp_expr_sub(cp, &k2, 9); k->u32 = k->u32 << k2.u32; + continue; + } else if (cp_opt(cp, CTOK_SHR)) { + cp_expr_sub(cp, &k2, 9); + if (k->id == CTID_INT32) + k->i32 = k->i32 >> k2.i32; + else + k->u32 = k->u32 >> k2.u32; + continue; + } + case 9: + if (cp_opt(cp, '+')) { + cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 + k2.u32; + arith_result: + if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ + continue; + } else if (cp_opt(cp, '-')) { + cp_expr_sub(cp, &k2, 10); k->u32 = k->u32 - k2.u32; goto arith_result; + } + case 10: + if (cp_opt(cp, '*')) { + cp_expr_unary(cp, &k2); k->u32 = k->u32 * k2.u32; goto arith_result; + } else if (cp_opt(cp, '/')) { + cp_expr_unary(cp, &k2); + if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ + if (k2.u32 == 0 || + (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1)) + cp_err(cp, LJ_ERR_BADVAL); + if (k->id == CTID_INT32) + k->i32 = k->i32 / k2.i32; + else + k->u32 = k->u32 / k2.u32; + continue; + } else if (cp_opt(cp, '%')) { + cp_expr_unary(cp, &k2); + if (k2.id > k->id) k->id = k2.id; /* Trivial promotion to unsigned. */ + if (k2.u32 == 0 || + (k->id == CTID_INT32 && k->u32 == 0x80000000u && k2.i32 == -1)) + cp_err(cp, LJ_ERR_BADVAL); + if (k->id == CTID_INT32) + k->i32 = k->i32 % k2.i32; + else + k->u32 = k->u32 % k2.u32; + continue; + } + default: + return; + } + } +} + +/* Parse and evaluate unary expression. */ +static void cp_expr_unary(CPState *cp, CPValue *k) +{ + if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS); + cp_expr_prefix(cp, k); + cp_expr_postfix(cp, k); + cp->depth--; +} + +/* Parse and evaluate sub-expression. */ +static void cp_expr_sub(CPState *cp, CPValue *k, int pri) +{ + cp_expr_unary(cp, k); + cp_expr_infix(cp, k, pri); +} + +/* Parse constant integer expression. */ +static void cp_expr_kint(CPState *cp, CPValue *k) +{ + CType *ct; + cp_expr_sub(cp, k, 0); + ct = ctype_raw(cp->cts, k->id); + if (!ctype_isinteger(ct->info)) cp_err(cp, LJ_ERR_BADVAL); +} + +/* Parse (non-negative) size expression. */ +static CTSize cp_expr_ksize(CPState *cp) +{ + CPValue k; + cp_expr_kint(cp, &k); + if (k.u32 >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE); + return k.u32; +} + +/* -- Type declaration stack management ----------------------------------- */ + +/* Add declaration element behind the insertion position. */ +static CPDeclIdx cp_add(CPDecl *decl, CTInfo info, CTSize size) +{ + CPDeclIdx top = decl->top; + if (top >= CPARSE_MAX_DECLSTACK) cp_err(decl->cp, LJ_ERR_XLEVELS); + decl->stack[top].info = info; + decl->stack[top].size = size; + decl->stack[top].sib = 0; + setgcrefnull(decl->stack[top].name); + decl->stack[top].next = decl->stack[decl->pos].next; + decl->stack[decl->pos].next = (CTypeID1)top; + decl->top = top+1; + return top; +} + +/* Push declaration element before the insertion position. */ +static CPDeclIdx cp_push(CPDecl *decl, CTInfo info, CTSize size) +{ + return (decl->pos = cp_add(decl, info, size)); +} + +/* Push or merge attributes. */ +static void cp_push_attributes(CPDecl *decl) +{ + CType *ct = &decl->stack[decl->pos]; + if (ctype_isfunc(ct->info)) { /* Ok to modify in-place. */ +#if LJ_TARGET_X86 + if ((decl->fattr & CTFP_CCONV)) + ct->info = (ct->info & (CTMASK_NUM|CTF_VARARG|CTMASK_CID)) + + (decl->fattr & ~CTMASK_CID); +#endif + } else { + if ((decl->attr & CTFP_ALIGNED) && !(decl->mode & CPARSE_MODE_FIELD)) + cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_ALIGN)), + ctype_align(decl->attr)); + } +} + +/* Push unrolled type to declaration stack and merge qualifiers. */ +static void cp_push_type(CPDecl *decl, CTypeID id) +{ + CType *ct = ctype_get(decl->cp->cts, id); + CTInfo info = ct->info; + CTSize size = ct->size; + switch (ctype_type(info)) { + case CT_STRUCT: case CT_ENUM: + cp_push(decl, CTINFO(CT_TYPEDEF, id), 0); /* Don't copy unique types. */ + if ((decl->attr & CTF_QUAL)) { /* Push unmerged qualifiers. */ + cp_push(decl, CTINFO(CT_ATTRIB, CTATTRIB(CTA_QUAL)), + (decl->attr & CTF_QUAL)); + decl->attr &= ~CTF_QUAL; + } + break; + case CT_ATTRIB: + if (ctype_isxattrib(info, CTA_QUAL)) + decl->attr &= ~size; /* Remove redundant qualifiers. */ + cp_push_type(decl, ctype_cid(info)); /* Unroll. */ + cp_push(decl, info & ~CTMASK_CID, size); /* Copy type. */ + break; + case CT_ARRAY: + cp_push_type(decl, ctype_cid(info)); /* Unroll. */ + cp_push(decl, info & ~CTMASK_CID, size); /* Copy type. */ + decl->stack[decl->pos].sib = 1; /* Mark as already checked and sized. */ + /* Note: this is not copied to the ct->sib in the C type table. */ + break; + case CT_FUNC: + /* Copy type, link parameters (shared). */ + decl->stack[cp_push(decl, info, size)].sib = ct->sib; + break; + default: + /* Copy type, merge common qualifiers. */ + cp_push(decl, info|(decl->attr & CTF_QUAL), size); + decl->attr &= ~CTF_QUAL; + break; + } +} + +/* Consume the declaration element chain and intern the C type. */ +static CTypeID cp_decl_intern(CPState *cp, CPDecl *decl) +{ + CTypeID id = 0; + CPDeclIdx idx = 0; + CTSize csize = CTSIZE_INVALID; + CTSize cinfo = 0; + do { + CType *ct = &decl->stack[idx]; + CTInfo info = ct->info; + CTInfo size = ct->size; + /* The cid is already part of info for copies of pointers/functions. */ + idx = ct->next; + if (ctype_istypedef(info)) { + lua_assert(id == 0); + id = ctype_cid(info); + /* Always refetch info/size, since struct/enum may have been completed. */ + cinfo = ctype_get(cp->cts, id)->info; + csize = ctype_get(cp->cts, id)->size; + lua_assert(ctype_isstruct(cinfo) || ctype_isenum(cinfo)); + } else if (ctype_isfunc(info)) { /* Intern function. */ + CType *fct; + CTypeID fid; + CTypeID sib; + if (id) { + CType *refct = ctype_raw(cp->cts, id); + /* Reject function or refarray return types. */ + if (ctype_isfunc(refct->info) || ctype_isrefarray(refct->info)) + cp_err(cp, LJ_ERR_FFI_INVTYPE); + } + /* No intervening attributes allowed, skip forward. */ + while (idx) { + CType *ctn = &decl->stack[idx]; + if (!ctype_isattrib(ctn->info)) break; + idx = ctn->next; /* Skip attribute. */ + } + sib = ct->sib; /* Next line may reallocate the C type table. */ + fid = lj_ctype_new(cp->cts, &fct); + csize = CTSIZE_INVALID; + fct->info = cinfo = info + id; + fct->size = size; + fct->sib = sib; + id = fid; + } else if (ctype_isattrib(info)) { + if (ctype_isxattrib(info, CTA_QUAL)) + cinfo |= size; + else if (ctype_isxattrib(info, CTA_ALIGN)) + CTF_INSERT(cinfo, ALIGN, size); + id = lj_ctype_intern(cp->cts, info+id, size); + /* Inherit csize/cinfo from original type. */ + } else { + if (ctype_isnum(info)) { /* Handle mode/vector-size attributes. */ + lua_assert(id == 0); + if (!(info & CTF_BOOL)) { + CTSize msize = ctype_msizeP(decl->attr); + CTSize vsize = ctype_vsizeP(decl->attr); + if (msize && (!(info & CTF_FP) || (msize == 4 || msize == 8))) { + CTSize malign = lj_fls(msize); + if (malign > 4) malign = 4; /* Limit alignment. */ + CTF_INSERT(info, ALIGN, malign); + size = msize; /* Override size via mode. */ + } + if (vsize) { /* Vector size set? */ + CTSize esize = lj_fls(size); + if (vsize >= esize) { + /* Intern the element type first. */ + id = lj_ctype_intern(cp->cts, info, size); + /* Then create a vector (array) with vsize alignment. */ + size = (1u << vsize); + if (vsize > 4) vsize = 4; /* Limit alignment. */ + if (ctype_align(info) > vsize) vsize = ctype_align(info); + info = CTINFO(CT_ARRAY, (info & CTF_QUAL) + CTF_VECTOR + + CTALIGN(vsize)); + } + } + } + } else if (ctype_isptr(info)) { + /* Reject pointer/ref to ref. */ + if (id && ctype_isref(ctype_raw(cp->cts, id)->info)) + cp_err(cp, LJ_ERR_FFI_INVTYPE); + if (ctype_isref(info)) { + info &= ~CTF_VOLATILE; /* Refs are always const, never volatile. */ + /* No intervening attributes allowed, skip forward. */ + while (idx) { + CType *ctn = &decl->stack[idx]; + if (!ctype_isattrib(ctn->info)) break; + idx = ctn->next; /* Skip attribute. */ + } + } + } else if (ctype_isarray(info)) { /* Check for valid array size etc. */ + if (ct->sib == 0) { /* Only check/size arrays not copied by unroll. */ + if (ctype_isref(cinfo)) /* Reject arrays of refs. */ + cp_err(cp, LJ_ERR_FFI_INVTYPE); + /* Reject VLS or unknown-sized types. */ + if (ctype_isvltype(cinfo) || csize == CTSIZE_INVALID) + cp_err(cp, LJ_ERR_FFI_INVSIZE); + /* a[] and a[?] keep their invalid size. */ + if (size != CTSIZE_INVALID) { + uint64_t xsz = (uint64_t)size * csize; + if (xsz >= 0x80000000u) cp_err(cp, LJ_ERR_FFI_INVSIZE); + size = (CTSize)xsz; + } + } + if ((cinfo & CTF_ALIGN) > (info & CTF_ALIGN)) /* Find max. align. */ + info = (info & ~CTF_ALIGN) | (cinfo & CTF_ALIGN); + info |= (cinfo & CTF_QUAL); /* Inherit qual. */ + } else { + lua_assert(ctype_isvoid(info)); + } + csize = size; + cinfo = info+id; + id = lj_ctype_intern(cp->cts, info+id, size); + } + } while (idx); + return id; +} + +/* -- C declaration parser ------------------------------------------------ */ + +#define H_(le, be) LJ_ENDIAN_SELECT(0x##le, 0x##be) + +/* Reset declaration state to declaration specifier. */ +static void cp_decl_reset(CPDecl *decl) +{ + decl->pos = decl->specpos; + decl->top = decl->specpos+1; + decl->stack[decl->specpos].next = 0; + decl->attr = decl->specattr; + decl->fattr = decl->specfattr; + decl->name = NULL; + decl->redir = NULL; +} + +/* Parse constant initializer. */ +/* NYI: FP constants and strings as initializers. */ +static CTypeID cp_decl_constinit(CPState *cp, CType **ctp, CTypeID ctypeid) +{ + CType *ctt = ctype_get(cp->cts, ctypeid); + CTInfo info; + CTSize size; + CPValue k; + CTypeID constid; + while (ctype_isattrib(ctt->info)) { /* Skip attributes. */ + ctypeid = ctype_cid(ctt->info); /* Update ID, too. */ + ctt = ctype_get(cp->cts, ctypeid); + } + info = ctt->info; + size = ctt->size; + if (!ctype_isinteger(info) || !(info & CTF_CONST) || size > 4) + cp_err(cp, LJ_ERR_FFI_INVTYPE); + cp_check(cp, '='); + cp_expr_sub(cp, &k, 0); + constid = lj_ctype_new(cp->cts, ctp); + (*ctp)->info = CTINFO(CT_CONSTVAL, CTF_CONST|ctypeid); + k.u32 <<= 8*(4-size); + if ((info & CTF_UNSIGNED)) + k.u32 >>= 8*(4-size); + else + k.u32 = (uint32_t)((int32_t)k.u32 >> 8*(4-size)); + (*ctp)->size = k.u32; + return constid; +} + +/* Parse size in parentheses as part of attribute. */ +static CTSize cp_decl_sizeattr(CPState *cp) +{ + CTSize sz; + uint32_t oldtmask = cp->tmask; + cp->tmask = CPNS_DEFAULT; /* Required for expression evaluator. */ + cp_check(cp, '('); + sz = cp_expr_ksize(cp); + cp->tmask = oldtmask; + cp_check(cp, ')'); + return sz; +} + +/* Parse alignment attribute. */ +static void cp_decl_align(CPState *cp, CPDecl *decl) +{ + CTSize al = 4; /* Unspecified alignment is 16 bytes. */ + if (cp->tok == '(') { + al = cp_decl_sizeattr(cp); + al = al ? lj_fls(al) : 0; + } + CTF_INSERT(decl->attr, ALIGN, al); + decl->attr |= CTFP_ALIGNED; +} + +/* Parse GCC asm("name") redirect. */ +static void cp_decl_asm(CPState *cp, CPDecl *decl) +{ + UNUSED(decl); + cp_next(cp); + cp_check(cp, '('); + if (cp->tok == CTOK_STRING) { + GCstr *str = cp->str; + while (cp_next(cp) == CTOK_STRING) { + lj_str_pushf(cp->L, "%s%s", strdata(str), strdata(cp->str)); + cp->L->top--; + str = strV(cp->L->top); + } + decl->redir = str; + } + cp_check(cp, ')'); +} + +/* Parse GCC __attribute__((mode(...))). */ +static void cp_decl_mode(CPState *cp, CPDecl *decl) +{ + cp_check(cp, '('); + if (cp->tok == CTOK_IDENT) { + const char *s = strdata(cp->str); + CTSize sz = 0, vlen = 0; + if (s[0] == '_' && s[1] == '_') s += 2; + if (*s == 'V') { + s++; + vlen = *s++ - '0'; + if (*s >= '0' && *s <= '9') + vlen = vlen*10 + (*s++ - '0'); + } + switch (*s++) { + case 'Q': sz = 1; break; + case 'H': sz = 2; break; + case 'S': sz = 4; break; + case 'D': sz = 8; break; + case 'T': sz = 16; break; + case 'O': sz = 32; break; + default: goto bad_size; + } + if (*s == 'I' || *s == 'F') { + CTF_INSERT(decl->attr, MSIZEP, sz); + if (vlen) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vlen*sz)); + } + bad_size: + cp_next(cp); + } + cp_check(cp, ')'); +} + +/* Parse GCC __attribute__((...)). */ +static void cp_decl_gccattribute(CPState *cp, CPDecl *decl) +{ + cp_next(cp); + cp_check(cp, '('); + cp_check(cp, '('); + while (cp->tok != ')') { + if (cp->tok == CTOK_IDENT) { + GCstr *attrstr = cp->str; + cp_next(cp); + switch (attrstr->hash) { + case H_(64a9208e,8ce14319): case H_(8e6331b2,95a282af): /* aligned */ + cp_decl_align(cp, decl); + break; + case H_(42eb47de,f0ede26c): case H_(29f48a09,cf383e0c): /* packed */ + decl->attr |= CTFP_PACKED; + break; + case H_(0a84eef6,8dfab04c): case H_(995cf92c,d5696591): /* mode */ + cp_decl_mode(cp, decl); + break; + case H_(0ab31997,2d5213fa): case H_(bf875611,200e9990): /* vector_size */ + { + CTSize vsize = cp_decl_sizeattr(cp); + if (vsize) CTF_INSERT(decl->attr, VSIZEP, lj_fls(vsize)); + } + break; +#if LJ_TARGET_X86 + case H_(5ad22db8,c689b848): case H_(439150fa,65ea78cb): /* regparm */ + CTF_INSERT(decl->fattr, REGPARM, cp_decl_sizeattr(cp)); + decl->fattr |= CTFP_CCONV; + break; + case H_(18fc0b98,7ff4c074): case H_(4e62abed,0a747424): /* cdecl */ + CTF_INSERT(decl->fattr, CCONV, CTCC_CDECL); + decl->fattr |= CTFP_CCONV; + break; + case H_(72b2e41b,494c5a44): case H_(f2356d59,f25fc9bd): /* thiscall */ + CTF_INSERT(decl->fattr, CCONV, CTCC_THISCALL); + decl->fattr |= CTFP_CCONV; + break; + case H_(0d0ffc42,ab746f88): case H_(21c54ba1,7f0ca7e3): /* fastcall */ + CTF_INSERT(decl->fattr, CCONV, CTCC_FASTCALL); + decl->fattr |= CTFP_CCONV; + break; + case H_(ef76b040,9412e06a): case H_(de56697b,c750e6e1): /* stdcall */ + CTF_INSERT(decl->fattr, CCONV, CTCC_STDCALL); + decl->fattr |= CTFP_CCONV; + break; + case H_(ea78b622,f234bd8e): case H_(252ffb06,8d50f34b): /* sseregparm */ + decl->fattr |= CTF_SSEREGPARM; + decl->fattr |= CTFP_CCONV; + break; +#endif + default: /* Skip all other attributes. */ + goto skip_attr; + } + } else if (cp->tok >= CTOK_FIRSTDECL) { /* For __attribute((const)) etc. */ + cp_next(cp); + skip_attr: + if (cp_opt(cp, '(')) { + while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); + cp_check(cp, ')'); + } + } else { + break; + } + if (!cp_opt(cp, ',')) break; + } + cp_check(cp, ')'); + cp_check(cp, ')'); +} + +/* Parse MSVC __declspec(...). */ +static void cp_decl_msvcattribute(CPState *cp, CPDecl *decl) +{ + cp_next(cp); + cp_check(cp, '('); + while (cp->tok == CTOK_IDENT) { + GCstr *attrstr = cp->str; + cp_next(cp); + switch (attrstr->hash) { + case H_(bc2395fa,98f267f8): /* align */ + cp_decl_align(cp, decl); + break; + default: /* Ignore all other attributes. */ + if (cp_opt(cp, '(')) { + while (cp->tok != ')' && cp->tok != CTOK_EOF) cp_next(cp); + cp_check(cp, ')'); + } + break; + } + } + cp_check(cp, ')'); +} + +/* Parse declaration attributes (and common qualifiers). */ +static void cp_decl_attributes(CPState *cp, CPDecl *decl) +{ + for (;;) { + switch (cp->tok) { + case CTOK_CONST: decl->attr |= CTF_CONST; break; + case CTOK_VOLATILE: decl->attr |= CTF_VOLATILE; break; + case CTOK_RESTRICT: break; /* Ignore. */ + case CTOK_EXTENSION: break; /* Ignore. */ + case CTOK_ATTRIBUTE: cp_decl_gccattribute(cp, decl); continue; + case CTOK_ASM: cp_decl_asm(cp, decl); continue; + case CTOK_DECLSPEC: cp_decl_msvcattribute(cp, decl); continue; + case CTOK_CCDECL: +#if LJ_TARGET_X86 + CTF_INSERT(decl->fattr, CCONV, cp->ct->size); + decl->fattr |= CTFP_CCONV; +#endif + break; + case CTOK_PTRSZ: +#if LJ_64 + CTF_INSERT(decl->attr, MSIZEP, cp->ct->size); +#endif + break; + default: return; + } + cp_next(cp); + } +} + +/* Parse struct/union/enum name. */ +static CTypeID cp_struct_name(CPState *cp, CPDecl *sdecl, CTInfo info) +{ + CTypeID sid; + CType *ct; + cp->tmask = CPNS_STRUCT; + cp_next(cp); + cp_decl_attributes(cp, sdecl); + cp->tmask = CPNS_DEFAULT; + if (cp->tok != '{') { + if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); + if (cp->val.id) { /* Name of existing struct/union/enum. */ + sid = cp->val.id; + ct = cp->ct; + if ((ct->info ^ info) & (CTMASK_NUM|CTF_UNION)) /* Wrong type. */ + cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name)))); + } else { /* Create named, incomplete struct/union/enum. */ + if ((cp->mode & CPARSE_MODE_NOIMPLICIT)) + cp_errmsg(cp, 0, LJ_ERR_FFI_BADTAG, strdata(cp->str)); + sid = lj_ctype_new(cp->cts, &ct); + ct->info = info; + ct->size = CTSIZE_INVALID; + ctype_setname(ct, cp->str); + lj_ctype_addname(cp->cts, ct, sid); + } + cp_next(cp); + } else { /* Create anonymous, incomplete struct/union/enum. */ + sid = lj_ctype_new(cp->cts, &ct); + ct->info = info; + ct->size = CTSIZE_INVALID; + } + if (cp->tok == '{') { + if (ct->size != CTSIZE_INVALID || ct->sib) + cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(gco2str(gcref(ct->name)))); + ct->sib = 1; /* Indicate the type is currently being defined. */ + } + return sid; +} + +/* Determine field alignment. */ +static CTSize cp_field_align(CPState *cp, CType *ct, CTInfo info) +{ + CTSize align = ctype_align(info); + UNUSED(cp); UNUSED(ct); +#if (LJ_TARGET_X86 && !LJ_ABI_WIN) || (LJ_TARGET_ARM && __APPLE__) + /* The SYSV i386 and iOS ABIs limit alignment of non-vector fields to 2^2. */ + if (align > 2 && !(info & CTFP_ALIGNED)) { + if (ctype_isarray(info) && !(info & CTF_VECTOR)) { + do { + ct = ctype_rawchild(cp->cts, ct); + info = ct->info; + } while (ctype_isarray(info) && !(info & CTF_VECTOR)); + } + if (ctype_isnum(info) || ctype_isenum(info)) + align = 2; + } +#endif + return align; +} + +/* Layout struct/union fields. */ +static void cp_struct_layout(CPState *cp, CTypeID sid, CTInfo sattr) +{ + CTSize bofs = 0, bmaxofs = 0; /* Bit offset and max. bit offset. */ + CTSize maxalign = ctype_align(sattr); + CType *sct = ctype_get(cp->cts, sid); + CTInfo sinfo = sct->info; + CTypeID fieldid = sct->sib; + while (fieldid) { + CType *ct = ctype_get(cp->cts, fieldid); + CTInfo attr = ct->size; /* Field declaration attributes (temp.). */ + + if (ctype_isfield(ct->info) || + (ctype_isxattrib(ct->info, CTA_SUBTYPE) && attr)) { + CTSize align, amask; /* Alignment (pow2) and alignment mask (bits). */ + CTSize sz; + CTInfo info = lj_ctype_info(cp->cts, ctype_cid(ct->info), &sz); + CTSize bsz, csz = 8*sz; /* Field size and container size (in bits). */ + sinfo |= (info & (CTF_QUAL|CTF_VLA)); /* Merge pseudo-qualifiers. */ + + /* Check for size overflow and determine alignment. */ + if (sz >= 0x20000000u || bofs + csz < bofs) { + if (!(sz == CTSIZE_INVALID && ctype_isarray(info) && + !(sinfo & CTF_UNION))) + cp_err(cp, LJ_ERR_FFI_INVSIZE); + csz = sz = 0; /* Treat a[] and a[?] as zero-sized. */ + } + align = cp_field_align(cp, ct, info); + if (((attr|sattr) & CTFP_PACKED) || + ((attr & CTFP_ALIGNED) && ctype_align(attr) > align)) + align = ctype_align(attr); + if (cp->packstack[cp->curpack] < align) + align = cp->packstack[cp->curpack]; + if (align > maxalign) maxalign = align; + amask = (8u << align) - 1; + + bsz = ctype_bitcsz(ct->info); /* Bitfield size (temp.). */ + if (bsz == CTBSZ_FIELD || !ctype_isfield(ct->info)) { + bsz = csz; /* Regular fields or subtypes always fill the container. */ + bofs = (bofs + amask) & ~amask; /* Start new aligned field. */ + ct->size = (bofs >> 3); /* Store field offset. */ + } else { /* Bitfield. */ + if (bsz == 0 || (attr & CTFP_ALIGNED) || + (!((attr|sattr) & CTFP_PACKED) && (bofs & amask) + bsz > csz)) + bofs = (bofs + amask) & ~amask; /* Start new aligned field. */ + + /* Prefer regular field over bitfield. */ + if (bsz == csz && (bofs & amask) == 0) { + ct->info = CTINFO(CT_FIELD, ctype_cid(ct->info)); + ct->size = (bofs >> 3); /* Store field offset. */ + } else { + ct->info = CTINFO(CT_BITFIELD, + (info & (CTF_QUAL|CTF_UNSIGNED|CTF_BOOL)) + + (csz << (CTSHIFT_BITCSZ-3)) + (bsz << CTSHIFT_BITBSZ)); +#if LJ_BE + ct->info += ((csz - (bofs & (csz-1)) - bsz) << CTSHIFT_BITPOS); +#else + ct->info += ((bofs & (csz-1)) << CTSHIFT_BITPOS); +#endif + ct->size = ((bofs & ~(csz-1)) >> 3); /* Store container offset. */ + } + } + + /* Determine next offset or max. offset. */ + if ((sinfo & CTF_UNION)) { + if (bsz > bmaxofs) bmaxofs = bsz; + } else { + bofs += bsz; + } + } /* All other fields in the chain are already set up. */ + + fieldid = ct->sib; + } + + /* Complete struct/union. */ + sct->info = sinfo + CTALIGN(maxalign); + bofs = (sinfo & CTF_UNION) ? bmaxofs : bofs; + maxalign = (8u << maxalign) - 1; + sct->size = (((bofs + maxalign) & ~maxalign) >> 3); +} + +/* Parse struct/union declaration. */ +static CTypeID cp_decl_struct(CPState *cp, CPDecl *sdecl, CTInfo sinfo) +{ + CTypeID sid = cp_struct_name(cp, sdecl, sinfo); + if (cp_opt(cp, '{')) { /* Struct/union definition. */ + CTypeID lastid = sid; + int lastdecl = 0; + while (cp->tok != '}') { + CPDecl decl; + CPscl scl = cp_decl_spec(cp, &decl, CDF_STATIC); + decl.mode = scl ? CPARSE_MODE_DIRECT : + CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT|CPARSE_MODE_FIELD; + + for (;;) { + CTypeID ctypeid; + + if (lastdecl) cp_err_token(cp, '}'); + + /* Parse field declarator. */ + decl.bits = CTSIZE_INVALID; + cp_declarator(cp, &decl); + ctypeid = cp_decl_intern(cp, &decl); + + if ((scl & CDF_STATIC)) { /* Static constant in struct namespace. */ + CType *ct; + CTypeID fieldid = cp_decl_constinit(cp, &ct, ctypeid); + ctype_get(cp->cts, lastid)->sib = fieldid; + lastid = fieldid; + ctype_setname(ct, decl.name); + } else { + CTSize bsz = CTBSZ_FIELD; /* Temp. for layout phase. */ + CType *ct; + CTypeID fieldid = lj_ctype_new(cp->cts, &ct); /* Do this first. */ + CType *tct = ctype_raw(cp->cts, ctypeid); + + if (decl.bits == CTSIZE_INVALID) { /* Regular field. */ + if (ctype_isarray(tct->info) && tct->size == CTSIZE_INVALID) + lastdecl = 1; /* a[] or a[?] must be the last declared field. */ + + /* Accept transparent struct/union/enum. */ + if (!decl.name) { + if (!((ctype_isstruct(tct->info) && !(tct->info & CTF_VLA)) || + ctype_isenum(tct->info))) + cp_err_token(cp, CTOK_IDENT); + ct->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_SUBTYPE) + ctypeid); + ct->size = ctype_isstruct(tct->info) ? + (decl.attr|0x80000000u) : 0; /* For layout phase. */ + goto add_field; + } + } else { /* Bitfield. */ + bsz = decl.bits; + if (!ctype_isinteger_or_bool(tct->info) || + (bsz == 0 && decl.name) || 8*tct->size > CTBSZ_MAX || + bsz > ((tct->info & CTF_BOOL) ? 1 : 8*tct->size)) + cp_errmsg(cp, ':', LJ_ERR_BADVAL); + } + + /* Create temporary field for layout phase. */ + ct->info = CTINFO(CT_FIELD, ctypeid + (bsz << CTSHIFT_BITCSZ)); + ct->size = decl.attr; + if (decl.name) ctype_setname(ct, decl.name); + + add_field: + ctype_get(cp->cts, lastid)->sib = fieldid; + lastid = fieldid; + } + if (!cp_opt(cp, ',')) break; + cp_decl_reset(&decl); + } + cp_check(cp, ';'); + } + cp_check(cp, '}'); + ctype_get(cp->cts, lastid)->sib = 0; /* Drop sib = 1 for empty structs. */ + cp_decl_attributes(cp, sdecl); /* Layout phase needs postfix attributes. */ + cp_struct_layout(cp, sid, sdecl->attr); + } + return sid; +} + +/* Parse enum declaration. */ +static CTypeID cp_decl_enum(CPState *cp, CPDecl *sdecl) +{ + CTypeID eid = cp_struct_name(cp, sdecl, CTINFO(CT_ENUM, CTID_VOID)); + CTInfo einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_UINT32); + CTSize esize = 4; /* Only 32 bit enums are supported. */ + if (cp_opt(cp, '{')) { /* Enum definition. */ + CPValue k; + CTypeID lastid = eid; + k.u32 = 0; + k.id = CTID_INT32; + do { + GCstr *name = cp->str; + if (cp->tok != CTOK_IDENT) cp_err_token(cp, CTOK_IDENT); + if (cp->val.id) cp_errmsg(cp, 0, LJ_ERR_FFI_REDEF, strdata(name)); + cp_next(cp); + if (cp_opt(cp, '=')) { + cp_expr_kint(cp, &k); + if (k.id == CTID_UINT32) { + /* C99 says that enum constants are always (signed) integers. + ** But since unsigned constants like 0x80000000 are quite common, + ** those are left as uint32_t. + */ + if (k.i32 >= 0) k.id = CTID_INT32; + } else { + /* OTOH it's common practice and even mandated by some ABIs + ** that the enum type itself is unsigned, unless there are any + ** negative constants. + */ + k.id = CTID_INT32; + if (k.i32 < 0) einfo = CTINFO(CT_ENUM, CTALIGN(2) + CTID_INT32); + } + } + /* Add named enum constant. */ + { + CType *ct; + CTypeID constid = lj_ctype_new(cp->cts, &ct); + ctype_get(cp->cts, lastid)->sib = constid; + lastid = constid; + ctype_setname(ct, name); + ct->info = CTINFO(CT_CONSTVAL, CTF_CONST|k.id); + ct->size = k.u32++; + if (k.u32 == 0x80000000u) k.id = CTID_UINT32; + lj_ctype_addname(cp->cts, ct, constid); + } + if (!cp_opt(cp, ',')) break; + } while (cp->tok != '}'); /* Trailing ',' is ok. */ + cp_check(cp, '}'); + /* Complete enum. */ + ctype_get(cp->cts, eid)->info = einfo; + ctype_get(cp->cts, eid)->size = esize; + } + return eid; +} + +/* Parse declaration specifiers. */ +static CPscl cp_decl_spec(CPState *cp, CPDecl *decl, CPscl scl) +{ + uint32_t cds = 0, sz = 0; + CTypeID tdef = 0; + + decl->cp = cp; + decl->mode = cp->mode; + decl->name = NULL; + decl->redir = NULL; + decl->attr = 0; + decl->fattr = 0; + decl->pos = decl->top = 0; + decl->stack[0].next = 0; + + for (;;) { /* Parse basic types. */ + cp_decl_attributes(cp, decl); + if (cp->tok >= CTOK_FIRSTDECL && cp->tok <= CTOK_LASTDECLFLAG) { + uint32_t cbit; + if (cp->ct->size) { + if (sz) goto end_decl; + sz = cp->ct->size; + } + cbit = (1u << (cp->tok - CTOK_FIRSTDECL)); + cds = cds | cbit | ((cbit & cds & CDF_LONG) << 1); + if (cp->tok >= CTOK_FIRSTSCL) { + if (!(scl & cbit)) cp_errmsg(cp, cp->tok, LJ_ERR_FFI_BADSCL); + } else if (tdef) { + goto end_decl; + } + cp_next(cp); + continue; + } + if (sz || tdef || + (cds & (CDF_SHORT|CDF_LONG|CDF_SIGNED|CDF_UNSIGNED|CDF_COMPLEX))) + break; + switch (cp->tok) { + case CTOK_STRUCT: + tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, 0)); + continue; + case CTOK_UNION: + tdef = cp_decl_struct(cp, decl, CTINFO(CT_STRUCT, CTF_UNION)); + continue; + case CTOK_ENUM: + tdef = cp_decl_enum(cp, decl); + continue; + case CTOK_IDENT: + if (ctype_istypedef(cp->ct->info)) { + tdef = ctype_cid(cp->ct->info); /* Get typedef. */ + cp_next(cp); + continue; + } + break; + case '$': + tdef = cp->val.id; + cp_next(cp); + continue; + default: + break; + } + break; + } +end_decl: + + if ((cds & CDF_COMPLEX)) /* Use predefined complex types. */ + tdef = sz == 4 ? CTID_COMPLEX_FLOAT : CTID_COMPLEX_DOUBLE; + + if (tdef) { + cp_push_type(decl, tdef); + } else if ((cds & CDF_VOID)) { + cp_push(decl, CTINFO(CT_VOID, (decl->attr & CTF_QUAL)), CTSIZE_INVALID); + decl->attr &= ~CTF_QUAL; + } else { + /* Determine type info and size. */ + CTInfo info = CTINFO(CT_NUM, (cds & CDF_UNSIGNED) ? CTF_UNSIGNED : 0); + if ((cds & CDF_BOOL)) { + if ((cds & ~(CDF_SCL|CDF_BOOL|CDF_INT|CDF_SIGNED|CDF_UNSIGNED))) + cp_errmsg(cp, 0, LJ_ERR_FFI_INVTYPE); + info |= CTF_BOOL; + if (!(cds & CDF_SIGNED)) info |= CTF_UNSIGNED; + if (!sz) { + sz = 1; + } + } else if ((cds & CDF_FP)) { + info = CTINFO(CT_NUM, CTF_FP); + if ((cds & CDF_LONG)) sz = sizeof(long double); + } else if ((cds & CDF_CHAR)) { + if ((cds & (CDF_CHAR|CDF_SIGNED|CDF_UNSIGNED)) == CDF_CHAR) + info |= CTF_UCHAR; /* Handle platforms where char is unsigned. */ + } else if ((cds & CDF_SHORT)) { + sz = sizeof(short); + } else if ((cds & CDF_LONGLONG)) { + sz = 8; + } else if ((cds & CDF_LONG)) { + info |= CTF_LONG; + sz = sizeof(long); + } else if (!sz) { + if (!(cds & (CDF_SIGNED|CDF_UNSIGNED))) + cp_errmsg(cp, cp->tok, LJ_ERR_FFI_DECLSPEC); + sz = sizeof(int); + } + lua_assert(sz != 0); + info += CTALIGN(lj_fls(sz)); /* Use natural alignment. */ + info += (decl->attr & CTF_QUAL); /* Merge qualifiers. */ + cp_push(decl, info, sz); + decl->attr &= ~CTF_QUAL; + } + decl->specpos = decl->pos; + decl->specattr = decl->attr; + decl->specfattr = decl->fattr; + return (cds & CDF_SCL); /* Return storage class. */ +} + +/* Parse array declaration. */ +static void cp_decl_array(CPState *cp, CPDecl *decl) +{ + CTInfo info = CTINFO(CT_ARRAY, 0); + CTSize nelem = CTSIZE_INVALID; /* Default size for a[] or a[?]. */ + cp_decl_attributes(cp, decl); + if (cp_opt(cp, '?')) + info |= CTF_VLA; /* Create variable-length array a[?]. */ + else if (cp->tok != ']') + nelem = cp_expr_ksize(cp); + cp_check(cp, ']'); + cp_add(decl, info, nelem); +} + +/* Parse function declaration. */ +static void cp_decl_func(CPState *cp, CPDecl *fdecl) +{ + CTSize nargs = 0; + CTInfo info = CTINFO(CT_FUNC, 0); + CTypeID lastid = 0, anchor = 0; + if (cp->tok != ')') { + do { + CPDecl decl; + CTypeID ctypeid, fieldid; + CType *ct; + if (cp_opt(cp, '.')) { /* Vararg function. */ + cp_check(cp, '.'); /* Workaround for the minimalistic lexer. */ + cp_check(cp, '.'); + info |= CTF_VARARG; + break; + } + cp_decl_spec(cp, &decl, CDF_REGISTER); + decl.mode = CPARSE_MODE_DIRECT|CPARSE_MODE_ABSTRACT; + cp_declarator(cp, &decl); + ctypeid = cp_decl_intern(cp, &decl); + ct = ctype_raw(cp->cts, ctypeid); + if (ctype_isvoid(ct->info)) + break; + else if (ctype_isrefarray(ct->info)) + ctypeid = lj_ctype_intern(cp->cts, + CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ct->info)), CTSIZE_PTR); + else if (ctype_isfunc(ct->info)) + ctypeid = lj_ctype_intern(cp->cts, + CTINFO(CT_PTR, CTALIGN_PTR|ctypeid), CTSIZE_PTR); + /* Add new parameter. */ + fieldid = lj_ctype_new(cp->cts, &ct); + if (anchor) + ctype_get(cp->cts, lastid)->sib = fieldid; + else + anchor = fieldid; + lastid = fieldid; + if (decl.name) ctype_setname(ct, decl.name); + ct->info = CTINFO(CT_FIELD, ctypeid); + ct->size = nargs++; + } while (cp_opt(cp, ',')); + } + cp_check(cp, ')'); + if (cp_opt(cp, '{')) { /* Skip function definition. */ + int level = 1; + cp->mode |= CPARSE_MODE_SKIP; + for (;;) { + if (cp->tok == '{') level++; + else if (cp->tok == '}' && --level == 0) break; + else if (cp->tok == CTOK_EOF) cp_err_token(cp, '}'); + cp_next(cp); + } + cp->mode &= ~CPARSE_MODE_SKIP; + cp->tok = ';'; /* Ok for cp_decl_multi(), error in cp_decl_single(). */ + } + info |= (fdecl->fattr & ~CTMASK_CID); + fdecl->fattr = 0; + fdecl->stack[cp_add(fdecl, info, nargs)].sib = anchor; +} + +/* Parse declarator. */ +static void cp_declarator(CPState *cp, CPDecl *decl) +{ + if (++cp->depth > CPARSE_MAX_DECLDEPTH) cp_err(cp, LJ_ERR_XLEVELS); + + for (;;) { /* Head of declarator. */ + if (cp_opt(cp, '*')) { /* Pointer. */ + CTSize sz; + CTInfo info; + cp_decl_attributes(cp, decl); + sz = CTSIZE_PTR; + info = CTINFO(CT_PTR, CTALIGN_PTR); +#if LJ_64 + if (ctype_msizeP(decl->attr) == 4) { + sz = 4; + info = CTINFO(CT_PTR, CTALIGN(2)); + } +#endif + info += (decl->attr & (CTF_QUAL|CTF_REF)); + decl->attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<attr &= ~(CTF_QUAL|(CTMASK_MSIZEP<mode & CPARSE_MODE_ABSTRACT) && + (cp->tok == ')' || cp_istypedecl(cp))) goto func_decl; + pos = decl->pos; + cp_declarator(cp, decl); + cp_check(cp, ')'); + decl->pos = pos; + } else if (cp->tok == CTOK_IDENT) { /* Direct declarator. */ + if (!(decl->mode & CPARSE_MODE_DIRECT)) cp_err_token(cp, CTOK_EOF); + decl->name = cp->str; + decl->nameid = cp->val.id; + cp_next(cp); + } else { /* Abstract declarator. */ + if (!(decl->mode & CPARSE_MODE_ABSTRACT)) cp_err_token(cp, CTOK_IDENT); + } + + for (;;) { /* Tail of declarator. */ + if (cp_opt(cp, '[')) { /* Array. */ + cp_decl_array(cp, decl); + } else if (cp_opt(cp, '(')) { /* Function. */ + func_decl: + cp_decl_func(cp, decl); + } else { + break; + } + } + + if ((decl->mode & CPARSE_MODE_FIELD) && cp_opt(cp, ':')) /* Field width. */ + decl->bits = cp_expr_ksize(cp); + + /* Process postfix attributes. */ + cp_decl_attributes(cp, decl); + cp_push_attributes(decl); + + cp->depth--; +} + +/* Parse an abstract type declaration and return it's C type ID. */ +static CTypeID cp_decl_abstract(CPState *cp) +{ + CPDecl decl; + cp_decl_spec(cp, &decl, 0); + decl.mode = CPARSE_MODE_ABSTRACT; + cp_declarator(cp, &decl); + return cp_decl_intern(cp, &decl); +} + +/* Handle pragmas. */ +static void cp_pragma(CPState *cp, BCLine pragmaline) +{ + cp_next(cp); + if (cp->tok == CTOK_IDENT && + cp->str->hash == H_(e79b999f,42ca3e85)) { /* pack */ + cp_next(cp); + cp_check(cp, '('); + if (cp->tok == CTOK_IDENT) { + if (cp->str->hash == H_(738e923c,a1b65954)) { /* push */ + if (cp->curpack < CPARSE_MAX_PACKSTACK) { + cp->packstack[cp->curpack+1] = cp->packstack[cp->curpack]; + cp->curpack++; + } + } else if (cp->str->hash == H_(6c71cf27,6c71cf27)) { /* pop */ + if (cp->curpack > 0) cp->curpack--; + } else { + cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); + } + cp_next(cp); + if (!cp_opt(cp, ',')) goto end_pack; + } + if (cp->tok == CTOK_INTEGER) { + cp->packstack[cp->curpack] = cp->val.u32 ? lj_fls(cp->val.u32) : 0; + cp_next(cp); + } else { + cp->packstack[cp->curpack] = 255; + } + end_pack: + cp_check(cp, ')'); + } else { /* Ignore all other pragmas. */ + while (cp->tok != CTOK_EOF && cp->linenumber == pragmaline) + cp_next(cp); + } +} + +/* Parse multiple C declarations of types or extern identifiers. */ +static void cp_decl_multi(CPState *cp) +{ + int first = 1; + while (cp->tok != CTOK_EOF) { + CPDecl decl; + CPscl scl; + if (cp_opt(cp, ';')) { /* Skip empty statements. */ + first = 0; + continue; + } + if (cp->tok == '#') { /* Workaround, since we have no preprocessor, yet. */ + BCLine pragmaline = cp->linenumber; + if (!(cp_next(cp) == CTOK_IDENT && + cp->str->hash == H_(f5e6b4f8,1d509107))) /* pragma */ + cp_errmsg(cp, cp->tok, LJ_ERR_XSYMBOL); + cp_pragma(cp, pragmaline); + continue; + } + scl = cp_decl_spec(cp, &decl, CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC); + if ((cp->tok == ';' || cp->tok == CTOK_EOF) && + ctype_istypedef(decl.stack[0].info)) { + CTInfo info = ctype_rawchild(cp->cts, &decl.stack[0])->info; + if (ctype_isstruct(info) || ctype_isenum(info)) + goto decl_end; /* Accept empty declaration of struct/union/enum. */ + } + for (;;) { + CTypeID ctypeid; + cp_declarator(cp, &decl); + ctypeid = cp_decl_intern(cp, &decl); + if (decl.name && !decl.nameid) { /* NYI: redeclarations are ignored. */ + CType *ct; + CTypeID id; + if ((scl & CDF_TYPEDEF)) { /* Create new typedef. */ + id = lj_ctype_new(cp->cts, &ct); + ct->info = CTINFO(CT_TYPEDEF, ctypeid); + goto noredir; + } else if (ctype_isfunc(ctype_get(cp->cts, ctypeid)->info)) { + /* Treat both static and extern function declarations as extern. */ + ct = ctype_get(cp->cts, ctypeid); + /* We always get new anonymous functions (typedefs are copied). */ + lua_assert(gcref(ct->name) == NULL); + id = ctypeid; /* Just name it. */ + } else if ((scl & CDF_STATIC)) { /* Accept static constants. */ + id = cp_decl_constinit(cp, &ct, ctypeid); + goto noredir; + } else { /* External references have extern or no storage class. */ + id = lj_ctype_new(cp->cts, &ct); + ct->info = CTINFO(CT_EXTERN, ctypeid); + } + if (decl.redir) { /* Add attribute for redirected symbol name. */ + CType *cta; + CTypeID aid = lj_ctype_new(cp->cts, &cta); + ct = ctype_get(cp->cts, id); /* Table may have been reallocated. */ + cta->info = CTINFO(CT_ATTRIB, CTATTRIB(CTA_REDIR)); + cta->sib = ct->sib; + ct->sib = aid; + ctype_setname(cta, decl.redir); + } + noredir: + ctype_setname(ct, decl.name); + lj_ctype_addname(cp->cts, ct, id); + } + if (!cp_opt(cp, ',')) break; + cp_decl_reset(&decl); + } + decl_end: + if (cp->tok == CTOK_EOF && first) break; /* May omit ';' for 1 decl. */ + first = 0; + cp_check(cp, ';'); + } +} + +/* Parse a single C type declaration. */ +static void cp_decl_single(CPState *cp) +{ + CPDecl decl; + cp_decl_spec(cp, &decl, 0); + cp_declarator(cp, &decl); + cp->val.id = cp_decl_intern(cp, &decl); + if (cp->tok != CTOK_EOF) cp_err_token(cp, CTOK_EOF); +} + +#undef H_ + +/* ------------------------------------------------------------------------ */ + +/* Protected callback for C parser. */ +static TValue *cpcparser(lua_State *L, lua_CFunction dummy, void *ud) +{ + CPState *cp = (CPState *)ud; + UNUSED(dummy); + cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ + cp_init(cp); + if ((cp->mode & CPARSE_MODE_MULTI)) + cp_decl_multi(cp); + else + cp_decl_single(cp); + if (cp->param && cp->param != cp->L->top) + cp_err(cp, LJ_ERR_FFI_NUMPARAM); + lua_assert(cp->depth == 0); + return NULL; +} + +/* C parser. */ +int lj_cparse(CPState *cp) +{ + LJ_CTYPE_SAVE(cp->cts); + int errcode = lj_vm_cpcall(cp->L, NULL, cp, cpcparser); + if (errcode) + LJ_CTYPE_RESTORE(cp->cts); + cp_cleanup(cp); + return errcode; +} + +#endif diff --git a/external/lua/luajit/src/src/lj_cparse.h b/external/lua/luajit/src/src/lj_cparse.h new file mode 100644 index 0000000000..c097b14a6e --- /dev/null +++ b/external/lua/luajit/src/src/lj_cparse.h @@ -0,0 +1,65 @@ +/* +** C declaration parser. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_CPARSE_H +#define _LJ_CPARSE_H + +#include "lj_obj.h" +#include "lj_ctype.h" + +#if LJ_HASFFI + +/* C parser limits. */ +#define CPARSE_MAX_BUF 32768 /* Max. token buffer size. */ +#define CPARSE_MAX_DECLSTACK 100 /* Max. declaration stack depth. */ +#define CPARSE_MAX_DECLDEPTH 20 /* Max. recursive declaration depth. */ +#define CPARSE_MAX_PACKSTACK 7 /* Max. pack pragma stack depth. */ + +/* Flags for C parser mode. */ +#define CPARSE_MODE_MULTI 1 /* Process multiple declarations. */ +#define CPARSE_MODE_ABSTRACT 2 /* Accept abstract declarators. */ +#define CPARSE_MODE_DIRECT 4 /* Accept direct declarators. */ +#define CPARSE_MODE_FIELD 8 /* Accept field width in bits, too. */ +#define CPARSE_MODE_NOIMPLICIT 16 /* Reject implicit declarations. */ +#define CPARSE_MODE_SKIP 32 /* Skip definitions, ignore errors. */ + +typedef int CPChar; /* C parser character. Unsigned ext. from char. */ +typedef int CPToken; /* C parser token. */ + +/* C parser internal value representation. */ +typedef struct CPValue { + union { + int32_t i32; /* Value for CTID_INT32. */ + uint32_t u32; /* Value for CTID_UINT32. */ + }; + CTypeID id; /* C Type ID of the value. */ +} CPValue; + +/* C parser state. */ +typedef struct CPState { + CPChar c; /* Current character. */ + CPToken tok; /* Current token. */ + CPValue val; /* Token value. */ + GCstr *str; /* Interned string of identifier/keyword. */ + CType *ct; /* C type table entry. */ + const char *p; /* Current position in input buffer. */ + SBuf sb; /* String buffer for tokens. */ + lua_State *L; /* Lua state. */ + CTState *cts; /* C type state. */ + TValue *param; /* C type parameters. */ + const char *srcname; /* Current source name. */ + BCLine linenumber; /* Input line counter. */ + int depth; /* Recursive declaration depth. */ + uint32_t tmask; /* Type mask for next identifier. */ + uint32_t mode; /* C parser mode. */ + uint8_t packstack[CPARSE_MAX_PACKSTACK]; /* Stack for pack pragmas. */ + uint8_t curpack; /* Current position in pack pragma stack. */ +} CPState; + +LJ_FUNC int lj_cparse(CPState *cp); + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_crecord.c b/external/lua/luajit/src/src/lj_crecord.c new file mode 100644 index 0000000000..b60eb7b3c1 --- /dev/null +++ b/external/lua/luajit/src/src/lj_crecord.c @@ -0,0 +1,1653 @@ +/* +** Trace recorder for C data operations. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_ffrecord_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT && LJ_HASFFI + +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_frame.h" +#include "lj_ctype.h" +#include "lj_cdata.h" +#include "lj_cparse.h" +#include "lj_cconv.h" +#include "lj_clib.h" +#include "lj_ccall.h" +#include "lj_ff.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_ircall.h" +#include "lj_iropt.h" +#include "lj_trace.h" +#include "lj_record.h" +#include "lj_ffrecord.h" +#include "lj_snap.h" +#include "lj_crecord.h" +#include "lj_dispatch.h" + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) + +/* Pass IR on to next optimization in chain (FOLD). */ +#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) + +#define emitconv(a, dt, st, flags) \ + emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags)) + +/* -- C type checks ------------------------------------------------------- */ + +static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o) +{ + GCcdata *cd; + TRef trtypeid; + if (!tref_iscdata(tr)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + cd = cdataV(o); + /* Specialize to the CTypeID. */ + trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_CTYPEID); + emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->ctypeid)); + return cd; +} + +/* Specialize to the CTypeID held by a cdata constructor. */ +static CTypeID crec_constructor(jit_State *J, GCcdata *cd, TRef tr) +{ + CTypeID id; + lua_assert(tref_iscdata(tr) && cd->ctypeid == CTID_CTYPEID); + id = *(CTypeID *)cdataptr(cd); + tr = emitir(IRT(IR_FLOAD, IRT_INT), tr, IRFL_CDATA_INT); + emitir(IRTG(IR_EQ, IRT_INT), tr, lj_ir_kint(J, (int32_t)id)); + return id; +} + +static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o) +{ + if (tref_isstr(tr)) { + GCstr *s = strV(o); + CPState cp; + CTypeID oldtop; + /* Specialize to the string containing the C type declaration. */ + emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s)); + cp.L = J->L; + cp.cts = ctype_ctsG(J2G(J)); + oldtop = cp.cts->top; + cp.srcname = strdata(s); + cp.p = strdata(s); + cp.param = NULL; + cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT; + if (lj_cparse(&cp) || cp.cts->top > oldtop) /* Avoid new struct defs. */ + lj_trace_err(J, LJ_TRERR_BADTYPE); + return cp.val.id; + } else { + GCcdata *cd = argv2cdata(J, tr, o); + return cd->ctypeid == CTID_CTYPEID ? crec_constructor(J, cd, tr) : + cd->ctypeid; + } +} + +/* Convert CType to IRType (if possible). */ +static IRType crec_ct2irt(CTState *cts, CType *ct) +{ + if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); + if (LJ_LIKELY(ctype_isnum(ct->info))) { + if ((ct->info & CTF_FP)) { + if (ct->size == sizeof(double)) + return IRT_NUM; + else if (ct->size == sizeof(float)) + return IRT_FLOAT; + } else { + uint32_t b = lj_fls(ct->size); + if (b <= 3) + return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0); + } + } else if (ctype_isptr(ct->info)) { + return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32; + } else if (ctype_iscomplex(ct->info)) { + if (ct->size == 2*sizeof(double)) + return IRT_NUM; + else if (ct->size == 2*sizeof(float)) + return IRT_FLOAT; + } + return IRT_CDATA; +} + +/* -- Optimized memory fill and copy -------------------------------------- */ + +/* Maximum length and unroll of inlined copy/fill. */ +#define CREC_COPY_MAXUNROLL 16 +#define CREC_COPY_MAXLEN 128 + +#define CREC_FILL_MAXUNROLL 16 +#if LJ_TARGET_UNALIGNED +#define CREC_FILL_MAXLEN (CTSIZE_PTR * CREC_FILL_MAXUNROLL) +#else +#define CREC_FILL_MAXLEN CREC_FILL_MAXUNROLL +#endif + +/* Number of windowed registers used for optimized memory copy. */ +#if LJ_TARGET_X86 +#define CREC_COPY_REGWIN 2 +#elif LJ_TARGET_PPC || LJ_TARGET_MIPS +#define CREC_COPY_REGWIN 8 +#else +#define CREC_COPY_REGWIN 4 +#endif + +/* List of memory offsets for copy/fill. */ +typedef struct CRecMemList { + CTSize ofs; /* Offset in bytes. */ + IRType tp; /* Type of load/store. */ + TRef trofs; /* TRef of interned offset. */ + TRef trval; /* TRef of load value. */ +} CRecMemList; + +/* Generate copy list for element-wise struct copy. */ +static MSize crec_copy_struct(CRecMemList *ml, CTState *cts, CType *ct) +{ + CTypeID fid = ct->sib; + MSize mlp = 0; + while (fid) { + CType *df = ctype_get(cts, fid); + fid = df->sib; + if (ctype_isfield(df->info)) { + CType *cct; + IRType tp; + if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ + cct = ctype_rawchild(cts, df); /* Field type. */ + tp = crec_ct2irt(cts, cct); + if (tp == IRT_CDATA) return 0; /* NYI: aggregates. */ + if (mlp >= CREC_COPY_MAXUNROLL) return 0; + ml[mlp].ofs = df->size; + ml[mlp].tp = tp; + mlp++; + if (ctype_iscomplex(cct->info)) { + if (mlp >= CREC_COPY_MAXUNROLL) return 0; + ml[mlp].ofs = df->size + (cct->size >> 1); + ml[mlp].tp = tp; + mlp++; + } + } else if (!ctype_isconstval(df->info)) { + /* NYI: bitfields and sub-structures. */ + return 0; + } + } + return mlp; +} + +/* Generate unrolled copy list, from highest to lowest step size/alignment. */ +static MSize crec_copy_unroll(CRecMemList *ml, CTSize len, CTSize step, + IRType tp) +{ + CTSize ofs = 0; + MSize mlp = 0; + if (tp == IRT_CDATA) tp = IRT_U8 + 2*lj_fls(step); + do { + while (ofs + step <= len) { + if (mlp >= CREC_COPY_MAXUNROLL) return 0; + ml[mlp].ofs = ofs; + ml[mlp].tp = tp; + mlp++; + ofs += step; + } + step >>= 1; + tp -= 2; + } while (ofs < len); + return mlp; +} + +/* +** Emit copy list with windowed loads/stores. +** LJ_TARGET_UNALIGNED: may emit unaligned loads/stores (not marked as such). +*/ +static void crec_copy_emit(jit_State *J, CRecMemList *ml, MSize mlp, + TRef trdst, TRef trsrc) +{ + MSize i, j, rwin = 0; + for (i = 0, j = 0; i < mlp; ) { + TRef trofs = lj_ir_kintp(J, ml[i].ofs); + TRef trsptr = emitir(IRT(IR_ADD, IRT_PTR), trsrc, trofs); + ml[i].trval = emitir(IRT(IR_XLOAD, ml[i].tp), trsptr, 0); + ml[i].trofs = trofs; + i++; + rwin += (LJ_SOFTFP && ml[i].tp == IRT_NUM) ? 2 : 1; + if (rwin >= CREC_COPY_REGWIN || i >= mlp) { /* Flush buffered stores. */ + rwin = 0; + for ( ; j < i; j++) { + TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, ml[j].trofs); + emitir(IRT(IR_XSTORE, ml[j].tp), trdptr, ml[j].trval); + } + } + } +} + +/* Optimized memory copy. */ +static void crec_copy(jit_State *J, TRef trdst, TRef trsrc, TRef trlen, + CType *ct) +{ + if (tref_isk(trlen)) { /* Length must be constant. */ + CRecMemList ml[CREC_COPY_MAXUNROLL]; + MSize mlp = 0; + CTSize step = 1, len = (CTSize)IR(tref_ref(trlen))->i; + IRType tp = IRT_CDATA; + int needxbar = 0; + if (len == 0) return; /* Shortcut. */ + if (len > CREC_COPY_MAXLEN) goto fallback; + if (ct) { + CTState *cts = ctype_ctsG(J2G(J)); + lua_assert(ctype_isarray(ct->info) || ctype_isstruct(ct->info)); + if (ctype_isarray(ct->info)) { + CType *cct = ctype_rawchild(cts, ct); + tp = crec_ct2irt(cts, cct); + if (tp == IRT_CDATA) goto rawcopy; + step = lj_ir_type_size[tp]; + lua_assert((len & (step-1)) == 0); + } else if ((ct->info & CTF_UNION)) { + step = (1u << ctype_align(ct->info)); + goto rawcopy; + } else { + mlp = crec_copy_struct(ml, cts, ct); + goto emitcopy; + } + } else { + rawcopy: + needxbar = 1; + if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR) + step = CTSIZE_PTR; + } + mlp = crec_copy_unroll(ml, len, step, tp); + emitcopy: + if (mlp) { + crec_copy_emit(J, ml, mlp, trdst, trsrc); + if (needxbar) + emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); + return; + } + } +fallback: + /* Call memcpy. Always needs a barrier to disable alias analysis. */ + lj_ir_call(J, IRCALL_memcpy, trdst, trsrc, trlen); + emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); +} + +/* Generate unrolled fill list, from highest to lowest step size/alignment. */ +static MSize crec_fill_unroll(CRecMemList *ml, CTSize len, CTSize step) +{ + CTSize ofs = 0; + MSize mlp = 0; + IRType tp = IRT_U8 + 2*lj_fls(step); + do { + while (ofs + step <= len) { + if (mlp >= CREC_COPY_MAXUNROLL) return 0; + ml[mlp].ofs = ofs; + ml[mlp].tp = tp; + mlp++; + ofs += step; + } + step >>= 1; + tp -= 2; + } while (ofs < len); + return mlp; +} + +/* +** Emit stores for fill list. +** LJ_TARGET_UNALIGNED: may emit unaligned stores (not marked as such). +*/ +static void crec_fill_emit(jit_State *J, CRecMemList *ml, MSize mlp, + TRef trdst, TRef trfill) +{ + MSize i; + for (i = 0; i < mlp; i++) { + TRef trofs = lj_ir_kintp(J, ml[i].ofs); + TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, trofs); + emitir(IRT(IR_XSTORE, ml[i].tp), trdptr, trfill); + } +} + +/* Optimized memory fill. */ +static void crec_fill(jit_State *J, TRef trdst, TRef trlen, TRef trfill, + CTSize step) +{ + if (tref_isk(trlen)) { /* Length must be constant. */ + CRecMemList ml[CREC_FILL_MAXUNROLL]; + MSize mlp; + CTSize len = (CTSize)IR(tref_ref(trlen))->i; + if (len == 0) return; /* Shortcut. */ + if (len > CREC_FILL_MAXLEN) goto fallback; + if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR) + step = CTSIZE_PTR; + mlp = crec_fill_unroll(ml, len, step); + if (!mlp) goto fallback; + if (tref_isk(trfill) || ml[0].tp != IRT_U8) + trfill = emitconv(trfill, IRT_INT, IRT_U8, 0); + if (ml[0].tp != IRT_U8) { /* Scatter U8 to U16/U32/U64. */ + if (CTSIZE_PTR == 8 && ml[0].tp == IRT_U64) { + if (tref_isk(trfill)) /* Pointless on x64 with zero-extended regs. */ + trfill = emitconv(trfill, IRT_U64, IRT_U32, 0); + trfill = emitir(IRT(IR_MUL, IRT_U64), trfill, + lj_ir_kint64(J, U64x(01010101,01010101))); + } else { + trfill = emitir(IRTI(IR_MUL), trfill, + lj_ir_kint(J, ml[0].tp == IRT_U16 ? 0x0101 : 0x01010101)); + } + } + crec_fill_emit(J, ml, mlp, trdst, trfill); + } else { +fallback: + /* Call memset. Always needs a barrier to disable alias analysis. */ + lj_ir_call(J, IRCALL_memset, trdst, trfill, trlen); /* Note: arg order! */ + } + emitir(IRT(IR_XBAR, IRT_NIL), 0, 0); +} + +/* -- Convert C type to C type -------------------------------------------- */ + +/* +** This code mirrors the code in lj_cconv.c. It performs the same steps +** for the trace recorder that lj_cconv.c does for the interpreter. +** +** One major difference is that we can get away with much fewer checks +** here. E.g. checks for casts, constness or correct types can often be +** omitted, even if they might fail. The interpreter subsequently throws +** an error, which aborts the trace. +** +** All operations are specialized to their C types, so the on-trace +** outcome must be the same as the outcome in the interpreter. If the +** interpreter doesn't throw an error, then the trace is correct, too. +** Care must be taken not to generate invalid (temporary) IR or to +** trigger asserts. +*/ + +/* Determine whether a passed number or cdata number is non-zero. */ +static int crec_isnonzero(CType *s, void *p) +{ + if (p == (void *)0) + return 0; + if (p == (void *)1) + return 1; + if ((s->info & CTF_FP)) { + if (s->size == sizeof(float)) + return (*(float *)p != 0); + else + return (*(double *)p != 0); + } else { + if (s->size == 1) + return (*(uint8_t *)p != 0); + else if (s->size == 2) + return (*(uint16_t *)p != 0); + else if (s->size == 4) + return (*(uint32_t *)p != 0); + else + return (*(uint64_t *)p != 0); + } +} + +static TRef crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp, + void *svisnz) +{ + IRType dt = crec_ct2irt(ctype_ctsG(J2G(J)), d); + IRType st = crec_ct2irt(ctype_ctsG(J2G(J)), s); + CTSize dsize = d->size, ssize = s->size; + CTInfo dinfo = d->info, sinfo = s->info; + + if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT) + goto err_conv; + + /* + ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and + ** numbers up to 8 bytes. Otherwise sp holds a pointer. + */ + + switch (cconv_idx2(dinfo, sinfo)) { + /* Destination is a bool. */ + case CCX(B, B): + goto xstore; /* Source operand is already normalized. */ + case CCX(B, I): + case CCX(B, F): + if (st != IRT_CDATA) { + /* Specialize to the result of a comparison against 0. */ + TRef zero = (st == IRT_NUM || st == IRT_FLOAT) ? lj_ir_knum(J, 0) : + (st == IRT_I64 || st == IRT_U64) ? lj_ir_kint64(J, 0) : + lj_ir_kint(J, 0); + int isnz = crec_isnonzero(s, svisnz); + emitir(IRTG(isnz ? IR_NE : IR_EQ, st), sp, zero); + sp = lj_ir_kint(J, isnz); + goto xstore; + } + goto err_nyi; + + /* Destination is an integer. */ + case CCX(I, B): + case CCX(I, I): + conv_I_I: + if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; + /* Extend 32 to 64 bit integer. */ + if (dsize == 8 && ssize < 8 && !(LJ_64 && (sinfo & CTF_UNSIGNED))) + sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, + (sinfo & CTF_UNSIGNED) ? 0 : IRCONV_SEXT); + else if (dsize < 8 && ssize == 8) /* Truncate from 64 bit integer. */ + sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, 0); + else if (st == IRT_INT) + sp = lj_opt_narrow_toint(J, sp); + xstore: + if (dt == IRT_I64 || dt == IRT_U64) lj_needsplit(J); + if (dp == 0) return sp; + emitir(IRT(IR_XSTORE, dt), dp, sp); + break; + case CCX(I, C): + sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */ + /* fallthrough */ + case CCX(I, F): + if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; + sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, IRCONV_TRUNC|IRCONV_ANY); + goto xstore; + case CCX(I, P): + case CCX(I, A): + sinfo = CTINFO(CT_NUM, CTF_UNSIGNED); + ssize = CTSIZE_PTR; + st = IRT_UINTP; + goto conv_I_I; + + /* Destination is a floating-point number. */ + case CCX(F, B): + case CCX(F, I): + conv_F_I: + if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; + sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, 0); + goto xstore; + case CCX(F, C): + sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */ + /* fallthrough */ + case CCX(F, F): + conv_F_F: + if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; + if (dt != st) sp = emitconv(sp, dt, st, 0); + goto xstore; + + /* Destination is a complex number. */ + case CCX(C, I): + case CCX(C, F): + { /* Clear im. */ + TRef ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1))); + emitir(IRT(IR_XSTORE, dt), ptr, lj_ir_knum(J, 0)); + } + /* Convert to re. */ + if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I; + + case CCX(C, C): + if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi; + { + TRef re, im, ptr; + re = emitir(IRT(IR_XLOAD, st), sp, 0); + ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, (ssize >> 1))); + im = emitir(IRT(IR_XLOAD, st), ptr, 0); + if (dt != st) { + re = emitconv(re, dt, st, 0); + im = emitconv(im, dt, st, 0); + } + emitir(IRT(IR_XSTORE, dt), dp, re); + ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1))); + emitir(IRT(IR_XSTORE, dt), ptr, im); + } + break; + + /* Destination is a vector. */ + case CCX(V, I): + case CCX(V, F): + case CCX(V, C): + case CCX(V, V): + goto err_nyi; + + /* Destination is a pointer. */ + case CCX(P, P): + case CCX(P, A): + case CCX(P, S): + /* There are only 32 bit pointers/addresses on 32 bit machines. + ** Also ok on x64, since all 32 bit ops clear the upper part of the reg. + */ + goto xstore; + case CCX(P, I): + if (st == IRT_CDATA) goto err_nyi; + if (!LJ_64 && ssize == 8) /* Truncate from 64 bit integer. */ + sp = emitconv(sp, IRT_U32, st, 0); + goto xstore; + case CCX(P, F): + if (st == IRT_CDATA) goto err_nyi; + /* The signed conversion is cheaper. x64 really has 47 bit pointers. */ + sp = emitconv(sp, (LJ_64 && dsize == 8) ? IRT_I64 : IRT_U32, + st, IRCONV_TRUNC|IRCONV_ANY); + goto xstore; + + /* Destination is an array. */ + case CCX(A, A): + /* Destination is a struct/union. */ + case CCX(S, S): + if (dp == 0) goto err_conv; + crec_copy(J, dp, sp, lj_ir_kint(J, dsize), d); + break; + + default: + err_conv: + err_nyi: + lj_trace_err(J, LJ_TRERR_NYICONV); + break; + } + return 0; +} + +/* -- Convert C type to TValue (load) ------------------------------------- */ + +static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp) +{ + CTState *cts = ctype_ctsG(J2G(J)); + IRType t = crec_ct2irt(cts, s); + CTInfo sinfo = s->info; + if (ctype_isnum(sinfo)) { + TRef tr; + if (t == IRT_CDATA) + goto err_nyi; /* NYI: copyval of >64 bit integers. */ + tr = emitir(IRT(IR_XLOAD, t), sp, 0); + if (t == IRT_FLOAT || t == IRT_U32) { /* Keep uint32_t/float as numbers. */ + return emitconv(tr, IRT_NUM, t, 0); + } else if (t == IRT_I64 || t == IRT_U64) { /* Box 64 bit integer. */ + sp = tr; + lj_needsplit(J); + } else if ((sinfo & CTF_BOOL)) { + /* Assume not equal to zero. Fixup and emit pending guard later. */ + lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0)); + J->postproc = LJ_POST_FIXGUARD; + return TREF_TRUE; + } else { + return tr; + } + } else if (ctype_isptr(sinfo) || ctype_isenum(sinfo)) { + sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Box pointers and enums. */ + } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) { + cts->L = J->L; + sid = lj_ctype_intern(cts, CTINFO_REF(sid), CTSIZE_PTR); /* Create ref. */ + } else if (ctype_iscomplex(sinfo)) { /* Unbox/box complex. */ + ptrdiff_t esz = (ptrdiff_t)(s->size >> 1); + TRef ptr, tr1, tr2, dp; + dp = emitir(IRTG(IR_CNEW, IRT_CDATA), lj_ir_kint(J, sid), TREF_NIL); + tr1 = emitir(IRT(IR_XLOAD, t), sp, 0); + ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, esz)); + tr2 = emitir(IRT(IR_XLOAD, t), ptr, 0); + ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata))); + emitir(IRT(IR_XSTORE, t), ptr, tr1); + ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)+esz)); + emitir(IRT(IR_XSTORE, t), ptr, tr2); + return dp; + } else { + /* NYI: copyval of vectors. */ + err_nyi: + lj_trace_err(J, LJ_TRERR_NYICONV); + } + /* Box pointer, ref, enum or 64 bit integer. */ + return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, sid), sp); +} + +/* -- Convert TValue to C type (store) ------------------------------------ */ + +static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, cTValue *sval) +{ + CTState *cts = ctype_ctsG(J2G(J)); + CTypeID sid = CTID_P_VOID; + void *svisnz = 0; + CType *s; + if (LJ_LIKELY(tref_isinteger(sp))) { + sid = CTID_INT32; + svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval)); + } else if (tref_isnum(sp)) { + sid = CTID_DOUBLE; + svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval)); + } else if (tref_isbool(sp)) { + sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0); + sid = CTID_BOOL; + } else if (tref_isnil(sp)) { + sp = lj_ir_kptr(J, NULL); + } else if (tref_isudata(sp)) { + GCudata *ud = udataV(sval); + if (ud->udtype == UDTYPE_IO_FILE) { + TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), sp, IRFL_UDATA_UDTYPE); + emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE)); + sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp, IRFL_UDATA_FILE); + } else { + sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCudata))); + } + } else if (tref_isstr(sp)) { + if (ctype_isenum(d->info)) { /* Match string against enum constant. */ + GCstr *str = strV(sval); + CTSize ofs; + CType *cct = lj_ctype_getfield(cts, d, str, &ofs); + /* Specialize to the name of the enum constant. */ + emitir(IRTG(IR_EQ, IRT_STR), sp, lj_ir_kstr(J, str)); + if (cct && ctype_isconstval(cct->info)) { + lua_assert(ctype_child(cts, cct)->size == 4); + svisnz = (void *)(intptr_t)(ofs != 0); + sp = lj_ir_kint(J, (int32_t)ofs); + sid = ctype_cid(cct->info); + } /* else: interpreter will throw. */ + } else if (ctype_isrefarray(d->info)) { /* Copy string to array. */ + lj_trace_err(J, LJ_TRERR_BADTYPE); /* NYI */ + } else { /* Otherwise pass the string data as a const char[]. */ + sp = emitir(IRT(IR_STRREF, IRT_P32), sp, lj_ir_kint(J, 0)); + sid = CTID_A_CCHAR; + } + } else { /* NYI: tref_istab(sp), tref_islightud(sp). */ + IRType t; + sid = argv2cdata(J, sp, sval)->ctypeid; + s = ctype_raw(cts, sid); + svisnz = cdataptr(cdataV(sval)); + t = crec_ct2irt(cts, s); + if (ctype_isptr(s->info)) { + sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_PTR); + if (ctype_isref(s->info)) { + svisnz = *(void **)svisnz; + s = ctype_rawchild(cts, s); + if (ctype_isenum(s->info)) s = ctype_child(cts, s); + t = crec_ct2irt(cts, s); + } else { + goto doconv; + } + } else if (t == IRT_I64 || t == IRT_U64) { + sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64); + lj_needsplit(J); + goto doconv; + } else if (t == IRT_INT || t == IRT_U32) { + if (ctype_isenum(s->info)) s = ctype_child(cts, s); + sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT); + goto doconv; + } else { + sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata))); + } + if (ctype_isnum(s->info) && t != IRT_CDATA) + sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Load number value. */ + goto doconv; + } + s = ctype_get(cts, sid); +doconv: + if (ctype_isenum(d->info)) d = ctype_child(cts, d); + return crec_ct_ct(J, d, s, dp, sp, svisnz); +} + +/* -- C data metamethods -------------------------------------------------- */ + +/* This would be rather difficult in FOLD, so do it here: +** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k) +** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz) +*/ +static TRef crec_reassoc_ofs(jit_State *J, TRef tr, ptrdiff_t *ofsp, MSize sz) +{ + IRIns *ir = IR(tref_ref(tr)); + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && irref_isk(ir->op2) && + (ir->o == IR_ADD || ir->o == IR_ADDOV || ir->o == IR_SUBOV)) { + IRIns *irk = IR(ir->op2); + ptrdiff_t k; + if (LJ_64 && irk->o == IR_KINT64) + k = (ptrdiff_t)ir_kint64(irk)->u64 * sz; + else + k = (ptrdiff_t)irk->i * sz; + if (ir->o == IR_SUBOV) *ofsp -= k; else *ofsp += k; + tr = ir->op1; /* Not a TRef, but the caller doesn't care. */ + } + return tr; +} + +/* Record ctype __index/__newindex metamethods. */ +static void crec_index_meta(jit_State *J, CTState *cts, CType *ct, + RecordFFData *rd) +{ + CTypeID id = ctype_typeid(cts, ct); + cTValue *tv = lj_ctype_meta(cts, id, rd->data ? MM_newindex : MM_index); + if (!tv) + lj_trace_err(J, LJ_TRERR_BADTYPE); + if (tvisfunc(tv)) { + J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME; + rd->nres = -1; /* Pending tailcall. */ + } else if (rd->data == 0 && tvistab(tv) && tref_isstr(J->base[1])) { + /* Specialize to result of __index lookup. */ + cTValue *o = lj_tab_get(J->L, tabV(tv), &rd->argv[1]); + J->base[0] = lj_record_constify(J, o); + if (!J->base[0]) + lj_trace_err(J, LJ_TRERR_BADTYPE); + /* Always specialize to the key. */ + emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1]))); + } else { + /* NYI: resolving of non-function metamethods. */ + /* NYI: non-string keys for __index table. */ + /* NYI: stores to __newindex table. */ + lj_trace_err(J, LJ_TRERR_BADTYPE); + } +} + +void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd) +{ + TRef idx, ptr = J->base[0]; + ptrdiff_t ofs = sizeof(GCcdata); + GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]); + CTState *cts = ctype_ctsG(J2G(J)); + CType *ct = ctype_raw(cts, cd->ctypeid); + CTypeID sid = 0; + + /* Resolve pointer or reference for cdata object. */ + if (ctype_isptr(ct->info)) { + IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32; + if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct); + ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_PTR); + ofs = 0; + ptr = crec_reassoc_ofs(J, ptr, &ofs, 1); + } + +again: + idx = J->base[1]; + if (tref_isnumber(idx)) { + idx = lj_opt_narrow_cindex(J, idx); + if (ctype_ispointer(ct->info)) { + CTSize sz; + integer_key: + if ((ct->info & CTF_COMPLEX)) + idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1)); + sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info))); + idx = crec_reassoc_ofs(J, idx, &ofs, sz); +#if LJ_TARGET_ARM || LJ_TARGET_PPC + /* Hoist base add to allow fusion of index/shift into operands. */ + if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs +#if LJ_TARGET_ARM + && (sz == 1 || sz == 4) +#endif + ) { + ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); + ofs = 0; + } +#endif + idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz)); + ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr); + } + } else if (tref_iscdata(idx)) { + GCcdata *cdk = cdataV(&rd->argv[1]); + CType *ctk = ctype_raw(cts, cdk->ctypeid); + IRType t = crec_ct2irt(cts, ctk); + if (ctype_ispointer(ct->info) && t >= IRT_I8 && t <= IRT_U64) { + if (ctk->size == 8) { + idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT64); + } else if (ctk->size == 4) { + idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT); + } else { + idx = emitir(IRT(IR_ADD, IRT_PTR), idx, + lj_ir_kintp(J, sizeof(GCcdata))); + idx = emitir(IRT(IR_XLOAD, t), idx, 0); + } + if (LJ_64 && ctk->size < sizeof(intptr_t) && !(ctk->info & CTF_UNSIGNED)) + idx = emitconv(idx, IRT_INTP, IRT_INT, IRCONV_SEXT); + if (!LJ_64 && ctk->size > sizeof(intptr_t)) { + idx = emitconv(idx, IRT_INTP, t, 0); + lj_needsplit(J); + } + goto integer_key; + } + } else if (tref_isstr(idx)) { + GCstr *name = strV(&rd->argv[1]); + if (cd->ctypeid == CTID_CTYPEID) + ct = ctype_raw(cts, crec_constructor(J, cd, ptr)); + if (ctype_isstruct(ct->info)) { + CTSize fofs; + CType *fct; + fct = lj_ctype_getfield(cts, ct, name, &fofs); + if (fct) { + /* Always specialize to the field name. */ + emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name)); + if (ctype_isconstval(fct->info)) { + if (fct->size >= 0x80000000u && + (ctype_child(cts, fct)->info & CTF_UNSIGNED)) { + J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size); + return; + } + J->base[0] = lj_ir_kint(J, (int32_t)fct->size); + return; /* Interpreter will throw for newindex. */ + } else if (ctype_isbitfield(fct->info)) { + lj_trace_err(J, LJ_TRERR_NYICONV); + } else { + lua_assert(ctype_isfield(fct->info)); + sid = ctype_cid(fct->info); + } + ofs += (ptrdiff_t)fofs; + } + } else if (ctype_iscomplex(ct->info)) { + if (name->len == 2 && + ((strdata(name)[0] == 'r' && strdata(name)[1] == 'e') || + (strdata(name)[0] == 'i' && strdata(name)[1] == 'm'))) { + /* Always specialize to the field name. */ + emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name)); + if (strdata(name)[0] == 'i') ofs += (ct->size >> 1); + sid = ctype_cid(ct->info); + } + } + } + if (!sid) { + if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */ + CType *cct = ctype_rawchild(cts, ct); + if (ctype_isstruct(cct->info)) { + ct = cct; + if (tref_isstr(idx)) goto again; + } + } + crec_index_meta(J, cts, ct, rd); + return; + } + + if (ofs) + ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs)); + + /* Resolve reference for field. */ + ct = ctype_get(cts, sid); + if (ctype_isref(ct->info)) + ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0); + + while (ctype_isattrib(ct->info)) + ct = ctype_child(cts, ct); /* Skip attributes. */ + + if (rd->data == 0) { /* __index metamethod. */ + J->base[0] = crec_tv_ct(J, ct, sid, ptr); + } else { /* __newindex metamethod. */ + rd->nres = 0; + J->needsnap = 1; + crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]); + } +} + +/* Record setting a finalizer. */ +static void crec_finalizer(jit_State *J, TRef trcd, cTValue *fin) +{ + TRef trlo = lj_ir_call(J, IRCALL_lj_cdata_setfin, trcd); + TRef trhi = emitir(IRT(IR_ADD, IRT_P32), trlo, lj_ir_kint(J, 4)); + if (LJ_BE) { TRef tmp = trlo; trlo = trhi; trhi = tmp; } + if (tvisfunc(fin)) { + emitir(IRT(IR_XSTORE, IRT_P32), trlo, lj_ir_kfunc(J, funcV(fin))); + emitir(IRTI(IR_XSTORE), trhi, lj_ir_kint(J, LJ_TFUNC)); + } else if (tviscdata(fin)) { + emitir(IRT(IR_XSTORE, IRT_P32), trlo, + lj_ir_kgc(J, obj2gco(cdataV(fin)), IRT_CDATA)); + emitir(IRTI(IR_XSTORE), trhi, lj_ir_kint(J, LJ_TCDATA)); + } else { + lj_trace_err(J, LJ_TRERR_BADTYPE); + } + J->needsnap = 1; +} + +/* Record cdata allocation. */ +static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id) +{ + CTState *cts = ctype_ctsG(J2G(J)); + CTSize sz; + CTInfo info = lj_ctype_info(cts, id, &sz); + CType *d = ctype_raw(cts, id); + TRef trid; + if (!sz || sz > 128 || (info & CTF_VLA) || ctype_align(info) > CT_MEMALIGN) + lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: large/special allocations. */ + trid = lj_ir_kint(J, id); + /* Use special instruction to box pointer or 32/64 bit integer. */ + if (ctype_isptr(info) || (ctype_isinteger(info) && (sz == 4 || sz == 8))) { + TRef sp = J->base[1] ? crec_ct_tv(J, d, 0, J->base[1], &rd->argv[1]) : + ctype_isptr(info) ? lj_ir_kptr(J, NULL) : + sz == 4 ? lj_ir_kint(J, 0) : + (lj_needsplit(J), lj_ir_kint64(J, 0)); + J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, sp); + } else { + TRef trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, TREF_NIL); + cTValue *fin; + J->base[0] = trcd; + if (J->base[1] && !J->base[2] && + !lj_cconv_multi_init(cts, d, &rd->argv[1])) { + goto single_init; + } else if (ctype_isarray(d->info)) { + CType *dc = ctype_rawchild(cts, d); /* Array element type. */ + CTSize ofs, esize = dc->size; + TRef sp = 0; + TValue tv; + TValue *sval = &tv; + MSize i; + tv.u64 = 0; + if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info))) + lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init array of aggregates. */ + for (i = 1, ofs = 0; ofs < sz; ofs += esize) { + TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, + lj_ir_kintp(J, ofs + sizeof(GCcdata))); + if (J->base[i]) { + sp = J->base[i]; + sval = &rd->argv[i]; + i++; + } else if (i != 2) { + sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL; + } + crec_ct_tv(J, dc, dp, sp, sval); + } + } else if (ctype_isstruct(d->info)) { + CTypeID fid = d->sib; + MSize i = 1; + while (fid) { + CType *df = ctype_get(cts, fid); + fid = df->sib; + if (ctype_isfield(df->info)) { + CType *dc; + TRef sp, dp; + TValue tv; + TValue *sval = &tv; + setintV(&tv, 0); + if (!gcref(df->name)) continue; /* Ignore unnamed fields. */ + dc = ctype_rawchild(cts, df); /* Field type. */ + if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) || + ctype_isenum(dc->info))) + lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init aggregates. */ + if (J->base[i]) { + sp = J->base[i]; + sval = &rd->argv[i]; + i++; + } else { + sp = ctype_isptr(dc->info) ? TREF_NIL : lj_ir_kint(J, 0); + } + dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, + lj_ir_kintp(J, df->size + sizeof(GCcdata))); + crec_ct_tv(J, dc, dp, sp, sval); + } else if (!ctype_isconstval(df->info)) { + /* NYI: init bitfields and sub-structures. */ + lj_trace_err(J, LJ_TRERR_NYICONV); + } + } + } else { + TRef dp; + single_init: + dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata))); + if (J->base[1]) { + crec_ct_tv(J, d, dp, J->base[1], &rd->argv[1]); + } else { + TValue tv; + tv.u64 = 0; + crec_ct_tv(J, d, dp, lj_ir_kint(J, 0), &tv); + } + } + /* Handle __gc metamethod. */ + fin = lj_ctype_meta(cts, id, MM_gc); + if (fin) + crec_finalizer(J, trcd, fin); + } +} + +/* Record argument conversions. */ +static TRef crec_call_args(jit_State *J, RecordFFData *rd, + CTState *cts, CType *ct) +{ + TRef args[CCI_NARGS_MAX]; + CTypeID fid; + MSize i, n; + TRef tr, *base; + cTValue *o; +#if LJ_TARGET_X86 +#if LJ_ABI_WIN + TRef *arg0 = NULL, *arg1 = NULL; +#endif + int ngpr = 0; + if (ctype_cconv(ct->info) == CTCC_THISCALL) + ngpr = 1; + else if (ctype_cconv(ct->info) == CTCC_FASTCALL) + ngpr = 2; +#endif + + /* Skip initial attributes. */ + fid = ct->sib; + while (fid) { + CType *ctf = ctype_get(cts, fid); + if (!ctype_isattrib(ctf->info)) break; + fid = ctf->sib; + } + args[0] = TREF_NIL; + for (n = 0, base = J->base+1, o = rd->argv+1; *base; n++, base++, o++) { + CTypeID did; + CType *d; + + if (n >= CCI_NARGS_MAX) + lj_trace_err(J, LJ_TRERR_NYICALL); + + if (fid) { /* Get argument type from field. */ + CType *ctf = ctype_get(cts, fid); + fid = ctf->sib; + lua_assert(ctype_isfield(ctf->info)); + did = ctype_cid(ctf->info); + } else { + if (!(ct->info & CTF_VARARG)) + lj_trace_err(J, LJ_TRERR_NYICALL); /* Too many arguments. */ + did = lj_ccall_ctid_vararg(cts, o); /* Infer vararg type. */ + } + d = ctype_raw(cts, did); + if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || + ctype_isenum(d->info))) + lj_trace_err(J, LJ_TRERR_NYICALL); + tr = crec_ct_tv(J, d, 0, *base, o); + if (ctype_isinteger_or_bool(d->info)) { + if (d->size < 4) { + if ((d->info & CTF_UNSIGNED)) + tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_U8 : IRT_U16, 0); + else + tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_I8 : IRT_I16,IRCONV_SEXT); + } + } else if (LJ_SOFTFP && ctype_isfp(d->info) && d->size > 4) { + lj_needsplit(J); + } +#if LJ_TARGET_X86 + /* 64 bit args must not end up in registers for fastcall/thiscall. */ +#if LJ_ABI_WIN + if (!ctype_isfp(d->info)) { + /* Sigh, the Windows/x86 ABI allows reordering across 64 bit args. */ + if (tref_typerange(tr, IRT_I64, IRT_U64)) { + if (ngpr) { + arg0 = &args[n]; args[n++] = TREF_NIL; ngpr--; + if (ngpr) { + arg1 = &args[n]; args[n++] = TREF_NIL; ngpr--; + } + } + } else { + if (arg0) { *arg0 = tr; arg0 = NULL; n--; continue; } + if (arg1) { *arg1 = tr; arg1 = NULL; n--; continue; } + if (ngpr) ngpr--; + } + } +#else + if (!ctype_isfp(d->info) && ngpr) { + if (tref_typerange(tr, IRT_I64, IRT_U64)) { + /* No reordering for other x86 ABIs. Simply add alignment args. */ + do { args[n++] = TREF_NIL; } while (--ngpr); + } else { + ngpr--; + } + } +#endif +#endif + args[n] = tr; + } + tr = args[0]; + for (i = 1; i < n; i++) + tr = emitir(IRT(IR_CARG, IRT_NIL), tr, args[i]); + return tr; +} + +/* Create a snapshot for the caller, simulating a 'false' return value. */ +static void crec_snap_caller(jit_State *J) +{ + lua_State *L = J->L; + TValue *base = L->base, *top = L->top; + const BCIns *pc = J->pc; + TRef ftr = J->base[-1]; + ptrdiff_t delta; + if (!frame_islua(base-1)) + lj_trace_err(J, LJ_TRERR_NYICALL); + J->pc = frame_pc(base-1); delta = 1+bc_a(J->pc[-1]); + L->top = base; L->base = base - delta; + J->base[-1] = TREF_FALSE; + J->base -= delta; J->baseslot -= (BCReg)delta; + J->maxslot = (BCReg)delta; J->framedepth--; + lj_snap_add(J); + L->base = base; L->top = top; + J->framedepth++; J->maxslot = 1; + J->base += delta; J->baseslot += (BCReg)delta; + J->base[-1] = ftr; J->pc = pc; +} + +/* Record function call. */ +static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd) +{ + CTState *cts = ctype_ctsG(J2G(J)); + CType *ct = ctype_raw(cts, cd->ctypeid); + IRType tp = IRT_PTR; + if (ctype_isptr(ct->info)) { + tp = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32; + ct = ctype_rawchild(cts, ct); + } + if (ctype_isfunc(ct->info)) { + TRef func = emitir(IRT(IR_FLOAD, tp), J->base[0], IRFL_CDATA_PTR); + CType *ctr = ctype_rawchild(cts, ct); + IRType t = crec_ct2irt(cts, ctr); + TRef tr; + TValue tv; + /* Check for blacklisted C functions that might call a callback. */ + setlightudV(&tv, + cdata_getptr(cdataptr(cd), (LJ_64 && tp == IRT_P64) ? 8 : 4)); + if (tvistrue(lj_tab_get(J->L, cts->miscmap, &tv))) + lj_trace_err(J, LJ_TRERR_BLACKL); + if (ctype_isvoid(ctr->info)) { + t = IRT_NIL; + rd->nres = 0; + } else if (!(ctype_isnum(ctr->info) || ctype_isptr(ctr->info) || + ctype_isenum(ctr->info)) || t == IRT_CDATA) { + lj_trace_err(J, LJ_TRERR_NYICALL); + } + if ((ct->info & CTF_VARARG) +#if LJ_TARGET_X86 + || ctype_cconv(ct->info) != CTCC_CDECL +#endif + ) + func = emitir(IRT(IR_CARG, IRT_NIL), func, + lj_ir_kint(J, ctype_typeid(cts, ct))); + tr = emitir(IRT(IR_CALLXS, t), crec_call_args(J, rd, cts, ct), func); + if (ctype_isbool(ctr->info)) { + if (frame_islua(J->L->base-1) && bc_b(frame_pc(J->L->base-1)[-1]) == 1) { + /* Don't check result if ignored. */ + tr = TREF_NIL; + } else { + crec_snap_caller(J); +#if LJ_TARGET_X86ORX64 + /* Note: only the x86/x64 backend supports U8 and only for EQ(tr, 0). */ + lj_ir_set(J, IRTG(IR_NE, IRT_U8), tr, lj_ir_kint(J, 0)); +#else + lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0)); +#endif + J->postproc = LJ_POST_FIXGUARDSNAP; + tr = TREF_TRUE; + } + } else if (t == IRT_PTR || (LJ_64 && t == IRT_P32) || + t == IRT_I64 || t == IRT_U64 || ctype_isenum(ctr->info)) { + TRef trid = lj_ir_kint(J, ctype_cid(ct->info)); + tr = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, tr); + if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J); + } else if (t == IRT_FLOAT || t == IRT_U32) { + tr = emitconv(tr, IRT_NUM, t, 0); + } else if (t == IRT_I8 || t == IRT_I16) { + tr = emitconv(tr, IRT_INT, t, IRCONV_SEXT); + } else if (t == IRT_U8 || t == IRT_U16) { + tr = emitconv(tr, IRT_INT, t, 0); + } + J->base[0] = tr; + J->needsnap = 1; + return 1; + } + return 0; +} + +void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd) +{ + CTState *cts = ctype_ctsG(J2G(J)); + GCcdata *cd = argv2cdata(J, J->base[0], &rd->argv[0]); + CTypeID id = cd->ctypeid; + CType *ct; + cTValue *tv; + MMS mm = MM_call; + if (id == CTID_CTYPEID) { + id = crec_constructor(J, cd, J->base[0]); + mm = MM_new; + } else if (crec_call(J, rd, cd)) { + return; + } + /* Record ctype __call/__new metamethod. */ + ct = ctype_raw(cts, id); + tv = lj_ctype_meta(cts, ctype_isptr(ct->info) ? ctype_cid(ct->info) : id, mm); + if (tv) { + if (tvisfunc(tv)) { + J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME; + rd->nres = -1; /* Pending tailcall. */ + return; + } + } else if (mm == MM_new) { + crec_alloc(J, rd, id); + return; + } + /* No metamethod or NYI: non-function metamethods. */ + lj_trace_err(J, LJ_TRERR_BADTYPE); +} + +static TRef crec_arith_int64(jit_State *J, TRef *sp, CType **s, MMS mm) +{ + if (ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) { + IRType dt; + CTypeID id; + TRef tr; + MSize i; + IROp op; + lj_needsplit(J); + if (((s[0]->info & CTF_UNSIGNED) && s[0]->size == 8) || + ((s[1]->info & CTF_UNSIGNED) && s[1]->size == 8)) { + dt = IRT_U64; id = CTID_UINT64; + } else { + dt = IRT_I64; id = CTID_INT64; + if (mm < MM_add && + !((s[0]->info | s[1]->info) & CTF_FP) && + s[0]->size == 4 && s[1]->size == 4) { /* Try to narrow comparison. */ + if (!((s[0]->info ^ s[1]->info) & CTF_UNSIGNED) || + (tref_isk(sp[1]) && IR(tref_ref(sp[1]))->i >= 0)) { + dt = (s[0]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT; + goto comp; + } else if (tref_isk(sp[0]) && IR(tref_ref(sp[0]))->i >= 0) { + dt = (s[1]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT; + goto comp; + } + } + } + for (i = 0; i < 2; i++) { + IRType st = tref_type(sp[i]); + if (st == IRT_NUM || st == IRT_FLOAT) + sp[i] = emitconv(sp[i], dt, st, IRCONV_TRUNC|IRCONV_ANY); + else if (!(st == IRT_I64 || st == IRT_U64)) + sp[i] = emitconv(sp[i], dt, IRT_INT, + (s[i]->info & CTF_UNSIGNED) ? 0 : IRCONV_SEXT); + } + if (mm < MM_add) { + comp: + /* Assume true comparison. Fixup and emit pending guard later. */ + if (mm == MM_eq) { + op = IR_EQ; + } else { + op = mm == MM_lt ? IR_LT : IR_LE; + if (dt == IRT_U32 || dt == IRT_U64) + op += (IR_ULT-IR_LT); + } + lj_ir_set(J, IRTG(op, dt), sp[0], sp[1]); + J->postproc = LJ_POST_FIXGUARD; + return TREF_TRUE; + } else { + tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, dt), sp[0], sp[1]); + } + return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr); + } + return 0; +} + +static TRef crec_arith_ptr(jit_State *J, TRef *sp, CType **s, MMS mm) +{ + CTState *cts = ctype_ctsG(J2G(J)); + CType *ctp = s[0]; + if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) { + if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) && + (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) { + if (mm == MM_sub) { /* Pointer difference. */ + TRef tr; + CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info)); + if (sz == 0 || (sz & (sz-1)) != 0) + return 0; /* NYI: integer division. */ + tr = emitir(IRT(IR_SUB, IRT_INTP), sp[0], sp[1]); + tr = emitir(IRT(IR_BSAR, IRT_INTP), tr, lj_ir_kint(J, lj_fls(sz))); +#if LJ_64 + tr = emitconv(tr, IRT_NUM, IRT_INTP, 0); +#endif + return tr; + } else { /* Pointer comparison (unsigned). */ + /* Assume true comparison. Fixup and emit pending guard later. */ + IROp op = mm == MM_eq ? IR_EQ : mm == MM_lt ? IR_ULT : IR_ULE; + lj_ir_set(J, IRTG(op, IRT_PTR), sp[0], sp[1]); + J->postproc = LJ_POST_FIXGUARD; + return TREF_TRUE; + } + } + if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(s[1]->info))) + return 0; + } else if (mm == MM_add && ctype_isnum(ctp->info) && + (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) { + TRef tr = sp[0]; sp[0] = sp[1]; sp[1] = tr; /* Swap pointer and index. */ + ctp = s[1]; + } else { + return 0; + } + { + TRef tr = sp[1]; + IRType t = tref_type(tr); + CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info)); + CTypeID id; +#if LJ_64 + if (t == IRT_NUM || t == IRT_FLOAT) + tr = emitconv(tr, IRT_INTP, t, IRCONV_TRUNC|IRCONV_ANY); + else if (!(t == IRT_I64 || t == IRT_U64)) + tr = emitconv(tr, IRT_INTP, IRT_INT, + ((t - IRT_I8) & 1) ? 0 : IRCONV_SEXT); +#else + if (!tref_typerange(sp[1], IRT_I8, IRT_U32)) { + tr = emitconv(tr, IRT_INTP, t, + (t == IRT_NUM || t == IRT_FLOAT) ? + IRCONV_TRUNC|IRCONV_ANY : 0); + } +#endif + tr = emitir(IRT(IR_MUL, IRT_INTP), tr, lj_ir_kintp(J, sz)); + tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, IRT_PTR), sp[0], tr); + id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)), + CTSIZE_PTR); + return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr); + } +} + +/* Record ctype arithmetic metamethods. */ +static void crec_arith_meta(jit_State *J, CTState *cts, RecordFFData *rd) +{ + cTValue *tv = NULL; + if (J->base[0]) { + if (tviscdata(&rd->argv[0])) { + CTypeID id = argv2cdata(J, J->base[0], &rd->argv[0])->ctypeid; + CType *ct = ctype_raw(cts, id); + if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); + tv = lj_ctype_meta(cts, id, (MMS)rd->data); + } + if (!tv && J->base[1] && tviscdata(&rd->argv[1])) { + CTypeID id = argv2cdata(J, J->base[1], &rd->argv[1])->ctypeid; + CType *ct = ctype_raw(cts, id); + if (ctype_isptr(ct->info)) id = ctype_cid(ct->info); + tv = lj_ctype_meta(cts, id, (MMS)rd->data); + } + } + if (tv) { + if (tvisfunc(tv)) { + J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME; + rd->nres = -1; /* Pending tailcall. */ + return; + } /* NYI: non-function metamethods. */ + } else if ((MMS)rd->data == MM_eq) { + J->base[0] = TREF_FALSE; + return; + } + lj_trace_err(J, LJ_TRERR_BADTYPE); +} + +void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd) +{ + CTState *cts = ctype_ctsG(J2G(J)); + TRef sp[2]; + CType *s[2]; + MSize i; + for (i = 0; i < 2; i++) { + TRef tr = J->base[i]; + CType *ct = ctype_get(cts, CTID_DOUBLE); + if (!tr) { + goto trymeta; + } else if (tref_iscdata(tr)) { + CTypeID id = argv2cdata(J, tr, &rd->argv[i])->ctypeid; + IRType t; + ct = ctype_raw(cts, id); + t = crec_ct2irt(cts, ct); + if (ctype_isptr(ct->info)) { /* Resolve pointer or reference. */ + tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_PTR); + if (ctype_isref(ct->info)) { + ct = ctype_rawchild(cts, ct); + t = crec_ct2irt(cts, ct); + } + } else if (t == IRT_I64 || t == IRT_U64) { + tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT64); + lj_needsplit(J); + goto ok; + } else if (t == IRT_INT || t == IRT_U32) { + tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT); + if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); + goto ok; + } else if (ctype_isfunc(ct->info)) { + tr = emitir(IRT(IR_FLOAD, IRT_PTR), tr, IRFL_CDATA_PTR); + ct = ctype_get(cts, + lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR)); + goto ok; + } else { + tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata))); + } + if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); + if (ctype_isnum(ct->info)) { + if (t == IRT_CDATA) goto trymeta; + if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J); + tr = emitir(IRT(IR_XLOAD, t), tr, 0); + } else if (!(ctype_isptr(ct->info) || ctype_isrefarray(ct->info))) { + goto trymeta; + } + } else if (tref_isnil(tr)) { + tr = lj_ir_kptr(J, NULL); + ct = ctype_get(cts, CTID_P_VOID); + } else if (tref_isinteger(tr)) { + ct = ctype_get(cts, CTID_INT32); + } else if (tref_isstr(tr)) { + TRef tr2 = J->base[1-i]; + CTypeID id = argv2cdata(J, tr2, &rd->argv[1-i])->ctypeid; + ct = ctype_raw(cts, id); + if (ctype_isenum(ct->info)) { /* Match string against enum constant. */ + GCstr *str = strV(&rd->argv[i]); + CTSize ofs; + CType *cct = lj_ctype_getfield(cts, ct, str, &ofs); + if (cct && ctype_isconstval(cct->info)) { + /* Specialize to the name of the enum constant. */ + emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, str)); + ct = ctype_child(cts, cct); + tr = lj_ir_kint(J, (int32_t)ofs); + } /* else: interpreter will throw. */ + } /* else: interpreter will throw. */ + } else if (!tref_isnum(tr)) { + goto trymeta; + } + ok: + s[i] = ct; + sp[i] = tr; + } + { + TRef tr; + if ((tr = crec_arith_int64(J, sp, s, (MMS)rd->data)) || + (tr = crec_arith_ptr(J, sp, s, (MMS)rd->data))) { + J->base[0] = tr; + /* Fixup cdata comparisons, too. Avoids some cdata escapes. */ + if (J->postproc == LJ_POST_FIXGUARD && frame_iscont(J->L->base-1) && + !irt_isguard(J->guardemit)) { + const BCIns *pc = frame_contpc(J->L->base-1) - 1; + if (bc_op(*pc) <= BC_ISNEP) { + setframe_pc(&J2G(J)->tmptv, pc); + J2G(J)->tmptv.u32.lo = ((tref_istrue(tr) ^ bc_op(*pc)) & 1); + J->postproc = LJ_POST_FIXCOMP; + } + } + } else { + trymeta: + crec_arith_meta(J, cts, rd); + } + } +} + +/* -- C library namespace metamethods ------------------------------------- */ + +void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd) +{ + CTState *cts = ctype_ctsG(J2G(J)); + if (tref_isudata(J->base[0]) && tref_isstr(J->base[1]) && + udataV(&rd->argv[0])->udtype == UDTYPE_FFI_CLIB) { + CLibrary *cl = (CLibrary *)uddata(udataV(&rd->argv[0])); + GCstr *name = strV(&rd->argv[1]); + CType *ct; + CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX); + cTValue *tv = lj_tab_getstr(cl->cache, name); + rd->nres = rd->data; + if (id && tv && !tvisnil(tv)) { + /* Specialize to the symbol name and make the result a constant. */ + emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, name)); + if (ctype_isconstval(ct->info)) { + if (ct->size >= 0x80000000u && + (ctype_child(cts, ct)->info & CTF_UNSIGNED)) + J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)ct->size); + else + J->base[0] = lj_ir_kint(J, (int32_t)ct->size); + } else if (ctype_isextern(ct->info)) { + CTypeID sid = ctype_cid(ct->info); + void *sp = *(void **)cdataptr(cdataV(tv)); + TRef ptr; + ct = ctype_raw(cts, sid); + if (LJ_64 && !checkptr32(sp)) + ptr = lj_ir_kintp(J, (uintptr_t)sp); + else + ptr = lj_ir_kptr(J, sp); + if (rd->data) { + J->base[0] = crec_tv_ct(J, ct, sid, ptr); + } else { + J->needsnap = 1; + crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]); + } + } else { + J->base[0] = lj_ir_kgc(J, obj2gco(cdataV(tv)), IRT_CDATA); + } + } else { + lj_trace_err(J, LJ_TRERR_NOCACHE); + } + } /* else: interpreter will throw. */ +} + +/* -- FFI library functions ----------------------------------------------- */ + +static TRef crec_toint(jit_State *J, CTState *cts, TRef sp, TValue *sval) +{ + return crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, sp, sval); +} + +void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd) +{ + crec_alloc(J, rd, argv2ctype(J, J->base[0], &rd->argv[0])); +} + +void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd) +{ + UNUSED(rd); + if (J->base[0]) + lj_trace_err(J, LJ_TRERR_NYICALL); + J->base[0] = lj_ir_call(J, IRCALL_lj_vm_errno); +} + +void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd) +{ + CTState *cts = ctype_ctsG(J2G(J)); + TRef tr = J->base[0]; + if (tr) { + TRef trlen = J->base[1]; + if (trlen) { + trlen = crec_toint(J, cts, trlen, &rd->argv[1]); + tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, &rd->argv[0]); + } else { + tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CCHAR), 0, tr, &rd->argv[0]); + trlen = lj_ir_call(J, IRCALL_strlen, tr); + } + J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), tr, trlen); + } /* else: interpreter will throw. */ +} + +void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd) +{ + CTState *cts = ctype_ctsG(J2G(J)); + TRef trdst = J->base[0], trsrc = J->base[1], trlen = J->base[2]; + if (trdst && trsrc && (trlen || tref_isstr(trsrc))) { + trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]); + trsrc = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, trsrc, &rd->argv[1]); + if (trlen) { + trlen = crec_toint(J, cts, trlen, &rd->argv[2]); + } else { + trlen = emitir(IRTI(IR_FLOAD), J->base[1], IRFL_STR_LEN); + trlen = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1)); + } + rd->nres = 0; + crec_copy(J, trdst, trsrc, trlen, NULL); + } /* else: interpreter will throw. */ +} + +void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd) +{ + CTState *cts = ctype_ctsG(J2G(J)); + TRef trdst = J->base[0], trlen = J->base[1], trfill = J->base[2]; + if (trdst && trlen) { + CTSize step = 1; + if (tviscdata(&rd->argv[0])) { /* Get alignment of original destination. */ + CTSize sz; + CType *ct = ctype_raw(cts, cdataV(&rd->argv[0])->ctypeid); + if (ctype_isptr(ct->info)) + ct = ctype_rawchild(cts, ct); + step = (1u<argv[0]); + trlen = crec_toint(J, cts, trlen, &rd->argv[1]); + if (trfill) + trfill = crec_toint(J, cts, trfill, &rd->argv[2]); + else + trfill = lj_ir_kint(J, 0); + rd->nres = 0; + crec_fill(J, trdst, trlen, trfill, step); + } /* else: interpreter will throw. */ +} + +void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd) +{ + if (tref_iscdata(J->base[0])) { + TRef trid = lj_ir_kint(J, argv2ctype(J, J->base[0], &rd->argv[0])); + J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), + lj_ir_kint(J, CTID_CTYPEID), trid); + } else { + setfuncV(J->L, &J->errinfo, J->fn); + lj_trace_err_info(J, LJ_TRERR_NYIFFU); + } +} + +void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd) +{ + argv2ctype(J, J->base[0], &rd->argv[0]); + if (tref_iscdata(J->base[1])) { + argv2ctype(J, J->base[1], &rd->argv[1]); + J->postproc = LJ_POST_FIXBOOL; + J->base[0] = TREF_TRUE; + } else { + J->base[0] = TREF_FALSE; + } +} + +void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd) +{ + if (tref_isstr(J->base[0])) { + /* Specialize to the ABI string to make the boolean result a constant. */ + emitir(IRTG(IR_EQ, IRT_STR), J->base[0], lj_ir_kstr(J, strV(&rd->argv[0]))); + J->postproc = LJ_POST_FIXBOOL; + J->base[0] = TREF_TRUE; + } else { + lj_trace_err(J, LJ_TRERR_BADTYPE); + } +} + +/* Record ffi.sizeof(), ffi.alignof(), ffi.offsetof(). */ +void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd) +{ + CTypeID id = argv2ctype(J, J->base[0], &rd->argv[0]); + if (rd->data == FF_ffi_sizeof) { + CType *ct = lj_ctype_rawref(ctype_ctsG(J2G(J)), id); + if (ctype_isvltype(ct->info)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + } else if (rd->data == FF_ffi_offsetof) { /* Specialize to the field name. */ + if (!tref_isstr(J->base[1])) + lj_trace_err(J, LJ_TRERR_BADTYPE); + emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1]))); + rd->nres = 3; /* Just in case. */ + } + J->postproc = LJ_POST_FIXCONST; + J->base[0] = J->base[1] = J->base[2] = TREF_NIL; +} + +void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd) +{ + argv2cdata(J, J->base[0], &rd->argv[0]); + crec_finalizer(J, J->base[0], &rd->argv[1]); +} + +/* -- Miscellaneous library functions ------------------------------------- */ + +void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd) +{ + CTState *cts = ctype_ctsG(J2G(J)); + CType *d, *ct = lj_ctype_rawref(cts, cdataV(&rd->argv[0])->ctypeid); + if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct); + if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) { + if (ctype_isinteger_or_bool(ct->info) && ct->size <= 4 && + !(ct->size == 4 && (ct->info & CTF_UNSIGNED))) + d = ctype_get(cts, CTID_INT32); + else + d = ctype_get(cts, CTID_DOUBLE); + J->base[0] = crec_ct_tv(J, d, 0, J->base[0], &rd->argv[0]); + } else { + J->base[0] = TREF_NIL; + } +} + +#undef IR +#undef emitir +#undef emitconv + +#endif diff --git a/external/lua/luajit/src/src/lj_crecord.h b/external/lua/luajit/src/src/lj_crecord.h new file mode 100644 index 0000000000..dea05f78ab --- /dev/null +++ b/external/lua/luajit/src/src/lj_crecord.h @@ -0,0 +1,31 @@ +/* +** Trace recorder for C data operations. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_CRECORD_H +#define _LJ_CRECORD_H + +#include "lj_obj.h" +#include "lj_jit.h" +#include "lj_ffrecord.h" + +#if LJ_HASJIT && LJ_HASFFI +LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd); +LJ_FUNC void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd); +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_ctype.c b/external/lua/luajit/src/src/lj_ctype.c new file mode 100644 index 0000000000..57a0d7cc81 --- /dev/null +++ b/external/lua/luajit/src/src/lj_ctype.c @@ -0,0 +1,634 @@ +/* +** C type management. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include "lj_obj.h" + +#if LJ_HASFFI + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_ctype.h" +#include "lj_ccallback.h" + +/* -- C type definitions -------------------------------------------------- */ + +/* Predefined typedefs. */ +#define CTTDDEF(_) \ + /* Vararg handling. */ \ + _("va_list", P_VOID) \ + _("__builtin_va_list", P_VOID) \ + _("__gnuc_va_list", P_VOID) \ + /* From stddef.h. */ \ + _("ptrdiff_t", INT_PSZ) \ + _("size_t", UINT_PSZ) \ + _("wchar_t", WCHAR) \ + /* Subset of stdint.h. */ \ + _("int8_t", INT8) \ + _("int16_t", INT16) \ + _("int32_t", INT32) \ + _("int64_t", INT64) \ + _("uint8_t", UINT8) \ + _("uint16_t", UINT16) \ + _("uint32_t", UINT32) \ + _("uint64_t", UINT64) \ + _("intptr_t", INT_PSZ) \ + _("uintptr_t", UINT_PSZ) \ + /* End of typedef list. */ + +/* Keywords (only the ones we actually care for). */ +#define CTKWDEF(_) \ + /* Type specifiers. */ \ + _("void", -1, CTOK_VOID) \ + _("_Bool", 0, CTOK_BOOL) \ + _("bool", 1, CTOK_BOOL) \ + _("char", 1, CTOK_CHAR) \ + _("int", 4, CTOK_INT) \ + _("__int8", 1, CTOK_INT) \ + _("__int16", 2, CTOK_INT) \ + _("__int32", 4, CTOK_INT) \ + _("__int64", 8, CTOK_INT) \ + _("float", 4, CTOK_FP) \ + _("double", 8, CTOK_FP) \ + _("long", 0, CTOK_LONG) \ + _("short", 0, CTOK_SHORT) \ + _("_Complex", 0, CTOK_COMPLEX) \ + _("complex", 0, CTOK_COMPLEX) \ + _("__complex", 0, CTOK_COMPLEX) \ + _("__complex__", 0, CTOK_COMPLEX) \ + _("signed", 0, CTOK_SIGNED) \ + _("__signed", 0, CTOK_SIGNED) \ + _("__signed__", 0, CTOK_SIGNED) \ + _("unsigned", 0, CTOK_UNSIGNED) \ + /* Type qualifiers. */ \ + _("const", 0, CTOK_CONST) \ + _("__const", 0, CTOK_CONST) \ + _("__const__", 0, CTOK_CONST) \ + _("volatile", 0, CTOK_VOLATILE) \ + _("__volatile", 0, CTOK_VOLATILE) \ + _("__volatile__", 0, CTOK_VOLATILE) \ + _("restrict", 0, CTOK_RESTRICT) \ + _("__restrict", 0, CTOK_RESTRICT) \ + _("__restrict__", 0, CTOK_RESTRICT) \ + _("inline", 0, CTOK_INLINE) \ + _("__inline", 0, CTOK_INLINE) \ + _("__inline__", 0, CTOK_INLINE) \ + /* Storage class specifiers. */ \ + _("typedef", 0, CTOK_TYPEDEF) \ + _("extern", 0, CTOK_EXTERN) \ + _("static", 0, CTOK_STATIC) \ + _("auto", 0, CTOK_AUTO) \ + _("register", 0, CTOK_REGISTER) \ + /* GCC Attributes. */ \ + _("__extension__", 0, CTOK_EXTENSION) \ + _("__attribute", 0, CTOK_ATTRIBUTE) \ + _("__attribute__", 0, CTOK_ATTRIBUTE) \ + _("asm", 0, CTOK_ASM) \ + _("__asm", 0, CTOK_ASM) \ + _("__asm__", 0, CTOK_ASM) \ + /* MSVC Attributes. */ \ + _("__declspec", 0, CTOK_DECLSPEC) \ + _("__cdecl", CTCC_CDECL, CTOK_CCDECL) \ + _("__thiscall", CTCC_THISCALL, CTOK_CCDECL) \ + _("__fastcall", CTCC_FASTCALL, CTOK_CCDECL) \ + _("__stdcall", CTCC_STDCALL, CTOK_CCDECL) \ + _("__ptr32", 4, CTOK_PTRSZ) \ + _("__ptr64", 8, CTOK_PTRSZ) \ + /* Other type specifiers. */ \ + _("struct", 0, CTOK_STRUCT) \ + _("union", 0, CTOK_UNION) \ + _("enum", 0, CTOK_ENUM) \ + /* Operators. */ \ + _("sizeof", 0, CTOK_SIZEOF) \ + _("__alignof", 0, CTOK_ALIGNOF) \ + _("__alignof__", 0, CTOK_ALIGNOF) \ + /* End of keyword list. */ + +/* Type info for predefined types. Size merged in. */ +static CTInfo lj_ctype_typeinfo[] = { +#define CTTYINFODEF(id, sz, ct, info) CTINFO((ct),(((sz)&0x3fu)<<10)+(info)), +#define CTTDINFODEF(name, id) CTINFO(CT_TYPEDEF, CTID_##id), +#define CTKWINFODEF(name, sz, kw) CTINFO(CT_KW,(((sz)&0x3fu)<<10)+(kw)), +CTTYDEF(CTTYINFODEF) +CTTDDEF(CTTDINFODEF) +CTKWDEF(CTKWINFODEF) +#undef CTTYINFODEF +#undef CTTDINFODEF +#undef CTKWINFODEF + 0 +}; + +/* Predefined type names collected in a single string. */ +static const char * const lj_ctype_typenames = +#define CTTDNAMEDEF(name, id) name "\0" +#define CTKWNAMEDEF(name, sz, cds) name "\0" +CTTDDEF(CTTDNAMEDEF) +CTKWDEF(CTKWNAMEDEF) +#undef CTTDNAMEDEF +#undef CTKWNAMEDEF +; + +#define CTTYPEINFO_NUM (sizeof(lj_ctype_typeinfo)/sizeof(CTInfo)-1) +#ifdef LUAJIT_CTYPE_CHECK_ANCHOR +#define CTTYPETAB_MIN CTTYPEINFO_NUM +#else +#define CTTYPETAB_MIN 128 +#endif + +/* -- C type interning ---------------------------------------------------- */ + +#define ct_hashtype(info, size) (hashrot(info, size) & CTHASH_MASK) +#define ct_hashname(name) \ + (hashrot(u32ptr(name), u32ptr(name) + HASH_BIAS) & CTHASH_MASK) + +/* Create new type element. */ +CTypeID lj_ctype_new(CTState *cts, CType **ctp) +{ + CTypeID id = cts->top; + CType *ct; + lua_assert(cts->L); + if (LJ_UNLIKELY(id >= cts->sizetab)) { + if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); +#ifdef LUAJIT_CTYPE_CHECK_ANCHOR + ct = lj_mem_newvec(cts->L, id+1, CType); + memcpy(ct, cts->tab, id*sizeof(CType)); + memset(cts->tab, 0, id*sizeof(CType)); + lj_mem_freevec(cts->g, cts->tab, cts->sizetab, CType); + cts->tab = ct; + cts->sizetab = id+1; +#else + lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); +#endif + } + cts->top = id+1; + *ctp = ct = &cts->tab[id]; + ct->info = 0; + ct->size = 0; + ct->sib = 0; + ct->next = 0; + setgcrefnull(ct->name); + return id; +} + +/* Intern a type element. */ +CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size) +{ + uint32_t h = ct_hashtype(info, size); + CTypeID id = cts->hash[h]; + lua_assert(cts->L); + while (id) { + CType *ct = ctype_get(cts, id); + if (ct->info == info && ct->size == size) + return id; + id = ct->next; + } + id = cts->top; + if (LJ_UNLIKELY(id >= cts->sizetab)) { + if (id >= CTID_MAX) lj_err_msg(cts->L, LJ_ERR_TABOV); + lj_mem_growvec(cts->L, cts->tab, cts->sizetab, CTID_MAX, CType); + } + cts->top = id+1; + cts->tab[id].info = info; + cts->tab[id].size = size; + cts->tab[id].sib = 0; + cts->tab[id].next = cts->hash[h]; + setgcrefnull(cts->tab[id].name); + cts->hash[h] = (CTypeID1)id; + return id; +} + +/* Add type element to hash table. */ +static void ctype_addtype(CTState *cts, CType *ct, CTypeID id) +{ + uint32_t h = ct_hashtype(ct->info, ct->size); + ct->next = cts->hash[h]; + cts->hash[h] = (CTypeID1)id; +} + +/* Add named element to hash table. */ +void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id) +{ + uint32_t h = ct_hashname(gcref(ct->name)); + ct->next = cts->hash[h]; + cts->hash[h] = (CTypeID1)id; +} + +/* Get a C type by name, matching the type mask. */ +CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, uint32_t tmask) +{ + CTypeID id = cts->hash[ct_hashname(name)]; + while (id) { + CType *ct = ctype_get(cts, id); + if (gcref(ct->name) == obj2gco(name) && + ((tmask >> ctype_type(ct->info)) & 1)) { + *ctp = ct; + return id; + } + id = ct->next; + } + *ctp = &cts->tab[0]; /* Simplify caller logic. ctype_get() would assert. */ + return 0; +} + +/* Get a struct/union/enum/function field by name. */ +CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, CTSize *ofs, + CTInfo *qual) +{ + while (ct->sib) { + ct = ctype_get(cts, ct->sib); + if (gcref(ct->name) == obj2gco(name)) { + *ofs = ct->size; + return ct; + } + if (ctype_isxattrib(ct->info, CTA_SUBTYPE)) { + CType *fct, *cct = ctype_child(cts, ct); + CTInfo q = 0; + while (ctype_isattrib(cct->info)) { + if (ctype_attrib(cct->info) == CTA_QUAL) q |= cct->size; + cct = ctype_child(cts, cct); + } + fct = lj_ctype_getfieldq(cts, cct, name, ofs, qual); + if (fct) { + if (qual) *qual |= q; + *ofs += ct->size; + return fct; + } + } + } + return NULL; /* Not found. */ +} + +/* -- C type information -------------------------------------------------- */ + +/* Follow references and get raw type for a C type ID. */ +CType *lj_ctype_rawref(CTState *cts, CTypeID id) +{ + CType *ct = ctype_get(cts, id); + while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) + ct = ctype_child(cts, ct); + return ct; +} + +/* Get size for a C type ID. Does NOT support VLA/VLS. */ +CTSize lj_ctype_size(CTState *cts, CTypeID id) +{ + CType *ct = ctype_raw(cts, id); + return ctype_hassize(ct->info) ? ct->size : CTSIZE_INVALID; +} + +/* Get size for a variable-length C type. Does NOT support other C types. */ +CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem) +{ + uint64_t xsz = 0; + if (ctype_isstruct(ct->info)) { + CTypeID arrid = 0, fid = ct->sib; + xsz = ct->size; /* Add the struct size. */ + while (fid) { + CType *ctf = ctype_get(cts, fid); + if (ctype_type(ctf->info) == CT_FIELD) + arrid = ctype_cid(ctf->info); /* Remember last field of VLS. */ + fid = ctf->sib; + } + ct = ctype_raw(cts, arrid); + } + lua_assert(ctype_isvlarray(ct->info)); /* Must be a VLA. */ + ct = ctype_rawchild(cts, ct); /* Get array element. */ + lua_assert(ctype_hassize(ct->info)); + /* Calculate actual size of VLA and check for overflow. */ + xsz += (uint64_t)ct->size * nelem; + return xsz < 0x80000000u ? (CTSize)xsz : CTSIZE_INVALID; +} + +/* Get type, qualifiers, size and alignment for a C type ID. */ +CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp) +{ + CTInfo qual = 0; + CType *ct = ctype_get(cts, id); + for (;;) { + CTInfo info = ct->info; + if (ctype_isenum(info)) { + /* Follow child. Need to look at its attributes, too. */ + } else if (ctype_isattrib(info)) { + if (ctype_isxattrib(info, CTA_QUAL)) + qual |= ct->size; + else if (ctype_isxattrib(info, CTA_ALIGN) && !(qual & CTFP_ALIGNED)) + qual |= CTFP_ALIGNED + CTALIGN(ct->size); + } else { + if (!(qual & CTFP_ALIGNED)) qual |= (info & CTF_ALIGN); + qual |= (info & ~(CTF_ALIGN|CTMASK_CID)); + lua_assert(ctype_hassize(info) || ctype_isfunc(info)); + *szp = ctype_isfunc(info) ? CTSIZE_INVALID : ct->size; + break; + } + ct = ctype_get(cts, ctype_cid(info)); + } + return qual; +} + +/* Get ctype metamethod. */ +cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm) +{ + CType *ct = ctype_get(cts, id); + cTValue *tv; + while (ctype_isattrib(ct->info) || ctype_isref(ct->info)) { + id = ctype_cid(ct->info); + ct = ctype_get(cts, id); + } + if (ctype_isptr(ct->info) && + ctype_isfunc(ctype_get(cts, ctype_cid(ct->info))->info)) + tv = lj_tab_getstr(cts->miscmap, &cts->g->strempty); + else + tv = lj_tab_getinth(cts->miscmap, -(int32_t)id); + if (tv && tvistab(tv) && + (tv = lj_tab_getstr(tabV(tv), mmname_str(cts->g, mm))) && !tvisnil(tv)) + return tv; + return NULL; +} + +/* -- C type representation ----------------------------------------------- */ + +/* Fixed max. length of a C type representation. */ +#define CTREPR_MAX 512 + +typedef struct CTRepr { + char *pb, *pe; + CTState *cts; + lua_State *L; + int needsp; + int ok; + char buf[CTREPR_MAX]; +} CTRepr; + +/* Prepend string. */ +static void ctype_prepstr(CTRepr *ctr, const char *str, MSize len) +{ + char *p = ctr->pb; + if (ctr->buf + len+1 > p) { ctr->ok = 0; return; } + if (ctr->needsp) *--p = ' '; + ctr->needsp = 1; + p -= len; + while (len-- > 0) p[len] = str[len]; + ctr->pb = p; +} + +#define ctype_preplit(ctr, str) ctype_prepstr((ctr), "" str, sizeof(str)-1) + +/* Prepend char. */ +static void ctype_prepc(CTRepr *ctr, int c) +{ + if (ctr->buf >= ctr->pb) { ctr->ok = 0; return; } + *--ctr->pb = c; +} + +/* Prepend number. */ +static void ctype_prepnum(CTRepr *ctr, uint32_t n) +{ + char *p = ctr->pb; + if (ctr->buf + 10+1 > p) { ctr->ok = 0; return; } + do { *--p = (char)('0' + n % 10); } while (n /= 10); + ctr->pb = p; + ctr->needsp = 0; +} + +/* Append char. */ +static void ctype_appc(CTRepr *ctr, int c) +{ + if (ctr->pe >= ctr->buf + CTREPR_MAX) { ctr->ok = 0; return; } + *ctr->pe++ = c; +} + +/* Append number. */ +static void ctype_appnum(CTRepr *ctr, uint32_t n) +{ + char buf[10]; + char *p = buf+sizeof(buf); + char *q = ctr->pe; + if (q > ctr->buf + CTREPR_MAX - 10) { ctr->ok = 0; return; } + do { *--p = (char)('0' + n % 10); } while (n /= 10); + do { *q++ = *p++; } while (p < buf+sizeof(buf)); + ctr->pe = q; +} + +/* Prepend qualifiers. */ +static void ctype_prepqual(CTRepr *ctr, CTInfo info) +{ + if ((info & CTF_VOLATILE)) ctype_preplit(ctr, "volatile"); + if ((info & CTF_CONST)) ctype_preplit(ctr, "const"); +} + +/* Prepend named type. */ +static void ctype_preptype(CTRepr *ctr, CType *ct, CTInfo qual, const char *t) +{ + if (gcref(ct->name)) { + GCstr *str = gco2str(gcref(ct->name)); + ctype_prepstr(ctr, strdata(str), str->len); + } else { + if (ctr->needsp) ctype_prepc(ctr, ' '); + ctype_prepnum(ctr, ctype_typeid(ctr->cts, ct)); + ctr->needsp = 1; + } + ctype_prepstr(ctr, t, (MSize)strlen(t)); + ctype_prepqual(ctr, qual); +} + +static void ctype_repr(CTRepr *ctr, CTypeID id) +{ + CType *ct = ctype_get(ctr->cts, id); + CTInfo qual = 0; + int ptrto = 0; + for (;;) { + CTInfo info = ct->info; + CTSize size = ct->size; + switch (ctype_type(info)) { + case CT_NUM: + if ((info & CTF_BOOL)) { + ctype_preplit(ctr, "bool"); + } else if ((info & CTF_FP)) { + if (size == sizeof(double)) ctype_preplit(ctr, "double"); + else if (size == sizeof(float)) ctype_preplit(ctr, "float"); + else ctype_preplit(ctr, "long double"); + } else if (size == 1) { + if (!((info ^ CTF_UCHAR) & CTF_UNSIGNED)) ctype_preplit(ctr, "char"); + else if (CTF_UCHAR) ctype_preplit(ctr, "signed char"); + else ctype_preplit(ctr, "unsigned char"); + } else if (size < 8) { + if (size == 4) ctype_preplit(ctr, "int"); + else ctype_preplit(ctr, "short"); + if ((info & CTF_UNSIGNED)) ctype_preplit(ctr, "unsigned"); + } else { + ctype_preplit(ctr, "_t"); + ctype_prepnum(ctr, size*8); + ctype_preplit(ctr, "int"); + if ((info & CTF_UNSIGNED)) ctype_prepc(ctr, 'u'); + } + ctype_prepqual(ctr, (qual|info)); + return; + case CT_VOID: + ctype_preplit(ctr, "void"); + ctype_prepqual(ctr, (qual|info)); + return; + case CT_STRUCT: + ctype_preptype(ctr, ct, qual, (info & CTF_UNION) ? "union" : "struct"); + return; + case CT_ENUM: + if (id == CTID_CTYPEID) { + ctype_preplit(ctr, "ctype"); + return; + } + ctype_preptype(ctr, ct, qual, "enum"); + return; + case CT_ATTRIB: + if (ctype_attrib(info) == CTA_QUAL) qual |= size; + break; + case CT_PTR: + if ((info & CTF_REF)) { + ctype_prepc(ctr, '&'); + } else { + ctype_prepqual(ctr, (qual|info)); + if (LJ_64 && size == 4) ctype_preplit(ctr, "__ptr32"); + ctype_prepc(ctr, '*'); + } + qual = 0; + ptrto = 1; + ctr->needsp = 1; + break; + case CT_ARRAY: + if (ctype_isrefarray(info)) { + ctr->needsp = 1; + if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } + ctype_appc(ctr, '['); + if (size != CTSIZE_INVALID) { + CTSize csize = ctype_child(ctr->cts, ct)->size; + ctype_appnum(ctr, csize ? size/csize : 0); + } else if ((info & CTF_VLA)) { + ctype_appc(ctr, '?'); + } + ctype_appc(ctr, ']'); + } else if ((info & CTF_COMPLEX)) { + if (size == 2*sizeof(float)) ctype_preplit(ctr, "float"); + ctype_preplit(ctr, "complex"); + return; + } else { + ctype_preplit(ctr, ")))"); + ctype_prepnum(ctr, size); + ctype_preplit(ctr, "__attribute__((vector_size("); + } + break; + case CT_FUNC: + ctr->needsp = 1; + if (ptrto) { ptrto = 0; ctype_prepc(ctr, '('); ctype_appc(ctr, ')'); } + ctype_appc(ctr, '('); + ctype_appc(ctr, ')'); + break; + default: + lua_assert(0); + break; + } + ct = ctype_get(ctr->cts, ctype_cid(info)); + } +} + +/* Return a printable representation of a C type. */ +GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name) +{ + global_State *g = G(L); + CTRepr ctr; + ctr.pb = ctr.pe = &ctr.buf[CTREPR_MAX/2]; + ctr.cts = ctype_ctsG(g); + ctr.L = L; + ctr.ok = 1; + ctr.needsp = 0; + if (name) ctype_prepstr(&ctr, strdata(name), name->len); + ctype_repr(&ctr, id); + if (LJ_UNLIKELY(!ctr.ok)) return lj_str_newlit(L, "?"); + return lj_str_new(L, ctr.pb, ctr.pe - ctr.pb); +} + +/* Convert int64_t/uint64_t to string with 'LL' or 'ULL' suffix. */ +GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned) +{ + char buf[1+20+3]; + char *p = buf+sizeof(buf); + int sign = 0; + *--p = 'L'; *--p = 'L'; + if (isunsigned) { + *--p = 'U'; + } else if ((int64_t)n < 0) { + n = (uint64_t)-(int64_t)n; + sign = 1; + } + do { *--p = (char)('0' + n % 10); } while (n /= 10); + if (sign) *--p = '-'; + return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p)); +} + +/* Convert complex to string with 'i' or 'I' suffix. */ +GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size) +{ + char buf[2*LJ_STR_NUMBUF+2+1]; + TValue re, im; + size_t len; + if (size == 2*sizeof(double)) { + re.n = *(double *)sp; im.n = ((double *)sp)[1]; + } else { + re.n = (double)*(float *)sp; im.n = (double)((float *)sp)[1]; + } + len = lj_str_bufnum(buf, &re); + if (!(im.u32.hi & 0x80000000u) || im.n != im.n) buf[len++] = '+'; + len += lj_str_bufnum(buf+len, &im); + buf[len] = buf[len-1] >= 'a' ? 'I' : 'i'; + return lj_str_new(L, buf, len+1); +} + +/* -- C type state -------------------------------------------------------- */ + +/* Initialize C type table and state. */ +CTState *lj_ctype_init(lua_State *L) +{ + CTState *cts = lj_mem_newt(L, sizeof(CTState), CTState); + CType *ct = lj_mem_newvec(L, CTTYPETAB_MIN, CType); + const char *name = lj_ctype_typenames; + CTypeID id; + memset(cts, 0, sizeof(CTState)); + cts->tab = ct; + cts->sizetab = CTTYPETAB_MIN; + cts->top = CTTYPEINFO_NUM; + cts->L = NULL; + cts->g = G(L); + for (id = 0; id < CTTYPEINFO_NUM; id++, ct++) { + CTInfo info = lj_ctype_typeinfo[id]; + ct->size = (CTSize)((int32_t)(info << 16) >> 26); + ct->info = info & 0xffff03ffu; + ct->sib = 0; + if (ctype_type(info) == CT_KW || ctype_istypedef(info)) { + size_t len = strlen(name); + GCstr *str = lj_str_new(L, name, len); + ctype_setname(ct, str); + name += len+1; + lj_ctype_addname(cts, ct, id); + } else { + setgcrefnull(ct->name); + ct->next = 0; + if (!ctype_isenum(info)) ctype_addtype(cts, ct, id); + } + } + setmref(G(L)->ctype_state, cts); + return cts; +} + +/* Free C type table and state. */ +void lj_ctype_freestate(global_State *g) +{ + CTState *cts = ctype_ctsG(g); + if (cts) { + lj_ccallback_mcode_free(cts); + lj_mem_freevec(g, cts->tab, cts->sizetab, CType); + lj_mem_freevec(g, cts->cb.cbid, cts->cb.sizeid, CTypeID1); + lj_mem_freet(g, cts); + } +} + +#endif diff --git a/external/lua/luajit/src/src/lj_ctype.h b/external/lua/luajit/src/src/lj_ctype.h new file mode 100644 index 0000000000..c43a2ba875 --- /dev/null +++ b/external/lua/luajit/src/src/lj_ctype.h @@ -0,0 +1,461 @@ +/* +** C type management. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_CTYPE_H +#define _LJ_CTYPE_H + +#include "lj_obj.h" +#include "lj_gc.h" + +#if LJ_HASFFI + +/* -- C type definitions -------------------------------------------------- */ + +/* C type numbers. Highest 4 bits of C type info. ORDER CT. */ +enum { + /* Externally visible types. */ + CT_NUM, /* Integer or floating-point numbers. */ + CT_STRUCT, /* Struct or union. */ + CT_PTR, /* Pointer or reference. */ + CT_ARRAY, /* Array or complex type. */ + CT_MAYCONVERT = CT_ARRAY, + CT_VOID, /* Void type. */ + CT_ENUM, /* Enumeration. */ + CT_HASSIZE = CT_ENUM, /* Last type where ct->size holds the actual size. */ + CT_FUNC, /* Function. */ + CT_TYPEDEF, /* Typedef. */ + CT_ATTRIB, /* Miscellaneous attributes. */ + /* Internal element types. */ + CT_FIELD, /* Struct/union field or function parameter. */ + CT_BITFIELD, /* Struct/union bitfield. */ + CT_CONSTVAL, /* Constant value. */ + CT_EXTERN, /* External reference. */ + CT_KW /* Keyword. */ +}; + +LJ_STATIC_ASSERT(((int)CT_PTR & (int)CT_ARRAY) == CT_PTR); +LJ_STATIC_ASSERT(((int)CT_STRUCT & (int)CT_ARRAY) == CT_STRUCT); + +/* +** ---------- info ------------ +** |type flags... A cid | size | sib | next | name | +** +----------------------------+--------+-------+-------+-------+-- +** |NUM BFvcUL.. A | size | | type | | +** |STRUCT ..vcU..V A | size | field | name? | name? | +** |PTR ..vcR... A cid | size | | type | | +** |ARRAY VCvc...V A cid | size | | type | | +** |VOID ..vc.... A | size | | type | | +** |ENUM A cid | size | const | name? | name? | +** |FUNC ....VS.. cc cid | nargs | field | name? | name? | +** |TYPEDEF cid | | | name | name | +** |ATTRIB attrnum cid | attr | sib? | type? | | +** |FIELD cid | offset | field | | name? | +** |BITFIELD B.vcU csz bsz pos | offset | field | | name? | +** |CONSTVAL c cid | value | const | name | name | +** |EXTERN cid | | sib? | name | name | +** |KW tok | size | | name | name | +** +----------------------------+--------+-------+-------+-------+-- +** ^^ ^^--- bits used for C type conversion dispatch +*/ + +/* C type info flags. TFFArrrr */ +#define CTF_BOOL 0x08000000u /* Boolean: NUM, BITFIELD. */ +#define CTF_FP 0x04000000u /* Floating-point: NUM. */ +#define CTF_CONST 0x02000000u /* Const qualifier. */ +#define CTF_VOLATILE 0x01000000u /* Volatile qualifier. */ +#define CTF_UNSIGNED 0x00800000u /* Unsigned: NUM, BITFIELD. */ +#define CTF_LONG 0x00400000u /* Long: NUM. */ +#define CTF_VLA 0x00100000u /* Variable-length: ARRAY, STRUCT. */ +#define CTF_REF 0x00800000u /* Reference: PTR. */ +#define CTF_VECTOR 0x08000000u /* Vector: ARRAY. */ +#define CTF_COMPLEX 0x04000000u /* Complex: ARRAY. */ +#define CTF_UNION 0x00800000u /* Union: STRUCT. */ +#define CTF_VARARG 0x00800000u /* Vararg: FUNC. */ +#define CTF_SSEREGPARM 0x00400000u /* SSE register parameters: FUNC. */ + +#define CTF_QUAL (CTF_CONST|CTF_VOLATILE) +#define CTF_ALIGN (CTMASK_ALIGN< 0 ? CTF_UNSIGNED : 0) + +/* Flags used in parser. .F.Ammvf cp->attr */ +#define CTFP_ALIGNED 0x00000001u /* cp->attr + ALIGN */ +#define CTFP_PACKED 0x00000002u /* cp->attr */ +/* ...C...f cp->fattr */ +#define CTFP_CCONV 0x00000001u /* cp->fattr + CCONV/[SSE]REGPARM */ + +/* C type info bitfields. */ +#define CTMASK_CID 0x0000ffffu /* Max. 65536 type IDs. */ +#define CTMASK_NUM 0xf0000000u /* Max. 16 type numbers. */ +#define CTSHIFT_NUM 28 +#define CTMASK_ALIGN 15 /* Max. alignment is 2^15. */ +#define CTSHIFT_ALIGN 16 +#define CTMASK_ATTRIB 255 /* Max. 256 attributes. */ +#define CTSHIFT_ATTRIB 16 +#define CTMASK_CCONV 3 /* Max. 4 calling conventions. */ +#define CTSHIFT_CCONV 16 +#define CTMASK_REGPARM 3 /* Max. 0-3 regparms. */ +#define CTSHIFT_REGPARM 18 +/* Bitfields only used in parser. */ +#define CTMASK_VSIZEP 15 /* Max. vector size is 2^15. */ +#define CTSHIFT_VSIZEP 4 +#define CTMASK_MSIZEP 255 /* Max. type size (via mode) is 128. */ +#define CTSHIFT_MSIZEP 8 + +/* Info bits for BITFIELD. Max. size of bitfield is 64 bits. */ +#define CTBSZ_MAX 32 /* Max. size of bitfield is 32 bit. */ +#define CTBSZ_FIELD 127 /* Temp. marker for regular field. */ +#define CTMASK_BITPOS 127 +#define CTMASK_BITBSZ 127 +#define CTMASK_BITCSZ 127 +#define CTSHIFT_BITPOS 0 +#define CTSHIFT_BITBSZ 8 +#define CTSHIFT_BITCSZ 16 + +#define CTF_INSERT(info, field, val) \ + info = (info & ~(CTMASK_##field<> CTSHIFT_NUM) +#define ctype_cid(info) ((CTypeID)((info) & CTMASK_CID)) +#define ctype_align(info) (((info) >> CTSHIFT_ALIGN) & CTMASK_ALIGN) +#define ctype_attrib(info) (((info) >> CTSHIFT_ATTRIB) & CTMASK_ATTRIB) +#define ctype_bitpos(info) (((info) >> CTSHIFT_BITPOS) & CTMASK_BITPOS) +#define ctype_bitbsz(info) (((info) >> CTSHIFT_BITBSZ) & CTMASK_BITBSZ) +#define ctype_bitcsz(info) (((info) >> CTSHIFT_BITCSZ) & CTMASK_BITCSZ) +#define ctype_vsizeP(info) (((info) >> CTSHIFT_VSIZEP) & CTMASK_VSIZEP) +#define ctype_msizeP(info) (((info) >> CTSHIFT_MSIZEP) & CTMASK_MSIZEP) +#define ctype_cconv(info) (((info) >> CTSHIFT_CCONV) & CTMASK_CCONV) + +/* Simple type checks. */ +#define ctype_isnum(info) (ctype_type((info)) == CT_NUM) +#define ctype_isvoid(info) (ctype_type((info)) == CT_VOID) +#define ctype_isptr(info) (ctype_type((info)) == CT_PTR) +#define ctype_isarray(info) (ctype_type((info)) == CT_ARRAY) +#define ctype_isstruct(info) (ctype_type((info)) == CT_STRUCT) +#define ctype_isfunc(info) (ctype_type((info)) == CT_FUNC) +#define ctype_isenum(info) (ctype_type((info)) == CT_ENUM) +#define ctype_istypedef(info) (ctype_type((info)) == CT_TYPEDEF) +#define ctype_isattrib(info) (ctype_type((info)) == CT_ATTRIB) +#define ctype_isfield(info) (ctype_type((info)) == CT_FIELD) +#define ctype_isbitfield(info) (ctype_type((info)) == CT_BITFIELD) +#define ctype_isconstval(info) (ctype_type((info)) == CT_CONSTVAL) +#define ctype_isextern(info) (ctype_type((info)) == CT_EXTERN) +#define ctype_hassize(info) (ctype_type((info)) <= CT_HASSIZE) + +/* Combined type and flag checks. */ +#define ctype_isinteger(info) \ + (((info) & (CTMASK_NUM|CTF_BOOL|CTF_FP)) == CTINFO(CT_NUM, 0)) +#define ctype_isinteger_or_bool(info) \ + (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, 0)) +#define ctype_isbool(info) \ + (((info) & (CTMASK_NUM|CTF_BOOL)) == CTINFO(CT_NUM, CTF_BOOL)) +#define ctype_isfp(info) \ + (((info) & (CTMASK_NUM|CTF_FP)) == CTINFO(CT_NUM, CTF_FP)) + +#define ctype_ispointer(info) \ + ((ctype_type(info) >> 1) == (CT_PTR >> 1)) /* Pointer or array. */ +#define ctype_isref(info) \ + (((info) & (CTMASK_NUM|CTF_REF)) == CTINFO(CT_PTR, CTF_REF)) + +#define ctype_isrefarray(info) \ + (((info) & (CTMASK_NUM|CTF_VECTOR|CTF_COMPLEX)) == CTINFO(CT_ARRAY, 0)) +#define ctype_isvector(info) \ + (((info) & (CTMASK_NUM|CTF_VECTOR)) == CTINFO(CT_ARRAY, CTF_VECTOR)) +#define ctype_iscomplex(info) \ + (((info) & (CTMASK_NUM|CTF_COMPLEX)) == CTINFO(CT_ARRAY, CTF_COMPLEX)) + +#define ctype_isvltype(info) \ + (((info) & ((CTMASK_NUM|CTF_VLA) - (2u<") _(STRING, "") \ + _(INTEGER, "") _(EOF, "") \ + _(OROR, "||") _(ANDAND, "&&") _(EQ, "==") _(NE, "!=") \ + _(LE, "<=") _(GE, ">=") _(SHL, "<<") _(SHR, ">>") _(DEREF, "->") + +/* Simple declaration specifiers. */ +#define CDSDEF(_) \ + _(VOID) _(BOOL) _(CHAR) _(INT) _(FP) \ + _(LONG) _(LONGLONG) _(SHORT) _(COMPLEX) _(SIGNED) _(UNSIGNED) \ + _(CONST) _(VOLATILE) _(RESTRICT) _(INLINE) \ + _(TYPEDEF) _(EXTERN) _(STATIC) _(AUTO) _(REGISTER) + +/* C keywords. */ +#define CKWDEF(_) \ + CDSDEF(_) _(EXTENSION) _(ASM) _(ATTRIBUTE) \ + _(DECLSPEC) _(CCDECL) _(PTRSZ) \ + _(STRUCT) _(UNION) _(ENUM) \ + _(SIZEOF) _(ALIGNOF) + +/* C token numbers. */ +enum { + CTOK_OFS = 255, +#define CTOKNUM(name, sym) CTOK_##name, +#define CKWNUM(name) CTOK_##name, +CTOKDEF(CTOKNUM) +CKWDEF(CKWNUM) +#undef CTOKNUM +#undef CKWNUM + CTOK_FIRSTDECL = CTOK_VOID, + CTOK_FIRSTSCL = CTOK_TYPEDEF, + CTOK_LASTDECLFLAG = CTOK_REGISTER, + CTOK_LASTDECL = CTOK_ENUM +}; + +/* Declaration specifier flags. */ +enum { +#define CDSFLAG(name) CDF_##name = (1u << (CTOK_##name - CTOK_FIRSTDECL)), +CDSDEF(CDSFLAG) +#undef CDSFLAG + CDF__END +}; + +#define CDF_SCL (CDF_TYPEDEF|CDF_EXTERN|CDF_STATIC|CDF_AUTO|CDF_REGISTER) + +/* -- C type management --------------------------------------------------- */ + +#define ctype_ctsG(g) (mref((g)->ctype_state, CTState)) + +/* Get C type state. */ +static LJ_AINLINE CTState *ctype_cts(lua_State *L) +{ + CTState *cts = ctype_ctsG(G(L)); + cts->L = L; /* Save L for errors and allocations. */ + return cts; +} + +/* Save and restore state of C type table. */ +#define LJ_CTYPE_SAVE(cts) CTState savects_ = *(cts) +#define LJ_CTYPE_RESTORE(cts) \ + ((cts)->top = savects_.top, \ + memcpy((cts)->hash, savects_.hash, sizeof(savects_.hash))) + +/* Check C type ID for validity when assertions are enabled. */ +static LJ_AINLINE CTypeID ctype_check(CTState *cts, CTypeID id) +{ + lua_assert(id > 0 && id < cts->top); UNUSED(cts); + return id; +} + +/* Get C type for C type ID. */ +static LJ_AINLINE CType *ctype_get(CTState *cts, CTypeID id) +{ + return &cts->tab[ctype_check(cts, id)]; +} + +/* Get C type ID for a C type. */ +#define ctype_typeid(cts, ct) ((CTypeID)((ct) - (cts)->tab)) + +/* Get child C type. */ +static LJ_AINLINE CType *ctype_child(CTState *cts, CType *ct) +{ + lua_assert(!(ctype_isvoid(ct->info) || ctype_isstruct(ct->info) || + ctype_isbitfield(ct->info))); /* These don't have children. */ + return ctype_get(cts, ctype_cid(ct->info)); +} + +/* Get raw type for a C type ID. */ +static LJ_AINLINE CType *ctype_raw(CTState *cts, CTypeID id) +{ + CType *ct = ctype_get(cts, id); + while (ctype_isattrib(ct->info)) ct = ctype_child(cts, ct); + return ct; +} + +/* Get raw type of the child of a C type. */ +static LJ_AINLINE CType *ctype_rawchild(CTState *cts, CType *ct) +{ + do { ct = ctype_child(cts, ct); } while (ctype_isattrib(ct->info)); + return ct; +} + +/* Set the name of a C type table element. */ +static LJ_AINLINE void ctype_setname(CType *ct, GCstr *s) +{ + /* NOBARRIER: mark string as fixed -- the C type table is never collected. */ + fixstring(s); + setgcref(ct->name, obj2gco(s)); +} + +LJ_FUNC CTypeID lj_ctype_new(CTState *cts, CType **ctp); +LJ_FUNC CTypeID lj_ctype_intern(CTState *cts, CTInfo info, CTSize size); +LJ_FUNC void lj_ctype_addname(CTState *cts, CType *ct, CTypeID id); +LJ_FUNC CTypeID lj_ctype_getname(CTState *cts, CType **ctp, GCstr *name, + uint32_t tmask); +LJ_FUNC CType *lj_ctype_getfieldq(CTState *cts, CType *ct, GCstr *name, + CTSize *ofs, CTInfo *qual); +#define lj_ctype_getfield(cts, ct, name, ofs) \ + lj_ctype_getfieldq((cts), (ct), (name), (ofs), NULL) +LJ_FUNC CType *lj_ctype_rawref(CTState *cts, CTypeID id); +LJ_FUNC CTSize lj_ctype_size(CTState *cts, CTypeID id); +LJ_FUNC CTSize lj_ctype_vlsize(CTState *cts, CType *ct, CTSize nelem); +LJ_FUNC CTInfo lj_ctype_info(CTState *cts, CTypeID id, CTSize *szp); +LJ_FUNC cTValue *lj_ctype_meta(CTState *cts, CTypeID id, MMS mm); +LJ_FUNC GCstr *lj_ctype_repr(lua_State *L, CTypeID id, GCstr *name); +LJ_FUNC GCstr *lj_ctype_repr_int64(lua_State *L, uint64_t n, int isunsigned); +LJ_FUNC GCstr *lj_ctype_repr_complex(lua_State *L, void *sp, CTSize size); +LJ_FUNC CTState *lj_ctype_init(lua_State *L); +LJ_FUNC void lj_ctype_freestate(global_State *g); + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_debug.c b/external/lua/luajit/src/src/lj_debug.c new file mode 100644 index 0000000000..be7fb2b1f0 --- /dev/null +++ b/external/lua/luajit/src/src/lj_debug.c @@ -0,0 +1,596 @@ +/* +** Debugging and introspection. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_debug_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_err.h" +#include "lj_debug.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_state.h" +#include "lj_frame.h" +#include "lj_bc.h" +#if LJ_HASJIT +#include "lj_jit.h" +#endif + +/* -- Frames -------------------------------------------------------------- */ + +/* Get frame corresponding to a level. */ +cTValue *lj_debug_frame(lua_State *L, int level, int *size) +{ + cTValue *frame, *nextframe, *bot = tvref(L->stack); + /* Traverse frames backwards. */ + for (nextframe = frame = L->base-1; frame > bot; ) { + if (frame_gc(frame) == obj2gco(L)) + level++; /* Skip dummy frames. See lj_meta_call(). */ + if (level-- == 0) { + *size = (int)(nextframe - frame); + return frame; /* Level found. */ + } + nextframe = frame; + if (frame_islua(frame)) { + frame = frame_prevl(frame); + } else { + if (frame_isvarg(frame)) + level++; /* Skip vararg pseudo-frame. */ + frame = frame_prevd(frame); + } + } + *size = level; + return NULL; /* Level not found. */ +} + +/* Invalid bytecode position. */ +#define NO_BCPOS (~(BCPos)0) + +/* Return bytecode position for function/frame or NO_BCPOS. */ +static BCPos debug_framepc(lua_State *L, GCfunc *fn, cTValue *nextframe) +{ + const BCIns *ins; + GCproto *pt; + BCPos pos; + lua_assert(fn->c.gct == ~LJ_TFUNC || fn->c.gct == ~LJ_TTHREAD); + if (!isluafunc(fn)) { /* Cannot derive a PC for non-Lua functions. */ + return NO_BCPOS; + } else if (nextframe == NULL) { /* Lua function on top. */ + void *cf = cframe_raw(L->cframe); + if (cf == NULL || (char *)cframe_pc(cf) == (char *)cframe_L(cf)) + return NO_BCPOS; + ins = cframe_pc(cf); /* Only happens during error/hook handling. */ + } else { + if (frame_islua(nextframe)) { + ins = frame_pc(nextframe); + } else if (frame_iscont(nextframe)) { + ins = frame_contpc(nextframe); + } else { + /* Lua function below errfunc/gc/hook: find cframe to get the PC. */ + void *cf = cframe_raw(L->cframe); + TValue *f = L->base-1; + if (cf == NULL) + return NO_BCPOS; + while (f > nextframe) { + if (frame_islua(f)) { + f = frame_prevl(f); + } else { + if (frame_isc(f)) + cf = cframe_raw(cframe_prev(cf)); + f = frame_prevd(f); + } + } + if (cframe_prev(cf)) + cf = cframe_raw(cframe_prev(cf)); + ins = cframe_pc(cf); + } + } + pt = funcproto(fn); + pos = proto_bcpos(pt, ins) - 1; +#if LJ_HASJIT + if (pos > pt->sizebc) { /* Undo the effects of lj_trace_exit for JLOOP. */ + GCtrace *T = (GCtrace *)((char *)(ins-1) - offsetof(GCtrace, startins)); + lua_assert(bc_isret(bc_op(ins[-1]))); + pos = proto_bcpos(pt, mref(T->startpc, const BCIns)); + } +#endif + return pos; +} + +/* -- Line numbers -------------------------------------------------------- */ + +/* Get line number for a bytecode position. */ +BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc) +{ + const void *lineinfo = proto_lineinfo(pt); + if (pc <= pt->sizebc && lineinfo) { + BCLine first = pt->firstline; + if (pc == pt->sizebc) return first + pt->numline; + if (pc-- == 0) return first; + if (pt->numline < 256) + return first + (BCLine)((const uint8_t *)lineinfo)[pc]; + else if (pt->numline < 65536) + return first + (BCLine)((const uint16_t *)lineinfo)[pc]; + else + return first + (BCLine)((const uint32_t *)lineinfo)[pc]; + } + return 0; +} + +/* Get line number for function/frame. */ +static BCLine debug_frameline(lua_State *L, GCfunc *fn, cTValue *nextframe) +{ + BCPos pc = debug_framepc(L, fn, nextframe); + if (pc != NO_BCPOS) { + GCproto *pt = funcproto(fn); + lua_assert(pc <= pt->sizebc); + return lj_debug_line(pt, pc); + } + return -1; +} + +/* -- Variable names ------------------------------------------------------ */ + +/* Read ULEB128 value. */ +static uint32_t debug_read_uleb128(const uint8_t **pp) +{ + const uint8_t *p = *pp; + uint32_t v = *p++; + if (LJ_UNLIKELY(v >= 0x80)) { + int sh = 0; + v &= 0x7f; + do { v |= ((*p & 0x7f) << (sh += 7)); } while (*p++ >= 0x80); + } + *pp = p; + return v; +} + +/* Get name of a local variable from slot number and PC. */ +static const char *debug_varname(const GCproto *pt, BCPos pc, BCReg slot) +{ + const uint8_t *p = proto_varinfo(pt); + if (p) { + BCPos lastpc = 0; + for (;;) { + const char *name = (const char *)p; + uint32_t vn = *p++; + BCPos startpc, endpc; + if (vn < VARNAME__MAX) { + if (vn == VARNAME_END) break; /* End of varinfo. */ + } else { + while (*p++) ; /* Skip over variable name string. */ + } + lastpc = startpc = lastpc + debug_read_uleb128(&p); + if (startpc > pc) break; + endpc = startpc + debug_read_uleb128(&p); + if (pc < endpc && slot-- == 0) { + if (vn < VARNAME__MAX) { +#define VARNAMESTR(name, str) str "\0" + name = VARNAMEDEF(VARNAMESTR); +#undef VARNAMESTR + if (--vn) while (*name++ || --vn) ; + } + return name; + } + } + } + return NULL; +} + +/* Get name of local variable from 1-based slot number and function/frame. */ +static TValue *debug_localname(lua_State *L, const lua_Debug *ar, + const char **name, BCReg slot1) +{ + uint32_t offset = (uint32_t)ar->i_ci & 0xffff; + uint32_t size = (uint32_t)ar->i_ci >> 16; + TValue *frame = tvref(L->stack) + offset; + TValue *nextframe = size ? frame + size : NULL; + GCfunc *fn = frame_func(frame); + BCPos pc = debug_framepc(L, fn, nextframe); + if (!nextframe) nextframe = L->top; + if ((int)slot1 < 0) { /* Negative slot number is for varargs. */ + if (pc != NO_BCPOS) { + GCproto *pt = funcproto(fn); + if ((pt->flags & PROTO_VARARG)) { + slot1 = pt->numparams + (BCReg)(-(int)slot1); + if (frame_isvarg(frame)) { /* Vararg frame has been set up? (pc!=0) */ + nextframe = frame; + frame = frame_prevd(frame); + } + if (frame + slot1 < nextframe) { + *name = "(*vararg)"; + return frame+slot1; + } + } + } + return NULL; + } + if (pc != NO_BCPOS && + (*name = debug_varname(funcproto(fn), pc, slot1-1)) != NULL) + ; + else if (slot1 > 0 && frame + slot1 < nextframe) + *name = "(*temporary)"; + return frame+slot1; +} + +/* Get name of upvalue. */ +const char *lj_debug_uvname(GCproto *pt, uint32_t idx) +{ + const uint8_t *p = proto_uvinfo(pt); + lua_assert(idx < pt->sizeuv); + if (!p) return ""; + if (idx) while (*p++ || --idx) ; + return (const char *)p; +} + +/* Get name and value of upvalue. */ +const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp) +{ + if (tvisfunc(o)) { + GCfunc *fn = funcV(o); + if (isluafunc(fn)) { + GCproto *pt = funcproto(fn); + if (idx < pt->sizeuv) { + *tvp = uvval(&gcref(fn->l.uvptr[idx])->uv); + return lj_debug_uvname(pt, idx); + } + } else { + if (idx < fn->c.nupvalues) { + *tvp = &fn->c.upvalue[idx]; + return ""; + } + } + } + return NULL; +} + +/* Deduce name of an object from slot number and PC. */ +const char *lj_debug_slotname(GCproto *pt, const BCIns *ip, BCReg slot, + const char **name) +{ + const char *lname; +restart: + lname = debug_varname(pt, proto_bcpos(pt, ip), slot); + if (lname != NULL) { *name = lname; return "local"; } + while (--ip > proto_bc(pt)) { + BCIns ins = *ip; + BCOp op = bc_op(ins); + BCReg ra = bc_a(ins); + if (bcmode_a(op) == BCMbase) { + if (slot >= ra && (op != BC_KNIL || slot <= bc_d(ins))) + return NULL; + } else if (bcmode_a(op) == BCMdst && ra == slot) { + switch (bc_op(ins)) { + case BC_MOV: + if (ra == slot) { slot = bc_d(ins); goto restart; } + break; + case BC_GGET: + *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_d(ins)))); + return "global"; + case BC_TGETS: + *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_c(ins)))); + if (ip > proto_bc(pt)) { + BCIns insp = ip[-1]; + if (bc_op(insp) == BC_MOV && bc_a(insp) == ra+1 && + bc_d(insp) == bc_b(ins)) + return "method"; + } + return "field"; + case BC_UGET: + *name = lj_debug_uvname(pt, bc_d(ins)); + return "upvalue"; + default: + return NULL; + } + } + } + return NULL; +} + +/* Deduce function name from caller of a frame. */ +const char *lj_debug_funcname(lua_State *L, TValue *frame, const char **name) +{ + TValue *pframe; + GCfunc *fn; + BCPos pc; + if (frame <= tvref(L->stack)) + return NULL; + if (frame_isvarg(frame)) + frame = frame_prevd(frame); + pframe = frame_prev(frame); + fn = frame_func(pframe); + pc = debug_framepc(L, fn, frame); + if (pc != NO_BCPOS) { + GCproto *pt = funcproto(fn); + const BCIns *ip = &proto_bc(pt)[check_exp(pc < pt->sizebc, pc)]; + MMS mm = bcmode_mm(bc_op(*ip)); + if (mm == MM_call) { + BCReg slot = bc_a(*ip); + if (bc_op(*ip) == BC_ITERC) slot -= 3; + return lj_debug_slotname(pt, ip, slot, name); + } else if (mm != MM__MAX) { + *name = strdata(mmname_str(G(L), mm)); + return "metamethod"; + } + } + return NULL; +} + +/* -- Source code locations ----------------------------------------------- */ + +/* Generate shortened source name. */ +void lj_debug_shortname(char *out, GCstr *str) +{ + const char *src = strdata(str); + if (*src == '=') { + strncpy(out, src+1, LUA_IDSIZE); /* Remove first char. */ + out[LUA_IDSIZE-1] = '\0'; /* Ensures null termination. */ + } else if (*src == '@') { /* Output "source", or "...source". */ + size_t len = str->len-1; + src++; /* Skip the `@' */ + if (len >= LUA_IDSIZE) { + src += len-(LUA_IDSIZE-4); /* Get last part of file name. */ + *out++ = '.'; *out++ = '.'; *out++ = '.'; + } + strcpy(out, src); + } else { /* Output [string "string"]. */ + size_t len; /* Length, up to first control char. */ + for (len = 0; len < LUA_IDSIZE-12; len++) + if (((const unsigned char *)src)[len] < ' ') break; + strcpy(out, "[string \""); out += 9; + if (src[len] != '\0') { /* Must truncate? */ + if (len > LUA_IDSIZE-15) len = LUA_IDSIZE-15; + strncpy(out, src, len); out += len; + strcpy(out, "..."); out += 3; + } else { + strcpy(out, src); out += len; + } + strcpy(out, "\"]"); + } +} + +/* Add current location of a frame to error message. */ +void lj_debug_addloc(lua_State *L, const char *msg, + cTValue *frame, cTValue *nextframe) +{ + if (frame) { + GCfunc *fn = frame_func(frame); + if (isluafunc(fn)) { + BCLine line = debug_frameline(L, fn, nextframe); + if (line >= 0) { + char buf[LUA_IDSIZE]; + lj_debug_shortname(buf, proto_chunkname(funcproto(fn))); + lj_str_pushf(L, "%s:%d: %s", buf, line, msg); + return; + } + } + } + lj_str_pushf(L, "%s", msg); +} + +/* Push location string for a bytecode position to Lua stack. */ +void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc) +{ + GCstr *name = proto_chunkname(pt); + const char *s = strdata(name); + MSize i, len = name->len; + BCLine line = lj_debug_line(pt, pc); + if (*s == '@') { + s++; len--; + for (i = len; i > 0; i--) + if (s[i] == '/' || s[i] == '\\') { + s += i+1; + break; + } + lj_str_pushf(L, "%s:%d", s, line); + } else if (len > 40) { + lj_str_pushf(L, "%p:%d", pt, line); + } else if (*s == '=') { + lj_str_pushf(L, "%s:%d", s+1, line); + } else { + lj_str_pushf(L, "\"%s\":%d", s, line); + } +} + +/* -- Public debug API ---------------------------------------------------- */ + +/* lua_getupvalue() and lua_setupvalue() are in lj_api.c. */ + +LUA_API const char *lua_getlocal(lua_State *L, const lua_Debug *ar, int n) +{ + const char *name = NULL; + if (ar) { + TValue *o = debug_localname(L, ar, &name, (BCReg)n); + if (name) { + copyTV(L, L->top, o); + incr_top(L); + } + } else if (tvisfunc(L->top-1) && isluafunc(funcV(L->top-1))) { + name = debug_varname(funcproto(funcV(L->top-1)), 0, (BCReg)n-1); + } + return name; +} + +LUA_API const char *lua_setlocal(lua_State *L, const lua_Debug *ar, int n) +{ + const char *name = NULL; + TValue *o = debug_localname(L, ar, &name, (BCReg)n); + if (name) + copyTV(L, o, L->top-1); + L->top--; + return name; +} + +int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, int ext) +{ + int opt_f = 0, opt_L = 0; + TValue *frame = NULL; + TValue *nextframe = NULL; + GCfunc *fn; + if (*what == '>') { + TValue *func = L->top - 1; + api_check(L, tvisfunc(func)); + fn = funcV(func); + L->top--; + what++; + } else { + uint32_t offset = (uint32_t)ar->i_ci & 0xffff; + uint32_t size = (uint32_t)ar->i_ci >> 16; + lua_assert(offset != 0); + frame = tvref(L->stack) + offset; + if (size) nextframe = frame + size; + lua_assert(frame <= tvref(L->maxstack) && + (!nextframe || nextframe <= tvref(L->maxstack))); + fn = frame_func(frame); + lua_assert(fn->c.gct == ~LJ_TFUNC); + } + for (; *what; what++) { + if (*what == 'S') { + if (isluafunc(fn)) { + GCproto *pt = funcproto(fn); + BCLine firstline = pt->firstline; + GCstr *name = proto_chunkname(pt); + ar->source = strdata(name); + lj_debug_shortname(ar->short_src, name); + ar->linedefined = (int)firstline; + ar->lastlinedefined = (int)(firstline + pt->numline); + ar->what = firstline ? "Lua" : "main"; + } else { + ar->source = "=[C]"; + ar->short_src[0] = '['; + ar->short_src[1] = 'C'; + ar->short_src[2] = ']'; + ar->short_src[3] = '\0'; + ar->linedefined = -1; + ar->lastlinedefined = -1; + ar->what = "C"; + } + } else if (*what == 'l') { + ar->currentline = frame ? debug_frameline(L, fn, nextframe) : -1; + } else if (*what == 'u') { + ar->nups = fn->c.nupvalues; + if (ext) { + if (isluafunc(fn)) { + GCproto *pt = funcproto(fn); + ar->nparams = pt->numparams; + ar->isvararg = !!(pt->flags & PROTO_VARARG); + } else { + ar->nparams = 0; + ar->isvararg = 1; + } + } + } else if (*what == 'n') { + ar->namewhat = frame ? lj_debug_funcname(L, frame, &ar->name) : NULL; + if (ar->namewhat == NULL) { + ar->namewhat = ""; + ar->name = NULL; + } + } else if (*what == 'f') { + opt_f = 1; + } else if (*what == 'L') { + opt_L = 1; + } else { + return 0; /* Bad option. */ + } + } + if (opt_f) { + setfuncV(L, L->top, fn); + incr_top(L); + } + if (opt_L) { + if (isluafunc(fn)) { + GCtab *t = lj_tab_new(L, 0, 0); + GCproto *pt = funcproto(fn); + const void *lineinfo = proto_lineinfo(pt); + if (lineinfo) { + BCLine first = pt->firstline; + int sz = pt->numline < 256 ? 1 : pt->numline < 65536 ? 2 : 4; + MSize i, szl = pt->sizebc-1; + for (i = 0; i < szl; i++) { + BCLine line = first + + (sz == 1 ? (BCLine)((const uint8_t *)lineinfo)[i] : + sz == 2 ? (BCLine)((const uint16_t *)lineinfo)[i] : + (BCLine)((const uint32_t *)lineinfo)[i]); + setboolV(lj_tab_setint(L, t, line), 1); + } + } + settabV(L, L->top, t); + } else { + setnilV(L->top); + } + incr_top(L); + } + return 1; /* Ok. */ +} + +LUA_API int lua_getinfo(lua_State *L, const char *what, lua_Debug *ar) +{ + return lj_debug_getinfo(L, what, (lj_Debug *)ar, 0); +} + +LUA_API int lua_getstack(lua_State *L, int level, lua_Debug *ar) +{ + int size; + cTValue *frame = lj_debug_frame(L, level, &size); + if (frame) { + ar->i_ci = (size << 16) + (int)(frame - tvref(L->stack)); + return 1; + } else { + ar->i_ci = level - size; + return 0; + } +} + +/* Number of frames for the leading and trailing part of a traceback. */ +#define TRACEBACK_LEVELS1 12 +#define TRACEBACK_LEVELS2 10 + +LUALIB_API void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, + int level) +{ + int top = (int)(L->top - L->base); + int lim = TRACEBACK_LEVELS1; + lua_Debug ar; + if (msg) lua_pushfstring(L, "%s\n", msg); + lua_pushliteral(L, "stack traceback:"); + while (lua_getstack(L1, level++, &ar)) { + GCfunc *fn; + if (level > lim) { + if (!lua_getstack(L1, level + TRACEBACK_LEVELS2, &ar)) { + level--; + } else { + lua_pushliteral(L, "\n\t..."); + lua_getstack(L1, -10, &ar); + level = ar.i_ci - TRACEBACK_LEVELS2; + } + lim = 2147483647; + continue; + } + lua_getinfo(L1, "Snlf", &ar); + fn = funcV(L1->top-1); L1->top--; + if (isffunc(fn) && !*ar.namewhat) + lua_pushfstring(L, "\n\t[builtin#%d]:", fn->c.ffid); + else + lua_pushfstring(L, "\n\t%s:", ar.short_src); + if (ar.currentline > 0) + lua_pushfstring(L, "%d:", ar.currentline); + if (*ar.namewhat) { + lua_pushfstring(L, " in function " LUA_QS, ar.name); + } else { + if (*ar.what == 'm') { + lua_pushliteral(L, " in main chunk"); + } else if (*ar.what == 'C') { + lua_pushfstring(L, " at %p", fn->c.f); + } else { + lua_pushfstring(L, " in function <%s:%d>", + ar.short_src, ar.linedefined); + } + } + if ((int)(L->top - L->base) - top >= 15) + lua_concat(L, (int)(L->top - L->base) - top); + } + lua_concat(L, (int)(L->top - L->base) - top); +} + diff --git a/external/lua/luajit/src/src/lj_debug.h b/external/lua/luajit/src/src/lj_debug.h new file mode 100644 index 0000000000..7cf57de778 --- /dev/null +++ b/external/lua/luajit/src/src/lj_debug.h @@ -0,0 +1,61 @@ +/* +** Debugging and introspection. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_DEBUG_H +#define _LJ_DEBUG_H + +#include "lj_obj.h" + +typedef struct lj_Debug { + /* Common fields. Must be in the same order as in lua.h. */ + int event; + const char *name; + const char *namewhat; + const char *what; + const char *source; + int currentline; + int nups; + int linedefined; + int lastlinedefined; + char short_src[LUA_IDSIZE]; + int i_ci; + /* Extended fields. Only valid if lj_debug_getinfo() is called with ext = 1.*/ + int nparams; + int isvararg; +} lj_Debug; + +LJ_FUNC cTValue *lj_debug_frame(lua_State *L, int level, int *size); +LJ_FUNC BCLine LJ_FASTCALL lj_debug_line(GCproto *pt, BCPos pc); +LJ_FUNC const char *lj_debug_uvname(GCproto *pt, uint32_t idx); +LJ_FUNC const char *lj_debug_uvnamev(cTValue *o, uint32_t idx, TValue **tvp); +LJ_FUNC const char *lj_debug_slotname(GCproto *pt, const BCIns *pc, + BCReg slot, const char **name); +LJ_FUNC const char *lj_debug_funcname(lua_State *L, TValue *frame, + const char **name); +LJ_FUNC void lj_debug_shortname(char *out, GCstr *str); +LJ_FUNC void lj_debug_addloc(lua_State *L, const char *msg, + cTValue *frame, cTValue *nextframe); +LJ_FUNC void lj_debug_pushloc(lua_State *L, GCproto *pt, BCPos pc); +LJ_FUNC int lj_debug_getinfo(lua_State *L, const char *what, lj_Debug *ar, + int ext); + +/* Fixed internal variable names. */ +#define VARNAMEDEF(_) \ + _(FOR_IDX, "(for index)") \ + _(FOR_STOP, "(for limit)") \ + _(FOR_STEP, "(for step)") \ + _(FOR_GEN, "(for generator)") \ + _(FOR_STATE, "(for state)") \ + _(FOR_CTL, "(for control)") + +enum { + VARNAME_END, +#define VARNAMEENUM(name, str) VARNAME_##name, + VARNAMEDEF(VARNAMEENUM) +#undef VARNAMEENUM + VARNAME__MAX +}; + +#endif diff --git a/external/lua/luajit/src/src/lj_def.h b/external/lua/luajit/src/src/lj_def.h new file mode 100644 index 0000000000..83eb67bcd9 --- /dev/null +++ b/external/lua/luajit/src/src/lj_def.h @@ -0,0 +1,349 @@ +/* +** LuaJIT common internal definitions. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_DEF_H +#define _LJ_DEF_H + +#include "lua.h" + +#if defined(_MSC_VER) +/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */ +typedef __int8 int8_t; +typedef __int16 int16_t; +typedef __int32 int32_t; +typedef __int64 int64_t; +typedef unsigned __int8 uint8_t; +typedef unsigned __int16 uint16_t; +typedef unsigned __int32 uint32_t; +typedef unsigned __int64 uint64_t; +#ifdef _WIN64 +typedef __int64 intptr_t; +typedef unsigned __int64 uintptr_t; +#else +typedef __int32 intptr_t; +typedef unsigned __int32 uintptr_t; +#endif +#elif defined(__symbian__) +/* Cough. */ +typedef signed char int8_t; +typedef short int int16_t; +typedef int int32_t; +typedef long long int64_t; +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long uint64_t; +typedef int intptr_t; +typedef unsigned int uintptr_t; +#else +#include +#endif + +/* Needed everywhere. */ +#include +#include + +/* Various VM limits. */ +#define LJ_MAX_MEM 0x7fffff00 /* Max. total memory allocation. */ +#define LJ_MAX_ALLOC LJ_MAX_MEM /* Max. individual allocation length. */ +#define LJ_MAX_STR LJ_MAX_MEM /* Max. string length. */ +#define LJ_MAX_UDATA LJ_MAX_MEM /* Max. userdata length. */ + +#define LJ_MAX_STRTAB (1<<26) /* Max. string table size. */ +#define LJ_MAX_HBITS 26 /* Max. hash bits. */ +#define LJ_MAX_ABITS 28 /* Max. bits of array key. */ +#define LJ_MAX_ASIZE ((1<<(LJ_MAX_ABITS-1))+1) /* Max. array part size. */ +#define LJ_MAX_COLOSIZE 16 /* Max. elems for colocated array. */ + +#define LJ_MAX_LINE LJ_MAX_MEM /* Max. source code line number. */ +#define LJ_MAX_XLEVEL 200 /* Max. syntactic nesting level. */ +#define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */ +#define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */ +#define LJ_MAX_LOCVAR 200 /* Max. # of local variables. */ +#define LJ_MAX_UPVAL 60 /* Max. # of upvalues. */ + +#define LJ_MAX_IDXCHAIN 100 /* __index/__newindex chain limit. */ +#define LJ_STACK_EXTRA 5 /* Extra stack space (metamethods). */ + +#define LJ_NUM_CBPAGE 1 /* Number of FFI callback pages. */ + +/* Minimum table/buffer sizes. */ +#define LJ_MIN_GLOBAL 6 /* Min. global table size (hbits). */ +#define LJ_MIN_REGISTRY 2 /* Min. registry size (hbits). */ +#define LJ_MIN_STRTAB 256 /* Min. string table size (pow2). */ +#define LJ_MIN_SBUF 32 /* Min. string buffer length. */ +#define LJ_MIN_VECSZ 8 /* Min. size for growable vectors. */ +#define LJ_MIN_IRSZ 32 /* Min. size for growable IR. */ +#define LJ_MIN_K64SZ 16 /* Min. size for chained K64Array. */ + +/* JIT compiler limits. */ +#define LJ_MAX_JSLOTS 250 /* Max. # of stack slots for a trace. */ +#define LJ_MAX_PHI 64 /* Max. # of PHIs for a loop. */ +#define LJ_MAX_EXITSTUBGR 16 /* Max. # of exit stub groups. */ + +/* Various macros. */ +#ifndef UNUSED +#define UNUSED(x) ((void)(x)) /* to avoid warnings */ +#endif + +#define U64x(hi, lo) (((uint64_t)0x##hi << 32) + (uint64_t)0x##lo) +#define i32ptr(p) ((int32_t)(intptr_t)(void *)(p)) +#define u32ptr(p) ((uint32_t)(intptr_t)(void *)(p)) + +#define checki8(x) ((x) == (int32_t)(int8_t)(x)) +#define checku8(x) ((x) == (int32_t)(uint8_t)(x)) +#define checki16(x) ((x) == (int32_t)(int16_t)(x)) +#define checku16(x) ((x) == (int32_t)(uint16_t)(x)) +#define checki32(x) ((x) == (int32_t)(x)) +#define checku32(x) ((x) == (uint32_t)(x)) +#define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x)) + +/* Every half-decent C compiler transforms this into a rotate instruction. */ +#define lj_rol(x, n) (((x)<<(n)) | ((x)>>(8*sizeof(x)-(n)))) +#define lj_ror(x, n) (((x)<<(8*sizeof(x)-(n))) | ((x)>>(n))) + +/* A really naive Bloom filter. But sufficient for our needs. */ +typedef uintptr_t BloomFilter; +#define BLOOM_MASK (8*sizeof(BloomFilter) - 1) +#define bloombit(x) ((uintptr_t)1 << ((x) & BLOOM_MASK)) +#define bloomset(b, x) ((b) |= bloombit((x))) +#define bloomtest(b, x) ((b) & bloombit((x))) + +#if defined(__GNUC__) + +#define LJ_NORET __attribute__((noreturn)) +#define LJ_ALIGN(n) __attribute__((aligned(n))) +#define LJ_INLINE inline +#define LJ_AINLINE inline __attribute__((always_inline)) +#define LJ_NOINLINE __attribute__((noinline)) + +#if defined(__ELF__) || defined(__MACH__) +#if !((defined(__sun__) && defined(__svr4__)) || defined(__CELLOS_LV2__)) +#define LJ_NOAPI extern __attribute__((visibility("hidden"))) +#endif +#endif + +/* Note: it's only beneficial to use fastcall on x86 and then only for up to +** two non-FP args. The amalgamated compile covers all LJ_FUNC cases. Only +** indirect calls and related tail-called C functions are marked as fastcall. +*/ +#if defined(__i386__) +#define LJ_FASTCALL __attribute__((fastcall)) +#endif + +#define LJ_LIKELY(x) __builtin_expect(!!(x), 1) +#define LJ_UNLIKELY(x) __builtin_expect(!!(x), 0) + +#define lj_ffs(x) ((uint32_t)__builtin_ctz(x)) +/* Don't ask ... */ +#if defined(__INTEL_COMPILER) && (defined(__i386__) || defined(__x86_64__)) +static LJ_AINLINE uint32_t lj_fls(uint32_t x) +{ + uint32_t r; __asm__("bsrl %1, %0" : "=r" (r) : "rm" (x) : "cc"); return r; +} +#else +#define lj_fls(x) ((uint32_t)(__builtin_clz(x)^31)) +#endif + +#if defined(__arm__) +static LJ_AINLINE uint32_t lj_bswap(uint32_t x) +{ + uint32_t r; +#if __ARM_ARCH_6__ || __ARM_ARCH_6J__ || __ARM_ARCH_6T2__ || __ARM_ARCH_6Z__ ||\ + __ARM_ARCH_6ZK__ || __ARM_ARCH_7__ || __ARM_ARCH_7A__ || __ARM_ARCH_7R__ + __asm__("rev %0, %1" : "=r" (r) : "r" (x)); + return r; +#else +#ifdef __thumb__ + r = x ^ lj_ror(x, 16); +#else + __asm__("eor %0, %1, %1, ror #16" : "=r" (r) : "r" (x)); +#endif + return ((r & 0xff00ffffu) >> 8) ^ lj_ror(x, 8); +#endif +} + +static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) +{ + return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32)); +} +#elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) +static LJ_AINLINE uint32_t lj_bswap(uint32_t x) +{ + return (uint32_t)__builtin_bswap32((int32_t)x); +} + +static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) +{ + return (uint64_t)__builtin_bswap64((int64_t)x); +} +#elif defined(__i386__) || defined(__x86_64__) +static LJ_AINLINE uint32_t lj_bswap(uint32_t x) +{ + uint32_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r; +} + +#if defined(__i386__) +static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) +{ + return ((uint64_t)lj_bswap((uint32_t)x)<<32) | lj_bswap((uint32_t)(x>>32)); +} +#else +static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) +{ + uint64_t r; __asm__("bswap %0" : "=r" (r) : "0" (x)); return r; +} +#endif +#else +static LJ_AINLINE uint32_t lj_bswap(uint32_t x) +{ + return (x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24); +} + +static LJ_AINLINE uint64_t lj_bswap64(uint64_t x) +{ + return (uint64_t)lj_bswap((uint32_t)(x >> 32)) | + ((uint64_t)lj_bswap((uint32_t)x) << 32); +} +#endif + +typedef union __attribute__((packed)) Unaligned16 { + uint16_t u; + uint8_t b[2]; +} Unaligned16; + +typedef union __attribute__((packed)) Unaligned32 { + uint32_t u; + uint8_t b[4]; +} Unaligned32; + +/* Unaligned load of uint16_t. */ +static LJ_AINLINE uint16_t lj_getu16(const void *p) +{ + return ((const Unaligned16 *)p)->u; +} + +/* Unaligned load of uint32_t. */ +static LJ_AINLINE uint32_t lj_getu32(const void *p) +{ + return ((const Unaligned32 *)p)->u; +} + +#elif defined(_MSC_VER) + +#define LJ_NORET __declspec(noreturn) +#define LJ_ALIGN(n) __declspec(align(n)) +#define LJ_INLINE __inline +#define LJ_AINLINE __forceinline +#define LJ_NOINLINE __declspec(noinline) +#if defined(_M_IX86) +#define LJ_FASTCALL __fastcall +#endif + +#ifdef _M_PPC +unsigned int _CountLeadingZeros(long); +#pragma intrinsic(_CountLeadingZeros) +static LJ_AINLINE uint32_t lj_fls(uint32_t x) +{ + return _CountLeadingZeros(x) ^ 31; +} +#else +unsigned char _BitScanForward(uint32_t *, unsigned long); +unsigned char _BitScanReverse(uint32_t *, unsigned long); +#pragma intrinsic(_BitScanForward) +#pragma intrinsic(_BitScanReverse) + +static LJ_AINLINE uint32_t lj_ffs(uint32_t x) +{ + uint32_t r; _BitScanForward(&r, x); return r; +} + +static LJ_AINLINE uint32_t lj_fls(uint32_t x) +{ + uint32_t r; _BitScanReverse(&r, x); return r; +} +#endif + +unsigned long _byteswap_ulong(unsigned long); +uint64_t _byteswap_uint64(uint64_t); +#define lj_bswap(x) (_byteswap_ulong((x))) +#define lj_bswap64(x) (_byteswap_uint64((x))) + +#if defined(_M_PPC) && defined(LUAJIT_NO_UNALIGNED) +/* +** Replacement for unaligned loads on Xbox 360. Disabled by default since it's +** usually more costly than the occasional stall when crossing a cache-line. +*/ +static LJ_AINLINE uint16_t lj_getu16(const void *v) +{ + const uint8_t *p = (const uint8_t *)v; + return (uint16_t)((p[0]<<8) | p[1]); +} +static LJ_AINLINE uint32_t lj_getu32(const void *v) +{ + const uint8_t *p = (const uint8_t *)v; + return (uint32_t)((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]); +} +#else +/* Unaligned loads are generally ok on x86/x64. */ +#define lj_getu16(p) (*(uint16_t *)(p)) +#define lj_getu32(p) (*(uint32_t *)(p)) +#endif + +#else +#error "missing defines for your compiler" +#endif + +/* Optional defines. */ +#ifndef LJ_FASTCALL +#define LJ_FASTCALL +#endif +#ifndef LJ_NORET +#define LJ_NORET +#endif +#ifndef LJ_NOAPI +#define LJ_NOAPI extern +#endif +#ifndef LJ_LIKELY +#define LJ_LIKELY(x) (x) +#define LJ_UNLIKELY(x) (x) +#endif + +/* Attributes for internal functions. */ +#define LJ_DATA LJ_NOAPI +#define LJ_DATADEF +#define LJ_ASMF LJ_NOAPI +#define LJ_FUNCA LJ_NOAPI +#if defined(ljamalg_c) +#define LJ_FUNC static +#else +#define LJ_FUNC LJ_NOAPI +#endif +#define LJ_FUNC_NORET LJ_FUNC LJ_NORET +#define LJ_FUNCA_NORET LJ_FUNCA LJ_NORET +#define LJ_ASMF_NORET LJ_ASMF LJ_NORET + +/* Runtime assertions. */ +#ifdef lua_assert +#define check_exp(c, e) (lua_assert(c), (e)) +#define api_check(l, e) lua_assert(e) +#else +#define lua_assert(c) ((void)0) +#define check_exp(c, e) (e) +#define api_check luai_apicheck +#endif + +/* Static assertions. */ +#define LJ_ASSERT_NAME2(name, line) name ## line +#define LJ_ASSERT_NAME(line) LJ_ASSERT_NAME2(lj_assert_, line) +#ifdef __COUNTER__ +#define LJ_STATIC_ASSERT(cond) \ + extern void LJ_ASSERT_NAME(__COUNTER__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) +#else +#define LJ_STATIC_ASSERT(cond) \ + extern void LJ_ASSERT_NAME(__LINE__)(int STATIC_ASSERTION_FAILED[(cond)?1:-1]) +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_dispatch.c b/external/lua/luajit/src/src/lj_dispatch.c new file mode 100644 index 0000000000..d57f1a6f92 --- /dev/null +++ b/external/lua/luajit/src/src/lj_dispatch.c @@ -0,0 +1,494 @@ +/* +** Instruction dispatch handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_dispatch_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_err.h" +#include "lj_func.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_meta.h" +#include "lj_debug.h" +#include "lj_state.h" +#include "lj_frame.h" +#include "lj_bc.h" +#include "lj_ff.h" +#if LJ_HASJIT +#include "lj_jit.h" +#endif +#if LJ_HASFFI +#include "lj_ccallback.h" +#endif +#include "lj_trace.h" +#include "lj_dispatch.h" +#include "lj_vm.h" +#include "luajit.h" + +/* Bump GG_NUM_ASMFF in lj_dispatch.h as needed. Ugly. */ +LJ_STATIC_ASSERT(GG_NUM_ASMFF == FF_NUM_ASMFUNC); + +/* -- Dispatch table management ------------------------------------------- */ + +#if LJ_TARGET_MIPS +#include +LJ_FUNCA_NORET void LJ_FASTCALL lj_ffh_coroutine_wrap_err(lua_State *L, + lua_State *co); + +#define GOTFUNC(name) (ASMFunction)name, +static const ASMFunction dispatch_got[] = { + GOTDEF(GOTFUNC) +}; +#undef GOTFUNC +#endif + +/* Initialize instruction dispatch table and hot counters. */ +void lj_dispatch_init(GG_State *GG) +{ + uint32_t i; + ASMFunction *disp = GG->dispatch; + for (i = 0; i < GG_LEN_SDISP; i++) + disp[GG_LEN_DDISP+i] = disp[i] = makeasmfunc(lj_bc_ofs[i]); + for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++) + disp[i] = makeasmfunc(lj_bc_ofs[i]); + /* The JIT engine is off by default. luaopen_jit() turns it on. */ + disp[BC_FORL] = disp[BC_IFORL]; + disp[BC_ITERL] = disp[BC_IITERL]; + disp[BC_LOOP] = disp[BC_ILOOP]; + disp[BC_FUNCF] = disp[BC_IFUNCF]; + disp[BC_FUNCV] = disp[BC_IFUNCV]; + GG->g.bc_cfunc_ext = GG->g.bc_cfunc_int = BCINS_AD(BC_FUNCC, LUA_MINSTACK, 0); + for (i = 0; i < GG_NUM_ASMFF; i++) + GG->bcff[i] = BCINS_AD(BC__MAX+i, 0, 0); +#if LJ_TARGET_MIPS + memcpy(GG->got, dispatch_got, LJ_GOT__MAX*4); +#endif +} + +#if LJ_HASJIT +/* Initialize hotcount table. */ +void lj_dispatch_init_hotcount(global_State *g) +{ + int32_t hotloop = G2J(g)->param[JIT_P_hotloop]; + HotCount start = (HotCount)(hotloop*HOTCOUNT_LOOP - 1); + HotCount *hotcount = G2GG(g)->hotcount; + uint32_t i; + for (i = 0; i < HOTCOUNT_SIZE; i++) + hotcount[i] = start; +} +#endif + +/* Internal dispatch mode bits. */ +#define DISPMODE_JIT 0x01 /* JIT compiler on. */ +#define DISPMODE_REC 0x02 /* Recording active. */ +#define DISPMODE_INS 0x04 /* Override instruction dispatch. */ +#define DISPMODE_CALL 0x08 /* Override call dispatch. */ +#define DISPMODE_RET 0x10 /* Override return dispatch. */ + +/* Update dispatch table depending on various flags. */ +void lj_dispatch_update(global_State *g) +{ + uint8_t oldmode = g->dispatchmode; + uint8_t mode = 0; +#if LJ_HASJIT + mode |= (G2J(g)->flags & JIT_F_ON) ? DISPMODE_JIT : 0; + mode |= G2J(g)->state != LJ_TRACE_IDLE ? + (DISPMODE_REC|DISPMODE_INS|DISPMODE_CALL) : 0; +#endif + mode |= (g->hookmask & (LUA_MASKLINE|LUA_MASKCOUNT)) ? DISPMODE_INS : 0; + mode |= (g->hookmask & LUA_MASKCALL) ? DISPMODE_CALL : 0; + mode |= (g->hookmask & LUA_MASKRET) ? DISPMODE_RET : 0; + if (oldmode != mode) { /* Mode changed? */ + ASMFunction *disp = G2GG(g)->dispatch; + ASMFunction f_forl, f_iterl, f_loop, f_funcf, f_funcv; + g->dispatchmode = mode; + + /* Hotcount if JIT is on, but not while recording. */ + if ((mode & (DISPMODE_JIT|DISPMODE_REC)) == DISPMODE_JIT) { + f_forl = makeasmfunc(lj_bc_ofs[BC_FORL]); + f_iterl = makeasmfunc(lj_bc_ofs[BC_ITERL]); + f_loop = makeasmfunc(lj_bc_ofs[BC_LOOP]); + f_funcf = makeasmfunc(lj_bc_ofs[BC_FUNCF]); + f_funcv = makeasmfunc(lj_bc_ofs[BC_FUNCV]); + } else { /* Otherwise use the non-hotcounting instructions. */ + f_forl = disp[GG_LEN_DDISP+BC_IFORL]; + f_iterl = disp[GG_LEN_DDISP+BC_IITERL]; + f_loop = disp[GG_LEN_DDISP+BC_ILOOP]; + f_funcf = makeasmfunc(lj_bc_ofs[BC_IFUNCF]); + f_funcv = makeasmfunc(lj_bc_ofs[BC_IFUNCV]); + } + /* Init static counting instruction dispatch first (may be copied below). */ + disp[GG_LEN_DDISP+BC_FORL] = f_forl; + disp[GG_LEN_DDISP+BC_ITERL] = f_iterl; + disp[GG_LEN_DDISP+BC_LOOP] = f_loop; + + /* Set dynamic instruction dispatch. */ + if ((oldmode ^ mode) & (DISPMODE_REC|DISPMODE_INS)) { + /* Need to update the whole table. */ + if (!(mode & (DISPMODE_REC|DISPMODE_INS))) { /* No ins dispatch? */ + /* Copy static dispatch table to dynamic dispatch table. */ + memcpy(&disp[0], &disp[GG_LEN_DDISP], GG_LEN_SDISP*sizeof(ASMFunction)); + /* Overwrite with dynamic return dispatch. */ + if ((mode & DISPMODE_RET)) { + disp[BC_RETM] = lj_vm_rethook; + disp[BC_RET] = lj_vm_rethook; + disp[BC_RET0] = lj_vm_rethook; + disp[BC_RET1] = lj_vm_rethook; + } + } else { + /* The recording dispatch also checks for hooks. */ + ASMFunction f = (mode & DISPMODE_REC) ? lj_vm_record : lj_vm_inshook; + uint32_t i; + for (i = 0; i < GG_LEN_SDISP; i++) + disp[i] = f; + } + } else if (!(mode & (DISPMODE_REC|DISPMODE_INS))) { + /* Otherwise set dynamic counting ins. */ + disp[BC_FORL] = f_forl; + disp[BC_ITERL] = f_iterl; + disp[BC_LOOP] = f_loop; + /* Set dynamic return dispatch. */ + if ((mode & DISPMODE_RET)) { + disp[BC_RETM] = lj_vm_rethook; + disp[BC_RET] = lj_vm_rethook; + disp[BC_RET0] = lj_vm_rethook; + disp[BC_RET1] = lj_vm_rethook; + } else { + disp[BC_RETM] = disp[GG_LEN_DDISP+BC_RETM]; + disp[BC_RET] = disp[GG_LEN_DDISP+BC_RET]; + disp[BC_RET0] = disp[GG_LEN_DDISP+BC_RET0]; + disp[BC_RET1] = disp[GG_LEN_DDISP+BC_RET1]; + } + } + + /* Set dynamic call dispatch. */ + if ((oldmode ^ mode) & DISPMODE_CALL) { /* Update the whole table? */ + uint32_t i; + if ((mode & DISPMODE_CALL) == 0) { /* No call hooks? */ + for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++) + disp[i] = makeasmfunc(lj_bc_ofs[i]); + } else { + for (i = GG_LEN_SDISP; i < GG_LEN_DDISP; i++) + disp[i] = lj_vm_callhook; + } + } + if (!(mode & DISPMODE_CALL)) { /* Overwrite dynamic counting ins. */ + disp[BC_FUNCF] = f_funcf; + disp[BC_FUNCV] = f_funcv; + } + +#if LJ_HASJIT + /* Reset hotcounts for JIT off to on transition. */ + if ((mode & DISPMODE_JIT) && !(oldmode & DISPMODE_JIT)) + lj_dispatch_init_hotcount(g); +#endif + } +} + +/* -- JIT mode setting ---------------------------------------------------- */ + +#if LJ_HASJIT +/* Set JIT mode for a single prototype. */ +static void setptmode(global_State *g, GCproto *pt, int mode) +{ + if ((mode & LUAJIT_MODE_ON)) { /* (Re-)enable JIT compilation. */ + pt->flags &= ~PROTO_NOJIT; + lj_trace_reenableproto(pt); /* Unpatch all ILOOP etc. bytecodes. */ + } else { /* Flush and/or disable JIT compilation. */ + if (!(mode & LUAJIT_MODE_FLUSH)) + pt->flags |= PROTO_NOJIT; + lj_trace_flushproto(g, pt); /* Flush all traces of prototype. */ + } +} + +/* Recursively set the JIT mode for all children of a prototype. */ +static void setptmode_all(global_State *g, GCproto *pt, int mode) +{ + ptrdiff_t i; + if (!(pt->flags & PROTO_CHILD)) return; + for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) { + GCobj *o = proto_kgc(pt, i); + if (o->gch.gct == ~LJ_TPROTO) { + setptmode(g, gco2pt(o), mode); + setptmode_all(g, gco2pt(o), mode); + } + } +} +#endif + +/* Public API function: control the JIT engine. */ +int luaJIT_setmode(lua_State *L, int idx, int mode) +{ + global_State *g = G(L); + int mm = mode & LUAJIT_MODE_MASK; + lj_trace_abort(g); /* Abort recording on any state change. */ + /* Avoid pulling the rug from under our own feet. */ + if ((g->hookmask & HOOK_GC)) + lj_err_caller(L, LJ_ERR_NOGCMM); + switch (mm) { +#if LJ_HASJIT + case LUAJIT_MODE_ENGINE: + if ((mode & LUAJIT_MODE_FLUSH)) { + lj_trace_flushall(L); + } else { + if (!(mode & LUAJIT_MODE_ON)) + G2J(g)->flags &= ~(uint32_t)JIT_F_ON; +#if LJ_TARGET_X86ORX64 + else if ((G2J(g)->flags & JIT_F_SSE2)) + G2J(g)->flags |= (uint32_t)JIT_F_ON; + else + return 0; /* Don't turn on JIT compiler without SSE2 support. */ +#else + else + G2J(g)->flags |= (uint32_t)JIT_F_ON; +#endif + lj_dispatch_update(g); + } + break; + case LUAJIT_MODE_FUNC: + case LUAJIT_MODE_ALLFUNC: + case LUAJIT_MODE_ALLSUBFUNC: { + cTValue *tv = idx == 0 ? frame_prev(L->base-1) : + idx > 0 ? L->base + (idx-1) : L->top + idx; + GCproto *pt; + if ((idx == 0 || tvisfunc(tv)) && isluafunc(&gcval(tv)->fn)) + pt = funcproto(&gcval(tv)->fn); /* Cannot use funcV() for frame slot. */ + else if (tvisproto(tv)) + pt = protoV(tv); + else + return 0; /* Failed. */ + if (mm != LUAJIT_MODE_ALLSUBFUNC) + setptmode(g, pt, mode); + if (mm != LUAJIT_MODE_FUNC) + setptmode_all(g, pt, mode); + break; + } + case LUAJIT_MODE_TRACE: + if (!(mode & LUAJIT_MODE_FLUSH)) + return 0; /* Failed. */ + lj_trace_flush(G2J(g), idx); + break; +#else + case LUAJIT_MODE_ENGINE: + case LUAJIT_MODE_FUNC: + case LUAJIT_MODE_ALLFUNC: + case LUAJIT_MODE_ALLSUBFUNC: + UNUSED(idx); + if ((mode & LUAJIT_MODE_ON)) + return 0; /* Failed. */ + break; +#endif + case LUAJIT_MODE_WRAPCFUNC: + if ((mode & LUAJIT_MODE_ON)) { + if (idx != 0) { + cTValue *tv = idx > 0 ? L->base + (idx-1) : L->top + idx; + if (tvislightud(tv)) + g->wrapf = (lua_CFunction)lightudV(tv); + else + return 0; /* Failed. */ + } else { + return 0; /* Failed. */ + } + g->bc_cfunc_ext = BCINS_AD(BC_FUNCCW, 0, 0); + } else { + g->bc_cfunc_ext = BCINS_AD(BC_FUNCC, 0, 0); + } + break; + default: + return 0; /* Failed. */ + } + return 1; /* OK. */ +} + +/* Enforce (dynamic) linker error for version mismatches. See luajit.c. */ +LUA_API void LUAJIT_VERSION_SYM(void) +{ +} + +/* -- Hooks --------------------------------------------------------------- */ + +/* This function can be called asynchronously (e.g. during a signal). */ +LUA_API int lua_sethook(lua_State *L, lua_Hook func, int mask, int count) +{ + global_State *g = G(L); + mask &= HOOK_EVENTMASK; + if (func == NULL || mask == 0) { mask = 0; func = NULL; } /* Consistency. */ + g->hookf = func; + g->hookcount = g->hookcstart = (int32_t)count; + g->hookmask = (uint8_t)((g->hookmask & ~HOOK_EVENTMASK) | mask); + lj_trace_abort(g); /* Abort recording on any hook change. */ + lj_dispatch_update(g); + return 1; +} + +LUA_API lua_Hook lua_gethook(lua_State *L) +{ + return G(L)->hookf; +} + +LUA_API int lua_gethookmask(lua_State *L) +{ + return G(L)->hookmask & HOOK_EVENTMASK; +} + +LUA_API int lua_gethookcount(lua_State *L) +{ + return (int)G(L)->hookcstart; +} + +/* Call a hook. */ +static void callhook(lua_State *L, int event, BCLine line) +{ + global_State *g = G(L); + lua_Hook hookf = g->hookf; + if (hookf && !hook_active(g)) { + lua_Debug ar; + lj_trace_abort(g); /* Abort recording on any hook call. */ + ar.event = event; + ar.currentline = line; + /* Top frame, nextframe = NULL. */ + ar.i_ci = (int)((L->base-1) - tvref(L->stack)); + lj_state_checkstack(L, 1+LUA_MINSTACK); + hook_enter(g); + hookf(L, &ar); + lua_assert(hook_active(g)); + hook_leave(g); + } +} + +/* -- Dispatch callbacks -------------------------------------------------- */ + +/* Calculate number of used stack slots in the current frame. */ +static BCReg cur_topslot(GCproto *pt, const BCIns *pc, uint32_t nres) +{ + BCIns ins = pc[-1]; + if (bc_op(ins) == BC_UCLO) + ins = pc[bc_j(ins)]; + switch (bc_op(ins)) { + case BC_CALLM: case BC_CALLMT: return bc_a(ins) + bc_c(ins) + nres-1+1; + case BC_RETM: return bc_a(ins) + bc_d(ins) + nres-1; + case BC_TSETM: return bc_a(ins) + nres-1; + default: return pt->framesize; + } +} + +/* Instruction dispatch. Used by instr/line/return hooks or when recording. */ +void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc) +{ + ERRNO_SAVE + GCfunc *fn = curr_func(L); + GCproto *pt = funcproto(fn); + void *cf = cframe_raw(L->cframe); + const BCIns *oldpc = cframe_pc(cf); + global_State *g = G(L); + BCReg slots; + setcframe_pc(cf, pc); + slots = cur_topslot(pt, pc, cframe_multres_n(cf)); + L->top = L->base + slots; /* Fix top. */ +#if LJ_HASJIT + { + jit_State *J = G2J(g); + if (J->state != LJ_TRACE_IDLE) { +#ifdef LUA_USE_ASSERT + ptrdiff_t delta = L->top - L->base; +#endif + J->L = L; + lj_trace_ins(J, pc-1); /* The interpreter bytecode PC is offset by 1. */ + lua_assert(L->top - L->base == delta); + } + } +#endif + if ((g->hookmask & LUA_MASKCOUNT) && g->hookcount == 0) { + g->hookcount = g->hookcstart; + callhook(L, LUA_HOOKCOUNT, -1); + L->top = L->base + slots; /* Fix top again. */ + } + if ((g->hookmask & LUA_MASKLINE)) { + BCPos npc = proto_bcpos(pt, pc) - 1; + BCPos opc = proto_bcpos(pt, oldpc) - 1; + BCLine line = lj_debug_line(pt, npc); + if (pc <= oldpc || opc >= pt->sizebc || line != lj_debug_line(pt, opc)) { + callhook(L, LUA_HOOKLINE, line); + L->top = L->base + slots; /* Fix top again. */ + } + } + if ((g->hookmask & LUA_MASKRET) && bc_isret(bc_op(pc[-1]))) + callhook(L, LUA_HOOKRET, -1); + ERRNO_RESTORE +} + +/* Initialize call. Ensure stack space and return # of missing parameters. */ +static int call_init(lua_State *L, GCfunc *fn) +{ + if (isluafunc(fn)) { + GCproto *pt = funcproto(fn); + int numparams = pt->numparams; + int gotparams = (int)(L->top - L->base); + int need = pt->framesize; + if ((pt->flags & PROTO_VARARG)) need += 1+gotparams; + lj_state_checkstack(L, (MSize)need); + numparams -= gotparams; + return numparams >= 0 ? numparams : 0; + } else { + lj_state_checkstack(L, LUA_MINSTACK); + return 0; + } +} + +/* Call dispatch. Used by call hooks, hot calls or when recording. */ +ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns *pc) +{ + ERRNO_SAVE + GCfunc *fn = curr_func(L); + BCOp op; + global_State *g = G(L); +#if LJ_HASJIT + jit_State *J = G2J(g); +#endif + int missing = call_init(L, fn); +#if LJ_HASJIT + J->L = L; + if ((uintptr_t)pc & 1) { /* Marker for hot call. */ +#ifdef LUA_USE_ASSERT + ptrdiff_t delta = L->top - L->base; +#endif + pc = (const BCIns *)((uintptr_t)pc & ~(uintptr_t)1); + lj_trace_hot(J, pc); + lua_assert(L->top - L->base == delta); + goto out; + } else if (J->state != LJ_TRACE_IDLE && + !(g->hookmask & (HOOK_GC|HOOK_VMEVENT))) { +#ifdef LUA_USE_ASSERT + ptrdiff_t delta = L->top - L->base; +#endif + /* Record the FUNC* bytecodes, too. */ + lj_trace_ins(J, pc-1); /* The interpreter bytecode PC is offset by 1. */ + lua_assert(L->top - L->base == delta); + } +#endif + if ((g->hookmask & LUA_MASKCALL)) { + int i; + for (i = 0; i < missing; i++) /* Add missing parameters. */ + setnilV(L->top++); + callhook(L, LUA_HOOKCALL, -1); + /* Preserve modifications of missing parameters by lua_setlocal(). */ + while (missing-- > 0 && tvisnil(L->top - 1)) + L->top--; + } +#if LJ_HASJIT +out: +#endif + op = bc_op(pc[-1]); /* Get FUNC* op. */ +#if LJ_HASJIT + /* Use the non-hotcounting variants if JIT is off or while recording. */ + if ((!(J->flags & JIT_F_ON) || J->state != LJ_TRACE_IDLE) && + (op == BC_FUNCF || op == BC_FUNCV)) + op = (BCOp)((int)op+(int)BC_IFUNCF-(int)BC_FUNCF); +#endif + ERRNO_RESTORE + return makeasmfunc(lj_bc_ofs[op]); /* Return static dispatch target. */ +} + diff --git a/external/lua/luajit/src/src/lj_dispatch.h b/external/lua/luajit/src/src/lj_dispatch.h new file mode 100644 index 0000000000..a56b626021 --- /dev/null +++ b/external/lua/luajit/src/src/lj_dispatch.h @@ -0,0 +1,131 @@ +/* +** Instruction dispatch handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_DISPATCH_H +#define _LJ_DISPATCH_H + +#include "lj_obj.h" +#include "lj_bc.h" +#if LJ_HASJIT +#include "lj_jit.h" +#endif + +#if LJ_TARGET_MIPS +/* Need our own global offset table for the dreaded MIPS calling conventions. */ +#if LJ_HASJIT +#define JITGOTDEF(_) _(lj_trace_exit) _(lj_trace_hot) +#else +#define JITGOTDEF(_) +#endif +#if LJ_HASFFI +#define FFIGOTDEF(_) \ + _(lj_meta_equal_cd) _(lj_ccallback_enter) _(lj_ccallback_leave) +#else +#define FFIGOTDEF(_) +#endif +#define GOTDEF(_) \ + _(floor) _(ceil) _(trunc) _(log) _(log10) _(exp) _(sin) _(cos) _(tan) \ + _(asin) _(acos) _(atan) _(sinh) _(cosh) _(tanh) _(frexp) _(modf) _(atan2) \ + _(pow) _(fmod) _(ldexp) \ + _(lj_dispatch_call) _(lj_dispatch_ins) _(lj_err_throw) \ + _(lj_ffh_coroutine_wrap_err) _(lj_func_closeuv) _(lj_func_newL_gc) \ + _(lj_gc_barrieruv) _(lj_gc_step) _(lj_gc_step_fixtop) _(lj_meta_arith) \ + _(lj_meta_call) _(lj_meta_cat) _(lj_meta_comp) _(lj_meta_equal) \ + _(lj_meta_for) _(lj_meta_len) _(lj_meta_tget) _(lj_meta_tset) \ + _(lj_state_growstack) _(lj_str_fromnum) _(lj_str_fromnumber) _(lj_str_new) \ + _(lj_tab_dup) _(lj_tab_get) _(lj_tab_getinth) _(lj_tab_len) _(lj_tab_new) \ + _(lj_tab_newkey) _(lj_tab_next) _(lj_tab_reasize) \ + JITGOTDEF(_) FFIGOTDEF(_) + +enum { +#define GOTENUM(name) LJ_GOT_##name, +GOTDEF(GOTENUM) +#undef GOTENUM + LJ_GOT__MAX +}; +#endif + +/* Type of hot counter. Must match the code in the assembler VM. */ +/* 16 bits are sufficient. Only 0.0015% overhead with maximum slot penalty. */ +typedef uint16_t HotCount; + +/* Number of hot counter hash table entries (must be a power of two). */ +#define HOTCOUNT_SIZE 64 +#define HOTCOUNT_PCMASK ((HOTCOUNT_SIZE-1)*sizeof(HotCount)) + +/* Hotcount decrements. */ +#define HOTCOUNT_LOOP 2 +#define HOTCOUNT_CALL 1 + +/* This solves a circular dependency problem -- bump as needed. Sigh. */ +#define GG_NUM_ASMFF 62 + +#define GG_LEN_DDISP (BC__MAX + GG_NUM_ASMFF) +#define GG_LEN_SDISP BC_FUNCF +#define GG_LEN_DISP (GG_LEN_DDISP + GG_LEN_SDISP) + +/* Global state, main thread and extra fields are allocated together. */ +typedef struct GG_State { + lua_State L; /* Main thread. */ + global_State g; /* Global state. */ +#if LJ_TARGET_MIPS + ASMFunction got[LJ_GOT__MAX]; /* Global offset table. */ +#endif +#if LJ_HASJIT + jit_State J; /* JIT state. */ + HotCount hotcount[HOTCOUNT_SIZE]; /* Hot counters. */ +#endif + ASMFunction dispatch[GG_LEN_DISP]; /* Instruction dispatch tables. */ + BCIns bcff[GG_NUM_ASMFF]; /* Bytecode for ASM fast functions. */ +} GG_State; + +#define GG_OFS(field) ((int)offsetof(GG_State, field)) +#define G2GG(gl) ((GG_State *)((char *)(gl) - GG_OFS(g))) +#define J2GG(j) ((GG_State *)((char *)(j) - GG_OFS(J))) +#define L2GG(L) (G2GG(G(L))) +#define J2G(J) (&J2GG(J)->g) +#define G2J(gl) (&G2GG(gl)->J) +#define L2J(L) (&L2GG(L)->J) +#define GG_G2DISP (GG_OFS(dispatch) - GG_OFS(g)) +#define GG_DISP2G (GG_OFS(g) - GG_OFS(dispatch)) +#define GG_DISP2J (GG_OFS(J) - GG_OFS(dispatch)) +#define GG_DISP2HOT (GG_OFS(hotcount) - GG_OFS(dispatch)) +#define GG_DISP2STATIC (GG_LEN_DDISP*(int)sizeof(ASMFunction)) + +#define hotcount_get(gg, pc) \ + (gg)->hotcount[(u32ptr(pc)>>2) & (HOTCOUNT_SIZE-1)] +#define hotcount_set(gg, pc, val) \ + (hotcount_get((gg), (pc)) = (HotCount)(val)) + +/* Dispatch table management. */ +LJ_FUNC void lj_dispatch_init(GG_State *GG); +#if LJ_HASJIT +LJ_FUNC void lj_dispatch_init_hotcount(global_State *g); +#endif +LJ_FUNC void lj_dispatch_update(global_State *g); + +/* Instruction dispatch callback for hooks or when recording. */ +LJ_FUNCA void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc); +LJ_FUNCA ASMFunction LJ_FASTCALL lj_dispatch_call(lua_State *L, const BCIns*pc); +LJ_FUNCA void LJ_FASTCALL lj_dispatch_return(lua_State *L, const BCIns *pc); + +#if LJ_HASFFI && !defined(_BUILDVM_H) +/* Save/restore errno and GetLastError() around hooks, exits and recording. */ +#include +#if LJ_TARGET_WINDOWS +#define WIN32_LEAN_AND_MEAN +#include +#define ERRNO_SAVE int olderr = errno; DWORD oldwerr = GetLastError(); +#define ERRNO_RESTORE errno = olderr; SetLastError(oldwerr); +#else +#define ERRNO_SAVE int olderr = errno; +#define ERRNO_RESTORE errno = olderr; +#endif +#else +#define ERRNO_SAVE +#define ERRNO_RESTORE +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_emit_arm.h b/external/lua/luajit/src/src/lj_emit_arm.h new file mode 100644 index 0000000000..b76a9a45f9 --- /dev/null +++ b/external/lua/luajit/src/src/lj_emit_arm.h @@ -0,0 +1,356 @@ +/* +** ARM instruction emitter. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* -- Constant encoding --------------------------------------------------- */ + +static uint8_t emit_invai[16] = { + /* AND */ (ARMI_AND^ARMI_BIC) >> 21, + /* EOR */ 0, + /* SUB */ (ARMI_SUB^ARMI_ADD) >> 21, + /* RSB */ 0, + /* ADD */ (ARMI_ADD^ARMI_SUB) >> 21, + /* ADC */ (ARMI_ADC^ARMI_SBC) >> 21, + /* SBC */ (ARMI_SBC^ARMI_ADC) >> 21, + /* RSC */ 0, + /* TST */ 0, + /* TEQ */ 0, + /* CMP */ (ARMI_CMP^ARMI_CMN) >> 21, + /* CMN */ (ARMI_CMN^ARMI_CMP) >> 21, + /* ORR */ 0, + /* MOV */ (ARMI_MOV^ARMI_MVN) >> 21, + /* BIC */ (ARMI_BIC^ARMI_AND) >> 21, + /* MVN */ (ARMI_MVN^ARMI_MOV) >> 21 +}; + +/* Encode constant in K12 format for data processing instructions. */ +static uint32_t emit_isk12(ARMIns ai, int32_t n) +{ + uint32_t invai, i, m = (uint32_t)n; + /* K12: unsigned 8 bit value, rotated in steps of two bits. */ + for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2)) + if (m <= 255) return ARMI_K12|m|i; + /* Otherwise try negation/complement with the inverse instruction. */ + invai = emit_invai[((ai >> 21) & 15)]; + if (!invai) return 0; /* Failed. No inverse instruction. */ + m = ~(uint32_t)n; + if (invai == ((ARMI_SUB^ARMI_ADD) >> 21) || + invai == (ARMI_CMP^ARMI_CMN) >> 21) m++; + for (i = 0; i < 4096; i += 256, m = lj_rol(m, 2)) + if (m <= 255) return ARMI_K12|(invai<<21)|m|i; + return 0; /* Failed. */ +} + +/* -- Emit basic instructions --------------------------------------------- */ + +static void emit_dnm(ASMState *as, ARMIns ai, Reg rd, Reg rn, Reg rm) +{ + *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn) | ARMF_M(rm); +} + +static void emit_dm(ASMState *as, ARMIns ai, Reg rd, Reg rm) +{ + *--as->mcp = ai | ARMF_D(rd) | ARMF_M(rm); +} + +static void emit_dn(ASMState *as, ARMIns ai, Reg rd, Reg rn) +{ + *--as->mcp = ai | ARMF_D(rd) | ARMF_N(rn); +} + +static void emit_nm(ASMState *as, ARMIns ai, Reg rn, Reg rm) +{ + *--as->mcp = ai | ARMF_N(rn) | ARMF_M(rm); +} + +static void emit_d(ASMState *as, ARMIns ai, Reg rd) +{ + *--as->mcp = ai | ARMF_D(rd); +} + +static void emit_n(ASMState *as, ARMIns ai, Reg rn) +{ + *--as->mcp = ai | ARMF_N(rn); +} + +static void emit_m(ASMState *as, ARMIns ai, Reg rm) +{ + *--as->mcp = ai | ARMF_M(rm); +} + +static void emit_lsox(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) +{ + lua_assert(ofs >= -255 && ofs <= 255); + if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; + *--as->mcp = ai | ARMI_LS_P | ARMI_LSX_I | ARMF_D(rd) | ARMF_N(rn) | + ((ofs & 0xf0) << 4) | (ofs & 0x0f); +} + +static void emit_lso(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) +{ + lua_assert(ofs >= -4095 && ofs <= 4095); + /* Combine LDR/STR pairs to LDRD/STRD. */ + if (*as->mcp == (ai|ARMI_LS_P|ARMI_LS_U|ARMF_D(rd^1)|ARMF_N(rn)|(ofs^4)) && + (ai & ~(ARMI_LDR^ARMI_STR)) == ARMI_STR && rd != rn && + (uint32_t)ofs <= 252 && !(ofs & 3) && !((rd ^ (ofs >>2)) & 1) && + as->mcp != as->mcloop) { + as->mcp++; + emit_lsox(as, ai == ARMI_LDR ? ARMI_LDRD : ARMI_STRD, rd&~1, rn, ofs&~4); + return; + } + if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; + *--as->mcp = ai | ARMI_LS_P | ARMF_D(rd) | ARMF_N(rn) | ofs; +} + +#if !LJ_SOFTFP +static void emit_vlso(ASMState *as, ARMIns ai, Reg rd, Reg rn, int32_t ofs) +{ + lua_assert(ofs >= -1020 && ofs <= 1020 && (ofs&3) == 0); + if (ofs < 0) ofs = -ofs; else ai |= ARMI_LS_U; + *--as->mcp = ai | ARMI_LS_P | ARMF_D(rd & 15) | ARMF_N(rn) | (ofs >> 2); +} +#endif + +/* -- Emit loads/stores --------------------------------------------------- */ + +/* Prefer spills of BASE/L. */ +#define emit_canremat(ref) ((ref) < ASMREF_L) + +/* Try to find a one step delta relative to another constant. */ +static int emit_kdelta1(ASMState *as, Reg d, int32_t i) +{ + RegSet work = ~as->freeset & RSET_GPR; + while (work) { + Reg r = rset_picktop(work); + IRRef ref = regcost_ref(as->cost[r]); + lua_assert(r != d); + if (emit_canremat(ref)) { + int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); + uint32_t k = emit_isk12(ARMI_ADD, delta); + if (k) { + if (k == ARMI_K12) + emit_dm(as, ARMI_MOV, d, r); + else + emit_dn(as, ARMI_ADD^k, d, r); + return 1; + } + } + rset_clear(work, r); + } + return 0; /* Failed. */ +} + +/* Try to find a two step delta relative to another constant. */ +static int emit_kdelta2(ASMState *as, Reg d, int32_t i) +{ + RegSet work = ~as->freeset & RSET_GPR; + while (work) { + Reg r = rset_picktop(work); + IRRef ref = regcost_ref(as->cost[r]); + lua_assert(r != d); + if (emit_canremat(ref)) { + int32_t other = ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i; + if (other) { + int32_t delta = i - other; + uint32_t sh, inv = 0, k2, k; + if (delta < 0) { delta = -delta; inv = ARMI_ADD^ARMI_SUB; } + sh = lj_ffs(delta) & ~1; + k2 = emit_isk12(0, delta & (255 << sh)); + k = emit_isk12(0, delta & ~(255 << sh)); + if (k) { + emit_dn(as, ARMI_ADD^k2^inv, d, d); + emit_dn(as, ARMI_ADD^k^inv, d, r); + return 1; + } + } + } + rset_clear(work, r); + } + return 0; /* Failed. */ +} + +/* Load a 32 bit constant into a GPR. */ +static void emit_loadi(ASMState *as, Reg r, int32_t i) +{ + uint32_t k = emit_isk12(ARMI_MOV, i); + lua_assert(rset_test(as->freeset, r) || r == RID_TMP); + if (k) { + /* Standard K12 constant. */ + emit_d(as, ARMI_MOV^k, r); + } else if ((as->flags & JIT_F_ARMV6T2) && (uint32_t)i < 0x00010000u) { + /* 16 bit loword constant for ARMv6T2. */ + emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), r); + } else if (emit_kdelta1(as, r, i)) { + /* One step delta relative to another constant. */ + } else if ((as->flags & JIT_F_ARMV6T2)) { + /* 32 bit hiword/loword constant for ARMv6T2. */ + emit_d(as, ARMI_MOVT|((i>>16) & 0x0fff)|(((i>>16) & 0xf000)<<4), r); + emit_d(as, ARMI_MOVW|(i & 0x0fff)|((i & 0xf000)<<4), r); + } else if (emit_kdelta2(as, r, i)) { + /* Two step delta relative to another constant. */ + } else { + /* Otherwise construct the constant with up to 4 instructions. */ + /* NYI: use mvn+bic, use pc-relative loads. */ + for (;;) { + uint32_t sh = lj_ffs(i) & ~1; + int32_t m = i & (255 << sh); + i &= ~(255 << sh); + if (i == 0) { + emit_d(as, ARMI_MOV ^ emit_isk12(0, m), r); + break; + } + emit_dn(as, ARMI_ORR ^ emit_isk12(0, m), r, r); + } + } +} + +#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) + +static Reg ra_allock(ASMState *as, int32_t k, RegSet allow); + +/* Get/set from constant pointer. */ +static void emit_lsptr(ASMState *as, ARMIns ai, Reg r, void *p) +{ + int32_t i = i32ptr(p); + emit_lso(as, ai, r, ra_allock(as, (i & ~4095), rset_exclude(RSET_GPR, r)), + (i & 4095)); +} + +#if !LJ_SOFTFP +/* Load a number constant into an FPR. */ +static void emit_loadn(ASMState *as, Reg r, cTValue *tv) +{ + int32_t i; + if ((as->flags & JIT_F_VFPV3) && !tv->u32.lo) { + uint32_t hi = tv->u32.hi; + uint32_t b = ((hi >> 22) & 0x1ff); + if (!(hi & 0xffff) && (b == 0x100 || b == 0x0ff)) { + *--as->mcp = ARMI_VMOVI_D | ARMF_D(r & 15) | + ((tv->u32.hi >> 12) & 0x00080000) | + ((tv->u32.hi >> 4) & 0x00070000) | + ((tv->u32.hi >> 16) & 0x0000000f); + return; + } + } + i = i32ptr(tv); + emit_vlso(as, ARMI_VLDR_D, r, + ra_allock(as, (i & ~1020), RSET_GPR), (i & 1020)); +} +#endif + +/* Get/set global_State fields. */ +#define emit_getgl(as, r, field) \ + emit_lsptr(as, ARMI_LDR, (r), (void *)&J2G(as->J)->field) +#define emit_setgl(as, r, field) \ + emit_lsptr(as, ARMI_STR, (r), (void *)&J2G(as->J)->field) + +/* Trace number is determined from pc of exit instruction. */ +#define emit_setvmstate(as, i) UNUSED(i) + +/* -- Emit control-flow instructions -------------------------------------- */ + +/* Label for internal jumps. */ +typedef MCode *MCLabel; + +/* Return label pointing to current PC. */ +#define emit_label(as) ((as)->mcp) + +static void emit_branch(ASMState *as, ARMIns ai, MCode *target) +{ + MCode *p = as->mcp; + ptrdiff_t delta = (target - p) - 1; + lua_assert(((delta + 0x00800000) >> 24) == 0); + *--p = ai | ((uint32_t)delta & 0x00ffffffu); + as->mcp = p; +} + +#define emit_jmp(as, target) emit_branch(as, ARMI_B, (target)) + +static void emit_call(ASMState *as, void *target) +{ + MCode *p = --as->mcp; + ptrdiff_t delta = ((char *)target - (char *)p) - 8; + if ((((delta>>2) + 0x00800000) >> 24) == 0) { + if ((delta & 1)) + *p = ARMI_BLX | ((uint32_t)(delta>>2) & 0x00ffffffu) | ((delta&2) << 27); + else + *p = ARMI_BL | ((uint32_t)(delta>>2) & 0x00ffffffu); + } else { /* Target out of range: need indirect call. But don't use R0-R3. */ + Reg r = ra_allock(as, i32ptr(target), RSET_RANGE(RID_R4, RID_R12+1)); + *p = ARMI_BLXr | ARMF_M(r); + } +} + +/* -- Emit generic operations --------------------------------------------- */ + +/* Generic move between two regs. */ +static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) +{ +#if LJ_SOFTFP + lua_assert(!irt_isnum(ir->t)); UNUSED(ir); +#else + if (dst >= RID_MAX_GPR) { + emit_dm(as, irt_isnum(ir->t) ? ARMI_VMOV_D : ARMI_VMOV_S, + (dst & 15), (src & 15)); + return; + } +#endif + if (as->mcp != as->mcloop) { /* Swap early registers for loads/stores. */ + MCode ins = *as->mcp, swp = (src^dst); + if ((ins & 0x0c000000) == 0x04000000 && (ins & 0x02000010) != 0x02000010) { + if (!((ins ^ (dst << 16)) & 0x000f0000)) + *as->mcp = ins ^ (swp << 16); /* Swap N in load/store. */ + if (!(ins & 0x00100000) && !((ins ^ (dst << 12)) & 0x0000f000)) + *as->mcp = ins ^ (swp << 12); /* Swap D in store. */ + } + } + emit_dm(as, ARMI_MOV, dst, src); +} + +/* Generic load of register from stack slot. */ +static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) +{ +#if LJ_SOFTFP + lua_assert(!irt_isnum(ir->t)); UNUSED(ir); +#else + if (r >= RID_MAX_GPR) + emit_vlso(as, irt_isnum(ir->t) ? ARMI_VLDR_D : ARMI_VLDR_S, r, RID_SP, ofs); + else +#endif + emit_lso(as, ARMI_LDR, r, RID_SP, ofs); +} + +/* Generic store of register to stack slot. */ +static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) +{ +#if LJ_SOFTFP + lua_assert(!irt_isnum(ir->t)); UNUSED(ir); +#else + if (r >= RID_MAX_GPR) + emit_vlso(as, irt_isnum(ir->t) ? ARMI_VSTR_D : ARMI_VSTR_S, r, RID_SP, ofs); + else +#endif + emit_lso(as, ARMI_STR, r, RID_SP, ofs); +} + +/* Emit an arithmetic/logic operation with a constant operand. */ +static void emit_opk(ASMState *as, ARMIns ai, Reg dest, Reg src, + int32_t i, RegSet allow) +{ + uint32_t k = emit_isk12(ai, i); + if (k) + emit_dn(as, ai^k, dest, src); + else + emit_dnm(as, ai, dest, src, ra_allock(as, i, allow)); +} + +/* Add offset to pointer. */ +static void emit_addptr(ASMState *as, Reg r, int32_t ofs) +{ + if (ofs) + emit_opk(as, ARMI_ADD, r, r, ofs, rset_exclude(RSET_GPR, r)); +} + +#define emit_spsub(as, ofs) emit_addptr(as, RID_SP, -(ofs)) + diff --git a/external/lua/luajit/src/src/lj_emit_mips.h b/external/lua/luajit/src/src/lj_emit_mips.h new file mode 100644 index 0000000000..74821b8b87 --- /dev/null +++ b/external/lua/luajit/src/src/lj_emit_mips.h @@ -0,0 +1,211 @@ +/* +** MIPS instruction emitter. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* -- Emit basic instructions --------------------------------------------- */ + +static void emit_dst(ASMState *as, MIPSIns mi, Reg rd, Reg rs, Reg rt) +{ + *--as->mcp = mi | MIPSF_D(rd) | MIPSF_S(rs) | MIPSF_T(rt); +} + +static void emit_dta(ASMState *as, MIPSIns mi, Reg rd, Reg rt, uint32_t a) +{ + *--as->mcp = mi | MIPSF_D(rd) | MIPSF_T(rt) | MIPSF_A(a); +} + +#define emit_ds(as, mi, rd, rs) emit_dst(as, (mi), (rd), (rs), 0) +#define emit_tg(as, mi, rt, rg) emit_dst(as, (mi), (rg)&31, 0, (rt)) + +static void emit_tsi(ASMState *as, MIPSIns mi, Reg rt, Reg rs, int32_t i) +{ + *--as->mcp = mi | MIPSF_T(rt) | MIPSF_S(rs) | (i & 0xffff); +} + +#define emit_ti(as, mi, rt, i) emit_tsi(as, (mi), (rt), 0, (i)) +#define emit_hsi(as, mi, rh, rs, i) emit_tsi(as, (mi), (rh) & 31, (rs), (i)) + +static void emit_fgh(ASMState *as, MIPSIns mi, Reg rf, Reg rg, Reg rh) +{ + *--as->mcp = mi | MIPSF_F(rf&31) | MIPSF_G(rg&31) | MIPSF_H(rh&31); +} + +#define emit_fg(as, mi, rf, rg) emit_fgh(as, (mi), (rf), (rg), 0) + +static void emit_rotr(ASMState *as, Reg dest, Reg src, Reg tmp, uint32_t shift) +{ + if ((as->flags & JIT_F_MIPS32R2)) { + emit_dta(as, MIPSI_ROTR, dest, src, shift); + } else { + emit_dst(as, MIPSI_OR, dest, dest, tmp); + emit_dta(as, MIPSI_SLL, dest, src, (-shift)&31); + emit_dta(as, MIPSI_SRL, tmp, src, shift); + } +} + +/* -- Emit loads/stores --------------------------------------------------- */ + +/* Prefer rematerialization of BASE/L from global_State over spills. */ +#define emit_canremat(ref) ((ref) <= REF_BASE) + +/* Try to find a one step delta relative to another constant. */ +static int emit_kdelta1(ASMState *as, Reg t, int32_t i) +{ + RegSet work = ~as->freeset & RSET_GPR; + while (work) { + Reg r = rset_picktop(work); + IRRef ref = regcost_ref(as->cost[r]); + lua_assert(r != t); + if (ref < ASMREF_L) { + int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); + if (checki16(delta)) { + emit_tsi(as, MIPSI_ADDIU, t, r, delta); + return 1; + } + } + rset_clear(work, r); + } + return 0; /* Failed. */ +} + +/* Load a 32 bit constant into a GPR. */ +static void emit_loadi(ASMState *as, Reg r, int32_t i) +{ + if (checki16(i)) { + emit_ti(as, MIPSI_LI, r, i); + } else { + if ((i & 0xffff)) { + int32_t jgl = i32ptr(J2G(as->J)); + if ((uint32_t)(i-jgl) < 65536) { + emit_tsi(as, MIPSI_ADDIU, r, RID_JGL, i-jgl-32768); + return; + } else if (emit_kdelta1(as, r, i)) { + return; + } else if ((i >> 16) == 0) { + emit_tsi(as, MIPSI_ORI, r, RID_ZERO, i); + return; + } + emit_tsi(as, MIPSI_ORI, r, r, i); + } + emit_ti(as, MIPSI_LUI, r, (i >> 16)); + } +} + +#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) + +static Reg ra_allock(ASMState *as, int32_t k, RegSet allow); +static void ra_allockreg(ASMState *as, int32_t k, Reg r); + +/* Get/set from constant pointer. */ +static void emit_lsptr(ASMState *as, MIPSIns mi, Reg r, void *p, RegSet allow) +{ + int32_t jgl = i32ptr(J2G(as->J)); + int32_t i = i32ptr(p); + Reg base; + if ((uint32_t)(i-jgl) < 65536) { + i = i-jgl-32768; + base = RID_JGL; + } else { + base = ra_allock(as, i-(int16_t)i, allow); + } + emit_tsi(as, mi, r, base, i); +} + +#define emit_loadn(as, r, tv) \ + emit_lsptr(as, MIPSI_LDC1, ((r) & 31), (void *)(tv), RSET_GPR) + +/* Get/set global_State fields. */ +static void emit_lsglptr(ASMState *as, MIPSIns mi, Reg r, int32_t ofs) +{ + emit_tsi(as, mi, r, RID_JGL, ofs-32768); +} + +#define emit_getgl(as, r, field) \ + emit_lsglptr(as, MIPSI_LW, (r), (int32_t)offsetof(global_State, field)) +#define emit_setgl(as, r, field) \ + emit_lsglptr(as, MIPSI_SW, (r), (int32_t)offsetof(global_State, field)) + +/* Trace number is determined from per-trace exit stubs. */ +#define emit_setvmstate(as, i) UNUSED(i) + +/* -- Emit control-flow instructions -------------------------------------- */ + +/* Label for internal jumps. */ +typedef MCode *MCLabel; + +/* Return label pointing to current PC. */ +#define emit_label(as) ((as)->mcp) + +static void emit_branch(ASMState *as, MIPSIns mi, Reg rs, Reg rt, MCode *target) +{ + MCode *p = as->mcp; + ptrdiff_t delta = target - p; + lua_assert(((delta + 0x8000) >> 16) == 0); + *--p = mi | MIPSF_S(rs) | MIPSF_T(rt) | ((uint32_t)delta & 0xffffu); + as->mcp = p; +} + +static void emit_jmp(ASMState *as, MCode *target) +{ + *--as->mcp = MIPSI_NOP; + emit_branch(as, MIPSI_B, RID_ZERO, RID_ZERO, (target)); +} + +static void emit_call(ASMState *as, void *target) +{ + MCode *p = as->mcp; + *--p = MIPSI_NOP; + if ((((uintptr_t)target ^ (uintptr_t)p) >> 28) == 0) + *--p = MIPSI_JAL | (((uintptr_t)target >>2) & 0x03ffffffu); + else /* Target out of range: need indirect call. */ + *--p = MIPSI_JALR | MIPSF_S(RID_CFUNCADDR); + as->mcp = p; + ra_allockreg(as, i32ptr(target), RID_CFUNCADDR); +} + +/* -- Emit generic operations --------------------------------------------- */ + +#define emit_move(as, dst, src) \ + emit_ds(as, MIPSI_MOVE, (dst), (src)) + +/* Generic move between two regs. */ +static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) +{ + if (dst < RID_MAX_GPR) + emit_move(as, dst, src); + else + emit_fg(as, irt_isnum(ir->t) ? MIPSI_MOV_D : MIPSI_MOV_S, dst, src); +} + +/* Generic load of register from stack slot. */ +static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) +{ + if (r < RID_MAX_GPR) + emit_tsi(as, MIPSI_LW, r, RID_SP, ofs); + else + emit_tsi(as, irt_isnum(ir->t) ? MIPSI_LDC1 : MIPSI_LWC1, + (r & 31), RID_SP, ofs); +} + +/* Generic store of register to stack slot. */ +static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) +{ + if (r < RID_MAX_GPR) + emit_tsi(as, MIPSI_SW, r, RID_SP, ofs); + else + emit_tsi(as, irt_isnum(ir->t) ? MIPSI_SDC1 : MIPSI_SWC1, + (r&31), RID_SP, ofs); +} + +/* Add offset to pointer. */ +static void emit_addptr(ASMState *as, Reg r, int32_t ofs) +{ + if (ofs) { + lua_assert(checki16(ofs)); + emit_tsi(as, MIPSI_ADDIU, r, r, ofs); + } +} + +#define emit_spsub(as, ofs) emit_addptr(as, RID_SP, -(ofs)) + diff --git a/external/lua/luajit/src/src/lj_emit_ppc.h b/external/lua/luajit/src/src/lj_emit_ppc.h new file mode 100644 index 0000000000..a589f3a607 --- /dev/null +++ b/external/lua/luajit/src/src/lj_emit_ppc.h @@ -0,0 +1,238 @@ +/* +** PPC instruction emitter. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* -- Emit basic instructions --------------------------------------------- */ + +static void emit_tab(ASMState *as, PPCIns pi, Reg rt, Reg ra, Reg rb) +{ + *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | PPCF_B(rb); +} + +#define emit_asb(as, pi, ra, rs, rb) emit_tab(as, (pi), (rs), (ra), (rb)) +#define emit_as(as, pi, ra, rs) emit_tab(as, (pi), (rs), (ra), 0) +#define emit_ab(as, pi, ra, rb) emit_tab(as, (pi), 0, (ra), (rb)) + +static void emit_tai(ASMState *as, PPCIns pi, Reg rt, Reg ra, int32_t i) +{ + *--as->mcp = pi | PPCF_T(rt) | PPCF_A(ra) | (i & 0xffff); +} + +#define emit_ti(as, pi, rt, i) emit_tai(as, (pi), (rt), 0, (i)) +#define emit_ai(as, pi, ra, i) emit_tai(as, (pi), 0, (ra), (i)) +#define emit_asi(as, pi, ra, rs, i) emit_tai(as, (pi), (rs), (ra), (i)) + +#define emit_fab(as, pi, rf, ra, rb) \ + emit_tab(as, (pi), (rf)&31, (ra)&31, (rb)&31) +#define emit_fb(as, pi, rf, rb) emit_tab(as, (pi), (rf)&31, 0, (rb)&31) +#define emit_fac(as, pi, rf, ra, rc) \ + emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, 0) +#define emit_facb(as, pi, rf, ra, rc, rb) \ + emit_tab(as, (pi) | PPCF_C((rc) & 31), (rf)&31, (ra)&31, (rb)&31) +#define emit_fai(as, pi, rf, ra, i) emit_tai(as, (pi), (rf)&31, (ra), (i)) + +static void emit_rot(ASMState *as, PPCIns pi, Reg ra, Reg rs, + int32_t n, int32_t b, int32_t e) +{ + *--as->mcp = pi | PPCF_T(rs) | PPCF_A(ra) | PPCF_B(n) | + PPCF_MB(b) | PPCF_ME(e); +} + +static void emit_slwi(ASMState *as, Reg ra, Reg rs, int32_t n) +{ + lua_assert(n >= 0 && n < 32); + emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31-n); +} + +static void emit_rotlwi(ASMState *as, Reg ra, Reg rs, int32_t n) +{ + lua_assert(n >= 0 && n < 32); + emit_rot(as, PPCI_RLWINM, ra, rs, n, 0, 31); +} + +/* -- Emit loads/stores --------------------------------------------------- */ + +/* Prefer rematerialization of BASE/L from global_State over spills. */ +#define emit_canremat(ref) ((ref) <= REF_BASE) + +/* Try to find a one step delta relative to another constant. */ +static int emit_kdelta1(ASMState *as, Reg t, int32_t i) +{ + RegSet work = ~as->freeset & RSET_GPR; + while (work) { + Reg r = rset_picktop(work); + IRRef ref = regcost_ref(as->cost[r]); + lua_assert(r != t); + if (ref < ASMREF_L) { + int32_t delta = i - (ra_iskref(ref) ? ra_krefk(as, ref) : IR(ref)->i); + if (checki16(delta)) { + emit_tai(as, PPCI_ADDI, t, r, delta); + return 1; + } + } + rset_clear(work, r); + } + return 0; /* Failed. */ +} + +/* Load a 32 bit constant into a GPR. */ +static void emit_loadi(ASMState *as, Reg r, int32_t i) +{ + if (checki16(i)) { + emit_ti(as, PPCI_LI, r, i); + } else { + if ((i & 0xffff)) { + int32_t jgl = i32ptr(J2G(as->J)); + if ((uint32_t)(i-jgl) < 65536) { + emit_tai(as, PPCI_ADDI, r, RID_JGL, i-jgl-32768); + return; + } else if (emit_kdelta1(as, r, i)) { + return; + } + emit_asi(as, PPCI_ORI, r, r, i); + } + emit_ti(as, PPCI_LIS, r, (i >> 16)); + } +} + +#define emit_loada(as, r, addr) emit_loadi(as, (r), i32ptr((addr))) + +static Reg ra_allock(ASMState *as, int32_t k, RegSet allow); + +/* Get/set from constant pointer. */ +static void emit_lsptr(ASMState *as, PPCIns pi, Reg r, void *p, RegSet allow) +{ + int32_t jgl = i32ptr(J2G(as->J)); + int32_t i = i32ptr(p); + Reg base; + if ((uint32_t)(i-jgl) < 65536) { + i = i-jgl-32768; + base = RID_JGL; + } else { + base = ra_allock(as, i-(int16_t)i, allow); + } + emit_tai(as, pi, r, base, i); +} + +#define emit_loadn(as, r, tv) \ + emit_lsptr(as, PPCI_LFD, ((r) & 31), (void *)(tv), RSET_GPR) + +/* Get/set global_State fields. */ +static void emit_lsglptr(ASMState *as, PPCIns pi, Reg r, int32_t ofs) +{ + emit_tai(as, pi, r, RID_JGL, ofs-32768); +} + +#define emit_getgl(as, r, field) \ + emit_lsglptr(as, PPCI_LWZ, (r), (int32_t)offsetof(global_State, field)) +#define emit_setgl(as, r, field) \ + emit_lsglptr(as, PPCI_STW, (r), (int32_t)offsetof(global_State, field)) + +/* Trace number is determined from per-trace exit stubs. */ +#define emit_setvmstate(as, i) UNUSED(i) + +/* -- Emit control-flow instructions -------------------------------------- */ + +/* Label for internal jumps. */ +typedef MCode *MCLabel; + +/* Return label pointing to current PC. */ +#define emit_label(as) ((as)->mcp) + +static void emit_condbranch(ASMState *as, PPCIns pi, PPCCC cc, MCode *target) +{ + MCode *p = --as->mcp; + ptrdiff_t delta = (char *)target - (char *)p; + lua_assert(((delta + 0x8000) >> 16) == 0); + pi ^= (delta & 0x8000) * (PPCF_Y/0x8000); + *p = pi | PPCF_CC(cc) | ((uint32_t)delta & 0xffffu); +} + +static void emit_jmp(ASMState *as, MCode *target) +{ + MCode *p = --as->mcp; + ptrdiff_t delta = (char *)target - (char *)p; + *p = PPCI_B | (delta & 0x03fffffcu); +} + +static void emit_call(ASMState *as, void *target) +{ + MCode *p = --as->mcp; + ptrdiff_t delta = (char *)target - (char *)p; + if ((((delta>>2) + 0x00800000) >> 24) == 0) { + *p = PPCI_BL | (delta & 0x03fffffcu); + } else { /* Target out of range: need indirect call. Don't use arg reg. */ + RegSet allow = RSET_GPR & ~RSET_RANGE(RID_R0, REGARG_LASTGPR+1); + Reg r = ra_allock(as, i32ptr(target), allow); + *p = PPCI_BCTRL; + p[-1] = PPCI_MTCTR | PPCF_T(r); + as->mcp = p-1; + } +} + +/* -- Emit generic operations --------------------------------------------- */ + +#define emit_mr(as, dst, src) \ + emit_asb(as, PPCI_MR, (dst), (src), (src)) + +/* Generic move between two regs. */ +static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) +{ + UNUSED(ir); + if (dst < RID_MAX_GPR) + emit_mr(as, dst, src); + else + emit_fb(as, PPCI_FMR, dst, src); +} + +/* Generic load of register from stack slot. */ +static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) +{ + if (r < RID_MAX_GPR) + emit_tai(as, PPCI_LWZ, r, RID_SP, ofs); + else + emit_fai(as, irt_isnum(ir->t) ? PPCI_LFD : PPCI_LFS, r, RID_SP, ofs); +} + +/* Generic store of register to stack slot. */ +static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) +{ + if (r < RID_MAX_GPR) + emit_tai(as, PPCI_STW, r, RID_SP, ofs); + else + emit_fai(as, irt_isnum(ir->t) ? PPCI_STFD : PPCI_STFS, r, RID_SP, ofs); +} + +/* Emit a compare (for equality) with a constant operand. */ +static void emit_cmpi(ASMState *as, Reg r, int32_t k) +{ + if (checki16(k)) { + emit_ai(as, PPCI_CMPWI, r, k); + } else if (checku16(k)) { + emit_ai(as, PPCI_CMPLWI, r, k); + } else { + emit_ai(as, PPCI_CMPLWI, RID_TMP, k); + emit_asi(as, PPCI_XORIS, RID_TMP, r, (k >> 16)); + } +} + +/* Add offset to pointer. */ +static void emit_addptr(ASMState *as, Reg r, int32_t ofs) +{ + if (ofs) { + emit_tai(as, PPCI_ADDI, r, r, ofs); + if (!checki16(ofs)) + emit_tai(as, PPCI_ADDIS, r, r, (ofs + 32768) >> 16); + } +} + +static void emit_spsub(ASMState *as, int32_t ofs) +{ + if (ofs) { + emit_tai(as, PPCI_STWU, RID_TMP, RID_SP, -ofs); + emit_tai(as, PPCI_ADDI, RID_TMP, RID_SP, + CFRAME_SIZE + (as->parent ? as->parent->spadjust : 0)); + } +} + diff --git a/external/lua/luajit/src/src/lj_emit_x86.h b/external/lua/luajit/src/src/lj_emit_x86.h new file mode 100644 index 0000000000..bd184a3058 --- /dev/null +++ b/external/lua/luajit/src/src/lj_emit_x86.h @@ -0,0 +1,466 @@ +/* +** x86/x64 instruction emitter. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* -- Emit basic instructions --------------------------------------------- */ + +#define MODRM(mode, r1, r2) ((MCode)((mode)+(((r1)&7)<<3)+((r2)&7))) + +#if LJ_64 +#define REXRB(p, rr, rb) \ + { MCode rex = 0x40 + (((rr)>>1)&4) + (((rb)>>3)&1); \ + if (rex != 0x40) *--(p) = rex; } +#define FORCE_REX 0x200 +#define REX_64 (FORCE_REX|0x080000) +#else +#define REXRB(p, rr, rb) ((void)0) +#define FORCE_REX 0 +#define REX_64 0 +#endif + +#define emit_i8(as, i) (*--as->mcp = (MCode)(i)) +#define emit_i32(as, i) (*(int32_t *)(as->mcp-4) = (i), as->mcp -= 4) +#define emit_u32(as, u) (*(uint32_t *)(as->mcp-4) = (u), as->mcp -= 4) + +#define emit_x87op(as, xo) \ + (*(uint16_t *)(as->mcp-2) = (uint16_t)(xo), as->mcp -= 2) + +/* op */ +static LJ_AINLINE MCode *emit_op(x86Op xo, Reg rr, Reg rb, Reg rx, + MCode *p, int delta) +{ + int n = (int8_t)xo; +#if defined(__GNUC__) + if (__builtin_constant_p(xo) && n == -2) + p[delta-2] = (MCode)(xo >> 24); + else if (__builtin_constant_p(xo) && n == -3) + *(uint16_t *)(p+delta-3) = (uint16_t)(xo >> 16); + else +#endif + *(uint32_t *)(p+delta-5) = (uint32_t)xo; + p += n + delta; +#if LJ_64 + { + uint32_t rex = 0x40 + ((rr>>1)&(4+(FORCE_REX>>1)))+((rx>>2)&2)+((rb>>3)&1); + if (rex != 0x40) { + rex |= (rr >> 16); + if (n == -4) { *p = (MCode)rex; rex = (MCode)(xo >> 8); } + else if ((xo & 0xffffff) == 0x6600fd) { *p = (MCode)rex; rex = 0x66; } + *--p = (MCode)rex; + } + } +#else + UNUSED(rr); UNUSED(rb); UNUSED(rx); +#endif + return p; +} + +/* op + modrm */ +#define emit_opm(xo, mode, rr, rb, p, delta) \ + (p[(delta)-1] = MODRM((mode), (rr), (rb)), \ + emit_op((xo), (rr), (rb), 0, (p), (delta))) + +/* op + modrm + sib */ +#define emit_opmx(xo, mode, scale, rr, rb, rx, p) \ + (p[-1] = MODRM((scale), (rx), (rb)), \ + p[-2] = MODRM((mode), (rr), RID_ESP), \ + emit_op((xo), (rr), (rb), (rx), (p), -1)) + +/* op r1, r2 */ +static void emit_rr(ASMState *as, x86Op xo, Reg r1, Reg r2) +{ + MCode *p = as->mcp; + as->mcp = emit_opm(xo, XM_REG, r1, r2, p, 0); +} + +#if LJ_64 && defined(LUA_USE_ASSERT) +/* [addr] is sign-extended in x64 and must be in lower 2G (not 4G). */ +static int32_t ptr2addr(const void *p) +{ + lua_assert((uintptr_t)p < (uintptr_t)0x80000000); + return i32ptr(p); +} +#else +#define ptr2addr(p) (i32ptr((p))) +#endif + +/* op r, [addr] */ +static void emit_rma(ASMState *as, x86Op xo, Reg rr, const void *addr) +{ + MCode *p = as->mcp; + *(int32_t *)(p-4) = ptr2addr(addr); +#if LJ_64 + p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP); + as->mcp = emit_opm(xo, XM_OFS0, rr, RID_ESP, p, -5); +#else + as->mcp = emit_opm(xo, XM_OFS0, rr, RID_EBP, p, -4); +#endif +} + +/* op r, [base+ofs] */ +static void emit_rmro(ASMState *as, x86Op xo, Reg rr, Reg rb, int32_t ofs) +{ + MCode *p = as->mcp; + x86Mode mode; + if (ra_hasreg(rb)) { + if (ofs == 0 && (rb&7) != RID_EBP) { + mode = XM_OFS0; + } else if (checki8(ofs)) { + *--p = (MCode)ofs; + mode = XM_OFS8; + } else { + p -= 4; + *(int32_t *)p = ofs; + mode = XM_OFS32; + } + if ((rb&7) == RID_ESP) + *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP); + } else { + *(int32_t *)(p-4) = ofs; +#if LJ_64 + p[-5] = MODRM(XM_SCALE1, RID_ESP, RID_EBP); + p -= 5; + rb = RID_ESP; +#else + p -= 4; + rb = RID_EBP; +#endif + mode = XM_OFS0; + } + as->mcp = emit_opm(xo, mode, rr, rb, p, 0); +} + +/* op r, [base+idx*scale+ofs] */ +static void emit_rmrxo(ASMState *as, x86Op xo, Reg rr, Reg rb, Reg rx, + x86Mode scale, int32_t ofs) +{ + MCode *p = as->mcp; + x86Mode mode; + if (ofs == 0 && (rb&7) != RID_EBP) { + mode = XM_OFS0; + } else if (checki8(ofs)) { + mode = XM_OFS8; + *--p = (MCode)ofs; + } else { + mode = XM_OFS32; + p -= 4; + *(int32_t *)p = ofs; + } + as->mcp = emit_opmx(xo, mode, scale, rr, rb, rx, p); +} + +/* op r, i */ +static void emit_gri(ASMState *as, x86Group xg, Reg rb, int32_t i) +{ + MCode *p = as->mcp; + x86Op xo; + if (checki8(i)) { + *--p = (MCode)i; + xo = XG_TOXOi8(xg); + } else { + p -= 4; + *(int32_t *)p = i; + xo = XG_TOXOi(xg); + } + as->mcp = emit_opm(xo, XM_REG, (Reg)(xg & 7) | (rb & REX_64), rb, p, 0); +} + +/* op [base+ofs], i */ +static void emit_gmroi(ASMState *as, x86Group xg, Reg rb, int32_t ofs, + int32_t i) +{ + x86Op xo; + if (checki8(i)) { + emit_i8(as, i); + xo = XG_TOXOi8(xg); + } else { + emit_i32(as, i); + xo = XG_TOXOi(xg); + } + emit_rmro(as, xo, (Reg)(xg & 7), rb, ofs); +} + +#define emit_shifti(as, xg, r, i) \ + (emit_i8(as, (i)), emit_rr(as, XO_SHIFTi, (Reg)(xg), (r))) + +/* op r, rm/mrm */ +static void emit_mrm(ASMState *as, x86Op xo, Reg rr, Reg rb) +{ + MCode *p = as->mcp; + x86Mode mode = XM_REG; + if (rb == RID_MRM) { + rb = as->mrm.base; + if (rb == RID_NONE) { + rb = RID_EBP; + mode = XM_OFS0; + p -= 4; + *(int32_t *)p = as->mrm.ofs; + if (as->mrm.idx != RID_NONE) + goto mrmidx; +#if LJ_64 + *--p = MODRM(XM_SCALE1, RID_ESP, RID_EBP); + rb = RID_ESP; +#endif + } else { + if (as->mrm.ofs == 0 && (rb&7) != RID_EBP) { + mode = XM_OFS0; + } else if (checki8(as->mrm.ofs)) { + *--p = (MCode)as->mrm.ofs; + mode = XM_OFS8; + } else { + p -= 4; + *(int32_t *)p = as->mrm.ofs; + mode = XM_OFS32; + } + if (as->mrm.idx != RID_NONE) { + mrmidx: + as->mcp = emit_opmx(xo, mode, as->mrm.scale, rr, rb, as->mrm.idx, p); + return; + } + if ((rb&7) == RID_ESP) + *--p = MODRM(XM_SCALE1, RID_ESP, RID_ESP); + } + } + as->mcp = emit_opm(xo, mode, rr, rb, p, 0); +} + +/* op rm/mrm, i */ +static void emit_gmrmi(ASMState *as, x86Group xg, Reg rb, int32_t i) +{ + x86Op xo; + if (checki8(i)) { + emit_i8(as, i); + xo = XG_TOXOi8(xg); + } else { + emit_i32(as, i); + xo = XG_TOXOi(xg); + } + emit_mrm(as, xo, (Reg)(xg & 7) | (rb & REX_64), (rb & ~REX_64)); +} + +/* -- Emit loads/stores --------------------------------------------------- */ + +/* Instruction selection for XMM moves. */ +#define XMM_MOVRR(as) ((as->flags & JIT_F_SPLIT_XMM) ? XO_MOVSD : XO_MOVAPS) +#define XMM_MOVRM(as) ((as->flags & JIT_F_SPLIT_XMM) ? XO_MOVLPD : XO_MOVSD) + +/* mov [base+ofs], i */ +static void emit_movmroi(ASMState *as, Reg base, int32_t ofs, int32_t i) +{ + emit_i32(as, i); + emit_rmro(as, XO_MOVmi, 0, base, ofs); +} + +/* mov [base+ofs], r */ +#define emit_movtomro(as, r, base, ofs) \ + emit_rmro(as, XO_MOVto, (r), (base), (ofs)) + +/* Get/set global_State fields. */ +#define emit_opgl(as, xo, r, field) \ + emit_rma(as, (xo), (r), (void *)&J2G(as->J)->field) +#define emit_getgl(as, r, field) emit_opgl(as, XO_MOV, (r), field) +#define emit_setgl(as, r, field) emit_opgl(as, XO_MOVto, (r), field) + +#define emit_setvmstate(as, i) \ + (emit_i32(as, i), emit_opgl(as, XO_MOVmi, 0, vmstate)) + +/* mov r, i / xor r, r */ +static void emit_loadi(ASMState *as, Reg r, int32_t i) +{ + /* XOR r,r is shorter, but modifies the flags. This is bad for HIOP. */ + if (i == 0 && !(LJ_32 && (IR(as->curins)->o == IR_HIOP || + (as->curins+1 < as->T->nins && + IR(as->curins+1)->o == IR_HIOP)))) { + emit_rr(as, XO_ARITH(XOg_XOR), r, r); + } else { + MCode *p = as->mcp; + *(int32_t *)(p-4) = i; + p[-5] = (MCode)(XI_MOVri+(r&7)); + p -= 5; + REXRB(p, 0, r); + as->mcp = p; + } +} + +/* mov r, addr */ +#define emit_loada(as, r, addr) \ + emit_loadi(as, (r), ptr2addr((addr))) + +#if LJ_64 +/* mov r, imm64 or shorter 32 bit extended load. */ +static void emit_loadu64(ASMState *as, Reg r, uint64_t u64) +{ + if (checku32(u64)) { /* 32 bit load clears upper 32 bits. */ + emit_loadi(as, r, (int32_t)u64); + } else if (checki32((int64_t)u64)) { /* Sign-extended 32 bit load. */ + MCode *p = as->mcp; + *(int32_t *)(p-4) = (int32_t)u64; + as->mcp = emit_opm(XO_MOVmi, XM_REG, REX_64, r, p, -4); + } else { /* Full-size 64 bit load. */ + MCode *p = as->mcp; + *(uint64_t *)(p-8) = u64; + p[-9] = (MCode)(XI_MOVri+(r&7)); + p[-10] = 0x48 + ((r>>3)&1); + p -= 10; + as->mcp = p; + } +} +#endif + +/* movsd r, [&tv->n] / xorps r, r */ +static void emit_loadn(ASMState *as, Reg r, cTValue *tv) +{ + if (tvispzero(tv)) /* Use xor only for +0. */ + emit_rr(as, XO_XORPS, r, r); + else + emit_rma(as, XMM_MOVRM(as), r, &tv->n); +} + +/* -- Emit control-flow instructions -------------------------------------- */ + +/* Label for short jumps. */ +typedef MCode *MCLabel; + +#if LJ_32 && LJ_HASFFI +/* jmp short target */ +static void emit_sjmp(ASMState *as, MCLabel target) +{ + MCode *p = as->mcp; + ptrdiff_t delta = target - p; + lua_assert(delta == (int8_t)delta); + p[-1] = (MCode)(int8_t)delta; + p[-2] = XI_JMPs; + as->mcp = p - 2; +} +#endif + +/* jcc short target */ +static void emit_sjcc(ASMState *as, int cc, MCLabel target) +{ + MCode *p = as->mcp; + ptrdiff_t delta = target - p; + lua_assert(delta == (int8_t)delta); + p[-1] = (MCode)(int8_t)delta; + p[-2] = (MCode)(XI_JCCs+(cc&15)); + as->mcp = p - 2; +} + +/* jcc short (pending target) */ +static MCLabel emit_sjcc_label(ASMState *as, int cc) +{ + MCode *p = as->mcp; + p[-1] = 0; + p[-2] = (MCode)(XI_JCCs+(cc&15)); + as->mcp = p - 2; + return p; +} + +/* Fixup jcc short target. */ +static void emit_sfixup(ASMState *as, MCLabel source) +{ + source[-1] = (MCode)(as->mcp-source); +} + +/* Return label pointing to current PC. */ +#define emit_label(as) ((as)->mcp) + +/* Compute relative 32 bit offset for jump and call instructions. */ +static LJ_AINLINE int32_t jmprel(MCode *p, MCode *target) +{ + ptrdiff_t delta = target - p; + lua_assert(delta == (int32_t)delta); + return (int32_t)delta; +} + +/* jcc target */ +static void emit_jcc(ASMState *as, int cc, MCode *target) +{ + MCode *p = as->mcp; + *(int32_t *)(p-4) = jmprel(p, target); + p[-5] = (MCode)(XI_JCCn+(cc&15)); + p[-6] = 0x0f; + as->mcp = p - 6; +} + +/* jmp target */ +static void emit_jmp(ASMState *as, MCode *target) +{ + MCode *p = as->mcp; + *(int32_t *)(p-4) = jmprel(p, target); + p[-5] = XI_JMP; + as->mcp = p - 5; +} + +/* call target */ +static void emit_call_(ASMState *as, MCode *target) +{ + MCode *p = as->mcp; +#if LJ_64 + if (target-p != (int32_t)(target-p)) { + /* Assumes RID_RET is never an argument to calls and always clobbered. */ + emit_rr(as, XO_GROUP5, XOg_CALL, RID_RET); + emit_loadu64(as, RID_RET, (uint64_t)target); + return; + } +#endif + *(int32_t *)(p-4) = jmprel(p, target); + p[-5] = XI_CALL; + as->mcp = p - 5; +} + +#define emit_call(as, f) emit_call_(as, (MCode *)(void *)(f)) + +/* -- Emit generic operations --------------------------------------------- */ + +/* Use 64 bit operations to handle 64 bit IR types. */ +#if LJ_64 +#define REX_64IR(ir, r) ((r) + (irt_is64((ir)->t) ? REX_64 : 0)) +#else +#define REX_64IR(ir, r) (r) +#endif + +/* Generic move between two regs. */ +static void emit_movrr(ASMState *as, IRIns *ir, Reg dst, Reg src) +{ + UNUSED(ir); + if (dst < RID_MAX_GPR) + emit_rr(as, XO_MOV, REX_64IR(ir, dst), src); + else + emit_rr(as, XMM_MOVRR(as), dst, src); +} + +/* Generic load of register from stack slot. */ +static void emit_spload(ASMState *as, IRIns *ir, Reg r, int32_t ofs) +{ + if (r < RID_MAX_GPR) + emit_rmro(as, XO_MOV, REX_64IR(ir, r), RID_ESP, ofs); + else + emit_rmro(as, irt_isnum(ir->t) ? XMM_MOVRM(as) : XO_MOVSS, r, RID_ESP, ofs); +} + +/* Generic store of register to stack slot. */ +static void emit_spstore(ASMState *as, IRIns *ir, Reg r, int32_t ofs) +{ + if (r < RID_MAX_GPR) + emit_rmro(as, XO_MOVto, REX_64IR(ir, r), RID_ESP, ofs); + else + emit_rmro(as, irt_isnum(ir->t) ? XO_MOVSDto : XO_MOVSSto, r, RID_ESP, ofs); +} + +/* Add offset to pointer. */ +static void emit_addptr(ASMState *as, Reg r, int32_t ofs) +{ + if (ofs) { + if ((as->flags & JIT_F_LEA_AGU)) + emit_rmro(as, XO_LEA, r, r, ofs); + else + emit_gri(as, XG_ARITHi(XOg_ADD), r, ofs); + } +} + +#define emit_spsub(as, ofs) emit_addptr(as, RID_ESP|REX_64, -(ofs)) + +/* Prefer rematerialization of BASE/L from global_State over spills. */ +#define emit_canremat(ref) ((ref) <= REF_BASE) + diff --git a/external/lua/luajit/src/src/lj_err.c b/external/lua/luajit/src/src/lj_err.c new file mode 100644 index 0000000000..4a33a233db --- /dev/null +++ b/external/lua/luajit/src/src/lj_err.c @@ -0,0 +1,781 @@ +/* +** Error handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_err_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_err.h" +#include "lj_debug.h" +#include "lj_str.h" +#include "lj_func.h" +#include "lj_state.h" +#include "lj_frame.h" +#include "lj_ff.h" +#include "lj_trace.h" +#include "lj_vm.h" + +/* +** LuaJIT can either use internal or external frame unwinding: +** +** - Internal frame unwinding (INT) is free-standing and doesn't require +** any OS or library support. +** +** - External frame unwinding (EXT) uses the system-provided unwind handler. +** +** Pros and Cons: +** +** - EXT requires unwind tables for *all* functions on the C stack between +** the pcall/catch and the error/throw. This is the default on x64, +** but needs to be manually enabled on x86/PPC for non-C++ code. +** +** - INT is faster when actually throwing errors (but this happens rarely). +** Setting up error handlers is zero-cost in any case. +** +** - EXT provides full interoperability with C++ exceptions. You can throw +** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames. +** C++ destructors are called as needed. C++ exceptions caught by pcall +** are converted to the string "C++ exception". Lua errors can be caught +** with catch (...) in C++. +** +** - INT has only limited support for automatically catching C++ exceptions +** on POSIX systems using DWARF2 stack unwinding. Other systems may use +** the wrapper function feature. Lua errors thrown through C++ frames +** cannot be caught by C++ code and C++ destructors are not run. +** +** EXT is the default on x64 systems, INT is the default on all other systems. +** +** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack +** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled +** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set +** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules +** and all C libraries that have callbacks which may be used to call back +** into Lua. C++ code must *not* be compiled with -fno-exceptions. +** +** EXT cannot be enabled on WIN32 since system exceptions use code-driven SEH. +** EXT is mandatory on WIN64 since the calling convention has an abundance +** of callee-saved registers (rbx, rbp, rsi, rdi, r12-r15, xmm6-xmm15). +** EXT is mandatory on POSIX/x64 since the interpreter doesn't save r12/r13. +*/ + +#if defined(__GNUC__) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) +#define LJ_UNWIND_EXT 1 +#elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS +#define LJ_UNWIND_EXT 1 +#endif + +/* -- Error messages ------------------------------------------------------ */ + +/* Error message strings. */ +LJ_DATADEF const char *lj_err_allmsg = +#define ERRDEF(name, msg) msg "\0" +#include "lj_errmsg.h" +; + +/* -- Internal frame unwinding -------------------------------------------- */ + +/* Unwind Lua stack and move error message to new top. */ +LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top) +{ + lj_func_closeuv(L, top); + if (top < L->top-1) { + copyTV(L, top, L->top-1); + L->top = top+1; + } + lj_state_relimitstack(L); +} + +/* Unwind until stop frame. Optionally cleanup frames. */ +static void *err_unwind(lua_State *L, void *stopcf, int errcode) +{ + TValue *frame = L->base-1; + void *cf = L->cframe; + while (cf) { + int32_t nres = cframe_nres(cframe_raw(cf)); + if (nres < 0) { /* C frame without Lua frame? */ + TValue *top = restorestack(L, -nres); + if (frame < top) { /* Frame reached? */ + if (errcode) { + L->cframe = cframe_prev(cf); + L->base = frame+1; + unwindstack(L, top); + } + return cf; + } + } + if (frame <= tvref(L->stack)) + break; + switch (frame_typep(frame)) { + case FRAME_LUA: /* Lua frame. */ + case FRAME_LUAP: + frame = frame_prevl(frame); + break; + case FRAME_C: /* C frame. */ +#if LJ_HASFFI + unwind_c: +#endif +#if LJ_UNWIND_EXT + if (errcode) { + L->cframe = cframe_prev(cf); + L->base = frame_prevd(frame) + 1; + unwindstack(L, frame); + } else if (cf != stopcf) { + cf = cframe_prev(cf); + frame = frame_prevd(frame); + break; + } + return NULL; /* Continue unwinding. */ +#else + UNUSED(stopcf); + cf = cframe_prev(cf); + frame = frame_prevd(frame); + break; +#endif + case FRAME_CP: /* Protected C frame. */ + if (cframe_canyield(cf)) { /* Resume? */ + if (errcode) { + hook_leave(G(L)); /* Assumes nobody uses coroutines inside hooks. */ + L->cframe = NULL; + L->status = (uint8_t)errcode; + } + return cf; + } + if (errcode) { + L->cframe = cframe_prev(cf); + L->base = frame_prevd(frame) + 1; + unwindstack(L, frame); + } + return cf; + case FRAME_CONT: /* Continuation frame. */ +#if LJ_HASFFI + if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK) + goto unwind_c; +#endif + case FRAME_VARG: /* Vararg frame. */ + frame = frame_prevd(frame); + break; + case FRAME_PCALL: /* FF pcall() frame. */ + case FRAME_PCALLH: /* FF pcall() frame inside hook. */ + if (errcode) { + if (errcode == LUA_YIELD) { + frame = frame_prevd(frame); + break; + } + if (frame_typep(frame) == FRAME_PCALL) + hook_leave(G(L)); + L->cframe = cf; + L->base = frame_prevd(frame) + 1; + unwindstack(L, L->base); + } + return (void *)((intptr_t)cf | CFRAME_UNWIND_FF); + } + } + /* No C frame. */ + if (errcode) { + L->cframe = NULL; + L->base = tvref(L->stack)+1; + unwindstack(L, L->base); + if (G(L)->panic) + G(L)->panic(L); + exit(EXIT_FAILURE); + } + return L; /* Anything non-NULL will do. */ +} + +/* -- External frame unwinding -------------------------------------------- */ + +#if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_TARGET_WINDOWS + +/* +** We have to use our own definitions instead of the mandatory (!) unwind.h, +** since various OS, distros and compilers mess up the header installation. +*/ + +typedef struct _Unwind_Exception +{ + uint64_t exclass; + void (*excleanup)(int, struct _Unwind_Exception); + uintptr_t p1, p2; +} __attribute__((__aligned__)) _Unwind_Exception; + +typedef struct _Unwind_Context _Unwind_Context; + +#define _URC_OK 0 +#define _URC_FATAL_PHASE1_ERROR 3 +#define _URC_HANDLER_FOUND 6 +#define _URC_INSTALL_CONTEXT 7 +#define _URC_CONTINUE_UNWIND 8 +#define _URC_FAILURE 9 + +#if !LJ_TARGET_ARM + +extern uintptr_t _Unwind_GetCFA(_Unwind_Context *); +extern void _Unwind_SetGR(_Unwind_Context *, int, uintptr_t); +extern void _Unwind_SetIP(_Unwind_Context *, uintptr_t); +extern void _Unwind_DeleteException(_Unwind_Exception *); +extern int _Unwind_RaiseException(_Unwind_Exception *); + +#define _UA_SEARCH_PHASE 1 +#define _UA_CLEANUP_PHASE 2 +#define _UA_HANDLER_FRAME 4 +#define _UA_FORCE_UNWIND 8 + +#define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */ +#define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c)) +#define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff) +#define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff)) + +/* DWARF2 personality handler referenced from interpreter .eh_frame. */ +LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions, + uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx) +{ + void *cf; + lua_State *L; + if (version != 1) + return _URC_FATAL_PHASE1_ERROR; + UNUSED(uexclass); + cf = (void *)_Unwind_GetCFA(ctx); + L = cframe_L(cf); + if ((actions & _UA_SEARCH_PHASE)) { +#if LJ_UNWIND_EXT + if (err_unwind(L, cf, 0) == NULL) + return _URC_CONTINUE_UNWIND; +#endif + if (!LJ_UEXCLASS_CHECK(uexclass)) { + setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP)); + } + return _URC_HANDLER_FOUND; + } + if ((actions & _UA_CLEANUP_PHASE)) { + int errcode; + if (LJ_UEXCLASS_CHECK(uexclass)) { + errcode = LJ_UEXCLASS_ERRCODE(uexclass); + } else { + if ((actions & _UA_HANDLER_FRAME)) + _Unwind_DeleteException(uex); + errcode = LUA_ERRRUN; + } +#if LJ_UNWIND_EXT + cf = err_unwind(L, cf, errcode); + if ((actions & _UA_FORCE_UNWIND)) { + return _URC_CONTINUE_UNWIND; + } else if (cf) { + _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode); + _Unwind_SetIP(ctx, (uintptr_t)(cframe_unwind_ff(cf) ? + lj_vm_unwind_ff_eh : + lj_vm_unwind_c_eh)); + return _URC_INSTALL_CONTEXT; + } +#if LJ_TARGET_X86ORX64 + else if ((actions & _UA_HANDLER_FRAME)) { + /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/ + ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h + */ + _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode); + _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow); + return _URC_INSTALL_CONTEXT; + } +#endif +#else + /* This is not the proper way to escape from the unwinder. We get away with + ** it on non-x64 because the interpreter restores all callee-saved regs. + */ + lj_err_throw(L, errcode); +#endif + } + return _URC_CONTINUE_UNWIND; +} + +#if LJ_UNWIND_EXT +#if LJ_TARGET_OSX || defined(__OpenBSD__) +/* Sorry, no thread safety for OSX. Complain to Apple, not me. */ +static _Unwind_Exception static_uex; +#else +static __thread _Unwind_Exception static_uex; +#endif + +/* Raise DWARF2 exception. */ +static void err_raise_ext(int errcode) +{ + static_uex.exclass = LJ_UEXCLASS_MAKE(errcode); + static_uex.excleanup = NULL; + _Unwind_RaiseException(&static_uex); +} +#endif + +#else + +extern void _Unwind_DeleteException(void *); +extern int __gnu_unwind_frame (void *, _Unwind_Context *); +extern int _Unwind_VRS_Set(_Unwind_Context *, int, uint32_t, int, void *); +extern int _Unwind_VRS_Get(_Unwind_Context *, int, uint32_t, int, void *); + +static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r) +{ + uint32_t v; + _Unwind_VRS_Get(ctx, 0, r, 0, &v); + return v; +} + +static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v) +{ + _Unwind_VRS_Set(ctx, 0, r, 0, &v); +} + +#define _US_VIRTUAL_UNWIND_FRAME 0 +#define _US_UNWIND_FRAME_STARTING 1 +#define _US_ACTION_MASK 3 +#define _US_FORCE_UNWIND 8 + +/* ARM unwinder personality handler referenced from interpreter .ARM.extab. */ +LJ_FUNCA int lj_err_unwind_arm(int state, void *ucb, _Unwind_Context *ctx) +{ + void *cf = (void *)_Unwind_GetGR(ctx, 13); + lua_State *L = cframe_L(cf); + if ((state & _US_ACTION_MASK) == _US_VIRTUAL_UNWIND_FRAME) { + setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP)); + return _URC_HANDLER_FOUND; + } + if ((state&(_US_ACTION_MASK|_US_FORCE_UNWIND)) == _US_UNWIND_FRAME_STARTING) { + _Unwind_DeleteException(ucb); + _Unwind_SetGR(ctx, 15, (uint32_t)(void *)lj_err_throw); + _Unwind_SetGR(ctx, 0, (uint32_t)L); + _Unwind_SetGR(ctx, 1, (uint32_t)LUA_ERRRUN); + return _URC_INSTALL_CONTEXT; + } + if (__gnu_unwind_frame(ucb, ctx) != _URC_OK) + return _URC_FAILURE; + return _URC_CONTINUE_UNWIND; +} + +#endif + +#elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS + +/* +** Someone in Redmond owes me several days of my life. A lot of this is +** undocumented or just plain wrong on MSDN. Some of it can be gathered +** from 3rd party docs or must be found by trial-and-error. They really +** don't want you to write your own language-specific exception handler +** or to interact gracefully with MSVC. :-( +** +** Apparently MSVC doesn't call C++ destructors for foreign exceptions +** unless you compile your C++ code with /EHa. Unfortunately this means +** catch (...) also catches things like access violations. The use of +** _set_se_translator doesn't really help, because it requires /EHa, too. +*/ + +#define WIN32_LEAN_AND_MEAN +#include + +/* Taken from: http://www.nynaeve.net/?p=99 */ +typedef struct UndocumentedDispatcherContext { + ULONG64 ControlPc; + ULONG64 ImageBase; + PRUNTIME_FUNCTION FunctionEntry; + ULONG64 EstablisherFrame; + ULONG64 TargetIp; + PCONTEXT ContextRecord; + PEXCEPTION_ROUTINE LanguageHandler; + PVOID HandlerData; + PUNWIND_HISTORY_TABLE HistoryTable; + ULONG ScopeIndex; + ULONG Fill0; +} UndocumentedDispatcherContext; + +#ifdef _MSC_VER +/* Another wild guess. */ +extern __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow); +#endif + +#define LJ_MSVC_EXCODE ((DWORD)0xe06d7363) + +#define LJ_EXCODE ((DWORD)0xe24c4a00) +#define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c)) +#define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff) +#define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff)) + +/* Win64 exception handler for interpreter frame. */ +LJ_FUNCA EXCEPTION_DISPOSITION lj_err_unwind_win64(EXCEPTION_RECORD *rec, + void *cf, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch) +{ + lua_State *L = cframe_L(cf); + int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ? + LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN; + if ((rec->ExceptionFlags & 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */ + /* Unwind internal frames. */ + err_unwind(L, cf, errcode); + } else { + void *cf2 = err_unwind(L, cf, 0); + if (cf2) { /* We catch it, so start unwinding the upper frames. */ + if (rec->ExceptionCode == LJ_MSVC_EXCODE) { +#ifdef _MSC_VER + __DestructExceptionObject(rec, 1); +#endif + setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP)); + } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) { + /* Don't catch access violations etc. */ + return ExceptionContinueSearch; + } + /* Unwind the stack and call all handlers for all lower C frames + ** (including ourselves) again with EH_UNWINDING set. Then set + ** rsp = cf, rax = errcode and jump to the specified target. + */ + RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ? + lj_vm_unwind_ff_eh : + lj_vm_unwind_c_eh), + rec, (void *)errcode, ctx, dispatch->HistoryTable); + /* RtlUnwindEx should never return. */ + } + } + return ExceptionContinueSearch; +} + +/* Raise Windows exception. */ +static void err_raise_ext(int errcode) +{ + RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL); +} + +#endif + +/* -- Error handling ------------------------------------------------------ */ + +/* Throw error. Find catch frame, unwind stack and continue. */ +LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode) +{ + global_State *g = G(L); + lj_trace_abort(g); + setgcrefnull(g->jit_L); + L->status = 0; +#if LJ_UNWIND_EXT + err_raise_ext(errcode); + /* + ** A return from this function signals a corrupt C stack that cannot be + ** unwound. We have no choice but to call the panic function and exit. + ** + ** Usually this is caused by a C function without unwind information. + ** This should never happen on x64, but may happen if you've manually + ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every* + ** non-C++ file with -funwind-tables. + */ + if (G(L)->panic) + G(L)->panic(L); +#else + { + void *cf = err_unwind(L, NULL, errcode); + if (cframe_unwind_ff(cf)) + lj_vm_unwind_ff(cframe_raw(cf)); + else + lj_vm_unwind_c(cframe_raw(cf), errcode); + } +#endif + exit(EXIT_FAILURE); +} + +/* Return string object for error message. */ +LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em) +{ + return lj_str_newz(L, err2msg(em)); +} + +/* Out-of-memory error. */ +LJ_NOINLINE void lj_err_mem(lua_State *L) +{ + if (L->status == LUA_ERRERR+1) /* Don't touch the stack during lua_open. */ + lj_vm_unwind_c(L->cframe, LUA_ERRMEM); + setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM)); + lj_err_throw(L, LUA_ERRMEM); +} + +/* Find error function for runtime errors. Requires an extra stack traversal. */ +static ptrdiff_t finderrfunc(lua_State *L) +{ + cTValue *frame = L->base-1, *bot = tvref(L->stack); + void *cf = L->cframe; + while (frame > bot) { + lua_assert(cf != NULL); + while (cframe_nres(cframe_raw(cf)) < 0) { /* cframe without frame? */ + if (frame >= restorestack(L, -cframe_nres(cf))) + break; + if (cframe_errfunc(cf) >= 0) /* Error handler not inherited (-1)? */ + return cframe_errfunc(cf); + cf = cframe_prev(cf); /* Else unwind cframe and continue searching. */ + if (cf == NULL) + return 0; + } + switch (frame_typep(frame)) { + case FRAME_LUA: + case FRAME_LUAP: + frame = frame_prevl(frame); + break; + case FRAME_C: + cf = cframe_prev(cf); + /* fallthrough */ + case FRAME_CONT: +#if LJ_HASFFI + if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK) + cf = cframe_prev(cf); +#endif + case FRAME_VARG: + frame = frame_prevd(frame); + break; + case FRAME_CP: + if (cframe_canyield(cf)) return 0; + if (cframe_errfunc(cf) >= 0) + return cframe_errfunc(cf); + frame = frame_prevd(frame); + break; + case FRAME_PCALL: + case FRAME_PCALLH: + if (frame_ftsz(frame) >= (ptrdiff_t)(2*sizeof(TValue))) /* xpcall? */ + return savestack(L, frame-1); /* Point to xpcall's errorfunc. */ + return 0; + default: + lua_assert(0); + return 0; + } + } + return 0; +} + +/* Runtime error. */ +LJ_NOINLINE void lj_err_run(lua_State *L) +{ + ptrdiff_t ef = finderrfunc(L); + if (ef) { + TValue *errfunc = restorestack(L, ef); + TValue *top = L->top; + lj_trace_abort(G(L)); + if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) { + setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR)); + lj_err_throw(L, LUA_ERRERR); + } + L->status = LUA_ERRERR; + copyTV(L, top, top-1); + copyTV(L, top-1, errfunc); + L->top = top+1; + lj_vm_call(L, top, 1+1); /* Stack: |errfunc|msg| -> |msg| */ + } + lj_err_throw(L, LUA_ERRRUN); +} + +/* Formatted runtime error message. */ +LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...) +{ + const char *msg; + va_list argp; + va_start(argp, em); + if (curr_funcisL(L)) L->top = curr_topL(L); + msg = lj_str_pushvf(L, err2msg(em), argp); + va_end(argp); + lj_debug_addloc(L, msg, L->base-1, NULL); + lj_err_run(L); +} + +/* Non-vararg variant for better calling conventions. */ +LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em) +{ + err_msgv(L, em); +} + +/* Lexer error. */ +LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok, + BCLine line, ErrMsg em, va_list argp) +{ + char buff[LUA_IDSIZE]; + const char *msg; + lj_debug_shortname(buff, src); + msg = lj_str_pushvf(L, err2msg(em), argp); + msg = lj_str_pushf(L, "%s:%d: %s", buff, line, msg); + if (tok) + lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok); + lj_err_throw(L, LUA_ERRSYNTAX); +} + +/* Typecheck error for operands. */ +LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm) +{ + const char *tname = lj_typename(o); + const char *opname = err2msg(opm); + if (curr_funcisL(L)) { + GCproto *pt = curr_proto(L); + const BCIns *pc = cframe_Lpc(L) - 1; + const char *oname = NULL; + const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname); + if (kind) + err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname); + } + err_msgv(L, LJ_ERR_BADOPRV, opname, tname); +} + +/* Typecheck error for ordered comparisons. */ +LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2) +{ + const char *t1 = lj_typename(o1); + const char *t2 = lj_typename(o2); + err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2); + /* This assumes the two "boolean" entries are commoned by the C compiler. */ +} + +/* Typecheck error for __call. */ +LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o) +{ + /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object: + ** L->base still points to the caller. So add a dummy frame with L instead + ** of a function. See lua_getstack(). + */ + const BCIns *pc = cframe_Lpc(L); + if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) { + const char *tname = lj_typename(o); + setframe_pc(o, pc); + setframe_gc(o, obj2gco(L)); + L->top = L->base = o+1; + err_msgv(L, LJ_ERR_BADCALL, tname); + } + lj_err_optype(L, o, LJ_ERR_OPCALL); +} + +/* Error in context of caller. */ +LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg) +{ + TValue *frame = L->base-1; + TValue *pframe = NULL; + if (frame_islua(frame)) { + pframe = frame_prevl(frame); + } else if (frame_iscont(frame)) { +#if LJ_HASFFI + if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK) { + pframe = frame; + frame = NULL; + } else +#endif + { + pframe = frame_prevd(frame); +#if LJ_HASFFI + /* Remove frame for FFI metamethods. */ + if (frame_func(frame)->c.ffid >= FF_ffi_meta___index && + frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) { + L->base = pframe+1; + L->top = frame; + setcframe_pc(cframe_raw(L->cframe), frame_contpc(frame)); + } +#endif + } + } + lj_debug_addloc(L, msg, pframe, frame); + lj_err_run(L); +} + +/* Formatted error in context of caller. */ +LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...) +{ + const char *msg; + va_list argp; + va_start(argp, em); + msg = lj_str_pushvf(L, err2msg(em), argp); + va_end(argp); + lj_err_callermsg(L, msg); +} + +/* Error in context of caller. */ +LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em) +{ + lj_err_callermsg(L, err2msg(em)); +} + +/* Argument error message. */ +LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg, + const char *msg) +{ + const char *fname = "?"; + const char *ftype = lj_debug_funcname(L, L->base - 1, &fname); + if (narg < 0 && narg > LUA_REGISTRYINDEX) + narg = (int)(L->top - L->base) + narg + 1; + if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */ + msg = lj_str_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg); + else + msg = lj_str_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg); + lj_err_callermsg(L, msg); +} + +/* Formatted argument error. */ +LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...) +{ + const char *msg; + va_list argp; + va_start(argp, em); + msg = lj_str_pushvf(L, err2msg(em), argp); + va_end(argp); + err_argmsg(L, narg, msg); +} + +/* Argument error. */ +LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em) +{ + err_argmsg(L, narg, err2msg(em)); +} + +/* Typecheck error for arguments. */ +LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname) +{ + TValue *o = narg < 0 ? L->top + narg : L->base + narg-1; + const char *tname = o < L->top ? lj_typename(o) : lj_obj_typename[0]; + const char *msg = lj_str_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname); + err_argmsg(L, narg, msg); +} + +/* Typecheck error for arguments. */ +LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt) +{ + lj_err_argtype(L, narg, lj_obj_typename[tt+1]); +} + +/* -- Public error handling API ------------------------------------------- */ + +LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf) +{ + lua_CFunction old = G(L)->panic; + G(L)->panic = panicf; + return old; +} + +/* Forwarders for the public API (C calling convention and no LJ_NORET). */ +LUA_API int lua_error(lua_State *L) +{ + lj_err_run(L); + return 0; /* unreachable */ +} + +LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg) +{ + err_argmsg(L, narg, msg); + return 0; /* unreachable */ +} + +LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname) +{ + lj_err_argtype(L, narg, xname); + return 0; /* unreachable */ +} + +LUALIB_API void luaL_where(lua_State *L, int level) +{ + int size; + cTValue *frame = lj_debug_frame(L, level, &size); + lj_debug_addloc(L, "", frame, size ? frame+size : NULL); +} + +LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...) +{ + const char *msg; + va_list argp; + va_start(argp, fmt); + msg = lj_str_pushvf(L, fmt, argp); + va_end(argp); + lj_err_callermsg(L, msg); + return 0; /* unreachable */ +} + diff --git a/external/lua/luajit/src/src/lj_err.h b/external/lua/luajit/src/src/lj_err.h new file mode 100644 index 0000000000..dbea409100 --- /dev/null +++ b/external/lua/luajit/src/src/lj_err.h @@ -0,0 +1,41 @@ +/* +** Error handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_ERR_H +#define _LJ_ERR_H + +#include + +#include "lj_obj.h" + +typedef enum { +#define ERRDEF(name, msg) \ + LJ_ERR_##name, LJ_ERR_##name##_ = LJ_ERR_##name + sizeof(msg)-1, +#include "lj_errmsg.h" + LJ_ERR__MAX +} ErrMsg; + +LJ_DATA const char *lj_err_allmsg; +#define err2msg(em) (lj_err_allmsg+(int)(em)) + +LJ_FUNC GCstr *lj_err_str(lua_State *L, ErrMsg em); +LJ_FUNCA_NORET void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode); +LJ_FUNC_NORET void lj_err_mem(lua_State *L); +LJ_FUNC_NORET void lj_err_run(lua_State *L); +LJ_FUNC_NORET void lj_err_msg(lua_State *L, ErrMsg em); +LJ_FUNC_NORET void lj_err_lex(lua_State *L, GCstr *src, const char *tok, + BCLine line, ErrMsg em, va_list argp); +LJ_FUNC_NORET void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm); +LJ_FUNC_NORET void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2); +LJ_FUNC_NORET void lj_err_optype_call(lua_State *L, TValue *o); +LJ_FUNC_NORET void lj_err_callermsg(lua_State *L, const char *msg); +LJ_FUNC_NORET void lj_err_callerv(lua_State *L, ErrMsg em, ...); +LJ_FUNC_NORET void lj_err_caller(lua_State *L, ErrMsg em); +LJ_FUNC_NORET void lj_err_arg(lua_State *L, int narg, ErrMsg em); +LJ_FUNC_NORET void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...); +LJ_FUNC_NORET void lj_err_argtype(lua_State *L, int narg, const char *xname); +LJ_FUNC_NORET void lj_err_argt(lua_State *L, int narg, int tt); + +#endif diff --git a/external/lua/luajit/src/src/lj_errmsg.h b/external/lua/luajit/src/src/lj_errmsg.h new file mode 100644 index 0000000000..fd46acd496 --- /dev/null +++ b/external/lua/luajit/src/src/lj_errmsg.h @@ -0,0 +1,192 @@ +/* +** VM error messages. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* This file may be included multiple times with different ERRDEF macros. */ + +/* Basic error handling. */ +ERRDEF(ERRMEM, "not enough memory") +ERRDEF(ERRERR, "error in error handling") +ERRDEF(ERRCPP, "C++ exception") + +/* Allocations. */ +ERRDEF(STROV, "string length overflow") +ERRDEF(UDATAOV, "userdata length overflow") +ERRDEF(STKOV, "stack overflow") +ERRDEF(STKOVM, "stack overflow (%s)") +ERRDEF(TABOV, "table overflow") + +/* Table indexing. */ +ERRDEF(NANIDX, "table index is NaN") +ERRDEF(NILIDX, "table index is nil") +ERRDEF(NEXTIDX, "invalid key to " LUA_QL("next")) + +/* Metamethod resolving. */ +ERRDEF(BADCALL, "attempt to call a %s value") +ERRDEF(BADOPRT, "attempt to %s %s " LUA_QS " (a %s value)") +ERRDEF(BADOPRV, "attempt to %s a %s value") +ERRDEF(BADCMPT, "attempt to compare %s with %s") +ERRDEF(BADCMPV, "attempt to compare two %s values") +ERRDEF(GETLOOP, "loop in gettable") +ERRDEF(SETLOOP, "loop in settable") +ERRDEF(OPCALL, "call") +ERRDEF(OPINDEX, "index") +ERRDEF(OPARITH, "perform arithmetic on") +ERRDEF(OPCAT, "concatenate") +ERRDEF(OPLEN, "get length of") + +/* Type checks. */ +ERRDEF(BADSELF, "calling " LUA_QS " on bad self (%s)") +ERRDEF(BADARG, "bad argument #%d to " LUA_QS " (%s)") +ERRDEF(BADTYPE, "%s expected, got %s") +ERRDEF(BADVAL, "invalid value") +ERRDEF(NOVAL, "value expected") +ERRDEF(NOCORO, "coroutine expected") +ERRDEF(NOTABN, "nil or table expected") +ERRDEF(NOLFUNC, "Lua function expected") +ERRDEF(NOFUNCL, "function or level expected") +ERRDEF(NOSFT, "string/function/table expected") +ERRDEF(NOPROXY, "boolean or proxy expected") +ERRDEF(FORINIT, LUA_QL("for") " initial value must be a number") +ERRDEF(FORLIM, LUA_QL("for") " limit must be a number") +ERRDEF(FORSTEP, LUA_QL("for") " step must be a number") + +/* C API checks. */ +ERRDEF(NOENV, "no calling environment") +ERRDEF(CYIELD, "attempt to yield across C-call boundary") +ERRDEF(BADLU, "bad light userdata pointer") +ERRDEF(NOGCMM, "bad action while in __gc metamethod") +#if LJ_TARGET_WINDOWS +ERRDEF(BADFPU, "bad FPU precision (use D3DCREATE_FPU_PRESERVE with DirectX)") +#endif + +/* Standard library function errors. */ +ERRDEF(ASSERT, "assertion failed!") +ERRDEF(PROTMT, "cannot change a protected metatable") +ERRDEF(UNPACK, "too many results to unpack") +ERRDEF(RDRSTR, "reader function must return a string") +ERRDEF(PRTOSTR, LUA_QL("tostring") " must return a string to " LUA_QL("print")) +ERRDEF(IDXRNG, "index out of range") +ERRDEF(BASERNG, "base out of range") +ERRDEF(LVLRNG, "level out of range") +ERRDEF(INVLVL, "invalid level") +ERRDEF(INVOPT, "invalid option") +ERRDEF(INVOPTM, "invalid option " LUA_QS) +ERRDEF(INVFMT, "invalid format") +ERRDEF(SETFENV, LUA_QL("setfenv") " cannot change environment of given object") +ERRDEF(CORUN, "cannot resume running coroutine") +ERRDEF(CODEAD, "cannot resume dead coroutine") +ERRDEF(COSUSP, "cannot resume non-suspended coroutine") +ERRDEF(TABINS, "wrong number of arguments to " LUA_QL("insert")) +ERRDEF(TABCAT, "invalid value (%s) at index %d in table for " LUA_QL("concat")) +ERRDEF(TABSORT, "invalid order function for sorting") +ERRDEF(IOCLFL, "attempt to use a closed file") +ERRDEF(IOSTDCL, "standard file is closed") +ERRDEF(OSUNIQF, "unable to generate a unique filename") +ERRDEF(OSDATEF, "field " LUA_QS " missing in date table") +ERRDEF(STRDUMP, "unable to dump given function") +ERRDEF(STRSLC, "string slice too long") +ERRDEF(STRPATB, "missing " LUA_QL("[") " after " LUA_QL("%f") " in pattern") +ERRDEF(STRPATC, "invalid pattern capture") +ERRDEF(STRPATE, "malformed pattern (ends with " LUA_QL("%") ")") +ERRDEF(STRPATM, "malformed pattern (missing " LUA_QL("]") ")") +ERRDEF(STRPATU, "unbalanced pattern") +ERRDEF(STRPATX, "pattern too complex") +ERRDEF(STRCAPI, "invalid capture index") +ERRDEF(STRCAPN, "too many captures") +ERRDEF(STRCAPU, "unfinished capture") +ERRDEF(STRFMTO, "invalid option " LUA_QL("%%%c") " to " LUA_QL("format")) +ERRDEF(STRFMTR, "invalid format (repeated flags)") +ERRDEF(STRFMTW, "invalid format (width or precision too long)") +ERRDEF(STRGSRV, "invalid replacement value (a %s)") +ERRDEF(BADMODN, "name conflict for module " LUA_QS) +#if LJ_HASJIT +#if LJ_TARGET_X86ORX64 +ERRDEF(NOJIT, "JIT compiler disabled, CPU does not support SSE2") +#else +ERRDEF(NOJIT, "JIT compiler disabled") +#endif +#elif defined(LJ_ARCH_NOJIT) +ERRDEF(NOJIT, "no JIT compiler for this architecture (yet)") +#else +ERRDEF(NOJIT, "JIT compiler permanently disabled by build option") +#endif +ERRDEF(JITOPT, "unknown or malformed optimization flag " LUA_QS) + +/* Lexer/parser errors. */ +ERRDEF(XMODE, "attempt to load chunk with wrong mode") +ERRDEF(XNEAR, "%s near " LUA_QS) +ERRDEF(XELEM, "lexical element too long") +ERRDEF(XLINES, "chunk has too many lines") +ERRDEF(XLEVELS, "chunk has too many syntax levels") +ERRDEF(XNUMBER, "malformed number") +ERRDEF(XLSTR, "unfinished long string") +ERRDEF(XLCOM, "unfinished long comment") +ERRDEF(XSTR, "unfinished string") +ERRDEF(XESC, "invalid escape sequence") +ERRDEF(XLDELIM, "invalid long string delimiter") +ERRDEF(XTOKEN, LUA_QS " expected") +ERRDEF(XJUMP, "control structure too long") +ERRDEF(XSLOTS, "function or expression too complex") +ERRDEF(XLIMC, "chunk has more than %d local variables") +ERRDEF(XLIMM, "main function has more than %d %s") +ERRDEF(XLIMF, "function at line %d has more than %d %s") +ERRDEF(XMATCH, LUA_QS " expected (to close " LUA_QS " at line %d)") +ERRDEF(XFIXUP, "function too long for return fixup") +ERRDEF(XPARAM, " or " LUA_QL("...") " expected") +#if !LJ_52 +ERRDEF(XAMBIG, "ambiguous syntax (function call x new statement)") +#endif +ERRDEF(XFUNARG, "function arguments expected") +ERRDEF(XSYMBOL, "unexpected symbol") +ERRDEF(XDOTS, "cannot use " LUA_QL("...") " outside a vararg function") +ERRDEF(XSYNTAX, "syntax error") +ERRDEF(XFOR, LUA_QL("=") " or " LUA_QL("in") " expected") +ERRDEF(XBREAK, "no loop to break") +ERRDEF(XLUNDEF, "undefined label " LUA_QS) +ERRDEF(XLDUP, "duplicate label " LUA_QS) +ERRDEF(XGSCOPE, " jumps into the scope of local " LUA_QS) + +/* Bytecode reader errors. */ +ERRDEF(BCFMT, "cannot load incompatible bytecode") +ERRDEF(BCBAD, "cannot load malformed bytecode") + +#if LJ_HASFFI +/* FFI errors. */ +ERRDEF(FFI_INVTYPE, "invalid C type") +ERRDEF(FFI_INVSIZE, "size of C type is unknown or too large") +ERRDEF(FFI_BADSCL, "bad storage class") +ERRDEF(FFI_DECLSPEC, "declaration specifier expected") +ERRDEF(FFI_BADTAG, "undeclared or implicit tag " LUA_QS) +ERRDEF(FFI_REDEF, "attempt to redefine " LUA_QS) +ERRDEF(FFI_NUMPARAM, "wrong number of type parameters") +ERRDEF(FFI_INITOV, "too many initializers for " LUA_QS) +ERRDEF(FFI_BADCONV, "cannot convert " LUA_QS " to " LUA_QS) +ERRDEF(FFI_BADLEN, "attempt to get length of " LUA_QS) +ERRDEF(FFI_BADCONCAT, "attempt to concatenate " LUA_QS " and " LUA_QS) +ERRDEF(FFI_BADARITH, "attempt to perform arithmetic on " LUA_QS " and " LUA_QS) +ERRDEF(FFI_BADCOMP, "attempt to compare " LUA_QS " with " LUA_QS) +ERRDEF(FFI_BADCALL, LUA_QS " is not callable") +ERRDEF(FFI_NUMARG, "wrong number of arguments for function call") +ERRDEF(FFI_BADMEMBER, LUA_QS " has no member named " LUA_QS) +ERRDEF(FFI_BADIDX, LUA_QS " cannot be indexed") +ERRDEF(FFI_BADIDXW, LUA_QS " cannot be indexed with " LUA_QS) +ERRDEF(FFI_BADMM, LUA_QS " has no " LUA_QS " metamethod") +ERRDEF(FFI_WRCONST, "attempt to write to constant location") +ERRDEF(FFI_NODECL, "missing declaration for symbol " LUA_QS) +ERRDEF(FFI_BADCBACK, "bad callback") +#if LJ_OS_NOJIT +ERRDEF(FFI_CBACKOV, "no support for callbacks on this OS") +#else +ERRDEF(FFI_CBACKOV, "too many callbacks") +#endif +ERRDEF(FFI_NYIPACKBIT, "NYI: packed bit fields") +ERRDEF(FFI_NYICALL, "NYI: cannot call this C function (yet)") +#endif + +#undef ERRDEF + +/* Detecting unused error messages: + awk -F, '/^ERRDEF/ { gsub(/ERRDEF./, ""); printf "grep -q LJ_ERR_%s *.[ch] || echo %s\n", $1, $1}' lj_errmsg.h | sh +*/ diff --git a/external/lua/luajit/src/src/lj_ff.h b/external/lua/luajit/src/src/lj_ff.h new file mode 100644 index 0000000000..b4a430108c --- /dev/null +++ b/external/lua/luajit/src/src/lj_ff.h @@ -0,0 +1,18 @@ +/* +** Fast function IDs. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_FF_H +#define _LJ_FF_H + +/* Fast function ID. */ +typedef enum { + FF_LUA_ = FF_LUA, /* Lua function (must be 0). */ + FF_C_ = FF_C, /* Regular C function (must be 1). */ +#define FFDEF(name) FF_##name, +#include "lj_ffdef.h" + FF__MAX +} FastFunc; + +#endif diff --git a/external/lua/luajit/src/src/lj_ffrecord.c b/external/lua/luajit/src/src/lj_ffrecord.c new file mode 100644 index 0000000000..4aa4f06498 --- /dev/null +++ b/external/lua/luajit/src/src/lj_ffrecord.c @@ -0,0 +1,889 @@ +/* +** Fast function call recorder. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_ffrecord_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_frame.h" +#include "lj_bc.h" +#include "lj_ff.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_ircall.h" +#include "lj_iropt.h" +#include "lj_trace.h" +#include "lj_record.h" +#include "lj_ffrecord.h" +#include "lj_crecord.h" +#include "lj_dispatch.h" +#include "lj_vm.h" +#include "lj_strscan.h" + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) + +/* Pass IR on to next optimization in chain (FOLD). */ +#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) + +/* -- Fast function recording handlers ------------------------------------ */ + +/* Conventions for fast function call handlers: +** +** The argument slots start at J->base[0]. All of them are guaranteed to be +** valid and type-specialized references. J->base[J->maxslot] is set to 0 +** as a sentinel. The runtime argument values start at rd->argv[0]. +** +** In general fast functions should check for presence of all of their +** arguments and for the correct argument types. Some simplifications +** are allowed if the interpreter throws instead. But even if recording +** is aborted, the generated IR must be consistent (no zero-refs). +** +** The number of results in rd->nres is set to 1. Handlers that return +** a different number of results need to override it. A negative value +** prevents return processing (e.g. for pending calls). +** +** Results need to be stored starting at J->base[0]. Return processing +** moves them to the right slots later. +** +** The per-ffid auxiliary data is the value of the 2nd part of the +** LJLIB_REC() annotation. This allows handling similar functionality +** in a common handler. +*/ + +/* Type of handler to record a fast function. */ +typedef void (LJ_FASTCALL *RecordFunc)(jit_State *J, RecordFFData *rd); + +/* Get runtime value of int argument. */ +static int32_t argv2int(jit_State *J, TValue *o) +{ + if (!lj_strscan_numberobj(o)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + return tvisint(o) ? intV(o) : lj_num2int(numV(o)); +} + +/* Get runtime value of string argument. */ +static GCstr *argv2str(jit_State *J, TValue *o) +{ + if (LJ_LIKELY(tvisstr(o))) { + return strV(o); + } else { + GCstr *s; + if (!tvisnumber(o)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + if (tvisint(o)) + s = lj_str_fromint(J->L, intV(o)); + else + s = lj_str_fromnum(J->L, &o->n); + setstrV(J->L, o, s); + return s; + } +} + +/* Return number of results wanted by caller. */ +static ptrdiff_t results_wanted(jit_State *J) +{ + TValue *frame = J->L->base-1; + if (frame_islua(frame)) + return (ptrdiff_t)bc_b(frame_pc(frame)[-1]) - 1; + else + return -1; +} + +/* Throw error for unsupported variant of fast function. */ +LJ_NORET static void recff_nyiu(jit_State *J) +{ + setfuncV(J->L, &J->errinfo, J->fn); + lj_trace_err_info(J, LJ_TRERR_NYIFFU); +} + +/* Fallback handler for all fast functions that are not recorded (yet). */ +static void LJ_FASTCALL recff_nyi(jit_State *J, RecordFFData *rd) +{ + setfuncV(J->L, &J->errinfo, J->fn); + lj_trace_err_info(J, LJ_TRERR_NYIFF); + UNUSED(rd); +} + +/* C functions can have arbitrary side-effects and are not recorded (yet). */ +static void LJ_FASTCALL recff_c(jit_State *J, RecordFFData *rd) +{ + setfuncV(J->L, &J->errinfo, J->fn); + lj_trace_err_info(J, LJ_TRERR_NYICF); + UNUSED(rd); +} + +/* -- Base library fast functions ----------------------------------------- */ + +static void LJ_FASTCALL recff_assert(jit_State *J, RecordFFData *rd) +{ + /* Arguments already specialized. The interpreter throws for nil/false. */ + rd->nres = J->maxslot; /* Pass through all arguments. */ +} + +static void LJ_FASTCALL recff_type(jit_State *J, RecordFFData *rd) +{ + /* Arguments already specialized. Result is a constant string. Neat, huh? */ + uint32_t t; + if (tvisnumber(&rd->argv[0])) + t = ~LJ_TNUMX; + else if (LJ_64 && tvislightud(&rd->argv[0])) + t = ~LJ_TLIGHTUD; + else + t = ~itype(&rd->argv[0]); + J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[t])); + UNUSED(rd); +} + +static void LJ_FASTCALL recff_getmetatable(jit_State *J, RecordFFData *rd) +{ + TRef tr = J->base[0]; + if (tr) { + RecordIndex ix; + ix.tab = tr; + copyTV(J->L, &ix.tabv, &rd->argv[0]); + if (lj_record_mm_lookup(J, &ix, MM_metatable)) + J->base[0] = ix.mobj; + else + J->base[0] = ix.mt; + } /* else: Interpreter will throw. */ +} + +static void LJ_FASTCALL recff_setmetatable(jit_State *J, RecordFFData *rd) +{ + TRef tr = J->base[0]; + TRef mt = J->base[1]; + if (tref_istab(tr) && (tref_istab(mt) || (mt && tref_isnil(mt)))) { + TRef fref, mtref; + RecordIndex ix; + ix.tab = tr; + copyTV(J->L, &ix.tabv, &rd->argv[0]); + lj_record_mm_lookup(J, &ix, MM_metatable); /* Guard for no __metatable. */ + fref = emitir(IRT(IR_FREF, IRT_P32), tr, IRFL_TAB_META); + mtref = tref_isnil(mt) ? lj_ir_knull(J, IRT_TAB) : mt; + emitir(IRT(IR_FSTORE, IRT_TAB), fref, mtref); + if (!tref_isnil(mt)) + emitir(IRT(IR_TBAR, IRT_TAB), tr, 0); + J->base[0] = tr; + J->needsnap = 1; + } /* else: Interpreter will throw. */ +} + +static void LJ_FASTCALL recff_rawget(jit_State *J, RecordFFData *rd) +{ + RecordIndex ix; + ix.tab = J->base[0]; ix.key = J->base[1]; + if (tref_istab(ix.tab) && ix.key) { + ix.val = 0; ix.idxchain = 0; + settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); + copyTV(J->L, &ix.keyv, &rd->argv[1]); + J->base[0] = lj_record_idx(J, &ix); + } /* else: Interpreter will throw. */ +} + +static void LJ_FASTCALL recff_rawset(jit_State *J, RecordFFData *rd) +{ + RecordIndex ix; + ix.tab = J->base[0]; ix.key = J->base[1]; ix.val = J->base[2]; + if (tref_istab(ix.tab) && ix.key && ix.val) { + ix.idxchain = 0; + settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); + copyTV(J->L, &ix.keyv, &rd->argv[1]); + copyTV(J->L, &ix.valv, &rd->argv[2]); + lj_record_idx(J, &ix); + /* Pass through table at J->base[0] as result. */ + } /* else: Interpreter will throw. */ +} + +static void LJ_FASTCALL recff_rawequal(jit_State *J, RecordFFData *rd) +{ + TRef tra = J->base[0]; + TRef trb = J->base[1]; + if (tra && trb) { + int diff = lj_record_objcmp(J, tra, trb, &rd->argv[0], &rd->argv[1]); + J->base[0] = diff ? TREF_FALSE : TREF_TRUE; + } /* else: Interpreter will throw. */ +} + +#if LJ_52 +static void LJ_FASTCALL recff_rawlen(jit_State *J, RecordFFData *rd) +{ + TRef tr = J->base[0]; + if (tref_isstr(tr)) + J->base[0] = emitir(IRTI(IR_FLOAD), tr, IRFL_STR_LEN); + else if (tref_istab(tr)) + J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, tr); + /* else: Interpreter will throw. */ + UNUSED(rd); +} +#endif + +/* Determine mode of select() call. */ +int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv) +{ + if (tref_isstr(tr) && *strVdata(tv) == '#') { /* select('#', ...) */ + if (strV(tv)->len == 1) { + emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, strV(tv))); + } else { + TRef trptr = emitir(IRT(IR_STRREF, IRT_P32), tr, lj_ir_kint(J, 0)); + TRef trchar = emitir(IRT(IR_XLOAD, IRT_U8), trptr, IRXLOAD_READONLY); + emitir(IRTG(IR_EQ, IRT_INT), trchar, lj_ir_kint(J, '#')); + } + return 0; + } else { /* select(n, ...) */ + int32_t start = argv2int(J, tv); + if (start == 0) lj_trace_err(J, LJ_TRERR_BADTYPE); /* A bit misleading. */ + return start; + } +} + +static void LJ_FASTCALL recff_select(jit_State *J, RecordFFData *rd) +{ + TRef tr = J->base[0]; + if (tr) { + ptrdiff_t start = lj_ffrecord_select_mode(J, tr, &rd->argv[0]); + if (start == 0) { /* select('#', ...) */ + J->base[0] = lj_ir_kint(J, J->maxslot - 1); + } else if (tref_isk(tr)) { /* select(k, ...) */ + ptrdiff_t n = (ptrdiff_t)J->maxslot; + if (start < 0) start += n; + else if (start > n) start = n; + rd->nres = n - start; + if (start >= 1) { + ptrdiff_t i; + for (i = 0; i < n - start; i++) + J->base[i] = J->base[start+i]; + } /* else: Interpreter will throw. */ + } else { + recff_nyiu(J); + } + } /* else: Interpreter will throw. */ +} + +static void LJ_FASTCALL recff_tonumber(jit_State *J, RecordFFData *rd) +{ + TRef tr = J->base[0]; + TRef base = J->base[1]; + if (tr && base) { + base = lj_opt_narrow_toint(J, base); + if (!tref_isk(base) || IR(tref_ref(base))->i != 10) + recff_nyiu(J); + } + if (tref_isnumber_str(tr)) { + if (tref_isstr(tr)) { + TValue tmp; + if (!lj_strscan_num(strV(&rd->argv[0]), &tmp)) + recff_nyiu(J); /* Would need an inverted STRTO for this case. */ + tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); + } +#if LJ_HASFFI + } else if (tref_iscdata(tr)) { + lj_crecord_tonumber(J, rd); + return; +#endif + } else { + tr = TREF_NIL; + } + J->base[0] = tr; + UNUSED(rd); +} + +static TValue *recff_metacall_cp(lua_State *L, lua_CFunction dummy, void *ud) +{ + jit_State *J = (jit_State *)ud; + lj_record_tailcall(J, 0, 1); + UNUSED(L); UNUSED(dummy); + return NULL; +} + +static int recff_metacall(jit_State *J, RecordFFData *rd, MMS mm) +{ + RecordIndex ix; + ix.tab = J->base[0]; + copyTV(J->L, &ix.tabv, &rd->argv[0]); + if (lj_record_mm_lookup(J, &ix, mm)) { /* Has metamethod? */ + int errcode; + TValue argv0; + /* Temporarily insert metamethod below object. */ + J->base[1] = J->base[0]; + J->base[0] = ix.mobj; + copyTV(J->L, &argv0, &rd->argv[0]); + copyTV(J->L, &rd->argv[1], &rd->argv[0]); + copyTV(J->L, &rd->argv[0], &ix.mobjv); + /* Need to protect lj_record_tailcall because it may throw. */ + errcode = lj_vm_cpcall(J->L, NULL, J, recff_metacall_cp); + /* Always undo Lua stack changes to avoid confusing the interpreter. */ + copyTV(J->L, &rd->argv[0], &argv0); + if (errcode) + lj_err_throw(J->L, errcode); /* Propagate errors. */ + rd->nres = -1; /* Pending call. */ + return 1; /* Tailcalled to metamethod. */ + } + return 0; +} + +static void LJ_FASTCALL recff_tostring(jit_State *J, RecordFFData *rd) +{ + TRef tr = J->base[0]; + if (tref_isstr(tr)) { + /* Ignore __tostring in the string base metatable. */ + /* Pass on result in J->base[0]. */ + } else if (!recff_metacall(J, rd, MM_tostring)) { + if (tref_isnumber(tr)) { + J->base[0] = emitir(IRT(IR_TOSTR, IRT_STR), tr, 0); + } else if (tref_ispri(tr)) { + J->base[0] = lj_ir_kstr(J, strV(&J->fn->c.upvalue[tref_type(tr)])); + } else { + recff_nyiu(J); + } + } +} + +static void LJ_FASTCALL recff_ipairs_aux(jit_State *J, RecordFFData *rd) +{ + RecordIndex ix; + ix.tab = J->base[0]; + if (tref_istab(ix.tab)) { + if (!tvisnumber(&rd->argv[1])) /* No support for string coercion. */ + lj_trace_err(J, LJ_TRERR_BADTYPE); + setintV(&ix.keyv, numberVint(&rd->argv[1])+1); + settabV(J->L, &ix.tabv, tabV(&rd->argv[0])); + ix.val = 0; ix.idxchain = 0; + ix.key = lj_opt_narrow_toint(J, J->base[1]); + J->base[0] = ix.key = emitir(IRTI(IR_ADD), ix.key, lj_ir_kint(J, 1)); + J->base[1] = lj_record_idx(J, &ix); + rd->nres = tref_isnil(J->base[1]) ? 0 : 2; + } /* else: Interpreter will throw. */ +} + +static void LJ_FASTCALL recff_ipairs(jit_State *J, RecordFFData *rd) +{ + if (!(LJ_52 && recff_metacall(J, rd, MM_ipairs))) { + TRef tab = J->base[0]; + if (tref_istab(tab)) { + J->base[0] = lj_ir_kfunc(J, funcV(&J->fn->c.upvalue[0])); + J->base[1] = tab; + J->base[2] = lj_ir_kint(J, 0); + rd->nres = 3; + } /* else: Interpreter will throw. */ + } +} + +static void LJ_FASTCALL recff_pcall(jit_State *J, RecordFFData *rd) +{ + if (J->maxslot >= 1) { + lj_record_call(J, 0, J->maxslot - 1); + rd->nres = -1; /* Pending call. */ + } /* else: Interpreter will throw. */ +} + +static TValue *recff_xpcall_cp(lua_State *L, lua_CFunction dummy, void *ud) +{ + jit_State *J = (jit_State *)ud; + lj_record_call(J, 1, J->maxslot - 2); + UNUSED(L); UNUSED(dummy); + return NULL; +} + +static void LJ_FASTCALL recff_xpcall(jit_State *J, RecordFFData *rd) +{ + if (J->maxslot >= 2) { + TValue argv0, argv1; + TRef tmp; + int errcode; + /* Swap function and traceback. */ + tmp = J->base[0]; J->base[0] = J->base[1]; J->base[1] = tmp; + copyTV(J->L, &argv0, &rd->argv[0]); + copyTV(J->L, &argv1, &rd->argv[1]); + copyTV(J->L, &rd->argv[0], &argv1); + copyTV(J->L, &rd->argv[1], &argv0); + /* Need to protect lj_record_call because it may throw. */ + errcode = lj_vm_cpcall(J->L, NULL, J, recff_xpcall_cp); + /* Always undo Lua stack swap to avoid confusing the interpreter. */ + copyTV(J->L, &rd->argv[0], &argv0); + copyTV(J->L, &rd->argv[1], &argv1); + if (errcode) + lj_err_throw(J->L, errcode); /* Propagate errors. */ + rd->nres = -1; /* Pending call. */ + } /* else: Interpreter will throw. */ +} + +/* -- Math library fast functions ----------------------------------------- */ + +static void LJ_FASTCALL recff_math_abs(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_ir_tonum(J, J->base[0]); + J->base[0] = emitir(IRTN(IR_ABS), tr, lj_ir_knum_abs(J)); + UNUSED(rd); +} + +/* Record rounding functions math.floor and math.ceil. */ +static void LJ_FASTCALL recff_math_round(jit_State *J, RecordFFData *rd) +{ + TRef tr = J->base[0]; + if (!tref_isinteger(tr)) { /* Pass through integers unmodified. */ + tr = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, tr), rd->data); + /* Result is integral (or NaN/Inf), but may not fit an int32_t. */ + if (LJ_DUALNUM) { /* Try to narrow using a guarded conversion to int. */ + lua_Number n = lj_vm_foldfpm(numberVnum(&rd->argv[0]), rd->data); + if (n == (lua_Number)lj_num2int(n)) + tr = emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_CHECK); + } + J->base[0] = tr; + } +} + +/* Record unary math.* functions, mapped to IR_FPMATH opcode. */ +static void LJ_FASTCALL recff_math_unary(jit_State *J, RecordFFData *rd) +{ + J->base[0] = emitir(IRTN(IR_FPMATH), lj_ir_tonum(J, J->base[0]), rd->data); +} + +/* Record math.log. */ +static void LJ_FASTCALL recff_math_log(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_ir_tonum(J, J->base[0]); + if (J->base[1]) { +#ifdef LUAJIT_NO_LOG2 + uint32_t fpm = IRFPM_LOG; +#else + uint32_t fpm = IRFPM_LOG2; +#endif + TRef trb = lj_ir_tonum(J, J->base[1]); + tr = emitir(IRTN(IR_FPMATH), tr, fpm); + trb = emitir(IRTN(IR_FPMATH), trb, fpm); + trb = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), trb); + tr = emitir(IRTN(IR_MUL), tr, trb); + } else { + tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_LOG); + } + J->base[0] = tr; + UNUSED(rd); +} + +/* Record math.atan2. */ +static void LJ_FASTCALL recff_math_atan2(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_ir_tonum(J, J->base[0]); + TRef tr2 = lj_ir_tonum(J, J->base[1]); + J->base[0] = emitir(IRTN(IR_ATAN2), tr, tr2); + UNUSED(rd); +} + +/* Record math.ldexp. */ +static void LJ_FASTCALL recff_math_ldexp(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_ir_tonum(J, J->base[0]); +#if LJ_TARGET_X86ORX64 + TRef tr2 = lj_ir_tonum(J, J->base[1]); +#else + TRef tr2 = lj_opt_narrow_toint(J, J->base[1]); +#endif + J->base[0] = emitir(IRTN(IR_LDEXP), tr, tr2); + UNUSED(rd); +} + +/* Record math.asin, math.acos, math.atan. */ +static void LJ_FASTCALL recff_math_atrig(jit_State *J, RecordFFData *rd) +{ + TRef y = lj_ir_tonum(J, J->base[0]); + TRef x = lj_ir_knum_one(J); + uint32_t ffid = rd->data; + if (ffid != FF_math_atan) { + TRef tmp = emitir(IRTN(IR_MUL), y, y); + tmp = emitir(IRTN(IR_SUB), x, tmp); + tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_SQRT); + if (ffid == FF_math_asin) { x = tmp; } else { x = y; y = tmp; } + } + J->base[0] = emitir(IRTN(IR_ATAN2), y, x); +} + +static void LJ_FASTCALL recff_math_htrig(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_ir_tonum(J, J->base[0]); + J->base[0] = emitir(IRTN(IR_CALLN), tr, rd->data); +} + +static void LJ_FASTCALL recff_math_modf(jit_State *J, RecordFFData *rd) +{ + TRef tr = J->base[0]; + if (tref_isinteger(tr)) { + J->base[0] = tr; + J->base[1] = lj_ir_kint(J, 0); + } else { + TRef trt; + tr = lj_ir_tonum(J, tr); + trt = emitir(IRTN(IR_FPMATH), tr, IRFPM_TRUNC); + J->base[0] = trt; + J->base[1] = emitir(IRTN(IR_SUB), tr, trt); + } + rd->nres = 2; +} + +static void LJ_FASTCALL recff_math_degrad(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_ir_tonum(J, J->base[0]); + TRef trm = lj_ir_knum(J, numV(&J->fn->c.upvalue[0])); + J->base[0] = emitir(IRTN(IR_MUL), tr, trm); + UNUSED(rd); +} + +static void LJ_FASTCALL recff_math_pow(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_ir_tonum(J, J->base[0]); + if (!tref_isnumber_str(J->base[1])) + lj_trace_err(J, LJ_TRERR_BADTYPE); + J->base[0] = lj_opt_narrow_pow(J, tr, J->base[1], &rd->argv[1]); + UNUSED(rd); +} + +static void LJ_FASTCALL recff_math_minmax(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_ir_tonumber(J, J->base[0]); + uint32_t op = rd->data; + BCReg i; + for (i = 1; J->base[i] != 0; i++) { + TRef tr2 = lj_ir_tonumber(J, J->base[i]); + IRType t = IRT_INT; + if (!(tref_isinteger(tr) && tref_isinteger(tr2))) { + if (tref_isinteger(tr)) tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); + if (tref_isinteger(tr2)) tr2 = emitir(IRTN(IR_CONV), tr2, IRCONV_NUM_INT); + t = IRT_NUM; + } + tr = emitir(IRT(op, t), tr, tr2); + } + J->base[0] = tr; +} + +static void LJ_FASTCALL recff_math_random(jit_State *J, RecordFFData *rd) +{ + GCudata *ud = udataV(&J->fn->c.upvalue[0]); + TRef tr, one; + lj_ir_kgc(J, obj2gco(ud), IRT_UDATA); /* Prevent collection. */ + tr = lj_ir_call(J, IRCALL_lj_math_random_step, lj_ir_kptr(J, uddata(ud))); + one = lj_ir_knum_one(J); + tr = emitir(IRTN(IR_SUB), tr, one); + if (J->base[0]) { + TRef tr1 = lj_ir_tonum(J, J->base[0]); + if (J->base[1]) { /* d = floor(d*(r2-r1+1.0)) + r1 */ + TRef tr2 = lj_ir_tonum(J, J->base[1]); + tr2 = emitir(IRTN(IR_SUB), tr2, tr1); + tr2 = emitir(IRTN(IR_ADD), tr2, one); + tr = emitir(IRTN(IR_MUL), tr, tr2); + tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR); + tr = emitir(IRTN(IR_ADD), tr, tr1); + } else { /* d = floor(d*r1) + 1.0 */ + tr = emitir(IRTN(IR_MUL), tr, tr1); + tr = emitir(IRTN(IR_FPMATH), tr, IRFPM_FLOOR); + tr = emitir(IRTN(IR_ADD), tr, one); + } + } + J->base[0] = tr; + UNUSED(rd); +} + +/* -- Bit library fast functions ------------------------------------------ */ + +/* Record unary bit.tobit, bit.bnot, bit.bswap. */ +static void LJ_FASTCALL recff_bit_unary(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_opt_narrow_tobit(J, J->base[0]); + J->base[0] = (rd->data == IR_TOBIT) ? tr : emitir(IRTI(rd->data), tr, 0); +} + +/* Record N-ary bit.band, bit.bor, bit.bxor. */ +static void LJ_FASTCALL recff_bit_nary(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_opt_narrow_tobit(J, J->base[0]); + uint32_t op = rd->data; + BCReg i; + for (i = 1; J->base[i] != 0; i++) + tr = emitir(IRTI(op), tr, lj_opt_narrow_tobit(J, J->base[i])); + J->base[0] = tr; +} + +/* Record bit shifts. */ +static void LJ_FASTCALL recff_bit_shift(jit_State *J, RecordFFData *rd) +{ + TRef tr = lj_opt_narrow_tobit(J, J->base[0]); + TRef tsh = lj_opt_narrow_tobit(J, J->base[1]); + IROp op = (IROp)rd->data; + if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) && + !tref_isk(tsh)) + tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 31)); +#ifdef LJ_TARGET_UNIFYROT + if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) { + op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR; + tsh = emitir(IRTI(IR_NEG), tsh, tsh); + } +#endif + J->base[0] = emitir(IRTI(op), tr, tsh); +} + +/* -- String library fast functions --------------------------------------- */ + +static void LJ_FASTCALL recff_string_len(jit_State *J, RecordFFData *rd) +{ + J->base[0] = emitir(IRTI(IR_FLOAD), lj_ir_tostr(J, J->base[0]), IRFL_STR_LEN); + UNUSED(rd); +} + +/* Handle string.byte (rd->data = 0) and string.sub (rd->data = 1). */ +static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd) +{ + TRef trstr = lj_ir_tostr(J, J->base[0]); + TRef trlen = emitir(IRTI(IR_FLOAD), trstr, IRFL_STR_LEN); + TRef tr0 = lj_ir_kint(J, 0); + TRef trstart, trend; + GCstr *str = argv2str(J, &rd->argv[0]); + int32_t start, end; + if (rd->data) { /* string.sub(str, start [,end]) */ + start = argv2int(J, &rd->argv[1]); + trstart = lj_opt_narrow_toint(J, J->base[1]); + trend = J->base[2]; + if (tref_isnil(trend)) { + trend = lj_ir_kint(J, -1); + end = -1; + } else { + trend = lj_opt_narrow_toint(J, trend); + end = argv2int(J, &rd->argv[2]); + } + } else { /* string.byte(str, [,start [,end]]) */ + if (J->base[1]) { + start = argv2int(J, &rd->argv[1]); + trstart = lj_opt_narrow_toint(J, J->base[1]); + trend = J->base[2]; + if (tref_isnil(trend)) { + trend = trstart; + end = start; + } else { + trend = lj_opt_narrow_toint(J, trend); + end = argv2int(J, &rd->argv[2]); + } + } else { + trend = trstart = lj_ir_kint(J, 1); + end = start = 1; + } + } + if (end < 0) { + emitir(IRTGI(IR_LT), trend, tr0); + trend = emitir(IRTI(IR_ADD), emitir(IRTI(IR_ADD), trlen, trend), + lj_ir_kint(J, 1)); + end = end+(int32_t)str->len+1; + } else if ((MSize)end <= str->len) { + emitir(IRTGI(IR_ULE), trend, trlen); + } else { + emitir(IRTGI(IR_GT), trend, trlen); + end = (int32_t)str->len; + trend = trlen; + } + if (start < 0) { + emitir(IRTGI(IR_LT), trstart, tr0); + trstart = emitir(IRTI(IR_ADD), trlen, trstart); + start = start+(int32_t)str->len; + emitir(start < 0 ? IRTGI(IR_LT) : IRTGI(IR_GE), trstart, tr0); + if (start < 0) { + trstart = tr0; + start = 0; + } + } else { + if (start == 0) { + emitir(IRTGI(IR_EQ), trstart, tr0); + trstart = tr0; + } else { + trstart = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, -1)); + emitir(IRTGI(IR_GE), trstart, tr0); + start--; + } + } + if (rd->data) { /* Return string.sub result. */ + if (end - start >= 0) { + /* Also handle empty range here, to avoid extra traces. */ + TRef trptr, trslen = emitir(IRTI(IR_SUB), trend, trstart); + emitir(IRTGI(IR_GE), trslen, tr0); + trptr = emitir(IRT(IR_STRREF, IRT_P32), trstr, trstart); + J->base[0] = emitir(IRT(IR_SNEW, IRT_STR), trptr, trslen); + } else { /* Range underflow: return empty string. */ + emitir(IRTGI(IR_LT), trend, trstart); + J->base[0] = lj_ir_kstr(J, lj_str_new(J->L, strdata(str), 0)); + } + } else { /* Return string.byte result(s). */ + ptrdiff_t i, len = end - start; + if (len > 0) { + TRef trslen = emitir(IRTI(IR_SUB), trend, trstart); + emitir(IRTGI(IR_EQ), trslen, lj_ir_kint(J, (int32_t)len)); + if (J->baseslot + len > LJ_MAX_JSLOTS) + lj_trace_err_info(J, LJ_TRERR_STACKOV); + rd->nres = len; + for (i = 0; i < len; i++) { + TRef tmp = emitir(IRTI(IR_ADD), trstart, lj_ir_kint(J, (int32_t)i)); + tmp = emitir(IRT(IR_STRREF, IRT_P32), trstr, tmp); + J->base[i] = emitir(IRT(IR_XLOAD, IRT_U8), tmp, IRXLOAD_READONLY); + } + } else { /* Empty range or range underflow: return no results. */ + emitir(IRTGI(IR_LE), trend, trstart); + rd->nres = 0; + } + } +} + +/* -- Table library fast functions ---------------------------------------- */ + +static void LJ_FASTCALL recff_table_getn(jit_State *J, RecordFFData *rd) +{ + if (tref_istab(J->base[0])) + J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, J->base[0]); + /* else: Interpreter will throw. */ + UNUSED(rd); +} + +static void LJ_FASTCALL recff_table_remove(jit_State *J, RecordFFData *rd) +{ + TRef tab = J->base[0]; + rd->nres = 0; + if (tref_istab(tab)) { + if (!J->base[1] || tref_isnil(J->base[1])) { /* Simple pop: t[#t] = nil */ + TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, tab); + GCtab *t = tabV(&rd->argv[0]); + MSize len = lj_tab_len(t); + emitir(IRTGI(len ? IR_NE : IR_EQ), trlen, lj_ir_kint(J, 0)); + if (len) { + RecordIndex ix; + ix.tab = tab; + ix.key = trlen; + settabV(J->L, &ix.tabv, t); + setintV(&ix.keyv, len); + ix.idxchain = 0; + if (results_wanted(J) != 0) { /* Specialize load only if needed. */ + ix.val = 0; + J->base[0] = lj_record_idx(J, &ix); /* Load previous value. */ + rd->nres = 1; + /* Assumes ix.key/ix.tab is not modified for raw lj_record_idx(). */ + } + ix.val = TREF_NIL; + lj_record_idx(J, &ix); /* Remove value. */ + } + } else { /* Complex case: remove in the middle. */ + recff_nyiu(J); + } + } /* else: Interpreter will throw. */ +} + +static void LJ_FASTCALL recff_table_insert(jit_State *J, RecordFFData *rd) +{ + RecordIndex ix; + ix.tab = J->base[0]; + ix.val = J->base[1]; + rd->nres = 0; + if (tref_istab(ix.tab) && ix.val) { + if (!J->base[2]) { /* Simple push: t[#t+1] = v */ + TRef trlen = lj_ir_call(J, IRCALL_lj_tab_len, ix.tab); + GCtab *t = tabV(&rd->argv[0]); + ix.key = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1)); + settabV(J->L, &ix.tabv, t); + setintV(&ix.keyv, lj_tab_len(t) + 1); + ix.idxchain = 0; + lj_record_idx(J, &ix); /* Set new value. */ + } else { /* Complex case: insert in the middle. */ + recff_nyiu(J); + } + } /* else: Interpreter will throw. */ +} + +/* -- I/O library fast functions ------------------------------------------ */ + +/* Get FILE* for I/O function. Any I/O error aborts recording, so there's +** no need to encode the alternate cases for any of the guards. +*/ +static TRef recff_io_fp(jit_State *J, TRef *udp, int32_t id) +{ + TRef tr, ud, fp; + if (id) { /* io.func() */ + tr = lj_ir_kptr(J, &J2G(J)->gcroot[id]); + ud = emitir(IRT(IR_XLOAD, IRT_UDATA), tr, 0); + } else { /* fp:method() */ + ud = J->base[0]; + if (!tref_isudata(ud)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + tr = emitir(IRT(IR_FLOAD, IRT_U8), ud, IRFL_UDATA_UDTYPE); + emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE)); + } + *udp = ud; + fp = emitir(IRT(IR_FLOAD, IRT_PTR), ud, IRFL_UDATA_FILE); + emitir(IRTG(IR_NE, IRT_PTR), fp, lj_ir_knull(J, IRT_PTR)); + return fp; +} + +static void LJ_FASTCALL recff_io_write(jit_State *J, RecordFFData *rd) +{ + TRef ud, fp = recff_io_fp(J, &ud, rd->data); + TRef zero = lj_ir_kint(J, 0); + TRef one = lj_ir_kint(J, 1); + ptrdiff_t i = rd->data == 0 ? 1 : 0; + for (; J->base[i]; i++) { + TRef str = lj_ir_tostr(J, J->base[i]); + TRef buf = emitir(IRT(IR_STRREF, IRT_P32), str, zero); + TRef len = emitir(IRTI(IR_FLOAD), str, IRFL_STR_LEN); + if (tref_isk(len) && IR(tref_ref(len))->i == 1) { + TRef tr = emitir(IRT(IR_XLOAD, IRT_U8), buf, IRXLOAD_READONLY); + tr = lj_ir_call(J, IRCALL_fputc, tr, fp); + if (results_wanted(J) != 0) /* Check result only if not ignored. */ + emitir(IRTGI(IR_NE), tr, lj_ir_kint(J, -1)); + } else { + TRef tr = lj_ir_call(J, IRCALL_fwrite, buf, one, len, fp); + if (results_wanted(J) != 0) /* Check result only if not ignored. */ + emitir(IRTGI(IR_EQ), tr, len); + } + } + J->base[0] = LJ_52 ? ud : TREF_TRUE; +} + +static void LJ_FASTCALL recff_io_flush(jit_State *J, RecordFFData *rd) +{ + TRef ud, fp = recff_io_fp(J, &ud, rd->data); + TRef tr = lj_ir_call(J, IRCALL_fflush, fp); + if (results_wanted(J) != 0) /* Check result only if not ignored. */ + emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0)); + J->base[0] = TREF_TRUE; +} + +/* -- Record calls to fast functions -------------------------------------- */ + +#include "lj_recdef.h" + +static uint32_t recdef_lookup(GCfunc *fn) +{ + if (fn->c.ffid < sizeof(recff_idmap)/sizeof(recff_idmap[0])) + return recff_idmap[fn->c.ffid]; + else + return 0; +} + +/* Record entry to a fast function or C function. */ +void lj_ffrecord_func(jit_State *J) +{ + RecordFFData rd; + uint32_t m = recdef_lookup(J->fn); + rd.data = m & 0xff; + rd.nres = 1; /* Default is one result. */ + rd.argv = J->L->base; + J->base[J->maxslot] = 0; /* Mark end of arguments. */ + (recff_func[m >> 8])(J, &rd); /* Call recff_* handler. */ + if (rd.nres >= 0) { + if (J->postproc == LJ_POST_NONE) J->postproc = LJ_POST_FFRETRY; + lj_record_ret(J, 0, rd.nres); + } +} + +#undef IR +#undef emitir + +#endif diff --git a/external/lua/luajit/src/src/lj_ffrecord.h b/external/lua/luajit/src/src/lj_ffrecord.h new file mode 100644 index 0000000000..9a30f359ed --- /dev/null +++ b/external/lua/luajit/src/src/lj_ffrecord.h @@ -0,0 +1,24 @@ +/* +** Fast function call recorder. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_FFRECORD_H +#define _LJ_FFRECORD_H + +#include "lj_obj.h" +#include "lj_jit.h" + +#if LJ_HASJIT +/* Data used by handlers to record a fast function. */ +typedef struct RecordFFData { + TValue *argv; /* Runtime argument values. */ + ptrdiff_t nres; /* Number of returned results (defaults to 1). */ + uint32_t data; /* Per-ffid auxiliary data (opcode, literal etc.). */ +} RecordFFData; + +LJ_FUNC int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv); +LJ_FUNC void lj_ffrecord_func(jit_State *J); +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_frame.h b/external/lua/luajit/src/src/lj_frame.h new file mode 100644 index 0000000000..60ce1a9f0d --- /dev/null +++ b/external/lua/luajit/src/src/lj_frame.h @@ -0,0 +1,183 @@ +/* +** Stack frames. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_FRAME_H +#define _LJ_FRAME_H + +#include "lj_obj.h" +#include "lj_bc.h" + +/* -- Lua stack frame ----------------------------------------------------- */ + +/* Frame type markers in callee function slot (callee base-1). */ +enum { + FRAME_LUA, FRAME_C, FRAME_CONT, FRAME_VARG, + FRAME_LUAP, FRAME_CP, FRAME_PCALL, FRAME_PCALLH +}; +#define FRAME_TYPE 3 +#define FRAME_P 4 +#define FRAME_TYPEP (FRAME_TYPE|FRAME_P) + +/* Macros to access and modify Lua frames. */ +#define frame_gc(f) (gcref((f)->fr.func)) +#define frame_func(f) (&frame_gc(f)->fn) +#define frame_ftsz(f) ((f)->fr.tp.ftsz) + +#define frame_type(f) (frame_ftsz(f) & FRAME_TYPE) +#define frame_typep(f) (frame_ftsz(f) & FRAME_TYPEP) +#define frame_islua(f) (frame_type(f) == FRAME_LUA) +#define frame_isc(f) (frame_type(f) == FRAME_C) +#define frame_iscont(f) (frame_typep(f) == FRAME_CONT) +#define frame_isvarg(f) (frame_typep(f) == FRAME_VARG) +#define frame_ispcall(f) ((frame_ftsz(f) & 6) == FRAME_PCALL) + +#define frame_pc(f) (mref((f)->fr.tp.pcr, const BCIns)) +#define frame_contpc(f) (frame_pc((f)-1)) +#if LJ_64 +#define frame_contf(f) \ + ((ASMFunction)(void *)((intptr_t)lj_vm_asm_begin + \ + (intptr_t)(int32_t)((f)-1)->u32.lo)) +#else +#define frame_contf(f) ((ASMFunction)gcrefp(((f)-1)->gcr, void)) +#endif +#define frame_delta(f) (frame_ftsz(f) >> 3) +#define frame_sized(f) (frame_ftsz(f) & ~FRAME_TYPEP) + +#define frame_prevl(f) ((f) - (1+bc_a(frame_pc(f)[-1]))) +#define frame_prevd(f) ((TValue *)((char *)(f) - frame_sized(f))) +#define frame_prev(f) (frame_islua(f)?frame_prevl(f):frame_prevd(f)) +/* Note: this macro does not skip over FRAME_VARG. */ + +#define setframe_pc(f, pc) (setmref((f)->fr.tp.pcr, (pc))) +#define setframe_ftsz(f, sz) ((f)->fr.tp.ftsz = (sz)) +#define setframe_gc(f, p) (setgcref((f)->fr.func, (p))) + +/* -- C stack frame ------------------------------------------------------- */ + +/* Macros to access and modify the C stack frame chain. */ + +/* These definitions must match with the arch-specific *.dasc files. */ +#if LJ_TARGET_X86 +#define CFRAME_OFS_ERRF (15*4) +#define CFRAME_OFS_NRES (14*4) +#define CFRAME_OFS_PREV (13*4) +#define CFRAME_OFS_L (12*4) +#define CFRAME_OFS_PC (6*4) +#define CFRAME_OFS_MULTRES (5*4) +#define CFRAME_SIZE (12*4) +#define CFRAME_SHIFT_MULTRES 0 +#elif LJ_TARGET_X64 +#if LJ_ABI_WIN +#define CFRAME_OFS_PREV (13*8) +#define CFRAME_OFS_PC (25*4) +#define CFRAME_OFS_L (24*4) +#define CFRAME_OFS_ERRF (23*4) +#define CFRAME_OFS_NRES (22*4) +#define CFRAME_OFS_MULTRES (21*4) +#define CFRAME_SIZE (10*8) +#define CFRAME_SIZE_JIT (CFRAME_SIZE + 9*16 + 4*8) +#define CFRAME_SHIFT_MULTRES 0 +#else +#define CFRAME_OFS_PREV (4*8) +#define CFRAME_OFS_PC (7*4) +#define CFRAME_OFS_L (6*4) +#define CFRAME_OFS_ERRF (5*4) +#define CFRAME_OFS_NRES (4*4) +#define CFRAME_OFS_MULTRES (1*4) +#define CFRAME_SIZE (10*8) +#define CFRAME_SIZE_JIT (CFRAME_SIZE + 16) +#define CFRAME_SHIFT_MULTRES 0 +#endif +#elif LJ_TARGET_ARM +#define CFRAME_OFS_ERRF 24 +#define CFRAME_OFS_NRES 20 +#define CFRAME_OFS_PREV 16 +#define CFRAME_OFS_L 12 +#define CFRAME_OFS_PC 8 +#define CFRAME_OFS_MULTRES 4 +#if LJ_ARCH_HASFPU +#define CFRAME_SIZE 128 +#else +#define CFRAME_SIZE 64 +#endif +#define CFRAME_SHIFT_MULTRES 3 +#elif LJ_TARGET_PPC +#if LJ_TARGET_XBOX360 +#define CFRAME_OFS_ERRF 424 +#define CFRAME_OFS_NRES 420 +#define CFRAME_OFS_PREV 400 +#define CFRAME_OFS_L 416 +#define CFRAME_OFS_PC 412 +#define CFRAME_OFS_MULTRES 408 +#define CFRAME_SIZE 384 +#define CFRAME_SHIFT_MULTRES 3 +#elif LJ_ARCH_PPC64 +#define CFRAME_OFS_ERRF 472 +#define CFRAME_OFS_NRES 468 +#define CFRAME_OFS_PREV 448 +#define CFRAME_OFS_L 464 +#define CFRAME_OFS_PC 460 +#define CFRAME_OFS_MULTRES 456 +#define CFRAME_SIZE 400 +#define CFRAME_SHIFT_MULTRES 3 +#else +#define CFRAME_OFS_ERRF 48 +#define CFRAME_OFS_NRES 44 +#define CFRAME_OFS_PREV 40 +#define CFRAME_OFS_L 36 +#define CFRAME_OFS_PC 32 +#define CFRAME_OFS_MULTRES 28 +#define CFRAME_SIZE 272 +#define CFRAME_SHIFT_MULTRES 3 +#endif +#elif LJ_TARGET_PPCSPE +#define CFRAME_OFS_ERRF 28 +#define CFRAME_OFS_NRES 24 +#define CFRAME_OFS_PREV 20 +#define CFRAME_OFS_L 16 +#define CFRAME_OFS_PC 12 +#define CFRAME_OFS_MULTRES 8 +#define CFRAME_SIZE 184 +#define CFRAME_SHIFT_MULTRES 3 +#elif LJ_TARGET_MIPS +#define CFRAME_OFS_ERRF 124 +#define CFRAME_OFS_NRES 120 +#define CFRAME_OFS_PREV 116 +#define CFRAME_OFS_L 112 +#define CFRAME_OFS_PC 20 +#define CFRAME_OFS_MULTRES 16 +#define CFRAME_SIZE 112 +#define CFRAME_SHIFT_MULTRES 3 +#else +#error "Missing CFRAME_* definitions for this architecture" +#endif + +#ifndef CFRAME_SIZE_JIT +#define CFRAME_SIZE_JIT CFRAME_SIZE +#endif + +#define CFRAME_RESUME 1 +#define CFRAME_UNWIND_FF 2 /* Only used in unwinder. */ +#define CFRAME_RAWMASK (~(intptr_t)(CFRAME_RESUME|CFRAME_UNWIND_FF)) + +#define cframe_errfunc(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_ERRF)) +#define cframe_nres(cf) (*(int32_t *)(((char *)(cf))+CFRAME_OFS_NRES)) +#define cframe_prev(cf) (*(void **)(((char *)(cf))+CFRAME_OFS_PREV)) +#define cframe_multres(cf) (*(uint32_t *)(((char *)(cf))+CFRAME_OFS_MULTRES)) +#define cframe_multres_n(cf) (cframe_multres((cf)) >> CFRAME_SHIFT_MULTRES) +#define cframe_L(cf) \ + (&gcref(*(GCRef *)(((char *)(cf))+CFRAME_OFS_L))->th) +#define cframe_pc(cf) \ + (mref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), const BCIns)) +#define setcframe_L(cf, L) \ + (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_L), (L))) +#define setcframe_pc(cf, pc) \ + (setmref(*(MRef *)(((char *)(cf))+CFRAME_OFS_PC), (pc))) +#define cframe_canyield(cf) ((intptr_t)(cf) & CFRAME_RESUME) +#define cframe_unwind_ff(cf) ((intptr_t)(cf) & CFRAME_UNWIND_FF) +#define cframe_raw(cf) ((void *)((intptr_t)(cf) & CFRAME_RAWMASK)) +#define cframe_Lpc(L) cframe_pc(cframe_raw(L->cframe)) + +#endif diff --git a/external/lua/luajit/src/src/lj_func.c b/external/lua/luajit/src/src/lj_func.c new file mode 100644 index 0000000000..83f2d0b58a --- /dev/null +++ b/external/lua/luajit/src/src/lj_func.c @@ -0,0 +1,185 @@ +/* +** Function handling (prototypes, functions and upvalues). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lj_func_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_func.h" +#include "lj_trace.h" +#include "lj_vm.h" + +/* -- Prototypes ---------------------------------------------------------- */ + +void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt) +{ + lj_mem_free(g, pt, pt->sizept); +} + +/* -- Upvalues ------------------------------------------------------------ */ + +static void unlinkuv(GCupval *uv) +{ + lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv); + setgcrefr(uvnext(uv)->prev, uv->prev); + setgcrefr(uvprev(uv)->next, uv->next); +} + +/* Find existing open upvalue for a stack slot or create a new one. */ +static GCupval *func_finduv(lua_State *L, TValue *slot) +{ + global_State *g = G(L); + GCRef *pp = &L->openupval; + GCupval *p; + GCupval *uv; + /* Search the sorted list of open upvalues. */ + while (gcref(*pp) != NULL && uvval((p = gco2uv(gcref(*pp)))) >= slot) { + lua_assert(!p->closed && uvval(p) != &p->tv); + if (uvval(p) == slot) { /* Found open upvalue pointing to same slot? */ + if (isdead(g, obj2gco(p))) /* Resurrect it, if it's dead. */ + flipwhite(obj2gco(p)); + return p; + } + pp = &p->nextgc; + } + /* No matching upvalue found. Create a new one. */ + uv = lj_mem_newt(L, sizeof(GCupval), GCupval); + newwhite(g, uv); + uv->gct = ~LJ_TUPVAL; + uv->closed = 0; /* Still open. */ + setmref(uv->v, slot); /* Pointing to the stack slot. */ + /* NOBARRIER: The GCupval is new (marked white) and open. */ + setgcrefr(uv->nextgc, *pp); /* Insert into sorted list of open upvalues. */ + setgcref(*pp, obj2gco(uv)); + setgcref(uv->prev, obj2gco(&g->uvhead)); /* Insert into GC list, too. */ + setgcrefr(uv->next, g->uvhead.next); + setgcref(uvnext(uv)->prev, obj2gco(uv)); + setgcref(g->uvhead.next, obj2gco(uv)); + lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv); + return uv; +} + +/* Create an empty and closed upvalue. */ +static GCupval *func_emptyuv(lua_State *L) +{ + GCupval *uv = (GCupval *)lj_mem_newgco(L, sizeof(GCupval)); + uv->gct = ~LJ_TUPVAL; + uv->closed = 1; + setnilV(&uv->tv); + setmref(uv->v, &uv->tv); + return uv; +} + +/* Close all open upvalues pointing to some stack level or above. */ +void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level) +{ + GCupval *uv; + global_State *g = G(L); + while (gcref(L->openupval) != NULL && + uvval((uv = gco2uv(gcref(L->openupval)))) >= level) { + GCobj *o = obj2gco(uv); + lua_assert(!isblack(o) && !uv->closed && uvval(uv) != &uv->tv); + setgcrefr(L->openupval, uv->nextgc); /* No longer in open list. */ + if (isdead(g, o)) { + lj_func_freeuv(g, uv); + } else { + unlinkuv(uv); + lj_gc_closeuv(g, uv); + } + } +} + +void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv) +{ + if (!uv->closed) + unlinkuv(uv); + lj_mem_freet(g, uv); +} + +/* -- Functions (closures) ------------------------------------------------ */ + +GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env) +{ + GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeCfunc(nelems)); + fn->c.gct = ~LJ_TFUNC; + fn->c.ffid = FF_C; + fn->c.nupvalues = (uint8_t)nelems; + /* NOBARRIER: The GCfunc is new (marked white). */ + setmref(fn->c.pc, &G(L)->bc_cfunc_ext); + setgcref(fn->c.env, obj2gco(env)); + return fn; +} + +static GCfunc *func_newL(lua_State *L, GCproto *pt, GCtab *env) +{ + uint32_t count; + GCfunc *fn = (GCfunc *)lj_mem_newgco(L, sizeLfunc((MSize)pt->sizeuv)); + fn->l.gct = ~LJ_TFUNC; + fn->l.ffid = FF_LUA; + fn->l.nupvalues = 0; /* Set to zero until upvalues are initialized. */ + /* NOBARRIER: Really a setgcref. But the GCfunc is new (marked white). */ + setmref(fn->l.pc, proto_bc(pt)); + setgcref(fn->l.env, obj2gco(env)); + /* Saturating 3 bit counter (0..7) for created closures. */ + count = (uint32_t)pt->flags + PROTO_CLCOUNT; + pt->flags = (uint8_t)(count - ((count >> PROTO_CLC_BITS) & PROTO_CLCOUNT)); + return fn; +} + +/* Create a new Lua function with empty upvalues. */ +GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env) +{ + GCfunc *fn = func_newL(L, pt, env); + MSize i, nuv = pt->sizeuv; + /* NOBARRIER: The GCfunc is new (marked white). */ + for (i = 0; i < nuv; i++) { + GCupval *uv = func_emptyuv(L); + uv->dhash = (uint32_t)(uintptr_t)pt ^ ((uint32_t)proto_uv(pt)[i] << 24); + setgcref(fn->l.uvptr[i], obj2gco(uv)); + } + fn->l.nupvalues = (uint8_t)nuv; + return fn; +} + +/* Do a GC check and create a new Lua function with inherited upvalues. */ +GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent) +{ + GCfunc *fn; + GCRef *puv; + MSize i, nuv; + TValue *base; + lj_gc_check_fixtop(L); + fn = func_newL(L, pt, tabref(parent->env)); + /* NOBARRIER: The GCfunc is new (marked white). */ + puv = parent->uvptr; + nuv = pt->sizeuv; + base = L->base; + for (i = 0; i < nuv; i++) { + uint32_t v = proto_uv(pt)[i]; + GCupval *uv; + if ((v & PROTO_UV_LOCAL)) { + uv = func_finduv(L, base + (v & 0xff)); + uv->immutable = ((v / PROTO_UV_IMMUTABLE) & 1); + uv->dhash = (uint32_t)(uintptr_t)mref(parent->pc, char) ^ (v << 24); + } else { + uv = &gcref(puv[v])->uv; + } + setgcref(fn->l.uvptr[i], obj2gco(uv)); + } + fn->l.nupvalues = (uint8_t)nuv; + return fn; +} + +void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *fn) +{ + MSize size = isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) : + sizeCfunc((MSize)fn->c.nupvalues); + lj_mem_free(g, fn, size); +} + diff --git a/external/lua/luajit/src/src/lj_func.h b/external/lua/luajit/src/src/lj_func.h new file mode 100644 index 0000000000..73280a8e1a --- /dev/null +++ b/external/lua/luajit/src/src/lj_func.h @@ -0,0 +1,24 @@ +/* +** Function handling (prototypes, functions and upvalues). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_FUNC_H +#define _LJ_FUNC_H + +#include "lj_obj.h" + +/* Prototypes. */ +LJ_FUNC void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt); + +/* Upvalues. */ +LJ_FUNCA void LJ_FASTCALL lj_func_closeuv(lua_State *L, TValue *level); +LJ_FUNC void LJ_FASTCALL lj_func_freeuv(global_State *g, GCupval *uv); + +/* Functions (closures). */ +LJ_FUNC GCfunc *lj_func_newC(lua_State *L, MSize nelems, GCtab *env); +LJ_FUNC GCfunc *lj_func_newL_empty(lua_State *L, GCproto *pt, GCtab *env); +LJ_FUNCA GCfunc *lj_func_newL_gc(lua_State *L, GCproto *pt, GCfuncL *parent); +LJ_FUNC void LJ_FASTCALL lj_func_free(global_State *g, GCfunc *c); + +#endif diff --git a/external/lua/luajit/src/src/lj_gc.c b/external/lua/luajit/src/src/lj_gc.c new file mode 100644 index 0000000000..79f8b72044 --- /dev/null +++ b/external/lua/luajit/src/src/lj_gc.c @@ -0,0 +1,839 @@ +/* +** Garbage collector. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lj_gc_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_func.h" +#include "lj_udata.h" +#include "lj_meta.h" +#include "lj_state.h" +#include "lj_frame.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#include "lj_cdata.h" +#endif +#include "lj_trace.h" +#include "lj_vm.h" + +#define GCSTEPSIZE 1024u +#define GCSWEEPMAX 40 +#define GCSWEEPCOST 10 +#define GCFINALIZECOST 100 + +/* Macros to set GCobj colors and flags. */ +#define white2gray(x) ((x)->gch.marked &= (uint8_t)~LJ_GC_WHITES) +#define gray2black(x) ((x)->gch.marked |= LJ_GC_BLACK) +#define isfinalized(u) ((u)->marked & LJ_GC_FINALIZED) + +/* -- Mark phase ---------------------------------------------------------- */ + +/* Mark a TValue (if needed). */ +#define gc_marktv(g, tv) \ + { lua_assert(!tvisgcv(tv) || (~itype(tv) == gcval(tv)->gch.gct)); \ + if (tviswhite(tv)) gc_mark(g, gcV(tv)); } + +/* Mark a GCobj (if needed). */ +#define gc_markobj(g, o) \ + { if (iswhite(obj2gco(o))) gc_mark(g, obj2gco(o)); } + +/* Mark a string object. */ +#define gc_mark_str(s) ((s)->marked &= (uint8_t)~LJ_GC_WHITES) + +/* Mark a white GCobj. */ +static void gc_mark(global_State *g, GCobj *o) +{ + int gct = o->gch.gct; + lua_assert(iswhite(o) && !isdead(g, o)); + white2gray(o); + if (LJ_UNLIKELY(gct == ~LJ_TUDATA)) { + GCtab *mt = tabref(gco2ud(o)->metatable); + gray2black(o); /* Userdata are never gray. */ + if (mt) gc_markobj(g, mt); + gc_markobj(g, tabref(gco2ud(o)->env)); + } else if (LJ_UNLIKELY(gct == ~LJ_TUPVAL)) { + GCupval *uv = gco2uv(o); + gc_marktv(g, uvval(uv)); + if (uv->closed) + gray2black(o); /* Closed upvalues are never gray. */ + } else if (gct != ~LJ_TSTR && gct != ~LJ_TCDATA) { + lua_assert(gct == ~LJ_TFUNC || gct == ~LJ_TTAB || + gct == ~LJ_TTHREAD || gct == ~LJ_TPROTO); + setgcrefr(o->gch.gclist, g->gc.gray); + setgcref(g->gc.gray, o); + } +} + +/* Mark GC roots. */ +static void gc_mark_gcroot(global_State *g) +{ + ptrdiff_t i; + for (i = 0; i < GCROOT_MAX; i++) + if (gcref(g->gcroot[i]) != NULL) + gc_markobj(g, gcref(g->gcroot[i])); +} + +/* Start a GC cycle and mark the root set. */ +static void gc_mark_start(global_State *g) +{ + setgcrefnull(g->gc.gray); + setgcrefnull(g->gc.grayagain); + setgcrefnull(g->gc.weak); + gc_markobj(g, mainthread(g)); + gc_markobj(g, tabref(mainthread(g)->env)); + gc_marktv(g, &g->registrytv); + gc_mark_gcroot(g); + g->gc.state = GCSpropagate; +} + +/* Mark open upvalues. */ +static void gc_mark_uv(global_State *g) +{ + GCupval *uv; + for (uv = uvnext(&g->uvhead); uv != &g->uvhead; uv = uvnext(uv)) { + lua_assert(uvprev(uvnext(uv)) == uv && uvnext(uvprev(uv)) == uv); + if (isgray(obj2gco(uv))) + gc_marktv(g, uvval(uv)); + } +} + +/* Mark userdata in mmudata list. */ +static void gc_mark_mmudata(global_State *g) +{ + GCobj *root = gcref(g->gc.mmudata); + GCobj *u = root; + if (u) { + do { + u = gcnext(u); + makewhite(g, u); /* Could be from previous GC. */ + gc_mark(g, u); + } while (u != root); + } +} + +/* Separate userdata objects to be finalized to mmudata list. */ +size_t lj_gc_separateudata(global_State *g, int all) +{ + size_t m = 0; + GCRef *p = &mainthread(g)->nextgc; + GCobj *o; + while ((o = gcref(*p)) != NULL) { + if (!(iswhite(o) || all) || isfinalized(gco2ud(o))) { + p = &o->gch.nextgc; /* Nothing to do. */ + } else if (!lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc)) { + markfinalized(o); /* Done, as there's no __gc metamethod. */ + p = &o->gch.nextgc; + } else { /* Otherwise move userdata to be finalized to mmudata list. */ + m += sizeudata(gco2ud(o)); + markfinalized(o); + *p = o->gch.nextgc; + if (gcref(g->gc.mmudata)) { /* Link to end of mmudata list. */ + GCobj *root = gcref(g->gc.mmudata); + setgcrefr(o->gch.nextgc, root->gch.nextgc); + setgcref(root->gch.nextgc, o); + setgcref(g->gc.mmudata, o); + } else { /* Create circular list. */ + setgcref(o->gch.nextgc, o); + setgcref(g->gc.mmudata, o); + } + } + } + return m; +} + +/* -- Propagation phase --------------------------------------------------- */ + +/* Traverse a table. */ +static int gc_traverse_tab(global_State *g, GCtab *t) +{ + int weak = 0; + cTValue *mode; + GCtab *mt = tabref(t->metatable); + if (mt) + gc_markobj(g, mt); + mode = lj_meta_fastg(g, mt, MM_mode); + if (mode && tvisstr(mode)) { /* Valid __mode field? */ + const char *modestr = strVdata(mode); + int c; + while ((c = *modestr++)) { + if (c == 'k') weak |= LJ_GC_WEAKKEY; + else if (c == 'v') weak |= LJ_GC_WEAKVAL; + else if (c == 'K') weak = (int)(~0u & ~LJ_GC_WEAKVAL); + } + if (weak > 0) { /* Weak tables are cleared in the atomic phase. */ + t->marked = (uint8_t)((t->marked & ~LJ_GC_WEAK) | weak); + setgcrefr(t->gclist, g->gc.weak); + setgcref(g->gc.weak, obj2gco(t)); + } + } + if (weak == LJ_GC_WEAK) /* Nothing to mark if both keys/values are weak. */ + return 1; + if (!(weak & LJ_GC_WEAKVAL)) { /* Mark array part. */ + MSize i, asize = t->asize; + for (i = 0; i < asize; i++) + gc_marktv(g, arrayslot(t, i)); + } + if (t->hmask > 0) { /* Mark hash part. */ + Node *node = noderef(t->node); + MSize i, hmask = t->hmask; + for (i = 0; i <= hmask; i++) { + Node *n = &node[i]; + if (!tvisnil(&n->val)) { /* Mark non-empty slot. */ + lua_assert(!tvisnil(&n->key)); + if (!(weak & LJ_GC_WEAKKEY)) gc_marktv(g, &n->key); + if (!(weak & LJ_GC_WEAKVAL)) gc_marktv(g, &n->val); + } + } + } + return weak; +} + +/* Traverse a function. */ +static void gc_traverse_func(global_State *g, GCfunc *fn) +{ + gc_markobj(g, tabref(fn->c.env)); + if (isluafunc(fn)) { + uint32_t i; + lua_assert(fn->l.nupvalues <= funcproto(fn)->sizeuv); + gc_markobj(g, funcproto(fn)); + for (i = 0; i < fn->l.nupvalues; i++) /* Mark Lua function upvalues. */ + gc_markobj(g, &gcref(fn->l.uvptr[i])->uv); + } else { + uint32_t i; + for (i = 0; i < fn->c.nupvalues; i++) /* Mark C function upvalues. */ + gc_marktv(g, &fn->c.upvalue[i]); + } +} + +#if LJ_HASJIT +/* Mark a trace. */ +static void gc_marktrace(global_State *g, TraceNo traceno) +{ + GCobj *o = obj2gco(traceref(G2J(g), traceno)); + lua_assert(traceno != G2J(g)->cur.traceno); + if (iswhite(o)) { + white2gray(o); + setgcrefr(o->gch.gclist, g->gc.gray); + setgcref(g->gc.gray, o); + } +} + +/* Traverse a trace. */ +static void gc_traverse_trace(global_State *g, GCtrace *T) +{ + IRRef ref; + if (T->traceno == 0) return; + for (ref = T->nk; ref < REF_TRUE; ref++) { + IRIns *ir = &T->ir[ref]; + if (ir->o == IR_KGC) + gc_markobj(g, ir_kgc(ir)); + } + if (T->link) gc_marktrace(g, T->link); + if (T->nextroot) gc_marktrace(g, T->nextroot); + if (T->nextside) gc_marktrace(g, T->nextside); + gc_markobj(g, gcref(T->startpt)); +} + +/* The current trace is a GC root while not anchored in the prototype (yet). */ +#define gc_traverse_curtrace(g) gc_traverse_trace(g, &G2J(g)->cur) +#else +#define gc_traverse_curtrace(g) UNUSED(g) +#endif + +/* Traverse a prototype. */ +static void gc_traverse_proto(global_State *g, GCproto *pt) +{ + ptrdiff_t i; + gc_mark_str(proto_chunkname(pt)); + for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) /* Mark collectable consts. */ + gc_markobj(g, proto_kgc(pt, i)); +#if LJ_HASJIT + if (pt->trace) gc_marktrace(g, pt->trace); +#endif +} + +/* Traverse the frame structure of a stack. */ +static MSize gc_traverse_frames(global_State *g, lua_State *th) +{ + TValue *frame, *top = th->top-1, *bot = tvref(th->stack); + /* Note: extra vararg frame not skipped, marks function twice (harmless). */ + for (frame = th->base-1; frame > bot; frame = frame_prev(frame)) { + GCfunc *fn = frame_func(frame); + TValue *ftop = frame; + if (isluafunc(fn)) ftop += funcproto(fn)->framesize; + if (ftop > top) top = ftop; + gc_markobj(g, fn); /* Need to mark hidden function (or L). */ + } + top++; /* Correct bias of -1 (frame == base-1). */ + if (top > tvref(th->maxstack)) top = tvref(th->maxstack); + return (MSize)(top - bot); /* Return minimum needed stack size. */ +} + +/* Traverse a thread object. */ +static void gc_traverse_thread(global_State *g, lua_State *th) +{ + TValue *o, *top = th->top; + for (o = tvref(th->stack)+1; o < top; o++) + gc_marktv(g, o); + if (g->gc.state == GCSatomic) { + top = tvref(th->stack) + th->stacksize; + for (; o < top; o++) /* Clear unmarked slots. */ + setnilV(o); + } + gc_markobj(g, tabref(th->env)); + lj_state_shrinkstack(th, gc_traverse_frames(g, th)); +} + +/* Propagate one gray object. Traverse it and turn it black. */ +static size_t propagatemark(global_State *g) +{ + GCobj *o = gcref(g->gc.gray); + int gct = o->gch.gct; + lua_assert(isgray(o)); + gray2black(o); + setgcrefr(g->gc.gray, o->gch.gclist); /* Remove from gray list. */ + if (LJ_LIKELY(gct == ~LJ_TTAB)) { + GCtab *t = gco2tab(o); + if (gc_traverse_tab(g, t) > 0) + black2gray(o); /* Keep weak tables gray. */ + return sizeof(GCtab) + sizeof(TValue) * t->asize + + sizeof(Node) * (t->hmask + 1); + } else if (LJ_LIKELY(gct == ~LJ_TFUNC)) { + GCfunc *fn = gco2func(o); + gc_traverse_func(g, fn); + return isluafunc(fn) ? sizeLfunc((MSize)fn->l.nupvalues) : + sizeCfunc((MSize)fn->c.nupvalues); + } else if (LJ_LIKELY(gct == ~LJ_TPROTO)) { + GCproto *pt = gco2pt(o); + gc_traverse_proto(g, pt); + return pt->sizept; + } else if (LJ_LIKELY(gct == ~LJ_TTHREAD)) { + lua_State *th = gco2th(o); + setgcrefr(th->gclist, g->gc.grayagain); + setgcref(g->gc.grayagain, o); + black2gray(o); /* Threads are never black. */ + gc_traverse_thread(g, th); + return sizeof(lua_State) + sizeof(TValue) * th->stacksize; + } else { +#if LJ_HASJIT + GCtrace *T = gco2trace(o); + gc_traverse_trace(g, T); + return ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) + + T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry); +#else + lua_assert(0); + return 0; +#endif + } +} + +/* Propagate all gray objects. */ +static size_t gc_propagate_gray(global_State *g) +{ + size_t m = 0; + while (gcref(g->gc.gray) != NULL) + m += propagatemark(g); + return m; +} + +/* -- Sweep phase --------------------------------------------------------- */ + +/* Try to shrink some common data structures. */ +static void gc_shrink(global_State *g, lua_State *L) +{ + if (g->strnum <= (g->strmask >> 2) && g->strmask > LJ_MIN_STRTAB*2-1) + lj_str_resize(L, g->strmask >> 1); /* Shrink string table. */ + if (g->tmpbuf.sz > LJ_MIN_SBUF*2) + lj_str_resizebuf(L, &g->tmpbuf, g->tmpbuf.sz >> 1); /* Shrink temp buf. */ +} + +/* Type of GC free functions. */ +typedef void (LJ_FASTCALL *GCFreeFunc)(global_State *g, GCobj *o); + +/* GC free functions for LJ_TSTR .. LJ_TUDATA. ORDER LJ_T */ +static const GCFreeFunc gc_freefunc[] = { + (GCFreeFunc)lj_str_free, + (GCFreeFunc)lj_func_freeuv, + (GCFreeFunc)lj_state_free, + (GCFreeFunc)lj_func_freeproto, + (GCFreeFunc)lj_func_free, +#if LJ_HASJIT + (GCFreeFunc)lj_trace_free, +#else + (GCFreeFunc)0, +#endif +#if LJ_HASFFI + (GCFreeFunc)lj_cdata_free, +#else + (GCFreeFunc)0, +#endif + (GCFreeFunc)lj_tab_free, + (GCFreeFunc)lj_udata_free +}; + +/* Full sweep of a GC list. */ +#define gc_fullsweep(g, p) gc_sweep(g, (p), LJ_MAX_MEM) + +/* Partial sweep of a GC list. */ +static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim) +{ + /* Mask with other white and LJ_GC_FIXED. Or LJ_GC_SFIXED on shutdown. */ + int ow = otherwhite(g); + GCobj *o; + while ((o = gcref(*p)) != NULL && lim-- > 0) { + if (o->gch.gct == ~LJ_TTHREAD) /* Need to sweep open upvalues, too. */ + gc_fullsweep(g, &gco2th(o)->openupval); + if (((o->gch.marked ^ LJ_GC_WHITES) & ow)) { /* Black or current white? */ + lua_assert(!isdead(g, o) || (o->gch.marked & LJ_GC_FIXED)); + makewhite(g, o); /* Value is alive, change to the current white. */ + p = &o->gch.nextgc; + } else { /* Otherwise value is dead, free it. */ + lua_assert(isdead(g, o) || ow == LJ_GC_SFIXED); + setgcrefr(*p, o->gch.nextgc); + if (o == gcref(g->gc.root)) + setgcrefr(g->gc.root, o->gch.nextgc); /* Adjust list anchor. */ + gc_freefunc[o->gch.gct - ~LJ_TSTR](g, o); + } + } + return p; +} + +/* Check whether we can clear a key or a value slot from a table. */ +static int gc_mayclear(cTValue *o, int val) +{ + if (tvisgcv(o)) { /* Only collectable objects can be weak references. */ + if (tvisstr(o)) { /* But strings cannot be used as weak references. */ + gc_mark_str(strV(o)); /* And need to be marked. */ + return 0; + } + if (iswhite(gcV(o))) + return 1; /* Object is about to be collected. */ + if (tvisudata(o) && val && isfinalized(udataV(o))) + return 1; /* Finalized userdata is dropped only from values. */ + } + return 0; /* Cannot clear. */ +} + +/* Clear collected entries from weak tables. */ +static void gc_clearweak(GCobj *o) +{ + while (o) { + GCtab *t = gco2tab(o); + lua_assert((t->marked & LJ_GC_WEAK)); + if ((t->marked & LJ_GC_WEAKVAL)) { + MSize i, asize = t->asize; + for (i = 0; i < asize; i++) { + /* Clear array slot when value is about to be collected. */ + TValue *tv = arrayslot(t, i); + if (gc_mayclear(tv, 1)) + setnilV(tv); + } + } + if (t->hmask > 0) { + Node *node = noderef(t->node); + MSize i, hmask = t->hmask; + for (i = 0; i <= hmask; i++) { + Node *n = &node[i]; + /* Clear hash slot when key or value is about to be collected. */ + if (!tvisnil(&n->val) && (gc_mayclear(&n->key, 0) || + gc_mayclear(&n->val, 1))) + setnilV(&n->val); + } + } + o = gcref(t->gclist); + } +} + +/* Call a userdata or cdata finalizer. */ +static void gc_call_finalizer(global_State *g, lua_State *L, + cTValue *mo, GCobj *o) +{ + /* Save and restore lots of state around the __gc callback. */ + uint8_t oldh = hook_save(g); + MSize oldt = g->gc.threshold; + int errcode; + TValue *top; + lj_trace_abort(g); + top = L->top; + L->top = top+2; + hook_entergc(g); /* Disable hooks and new traces during __gc. */ + g->gc.threshold = LJ_MAX_MEM; /* Prevent GC steps. */ + copyTV(L, top, mo); + setgcV(L, top+1, o, ~o->gch.gct); + errcode = lj_vm_pcall(L, top+1, 1+0, -1); /* Stack: |mo|o| -> | */ + hook_restore(g, oldh); + g->gc.threshold = oldt; /* Restore GC threshold. */ + if (errcode) + lj_err_throw(L, errcode); /* Propagate errors. */ +} + +/* Finalize one userdata or cdata object from the mmudata list. */ +static void gc_finalize(lua_State *L) +{ + global_State *g = G(L); + GCobj *o = gcnext(gcref(g->gc.mmudata)); + cTValue *mo; + lua_assert(gcref(g->jit_L) == NULL); /* Must not be called on trace. */ + /* Unchain from list of userdata to be finalized. */ + if (o == gcref(g->gc.mmudata)) + setgcrefnull(g->gc.mmudata); + else + setgcrefr(gcref(g->gc.mmudata)->gch.nextgc, o->gch.nextgc); +#if LJ_HASFFI + if (o->gch.gct == ~LJ_TCDATA) { + TValue tmp, *tv; + /* Add cdata back to the GC list and make it white. */ + setgcrefr(o->gch.nextgc, g->gc.root); + setgcref(g->gc.root, o); + makewhite(g, o); + o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN; + /* Resolve finalizer. */ + setcdataV(L, &tmp, gco2cd(o)); + tv = lj_tab_set(L, ctype_ctsG(g)->finalizer, &tmp); + if (!tvisnil(tv)) { + copyTV(L, &tmp, tv); + setnilV(tv); /* Clear entry in finalizer table. */ + gc_call_finalizer(g, L, &tmp, o); + } + return; + } +#endif + /* Add userdata back to the main userdata list and make it white. */ + setgcrefr(o->gch.nextgc, mainthread(g)->nextgc); + setgcref(mainthread(g)->nextgc, o); + makewhite(g, o); + /* Resolve the __gc metamethod. */ + mo = lj_meta_fastg(g, tabref(gco2ud(o)->metatable), MM_gc); + if (mo) + gc_call_finalizer(g, L, mo, o); +} + +/* Finalize all userdata objects from mmudata list. */ +void lj_gc_finalize_udata(lua_State *L) +{ + while (gcref(G(L)->gc.mmudata) != NULL) + gc_finalize(L); +} + +#if LJ_HASFFI +/* Finalize all cdata objects from finalizer table. */ +void lj_gc_finalize_cdata(lua_State *L) +{ + global_State *g = G(L); + CTState *cts = ctype_ctsG(g); + if (cts) { + GCtab *t = cts->finalizer; + Node *node = noderef(t->node); + ptrdiff_t i; + setgcrefnull(t->metatable); /* Mark finalizer table as disabled. */ + for (i = (ptrdiff_t)t->hmask; i >= 0; i--) + if (!tvisnil(&node[i].val) && tviscdata(&node[i].key)) { + GCobj *o = gcV(&node[i].key); + TValue tmp; + makewhite(g, o); + o->gch.marked &= (uint8_t)~LJ_GC_CDATA_FIN; + copyTV(L, &tmp, &node[i].val); + setnilV(&node[i].val); + gc_call_finalizer(g, L, &tmp, o); + } + } +} +#endif + +/* Free all remaining GC objects. */ +void lj_gc_freeall(global_State *g) +{ + MSize i, strmask; + /* Free everything, except super-fixed objects (the main thread). */ + g->gc.currentwhite = LJ_GC_WHITES | LJ_GC_SFIXED; + gc_fullsweep(g, &g->gc.root); + strmask = g->strmask; + for (i = 0; i <= strmask; i++) /* Free all string hash chains. */ + gc_fullsweep(g, &g->strhash[i]); +} + +/* -- Collector ----------------------------------------------------------- */ + +/* Atomic part of the GC cycle, transitioning from mark to sweep phase. */ +static void atomic(global_State *g, lua_State *L) +{ + size_t udsize; + + gc_mark_uv(g); /* Need to remark open upvalues (the thread may be dead). */ + gc_propagate_gray(g); /* Propagate any left-overs. */ + + setgcrefr(g->gc.gray, g->gc.weak); /* Empty the list of weak tables. */ + setgcrefnull(g->gc.weak); + lua_assert(!iswhite(obj2gco(mainthread(g)))); + gc_markobj(g, L); /* Mark running thread. */ + gc_traverse_curtrace(g); /* Traverse current trace. */ + gc_mark_gcroot(g); /* Mark GC roots (again). */ + gc_propagate_gray(g); /* Propagate all of the above. */ + + setgcrefr(g->gc.gray, g->gc.grayagain); /* Empty the 2nd chance list. */ + setgcrefnull(g->gc.grayagain); + gc_propagate_gray(g); /* Propagate it. */ + + udsize = lj_gc_separateudata(g, 0); /* Separate userdata to be finalized. */ + gc_mark_mmudata(g); /* Mark them. */ + udsize += gc_propagate_gray(g); /* And propagate the marks. */ + + /* All marking done, clear weak tables. */ + gc_clearweak(gcref(g->gc.weak)); + + /* Prepare for sweep phase. */ + g->gc.currentwhite = (uint8_t)otherwhite(g); /* Flip current white. */ + g->strempty.marked = g->gc.currentwhite; + setmref(g->gc.sweep, &g->gc.root); + g->gc.estimate = g->gc.total - (MSize)udsize; /* Initial estimate. */ +} + +/* GC state machine. Returns a cost estimate for each step performed. */ +static size_t gc_onestep(lua_State *L) +{ + global_State *g = G(L); + switch (g->gc.state) { + case GCSpause: + gc_mark_start(g); /* Start a new GC cycle by marking all GC roots. */ + return 0; + case GCSpropagate: + if (gcref(g->gc.gray) != NULL) + return propagatemark(g); /* Propagate one gray object. */ + g->gc.state = GCSatomic; /* End of mark phase. */ + return 0; + case GCSatomic: + if (gcref(g->jit_L)) /* Don't run atomic phase on trace. */ + return LJ_MAX_MEM; + atomic(g, L); + g->gc.state = GCSsweepstring; /* Start of sweep phase. */ + g->gc.sweepstr = 0; + return 0; + case GCSsweepstring: { + MSize old = g->gc.total; + gc_fullsweep(g, &g->strhash[g->gc.sweepstr++]); /* Sweep one chain. */ + if (g->gc.sweepstr > g->strmask) + g->gc.state = GCSsweep; /* All string hash chains sweeped. */ + lua_assert(old >= g->gc.total); + g->gc.estimate -= old - g->gc.total; + return GCSWEEPCOST; + } + case GCSsweep: { + MSize old = g->gc.total; + setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX)); + if (gcref(*mref(g->gc.sweep, GCRef)) == NULL) { + gc_shrink(g, L); + if (gcref(g->gc.mmudata)) { /* Need any finalizations? */ + g->gc.state = GCSfinalize; + } else { /* Otherwise skip this phase to help the JIT. */ + g->gc.state = GCSpause; /* End of GC cycle. */ + g->gc.debt = 0; + } + } + lua_assert(old >= g->gc.total); + g->gc.estimate -= old - g->gc.total; + return GCSWEEPMAX*GCSWEEPCOST; + } + case GCSfinalize: + if (gcref(g->gc.mmudata) != NULL) { + if (gcref(g->jit_L)) /* Don't call finalizers on trace. */ + return LJ_MAX_MEM; + gc_finalize(L); /* Finalize one userdata object. */ + if (g->gc.estimate > GCFINALIZECOST) + g->gc.estimate -= GCFINALIZECOST; + return GCFINALIZECOST; + } + g->gc.state = GCSpause; /* End of GC cycle. */ + g->gc.debt = 0; + return 0; + default: + lua_assert(0); + return 0; + } +} + +/* Perform a limited amount of incremental GC steps. */ +int LJ_FASTCALL lj_gc_step(lua_State *L) +{ + global_State *g = G(L); + MSize lim; + int32_t ostate = g->vmstate; + setvmstate(g, GC); + lim = (GCSTEPSIZE/100) * g->gc.stepmul; + if (lim == 0) + lim = LJ_MAX_MEM; + g->gc.debt += g->gc.total - g->gc.threshold; + do { + lim -= (MSize)gc_onestep(L); + if (g->gc.state == GCSpause) { + g->gc.threshold = (g->gc.estimate/100) * g->gc.pause; + g->vmstate = ostate; + return 1; /* Finished a GC cycle. */ + } + } while ((int32_t)lim > 0); + if (g->gc.debt < GCSTEPSIZE) { + g->gc.threshold = g->gc.total + GCSTEPSIZE; + } else { + g->gc.debt -= GCSTEPSIZE; + g->gc.threshold = g->gc.total; + } + g->vmstate = ostate; + return 0; +} + +/* Ditto, but fix the stack top first. */ +void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L) +{ + if (curr_funcisL(L)) L->top = curr_topL(L); + lj_gc_step(L); +} + +#if LJ_HASJIT +/* Perform multiple GC steps. Called from JIT-compiled code. */ +int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps) +{ + lua_State *L = gco2th(gcref(g->jit_L)); + L->base = mref(G(L)->jit_base, TValue); + L->top = curr_topL(L); + while (steps-- > 0 && lj_gc_step(L) == 0) + ; + /* Return 1 to force a trace exit. */ + return (G(L)->gc.state == GCSatomic || G(L)->gc.state == GCSfinalize); +} +#endif + +/* Perform a full GC cycle. */ +void lj_gc_fullgc(lua_State *L) +{ + global_State *g = G(L); + int32_t ostate = g->vmstate; + setvmstate(g, GC); + if (g->gc.state <= GCSatomic) { /* Caught somewhere in the middle. */ + setmref(g->gc.sweep, &g->gc.root); /* Sweep everything (preserving it). */ + setgcrefnull(g->gc.gray); /* Reset lists from partial propagation. */ + setgcrefnull(g->gc.grayagain); + setgcrefnull(g->gc.weak); + g->gc.state = GCSsweepstring; /* Fast forward to the sweep phase. */ + g->gc.sweepstr = 0; + } + while (g->gc.state == GCSsweepstring || g->gc.state == GCSsweep) + gc_onestep(L); /* Finish sweep. */ + lua_assert(g->gc.state == GCSfinalize || g->gc.state == GCSpause); + /* Now perform a full GC. */ + g->gc.state = GCSpause; + do { gc_onestep(L); } while (g->gc.state != GCSpause); + g->gc.threshold = (g->gc.estimate/100) * g->gc.pause; + g->vmstate = ostate; +} + +/* -- Write barriers ------------------------------------------------------ */ + +/* Move the GC propagation frontier forward. */ +void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v) +{ + lua_assert(isblack(o) && iswhite(v) && !isdead(g, v) && !isdead(g, o)); + lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); + lua_assert(o->gch.gct != ~LJ_TTAB); + /* Preserve invariant during propagation. Otherwise it doesn't matter. */ + if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) + gc_mark(g, v); /* Move frontier forward. */ + else + makewhite(g, o); /* Make it white to avoid the following barrier. */ +} + +/* Specialized barrier for closed upvalue. Pass &uv->tv. */ +void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv) +{ +#define TV2MARKED(x) \ + (*((uint8_t *)(x) - offsetof(GCupval, tv) + offsetof(GCupval, marked))) + if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) + gc_mark(g, gcV(tv)); + else + TV2MARKED(tv) = (TV2MARKED(tv) & (uint8_t)~LJ_GC_COLORS) | curwhite(g); +#undef TV2MARKED +} + +/* Close upvalue. Also needs a write barrier. */ +void lj_gc_closeuv(global_State *g, GCupval *uv) +{ + GCobj *o = obj2gco(uv); + /* Copy stack slot to upvalue itself and point to the copy. */ + copyTV(mainthread(g), &uv->tv, uvval(uv)); + setmref(uv->v, &uv->tv); + uv->closed = 1; + setgcrefr(o->gch.nextgc, g->gc.root); + setgcref(g->gc.root, o); + if (isgray(o)) { /* A closed upvalue is never gray, so fix this. */ + if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) { + gray2black(o); /* Make it black and preserve invariant. */ + if (tviswhite(&uv->tv)) + lj_gc_barrierf(g, o, gcV(&uv->tv)); + } else { + makewhite(g, o); /* Make it white, i.e. sweep the upvalue. */ + lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); + } + } +} + +#if LJ_HASJIT +/* Mark a trace if it's saved during the propagation phase. */ +void lj_gc_barriertrace(global_State *g, uint32_t traceno) +{ + if (g->gc.state == GCSpropagate || g->gc.state == GCSatomic) + gc_marktrace(g, traceno); +} +#endif + +/* -- Allocator ----------------------------------------------------------- */ + +/* Call pluggable memory allocator to allocate or resize a fragment. */ +void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz) +{ + global_State *g = G(L); + lua_assert((osz == 0) == (p == NULL)); + p = g->allocf(g->allocd, p, osz, nsz); + if (p == NULL && nsz > 0) + lj_err_mem(L); + lua_assert((nsz == 0) == (p == NULL)); + lua_assert(checkptr32(p)); + g->gc.total = (g->gc.total - osz) + nsz; + return p; +} + +/* Allocate new GC object and link it to the root set. */ +void * LJ_FASTCALL lj_mem_newgco(lua_State *L, MSize size) +{ + global_State *g = G(L); + GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size); + if (o == NULL) + lj_err_mem(L); + lua_assert(checkptr32(o)); + g->gc.total += size; + setgcrefr(o->gch.nextgc, g->gc.root); + setgcref(g->gc.root, o); + newwhite(g, o); + return o; +} + +/* Resize growable vector. */ +void *lj_mem_grow(lua_State *L, void *p, MSize *szp, MSize lim, MSize esz) +{ + MSize sz = (*szp) << 1; + if (sz < LJ_MIN_VECSZ) + sz = LJ_MIN_VECSZ; + if (sz > lim) + sz = lim; + p = lj_mem_realloc(L, p, (*szp)*esz, sz*esz); + *szp = sz; + return p; +} + diff --git a/external/lua/luajit/src/src/lj_gc.h b/external/lua/luajit/src/src/lj_gc.h new file mode 100644 index 0000000000..22f7fea54c --- /dev/null +++ b/external/lua/luajit/src/src/lj_gc.h @@ -0,0 +1,134 @@ +/* +** Garbage collector. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_GC_H +#define _LJ_GC_H + +#include "lj_obj.h" + +/* Garbage collector states. Order matters. */ +enum { + GCSpause, GCSpropagate, GCSatomic, GCSsweepstring, GCSsweep, GCSfinalize +}; + +/* Bitmasks for marked field of GCobj. */ +#define LJ_GC_WHITE0 0x01 +#define LJ_GC_WHITE1 0x02 +#define LJ_GC_BLACK 0x04 +#define LJ_GC_FINALIZED 0x08 +#define LJ_GC_WEAKKEY 0x08 +#define LJ_GC_WEAKVAL 0x10 +#define LJ_GC_CDATA_FIN 0x10 +#define LJ_GC_FIXED 0x20 +#define LJ_GC_SFIXED 0x40 + +#define LJ_GC_WHITES (LJ_GC_WHITE0 | LJ_GC_WHITE1) +#define LJ_GC_COLORS (LJ_GC_WHITES | LJ_GC_BLACK) +#define LJ_GC_WEAK (LJ_GC_WEAKKEY | LJ_GC_WEAKVAL) + +/* Macros to test and set GCobj colors. */ +#define iswhite(x) ((x)->gch.marked & LJ_GC_WHITES) +#define isblack(x) ((x)->gch.marked & LJ_GC_BLACK) +#define isgray(x) (!((x)->gch.marked & (LJ_GC_BLACK|LJ_GC_WHITES))) +#define tviswhite(x) (tvisgcv(x) && iswhite(gcV(x))) +#define otherwhite(g) (g->gc.currentwhite ^ LJ_GC_WHITES) +#define isdead(g, v) ((v)->gch.marked & otherwhite(g) & LJ_GC_WHITES) + +#define curwhite(g) ((g)->gc.currentwhite & LJ_GC_WHITES) +#define newwhite(g, x) (obj2gco(x)->gch.marked = (uint8_t)curwhite(g)) +#define makewhite(g, x) \ + ((x)->gch.marked = ((x)->gch.marked & (uint8_t)~LJ_GC_COLORS) | curwhite(g)) +#define flipwhite(x) ((x)->gch.marked ^= LJ_GC_WHITES) +#define black2gray(x) ((x)->gch.marked &= (uint8_t)~LJ_GC_BLACK) +#define fixstring(s) ((s)->marked |= LJ_GC_FIXED) +#define markfinalized(x) ((x)->gch.marked |= LJ_GC_FINALIZED) + +/* Collector. */ +LJ_FUNC size_t lj_gc_separateudata(global_State *g, int all); +LJ_FUNC void lj_gc_finalize_udata(lua_State *L); +#if LJ_HASFFI +LJ_FUNC void lj_gc_finalize_cdata(lua_State *L); +#else +#define lj_gc_finalize_cdata(L) UNUSED(L) +#endif +LJ_FUNC void lj_gc_freeall(global_State *g); +LJ_FUNCA int LJ_FASTCALL lj_gc_step(lua_State *L); +LJ_FUNCA void LJ_FASTCALL lj_gc_step_fixtop(lua_State *L); +#if LJ_HASJIT +LJ_FUNC int LJ_FASTCALL lj_gc_step_jit(global_State *g, MSize steps); +#endif +LJ_FUNC void lj_gc_fullgc(lua_State *L); + +/* GC check: drive collector forward if the GC threshold has been reached. */ +#define lj_gc_check(L) \ + { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \ + lj_gc_step(L); } +#define lj_gc_check_fixtop(L) \ + { if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) \ + lj_gc_step_fixtop(L); } + +/* Write barriers. */ +LJ_FUNC void lj_gc_barrierf(global_State *g, GCobj *o, GCobj *v); +LJ_FUNCA void LJ_FASTCALL lj_gc_barrieruv(global_State *g, TValue *tv); +LJ_FUNC void lj_gc_closeuv(global_State *g, GCupval *uv); +#if LJ_HASJIT +LJ_FUNC void lj_gc_barriertrace(global_State *g, uint32_t traceno); +#endif + +/* Move the GC propagation frontier back for tables (make it gray again). */ +static LJ_AINLINE void lj_gc_barrierback(global_State *g, GCtab *t) +{ + GCobj *o = obj2gco(t); + lua_assert(isblack(o) && !isdead(g, o)); + lua_assert(g->gc.state != GCSfinalize && g->gc.state != GCSpause); + black2gray(o); + setgcrefr(t->gclist, g->gc.grayagain); + setgcref(g->gc.grayagain, o); +} + +/* Barrier for stores to table objects. TValue and GCobj variant. */ +#define lj_gc_anybarriert(L, t) \ + { if (LJ_UNLIKELY(isblack(obj2gco(t)))) lj_gc_barrierback(G(L), (t)); } +#define lj_gc_barriert(L, t, tv) \ + { if (tviswhite(tv) && isblack(obj2gco(t))) \ + lj_gc_barrierback(G(L), (t)); } +#define lj_gc_objbarriert(L, t, o) \ + { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) \ + lj_gc_barrierback(G(L), (t)); } + +/* Barrier for stores to any other object. TValue and GCobj variant. */ +#define lj_gc_barrier(L, p, tv) \ + { if (tviswhite(tv) && isblack(obj2gco(p))) \ + lj_gc_barrierf(G(L), obj2gco(p), gcV(tv)); } +#define lj_gc_objbarrier(L, p, o) \ + { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ + lj_gc_barrierf(G(L), obj2gco(p), obj2gco(o)); } + +/* Allocator. */ +LJ_FUNC void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz); +LJ_FUNC void * LJ_FASTCALL lj_mem_newgco(lua_State *L, MSize size); +LJ_FUNC void *lj_mem_grow(lua_State *L, void *p, + MSize *szp, MSize lim, MSize esz); + +#define lj_mem_new(L, s) lj_mem_realloc(L, NULL, 0, (s)) + +static LJ_AINLINE void lj_mem_free(global_State *g, void *p, size_t osize) +{ + g->gc.total -= (MSize)osize; + g->allocf(g->allocd, p, osize, 0); +} + +#define lj_mem_newvec(L, n, t) ((t *)lj_mem_new(L, (MSize)((n)*sizeof(t)))) +#define lj_mem_reallocvec(L, p, on, n, t) \ + ((p) = (t *)lj_mem_realloc(L, p, (on)*sizeof(t), (MSize)((n)*sizeof(t)))) +#define lj_mem_growvec(L, p, n, m, t) \ + ((p) = (t *)lj_mem_grow(L, (p), &(n), (m), (MSize)sizeof(t))) +#define lj_mem_freevec(g, p, n, t) lj_mem_free(g, (p), (n)*sizeof(t)) + +#define lj_mem_newobj(L, t) ((t *)lj_mem_newgco(L, sizeof(t))) +#define lj_mem_newt(L, s, t) ((t *)lj_mem_new(L, (s))) +#define lj_mem_freet(g, p) lj_mem_free(g, (p), sizeof(*(p))) + +#endif diff --git a/external/lua/luajit/src/src/lj_gdbjit.c b/external/lua/luajit/src/src/lj_gdbjit.c new file mode 100644 index 0000000000..284195a15d --- /dev/null +++ b/external/lua/luajit/src/src/lj_gdbjit.c @@ -0,0 +1,793 @@ +/* +** Client for the GDB JIT API. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_gdbjit_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_debug.h" +#include "lj_frame.h" +#include "lj_jit.h" +#include "lj_dispatch.h" + +/* This is not compiled in by default. +** Enable with -DLUAJIT_USE_GDBJIT in the Makefile and recompile everything. +*/ +#ifdef LUAJIT_USE_GDBJIT + +/* The GDB JIT API allows JIT compilers to pass debug information about +** JIT-compiled code back to GDB. You need at least GDB 7.0 or higher +** to see it in action. +** +** This is a passive API, so it works even when not running under GDB +** or when attaching to an already running process. Alas, this implies +** enabling it always has a non-negligible overhead -- do not use in +** release mode! +** +** The LuaJIT GDB JIT client is rather minimal at the moment. It gives +** each trace a symbol name and adds a source location and frame unwind +** information. Obviously LuaJIT itself and any embedding C application +** should be compiled with debug symbols, too (see the Makefile). +** +** Traces are named TRACE_1, TRACE_2, ... these correspond to the trace +** numbers from -jv or -jdump. Use "break TRACE_1" or "tbreak TRACE_1" etc. +** to set breakpoints on specific traces (even ahead of their creation). +** +** The source location for each trace allows listing the corresponding +** source lines with the GDB command "list" (but only if the Lua source +** has been loaded from a file). Currently this is always set to the +** location where the trace has been started. +** +** Frame unwind information can be inspected with the GDB command +** "info frame". This also allows proper backtraces across JIT-compiled +** code with the GDB command "bt". +** +** You probably want to add the following settings to a .gdbinit file +** (or add them to ~/.gdbinit): +** set disassembly-flavor intel +** set breakpoint pending on +** +** Here's a sample GDB session: +** ------------------------------------------------------------------------ + +$ cat >x.lua +for outer=1,100 do + for inner=1,100 do end +end +^D + +$ luajit -jv x.lua +[TRACE 1 x.lua:2] +[TRACE 2 (1/3) x.lua:1 -> 1] + +$ gdb --quiet --args luajit x.lua +(gdb) tbreak TRACE_1 +Function "TRACE_1" not defined. +Temporary breakpoint 1 (TRACE_1) pending. +(gdb) run +Starting program: luajit x.lua + +Temporary breakpoint 1, TRACE_1 () at x.lua:2 +2 for inner=1,100 do end +(gdb) list +1 for outer=1,100 do +2 for inner=1,100 do end +3 end +(gdb) bt +#0 TRACE_1 () at x.lua:2 +#1 0x08053690 in lua_pcall [...] +[...] +#7 0x0806ff90 in main [...] +(gdb) disass TRACE_1 +Dump of assembler code for function TRACE_1: +0xf7fd9fba : mov DWORD PTR ds:0xf7e0e2a0,0x1 +0xf7fd9fc4 : movsd xmm7,QWORD PTR [edx+0x20] +[...] +0xf7fd9ff8 : jmp 0xf7fd2014 +End of assembler dump. +(gdb) tbreak TRACE_2 +Function "TRACE_2" not defined. +Temporary breakpoint 2 (TRACE_2) pending. +(gdb) cont +Continuing. + +Temporary breakpoint 2, TRACE_2 () at x.lua:1 +1 for outer=1,100 do +(gdb) info frame +Stack level 0, frame at 0xffffd7c0: + eip = 0xf7fd9f60 in TRACE_2 (x.lua:1); saved eip 0x8053690 + called by frame at 0xffffd7e0 + source language unknown. + Arglist at 0xffffd78c, args: + Locals at 0xffffd78c, Previous frame's sp is 0xffffd7c0 + Saved registers: + ebx at 0xffffd7ac, ebp at 0xffffd7b8, esi at 0xffffd7b0, edi at 0xffffd7b4, + eip at 0xffffd7bc +(gdb) + +** ------------------------------------------------------------------------ +*/ + +/* -- GDB JIT API --------------------------------------------------------- */ + +/* GDB JIT actions. */ +enum { + GDBJIT_NOACTION = 0, + GDBJIT_REGISTER, + GDBJIT_UNREGISTER +}; + +/* GDB JIT entry. */ +typedef struct GDBJITentry { + struct GDBJITentry *next_entry; + struct GDBJITentry *prev_entry; + const char *symfile_addr; + uint64_t symfile_size; +} GDBJITentry; + +/* GDB JIT descriptor. */ +typedef struct GDBJITdesc { + uint32_t version; + uint32_t action_flag; + GDBJITentry *relevant_entry; + GDBJITentry *first_entry; +} GDBJITdesc; + +GDBJITdesc __jit_debug_descriptor = { + 1, GDBJIT_NOACTION, NULL, NULL +}; + +/* GDB sets a breakpoint at this function. */ +void LJ_NOINLINE __jit_debug_register_code() +{ + __asm__ __volatile__(""); +}; + +/* -- In-memory ELF object definitions ------------------------------------ */ + +/* ELF definitions. */ +typedef struct ELFheader { + uint8_t emagic[4]; + uint8_t eclass; + uint8_t eendian; + uint8_t eversion; + uint8_t eosabi; + uint8_t eabiversion; + uint8_t epad[7]; + uint16_t type; + uint16_t machine; + uint32_t version; + uintptr_t entry; + uintptr_t phofs; + uintptr_t shofs; + uint32_t flags; + uint16_t ehsize; + uint16_t phentsize; + uint16_t phnum; + uint16_t shentsize; + uint16_t shnum; + uint16_t shstridx; +} ELFheader; + +typedef struct ELFsectheader { + uint32_t name; + uint32_t type; + uintptr_t flags; + uintptr_t addr; + uintptr_t ofs; + uintptr_t size; + uint32_t link; + uint32_t info; + uintptr_t align; + uintptr_t entsize; +} ELFsectheader; + +#define ELFSECT_IDX_ABS 0xfff1 + +enum { + ELFSECT_TYPE_PROGBITS = 1, + ELFSECT_TYPE_SYMTAB = 2, + ELFSECT_TYPE_STRTAB = 3, + ELFSECT_TYPE_NOBITS = 8 +}; + +#define ELFSECT_FLAGS_WRITE 1 +#define ELFSECT_FLAGS_ALLOC 2 +#define ELFSECT_FLAGS_EXEC 4 + +typedef struct ELFsymbol { +#if LJ_64 + uint32_t name; + uint8_t info; + uint8_t other; + uint16_t sectidx; + uintptr_t value; + uint64_t size; +#else + uint32_t name; + uintptr_t value; + uint32_t size; + uint8_t info; + uint8_t other; + uint16_t sectidx; +#endif +} ELFsymbol; + +enum { + ELFSYM_TYPE_FUNC = 2, + ELFSYM_TYPE_FILE = 4, + ELFSYM_BIND_LOCAL = 0 << 4, + ELFSYM_BIND_GLOBAL = 1 << 4, +}; + +/* DWARF definitions. */ +#define DW_CIE_VERSION 1 + +enum { + DW_CFA_nop = 0x0, + DW_CFA_offset_extended = 0x5, + DW_CFA_def_cfa = 0xc, + DW_CFA_def_cfa_offset = 0xe, + DW_CFA_offset_extended_sf = 0x11, + DW_CFA_advance_loc = 0x40, + DW_CFA_offset = 0x80 +}; + +enum { + DW_EH_PE_udata4 = 3, + DW_EH_PE_textrel = 0x20 +}; + +enum { + DW_TAG_compile_unit = 0x11 +}; + +enum { + DW_children_no = 0, + DW_children_yes = 1 +}; + +enum { + DW_AT_name = 0x03, + DW_AT_stmt_list = 0x10, + DW_AT_low_pc = 0x11, + DW_AT_high_pc = 0x12 +}; + +enum { + DW_FORM_addr = 0x01, + DW_FORM_data4 = 0x06, + DW_FORM_string = 0x08 +}; + +enum { + DW_LNS_extended_op = 0, + DW_LNS_copy = 1, + DW_LNS_advance_pc = 2, + DW_LNS_advance_line = 3 +}; + +enum { + DW_LNE_end_sequence = 1, + DW_LNE_set_address = 2 +}; + +enum { +#if LJ_TARGET_X86 + DW_REG_AX, DW_REG_CX, DW_REG_DX, DW_REG_BX, + DW_REG_SP, DW_REG_BP, DW_REG_SI, DW_REG_DI, + DW_REG_RA, +#elif LJ_TARGET_X64 + /* Yes, the order is strange, but correct. */ + DW_REG_AX, DW_REG_DX, DW_REG_CX, DW_REG_BX, + DW_REG_SI, DW_REG_DI, DW_REG_BP, DW_REG_SP, + DW_REG_8, DW_REG_9, DW_REG_10, DW_REG_11, + DW_REG_12, DW_REG_13, DW_REG_14, DW_REG_15, + DW_REG_RA, +#elif LJ_TARGET_ARM + DW_REG_SP = 13, + DW_REG_RA = 14, +#elif LJ_TARGET_PPC + DW_REG_SP = 1, + DW_REG_RA = 65, + DW_REG_CR = 70, +#elif LJ_TARGET_MIPS + DW_REG_SP = 29, + DW_REG_RA = 31, +#else +#error "Unsupported target architecture" +#endif +}; + +/* Minimal list of sections for the in-memory ELF object. */ +enum { + GDBJIT_SECT_NULL, + GDBJIT_SECT_text, + GDBJIT_SECT_eh_frame, + GDBJIT_SECT_shstrtab, + GDBJIT_SECT_strtab, + GDBJIT_SECT_symtab, + GDBJIT_SECT_debug_info, + GDBJIT_SECT_debug_abbrev, + GDBJIT_SECT_debug_line, + GDBJIT_SECT__MAX +}; + +enum { + GDBJIT_SYM_UNDEF, + GDBJIT_SYM_FILE, + GDBJIT_SYM_FUNC, + GDBJIT_SYM__MAX +}; + +/* In-memory ELF object. */ +typedef struct GDBJITobj { + ELFheader hdr; /* ELF header. */ + ELFsectheader sect[GDBJIT_SECT__MAX]; /* ELF sections. */ + ELFsymbol sym[GDBJIT_SYM__MAX]; /* ELF symbol table. */ + uint8_t space[4096]; /* Space for various section data. */ +} GDBJITobj; + +/* Combined structure for GDB JIT entry and ELF object. */ +typedef struct GDBJITentryobj { + GDBJITentry entry; + size_t sz; + GDBJITobj obj; +} GDBJITentryobj; + +/* Template for in-memory ELF header. */ +static const ELFheader elfhdr_template = { + .emagic = { 0x7f, 'E', 'L', 'F' }, + .eclass = LJ_64 ? 2 : 1, + .eendian = LJ_ENDIAN_SELECT(1, 2), + .eversion = 1, +#if LJ_TARGET_LINUX + .eosabi = 0, /* Nope, it's not 3. */ +#elif defined(__FreeBSD__) + .eosabi = 9, +#elif defined(__NetBSD__) + .eosabi = 2, +#elif defined(__OpenBSD__) + .eosabi = 12, +#elif (defined(__sun__) && defined(__svr4__)) + .eosabi = 6, +#else + .eosabi = 0, +#endif + .eabiversion = 0, + .epad = { 0, 0, 0, 0, 0, 0, 0 }, + .type = 1, +#if LJ_TARGET_X86 + .machine = 3, +#elif LJ_TARGET_X64 + .machine = 62, +#elif LJ_TARGET_ARM + .machine = 40, +#elif LJ_TARGET_PPC + .machine = 20, +#elif LJ_TARGET_MIPS + .machine = 8, +#else +#error "Unsupported target architecture" +#endif + .version = 1, + .entry = 0, + .phofs = 0, + .shofs = offsetof(GDBJITobj, sect), + .flags = 0, + .ehsize = sizeof(ELFheader), + .phentsize = 0, + .phnum = 0, + .shentsize = sizeof(ELFsectheader), + .shnum = GDBJIT_SECT__MAX, + .shstridx = GDBJIT_SECT_shstrtab +}; + +/* -- In-memory ELF object generation ------------------------------------- */ + +/* Context for generating the ELF object for the GDB JIT API. */ +typedef struct GDBJITctx { + uint8_t *p; /* Pointer to next address in obj.space. */ + uint8_t *startp; /* Pointer to start address in obj.space. */ + GCtrace *T; /* Generate symbols for this trace. */ + uintptr_t mcaddr; /* Machine code address. */ + MSize szmcode; /* Size of machine code. */ + MSize spadjp; /* Stack adjustment for parent trace or interpreter. */ + MSize spadj; /* Stack adjustment for trace itself. */ + BCLine lineno; /* Starting line number. */ + const char *filename; /* Starting file name. */ + size_t objsize; /* Final size of ELF object. */ + GDBJITobj obj; /* In-memory ELF object. */ +} GDBJITctx; + +/* Add a zero-terminated string. */ +static uint32_t gdbjit_strz(GDBJITctx *ctx, const char *str) +{ + uint8_t *p = ctx->p; + uint32_t ofs = (uint32_t)(p - ctx->startp); + do { + *p++ = (uint8_t)*str; + } while (*str++); + ctx->p = p; + return ofs; +} + +/* Append a decimal number. */ +static void gdbjit_catnum(GDBJITctx *ctx, uint32_t n) +{ + if (n >= 10) { uint32_t m = n / 10; n = n % 10; gdbjit_catnum(ctx, m); } + *ctx->p++ = '0' + n; +} + +/* Add a ULEB128 value. */ +static void gdbjit_uleb128(GDBJITctx *ctx, uint32_t v) +{ + uint8_t *p = ctx->p; + for (; v >= 0x80; v >>= 7) + *p++ = (uint8_t)((v & 0x7f) | 0x80); + *p++ = (uint8_t)v; + ctx->p = p; +} + +/* Add a SLEB128 value. */ +static void gdbjit_sleb128(GDBJITctx *ctx, int32_t v) +{ + uint8_t *p = ctx->p; + for (; (uint32_t)(v+0x40) >= 0x80; v >>= 7) + *p++ = (uint8_t)((v & 0x7f) | 0x80); + *p++ = (uint8_t)(v & 0x7f); + ctx->p = p; +} + +/* Shortcuts to generate DWARF structures. */ +#define DB(x) (*p++ = (x)) +#define DI8(x) (*(int8_t *)p = (x), p++) +#define DU16(x) (*(uint16_t *)p = (x), p += 2) +#define DU32(x) (*(uint32_t *)p = (x), p += 4) +#define DADDR(x) (*(uintptr_t *)p = (x), p += sizeof(uintptr_t)) +#define DUV(x) (ctx->p = p, gdbjit_uleb128(ctx, (x)), p = ctx->p) +#define DSV(x) (ctx->p = p, gdbjit_sleb128(ctx, (x)), p = ctx->p) +#define DSTR(str) (ctx->p = p, gdbjit_strz(ctx, (str)), p = ctx->p) +#define DALIGNNOP(s) while ((uintptr_t)p & ((s)-1)) *p++ = DW_CFA_nop +#define DSECT(name, stmt) \ + { uint32_t *szp_##name = (uint32_t *)p; p += 4; stmt \ + *szp_##name = (uint32_t)((p-(uint8_t *)szp_##name)-4); } \ + +/* Initialize ELF section headers. */ +static void LJ_FASTCALL gdbjit_secthdr(GDBJITctx *ctx) +{ + ELFsectheader *sect; + + *ctx->p++ = '\0'; /* Empty string at start of string table. */ + +#define SECTDEF(id, tp, al) \ + sect = &ctx->obj.sect[GDBJIT_SECT_##id]; \ + sect->name = gdbjit_strz(ctx, "." #id); \ + sect->type = ELFSECT_TYPE_##tp; \ + sect->align = (al) + + SECTDEF(text, NOBITS, 16); + sect->flags = ELFSECT_FLAGS_ALLOC|ELFSECT_FLAGS_EXEC; + sect->addr = ctx->mcaddr; + sect->ofs = 0; + sect->size = ctx->szmcode; + + SECTDEF(eh_frame, PROGBITS, sizeof(uintptr_t)); + sect->flags = ELFSECT_FLAGS_ALLOC; + + SECTDEF(shstrtab, STRTAB, 1); + SECTDEF(strtab, STRTAB, 1); + + SECTDEF(symtab, SYMTAB, sizeof(uintptr_t)); + sect->ofs = offsetof(GDBJITobj, sym); + sect->size = sizeof(ctx->obj.sym); + sect->link = GDBJIT_SECT_strtab; + sect->entsize = sizeof(ELFsymbol); + sect->info = GDBJIT_SYM_FUNC; + + SECTDEF(debug_info, PROGBITS, 1); + SECTDEF(debug_abbrev, PROGBITS, 1); + SECTDEF(debug_line, PROGBITS, 1); + +#undef SECTDEF +} + +/* Initialize symbol table. */ +static void LJ_FASTCALL gdbjit_symtab(GDBJITctx *ctx) +{ + ELFsymbol *sym; + + *ctx->p++ = '\0'; /* Empty string at start of string table. */ + + sym = &ctx->obj.sym[GDBJIT_SYM_FILE]; + sym->name = gdbjit_strz(ctx, "JIT mcode"); + sym->sectidx = ELFSECT_IDX_ABS; + sym->info = ELFSYM_TYPE_FILE|ELFSYM_BIND_LOCAL; + + sym = &ctx->obj.sym[GDBJIT_SYM_FUNC]; + sym->name = gdbjit_strz(ctx, "TRACE_"); ctx->p--; + gdbjit_catnum(ctx, ctx->T->traceno); *ctx->p++ = '\0'; + sym->sectidx = GDBJIT_SECT_text; + sym->value = 0; + sym->size = ctx->szmcode; + sym->info = ELFSYM_TYPE_FUNC|ELFSYM_BIND_GLOBAL; +} + +/* Initialize .eh_frame section. */ +static void LJ_FASTCALL gdbjit_ehframe(GDBJITctx *ctx) +{ + uint8_t *p = ctx->p; + uint8_t *framep = p; + + /* Emit DWARF EH CIE. */ + DSECT(CIE, + DU32(0); /* Offset to CIE itself. */ + DB(DW_CIE_VERSION); + DSTR("zR"); /* Augmentation. */ + DUV(1); /* Code alignment factor. */ + DSV(-(int32_t)sizeof(uintptr_t)); /* Data alignment factor. */ + DB(DW_REG_RA); /* Return address register. */ + DB(1); DB(DW_EH_PE_textrel|DW_EH_PE_udata4); /* Augmentation data. */ + DB(DW_CFA_def_cfa); DUV(DW_REG_SP); DUV(sizeof(uintptr_t)); +#if LJ_TARGET_PPC + DB(DW_CFA_offset_extended_sf); DB(DW_REG_RA); DSV(-1); +#else + DB(DW_CFA_offset|DW_REG_RA); DUV(1); +#endif + DALIGNNOP(sizeof(uintptr_t)); + ) + + /* Emit DWARF EH FDE. */ + DSECT(FDE, + DU32((uint32_t)(p-framep)); /* Offset to CIE. */ + DU32(0); /* Machine code offset relative to .text. */ + DU32(ctx->szmcode); /* Machine code length. */ + DB(0); /* Augmentation data. */ + /* Registers saved in CFRAME. */ +#if LJ_TARGET_X86 + DB(DW_CFA_offset|DW_REG_BP); DUV(2); + DB(DW_CFA_offset|DW_REG_DI); DUV(3); + DB(DW_CFA_offset|DW_REG_SI); DUV(4); + DB(DW_CFA_offset|DW_REG_BX); DUV(5); +#elif LJ_TARGET_X64 + DB(DW_CFA_offset|DW_REG_BP); DUV(2); + DB(DW_CFA_offset|DW_REG_BX); DUV(3); + DB(DW_CFA_offset|DW_REG_15); DUV(4); + DB(DW_CFA_offset|DW_REG_14); DUV(5); + /* Extra registers saved for JIT-compiled code. */ + DB(DW_CFA_offset|DW_REG_13); DUV(9); + DB(DW_CFA_offset|DW_REG_12); DUV(10); +#elif LJ_TARGET_ARM + { + int i; + for (i = 11; i >= 4; i--) { DB(DW_CFA_offset|i); DUV(2+(11-i)); } + } +#elif LJ_TARGET_PPC + { + int i; + DB(DW_CFA_offset_extended); DB(DW_REG_CR); DUV(55); + for (i = 14; i <= 31; i++) { + DB(DW_CFA_offset|i); DUV(37+(31-i)); + DB(DW_CFA_offset|32|i); DUV(2+2*(31-i)); + } + } +#elif LJ_TARGET_MIPS + { + int i; + DB(DW_CFA_offset|30); DUV(2); + for (i = 23; i >= 16; i--) { DB(DW_CFA_offset|i); DUV(26-i); } + for (i = 30; i >= 20; i -= 2) { DB(DW_CFA_offset|32|i); DUV(42-i); } + } +#else +#error "Unsupported target architecture" +#endif + if (ctx->spadjp != ctx->spadj) { /* Parent/interpreter stack frame size. */ + DB(DW_CFA_def_cfa_offset); DUV(ctx->spadjp); + DB(DW_CFA_advance_loc|1); /* Only an approximation. */ + } + DB(DW_CFA_def_cfa_offset); DUV(ctx->spadj); /* Trace stack frame size. */ + DALIGNNOP(sizeof(uintptr_t)); + ) + + ctx->p = p; +} + +/* Initialize .debug_info section. */ +static void LJ_FASTCALL gdbjit_debuginfo(GDBJITctx *ctx) +{ + uint8_t *p = ctx->p; + + DSECT(info, + DU16(2); /* DWARF version. */ + DU32(0); /* Abbrev offset. */ + DB(sizeof(uintptr_t)); /* Pointer size. */ + + DUV(1); /* Abbrev #1: DW_TAG_compile_unit. */ + DSTR(ctx->filename); /* DW_AT_name. */ + DADDR(ctx->mcaddr); /* DW_AT_low_pc. */ + DADDR(ctx->mcaddr + ctx->szmcode); /* DW_AT_high_pc. */ + DU32(0); /* DW_AT_stmt_list. */ + ) + + ctx->p = p; +} + +/* Initialize .debug_abbrev section. */ +static void LJ_FASTCALL gdbjit_debugabbrev(GDBJITctx *ctx) +{ + uint8_t *p = ctx->p; + + /* Abbrev #1: DW_TAG_compile_unit. */ + DUV(1); DUV(DW_TAG_compile_unit); + DB(DW_children_no); + DUV(DW_AT_name); DUV(DW_FORM_string); + DUV(DW_AT_low_pc); DUV(DW_FORM_addr); + DUV(DW_AT_high_pc); DUV(DW_FORM_addr); + DUV(DW_AT_stmt_list); DUV(DW_FORM_data4); + DB(0); DB(0); + + ctx->p = p; +} + +#define DLNE(op, s) (DB(DW_LNS_extended_op), DUV(1+(s)), DB((op))) + +/* Initialize .debug_line section. */ +static void LJ_FASTCALL gdbjit_debugline(GDBJITctx *ctx) +{ + uint8_t *p = ctx->p; + + DSECT(line, + DU16(2); /* DWARF version. */ + DSECT(header, + DB(1); /* Minimum instruction length. */ + DB(1); /* is_stmt. */ + DI8(0); /* Line base for special opcodes. */ + DB(2); /* Line range for special opcodes. */ + DB(3+1); /* Opcode base at DW_LNS_advance_line+1. */ + DB(0); DB(1); DB(1); /* Standard opcode lengths. */ + /* Directory table. */ + DB(0); + /* File name table. */ + DSTR(ctx->filename); DUV(0); DUV(0); DUV(0); + DB(0); + ) + + DLNE(DW_LNE_set_address, sizeof(uintptr_t)); DADDR(ctx->mcaddr); + if (ctx->lineno) { + DB(DW_LNS_advance_line); DSV(ctx->lineno-1); + } + DB(DW_LNS_copy); + DB(DW_LNS_advance_pc); DUV(ctx->szmcode); + DLNE(DW_LNE_end_sequence, 0); + ) + + ctx->p = p; +} + +#undef DLNE + +/* Undef shortcuts. */ +#undef DB +#undef DI8 +#undef DU16 +#undef DU32 +#undef DADDR +#undef DUV +#undef DSV +#undef DSTR +#undef DALIGNNOP +#undef DSECT + +/* Type of a section initializer callback. */ +typedef void (LJ_FASTCALL *GDBJITinitf)(GDBJITctx *ctx); + +/* Call section initializer and set the section offset and size. */ +static void gdbjit_initsect(GDBJITctx *ctx, int sect, GDBJITinitf initf) +{ + ctx->startp = ctx->p; + ctx->obj.sect[sect].ofs = (uintptr_t)((char *)ctx->p - (char *)&ctx->obj); + initf(ctx); + ctx->obj.sect[sect].size = (uintptr_t)(ctx->p - ctx->startp); +} + +#define SECTALIGN(p, a) \ + ((p) = (uint8_t *)(((uintptr_t)(p) + ((a)-1)) & ~(uintptr_t)((a)-1))) + +/* Build in-memory ELF object. */ +static void gdbjit_buildobj(GDBJITctx *ctx) +{ + GDBJITobj *obj = &ctx->obj; + /* Fill in ELF header and clear structures. */ + memcpy(&obj->hdr, &elfhdr_template, sizeof(ELFheader)); + memset(&obj->sect, 0, sizeof(ELFsectheader)*GDBJIT_SECT__MAX); + memset(&obj->sym, 0, sizeof(ELFsymbol)*GDBJIT_SYM__MAX); + /* Initialize sections. */ + ctx->p = obj->space; + gdbjit_initsect(ctx, GDBJIT_SECT_shstrtab, gdbjit_secthdr); + gdbjit_initsect(ctx, GDBJIT_SECT_strtab, gdbjit_symtab); + gdbjit_initsect(ctx, GDBJIT_SECT_debug_info, gdbjit_debuginfo); + gdbjit_initsect(ctx, GDBJIT_SECT_debug_abbrev, gdbjit_debugabbrev); + gdbjit_initsect(ctx, GDBJIT_SECT_debug_line, gdbjit_debugline); + SECTALIGN(ctx->p, sizeof(uintptr_t)); + gdbjit_initsect(ctx, GDBJIT_SECT_eh_frame, gdbjit_ehframe); + ctx->objsize = (size_t)((char *)ctx->p - (char *)obj); + lua_assert(ctx->objsize < sizeof(GDBJITobj)); +} + +#undef SECTALIGN + +/* -- Interface to GDB JIT API -------------------------------------------- */ + +/* Add new entry to GDB JIT symbol chain. */ +static void gdbjit_newentry(lua_State *L, GDBJITctx *ctx) +{ + /* Allocate memory for GDB JIT entry and ELF object. */ + MSize sz = (MSize)(sizeof(GDBJITentryobj) - sizeof(GDBJITobj) + ctx->objsize); + GDBJITentryobj *eo = lj_mem_newt(L, sz, GDBJITentryobj); + memcpy(&eo->obj, &ctx->obj, ctx->objsize); /* Copy ELF object. */ + eo->sz = sz; + ctx->T->gdbjit_entry = (void *)eo; + /* Link new entry to chain and register it. */ + eo->entry.prev_entry = NULL; + eo->entry.next_entry = __jit_debug_descriptor.first_entry; + if (eo->entry.next_entry) + eo->entry.next_entry->prev_entry = &eo->entry; + eo->entry.symfile_addr = (const char *)&eo->obj; + eo->entry.symfile_size = ctx->objsize; + __jit_debug_descriptor.first_entry = &eo->entry; + __jit_debug_descriptor.relevant_entry = &eo->entry; + __jit_debug_descriptor.action_flag = GDBJIT_REGISTER; + __jit_debug_register_code(); +} + +/* Add debug info for newly compiled trace and notify GDB. */ +void lj_gdbjit_addtrace(jit_State *J, GCtrace *T) +{ + GDBJITctx ctx; + GCproto *pt = &gcref(T->startpt)->pt; + TraceNo parent = T->ir[REF_BASE].op1; + const BCIns *startpc = mref(T->startpc, const BCIns); + ctx.T = T; + ctx.mcaddr = (uintptr_t)T->mcode; + ctx.szmcode = T->szmcode; + ctx.spadjp = CFRAME_SIZE_JIT + + (MSize)(parent ? traceref(J, parent)->spadjust : 0); + ctx.spadj = CFRAME_SIZE_JIT + T->spadjust; + lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc); + ctx.lineno = lj_debug_line(pt, proto_bcpos(pt, startpc)); + ctx.filename = proto_chunknamestr(pt); + if (*ctx.filename == '@' || *ctx.filename == '=') + ctx.filename++; + else + ctx.filename = "(string)"; + gdbjit_buildobj(&ctx); + gdbjit_newentry(J->L, &ctx); +} + +/* Delete debug info for trace and notify GDB. */ +void lj_gdbjit_deltrace(jit_State *J, GCtrace *T) +{ + GDBJITentryobj *eo = (GDBJITentryobj *)T->gdbjit_entry; + if (eo) { + if (eo->entry.prev_entry) + eo->entry.prev_entry->next_entry = eo->entry.next_entry; + else + __jit_debug_descriptor.first_entry = eo->entry.next_entry; + if (eo->entry.next_entry) + eo->entry.next_entry->prev_entry = eo->entry.prev_entry; + __jit_debug_descriptor.relevant_entry = &eo->entry; + __jit_debug_descriptor.action_flag = GDBJIT_UNREGISTER; + __jit_debug_register_code(); + lj_mem_free(J2G(J), eo, eo->sz); + } +} + +#endif +#endif diff --git a/external/lua/luajit/src/src/lj_gdbjit.h b/external/lua/luajit/src/src/lj_gdbjit.h new file mode 100644 index 0000000000..481cb22a36 --- /dev/null +++ b/external/lua/luajit/src/src/lj_gdbjit.h @@ -0,0 +1,22 @@ +/* +** Client for the GDB JIT API. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_GDBJIT_H +#define _LJ_GDBJIT_H + +#include "lj_obj.h" +#include "lj_jit.h" + +#if LJ_HASJIT && defined(LUAJIT_USE_GDBJIT) + +LJ_FUNC void lj_gdbjit_addtrace(jit_State *J, GCtrace *T); +LJ_FUNC void lj_gdbjit_deltrace(jit_State *J, GCtrace *T); + +#else +#define lj_gdbjit_addtrace(J, T) UNUSED(T) +#define lj_gdbjit_deltrace(J, T) UNUSED(T) +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_ir.c b/external/lua/luajit/src/src/lj_ir.c new file mode 100644 index 0000000000..e1a59105cf --- /dev/null +++ b/external/lua/luajit/src/src/lj_ir.c @@ -0,0 +1,501 @@ +/* +** SSA IR (Intermediate Representation) emitter. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_ir_c +#define LUA_CORE + +/* For pointers to libc/libm functions. */ +#include +#include + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_gc.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_ircall.h" +#include "lj_iropt.h" +#include "lj_trace.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#include "lj_cdata.h" +#include "lj_carith.h" +#endif +#include "lj_vm.h" +#include "lj_strscan.h" +#include "lj_lib.h" + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) +#define fins (&J->fold.ins) + +/* Pass IR on to next optimization in chain (FOLD). */ +#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) + +/* -- IR tables ----------------------------------------------------------- */ + +/* IR instruction modes. */ +LJ_DATADEF const uint8_t lj_ir_mode[IR__MAX+1] = { +IRDEF(IRMODE) + 0 +}; + +/* IR type sizes. */ +LJ_DATADEF const uint8_t lj_ir_type_size[IRT__MAX+1] = { +#define IRTSIZE(name, size) size, +IRTDEF(IRTSIZE) +#undef IRTSIZE + 0 +}; + +/* C call info for CALL* instructions. */ +LJ_DATADEF const CCallInfo lj_ir_callinfo[] = { +#define IRCALLCI(cond, name, nargs, kind, type, flags) \ + { (ASMFunction)IRCALLCOND_##cond(name), \ + (nargs)|(CCI_CALL_##kind)|(IRT_##type<irbuf + J->irbotlim; + MSize szins = J->irtoplim - J->irbotlim; + if (szins) { + baseir = (IRIns *)lj_mem_realloc(J->L, baseir, szins*sizeof(IRIns), + 2*szins*sizeof(IRIns)); + J->irtoplim = J->irbotlim + 2*szins; + } else { + baseir = (IRIns *)lj_mem_realloc(J->L, NULL, 0, LJ_MIN_IRSZ*sizeof(IRIns)); + J->irbotlim = REF_BASE - LJ_MIN_IRSZ/4; + J->irtoplim = J->irbotlim + LJ_MIN_IRSZ; + } + J->cur.ir = J->irbuf = baseir - J->irbotlim; +} + +/* Grow IR buffer at the bottom or shift it up. */ +static void lj_ir_growbot(jit_State *J) +{ + IRIns *baseir = J->irbuf + J->irbotlim; + MSize szins = J->irtoplim - J->irbotlim; + lua_assert(szins != 0); + lua_assert(J->cur.nk == J->irbotlim); + if (J->cur.nins + (szins >> 1) < J->irtoplim) { + /* More than half of the buffer is free on top: shift up by a quarter. */ + MSize ofs = szins >> 2; + memmove(baseir + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns)); + J->irbotlim -= ofs; + J->irtoplim -= ofs; + J->cur.ir = J->irbuf = baseir - J->irbotlim; + } else { + /* Double the buffer size, but split the growth amongst top/bottom. */ + IRIns *newbase = lj_mem_newt(J->L, 2*szins*sizeof(IRIns), IRIns); + MSize ofs = szins >= 256 ? 128 : (szins >> 1); /* Limit bottom growth. */ + memcpy(newbase + ofs, baseir, (J->cur.nins - J->irbotlim)*sizeof(IRIns)); + lj_mem_free(G(J->L), baseir, szins*sizeof(IRIns)); + J->irbotlim -= ofs; + J->irtoplim = J->irbotlim + 2*szins; + J->cur.ir = J->irbuf = newbase - J->irbotlim; + } +} + +/* Emit IR without any optimizations. */ +TRef LJ_FASTCALL lj_ir_emit(jit_State *J) +{ + IRRef ref = lj_ir_nextins(J); + IRIns *ir = IR(ref); + IROp op = fins->o; + ir->prev = J->chain[op]; + J->chain[op] = (IRRef1)ref; + ir->o = op; + ir->op1 = fins->op1; + ir->op2 = fins->op2; + J->guardemit.irt |= fins->t.irt; + return TREF(ref, irt_t((ir->t = fins->t))); +} + +/* Emit call to a C function. */ +TRef lj_ir_call(jit_State *J, IRCallID id, ...) +{ + const CCallInfo *ci = &lj_ir_callinfo[id]; + uint32_t n = CCI_NARGS(ci); + TRef tr = TREF_NIL; + va_list argp; + va_start(argp, id); + if ((ci->flags & CCI_L)) n--; + if (n > 0) + tr = va_arg(argp, IRRef); + while (n-- > 1) + tr = emitir(IRT(IR_CARG, IRT_NIL), tr, va_arg(argp, IRRef)); + va_end(argp); + if (CCI_OP(ci) == IR_CALLS) + J->needsnap = 1; /* Need snapshot after call with side effect. */ + return emitir(CCI_OPTYPE(ci), tr, id); +} + +/* -- Interning of constants ---------------------------------------------- */ + +/* +** IR instructions for constants are kept between J->cur.nk >= ref < REF_BIAS. +** They are chained like all other instructions, but grow downwards. +** The are interned (like strings in the VM) to facilitate reference +** comparisons. The same constant must get the same reference. +*/ + +/* Get ref of next IR constant and optionally grow IR. +** Note: this may invalidate all IRIns *! +*/ +static LJ_AINLINE IRRef ir_nextk(jit_State *J) +{ + IRRef ref = J->cur.nk; + if (LJ_UNLIKELY(ref <= J->irbotlim)) lj_ir_growbot(J); + J->cur.nk = --ref; + return ref; +} + +/* Intern int32_t constant. */ +TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k) +{ + IRIns *ir, *cir = J->cur.ir; + IRRef ref; + for (ref = J->chain[IR_KINT]; ref; ref = cir[ref].prev) + if (cir[ref].i == k) + goto found; + ref = ir_nextk(J); + ir = IR(ref); + ir->i = k; + ir->t.irt = IRT_INT; + ir->o = IR_KINT; + ir->prev = J->chain[IR_KINT]; + J->chain[IR_KINT] = (IRRef1)ref; +found: + return TREF(ref, IRT_INT); +} + +/* The MRef inside the KNUM/KINT64 IR instructions holds the address of the +** 64 bit constant. The constants themselves are stored in a chained array +** and shared across traces. +** +** Rationale for choosing this data structure: +** - The address of the constants is embedded in the generated machine code +** and must never move. A resizable array or hash table wouldn't work. +** - Most apps need very few non-32 bit integer constants (less than a dozen). +** - Linear search is hard to beat in terms of speed and low complexity. +*/ +typedef struct K64Array { + MRef next; /* Pointer to next list. */ + MSize numk; /* Number of used elements in this array. */ + TValue k[LJ_MIN_K64SZ]; /* Array of constants. */ +} K64Array; + +/* Free all chained arrays. */ +void lj_ir_k64_freeall(jit_State *J) +{ + K64Array *k; + for (k = mref(J->k64, K64Array); k; ) { + K64Array *next = mref(k->next, K64Array); + lj_mem_free(J2G(J), k, sizeof(K64Array)); + k = next; + } +} + +/* Find 64 bit constant in chained array or add it. */ +cTValue *lj_ir_k64_find(jit_State *J, uint64_t u64) +{ + K64Array *k, *kp = NULL; + TValue *ntv; + MSize idx; + /* Search for the constant in the whole chain of arrays. */ + for (k = mref(J->k64, K64Array); k; k = mref(k->next, K64Array)) { + kp = k; /* Remember previous element in list. */ + for (idx = 0; idx < k->numk; idx++) { /* Search one array. */ + TValue *tv = &k->k[idx]; + if (tv->u64 == u64) /* Needed for +-0/NaN/absmask. */ + return tv; + } + } + /* Constant was not found, need to add it. */ + if (!(kp && kp->numk < LJ_MIN_K64SZ)) { /* Allocate a new array. */ + K64Array *kn = lj_mem_newt(J->L, sizeof(K64Array), K64Array); + setmref(kn->next, NULL); + kn->numk = 0; + if (kp) + setmref(kp->next, kn); /* Chain to the end of the list. */ + else + setmref(J->k64, kn); /* Link first array. */ + kp = kn; + } + ntv = &kp->k[kp->numk++]; /* Add to current array. */ + ntv->u64 = u64; + return ntv; +} + +/* Intern 64 bit constant, given by its address. */ +TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv) +{ + IRIns *ir, *cir = J->cur.ir; + IRRef ref; + IRType t = op == IR_KNUM ? IRT_NUM : IRT_I64; + for (ref = J->chain[op]; ref; ref = cir[ref].prev) + if (ir_k64(&cir[ref]) == tv) + goto found; + ref = ir_nextk(J); + ir = IR(ref); + lua_assert(checkptr32(tv)); + setmref(ir->ptr, tv); + ir->t.irt = t; + ir->o = op; + ir->prev = J->chain[op]; + J->chain[op] = (IRRef1)ref; +found: + return TREF(ref, t); +} + +/* Intern FP constant, given by its 64 bit pattern. */ +TRef lj_ir_knum_u64(jit_State *J, uint64_t u64) +{ + return lj_ir_k64(J, IR_KNUM, lj_ir_k64_find(J, u64)); +} + +/* Intern 64 bit integer constant. */ +TRef lj_ir_kint64(jit_State *J, uint64_t u64) +{ + return lj_ir_k64(J, IR_KINT64, lj_ir_k64_find(J, u64)); +} + +/* Check whether a number is int and return it. -0 is NOT considered an int. */ +static int numistrueint(lua_Number n, int32_t *kp) +{ + int32_t k = lj_num2int(n); + if (n == (lua_Number)k) { + if (kp) *kp = k; + if (k == 0) { /* Special check for -0. */ + TValue tv; + setnumV(&tv, n); + if (tv.u32.hi != 0) + return 0; + } + return 1; + } + return 0; +} + +/* Intern number as int32_t constant if possible, otherwise as FP constant. */ +TRef lj_ir_knumint(jit_State *J, lua_Number n) +{ + int32_t k; + if (numistrueint(n, &k)) + return lj_ir_kint(J, k); + else + return lj_ir_knum(J, n); +} + +/* Intern GC object "constant". */ +TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t) +{ + IRIns *ir, *cir = J->cur.ir; + IRRef ref; + lua_assert(!isdead(J2G(J), o)); + for (ref = J->chain[IR_KGC]; ref; ref = cir[ref].prev) + if (ir_kgc(&cir[ref]) == o) + goto found; + ref = ir_nextk(J); + ir = IR(ref); + /* NOBARRIER: Current trace is a GC root. */ + setgcref(ir->gcr, o); + ir->t.irt = (uint8_t)t; + ir->o = IR_KGC; + ir->prev = J->chain[IR_KGC]; + J->chain[IR_KGC] = (IRRef1)ref; +found: + return TREF(ref, t); +} + +/* Intern 32 bit pointer constant. */ +TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr) +{ + IRIns *ir, *cir = J->cur.ir; + IRRef ref; + lua_assert((void *)(intptr_t)i32ptr(ptr) == ptr); + for (ref = J->chain[op]; ref; ref = cir[ref].prev) + if (mref(cir[ref].ptr, void) == ptr) + goto found; + ref = ir_nextk(J); + ir = IR(ref); + setmref(ir->ptr, ptr); + ir->t.irt = IRT_P32; + ir->o = op; + ir->prev = J->chain[op]; + J->chain[op] = (IRRef1)ref; +found: + return TREF(ref, IRT_P32); +} + +/* Intern typed NULL constant. */ +TRef lj_ir_knull(jit_State *J, IRType t) +{ + IRIns *ir, *cir = J->cur.ir; + IRRef ref; + for (ref = J->chain[IR_KNULL]; ref; ref = cir[ref].prev) + if (irt_t(cir[ref].t) == t) + goto found; + ref = ir_nextk(J); + ir = IR(ref); + ir->i = 0; + ir->t.irt = (uint8_t)t; + ir->o = IR_KNULL; + ir->prev = J->chain[IR_KNULL]; + J->chain[IR_KNULL] = (IRRef1)ref; +found: + return TREF(ref, t); +} + +/* Intern key slot. */ +TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot) +{ + IRIns *ir, *cir = J->cur.ir; + IRRef2 op12 = IRREF2((IRRef1)key, (IRRef1)slot); + IRRef ref; + /* Const part is not touched by CSE/DCE, so 0-65535 is ok for IRMlit here. */ + lua_assert(tref_isk(key) && slot == (IRRef)(IRRef1)slot); + for (ref = J->chain[IR_KSLOT]; ref; ref = cir[ref].prev) + if (cir[ref].op12 == op12) + goto found; + ref = ir_nextk(J); + ir = IR(ref); + ir->op12 = op12; + ir->t.irt = IRT_P32; + ir->o = IR_KSLOT; + ir->prev = J->chain[IR_KSLOT]; + J->chain[IR_KSLOT] = (IRRef1)ref; +found: + return TREF(ref, IRT_P32); +} + +/* -- Access to IR constants ---------------------------------------------- */ + +/* Copy value of IR constant. */ +void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir) +{ + UNUSED(L); + lua_assert(ir->o != IR_KSLOT); /* Common mistake. */ + switch (ir->o) { + case IR_KPRI: setitype(tv, irt_toitype(ir->t)); break; + case IR_KINT: setintV(tv, ir->i); break; + case IR_KGC: setgcV(L, tv, ir_kgc(ir), irt_toitype(ir->t)); break; + case IR_KPTR: case IR_KKPTR: case IR_KNULL: + setlightudV(tv, mref(ir->ptr, void)); + break; + case IR_KNUM: setnumV(tv, ir_knum(ir)->n); break; +#if LJ_HASFFI + case IR_KINT64: { + GCcdata *cd = lj_cdata_new_(L, CTID_INT64, 8); + *(uint64_t *)cdataptr(cd) = ir_kint64(ir)->u64; + setcdataV(L, tv, cd); + break; + } +#endif + default: lua_assert(0); break; + } +} + +/* -- Convert IR operand types -------------------------------------------- */ + +/* Convert from string to number. */ +TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr) +{ + if (!tref_isnumber(tr)) { + if (tref_isstr(tr)) + tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); + else + lj_trace_err(J, LJ_TRERR_BADTYPE); + } + return tr; +} + +/* Convert from integer or string to number. */ +TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr) +{ + if (!tref_isnum(tr)) { + if (tref_isinteger(tr)) + tr = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); + else if (tref_isstr(tr)) + tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); + else + lj_trace_err(J, LJ_TRERR_BADTYPE); + } + return tr; +} + +/* Convert from integer or number to string. */ +TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr) +{ + if (!tref_isstr(tr)) { + if (!tref_isnumber(tr)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + tr = emitir(IRT(IR_TOSTR, IRT_STR), tr, 0); + } + return tr; +} + +/* -- Miscellaneous IR ops ------------------------------------------------ */ + +/* Evaluate numeric comparison. */ +int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op) +{ + switch (op) { + case IR_EQ: return (a == b); + case IR_NE: return (a != b); + case IR_LT: return (a < b); + case IR_GE: return (a >= b); + case IR_LE: return (a <= b); + case IR_GT: return (a > b); + case IR_ULT: return !(a >= b); + case IR_UGE: return !(a < b); + case IR_ULE: return !(a > b); + case IR_UGT: return !(a <= b); + default: lua_assert(0); return 0; + } +} + +/* Evaluate string comparison. */ +int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op) +{ + int res = lj_str_cmp(a, b); + switch (op) { + case IR_LT: return (res < 0); + case IR_GE: return (res >= 0); + case IR_LE: return (res <= 0); + case IR_GT: return (res > 0); + default: lua_assert(0); return 0; + } +} + +/* Rollback IR to previous state. */ +void lj_ir_rollback(jit_State *J, IRRef ref) +{ + IRRef nins = J->cur.nins; + while (nins > ref) { + IRIns *ir; + nins--; + ir = IR(nins); + J->chain[ir->o] = ir->prev; + } + J->cur.nins = nins; +} + +#undef IR +#undef fins +#undef emitir + +#endif diff --git a/external/lua/luajit/src/src/lj_ir.h b/external/lua/luajit/src/src/lj_ir.h new file mode 100644 index 0000000000..a98243253f --- /dev/null +++ b/external/lua/luajit/src/src/lj_ir.h @@ -0,0 +1,551 @@ +/* +** SSA IR (Intermediate Representation) format. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_IR_H +#define _LJ_IR_H + +#include "lj_obj.h" + +/* -- IR instructions ----------------------------------------------------- */ + +/* IR instruction definition. Order matters, see below. ORDER IR */ +#define IRDEF(_) \ + /* Guarded assertions. */ \ + /* Must be properly aligned to flip opposites (^1) and (un)ordered (^4). */ \ + _(LT, N , ref, ref) \ + _(GE, N , ref, ref) \ + _(LE, N , ref, ref) \ + _(GT, N , ref, ref) \ + \ + _(ULT, N , ref, ref) \ + _(UGE, N , ref, ref) \ + _(ULE, N , ref, ref) \ + _(UGT, N , ref, ref) \ + \ + _(EQ, C , ref, ref) \ + _(NE, C , ref, ref) \ + \ + _(ABC, N , ref, ref) \ + _(RETF, S , ref, ref) \ + \ + /* Miscellaneous ops. */ \ + _(NOP, N , ___, ___) \ + _(BASE, N , lit, lit) \ + _(PVAL, N , lit, ___) \ + _(GCSTEP, S , ___, ___) \ + _(HIOP, S , ref, ref) \ + _(LOOP, S , ___, ___) \ + _(USE, S , ref, ___) \ + _(PHI, S , ref, ref) \ + _(RENAME, S , ref, lit) \ + \ + /* Constants. */ \ + _(KPRI, N , ___, ___) \ + _(KINT, N , cst, ___) \ + _(KGC, N , cst, ___) \ + _(KPTR, N , cst, ___) \ + _(KKPTR, N , cst, ___) \ + _(KNULL, N , cst, ___) \ + _(KNUM, N , cst, ___) \ + _(KINT64, N , cst, ___) \ + _(KSLOT, N , ref, lit) \ + \ + /* Bit ops. */ \ + _(BNOT, N , ref, ___) \ + _(BSWAP, N , ref, ___) \ + _(BAND, C , ref, ref) \ + _(BOR, C , ref, ref) \ + _(BXOR, C , ref, ref) \ + _(BSHL, N , ref, ref) \ + _(BSHR, N , ref, ref) \ + _(BSAR, N , ref, ref) \ + _(BROL, N , ref, ref) \ + _(BROR, N , ref, ref) \ + \ + /* Arithmetic ops. ORDER ARITH */ \ + _(ADD, C , ref, ref) \ + _(SUB, N , ref, ref) \ + _(MUL, C , ref, ref) \ + _(DIV, N , ref, ref) \ + _(MOD, N , ref, ref) \ + _(POW, N , ref, ref) \ + _(NEG, N , ref, ref) \ + \ + _(ABS, N , ref, ref) \ + _(ATAN2, N , ref, ref) \ + _(LDEXP, N , ref, ref) \ + _(MIN, C , ref, ref) \ + _(MAX, C , ref, ref) \ + _(FPMATH, N , ref, lit) \ + \ + /* Overflow-checking arithmetic ops. */ \ + _(ADDOV, CW, ref, ref) \ + _(SUBOV, NW, ref, ref) \ + _(MULOV, CW, ref, ref) \ + \ + /* Memory ops. A = array, H = hash, U = upvalue, F = field, S = stack. */ \ + \ + /* Memory references. */ \ + _(AREF, R , ref, ref) \ + _(HREFK, R , ref, ref) \ + _(HREF, L , ref, ref) \ + _(NEWREF, S , ref, ref) \ + _(UREFO, LW, ref, lit) \ + _(UREFC, LW, ref, lit) \ + _(FREF, R , ref, lit) \ + _(STRREF, N , ref, ref) \ + \ + /* Loads and Stores. These must be in the same order. */ \ + _(ALOAD, L , ref, ___) \ + _(HLOAD, L , ref, ___) \ + _(ULOAD, L , ref, ___) \ + _(FLOAD, L , ref, lit) \ + _(XLOAD, L , ref, lit) \ + _(SLOAD, L , lit, lit) \ + _(VLOAD, L , ref, ___) \ + \ + _(ASTORE, S , ref, ref) \ + _(HSTORE, S , ref, ref) \ + _(USTORE, S , ref, ref) \ + _(FSTORE, S , ref, ref) \ + _(XSTORE, S , ref, ref) \ + \ + /* Allocations. */ \ + _(SNEW, N , ref, ref) /* CSE is ok, not marked as A. */ \ + _(XSNEW, A , ref, ref) \ + _(TNEW, AW, lit, lit) \ + _(TDUP, AW, ref, ___) \ + _(CNEW, AW, ref, ref) \ + _(CNEWI, NW, ref, ref) /* CSE is ok, not marked as A. */ \ + \ + /* Barriers. */ \ + _(TBAR, S , ref, ___) \ + _(OBAR, S , ref, ref) \ + _(XBAR, S , ___, ___) \ + \ + /* Type conversions. */ \ + _(CONV, NW, ref, lit) \ + _(TOBIT, N , ref, ref) \ + _(TOSTR, N , ref, ___) \ + _(STRTO, N , ref, ___) \ + \ + /* Calls. */ \ + _(CALLN, N , ref, lit) \ + _(CALLL, L , ref, lit) \ + _(CALLS, S , ref, lit) \ + _(CALLXS, S , ref, ref) \ + _(CARG, N , ref, ref) \ + \ + /* End of list. */ + +/* IR opcodes (max. 256). */ +typedef enum { +#define IRENUM(name, m, m1, m2) IR_##name, +IRDEF(IRENUM) +#undef IRENUM + IR__MAX +} IROp; + +/* Stored opcode. */ +typedef uint8_t IROp1; + +LJ_STATIC_ASSERT(((int)IR_EQ^1) == (int)IR_NE); +LJ_STATIC_ASSERT(((int)IR_LT^1) == (int)IR_GE); +LJ_STATIC_ASSERT(((int)IR_LE^1) == (int)IR_GT); +LJ_STATIC_ASSERT(((int)IR_LT^3) == (int)IR_GT); +LJ_STATIC_ASSERT(((int)IR_LT^4) == (int)IR_ULT); + +/* Delta between xLOAD and xSTORE. */ +#define IRDELTA_L2S ((int)IR_ASTORE - (int)IR_ALOAD) + +LJ_STATIC_ASSERT((int)IR_HLOAD + IRDELTA_L2S == (int)IR_HSTORE); +LJ_STATIC_ASSERT((int)IR_ULOAD + IRDELTA_L2S == (int)IR_USTORE); +LJ_STATIC_ASSERT((int)IR_FLOAD + IRDELTA_L2S == (int)IR_FSTORE); +LJ_STATIC_ASSERT((int)IR_XLOAD + IRDELTA_L2S == (int)IR_XSTORE); + +/* -- Named IR literals --------------------------------------------------- */ + +/* FPMATH sub-functions. ORDER FPM. */ +#define IRFPMDEF(_) \ + _(FLOOR) _(CEIL) _(TRUNC) /* Must be first and in this order. */ \ + _(SQRT) _(EXP) _(EXP2) _(LOG) _(LOG2) _(LOG10) \ + _(SIN) _(COS) _(TAN) \ + _(OTHER) + +typedef enum { +#define FPMENUM(name) IRFPM_##name, +IRFPMDEF(FPMENUM) +#undef FPMENUM + IRFPM__MAX +} IRFPMathOp; + +/* FLOAD fields. */ +#define IRFLDEF(_) \ + _(STR_LEN, offsetof(GCstr, len)) \ + _(FUNC_ENV, offsetof(GCfunc, l.env)) \ + _(FUNC_PC, offsetof(GCfunc, l.pc)) \ + _(TAB_META, offsetof(GCtab, metatable)) \ + _(TAB_ARRAY, offsetof(GCtab, array)) \ + _(TAB_NODE, offsetof(GCtab, node)) \ + _(TAB_ASIZE, offsetof(GCtab, asize)) \ + _(TAB_HMASK, offsetof(GCtab, hmask)) \ + _(TAB_NOMM, offsetof(GCtab, nomm)) \ + _(UDATA_META, offsetof(GCudata, metatable)) \ + _(UDATA_UDTYPE, offsetof(GCudata, udtype)) \ + _(UDATA_FILE, sizeof(GCudata)) \ + _(CDATA_CTYPEID, offsetof(GCcdata, ctypeid)) \ + _(CDATA_PTR, sizeof(GCcdata)) \ + _(CDATA_INT, sizeof(GCcdata)) \ + _(CDATA_INT64, sizeof(GCcdata)) \ + _(CDATA_INT64_4, sizeof(GCcdata) + 4) + +typedef enum { +#define FLENUM(name, ofs) IRFL_##name, +IRFLDEF(FLENUM) +#undef FLENUM + IRFL__MAX +} IRFieldID; + +/* SLOAD mode bits, stored in op2. */ +#define IRSLOAD_PARENT 0x01 /* Coalesce with parent trace. */ +#define IRSLOAD_FRAME 0x02 /* Load hiword of frame. */ +#define IRSLOAD_TYPECHECK 0x04 /* Needs type check. */ +#define IRSLOAD_CONVERT 0x08 /* Number to integer conversion. */ +#define IRSLOAD_READONLY 0x10 /* Read-only, omit slot store. */ +#define IRSLOAD_INHERIT 0x20 /* Inherited by exits/side traces. */ + +/* XLOAD mode, stored in op2. */ +#define IRXLOAD_READONLY 1 /* Load from read-only data. */ +#define IRXLOAD_VOLATILE 2 /* Load from volatile data. */ +#define IRXLOAD_UNALIGNED 4 /* Unaligned load. */ + +/* CONV mode, stored in op2. */ +#define IRCONV_SRCMASK 0x001f /* Source IRType. */ +#define IRCONV_DSTMASK 0x03e0 /* Dest. IRType (also in ir->t). */ +#define IRCONV_DSH 5 +#define IRCONV_NUM_INT ((IRT_NUM<>2)&3)) +#define irm_iscomm(m) ((m) & IRM_C) +#define irm_kind(m) ((m) & IRM_S) + +#define IRMODE(name, m, m1, m2) (((IRM##m1)|((IRM##m2)<<2)|(IRM_##m))^IRM_W), + +LJ_DATA const uint8_t lj_ir_mode[IR__MAX+1]; + +/* -- IR instruction types ------------------------------------------------ */ + +/* Map of itypes to non-negative numbers. ORDER LJ_T. +** LJ_TUPVAL/LJ_TTRACE never appear in a TValue. Use these itypes for +** IRT_P32 and IRT_P64, which never escape the IR. +** The various integers are only used in the IR and can only escape to +** a TValue after implicit or explicit conversion. Their types must be +** contiguous and next to IRT_NUM (see the typerange macros below). +*/ +#define IRTDEF(_) \ + _(NIL, 4) _(FALSE, 4) _(TRUE, 4) _(LIGHTUD, LJ_64 ? 8 : 4) _(STR, 4) \ + _(P32, 4) _(THREAD, 4) _(PROTO, 4) _(FUNC, 4) _(P64, 8) _(CDATA, 4) \ + _(TAB, 4) _(UDATA, 4) \ + _(FLOAT, 4) _(NUM, 8) _(I8, 1) _(U8, 1) _(I16, 2) _(U16, 2) \ + _(INT, 4) _(U32, 4) _(I64, 8) _(U64, 8) \ + _(SOFTFP, 4) /* There is room for 9 more types. */ + +/* IR result type and flags (8 bit). */ +typedef enum { +#define IRTENUM(name, size) IRT_##name, +IRTDEF(IRTENUM) +#undef IRTENUM + IRT__MAX, + + /* Native pointer type and the corresponding integer type. */ + IRT_PTR = LJ_64 ? IRT_P64 : IRT_P32, + IRT_INTP = LJ_64 ? IRT_I64 : IRT_INT, + IRT_UINTP = LJ_64 ? IRT_U64 : IRT_U32, + + /* Additional flags. */ + IRT_MARK = 0x20, /* Marker for misc. purposes. */ + IRT_ISPHI = 0x40, /* Instruction is left or right PHI operand. */ + IRT_GUARD = 0x80, /* Instruction is a guard. */ + + /* Masks. */ + IRT_TYPE = 0x1f, + IRT_T = 0xff +} IRType; + +#define irtype_ispri(irt) ((uint32_t)(irt) <= IRT_TRUE) + +/* Stored IRType. */ +typedef struct IRType1 { uint8_t irt; } IRType1; + +#define IRT(o, t) ((uint32_t)(((o)<<8) | (t))) +#define IRTI(o) (IRT((o), IRT_INT)) +#define IRTN(o) (IRT((o), IRT_NUM)) +#define IRTG(o, t) (IRT((o), IRT_GUARD|(t))) +#define IRTGI(o) (IRT((o), IRT_GUARD|IRT_INT)) + +#define irt_t(t) ((IRType)(t).irt) +#define irt_type(t) ((IRType)((t).irt & IRT_TYPE)) +#define irt_sametype(t1, t2) ((((t1).irt ^ (t2).irt) & IRT_TYPE) == 0) +#define irt_typerange(t, first, last) \ + ((uint32_t)((t).irt & IRT_TYPE) - (uint32_t)(first) <= (uint32_t)(last-first)) + +#define irt_isnil(t) (irt_type(t) == IRT_NIL) +#define irt_ispri(t) ((uint32_t)irt_type(t) <= IRT_TRUE) +#define irt_islightud(t) (irt_type(t) == IRT_LIGHTUD) +#define irt_isstr(t) (irt_type(t) == IRT_STR) +#define irt_istab(t) (irt_type(t) == IRT_TAB) +#define irt_iscdata(t) (irt_type(t) == IRT_CDATA) +#define irt_isfloat(t) (irt_type(t) == IRT_FLOAT) +#define irt_isnum(t) (irt_type(t) == IRT_NUM) +#define irt_isint(t) (irt_type(t) == IRT_INT) +#define irt_isi8(t) (irt_type(t) == IRT_I8) +#define irt_isu8(t) (irt_type(t) == IRT_U8) +#define irt_isi16(t) (irt_type(t) == IRT_I16) +#define irt_isu16(t) (irt_type(t) == IRT_U16) +#define irt_isu32(t) (irt_type(t) == IRT_U32) +#define irt_isi64(t) (irt_type(t) == IRT_I64) +#define irt_isu64(t) (irt_type(t) == IRT_U64) + +#define irt_isfp(t) (irt_isnum(t) || irt_isfloat(t)) +#define irt_isinteger(t) (irt_typerange((t), IRT_I8, IRT_INT)) +#define irt_isgcv(t) (irt_typerange((t), IRT_STR, IRT_UDATA)) +#define irt_isaddr(t) (irt_typerange((t), IRT_LIGHTUD, IRT_UDATA)) +#define irt_isint64(t) (irt_typerange((t), IRT_I64, IRT_U64)) + +#if LJ_64 +#define IRT_IS64 \ + ((1u<> irt_type(t)) & 1) +#define irt_is64orfp(t) (((IRT_IS64|(1u<>irt_type(t)) & 1) + +#define irt_size(t) (lj_ir_type_size[irt_t((t))]) + +LJ_DATA const uint8_t lj_ir_type_size[]; + +static LJ_AINLINE IRType itype2irt(const TValue *tv) +{ + if (tvisint(tv)) + return IRT_INT; + else if (tvisnum(tv)) + return IRT_NUM; +#if LJ_64 + else if (tvislightud(tv)) + return IRT_LIGHTUD; +#endif + else + return (IRType)~itype(tv); +} + +static LJ_AINLINE uint32_t irt_toitype_(IRType t) +{ + lua_assert(!LJ_64 || t != IRT_LIGHTUD); + if (LJ_DUALNUM && t > IRT_NUM) { + return LJ_TISNUM; + } else { + lua_assert(t <= IRT_NUM); + return ~(uint32_t)t; + } +} + +#define irt_toitype(t) irt_toitype_(irt_type((t))) + +#define irt_isguard(t) ((t).irt & IRT_GUARD) +#define irt_ismarked(t) ((t).irt & IRT_MARK) +#define irt_setmark(t) ((t).irt |= IRT_MARK) +#define irt_clearmark(t) ((t).irt &= ~IRT_MARK) +#define irt_isphi(t) ((t).irt & IRT_ISPHI) +#define irt_setphi(t) ((t).irt |= IRT_ISPHI) +#define irt_clearphi(t) ((t).irt &= ~IRT_ISPHI) + +/* Stored combined IR opcode and type. */ +typedef uint16_t IROpT; + +/* -- IR references ------------------------------------------------------- */ + +/* IR references. */ +typedef uint16_t IRRef1; /* One stored reference. */ +typedef uint32_t IRRef2; /* Two stored references. */ +typedef uint32_t IRRef; /* Used to pass around references. */ + +/* Fixed references. */ +enum { + REF_BIAS = 0x8000, + REF_TRUE = REF_BIAS-3, + REF_FALSE = REF_BIAS-2, + REF_NIL = REF_BIAS-1, /* \--- Constants grow downwards. */ + REF_BASE = REF_BIAS, /* /--- IR grows upwards. */ + REF_FIRST = REF_BIAS+1, + REF_DROP = 0xffff +}; + +/* Note: IRMlit operands must be < REF_BIAS, too! +** This allows for fast and uniform manipulation of all operands +** without looking up the operand mode in lj_ir_mode: +** - CSE calculates the maximum reference of two operands. +** This must work with mixed reference/literal operands, too. +** - DCE marking only checks for operand >= REF_BIAS. +** - LOOP needs to substitute reference operands. +** Constant references and literals must not be modified. +*/ + +#define IRREF2(lo, hi) ((IRRef2)(lo) | ((IRRef2)(hi) << 16)) + +#define irref_isk(ref) ((ref) < REF_BIAS) + +/* Tagged IR references (32 bit). +** +** +-------+-------+---------------+ +** | irt | flags | ref | +** +-------+-------+---------------+ +** +** The tag holds a copy of the IRType and speeds up IR type checks. +*/ +typedef uint32_t TRef; + +#define TREF_REFMASK 0x0000ffff +#define TREF_FRAME 0x00010000 +#define TREF_CONT 0x00020000 + +#define TREF(ref, t) ((TRef)((ref) + ((t)<<24))) + +#define tref_ref(tr) ((IRRef1)(tr)) +#define tref_t(tr) ((IRType)((tr)>>24)) +#define tref_type(tr) ((IRType)(((tr)>>24) & IRT_TYPE)) +#define tref_typerange(tr, first, last) \ + ((((tr)>>24) & IRT_TYPE) - (TRef)(first) <= (TRef)(last-first)) + +#define tref_istype(tr, t) (((tr) & (IRT_TYPE<<24)) == ((t)<<24)) +#define tref_isnil(tr) (tref_istype((tr), IRT_NIL)) +#define tref_isfalse(tr) (tref_istype((tr), IRT_FALSE)) +#define tref_istrue(tr) (tref_istype((tr), IRT_TRUE)) +#define tref_isstr(tr) (tref_istype((tr), IRT_STR)) +#define tref_isfunc(tr) (tref_istype((tr), IRT_FUNC)) +#define tref_iscdata(tr) (tref_istype((tr), IRT_CDATA)) +#define tref_istab(tr) (tref_istype((tr), IRT_TAB)) +#define tref_isudata(tr) (tref_istype((tr), IRT_UDATA)) +#define tref_isnum(tr) (tref_istype((tr), IRT_NUM)) +#define tref_isint(tr) (tref_istype((tr), IRT_INT)) + +#define tref_isbool(tr) (tref_typerange((tr), IRT_FALSE, IRT_TRUE)) +#define tref_ispri(tr) (tref_typerange((tr), IRT_NIL, IRT_TRUE)) +#define tref_istruecond(tr) (!tref_typerange((tr), IRT_NIL, IRT_FALSE)) +#define tref_isinteger(tr) (tref_typerange((tr), IRT_I8, IRT_INT)) +#define tref_isnumber(tr) (tref_typerange((tr), IRT_NUM, IRT_INT)) +#define tref_isnumber_str(tr) (tref_isnumber((tr)) || tref_isstr((tr))) +#define tref_isgcv(tr) (tref_typerange((tr), IRT_STR, IRT_UDATA)) + +#define tref_isk(tr) (irref_isk(tref_ref((tr)))) +#define tref_isk2(tr1, tr2) (irref_isk(tref_ref((tr1) | (tr2)))) + +#define TREF_PRI(t) (TREF(REF_NIL-(t), (t))) +#define TREF_NIL (TREF_PRI(IRT_NIL)) +#define TREF_FALSE (TREF_PRI(IRT_FALSE)) +#define TREF_TRUE (TREF_PRI(IRT_TRUE)) + +/* -- IR format ----------------------------------------------------------- */ + +/* IR instruction format (64 bit). +** +** 16 16 8 8 8 8 +** +-------+-------+---+---+---+---+ +** | op1 | op2 | t | o | r | s | +** +-------+-------+---+---+---+---+ +** | op12/i/gco | ot | prev | (alternative fields in union) +** +---------------+-------+-------+ +** 32 16 16 +** +** prev is only valid prior to register allocation and then reused for r + s. +*/ + +typedef union IRIns { + struct { + LJ_ENDIAN_LOHI( + IRRef1 op1; /* IR operand 1. */ + , IRRef1 op2; /* IR operand 2. */ + ) + IROpT ot; /* IR opcode and type (overlaps t and o). */ + IRRef1 prev; /* Previous ins in same chain (overlaps r and s). */ + }; + struct { + IRRef2 op12; /* IR operand 1 and 2 (overlaps op1 and op2). */ + LJ_ENDIAN_LOHI( + IRType1 t; /* IR type. */ + , IROp1 o; /* IR opcode. */ + ) + LJ_ENDIAN_LOHI( + uint8_t r; /* Register allocation (overlaps prev). */ + , uint8_t s; /* Spill slot allocation (overlaps prev). */ + ) + }; + int32_t i; /* 32 bit signed integer literal (overlaps op12). */ + GCRef gcr; /* GCobj constant (overlaps op12). */ + MRef ptr; /* Pointer constant (overlaps op12). */ +} IRIns; + +#define ir_kgc(ir) check_exp((ir)->o == IR_KGC, gcref((ir)->gcr)) +#define ir_kstr(ir) (gco2str(ir_kgc((ir)))) +#define ir_ktab(ir) (gco2tab(ir_kgc((ir)))) +#define ir_kfunc(ir) (gco2func(ir_kgc((ir)))) +#define ir_kcdata(ir) (gco2cd(ir_kgc((ir)))) +#define ir_knum(ir) check_exp((ir)->o == IR_KNUM, mref((ir)->ptr, cTValue)) +#define ir_kint64(ir) check_exp((ir)->o == IR_KINT64, mref((ir)->ptr,cTValue)) +#define ir_k64(ir) \ + check_exp((ir)->o == IR_KNUM || (ir)->o == IR_KINT64, mref((ir)->ptr,cTValue)) +#define ir_kptr(ir) \ + check_exp((ir)->o == IR_KPTR || (ir)->o == IR_KKPTR, mref((ir)->ptr, void)) + +/* A store or any other op with a non-weak guard has a side-effect. */ +static LJ_AINLINE int ir_sideeff(IRIns *ir) +{ + return (((ir->t.irt | ~IRT_GUARD) & lj_ir_mode[ir->o]) >= IRM_S); +} + +LJ_STATIC_ASSERT((int)IRT_GUARD == (int)IRM_W); + +#endif diff --git a/external/lua/luajit/src/src/lj_ircall.h b/external/lua/luajit/src/src/lj_ircall.h new file mode 100644 index 0000000000..7fcc532ebb --- /dev/null +++ b/external/lua/luajit/src/src/lj_ircall.h @@ -0,0 +1,271 @@ +/* +** IR CALL* instruction definitions. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_IRCALL_H +#define _LJ_IRCALL_H + +#include "lj_obj.h" +#include "lj_ir.h" +#include "lj_jit.h" + +/* C call info for CALL* instructions. */ +typedef struct CCallInfo { + ASMFunction func; /* Function pointer. */ + uint32_t flags; /* Number of arguments and flags. */ +} CCallInfo; + +#define CCI_NARGS(ci) ((ci)->flags & 0xff) /* Extract # of args. */ +#define CCI_NARGS_MAX 32 /* Max. # of args. */ + +#define CCI_OTSHIFT 16 +#define CCI_OPTYPE(ci) ((ci)->flags >> CCI_OTSHIFT) /* Get op/type. */ +#define CCI_OPSHIFT 24 +#define CCI_OP(ci) ((ci)->flags >> CCI_OPSHIFT) /* Get op. */ + +#define CCI_CALL_N (IR_CALLN << CCI_OPSHIFT) +#define CCI_CALL_L (IR_CALLL << CCI_OPSHIFT) +#define CCI_CALL_S (IR_CALLS << CCI_OPSHIFT) +#define CCI_CALL_FN (CCI_CALL_N|CCI_CC_FASTCALL) +#define CCI_CALL_FL (CCI_CALL_L|CCI_CC_FASTCALL) +#define CCI_CALL_FS (CCI_CALL_S|CCI_CC_FASTCALL) + +/* C call info flags. */ +#define CCI_L 0x0100 /* Implicit L arg. */ +#define CCI_CASTU64 0x0200 /* Cast u64 result to number. */ +#define CCI_NOFPRCLOBBER 0x0400 /* Does not clobber any FPRs. */ +#define CCI_VARARG 0x0800 /* Vararg function. */ + +#define CCI_CC_MASK 0x3000 /* Calling convention mask. */ +#define CCI_CC_SHIFT 12 +/* ORDER CC */ +#define CCI_CC_CDECL 0x0000 /* Default cdecl calling convention. */ +#define CCI_CC_THISCALL 0x1000 /* Thiscall calling convention. */ +#define CCI_CC_FASTCALL 0x2000 /* Fastcall calling convention. */ +#define CCI_CC_STDCALL 0x3000 /* Stdcall calling convention. */ + +/* Helpers for conditional function definitions. */ +#define IRCALLCOND_ANY(x) x + +#if LJ_TARGET_X86ORX64 +#define IRCALLCOND_FPMATH(x) NULL +#else +#define IRCALLCOND_FPMATH(x) x +#endif + +#if LJ_SOFTFP +#define IRCALLCOND_SOFTFP(x) x +#if LJ_HASFFI +#define IRCALLCOND_SOFTFP_FFI(x) x +#else +#define IRCALLCOND_SOFTFP_FFI(x) NULL +#endif +#else +#define IRCALLCOND_SOFTFP(x) NULL +#define IRCALLCOND_SOFTFP_FFI(x) NULL +#endif + +#define LJ_NEED_FP64 (LJ_TARGET_ARM || LJ_TARGET_PPC || LJ_TARGET_MIPS) + +#if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64) +#define IRCALLCOND_FP64_FFI(x) x +#else +#define IRCALLCOND_FP64_FFI(x) NULL +#endif + +#if LJ_HASFFI +#define IRCALLCOND_FFI(x) x +#if LJ_32 +#define IRCALLCOND_FFI32(x) x +#else +#define IRCALLCOND_FFI32(x) NULL +#endif +#else +#define IRCALLCOND_FFI(x) NULL +#define IRCALLCOND_FFI32(x) NULL +#endif + +#if LJ_SOFTFP +#define ARG1_FP 2 /* Treat as 2 32 bit arguments. */ +#else +#define ARG1_FP 1 +#endif + +#if LJ_32 +#define ARG2_64 4 /* Treat as 4 32 bit arguments. */ +#else +#define ARG2_64 2 +#endif + +/* Function definitions for CALL* instructions. */ +#define IRCALLDEF(_) \ + _(ANY, lj_str_cmp, 2, FN, INT, CCI_NOFPRCLOBBER) \ + _(ANY, lj_str_new, 3, S, STR, CCI_L) \ + _(ANY, lj_strscan_num, 2, FN, INT, 0) \ + _(ANY, lj_str_fromint, 2, FN, STR, CCI_L) \ + _(ANY, lj_str_fromnum, 2, FN, STR, CCI_L) \ + _(ANY, lj_tab_new1, 2, FS, TAB, CCI_L) \ + _(ANY, lj_tab_dup, 2, FS, TAB, CCI_L) \ + _(ANY, lj_tab_newkey, 3, S, P32, CCI_L) \ + _(ANY, lj_tab_len, 1, FL, INT, 0) \ + _(ANY, lj_gc_step_jit, 2, FS, NIL, CCI_L) \ + _(ANY, lj_gc_barrieruv, 2, FS, NIL, 0) \ + _(ANY, lj_mem_newgco, 2, FS, P32, CCI_L) \ + _(ANY, lj_math_random_step, 1, FS, NUM, CCI_CASTU64|CCI_NOFPRCLOBBER) \ + _(ANY, lj_vm_modi, 2, FN, INT, 0) \ + _(ANY, sinh, ARG1_FP, N, NUM, 0) \ + _(ANY, cosh, ARG1_FP, N, NUM, 0) \ + _(ANY, tanh, ARG1_FP, N, NUM, 0) \ + _(ANY, fputc, 2, S, INT, 0) \ + _(ANY, fwrite, 4, S, INT, 0) \ + _(ANY, fflush, 1, S, INT, 0) \ + /* ORDER FPM */ \ + _(FPMATH, lj_vm_floor, ARG1_FP, N, NUM, 0) \ + _(FPMATH, lj_vm_ceil, ARG1_FP, N, NUM, 0) \ + _(FPMATH, lj_vm_trunc, ARG1_FP, N, NUM, 0) \ + _(FPMATH, sqrt, ARG1_FP, N, NUM, 0) \ + _(FPMATH, exp, ARG1_FP, N, NUM, 0) \ + _(FPMATH, lj_vm_exp2, ARG1_FP, N, NUM, 0) \ + _(FPMATH, log, ARG1_FP, N, NUM, 0) \ + _(FPMATH, lj_vm_log2, ARG1_FP, N, NUM, 0) \ + _(FPMATH, log10, ARG1_FP, N, NUM, 0) \ + _(FPMATH, sin, ARG1_FP, N, NUM, 0) \ + _(FPMATH, cos, ARG1_FP, N, NUM, 0) \ + _(FPMATH, tan, ARG1_FP, N, NUM, 0) \ + _(FPMATH, lj_vm_powi, ARG1_FP+1, N, NUM, 0) \ + _(FPMATH, pow, ARG1_FP*2, N, NUM, 0) \ + _(FPMATH, atan2, ARG1_FP*2, N, NUM, 0) \ + _(FPMATH, ldexp, ARG1_FP+1, N, NUM, 0) \ + _(SOFTFP, lj_vm_tobit, 2, N, INT, 0) \ + _(SOFTFP, softfp_add, 4, N, NUM, 0) \ + _(SOFTFP, softfp_sub, 4, N, NUM, 0) \ + _(SOFTFP, softfp_mul, 4, N, NUM, 0) \ + _(SOFTFP, softfp_div, 4, N, NUM, 0) \ + _(SOFTFP, softfp_cmp, 4, N, NIL, 0) \ + _(SOFTFP, softfp_i2d, 1, N, NUM, 0) \ + _(SOFTFP, softfp_d2i, 2, N, INT, 0) \ + _(SOFTFP_FFI, softfp_ui2d, 1, N, NUM, 0) \ + _(SOFTFP_FFI, softfp_f2d, 1, N, NUM, 0) \ + _(SOFTFP_FFI, softfp_d2ui, 2, N, INT, 0) \ + _(SOFTFP_FFI, softfp_d2f, 2, N, FLOAT, 0) \ + _(SOFTFP_FFI, softfp_i2f, 1, N, FLOAT, 0) \ + _(SOFTFP_FFI, softfp_ui2f, 1, N, FLOAT, 0) \ + _(SOFTFP_FFI, softfp_f2i, 1, N, INT, 0) \ + _(SOFTFP_FFI, softfp_f2ui, 1, N, INT, 0) \ + _(FP64_FFI, fp64_l2d, 2, N, NUM, 0) \ + _(FP64_FFI, fp64_ul2d, 2, N, NUM, 0) \ + _(FP64_FFI, fp64_l2f, 2, N, FLOAT, 0) \ + _(FP64_FFI, fp64_ul2f, 2, N, FLOAT, 0) \ + _(FP64_FFI, fp64_d2l, ARG1_FP, N, I64, 0) \ + _(FP64_FFI, fp64_d2ul, ARG1_FP, N, U64, 0) \ + _(FP64_FFI, fp64_f2l, 1, N, I64, 0) \ + _(FP64_FFI, fp64_f2ul, 1, N, U64, 0) \ + _(FFI, lj_carith_divi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \ + _(FFI, lj_carith_divu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \ + _(FFI, lj_carith_modi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \ + _(FFI, lj_carith_modu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \ + _(FFI, lj_carith_powi64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) \ + _(FFI, lj_carith_powu64, ARG2_64, N, U64, CCI_NOFPRCLOBBER) \ + _(FFI, lj_cdata_setfin, 2, FN, P32, CCI_L) \ + _(FFI, strlen, 1, L, INTP, 0) \ + _(FFI, memcpy, 3, S, PTR, 0) \ + _(FFI, memset, 3, S, PTR, 0) \ + _(FFI, lj_vm_errno, 0, S, INT, CCI_NOFPRCLOBBER) \ + _(FFI32, lj_carith_mul64, ARG2_64, N, I64, CCI_NOFPRCLOBBER) + \ + /* End of list. */ + +typedef enum { +#define IRCALLENUM(cond, name, nargs, kind, type, flags) IRCALL_##name, +IRCALLDEF(IRCALLENUM) +#undef IRCALLENUM + IRCALL__MAX +} IRCallID; + +LJ_FUNC TRef lj_ir_call(jit_State *J, IRCallID id, ...); + +LJ_DATA const CCallInfo lj_ir_callinfo[IRCALL__MAX+1]; + +/* Soft-float declarations. */ +#if LJ_SOFTFP +#if LJ_TARGET_ARM +#define softfp_add __aeabi_dadd +#define softfp_sub __aeabi_dsub +#define softfp_mul __aeabi_dmul +#define softfp_div __aeabi_ddiv +#define softfp_cmp __aeabi_cdcmple +#define softfp_i2d __aeabi_i2d +#define softfp_d2i __aeabi_d2iz +#define softfp_ui2d __aeabi_ui2d +#define softfp_f2d __aeabi_f2d +#define softfp_d2ui __aeabi_d2uiz +#define softfp_d2f __aeabi_d2f +#define softfp_i2f __aeabi_i2f +#define softfp_ui2f __aeabi_ui2f +#define softfp_f2i __aeabi_f2iz +#define softfp_f2ui __aeabi_f2uiz +#define fp64_l2d __aeabi_l2d +#define fp64_ul2d __aeabi_ul2d +#define fp64_l2f __aeabi_l2f +#define fp64_ul2f __aeabi_ul2f +#if LJ_TARGET_IOS +#define fp64_d2l __fixdfdi +#define fp64_d2ul __fixunsdfdi +#define fp64_f2l __fixsfdi +#define fp64_f2ul __fixunssfdi +#else +#define fp64_d2l __aeabi_d2lz +#define fp64_d2ul __aeabi_d2ulz +#define fp64_f2l __aeabi_f2lz +#define fp64_f2ul __aeabi_f2ulz +#endif +#else +#error "Missing soft-float definitions for target architecture" +#endif +extern double softfp_add(double a, double b); +extern double softfp_sub(double a, double b); +extern double softfp_mul(double a, double b); +extern double softfp_div(double a, double b); +extern void softfp_cmp(double a, double b); +extern double softfp_i2d(int32_t a); +extern int32_t softfp_d2i(double a); +#if LJ_HASFFI +extern double softfp_ui2d(uint32_t a); +extern double softfp_f2d(float a); +extern uint32_t softfp_d2ui(double a); +extern float softfp_d2f(double a); +extern float softfp_i2f(int32_t a); +extern float softfp_ui2f(uint32_t a); +extern int32_t softfp_f2i(float a); +extern uint32_t softfp_f2ui(float a); +#endif +#endif + +#if LJ_HASFFI && LJ_NEED_FP64 && !(LJ_TARGET_ARM && LJ_SOFTFP) +#ifdef __GNUC__ +#define fp64_l2d __floatdidf +#define fp64_ul2d __floatundidf +#define fp64_l2f __floatdisf +#define fp64_ul2f __floatundisf +#define fp64_d2l __fixdfdi +#define fp64_d2ul __fixunsdfdi +#define fp64_f2l __fixsfdi +#define fp64_f2ul __fixunssfdi +#else +#error "Missing fp64 helper definitions for this compiler" +#endif +#endif + +#if LJ_HASFFI && (LJ_SOFTFP || LJ_NEED_FP64) +extern double fp64_l2d(int64_t a); +extern double fp64_ul2d(uint64_t a); +extern float fp64_l2f(int64_t a); +extern float fp64_ul2f(uint64_t a); +extern int64_t fp64_d2l(double a); +extern uint64_t fp64_d2ul(double a); +extern int64_t fp64_f2l(float a); +extern uint64_t fp64_f2ul(float a); +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_iropt.h b/external/lua/luajit/src/src/lj_iropt.h new file mode 100644 index 0000000000..a74f018f31 --- /dev/null +++ b/external/lua/luajit/src/src/lj_iropt.h @@ -0,0 +1,161 @@ +/* +** Common header for IR emitter and optimizations. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_IROPT_H +#define _LJ_IROPT_H + +#include + +#include "lj_obj.h" +#include "lj_jit.h" + +#if LJ_HASJIT +/* IR emitter. */ +LJ_FUNC void LJ_FASTCALL lj_ir_growtop(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_ir_emit(jit_State *J); + +/* Save current IR in J->fold.ins, but do not emit it (yet). */ +static LJ_AINLINE void lj_ir_set_(jit_State *J, uint16_t ot, IRRef1 a, IRRef1 b) +{ + J->fold.ins.ot = ot; J->fold.ins.op1 = a; J->fold.ins.op2 = b; +} + +#define lj_ir_set(J, ot, a, b) \ + lj_ir_set_(J, (uint16_t)(ot), (IRRef1)(a), (IRRef1)(b)) + +/* Get ref of next IR instruction and optionally grow IR. +** Note: this may invalidate all IRIns*! +*/ +static LJ_AINLINE IRRef lj_ir_nextins(jit_State *J) +{ + IRRef ref = J->cur.nins; + if (LJ_UNLIKELY(ref >= J->irtoplim)) lj_ir_growtop(J); + J->cur.nins = ref + 1; + return ref; +} + +/* Interning of constants. */ +LJ_FUNC TRef LJ_FASTCALL lj_ir_kint(jit_State *J, int32_t k); +LJ_FUNC void lj_ir_k64_freeall(jit_State *J); +LJ_FUNC TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv); +LJ_FUNC cTValue *lj_ir_k64_find(jit_State *J, uint64_t u64); +LJ_FUNC TRef lj_ir_knum_u64(jit_State *J, uint64_t u64); +LJ_FUNC TRef lj_ir_knumint(jit_State *J, lua_Number n); +LJ_FUNC TRef lj_ir_kint64(jit_State *J, uint64_t u64); +LJ_FUNC TRef lj_ir_kgc(jit_State *J, GCobj *o, IRType t); +LJ_FUNC TRef lj_ir_kptr_(jit_State *J, IROp op, void *ptr); +LJ_FUNC TRef lj_ir_knull(jit_State *J, IRType t); +LJ_FUNC TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot); + +#if LJ_64 +#define lj_ir_kintp(J, k) lj_ir_kint64(J, (uint64_t)(k)) +#else +#define lj_ir_kintp(J, k) lj_ir_kint(J, (int32_t)(k)) +#endif + +static LJ_AINLINE TRef lj_ir_knum(jit_State *J, lua_Number n) +{ + TValue tv; + tv.n = n; + return lj_ir_knum_u64(J, tv.u64); +} + +#define lj_ir_kstr(J, str) lj_ir_kgc(J, obj2gco((str)), IRT_STR) +#define lj_ir_ktab(J, tab) lj_ir_kgc(J, obj2gco((tab)), IRT_TAB) +#define lj_ir_kfunc(J, func) lj_ir_kgc(J, obj2gco((func)), IRT_FUNC) +#define lj_ir_kptr(J, ptr) lj_ir_kptr_(J, IR_KPTR, (ptr)) +#define lj_ir_kkptr(J, ptr) lj_ir_kptr_(J, IR_KKPTR, (ptr)) + +/* Special FP constants. */ +#define lj_ir_knum_zero(J) lj_ir_knum_u64(J, U64x(00000000,00000000)) +#define lj_ir_knum_one(J) lj_ir_knum_u64(J, U64x(3ff00000,00000000)) +#define lj_ir_knum_tobit(J) lj_ir_knum_u64(J, U64x(43380000,00000000)) + +/* Special 128 bit SIMD constants. */ +#define lj_ir_knum_abs(J) lj_ir_k64(J, IR_KNUM, LJ_KSIMD(J, LJ_KSIMD_ABS)) +#define lj_ir_knum_neg(J) lj_ir_k64(J, IR_KNUM, LJ_KSIMD(J, LJ_KSIMD_NEG)) + +/* Access to constants. */ +LJ_FUNC void lj_ir_kvalue(lua_State *L, TValue *tv, const IRIns *ir); + +/* Convert IR operand types. */ +LJ_FUNC TRef LJ_FASTCALL lj_ir_tonumber(jit_State *J, TRef tr); +LJ_FUNC TRef LJ_FASTCALL lj_ir_tonum(jit_State *J, TRef tr); +LJ_FUNC TRef LJ_FASTCALL lj_ir_tostr(jit_State *J, TRef tr); + +/* Miscellaneous IR ops. */ +LJ_FUNC int lj_ir_numcmp(lua_Number a, lua_Number b, IROp op); +LJ_FUNC int lj_ir_strcmp(GCstr *a, GCstr *b, IROp op); +LJ_FUNC void lj_ir_rollback(jit_State *J, IRRef ref); + +/* Emit IR instructions with on-the-fly optimizations. */ +LJ_FUNC TRef LJ_FASTCALL lj_opt_fold(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_cse(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim); + +/* Special return values for the fold functions. */ +enum { + NEXTFOLD, /* Couldn't fold, pass on. */ + RETRYFOLD, /* Retry fold with modified fins. */ + KINTFOLD, /* Return ref for int constant in fins->i. */ + FAILFOLD, /* Guard would always fail. */ + DROPFOLD, /* Guard eliminated. */ + MAX_FOLD +}; + +#define INTFOLD(k) ((J->fold.ins.i = (k)), (TRef)KINTFOLD) +#define INT64FOLD(k) (lj_ir_kint64(J, (k))) +#define CONDFOLD(cond) ((TRef)FAILFOLD + (TRef)(cond)) +#define LEFTFOLD (J->fold.ins.op1) +#define RIGHTFOLD (J->fold.ins.op2) +#define CSEFOLD (lj_opt_cse(J)) +#define EMITFOLD (lj_ir_emit(J)) + +/* Load/store forwarding. */ +LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_tab_len(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J); +LJ_FUNC int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J); +LJ_FUNC int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim); +LJ_FUNC int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref); + +/* Dead-store elimination. */ +LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J); + +/* Narrowing. */ +LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_convert(jit_State *J); +LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_index(jit_State *J, TRef key); +LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_toint(jit_State *J, TRef tr); +LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_tobit(jit_State *J, TRef tr); +#if LJ_HASFFI +LJ_FUNC TRef LJ_FASTCALL lj_opt_narrow_cindex(jit_State *J, TRef key); +#endif +LJ_FUNC TRef lj_opt_narrow_arith(jit_State *J, TRef rb, TRef rc, + TValue *vb, TValue *vc, IROp op); +LJ_FUNC TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc); +LJ_FUNC TRef lj_opt_narrow_mod(jit_State *J, TRef rb, TRef rc, TValue *vc); +LJ_FUNC TRef lj_opt_narrow_pow(jit_State *J, TRef rb, TRef rc, TValue *vc); +LJ_FUNC IRType lj_opt_narrow_forl(jit_State *J, cTValue *forbase); + +/* Optimization passes. */ +LJ_FUNC void lj_opt_dce(jit_State *J); +LJ_FUNC int lj_opt_loop(jit_State *J); +#if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) +LJ_FUNC void lj_opt_split(jit_State *J); +#else +#define lj_opt_split(J) UNUSED(J) +#endif +LJ_FUNC void lj_opt_sink(jit_State *J); + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_jit.h b/external/lua/luajit/src/src/lj_jit.h new file mode 100644 index 0000000000..c0b1c41e68 --- /dev/null +++ b/external/lua/luajit/src/src/lj_jit.h @@ -0,0 +1,416 @@ +/* +** Common definitions for the JIT compiler. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_JIT_H +#define _LJ_JIT_H + +#include "lj_obj.h" +#include "lj_ir.h" + +/* JIT engine flags. */ +#define JIT_F_ON 0x00000001 + +/* CPU-specific JIT engine flags. */ +#if LJ_TARGET_X86ORX64 +#define JIT_F_CMOV 0x00000010 +#define JIT_F_SSE2 0x00000020 +#define JIT_F_SSE3 0x00000040 +#define JIT_F_SSE4_1 0x00000080 +#define JIT_F_P4 0x00000100 +#define JIT_F_PREFER_IMUL 0x00000200 +#define JIT_F_SPLIT_XMM 0x00000400 +#define JIT_F_LEA_AGU 0x00000800 + +/* Names for the CPU-specific flags. Must match the order above. */ +#define JIT_F_CPU_FIRST JIT_F_CMOV +#define JIT_F_CPUSTRING "\4CMOV\4SSE2\4SSE3\6SSE4.1\2P4\3AMD\2K8\4ATOM" +#elif LJ_TARGET_ARM +#define JIT_F_ARMV6_ 0x00000010 +#define JIT_F_ARMV6T2_ 0x00000020 +#define JIT_F_ARMV7 0x00000040 +#define JIT_F_VFPV2 0x00000080 +#define JIT_F_VFPV3 0x00000100 + +#define JIT_F_ARMV6 (JIT_F_ARMV6_|JIT_F_ARMV6T2_|JIT_F_ARMV7) +#define JIT_F_ARMV6T2 (JIT_F_ARMV6T2_|JIT_F_ARMV7) +#define JIT_F_VFP (JIT_F_VFPV2|JIT_F_VFPV3) + +/* Names for the CPU-specific flags. Must match the order above. */ +#define JIT_F_CPU_FIRST JIT_F_ARMV6_ +#define JIT_F_CPUSTRING "\5ARMv6\7ARMv6T2\5ARMv7\5VFPv2\5VFPv3" +#elif LJ_TARGET_PPC +#define JIT_F_SQRT 0x00000010 +#define JIT_F_ROUND 0x00000020 + +/* Names for the CPU-specific flags. Must match the order above. */ +#define JIT_F_CPU_FIRST JIT_F_SQRT +#define JIT_F_CPUSTRING "\4SQRT\5ROUND" +#elif LJ_TARGET_MIPS +#define JIT_F_MIPS32R2 0x00000010 + +/* Names for the CPU-specific flags. Must match the order above. */ +#define JIT_F_CPU_FIRST JIT_F_MIPS32R2 +#define JIT_F_CPUSTRING "\010MIPS32R2" +#else +#define JIT_F_CPU_FIRST 0 +#define JIT_F_CPUSTRING "" +#endif + +/* Optimization flags. */ +#define JIT_F_OPT_MASK 0x0fff0000 + +#define JIT_F_OPT_FOLD 0x00010000 +#define JIT_F_OPT_CSE 0x00020000 +#define JIT_F_OPT_DCE 0x00040000 +#define JIT_F_OPT_FWD 0x00080000 +#define JIT_F_OPT_DSE 0x00100000 +#define JIT_F_OPT_NARROW 0x00200000 +#define JIT_F_OPT_LOOP 0x00400000 +#define JIT_F_OPT_ABC 0x00800000 +#define JIT_F_OPT_SINK 0x01000000 +#define JIT_F_OPT_FUSE 0x02000000 + +/* Optimizations names for -O. Must match the order above. */ +#define JIT_F_OPT_FIRST JIT_F_OPT_FOLD +#define JIT_F_OPTSTRING \ + "\4fold\3cse\3dce\3fwd\3dse\6narrow\4loop\3abc\4sink\4fuse" + +/* Optimization levels set a fixed combination of flags. */ +#define JIT_F_OPT_0 0 +#define JIT_F_OPT_1 (JIT_F_OPT_FOLD|JIT_F_OPT_CSE|JIT_F_OPT_DCE) +#define JIT_F_OPT_2 (JIT_F_OPT_1|JIT_F_OPT_NARROW|JIT_F_OPT_LOOP) +#define JIT_F_OPT_3 (JIT_F_OPT_2|\ + JIT_F_OPT_FWD|JIT_F_OPT_DSE|JIT_F_OPT_ABC|JIT_F_OPT_SINK|JIT_F_OPT_FUSE) +#define JIT_F_OPT_DEFAULT JIT_F_OPT_3 + +#if LJ_TARGET_WINDOWS || LJ_64 +/* See: http://blogs.msdn.com/oldnewthing/archive/2003/10/08/55239.aspx */ +#define JIT_P_sizemcode_DEFAULT 64 +#else +/* Could go as low as 4K, but the mmap() overhead would be rather high. */ +#define JIT_P_sizemcode_DEFAULT 32 +#endif + +/* Optimization parameters and their defaults. Length is a char in octal! */ +#define JIT_PARAMDEF(_) \ + _(\010, maxtrace, 1000) /* Max. # of traces in cache. */ \ + _(\011, maxrecord, 4000) /* Max. # of recorded IR instructions. */ \ + _(\012, maxirconst, 500) /* Max. # of IR constants of a trace. */ \ + _(\007, maxside, 100) /* Max. # of side traces of a root trace. */ \ + _(\007, maxsnap, 500) /* Max. # of snapshots for a trace. */ \ + \ + _(\007, hotloop, 56) /* # of iter. to detect a hot loop/call. */ \ + _(\007, hotexit, 10) /* # of taken exits to start a side trace. */ \ + _(\007, tryside, 4) /* # of attempts to compile a side trace. */ \ + \ + _(\012, instunroll, 4) /* Max. unroll for instable loops. */ \ + _(\012, loopunroll, 15) /* Max. unroll for loop ops in side traces. */ \ + _(\012, callunroll, 3) /* Max. unroll for recursive calls. */ \ + _(\011, recunroll, 2) /* Min. unroll for true recursion. */ \ + \ + /* Size of each machine code area (in KBytes). */ \ + _(\011, sizemcode, JIT_P_sizemcode_DEFAULT) \ + /* Max. total size of all machine code areas (in KBytes). */ \ + _(\010, maxmcode, 512) \ + /* End of list. */ + +enum { +#define JIT_PARAMENUM(len, name, value) JIT_P_##name, +JIT_PARAMDEF(JIT_PARAMENUM) +#undef JIT_PARAMENUM + JIT_P__MAX +}; + +#define JIT_PARAMSTR(len, name, value) #len #name +#define JIT_P_STRING JIT_PARAMDEF(JIT_PARAMSTR) + +/* Trace compiler state. */ +typedef enum { + LJ_TRACE_IDLE, /* Trace compiler idle. */ + LJ_TRACE_ACTIVE = 0x10, + LJ_TRACE_RECORD, /* Bytecode recording active. */ + LJ_TRACE_START, /* New trace started. */ + LJ_TRACE_END, /* End of trace. */ + LJ_TRACE_ASM, /* Assemble trace. */ + LJ_TRACE_ERR /* Trace aborted with error. */ +} TraceState; + +/* Post-processing action. */ +typedef enum { + LJ_POST_NONE, /* No action. */ + LJ_POST_FIXCOMP, /* Fixup comparison and emit pending guard. */ + LJ_POST_FIXGUARD, /* Fixup and emit pending guard. */ + LJ_POST_FIXGUARDSNAP, /* Fixup and emit pending guard and snapshot. */ + LJ_POST_FIXBOOL, /* Fixup boolean result. */ + LJ_POST_FIXCONST, /* Fixup constant results. */ + LJ_POST_FFRETRY /* Suppress recording of retried fast functions. */ +} PostProc; + +/* Machine code type. */ +#if LJ_TARGET_X86ORX64 +typedef uint8_t MCode; +#else +typedef uint32_t MCode; +#endif + +/* Stack snapshot header. */ +typedef struct SnapShot { + uint16_t mapofs; /* Offset into snapshot map. */ + IRRef1 ref; /* First IR ref for this snapshot. */ + uint8_t nslots; /* Number of valid slots. */ + uint8_t topslot; /* Maximum frame extent. */ + uint8_t nent; /* Number of compressed entries. */ + uint8_t count; /* Count of taken exits for this snapshot. */ +} SnapShot; + +#define SNAPCOUNT_DONE 255 /* Already compiled and linked a side trace. */ + +/* Compressed snapshot entry. */ +typedef uint32_t SnapEntry; + +#define SNAP_FRAME 0x010000 /* Frame slot. */ +#define SNAP_CONT 0x020000 /* Continuation slot. */ +#define SNAP_NORESTORE 0x040000 /* No need to restore slot. */ +#define SNAP_SOFTFPNUM 0x080000 /* Soft-float number. */ +LJ_STATIC_ASSERT(SNAP_FRAME == TREF_FRAME); +LJ_STATIC_ASSERT(SNAP_CONT == TREF_CONT); + +#define SNAP(slot, flags, ref) (((SnapEntry)(slot) << 24) + (flags) + (ref)) +#define SNAP_TR(slot, tr) \ + (((SnapEntry)(slot) << 24) + ((tr) & (TREF_CONT|TREF_FRAME|TREF_REFMASK))) +#define SNAP_MKPC(pc) ((SnapEntry)u32ptr(pc)) +#define SNAP_MKFTSZ(ftsz) ((SnapEntry)(ftsz)) +#define snap_ref(sn) ((sn) & 0xffff) +#define snap_slot(sn) ((BCReg)((sn) >> 24)) +#define snap_isframe(sn) ((sn) & SNAP_FRAME) +#define snap_pc(sn) ((const BCIns *)(uintptr_t)(sn)) +#define snap_setref(sn, ref) (((sn) & (0xffff0000&~SNAP_NORESTORE)) | (ref)) + +/* Snapshot and exit numbers. */ +typedef uint32_t SnapNo; +typedef uint32_t ExitNo; + +/* Trace number. */ +typedef uint32_t TraceNo; /* Used to pass around trace numbers. */ +typedef uint16_t TraceNo1; /* Stored trace number. */ + +/* Type of link. ORDER LJ_TRLINK */ +typedef enum { + LJ_TRLINK_NONE, /* Incomplete trace. No link, yet. */ + LJ_TRLINK_ROOT, /* Link to other root trace. */ + LJ_TRLINK_LOOP, /* Loop to same trace. */ + LJ_TRLINK_TAILREC, /* Tail-recursion. */ + LJ_TRLINK_UPREC, /* Up-recursion. */ + LJ_TRLINK_DOWNREC, /* Down-recursion. */ + LJ_TRLINK_INTERP, /* Fallback to interpreter. */ + LJ_TRLINK_RETURN /* Return to interpreter. */ +} TraceLink; + +/* Trace object. */ +typedef struct GCtrace { + GCHeader; + uint8_t topslot; /* Top stack slot already checked to be allocated. */ + uint8_t linktype; /* Type of link. */ + IRRef nins; /* Next IR instruction. Biased with REF_BIAS. */ + GCRef gclist; + IRIns *ir; /* IR instructions/constants. Biased with REF_BIAS. */ + IRRef nk; /* Lowest IR constant. Biased with REF_BIAS. */ + uint16_t nsnap; /* Number of snapshots. */ + uint16_t nsnapmap; /* Number of snapshot map elements. */ + SnapShot *snap; /* Snapshot array. */ + SnapEntry *snapmap; /* Snapshot map. */ + GCRef startpt; /* Starting prototype. */ + MRef startpc; /* Bytecode PC of starting instruction. */ + BCIns startins; /* Original bytecode of starting instruction. */ + MSize szmcode; /* Size of machine code. */ + MCode *mcode; /* Start of machine code. */ + MSize mcloop; /* Offset of loop start in machine code. */ + uint16_t nchild; /* Number of child traces (root trace only). */ + uint16_t spadjust; /* Stack pointer adjustment (offset in bytes). */ + TraceNo1 traceno; /* Trace number. */ + TraceNo1 link; /* Linked trace (or self for loops). */ + TraceNo1 root; /* Root trace of side trace (or 0 for root traces). */ + TraceNo1 nextroot; /* Next root trace for same prototype. */ + TraceNo1 nextside; /* Next side trace of same root trace. */ + uint8_t sinktags; /* Trace has SINK tags. */ + uint8_t unused1; +#ifdef LUAJIT_USE_GDBJIT + void *gdbjit_entry; /* GDB JIT entry. */ +#endif +} GCtrace; + +#define gco2trace(o) check_exp((o)->gch.gct == ~LJ_TTRACE, (GCtrace *)(o)) +#define traceref(J, n) \ + check_exp((n)>0 && (MSize)(n)sizetrace, (GCtrace *)gcref(J->trace[(n)])) + +LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtrace, gclist)); + +static LJ_AINLINE MSize snap_nextofs(GCtrace *T, SnapShot *snap) +{ + if (snap+1 == &T->snap[T->nsnap]) + return T->nsnapmap; + else + return (snap+1)->mapofs; +} + +/* Round-robin penalty cache for bytecodes leading to aborted traces. */ +typedef struct HotPenalty { + MRef pc; /* Starting bytecode PC. */ + uint16_t val; /* Penalty value, i.e. hotcount start. */ + uint16_t reason; /* Abort reason (really TraceErr). */ +} HotPenalty; + +#define PENALTY_SLOTS 64 /* Penalty cache slot. Must be a power of 2. */ +#define PENALTY_MIN (36*2) /* Minimum penalty value. */ +#define PENALTY_MAX 60000 /* Maximum penalty value. */ +#define PENALTY_RNDBITS 4 /* # of random bits to add to penalty value. */ + +/* Round-robin backpropagation cache for narrowing conversions. */ +typedef struct BPropEntry { + IRRef1 key; /* Key: original reference. */ + IRRef1 val; /* Value: reference after conversion. */ + IRRef mode; /* Mode for this entry (currently IRCONV_*). */ +} BPropEntry; + +/* Number of slots for the backpropagation cache. Must be a power of 2. */ +#define BPROP_SLOTS 16 + +/* Scalar evolution analysis cache. */ +typedef struct ScEvEntry { + IRRef1 idx; /* Index reference. */ + IRRef1 start; /* Constant start reference. */ + IRRef1 stop; /* Constant stop reference. */ + IRRef1 step; /* Constant step reference. */ + IRType1 t; /* Scalar type. */ + uint8_t dir; /* Direction. 1: +, 0: -. */ +} ScEvEntry; + +/* 128 bit SIMD constants. */ +enum { + LJ_KSIMD_ABS, + LJ_KSIMD_NEG, + LJ_KSIMD__MAX +}; + +/* Get 16 byte aligned pointer to SIMD constant. */ +#define LJ_KSIMD(J, n) \ + ((TValue *)(((intptr_t)&J->ksimd[2*(n)] + 15) & ~(intptr_t)15)) + +/* Set/reset flag to activate the SPLIT pass for the current trace. */ +#if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) +#define lj_needsplit(J) (J->needsplit = 1) +#define lj_resetsplit(J) (J->needsplit = 0) +#else +#define lj_needsplit(J) UNUSED(J) +#define lj_resetsplit(J) UNUSED(J) +#endif + +/* Fold state is used to fold instructions on-the-fly. */ +typedef struct FoldState { + IRIns ins; /* Currently emitted instruction. */ + IRIns left; /* Instruction referenced by left operand. */ + IRIns right; /* Instruction referenced by right operand. */ +} FoldState; + +/* JIT compiler state. */ +typedef struct jit_State { + GCtrace cur; /* Current trace. */ + + lua_State *L; /* Current Lua state. */ + const BCIns *pc; /* Current PC. */ + GCfunc *fn; /* Current function. */ + GCproto *pt; /* Current prototype. */ + TRef *base; /* Current frame base, points into J->slots. */ + + uint32_t flags; /* JIT engine flags. */ + BCReg maxslot; /* Relative to baseslot. */ + BCReg baseslot; /* Current frame base, offset into J->slots. */ + + uint8_t mergesnap; /* Allowed to merge with next snapshot. */ + uint8_t needsnap; /* Need snapshot before recording next bytecode. */ + IRType1 guardemit; /* Accumulated IRT_GUARD for emitted instructions. */ + uint8_t bcskip; /* Number of bytecode instructions to skip. */ + + FoldState fold; /* Fold state. */ + + const BCIns *bc_min; /* Start of allowed bytecode range for root trace. */ + MSize bc_extent; /* Extent of the range. */ + + TraceState state; /* Trace compiler state. */ + + int32_t instunroll; /* Unroll counter for instable loops. */ + int32_t loopunroll; /* Unroll counter for loop ops in side traces. */ + int32_t tailcalled; /* Number of successive tailcalls. */ + int32_t framedepth; /* Current frame depth. */ + int32_t retdepth; /* Return frame depth (count of RETF). */ + + MRef k64; /* Pointer to chained array of 64 bit constants. */ + TValue ksimd[LJ_KSIMD__MAX*2+1]; /* 16 byte aligned SIMD constants. */ + + IRIns *irbuf; /* Temp. IR instruction buffer. Biased with REF_BIAS. */ + IRRef irtoplim; /* Upper limit of instuction buffer (biased). */ + IRRef irbotlim; /* Lower limit of instuction buffer (biased). */ + IRRef loopref; /* Last loop reference or ref of final LOOP (or 0). */ + + MSize sizesnap; /* Size of temp. snapshot buffer. */ + SnapShot *snapbuf; /* Temp. snapshot buffer. */ + SnapEntry *snapmapbuf; /* Temp. snapshot map buffer. */ + MSize sizesnapmap; /* Size of temp. snapshot map buffer. */ + + PostProc postproc; /* Required post-processing after execution. */ +#if LJ_SOFTFP || (LJ_32 && LJ_HASFFI) + int needsplit; /* Need SPLIT pass. */ +#endif + + GCRef *trace; /* Array of traces. */ + TraceNo freetrace; /* Start of scan for next free trace. */ + MSize sizetrace; /* Size of trace array. */ + + IRRef1 chain[IR__MAX]; /* IR instruction skip-list chain anchors. */ + TRef slot[LJ_MAX_JSLOTS+LJ_STACK_EXTRA]; /* Stack slot map. */ + + int32_t param[JIT_P__MAX]; /* JIT engine parameters. */ + + MCode *exitstubgroup[LJ_MAX_EXITSTUBGR]; /* Exit stub group addresses. */ + + HotPenalty penalty[PENALTY_SLOTS]; /* Penalty slots. */ + uint32_t penaltyslot; /* Round-robin index into penalty slots. */ + uint32_t prngstate; /* PRNG state. */ + + BPropEntry bpropcache[BPROP_SLOTS]; /* Backpropagation cache slots. */ + uint32_t bpropslot; /* Round-robin index into bpropcache slots. */ + + ScEvEntry scev; /* Scalar evolution analysis cache slots. */ + + const BCIns *startpc; /* Bytecode PC of starting instruction. */ + TraceNo parent; /* Parent of current side trace (0 for root traces). */ + ExitNo exitno; /* Exit number in parent of current side trace. */ + + BCIns *patchpc; /* PC for pending re-patch. */ + BCIns patchins; /* Instruction for pending re-patch. */ + + int mcprot; /* Protection of current mcode area. */ + MCode *mcarea; /* Base of current mcode area. */ + MCode *mctop; /* Top of current mcode area. */ + MCode *mcbot; /* Bottom of current mcode area. */ + size_t szmcarea; /* Size of current mcode area. */ + size_t szallmcarea; /* Total size of all allocated mcode areas. */ + + TValue errinfo; /* Additional info element for trace errors. */ +} +#if LJ_TARGET_ARM +LJ_ALIGN(16) /* For DISPATCH-relative addresses in assembler part. */ +#endif +jit_State; + +/* Trivial PRNG e.g. used for penalty randomization. */ +static LJ_AINLINE uint32_t LJ_PRNG_BITS(jit_State *J, int bits) +{ + /* Yes, this LCG is very weak, but that doesn't matter for our use case. */ + J->prngstate = J->prngstate * 1103515245 + 12345; + return J->prngstate >> (32-bits); +} + +#endif diff --git a/external/lua/luajit/src/src/lj_lex.c b/external/lua/luajit/src/src/lj_lex.c new file mode 100644 index 0000000000..9f2b06f8f5 --- /dev/null +++ b/external/lua/luajit/src/src/lj_lex.c @@ -0,0 +1,481 @@ +/* +** Lexical analyzer. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lj_lex_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#if LJ_HASFFI +#include "lj_tab.h" +#include "lj_ctype.h" +#include "lj_cdata.h" +#include "lualib.h" +#endif +#include "lj_state.h" +#include "lj_lex.h" +#include "lj_parse.h" +#include "lj_char.h" +#include "lj_strscan.h" + +/* Lua lexer token names. */ +static const char *const tokennames[] = { +#define TKSTR1(name) #name, +#define TKSTR2(name, sym) #sym, +TKDEF(TKSTR1, TKSTR2) +#undef TKSTR1 +#undef TKSTR2 + NULL +}; + +/* -- Buffer handling ----------------------------------------------------- */ + +#define char2int(c) ((int)(uint8_t)(c)) +#define next(ls) \ + (ls->current = (ls->n--) > 0 ? char2int(*ls->p++) : fillbuf(ls)) +#define save_and_next(ls) (save(ls, ls->current), next(ls)) +#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') +#define END_OF_STREAM (-1) + +static int fillbuf(LexState *ls) +{ + size_t sz; + const char *buf = ls->rfunc(ls->L, ls->rdata, &sz); + if (buf == NULL || sz == 0) return END_OF_STREAM; + ls->n = (MSize)sz - 1; + ls->p = buf; + return char2int(*(ls->p++)); +} + +static LJ_NOINLINE void save_grow(LexState *ls, int c) +{ + MSize newsize; + if (ls->sb.sz >= LJ_MAX_STR/2) + lj_lex_error(ls, 0, LJ_ERR_XELEM); + newsize = ls->sb.sz * 2; + lj_str_resizebuf(ls->L, &ls->sb, newsize); + ls->sb.buf[ls->sb.n++] = (char)c; +} + +static LJ_AINLINE void save(LexState *ls, int c) +{ + if (LJ_UNLIKELY(ls->sb.n + 1 > ls->sb.sz)) + save_grow(ls, c); + else + ls->sb.buf[ls->sb.n++] = (char)c; +} + +static void inclinenumber(LexState *ls) +{ + int old = ls->current; + lua_assert(currIsNewline(ls)); + next(ls); /* skip `\n' or `\r' */ + if (currIsNewline(ls) && ls->current != old) + next(ls); /* skip `\n\r' or `\r\n' */ + if (++ls->linenumber >= LJ_MAX_LINE) + lj_lex_error(ls, ls->token, LJ_ERR_XLINES); +} + +/* -- Scanner for terminals ----------------------------------------------- */ + +/* Parse a number literal. */ +static void lex_number(LexState *ls, TValue *tv) +{ + StrScanFmt fmt; + int c, xp = 'e'; + lua_assert(lj_char_isdigit(ls->current)); + if ((c = ls->current) == '0') { + save_and_next(ls); + if ((ls->current | 0x20) == 'x') xp = 'p'; + } + while (lj_char_isident(ls->current) || ls->current == '.' || + ((ls->current == '-' || ls->current == '+') && (c | 0x20) == xp)) { + c = ls->current; + save_and_next(ls); + } + save(ls, '\0'); + fmt = lj_strscan_scan((const uint8_t *)ls->sb.buf, tv, + (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) | + (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0)); + if (LJ_DUALNUM && fmt == STRSCAN_INT) { + setitype(tv, LJ_TISNUM); + } else if (fmt == STRSCAN_NUM) { + /* Already in correct format. */ +#if LJ_HASFFI + } else if (fmt != STRSCAN_ERROR) { + lua_State *L = ls->L; + GCcdata *cd; + lua_assert(fmt == STRSCAN_I64 || fmt == STRSCAN_U64 || fmt == STRSCAN_IMAG); + if (!ctype_ctsG(G(L))) { + ptrdiff_t oldtop = savestack(L, L->top); + luaopen_ffi(L); /* Load FFI library on-demand. */ + L->top = restorestack(L, oldtop); + } + if (fmt == STRSCAN_IMAG) { + cd = lj_cdata_new_(L, CTID_COMPLEX_DOUBLE, 2*sizeof(double)); + ((double *)cdataptr(cd))[0] = 0; + ((double *)cdataptr(cd))[1] = numV(tv); + } else { + cd = lj_cdata_new_(L, fmt==STRSCAN_I64 ? CTID_INT64 : CTID_UINT64, 8); + *(uint64_t *)cdataptr(cd) = tv->u64; + } + lj_parse_keepcdata(ls, tv, cd); +#endif + } else { + lua_assert(fmt == STRSCAN_ERROR); + lj_lex_error(ls, TK_number, LJ_ERR_XNUMBER); + } +} + +static int skip_sep(LexState *ls) +{ + int count = 0; + int s = ls->current; + lua_assert(s == '[' || s == ']'); + save_and_next(ls); + while (ls->current == '=') { + save_and_next(ls); + count++; + } + return (ls->current == s) ? count : (-count) - 1; +} + +static void read_long_string(LexState *ls, TValue *tv, int sep) +{ + save_and_next(ls); /* skip 2nd `[' */ + if (currIsNewline(ls)) /* string starts with a newline? */ + inclinenumber(ls); /* skip it */ + for (;;) { + switch (ls->current) { + case END_OF_STREAM: + lj_lex_error(ls, TK_eof, tv ? LJ_ERR_XLSTR : LJ_ERR_XLCOM); + break; + case ']': + if (skip_sep(ls) == sep) { + save_and_next(ls); /* skip 2nd `]' */ + goto endloop; + } + break; + case '\n': + case '\r': + save(ls, '\n'); + inclinenumber(ls); + if (!tv) lj_str_resetbuf(&ls->sb); /* avoid wasting space */ + break; + default: + if (tv) save_and_next(ls); + else next(ls); + break; + } + } endloop: + if (tv) { + GCstr *str = lj_parse_keepstr(ls, ls->sb.buf + (2 + (MSize)sep), + ls->sb.n - 2*(2 + (MSize)sep)); + setstrV(ls->L, tv, str); + } +} + +static void read_string(LexState *ls, int delim, TValue *tv) +{ + save_and_next(ls); + while (ls->current != delim) { + switch (ls->current) { + case END_OF_STREAM: + lj_lex_error(ls, TK_eof, LJ_ERR_XSTR); + continue; + case '\n': + case '\r': + lj_lex_error(ls, TK_string, LJ_ERR_XSTR); + continue; + case '\\': { + int c = next(ls); /* Skip the '\\'. */ + switch (c) { + case 'a': c = '\a'; break; + case 'b': c = '\b'; break; + case 'f': c = '\f'; break; + case 'n': c = '\n'; break; + case 'r': c = '\r'; break; + case 't': c = '\t'; break; + case 'v': c = '\v'; break; + case 'x': /* Hexadecimal escape '\xXX'. */ + c = (next(ls) & 15u) << 4; + if (!lj_char_isdigit(ls->current)) { + if (!lj_char_isxdigit(ls->current)) goto err_xesc; + c += 9 << 4; + } + c += (next(ls) & 15u); + if (!lj_char_isdigit(ls->current)) { + if (!lj_char_isxdigit(ls->current)) goto err_xesc; + c += 9; + } + break; + case 'z': /* Skip whitespace. */ + next(ls); + while (lj_char_isspace(ls->current)) + if (currIsNewline(ls)) inclinenumber(ls); else next(ls); + continue; + case '\n': case '\r': save(ls, '\n'); inclinenumber(ls); continue; + case '\\': case '\"': case '\'': break; + case END_OF_STREAM: continue; + default: + if (!lj_char_isdigit(c)) + goto err_xesc; + c -= '0'; /* Decimal escape '\ddd'. */ + if (lj_char_isdigit(next(ls))) { + c = c*10 + (ls->current - '0'); + if (lj_char_isdigit(next(ls))) { + c = c*10 + (ls->current - '0'); + if (c > 255) { + err_xesc: + lj_lex_error(ls, TK_string, LJ_ERR_XESC); + } + next(ls); + } + } + save(ls, c); + continue; + } + save(ls, c); + next(ls); + continue; + } + default: + save_and_next(ls); + break; + } + } + save_and_next(ls); /* skip delimiter */ + setstrV(ls->L, tv, lj_parse_keepstr(ls, ls->sb.buf + 1, ls->sb.n - 2)); +} + +/* -- Main lexical scanner ------------------------------------------------ */ + +static int llex(LexState *ls, TValue *tv) +{ + lj_str_resetbuf(&ls->sb); + for (;;) { + if (lj_char_isident(ls->current)) { + GCstr *s; + if (lj_char_isdigit(ls->current)) { /* Numeric literal. */ + lex_number(ls, tv); + return TK_number; + } + /* Identifier or reserved word. */ + do { + save_and_next(ls); + } while (lj_char_isident(ls->current)); + s = lj_parse_keepstr(ls, ls->sb.buf, ls->sb.n); + setstrV(ls->L, tv, s); + if (s->reserved > 0) /* Reserved word? */ + return TK_OFS + s->reserved; + return TK_name; + } + switch (ls->current) { + case '\n': + case '\r': + inclinenumber(ls); + continue; + case ' ': + case '\t': + case '\v': + case '\f': + next(ls); + continue; + case '-': + next(ls); + if (ls->current != '-') return '-'; + /* else is a comment */ + next(ls); + if (ls->current == '[') { + int sep = skip_sep(ls); + lj_str_resetbuf(&ls->sb); /* `skip_sep' may dirty the buffer */ + if (sep >= 0) { + read_long_string(ls, NULL, sep); /* long comment */ + lj_str_resetbuf(&ls->sb); + continue; + } + } + /* else short comment */ + while (!currIsNewline(ls) && ls->current != END_OF_STREAM) + next(ls); + continue; + case '[': { + int sep = skip_sep(ls); + if (sep >= 0) { + read_long_string(ls, tv, sep); + return TK_string; + } else if (sep == -1) { + return '['; + } else { + lj_lex_error(ls, TK_string, LJ_ERR_XLDELIM); + continue; + } + } + case '=': + next(ls); + if (ls->current != '=') return '='; else { next(ls); return TK_eq; } + case '<': + next(ls); + if (ls->current != '=') return '<'; else { next(ls); return TK_le; } + case '>': + next(ls); + if (ls->current != '=') return '>'; else { next(ls); return TK_ge; } + case '~': + next(ls); + if (ls->current != '=') return '~'; else { next(ls); return TK_ne; } + case ':': + next(ls); + if (ls->current != ':') return ':'; else { next(ls); return TK_label; } + case '"': + case '\'': + read_string(ls, ls->current, tv); + return TK_string; + case '.': + save_and_next(ls); + if (ls->current == '.') { + next(ls); + if (ls->current == '.') { + next(ls); + return TK_dots; /* ... */ + } + return TK_concat; /* .. */ + } else if (!lj_char_isdigit(ls->current)) { + return '.'; + } else { + lex_number(ls, tv); + return TK_number; + } + case END_OF_STREAM: + return TK_eof; + default: { + int c = ls->current; + next(ls); + return c; /* Single-char tokens (+ - / ...). */ + } + } + } +} + +/* -- Lexer API ----------------------------------------------------------- */ + +/* Setup lexer state. */ +int lj_lex_setup(lua_State *L, LexState *ls) +{ + int header = 0; + ls->L = L; + ls->fs = NULL; + ls->n = 0; + ls->p = NULL; + ls->vstack = NULL; + ls->sizevstack = 0; + ls->vtop = 0; + ls->bcstack = NULL; + ls->sizebcstack = 0; + ls->lookahead = TK_eof; /* No look-ahead token. */ + ls->linenumber = 1; + ls->lastline = 1; + lj_str_resizebuf(ls->L, &ls->sb, LJ_MIN_SBUF); + next(ls); /* Read-ahead first char. */ + if (ls->current == 0xef && ls->n >= 2 && char2int(ls->p[0]) == 0xbb && + char2int(ls->p[1]) == 0xbf) { /* Skip UTF-8 BOM (if buffered). */ + ls->n -= 2; + ls->p += 2; + next(ls); + header = 1; + } + if (ls->current == '#') { /* Skip POSIX #! header line. */ + do { + next(ls); + if (ls->current == END_OF_STREAM) return 0; + } while (!currIsNewline(ls)); + inclinenumber(ls); + header = 1; + } + if (ls->current == LUA_SIGNATURE[0]) { /* Bytecode dump. */ + if (header) { + /* + ** Loading bytecode with an extra header is disabled for security + ** reasons. This may circumvent the usual check for bytecode vs. + ** Lua code by looking at the first char. Since this is a potential + ** security violation no attempt is made to echo the chunkname either. + */ + setstrV(L, L->top++, lj_err_str(L, LJ_ERR_BCBAD)); + lj_err_throw(L, LUA_ERRSYNTAX); + } + return 1; + } + return 0; +} + +/* Cleanup lexer state. */ +void lj_lex_cleanup(lua_State *L, LexState *ls) +{ + global_State *g = G(L); + lj_mem_freevec(g, ls->bcstack, ls->sizebcstack, BCInsLine); + lj_mem_freevec(g, ls->vstack, ls->sizevstack, VarInfo); + lj_str_freebuf(g, &ls->sb); +} + +void lj_lex_next(LexState *ls) +{ + ls->lastline = ls->linenumber; + if (LJ_LIKELY(ls->lookahead == TK_eof)) { /* No lookahead token? */ + ls->token = llex(ls, &ls->tokenval); /* Get next token. */ + } else { /* Otherwise return lookahead token. */ + ls->token = ls->lookahead; + ls->lookahead = TK_eof; + ls->tokenval = ls->lookaheadval; + } +} + +LexToken lj_lex_lookahead(LexState *ls) +{ + lua_assert(ls->lookahead == TK_eof); + ls->lookahead = llex(ls, &ls->lookaheadval); + return ls->lookahead; +} + +const char *lj_lex_token2str(LexState *ls, LexToken token) +{ + if (token > TK_OFS) + return tokennames[token-TK_OFS-1]; + else if (!lj_char_iscntrl(token)) + return lj_str_pushf(ls->L, "%c", token); + else + return lj_str_pushf(ls->L, "char(%d)", token); +} + +void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...) +{ + const char *tok; + va_list argp; + if (token == 0) { + tok = NULL; + } else if (token == TK_name || token == TK_string || token == TK_number) { + save(ls, '\0'); + tok = ls->sb.buf; + } else { + tok = lj_lex_token2str(ls, token); + } + va_start(argp, em); + lj_err_lex(ls->L, ls->chunkname, tok, ls->linenumber, em, argp); + va_end(argp); +} + +void lj_lex_init(lua_State *L) +{ + uint32_t i; + for (i = 0; i < TK_RESERVED; i++) { + GCstr *s = lj_str_newz(L, tokennames[i]); + fixstring(s); /* Reserved words are never collected. */ + s->reserved = (uint8_t)(i+1); + } +} + diff --git a/external/lua/luajit/src/src/lj_lex.h b/external/lua/luajit/src/src/lj_lex.h new file mode 100644 index 0000000000..6e18e4b080 --- /dev/null +++ b/external/lua/luajit/src/src/lj_lex.h @@ -0,0 +1,85 @@ +/* +** Lexical analyzer. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_LEX_H +#define _LJ_LEX_H + +#include + +#include "lj_obj.h" +#include "lj_err.h" + +/* Lua lexer tokens. */ +#define TKDEF(_, __) \ + _(and) _(break) _(do) _(else) _(elseif) _(end) _(false) \ + _(for) _(function) _(goto) _(if) _(in) _(local) _(nil) _(not) _(or) \ + _(repeat) _(return) _(then) _(true) _(until) _(while) \ + __(concat, ..) __(dots, ...) __(eq, ==) __(ge, >=) __(le, <=) __(ne, ~=) \ + __(label, ::) __(number, ) __(name, ) __(string, ) \ + __(eof, ) + +enum { + TK_OFS = 256, +#define TKENUM1(name) TK_##name, +#define TKENUM2(name, sym) TK_##name, +TKDEF(TKENUM1, TKENUM2) +#undef TKENUM1 +#undef TKENUM2 + TK_RESERVED = TK_while - TK_OFS +}; + +typedef int LexToken; + +/* Combined bytecode ins/line. Only used during bytecode generation. */ +typedef struct BCInsLine { + BCIns ins; /* Bytecode instruction. */ + BCLine line; /* Line number for this bytecode. */ +} BCInsLine; + +/* Info for local variables. Only used during bytecode generation. */ +typedef struct VarInfo { + GCRef name; /* Local variable name or goto/label name. */ + BCPos startpc; /* First point where the local variable is active. */ + BCPos endpc; /* First point where the local variable is dead. */ + uint8_t slot; /* Variable slot. */ + uint8_t info; /* Variable/goto/label info. */ +} VarInfo; + +/* Lua lexer state. */ +typedef struct LexState { + struct FuncState *fs; /* Current FuncState. Defined in lj_parse.c. */ + struct lua_State *L; /* Lua state. */ + TValue tokenval; /* Current token value. */ + TValue lookaheadval; /* Lookahead token value. */ + int current; /* Current character (charint). */ + LexToken token; /* Current token. */ + LexToken lookahead; /* Lookahead token. */ + MSize n; /* Bytes left in input buffer. */ + const char *p; /* Current position in input buffer. */ + SBuf sb; /* String buffer for tokens. */ + lua_Reader rfunc; /* Reader callback. */ + void *rdata; /* Reader callback data. */ + BCLine linenumber; /* Input line counter. */ + BCLine lastline; /* Line of last token. */ + GCstr *chunkname; /* Current chunk name (interned string). */ + const char *chunkarg; /* Chunk name argument. */ + const char *mode; /* Allow loading bytecode (b) and/or source text (t). */ + VarInfo *vstack; /* Stack for names and extents of local variables. */ + MSize sizevstack; /* Size of variable stack. */ + MSize vtop; /* Top of variable stack. */ + BCInsLine *bcstack; /* Stack for bytecode instructions/line numbers. */ + MSize sizebcstack; /* Size of bytecode stack. */ + uint32_t level; /* Syntactical nesting level. */ +} LexState; + +LJ_FUNC int lj_lex_setup(lua_State *L, LexState *ls); +LJ_FUNC void lj_lex_cleanup(lua_State *L, LexState *ls); +LJ_FUNC void lj_lex_next(LexState *ls); +LJ_FUNC LexToken lj_lex_lookahead(LexState *ls); +LJ_FUNC const char *lj_lex_token2str(LexState *ls, LexToken token); +LJ_FUNC_NORET void lj_lex_error(LexState *ls, LexToken token, ErrMsg em, ...); +LJ_FUNC void lj_lex_init(lua_State *L); + +#endif diff --git a/external/lua/luajit/src/src/lj_lib.c b/external/lua/luajit/src/src/lj_lib.c new file mode 100644 index 0000000000..331eaa6a8c --- /dev/null +++ b/external/lua/luajit/src/src/lj_lib.c @@ -0,0 +1,258 @@ +/* +** Library function support. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_lib_c +#define LUA_CORE + +#include "lauxlib.h" + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_func.h" +#include "lj_bc.h" +#include "lj_dispatch.h" +#include "lj_vm.h" +#include "lj_strscan.h" +#include "lj_lib.h" + +/* -- Library initialization ---------------------------------------------- */ + +static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize) +{ + if (libname) { + luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); + lua_getfield(L, -1, libname); + if (!tvistab(L->top-1)) { + L->top--; + if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, hsize) != NULL) + lj_err_callerv(L, LJ_ERR_BADMODN, libname); + settabV(L, L->top, tabV(L->top-1)); + L->top++; + lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ + } + L->top--; + settabV(L, L->top-1, tabV(L->top)); + } else { + lua_createtable(L, 0, hsize); + } + return tabV(L->top-1); +} + +void lj_lib_register(lua_State *L, const char *libname, + const uint8_t *p, const lua_CFunction *cf) +{ + GCtab *env = tabref(L->env); + GCfunc *ofn = NULL; + int ffid = *p++; + BCIns *bcff = &L2GG(L)->bcff[*p++]; + GCtab *tab = lib_create_table(L, libname, *p++); + ptrdiff_t tpos = L->top - L->base; + + /* Avoid barriers further down. */ + lj_gc_anybarriert(L, tab); + tab->nomm = 0; + + for (;;) { + uint32_t tag = *p++; + MSize len = tag & LIBINIT_LENMASK; + tag &= LIBINIT_TAGMASK; + if (tag != LIBINIT_STRING) { + const char *name; + MSize nuv = (MSize)(L->top - L->base - tpos); + GCfunc *fn = lj_func_newC(L, nuv, env); + if (nuv) { + L->top = L->base + tpos; + memcpy(fn->c.upvalue, L->top, sizeof(TValue)*nuv); + } + fn->c.ffid = (uint8_t)(ffid++); + name = (const char *)p; + p += len; + if (tag == LIBINIT_CF) + setmref(fn->c.pc, &G(L)->bc_cfunc_int); + else + setmref(fn->c.pc, bcff++); + if (tag == LIBINIT_ASM_) + fn->c.f = ofn->c.f; /* Copy handler from previous function. */ + else + fn->c.f = *cf++; /* Get cf or handler from C function table. */ + if (len) { + /* NOBARRIER: See above for common barrier. */ + setfuncV(L, lj_tab_setstr(L, tab, lj_str_new(L, name, len)), fn); + } + ofn = fn; + } else { + switch (tag | len) { + case LIBINIT_SET: + L->top -= 2; + if (tvisstr(L->top+1) && strV(L->top+1)->len == 0) + env = tabV(L->top); + else /* NOBARRIER: See above for common barrier. */ + copyTV(L, lj_tab_set(L, tab, L->top+1), L->top); + break; + case LIBINIT_NUMBER: + memcpy(&L->top->n, p, sizeof(double)); + L->top++; + p += sizeof(double); + break; + case LIBINIT_COPY: + copyTV(L, L->top, L->top - *p++); + L->top++; + break; + case LIBINIT_LASTCL: + setfuncV(L, L->top++, ofn); + break; + case LIBINIT_FFID: + ffid++; + break; + case LIBINIT_END: + return; + default: + setstrV(L, L->top++, lj_str_new(L, (const char *)p, len)); + p += len; + break; + } + } + } +} + +/* -- Type checks --------------------------------------------------------- */ + +TValue *lj_lib_checkany(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (o >= L->top) + lj_err_arg(L, narg, LJ_ERR_NOVAL); + return o; +} + +GCstr *lj_lib_checkstr(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (o < L->top) { + if (LJ_LIKELY(tvisstr(o))) { + return strV(o); + } else if (tvisnumber(o)) { + GCstr *s = lj_str_fromnumber(L, o); + setstrV(L, o, s); + return s; + } + } + lj_err_argt(L, narg, LUA_TSTRING); + return NULL; /* unreachable */ +} + +GCstr *lj_lib_optstr(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + return (o < L->top && !tvisnil(o)) ? lj_lib_checkstr(L, narg) : NULL; +} + +#if LJ_DUALNUM +void lj_lib_checknumber(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (!(o < L->top && lj_strscan_numberobj(o))) + lj_err_argt(L, narg, LUA_TNUMBER); +} +#endif + +lua_Number lj_lib_checknum(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (!(o < L->top && + (tvisnumber(o) || (tvisstr(o) && lj_strscan_num(strV(o), o))))) + lj_err_argt(L, narg, LUA_TNUMBER); + if (LJ_UNLIKELY(tvisint(o))) { + lua_Number n = (lua_Number)intV(o); + setnumV(o, n); + return n; + } else { + return numV(o); + } +} + +int32_t lj_lib_checkint(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (!(o < L->top && lj_strscan_numberobj(o))) + lj_err_argt(L, narg, LUA_TNUMBER); + if (LJ_LIKELY(tvisint(o))) { + return intV(o); + } else { + int32_t i = lj_num2int(numV(o)); + if (LJ_DUALNUM) setintV(o, i); + return i; + } +} + +int32_t lj_lib_optint(lua_State *L, int narg, int32_t def) +{ + TValue *o = L->base + narg-1; + return (o < L->top && !tvisnil(o)) ? lj_lib_checkint(L, narg) : def; +} + +int32_t lj_lib_checkbit(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (!(o < L->top && lj_strscan_numberobj(o))) + lj_err_argt(L, narg, LUA_TNUMBER); + if (LJ_LIKELY(tvisint(o))) { + return intV(o); + } else { + int32_t i = lj_num2bit(numV(o)); + if (LJ_DUALNUM) setintV(o, i); + return i; + } +} + +GCfunc *lj_lib_checkfunc(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (!(o < L->top && tvisfunc(o))) + lj_err_argt(L, narg, LUA_TFUNCTION); + return funcV(o); +} + +GCtab *lj_lib_checktab(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (!(o < L->top && tvistab(o))) + lj_err_argt(L, narg, LUA_TTABLE); + return tabV(o); +} + +GCtab *lj_lib_checktabornil(lua_State *L, int narg) +{ + TValue *o = L->base + narg-1; + if (o < L->top) { + if (tvistab(o)) + return tabV(o); + else if (tvisnil(o)) + return NULL; + } + lj_err_arg(L, narg, LJ_ERR_NOTABN); + return NULL; /* unreachable */ +} + +int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst) +{ + GCstr *s = def >= 0 ? lj_lib_optstr(L, narg) : lj_lib_checkstr(L, narg); + if (s) { + const char *opt = strdata(s); + MSize len = s->len; + int i; + for (i = 0; *(const uint8_t *)lst; i++) { + if (*(const uint8_t *)lst == len && memcmp(opt, lst+1, len) == 0) + return i; + lst += 1+*(const uint8_t *)lst; + } + lj_err_argv(L, narg, LJ_ERR_INVOPTM, opt); + } + return def; +} + diff --git a/external/lua/luajit/src/src/lj_lib.h b/external/lua/luajit/src/src/lj_lib.h new file mode 100644 index 0000000000..2fe6d2a8d4 --- /dev/null +++ b/external/lua/luajit/src/src/lj_lib.h @@ -0,0 +1,112 @@ +/* +** Library function support. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_LIB_H +#define _LJ_LIB_H + +#include "lj_obj.h" + +/* +** A fallback handler is called by the assembler VM if the fast path fails: +** +** - too few arguments: unrecoverable. +** - wrong argument type: recoverable, if coercion succeeds. +** - bad argument value: unrecoverable. +** - stack overflow: recoverable, if stack reallocation succeeds. +** - extra handling: recoverable. +** +** The unrecoverable cases throw an error with lj_err_arg(), lj_err_argtype(), +** lj_err_caller() or lj_err_callermsg(). +** The recoverable cases return 0 or the number of results + 1. +** The assembler VM retries the fast path only if 0 is returned. +** This time the fallback must not be called again or it gets stuck in a loop. +*/ + +/* Return values from fallback handler. */ +#define FFH_RETRY 0 +#define FFH_UNREACHABLE FFH_RETRY +#define FFH_RES(n) ((n)+1) +#define FFH_TAILCALL (-1) + +LJ_FUNC TValue *lj_lib_checkany(lua_State *L, int narg); +LJ_FUNC GCstr *lj_lib_checkstr(lua_State *L, int narg); +LJ_FUNC GCstr *lj_lib_optstr(lua_State *L, int narg); +#if LJ_DUALNUM +LJ_FUNC void lj_lib_checknumber(lua_State *L, int narg); +#else +#define lj_lib_checknumber(L, narg) lj_lib_checknum((L), (narg)) +#endif +LJ_FUNC lua_Number lj_lib_checknum(lua_State *L, int narg); +LJ_FUNC int32_t lj_lib_checkint(lua_State *L, int narg); +LJ_FUNC int32_t lj_lib_optint(lua_State *L, int narg, int32_t def); +LJ_FUNC int32_t lj_lib_checkbit(lua_State *L, int narg); +LJ_FUNC GCfunc *lj_lib_checkfunc(lua_State *L, int narg); +LJ_FUNC GCtab *lj_lib_checktab(lua_State *L, int narg); +LJ_FUNC GCtab *lj_lib_checktabornil(lua_State *L, int narg); +LJ_FUNC int lj_lib_checkopt(lua_State *L, int narg, int def, const char *lst); + +/* Avoid including lj_frame.h. */ +#define lj_lib_upvalue(L, n) \ + (&gcref((L->base-1)->fr.func)->fn.c.upvalue[(n)-1]) + +#if LJ_TARGET_WINDOWS +#define lj_lib_checkfpu(L) \ + do { setnumV(L->top++, (lua_Number)1437217655); \ + if (lua_tointeger(L, -1) != 1437217655) lj_err_caller(L, LJ_ERR_BADFPU); \ + L->top--; } while (0) +#else +#define lj_lib_checkfpu(L) UNUSED(L) +#endif + +/* Push internal function on the stack. */ +static LJ_AINLINE void lj_lib_pushcc(lua_State *L, lua_CFunction f, + int id, int n) +{ + GCfunc *fn; + lua_pushcclosure(L, f, n); + fn = funcV(L->top-1); + fn->c.ffid = (uint8_t)id; + setmref(fn->c.pc, &G(L)->bc_cfunc_int); +} + +#define lj_lib_pushcf(L, fn, id) (lj_lib_pushcc(L, (fn), (id), 0)) + +/* Library function declarations. Scanned by buildvm. */ +#define LJLIB_CF(name) static int lj_cf_##name(lua_State *L) +#define LJLIB_ASM(name) static int lj_ffh_##name(lua_State *L) +#define LJLIB_ASM_(name) +#define LJLIB_SET(name) +#define LJLIB_PUSH(arg) +#define LJLIB_REC(handler) +#define LJLIB_NOREGUV +#define LJLIB_NOREG + +#define LJ_LIB_REG(L, regname, name) \ + lj_lib_register(L, regname, lj_lib_init_##name, lj_lib_cf_##name) + +LJ_FUNC void lj_lib_register(lua_State *L, const char *libname, + const uint8_t *init, const lua_CFunction *cf); + +/* Library init data tags. */ +#define LIBINIT_LENMASK 0x3f +#define LIBINIT_TAGMASK 0xc0 +#define LIBINIT_CF 0x00 +#define LIBINIT_ASM 0x40 +#define LIBINIT_ASM_ 0x80 +#define LIBINIT_STRING 0xc0 +#define LIBINIT_MAXSTR 0x39 +#define LIBINIT_SET 0xfa +#define LIBINIT_NUMBER 0xfb +#define LIBINIT_COPY 0xfc +#define LIBINIT_LASTCL 0xfd +#define LIBINIT_FFID 0xfe +#define LIBINIT_END 0xff + +/* Exported library functions. */ + +typedef struct RandomState RandomState; +LJ_FUNC uint64_t LJ_FASTCALL lj_math_random_step(RandomState *rs); + +#endif diff --git a/external/lua/luajit/src/src/lj_load.c b/external/lua/luajit/src/src/lj_load.c new file mode 100644 index 0000000000..9d892678e9 --- /dev/null +++ b/external/lua/luajit/src/src/lj_load.c @@ -0,0 +1,168 @@ +/* +** Load and dump code. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include +#include + +#define lj_load_c +#define LUA_CORE + +#include "lua.h" +#include "lauxlib.h" + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_func.h" +#include "lj_frame.h" +#include "lj_vm.h" +#include "lj_lex.h" +#include "lj_bcdump.h" +#include "lj_parse.h" + +/* -- Load Lua source code and bytecode ----------------------------------- */ + +static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud) +{ + LexState *ls = (LexState *)ud; + GCproto *pt; + GCfunc *fn; + int bc; + UNUSED(dummy); + cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ + bc = lj_lex_setup(L, ls); + if (ls->mode && !strchr(ls->mode, bc ? 'b' : 't')) { + setstrV(L, L->top++, lj_err_str(L, LJ_ERR_XMODE)); + lj_err_throw(L, LUA_ERRSYNTAX); + } + pt = bc ? lj_bcread(ls) : lj_parse(ls); + fn = lj_func_newL_empty(L, pt, tabref(L->env)); + /* Don't combine above/below into one statement. */ + setfuncV(L, L->top++, fn); + return NULL; +} + +LUA_API int lua_loadx(lua_State *L, lua_Reader reader, void *data, + const char *chunkname, const char *mode) +{ + LexState ls; + int status; + ls.rfunc = reader; + ls.rdata = data; + ls.chunkarg = chunkname ? chunkname : "?"; + ls.mode = mode; + lj_str_initbuf(&ls.sb); + status = lj_vm_cpcall(L, NULL, &ls, cpparser); + lj_lex_cleanup(L, &ls); + lj_gc_check(L); + return status; +} + +LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data, + const char *chunkname) +{ + return lua_loadx(L, reader, data, chunkname, NULL); +} + +typedef struct FileReaderCtx { + FILE *fp; + char buf[LUAL_BUFFERSIZE]; +} FileReaderCtx; + +static const char *reader_file(lua_State *L, void *ud, size_t *size) +{ + FileReaderCtx *ctx = (FileReaderCtx *)ud; + UNUSED(L); + if (feof(ctx->fp)) return NULL; + *size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp); + return *size > 0 ? ctx->buf : NULL; +} + +LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename, + const char *mode) +{ + FileReaderCtx ctx; + int status; + const char *chunkname; + if (filename) { + ctx.fp = fopen(filename, "rb"); + if (ctx.fp == NULL) { + lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno)); + return LUA_ERRFILE; + } + chunkname = lua_pushfstring(L, "@%s", filename); + } else { + ctx.fp = stdin; + chunkname = "=stdin"; + } + status = lua_loadx(L, reader_file, &ctx, chunkname, mode); + if (ferror(ctx.fp)) { + L->top -= filename ? 2 : 1; + lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno)); + if (filename) + fclose(ctx.fp); + return LUA_ERRFILE; + } + if (filename) { + L->top--; + copyTV(L, L->top-1, L->top); + fclose(ctx.fp); + } + return status; +} + +LUALIB_API int luaL_loadfile(lua_State *L, const char *filename) +{ + return luaL_loadfilex(L, filename, NULL); +} + +typedef struct StringReaderCtx { + const char *str; + size_t size; +} StringReaderCtx; + +static const char *reader_string(lua_State *L, void *ud, size_t *size) +{ + StringReaderCtx *ctx = (StringReaderCtx *)ud; + UNUSED(L); + if (ctx->size == 0) return NULL; + *size = ctx->size; + ctx->size = 0; + return ctx->str; +} + +LUALIB_API int luaL_loadbufferx(lua_State *L, const char *buf, size_t size, + const char *name, const char *mode) +{ + StringReaderCtx ctx; + ctx.str = buf; + ctx.size = size; + return lua_loadx(L, reader_string, &ctx, name, mode); +} + +LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size, + const char *name) +{ + return luaL_loadbufferx(L, buf, size, name, NULL); +} + +LUALIB_API int luaL_loadstring(lua_State *L, const char *s) +{ + return luaL_loadbuffer(L, s, strlen(s), s); +} + +/* -- Dump bytecode ------------------------------------------------------- */ + +LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data) +{ + cTValue *o = L->top-1; + api_check(L, L->top > L->base); + if (tvisfunc(o) && isluafunc(funcV(o))) + return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0); + else + return 1; +} + diff --git a/external/lua/luajit/src/src/lj_mcode.c b/external/lua/luajit/src/src/lj_mcode.c new file mode 100644 index 0000000000..5f7582c483 --- /dev/null +++ b/external/lua/luajit/src/src/lj_mcode.c @@ -0,0 +1,359 @@ +/* +** Machine code management. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_mcode_c +#define LUA_CORE + +#include "lj_obj.h" +#if LJ_HASJIT +#include "lj_gc.h" +#include "lj_jit.h" +#include "lj_mcode.h" +#include "lj_trace.h" +#include "lj_dispatch.h" +#endif +#if LJ_HASJIT || LJ_HASFFI +#include "lj_vm.h" +#endif + +/* -- OS-specific functions ----------------------------------------------- */ + +#if LJ_HASJIT || LJ_HASFFI + +/* Define this if you want to run LuaJIT with Valgrind. */ +#ifdef LUAJIT_USE_VALGRIND +#include +#endif + +#if LJ_TARGET_IOS +void sys_icache_invalidate(void *start, size_t len); +#endif + +/* Synchronize data/instruction cache. */ +void lj_mcode_sync(void *start, void *end) +{ +#ifdef LUAJIT_USE_VALGRIND + VALGRIND_DISCARD_TRANSLATIONS(start, (char *)end-(char *)start); +#endif +#if LJ_TARGET_X86ORX64 + UNUSED(start); UNUSED(end); +#elif LJ_TARGET_IOS + sys_icache_invalidate(start, (char *)end-(char *)start); +#elif LJ_TARGET_PPC + lj_vm_cachesync(start, end); +#elif defined(__GNUC__) + __clear_cache(start, end); +#else +#error "Missing builtin to flush instruction cache" +#endif +} + +#endif + +#if LJ_HASJIT + +#if LJ_TARGET_WINDOWS + +#define WIN32_LEAN_AND_MEAN +#include + +#define MCPROT_RW PAGE_READWRITE +#define MCPROT_RX PAGE_EXECUTE_READ +#define MCPROT_RWX PAGE_EXECUTE_READWRITE + +static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, DWORD prot) +{ + void *p = VirtualAlloc((void *)hint, sz, + MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, prot); + if (!p && !hint) + lj_trace_err(J, LJ_TRERR_MCODEAL); + return p; +} + +static void mcode_free(jit_State *J, void *p, size_t sz) +{ + UNUSED(J); UNUSED(sz); + VirtualFree(p, 0, MEM_RELEASE); +} + +static void mcode_setprot(void *p, size_t sz, DWORD prot) +{ + DWORD oprot; + VirtualProtect(p, sz, prot, &oprot); +} + +#elif LJ_TARGET_POSIX + +#include + +#ifndef MAP_ANONYMOUS +#define MAP_ANONYMOUS MAP_ANON +#endif + +#define MCPROT_RW (PROT_READ|PROT_WRITE) +#define MCPROT_RX (PROT_READ|PROT_EXEC) +#define MCPROT_RWX (PROT_READ|PROT_WRITE|PROT_EXEC) + +static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot) +{ + void *p = mmap((void *)hint, sz, prot, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (p == MAP_FAILED) { + if (!hint) lj_trace_err(J, LJ_TRERR_MCODEAL); + p = NULL; + } + return p; +} + +static void mcode_free(jit_State *J, void *p, size_t sz) +{ + UNUSED(J); + munmap(p, sz); +} + +static void mcode_setprot(void *p, size_t sz, int prot) +{ + mprotect(p, sz, prot); +} + +#elif LJ_64 + +#error "Missing OS support for explicit placement of executable memory" + +#else + +/* Fallback allocator. This will fail if memory is not executable by default. */ +#define LUAJIT_UNPROTECT_MCODE +#define MCPROT_RW 0 +#define MCPROT_RX 0 +#define MCPROT_RWX 0 + +static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot) +{ + UNUSED(hint); UNUSED(prot); + return lj_mem_new(J->L, sz); +} + +static void mcode_free(jit_State *J, void *p, size_t sz) +{ + lj_mem_free(J2G(J), p, sz); +} + +#define mcode_setprot(p, sz, prot) UNUSED(p) + +#endif + +/* -- MCode area protection ----------------------------------------------- */ + +/* Define this ONLY if the page protection twiddling becomes a bottleneck. */ +#ifdef LUAJIT_UNPROTECT_MCODE + +/* It's generally considered to be a potential security risk to have +** pages with simultaneous write *and* execute access in a process. +** +** Do not even think about using this mode for server processes or +** apps handling untrusted external data (such as a browser). +** +** The security risk is not in LuaJIT itself -- but if an adversary finds +** any *other* flaw in your C application logic, then any RWX memory page +** simplifies writing an exploit considerably. +*/ +#define MCPROT_GEN MCPROT_RWX +#define MCPROT_RUN MCPROT_RWX + +static void mcode_protect(jit_State *J, int prot) +{ + UNUSED(J); UNUSED(prot); +} + +#else + +/* This is the default behaviour and much safer: +** +** Most of the time the memory pages holding machine code are executable, +** but NONE of them is writable. +** +** The current memory area is marked read-write (but NOT executable) only +** during the short time window while the assembler generates machine code. +*/ +#define MCPROT_GEN MCPROT_RW +#define MCPROT_RUN MCPROT_RX + +/* Change protection of MCode area. */ +static void mcode_protect(jit_State *J, int prot) +{ + if (J->mcprot != prot) { + mcode_setprot(J->mcarea, J->szmcarea, prot); + J->mcprot = prot; + } +} + +#endif + +/* -- MCode area allocation ----------------------------------------------- */ + +#if LJ_TARGET_X64 +#define mcode_validptr(p) ((p) && (uintptr_t)(p) < (uintptr_t)1<<47) +#else +#define mcode_validptr(p) ((p) && (uintptr_t)(p) < 0xffff0000) +#endif + +#ifdef LJ_TARGET_JUMPRANGE + +/* Get memory within relative jump distance of our code in 64 bit mode. */ +static void *mcode_alloc(jit_State *J, size_t sz) +{ + /* Target an address in the static assembler code (64K aligned). + ** Try addresses within a distance of target-range/2+1MB..target+range/2-1MB. + */ +#if LJ_TARGET_MIPS + /* Use the middle of the 256MB-aligned region. */ + uintptr_t target = ((uintptr_t)(void *)lj_vm_exit_handler & 0xf0000000u) + + 0x08000000u; +#else + uintptr_t target = (uintptr_t)(void *)lj_vm_exit_handler & ~(uintptr_t)0xffff; +#endif + const uintptr_t range = (1u << LJ_TARGET_JUMPRANGE) - (1u << 21); + /* First try a contiguous area below the last one. */ + uintptr_t hint = J->mcarea ? (uintptr_t)J->mcarea - sz : 0; + int i; + for (i = 0; i < 32; i++) { /* 32 attempts ought to be enough ... */ + if (mcode_validptr(hint)) { + void *p = mcode_alloc_at(J, hint, sz, MCPROT_GEN); + + if (mcode_validptr(p) && + ((uintptr_t)p + sz - target < range || target - (uintptr_t)p < range)) + return p; + if (p) mcode_free(J, p, sz); /* Free badly placed area. */ + } + /* Next try probing pseudo-random addresses. */ + do { + hint = (0x78fb ^ LJ_PRNG_BITS(J, 15)) << 16; /* 64K aligned. */ + } while (!(hint + sz < range)); + hint = target + hint - (range>>1); + } + lj_trace_err(J, LJ_TRERR_MCODEAL); /* Give up. OS probably ignores hints? */ + return NULL; +} + +#else + +/* All memory addresses are reachable by relative jumps. */ +#define mcode_alloc(J, sz) mcode_alloc_at((J), 0, (sz), MCPROT_GEN) + +#endif + +/* -- MCode area management ----------------------------------------------- */ + +/* Linked list of MCode areas. */ +typedef struct MCLink { + MCode *next; /* Next area. */ + size_t size; /* Size of current area. */ +} MCLink; + +/* Allocate a new MCode area. */ +static void mcode_allocarea(jit_State *J) +{ + MCode *oldarea = J->mcarea; + size_t sz = (size_t)J->param[JIT_P_sizemcode] << 10; + sz = (sz + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1); + J->mcarea = (MCode *)mcode_alloc(J, sz); + J->szmcarea = sz; + J->mcprot = MCPROT_GEN; + J->mctop = (MCode *)((char *)J->mcarea + J->szmcarea); + J->mcbot = (MCode *)((char *)J->mcarea + sizeof(MCLink)); + ((MCLink *)J->mcarea)->next = oldarea; + ((MCLink *)J->mcarea)->size = sz; + J->szallmcarea += sz; +} + +/* Free all MCode areas. */ +void lj_mcode_free(jit_State *J) +{ + MCode *mc = J->mcarea; + J->mcarea = NULL; + J->szallmcarea = 0; + while (mc) { + MCode *next = ((MCLink *)mc)->next; + mcode_free(J, mc, ((MCLink *)mc)->size); + mc = next; + } +} + +/* -- MCode transactions -------------------------------------------------- */ + +/* Reserve the remainder of the current MCode area. */ +MCode *lj_mcode_reserve(jit_State *J, MCode **lim) +{ + if (!J->mcarea) + mcode_allocarea(J); + else + mcode_protect(J, MCPROT_GEN); + *lim = J->mcbot; + return J->mctop; +} + +/* Commit the top part of the current MCode area. */ +void lj_mcode_commit(jit_State *J, MCode *top) +{ + J->mctop = top; + mcode_protect(J, MCPROT_RUN); +} + +/* Abort the reservation. */ +void lj_mcode_abort(jit_State *J) +{ + mcode_protect(J, MCPROT_RUN); +} + +/* Set/reset protection to allow patching of MCode areas. */ +MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish) +{ +#ifdef LUAJIT_UNPROTECT_MCODE + UNUSED(J); UNUSED(ptr); UNUSED(finish); + return NULL; +#else + if (finish) { + if (J->mcarea == ptr) + mcode_protect(J, MCPROT_RUN); + else + mcode_setprot(ptr, ((MCLink *)ptr)->size, MCPROT_RUN); + return NULL; + } else { + MCode *mc = J->mcarea; + /* Try current area first to use the protection cache. */ + if (ptr >= mc && ptr < (MCode *)((char *)mc + J->szmcarea)) { + mcode_protect(J, MCPROT_GEN); + return mc; + } + /* Otherwise search through the list of MCode areas. */ + for (;;) { + mc = ((MCLink *)mc)->next; + lua_assert(mc != NULL); + if (ptr >= mc && ptr < (MCode *)((char *)mc + ((MCLink *)mc)->size)) { + mcode_setprot(mc, ((MCLink *)mc)->size, MCPROT_GEN); + return mc; + } + } + } +#endif +} + +/* Limit of MCode reservation reached. */ +void lj_mcode_limiterr(jit_State *J, size_t need) +{ + size_t sizemcode, maxmcode; + lj_mcode_abort(J); + sizemcode = (size_t)J->param[JIT_P_sizemcode] << 10; + sizemcode = (sizemcode + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1); + maxmcode = (size_t)J->param[JIT_P_maxmcode] << 10; + if ((size_t)need > sizemcode) + lj_trace_err(J, LJ_TRERR_MCODEOV); /* Too long for any area. */ + if (J->szallmcarea + sizemcode > maxmcode) + lj_trace_err(J, LJ_TRERR_MCODEAL); + mcode_allocarea(J); + lj_trace_err(J, LJ_TRERR_MCODELM); /* Retry with new area. */ +} + +#endif diff --git a/external/lua/luajit/src/src/lj_mcode.h b/external/lua/luajit/src/src/lj_mcode.h new file mode 100644 index 0000000000..2ba371bc1d --- /dev/null +++ b/external/lua/luajit/src/src/lj_mcode.h @@ -0,0 +1,30 @@ +/* +** Machine code management. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_MCODE_H +#define _LJ_MCODE_H + +#include "lj_obj.h" + +#if LJ_HASJIT || LJ_HASFFI +LJ_FUNC void lj_mcode_sync(void *start, void *end); +#endif + +#if LJ_HASJIT + +#include "lj_jit.h" + +LJ_FUNC void lj_mcode_free(jit_State *J); +LJ_FUNC MCode *lj_mcode_reserve(jit_State *J, MCode **lim); +LJ_FUNC void lj_mcode_commit(jit_State *J, MCode *m); +LJ_FUNC void lj_mcode_abort(jit_State *J); +LJ_FUNC MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish); +LJ_FUNC_NORET void lj_mcode_limiterr(jit_State *J, size_t need); + +#define lj_mcode_commitbot(J, m) (J->mcbot = (m)) + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_meta.c b/external/lua/luajit/src/src/lj_meta.c new file mode 100644 index 0000000000..441d571ac7 --- /dev/null +++ b/external/lua/luajit/src/src/lj_meta.c @@ -0,0 +1,466 @@ +/* +** Metamethod handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lj_meta_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_meta.h" +#include "lj_frame.h" +#include "lj_bc.h" +#include "lj_vm.h" +#include "lj_strscan.h" + +/* -- Metamethod handling ------------------------------------------------- */ + +/* String interning of metamethod names for fast indexing. */ +void lj_meta_init(lua_State *L) +{ +#define MMNAME(name) "__" #name + const char *metanames = MMDEF(MMNAME); +#undef MMNAME + global_State *g = G(L); + const char *p, *q; + uint32_t mm; + for (mm = 0, p = metanames; *p; mm++, p = q) { + GCstr *s; + for (q = p+2; *q && *q != '_'; q++) ; + s = lj_str_new(L, p, (size_t)(q-p)); + /* NOBARRIER: g->gcroot[] is a GC root. */ + setgcref(g->gcroot[GCROOT_MMNAME+mm], obj2gco(s)); + } +} + +/* Negative caching of a few fast metamethods. See the lj_meta_fast() macro. */ +cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name) +{ + cTValue *mo = lj_tab_getstr(mt, name); + lua_assert(mm <= MM_FAST); + if (!mo || tvisnil(mo)) { /* No metamethod? */ + mt->nomm |= (uint8_t)(1u<metatable); + else if (tvisudata(o)) + mt = tabref(udataV(o)->metatable); + else + mt = tabref(basemt_obj(G(L), o)); + if (mt) { + cTValue *mo = lj_tab_getstr(mt, mmname_str(G(L), mm)); + if (mo) + return mo; + } + return niltv(L); +} + +#if LJ_HASFFI +/* Tailcall from C function. */ +int lj_meta_tailcall(lua_State *L, cTValue *tv) +{ + TValue *base = L->base; + TValue *top = L->top; + const BCIns *pc = frame_pc(base-1); /* Preserve old PC from frame. */ + copyTV(L, base-1, tv); /* Replace frame with new object. */ + top->u32.lo = LJ_CONT_TAILCALL; + setframe_pc(top, pc); + setframe_gc(top+1, obj2gco(L)); /* Dummy frame object. */ + setframe_ftsz(top+1, (int)((char *)(top+2) - (char *)base) + FRAME_CONT); + L->base = L->top = top+2; + /* + ** before: [old_mo|PC] [... ...] + ** ^base ^top + ** after: [new_mo|itype] [... ...] [NULL|PC] [dummy|delta] + ** ^base/top + ** tailcall: [new_mo|PC] [... ...] + ** ^base ^top + */ + return 0; +} +#endif + +/* Setup call to metamethod to be run by Assembler VM. */ +static TValue *mmcall(lua_State *L, ASMFunction cont, cTValue *mo, + cTValue *a, cTValue *b) +{ + /* + ** |-- framesize -> top top+1 top+2 top+3 + ** before: [func slots ...] + ** mm setup: [func slots ...] [cont|?] [mo|tmtype] [a] [b] + ** in asm: [func slots ...] [cont|PC] [mo|delta] [a] [b] + ** ^-- func base ^-- mm base + ** after mm: [func slots ...] [result] + ** ^-- copy to base[PC_RA] --/ for lj_cont_ra + ** istruecond + branch for lj_cont_cond* + ** ignore for lj_cont_nop + ** next PC: [func slots ...] + */ + TValue *top = L->top; + if (curr_funcisL(L)) top = curr_topL(L); + setcont(top, cont); /* Assembler VM stores PC in upper word. */ + copyTV(L, top+1, mo); /* Store metamethod and two arguments. */ + copyTV(L, top+2, a); + copyTV(L, top+3, b); + return top+2; /* Return new base. */ +} + +/* -- C helpers for some instructions, called from assembler VM ----------- */ + +/* Helper for TGET*. __index chain and metamethod. */ +cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k) +{ + int loop; + for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) { + cTValue *mo; + if (LJ_LIKELY(tvistab(o))) { + GCtab *t = tabV(o); + cTValue *tv = lj_tab_get(L, t, k); + if (!tvisnil(tv) || + !(mo = lj_meta_fast(L, tabref(t->metatable), MM_index))) + return tv; + } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_index))) { + lj_err_optype(L, o, LJ_ERR_OPINDEX); + return NULL; /* unreachable */ + } + if (tvisfunc(mo)) { + L->top = mmcall(L, lj_cont_ra, mo, o, k); + return NULL; /* Trigger metamethod call. */ + } + o = mo; + } + lj_err_msg(L, LJ_ERR_GETLOOP); + return NULL; /* unreachable */ +} + +/* Helper for TSET*. __newindex chain and metamethod. */ +TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k) +{ + TValue tmp; + int loop; + for (loop = 0; loop < LJ_MAX_IDXCHAIN; loop++) { + cTValue *mo; + if (LJ_LIKELY(tvistab(o))) { + GCtab *t = tabV(o); + cTValue *tv = lj_tab_get(L, t, k); + if (LJ_LIKELY(!tvisnil(tv))) { + t->nomm = 0; /* Invalidate negative metamethod cache. */ + lj_gc_anybarriert(L, t); + return (TValue *)tv; + } else if (!(mo = lj_meta_fast(L, tabref(t->metatable), MM_newindex))) { + t->nomm = 0; /* Invalidate negative metamethod cache. */ + lj_gc_anybarriert(L, t); + if (tv != niltv(L)) + return (TValue *)tv; + if (tvisnil(k)) lj_err_msg(L, LJ_ERR_NILIDX); + else if (tvisint(k)) { setnumV(&tmp, (lua_Number)intV(k)); k = &tmp; } + else if (tvisnum(k) && tvisnan(k)) lj_err_msg(L, LJ_ERR_NANIDX); + return lj_tab_newkey(L, t, k); + } + } else if (tvisnil(mo = lj_meta_lookup(L, o, MM_newindex))) { + lj_err_optype(L, o, LJ_ERR_OPINDEX); + return NULL; /* unreachable */ + } + if (tvisfunc(mo)) { + L->top = mmcall(L, lj_cont_nop, mo, o, k); + /* L->top+2 = v filled in by caller. */ + return NULL; /* Trigger metamethod call. */ + } + copyTV(L, &tmp, mo); + o = &tmp; + } + lj_err_msg(L, LJ_ERR_SETLOOP); + return NULL; /* unreachable */ +} + +static cTValue *str2num(cTValue *o, TValue *n) +{ + if (tvisnum(o)) + return o; + else if (tvisint(o)) + return (setnumV(n, (lua_Number)intV(o)), n); + else if (tvisstr(o) && lj_strscan_num(strV(o), n)) + return n; + else + return NULL; +} + +/* Helper for arithmetic instructions. Coercion, metamethod. */ +TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, cTValue *rc, + BCReg op) +{ + MMS mm = bcmode_mm(op); + TValue tempb, tempc; + cTValue *b, *c; + if ((b = str2num(rb, &tempb)) != NULL && + (c = str2num(rc, &tempc)) != NULL) { /* Try coercion first. */ + setnumV(ra, lj_vm_foldarith(numV(b), numV(c), (int)mm-MM_add)); + return NULL; + } else { + cTValue *mo = lj_meta_lookup(L, rb, mm); + if (tvisnil(mo)) { + mo = lj_meta_lookup(L, rc, mm); + if (tvisnil(mo)) { + if (str2num(rb, &tempb) == NULL) rc = rb; + lj_err_optype(L, rc, LJ_ERR_OPARITH); + return NULL; /* unreachable */ + } + } + return mmcall(L, lj_cont_ra, mo, rb, rc); + } +} + +/* In-place coercion of a number to a string. */ +static LJ_AINLINE int tostring(lua_State *L, TValue *o) +{ + if (tvisstr(o)) { + return 1; + } else if (tvisnumber(o)) { + setstrV(L, o, lj_str_fromnumber(L, o)); + return 1; + } else { + return 0; + } +} + +/* Helper for CAT. Coercion, iterative concat, __concat metamethod. */ +TValue *lj_meta_cat(lua_State *L, TValue *top, int left) +{ + int fromc = 0; + if (left < 0) { left = -left; fromc = 1; } + do { + int n = 1; + if (!(tvisstr(top-1) || tvisnumber(top-1)) || !tostring(L, top)) { + cTValue *mo = lj_meta_lookup(L, top-1, MM_concat); + if (tvisnil(mo)) { + mo = lj_meta_lookup(L, top, MM_concat); + if (tvisnil(mo)) { + if (tvisstr(top-1) || tvisnumber(top-1)) top++; + lj_err_optype(L, top-1, LJ_ERR_OPCAT); + return NULL; /* unreachable */ + } + } + /* One of the top two elements is not a string, call __cat metamethod: + ** + ** before: [...][CAT stack .........................] + ** top-1 top top+1 top+2 + ** pick two: [...][CAT stack ...] [o1] [o2] + ** setup mm: [...][CAT stack ...] [cont|?] [mo|tmtype] [o1] [o2] + ** in asm: [...][CAT stack ...] [cont|PC] [mo|delta] [o1] [o2] + ** ^-- func base ^-- mm base + ** after mm: [...][CAT stack ...] <--push-- [result] + ** next step: [...][CAT stack .............] + */ + copyTV(L, top+2, top); /* Careful with the order of stack copies! */ + copyTV(L, top+1, top-1); + copyTV(L, top, mo); + setcont(top-1, lj_cont_cat); + return top+1; /* Trigger metamethod call. */ + } else if (strV(top)->len == 0) { /* Shortcut. */ + (void)tostring(L, top-1); + } else { + /* Pick as many strings as possible from the top and concatenate them: + ** + ** before: [...][CAT stack ...........................] + ** pick str: [...][CAT stack ...] [...... strings ......] + ** concat: [...][CAT stack ...] [result] + ** next step: [...][CAT stack ............] + */ + MSize tlen = strV(top)->len; + char *buffer; + int i; + for (n = 1; n <= left && tostring(L, top-n); n++) { + MSize len = strV(top-n)->len; + if (len >= LJ_MAX_STR - tlen) + lj_err_msg(L, LJ_ERR_STROV); + tlen += len; + } + buffer = lj_str_needbuf(L, &G(L)->tmpbuf, tlen); + n--; + tlen = 0; + for (i = n; i >= 0; i--) { + MSize len = strV(top-i)->len; + memcpy(buffer + tlen, strVdata(top-i), len); + tlen += len; + } + setstrV(L, top-n, lj_str_new(L, buffer, tlen)); + } + left -= n; + top -= n; + } while (left >= 1); + if (LJ_UNLIKELY(G(L)->gc.total >= G(L)->gc.threshold)) { + if (!fromc) L->top = curr_topL(L); + lj_gc_step(L); + } + return NULL; +} + +/* Helper for LEN. __len metamethod. */ +TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o) +{ + cTValue *mo = lj_meta_lookup(L, o, MM_len); + if (tvisnil(mo)) { + if (LJ_52 && tvistab(o)) + tabref(tabV(o)->metatable)->nomm |= (uint8_t)(1u<gch.metatable), MM_eq); + if (mo) { + TValue *top; + uint32_t it; + if (tabref(o1->gch.metatable) != tabref(o2->gch.metatable)) { + cTValue *mo2 = lj_meta_fast(L, tabref(o2->gch.metatable), MM_eq); + if (mo2 == NULL || !lj_obj_equal(mo, mo2)) + return (TValue *)(intptr_t)ne; + } + top = curr_top(L); + setcont(top, ne ? lj_cont_condf : lj_cont_condt); + copyTV(L, top+1, mo); + it = ~(uint32_t)o1->gch.gct; + setgcV(L, top+2, o1, it); + setgcV(L, top+3, o2, it); + return top+2; /* Trigger metamethod call. */ + } + return (TValue *)(intptr_t)ne; +} + +#if LJ_HASFFI +TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins) +{ + ASMFunction cont = (bc_op(ins) & 1) ? lj_cont_condf : lj_cont_condt; + int op = (int)bc_op(ins) & ~1; + TValue tv; + cTValue *mo, *o2, *o1 = &L->base[bc_a(ins)]; + cTValue *o1mm = o1; + if (op == BC_ISEQV) { + o2 = &L->base[bc_d(ins)]; + if (!tviscdata(o1mm)) o1mm = o2; + } else if (op == BC_ISEQS) { + setstrV(L, &tv, gco2str(proto_kgc(curr_proto(L), ~(ptrdiff_t)bc_d(ins)))); + o2 = &tv; + } else if (op == BC_ISEQN) { + o2 = &mref(curr_proto(L)->k, cTValue)[bc_d(ins)]; + } else { + lua_assert(op == BC_ISEQP); + setitype(&tv, ~bc_d(ins)); + o2 = &tv; + } + mo = lj_meta_lookup(L, o1mm, MM_eq); + if (LJ_LIKELY(!tvisnil(mo))) + return mmcall(L, cont, mo, o1, o2); + else + return (TValue *)(intptr_t)(bc_op(ins) & 1); +} +#endif + +/* Helper for ordered comparisons. String compare, __lt/__le metamethods. */ +TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op) +{ + if (LJ_HASFFI && (tviscdata(o1) || tviscdata(o2))) { + ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt; + MMS mm = (op & 2) ? MM_le : MM_lt; + cTValue *mo = lj_meta_lookup(L, tviscdata(o1) ? o1 : o2, mm); + if (LJ_UNLIKELY(tvisnil(mo))) goto err; + return mmcall(L, cont, mo, o1, o2); + } else if (LJ_52 || itype(o1) == itype(o2)) { + /* Never called with two numbers. */ + if (tvisstr(o1) && tvisstr(o2)) { + int32_t res = lj_str_cmp(strV(o1), strV(o2)); + return (TValue *)(intptr_t)(((op&2) ? res <= 0 : res < 0) ^ (op&1)); + } else { + trymt: + while (1) { + ASMFunction cont = (op & 1) ? lj_cont_condf : lj_cont_condt; + MMS mm = (op & 2) ? MM_le : MM_lt; + cTValue *mo = lj_meta_lookup(L, o1, mm); +#if LJ_52 + if (tvisnil(mo) && tvisnil((mo = lj_meta_lookup(L, o2, mm)))) +#else + cTValue *mo2 = lj_meta_lookup(L, o2, mm); + if (tvisnil(mo) || !lj_obj_equal(mo, mo2)) +#endif + { + if (op & 2) { /* MM_le not found: retry with MM_lt. */ + cTValue *ot = o1; o1 = o2; o2 = ot; /* Swap operands. */ + op ^= 3; /* Use LT and flip condition. */ + continue; + } + goto err; + } + return mmcall(L, cont, mo, o1, o2); + } + } + } else if (tvisbool(o1) && tvisbool(o2)) { + goto trymt; + } else { + err: + lj_err_comp(L, o1, o2); + return NULL; + } +} + +/* Helper for calls. __call metamethod. */ +void lj_meta_call(lua_State *L, TValue *func, TValue *top) +{ + cTValue *mo = lj_meta_lookup(L, func, MM_call); + TValue *p; + if (!tvisfunc(mo)) + lj_err_optype_call(L, func); + for (p = top; p > func; p--) copyTV(L, p, p-1); + copyTV(L, func, mo); +} + +/* Helper for FORI. Coercion. */ +void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o) +{ + if (!lj_strscan_numberobj(o)) lj_err_msg(L, LJ_ERR_FORINIT); + if (!lj_strscan_numberobj(o+1)) lj_err_msg(L, LJ_ERR_FORLIM); + if (!lj_strscan_numberobj(o+2)) lj_err_msg(L, LJ_ERR_FORSTEP); + if (LJ_DUALNUM) { + /* Ensure all slots are integers or all slots are numbers. */ + int32_t k[3]; + int nint = 0; + ptrdiff_t i; + for (i = 0; i <= 2; i++) { + if (tvisint(o+i)) { + k[i] = intV(o+i); nint++; + } else { + k[i] = lj_num2int(numV(o+i)); nint += ((lua_Number)k[i] == numV(o+i)); + } + } + if (nint == 3) { /* Narrow to integers. */ + setintV(o, k[0]); + setintV(o+1, k[1]); + setintV(o+2, k[2]); + } else if (nint != 0) { /* Widen to numbers. */ + if (tvisint(o)) setnumV(o, (lua_Number)intV(o)); + if (tvisint(o+1)) setnumV(o+1, (lua_Number)intV(o+1)); + if (tvisint(o+2)) setnumV(o+2, (lua_Number)intV(o+2)); + } + } +} + diff --git a/external/lua/luajit/src/src/lj_meta.h b/external/lua/luajit/src/src/lj_meta.h new file mode 100644 index 0000000000..6af5e51443 --- /dev/null +++ b/external/lua/luajit/src/src/lj_meta.h @@ -0,0 +1,37 @@ +/* +** Metamethod handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_META_H +#define _LJ_META_H + +#include "lj_obj.h" + +/* Metamethod handling */ +LJ_FUNC void lj_meta_init(lua_State *L); +LJ_FUNC cTValue *lj_meta_cache(GCtab *mt, MMS mm, GCstr *name); +LJ_FUNC cTValue *lj_meta_lookup(lua_State *L, cTValue *o, MMS mm); +#if LJ_HASFFI +LJ_FUNC int lj_meta_tailcall(lua_State *L, cTValue *tv); +#endif + +#define lj_meta_fastg(g, mt, mm) \ + ((mt) == NULL ? NULL : ((mt)->nomm & (1u<<(mm))) ? NULL : \ + lj_meta_cache(mt, mm, mmname_str(g, mm))) +#define lj_meta_fast(L, mt, mm) lj_meta_fastg(G(L), mt, mm) + +/* C helpers for some instructions, called from assembler VM. */ +LJ_FUNCA cTValue *lj_meta_tget(lua_State *L, cTValue *o, cTValue *k); +LJ_FUNCA TValue *lj_meta_tset(lua_State *L, cTValue *o, cTValue *k); +LJ_FUNCA TValue *lj_meta_arith(lua_State *L, TValue *ra, cTValue *rb, + cTValue *rc, BCReg op); +LJ_FUNCA TValue *lj_meta_cat(lua_State *L, TValue *top, int left); +LJ_FUNCA TValue * LJ_FASTCALL lj_meta_len(lua_State *L, cTValue *o); +LJ_FUNCA TValue *lj_meta_equal(lua_State *L, GCobj *o1, GCobj *o2, int ne); +LJ_FUNCA TValue * LJ_FASTCALL lj_meta_equal_cd(lua_State *L, BCIns ins); +LJ_FUNCA TValue *lj_meta_comp(lua_State *L, cTValue *o1, cTValue *o2, int op); +LJ_FUNCA void lj_meta_call(lua_State *L, TValue *func, TValue *top); +LJ_FUNCA void LJ_FASTCALL lj_meta_for(lua_State *L, TValue *o); + +#endif diff --git a/external/lua/luajit/src/src/lj_obj.c b/external/lua/luajit/src/src/lj_obj.c new file mode 100644 index 0000000000..322b7bec7c --- /dev/null +++ b/external/lua/luajit/src/src/lj_obj.c @@ -0,0 +1,35 @@ +/* +** Miscellaneous object handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_obj_c +#define LUA_CORE + +#include "lj_obj.h" + +/* Object type names. */ +LJ_DATADEF const char *const lj_obj_typename[] = { /* ORDER LUA_T */ + "no value", "nil", "boolean", "userdata", "number", "string", + "table", "function", "userdata", "thread", "proto", "cdata" +}; + +LJ_DATADEF const char *const lj_obj_itypename[] = { /* ORDER LJ_T */ + "nil", "boolean", "boolean", "userdata", "string", "upval", "thread", + "proto", "function", "trace", "cdata", "table", "userdata", "number" +}; + +/* Compare two objects without calling metamethods. */ +int lj_obj_equal(cTValue *o1, cTValue *o2) +{ + if (itype(o1) == itype(o2)) { + if (tvispri(o1)) + return 1; + if (!tvisnum(o1)) + return gcrefeq(o1->gcr, o2->gcr); + } else if (!tvisnumber(o1) || !tvisnumber(o2)) { + return 0; + } + return numberVnum(o1) == numberVnum(o2); +} + diff --git a/external/lua/luajit/src/src/lj_obj.h b/external/lua/luajit/src/src/lj_obj.h new file mode 100644 index 0000000000..b967819d07 --- /dev/null +++ b/external/lua/luajit/src/src/lj_obj.h @@ -0,0 +1,856 @@ +/* +** LuaJIT VM tags, values and objects. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#ifndef _LJ_OBJ_H +#define _LJ_OBJ_H + +#include "lua.h" +#include "lj_def.h" +#include "lj_arch.h" + +/* -- Memory references (32 bit address space) ---------------------------- */ + +/* Memory size. */ +typedef uint32_t MSize; + +/* Memory reference */ +typedef struct MRef { + uint32_t ptr32; /* Pseudo 32 bit pointer. */ +} MRef; + +#define mref(r, t) ((t *)(void *)(uintptr_t)(r).ptr32) + +#define setmref(r, p) ((r).ptr32 = (uint32_t)(uintptr_t)(void *)(p)) +#define setmrefr(r, v) ((r).ptr32 = (v).ptr32) + +/* -- GC object references (32 bit address space) ------------------------- */ + +/* GCobj reference */ +typedef struct GCRef { + uint32_t gcptr32; /* Pseudo 32 bit pointer. */ +} GCRef; + +/* Common GC header for all collectable objects. */ +#define GCHeader GCRef nextgc; uint8_t marked; uint8_t gct +/* This occupies 6 bytes, so use the next 2 bytes for non-32 bit fields. */ + +#define gcref(r) ((GCobj *)(uintptr_t)(r).gcptr32) +#define gcrefp(r, t) ((t *)(void *)(uintptr_t)(r).gcptr32) +#define gcrefu(r) ((r).gcptr32) +#define gcrefi(r) ((int32_t)(r).gcptr32) +#define gcrefeq(r1, r2) ((r1).gcptr32 == (r2).gcptr32) +#define gcnext(gc) (gcref((gc)->gch.nextgc)) + +#define setgcref(r, gc) ((r).gcptr32 = (uint32_t)(uintptr_t)&(gc)->gch) +#define setgcrefi(r, i) ((r).gcptr32 = (uint32_t)(i)) +#define setgcrefp(r, p) ((r).gcptr32 = (uint32_t)(uintptr_t)(p)) +#define setgcrefnull(r) ((r).gcptr32 = 0) +#define setgcrefr(r, v) ((r).gcptr32 = (v).gcptr32) + +/* IMPORTANT NOTE: +** +** All uses of the setgcref* macros MUST be accompanied with a write barrier. +** +** This is to ensure the integrity of the incremental GC. The invariant +** to preserve is that a black object never points to a white object. +** I.e. never store a white object into a field of a black object. +** +** It's ok to LEAVE OUT the write barrier ONLY in the following cases: +** - The source is not a GC object (NULL). +** - The target is a GC root. I.e. everything in global_State. +** - The target is a lua_State field (threads are never black). +** - The target is a stack slot, see setgcV et al. +** - The target is an open upvalue, i.e. pointing to a stack slot. +** - The target is a newly created object (i.e. marked white). But make +** sure nothing invokes the GC inbetween. +** - The target and the source are the same object (self-reference). +** - The target already contains the object (e.g. moving elements around). +** +** The most common case is a store to a stack slot. All other cases where +** a barrier has been omitted are annotated with a NOBARRIER comment. +** +** The same logic applies for stores to table slots (array part or hash +** part). ALL uses of lj_tab_set* require a barrier for the stored value +** *and* the stored key, based on the above rules. In practice this means +** a barrier is needed if *either* of the key or value are a GC object. +** +** It's ok to LEAVE OUT the write barrier in the following special cases: +** - The stored value is nil. The key doesn't matter because it's either +** not resurrected or lj_tab_newkey() will take care of the key barrier. +** - The key doesn't matter if the *previously* stored value is guaranteed +** to be non-nil (because the key is kept alive in the table). +** - The key doesn't matter if it's guaranteed not to be part of the table, +** since lj_tab_newkey() takes care of the key barrier. This applies +** trivially to new tables, but watch out for resurrected keys. Storing +** a nil value leaves the key in the table! +** +** In case of doubt use lj_gc_anybarriert() as it's rather cheap. It's used +** by the interpreter for all table stores. +** +** Note: In contrast to Lua's GC, LuaJIT's GC does *not* specially mark +** dead keys in tables. The reference is left in, but it's guaranteed to +** be never dereferenced as long as the value is nil. It's ok if the key is +** freed or if any object subsequently gets the same address. +** +** Not destroying dead keys helps to keep key hash slots stable. This avoids +** specialization back-off for HREFK when a value flips between nil and +** non-nil and the GC gets in the way. It also allows safely hoisting +** HREF/HREFK across GC steps. Dead keys are only removed if a table is +** resized (i.e. by NEWREF) and xREF must not be CSEd across a resize. +** +** The trade-off is that a write barrier for tables must take the key into +** account, too. Implicitly resurrecting the key by storing a non-nil value +** may invalidate the incremental GC invariant. +*/ + +/* -- Common type definitions --------------------------------------------- */ + +/* Types for handling bytecodes. Need this here, details in lj_bc.h. */ +typedef uint32_t BCIns; /* Bytecode instruction. */ +typedef uint32_t BCPos; /* Bytecode position. */ +typedef uint32_t BCReg; /* Bytecode register. */ +typedef int32_t BCLine; /* Bytecode line number. */ + +/* Internal assembler functions. Never call these directly from C. */ +typedef void (*ASMFunction)(void); + +/* Resizable string buffer. Need this here, details in lj_str.h. */ +typedef struct SBuf { + char *buf; /* String buffer base. */ + MSize n; /* String buffer length. */ + MSize sz; /* String buffer size. */ +} SBuf; + +/* -- Tags and values ----------------------------------------------------- */ + +/* Frame link. */ +typedef union { + int32_t ftsz; /* Frame type and size of previous frame. */ + MRef pcr; /* Overlaps PC for Lua frames. */ +} FrameLink; + +/* Tagged value. */ +typedef LJ_ALIGN(8) union TValue { + uint64_t u64; /* 64 bit pattern overlaps number. */ + lua_Number n; /* Number object overlaps split tag/value object. */ + struct { + LJ_ENDIAN_LOHI( + union { + GCRef gcr; /* GCobj reference (if any). */ + int32_t i; /* Integer value. */ + }; + , uint32_t it; /* Internal object tag. Must overlap MSW of number. */ + ) + }; + struct { + LJ_ENDIAN_LOHI( + GCRef func; /* Function for next frame (or dummy L). */ + , FrameLink tp; /* Link to previous frame. */ + ) + } fr; + struct { + LJ_ENDIAN_LOHI( + uint32_t lo; /* Lower 32 bits of number. */ + , uint32_t hi; /* Upper 32 bits of number. */ + ) + } u32; +} TValue; + +typedef const TValue cTValue; + +#define tvref(r) (mref(r, TValue)) + +/* More external and GCobj tags for internal objects. */ +#define LAST_TT LUA_TTHREAD +#define LUA_TPROTO (LAST_TT+1) +#define LUA_TCDATA (LAST_TT+2) + +/* Internal object tags. +** +** Internal tags overlap the MSW of a number object (must be a double). +** Interpreted as a double these are special NaNs. The FPU only generates +** one type of NaN (0xfff8_0000_0000_0000). So MSWs > 0xfff80000 are available +** for use as internal tags. Small negative numbers are used to shorten the +** encoding of type comparisons (reg/mem against sign-ext. 8 bit immediate). +** +** ---MSW---.---LSW--- +** primitive types | itype | | +** lightuserdata | itype | void * | (32 bit platforms) +** lightuserdata |ffff| void * | (64 bit platforms, 47 bit pointers) +** GC objects | itype | GCRef | +** int (LJ_DUALNUM)| itype | int | +** number -------double------ +** +** ORDER LJ_T +** Primitive types nil/false/true must be first, lightuserdata next. +** GC objects are at the end, table/userdata must be lowest. +** Also check lj_ir.h for similar ordering constraints. +*/ +#define LJ_TNIL (~0u) +#define LJ_TFALSE (~1u) +#define LJ_TTRUE (~2u) +#define LJ_TLIGHTUD (~3u) +#define LJ_TSTR (~4u) +#define LJ_TUPVAL (~5u) +#define LJ_TTHREAD (~6u) +#define LJ_TPROTO (~7u) +#define LJ_TFUNC (~8u) +#define LJ_TTRACE (~9u) +#define LJ_TCDATA (~10u) +#define LJ_TTAB (~11u) +#define LJ_TUDATA (~12u) +/* This is just the canonical number type used in some places. */ +#define LJ_TNUMX (~13u) + +/* Integers have itype == LJ_TISNUM doubles have itype < LJ_TISNUM */ +#if LJ_64 +#define LJ_TISNUM 0xfffeffffu +#else +#define LJ_TISNUM LJ_TNUMX +#endif +#define LJ_TISTRUECOND LJ_TFALSE +#define LJ_TISPRI LJ_TTRUE +#define LJ_TISGCV (LJ_TSTR+1) +#define LJ_TISTABUD LJ_TTAB + +/* -- String object ------------------------------------------------------- */ + +/* String object header. String payload follows. */ +typedef struct GCstr { + GCHeader; + uint8_t reserved; /* Used by lexer for fast lookup of reserved words. */ + uint8_t unused; + MSize hash; /* Hash of string. */ + MSize len; /* Size of string. */ +} GCstr; + +#define strref(r) (&gcref((r))->str) +#define strdata(s) ((const char *)((s)+1)) +#define strdatawr(s) ((char *)((s)+1)) +#define strVdata(o) strdata(strV(o)) +#define sizestring(s) (sizeof(struct GCstr)+(s)->len+1) + +/* -- Userdata object ----------------------------------------------------- */ + +/* Userdata object. Payload follows. */ +typedef struct GCudata { + GCHeader; + uint8_t udtype; /* Userdata type. */ + uint8_t unused2; + GCRef env; /* Should be at same offset in GCfunc. */ + MSize len; /* Size of payload. */ + GCRef metatable; /* Must be at same offset in GCtab. */ + uint32_t align1; /* To force 8 byte alignment of the payload. */ +} GCudata; + +/* Userdata types. */ +enum { + UDTYPE_USERDATA, /* Regular userdata. */ + UDTYPE_IO_FILE, /* I/O library FILE. */ + UDTYPE_FFI_CLIB, /* FFI C library namespace. */ + UDTYPE__MAX +}; + +#define uddata(u) ((void *)((u)+1)) +#define sizeudata(u) (sizeof(struct GCudata)+(u)->len) + +/* -- C data object ------------------------------------------------------- */ + +/* C data object. Payload follows. */ +typedef struct GCcdata { + GCHeader; + uint16_t ctypeid; /* C type ID. */ +} GCcdata; + +/* Prepended to variable-sized or realigned C data objects. */ +typedef struct GCcdataVar { + uint16_t offset; /* Offset to allocated memory (relative to GCcdata). */ + uint16_t extra; /* Extra space allocated (incl. GCcdata + GCcdatav). */ + MSize len; /* Size of payload. */ +} GCcdataVar; + +#define cdataptr(cd) ((void *)((cd)+1)) +#define cdataisv(cd) ((cd)->marked & 0x80) +#define cdatav(cd) ((GCcdataVar *)((char *)(cd) - sizeof(GCcdataVar))) +#define cdatavlen(cd) check_exp(cdataisv(cd), cdatav(cd)->len) +#define sizecdatav(cd) (cdatavlen(cd) + cdatav(cd)->extra) +#define memcdatav(cd) ((void *)((char *)(cd) - cdatav(cd)->offset)) + +/* -- Prototype object ---------------------------------------------------- */ + +#define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef)) +#define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1)) + +typedef struct GCproto { + GCHeader; + uint8_t numparams; /* Number of parameters. */ + uint8_t framesize; /* Fixed frame size. */ + MSize sizebc; /* Number of bytecode instructions. */ + GCRef gclist; + MRef k; /* Split constant array (points to the middle). */ + MRef uv; /* Upvalue list. local slot|0x8000 or parent uv idx. */ + MSize sizekgc; /* Number of collectable constants. */ + MSize sizekn; /* Number of lua_Number constants. */ + MSize sizept; /* Total size including colocated arrays. */ + uint8_t sizeuv; /* Number of upvalues. */ + uint8_t flags; /* Miscellaneous flags (see below). */ + uint16_t trace; /* Anchor for chain of root traces. */ + /* ------ The following fields are for debugging/tracebacks only ------ */ + GCRef chunkname; /* Name of the chunk this function was defined in. */ + BCLine firstline; /* First line of the function definition. */ + BCLine numline; /* Number of lines for the function definition. */ + MRef lineinfo; /* Compressed map from bytecode ins. to source line. */ + MRef uvinfo; /* Upvalue names. */ + MRef varinfo; /* Names and compressed extents of local variables. */ +} GCproto; + +/* Flags for prototype. */ +#define PROTO_CHILD 0x01 /* Has child prototypes. */ +#define PROTO_VARARG 0x02 /* Vararg function. */ +#define PROTO_FFI 0x04 /* Uses BC_KCDATA for FFI datatypes. */ +#define PROTO_NOJIT 0x08 /* JIT disabled for this function. */ +#define PROTO_ILOOP 0x10 /* Patched bytecode with ILOOP etc. */ +/* Only used during parsing. */ +#define PROTO_HAS_RETURN 0x20 /* Already emitted a return. */ +#define PROTO_FIXUP_RETURN 0x40 /* Need to fixup emitted returns. */ +/* Top bits used for counting created closures. */ +#define PROTO_CLCOUNT 0x20 /* Base of saturating 3 bit counter. */ +#define PROTO_CLC_BITS 3 +#define PROTO_CLC_POLY (3*PROTO_CLCOUNT) /* Polymorphic threshold. */ + +#define PROTO_UV_LOCAL 0x8000 /* Upvalue for local slot. */ +#define PROTO_UV_IMMUTABLE 0x4000 /* Immutable upvalue. */ + +#define proto_kgc(pt, idx) \ + check_exp((uintptr_t)(intptr_t)(idx) >= (uintptr_t)-(intptr_t)(pt)->sizekgc, \ + gcref(mref((pt)->k, GCRef)[(idx)])) +#define proto_knumtv(pt, idx) \ + check_exp((uintptr_t)(idx) < (pt)->sizekn, &mref((pt)->k, TValue)[(idx)]) +#define proto_bc(pt) ((BCIns *)((char *)(pt) + sizeof(GCproto))) +#define proto_bcpos(pt, pc) ((BCPos)((pc) - proto_bc(pt))) +#define proto_uv(pt) (mref((pt)->uv, uint16_t)) + +#define proto_chunkname(pt) (strref((pt)->chunkname)) +#define proto_chunknamestr(pt) (strdata(proto_chunkname((pt)))) +#define proto_lineinfo(pt) (mref((pt)->lineinfo, const void)) +#define proto_uvinfo(pt) (mref((pt)->uvinfo, const uint8_t)) +#define proto_varinfo(pt) (mref((pt)->varinfo, const uint8_t)) + +/* -- Upvalue object ------------------------------------------------------ */ + +typedef struct GCupval { + GCHeader; + uint8_t closed; /* Set if closed (i.e. uv->v == &uv->u.value). */ + uint8_t immutable; /* Immutable value. */ + union { + TValue tv; /* If closed: the value itself. */ + struct { /* If open: double linked list, anchored at thread. */ + GCRef prev; + GCRef next; + }; + }; + MRef v; /* Points to stack slot (open) or above (closed). */ + uint32_t dhash; /* Disambiguation hash: dh1 != dh2 => cannot alias. */ +} GCupval; + +#define uvprev(uv_) (&gcref((uv_)->prev)->uv) +#define uvnext(uv_) (&gcref((uv_)->next)->uv) +#define uvval(uv_) (mref((uv_)->v, TValue)) + +/* -- Function object (closures) ------------------------------------------ */ + +/* Common header for functions. env should be at same offset in GCudata. */ +#define GCfuncHeader \ + GCHeader; uint8_t ffid; uint8_t nupvalues; \ + GCRef env; GCRef gclist; MRef pc + +typedef struct GCfuncC { + GCfuncHeader; + lua_CFunction f; /* C function to be called. */ + TValue upvalue[1]; /* Array of upvalues (TValue). */ +} GCfuncC; + +typedef struct GCfuncL { + GCfuncHeader; + GCRef uvptr[1]; /* Array of _pointers_ to upvalue objects (GCupval). */ +} GCfuncL; + +typedef union GCfunc { + GCfuncC c; + GCfuncL l; +} GCfunc; + +#define FF_LUA 0 +#define FF_C 1 +#define isluafunc(fn) ((fn)->c.ffid == FF_LUA) +#define iscfunc(fn) ((fn)->c.ffid == FF_C) +#define isffunc(fn) ((fn)->c.ffid > FF_C) +#define funcproto(fn) \ + check_exp(isluafunc(fn), (GCproto *)(mref((fn)->l.pc, char)-sizeof(GCproto))) +#define sizeCfunc(n) (sizeof(GCfuncC)-sizeof(TValue)+sizeof(TValue)*(n)) +#define sizeLfunc(n) (sizeof(GCfuncL)-sizeof(GCRef)+sizeof(GCRef)*(n)) + +/* -- Table object -------------------------------------------------------- */ + +/* Hash node. */ +typedef struct Node { + TValue val; /* Value object. Must be first field. */ + TValue key; /* Key object. */ + MRef next; /* Hash chain. */ + MRef freetop; /* Top of free elements (stored in t->node[0]). */ +} Node; + +LJ_STATIC_ASSERT(offsetof(Node, val) == 0); + +typedef struct GCtab { + GCHeader; + uint8_t nomm; /* Negative cache for fast metamethods. */ + int8_t colo; /* Array colocation. */ + MRef array; /* Array part. */ + GCRef gclist; + GCRef metatable; /* Must be at same offset in GCudata. */ + MRef node; /* Hash part. */ + uint32_t asize; /* Size of array part (keys [0, asize-1]). */ + uint32_t hmask; /* Hash part mask (size of hash part - 1). */ +} GCtab; + +#define sizetabcolo(n) ((n)*sizeof(TValue) + sizeof(GCtab)) +#define tabref(r) (&gcref((r))->tab) +#define noderef(r) (mref((r), Node)) +#define nextnode(n) (mref((n)->next, Node)) + +/* -- State objects ------------------------------------------------------- */ + +/* VM states. */ +enum { + LJ_VMST_INTERP, /* Interpreter. */ + LJ_VMST_C, /* C function. */ + LJ_VMST_GC, /* Garbage collector. */ + LJ_VMST_EXIT, /* Trace exit handler. */ + LJ_VMST_RECORD, /* Trace recorder. */ + LJ_VMST_OPT, /* Optimizer. */ + LJ_VMST_ASM, /* Assembler. */ + LJ_VMST__MAX +}; + +#define setvmstate(g, st) ((g)->vmstate = ~LJ_VMST_##st) + +/* Metamethods. ORDER MM */ +#ifdef LJ_HASFFI +#define MMDEF_FFI(_) _(new) +#else +#define MMDEF_FFI(_) +#endif + +#if LJ_52 || LJ_HASFFI +#define MMDEF_PAIRS(_) _(pairs) _(ipairs) +#else +#define MMDEF_PAIRS(_) +#define MM_pairs 255 +#define MM_ipairs 255 +#endif + +#define MMDEF(_) \ + _(index) _(newindex) _(gc) _(mode) _(eq) _(len) \ + /* Only the above (fast) metamethods are negative cached (max. 8). */ \ + _(lt) _(le) _(concat) _(call) \ + /* The following must be in ORDER ARITH. */ \ + _(add) _(sub) _(mul) _(div) _(mod) _(pow) _(unm) \ + /* The following are used in the standard libraries. */ \ + _(metatable) _(tostring) MMDEF_FFI(_) MMDEF_PAIRS(_) + +typedef enum { +#define MMENUM(name) MM_##name, +MMDEF(MMENUM) +#undef MMENUM + MM__MAX, + MM____ = MM__MAX, + MM_FAST = MM_len +} MMS; + +/* GC root IDs. */ +typedef enum { + GCROOT_MMNAME, /* Metamethod names. */ + GCROOT_MMNAME_LAST = GCROOT_MMNAME + MM__MAX-1, + GCROOT_BASEMT, /* Metatables for base types. */ + GCROOT_BASEMT_NUM = GCROOT_BASEMT + ~LJ_TNUMX, + GCROOT_IO_INPUT, /* Userdata for default I/O input file. */ + GCROOT_IO_OUTPUT, /* Userdata for default I/O output file. */ + GCROOT_MAX +} GCRootID; + +#define basemt_it(g, it) ((g)->gcroot[GCROOT_BASEMT+~(it)]) +#define basemt_obj(g, o) ((g)->gcroot[GCROOT_BASEMT+itypemap(o)]) +#define mmname_str(g, mm) (strref((g)->gcroot[GCROOT_MMNAME+(mm)])) + +typedef struct GCState { + MSize total; /* Memory currently allocated. */ + MSize threshold; /* Memory threshold. */ + uint8_t currentwhite; /* Current white color. */ + uint8_t state; /* GC state. */ + uint8_t unused1; + uint8_t unused2; + MSize sweepstr; /* Sweep position in string table. */ + GCRef root; /* List of all collectable objects. */ + MRef sweep; /* Sweep position in root list. */ + GCRef gray; /* List of gray objects. */ + GCRef grayagain; /* List of objects for atomic traversal. */ + GCRef weak; /* List of weak tables (to be cleared). */ + GCRef mmudata; /* List of userdata (to be finalized). */ + MSize stepmul; /* Incremental GC step granularity. */ + MSize debt; /* Debt (how much GC is behind schedule). */ + MSize estimate; /* Estimate of memory actually in use. */ + MSize pause; /* Pause between successive GC cycles. */ +} GCState; + +/* Global state, shared by all threads of a Lua universe. */ +typedef struct global_State { + GCRef *strhash; /* String hash table (hash chain anchors). */ + MSize strmask; /* String hash mask (size of hash table - 1). */ + MSize strnum; /* Number of strings in hash table. */ + lua_Alloc allocf; /* Memory allocator. */ + void *allocd; /* Memory allocator data. */ + GCState gc; /* Garbage collector. */ + SBuf tmpbuf; /* Temporary buffer for string concatenation. */ + Node nilnode; /* Fallback 1-element hash part (nil key and value). */ + GCstr strempty; /* Empty string. */ + uint8_t stremptyz; /* Zero terminator of empty string. */ + uint8_t hookmask; /* Hook mask. */ + uint8_t dispatchmode; /* Dispatch mode. */ + uint8_t vmevmask; /* VM event mask. */ + GCRef mainthref; /* Link to main thread. */ + TValue registrytv; /* Anchor for registry. */ + TValue tmptv, tmptv2; /* Temporary TValues. */ + GCupval uvhead; /* Head of double-linked list of all open upvalues. */ + int32_t hookcount; /* Instruction hook countdown. */ + int32_t hookcstart; /* Start count for instruction hook counter. */ + lua_Hook hookf; /* Hook function. */ + lua_CFunction wrapf; /* Wrapper for C function calls. */ + lua_CFunction panic; /* Called as a last resort for errors. */ + volatile int32_t vmstate; /* VM state or current JIT code trace number. */ + BCIns bc_cfunc_int; /* Bytecode for internal C function calls. */ + BCIns bc_cfunc_ext; /* Bytecode for external C function calls. */ + GCRef jit_L; /* Current JIT code lua_State or NULL. */ + MRef jit_base; /* Current JIT code L->base. */ + MRef ctype_state; /* Pointer to C type state. */ + GCRef gcroot[GCROOT_MAX]; /* GC roots. */ +} global_State; + +#define mainthread(g) (&gcref(g->mainthref)->th) +#define niltv(L) \ + check_exp(tvisnil(&G(L)->nilnode.val), &G(L)->nilnode.val) +#define niltvg(g) \ + check_exp(tvisnil(&(g)->nilnode.val), &(g)->nilnode.val) + +/* Hook management. Hook event masks are defined in lua.h. */ +#define HOOK_EVENTMASK 0x0f +#define HOOK_ACTIVE 0x10 +#define HOOK_ACTIVE_SHIFT 4 +#define HOOK_VMEVENT 0x20 +#define HOOK_GC 0x40 +#define hook_active(g) ((g)->hookmask & HOOK_ACTIVE) +#define hook_enter(g) ((g)->hookmask |= HOOK_ACTIVE) +#define hook_entergc(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_GC)) +#define hook_vmevent(g) ((g)->hookmask |= (HOOK_ACTIVE|HOOK_VMEVENT)) +#define hook_leave(g) ((g)->hookmask &= ~HOOK_ACTIVE) +#define hook_save(g) ((g)->hookmask & ~HOOK_EVENTMASK) +#define hook_restore(g, h) \ + ((g)->hookmask = ((g)->hookmask & HOOK_EVENTMASK) | (h)) + +/* Per-thread state object. */ +struct lua_State { + GCHeader; + uint8_t dummy_ffid; /* Fake FF_C for curr_funcisL() on dummy frames. */ + uint8_t status; /* Thread status. */ + MRef glref; /* Link to global state. */ + GCRef gclist; /* GC chain. */ + TValue *base; /* Base of currently executing function. */ + TValue *top; /* First free slot in the stack. */ + MRef maxstack; /* Last free slot in the stack. */ + MRef stack; /* Stack base. */ + GCRef openupval; /* List of open upvalues in the stack. */ + GCRef env; /* Thread environment (table of globals). */ + void *cframe; /* End of C stack frame chain. */ + MSize stacksize; /* True stack size (incl. LJ_STACK_EXTRA). */ +}; + +#define G(L) (mref(L->glref, global_State)) +#define registry(L) (&G(L)->registrytv) + +/* Macros to access the currently executing (Lua) function. */ +#define curr_func(L) (&gcref((L->base-1)->fr.func)->fn) +#define curr_funcisL(L) (isluafunc(curr_func(L))) +#define curr_proto(L) (funcproto(curr_func(L))) +#define curr_topL(L) (L->base + curr_proto(L)->framesize) +#define curr_top(L) (curr_funcisL(L) ? curr_topL(L) : L->top) + +/* -- GC object definition and conversions -------------------------------- */ + +/* GC header for generic access to common fields of GC objects. */ +typedef struct GChead { + GCHeader; + uint8_t unused1; + uint8_t unused2; + GCRef env; + GCRef gclist; + GCRef metatable; +} GChead; + +/* The env field SHOULD be at the same offset for all GC objects. */ +LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCfuncL, env)); +LJ_STATIC_ASSERT(offsetof(GChead, env) == offsetof(GCudata, env)); + +/* The metatable field MUST be at the same offset for all GC objects. */ +LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCtab, metatable)); +LJ_STATIC_ASSERT(offsetof(GChead, metatable) == offsetof(GCudata, metatable)); + +/* The gclist field MUST be at the same offset for all GC objects. */ +LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(lua_State, gclist)); +LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCproto, gclist)); +LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCfuncL, gclist)); +LJ_STATIC_ASSERT(offsetof(GChead, gclist) == offsetof(GCtab, gclist)); + +typedef union GCobj { + GChead gch; + GCstr str; + GCupval uv; + lua_State th; + GCproto pt; + GCfunc fn; + GCcdata cd; + GCtab tab; + GCudata ud; +} GCobj; + +/* Macros to convert a GCobj pointer into a specific value. */ +#define gco2str(o) check_exp((o)->gch.gct == ~LJ_TSTR, &(o)->str) +#define gco2uv(o) check_exp((o)->gch.gct == ~LJ_TUPVAL, &(o)->uv) +#define gco2th(o) check_exp((o)->gch.gct == ~LJ_TTHREAD, &(o)->th) +#define gco2pt(o) check_exp((o)->gch.gct == ~LJ_TPROTO, &(o)->pt) +#define gco2func(o) check_exp((o)->gch.gct == ~LJ_TFUNC, &(o)->fn) +#define gco2cd(o) check_exp((o)->gch.gct == ~LJ_TCDATA, &(o)->cd) +#define gco2tab(o) check_exp((o)->gch.gct == ~LJ_TTAB, &(o)->tab) +#define gco2ud(o) check_exp((o)->gch.gct == ~LJ_TUDATA, &(o)->ud) + +/* Macro to convert any collectable object into a GCobj pointer. */ +#define obj2gco(v) ((GCobj *)(v)) + +/* -- TValue getters/setters ---------------------------------------------- */ + +#ifdef LUA_USE_ASSERT +#include "lj_gc.h" +#endif + +/* Macros to test types. */ +#define itype(o) ((o)->it) +#define tvisnil(o) (itype(o) == LJ_TNIL) +#define tvisfalse(o) (itype(o) == LJ_TFALSE) +#define tvistrue(o) (itype(o) == LJ_TTRUE) +#define tvisbool(o) (tvisfalse(o) || tvistrue(o)) +#if LJ_64 +#define tvislightud(o) (((int32_t)itype(o) >> 15) == -2) +#else +#define tvislightud(o) (itype(o) == LJ_TLIGHTUD) +#endif +#define tvisstr(o) (itype(o) == LJ_TSTR) +#define tvisfunc(o) (itype(o) == LJ_TFUNC) +#define tvisthread(o) (itype(o) == LJ_TTHREAD) +#define tvisproto(o) (itype(o) == LJ_TPROTO) +#define tviscdata(o) (itype(o) == LJ_TCDATA) +#define tvistab(o) (itype(o) == LJ_TTAB) +#define tvisudata(o) (itype(o) == LJ_TUDATA) +#define tvisnumber(o) (itype(o) <= LJ_TISNUM) +#define tvisint(o) (LJ_DUALNUM && itype(o) == LJ_TISNUM) +#define tvisnum(o) (itype(o) < LJ_TISNUM) + +#define tvistruecond(o) (itype(o) < LJ_TISTRUECOND) +#define tvispri(o) (itype(o) >= LJ_TISPRI) +#define tvistabud(o) (itype(o) <= LJ_TISTABUD) /* && !tvisnum() */ +#define tvisgcv(o) ((itype(o) - LJ_TISGCV) > (LJ_TNUMX - LJ_TISGCV)) + +/* Special macros to test numbers for NaN, +0, -0, +1 and raw equality. */ +#define tvisnan(o) ((o)->n != (o)->n) +#if LJ_64 +#define tviszero(o) (((o)->u64 << 1) == 0) +#else +#define tviszero(o) (((o)->u32.lo | ((o)->u32.hi << 1)) == 0) +#endif +#define tvispzero(o) ((o)->u64 == 0) +#define tvismzero(o) ((o)->u64 == U64x(80000000,00000000)) +#define tvispone(o) ((o)->u64 == U64x(3ff00000,00000000)) +#define rawnumequal(o1, o2) ((o1)->u64 == (o2)->u64) + +/* Macros to convert type ids. */ +#if LJ_64 +#define itypemap(o) \ + (tvisnumber(o) ? ~LJ_TNUMX : tvislightud(o) ? ~LJ_TLIGHTUD : ~itype(o)) +#else +#define itypemap(o) (tvisnumber(o) ? ~LJ_TNUMX : ~itype(o)) +#endif + +/* Macros to get tagged values. */ +#define gcval(o) (gcref((o)->gcr)) +#define boolV(o) check_exp(tvisbool(o), (LJ_TFALSE - (o)->it)) +#if LJ_64 +#define lightudV(o) \ + check_exp(tvislightud(o), (void *)((o)->u64 & U64x(00007fff,ffffffff))) +#else +#define lightudV(o) check_exp(tvislightud(o), gcrefp((o)->gcr, void)) +#endif +#define gcV(o) check_exp(tvisgcv(o), gcval(o)) +#define strV(o) check_exp(tvisstr(o), &gcval(o)->str) +#define funcV(o) check_exp(tvisfunc(o), &gcval(o)->fn) +#define threadV(o) check_exp(tvisthread(o), &gcval(o)->th) +#define protoV(o) check_exp(tvisproto(o), &gcval(o)->pt) +#define cdataV(o) check_exp(tviscdata(o), &gcval(o)->cd) +#define tabV(o) check_exp(tvistab(o), &gcval(o)->tab) +#define udataV(o) check_exp(tvisudata(o), &gcval(o)->ud) +#define numV(o) check_exp(tvisnum(o), (o)->n) +#define intV(o) check_exp(tvisint(o), (int32_t)(o)->i) + +/* Macros to set tagged values. */ +#define setitype(o, i) ((o)->it = (i)) +#define setnilV(o) ((o)->it = LJ_TNIL) +#define setboolV(o, x) ((o)->it = LJ_TFALSE-(uint32_t)(x)) + +static LJ_AINLINE void setlightudV(TValue *o, void *p) +{ +#if LJ_64 + o->u64 = (uint64_t)p | (((uint64_t)0xffff) << 48); +#else + setgcrefp(o->gcr, p); setitype(o, LJ_TLIGHTUD); +#endif +} + +#if LJ_64 +#define checklightudptr(L, p) \ + (((uint64_t)(p) >> 47) ? (lj_err_msg(L, LJ_ERR_BADLU), NULL) : (p)) +#define setcont(o, f) \ + ((o)->u64 = (uint64_t)(void *)(f) - (uint64_t)lj_vm_asm_begin) +#else +#define checklightudptr(L, p) (p) +#define setcont(o, f) setlightudV((o), (void *)(f)) +#endif + +#define tvchecklive(L, o) \ + UNUSED(L), lua_assert(!tvisgcv(o) || \ + ((~itype(o) == gcval(o)->gch.gct) && !isdead(G(L), gcval(o)))) + +static LJ_AINLINE void setgcV(lua_State *L, TValue *o, GCobj *v, uint32_t itype) +{ + setgcref(o->gcr, v); setitype(o, itype); tvchecklive(L, o); +} + +#define define_setV(name, type, tag) \ +static LJ_AINLINE void name(lua_State *L, TValue *o, type *v) \ +{ \ + setgcV(L, o, obj2gco(v), tag); \ +} +define_setV(setstrV, GCstr, LJ_TSTR) +define_setV(setthreadV, lua_State, LJ_TTHREAD) +define_setV(setprotoV, GCproto, LJ_TPROTO) +define_setV(setfuncV, GCfunc, LJ_TFUNC) +define_setV(setcdataV, GCcdata, LJ_TCDATA) +define_setV(settabV, GCtab, LJ_TTAB) +define_setV(setudataV, GCudata, LJ_TUDATA) + +#define setnumV(o, x) ((o)->n = (x)) +#define setnanV(o) ((o)->u64 = U64x(fff80000,00000000)) +#define setpinfV(o) ((o)->u64 = U64x(7ff00000,00000000)) +#define setminfV(o) ((o)->u64 = U64x(fff00000,00000000)) + +static LJ_AINLINE void setintV(TValue *o, int32_t i) +{ +#if LJ_DUALNUM + o->i = (uint32_t)i; setitype(o, LJ_TISNUM); +#else + o->n = (lua_Number)i; +#endif +} + +static LJ_AINLINE void setint64V(TValue *o, int64_t i) +{ + if (LJ_DUALNUM && LJ_LIKELY(i == (int64_t)(int32_t)i)) + setintV(o, (int32_t)i); + else + setnumV(o, (lua_Number)i); +} + +#if LJ_64 +#define setintptrV(o, i) setint64V((o), (i)) +#else +#define setintptrV(o, i) setintV((o), (i)) +#endif + +/* Copy tagged values. */ +static LJ_AINLINE void copyTV(lua_State *L, TValue *o1, const TValue *o2) +{ + *o1 = *o2; tvchecklive(L, o1); +} + +/* -- Number to integer conversion ---------------------------------------- */ + +#if LJ_SOFTFP +LJ_ASMF int32_t lj_vm_tobit(double x); +#endif + +static LJ_AINLINE int32_t lj_num2bit(lua_Number n) +{ +#if LJ_SOFTFP + return lj_vm_tobit(n); +#else + TValue o; + o.n = n + 6755399441055744.0; /* 2^52 + 2^51 */ + return (int32_t)o.u32.lo; +#endif +} + +#if LJ_TARGET_X86 && !defined(__SSE2__) +#define lj_num2int(n) lj_num2bit((n)) +#else +#define lj_num2int(n) ((int32_t)(n)) +#endif + +static LJ_AINLINE uint64_t lj_num2u64(lua_Number n) +{ +#ifdef _MSC_VER + if (n >= 9223372036854775808.0) /* They think it's a feature. */ + return (uint64_t)(int64_t)(n - 18446744073709551616.0); + else +#endif + return (uint64_t)n; +} + +static LJ_AINLINE int32_t numberVint(cTValue *o) +{ + if (LJ_LIKELY(tvisint(o))) + return intV(o); + else + return lj_num2int(numV(o)); +} + +static LJ_AINLINE lua_Number numberVnum(cTValue *o) +{ + if (LJ_UNLIKELY(tvisint(o))) + return (lua_Number)intV(o); + else + return numV(o); +} + +/* -- Miscellaneous object handling --------------------------------------- */ + +/* Names and maps for internal and external object tags. */ +LJ_DATA const char *const lj_obj_typename[1+LUA_TCDATA+1]; +LJ_DATA const char *const lj_obj_itypename[~LJ_TNUMX+1]; + +#define lj_typename(o) (lj_obj_itypename[itypemap(o)]) + +/* Compare two objects without calling metamethods. */ +LJ_FUNC int lj_obj_equal(cTValue *o1, cTValue *o2); + +#endif diff --git a/external/lua/luajit/src/src/lj_opt_dce.c b/external/lua/luajit/src/src/lj_opt_dce.c new file mode 100644 index 0000000000..d64cca76cc --- /dev/null +++ b/external/lua/luajit/src/src/lj_opt_dce.c @@ -0,0 +1,77 @@ +/* +** DCE: Dead Code Elimination. Pre-LOOP only -- ASM already performs DCE. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_opt_dce_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_iropt.h" + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) + +/* Scan through all snapshots and mark all referenced instructions. */ +static void dce_marksnap(jit_State *J) +{ + SnapNo i, nsnap = J->cur.nsnap; + for (i = 0; i < nsnap; i++) { + SnapShot *snap = &J->cur.snap[i]; + SnapEntry *map = &J->cur.snapmap[snap->mapofs]; + MSize n, nent = snap->nent; + for (n = 0; n < nent; n++) { + IRRef ref = snap_ref(map[n]); + if (ref >= REF_FIRST) + irt_setmark(IR(ref)->t); + } + } +} + +/* Backwards propagate marks. Replace unused instructions with NOPs. */ +static void dce_propagate(jit_State *J) +{ + IRRef1 *pchain[IR__MAX]; + IRRef ins; + uint32_t i; + for (i = 0; i < IR__MAX; i++) pchain[i] = &J->chain[i]; + for (ins = J->cur.nins-1; ins >= REF_FIRST; ins--) { + IRIns *ir = IR(ins); + if (irt_ismarked(ir->t)) { + irt_clearmark(ir->t); + pchain[ir->o] = &ir->prev; + } else if (!ir_sideeff(ir)) { + *pchain[ir->o] = ir->prev; /* Reroute original instruction chain. */ + ir->t.irt = IRT_NIL; + ir->o = IR_NOP; /* Replace instruction with NOP. */ + ir->op1 = ir->op2 = 0; + ir->prev = 0; + continue; + } + if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t); + if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t); + } +} + +/* Dead Code Elimination. +** +** First backpropagate marks for all used instructions. Then replace +** the unused ones with a NOP. Note that compressing the IR to eliminate +** the NOPs does not pay off. +*/ +void lj_opt_dce(jit_State *J) +{ + if ((J->flags & JIT_F_OPT_DCE)) { + dce_marksnap(J); + dce_propagate(J); + } +} + +#undef IR + +#endif diff --git a/external/lua/luajit/src/src/lj_opt_fold.c b/external/lua/luajit/src/src/lj_opt_fold.c new file mode 100644 index 0000000000..be50bf9784 --- /dev/null +++ b/external/lua/luajit/src/src/lj_opt_fold.c @@ -0,0 +1,2292 @@ +/* +** FOLD: Constant Folding, Algebraic Simplifications and Reassociation. +** ABCelim: Array Bounds Check Elimination. +** CSE: Common-Subexpression Elimination. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_opt_fold_c +#define LUA_CORE + +#include + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_iropt.h" +#include "lj_trace.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#endif +#include "lj_carith.h" +#include "lj_vm.h" +#include "lj_strscan.h" + +/* Here's a short description how the FOLD engine processes instructions: +** +** The FOLD engine receives a single instruction stored in fins (J->fold.ins). +** The instruction and its operands are used to select matching fold rules. +** These are applied iteratively until a fixed point is reached. +** +** The 8 bit opcode of the instruction itself plus the opcodes of the +** two instructions referenced by its operands form a 24 bit key +** 'ins left right' (unused operands -> 0, literals -> lowest 8 bits). +** +** This key is used for partial matching against the fold rules. The +** left/right operand fields of the key are successively masked with +** the 'any' wildcard, from most specific to least specific: +** +** ins left right +** ins any right +** ins left any +** ins any any +** +** The masked key is used to lookup a matching fold rule in a semi-perfect +** hash table. If a matching rule is found, the related fold function is run. +** Multiple rules can share the same fold function. A fold rule may return +** one of several special values: +** +** - NEXTFOLD means no folding was applied, because an additional test +** inside the fold function failed. Matching continues against less +** specific fold rules. Finally the instruction is passed on to CSE. +** +** - RETRYFOLD means the instruction was modified in-place. Folding is +** retried as if this instruction had just been received. +** +** All other return values are terminal actions -- no further folding is +** applied: +** +** - INTFOLD(i) returns a reference to the integer constant i. +** +** - LEFTFOLD and RIGHTFOLD return the left/right operand reference +** without emitting an instruction. +** +** - CSEFOLD and EMITFOLD pass the instruction directly to CSE or emit +** it without passing through any further optimizations. +** +** - FAILFOLD, DROPFOLD and CONDFOLD only apply to instructions which have +** no result (e.g. guarded assertions): FAILFOLD means the guard would +** always fail, i.e. the current trace is pointless. DROPFOLD means +** the guard is always true and has been eliminated. CONDFOLD is a +** shortcut for FAILFOLD + cond (i.e. drop if true, otherwise fail). +** +** - Any other return value is interpreted as an IRRef or TRef. This +** can be a reference to an existing or a newly created instruction. +** Only the least-significant 16 bits (IRRef1) are used to form a TRef +** which is finally returned to the caller. +** +** The FOLD engine receives instructions both from the trace recorder and +** substituted instructions from LOOP unrolling. This means all types +** of instructions may end up here, even though the recorder bypasses +** FOLD in some cases. Thus all loads, stores and allocations must have +** an any/any rule to avoid being passed on to CSE. +** +** Carefully read the following requirements before adding or modifying +** any fold rules: +** +** Requirement #1: All fold rules must preserve their destination type. +** +** Consistently use INTFOLD() (KINT result) or lj_ir_knum() (KNUM result). +** Never use lj_ir_knumint() which can have either a KINT or KNUM result. +** +** Requirement #2: Fold rules should not create *new* instructions which +** reference operands *across* PHIs. +** +** E.g. a RETRYFOLD with 'fins->op1 = fleft->op1' is invalid if the +** left operand is a PHI. Then fleft->op1 would point across the PHI +** frontier to an invariant instruction. Adding a PHI for this instruction +** would be counterproductive. The solution is to add a barrier which +** prevents folding across PHIs, i.e. 'PHIBARRIER(fleft)' in this case. +** The only exception is for recurrences with high latencies like +** repeated int->num->int conversions. +** +** One could relax this condition a bit if the referenced instruction is +** a PHI, too. But this often leads to worse code due to excessive +** register shuffling. +** +** Note: returning *existing* instructions (e.g. LEFTFOLD) is ok, though. +** Even returning fleft->op1 would be ok, because a new PHI will added, +** if needed. But again, this leads to excessive register shuffling and +** should be avoided. +** +** Requirement #3: The set of all fold rules must be monotonic to guarantee +** termination. +** +** The goal is optimization, so one primarily wants to add strength-reducing +** rules. This means eliminating an instruction or replacing an instruction +** with one or more simpler instructions. Don't add fold rules which point +** into the other direction. +** +** Some rules (like commutativity) do not directly reduce the strength of +** an instruction, but enable other fold rules (e.g. by moving constants +** to the right operand). These rules must be made unidirectional to avoid +** cycles. +** +** Rule of thumb: the trace recorder expands the IR and FOLD shrinks it. +*/ + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) +#define fins (&J->fold.ins) +#define fleft (&J->fold.left) +#define fright (&J->fold.right) +#define knumleft (ir_knum(fleft)->n) +#define knumright (ir_knum(fright)->n) + +/* Pass IR on to next optimization in chain (FOLD). */ +#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) + +/* Fold function type. Fastcall on x86 significantly reduces their size. */ +typedef IRRef (LJ_FASTCALL *FoldFunc)(jit_State *J); + +/* Macros for the fold specs, so buildvm can recognize them. */ +#define LJFOLD(x) +#define LJFOLDX(x) +#define LJFOLDF(name) static TRef LJ_FASTCALL fold_##name(jit_State *J) +/* Note: They must be at the start of a line or buildvm ignores them! */ + +/* Barrier to prevent using operands across PHIs. */ +#define PHIBARRIER(ir) if (irt_isphi((ir)->t)) return NEXTFOLD + +/* Barrier to prevent folding across a GC step. +** GC steps can only happen at the head of a trace and at LOOP. +** And the GC is only driven forward if there is at least one allocation. +*/ +#define gcstep_barrier(J, ref) \ + ((ref) < J->chain[IR_LOOP] && \ + (J->chain[IR_SNEW] || J->chain[IR_XSNEW] || \ + J->chain[IR_TNEW] || J->chain[IR_TDUP] || \ + J->chain[IR_CNEW] || J->chain[IR_CNEWI] || J->chain[IR_TOSTR])) + +/* -- Constant folding for FP numbers ------------------------------------- */ + +LJFOLD(ADD KNUM KNUM) +LJFOLD(SUB KNUM KNUM) +LJFOLD(MUL KNUM KNUM) +LJFOLD(DIV KNUM KNUM) +LJFOLD(NEG KNUM KNUM) +LJFOLD(ABS KNUM KNUM) +LJFOLD(ATAN2 KNUM KNUM) +LJFOLD(LDEXP KNUM KNUM) +LJFOLD(MIN KNUM KNUM) +LJFOLD(MAX KNUM KNUM) +LJFOLDF(kfold_numarith) +{ + lua_Number a = knumleft; + lua_Number b = knumright; + lua_Number y = lj_vm_foldarith(a, b, fins->o - IR_ADD); + return lj_ir_knum(J, y); +} + +LJFOLD(LDEXP KNUM KINT) +LJFOLDF(kfold_ldexp) +{ +#if LJ_TARGET_X86ORX64 + UNUSED(J); + return NEXTFOLD; +#else + return lj_ir_knum(J, ldexp(knumleft, fright->i)); +#endif +} + +LJFOLD(FPMATH KNUM any) +LJFOLDF(kfold_fpmath) +{ + lua_Number a = knumleft; + lua_Number y = lj_vm_foldfpm(a, fins->op2); + return lj_ir_knum(J, y); +} + +LJFOLD(POW KNUM KINT) +LJFOLDF(kfold_numpow) +{ + lua_Number a = knumleft; + lua_Number b = (lua_Number)fright->i; + lua_Number y = lj_vm_foldarith(a, b, IR_POW - IR_ADD); + return lj_ir_knum(J, y); +} + +/* Must not use kfold_kref for numbers (could be NaN). */ +LJFOLD(EQ KNUM KNUM) +LJFOLD(NE KNUM KNUM) +LJFOLD(LT KNUM KNUM) +LJFOLD(GE KNUM KNUM) +LJFOLD(LE KNUM KNUM) +LJFOLD(GT KNUM KNUM) +LJFOLD(ULT KNUM KNUM) +LJFOLD(UGE KNUM KNUM) +LJFOLD(ULE KNUM KNUM) +LJFOLD(UGT KNUM KNUM) +LJFOLDF(kfold_numcomp) +{ + return CONDFOLD(lj_ir_numcmp(knumleft, knumright, (IROp)fins->o)); +} + +/* -- Constant folding for 32 bit integers -------------------------------- */ + +static int32_t kfold_intop(int32_t k1, int32_t k2, IROp op) +{ + switch (op) { + case IR_ADD: k1 += k2; break; + case IR_SUB: k1 -= k2; break; + case IR_MUL: k1 *= k2; break; + case IR_MOD: k1 = lj_vm_modi(k1, k2); break; + case IR_NEG: k1 = -k1; break; + case IR_BAND: k1 &= k2; break; + case IR_BOR: k1 |= k2; break; + case IR_BXOR: k1 ^= k2; break; + case IR_BSHL: k1 <<= (k2 & 31); break; + case IR_BSHR: k1 = (int32_t)((uint32_t)k1 >> (k2 & 31)); break; + case IR_BSAR: k1 >>= (k2 & 31); break; + case IR_BROL: k1 = (int32_t)lj_rol((uint32_t)k1, (k2 & 31)); break; + case IR_BROR: k1 = (int32_t)lj_ror((uint32_t)k1, (k2 & 31)); break; + case IR_MIN: k1 = k1 < k2 ? k1 : k2; break; + case IR_MAX: k1 = k1 > k2 ? k1 : k2; break; + default: lua_assert(0); break; + } + return k1; +} + +LJFOLD(ADD KINT KINT) +LJFOLD(SUB KINT KINT) +LJFOLD(MUL KINT KINT) +LJFOLD(MOD KINT KINT) +LJFOLD(NEG KINT KINT) +LJFOLD(BAND KINT KINT) +LJFOLD(BOR KINT KINT) +LJFOLD(BXOR KINT KINT) +LJFOLD(BSHL KINT KINT) +LJFOLD(BSHR KINT KINT) +LJFOLD(BSAR KINT KINT) +LJFOLD(BROL KINT KINT) +LJFOLD(BROR KINT KINT) +LJFOLD(MIN KINT KINT) +LJFOLD(MAX KINT KINT) +LJFOLDF(kfold_intarith) +{ + return INTFOLD(kfold_intop(fleft->i, fright->i, (IROp)fins->o)); +} + +LJFOLD(ADDOV KINT KINT) +LJFOLD(SUBOV KINT KINT) +LJFOLD(MULOV KINT KINT) +LJFOLDF(kfold_intovarith) +{ + lua_Number n = lj_vm_foldarith((lua_Number)fleft->i, (lua_Number)fright->i, + fins->o - IR_ADDOV); + int32_t k = lj_num2int(n); + if (n != (lua_Number)k) + return FAILFOLD; + return INTFOLD(k); +} + +LJFOLD(BNOT KINT) +LJFOLDF(kfold_bnot) +{ + return INTFOLD(~fleft->i); +} + +LJFOLD(BSWAP KINT) +LJFOLDF(kfold_bswap) +{ + return INTFOLD((int32_t)lj_bswap((uint32_t)fleft->i)); +} + +LJFOLD(LT KINT KINT) +LJFOLD(GE KINT KINT) +LJFOLD(LE KINT KINT) +LJFOLD(GT KINT KINT) +LJFOLD(ULT KINT KINT) +LJFOLD(UGE KINT KINT) +LJFOLD(ULE KINT KINT) +LJFOLD(UGT KINT KINT) +LJFOLD(ABC KINT KINT) +LJFOLDF(kfold_intcomp) +{ + int32_t a = fleft->i, b = fright->i; + switch ((IROp)fins->o) { + case IR_LT: return CONDFOLD(a < b); + case IR_GE: return CONDFOLD(a >= b); + case IR_LE: return CONDFOLD(a <= b); + case IR_GT: return CONDFOLD(a > b); + case IR_ULT: return CONDFOLD((uint32_t)a < (uint32_t)b); + case IR_UGE: return CONDFOLD((uint32_t)a >= (uint32_t)b); + case IR_ULE: return CONDFOLD((uint32_t)a <= (uint32_t)b); + case IR_ABC: + case IR_UGT: return CONDFOLD((uint32_t)a > (uint32_t)b); + default: lua_assert(0); return FAILFOLD; + } +} + +LJFOLD(UGE any KINT) +LJFOLDF(kfold_intcomp0) +{ + if (fright->i == 0) + return DROPFOLD; + return NEXTFOLD; +} + +/* -- Constant folding for 64 bit integers -------------------------------- */ + +static uint64_t kfold_int64arith(uint64_t k1, uint64_t k2, IROp op) +{ + switch (op) { +#if LJ_64 || LJ_HASFFI + case IR_ADD: k1 += k2; break; + case IR_SUB: k1 -= k2; break; +#endif +#if LJ_HASFFI + case IR_MUL: k1 *= k2; break; + case IR_BAND: k1 &= k2; break; + case IR_BOR: k1 |= k2; break; + case IR_BXOR: k1 ^= k2; break; +#endif + default: UNUSED(k2); lua_assert(0); break; + } + return k1; +} + +LJFOLD(ADD KINT64 KINT64) +LJFOLD(SUB KINT64 KINT64) +LJFOLD(MUL KINT64 KINT64) +LJFOLD(BAND KINT64 KINT64) +LJFOLD(BOR KINT64 KINT64) +LJFOLD(BXOR KINT64 KINT64) +LJFOLDF(kfold_int64arith) +{ + return INT64FOLD(kfold_int64arith(ir_k64(fleft)->u64, + ir_k64(fright)->u64, (IROp)fins->o)); +} + +LJFOLD(DIV KINT64 KINT64) +LJFOLD(MOD KINT64 KINT64) +LJFOLD(POW KINT64 KINT64) +LJFOLDF(kfold_int64arith2) +{ +#if LJ_HASFFI + uint64_t k1 = ir_k64(fleft)->u64, k2 = ir_k64(fright)->u64; + if (irt_isi64(fins->t)) { + k1 = fins->o == IR_DIV ? lj_carith_divi64((int64_t)k1, (int64_t)k2) : + fins->o == IR_MOD ? lj_carith_modi64((int64_t)k1, (int64_t)k2) : + lj_carith_powi64((int64_t)k1, (int64_t)k2); + } else { + k1 = fins->o == IR_DIV ? lj_carith_divu64(k1, k2) : + fins->o == IR_MOD ? lj_carith_modu64(k1, k2) : + lj_carith_powu64(k1, k2); + } + return INT64FOLD(k1); +#else + UNUSED(J); lua_assert(0); return FAILFOLD; +#endif +} + +LJFOLD(BSHL KINT64 KINT) +LJFOLD(BSHR KINT64 KINT) +LJFOLD(BSAR KINT64 KINT) +LJFOLD(BROL KINT64 KINT) +LJFOLD(BROR KINT64 KINT) +LJFOLDF(kfold_int64shift) +{ +#if LJ_HASFFI || LJ_64 + uint64_t k = ir_k64(fleft)->u64; + int32_t sh = (fright->i & 63); + switch ((IROp)fins->o) { + case IR_BSHL: k <<= sh; break; +#if LJ_HASFFI + case IR_BSHR: k >>= sh; break; + case IR_BSAR: k = (uint64_t)((int64_t)k >> sh); break; + case IR_BROL: k = lj_rol(k, sh); break; + case IR_BROR: k = lj_ror(k, sh); break; +#endif + default: lua_assert(0); break; + } + return INT64FOLD(k); +#else + UNUSED(J); lua_assert(0); return FAILFOLD; +#endif +} + +LJFOLD(BNOT KINT64) +LJFOLDF(kfold_bnot64) +{ +#if LJ_HASFFI + return INT64FOLD(~ir_k64(fleft)->u64); +#else + UNUSED(J); lua_assert(0); return FAILFOLD; +#endif +} + +LJFOLD(BSWAP KINT64) +LJFOLDF(kfold_bswap64) +{ +#if LJ_HASFFI + return INT64FOLD(lj_bswap64(ir_k64(fleft)->u64)); +#else + UNUSED(J); lua_assert(0); return FAILFOLD; +#endif +} + +LJFOLD(LT KINT64 KINT64) +LJFOLD(GE KINT64 KINT64) +LJFOLD(LE KINT64 KINT64) +LJFOLD(GT KINT64 KINT64) +LJFOLD(ULT KINT64 KINT64) +LJFOLD(UGE KINT64 KINT64) +LJFOLD(ULE KINT64 KINT64) +LJFOLD(UGT KINT64 KINT64) +LJFOLDF(kfold_int64comp) +{ +#if LJ_HASFFI + uint64_t a = ir_k64(fleft)->u64, b = ir_k64(fright)->u64; + switch ((IROp)fins->o) { + case IR_LT: return CONDFOLD(a < b); + case IR_GE: return CONDFOLD(a >= b); + case IR_LE: return CONDFOLD(a <= b); + case IR_GT: return CONDFOLD(a > b); + case IR_ULT: return CONDFOLD((uint64_t)a < (uint64_t)b); + case IR_UGE: return CONDFOLD((uint64_t)a >= (uint64_t)b); + case IR_ULE: return CONDFOLD((uint64_t)a <= (uint64_t)b); + case IR_UGT: return CONDFOLD((uint64_t)a > (uint64_t)b); + default: lua_assert(0); return FAILFOLD; + } +#else + UNUSED(J); lua_assert(0); return FAILFOLD; +#endif +} + +LJFOLD(UGE any KINT64) +LJFOLDF(kfold_int64comp0) +{ +#if LJ_HASFFI + if (ir_k64(fright)->u64 == 0) + return DROPFOLD; + return NEXTFOLD; +#else + UNUSED(J); lua_assert(0); return FAILFOLD; +#endif +} + +/* -- Constant folding for strings ---------------------------------------- */ + +LJFOLD(SNEW KKPTR KINT) +LJFOLDF(kfold_snew_kptr) +{ + GCstr *s = lj_str_new(J->L, (const char *)ir_kptr(fleft), (size_t)fright->i); + return lj_ir_kstr(J, s); +} + +LJFOLD(SNEW any KINT) +LJFOLDF(kfold_snew_empty) +{ + if (fright->i == 0) + return lj_ir_kstr(J, &J2G(J)->strempty); + return NEXTFOLD; +} + +LJFOLD(STRREF KGC KINT) +LJFOLDF(kfold_strref) +{ + GCstr *str = ir_kstr(fleft); + lua_assert((MSize)fright->i <= str->len); + return lj_ir_kkptr(J, (char *)strdata(str) + fright->i); +} + +LJFOLD(STRREF SNEW any) +LJFOLDF(kfold_strref_snew) +{ + PHIBARRIER(fleft); + if (irref_isk(fins->op2) && fright->i == 0) { + return fleft->op1; /* strref(snew(ptr, len), 0) ==> ptr */ + } else { + /* Reassociate: strref(snew(strref(str, a), len), b) ==> strref(str, a+b) */ + IRIns *ir = IR(fleft->op1); + IRRef1 str = ir->op1; /* IRIns * is not valid across emitir. */ + lua_assert(ir->o == IR_STRREF); + PHIBARRIER(ir); + fins->op2 = emitir(IRTI(IR_ADD), ir->op2, fins->op2); /* Clobbers fins! */ + fins->op1 = str; + fins->ot = IRT(IR_STRREF, IRT_P32); + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(CALLN CARG IRCALL_lj_str_cmp) +LJFOLDF(kfold_strcmp) +{ + if (irref_isk(fleft->op1) && irref_isk(fleft->op2)) { + GCstr *a = ir_kstr(IR(fleft->op1)); + GCstr *b = ir_kstr(IR(fleft->op2)); + return INTFOLD(lj_str_cmp(a, b)); + } + return NEXTFOLD; +} + +/* -- Constant folding of pointer arithmetic ------------------------------ */ + +LJFOLD(ADD KGC KINT) +LJFOLD(ADD KGC KINT64) +LJFOLDF(kfold_add_kgc) +{ + GCobj *o = ir_kgc(fleft); +#if LJ_64 + ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64; +#else + ptrdiff_t ofs = fright->i; +#endif +#if LJ_HASFFI + if (irt_iscdata(fleft->t)) { + CType *ct = ctype_raw(ctype_ctsG(J2G(J)), gco2cd(o)->ctypeid); + if (ctype_isnum(ct->info) || ctype_isenum(ct->info) || + ctype_isptr(ct->info) || ctype_isfunc(ct->info) || + ctype_iscomplex(ct->info) || ctype_isvector(ct->info)) + return lj_ir_kkptr(J, (char *)o + ofs); + } +#endif + return lj_ir_kptr(J, (char *)o + ofs); +} + +LJFOLD(ADD KPTR KINT) +LJFOLD(ADD KPTR KINT64) +LJFOLD(ADD KKPTR KINT) +LJFOLD(ADD KKPTR KINT64) +LJFOLDF(kfold_add_kptr) +{ + void *p = ir_kptr(fleft); +#if LJ_64 + ptrdiff_t ofs = (ptrdiff_t)ir_kint64(fright)->u64; +#else + ptrdiff_t ofs = fright->i; +#endif + return lj_ir_kptr_(J, fleft->o, (char *)p + ofs); +} + +LJFOLD(ADD any KGC) +LJFOLD(ADD any KPTR) +LJFOLD(ADD any KKPTR) +LJFOLDF(kfold_add_kright) +{ + if (fleft->o == IR_KINT || fleft->o == IR_KINT64) { + IRRef1 tmp = fins->op1; fins->op1 = fins->op2; fins->op2 = tmp; + return RETRYFOLD; + } + return NEXTFOLD; +} + +/* -- Constant folding of conversions ------------------------------------- */ + +LJFOLD(TOBIT KNUM KNUM) +LJFOLDF(kfold_tobit) +{ + return INTFOLD(lj_num2bit(knumleft)); +} + +LJFOLD(CONV KINT IRCONV_NUM_INT) +LJFOLDF(kfold_conv_kint_num) +{ + return lj_ir_knum(J, (lua_Number)fleft->i); +} + +LJFOLD(CONV KINT IRCONV_NUM_U32) +LJFOLDF(kfold_conv_kintu32_num) +{ + return lj_ir_knum(J, (lua_Number)(uint32_t)fleft->i); +} + +LJFOLD(CONV KINT IRCONV_INT_I8) +LJFOLD(CONV KINT IRCONV_INT_U8) +LJFOLD(CONV KINT IRCONV_INT_I16) +LJFOLD(CONV KINT IRCONV_INT_U16) +LJFOLDF(kfold_conv_kint_ext) +{ + int32_t k = fleft->i; + if ((fins->op2 & IRCONV_SRCMASK) == IRT_I8) k = (int8_t)k; + else if ((fins->op2 & IRCONV_SRCMASK) == IRT_U8) k = (uint8_t)k; + else if ((fins->op2 & IRCONV_SRCMASK) == IRT_I16) k = (int16_t)k; + else k = (uint16_t)k; + return INTFOLD(k); +} + +LJFOLD(CONV KINT IRCONV_I64_INT) +LJFOLD(CONV KINT IRCONV_U64_INT) +LJFOLD(CONV KINT IRCONV_I64_U32) +LJFOLD(CONV KINT IRCONV_U64_U32) +LJFOLDF(kfold_conv_kint_i64) +{ + if ((fins->op2 & IRCONV_SEXT)) + return INT64FOLD((uint64_t)(int64_t)fleft->i); + else + return INT64FOLD((uint64_t)(int64_t)(uint32_t)fleft->i); +} + +LJFOLD(CONV KINT64 IRCONV_NUM_I64) +LJFOLDF(kfold_conv_kint64_num_i64) +{ + return lj_ir_knum(J, (lua_Number)(int64_t)ir_kint64(fleft)->u64); +} + +LJFOLD(CONV KINT64 IRCONV_NUM_U64) +LJFOLDF(kfold_conv_kint64_num_u64) +{ + return lj_ir_knum(J, (lua_Number)ir_kint64(fleft)->u64); +} + +LJFOLD(CONV KINT64 IRCONV_INT_I64) +LJFOLD(CONV KINT64 IRCONV_U32_I64) +LJFOLDF(kfold_conv_kint64_int_i64) +{ + return INTFOLD((int32_t)ir_kint64(fleft)->u64); +} + +LJFOLD(CONV KNUM IRCONV_INT_NUM) +LJFOLDF(kfold_conv_knum_int_num) +{ + lua_Number n = knumleft; + if (!(fins->op2 & IRCONV_TRUNC)) { + int32_t k = lj_num2int(n); + if (irt_isguard(fins->t) && n != (lua_Number)k) { + /* We're about to create a guard which always fails, like CONV +1.5. + ** Some pathological loops cause this during LICM, e.g.: + ** local x,k,t = 0,1.5,{1,[1.5]=2} + ** for i=1,200 do x = x+ t[k]; k = k == 1 and 1.5 or 1 end + ** assert(x == 300) + */ + return FAILFOLD; + } + return INTFOLD(k); + } else { + return INTFOLD((int32_t)n); + } +} + +LJFOLD(CONV KNUM IRCONV_U32_NUM) +LJFOLDF(kfold_conv_knum_u32_num) +{ + lua_assert((fins->op2 & IRCONV_TRUNC)); +#ifdef _MSC_VER + { /* Workaround for MSVC bug. */ + volatile uint32_t u = (uint32_t)knumleft; + return INTFOLD((int32_t)u); + } +#else + return INTFOLD((int32_t)(uint32_t)knumleft); +#endif +} + +LJFOLD(CONV KNUM IRCONV_I64_NUM) +LJFOLDF(kfold_conv_knum_i64_num) +{ + lua_assert((fins->op2 & IRCONV_TRUNC)); + return INT64FOLD((uint64_t)(int64_t)knumleft); +} + +LJFOLD(CONV KNUM IRCONV_U64_NUM) +LJFOLDF(kfold_conv_knum_u64_num) +{ + lua_assert((fins->op2 & IRCONV_TRUNC)); + return INT64FOLD(lj_num2u64(knumleft)); +} + +LJFOLD(TOSTR KNUM) +LJFOLDF(kfold_tostr_knum) +{ + return lj_ir_kstr(J, lj_str_fromnum(J->L, &knumleft)); +} + +LJFOLD(TOSTR KINT) +LJFOLDF(kfold_tostr_kint) +{ + return lj_ir_kstr(J, lj_str_fromint(J->L, fleft->i)); +} + +LJFOLD(STRTO KGC) +LJFOLDF(kfold_strto) +{ + TValue n; + if (lj_strscan_num(ir_kstr(fleft), &n)) + return lj_ir_knum(J, numV(&n)); + return FAILFOLD; +} + +/* -- Constant folding of equality checks --------------------------------- */ + +/* Don't constant-fold away FLOAD checks against KNULL. */ +LJFOLD(EQ FLOAD KNULL) +LJFOLD(NE FLOAD KNULL) +LJFOLDX(lj_opt_cse) + +/* But fold all other KNULL compares, since only KNULL is equal to KNULL. */ +LJFOLD(EQ any KNULL) +LJFOLD(NE any KNULL) +LJFOLD(EQ KNULL any) +LJFOLD(NE KNULL any) +LJFOLD(EQ KINT KINT) /* Constants are unique, so same refs <==> same value. */ +LJFOLD(NE KINT KINT) +LJFOLD(EQ KINT64 KINT64) +LJFOLD(NE KINT64 KINT64) +LJFOLD(EQ KGC KGC) +LJFOLD(NE KGC KGC) +LJFOLDF(kfold_kref) +{ + return CONDFOLD((fins->op1 == fins->op2) ^ (fins->o == IR_NE)); +} + +/* -- Algebraic shortcuts ------------------------------------------------- */ + +LJFOLD(FPMATH FPMATH IRFPM_FLOOR) +LJFOLD(FPMATH FPMATH IRFPM_CEIL) +LJFOLD(FPMATH FPMATH IRFPM_TRUNC) +LJFOLDF(shortcut_round) +{ + IRFPMathOp op = (IRFPMathOp)fleft->op2; + if (op == IRFPM_FLOOR || op == IRFPM_CEIL || op == IRFPM_TRUNC) + return LEFTFOLD; /* round(round_left(x)) = round_left(x) */ + return NEXTFOLD; +} + +LJFOLD(ABS ABS KNUM) +LJFOLDF(shortcut_left) +{ + return LEFTFOLD; /* f(g(x)) ==> g(x) */ +} + +LJFOLD(ABS NEG KNUM) +LJFOLDF(shortcut_dropleft) +{ + PHIBARRIER(fleft); + fins->op1 = fleft->op1; /* abs(neg(x)) ==> abs(x) */ + return RETRYFOLD; +} + +/* Note: no safe shortcuts with STRTO and TOSTR ("1e2" ==> +100 ==> "100"). */ +LJFOLD(NEG NEG any) +LJFOLD(BNOT BNOT) +LJFOLD(BSWAP BSWAP) +LJFOLDF(shortcut_leftleft) +{ + PHIBARRIER(fleft); /* See above. Fold would be ok, but not beneficial. */ + return fleft->op1; /* f(g(x)) ==> x */ +} + +/* -- FP algebraic simplifications ---------------------------------------- */ + +/* FP arithmetic is tricky -- there's not much to simplify. +** Please note the following common pitfalls before sending "improvements": +** x+0 ==> x is INVALID for x=-0 +** 0-x ==> -x is INVALID for x=+0 +** x*0 ==> 0 is INVALID for x=-0, x=+-Inf or x=NaN +*/ + +LJFOLD(ADD NEG any) +LJFOLDF(simplify_numadd_negx) +{ + PHIBARRIER(fleft); + fins->o = IR_SUB; /* (-a) + b ==> b - a */ + fins->op1 = fins->op2; + fins->op2 = fleft->op1; + return RETRYFOLD; +} + +LJFOLD(ADD any NEG) +LJFOLDF(simplify_numadd_xneg) +{ + PHIBARRIER(fright); + fins->o = IR_SUB; /* a + (-b) ==> a - b */ + fins->op2 = fright->op1; + return RETRYFOLD; +} + +LJFOLD(SUB any KNUM) +LJFOLDF(simplify_numsub_k) +{ + lua_Number n = knumright; + if (n == 0.0) /* x - (+-0) ==> x */ + return LEFTFOLD; + return NEXTFOLD; +} + +LJFOLD(SUB NEG KNUM) +LJFOLDF(simplify_numsub_negk) +{ + PHIBARRIER(fleft); + fins->op2 = fleft->op1; /* (-x) - k ==> (-k) - x */ + fins->op1 = (IRRef1)lj_ir_knum(J, -knumright); + return RETRYFOLD; +} + +LJFOLD(SUB any NEG) +LJFOLDF(simplify_numsub_xneg) +{ + PHIBARRIER(fright); + fins->o = IR_ADD; /* a - (-b) ==> a + b */ + fins->op2 = fright->op1; + return RETRYFOLD; +} + +LJFOLD(MUL any KNUM) +LJFOLD(DIV any KNUM) +LJFOLDF(simplify_nummuldiv_k) +{ + lua_Number n = knumright; + if (n == 1.0) { /* x o 1 ==> x */ + return LEFTFOLD; + } else if (n == -1.0) { /* x o -1 ==> -x */ + fins->o = IR_NEG; + fins->op2 = (IRRef1)lj_ir_knum_neg(J); + return RETRYFOLD; + } else if (fins->o == IR_MUL && n == 2.0) { /* x * 2 ==> x + x */ + fins->o = IR_ADD; + fins->op2 = fins->op1; + return RETRYFOLD; + } else if (fins->o == IR_DIV) { /* x / 2^k ==> x * 2^-k */ + uint64_t u = ir_knum(fright)->u64; + uint32_t ex = ((uint32_t)(u >> 52) & 0x7ff); + if ((u & U64x(000fffff,ffffffff)) == 0 && ex - 1 < 0x7fd) { + u = (u & ((uint64_t)1 << 63)) | ((uint64_t)(0x7fe - ex) << 52); + fins->o = IR_MUL; /* Multiply by exact reciprocal. */ + fins->op2 = lj_ir_knum_u64(J, u); + return RETRYFOLD; + } + } + return NEXTFOLD; +} + +LJFOLD(MUL NEG KNUM) +LJFOLD(DIV NEG KNUM) +LJFOLDF(simplify_nummuldiv_negk) +{ + PHIBARRIER(fleft); + fins->op1 = fleft->op1; /* (-a) o k ==> a o (-k) */ + fins->op2 = (IRRef1)lj_ir_knum(J, -knumright); + return RETRYFOLD; +} + +LJFOLD(MUL NEG NEG) +LJFOLD(DIV NEG NEG) +LJFOLDF(simplify_nummuldiv_negneg) +{ + PHIBARRIER(fleft); + PHIBARRIER(fright); + fins->op1 = fleft->op1; /* (-a) o (-b) ==> a o b */ + fins->op2 = fright->op1; + return RETRYFOLD; +} + +LJFOLD(POW any KINT) +LJFOLDF(simplify_numpow_xk) +{ + int32_t k = fright->i; + TRef ref = fins->op1; + if (k == 0) /* x ^ 0 ==> 1 */ + return lj_ir_knum_one(J); /* Result must be a number, not an int. */ + if (k == 1) /* x ^ 1 ==> x */ + return LEFTFOLD; + if ((uint32_t)(k+65536) > 2*65536u) /* Limit code explosion. */ + return NEXTFOLD; + if (k < 0) { /* x ^ (-k) ==> (1/x) ^ k. */ + ref = emitir(IRTN(IR_DIV), lj_ir_knum_one(J), ref); + k = -k; + } + /* Unroll x^k for 1 <= k <= 65536. */ + for (; (k & 1) == 0; k >>= 1) /* Handle leading zeros. */ + ref = emitir(IRTN(IR_MUL), ref, ref); + if ((k >>= 1) != 0) { /* Handle trailing bits. */ + TRef tmp = emitir(IRTN(IR_MUL), ref, ref); + for (; k != 1; k >>= 1) { + if (k & 1) + ref = emitir(IRTN(IR_MUL), ref, tmp); + tmp = emitir(IRTN(IR_MUL), tmp, tmp); + } + ref = emitir(IRTN(IR_MUL), ref, tmp); + } + return ref; +} + +LJFOLD(POW KNUM any) +LJFOLDF(simplify_numpow_kx) +{ + lua_Number n = knumleft; + if (n == 2.0) { /* 2.0 ^ i ==> ldexp(1.0, tonum(i)) */ + fins->o = IR_CONV; +#if LJ_TARGET_X86ORX64 + fins->op1 = fins->op2; + fins->op2 = IRCONV_NUM_INT; + fins->op2 = (IRRef1)lj_opt_fold(J); +#endif + fins->op1 = (IRRef1)lj_ir_knum_one(J); + fins->o = IR_LDEXP; + return RETRYFOLD; + } + return NEXTFOLD; +} + +/* -- Simplify conversions ------------------------------------------------ */ + +LJFOLD(CONV CONV IRCONV_NUM_INT) /* _NUM */ +LJFOLDF(shortcut_conv_num_int) +{ + PHIBARRIER(fleft); + /* Only safe with a guarded conversion to int. */ + if ((fleft->op2 & IRCONV_SRCMASK) == IRT_NUM && irt_isguard(fleft->t)) + return fleft->op1; /* f(g(x)) ==> x */ + return NEXTFOLD; +} + +LJFOLD(CONV CONV IRCONV_INT_NUM) /* _INT */ +LJFOLD(CONV CONV IRCONV_U32_NUM) /* _U32*/ +LJFOLDF(simplify_conv_int_num) +{ + /* Fold even across PHI to avoid expensive num->int conversions in loop. */ + if ((fleft->op2 & IRCONV_SRCMASK) == + ((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH)) + return fleft->op1; + return NEXTFOLD; +} + +LJFOLD(CONV CONV IRCONV_I64_NUM) /* _INT or _U32 */ +LJFOLD(CONV CONV IRCONV_U64_NUM) /* _INT or _U32 */ +LJFOLDF(simplify_conv_i64_num) +{ + PHIBARRIER(fleft); + if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT) { + /* Reduce to a sign-extension. */ + fins->op1 = fleft->op1; + fins->op2 = ((IRT_I64<<5)|IRT_INT|IRCONV_SEXT); + return RETRYFOLD; + } else if ((fleft->op2 & IRCONV_SRCMASK) == IRT_U32) { +#if LJ_TARGET_X64 + return fleft->op1; +#else + /* Reduce to a zero-extension. */ + fins->op1 = fleft->op1; + fins->op2 = (IRT_I64<<5)|IRT_U32; + return RETRYFOLD; +#endif + } + return NEXTFOLD; +} + +LJFOLD(CONV CONV IRCONV_INT_I64) /* _INT or _U32 */ +LJFOLD(CONV CONV IRCONV_INT_U64) /* _INT or _U32 */ +LJFOLD(CONV CONV IRCONV_U32_I64) /* _INT or _U32 */ +LJFOLD(CONV CONV IRCONV_U32_U64) /* _INT or _U32 */ +LJFOLDF(simplify_conv_int_i64) +{ + int src; + PHIBARRIER(fleft); + src = (fleft->op2 & IRCONV_SRCMASK); + if (src == IRT_INT || src == IRT_U32) { + if (src == ((fins->op2 & IRCONV_DSTMASK) >> IRCONV_DSH)) { + return fleft->op1; + } else { + fins->op2 = ((fins->op2 & IRCONV_DSTMASK) | src); + fins->op1 = fleft->op1; + return RETRYFOLD; + } + } + return NEXTFOLD; +} + +LJFOLD(CONV CONV IRCONV_FLOAT_NUM) /* _FLOAT */ +LJFOLDF(simplify_conv_flt_num) +{ + PHIBARRIER(fleft); + if ((fleft->op2 & IRCONV_SRCMASK) == IRT_FLOAT) + return fleft->op1; + return NEXTFOLD; +} + +/* Shortcut TOBIT + IRT_NUM <- IRT_INT/IRT_U32 conversion. */ +LJFOLD(TOBIT CONV KNUM) +LJFOLDF(simplify_tobit_conv) +{ + if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT || + (fleft->op2 & IRCONV_SRCMASK) == IRT_U32) { + /* Fold even across PHI to avoid expensive num->int conversions in loop. */ + lua_assert(irt_isnum(fleft->t)); + return fleft->op1; + } + return NEXTFOLD; +} + +/* Shortcut floor/ceil/round + IRT_NUM <- IRT_INT/IRT_U32 conversion. */ +LJFOLD(FPMATH CONV IRFPM_FLOOR) +LJFOLD(FPMATH CONV IRFPM_CEIL) +LJFOLD(FPMATH CONV IRFPM_TRUNC) +LJFOLDF(simplify_floor_conv) +{ + if ((fleft->op2 & IRCONV_SRCMASK) == IRT_INT || + (fleft->op2 & IRCONV_SRCMASK) == IRT_U32) + return LEFTFOLD; + return NEXTFOLD; +} + +/* Strength reduction of widening. */ +LJFOLD(CONV any IRCONV_I64_INT) +LJFOLD(CONV any IRCONV_U64_INT) +LJFOLDF(simplify_conv_sext) +{ + IRRef ref = fins->op1; + int64_t ofs = 0; + if (!(fins->op2 & IRCONV_SEXT)) + return NEXTFOLD; + PHIBARRIER(fleft); + if (fleft->o == IR_XLOAD && (irt_isu8(fleft->t) || irt_isu16(fleft->t))) + goto ok_reduce; + if (fleft->o == IR_ADD && irref_isk(fleft->op2)) { + ofs = (int64_t)IR(fleft->op2)->i; + ref = fleft->op1; + } + /* Use scalar evolution analysis results to strength-reduce sign-extension. */ + if (ref == J->scev.idx) { + IRRef lo = J->scev.dir ? J->scev.start : J->scev.stop; + lua_assert(irt_isint(J->scev.t)); + if (lo && IR(lo)->i + ofs >= 0) { + ok_reduce: +#if LJ_TARGET_X64 + /* Eliminate widening. All 32 bit ops do an implicit zero-extension. */ + return LEFTFOLD; +#else + /* Reduce to a (cheaper) zero-extension. */ + fins->op2 &= ~IRCONV_SEXT; + return RETRYFOLD; +#endif + } + } + return NEXTFOLD; +} + +/* Strength reduction of narrowing. */ +LJFOLD(CONV ADD IRCONV_INT_I64) +LJFOLD(CONV SUB IRCONV_INT_I64) +LJFOLD(CONV MUL IRCONV_INT_I64) +LJFOLD(CONV ADD IRCONV_INT_U64) +LJFOLD(CONV SUB IRCONV_INT_U64) +LJFOLD(CONV MUL IRCONV_INT_U64) +LJFOLD(CONV ADD IRCONV_U32_I64) +LJFOLD(CONV SUB IRCONV_U32_I64) +LJFOLD(CONV MUL IRCONV_U32_I64) +LJFOLD(CONV ADD IRCONV_U32_U64) +LJFOLD(CONV SUB IRCONV_U32_U64) +LJFOLD(CONV MUL IRCONV_U32_U64) +LJFOLDF(simplify_conv_narrow) +{ + IROp op = (IROp)fleft->o; + IRType t = irt_type(fins->t); + IRRef op1 = fleft->op1, op2 = fleft->op2, mode = fins->op2; + PHIBARRIER(fleft); + op1 = emitir(IRTI(IR_CONV), op1, mode); + op2 = emitir(IRTI(IR_CONV), op2, mode); + fins->ot = IRT(op, t); + fins->op1 = op1; + fins->op2 = op2; + return RETRYFOLD; +} + +/* Special CSE rule for CONV. */ +LJFOLD(CONV any any) +LJFOLDF(cse_conv) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { + IRRef op1 = fins->op1, op2 = (fins->op2 & IRCONV_MODEMASK); + uint8_t guard = irt_isguard(fins->t); + IRRef ref = J->chain[IR_CONV]; + while (ref > op1) { + IRIns *ir = IR(ref); + /* Commoning with stronger checks is ok. */ + if (ir->op1 == op1 && (ir->op2 & IRCONV_MODEMASK) == op2 && + irt_isguard(ir->t) >= guard) + return ref; + ref = ir->prev; + } + } + return EMITFOLD; /* No fallthrough to regular CSE. */ +} + +/* FP conversion narrowing. */ +LJFOLD(TOBIT ADD KNUM) +LJFOLD(TOBIT SUB KNUM) +LJFOLD(CONV ADD IRCONV_INT_NUM) +LJFOLD(CONV SUB IRCONV_INT_NUM) +LJFOLD(CONV ADD IRCONV_I64_NUM) +LJFOLD(CONV SUB IRCONV_I64_NUM) +LJFOLDF(narrow_convert) +{ + PHIBARRIER(fleft); + /* Narrowing ignores PHIs and repeating it inside the loop is not useful. */ + if (J->chain[IR_LOOP]) + return NEXTFOLD; + lua_assert(fins->o != IR_CONV || (fins->op2&IRCONV_CONVMASK) != IRCONV_TOBIT); + return lj_opt_narrow_convert(J); +} + +/* -- Integer algebraic simplifications ----------------------------------- */ + +LJFOLD(ADD any KINT) +LJFOLD(ADDOV any KINT) +LJFOLD(SUBOV any KINT) +LJFOLDF(simplify_intadd_k) +{ + if (fright->i == 0) /* i o 0 ==> i */ + return LEFTFOLD; + return NEXTFOLD; +} + +LJFOLD(MULOV any KINT) +LJFOLDF(simplify_intmul_k) +{ + if (fright->i == 0) /* i * 0 ==> 0 */ + return RIGHTFOLD; + if (fright->i == 1) /* i * 1 ==> i */ + return LEFTFOLD; + if (fright->i == 2) { /* i * 2 ==> i + i */ + fins->o = IR_ADDOV; + fins->op2 = fins->op1; + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(SUB any KINT) +LJFOLDF(simplify_intsub_k) +{ + if (fright->i == 0) /* i - 0 ==> i */ + return LEFTFOLD; + fins->o = IR_ADD; /* i - k ==> i + (-k) */ + fins->op2 = (IRRef1)lj_ir_kint(J, -fright->i); /* Overflow for -2^31 ok. */ + return RETRYFOLD; +} + +LJFOLD(SUB KINT any) +LJFOLD(SUB KINT64 any) +LJFOLDF(simplify_intsub_kleft) +{ + if (fleft->o == IR_KINT ? (fleft->i == 0) : (ir_kint64(fleft)->u64 == 0)) { + fins->o = IR_NEG; /* 0 - i ==> -i */ + fins->op1 = fins->op2; + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(ADD any KINT64) +LJFOLDF(simplify_intadd_k64) +{ + if (ir_kint64(fright)->u64 == 0) /* i + 0 ==> i */ + return LEFTFOLD; + return NEXTFOLD; +} + +LJFOLD(SUB any KINT64) +LJFOLDF(simplify_intsub_k64) +{ + uint64_t k = ir_kint64(fright)->u64; + if (k == 0) /* i - 0 ==> i */ + return LEFTFOLD; + fins->o = IR_ADD; /* i - k ==> i + (-k) */ + fins->op2 = (IRRef1)lj_ir_kint64(J, (uint64_t)-(int64_t)k); + return RETRYFOLD; +} + +static TRef simplify_intmul_k(jit_State *J, int32_t k) +{ + /* Note: many more simplifications are possible, e.g. 2^k1 +- 2^k2. + ** But this is mainly intended for simple address arithmetic. + ** Also it's easier for the backend to optimize the original multiplies. + */ + if (k == 1) { /* i * 1 ==> i */ + return LEFTFOLD; + } else if ((k & (k-1)) == 0) { /* i * 2^k ==> i << k */ + fins->o = IR_BSHL; + fins->op2 = lj_ir_kint(J, lj_fls((uint32_t)k)); + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(MUL any KINT) +LJFOLDF(simplify_intmul_k32) +{ + if (fright->i == 0) /* i * 0 ==> 0 */ + return INTFOLD(0); + else if (fright->i > 0) + return simplify_intmul_k(J, fright->i); + return NEXTFOLD; +} + +LJFOLD(MUL any KINT64) +LJFOLDF(simplify_intmul_k64) +{ + if (ir_kint64(fright)->u64 == 0) /* i * 0 ==> 0 */ + return INT64FOLD(0); +#if LJ_64 + /* NYI: SPLIT for BSHL and 32 bit backend support. */ + else if (ir_kint64(fright)->u64 < 0x80000000u) + return simplify_intmul_k(J, (int32_t)ir_kint64(fright)->u64); +#endif + return NEXTFOLD; +} + +LJFOLD(MOD any KINT) +LJFOLDF(simplify_intmod_k) +{ + int32_t k = fright->i; + lua_assert(k != 0); + if (k > 0 && (k & (k-1)) == 0) { /* i % (2^k) ==> i & (2^k-1) */ + fins->o = IR_BAND; + fins->op2 = lj_ir_kint(J, k-1); + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(MOD KINT any) +LJFOLDF(simplify_intmod_kleft) +{ + if (fleft->i == 0) + return INTFOLD(0); + return NEXTFOLD; +} + +LJFOLD(SUB any any) +LJFOLD(SUBOV any any) +LJFOLDF(simplify_intsub) +{ + if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) /* i - i ==> 0 */ + return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0); + return NEXTFOLD; +} + +LJFOLD(SUB ADD any) +LJFOLDF(simplify_intsubadd_leftcancel) +{ + if (!irt_isnum(fins->t)) { + PHIBARRIER(fleft); + if (fins->op2 == fleft->op1) /* (i + j) - i ==> j */ + return fleft->op2; + if (fins->op2 == fleft->op2) /* (i + j) - j ==> i */ + return fleft->op1; + } + return NEXTFOLD; +} + +LJFOLD(SUB SUB any) +LJFOLDF(simplify_intsubsub_leftcancel) +{ + if (!irt_isnum(fins->t)) { + PHIBARRIER(fleft); + if (fins->op2 == fleft->op1) { /* (i - j) - i ==> 0 - j */ + fins->op1 = (IRRef1)lj_ir_kint(J, 0); + fins->op2 = fleft->op2; + return RETRYFOLD; + } + } + return NEXTFOLD; +} + +LJFOLD(SUB any SUB) +LJFOLDF(simplify_intsubsub_rightcancel) +{ + if (!irt_isnum(fins->t)) { + PHIBARRIER(fright); + if (fins->op1 == fright->op1) /* i - (i - j) ==> j */ + return fright->op2; + } + return NEXTFOLD; +} + +LJFOLD(SUB any ADD) +LJFOLDF(simplify_intsubadd_rightcancel) +{ + if (!irt_isnum(fins->t)) { + PHIBARRIER(fright); + if (fins->op1 == fright->op1) { /* i - (i + j) ==> 0 - j */ + fins->op2 = fright->op2; + fins->op1 = (IRRef1)lj_ir_kint(J, 0); + return RETRYFOLD; + } + if (fins->op1 == fright->op2) { /* i - (j + i) ==> 0 - j */ + fins->op2 = fright->op1; + fins->op1 = (IRRef1)lj_ir_kint(J, 0); + return RETRYFOLD; + } + } + return NEXTFOLD; +} + +LJFOLD(SUB ADD ADD) +LJFOLDF(simplify_intsubaddadd_cancel) +{ + if (!irt_isnum(fins->t)) { + PHIBARRIER(fleft); + PHIBARRIER(fright); + if (fleft->op1 == fright->op1) { /* (i + j1) - (i + j2) ==> j1 - j2 */ + fins->op1 = fleft->op2; + fins->op2 = fright->op2; + return RETRYFOLD; + } + if (fleft->op1 == fright->op2) { /* (i + j1) - (j2 + i) ==> j1 - j2 */ + fins->op1 = fleft->op2; + fins->op2 = fright->op1; + return RETRYFOLD; + } + if (fleft->op2 == fright->op1) { /* (j1 + i) - (i + j2) ==> j1 - j2 */ + fins->op1 = fleft->op1; + fins->op2 = fright->op2; + return RETRYFOLD; + } + if (fleft->op2 == fright->op2) { /* (j1 + i) - (j2 + i) ==> j1 - j2 */ + fins->op1 = fleft->op1; + fins->op2 = fright->op1; + return RETRYFOLD; + } + } + return NEXTFOLD; +} + +LJFOLD(BAND any KINT) +LJFOLD(BAND any KINT64) +LJFOLDF(simplify_band_k) +{ + int64_t k = fright->o == IR_KINT ? (int64_t)fright->i : + (int64_t)ir_k64(fright)->u64; + if (k == 0) /* i & 0 ==> 0 */ + return RIGHTFOLD; + if (k == -1) /* i & -1 ==> i */ + return LEFTFOLD; + return NEXTFOLD; +} + +LJFOLD(BOR any KINT) +LJFOLD(BOR any KINT64) +LJFOLDF(simplify_bor_k) +{ + int64_t k = fright->o == IR_KINT ? (int64_t)fright->i : + (int64_t)ir_k64(fright)->u64; + if (k == 0) /* i | 0 ==> i */ + return LEFTFOLD; + if (k == -1) /* i | -1 ==> -1 */ + return RIGHTFOLD; + return NEXTFOLD; +} + +LJFOLD(BXOR any KINT) +LJFOLD(BXOR any KINT64) +LJFOLDF(simplify_bxor_k) +{ + int64_t k = fright->o == IR_KINT ? (int64_t)fright->i : + (int64_t)ir_k64(fright)->u64; + if (k == 0) /* i xor 0 ==> i */ + return LEFTFOLD; + if (k == -1) { /* i xor -1 ==> ~i */ + fins->o = IR_BNOT; + fins->op2 = 0; + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(BSHL any KINT) +LJFOLD(BSHR any KINT) +LJFOLD(BSAR any KINT) +LJFOLD(BROL any KINT) +LJFOLD(BROR any KINT) +LJFOLDF(simplify_shift_ik) +{ + int32_t mask = irt_is64(fins->t) ? 63 : 31; + int32_t k = (fright->i & mask); + if (k == 0) /* i o 0 ==> i */ + return LEFTFOLD; + if (k == 1 && fins->o == IR_BSHL) { /* i << 1 ==> i + i */ + fins->o = IR_ADD; + fins->op2 = fins->op1; + return RETRYFOLD; + } + if (k != fright->i) { /* i o k ==> i o (k & mask) */ + fins->op2 = (IRRef1)lj_ir_kint(J, k); + return RETRYFOLD; + } +#ifndef LJ_TARGET_UNIFYROT + if (fins->o == IR_BROR) { /* bror(i, k) ==> brol(i, (-k)&mask) */ + fins->o = IR_BROL; + fins->op2 = (IRRef1)lj_ir_kint(J, (-k)&mask); + return RETRYFOLD; + } +#endif + return NEXTFOLD; +} + +LJFOLD(BSHL any BAND) +LJFOLD(BSHR any BAND) +LJFOLD(BSAR any BAND) +LJFOLD(BROL any BAND) +LJFOLD(BROR any BAND) +LJFOLDF(simplify_shift_andk) +{ + IRIns *irk = IR(fright->op2); + PHIBARRIER(fright); + if ((fins->o < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) && + irk->o == IR_KINT) { /* i o (j & mask) ==> i o j */ + int32_t mask = irt_is64(fins->t) ? 63 : 31; + int32_t k = irk->i & mask; + if (k == mask) { + fins->op2 = fright->op1; + return RETRYFOLD; + } + } + return NEXTFOLD; +} + +LJFOLD(BSHL KINT any) +LJFOLD(BSHR KINT any) +LJFOLD(BSHL KINT64 any) +LJFOLD(BSHR KINT64 any) +LJFOLDF(simplify_shift1_ki) +{ + int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i : + (int64_t)ir_k64(fleft)->u64; + if (k == 0) /* 0 o i ==> 0 */ + return LEFTFOLD; + return NEXTFOLD; +} + +LJFOLD(BSAR KINT any) +LJFOLD(BROL KINT any) +LJFOLD(BROR KINT any) +LJFOLD(BSAR KINT64 any) +LJFOLD(BROL KINT64 any) +LJFOLD(BROR KINT64 any) +LJFOLDF(simplify_shift2_ki) +{ + int64_t k = fleft->o == IR_KINT ? (int64_t)fleft->i : + (int64_t)ir_k64(fleft)->u64; + if (k == 0 || k == -1) /* 0 o i ==> 0; -1 o i ==> -1 */ + return LEFTFOLD; + return NEXTFOLD; +} + +LJFOLD(BSHL BAND KINT) +LJFOLD(BSHR BAND KINT) +LJFOLD(BROL BAND KINT) +LJFOLD(BROR BAND KINT) +LJFOLDF(simplify_shiftk_andk) +{ + IRIns *irk = IR(fleft->op2); + PHIBARRIER(fleft); + if (irk->o == IR_KINT) { /* (i & k1) o k2 ==> (i o k2) & (k1 o k2) */ + int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o); + fins->op1 = fleft->op1; + fins->op1 = (IRRef1)lj_opt_fold(J); + fins->op2 = (IRRef1)lj_ir_kint(J, k); + fins->ot = IRTI(IR_BAND); + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(BAND BSHL KINT) +LJFOLD(BAND BSHR KINT) +LJFOLDF(simplify_andk_shiftk) +{ + IRIns *irk = IR(fleft->op2); + if (irk->o == IR_KINT && + kfold_intop(-1, irk->i, (IROp)fleft->o) == fright->i) + return LEFTFOLD; /* (i o k1) & k2 ==> i, if (-1 o k1) == k2 */ + return NEXTFOLD; +} + +/* -- Reassociation ------------------------------------------------------- */ + +LJFOLD(ADD ADD KINT) +LJFOLD(MUL MUL KINT) +LJFOLD(BAND BAND KINT) +LJFOLD(BOR BOR KINT) +LJFOLD(BXOR BXOR KINT) +LJFOLDF(reassoc_intarith_k) +{ + IRIns *irk = IR(fleft->op2); + if (irk->o == IR_KINT) { + int32_t k = kfold_intop(irk->i, fright->i, (IROp)fins->o); + if (k == irk->i) /* (i o k1) o k2 ==> i o k1, if (k1 o k2) == k1. */ + return LEFTFOLD; + PHIBARRIER(fleft); + fins->op1 = fleft->op1; + fins->op2 = (IRRef1)lj_ir_kint(J, k); + return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */ + } + return NEXTFOLD; +} + +LJFOLD(ADD ADD KINT64) +LJFOLD(MUL MUL KINT64) +LJFOLD(BAND BAND KINT64) +LJFOLD(BOR BOR KINT64) +LJFOLD(BXOR BXOR KINT64) +LJFOLDF(reassoc_intarith_k64) +{ +#if LJ_HASFFI || LJ_64 + IRIns *irk = IR(fleft->op2); + if (irk->o == IR_KINT64) { + uint64_t k = kfold_int64arith(ir_k64(irk)->u64, + ir_k64(fright)->u64, (IROp)fins->o); + PHIBARRIER(fleft); + fins->op1 = fleft->op1; + fins->op2 = (IRRef1)lj_ir_kint64(J, k); + return RETRYFOLD; /* (i o k1) o k2 ==> i o (k1 o k2) */ + } + return NEXTFOLD; +#else + UNUSED(J); lua_assert(0); return FAILFOLD; +#endif +} + +LJFOLD(MIN MIN any) +LJFOLD(MAX MAX any) +LJFOLD(BAND BAND any) +LJFOLD(BOR BOR any) +LJFOLDF(reassoc_dup) +{ + if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2) + return LEFTFOLD; /* (a o b) o a ==> a o b; (a o b) o b ==> a o b */ + return NEXTFOLD; +} + +LJFOLD(BXOR BXOR any) +LJFOLDF(reassoc_bxor) +{ + PHIBARRIER(fleft); + if (fins->op2 == fleft->op1) /* (a xor b) xor a ==> b */ + return fleft->op2; + if (fins->op2 == fleft->op2) /* (a xor b) xor b ==> a */ + return fleft->op1; + return NEXTFOLD; +} + +LJFOLD(BSHL BSHL KINT) +LJFOLD(BSHR BSHR KINT) +LJFOLD(BSAR BSAR KINT) +LJFOLD(BROL BROL KINT) +LJFOLD(BROR BROR KINT) +LJFOLDF(reassoc_shift) +{ + IRIns *irk = IR(fleft->op2); + PHIBARRIER(fleft); /* The (shift any KINT) rule covers k2 == 0 and more. */ + if (irk->o == IR_KINT) { /* (i o k1) o k2 ==> i o (k1 + k2) */ + int32_t mask = irt_is64(fins->t) ? 63 : 31; + int32_t k = (irk->i & mask) + (fright->i & mask); + if (k > mask) { /* Combined shift too wide? */ + if (fins->o == IR_BSHL || fins->o == IR_BSHR) + return mask == 31 ? INTFOLD(0) : INT64FOLD(0); + else if (fins->o == IR_BSAR) + k = mask; + else + k &= mask; + } + fins->op1 = fleft->op1; + fins->op2 = (IRRef1)lj_ir_kint(J, k); + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(MIN MIN KNUM) +LJFOLD(MAX MAX KNUM) +LJFOLD(MIN MIN KINT) +LJFOLD(MAX MAX KINT) +LJFOLDF(reassoc_minmax_k) +{ + IRIns *irk = IR(fleft->op2); + if (irk->o == IR_KNUM) { + lua_Number a = ir_knum(irk)->n; + lua_Number y = lj_vm_foldarith(a, knumright, fins->o - IR_ADD); + if (a == y) /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */ + return LEFTFOLD; + PHIBARRIER(fleft); + fins->op1 = fleft->op1; + fins->op2 = (IRRef1)lj_ir_knum(J, y); + return RETRYFOLD; /* (x o k1) o k2 ==> x o (k1 o k2) */ + } else if (irk->o == IR_KINT) { + int32_t a = irk->i; + int32_t y = kfold_intop(a, fright->i, fins->o); + if (a == y) /* (x o k1) o k2 ==> x o k1, if (k1 o k2) == k1. */ + return LEFTFOLD; + PHIBARRIER(fleft); + fins->op1 = fleft->op1; + fins->op2 = (IRRef1)lj_ir_kint(J, y); + return RETRYFOLD; /* (x o k1) o k2 ==> x o (k1 o k2) */ + } + return NEXTFOLD; +} + +LJFOLD(MIN MAX any) +LJFOLD(MAX MIN any) +LJFOLDF(reassoc_minmax_left) +{ + if (fins->op2 == fleft->op1 || fins->op2 == fleft->op2) + return RIGHTFOLD; /* (b o1 a) o2 b ==> b; (a o1 b) o2 b ==> b */ + return NEXTFOLD; +} + +LJFOLD(MIN any MAX) +LJFOLD(MAX any MIN) +LJFOLDF(reassoc_minmax_right) +{ + if (fins->op1 == fright->op1 || fins->op1 == fright->op2) + return LEFTFOLD; /* a o2 (a o1 b) ==> a; a o2 (b o1 a) ==> a */ + return NEXTFOLD; +} + +/* -- Array bounds check elimination -------------------------------------- */ + +/* Eliminate ABC across PHIs to handle t[i-1] forwarding case. +** ABC(asize, (i+k)+(-k)) ==> ABC(asize, i), but only if it already exists. +** Could be generalized to (i+k1)+k2 ==> i+(k1+k2), but needs better disambig. +*/ +LJFOLD(ABC any ADD) +LJFOLDF(abc_fwd) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) { + if (irref_isk(fright->op2)) { + IRIns *add2 = IR(fright->op1); + if (add2->o == IR_ADD && irref_isk(add2->op2) && + IR(fright->op2)->i == -IR(add2->op2)->i) { + IRRef ref = J->chain[IR_ABC]; + IRRef lim = add2->op1; + if (fins->op1 > lim) lim = fins->op1; + while (ref > lim) { + IRIns *ir = IR(ref); + if (ir->op1 == fins->op1 && ir->op2 == add2->op1) + return DROPFOLD; + ref = ir->prev; + } + } + } + } + return NEXTFOLD; +} + +/* Eliminate ABC for constants. +** ABC(asize, k1), ABC(asize k2) ==> ABC(asize, max(k1, k2)) +** Drop second ABC if k2 is lower. Otherwise patch first ABC with k2. +*/ +LJFOLD(ABC any KINT) +LJFOLDF(abc_k) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_ABC)) { + IRRef ref = J->chain[IR_ABC]; + IRRef asize = fins->op1; + while (ref > asize) { + IRIns *ir = IR(ref); + if (ir->op1 == asize && irref_isk(ir->op2)) { + int32_t k = IR(ir->op2)->i; + if (fright->i > k) + ir->op2 = fins->op2; + return DROPFOLD; + } + ref = ir->prev; + } + return EMITFOLD; /* Already performed CSE. */ + } + return NEXTFOLD; +} + +/* Eliminate invariant ABC inside loop. */ +LJFOLD(ABC any any) +LJFOLDF(abc_invar) +{ + if (!irt_isint(fins->t) && J->chain[IR_LOOP]) /* Currently marked as PTR. */ + return DROPFOLD; + return NEXTFOLD; +} + +/* -- Commutativity ------------------------------------------------------- */ + +/* The refs of commutative ops are canonicalized. Lower refs go to the right. +** Rationale behind this: +** - It (also) moves constants to the right. +** - It reduces the number of FOLD rules (e.g. (BOR any KINT) suffices). +** - It helps CSE to find more matches. +** - The assembler generates better code with constants at the right. +*/ + +LJFOLD(ADD any any) +LJFOLD(MUL any any) +LJFOLD(ADDOV any any) +LJFOLD(MULOV any any) +LJFOLDF(comm_swap) +{ + if (fins->op1 < fins->op2) { /* Move lower ref to the right. */ + IRRef1 tmp = fins->op1; + fins->op1 = fins->op2; + fins->op2 = tmp; + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(EQ any any) +LJFOLD(NE any any) +LJFOLDF(comm_equal) +{ + /* For non-numbers only: x == x ==> drop; x ~= x ==> fail */ + if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) + return CONDFOLD(fins->o == IR_EQ); + return fold_comm_swap(J); +} + +LJFOLD(LT any any) +LJFOLD(GE any any) +LJFOLD(LE any any) +LJFOLD(GT any any) +LJFOLD(ULT any any) +LJFOLD(UGE any any) +LJFOLD(ULE any any) +LJFOLD(UGT any any) +LJFOLDF(comm_comp) +{ + /* For non-numbers only: x <=> x ==> drop; x <> x ==> fail */ + if (fins->op1 == fins->op2 && !irt_isnum(fins->t)) + return CONDFOLD((fins->o ^ (fins->o >> 1)) & 1); + if (fins->op1 < fins->op2) { /* Move lower ref to the right. */ + IRRef1 tmp = fins->op1; + fins->op1 = fins->op2; + fins->op2 = tmp; + fins->o ^= 3; /* GT <-> LT, GE <-> LE, does not affect U */ + return RETRYFOLD; + } + return NEXTFOLD; +} + +LJFOLD(BAND any any) +LJFOLD(BOR any any) +LJFOLD(MIN any any) +LJFOLD(MAX any any) +LJFOLDF(comm_dup) +{ + if (fins->op1 == fins->op2) /* x o x ==> x */ + return LEFTFOLD; + return fold_comm_swap(J); +} + +LJFOLD(BXOR any any) +LJFOLDF(comm_bxor) +{ + if (fins->op1 == fins->op2) /* i xor i ==> 0 */ + return irt_is64(fins->t) ? INT64FOLD(0) : INTFOLD(0); + return fold_comm_swap(J); +} + +/* -- Simplification of compound expressions ------------------------------ */ + +static TRef kfold_xload(jit_State *J, IRIns *ir, const void *p) +{ + int32_t k; + switch (irt_type(ir->t)) { + case IRT_NUM: return lj_ir_knum_u64(J, *(uint64_t *)p); + case IRT_I8: k = (int32_t)*(int8_t *)p; break; + case IRT_U8: k = (int32_t)*(uint8_t *)p; break; + case IRT_I16: k = (int32_t)(int16_t)lj_getu16(p); break; + case IRT_U16: k = (int32_t)(uint16_t)lj_getu16(p); break; + case IRT_INT: case IRT_U32: k = (int32_t)lj_getu32(p); break; + case IRT_I64: case IRT_U64: return lj_ir_kint64(J, *(uint64_t *)p); + default: return 0; + } + return lj_ir_kint(J, k); +} + +/* Turn: string.sub(str, a, b) == kstr +** into: string.byte(str, a) == string.byte(kstr, 1) etc. +** Note: this creates unaligned XLOADs on x86/x64. +*/ +LJFOLD(EQ SNEW KGC) +LJFOLD(NE SNEW KGC) +LJFOLDF(merge_eqne_snew_kgc) +{ + GCstr *kstr = ir_kstr(fright); + int32_t len = (int32_t)kstr->len; + lua_assert(irt_isstr(fins->t)); + +#if LJ_TARGET_UNALIGNED +#define FOLD_SNEW_MAX_LEN 4 /* Handle string lengths 0, 1, 2, 3, 4. */ +#define FOLD_SNEW_TYPE8 IRT_I8 /* Creates shorter immediates. */ +#else +#define FOLD_SNEW_MAX_LEN 1 /* Handle string lengths 0 or 1. */ +#define FOLD_SNEW_TYPE8 IRT_U8 /* Prefer unsigned loads. */ +#endif + + if (len <= FOLD_SNEW_MAX_LEN) { + IROp op = (IROp)fins->o; + IRRef strref = fleft->op1; + lua_assert(IR(strref)->o == IR_STRREF); + if (op == IR_EQ) { + emitir(IRTGI(IR_EQ), fleft->op2, lj_ir_kint(J, len)); + /* Caveat: fins/fleft/fright is no longer valid after emitir. */ + } else { + /* NE is not expanded since this would need an OR of two conds. */ + if (!irref_isk(fleft->op2)) /* Only handle the constant length case. */ + return NEXTFOLD; + if (IR(fleft->op2)->i != len) + return DROPFOLD; + } + if (len > 0) { + /* A 4 byte load for length 3 is ok -- all strings have an extra NUL. */ + uint16_t ot = (uint16_t)(len == 1 ? IRT(IR_XLOAD, FOLD_SNEW_TYPE8) : + len == 2 ? IRT(IR_XLOAD, IRT_U16) : + IRTI(IR_XLOAD)); + TRef tmp = emitir(ot, strref, + IRXLOAD_READONLY | (len > 1 ? IRXLOAD_UNALIGNED : 0)); + TRef val = kfold_xload(J, IR(tref_ref(tmp)), strdata(kstr)); + if (len == 3) + tmp = emitir(IRTI(IR_BAND), tmp, + lj_ir_kint(J, LJ_ENDIAN_SELECT(0x00ffffff, 0xffffff00))); + fins->op1 = (IRRef1)tmp; + fins->op2 = (IRRef1)val; + fins->ot = (IROpT)IRTGI(op); + return RETRYFOLD; + } else { + return DROPFOLD; + } + } + return NEXTFOLD; +} + +/* -- Loads --------------------------------------------------------------- */ + +/* Loads cannot be folded or passed on to CSE in general. +** Alias analysis is needed to check for forwarding opportunities. +** +** Caveat: *all* loads must be listed here or they end up at CSE! +*/ + +LJFOLD(ALOAD any) +LJFOLDX(lj_opt_fwd_aload) + +/* From HREF fwd (see below). Must eliminate, not supported by fwd/backend. */ +LJFOLD(HLOAD KKPTR) +LJFOLDF(kfold_hload_kkptr) +{ + UNUSED(J); + lua_assert(ir_kptr(fleft) == niltvg(J2G(J))); + return TREF_NIL; +} + +LJFOLD(HLOAD any) +LJFOLDX(lj_opt_fwd_hload) + +LJFOLD(ULOAD any) +LJFOLDX(lj_opt_fwd_uload) + +LJFOLD(CALLL any IRCALL_lj_tab_len) +LJFOLDX(lj_opt_fwd_tab_len) + +/* Upvalue refs are really loads, but there are no corresponding stores. +** So CSE is ok for them, except for UREFO across a GC step (see below). +** If the referenced function is const, its upvalue addresses are const, too. +** This can be used to improve CSE by looking for the same address, +** even if the upvalues originate from a different function. +*/ +LJFOLD(UREFO KGC any) +LJFOLD(UREFC KGC any) +LJFOLDF(cse_uref) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { + IRRef ref = J->chain[fins->o]; + GCfunc *fn = ir_kfunc(fleft); + GCupval *uv = gco2uv(gcref(fn->l.uvptr[(fins->op2 >> 8)])); + while (ref > 0) { + IRIns *ir = IR(ref); + if (irref_isk(ir->op1)) { + GCfunc *fn2 = ir_kfunc(IR(ir->op1)); + if (gco2uv(gcref(fn2->l.uvptr[(ir->op2 >> 8)])) == uv) { + if (fins->o == IR_UREFO && gcstep_barrier(J, ref)) + break; + return ref; + } + } + ref = ir->prev; + } + } + return EMITFOLD; +} + +LJFOLD(HREFK any any) +LJFOLDX(lj_opt_fwd_hrefk) + +LJFOLD(HREF TNEW any) +LJFOLDF(fwd_href_tnew) +{ + if (lj_opt_fwd_href_nokey(J)) + return lj_ir_kkptr(J, niltvg(J2G(J))); + return NEXTFOLD; +} + +LJFOLD(HREF TDUP KPRI) +LJFOLD(HREF TDUP KGC) +LJFOLD(HREF TDUP KNUM) +LJFOLDF(fwd_href_tdup) +{ + TValue keyv; + lj_ir_kvalue(J->L, &keyv, fright); + if (lj_tab_get(J->L, ir_ktab(IR(fleft->op1)), &keyv) == niltvg(J2G(J)) && + lj_opt_fwd_href_nokey(J)) + return lj_ir_kkptr(J, niltvg(J2G(J))); + return NEXTFOLD; +} + +/* We can safely FOLD/CSE array/hash refs and field loads, since there +** are no corresponding stores. But we need to check for any NEWREF with +** an aliased table, as it may invalidate all of the pointers and fields. +** Only HREF needs the NEWREF check -- AREF and HREFK already depend on +** FLOADs. And NEWREF itself is treated like a store (see below). +*/ +LJFOLD(FLOAD TNEW IRFL_TAB_ASIZE) +LJFOLDF(fload_tab_tnew_asize) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) + return INTFOLD(fleft->op1); + return NEXTFOLD; +} + +LJFOLD(FLOAD TNEW IRFL_TAB_HMASK) +LJFOLDF(fload_tab_tnew_hmask) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) + return INTFOLD((1 << fleft->op2)-1); + return NEXTFOLD; +} + +LJFOLD(FLOAD TDUP IRFL_TAB_ASIZE) +LJFOLDF(fload_tab_tdup_asize) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) + return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->asize); + return NEXTFOLD; +} + +LJFOLD(FLOAD TDUP IRFL_TAB_HMASK) +LJFOLDF(fload_tab_tdup_hmask) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && lj_opt_fwd_tptr(J, fins->op1)) + return INTFOLD((int32_t)ir_ktab(IR(fleft->op1))->hmask); + return NEXTFOLD; +} + +LJFOLD(HREF any any) +LJFOLD(FLOAD any IRFL_TAB_ARRAY) +LJFOLD(FLOAD any IRFL_TAB_NODE) +LJFOLD(FLOAD any IRFL_TAB_ASIZE) +LJFOLD(FLOAD any IRFL_TAB_HMASK) +LJFOLDF(fload_tab_ah) +{ + TRef tr = lj_opt_cse(J); + return lj_opt_fwd_tptr(J, tref_ref(tr)) ? tr : EMITFOLD; +} + +/* Strings are immutable, so we can safely FOLD/CSE the related FLOAD. */ +LJFOLD(FLOAD KGC IRFL_STR_LEN) +LJFOLDF(fload_str_len_kgc) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) + return INTFOLD((int32_t)ir_kstr(fleft)->len); + return NEXTFOLD; +} + +LJFOLD(FLOAD SNEW IRFL_STR_LEN) +LJFOLDF(fload_str_len_snew) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) { + PHIBARRIER(fleft); + return fleft->op2; + } + return NEXTFOLD; +} + +/* The C type ID of cdata objects is immutable. */ +LJFOLD(FLOAD KGC IRFL_CDATA_CTYPEID) +LJFOLDF(fload_cdata_typeid_kgc) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) + return INTFOLD((int32_t)ir_kcdata(fleft)->ctypeid); + return NEXTFOLD; +} + +/* Get the contents of immutable cdata objects. */ +LJFOLD(FLOAD KGC IRFL_CDATA_PTR) +LJFOLD(FLOAD KGC IRFL_CDATA_INT) +LJFOLD(FLOAD KGC IRFL_CDATA_INT64) +LJFOLDF(fload_cdata_int64_kgc) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) { + void *p = cdataptr(ir_kcdata(fleft)); + if (irt_is64(fins->t)) + return INT64FOLD(*(uint64_t *)p); + else + return INTFOLD(*(int32_t *)p); + } + return NEXTFOLD; +} + +LJFOLD(FLOAD CNEW IRFL_CDATA_CTYPEID) +LJFOLD(FLOAD CNEWI IRFL_CDATA_CTYPEID) +LJFOLDF(fload_cdata_typeid_cnew) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) + return fleft->op1; /* No PHI barrier needed. CNEW/CNEWI op1 is const. */ + return NEXTFOLD; +} + +/* Pointer, int and int64 cdata objects are immutable. */ +LJFOLD(FLOAD CNEWI IRFL_CDATA_PTR) +LJFOLD(FLOAD CNEWI IRFL_CDATA_INT) +LJFOLD(FLOAD CNEWI IRFL_CDATA_INT64) +LJFOLDF(fload_cdata_ptr_int64_cnew) +{ + if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD)) + return fleft->op2; /* Fold even across PHI to avoid allocations. */ + return NEXTFOLD; +} + +LJFOLD(FLOAD any IRFL_STR_LEN) +LJFOLD(FLOAD any IRFL_CDATA_CTYPEID) +LJFOLD(FLOAD any IRFL_CDATA_PTR) +LJFOLD(FLOAD any IRFL_CDATA_INT) +LJFOLD(FLOAD any IRFL_CDATA_INT64) +LJFOLD(VLOAD any any) /* Vararg loads have no corresponding stores. */ +LJFOLDX(lj_opt_cse) + +/* All other field loads need alias analysis. */ +LJFOLD(FLOAD any any) +LJFOLDX(lj_opt_fwd_fload) + +/* This is for LOOP only. Recording handles SLOADs internally. */ +LJFOLD(SLOAD any any) +LJFOLDF(fwd_sload) +{ + if ((fins->op2 & IRSLOAD_FRAME)) { + TRef tr = lj_opt_cse(J); + return tref_ref(tr) < J->chain[IR_RETF] ? EMITFOLD : tr; + } else { + lua_assert(J->slot[fins->op1] != 0); + return J->slot[fins->op1]; + } +} + +/* Only fold for KKPTR. The pointer _and_ the contents must be const. */ +LJFOLD(XLOAD KKPTR any) +LJFOLDF(xload_kptr) +{ + TRef tr = kfold_xload(J, fins, ir_kptr(fleft)); + return tr ? tr : NEXTFOLD; +} + +LJFOLD(XLOAD any any) +LJFOLDX(lj_opt_fwd_xload) + +/* -- Write barriers ------------------------------------------------------ */ + +/* Write barriers are amenable to CSE, but not across any incremental +** GC steps. +** +** The same logic applies to open upvalue references, because a stack +** may be resized during a GC step (not the current stack, but maybe that +** of a coroutine). +*/ +LJFOLD(TBAR any) +LJFOLD(OBAR any any) +LJFOLD(UREFO any any) +LJFOLDF(barrier_tab) +{ + TRef tr = lj_opt_cse(J); + if (gcstep_barrier(J, tref_ref(tr))) /* CSE across GC step? */ + return EMITFOLD; /* Raw emit. Assumes fins is left intact by CSE. */ + return tr; +} + +LJFOLD(TBAR TNEW) +LJFOLD(TBAR TDUP) +LJFOLDF(barrier_tnew_tdup) +{ + /* New tables are always white and never need a barrier. */ + if (fins->op1 < J->chain[IR_LOOP]) /* Except across a GC step. */ + return NEXTFOLD; + return DROPFOLD; +} + +/* -- Stores and allocations ---------------------------------------------- */ + +/* Stores and allocations cannot be folded or passed on to CSE in general. +** But some stores can be eliminated with dead-store elimination (DSE). +** +** Caveat: *all* stores and allocs must be listed here or they end up at CSE! +*/ + +LJFOLD(ASTORE any any) +LJFOLD(HSTORE any any) +LJFOLDX(lj_opt_dse_ahstore) + +LJFOLD(USTORE any any) +LJFOLDX(lj_opt_dse_ustore) + +LJFOLD(FSTORE any any) +LJFOLDX(lj_opt_dse_fstore) + +LJFOLD(XSTORE any any) +LJFOLDX(lj_opt_dse_xstore) + +LJFOLD(NEWREF any any) /* Treated like a store. */ +LJFOLD(CALLS any any) +LJFOLD(CALLL any any) /* Safeguard fallback. */ +LJFOLD(CALLXS any any) +LJFOLD(XBAR) +LJFOLD(RETF any any) /* Modifies BASE. */ +LJFOLD(TNEW any any) +LJFOLD(TDUP any) +LJFOLD(CNEW any any) +LJFOLD(XSNEW any any) +LJFOLDX(lj_ir_emit) + +/* ------------------------------------------------------------------------ */ + +/* Every entry in the generated hash table is a 32 bit pattern: +** +** xxxxxxxx iiiiiii lllllll rrrrrrrrrr +** +** xxxxxxxx = 8 bit index into fold function table +** iiiiiii = 7 bit folded instruction opcode +** lllllll = 7 bit left instruction opcode +** rrrrrrrrrr = 8 bit right instruction opcode or 10 bits from literal field +*/ + +#include "lj_folddef.h" + +/* ------------------------------------------------------------------------ */ + +/* Fold IR instruction. */ +TRef LJ_FASTCALL lj_opt_fold(jit_State *J) +{ + uint32_t key, any; + IRRef ref; + + if (LJ_UNLIKELY((J->flags & JIT_F_OPT_MASK) != JIT_F_OPT_DEFAULT)) { + lua_assert(((JIT_F_OPT_FOLD|JIT_F_OPT_FWD|JIT_F_OPT_CSE|JIT_F_OPT_DSE) | + JIT_F_OPT_DEFAULT) == JIT_F_OPT_DEFAULT); + /* Folding disabled? Chain to CSE, but not for loads/stores/allocs. */ + if (!(J->flags & JIT_F_OPT_FOLD) && irm_kind(lj_ir_mode[fins->o]) == IRM_N) + return lj_opt_cse(J); + + /* Forwarding or CSE disabled? Emit raw IR for loads, except for SLOAD. */ + if ((J->flags & (JIT_F_OPT_FWD|JIT_F_OPT_CSE)) != + (JIT_F_OPT_FWD|JIT_F_OPT_CSE) && + irm_kind(lj_ir_mode[fins->o]) == IRM_L && fins->o != IR_SLOAD) + return lj_ir_emit(J); + + /* DSE disabled? Emit raw IR for stores. */ + if (!(J->flags & JIT_F_OPT_DSE) && irm_kind(lj_ir_mode[fins->o]) == IRM_S) + return lj_ir_emit(J); + } + + /* Fold engine start/retry point. */ +retry: + /* Construct key from opcode and operand opcodes (unless literal/none). */ + key = ((uint32_t)fins->o << 17); + if (fins->op1 >= J->cur.nk) { + key += (uint32_t)IR(fins->op1)->o << 10; + *fleft = *IR(fins->op1); + } + if (fins->op2 >= J->cur.nk) { + key += (uint32_t)IR(fins->op2)->o; + *fright = *IR(fins->op2); + } else { + key += (fins->op2 & 0x3ffu); /* Literal mask. Must include IRCONV_*MASK. */ + } + + /* Check for a match in order from most specific to least specific. */ + any = 0; + for (;;) { + uint32_t k = key | (any & 0x1ffff); + uint32_t h = fold_hashkey(k); + uint32_t fh = fold_hash[h]; /* Lookup key in semi-perfect hash table. */ + if ((fh & 0xffffff) == k || (fh = fold_hash[h+1], (fh & 0xffffff) == k)) { + ref = (IRRef)tref_ref(fold_func[fh >> 24](J)); + if (ref != NEXTFOLD) + break; + } + if (any == 0xfffff) /* Exhausted folding. Pass on to CSE. */ + return lj_opt_cse(J); + any = (any | (any >> 10)) ^ 0xffc00; + } + + /* Return value processing, ordered by frequency. */ + if (LJ_LIKELY(ref >= MAX_FOLD)) + return TREF(ref, irt_t(IR(ref)->t)); + if (ref == RETRYFOLD) + goto retry; + if (ref == KINTFOLD) + return lj_ir_kint(J, fins->i); + if (ref == FAILFOLD) + lj_trace_err(J, LJ_TRERR_GFAIL); + lua_assert(ref == DROPFOLD); + return REF_DROP; +} + +/* -- Common-Subexpression Elimination ------------------------------------ */ + +/* CSE an IR instruction. This is very fast due to the skip-list chains. */ +TRef LJ_FASTCALL lj_opt_cse(jit_State *J) +{ + /* Avoid narrow to wide store-to-load forwarding stall */ + IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16); + IROp op = fins->o; + if (LJ_LIKELY(J->flags & JIT_F_OPT_CSE)) { + /* Limited search for same operands in per-opcode chain. */ + IRRef ref = J->chain[op]; + IRRef lim = fins->op1; + if (fins->op2 > lim) lim = fins->op2; /* Relies on lit < REF_BIAS. */ + while (ref > lim) { + if (IR(ref)->op12 == op12) + return TREF(ref, irt_t(IR(ref)->t)); /* Common subexpression found. */ + ref = IR(ref)->prev; + } + } + /* Otherwise emit IR (inlined for speed). */ + { + IRRef ref = lj_ir_nextins(J); + IRIns *ir = IR(ref); + ir->prev = J->chain[op]; + ir->op12 = op12; + J->chain[op] = (IRRef1)ref; + ir->o = fins->o; + J->guardemit.irt |= fins->t.irt; + return TREF(ref, irt_t((ir->t = fins->t))); + } +} + +/* CSE with explicit search limit. */ +TRef LJ_FASTCALL lj_opt_cselim(jit_State *J, IRRef lim) +{ + IRRef ref = J->chain[fins->o]; + IRRef2 op12 = (IRRef2)fins->op1 + ((IRRef2)fins->op2 << 16); + while (ref > lim) { + if (IR(ref)->op12 == op12) + return ref; + ref = IR(ref)->prev; + } + return lj_ir_emit(J); +} + +/* ------------------------------------------------------------------------ */ + +#undef IR +#undef fins +#undef fleft +#undef fright +#undef knumleft +#undef knumright +#undef emitir + +#endif diff --git a/external/lua/luajit/src/src/lj_opt_loop.c b/external/lua/luajit/src/src/lj_opt_loop.c new file mode 100644 index 0000000000..3a119f4703 --- /dev/null +++ b/external/lua/luajit/src/src/lj_opt_loop.c @@ -0,0 +1,437 @@ +/* +** LOOP: Loop Optimizations. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_opt_loop_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_err.h" +#include "lj_str.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_iropt.h" +#include "lj_trace.h" +#include "lj_snap.h" +#include "lj_vm.h" + +/* Loop optimization: +** +** Traditional Loop-Invariant Code Motion (LICM) splits the instructions +** of a loop into invariant and variant instructions. The invariant +** instructions are hoisted out of the loop and only the variant +** instructions remain inside the loop body. +** +** Unfortunately LICM is mostly useless for compiling dynamic languages. +** The IR has many guards and most of the subsequent instructions are +** control-dependent on them. The first non-hoistable guard would +** effectively prevent hoisting of all subsequent instructions. +** +** That's why we use a special form of unrolling using copy-substitution, +** combined with redundancy elimination: +** +** The recorded instruction stream is re-emitted to the compiler pipeline +** with substituted operands. The substitution table is filled with the +** refs returned by re-emitting each instruction. This can be done +** on-the-fly, because the IR is in strict SSA form, where every ref is +** defined before its use. +** +** This aproach generates two code sections, separated by the LOOP +** instruction: +** +** 1. The recorded instructions form a kind of pre-roll for the loop. It +** contains a mix of invariant and variant instructions and performs +** exactly one loop iteration (but not necessarily the 1st iteration). +** +** 2. The loop body contains only the variant instructions and performs +** all remaining loop iterations. +** +** On first sight that looks like a waste of space, because the variant +** instructions are present twice. But the key insight is that the +** pre-roll honors the control-dependencies for *both* the pre-roll itself +** *and* the loop body! +** +** It also means one doesn't have to explicitly model control-dependencies +** (which, BTW, wouldn't help LICM much). And it's much easier to +** integrate sparse snapshotting with this approach. +** +** One of the nicest aspects of this approach is that all of the +** optimizations of the compiler pipeline (FOLD, CSE, FWD, etc.) can be +** reused with only minor restrictions (e.g. one should not fold +** instructions across loop-carried dependencies). +** +** But in general all optimizations can be applied which only need to look +** backwards into the generated instruction stream. At any point in time +** during the copy-substitution process this contains both a static loop +** iteration (the pre-roll) and a dynamic one (from the to-be-copied +** instruction up to the end of the partial loop body). +** +** Since control-dependencies are implicitly kept, CSE also applies to all +** kinds of guards. The major advantage is that all invariant guards can +** be hoisted, too. +** +** Load/store forwarding works across loop iterations, too. This is +** important if loop-carried dependencies are kept in upvalues or tables. +** E.g. 'self.idx = self.idx + 1' deep down in some OO-style method may +** become a forwarded loop-recurrence after inlining. +** +** Since the IR is in SSA form, loop-carried dependencies have to be +** modeled with PHI instructions. The potential candidates for PHIs are +** collected on-the-fly during copy-substitution. After eliminating the +** redundant ones, PHI instructions are emitted *below* the loop body. +** +** Note that this departure from traditional SSA form doesn't change the +** semantics of the PHI instructions themselves. But it greatly simplifies +** on-the-fly generation of the IR and the machine code. +*/ + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) + +/* Pass IR on to next optimization in chain (FOLD). */ +#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) + +/* Emit raw IR without passing through optimizations. */ +#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) + +/* -- PHI elimination ----------------------------------------------------- */ + +/* Emit or eliminate collected PHIs. */ +static void loop_emit_phi(jit_State *J, IRRef1 *subst, IRRef1 *phi, IRRef nphi, + SnapNo onsnap) +{ + int passx = 0; + IRRef i, nslots; + IRRef invar = J->chain[IR_LOOP]; + /* Pass #1: mark redundant and potentially redundant PHIs. */ + for (i = 0; i < nphi; i++) { + IRRef lref = phi[i]; + IRRef rref = subst[lref]; + if (lref == rref || rref == REF_DROP) { /* Invariants are redundant. */ + irt_setmark(IR(lref)->t); + } else if (!(IR(rref)->op1 == lref || IR(rref)->op2 == lref)) { + /* Quick check for simple recurrences failed, need pass2. */ + irt_setmark(IR(lref)->t); + passx = 1; + } + } + /* Pass #2: traverse variant part and clear marks of non-redundant PHIs. */ + if (passx) { + SnapNo s; + for (i = J->cur.nins-1; i > invar; i--) { + IRIns *ir = IR(i); + if (!irref_isk(ir->op2)) irt_clearmark(IR(ir->op2)->t); + if (!irref_isk(ir->op1)) { + irt_clearmark(IR(ir->op1)->t); + if (ir->op1 < invar && + ir->o >= IR_CALLN && ir->o <= IR_CARG) { /* ORDER IR */ + ir = IR(ir->op1); + while (ir->o == IR_CARG) { + if (!irref_isk(ir->op2)) irt_clearmark(IR(ir->op2)->t); + if (irref_isk(ir->op1)) break; + ir = IR(ir->op1); + irt_clearmark(ir->t); + } + } + } + } + for (s = J->cur.nsnap-1; s >= onsnap; s--) { + SnapShot *snap = &J->cur.snap[s]; + SnapEntry *map = &J->cur.snapmap[snap->mapofs]; + MSize n, nent = snap->nent; + for (n = 0; n < nent; n++) { + IRRef ref = snap_ref(map[n]); + if (!irref_isk(ref)) irt_clearmark(IR(ref)->t); + } + } + } + /* Pass #3: add PHIs for variant slots without a corresponding SLOAD. */ + nslots = J->baseslot+J->maxslot; + for (i = 1; i < nslots; i++) { + IRRef ref = tref_ref(J->slot[i]); + while (!irref_isk(ref) && ref != subst[ref]) { + IRIns *ir = IR(ref); + irt_clearmark(ir->t); /* Unmark potential uses, too. */ + if (irt_isphi(ir->t) || irt_ispri(ir->t)) + break; + irt_setphi(ir->t); + if (nphi >= LJ_MAX_PHI) + lj_trace_err(J, LJ_TRERR_PHIOV); + phi[nphi++] = (IRRef1)ref; + ref = subst[ref]; + if (ref > invar) + break; + } + } + /* Pass #4: propagate non-redundant PHIs. */ + while (passx) { + passx = 0; + for (i = 0; i < nphi; i++) { + IRRef lref = phi[i]; + IRIns *ir = IR(lref); + if (!irt_ismarked(ir->t)) { /* Propagate only from unmarked PHIs. */ + IRRef rref = subst[lref]; + if (lref == rref) { /* Mark redundant PHI. */ + irt_setmark(ir->t); + } else { + IRIns *irr = IR(rref); + if (irt_ismarked(irr->t)) { /* Right ref points to other PHI? */ + irt_clearmark(irr->t); /* Mark that PHI as non-redundant. */ + passx = 1; /* Retry. */ + } + } + } + } + } + /* Pass #5: emit PHI instructions or eliminate PHIs. */ + for (i = 0; i < nphi; i++) { + IRRef lref = phi[i]; + IRIns *ir = IR(lref); + if (!irt_ismarked(ir->t)) { /* Emit PHI if not marked. */ + IRRef rref = subst[lref]; + if (rref > invar) + irt_setphi(IR(rref)->t); + emitir_raw(IRT(IR_PHI, irt_type(ir->t)), lref, rref); + } else { /* Otherwise eliminate PHI. */ + irt_clearmark(ir->t); + irt_clearphi(ir->t); + } + } +} + +/* -- Loop unrolling using copy-substitution ------------------------------ */ + +/* Copy-substitute snapshot. */ +static void loop_subst_snap(jit_State *J, SnapShot *osnap, + SnapEntry *loopmap, IRRef1 *subst) +{ + SnapEntry *nmap, *omap = &J->cur.snapmap[osnap->mapofs]; + SnapEntry *nextmap = &J->cur.snapmap[snap_nextofs(&J->cur, osnap)]; + MSize nmapofs; + MSize on, ln, nn, onent = osnap->nent; + BCReg nslots = osnap->nslots; + SnapShot *snap = &J->cur.snap[J->cur.nsnap]; + if (irt_isguard(J->guardemit)) { /* Guard inbetween? */ + nmapofs = J->cur.nsnapmap; + J->cur.nsnap++; /* Add new snapshot. */ + } else { /* Otherwise overwrite previous snapshot. */ + snap--; + nmapofs = snap->mapofs; + } + J->guardemit.irt = 0; + /* Setup new snapshot. */ + snap->mapofs = (uint16_t)nmapofs; + snap->ref = (IRRef1)J->cur.nins; + snap->nslots = nslots; + snap->topslot = osnap->topslot; + snap->count = 0; + nmap = &J->cur.snapmap[nmapofs]; + /* Substitute snapshot slots. */ + on = ln = nn = 0; + while (on < onent) { + SnapEntry osn = omap[on], lsn = loopmap[ln]; + if (snap_slot(lsn) < snap_slot(osn)) { /* Copy slot from loop map. */ + nmap[nn++] = lsn; + ln++; + } else { /* Copy substituted slot from snapshot map. */ + if (snap_slot(lsn) == snap_slot(osn)) ln++; /* Shadowed loop slot. */ + if (!irref_isk(snap_ref(osn))) + osn = snap_setref(osn, subst[snap_ref(osn)]); + nmap[nn++] = osn; + on++; + } + } + while (snap_slot(loopmap[ln]) < nslots) /* Copy remaining loop slots. */ + nmap[nn++] = loopmap[ln++]; + snap->nent = (uint8_t)nn; + omap += onent; + nmap += nn; + while (omap < nextmap) /* Copy PC + frame links. */ + *nmap++ = *omap++; + J->cur.nsnapmap = (uint16_t)(nmap - J->cur.snapmap); +} + +/* Unroll loop. */ +static void loop_unroll(jit_State *J) +{ + IRRef1 phi[LJ_MAX_PHI]; + uint32_t nphi = 0; + IRRef1 *subst; + SnapNo onsnap; + SnapShot *osnap, *loopsnap; + SnapEntry *loopmap, *psentinel; + IRRef ins, invar; + + /* Use temp buffer for substitution table. + ** Only non-constant refs in [REF_BIAS,invar) are valid indexes. + ** Caveat: don't call into the VM or run the GC or the buffer may be gone. + */ + invar = J->cur.nins; + subst = (IRRef1 *)lj_str_needbuf(J->L, &G(J->L)->tmpbuf, + (invar-REF_BIAS)*sizeof(IRRef1)) - REF_BIAS; + subst[REF_BASE] = REF_BASE; + + /* LOOP separates the pre-roll from the loop body. */ + emitir_raw(IRTG(IR_LOOP, IRT_NIL), 0, 0); + + /* Grow snapshot buffer and map for copy-substituted snapshots. + ** Need up to twice the number of snapshots minus #0 and loop snapshot. + ** Need up to twice the number of entries plus fallback substitutions + ** from the loop snapshot entries for each new snapshot. + ** Caveat: both calls may reallocate J->cur.snap and J->cur.snapmap! + */ + onsnap = J->cur.nsnap; + lj_snap_grow_buf(J, 2*onsnap-2); + lj_snap_grow_map(J, J->cur.nsnapmap*2+(onsnap-2)*J->cur.snap[onsnap-1].nent); + + /* The loop snapshot is used for fallback substitutions. */ + loopsnap = &J->cur.snap[onsnap-1]; + loopmap = &J->cur.snapmap[loopsnap->mapofs]; + /* The PC of snapshot #0 and the loop snapshot must match. */ + psentinel = &loopmap[loopsnap->nent]; + lua_assert(*psentinel == J->cur.snapmap[J->cur.snap[0].nent]); + *psentinel = SNAP(255, 0, 0); /* Replace PC with temporary sentinel. */ + + /* Start substitution with snapshot #1 (#0 is empty for root traces). */ + osnap = &J->cur.snap[1]; + + /* Copy and substitute all recorded instructions and snapshots. */ + for (ins = REF_FIRST; ins < invar; ins++) { + IRIns *ir; + IRRef op1, op2; + + if (ins >= osnap->ref) /* Instruction belongs to next snapshot? */ + loop_subst_snap(J, osnap++, loopmap, subst); /* Copy-substitute it. */ + + /* Substitute instruction operands. */ + ir = IR(ins); + op1 = ir->op1; + if (!irref_isk(op1)) op1 = subst[op1]; + op2 = ir->op2; + if (!irref_isk(op2)) op2 = subst[op2]; + if (irm_kind(lj_ir_mode[ir->o]) == IRM_N && + op1 == ir->op1 && op2 == ir->op2) { /* Regular invariant ins? */ + subst[ins] = (IRRef1)ins; /* Shortcut. */ + } else { + /* Re-emit substituted instruction to the FOLD/CSE/etc. pipeline. */ + IRType1 t = ir->t; /* Get this first, since emitir may invalidate ir. */ + IRRef ref = tref_ref(emitir(ir->ot & ~IRT_ISPHI, op1, op2)); + subst[ins] = (IRRef1)ref; + if (ref != ins) { + IRIns *irr = IR(ref); + if (ref < invar) { /* Loop-carried dependency? */ + /* Potential PHI? */ + if (!irref_isk(ref) && !irt_isphi(irr->t) && !irt_ispri(irr->t)) { + irt_setphi(irr->t); + if (nphi >= LJ_MAX_PHI) + lj_trace_err(J, LJ_TRERR_PHIOV); + phi[nphi++] = (IRRef1)ref; + } + /* Check all loop-carried dependencies for type instability. */ + if (!irt_sametype(t, irr->t)) { + if (irt_isinteger(t) && irt_isinteger(irr->t)) + continue; + else if (irt_isnum(t) && irt_isinteger(irr->t)) /* Fix int->num. */ + ref = tref_ref(emitir(IRTN(IR_CONV), ref, IRCONV_NUM_INT)); + else if (irt_isnum(irr->t) && irt_isinteger(t)) /* Fix num->int. */ + ref = tref_ref(emitir(IRTGI(IR_CONV), ref, + IRCONV_INT_NUM|IRCONV_CHECK)); + else + lj_trace_err(J, LJ_TRERR_TYPEINS); + subst[ins] = (IRRef1)ref; + irr = IR(ref); + goto phiconv; + } + } else if (ref != REF_DROP && irr->o == IR_CONV && + ref > invar && irr->op1 < invar) { + /* May need an extra PHI for a CONV. */ + ref = irr->op1; + irr = IR(ref); + phiconv: + if (ref < invar && !irref_isk(ref) && !irt_isphi(irr->t)) { + irt_setphi(irr->t); + if (nphi >= LJ_MAX_PHI) + lj_trace_err(J, LJ_TRERR_PHIOV); + phi[nphi++] = (IRRef1)ref; + } + } + } + } + } + if (!irt_isguard(J->guardemit)) /* Drop redundant snapshot. */ + J->cur.nsnapmap = (uint16_t)J->cur.snap[--J->cur.nsnap].mapofs; + lua_assert(J->cur.nsnapmap <= J->sizesnapmap); + *psentinel = J->cur.snapmap[J->cur.snap[0].nent]; /* Restore PC. */ + + loop_emit_phi(J, subst, phi, nphi, onsnap); +} + +/* Undo any partial changes made by the loop optimization. */ +static void loop_undo(jit_State *J, IRRef ins, SnapNo nsnap, MSize nsnapmap) +{ + ptrdiff_t i; + SnapShot *snap = &J->cur.snap[nsnap-1]; + SnapEntry *map = J->cur.snapmap; + map[snap->mapofs + snap->nent] = map[J->cur.snap[0].nent]; /* Restore PC. */ + J->cur.nsnapmap = (uint16_t)nsnapmap; + J->cur.nsnap = nsnap; + J->guardemit.irt = 0; + lj_ir_rollback(J, ins); + for (i = 0; i < BPROP_SLOTS; i++) { /* Remove backprop. cache entries. */ + BPropEntry *bp = &J->bpropcache[i]; + if (bp->val >= ins) + bp->key = 0; + } + for (ins--; ins >= REF_FIRST; ins--) { /* Remove flags. */ + IRIns *ir = IR(ins); + irt_clearphi(ir->t); + irt_clearmark(ir->t); + } +} + +/* Protected callback for loop optimization. */ +static TValue *cploop_opt(lua_State *L, lua_CFunction dummy, void *ud) +{ + UNUSED(L); UNUSED(dummy); + loop_unroll((jit_State *)ud); + return NULL; +} + +/* Loop optimization. */ +int lj_opt_loop(jit_State *J) +{ + IRRef nins = J->cur.nins; + SnapNo nsnap = J->cur.nsnap; + MSize nsnapmap = J->cur.nsnapmap; + int errcode = lj_vm_cpcall(J->L, NULL, J, cploop_opt); + if (LJ_UNLIKELY(errcode)) { + lua_State *L = J->L; + if (errcode == LUA_ERRRUN && tvisnumber(L->top-1)) { /* Trace error? */ + int32_t e = numberVint(L->top-1); + switch ((TraceError)e) { + case LJ_TRERR_TYPEINS: /* Type instability. */ + case LJ_TRERR_GFAIL: /* Guard would always fail. */ + /* Unrolling via recording fixes many cases, e.g. a flipped boolean. */ + if (--J->instunroll < 0) /* But do not unroll forever. */ + break; + L->top--; /* Remove error object. */ + loop_undo(J, nins, nsnap, nsnapmap); + return 1; /* Loop optimization failed, continue recording. */ + default: + break; + } + } + lj_err_throw(L, errcode); /* Propagate all other errors. */ + } + return 0; /* Loop optimization is ok. */ +} + +#undef IR +#undef emitir +#undef emitir_raw + +#endif diff --git a/external/lua/luajit/src/src/lj_opt_mem.c b/external/lua/luajit/src/src/lj_opt_mem.c new file mode 100644 index 0000000000..bf77666b1b --- /dev/null +++ b/external/lua/luajit/src/src/lj_opt_mem.c @@ -0,0 +1,907 @@ +/* +** Memory access optimizations. +** AA: Alias Analysis using high-level semantic disambiguation. +** FWD: Load Forwarding (L2L) + Store Forwarding (S2L). +** DSE: Dead-Store Elimination. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_opt_mem_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_tab.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_iropt.h" + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) +#define fins (&J->fold.ins) +#define fleft (&J->fold.left) +#define fright (&J->fold.right) + +/* +** Caveat #1: return value is not always a TRef -- only use with tref_ref(). +** Caveat #2: FWD relies on active CSE for xREF operands -- see lj_opt_fold(). +*/ + +/* Return values from alias analysis. */ +typedef enum { + ALIAS_NO, /* The two refs CANNOT alias (exact). */ + ALIAS_MAY, /* The two refs MAY alias (inexact). */ + ALIAS_MUST /* The two refs MUST alias (exact). */ +} AliasRet; + +/* -- ALOAD/HLOAD forwarding and ASTORE/HSTORE elimination ---------------- */ + +/* Simplified escape analysis: check for intervening stores. */ +static AliasRet aa_escape(jit_State *J, IRIns *ir, IRIns *stop) +{ + IRRef ref = (IRRef)(ir - J->cur.ir); /* The ref that might be stored. */ + for (ir++; ir < stop; ir++) + if (ir->op2 == ref && + (ir->o == IR_ASTORE || ir->o == IR_HSTORE || + ir->o == IR_USTORE || ir->o == IR_FSTORE)) + return ALIAS_MAY; /* Reference was stored and might alias. */ + return ALIAS_NO; /* Reference was not stored. */ +} + +/* Alias analysis for two different table references. */ +static AliasRet aa_table(jit_State *J, IRRef ta, IRRef tb) +{ + IRIns *taba = IR(ta), *tabb = IR(tb); + int newa, newb; + lua_assert(ta != tb); + lua_assert(irt_istab(taba->t) && irt_istab(tabb->t)); + /* Disambiguate new allocations. */ + newa = (taba->o == IR_TNEW || taba->o == IR_TDUP); + newb = (tabb->o == IR_TNEW || tabb->o == IR_TDUP); + if (newa && newb) + return ALIAS_NO; /* Two different allocations never alias. */ + if (newb) { /* At least one allocation? */ + IRIns *tmp = taba; taba = tabb; tabb = tmp; + } else if (!newa) { + return ALIAS_MAY; /* Anything else: we just don't know. */ + } + return aa_escape(J, taba, tabb); +} + +/* Alias analysis for array and hash access using key-based disambiguation. */ +static AliasRet aa_ahref(jit_State *J, IRIns *refa, IRIns *refb) +{ + IRRef ka = refa->op2; + IRRef kb = refb->op2; + IRIns *keya, *keyb; + IRRef ta, tb; + if (refa == refb) + return ALIAS_MUST; /* Shortcut for same refs. */ + keya = IR(ka); + if (keya->o == IR_KSLOT) { ka = keya->op1; keya = IR(ka); } + keyb = IR(kb); + if (keyb->o == IR_KSLOT) { kb = keyb->op1; keyb = IR(kb); } + ta = (refa->o==IR_HREFK || refa->o==IR_AREF) ? IR(refa->op1)->op1 : refa->op1; + tb = (refb->o==IR_HREFK || refb->o==IR_AREF) ? IR(refb->op1)->op1 : refb->op1; + if (ka == kb) { + /* Same key. Check for same table with different ref (NEWREF vs. HREF). */ + if (ta == tb) + return ALIAS_MUST; /* Same key, same table. */ + else + return aa_table(J, ta, tb); /* Same key, possibly different table. */ + } + if (irref_isk(ka) && irref_isk(kb)) + return ALIAS_NO; /* Different constant keys. */ + if (refa->o == IR_AREF) { + /* Disambiguate array references based on index arithmetic. */ + int32_t ofsa = 0, ofsb = 0; + IRRef basea = ka, baseb = kb; + lua_assert(refb->o == IR_AREF); + /* Gather base and offset from t[base] or t[base+-ofs]. */ + if (keya->o == IR_ADD && irref_isk(keya->op2)) { + basea = keya->op1; + ofsa = IR(keya->op2)->i; + if (basea == kb && ofsa != 0) + return ALIAS_NO; /* t[base+-ofs] vs. t[base]. */ + } + if (keyb->o == IR_ADD && irref_isk(keyb->op2)) { + baseb = keyb->op1; + ofsb = IR(keyb->op2)->i; + if (ka == baseb && ofsb != 0) + return ALIAS_NO; /* t[base] vs. t[base+-ofs]. */ + } + if (basea == baseb && ofsa != ofsb) + return ALIAS_NO; /* t[base+-o1] vs. t[base+-o2] and o1 != o2. */ + } else { + /* Disambiguate hash references based on the type of their keys. */ + lua_assert((refa->o==IR_HREF || refa->o==IR_HREFK || refa->o==IR_NEWREF) && + (refb->o==IR_HREF || refb->o==IR_HREFK || refb->o==IR_NEWREF)); + if (!irt_sametype(keya->t, keyb->t)) + return ALIAS_NO; /* Different key types. */ + } + if (ta == tb) + return ALIAS_MAY; /* Same table, cannot disambiguate keys. */ + else + return aa_table(J, ta, tb); /* Try to disambiguate tables. */ +} + +/* Array and hash load forwarding. */ +static TRef fwd_ahload(jit_State *J, IRRef xref) +{ + IRIns *xr = IR(xref); + IRRef lim = xref; /* Search limit. */ + IRRef ref; + + /* Search for conflicting stores. */ + ref = J->chain[fins->o+IRDELTA_L2S]; + while (ref > xref) { + IRIns *store = IR(ref); + switch (aa_ahref(J, xr, IR(store->op1))) { + case ALIAS_NO: break; /* Continue searching. */ + case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ + case ALIAS_MUST: return store->op2; /* Store forwarding. */ + } + ref = store->prev; + } + + /* No conflicting store (yet): const-fold loads from allocations. */ + { + IRIns *ir = (xr->o == IR_HREFK || xr->o == IR_AREF) ? IR(xr->op1) : xr; + IRRef tab = ir->op1; + ir = IR(tab); + if (ir->o == IR_TNEW || (ir->o == IR_TDUP && irref_isk(xr->op2))) { + /* A NEWREF with a number key may end up pointing to the array part. + ** But it's referenced from HSTORE and not found in the ASTORE chain. + ** For now simply consider this a conflict without forwarding anything. + */ + if (xr->o == IR_AREF) { + IRRef ref2 = J->chain[IR_NEWREF]; + while (ref2 > tab) { + IRIns *newref = IR(ref2); + if (irt_isnum(IR(newref->op2)->t)) + goto cselim; + ref2 = newref->prev; + } + } + /* NEWREF inhibits CSE for HREF, and dependent FLOADs from HREFK/AREF. + ** But the above search for conflicting stores was limited by xref. + ** So continue searching, limited by the TNEW/TDUP. Store forwarding + ** is ok, too. A conflict does NOT limit the search for a matching load. + */ + while (ref > tab) { + IRIns *store = IR(ref); + switch (aa_ahref(J, xr, IR(store->op1))) { + case ALIAS_NO: break; /* Continue searching. */ + case ALIAS_MAY: goto cselim; /* Conflicting store. */ + case ALIAS_MUST: return store->op2; /* Store forwarding. */ + } + ref = store->prev; + } + lua_assert(ir->o != IR_TNEW || irt_isnil(fins->t)); + if (irt_ispri(fins->t)) { + return TREF_PRI(irt_type(fins->t)); + } else if (irt_isnum(fins->t) || (LJ_DUALNUM && irt_isint(fins->t)) || + irt_isstr(fins->t)) { + TValue keyv; + cTValue *tv; + IRIns *key = IR(xr->op2); + if (key->o == IR_KSLOT) key = IR(key->op1); + lj_ir_kvalue(J->L, &keyv, key); + tv = lj_tab_get(J->L, ir_ktab(IR(ir->op1)), &keyv); + lua_assert(itype2irt(tv) == irt_type(fins->t)); + if (irt_isnum(fins->t)) + return lj_ir_knum_u64(J, tv->u64); + else if (LJ_DUALNUM && irt_isint(fins->t)) + return lj_ir_kint(J, intV(tv)); + else + return lj_ir_kstr(J, strV(tv)); + } + /* Othwerwise: don't intern as a constant. */ + } + } + +cselim: + /* Try to find a matching load. Below the conflicting store, if any. */ + ref = J->chain[fins->o]; + while (ref > lim) { + IRIns *load = IR(ref); + if (load->op1 == xref) + return ref; /* Load forwarding. */ + ref = load->prev; + } + return 0; /* Conflict or no match. */ +} + +/* Reassociate ALOAD across PHIs to handle t[i-1] forwarding case. */ +static TRef fwd_aload_reassoc(jit_State *J) +{ + IRIns *irx = IR(fins->op1); + IRIns *key = IR(irx->op2); + if (key->o == IR_ADD && irref_isk(key->op2)) { + IRIns *add2 = IR(key->op1); + if (add2->o == IR_ADD && irref_isk(add2->op2) && + IR(key->op2)->i == -IR(add2->op2)->i) { + IRRef ref = J->chain[IR_AREF]; + IRRef lim = add2->op1; + if (irx->op1 > lim) lim = irx->op1; + while (ref > lim) { + IRIns *ir = IR(ref); + if (ir->op1 == irx->op1 && ir->op2 == add2->op1) + return fwd_ahload(J, ref); + ref = ir->prev; + } + } + } + return 0; +} + +/* ALOAD forwarding. */ +TRef LJ_FASTCALL lj_opt_fwd_aload(jit_State *J) +{ + IRRef ref; + if ((ref = fwd_ahload(J, fins->op1)) || + (ref = fwd_aload_reassoc(J))) + return ref; + return EMITFOLD; +} + +/* HLOAD forwarding. */ +TRef LJ_FASTCALL lj_opt_fwd_hload(jit_State *J) +{ + IRRef ref = fwd_ahload(J, fins->op1); + if (ref) + return ref; + return EMITFOLD; +} + +/* HREFK forwarding. */ +TRef LJ_FASTCALL lj_opt_fwd_hrefk(jit_State *J) +{ + IRRef tab = fleft->op1; + IRRef ref = J->chain[IR_NEWREF]; + while (ref > tab) { + IRIns *newref = IR(ref); + if (tab == newref->op1) { + if (fright->op1 == newref->op2) + return ref; /* Forward from NEWREF. */ + else + goto docse; + } else if (aa_table(J, tab, newref->op1) != ALIAS_NO) { + goto docse; + } + ref = newref->prev; + } + /* No conflicting NEWREF: key location unchanged for HREFK of TDUP. */ + if (IR(tab)->o == IR_TDUP) + fins->t.irt &= ~IRT_GUARD; /* Drop HREFK guard. */ +docse: + return CSEFOLD; +} + +/* Check whether HREF of TNEW/TDUP can be folded to niltv. */ +int LJ_FASTCALL lj_opt_fwd_href_nokey(jit_State *J) +{ + IRRef lim = fins->op1; /* Search limit. */ + IRRef ref; + + /* The key for an ASTORE may end up in the hash part after a NEWREF. */ + if (irt_isnum(fright->t) && J->chain[IR_NEWREF] > lim) { + ref = J->chain[IR_ASTORE]; + while (ref > lim) { + if (ref < J->chain[IR_NEWREF]) + return 0; /* Conflict. */ + ref = IR(ref)->prev; + } + } + + /* Search for conflicting stores. */ + ref = J->chain[IR_HSTORE]; + while (ref > lim) { + IRIns *store = IR(ref); + if (aa_ahref(J, fins, IR(store->op1)) != ALIAS_NO) + return 0; /* Conflict. */ + ref = store->prev; + } + + return 1; /* No conflict. Can fold to niltv. */ +} + +/* Check whether there's no aliasing NEWREF for the left operand. */ +int LJ_FASTCALL lj_opt_fwd_tptr(jit_State *J, IRRef lim) +{ + IRRef ta = fins->op1; + IRRef ref = J->chain[IR_NEWREF]; + while (ref > lim) { + IRIns *newref = IR(ref); + if (ta == newref->op1 || aa_table(J, ta, newref->op1) != ALIAS_NO) + return 0; /* Conflict. */ + ref = newref->prev; + } + return 1; /* No conflict. Can safely FOLD/CSE. */ +} + +/* ASTORE/HSTORE elimination. */ +TRef LJ_FASTCALL lj_opt_dse_ahstore(jit_State *J) +{ + IRRef xref = fins->op1; /* xREF reference. */ + IRRef val = fins->op2; /* Stored value reference. */ + IRIns *xr = IR(xref); + IRRef1 *refp = &J->chain[fins->o]; + IRRef ref = *refp; + while (ref > xref) { /* Search for redundant or conflicting stores. */ + IRIns *store = IR(ref); + switch (aa_ahref(J, xr, IR(store->op1))) { + case ALIAS_NO: + break; /* Continue searching. */ + case ALIAS_MAY: /* Store to MAYBE the same location. */ + if (store->op2 != val) /* Conflict if the value is different. */ + goto doemit; + break; /* Otherwise continue searching. */ + case ALIAS_MUST: /* Store to the same location. */ + if (store->op2 == val) /* Same value: drop the new store. */ + return DROPFOLD; + /* Different value: try to eliminate the redundant store. */ + if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ + IRIns *ir; + /* Check for any intervening guards (includes conflicting loads). */ + for (ir = IR(J->cur.nins-1); ir > store; ir--) + if (irt_isguard(ir->t)) + goto doemit; /* No elimination possible. */ + /* Remove redundant store from chain and replace with NOP. */ + *refp = store->prev; + store->o = IR_NOP; + store->t.irt = IRT_NIL; + store->op1 = store->op2 = 0; + store->prev = 0; + /* Now emit the new store instead. */ + } + goto doemit; + } + ref = *(refp = &store->prev); + } +doemit: + return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ +} + +/* -- ULOAD forwarding ---------------------------------------------------- */ + +/* The current alias analysis for upvalues is very simplistic. It only +** disambiguates between the unique upvalues of the same function. +** This is good enough for now, since most upvalues are read-only. +** +** A more precise analysis would be feasible with the help of the parser: +** generate a unique key for every upvalue, even across all prototypes. +** Lacking a realistic use-case, it's unclear whether this is beneficial. +*/ +static AliasRet aa_uref(IRIns *refa, IRIns *refb) +{ + if (refa->o != refb->o) + return ALIAS_NO; /* Different UREFx type. */ + if (refa->op1 == refb->op1) { /* Same function. */ + if (refa->op2 == refb->op2) + return ALIAS_MUST; /* Same function, same upvalue idx. */ + else + return ALIAS_NO; /* Same function, different upvalue idx. */ + } else { /* Different functions, check disambiguation hash values. */ + if (((refa->op2 ^ refb->op2) & 0xff)) + return ALIAS_NO; /* Upvalues with different hash values cannot alias. */ + else + return ALIAS_MAY; /* No conclusion can be drawn for same hash value. */ + } +} + +/* ULOAD forwarding. */ +TRef LJ_FASTCALL lj_opt_fwd_uload(jit_State *J) +{ + IRRef uref = fins->op1; + IRRef lim = uref; /* Search limit. */ + IRIns *xr = IR(uref); + IRRef ref; + + /* Search for conflicting stores. */ + ref = J->chain[IR_USTORE]; + while (ref > uref) { + IRIns *store = IR(ref); + switch (aa_uref(xr, IR(store->op1))) { + case ALIAS_NO: break; /* Continue searching. */ + case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ + case ALIAS_MUST: return store->op2; /* Store forwarding. */ + } + ref = store->prev; + } + +cselim: + /* Try to find a matching load. Below the conflicting store, if any. */ + return lj_opt_cselim(J, lim); +} + +/* USTORE elimination. */ +TRef LJ_FASTCALL lj_opt_dse_ustore(jit_State *J) +{ + IRRef xref = fins->op1; /* xREF reference. */ + IRRef val = fins->op2; /* Stored value reference. */ + IRIns *xr = IR(xref); + IRRef1 *refp = &J->chain[IR_USTORE]; + IRRef ref = *refp; + while (ref > xref) { /* Search for redundant or conflicting stores. */ + IRIns *store = IR(ref); + switch (aa_uref(xr, IR(store->op1))) { + case ALIAS_NO: + break; /* Continue searching. */ + case ALIAS_MAY: /* Store to MAYBE the same location. */ + if (store->op2 != val) /* Conflict if the value is different. */ + goto doemit; + break; /* Otherwise continue searching. */ + case ALIAS_MUST: /* Store to the same location. */ + if (store->op2 == val) /* Same value: drop the new store. */ + return DROPFOLD; + /* Different value: try to eliminate the redundant store. */ + if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ + IRIns *ir; + /* Check for any intervening guards (includes conflicting loads). */ + for (ir = IR(J->cur.nins-1); ir > store; ir--) + if (irt_isguard(ir->t)) + goto doemit; /* No elimination possible. */ + /* Remove redundant store from chain and replace with NOP. */ + *refp = store->prev; + store->o = IR_NOP; + store->t.irt = IRT_NIL; + store->op1 = store->op2 = 0; + store->prev = 0; + if (ref+1 < J->cur.nins && + store[1].o == IR_OBAR && store[1].op1 == xref) { + IRRef1 *bp = &J->chain[IR_OBAR]; + IRIns *obar; + for (obar = IR(*bp); *bp > ref+1; obar = IR(*bp)) + bp = &obar->prev; + /* Remove OBAR, too. */ + *bp = obar->prev; + obar->o = IR_NOP; + obar->t.irt = IRT_NIL; + obar->op1 = obar->op2 = 0; + obar->prev = 0; + } + /* Now emit the new store instead. */ + } + goto doemit; + } + ref = *(refp = &store->prev); + } +doemit: + return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ +} + +/* -- FLOAD forwarding and FSTORE elimination ----------------------------- */ + +/* Alias analysis for field access. +** Field loads are cheap and field stores are rare. +** Simple disambiguation based on field types is good enough. +*/ +static AliasRet aa_fref(jit_State *J, IRIns *refa, IRIns *refb) +{ + if (refa->op2 != refb->op2) + return ALIAS_NO; /* Different fields. */ + if (refa->op1 == refb->op1) + return ALIAS_MUST; /* Same field, same object. */ + else if (refa->op2 >= IRFL_TAB_META && refa->op2 <= IRFL_TAB_NOMM) + return aa_table(J, refa->op1, refb->op1); /* Disambiguate tables. */ + else + return ALIAS_MAY; /* Same field, possibly different object. */ +} + +/* Only the loads for mutable fields end up here (see FOLD). */ +TRef LJ_FASTCALL lj_opt_fwd_fload(jit_State *J) +{ + IRRef oref = fins->op1; /* Object reference. */ + IRRef fid = fins->op2; /* Field ID. */ + IRRef lim = oref; /* Search limit. */ + IRRef ref; + + /* Search for conflicting stores. */ + ref = J->chain[IR_FSTORE]; + while (ref > oref) { + IRIns *store = IR(ref); + switch (aa_fref(J, fins, IR(store->op1))) { + case ALIAS_NO: break; /* Continue searching. */ + case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ + case ALIAS_MUST: return store->op2; /* Store forwarding. */ + } + ref = store->prev; + } + + /* No conflicting store: const-fold field loads from allocations. */ + if (fid == IRFL_TAB_META) { + IRIns *ir = IR(oref); + if (ir->o == IR_TNEW || ir->o == IR_TDUP) + return lj_ir_knull(J, IRT_TAB); + } + +cselim: + /* Try to find a matching load. Below the conflicting store, if any. */ + return lj_opt_cselim(J, lim); +} + +/* FSTORE elimination. */ +TRef LJ_FASTCALL lj_opt_dse_fstore(jit_State *J) +{ + IRRef fref = fins->op1; /* FREF reference. */ + IRRef val = fins->op2; /* Stored value reference. */ + IRIns *xr = IR(fref); + IRRef1 *refp = &J->chain[IR_FSTORE]; + IRRef ref = *refp; + while (ref > fref) { /* Search for redundant or conflicting stores. */ + IRIns *store = IR(ref); + switch (aa_fref(J, xr, IR(store->op1))) { + case ALIAS_NO: + break; /* Continue searching. */ + case ALIAS_MAY: + if (store->op2 != val) /* Conflict if the value is different. */ + goto doemit; + break; /* Otherwise continue searching. */ + case ALIAS_MUST: + if (store->op2 == val) /* Same value: drop the new store. */ + return DROPFOLD; + /* Different value: try to eliminate the redundant store. */ + if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ + IRIns *ir; + /* Check for any intervening guards or conflicting loads. */ + for (ir = IR(J->cur.nins-1); ir > store; ir--) + if (irt_isguard(ir->t) || (ir->o == IR_FLOAD && ir->op2 == xr->op2)) + goto doemit; /* No elimination possible. */ + /* Remove redundant store from chain and replace with NOP. */ + *refp = store->prev; + store->o = IR_NOP; + store->t.irt = IRT_NIL; + store->op1 = store->op2 = 0; + store->prev = 0; + /* Now emit the new store instead. */ + } + goto doemit; + } + ref = *(refp = &store->prev); + } +doemit: + return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ +} + +/* -- XLOAD forwarding and XSTORE elimination ----------------------------- */ + +/* Find cdata allocation for a reference (if any). */ +static IRIns *aa_findcnew(jit_State *J, IRIns *ir) +{ + while (ir->o == IR_ADD) { + if (!irref_isk(ir->op1)) { + IRIns *ir1 = aa_findcnew(J, IR(ir->op1)); /* Left-recursion. */ + if (ir1) return ir1; + } + if (irref_isk(ir->op2)) return NULL; + ir = IR(ir->op2); /* Flatten right-recursion. */ + } + return ir->o == IR_CNEW ? ir : NULL; +} + +/* Alias analysis for two cdata allocations. */ +static AliasRet aa_cnew(jit_State *J, IRIns *refa, IRIns *refb) +{ + IRIns *cnewa = aa_findcnew(J, refa); + IRIns *cnewb = aa_findcnew(J, refb); + if (cnewa == cnewb) + return ALIAS_MAY; /* Same allocation or neither is an allocation. */ + if (cnewa && cnewb) + return ALIAS_NO; /* Two different allocations never alias. */ + if (cnewb) { cnewa = cnewb; refb = refa; } + return aa_escape(J, cnewa, refb); +} + +/* Alias analysis for XLOAD/XSTORE. */ +static AliasRet aa_xref(jit_State *J, IRIns *refa, IRIns *xa, IRIns *xb) +{ + ptrdiff_t ofsa = 0, ofsb = 0; + IRIns *refb = IR(xb->op1); + IRIns *basea = refa, *baseb = refb; + if (refa == refb && irt_sametype(xa->t, xb->t)) + return ALIAS_MUST; /* Shortcut for same refs with identical type. */ + /* Offset-based disambiguation. */ + if (refa->o == IR_ADD && irref_isk(refa->op2)) { + IRIns *irk = IR(refa->op2); + basea = IR(refa->op1); + ofsa = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 : + (ptrdiff_t)irk->i; + if (basea == refb && ofsa != 0) + return ALIAS_NO; /* base+-ofs vs. base. */ + } + if (refb->o == IR_ADD && irref_isk(refb->op2)) { + IRIns *irk = IR(refb->op2); + baseb = IR(refb->op1); + ofsb = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 : + (ptrdiff_t)irk->i; + if (refa == baseb && ofsb != 0) + return ALIAS_NO; /* base vs. base+-ofs. */ + } + /* This implements (very) strict aliasing rules. + ** Different types do NOT alias, except for differences in signedness. + ** Type punning through unions is allowed (but forces a reload). + */ + if (basea == baseb) { + ptrdiff_t sza = irt_size(xa->t), szb = irt_size(xb->t); + if (ofsa == ofsb) { + if (sza == szb && irt_isfp(xa->t) == irt_isfp(xb->t)) + return ALIAS_MUST; /* Same-sized, same-kind. May need to convert. */ + } else if (ofsa + sza <= ofsb || ofsb + szb <= ofsa) { + return ALIAS_NO; /* Non-overlapping base+-o1 vs. base+-o2. */ + } + /* NYI: extract, extend or reinterpret bits (int <-> fp). */ + return ALIAS_MAY; /* Overlapping or type punning: force reload. */ + } + if (!irt_sametype(xa->t, xb->t) && + !(irt_typerange(xa->t, IRT_I8, IRT_U64) && + ((xa->t.irt - IRT_I8) ^ (xb->t.irt - IRT_I8)) == 1)) + return ALIAS_NO; + /* NYI: structural disambiguation. */ + return aa_cnew(J, basea, baseb); /* Try to disambiguate allocations. */ +} + +/* Return CSEd reference or 0. Caveat: swaps lower ref to the right! */ +static IRRef reassoc_trycse(jit_State *J, IROp op, IRRef op1, IRRef op2) +{ + IRRef ref = J->chain[op]; + IRRef lim = op1; + if (op2 > lim) { lim = op2; op2 = op1; op1 = lim; } + while (ref > lim) { + IRIns *ir = IR(ref); + if (ir->op1 == op1 && ir->op2 == op2) + return ref; + ref = ir->prev; + } + return 0; +} + +/* Reassociate index references. */ +static IRRef reassoc_xref(jit_State *J, IRIns *ir) +{ + ptrdiff_t ofs = 0; + if (ir->o == IR_ADD && irref_isk(ir->op2)) { /* Get constant offset. */ + IRIns *irk = IR(ir->op2); + ofs = (LJ_64 && irk->o == IR_KINT64) ? (ptrdiff_t)ir_k64(irk)->u64 : + (ptrdiff_t)irk->i; + ir = IR(ir->op1); + } + if (ir->o == IR_ADD) { /* Add of base + index. */ + /* Index ref > base ref for loop-carried dependences. Only check op1. */ + IRIns *ir2, *ir1 = IR(ir->op1); + int32_t shift = 0; + IRRef idxref; + /* Determine index shifts. Don't bother with IR_MUL here. */ + if (ir1->o == IR_BSHL && irref_isk(ir1->op2)) + shift = IR(ir1->op2)->i; + else if (ir1->o == IR_ADD && ir1->op1 == ir1->op2) + shift = 1; + else + ir1 = ir; + ir2 = IR(ir1->op1); + /* A non-reassociated add. Must be a loop-carried dependence. */ + if (ir2->o == IR_ADD && irt_isint(ir2->t) && irref_isk(ir2->op2)) + ofs += (ptrdiff_t)IR(ir2->op2)->i << shift; + else + return 0; + idxref = ir2->op1; + /* Try to CSE the reassociated chain. Give up if not found. */ + if (ir1 != ir && + !(idxref = reassoc_trycse(J, ir1->o, idxref, + ir1->o == IR_BSHL ? ir1->op2 : idxref))) + return 0; + if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, ir->op2))) + return 0; + if (ofs != 0) { + IRRef refk = tref_ref(lj_ir_kintp(J, ofs)); + if (!(idxref = reassoc_trycse(J, IR_ADD, idxref, refk))) + return 0; + } + return idxref; /* Success, found a reassociated index reference. Phew. */ + } + return 0; /* Failure. */ +} + +/* XLOAD forwarding. */ +TRef LJ_FASTCALL lj_opt_fwd_xload(jit_State *J) +{ + IRRef xref = fins->op1; + IRIns *xr = IR(xref); + IRRef lim = xref; /* Search limit. */ + IRRef ref; + + if ((fins->op2 & IRXLOAD_READONLY)) + goto cselim; + if ((fins->op2 & IRXLOAD_VOLATILE)) + goto doemit; + + /* Search for conflicting stores. */ + ref = J->chain[IR_XSTORE]; +retry: + if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS]; + if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR]; + while (ref > lim) { + IRIns *store = IR(ref); + switch (aa_xref(J, xr, fins, store)) { + case ALIAS_NO: break; /* Continue searching. */ + case ALIAS_MAY: lim = ref; goto cselim; /* Limit search for load. */ + case ALIAS_MUST: + /* Emit conversion if the loaded type doesn't match the forwarded type. */ + if (!irt_sametype(fins->t, IR(store->op2)->t)) { + IRType st = irt_type(fins->t); + if (st == IRT_I8 || st == IRT_I16) { /* Trunc + sign-extend. */ + st |= IRCONV_SEXT; + } else if (st == IRT_U8 || st == IRT_U16) { /* Trunc + zero-extend. */ + } else if (st == IRT_INT) { + st = irt_type(IR(store->op2)->t); /* Needs dummy CONV.int.*. */ + } else { /* I64/U64 are boxed, U32 is hidden behind a CONV.num.u32. */ + goto store_fwd; + } + fins->ot = IRTI(IR_CONV); + fins->op1 = store->op2; + fins->op2 = (IRT_INT<<5)|st; + return RETRYFOLD; + } + store_fwd: + return store->op2; /* Store forwarding. */ + } + ref = store->prev; + } + +cselim: + /* Try to find a matching load. Below the conflicting store, if any. */ + ref = J->chain[IR_XLOAD]; + while (ref > lim) { + /* CSE for XLOAD depends on the type, but not on the IRXLOAD_* flags. */ + if (IR(ref)->op1 == xref && irt_sametype(IR(ref)->t, fins->t)) + return ref; + ref = IR(ref)->prev; + } + + /* Reassociate XLOAD across PHIs to handle a[i-1] forwarding case. */ + if (!(fins->op2 & IRXLOAD_READONLY) && J->chain[IR_LOOP] && + xref == fins->op1 && (xref = reassoc_xref(J, xr)) != 0) { + ref = J->chain[IR_XSTORE]; + while (ref > lim) /* Skip stores that have already been checked. */ + ref = IR(ref)->prev; + lim = xref; + xr = IR(xref); + goto retry; /* Retry with the reassociated reference. */ + } +doemit: + return EMITFOLD; +} + +/* XSTORE elimination. */ +TRef LJ_FASTCALL lj_opt_dse_xstore(jit_State *J) +{ + IRRef xref = fins->op1; + IRIns *xr = IR(xref); + IRRef lim = xref; /* Search limit. */ + IRRef val = fins->op2; /* Stored value reference. */ + IRRef1 *refp = &J->chain[IR_XSTORE]; + IRRef ref = *refp; + if (J->chain[IR_CALLXS] > lim) lim = J->chain[IR_CALLXS]; + if (J->chain[IR_XBAR] > lim) lim = J->chain[IR_XBAR]; + while (ref > lim) { /* Search for redundant or conflicting stores. */ + IRIns *store = IR(ref); + switch (aa_xref(J, xr, fins, store)) { + case ALIAS_NO: + break; /* Continue searching. */ + case ALIAS_MAY: + if (store->op2 != val) /* Conflict if the value is different. */ + goto doemit; + break; /* Otherwise continue searching. */ + case ALIAS_MUST: + if (store->op2 == val) /* Same value: drop the new store. */ + return DROPFOLD; + /* Different value: try to eliminate the redundant store. */ + if (ref > J->chain[IR_LOOP]) { /* Quick check to avoid crossing LOOP. */ + IRIns *ir; + /* Check for any intervening guards or any XLOADs (no AA performed). */ + for (ir = IR(J->cur.nins-1); ir > store; ir--) + if (irt_isguard(ir->t) || ir->o == IR_XLOAD) + goto doemit; /* No elimination possible. */ + /* Remove redundant store from chain and replace with NOP. */ + *refp = store->prev; + store->o = IR_NOP; + store->t.irt = IRT_NIL; + store->op1 = store->op2 = 0; + store->prev = 0; + /* Now emit the new store instead. */ + } + goto doemit; + } + ref = *(refp = &store->prev); + } +doemit: + return EMITFOLD; /* Otherwise we have a conflict or simply no match. */ +} + +/* -- Forwarding of lj_tab_len -------------------------------------------- */ + +/* This is rather simplistic right now, but better than nothing. */ +TRef LJ_FASTCALL lj_opt_fwd_tab_len(jit_State *J) +{ + IRRef tab = fins->op1; /* Table reference. */ + IRRef lim = tab; /* Search limit. */ + IRRef ref; + + /* Any ASTORE is a conflict and limits the search. */ + if (J->chain[IR_ASTORE] > lim) lim = J->chain[IR_ASTORE]; + + /* Search for conflicting HSTORE with numeric key. */ + ref = J->chain[IR_HSTORE]; + while (ref > lim) { + IRIns *store = IR(ref); + IRIns *href = IR(store->op1); + IRIns *key = IR(href->op2); + if (irt_isnum(key->o == IR_KSLOT ? IR(key->op1)->t : key->t)) { + lim = ref; /* Conflicting store found, limits search for TLEN. */ + break; + } + ref = store->prev; + } + + /* Try to find a matching load. Below the conflicting store, if any. */ + return lj_opt_cselim(J, lim); +} + +/* -- ASTORE/HSTORE previous type analysis -------------------------------- */ + +/* Check whether the previous value for a table store is non-nil. +** This can be derived either from a previous store or from a previous +** load (because all loads from tables perform a type check). +** +** The result of the analysis can be used to avoid the metatable check +** and the guard against HREF returning niltv. Both of these are cheap, +** so let's not spend too much effort on the analysis. +** +** A result of 1 is exact: previous value CANNOT be nil. +** A result of 0 is inexact: previous value MAY be nil. +*/ +int lj_opt_fwd_wasnonnil(jit_State *J, IROpT loadop, IRRef xref) +{ + /* First check stores. */ + IRRef ref = J->chain[loadop+IRDELTA_L2S]; + while (ref > xref) { + IRIns *store = IR(ref); + if (store->op1 == xref) { /* Same xREF. */ + /* A nil store MAY alias, but a non-nil store MUST alias. */ + return !irt_isnil(store->t); + } else if (irt_isnil(store->t)) { /* Must check any nil store. */ + IRRef skref = IR(store->op1)->op2; + IRRef xkref = IR(xref)->op2; + /* Same key type MAY alias. Need ALOAD check due to multiple int types. */ + if (loadop == IR_ALOAD || irt_sametype(IR(skref)->t, IR(xkref)->t)) { + if (skref == xkref || !irref_isk(skref) || !irref_isk(xkref)) + return 0; /* A nil store with same const key or var key MAY alias. */ + /* Different const keys CANNOT alias. */ + } /* Different key types CANNOT alias. */ + } /* Other non-nil stores MAY alias. */ + ref = store->prev; + } + + /* Check loads since nothing could be derived from stores. */ + ref = J->chain[loadop]; + while (ref > xref) { + IRIns *load = IR(ref); + if (load->op1 == xref) { /* Same xREF. */ + /* A nil load MAY alias, but a non-nil load MUST alias. */ + return !irt_isnil(load->t); + } /* Other non-nil loads MAY alias. */ + ref = load->prev; + } + return 0; /* Nothing derived at all, previous value MAY be nil. */ +} + +/* ------------------------------------------------------------------------ */ + +#undef IR +#undef fins +#undef fleft +#undef fright + +#endif diff --git a/external/lua/luajit/src/src/lj_opt_narrow.c b/external/lua/luajit/src/src/lj_opt_narrow.c new file mode 100644 index 0000000000..caf2a8df1c --- /dev/null +++ b/external/lua/luajit/src/src/lj_opt_narrow.c @@ -0,0 +1,648 @@ +/* +** NARROW: Narrowing of numbers to integers (double to int32_t). +** STRIPOV: Stripping of overflow checks. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_opt_narrow_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_bc.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_iropt.h" +#include "lj_trace.h" +#include "lj_vm.h" +#include "lj_strscan.h" + +/* Rationale for narrowing optimizations: +** +** Lua has only a single number type and this is a FP double by default. +** Narrowing doubles to integers does not pay off for the interpreter on a +** current-generation x86/x64 machine. Most FP operations need the same +** amount of execution resources as their integer counterparts, except +** with slightly longer latencies. Longer latencies are a non-issue for +** the interpreter, since they are usually hidden by other overhead. +** +** The total CPU execution bandwidth is the sum of the bandwidth of the FP +** and the integer units, because they execute in parallel. The FP units +** have an equal or higher bandwidth than the integer units. Not using +** them means losing execution bandwidth. Moving work away from them to +** the already quite busy integer units is a losing proposition. +** +** The situation for JIT-compiled code is a bit different: the higher code +** density makes the extra latencies much more visible. Tight loops expose +** the latencies for updating the induction variables. Array indexing +** requires narrowing conversions with high latencies and additional +** guards (to check that the index is really an integer). And many common +** optimizations only work on integers. +** +** One solution would be speculative, eager narrowing of all number loads. +** This causes many problems, like losing -0 or the need to resolve type +** mismatches between traces. It also effectively forces the integer type +** to have overflow-checking semantics. This impedes many basic +** optimizations and requires adding overflow checks to all integer +** arithmetic operations (whereas FP arithmetics can do without). +** +** Always replacing an FP op with an integer op plus an overflow check is +** counter-productive on a current-generation super-scalar CPU. Although +** the overflow check branches are highly predictable, they will clog the +** execution port for the branch unit and tie up reorder buffers. This is +** turning a pure data-flow dependency into a different data-flow +** dependency (with slightly lower latency) *plus* a control dependency. +** In general, you don't want to do this since latencies due to data-flow +** dependencies can be well hidden by out-of-order execution. +** +** A better solution is to keep all numbers as FP values and only narrow +** when it's beneficial to do so. LuaJIT uses predictive narrowing for +** induction variables and demand-driven narrowing for index expressions, +** integer arguments and bit operations. Additionally it can eliminate or +** hoist most of the resulting overflow checks. Regular arithmetic +** computations are never narrowed to integers. +** +** The integer type in the IR has convenient wrap-around semantics and +** ignores overflow. Extra operations have been added for +** overflow-checking arithmetic (ADDOV/SUBOV) instead of an extra type. +** Apart from reducing overall complexity of the compiler, this also +** nicely solves the problem where you want to apply algebraic +** simplifications to ADD, but not to ADDOV. And the x86/x64 assembler can +** use lea instead of an add for integer ADD, but not for ADDOV (lea does +** not affect the flags, but it helps to avoid register moves). +** +** +** All of the above has to be reconsidered for architectures with slow FP +** operations or without a hardware FPU. The dual-number mode of LuaJIT +** addresses this issue. Arithmetic operations are performed on integers +** as far as possible and overflow checks are added as needed. +** +** This implies that narrowing for integer arguments and bit operations +** should also strip overflow checks, e.g. replace ADDOV with ADD. The +** original overflow guards are weak and can be eliminated by DCE, if +** there's no other use. +** +** A slight twist is that it's usually beneficial to use overflow-checked +** integer arithmetics if all inputs are already integers. This is the only +** change that affects the single-number mode, too. +*/ + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) +#define fins (&J->fold.ins) + +/* Pass IR on to next optimization in chain (FOLD). */ +#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) + +#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) + +/* -- Elimination of narrowing type conversions --------------------------- */ + +/* Narrowing of index expressions and bit operations is demand-driven. The +** trace recorder emits a narrowing type conversion (CONV.int.num or TOBIT) +** in all of these cases (e.g. array indexing or string indexing). FOLD +** already takes care of eliminating simple redundant conversions like +** CONV.int.num(CONV.num.int(x)) ==> x. +** +** But the surrounding code is FP-heavy and arithmetic operations are +** performed on FP numbers (for the single-number mode). Consider a common +** example such as 'x=t[i+1]', with 'i' already an integer (due to induction +** variable narrowing). The index expression would be recorded as +** CONV.int.num(ADD(CONV.num.int(i), 1)) +** which is clearly suboptimal. +** +** One can do better by recursively backpropagating the narrowing type +** conversion across FP arithmetic operations. This turns FP ops into +** their corresponding integer counterparts. Depending on the semantics of +** the conversion they also need to check for overflow. Currently only ADD +** and SUB are supported. +** +** The above example can be rewritten as +** ADDOV(CONV.int.num(CONV.num.int(i)), 1) +** and then into ADDOV(i, 1) after folding of the conversions. The original +** FP ops remain in the IR and are eliminated by DCE since all references to +** them are gone. +** +** [In dual-number mode the trace recorder already emits ADDOV etc., but +** this can be further reduced. See below.] +** +** Special care has to be taken to avoid narrowing across an operation +** which is potentially operating on non-integral operands. One obvious +** case is when an expression contains a non-integral constant, but ends +** up as an integer index at runtime (like t[x+1.5] with x=0.5). +** +** Operations with two non-constant operands illustrate a similar problem +** (like t[a+b] with a=1.5 and b=2.5). Backpropagation has to stop there, +** unless it can be proven that either operand is integral (e.g. by CSEing +** a previous conversion). As a not-so-obvious corollary this logic also +** applies for a whole expression tree (e.g. t[(a+1)+(b+1)]). +** +** Correctness of the transformation is guaranteed by avoiding to expand +** the tree by adding more conversions than the one we would need to emit +** if not backpropagating. TOBIT employs a more optimistic rule, because +** the conversion has special semantics, designed to make the life of the +** compiler writer easier. ;-) +** +** Using on-the-fly backpropagation of an expression tree doesn't work +** because it's unknown whether the transform is correct until the end. +** This either requires IR rollback and cache invalidation for every +** subtree or a two-pass algorithm. The former didn't work out too well, +** so the code now combines a recursive collector with a stack-based +** emitter. +** +** [A recursive backpropagation algorithm with backtracking, employing +** skip-list lookup and round-robin caching, emitting stack operations +** on-the-fly for a stack-based interpreter -- and all of that in a meager +** kilobyte? Yep, compilers are a great treasure chest. Throw away your +** textbooks and read the codebase of a compiler today!] +** +** There's another optimization opportunity for array indexing: it's +** always accompanied by an array bounds-check. The outermost overflow +** check may be delegated to the ABC operation. This works because ABC is +** an unsigned comparison and wrap-around due to overflow creates negative +** numbers. +** +** But this optimization is only valid for constants that cannot overflow +** an int32_t into the range of valid array indexes [0..2^27+1). A check +** for +-2^30 is safe since -2^31 - 2^30 wraps to 2^30 and 2^31-1 + 2^30 +** wraps to -2^30-1. +** +** It's also good enough in practice, since e.g. t[i+1] or t[i-10] are +** quite common. So the above example finally ends up as ADD(i, 1)! +** +** Later on, the assembler is able to fuse the whole array reference and +** the ADD into the memory operands of loads and other instructions. This +** is why LuaJIT is able to generate very pretty (and fast) machine code +** for array indexing. And that, my dear, concludes another story about +** one of the hidden secrets of LuaJIT ... +*/ + +/* Maximum backpropagation depth and maximum stack size. */ +#define NARROW_MAX_BACKPROP 100 +#define NARROW_MAX_STACK 256 + +/* The stack machine has a 32 bit instruction format: [IROpT | IRRef1] +** The lower 16 bits hold a reference (or 0). The upper 16 bits hold +** the IR opcode + type or one of the following special opcodes: +*/ +enum { + NARROW_REF, /* Push ref. */ + NARROW_CONV, /* Push conversion of ref. */ + NARROW_SEXT, /* Push sign-extension of ref. */ + NARROW_INT /* Push KINT ref. The next code holds an int32_t. */ +}; + +typedef uint32_t NarrowIns; + +#define NARROWINS(op, ref) (((op) << 16) + (ref)) +#define narrow_op(ins) ((IROpT)((ins) >> 16)) +#define narrow_ref(ins) ((IRRef1)(ins)) + +/* Context used for narrowing of type conversions. */ +typedef struct NarrowConv { + jit_State *J; /* JIT compiler state. */ + NarrowIns *sp; /* Current stack pointer. */ + NarrowIns *maxsp; /* Maximum stack pointer minus redzone. */ + int lim; /* Limit on the number of emitted conversions. */ + IRRef mode; /* Conversion mode (IRCONV_*). */ + IRType t; /* Destination type: IRT_INT or IRT_I64. */ + NarrowIns stack[NARROW_MAX_STACK]; /* Stack holding stack-machine code. */ +} NarrowConv; + +/* Lookup a reference in the backpropagation cache. */ +static BPropEntry *narrow_bpc_get(jit_State *J, IRRef1 key, IRRef mode) +{ + ptrdiff_t i; + for (i = 0; i < BPROP_SLOTS; i++) { + BPropEntry *bp = &J->bpropcache[i]; + /* Stronger checks are ok, too. */ + if (bp->key == key && bp->mode >= mode && + ((bp->mode ^ mode) & IRCONV_MODEMASK) == 0) + return bp; + } + return NULL; +} + +/* Add an entry to the backpropagation cache. */ +static void narrow_bpc_set(jit_State *J, IRRef1 key, IRRef1 val, IRRef mode) +{ + uint32_t slot = J->bpropslot; + BPropEntry *bp = &J->bpropcache[slot]; + J->bpropslot = (slot + 1) & (BPROP_SLOTS-1); + bp->key = key; + bp->val = val; + bp->mode = mode; +} + +/* Backpropagate overflow stripping. */ +static void narrow_stripov_backprop(NarrowConv *nc, IRRef ref, int depth) +{ + jit_State *J = nc->J; + IRIns *ir = IR(ref); + if (ir->o == IR_ADDOV || ir->o == IR_SUBOV || + (ir->o == IR_MULOV && (nc->mode & IRCONV_CONVMASK) == IRCONV_ANY)) { + BPropEntry *bp = narrow_bpc_get(nc->J, ref, IRCONV_TOBIT); + if (bp) { + ref = bp->val; + } else if (++depth < NARROW_MAX_BACKPROP && nc->sp < nc->maxsp) { + narrow_stripov_backprop(nc, ir->op1, depth); + narrow_stripov_backprop(nc, ir->op2, depth); + *nc->sp++ = NARROWINS(IRT(ir->o - IR_ADDOV + IR_ADD, IRT_INT), ref); + return; + } + } + *nc->sp++ = NARROWINS(NARROW_REF, ref); +} + +/* Backpropagate narrowing conversion. Return number of needed conversions. */ +static int narrow_conv_backprop(NarrowConv *nc, IRRef ref, int depth) +{ + jit_State *J = nc->J; + IRIns *ir = IR(ref); + IRRef cref; + + /* Check the easy cases first. */ + if (ir->o == IR_CONV && (ir->op2 & IRCONV_SRCMASK) == IRT_INT) { + if ((nc->mode & IRCONV_CONVMASK) <= IRCONV_ANY) + narrow_stripov_backprop(nc, ir->op1, depth+1); + else + *nc->sp++ = NARROWINS(NARROW_REF, ir->op1); /* Undo conversion. */ + if (nc->t == IRT_I64) + *nc->sp++ = NARROWINS(NARROW_SEXT, 0); /* Sign-extend integer. */ + return 0; + } else if (ir->o == IR_KNUM) { /* Narrow FP constant. */ + lua_Number n = ir_knum(ir)->n; + if ((nc->mode & IRCONV_CONVMASK) == IRCONV_TOBIT) { + /* Allows a wider range of constants. */ + int64_t k64 = (int64_t)n; + if (n == (lua_Number)k64) { /* Only if const doesn't lose precision. */ + *nc->sp++ = NARROWINS(NARROW_INT, 0); + *nc->sp++ = (NarrowIns)k64; /* But always truncate to 32 bits. */ + return 0; + } + } else { + int32_t k = lj_num2int(n); + /* Only if constant is a small integer. */ + if (checki16(k) && n == (lua_Number)k) { + *nc->sp++ = NARROWINS(NARROW_INT, 0); + *nc->sp++ = (NarrowIns)k; + return 0; + } + } + return 10; /* Never narrow other FP constants (this is rare). */ + } + + /* Try to CSE the conversion. Stronger checks are ok, too. */ + cref = J->chain[fins->o]; + while (cref > ref) { + IRIns *cr = IR(cref); + if (cr->op1 == ref && + (fins->o == IR_TOBIT || + ((cr->op2 & IRCONV_MODEMASK) == (nc->mode & IRCONV_MODEMASK) && + irt_isguard(cr->t) >= irt_isguard(fins->t)))) { + *nc->sp++ = NARROWINS(NARROW_REF, cref); + return 0; /* Already there, no additional conversion needed. */ + } + cref = cr->prev; + } + + /* Backpropagate across ADD/SUB. */ + if (ir->o == IR_ADD || ir->o == IR_SUB) { + /* Try cache lookup first. */ + IRRef mode = nc->mode; + BPropEntry *bp; + /* Inner conversions need a stronger check. */ + if ((mode & IRCONV_CONVMASK) == IRCONV_INDEX && depth > 0) + mode += IRCONV_CHECK-IRCONV_INDEX; + bp = narrow_bpc_get(nc->J, (IRRef1)ref, mode); + if (bp) { + *nc->sp++ = NARROWINS(NARROW_REF, bp->val); + return 0; + } else if (nc->t == IRT_I64) { + /* Try sign-extending from an existing (checked) conversion to int. */ + mode = (IRT_INT<<5)|IRT_NUM|IRCONV_INDEX; + bp = narrow_bpc_get(nc->J, (IRRef1)ref, mode); + if (bp) { + *nc->sp++ = NARROWINS(NARROW_REF, bp->val); + *nc->sp++ = NARROWINS(NARROW_SEXT, 0); + return 0; + } + } + if (++depth < NARROW_MAX_BACKPROP && nc->sp < nc->maxsp) { + NarrowIns *savesp = nc->sp; + int count = narrow_conv_backprop(nc, ir->op1, depth); + count += narrow_conv_backprop(nc, ir->op2, depth); + if (count <= nc->lim) { /* Limit total number of conversions. */ + *nc->sp++ = NARROWINS(IRT(ir->o, nc->t), ref); + return count; + } + nc->sp = savesp; /* Too many conversions, need to backtrack. */ + } + } + + /* Otherwise add a conversion. */ + *nc->sp++ = NARROWINS(NARROW_CONV, ref); + return 1; +} + +/* Emit the conversions collected during backpropagation. */ +static IRRef narrow_conv_emit(jit_State *J, NarrowConv *nc) +{ + /* The fins fields must be saved now -- emitir() overwrites them. */ + IROpT guardot = irt_isguard(fins->t) ? IRTG(IR_ADDOV-IR_ADD, 0) : 0; + IROpT convot = fins->ot; + IRRef1 convop2 = fins->op2; + NarrowIns *next = nc->stack; /* List of instructions from backpropagation. */ + NarrowIns *last = nc->sp; + NarrowIns *sp = nc->stack; /* Recycle the stack to store operands. */ + while (next < last) { /* Simple stack machine to process the ins. list. */ + NarrowIns ref = *next++; + IROpT op = narrow_op(ref); + if (op == NARROW_REF) { + *sp++ = ref; + } else if (op == NARROW_CONV) { + *sp++ = emitir_raw(convot, ref, convop2); /* Raw emit avoids a loop. */ + } else if (op == NARROW_SEXT) { + lua_assert(sp >= nc->stack+1); + sp[-1] = emitir(IRT(IR_CONV, IRT_I64), sp[-1], + (IRT_I64<<5)|IRT_INT|IRCONV_SEXT); + } else if (op == NARROW_INT) { + lua_assert(next < last); + *sp++ = nc->t == IRT_I64 ? + lj_ir_kint64(J, (int64_t)(int32_t)*next++) : + lj_ir_kint(J, *next++); + } else { /* Regular IROpT. Pops two operands and pushes one result. */ + IRRef mode = nc->mode; + lua_assert(sp >= nc->stack+2); + sp--; + /* Omit some overflow checks for array indexing. See comments above. */ + if ((mode & IRCONV_CONVMASK) == IRCONV_INDEX) { + if (next == last && irref_isk(narrow_ref(sp[0])) && + (uint32_t)IR(narrow_ref(sp[0]))->i + 0x40000000u < 0x80000000u) + guardot = 0; + else /* Otherwise cache a stronger check. */ + mode += IRCONV_CHECK-IRCONV_INDEX; + } + sp[-1] = emitir(op+guardot, sp[-1], sp[0]); + /* Add to cache. */ + if (narrow_ref(ref)) + narrow_bpc_set(J, narrow_ref(ref), narrow_ref(sp[-1]), mode); + } + } + lua_assert(sp == nc->stack+1); + return nc->stack[0]; +} + +/* Narrow a type conversion of an arithmetic operation. */ +TRef LJ_FASTCALL lj_opt_narrow_convert(jit_State *J) +{ + if ((J->flags & JIT_F_OPT_NARROW)) { + NarrowConv nc; + nc.J = J; + nc.sp = nc.stack; + nc.maxsp = &nc.stack[NARROW_MAX_STACK-4]; + nc.t = irt_type(fins->t); + if (fins->o == IR_TOBIT) { + nc.mode = IRCONV_TOBIT; /* Used only in the backpropagation cache. */ + nc.lim = 2; /* TOBIT can use a more optimistic rule. */ + } else { + nc.mode = fins->op2; + nc.lim = 1; + } + if (narrow_conv_backprop(&nc, fins->op1, 0) <= nc.lim) + return narrow_conv_emit(J, &nc); + } + return NEXTFOLD; +} + +/* -- Narrowing of implicit conversions ----------------------------------- */ + +/* Recursively strip overflow checks. */ +static TRef narrow_stripov(jit_State *J, TRef tr, int lastop, IRRef mode) +{ + IRRef ref = tref_ref(tr); + IRIns *ir = IR(ref); + int op = ir->o; + if (op >= IR_ADDOV && op <= lastop) { + BPropEntry *bp = narrow_bpc_get(J, ref, mode); + if (bp) { + return TREF(bp->val, irt_t(IR(bp->val)->t)); + } else { + IRRef op1 = ir->op1, op2 = ir->op2; /* The IR may be reallocated. */ + op1 = narrow_stripov(J, op1, lastop, mode); + op2 = narrow_stripov(J, op2, lastop, mode); + tr = emitir(IRT(op - IR_ADDOV + IR_ADD, + ((mode & IRCONV_DSTMASK) >> IRCONV_DSH)), op1, op2); + narrow_bpc_set(J, ref, tref_ref(tr), mode); + } + } else if (LJ_64 && (mode & IRCONV_SEXT) && !irt_is64(ir->t)) { + tr = emitir(IRT(IR_CONV, IRT_INTP), tr, mode); + } + return tr; +} + +/* Narrow array index. */ +TRef LJ_FASTCALL lj_opt_narrow_index(jit_State *J, TRef tr) +{ + IRIns *ir; + lua_assert(tref_isnumber(tr)); + if (tref_isnum(tr)) /* Conversion may be narrowed, too. See above. */ + return emitir(IRTGI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_INDEX); + /* Omit some overflow checks for array indexing. See comments above. */ + ir = IR(tref_ref(tr)); + if ((ir->o == IR_ADDOV || ir->o == IR_SUBOV) && irref_isk(ir->op2) && + (uint32_t)IR(ir->op2)->i + 0x40000000u < 0x80000000u) + return emitir(IRTI(ir->o - IR_ADDOV + IR_ADD), ir->op1, ir->op2); + return tr; +} + +/* Narrow conversion to integer operand (overflow undefined). */ +TRef LJ_FASTCALL lj_opt_narrow_toint(jit_State *J, TRef tr) +{ + if (tref_isstr(tr)) + tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); + if (tref_isnum(tr)) /* Conversion may be narrowed, too. See above. */ + return emitir(IRTI(IR_CONV), tr, IRCONV_INT_NUM|IRCONV_ANY); + if (!tref_isinteger(tr)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + /* + ** Undefined overflow semantics allow stripping of ADDOV, SUBOV and MULOV. + ** Use IRCONV_TOBIT for the cache entries, since the semantics are the same. + */ + return narrow_stripov(J, tr, IR_MULOV, (IRT_INT<<5)|IRT_INT|IRCONV_TOBIT); +} + +/* Narrow conversion to bitop operand (overflow wrapped). */ +TRef LJ_FASTCALL lj_opt_narrow_tobit(jit_State *J, TRef tr) +{ + if (tref_isstr(tr)) + tr = emitir(IRTG(IR_STRTO, IRT_NUM), tr, 0); + if (tref_isnum(tr)) /* Conversion may be narrowed, too. See above. */ + return emitir(IRTI(IR_TOBIT), tr, lj_ir_knum_tobit(J)); + if (!tref_isinteger(tr)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + /* + ** Wrapped overflow semantics allow stripping of ADDOV and SUBOV. + ** MULOV cannot be stripped due to precision widening. + */ + return narrow_stripov(J, tr, IR_SUBOV, (IRT_INT<<5)|IRT_INT|IRCONV_TOBIT); +} + +#if LJ_HASFFI +/* Narrow C array index (overflow undefined). */ +TRef LJ_FASTCALL lj_opt_narrow_cindex(jit_State *J, TRef tr) +{ + lua_assert(tref_isnumber(tr)); + if (tref_isnum(tr)) + return emitir(IRT(IR_CONV, IRT_INTP), tr, + (IRT_INTP<<5)|IRT_NUM|IRCONV_TRUNC|IRCONV_ANY); + /* Undefined overflow semantics allow stripping of ADDOV, SUBOV and MULOV. */ + return narrow_stripov(J, tr, IR_MULOV, + LJ_64 ? ((IRT_INTP<<5)|IRT_INT|IRCONV_SEXT) : + ((IRT_INTP<<5)|IRT_INT|IRCONV_TOBIT)); +} +#endif + +/* -- Narrowing of arithmetic operators ----------------------------------- */ + +/* Check whether a number fits into an int32_t (-0 is ok, too). */ +static int numisint(lua_Number n) +{ + return (n == (lua_Number)lj_num2int(n)); +} + +/* Narrowing of arithmetic operations. */ +TRef lj_opt_narrow_arith(jit_State *J, TRef rb, TRef rc, + TValue *vb, TValue *vc, IROp op) +{ + if (tref_isstr(rb)) { + rb = emitir(IRTG(IR_STRTO, IRT_NUM), rb, 0); + lj_strscan_num(strV(vb), vb); + } + if (tref_isstr(rc)) { + rc = emitir(IRTG(IR_STRTO, IRT_NUM), rc, 0); + lj_strscan_num(strV(vc), vc); + } + /* Must not narrow MUL in non-DUALNUM variant, because it loses -0. */ + if ((op >= IR_ADD && op <= (LJ_DUALNUM ? IR_MUL : IR_SUB)) && + tref_isinteger(rb) && tref_isinteger(rc) && + numisint(lj_vm_foldarith(numberVnum(vb), numberVnum(vc), + (int)op - (int)IR_ADD))) + return emitir(IRTGI((int)op - (int)IR_ADD + (int)IR_ADDOV), rb, rc); + if (!tref_isnum(rb)) rb = emitir(IRTN(IR_CONV), rb, IRCONV_NUM_INT); + if (!tref_isnum(rc)) rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); + return emitir(IRTN(op), rb, rc); +} + +/* Narrowing of unary minus operator. */ +TRef lj_opt_narrow_unm(jit_State *J, TRef rc, TValue *vc) +{ + if (tref_isstr(rc)) { + rc = emitir(IRTG(IR_STRTO, IRT_NUM), rc, 0); + lj_strscan_num(strV(vc), vc); + } + if (tref_isinteger(rc)) { + if ((uint32_t)numberVint(vc) != 0x80000000u) + return emitir(IRTGI(IR_SUBOV), lj_ir_kint(J, 0), rc); + rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); + } + return emitir(IRTN(IR_NEG), rc, lj_ir_knum_neg(J)); +} + +/* Narrowing of modulo operator. */ +TRef lj_opt_narrow_mod(jit_State *J, TRef rb, TRef rc, TValue *vc) +{ + TRef tmp; + if (tvisstr(vc) && !lj_strscan_num(strV(vc), vc)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + if ((LJ_DUALNUM || (J->flags & JIT_F_OPT_NARROW)) && + tref_isinteger(rb) && tref_isinteger(rc) && + (tvisint(vc) ? intV(vc) != 0 : !tviszero(vc))) { + emitir(IRTGI(IR_NE), rc, lj_ir_kint(J, 0)); + return emitir(IRTI(IR_MOD), rb, rc); + } + /* b % c ==> b - floor(b/c)*c */ + rb = lj_ir_tonum(J, rb); + rc = lj_ir_tonum(J, rc); + tmp = emitir(IRTN(IR_DIV), rb, rc); + tmp = emitir(IRTN(IR_FPMATH), tmp, IRFPM_FLOOR); + tmp = emitir(IRTN(IR_MUL), tmp, rc); + return emitir(IRTN(IR_SUB), rb, tmp); +} + +/* Narrowing of power operator or math.pow. */ +TRef lj_opt_narrow_pow(jit_State *J, TRef rb, TRef rc, TValue *vc) +{ + if (tvisstr(vc) && !lj_strscan_num(strV(vc), vc)) + lj_trace_err(J, LJ_TRERR_BADTYPE); + /* Narrowing must be unconditional to preserve (-x)^i semantics. */ + if (tvisint(vc) || numisint(numV(vc))) { + int checkrange = 0; + /* Split pow is faster for bigger exponents. But do this only for (+k)^i. */ + if (tref_isk(rb) && (int32_t)ir_knum(IR(tref_ref(rb)))->u32.hi >= 0) { + int32_t k = numberVint(vc); + if (!(k >= -65536 && k <= 65536)) goto split_pow; + checkrange = 1; + } + if (!tref_isinteger(rc)) { + if (tref_isstr(rc)) + rc = emitir(IRTG(IR_STRTO, IRT_NUM), rc, 0); + /* Guarded conversion to integer! */ + rc = emitir(IRTGI(IR_CONV), rc, IRCONV_INT_NUM|IRCONV_CHECK); + } + if (checkrange && !tref_isk(rc)) { /* Range guard: -65536 <= i <= 65536 */ + TRef tmp = emitir(IRTI(IR_ADD), rc, lj_ir_kint(J, 65536)); + emitir(IRTGI(IR_ULE), tmp, lj_ir_kint(J, 2*65536)); + } + return emitir(IRTN(IR_POW), rb, rc); + } +split_pow: + /* FOLD covers most cases, but some are easier to do here. */ + if (tref_isk(rb) && tvispone(ir_knum(IR(tref_ref(rb))))) + return rb; /* 1 ^ x ==> 1 */ + rc = lj_ir_tonum(J, rc); + if (tref_isk(rc) && ir_knum(IR(tref_ref(rc)))->n == 0.5) + return emitir(IRTN(IR_FPMATH), rb, IRFPM_SQRT); /* x ^ 0.5 ==> sqrt(x) */ + /* Split up b^c into exp2(c*log2(b)). Assembler may rejoin later. */ + rb = emitir(IRTN(IR_FPMATH), rb, IRFPM_LOG2); + rc = emitir(IRTN(IR_MUL), rb, rc); + return emitir(IRTN(IR_FPMATH), rc, IRFPM_EXP2); +} + +/* -- Predictive narrowing of induction variables ------------------------- */ + +/* Narrow a single runtime value. */ +static int narrow_forl(jit_State *J, cTValue *o) +{ + if (tvisint(o)) return 1; + if (LJ_DUALNUM || (J->flags & JIT_F_OPT_NARROW)) return numisint(numV(o)); + return 0; +} + +/* Narrow the FORL index type by looking at the runtime values. */ +IRType lj_opt_narrow_forl(jit_State *J, cTValue *tv) +{ + lua_assert(tvisnumber(&tv[FORL_IDX]) && + tvisnumber(&tv[FORL_STOP]) && + tvisnumber(&tv[FORL_STEP])); + /* Narrow only if the runtime values of start/stop/step are all integers. */ + if (narrow_forl(J, &tv[FORL_IDX]) && + narrow_forl(J, &tv[FORL_STOP]) && + narrow_forl(J, &tv[FORL_STEP])) { + /* And if the loop index can't possibly overflow. */ + lua_Number step = numberVnum(&tv[FORL_STEP]); + lua_Number sum = numberVnum(&tv[FORL_STOP]) + step; + if (0 <= step ? (sum <= 2147483647.0) : (sum >= -2147483648.0)) + return IRT_INT; + } + return IRT_NUM; +} + +#undef IR +#undef fins +#undef emitir +#undef emitir_raw + +#endif diff --git a/external/lua/luajit/src/src/lj_opt_sink.c b/external/lua/luajit/src/src/lj_opt_sink.c new file mode 100644 index 0000000000..56e463687e --- /dev/null +++ b/external/lua/luajit/src/src/lj_opt_sink.c @@ -0,0 +1,245 @@ +/* +** SINK: Allocation Sinking and Store Sinking. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_opt_sink_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_iropt.h" +#include "lj_target.h" + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) + +/* Check whether the store ref points to an eligible allocation. */ +static IRIns *sink_checkalloc(jit_State *J, IRIns *irs) +{ + IRIns *ir = IR(irs->op1); + if (!irref_isk(ir->op2)) + return NULL; /* Non-constant key. */ + if (ir->o == IR_HREFK || ir->o == IR_AREF) + ir = IR(ir->op1); + else if (!(ir->o == IR_HREF || ir->o == IR_NEWREF || + ir->o == IR_FREF || ir->o == IR_ADD)) + return NULL; /* Unhandled reference type (for XSTORE). */ + ir = IR(ir->op1); + if (!(ir->o == IR_TNEW || ir->o == IR_TDUP || ir->o == IR_CNEW)) + return NULL; /* Not an allocation. */ + return ir; /* Return allocation. */ +} + +/* Recursively check whether a value depends on a PHI. */ +static int sink_phidep(jit_State *J, IRRef ref) +{ + IRIns *ir = IR(ref); + if (irt_isphi(ir->t)) return 1; + if (ir->op1 >= REF_FIRST && sink_phidep(J, ir->op1)) return 1; + if (ir->op2 >= REF_FIRST && sink_phidep(J, ir->op2)) return 1; + return 0; +} + +/* Check whether a value is a sinkable PHI or loop-invariant. */ +static int sink_checkphi(jit_State *J, IRIns *ira, IRRef ref) +{ + if (ref >= REF_FIRST) { + IRIns *ir = IR(ref); + if (irt_isphi(ir->t) || (ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT && + irt_isphi(IR(ir->op1)->t))) { + ira->prev++; + return 1; /* Sinkable PHI. */ + } + /* Otherwise the value must be loop-invariant. */ + return ref < J->loopref && !sink_phidep(J, ref); + } + return 1; /* Constant (non-PHI). */ +} + +/* Mark non-sinkable allocations using single-pass backward propagation. +** +** Roots for the marking process are: +** - Some PHIs or snapshots (see below). +** - Non-PHI, non-constant values stored to PHI allocations. +** - All guards. +** - Any remaining loads not eliminated by store-to-load forwarding. +** - Stores with non-constant keys. +** - All stored values. +*/ +static void sink_mark_ins(jit_State *J) +{ + IRIns *ir, *irlast = IR(J->cur.nins-1); + for (ir = irlast ; ; ir--) { + switch (ir->o) { + case IR_BASE: + return; /* Finished. */ + case IR_CALLL: /* IRCALL_lj_tab_len */ + case IR_ALOAD: case IR_HLOAD: case IR_XLOAD: case IR_TBAR: + irt_setmark(IR(ir->op1)->t); /* Mark ref for remaining loads. */ + break; + case IR_FLOAD: + if (irt_ismarked(ir->t) || ir->op2 == IRFL_TAB_META) + irt_setmark(IR(ir->op1)->t); /* Mark table for remaining loads. */ + break; + case IR_ASTORE: case IR_HSTORE: case IR_FSTORE: case IR_XSTORE: { + IRIns *ira = sink_checkalloc(J, ir); + if (!ira || (irt_isphi(ira->t) && !sink_checkphi(J, ira, ir->op2))) + irt_setmark(IR(ir->op1)->t); /* Mark ineligible ref. */ + irt_setmark(IR(ir->op2)->t); /* Mark stored value. */ + break; + } +#if LJ_HASFFI + case IR_CNEWI: + if (irt_isphi(ir->t) && + (!sink_checkphi(J, ir, ir->op2) || + (LJ_32 && ir+1 < irlast && (ir+1)->o == IR_HIOP && + !sink_checkphi(J, ir, (ir+1)->op2)))) + irt_setmark(ir->t); /* Mark ineligible allocation. */ + /* fallthrough */ +#endif + case IR_USTORE: + irt_setmark(IR(ir->op2)->t); /* Mark stored value. */ + break; +#if LJ_HASFFI + case IR_CALLXS: +#endif + case IR_CALLS: + irt_setmark(IR(ir->op1)->t); /* Mark (potentially) stored values. */ + break; + case IR_PHI: { + IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); + irl->prev = irr->prev = 0; /* Clear PHI value counts. */ + if (irl->o == irr->o && + (irl->o == IR_TNEW || irl->o == IR_TDUP || + (LJ_HASFFI && (irl->o == IR_CNEW || irl->o == IR_CNEWI)))) + break; + irt_setmark(irl->t); + irt_setmark(irr->t); + break; + } + default: + if (irt_ismarked(ir->t) || irt_isguard(ir->t)) { /* Propagate mark. */ + if (ir->op1 >= REF_FIRST) irt_setmark(IR(ir->op1)->t); + if (ir->op2 >= REF_FIRST) irt_setmark(IR(ir->op2)->t); + } + break; + } + } +} + +/* Mark all instructions referenced by a snapshot. */ +static void sink_mark_snap(jit_State *J, SnapShot *snap) +{ + SnapEntry *map = &J->cur.snapmap[snap->mapofs]; + MSize n, nent = snap->nent; + for (n = 0; n < nent; n++) { + IRRef ref = snap_ref(map[n]); + if (!irref_isk(ref)) + irt_setmark(IR(ref)->t); + } +} + +/* Iteratively remark PHI refs with differing marks or PHI value counts. */ +static void sink_remark_phi(jit_State *J) +{ + IRIns *ir; + int remark; + do { + remark = 0; + for (ir = IR(J->cur.nins-1); ir->o == IR_PHI; ir--) { + IRIns *irl = IR(ir->op1), *irr = IR(ir->op2); + if (((irl->t.irt ^ irr->t.irt) & IRT_MARK)) + remark = 1; + else if (irl->prev == irr->prev) + continue; + irt_setmark(IR(ir->op1)->t); + irt_setmark(IR(ir->op2)->t); + } + } while (remark); +} + +/* Sweep instructions and tag sunken allocations and stores. */ +static void sink_sweep_ins(jit_State *J) +{ + IRIns *ir, *irfirst = IR(J->cur.nk); + for (ir = IR(J->cur.nins-1) ; ir >= irfirst; ir--) { + switch (ir->o) { + case IR_ASTORE: case IR_HSTORE: case IR_FSTORE: case IR_XSTORE: { + IRIns *ira = sink_checkalloc(J, ir); + if (ira && !irt_ismarked(ira->t)) { + int delta = (int)(ir - ira); + ir->prev = REGSP(RID_SINK, delta > 255 ? 255 : delta); + } else { + ir->prev = REGSP_INIT; + } + break; + } + case IR_NEWREF: + if (!irt_ismarked(IR(ir->op1)->t)) { + ir->prev = REGSP(RID_SINK, 0); + } else { + irt_clearmark(ir->t); + ir->prev = REGSP_INIT; + } + break; +#if LJ_HASFFI + case IR_CNEW: case IR_CNEWI: +#endif + case IR_TNEW: case IR_TDUP: + if (!irt_ismarked(ir->t)) { + ir->t.irt &= ~IRT_GUARD; + ir->prev = REGSP(RID_SINK, 0); + J->cur.sinktags = 1; /* Signal present SINK tags to assembler. */ + } else { + irt_clearmark(ir->t); + ir->prev = REGSP_INIT; + } + break; + case IR_PHI: { + IRIns *ira = IR(ir->op2); + if (!irt_ismarked(ira->t) && + (ira->o == IR_TNEW || ira->o == IR_TDUP || + (LJ_HASFFI && (ira->o == IR_CNEW || ira->o == IR_CNEWI)))) { + ir->prev = REGSP(RID_SINK, 0); + } else { + ir->prev = REGSP_INIT; + } + break; + } + default: + irt_clearmark(ir->t); + ir->prev = REGSP_INIT; + break; + } + } +} + +/* Allocation sinking and store sinking. +** +** 1. Mark all non-sinkable allocations. +** 2. Then sink all remaining allocations and the related stores. +*/ +void lj_opt_sink(jit_State *J) +{ + const uint32_t need = (JIT_F_OPT_SINK|JIT_F_OPT_FWD| + JIT_F_OPT_DCE|JIT_F_OPT_CSE|JIT_F_OPT_FOLD); + if ((J->flags & need) == need && + (J->chain[IR_TNEW] || J->chain[IR_TDUP] || + (LJ_HASFFI && (J->chain[IR_CNEW] || J->chain[IR_CNEWI])))) { + if (!J->loopref) + sink_mark_snap(J, &J->cur.snap[J->cur.nsnap-1]); + sink_mark_ins(J); + if (J->loopref) + sink_remark_phi(J); + sink_sweep_ins(J); + } +} + +#undef IR + +#endif diff --git a/external/lua/luajit/src/src/lj_opt_split.c b/external/lua/luajit/src/src/lj_opt_split.c new file mode 100644 index 0000000000..303af03c68 --- /dev/null +++ b/external/lua/luajit/src/src/lj_opt_split.c @@ -0,0 +1,723 @@ +/* +** SPLIT: Split 64 bit IR instructions into 32 bit IR instructions. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_opt_split_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT && (LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) + +#include "lj_err.h" +#include "lj_str.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_ircall.h" +#include "lj_iropt.h" +#include "lj_vm.h" + +/* SPLIT pass: +** +** This pass splits up 64 bit IR instructions into multiple 32 bit IR +** instructions. It's only active for soft-float targets or for 32 bit CPUs +** which lack native 64 bit integer operations (the FFI is currently the +** only emitter for 64 bit integer instructions). +** +** Splitting the IR in a separate pass keeps each 32 bit IR assembler +** backend simple. Only a small amount of extra functionality needs to be +** implemented. This is much easier than adding support for allocating +** register pairs to each backend (believe me, I tried). A few simple, but +** important optimizations can be performed by the SPLIT pass, which would +** be tedious to do in the backend. +** +** The basic idea is to replace each 64 bit IR instruction with its 32 bit +** equivalent plus an extra HIOP instruction. The splitted IR is not passed +** through FOLD or any other optimizations, so each HIOP is guaranteed to +** immediately follow it's counterpart. The actual functionality of HIOP is +** inferred from the previous instruction. +** +** The operands of HIOP hold the hiword input references. The output of HIOP +** is the hiword output reference, which is also used to hold the hiword +** register or spill slot information. The register allocator treats this +** instruction independently of any other instruction, which improves code +** quality compared to using fixed register pairs. +** +** It's easier to split up some instructions into two regular 32 bit +** instructions. E.g. XLOAD is split up into two XLOADs with two different +** addresses. Obviously 64 bit constants need to be split up into two 32 bit +** constants, too. Some hiword instructions can be entirely omitted, e.g. +** when zero-extending a 32 bit value to 64 bits. 64 bit arguments for calls +** are split up into two 32 bit arguments each. +** +** On soft-float targets, floating-point instructions are directly converted +** to soft-float calls by the SPLIT pass (except for comparisons and MIN/MAX). +** HIOP for number results has the type IRT_SOFTFP ("sfp" in -jdump). +** +** Here's the IR and x64 machine code for 'x.b = x.a + 1' for a struct with +** two int64_t fields: +** +** 0100 p32 ADD base +8 +** 0101 i64 XLOAD 0100 +** 0102 i64 ADD 0101 +1 +** 0103 p32 ADD base +16 +** 0104 i64 XSTORE 0103 0102 +** +** mov rax, [esi+0x8] +** add rax, +0x01 +** mov [esi+0x10], rax +** +** Here's the transformed IR and the x86 machine code after the SPLIT pass: +** +** 0100 p32 ADD base +8 +** 0101 int XLOAD 0100 +** 0102 p32 ADD base +12 +** 0103 int XLOAD 0102 +** 0104 int ADD 0101 +1 +** 0105 int HIOP 0103 +0 +** 0106 p32 ADD base +16 +** 0107 int XSTORE 0106 0104 +** 0108 int HIOP 0106 0105 +** +** mov eax, [esi+0x8] +** mov ecx, [esi+0xc] +** add eax, +0x01 +** adc ecx, +0x00 +** mov [esi+0x10], eax +** mov [esi+0x14], ecx +** +** You may notice the reassociated hiword address computation, which is +** later fused into the mov operands by the assembler. +*/ + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) + +/* Directly emit the transformed IR without updating chains etc. */ +static IRRef split_emit(jit_State *J, uint16_t ot, IRRef1 op1, IRRef1 op2) +{ + IRRef nref = lj_ir_nextins(J); + IRIns *ir = IR(nref); + ir->ot = ot; + ir->op1 = op1; + ir->op2 = op2; + return nref; +} + +#if LJ_SOFTFP +/* Emit a (checked) number to integer conversion. */ +static IRRef split_num2int(jit_State *J, IRRef lo, IRRef hi, int check) +{ + IRRef tmp, res; +#if LJ_LE + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), lo, hi); +#else + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hi, lo); +#endif + res = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_softfp_d2i); + if (check) { + tmp = split_emit(J, IRTI(IR_CALLN), res, IRCALL_softfp_i2d); + split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); + split_emit(J, IRTGI(IR_EQ), tmp, lo); + split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP), tmp+1, hi); + } + return res; +} + +/* Emit a CALLN with one split 64 bit argument. */ +static IRRef split_call_l(jit_State *J, IRRef1 *hisubst, IRIns *oir, + IRIns *ir, IRCallID id) +{ + IRRef tmp, op1 = ir->op1; + J->cur.nins--; +#if LJ_LE + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); +#else + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); +#endif + ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); + return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); +} + +/* Emit a CALLN with one split 64 bit argument and a 32 bit argument. */ +static IRRef split_call_li(jit_State *J, IRRef1 *hisubst, IRIns *oir, + IRIns *ir, IRCallID id) +{ + IRRef tmp, op1 = ir->op1, op2 = ir->op2; + J->cur.nins--; +#if LJ_LE + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); +#else + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); +#endif + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev); + ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); + return split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); +} +#endif + +/* Emit a CALLN with two split 64 bit arguments. */ +static IRRef split_call_ll(jit_State *J, IRRef1 *hisubst, IRIns *oir, + IRIns *ir, IRCallID id) +{ + IRRef tmp, op1 = ir->op1, op2 = ir->op2; + J->cur.nins--; +#if LJ_LE + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev); + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]); +#else + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, hisubst[op2]); + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, oir[op2].prev); +#endif + ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, id); + return split_emit(J, + IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT), + tmp, tmp); +} + +/* Get a pointer to the other 32 bit word (LE: hiword, BE: loword). */ +static IRRef split_ptr(jit_State *J, IRIns *oir, IRRef ref) +{ + IRRef nref = oir[ref].prev; + IRIns *ir = IR(nref); + int32_t ofs = 4; + if (ir->o == IR_KPTR) + return lj_ir_kptr(J, (char *)ir_kptr(ir) + ofs); + if (ir->o == IR_ADD && irref_isk(ir->op2) && !irt_isphi(oir[ref].t)) { + /* Reassociate address. */ + ofs += IR(ir->op2)->i; + nref = ir->op1; + if (ofs == 0) return nref; + } + return split_emit(J, IRTI(IR_ADD), nref, lj_ir_kint(J, ofs)); +} + +/* Transform the old IR to the new IR. */ +static void split_ir(jit_State *J) +{ + IRRef nins = J->cur.nins, nk = J->cur.nk; + MSize irlen = nins - nk; + MSize need = (irlen+1)*(sizeof(IRIns) + sizeof(IRRef1)); + IRIns *oir = (IRIns *)lj_str_needbuf(J->L, &G(J->L)->tmpbuf, need); + IRRef1 *hisubst; + IRRef ref; + + /* Copy old IR to buffer. */ + memcpy(oir, IR(nk), irlen*sizeof(IRIns)); + /* Bias hiword substitution table and old IR. Loword kept in field prev. */ + hisubst = (IRRef1 *)&oir[irlen] - nk; + oir -= nk; + + /* Remove all IR instructions, but retain IR constants. */ + J->cur.nins = REF_FIRST; + J->loopref = 0; + + /* Process constants and fixed references. */ + for (ref = nk; ref <= REF_BASE; ref++) { + IRIns *ir = &oir[ref]; + if ((LJ_SOFTFP && ir->o == IR_KNUM) || ir->o == IR_KINT64) { + /* Split up 64 bit constant. */ + TValue tv = *ir_k64(ir); + ir->prev = lj_ir_kint(J, (int32_t)tv.u32.lo); + hisubst[ref] = lj_ir_kint(J, (int32_t)tv.u32.hi); + } else { + ir->prev = ref; /* Identity substitution for loword. */ + hisubst[ref] = 0; + } + } + + /* Process old IR instructions. */ + for (ref = REF_FIRST; ref < nins; ref++) { + IRIns *ir = &oir[ref]; + IRRef nref = lj_ir_nextins(J); + IRIns *nir = IR(nref); + IRRef hi = 0; + + /* Copy-substitute old instruction to new instruction. */ + nir->op1 = ir->op1 < nk ? ir->op1 : oir[ir->op1].prev; + nir->op2 = ir->op2 < nk ? ir->op2 : oir[ir->op2].prev; + ir->prev = nref; /* Loword substitution. */ + nir->o = ir->o; + nir->t.irt = ir->t.irt & ~(IRT_MARK|IRT_ISPHI); + hisubst[ref] = 0; + + /* Split 64 bit instructions. */ +#if LJ_SOFTFP + if (irt_isnum(ir->t)) { + nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD); /* Turn into INT op. */ + /* Note: hi ref = lo ref + 1! Required for SNAP_SOFTFPNUM logic. */ + switch (ir->o) { + case IR_ADD: + hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_add); + break; + case IR_SUB: + hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_sub); + break; + case IR_MUL: + hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_mul); + break; + case IR_DIV: + hi = split_call_ll(J, hisubst, oir, ir, IRCALL_softfp_div); + break; + case IR_POW: + hi = split_call_li(J, hisubst, oir, ir, IRCALL_lj_vm_powi); + break; + case IR_FPMATH: + /* Try to rejoin pow from EXP2, MUL and LOG2. */ + if (nir->op2 == IRFPM_EXP2 && nir->op1 > J->loopref) { + IRIns *irp = IR(nir->op1); + if (irp->o == IR_CALLN && irp->op2 == IRCALL_softfp_mul) { + IRIns *irm4 = IR(irp->op1); + IRIns *irm3 = IR(irm4->op1); + IRIns *irm12 = IR(irm3->op1); + IRIns *irl1 = IR(irm12->op1); + if (irm12->op1 > J->loopref && irl1->o == IR_CALLN && + irl1->op2 == IRCALL_lj_vm_log2) { + IRRef tmp = irl1->op1; /* Recycle first two args from LOG2. */ + IRRef arg3 = irm3->op2, arg4 = irm4->op2; + J->cur.nins--; + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, arg3); + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), tmp, arg4); + ir->prev = tmp = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_pow); + hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), tmp, tmp); + break; + } + } + } + hi = split_call_l(J, hisubst, oir, ir, IRCALL_lj_vm_floor + ir->op2); + break; + case IR_ATAN2: + hi = split_call_ll(J, hisubst, oir, ir, IRCALL_atan2); + break; + case IR_LDEXP: + hi = split_call_li(J, hisubst, oir, ir, IRCALL_ldexp); + break; + case IR_NEG: case IR_ABS: + nir->o = IR_CONV; /* Pass through loword. */ + nir->op2 = (IRT_INT << 5) | IRT_INT; + hi = split_emit(J, IRT(ir->o == IR_NEG ? IR_BXOR : IR_BAND, IRT_SOFTFP), + hisubst[ir->op1], hisubst[ir->op2]); + break; + case IR_SLOAD: + if ((nir->op2 & IRSLOAD_CONVERT)) { /* Convert from int to number. */ + nir->op2 &= ~IRSLOAD_CONVERT; + ir->prev = nref = split_emit(J, IRTI(IR_CALLN), nref, + IRCALL_softfp_i2d); + hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); + break; + } + /* fallthrough */ + case IR_ALOAD: case IR_HLOAD: case IR_ULOAD: case IR_VLOAD: + case IR_STRTO: + hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); + break; + case IR_XLOAD: { + IRIns inslo = *nir; /* Save/undo the emit of the lo XLOAD. */ + J->cur.nins--; + hi = split_ptr(J, oir, ir->op1); /* Insert the hiref ADD. */ + nref = lj_ir_nextins(J); + nir = IR(nref); + *nir = inslo; /* Re-emit lo XLOAD immediately before hi XLOAD. */ + hi = split_emit(J, IRT(IR_XLOAD, IRT_SOFTFP), hi, ir->op2); +#if LJ_LE + ir->prev = nref; +#else + ir->prev = hi; hi = nref; +#endif + break; + } + case IR_ASTORE: case IR_HSTORE: case IR_USTORE: case IR_XSTORE: + split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nir->op1, hisubst[ir->op2]); + break; + case IR_CONV: { /* Conversion to number. Others handled below. */ + IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); + UNUSED(st); +#if LJ_32 && LJ_HASFFI + if (st == IRT_I64 || st == IRT_U64) { + hi = split_call_l(J, hisubst, oir, ir, + st == IRT_I64 ? IRCALL_fp64_l2d : IRCALL_fp64_ul2d); + break; + } +#endif + lua_assert(st == IRT_INT || + (LJ_32 && LJ_HASFFI && (st == IRT_U32 || st == IRT_FLOAT))); + nir->o = IR_CALLN; +#if LJ_32 && LJ_HASFFI + nir->op2 = st == IRT_INT ? IRCALL_softfp_i2d : + st == IRT_FLOAT ? IRCALL_softfp_f2d : + IRCALL_softfp_ui2d; +#else + nir->op2 = IRCALL_softfp_i2d; +#endif + hi = split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); + break; + } + case IR_CALLN: + case IR_CALLL: + case IR_CALLS: + case IR_CALLXS: + goto split_call; + case IR_PHI: + if (nir->op1 == nir->op2) + J->cur.nins--; /* Drop useless PHIs. */ + if (hisubst[ir->op1] != hisubst[ir->op2]) + split_emit(J, IRT(IR_PHI, IRT_SOFTFP), + hisubst[ir->op1], hisubst[ir->op2]); + break; + case IR_HIOP: + J->cur.nins--; /* Drop joining HIOP. */ + ir->prev = nir->op1; + hi = nir->op2; + break; + default: + lua_assert(ir->o <= IR_NE || ir->o == IR_MIN || ir->o == IR_MAX); + hi = split_emit(J, IRTG(IR_HIOP, IRT_SOFTFP), + hisubst[ir->op1], hisubst[ir->op2]); + break; + } + } else +#endif +#if LJ_32 && LJ_HASFFI + if (irt_isint64(ir->t)) { + IRRef hiref = hisubst[ir->op1]; + nir->t.irt = IRT_INT | (nir->t.irt & IRT_GUARD); /* Turn into INT op. */ + switch (ir->o) { + case IR_ADD: + case IR_SUB: + /* Use plain op for hiword if loword cannot produce a carry/borrow. */ + if (irref_isk(nir->op2) && IR(nir->op2)->i == 0) { + ir->prev = nir->op1; /* Pass through loword. */ + nir->op1 = hiref; nir->op2 = hisubst[ir->op2]; + hi = nref; + break; + } + /* fallthrough */ + case IR_NEG: + hi = split_emit(J, IRTI(IR_HIOP), hiref, hisubst[ir->op2]); + break; + case IR_MUL: + hi = split_call_ll(J, hisubst, oir, ir, IRCALL_lj_carith_mul64); + break; + case IR_DIV: + hi = split_call_ll(J, hisubst, oir, ir, + irt_isi64(ir->t) ? IRCALL_lj_carith_divi64 : + IRCALL_lj_carith_divu64); + break; + case IR_MOD: + hi = split_call_ll(J, hisubst, oir, ir, + irt_isi64(ir->t) ? IRCALL_lj_carith_modi64 : + IRCALL_lj_carith_modu64); + break; + case IR_POW: + hi = split_call_ll(J, hisubst, oir, ir, + irt_isi64(ir->t) ? IRCALL_lj_carith_powi64 : + IRCALL_lj_carith_powu64); + break; + case IR_FLOAD: + lua_assert(ir->op2 == IRFL_CDATA_INT64); + hi = split_emit(J, IRTI(IR_FLOAD), nir->op1, IRFL_CDATA_INT64_4); +#if LJ_BE + ir->prev = hi; hi = nref; +#endif + break; + case IR_XLOAD: + hi = split_emit(J, IRTI(IR_XLOAD), split_ptr(J, oir, ir->op1), ir->op2); +#if LJ_BE + ir->prev = hi; hi = nref; +#endif + break; + case IR_XSTORE: + split_emit(J, IRTI(IR_HIOP), nir->op1, hisubst[ir->op2]); + break; + case IR_CONV: { /* Conversion to 64 bit integer. Others handled below. */ + IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); +#if LJ_SOFTFP + if (st == IRT_NUM) { /* NUM to 64 bit int conv. */ + hi = split_call_l(J, hisubst, oir, ir, + irt_isi64(ir->t) ? IRCALL_fp64_d2l : IRCALL_fp64_d2ul); + } else if (st == IRT_FLOAT) { /* FLOAT to 64 bit int conv. */ + nir->o = IR_CALLN; + nir->op2 = irt_isi64(ir->t) ? IRCALL_fp64_f2l : IRCALL_fp64_f2ul; + hi = split_emit(J, IRTI(IR_HIOP), nref, nref); + } +#else + if (st == IRT_NUM || st == IRT_FLOAT) { /* FP to 64 bit int conv. */ + hi = split_emit(J, IRTI(IR_HIOP), nir->op1, nref); + } +#endif + else if (st == IRT_I64 || st == IRT_U64) { /* 64/64 bit cast. */ + /* Drop cast, since assembler doesn't care. */ + goto fwdlo; + } else if ((ir->op2 & IRCONV_SEXT)) { /* Sign-extend to 64 bit. */ + IRRef k31 = lj_ir_kint(J, 31); + nir = IR(nref); /* May have been reallocated. */ + ir->prev = nir->op1; /* Pass through loword. */ + nir->o = IR_BSAR; /* hi = bsar(lo, 31). */ + nir->op2 = k31; + hi = nref; + } else { /* Zero-extend to 64 bit. */ + hi = lj_ir_kint(J, 0); + goto fwdlo; + } + break; + } + case IR_CALLXS: + goto split_call; + case IR_PHI: { + IRRef hiref2; + if ((irref_isk(nir->op1) && irref_isk(nir->op2)) || + nir->op1 == nir->op2) + J->cur.nins--; /* Drop useless PHIs. */ + hiref2 = hisubst[ir->op2]; + if (!((irref_isk(hiref) && irref_isk(hiref2)) || hiref == hiref2)) + split_emit(J, IRTI(IR_PHI), hiref, hiref2); + break; + } + case IR_HIOP: + J->cur.nins--; /* Drop joining HIOP. */ + ir->prev = nir->op1; + hi = nir->op2; + break; + default: + lua_assert(ir->o <= IR_NE); /* Comparisons. */ + split_emit(J, IRTGI(IR_HIOP), hiref, hisubst[ir->op2]); + break; + } + } else +#endif +#if LJ_SOFTFP + if (ir->o == IR_SLOAD) { + if ((nir->op2 & IRSLOAD_CONVERT)) { /* Convert from number to int. */ + nir->op2 &= ~IRSLOAD_CONVERT; + if (!(nir->op2 & IRSLOAD_TYPECHECK)) + nir->t.irt = IRT_INT; /* Drop guard. */ + split_emit(J, IRT(IR_HIOP, IRT_SOFTFP), nref, nref); + ir->prev = split_num2int(J, nref, nref+1, irt_isguard(ir->t)); + } + } else if (ir->o == IR_TOBIT) { + IRRef tmp, op1 = ir->op1; + J->cur.nins--; +#if LJ_LE + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), oir[op1].prev, hisubst[op1]); +#else + tmp = split_emit(J, IRT(IR_CARG, IRT_NIL), hisubst[op1], oir[op1].prev); +#endif + ir->prev = split_emit(J, IRTI(IR_CALLN), tmp, IRCALL_lj_vm_tobit); + } else if (ir->o == IR_TOSTR) { + if (hisubst[ir->op1]) { + if (irref_isk(ir->op1)) + nir->op1 = ir->op1; + else + split_emit(J, IRT(IR_HIOP, IRT_NIL), hisubst[ir->op1], nref); + } + } else if (ir->o == IR_HREF || ir->o == IR_NEWREF) { + if (irref_isk(ir->op2) && hisubst[ir->op2]) + nir->op2 = ir->op2; + } else +#endif + if (ir->o == IR_CONV) { /* See above, too. */ + IRType st = (IRType)(ir->op2 & IRCONV_SRCMASK); +#if LJ_32 && LJ_HASFFI + if (st == IRT_I64 || st == IRT_U64) { /* Conversion from 64 bit int. */ +#if LJ_SOFTFP + if (irt_isfloat(ir->t)) { + split_call_l(J, hisubst, oir, ir, + st == IRT_I64 ? IRCALL_fp64_l2f : IRCALL_fp64_ul2f); + J->cur.nins--; /* Drop unused HIOP. */ + } +#else + if (irt_isfp(ir->t)) { /* 64 bit integer to FP conversion. */ + ir->prev = split_emit(J, IRT(IR_HIOP, irt_type(ir->t)), + hisubst[ir->op1], nref); + } +#endif + else { /* Truncate to lower 32 bits. */ + fwdlo: + ir->prev = nir->op1; /* Forward loword. */ + /* Replace with NOP to avoid messing up the snapshot logic. */ + nir->ot = IRT(IR_NOP, IRT_NIL); + nir->op1 = nir->op2 = 0; + } + } +#endif +#if LJ_SOFTFP && LJ_32 && LJ_HASFFI + else if (irt_isfloat(ir->t)) { + if (st == IRT_NUM) { + split_call_l(J, hisubst, oir, ir, IRCALL_softfp_d2f); + J->cur.nins--; /* Drop unused HIOP. */ + } else { + nir->o = IR_CALLN; + nir->op2 = st == IRT_INT ? IRCALL_softfp_i2f : IRCALL_softfp_ui2f; + } + } else if (st == IRT_FLOAT) { + nir->o = IR_CALLN; + nir->op2 = irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui; + } else +#endif +#if LJ_SOFTFP + if (st == IRT_NUM || (LJ_32 && LJ_HASFFI && st == IRT_FLOAT)) { + if (irt_isguard(ir->t)) { + lua_assert(st == IRT_NUM && irt_isint(ir->t)); + J->cur.nins--; + ir->prev = split_num2int(J, nir->op1, hisubst[ir->op1], 1); + } else { + split_call_l(J, hisubst, oir, ir, +#if LJ_32 && LJ_HASFFI + st == IRT_NUM ? + (irt_isint(ir->t) ? IRCALL_softfp_d2i : IRCALL_softfp_d2ui) : + (irt_isint(ir->t) ? IRCALL_softfp_f2i : IRCALL_softfp_f2ui) +#else + IRCALL_softfp_d2i +#endif + ); + J->cur.nins--; /* Drop unused HIOP. */ + } + } +#endif + } else if (ir->o == IR_CALLXS) { + IRRef hiref; + split_call: + hiref = hisubst[ir->op1]; + if (hiref) { + IROpT ot = nir->ot; + IRRef op2 = nir->op2; + nir->ot = IRT(IR_CARG, IRT_NIL); +#if LJ_LE + nir->op2 = hiref; +#else + nir->op2 = nir->op1; nir->op1 = hiref; +#endif + ir->prev = nref = split_emit(J, ot, nref, op2); + } + if (LJ_SOFTFP ? irt_is64(ir->t) : irt_isint64(ir->t)) + hi = split_emit(J, + IRT(IR_HIOP, (LJ_SOFTFP && irt_isnum(ir->t)) ? IRT_SOFTFP : IRT_INT), + nref, nref); + } else if (ir->o == IR_CARG) { + IRRef hiref = hisubst[ir->op1]; + if (hiref) { + IRRef op2 = nir->op2; +#if LJ_LE + nir->op2 = hiref; +#else + nir->op2 = nir->op1; nir->op1 = hiref; +#endif + ir->prev = nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2); + nir = IR(nref); + } + hiref = hisubst[ir->op2]; + if (hiref) { +#if !LJ_TARGET_X86 + int carg = 0; + IRIns *cir; + for (cir = IR(nir->op1); cir->o == IR_CARG; cir = IR(cir->op1)) + carg++; + if ((carg & 1) == 0) { /* Align 64 bit arguments. */ + IRRef op2 = nir->op2; + nir->op2 = REF_NIL; + nref = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, op2); + nir = IR(nref); + } +#endif +#if LJ_BE + { IRRef tmp = nir->op2; nir->op2 = hiref; hiref = tmp; } +#endif + ir->prev = split_emit(J, IRT(IR_CARG, IRT_NIL), nref, hiref); + } + } else if (ir->o == IR_CNEWI) { + if (hisubst[ir->op2]) + split_emit(J, IRT(IR_HIOP, IRT_NIL), nref, hisubst[ir->op2]); + } else if (ir->o == IR_LOOP) { + J->loopref = nref; /* Needed by assembler. */ + } + hisubst[ref] = hi; /* Store hiword substitution. */ + } + + /* Add PHI marks. */ + for (ref = J->cur.nins-1; ref >= REF_FIRST; ref--) { + IRIns *ir = IR(ref); + if (ir->o != IR_PHI) break; + if (!irref_isk(ir->op1)) irt_setphi(IR(ir->op1)->t); + if (ir->op2 > J->loopref) irt_setphi(IR(ir->op2)->t); + } + + /* Substitute snapshot maps. */ + oir[nins].prev = J->cur.nins; /* Substitution for last snapshot. */ + { + SnapNo i, nsnap = J->cur.nsnap; + for (i = 0; i < nsnap; i++) { + SnapShot *snap = &J->cur.snap[i]; + SnapEntry *map = &J->cur.snapmap[snap->mapofs]; + MSize n, nent = snap->nent; + snap->ref = snap->ref == REF_FIRST ? REF_FIRST : oir[snap->ref].prev; + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + IRIns *ir = &oir[snap_ref(sn)]; + if (!(LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && irref_isk(snap_ref(sn)))) + map[n] = ((sn & 0xffff0000) | ir->prev); + } + } + } +} + +/* Protected callback for split pass. */ +static TValue *cpsplit(lua_State *L, lua_CFunction dummy, void *ud) +{ + jit_State *J = (jit_State *)ud; + split_ir(J); + UNUSED(L); UNUSED(dummy); + return NULL; +} + +#if defined(LUA_USE_ASSERT) || LJ_SOFTFP +/* Slow, but sure way to check whether a SPLIT pass is needed. */ +static int split_needsplit(jit_State *J) +{ + IRIns *ir, *irend; + IRRef ref; + for (ir = IR(REF_FIRST), irend = IR(J->cur.nins); ir < irend; ir++) + if (LJ_SOFTFP ? irt_is64orfp(ir->t) : irt_isint64(ir->t)) + return 1; + if (LJ_SOFTFP) { + for (ref = J->chain[IR_SLOAD]; ref; ref = IR(ref)->prev) + if ((IR(ref)->op2 & IRSLOAD_CONVERT)) + return 1; + if (J->chain[IR_TOBIT]) + return 1; + } + for (ref = J->chain[IR_CONV]; ref; ref = IR(ref)->prev) { + IRType st = (IR(ref)->op2 & IRCONV_SRCMASK); + if ((LJ_SOFTFP && (st == IRT_NUM || st == IRT_FLOAT)) || + st == IRT_I64 || st == IRT_U64) + return 1; + } + return 0; /* Nope. */ +} +#endif + +/* SPLIT pass. */ +void lj_opt_split(jit_State *J) +{ +#if LJ_SOFTFP + if (!J->needsplit) + J->needsplit = split_needsplit(J); +#else + lua_assert(J->needsplit >= split_needsplit(J)); /* Verify flag. */ +#endif + if (J->needsplit) { + int errcode = lj_vm_cpcall(J->L, NULL, J, cpsplit); + if (errcode) { + /* Completely reset the trace to avoid inconsistent dump on abort. */ + J->cur.nins = J->cur.nk = REF_BASE; + J->cur.nsnap = 0; + lj_err_throw(J->L, errcode); /* Propagate errors. */ + } + } +} + +#undef IR + +#endif diff --git a/external/lua/luajit/src/src/lj_parse.c b/external/lua/luajit/src/src/lj_parse.c new file mode 100644 index 0000000000..7ff7d72855 --- /dev/null +++ b/external/lua/luajit/src/src/lj_parse.c @@ -0,0 +1,2750 @@ +/* +** Lua parser (source code -> bytecode). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lj_parse_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_debug.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_func.h" +#include "lj_state.h" +#include "lj_bc.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#endif +#include "lj_lex.h" +#include "lj_parse.h" +#include "lj_vm.h" +#include "lj_vmevent.h" + +/* -- Parser structures and definitions ----------------------------------- */ + +/* Expression kinds. */ +typedef enum { + /* Constant expressions must be first and in this order: */ + VKNIL, + VKFALSE, + VKTRUE, + VKSTR, /* sval = string value */ + VKNUM, /* nval = number value */ + VKLAST = VKNUM, + VKCDATA, /* nval = cdata value, not treated as a constant expression */ + /* Non-constant expressions follow: */ + VLOCAL, /* info = local register, aux = vstack index */ + VUPVAL, /* info = upvalue index, aux = vstack index */ + VGLOBAL, /* sval = string value */ + VINDEXED, /* info = table register, aux = index reg/byte/string const */ + VJMP, /* info = instruction PC */ + VRELOCABLE, /* info = instruction PC */ + VNONRELOC, /* info = result register */ + VCALL, /* info = instruction PC, aux = base */ + VVOID +} ExpKind; + +/* Expression descriptor. */ +typedef struct ExpDesc { + union { + struct { + uint32_t info; /* Primary info. */ + uint32_t aux; /* Secondary info. */ + } s; + TValue nval; /* Number value. */ + GCstr *sval; /* String value. */ + } u; + ExpKind k; + BCPos t; /* True condition jump list. */ + BCPos f; /* False condition jump list. */ +} ExpDesc; + +/* Macros for expressions. */ +#define expr_hasjump(e) ((e)->t != (e)->f) + +#define expr_isk(e) ((e)->k <= VKLAST) +#define expr_isk_nojump(e) (expr_isk(e) && !expr_hasjump(e)) +#define expr_isnumk(e) ((e)->k == VKNUM) +#define expr_isnumk_nojump(e) (expr_isnumk(e) && !expr_hasjump(e)) +#define expr_isstrk(e) ((e)->k == VKSTR) + +#define expr_numtv(e) check_exp(expr_isnumk((e)), &(e)->u.nval) +#define expr_numberV(e) numberVnum(expr_numtv((e))) + +/* Initialize expression. */ +static LJ_AINLINE void expr_init(ExpDesc *e, ExpKind k, uint32_t info) +{ + e->k = k; + e->u.s.info = info; + e->f = e->t = NO_JMP; +} + +/* Check number constant for +-0. */ +static int expr_numiszero(ExpDesc *e) +{ + TValue *o = expr_numtv(e); + return tvisint(o) ? (intV(o) == 0) : tviszero(o); +} + +/* Per-function linked list of scope blocks. */ +typedef struct FuncScope { + struct FuncScope *prev; /* Link to outer scope. */ + MSize vstart; /* Start of block-local variables. */ + uint8_t nactvar; /* Number of active vars outside the scope. */ + uint8_t flags; /* Scope flags. */ +} FuncScope; + +#define FSCOPE_LOOP 0x01 /* Scope is a (breakable) loop. */ +#define FSCOPE_BREAK 0x02 /* Break used in scope. */ +#define FSCOPE_GOLA 0x04 /* Goto or label used in scope. */ +#define FSCOPE_UPVAL 0x08 /* Upvalue in scope. */ +#define FSCOPE_NOCLOSE 0x10 /* Do not close upvalues. */ + +#define NAME_BREAK ((GCstr *)(uintptr_t)1) + +/* Index into variable stack. */ +typedef uint16_t VarIndex; +#define LJ_MAX_VSTACK (65536 - LJ_MAX_UPVAL) + +/* Variable/goto/label info. */ +#define VSTACK_VAR_RW 0x01 /* R/W variable. */ +#define VSTACK_GOTO 0x02 /* Pending goto. */ +#define VSTACK_LABEL 0x04 /* Label. */ + +/* Per-function state. */ +typedef struct FuncState { + GCtab *kt; /* Hash table for constants. */ + LexState *ls; /* Lexer state. */ + lua_State *L; /* Lua state. */ + FuncScope *bl; /* Current scope. */ + struct FuncState *prev; /* Enclosing function. */ + BCPos pc; /* Next bytecode position. */ + BCPos lasttarget; /* Bytecode position of last jump target. */ + BCPos jpc; /* Pending jump list to next bytecode. */ + BCReg freereg; /* First free register. */ + BCReg nactvar; /* Number of active local variables. */ + BCReg nkn, nkgc; /* Number of lua_Number/GCobj constants */ + BCLine linedefined; /* First line of the function definition. */ + BCInsLine *bcbase; /* Base of bytecode stack. */ + BCPos bclim; /* Limit of bytecode stack. */ + MSize vbase; /* Base of variable stack for this function. */ + uint8_t flags; /* Prototype flags. */ + uint8_t numparams; /* Number of parameters. */ + uint8_t framesize; /* Fixed frame size. */ + uint8_t nuv; /* Number of upvalues */ + VarIndex varmap[LJ_MAX_LOCVAR]; /* Map from register to variable idx. */ + VarIndex uvmap[LJ_MAX_UPVAL]; /* Map from upvalue to variable idx. */ + VarIndex uvtmp[LJ_MAX_UPVAL]; /* Temporary upvalue map. */ +} FuncState; + +/* Binary and unary operators. ORDER OPR */ +typedef enum BinOpr { + OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, /* ORDER ARITH */ + OPR_CONCAT, + OPR_NE, OPR_EQ, + OPR_LT, OPR_GE, OPR_LE, OPR_GT, + OPR_AND, OPR_OR, + OPR_NOBINOPR +} BinOpr; + +LJ_STATIC_ASSERT((int)BC_ISGE-(int)BC_ISLT == (int)OPR_GE-(int)OPR_LT); +LJ_STATIC_ASSERT((int)BC_ISLE-(int)BC_ISLT == (int)OPR_LE-(int)OPR_LT); +LJ_STATIC_ASSERT((int)BC_ISGT-(int)BC_ISLT == (int)OPR_GT-(int)OPR_LT); +LJ_STATIC_ASSERT((int)BC_SUBVV-(int)BC_ADDVV == (int)OPR_SUB-(int)OPR_ADD); +LJ_STATIC_ASSERT((int)BC_MULVV-(int)BC_ADDVV == (int)OPR_MUL-(int)OPR_ADD); +LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD); +LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD); + +/* -- Error handling ------------------------------------------------------ */ + +LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em) +{ + lj_lex_error(ls, ls->token, em); +} + +LJ_NORET LJ_NOINLINE static void err_token(LexState *ls, LexToken token) +{ + lj_lex_error(ls, ls->token, LJ_ERR_XTOKEN, lj_lex_token2str(ls, token)); +} + +LJ_NORET static void err_limit(FuncState *fs, uint32_t limit, const char *what) +{ + if (fs->linedefined == 0) + lj_lex_error(fs->ls, 0, LJ_ERR_XLIMM, limit, what); + else + lj_lex_error(fs->ls, 0, LJ_ERR_XLIMF, fs->linedefined, limit, what); +} + +#define checklimit(fs, v, l, m) if ((v) >= (l)) err_limit(fs, l, m) +#define checklimitgt(fs, v, l, m) if ((v) > (l)) err_limit(fs, l, m) +#define checkcond(ls, c, em) { if (!(c)) err_syntax(ls, em); } + +/* -- Management of constants --------------------------------------------- */ + +/* Return bytecode encoding for primitive constant. */ +#define const_pri(e) check_exp((e)->k <= VKTRUE, (e)->k) + +#define tvhaskslot(o) ((o)->u32.hi == 0) +#define tvkslot(o) ((o)->u32.lo) + +/* Add a number constant. */ +static BCReg const_num(FuncState *fs, ExpDesc *e) +{ + lua_State *L = fs->L; + TValue *o; + lua_assert(expr_isnumk(e)); + o = lj_tab_set(L, fs->kt, &e->u.nval); + if (tvhaskslot(o)) + return tvkslot(o); + o->u64 = fs->nkn; + return fs->nkn++; +} + +/* Add a GC object constant. */ +static BCReg const_gc(FuncState *fs, GCobj *gc, uint32_t itype) +{ + lua_State *L = fs->L; + TValue key, *o; + setgcV(L, &key, gc, itype); + /* NOBARRIER: the key is new or kept alive. */ + o = lj_tab_set(L, fs->kt, &key); + if (tvhaskslot(o)) + return tvkslot(o); + o->u64 = fs->nkgc; + return fs->nkgc++; +} + +/* Add a string constant. */ +static BCReg const_str(FuncState *fs, ExpDesc *e) +{ + lua_assert(expr_isstrk(e) || e->k == VGLOBAL); + return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR); +} + +/* Anchor string constant to avoid GC. */ +GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t len) +{ + /* NOBARRIER: the key is new or kept alive. */ + lua_State *L = ls->L; + GCstr *s = lj_str_new(L, str, len); + TValue *tv = lj_tab_setstr(L, ls->fs->kt, s); + if (tvisnil(tv)) setboolV(tv, 1); + lj_gc_check(L); + return s; +} + +#if LJ_HASFFI +/* Anchor cdata to avoid GC. */ +void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd) +{ + /* NOBARRIER: the key is new or kept alive. */ + lua_State *L = ls->L; + setcdataV(L, tv, cd); + setboolV(lj_tab_set(L, ls->fs->kt, tv), 1); +} +#endif + +/* -- Jump list handling -------------------------------------------------- */ + +/* Get next element in jump list. */ +static BCPos jmp_next(FuncState *fs, BCPos pc) +{ + ptrdiff_t delta = bc_j(fs->bcbase[pc].ins); + if ((BCPos)delta == NO_JMP) + return NO_JMP; + else + return (BCPos)(((ptrdiff_t)pc+1)+delta); +} + +/* Check if any of the instructions on the jump list produce no value. */ +static int jmp_novalue(FuncState *fs, BCPos list) +{ + for (; list != NO_JMP; list = jmp_next(fs, list)) { + BCIns p = fs->bcbase[list >= 1 ? list-1 : list].ins; + if (!(bc_op(p) == BC_ISTC || bc_op(p) == BC_ISFC || bc_a(p) == NO_REG)) + return 1; + } + return 0; +} + +/* Patch register of test instructions. */ +static int jmp_patchtestreg(FuncState *fs, BCPos pc, BCReg reg) +{ + BCInsLine *ilp = &fs->bcbase[pc >= 1 ? pc-1 : pc]; + BCOp op = bc_op(ilp->ins); + if (op == BC_ISTC || op == BC_ISFC) { + if (reg != NO_REG && reg != bc_d(ilp->ins)) { + setbc_a(&ilp->ins, reg); + } else { /* Nothing to store or already in the right register. */ + setbc_op(&ilp->ins, op+(BC_IST-BC_ISTC)); + setbc_a(&ilp->ins, 0); + } + } else if (bc_a(ilp->ins) == NO_REG) { + if (reg == NO_REG) { + ilp->ins = BCINS_AJ(BC_JMP, bc_a(fs->bcbase[pc].ins), 0); + } else { + setbc_a(&ilp->ins, reg); + if (reg >= bc_a(ilp[1].ins)) + setbc_a(&ilp[1].ins, reg+1); + } + } else { + return 0; /* Cannot patch other instructions. */ + } + return 1; +} + +/* Drop values for all instructions on jump list. */ +static void jmp_dropval(FuncState *fs, BCPos list) +{ + for (; list != NO_JMP; list = jmp_next(fs, list)) + jmp_patchtestreg(fs, list, NO_REG); +} + +/* Patch jump instruction to target. */ +static void jmp_patchins(FuncState *fs, BCPos pc, BCPos dest) +{ + BCIns *jmp = &fs->bcbase[pc].ins; + BCPos offset = dest-(pc+1)+BCBIAS_J; + lua_assert(dest != NO_JMP); + if (offset > BCMAX_D) + err_syntax(fs->ls, LJ_ERR_XJUMP); + setbc_d(jmp, offset); +} + +/* Append to jump list. */ +static void jmp_append(FuncState *fs, BCPos *l1, BCPos l2) +{ + if (l2 == NO_JMP) { + return; + } else if (*l1 == NO_JMP) { + *l1 = l2; + } else { + BCPos list = *l1; + BCPos next; + while ((next = jmp_next(fs, list)) != NO_JMP) /* Find last element. */ + list = next; + jmp_patchins(fs, list, l2); + } +} + +/* Patch jump list and preserve produced values. */ +static void jmp_patchval(FuncState *fs, BCPos list, BCPos vtarget, + BCReg reg, BCPos dtarget) +{ + while (list != NO_JMP) { + BCPos next = jmp_next(fs, list); + if (jmp_patchtestreg(fs, list, reg)) + jmp_patchins(fs, list, vtarget); /* Jump to target with value. */ + else + jmp_patchins(fs, list, dtarget); /* Jump to default target. */ + list = next; + } +} + +/* Jump to following instruction. Append to list of pending jumps. */ +static void jmp_tohere(FuncState *fs, BCPos list) +{ + fs->lasttarget = fs->pc; + jmp_append(fs, &fs->jpc, list); +} + +/* Patch jump list to target. */ +static void jmp_patch(FuncState *fs, BCPos list, BCPos target) +{ + if (target == fs->pc) { + jmp_tohere(fs, list); + } else { + lua_assert(target < fs->pc); + jmp_patchval(fs, list, target, NO_REG, target); + } +} + +/* -- Bytecode register allocator ----------------------------------------- */ + +/* Bump frame size. */ +static void bcreg_bump(FuncState *fs, BCReg n) +{ + BCReg sz = fs->freereg + n; + if (sz > fs->framesize) { + if (sz >= LJ_MAX_SLOTS) + err_syntax(fs->ls, LJ_ERR_XSLOTS); + fs->framesize = (uint8_t)sz; + } +} + +/* Reserve registers. */ +static void bcreg_reserve(FuncState *fs, BCReg n) +{ + bcreg_bump(fs, n); + fs->freereg += n; +} + +/* Free register. */ +static void bcreg_free(FuncState *fs, BCReg reg) +{ + if (reg >= fs->nactvar) { + fs->freereg--; + lua_assert(reg == fs->freereg); + } +} + +/* Free register for expression. */ +static void expr_free(FuncState *fs, ExpDesc *e) +{ + if (e->k == VNONRELOC) + bcreg_free(fs, e->u.s.info); +} + +/* -- Bytecode emitter ---------------------------------------------------- */ + +/* Emit bytecode instruction. */ +static BCPos bcemit_INS(FuncState *fs, BCIns ins) +{ + BCPos pc = fs->pc; + LexState *ls = fs->ls; + jmp_patchval(fs, fs->jpc, pc, NO_REG, pc); + fs->jpc = NO_JMP; + if (LJ_UNLIKELY(pc >= fs->bclim)) { + ptrdiff_t base = fs->bcbase - ls->bcstack; + checklimit(fs, ls->sizebcstack, LJ_MAX_BCINS, "bytecode instructions"); + lj_mem_growvec(fs->L, ls->bcstack, ls->sizebcstack, LJ_MAX_BCINS,BCInsLine); + fs->bclim = (BCPos)(ls->sizebcstack - base); + fs->bcbase = ls->bcstack + base; + } + fs->bcbase[pc].ins = ins; + fs->bcbase[pc].line = ls->lastline; + fs->pc = pc+1; + return pc; +} + +#define bcemit_ABC(fs, o, a, b, c) bcemit_INS(fs, BCINS_ABC(o, a, b, c)) +#define bcemit_AD(fs, o, a, d) bcemit_INS(fs, BCINS_AD(o, a, d)) +#define bcemit_AJ(fs, o, a, j) bcemit_INS(fs, BCINS_AJ(o, a, j)) + +#define bcptr(fs, e) (&(fs)->bcbase[(e)->u.s.info].ins) + +/* -- Bytecode emitter for expressions ------------------------------------ */ + +/* Discharge non-constant expression to any register. */ +static void expr_discharge(FuncState *fs, ExpDesc *e) +{ + BCIns ins; + if (e->k == VUPVAL) { + ins = BCINS_AD(BC_UGET, 0, e->u.s.info); + } else if (e->k == VGLOBAL) { + ins = BCINS_AD(BC_GGET, 0, const_str(fs, e)); + } else if (e->k == VINDEXED) { + BCReg rc = e->u.s.aux; + if ((int32_t)rc < 0) { + ins = BCINS_ABC(BC_TGETS, 0, e->u.s.info, ~rc); + } else if (rc > BCMAX_C) { + ins = BCINS_ABC(BC_TGETB, 0, e->u.s.info, rc-(BCMAX_C+1)); + } else { + bcreg_free(fs, rc); + ins = BCINS_ABC(BC_TGETV, 0, e->u.s.info, rc); + } + bcreg_free(fs, e->u.s.info); + } else if (e->k == VCALL) { + e->u.s.info = e->u.s.aux; + e->k = VNONRELOC; + return; + } else if (e->k == VLOCAL) { + e->k = VNONRELOC; + return; + } else { + return; + } + e->u.s.info = bcemit_INS(fs, ins); + e->k = VRELOCABLE; +} + +/* Emit bytecode to set a range of registers to nil. */ +static void bcemit_nil(FuncState *fs, BCReg from, BCReg n) +{ + if (fs->pc > fs->lasttarget) { /* No jumps to current position? */ + BCIns *ip = &fs->bcbase[fs->pc-1].ins; + BCReg pto, pfrom = bc_a(*ip); + switch (bc_op(*ip)) { /* Try to merge with the previous instruction. */ + case BC_KPRI: + if (bc_d(*ip) != ~LJ_TNIL) break; + if (from == pfrom) { + if (n == 1) return; + } else if (from == pfrom+1) { + from = pfrom; + n++; + } else { + break; + } + *ip = BCINS_AD(BC_KNIL, from, from+n-1); /* Replace KPRI. */ + return; + case BC_KNIL: + pto = bc_d(*ip); + if (pfrom <= from && from <= pto+1) { /* Can we connect both ranges? */ + if (from+n-1 > pto) + setbc_d(ip, from+n-1); /* Patch previous instruction range. */ + return; + } + break; + default: + break; + } + } + /* Emit new instruction or replace old instruction. */ + bcemit_INS(fs, n == 1 ? BCINS_AD(BC_KPRI, from, VKNIL) : + BCINS_AD(BC_KNIL, from, from+n-1)); +} + +/* Discharge an expression to a specific register. Ignore branches. */ +static void expr_toreg_nobranch(FuncState *fs, ExpDesc *e, BCReg reg) +{ + BCIns ins; + expr_discharge(fs, e); + if (e->k == VKSTR) { + ins = BCINS_AD(BC_KSTR, reg, const_str(fs, e)); + } else if (e->k == VKNUM) { +#if LJ_DUALNUM + cTValue *tv = expr_numtv(e); + if (tvisint(tv) && checki16(intV(tv))) + ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)intV(tv)); + else +#else + lua_Number n = expr_numberV(e); + int32_t k = lj_num2int(n); + if (checki16(k) && n == (lua_Number)k) + ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)k); + else +#endif + ins = BCINS_AD(BC_KNUM, reg, const_num(fs, e)); +#if LJ_HASFFI + } else if (e->k == VKCDATA) { + fs->flags |= PROTO_FFI; + ins = BCINS_AD(BC_KCDATA, reg, + const_gc(fs, obj2gco(cdataV(&e->u.nval)), LJ_TCDATA)); +#endif + } else if (e->k == VRELOCABLE) { + setbc_a(bcptr(fs, e), reg); + goto noins; + } else if (e->k == VNONRELOC) { + if (reg == e->u.s.info) + goto noins; + ins = BCINS_AD(BC_MOV, reg, e->u.s.info); + } else if (e->k == VKNIL) { + bcemit_nil(fs, reg, 1); + goto noins; + } else if (e->k <= VKTRUE) { + ins = BCINS_AD(BC_KPRI, reg, const_pri(e)); + } else { + lua_assert(e->k == VVOID || e->k == VJMP); + return; + } + bcemit_INS(fs, ins); +noins: + e->u.s.info = reg; + e->k = VNONRELOC; +} + +/* Forward declaration. */ +static BCPos bcemit_jmp(FuncState *fs); + +/* Discharge an expression to a specific register. */ +static void expr_toreg(FuncState *fs, ExpDesc *e, BCReg reg) +{ + expr_toreg_nobranch(fs, e, reg); + if (e->k == VJMP) + jmp_append(fs, &e->t, e->u.s.info); /* Add it to the true jump list. */ + if (expr_hasjump(e)) { /* Discharge expression with branches. */ + BCPos jend, jfalse = NO_JMP, jtrue = NO_JMP; + if (jmp_novalue(fs, e->t) || jmp_novalue(fs, e->f)) { + BCPos jval = (e->k == VJMP) ? NO_JMP : bcemit_jmp(fs); + jfalse = bcemit_AD(fs, BC_KPRI, reg, VKFALSE); + bcemit_AJ(fs, BC_JMP, fs->freereg, 1); + jtrue = bcemit_AD(fs, BC_KPRI, reg, VKTRUE); + jmp_tohere(fs, jval); + } + jend = fs->pc; + fs->lasttarget = jend; + jmp_patchval(fs, e->f, jend, reg, jfalse); + jmp_patchval(fs, e->t, jend, reg, jtrue); + } + e->f = e->t = NO_JMP; + e->u.s.info = reg; + e->k = VNONRELOC; +} + +/* Discharge an expression to the next free register. */ +static void expr_tonextreg(FuncState *fs, ExpDesc *e) +{ + expr_discharge(fs, e); + expr_free(fs, e); + bcreg_reserve(fs, 1); + expr_toreg(fs, e, fs->freereg - 1); +} + +/* Discharge an expression to any register. */ +static BCReg expr_toanyreg(FuncState *fs, ExpDesc *e) +{ + expr_discharge(fs, e); + if (e->k == VNONRELOC) { + if (!expr_hasjump(e)) return e->u.s.info; /* Already in a register. */ + if (e->u.s.info >= fs->nactvar) { + expr_toreg(fs, e, e->u.s.info); /* Discharge to temp. register. */ + return e->u.s.info; + } + } + expr_tonextreg(fs, e); /* Discharge to next register. */ + return e->u.s.info; +} + +/* Partially discharge expression to a value. */ +static void expr_toval(FuncState *fs, ExpDesc *e) +{ + if (expr_hasjump(e)) + expr_toanyreg(fs, e); + else + expr_discharge(fs, e); +} + +/* Emit store for LHS expression. */ +static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e) +{ + BCIns ins; + if (var->k == VLOCAL) { + fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW; + expr_free(fs, e); + expr_toreg(fs, e, var->u.s.info); + return; + } else if (var->k == VUPVAL) { + fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW; + expr_toval(fs, e); + if (e->k <= VKTRUE) + ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e)); + else if (e->k == VKSTR) + ins = BCINS_AD(BC_USETS, var->u.s.info, const_str(fs, e)); + else if (e->k == VKNUM) + ins = BCINS_AD(BC_USETN, var->u.s.info, const_num(fs, e)); + else + ins = BCINS_AD(BC_USETV, var->u.s.info, expr_toanyreg(fs, e)); + } else if (var->k == VGLOBAL) { + BCReg ra = expr_toanyreg(fs, e); + ins = BCINS_AD(BC_GSET, ra, const_str(fs, var)); + } else { + BCReg ra, rc; + lua_assert(var->k == VINDEXED); + ra = expr_toanyreg(fs, e); + rc = var->u.s.aux; + if ((int32_t)rc < 0) { + ins = BCINS_ABC(BC_TSETS, ra, var->u.s.info, ~rc); + } else if (rc > BCMAX_C) { + ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1)); + } else { + /* Free late alloced key reg to avoid assert on free of value reg. */ + /* This can only happen when called from expr_table(). */ + lua_assert(e->k != VNONRELOC || ra < fs->nactvar || + rc < ra || (bcreg_free(fs, rc),1)); + ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc); + } + } + bcemit_INS(fs, ins); + expr_free(fs, e); +} + +/* Emit method lookup expression. */ +static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key) +{ + BCReg idx, func, obj = expr_toanyreg(fs, e); + expr_free(fs, e); + func = fs->freereg; + bcemit_AD(fs, BC_MOV, func+1, obj); /* Copy object to first argument. */ + lua_assert(expr_isstrk(key)); + idx = const_str(fs, key); + if (idx <= BCMAX_C) { + bcreg_reserve(fs, 2); + bcemit_ABC(fs, BC_TGETS, func, obj, idx); + } else { + bcreg_reserve(fs, 3); + bcemit_AD(fs, BC_KSTR, func+2, idx); + bcemit_ABC(fs, BC_TGETV, func, obj, func+2); + fs->freereg--; + } + e->u.s.info = func; + e->k = VNONRELOC; +} + +/* -- Bytecode emitter for branches --------------------------------------- */ + +/* Emit unconditional branch. */ +static BCPos bcemit_jmp(FuncState *fs) +{ + BCPos jpc = fs->jpc; + BCPos j = fs->pc - 1; + BCIns *ip = &fs->bcbase[j].ins; + fs->jpc = NO_JMP; + if ((int32_t)j >= (int32_t)fs->lasttarget && bc_op(*ip) == BC_UCLO) + setbc_j(ip, NO_JMP); + else + j = bcemit_AJ(fs, BC_JMP, fs->freereg, NO_JMP); + jmp_append(fs, &j, jpc); + return j; +} + +/* Invert branch condition of bytecode instruction. */ +static void invertcond(FuncState *fs, ExpDesc *e) +{ + BCIns *ip = &fs->bcbase[e->u.s.info - 1].ins; + setbc_op(ip, bc_op(*ip)^1); +} + +/* Emit conditional branch. */ +static BCPos bcemit_branch(FuncState *fs, ExpDesc *e, int cond) +{ + BCPos pc; + if (e->k == VRELOCABLE) { + BCIns *ip = bcptr(fs, e); + if (bc_op(*ip) == BC_NOT) { + *ip = BCINS_AD(cond ? BC_ISF : BC_IST, 0, bc_d(*ip)); + return bcemit_jmp(fs); + } + } + if (e->k != VNONRELOC) { + bcreg_reserve(fs, 1); + expr_toreg_nobranch(fs, e, fs->freereg-1); + } + bcemit_AD(fs, cond ? BC_ISTC : BC_ISFC, NO_REG, e->u.s.info); + pc = bcemit_jmp(fs); + expr_free(fs, e); + return pc; +} + +/* Emit branch on true condition. */ +static void bcemit_branch_t(FuncState *fs, ExpDesc *e) +{ + BCPos pc; + expr_discharge(fs, e); + if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE) + pc = NO_JMP; /* Never jump. */ + else if (e->k == VJMP) + invertcond(fs, e), pc = e->u.s.info; + else if (e->k == VKFALSE || e->k == VKNIL) + expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs); + else + pc = bcemit_branch(fs, e, 0); + jmp_append(fs, &e->f, pc); + jmp_tohere(fs, e->t); + e->t = NO_JMP; +} + +/* Emit branch on false condition. */ +static void bcemit_branch_f(FuncState *fs, ExpDesc *e) +{ + BCPos pc; + expr_discharge(fs, e); + if (e->k == VKNIL || e->k == VKFALSE) + pc = NO_JMP; /* Never jump. */ + else if (e->k == VJMP) + pc = e->u.s.info; + else if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE) + expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs); + else + pc = bcemit_branch(fs, e, 1); + jmp_append(fs, &e->t, pc); + jmp_tohere(fs, e->f); + e->f = NO_JMP; +} + +/* -- Bytecode emitter for operators -------------------------------------- */ + +/* Try constant-folding of arithmetic operators. */ +static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2) +{ + TValue o; + lua_Number n; + if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0; + n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD); + setnumV(&o, n); + if (tvisnan(&o) || tvismzero(&o)) return 0; /* Avoid NaN and -0 as consts. */ + if (LJ_DUALNUM) { + int32_t k = lj_num2int(n); + if ((lua_Number)k == n) { + setintV(&e1->u.nval, k); + return 1; + } + } + setnumV(&e1->u.nval, n); + return 1; +} + +/* Emit arithmetic operator. */ +static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2) +{ + BCReg rb, rc, t; + uint32_t op; + if (foldarith(opr, e1, e2)) + return; + if (opr == OPR_POW) { + op = BC_POW; + rc = expr_toanyreg(fs, e2); + rb = expr_toanyreg(fs, e1); + } else { + op = opr-OPR_ADD+BC_ADDVV; + /* Must discharge 2nd operand first since VINDEXED might free regs. */ + expr_toval(fs, e2); + if (expr_isnumk(e2) && (rc = const_num(fs, e2)) <= BCMAX_C) + op -= BC_ADDVV-BC_ADDVN; + else + rc = expr_toanyreg(fs, e2); + /* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */ + lua_assert(expr_isnumk(e1) || e1->k == VNONRELOC); + expr_toval(fs, e1); + /* Avoid two consts to satisfy bytecode constraints. */ + if (expr_isnumk(e1) && !expr_isnumk(e2) && + (t = const_num(fs, e1)) <= BCMAX_B) { + rb = rc; rc = t; op -= BC_ADDVV-BC_ADDNV; + } else { + rb = expr_toanyreg(fs, e1); + } + } + /* Using expr_free might cause asserts if the order is wrong. */ + if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--; + if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--; + e1->u.s.info = bcemit_ABC(fs, op, 0, rb, rc); + e1->k = VRELOCABLE; +} + +/* Emit comparison operator. */ +static void bcemit_comp(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2) +{ + ExpDesc *eret = e1; + BCIns ins; + expr_toval(fs, e1); + if (opr == OPR_EQ || opr == OPR_NE) { + BCOp op = opr == OPR_EQ ? BC_ISEQV : BC_ISNEV; + BCReg ra; + if (expr_isk(e1)) { e1 = e2; e2 = eret; } /* Need constant in 2nd arg. */ + ra = expr_toanyreg(fs, e1); /* First arg must be in a reg. */ + expr_toval(fs, e2); + switch (e2->k) { + case VKNIL: case VKFALSE: case VKTRUE: + ins = BCINS_AD(op+(BC_ISEQP-BC_ISEQV), ra, const_pri(e2)); + break; + case VKSTR: + ins = BCINS_AD(op+(BC_ISEQS-BC_ISEQV), ra, const_str(fs, e2)); + break; + case VKNUM: + ins = BCINS_AD(op+(BC_ISEQN-BC_ISEQV), ra, const_num(fs, e2)); + break; + default: + ins = BCINS_AD(op, ra, expr_toanyreg(fs, e2)); + break; + } + } else { + uint32_t op = opr-OPR_LT+BC_ISLT; + BCReg ra, rd; + if ((op-BC_ISLT) & 1) { /* GT -> LT, GE -> LE */ + e1 = e2; e2 = eret; /* Swap operands. */ + op = ((op-BC_ISLT)^3)+BC_ISLT; + expr_toval(fs, e1); + } + rd = expr_toanyreg(fs, e2); + ra = expr_toanyreg(fs, e1); + ins = BCINS_AD(op, ra, rd); + } + /* Using expr_free might cause asserts if the order is wrong. */ + if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--; + if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--; + bcemit_INS(fs, ins); + eret->u.s.info = bcemit_jmp(fs); + eret->k = VJMP; +} + +/* Fixup left side of binary operator. */ +static void bcemit_binop_left(FuncState *fs, BinOpr op, ExpDesc *e) +{ + if (op == OPR_AND) { + bcemit_branch_t(fs, e); + } else if (op == OPR_OR) { + bcemit_branch_f(fs, e); + } else if (op == OPR_CONCAT) { + expr_tonextreg(fs, e); + } else if (op == OPR_EQ || op == OPR_NE) { + if (!expr_isk_nojump(e)) expr_toanyreg(fs, e); + } else { + if (!expr_isnumk_nojump(e)) expr_toanyreg(fs, e); + } +} + +/* Emit binary operator. */ +static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2) +{ + if (op <= OPR_POW) { + bcemit_arith(fs, op, e1, e2); + } else if (op == OPR_AND) { + lua_assert(e1->t == NO_JMP); /* List must be closed. */ + expr_discharge(fs, e2); + jmp_append(fs, &e2->f, e1->f); + *e1 = *e2; + } else if (op == OPR_OR) { + lua_assert(e1->f == NO_JMP); /* List must be closed. */ + expr_discharge(fs, e2); + jmp_append(fs, &e2->t, e1->t); + *e1 = *e2; + } else if (op == OPR_CONCAT) { + expr_toval(fs, e2); + if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) { + lua_assert(e1->u.s.info == bc_b(*bcptr(fs, e2))-1); + expr_free(fs, e1); + setbc_b(bcptr(fs, e2), e1->u.s.info); + e1->u.s.info = e2->u.s.info; + } else { + expr_tonextreg(fs, e2); + expr_free(fs, e2); + expr_free(fs, e1); + e1->u.s.info = bcemit_ABC(fs, BC_CAT, 0, e1->u.s.info, e2->u.s.info); + } + e1->k = VRELOCABLE; + } else { + lua_assert(op == OPR_NE || op == OPR_EQ || + op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT); + bcemit_comp(fs, op, e1, e2); + } +} + +/* Emit unary operator. */ +static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e) +{ + if (op == BC_NOT) { + /* Swap true and false lists. */ + { BCPos temp = e->f; e->f = e->t; e->t = temp; } + jmp_dropval(fs, e->f); + jmp_dropval(fs, e->t); + expr_discharge(fs, e); + if (e->k == VKNIL || e->k == VKFALSE) { + e->k = VKTRUE; + return; + } else if (expr_isk(e) || (LJ_HASFFI && e->k == VKCDATA)) { + e->k = VKFALSE; + return; + } else if (e->k == VJMP) { + invertcond(fs, e); + return; + } else if (e->k == VRELOCABLE) { + bcreg_reserve(fs, 1); + setbc_a(bcptr(fs, e), fs->freereg-1); + e->u.s.info = fs->freereg-1; + e->k = VNONRELOC; + } else { + lua_assert(e->k == VNONRELOC); + } + } else { + lua_assert(op == BC_UNM || op == BC_LEN); + if (op == BC_UNM && !expr_hasjump(e)) { /* Constant-fold negations. */ +#if LJ_HASFFI + if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */ + GCcdata *cd = cdataV(&e->u.nval); + int64_t *p = (int64_t *)cdataptr(cd); + if (cd->ctypeid == CTID_COMPLEX_DOUBLE) + p[1] ^= (int64_t)U64x(80000000,00000000); + else + *p = -*p; + return; + } else +#endif + if (expr_isnumk(e) && !expr_numiszero(e)) { /* Avoid folding to -0. */ + TValue *o = expr_numtv(e); + if (tvisint(o)) { + int32_t k = intV(o); + if (k == -k) + setnumV(o, -(lua_Number)k); + else + setintV(o, -k); + return; + } else { + o->u64 ^= U64x(80000000,00000000); + return; + } + } + } + expr_toanyreg(fs, e); + } + expr_free(fs, e); + e->u.s.info = bcemit_AD(fs, op, 0, e->u.s.info); + e->k = VRELOCABLE; +} + +/* -- Lexer support ------------------------------------------------------- */ + +/* Check and consume optional token. */ +static int lex_opt(LexState *ls, LexToken tok) +{ + if (ls->token == tok) { + lj_lex_next(ls); + return 1; + } + return 0; +} + +/* Check and consume token. */ +static void lex_check(LexState *ls, LexToken tok) +{ + if (ls->token != tok) + err_token(ls, tok); + lj_lex_next(ls); +} + +/* Check for matching token. */ +static void lex_match(LexState *ls, LexToken what, LexToken who, BCLine line) +{ + if (!lex_opt(ls, what)) { + if (line == ls->linenumber) { + err_token(ls, what); + } else { + const char *swhat = lj_lex_token2str(ls, what); + const char *swho = lj_lex_token2str(ls, who); + lj_lex_error(ls, ls->token, LJ_ERR_XMATCH, swhat, swho, line); + } + } +} + +/* Check for string token. */ +static GCstr *lex_str(LexState *ls) +{ + GCstr *s; + if (ls->token != TK_name && (LJ_52 || ls->token != TK_goto)) + err_token(ls, TK_name); + s = strV(&ls->tokenval); + lj_lex_next(ls); + return s; +} + +/* -- Variable handling --------------------------------------------------- */ + +#define var_get(ls, fs, i) ((ls)->vstack[(fs)->varmap[(i)]]) + +/* Define a new local variable. */ +static void var_new(LexState *ls, BCReg n, GCstr *name) +{ + FuncState *fs = ls->fs; + MSize vtop = ls->vtop; + checklimit(fs, fs->nactvar+n, LJ_MAX_LOCVAR, "local variables"); + if (LJ_UNLIKELY(vtop >= ls->sizevstack)) { + if (ls->sizevstack >= LJ_MAX_VSTACK) + lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); + lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo); + } + lua_assert((uintptr_t)name < VARNAME__MAX || + lj_tab_getstr(fs->kt, name) != NULL); + /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */ + setgcref(ls->vstack[vtop].name, obj2gco(name)); + fs->varmap[fs->nactvar+n] = (uint16_t)vtop; + ls->vtop = vtop+1; +} + +#define var_new_lit(ls, n, v) \ + var_new(ls, (n), lj_parse_keepstr(ls, "" v, sizeof(v)-1)) + +#define var_new_fixed(ls, n, vn) \ + var_new(ls, (n), (GCstr *)(uintptr_t)(vn)) + +/* Add local variables. */ +static void var_add(LexState *ls, BCReg nvars) +{ + FuncState *fs = ls->fs; + BCReg nactvar = fs->nactvar; + while (nvars--) { + VarInfo *v = &var_get(ls, fs, nactvar); + v->startpc = fs->pc; + v->slot = nactvar++; + v->info = 0; + } + fs->nactvar = nactvar; +} + +/* Remove local variables. */ +static void var_remove(LexState *ls, BCReg tolevel) +{ + FuncState *fs = ls->fs; + while (fs->nactvar > tolevel) + var_get(ls, fs, --fs->nactvar).endpc = fs->pc; +} + +/* Lookup local variable name. */ +static BCReg var_lookup_local(FuncState *fs, GCstr *n) +{ + int i; + for (i = fs->nactvar-1; i >= 0; i--) { + if (n == strref(var_get(fs->ls, fs, i).name)) + return (BCReg)i; + } + return (BCReg)-1; /* Not found. */ +} + +/* Lookup or add upvalue index. */ +static MSize var_lookup_uv(FuncState *fs, MSize vidx, ExpDesc *e) +{ + MSize i, n = fs->nuv; + for (i = 0; i < n; i++) + if (fs->uvmap[i] == vidx) + return i; /* Already exists. */ + /* Otherwise create a new one. */ + checklimit(fs, fs->nuv, LJ_MAX_UPVAL, "upvalues"); + lua_assert(e->k == VLOCAL || e->k == VUPVAL); + fs->uvmap[n] = (uint16_t)vidx; + fs->uvtmp[n] = (uint16_t)(e->k == VLOCAL ? vidx : LJ_MAX_VSTACK+e->u.s.info); + fs->nuv = n+1; + return n; +} + +/* Forward declaration. */ +static void fscope_uvmark(FuncState *fs, BCReg level); + +/* Recursively lookup variables in enclosing functions. */ +static MSize var_lookup_(FuncState *fs, GCstr *name, ExpDesc *e, int first) +{ + if (fs) { + BCReg reg = var_lookup_local(fs, name); + if ((int32_t)reg >= 0) { /* Local in this function? */ + expr_init(e, VLOCAL, reg); + if (!first) + fscope_uvmark(fs, reg); /* Scope now has an upvalue. */ + return (MSize)(e->u.s.aux = (uint32_t)fs->varmap[reg]); + } else { + MSize vidx = var_lookup_(fs->prev, name, e, 0); /* Var in outer func? */ + if ((int32_t)vidx >= 0) { /* Yes, make it an upvalue here. */ + e->u.s.info = (uint8_t)var_lookup_uv(fs, vidx, e); + e->k = VUPVAL; + return vidx; + } + } + } else { /* Not found in any function, must be a global. */ + expr_init(e, VGLOBAL, 0); + e->u.sval = name; + } + return (MSize)-1; /* Global. */ +} + +/* Lookup variable name. */ +#define var_lookup(ls, e) \ + var_lookup_((ls)->fs, lex_str(ls), (e), 1) + +/* -- Goto an label handling ---------------------------------------------- */ + +/* Add a new goto or label. */ +static MSize gola_new(LexState *ls, GCstr *name, uint8_t info, BCPos pc) +{ + FuncState *fs = ls->fs; + MSize vtop = ls->vtop; + if (LJ_UNLIKELY(vtop >= ls->sizevstack)) { + if (ls->sizevstack >= LJ_MAX_VSTACK) + lj_lex_error(ls, 0, LJ_ERR_XLIMC, LJ_MAX_VSTACK); + lj_mem_growvec(ls->L, ls->vstack, ls->sizevstack, LJ_MAX_VSTACK, VarInfo); + } + lua_assert(name == NAME_BREAK || lj_tab_getstr(fs->kt, name) != NULL); + /* NOBARRIER: name is anchored in fs->kt and ls->vstack is not a GCobj. */ + setgcref(ls->vstack[vtop].name, obj2gco(name)); + ls->vstack[vtop].startpc = pc; + ls->vstack[vtop].slot = (uint8_t)fs->nactvar; + ls->vstack[vtop].info = info; + ls->vtop = vtop+1; + return vtop; +} + +#define gola_isgoto(v) ((v)->info & VSTACK_GOTO) +#define gola_islabel(v) ((v)->info & VSTACK_LABEL) +#define gola_isgotolabel(v) ((v)->info & (VSTACK_GOTO|VSTACK_LABEL)) + +/* Patch goto to jump to label. */ +static void gola_patch(LexState *ls, VarInfo *vg, VarInfo *vl) +{ + FuncState *fs = ls->fs; + BCPos pc = vg->startpc; + setgcrefnull(vg->name); /* Invalidate pending goto. */ + setbc_a(&fs->bcbase[pc].ins, vl->slot); + jmp_patch(fs, pc, vl->startpc); +} + +/* Patch goto to close upvalues. */ +static void gola_close(LexState *ls, VarInfo *vg) +{ + FuncState *fs = ls->fs; + BCPos pc = vg->startpc; + BCIns *ip = &fs->bcbase[pc].ins; + lua_assert(gola_isgoto(vg)); + lua_assert(bc_op(*ip) == BC_JMP || bc_op(*ip) == BC_UCLO); + setbc_a(ip, vg->slot); + if (bc_op(*ip) == BC_JMP) { + BCPos next = jmp_next(fs, pc); + if (next != NO_JMP) jmp_patch(fs, next, pc); /* Jump to UCLO. */ + setbc_op(ip, BC_UCLO); /* Turn into UCLO. */ + setbc_j(ip, NO_JMP); + } +} + +/* Resolve pending forward gotos for label. */ +static void gola_resolve(LexState *ls, FuncScope *bl, MSize idx) +{ + VarInfo *vg = ls->vstack + bl->vstart; + VarInfo *vl = ls->vstack + idx; + for (; vg < vl; vg++) + if (gcrefeq(vg->name, vl->name) && gola_isgoto(vg)) { + if (vg->slot < vl->slot) { + GCstr *name = strref(var_get(ls, ls->fs, vg->slot).name); + lua_assert((uintptr_t)name >= VARNAME__MAX); + ls->linenumber = ls->fs->bcbase[vg->startpc].line; + lua_assert(strref(vg->name) != NAME_BREAK); + lj_lex_error(ls, 0, LJ_ERR_XGSCOPE, + strdata(strref(vg->name)), strdata(name)); + } + gola_patch(ls, vg, vl); + } +} + +/* Fixup remaining gotos and labels for scope. */ +static void gola_fixup(LexState *ls, FuncScope *bl) +{ + VarInfo *v = ls->vstack + bl->vstart; + VarInfo *ve = ls->vstack + ls->vtop; + for (; v < ve; v++) { + GCstr *name = strref(v->name); + if (name != NULL) { /* Only consider remaining valid gotos/labels. */ + if (gola_islabel(v)) { + VarInfo *vg; + setgcrefnull(v->name); /* Invalidate label that goes out of scope. */ + for (vg = v+1; vg < ve; vg++) /* Resolve pending backward gotos. */ + if (strref(vg->name) == name && gola_isgoto(vg)) { + if ((bl->flags&FSCOPE_UPVAL) && vg->slot > v->slot) + gola_close(ls, vg); + gola_patch(ls, vg, v); + } + } else if (gola_isgoto(v)) { + if (bl->prev) { /* Propagate goto or break to outer scope. */ + bl->prev->flags |= name == NAME_BREAK ? FSCOPE_BREAK : FSCOPE_GOLA; + v->slot = bl->nactvar; + if ((bl->flags & FSCOPE_UPVAL)) + gola_close(ls, v); + } else { /* No outer scope: undefined goto label or no loop. */ + ls->linenumber = ls->fs->bcbase[v->startpc].line; + if (name == NAME_BREAK) + lj_lex_error(ls, 0, LJ_ERR_XBREAK); + else + lj_lex_error(ls, 0, LJ_ERR_XLUNDEF, strdata(name)); + } + } + } + } +} + +/* Find existing label. */ +static VarInfo *gola_findlabel(LexState *ls, GCstr *name) +{ + VarInfo *v = ls->vstack + ls->fs->bl->vstart; + VarInfo *ve = ls->vstack + ls->vtop; + for (; v < ve; v++) + if (strref(v->name) == name && gola_islabel(v)) + return v; + return NULL; +} + +/* -- Scope handling ------------------------------------------------------ */ + +/* Begin a scope. */ +static void fscope_begin(FuncState *fs, FuncScope *bl, int flags) +{ + bl->nactvar = (uint8_t)fs->nactvar; + bl->flags = flags; + bl->vstart = fs->ls->vtop; + bl->prev = fs->bl; + fs->bl = bl; + lua_assert(fs->freereg == fs->nactvar); +} + +/* End a scope. */ +static void fscope_end(FuncState *fs) +{ + FuncScope *bl = fs->bl; + LexState *ls = fs->ls; + fs->bl = bl->prev; + var_remove(ls, bl->nactvar); + fs->freereg = fs->nactvar; + lua_assert(bl->nactvar == fs->nactvar); + if ((bl->flags & (FSCOPE_UPVAL|FSCOPE_NOCLOSE)) == FSCOPE_UPVAL) + bcemit_AJ(fs, BC_UCLO, bl->nactvar, 0); + if ((bl->flags & FSCOPE_BREAK)) { + if ((bl->flags & FSCOPE_LOOP)) { + MSize idx = gola_new(ls, NAME_BREAK, VSTACK_LABEL, fs->pc); + ls->vtop = idx; /* Drop break label immediately. */ + gola_resolve(ls, bl, idx); + return; + } /* else: need the fixup step to propagate the breaks. */ + } else if (!(bl->flags & FSCOPE_GOLA)) { + return; + } + gola_fixup(ls, bl); +} + +/* Mark scope as having an upvalue. */ +static void fscope_uvmark(FuncState *fs, BCReg level) +{ + FuncScope *bl; + for (bl = fs->bl; bl && bl->nactvar > level; bl = bl->prev) + ; + if (bl) + bl->flags |= FSCOPE_UPVAL; +} + +/* -- Function state management ------------------------------------------- */ + +/* Fixup bytecode for prototype. */ +static void fs_fixup_bc(FuncState *fs, GCproto *pt, BCIns *bc, MSize n) +{ + BCInsLine *base = fs->bcbase; + MSize i; + pt->sizebc = n; + bc[0] = BCINS_AD((fs->flags & PROTO_VARARG) ? BC_FUNCV : BC_FUNCF, + fs->framesize, 0); + for (i = 1; i < n; i++) + bc[i] = base[i].ins; +} + +/* Fixup upvalues for child prototype, step #2. */ +static void fs_fixup_uv2(FuncState *fs, GCproto *pt) +{ + VarInfo *vstack = fs->ls->vstack; + uint16_t *uv = proto_uv(pt); + MSize i, n = pt->sizeuv; + for (i = 0; i < n; i++) { + VarIndex vidx = uv[i]; + if (vidx >= LJ_MAX_VSTACK) + uv[i] = vidx - LJ_MAX_VSTACK; + else if ((vstack[vidx].info & VSTACK_VAR_RW)) + uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL; + else + uv[i] = vstack[vidx].slot | PROTO_UV_LOCAL | PROTO_UV_IMMUTABLE; + } +} + +/* Fixup constants for prototype. */ +static void fs_fixup_k(FuncState *fs, GCproto *pt, void *kptr) +{ + GCtab *kt; + TValue *array; + Node *node; + MSize i, hmask; + checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants"); + checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants"); + setmref(pt->k, kptr); + pt->sizekn = fs->nkn; + pt->sizekgc = fs->nkgc; + kt = fs->kt; + array = tvref(kt->array); + for (i = 0; i < kt->asize; i++) + if (tvhaskslot(&array[i])) { + TValue *tv = &((TValue *)kptr)[tvkslot(&array[i])]; + if (LJ_DUALNUM) + setintV(tv, (int32_t)i); + else + setnumV(tv, (lua_Number)i); + } + node = noderef(kt->node); + hmask = kt->hmask; + for (i = 0; i <= hmask; i++) { + Node *n = &node[i]; + if (tvhaskslot(&n->val)) { + ptrdiff_t kidx = (ptrdiff_t)tvkslot(&n->val); + lua_assert(!tvisint(&n->key)); + if (tvisnum(&n->key)) { + TValue *tv = &((TValue *)kptr)[kidx]; + if (LJ_DUALNUM) { + lua_Number nn = numV(&n->key); + int32_t k = lj_num2int(nn); + lua_assert(!tvismzero(&n->key)); + if ((lua_Number)k == nn) + setintV(tv, k); + else + *tv = n->key; + } else { + *tv = n->key; + } + } else { + GCobj *o = gcV(&n->key); + setgcref(((GCRef *)kptr)[~kidx], o); + lj_gc_objbarrier(fs->L, pt, o); + if (tvisproto(&n->key)) + fs_fixup_uv2(fs, gco2pt(o)); + } + } + } +} + +/* Fixup upvalues for prototype, step #1. */ +static void fs_fixup_uv1(FuncState *fs, GCproto *pt, uint16_t *uv) +{ + setmref(pt->uv, uv); + pt->sizeuv = fs->nuv; + memcpy(uv, fs->uvtmp, fs->nuv*sizeof(VarIndex)); +} + +#ifndef LUAJIT_DISABLE_DEBUGINFO +/* Prepare lineinfo for prototype. */ +static size_t fs_prep_line(FuncState *fs, BCLine numline) +{ + return (fs->pc-1) << (numline < 256 ? 0 : numline < 65536 ? 1 : 2); +} + +/* Fixup lineinfo for prototype. */ +static void fs_fixup_line(FuncState *fs, GCproto *pt, + void *lineinfo, BCLine numline) +{ + BCInsLine *base = fs->bcbase + 1; + BCLine first = fs->linedefined; + MSize i = 0, n = fs->pc-1; + pt->firstline = fs->linedefined; + pt->numline = numline; + setmref(pt->lineinfo, lineinfo); + if (LJ_LIKELY(numline < 256)) { + uint8_t *li = (uint8_t *)lineinfo; + do { + BCLine delta = base[i].line - first; + lua_assert(delta >= 0 && delta < 256); + li[i] = (uint8_t)delta; + } while (++i < n); + } else if (LJ_LIKELY(numline < 65536)) { + uint16_t *li = (uint16_t *)lineinfo; + do { + BCLine delta = base[i].line - first; + lua_assert(delta >= 0 && delta < 65536); + li[i] = (uint16_t)delta; + } while (++i < n); + } else { + uint32_t *li = (uint32_t *)lineinfo; + do { + BCLine delta = base[i].line - first; + lua_assert(delta >= 0); + li[i] = (uint32_t)delta; + } while (++i < n); + } +} + +/* Resize buffer if needed. */ +static LJ_NOINLINE void fs_buf_resize(LexState *ls, MSize len) +{ + MSize sz = ls->sb.sz * 2; + while (ls->sb.n + len > sz) sz = sz * 2; + lj_str_resizebuf(ls->L, &ls->sb, sz); +} + +static LJ_AINLINE void fs_buf_need(LexState *ls, MSize len) +{ + if (LJ_UNLIKELY(ls->sb.n + len > ls->sb.sz)) + fs_buf_resize(ls, len); +} + +/* Add string to buffer. */ +static void fs_buf_str(LexState *ls, const char *str, MSize len) +{ + char *p = ls->sb.buf + ls->sb.n; + MSize i; + ls->sb.n += len; + for (i = 0; i < len; i++) p[i] = str[i]; +} + +/* Add ULEB128 value to buffer. */ +static void fs_buf_uleb128(LexState *ls, uint32_t v) +{ + MSize n = ls->sb.n; + uint8_t *p = (uint8_t *)ls->sb.buf; + for (; v >= 0x80; v >>= 7) + p[n++] = (uint8_t)((v & 0x7f) | 0x80); + p[n++] = (uint8_t)v; + ls->sb.n = n; +} + +/* Prepare variable info for prototype. */ +static size_t fs_prep_var(LexState *ls, FuncState *fs, size_t *ofsvar) +{ + VarInfo *vs =ls->vstack, *ve; + MSize i, n; + BCPos lastpc; + lj_str_resetbuf(&ls->sb); /* Copy to temp. string buffer. */ + /* Store upvalue names. */ + for (i = 0, n = fs->nuv; i < n; i++) { + GCstr *s = strref(vs[fs->uvmap[i]].name); + MSize len = s->len+1; + fs_buf_need(ls, len); + fs_buf_str(ls, strdata(s), len); + } + *ofsvar = ls->sb.n; + lastpc = 0; + /* Store local variable names and compressed ranges. */ + for (ve = vs + ls->vtop, vs += fs->vbase; vs < ve; vs++) { + if (!gola_isgotolabel(vs)) { + GCstr *s = strref(vs->name); + BCPos startpc; + if ((uintptr_t)s < VARNAME__MAX) { + fs_buf_need(ls, 1 + 2*5); + ls->sb.buf[ls->sb.n++] = (uint8_t)(uintptr_t)s; + } else { + MSize len = s->len+1; + fs_buf_need(ls, len + 2*5); + fs_buf_str(ls, strdata(s), len); + } + startpc = vs->startpc; + fs_buf_uleb128(ls, startpc-lastpc); + fs_buf_uleb128(ls, vs->endpc-startpc); + lastpc = startpc; + } + } + fs_buf_need(ls, 1); + ls->sb.buf[ls->sb.n++] = '\0'; /* Terminator for varinfo. */ + return ls->sb.n; +} + +/* Fixup variable info for prototype. */ +static void fs_fixup_var(LexState *ls, GCproto *pt, uint8_t *p, size_t ofsvar) +{ + setmref(pt->uvinfo, p); + setmref(pt->varinfo, (char *)p + ofsvar); + memcpy(p, ls->sb.buf, ls->sb.n); /* Copy from temp. string buffer. */ +} +#else + +/* Initialize with empty debug info, if disabled. */ +#define fs_prep_line(fs, numline) (UNUSED(numline), 0) +#define fs_fixup_line(fs, pt, li, numline) \ + pt->firstline = pt->numline = 0, setmref((pt)->lineinfo, NULL) +#define fs_prep_var(ls, fs, ofsvar) (UNUSED(ofsvar), 0) +#define fs_fixup_var(ls, pt, p, ofsvar) \ + setmref((pt)->uvinfo, NULL), setmref((pt)->varinfo, NULL) + +#endif + +/* Check if bytecode op returns. */ +static int bcopisret(BCOp op) +{ + switch (op) { + case BC_CALLMT: case BC_CALLT: + case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1: + return 1; + default: + return 0; + } +} + +/* Fixup return instruction for prototype. */ +static void fs_fixup_ret(FuncState *fs) +{ + BCPos lastpc = fs->pc; + if (lastpc <= fs->lasttarget || !bcopisret(bc_op(fs->bcbase[lastpc-1].ins))) { + if ((fs->bl->flags & FSCOPE_UPVAL)) + bcemit_AJ(fs, BC_UCLO, 0, 0); + bcemit_AD(fs, BC_RET0, 0, 1); /* Need final return. */ + } + fs->bl->flags |= FSCOPE_NOCLOSE; /* Handled above. */ + fscope_end(fs); + lua_assert(fs->bl == NULL); + /* May need to fixup returns encoded before first function was created. */ + if (fs->flags & PROTO_FIXUP_RETURN) { + BCPos pc; + for (pc = 1; pc < lastpc; pc++) { + BCIns ins = fs->bcbase[pc].ins; + BCPos offset; + switch (bc_op(ins)) { + case BC_CALLMT: case BC_CALLT: + case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1: + offset = bcemit_INS(fs, ins)-(pc+1)+BCBIAS_J; /* Copy return ins. */ + if (offset > BCMAX_D) + err_syntax(fs->ls, LJ_ERR_XFIXUP); + /* Replace with UCLO plus branch. */ + fs->bcbase[pc].ins = BCINS_AD(BC_UCLO, 0, offset); + break; + case BC_UCLO: + return; /* We're done. */ + default: + break; + } + } + } +} + +/* Finish a FuncState and return the new prototype. */ +static GCproto *fs_finish(LexState *ls, BCLine line) +{ + lua_State *L = ls->L; + FuncState *fs = ls->fs; + BCLine numline = line - fs->linedefined; + size_t sizept, ofsk, ofsuv, ofsli, ofsdbg, ofsvar; + GCproto *pt; + + /* Apply final fixups. */ + fs_fixup_ret(fs); + + /* Calculate total size of prototype including all colocated arrays. */ + sizept = sizeof(GCproto) + fs->pc*sizeof(BCIns) + fs->nkgc*sizeof(GCRef); + sizept = (sizept + sizeof(TValue)-1) & ~(sizeof(TValue)-1); + ofsk = sizept; sizept += fs->nkn*sizeof(TValue); + ofsuv = sizept; sizept += ((fs->nuv+1)&~1)*2; + ofsli = sizept; sizept += fs_prep_line(fs, numline); + ofsdbg = sizept; sizept += fs_prep_var(ls, fs, &ofsvar); + + /* Allocate prototype and initialize its fields. */ + pt = (GCproto *)lj_mem_newgco(L, (MSize)sizept); + pt->gct = ~LJ_TPROTO; + pt->sizept = (MSize)sizept; + pt->trace = 0; + pt->flags = (uint8_t)(fs->flags & ~(PROTO_HAS_RETURN|PROTO_FIXUP_RETURN)); + pt->numparams = fs->numparams; + pt->framesize = fs->framesize; + setgcref(pt->chunkname, obj2gco(ls->chunkname)); + + /* Close potentially uninitialized gap between bc and kgc. */ + *(uint32_t *)((char *)pt + ofsk - sizeof(GCRef)*(fs->nkgc+1)) = 0; + fs_fixup_bc(fs, pt, (BCIns *)((char *)pt + sizeof(GCproto)), fs->pc); + fs_fixup_k(fs, pt, (void *)((char *)pt + ofsk)); + fs_fixup_uv1(fs, pt, (uint16_t *)((char *)pt + ofsuv)); + fs_fixup_line(fs, pt, (void *)((char *)pt + ofsli), numline); + fs_fixup_var(ls, pt, (uint8_t *)((char *)pt + ofsdbg), ofsvar); + + lj_vmevent_send(L, BC, + setprotoV(L, L->top++, pt); + ); + + L->top--; /* Pop table of constants. */ + ls->vtop = fs->vbase; /* Reset variable stack. */ + ls->fs = fs->prev; + lua_assert(ls->fs != NULL || ls->token == TK_eof); + return pt; +} + +/* Initialize a new FuncState. */ +static void fs_init(LexState *ls, FuncState *fs) +{ + lua_State *L = ls->L; + fs->prev = ls->fs; ls->fs = fs; /* Append to list. */ + fs->ls = ls; + fs->vbase = ls->vtop; + fs->L = L; + fs->pc = 0; + fs->lasttarget = 0; + fs->jpc = NO_JMP; + fs->freereg = 0; + fs->nkgc = 0; + fs->nkn = 0; + fs->nactvar = 0; + fs->nuv = 0; + fs->bl = NULL; + fs->flags = 0; + fs->framesize = 1; /* Minimum frame size. */ + fs->kt = lj_tab_new(L, 0, 0); + /* Anchor table of constants in stack to avoid being collected. */ + settabV(L, L->top, fs->kt); + incr_top(L); +} + +/* -- Expressions --------------------------------------------------------- */ + +/* Forward declaration. */ +static void expr(LexState *ls, ExpDesc *v); + +/* Return string expression. */ +static void expr_str(LexState *ls, ExpDesc *e) +{ + expr_init(e, VKSTR, 0); + e->u.sval = lex_str(ls); +} + +/* Return index expression. */ +static void expr_index(FuncState *fs, ExpDesc *t, ExpDesc *e) +{ + /* Already called: expr_toval(fs, e). */ + t->k = VINDEXED; + if (expr_isnumk(e)) { +#if LJ_DUALNUM + if (tvisint(expr_numtv(e))) { + int32_t k = intV(expr_numtv(e)); + if (checku8(k)) { + t->u.s.aux = BCMAX_C+1+(uint32_t)k; /* 256..511: const byte key */ + return; + } + } +#else + lua_Number n = expr_numberV(e); + int32_t k = lj_num2int(n); + if (checku8(k) && n == (lua_Number)k) { + t->u.s.aux = BCMAX_C+1+(uint32_t)k; /* 256..511: const byte key */ + return; + } +#endif + } else if (expr_isstrk(e)) { + BCReg idx = const_str(fs, e); + if (idx <= BCMAX_C) { + t->u.s.aux = ~idx; /* -256..-1: const string key */ + return; + } + } + t->u.s.aux = expr_toanyreg(fs, e); /* 0..255: register */ +} + +/* Parse index expression with named field. */ +static void expr_field(LexState *ls, ExpDesc *v) +{ + FuncState *fs = ls->fs; + ExpDesc key; + expr_toanyreg(fs, v); + lj_lex_next(ls); /* Skip dot or colon. */ + expr_str(ls, &key); + expr_index(fs, v, &key); +} + +/* Parse index expression with brackets. */ +static void expr_bracket(LexState *ls, ExpDesc *v) +{ + lj_lex_next(ls); /* Skip '['. */ + expr(ls, v); + expr_toval(ls->fs, v); + lex_check(ls, ']'); +} + +/* Get value of constant expression. */ +static void expr_kvalue(TValue *v, ExpDesc *e) +{ + if (e->k <= VKTRUE) { + setitype(v, ~(uint32_t)e->k); + } else if (e->k == VKSTR) { + setgcref(v->gcr, obj2gco(e->u.sval)); + setitype(v, LJ_TSTR); + } else { + lua_assert(tvisnumber(expr_numtv(e))); + *v = *expr_numtv(e); + } +} + +/* Parse table constructor expression. */ +static void expr_table(LexState *ls, ExpDesc *e) +{ + FuncState *fs = ls->fs; + BCLine line = ls->linenumber; + GCtab *t = NULL; + int vcall = 0, needarr = 0, fixt = 0; + uint32_t narr = 1; /* First array index. */ + uint32_t nhash = 0; /* Number of hash entries. */ + BCReg freg = fs->freereg; + BCPos pc = bcemit_AD(fs, BC_TNEW, freg, 0); + expr_init(e, VNONRELOC, freg); + bcreg_reserve(fs, 1); + freg++; + lex_check(ls, '{'); + while (ls->token != '}') { + ExpDesc key, val; + vcall = 0; + if (ls->token == '[') { + expr_bracket(ls, &key); /* Already calls expr_toval. */ + if (!expr_isk(&key)) expr_index(fs, e, &key); + if (expr_isnumk(&key) && expr_numiszero(&key)) needarr = 1; else nhash++; + lex_check(ls, '='); + } else if ((ls->token == TK_name || (!LJ_52 && ls->token == TK_goto)) && + lj_lex_lookahead(ls) == '=') { + expr_str(ls, &key); + lex_check(ls, '='); + nhash++; + } else { + expr_init(&key, VKNUM, 0); + setintV(&key.u.nval, (int)narr); + narr++; + needarr = vcall = 1; + } + expr(ls, &val); + if (expr_isk(&key) && key.k != VKNIL && + (key.k == VKSTR || expr_isk_nojump(&val))) { + TValue k, *v; + if (!t) { /* Create template table on demand. */ + BCReg kidx; + t = lj_tab_new(fs->L, needarr ? narr : 0, hsize2hbits(nhash)); + kidx = const_gc(fs, obj2gco(t), LJ_TTAB); + fs->bcbase[pc].ins = BCINS_AD(BC_TDUP, freg-1, kidx); + } + vcall = 0; + expr_kvalue(&k, &key); + v = lj_tab_set(fs->L, t, &k); + lj_gc_anybarriert(fs->L, t); + if (expr_isk_nojump(&val)) { /* Add const key/value to template table. */ + expr_kvalue(v, &val); + } else { /* Otherwise create dummy string key (avoids lj_tab_newkey). */ + settabV(fs->L, v, t); /* Preserve key with table itself as value. */ + fixt = 1; /* Fix this later, after all resizes. */ + goto nonconst; + } + } else { + nonconst: + if (val.k != VCALL) { expr_toanyreg(fs, &val); vcall = 0; } + if (expr_isk(&key)) expr_index(fs, e, &key); + bcemit_store(fs, e, &val); + } + fs->freereg = freg; + if (!lex_opt(ls, ',') && !lex_opt(ls, ';')) break; + } + lex_match(ls, '}', '{', line); + if (vcall) { + BCInsLine *ilp = &fs->bcbase[fs->pc-1]; + ExpDesc en; + lua_assert(bc_a(ilp->ins) == freg && + bc_op(ilp->ins) == (narr > 256 ? BC_TSETV : BC_TSETB)); + expr_init(&en, VKNUM, 0); + en.u.nval.u32.lo = narr-1; + en.u.nval.u32.hi = 0x43300000; /* Biased integer to avoid denormals. */ + if (narr > 256) { fs->pc--; ilp--; } + ilp->ins = BCINS_AD(BC_TSETM, freg, const_num(fs, &en)); + setbc_b(&ilp[-1].ins, 0); + } + if (pc == fs->pc-1) { /* Make expr relocable if possible. */ + e->u.s.info = pc; + fs->freereg--; + e->k = VRELOCABLE; + } else { + e->k = VNONRELOC; /* May have been changed by expr_index. */ + } + if (!t) { /* Construct TNEW RD: hhhhhaaaaaaaaaaa. */ + BCIns *ip = &fs->bcbase[pc].ins; + if (!needarr) narr = 0; + else if (narr < 3) narr = 3; + else if (narr > 0x7ff) narr = 0x7ff; + setbc_d(ip, narr|(hsize2hbits(nhash)<<11)); + } else { + if (needarr && t->asize < narr) + lj_tab_reasize(fs->L, t, narr-1); + if (fixt) { /* Fix value for dummy keys in template table. */ + Node *node = noderef(t->node); + uint32_t i, hmask = t->hmask; + for (i = 0; i <= hmask; i++) { + Node *n = &node[i]; + if (tvistab(&n->val)) { + lua_assert(tabV(&n->val) == t); + setnilV(&n->val); /* Turn value into nil. */ + } + } + } + lj_gc_check(fs->L); + } +} + +/* Parse function parameters. */ +static BCReg parse_params(LexState *ls, int needself) +{ + FuncState *fs = ls->fs; + BCReg nparams = 0; + lex_check(ls, '('); + if (needself) + var_new_lit(ls, nparams++, "self"); + if (ls->token != ')') { + do { + if (ls->token == TK_name || (!LJ_52 && ls->token == TK_goto)) { + var_new(ls, nparams++, lex_str(ls)); + } else if (ls->token == TK_dots) { + lj_lex_next(ls); + fs->flags |= PROTO_VARARG; + break; + } else { + err_syntax(ls, LJ_ERR_XPARAM); + } + } while (lex_opt(ls, ',')); + } + var_add(ls, nparams); + lua_assert(fs->nactvar == nparams); + bcreg_reserve(fs, nparams); + lex_check(ls, ')'); + return nparams; +} + +/* Forward declaration. */ +static void parse_chunk(LexState *ls); + +/* Parse body of a function. */ +static void parse_body(LexState *ls, ExpDesc *e, int needself, BCLine line) +{ + FuncState fs, *pfs = ls->fs; + FuncScope bl; + GCproto *pt; + ptrdiff_t oldbase = pfs->bcbase - ls->bcstack; + fs_init(ls, &fs); + fscope_begin(&fs, &bl, 0); + fs.linedefined = line; + fs.numparams = (uint8_t)parse_params(ls, needself); + fs.bcbase = pfs->bcbase + pfs->pc; + fs.bclim = pfs->bclim - pfs->pc; + bcemit_AD(&fs, BC_FUNCF, 0, 0); /* Placeholder. */ + parse_chunk(ls); + if (ls->token != TK_end) lex_match(ls, TK_end, TK_function, line); + pt = fs_finish(ls, (ls->lastline = ls->linenumber)); + pfs->bcbase = ls->bcstack + oldbase; /* May have been reallocated. */ + pfs->bclim = (BCPos)(ls->sizebcstack - oldbase); + /* Store new prototype in the constant array of the parent. */ + expr_init(e, VRELOCABLE, + bcemit_AD(pfs, BC_FNEW, 0, const_gc(pfs, obj2gco(pt), LJ_TPROTO))); +#if LJ_HASFFI + pfs->flags |= (fs.flags & PROTO_FFI); +#endif + if (!(pfs->flags & PROTO_CHILD)) { + if (pfs->flags & PROTO_HAS_RETURN) + pfs->flags |= PROTO_FIXUP_RETURN; + pfs->flags |= PROTO_CHILD; + } + lj_lex_next(ls); +} + +/* Parse expression list. Last expression is left open. */ +static BCReg expr_list(LexState *ls, ExpDesc *v) +{ + BCReg n = 1; + expr(ls, v); + while (lex_opt(ls, ',')) { + expr_tonextreg(ls->fs, v); + expr(ls, v); + n++; + } + return n; +} + +/* Parse function argument list. */ +static void parse_args(LexState *ls, ExpDesc *e) +{ + FuncState *fs = ls->fs; + ExpDesc args; + BCIns ins; + BCReg base; + BCLine line = ls->linenumber; + if (ls->token == '(') { +#if !LJ_52 + if (line != ls->lastline) + err_syntax(ls, LJ_ERR_XAMBIG); +#endif + lj_lex_next(ls); + if (ls->token == ')') { /* f(). */ + args.k = VVOID; + } else { + expr_list(ls, &args); + if (args.k == VCALL) /* f(a, b, g()) or f(a, b, ...). */ + setbc_b(bcptr(fs, &args), 0); /* Pass on multiple results. */ + } + lex_match(ls, ')', '(', line); + } else if (ls->token == '{') { + expr_table(ls, &args); + } else if (ls->token == TK_string) { + expr_init(&args, VKSTR, 0); + args.u.sval = strV(&ls->tokenval); + lj_lex_next(ls); + } else { + err_syntax(ls, LJ_ERR_XFUNARG); + return; /* Silence compiler. */ + } + lua_assert(e->k == VNONRELOC); + base = e->u.s.info; /* Base register for call. */ + if (args.k == VCALL) { + ins = BCINS_ABC(BC_CALLM, base, 2, args.u.s.aux - base - 1); + } else { + if (args.k != VVOID) + expr_tonextreg(fs, &args); + ins = BCINS_ABC(BC_CALL, base, 2, fs->freereg - base); + } + expr_init(e, VCALL, bcemit_INS(fs, ins)); + e->u.s.aux = base; + fs->bcbase[fs->pc - 1].line = line; + fs->freereg = base+1; /* Leave one result by default. */ +} + +/* Parse primary expression. */ +static void expr_primary(LexState *ls, ExpDesc *v) +{ + FuncState *fs = ls->fs; + /* Parse prefix expression. */ + if (ls->token == '(') { + BCLine line = ls->linenumber; + lj_lex_next(ls); + expr(ls, v); + lex_match(ls, ')', '(', line); + expr_discharge(ls->fs, v); + } else if (ls->token == TK_name || (!LJ_52 && ls->token == TK_goto)) { + var_lookup(ls, v); + } else { + err_syntax(ls, LJ_ERR_XSYMBOL); + } + for (;;) { /* Parse multiple expression suffixes. */ + if (ls->token == '.') { + expr_field(ls, v); + } else if (ls->token == '[') { + ExpDesc key; + expr_toanyreg(fs, v); + expr_bracket(ls, &key); + expr_index(fs, v, &key); + } else if (ls->token == ':') { + ExpDesc key; + lj_lex_next(ls); + expr_str(ls, &key); + bcemit_method(fs, v, &key); + parse_args(ls, v); + } else if (ls->token == '(' || ls->token == TK_string || ls->token == '{') { + expr_tonextreg(fs, v); + parse_args(ls, v); + } else { + break; + } + } +} + +/* Parse simple expression. */ +static void expr_simple(LexState *ls, ExpDesc *v) +{ + switch (ls->token) { + case TK_number: + expr_init(v, (LJ_HASFFI && tviscdata(&ls->tokenval)) ? VKCDATA : VKNUM, 0); + copyTV(ls->L, &v->u.nval, &ls->tokenval); + break; + case TK_string: + expr_init(v, VKSTR, 0); + v->u.sval = strV(&ls->tokenval); + break; + case TK_nil: + expr_init(v, VKNIL, 0); + break; + case TK_true: + expr_init(v, VKTRUE, 0); + break; + case TK_false: + expr_init(v, VKFALSE, 0); + break; + case TK_dots: { /* Vararg. */ + FuncState *fs = ls->fs; + BCReg base; + checkcond(ls, fs->flags & PROTO_VARARG, LJ_ERR_XDOTS); + bcreg_reserve(fs, 1); + base = fs->freereg-1; + expr_init(v, VCALL, bcemit_ABC(fs, BC_VARG, base, 2, fs->numparams)); + v->u.s.aux = base; + break; + } + case '{': /* Table constructor. */ + expr_table(ls, v); + return; + case TK_function: + lj_lex_next(ls); + parse_body(ls, v, 0, ls->linenumber); + return; + default: + expr_primary(ls, v); + return; + } + lj_lex_next(ls); +} + +/* Manage syntactic levels to avoid blowing up the stack. */ +static void synlevel_begin(LexState *ls) +{ + if (++ls->level >= LJ_MAX_XLEVEL) + lj_lex_error(ls, 0, LJ_ERR_XLEVELS); +} + +#define synlevel_end(ls) ((ls)->level--) + +/* Convert token to binary operator. */ +static BinOpr token2binop(LexToken tok) +{ + switch (tok) { + case '+': return OPR_ADD; + case '-': return OPR_SUB; + case '*': return OPR_MUL; + case '/': return OPR_DIV; + case '%': return OPR_MOD; + case '^': return OPR_POW; + case TK_concat: return OPR_CONCAT; + case TK_ne: return OPR_NE; + case TK_eq: return OPR_EQ; + case '<': return OPR_LT; + case TK_le: return OPR_LE; + case '>': return OPR_GT; + case TK_ge: return OPR_GE; + case TK_and: return OPR_AND; + case TK_or: return OPR_OR; + default: return OPR_NOBINOPR; + } +} + +/* Priorities for each binary operator. ORDER OPR. */ +static const struct { + uint8_t left; /* Left priority. */ + uint8_t right; /* Right priority. */ +} priority[] = { + {6,6}, {6,6}, {7,7}, {7,7}, {7,7}, /* ADD SUB MUL DIV MOD */ + {10,9}, {5,4}, /* POW CONCAT (right associative) */ + {3,3}, {3,3}, /* EQ NE */ + {3,3}, {3,3}, {3,3}, {3,3}, /* LT GE GT LE */ + {2,2}, {1,1} /* AND OR */ +}; + +#define UNARY_PRIORITY 8 /* Priority for unary operators. */ + +/* Forward declaration. */ +static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit); + +/* Parse unary expression. */ +static void expr_unop(LexState *ls, ExpDesc *v) +{ + BCOp op; + if (ls->token == TK_not) { + op = BC_NOT; + } else if (ls->token == '-') { + op = BC_UNM; + } else if (ls->token == '#') { + op = BC_LEN; + } else { + expr_simple(ls, v); + return; + } + lj_lex_next(ls); + expr_binop(ls, v, UNARY_PRIORITY); + bcemit_unop(ls->fs, op, v); +} + +/* Parse binary expressions with priority higher than the limit. */ +static BinOpr expr_binop(LexState *ls, ExpDesc *v, uint32_t limit) +{ + BinOpr op; + synlevel_begin(ls); + expr_unop(ls, v); + op = token2binop(ls->token); + while (op != OPR_NOBINOPR && priority[op].left > limit) { + ExpDesc v2; + BinOpr nextop; + lj_lex_next(ls); + bcemit_binop_left(ls->fs, op, v); + /* Parse binary expression with higher priority. */ + nextop = expr_binop(ls, &v2, priority[op].right); + bcemit_binop(ls->fs, op, v, &v2); + op = nextop; + } + synlevel_end(ls); + return op; /* Return unconsumed binary operator (if any). */ +} + +/* Parse expression. */ +static void expr(LexState *ls, ExpDesc *v) +{ + expr_binop(ls, v, 0); /* Priority 0: parse whole expression. */ +} + +/* Assign expression to the next register. */ +static void expr_next(LexState *ls) +{ + ExpDesc e; + expr(ls, &e); + expr_tonextreg(ls->fs, &e); +} + +/* Parse conditional expression. */ +static BCPos expr_cond(LexState *ls) +{ + ExpDesc v; + expr(ls, &v); + if (v.k == VKNIL) v.k = VKFALSE; + bcemit_branch_t(ls->fs, &v); + return v.f; +} + +/* -- Assignments --------------------------------------------------------- */ + +/* List of LHS variables. */ +typedef struct LHSVarList { + ExpDesc v; /* LHS variable. */ + struct LHSVarList *prev; /* Link to previous LHS variable. */ +} LHSVarList; + +/* Eliminate write-after-read hazards for local variable assignment. */ +static void assign_hazard(LexState *ls, LHSVarList *lh, const ExpDesc *v) +{ + FuncState *fs = ls->fs; + BCReg reg = v->u.s.info; /* Check against this variable. */ + BCReg tmp = fs->freereg; /* Rename to this temp. register (if needed). */ + int hazard = 0; + for (; lh; lh = lh->prev) { + if (lh->v.k == VINDEXED) { + if (lh->v.u.s.info == reg) { /* t[i], t = 1, 2 */ + hazard = 1; + lh->v.u.s.info = tmp; + } + if (lh->v.u.s.aux == reg) { /* t[i], i = 1, 2 */ + hazard = 1; + lh->v.u.s.aux = tmp; + } + } + } + if (hazard) { + bcemit_AD(fs, BC_MOV, tmp, reg); /* Rename conflicting variable. */ + bcreg_reserve(fs, 1); + } +} + +/* Adjust LHS/RHS of an assignment. */ +static void assign_adjust(LexState *ls, BCReg nvars, BCReg nexps, ExpDesc *e) +{ + FuncState *fs = ls->fs; + int32_t extra = (int32_t)nvars - (int32_t)nexps; + if (e->k == VCALL) { + extra++; /* Compensate for the VCALL itself. */ + if (extra < 0) extra = 0; + setbc_b(bcptr(fs, e), extra+1); /* Fixup call results. */ + if (extra > 1) bcreg_reserve(fs, (BCReg)extra-1); + } else { + if (e->k != VVOID) + expr_tonextreg(fs, e); /* Close last expression. */ + if (extra > 0) { /* Leftover LHS are set to nil. */ + BCReg reg = fs->freereg; + bcreg_reserve(fs, (BCReg)extra); + bcemit_nil(fs, reg, (BCReg)extra); + } + } +} + +/* Recursively parse assignment statement. */ +static void parse_assignment(LexState *ls, LHSVarList *lh, BCReg nvars) +{ + ExpDesc e; + checkcond(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, LJ_ERR_XSYNTAX); + if (lex_opt(ls, ',')) { /* Collect LHS list and recurse upwards. */ + LHSVarList vl; + vl.prev = lh; + expr_primary(ls, &vl.v); + if (vl.v.k == VLOCAL) + assign_hazard(ls, lh, &vl.v); + checklimit(ls->fs, ls->level + nvars, LJ_MAX_XLEVEL, "variable names"); + parse_assignment(ls, &vl, nvars+1); + } else { /* Parse RHS. */ + BCReg nexps; + lex_check(ls, '='); + nexps = expr_list(ls, &e); + if (nexps == nvars) { + if (e.k == VCALL) { + if (bc_op(*bcptr(ls->fs, &e)) == BC_VARG) { /* Vararg assignment. */ + ls->fs->freereg--; + e.k = VRELOCABLE; + } else { /* Multiple call results. */ + e.u.s.info = e.u.s.aux; /* Base of call is not relocatable. */ + e.k = VNONRELOC; + } + } + bcemit_store(ls->fs, &lh->v, &e); + return; + } + assign_adjust(ls, nvars, nexps, &e); + if (nexps > nvars) + ls->fs->freereg -= nexps - nvars; /* Drop leftover regs. */ + } + /* Assign RHS to LHS and recurse downwards. */ + expr_init(&e, VNONRELOC, ls->fs->freereg-1); + bcemit_store(ls->fs, &lh->v, &e); +} + +/* Parse call statement or assignment. */ +static void parse_call_assign(LexState *ls) +{ + FuncState *fs = ls->fs; + LHSVarList vl; + expr_primary(ls, &vl.v); + if (vl.v.k == VCALL) { /* Function call statement. */ + setbc_b(bcptr(fs, &vl.v), 1); /* No results. */ + } else { /* Start of an assignment. */ + vl.prev = NULL; + parse_assignment(ls, &vl, 1); + } +} + +/* Parse 'local' statement. */ +static void parse_local(LexState *ls) +{ + if (lex_opt(ls, TK_function)) { /* Local function declaration. */ + ExpDesc v, b; + FuncState *fs = ls->fs; + var_new(ls, 0, lex_str(ls)); + expr_init(&v, VLOCAL, fs->freereg); + v.u.s.aux = fs->varmap[fs->freereg]; + bcreg_reserve(fs, 1); + var_add(ls, 1); + parse_body(ls, &b, 0, ls->linenumber); + /* bcemit_store(fs, &v, &b) without setting VSTACK_VAR_RW. */ + expr_free(fs, &b); + expr_toreg(fs, &b, v.u.s.info); + /* The upvalue is in scope, but the local is only valid after the store. */ + var_get(ls, fs, fs->nactvar - 1).startpc = fs->pc; + } else { /* Local variable declaration. */ + ExpDesc e; + BCReg nexps, nvars = 0; + do { /* Collect LHS. */ + var_new(ls, nvars++, lex_str(ls)); + } while (lex_opt(ls, ',')); + if (lex_opt(ls, '=')) { /* Optional RHS. */ + nexps = expr_list(ls, &e); + } else { /* Or implicitly set to nil. */ + e.k = VVOID; + nexps = 0; + } + assign_adjust(ls, nvars, nexps, &e); + var_add(ls, nvars); + } +} + +/* Parse 'function' statement. */ +static void parse_func(LexState *ls, BCLine line) +{ + FuncState *fs; + ExpDesc v, b; + int needself = 0; + lj_lex_next(ls); /* Skip 'function'. */ + /* Parse function name. */ + var_lookup(ls, &v); + while (ls->token == '.') /* Multiple dot-separated fields. */ + expr_field(ls, &v); + if (ls->token == ':') { /* Optional colon to signify method call. */ + needself = 1; + expr_field(ls, &v); + } + parse_body(ls, &b, needself, line); + fs = ls->fs; + bcemit_store(fs, &v, &b); + fs->bcbase[fs->pc - 1].line = line; /* Set line for the store. */ +} + +/* -- Control transfer statements ----------------------------------------- */ + +/* Check for end of block. */ +static int endofblock(LexToken token) +{ + switch (token) { + case TK_else: case TK_elseif: case TK_end: case TK_until: case TK_eof: + return 1; + default: + return 0; + } +} + +/* Parse 'return' statement. */ +static void parse_return(LexState *ls) +{ + BCIns ins; + FuncState *fs = ls->fs; + lj_lex_next(ls); /* Skip 'return'. */ + fs->flags |= PROTO_HAS_RETURN; + if (endofblock(ls->token) || ls->token == ';') { /* Bare return. */ + ins = BCINS_AD(BC_RET0, 0, 1); + } else { /* Return with one or more values. */ + ExpDesc e; /* Receives the _last_ expression in the list. */ + BCReg nret = expr_list(ls, &e); + if (nret == 1) { /* Return one result. */ + if (e.k == VCALL) { /* Check for tail call. */ + BCIns *ip = bcptr(fs, &e); + /* It doesn't pay off to add BC_VARGT just for 'return ...'. */ + if (bc_op(*ip) == BC_VARG) goto notailcall; + fs->pc--; + ins = BCINS_AD(bc_op(*ip)-BC_CALL+BC_CALLT, bc_a(*ip), bc_c(*ip)); + } else { /* Can return the result from any register. */ + ins = BCINS_AD(BC_RET1, expr_toanyreg(fs, &e), 2); + } + } else { + if (e.k == VCALL) { /* Append all results from a call. */ + notailcall: + setbc_b(bcptr(fs, &e), 0); + ins = BCINS_AD(BC_RETM, fs->nactvar, e.u.s.aux - fs->nactvar); + } else { + expr_tonextreg(fs, &e); /* Force contiguous registers. */ + ins = BCINS_AD(BC_RET, fs->nactvar, nret+1); + } + } + } + if (fs->flags & PROTO_CHILD) + bcemit_AJ(fs, BC_UCLO, 0, 0); /* May need to close upvalues first. */ + bcemit_INS(fs, ins); +} + +/* Parse 'break' statement. */ +static void parse_break(LexState *ls) +{ + ls->fs->bl->flags |= FSCOPE_BREAK; + gola_new(ls, NAME_BREAK, VSTACK_GOTO, bcemit_jmp(ls->fs)); +} + +/* Parse 'goto' statement. */ +static void parse_goto(LexState *ls) +{ + FuncState *fs = ls->fs; + GCstr *name = lex_str(ls); + VarInfo *vl = gola_findlabel(ls, name); + if (vl) /* Treat backwards goto within same scope like a loop. */ + bcemit_AJ(fs, BC_LOOP, vl->slot, -1); /* No BC range check. */ + fs->bl->flags |= FSCOPE_GOLA; + gola_new(ls, name, VSTACK_GOTO, bcemit_jmp(fs)); +} + +/* Parse label. */ +static void parse_label(LexState *ls) +{ + FuncState *fs = ls->fs; + GCstr *name; + MSize idx; + fs->lasttarget = fs->pc; + fs->bl->flags |= FSCOPE_GOLA; + lj_lex_next(ls); /* Skip '::'. */ + name = lex_str(ls); + if (gola_findlabel(ls, name)) + lj_lex_error(ls, 0, LJ_ERR_XLDUP, strdata(name)); + idx = gola_new(ls, name, VSTACK_LABEL, fs->pc); + lex_check(ls, TK_label); + /* Recursively parse trailing statements: labels and ';' (Lua 5.2 only). */ + for (;;) { + if (ls->token == TK_label) { + synlevel_begin(ls); + parse_label(ls); + synlevel_end(ls); + } else if (LJ_52 && ls->token == ';') { + lj_lex_next(ls); + } else { + break; + } + } + /* Trailing label is considered to be outside of scope. */ + if (endofblock(ls->token) && ls->token != TK_until) + ls->vstack[idx].slot = fs->bl->nactvar; + gola_resolve(ls, fs->bl, idx); +} + +/* -- Blocks, loops and conditional statements ---------------------------- */ + +/* Parse a block. */ +static void parse_block(LexState *ls) +{ + FuncState *fs = ls->fs; + FuncScope bl; + fscope_begin(fs, &bl, 0); + parse_chunk(ls); + fscope_end(fs); +} + +/* Parse 'while' statement. */ +static void parse_while(LexState *ls, BCLine line) +{ + FuncState *fs = ls->fs; + BCPos start, loop, condexit; + FuncScope bl; + lj_lex_next(ls); /* Skip 'while'. */ + start = fs->lasttarget = fs->pc; + condexit = expr_cond(ls); + fscope_begin(fs, &bl, FSCOPE_LOOP); + lex_check(ls, TK_do); + loop = bcemit_AD(fs, BC_LOOP, fs->nactvar, 0); + parse_block(ls); + jmp_patch(fs, bcemit_jmp(fs), start); + lex_match(ls, TK_end, TK_while, line); + fscope_end(fs); + jmp_tohere(fs, condexit); + jmp_patchins(fs, loop, fs->pc); +} + +/* Parse 'repeat' statement. */ +static void parse_repeat(LexState *ls, BCLine line) +{ + FuncState *fs = ls->fs; + BCPos loop = fs->lasttarget = fs->pc; + BCPos condexit; + FuncScope bl1, bl2; + fscope_begin(fs, &bl1, FSCOPE_LOOP); /* Breakable loop scope. */ + fscope_begin(fs, &bl2, 0); /* Inner scope. */ + lj_lex_next(ls); /* Skip 'repeat'. */ + bcemit_AD(fs, BC_LOOP, fs->nactvar, 0); + parse_chunk(ls); + lex_match(ls, TK_until, TK_repeat, line); + condexit = expr_cond(ls); /* Parse condition (still inside inner scope). */ + if (!(bl2.flags & FSCOPE_UPVAL)) { /* No upvalues? Just end inner scope. */ + fscope_end(fs); + } else { /* Otherwise generate: cond: UCLO+JMP out, !cond: UCLO+JMP loop. */ + parse_break(ls); /* Break from loop and close upvalues. */ + jmp_tohere(fs, condexit); + fscope_end(fs); /* End inner scope and close upvalues. */ + condexit = bcemit_jmp(fs); + } + jmp_patch(fs, condexit, loop); /* Jump backwards if !cond. */ + jmp_patchins(fs, loop, fs->pc); + fscope_end(fs); /* End loop scope. */ +} + +/* Parse numeric 'for'. */ +static void parse_for_num(LexState *ls, GCstr *varname, BCLine line) +{ + FuncState *fs = ls->fs; + BCReg base = fs->freereg; + FuncScope bl; + BCPos loop, loopend; + /* Hidden control variables. */ + var_new_fixed(ls, FORL_IDX, VARNAME_FOR_IDX); + var_new_fixed(ls, FORL_STOP, VARNAME_FOR_STOP); + var_new_fixed(ls, FORL_STEP, VARNAME_FOR_STEP); + /* Visible copy of index variable. */ + var_new(ls, FORL_EXT, varname); + lex_check(ls, '='); + expr_next(ls); + lex_check(ls, ','); + expr_next(ls); + if (lex_opt(ls, ',')) { + expr_next(ls); + } else { + bcemit_AD(fs, BC_KSHORT, fs->freereg, 1); /* Default step is 1. */ + bcreg_reserve(fs, 1); + } + var_add(ls, 3); /* Hidden control variables. */ + lex_check(ls, TK_do); + loop = bcemit_AJ(fs, BC_FORI, base, NO_JMP); + fscope_begin(fs, &bl, 0); /* Scope for visible variables. */ + var_add(ls, 1); + bcreg_reserve(fs, 1); + parse_block(ls); + fscope_end(fs); + /* Perform loop inversion. Loop control instructions are at the end. */ + loopend = bcemit_AJ(fs, BC_FORL, base, NO_JMP); + fs->bcbase[loopend].line = line; /* Fix line for control ins. */ + jmp_patchins(fs, loopend, loop+1); + jmp_patchins(fs, loop, fs->pc); +} + +/* Try to predict whether the iterator is next() and specialize the bytecode. +** Detecting next() and pairs() by name is simplistic, but quite effective. +** The interpreter backs off if the check for the closure fails at runtime. +*/ +static int predict_next(LexState *ls, FuncState *fs, BCPos pc) +{ + BCIns ins = fs->bcbase[pc].ins; + GCstr *name; + cTValue *o; + switch (bc_op(ins)) { + case BC_MOV: + name = gco2str(gcref(var_get(ls, fs, bc_d(ins)).name)); + break; + case BC_UGET: + name = gco2str(gcref(ls->vstack[fs->uvmap[bc_d(ins)]].name)); + break; + case BC_GGET: + /* There's no inverse index (yet), so lookup the strings. */ + o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "pairs")); + if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins)) + return 1; + o = lj_tab_getstr(fs->kt, lj_str_newlit(ls->L, "next")); + if (o && tvhaskslot(o) && tvkslot(o) == bc_d(ins)) + return 1; + return 0; + default: + return 0; + } + return (name->len == 5 && !strcmp(strdata(name), "pairs")) || + (name->len == 4 && !strcmp(strdata(name), "next")); +} + +/* Parse 'for' iterator. */ +static void parse_for_iter(LexState *ls, GCstr *indexname) +{ + FuncState *fs = ls->fs; + ExpDesc e; + BCReg nvars = 0; + BCLine line; + BCReg base = fs->freereg + 3; + BCPos loop, loopend, exprpc = fs->pc; + FuncScope bl; + int isnext; + /* Hidden control variables. */ + var_new_fixed(ls, nvars++, VARNAME_FOR_GEN); + var_new_fixed(ls, nvars++, VARNAME_FOR_STATE); + var_new_fixed(ls, nvars++, VARNAME_FOR_CTL); + /* Visible variables returned from iterator. */ + var_new(ls, nvars++, indexname); + while (lex_opt(ls, ',')) + var_new(ls, nvars++, lex_str(ls)); + lex_check(ls, TK_in); + line = ls->linenumber; + assign_adjust(ls, 3, expr_list(ls, &e), &e); + bcreg_bump(fs, 3); /* The iterator needs another 3 slots (func + 2 args). */ + isnext = (nvars <= 5 && predict_next(ls, fs, exprpc)); + var_add(ls, 3); /* Hidden control variables. */ + lex_check(ls, TK_do); + loop = bcemit_AJ(fs, isnext ? BC_ISNEXT : BC_JMP, base, NO_JMP); + fscope_begin(fs, &bl, 0); /* Scope for visible variables. */ + var_add(ls, nvars-3); + bcreg_reserve(fs, nvars-3); + parse_block(ls); + fscope_end(fs); + /* Perform loop inversion. Loop control instructions are at the end. */ + jmp_patchins(fs, loop, fs->pc); + bcemit_ABC(fs, isnext ? BC_ITERN : BC_ITERC, base, nvars-3+1, 2+1); + loopend = bcemit_AJ(fs, BC_ITERL, base, NO_JMP); + fs->bcbase[loopend-1].line = line; /* Fix line for control ins. */ + fs->bcbase[loopend].line = line; + jmp_patchins(fs, loopend, loop+1); +} + +/* Parse 'for' statement. */ +static void parse_for(LexState *ls, BCLine line) +{ + FuncState *fs = ls->fs; + GCstr *varname; + FuncScope bl; + fscope_begin(fs, &bl, FSCOPE_LOOP); + lj_lex_next(ls); /* Skip 'for'. */ + varname = lex_str(ls); /* Get first variable name. */ + if (ls->token == '=') + parse_for_num(ls, varname, line); + else if (ls->token == ',' || ls->token == TK_in) + parse_for_iter(ls, varname); + else + err_syntax(ls, LJ_ERR_XFOR); + lex_match(ls, TK_end, TK_for, line); + fscope_end(fs); /* Resolve break list. */ +} + +/* Parse condition and 'then' block. */ +static BCPos parse_then(LexState *ls) +{ + BCPos condexit; + lj_lex_next(ls); /* Skip 'if' or 'elseif'. */ + condexit = expr_cond(ls); + lex_check(ls, TK_then); + parse_block(ls); + return condexit; +} + +/* Parse 'if' statement. */ +static void parse_if(LexState *ls, BCLine line) +{ + FuncState *fs = ls->fs; + BCPos flist; + BCPos escapelist = NO_JMP; + flist = parse_then(ls); + while (ls->token == TK_elseif) { /* Parse multiple 'elseif' blocks. */ + jmp_append(fs, &escapelist, bcemit_jmp(fs)); + jmp_tohere(fs, flist); + flist = parse_then(ls); + } + if (ls->token == TK_else) { /* Parse optional 'else' block. */ + jmp_append(fs, &escapelist, bcemit_jmp(fs)); + jmp_tohere(fs, flist); + lj_lex_next(ls); /* Skip 'else'. */ + parse_block(ls); + } else { + jmp_append(fs, &escapelist, flist); + } + jmp_tohere(fs, escapelist); + lex_match(ls, TK_end, TK_if, line); +} + +/* -- Parse statements ---------------------------------------------------- */ + +/* Parse a statement. Returns 1 if it must be the last one in a chunk. */ +static int parse_stmt(LexState *ls) +{ + BCLine line = ls->linenumber; + switch (ls->token) { + case TK_if: + parse_if(ls, line); + break; + case TK_while: + parse_while(ls, line); + break; + case TK_do: + lj_lex_next(ls); + parse_block(ls); + lex_match(ls, TK_end, TK_do, line); + break; + case TK_for: + parse_for(ls, line); + break; + case TK_repeat: + parse_repeat(ls, line); + break; + case TK_function: + parse_func(ls, line); + break; + case TK_local: + lj_lex_next(ls); + parse_local(ls); + break; + case TK_return: + parse_return(ls); + return 1; /* Must be last. */ + case TK_break: + lj_lex_next(ls); + parse_break(ls); + return !LJ_52; /* Must be last in Lua 5.1. */ +#if LJ_52 + case ';': + lj_lex_next(ls); + break; +#endif + case TK_label: + parse_label(ls); + break; + case TK_goto: + if (LJ_52 || lj_lex_lookahead(ls) == TK_name) { + lj_lex_next(ls); + parse_goto(ls); + break; + } /* else: fallthrough */ + default: + parse_call_assign(ls); + break; + } + return 0; +} + +/* A chunk is a list of statements optionally separated by semicolons. */ +static void parse_chunk(LexState *ls) +{ + int islast = 0; + synlevel_begin(ls); + while (!islast && !endofblock(ls->token)) { + islast = parse_stmt(ls); + lex_opt(ls, ';'); + lua_assert(ls->fs->framesize >= ls->fs->freereg && + ls->fs->freereg >= ls->fs->nactvar); + ls->fs->freereg = ls->fs->nactvar; /* Free registers after each stmt. */ + } + synlevel_end(ls); +} + +/* Entry point of bytecode parser. */ +GCproto *lj_parse(LexState *ls) +{ + FuncState fs; + FuncScope bl; + GCproto *pt; + lua_State *L = ls->L; +#ifdef LUAJIT_DISABLE_DEBUGINFO + ls->chunkname = lj_str_newlit(L, "="); +#else + ls->chunkname = lj_str_newz(L, ls->chunkarg); +#endif + setstrV(L, L->top, ls->chunkname); /* Anchor chunkname string. */ + incr_top(L); + ls->level = 0; + fs_init(ls, &fs); + fs.linedefined = 0; + fs.numparams = 0; + fs.bcbase = NULL; + fs.bclim = 0; + fs.flags |= PROTO_VARARG; /* Main chunk is always a vararg func. */ + fscope_begin(&fs, &bl, 0); + bcemit_AD(&fs, BC_FUNCV, 0, 0); /* Placeholder. */ + lj_lex_next(ls); /* Read-ahead first token. */ + parse_chunk(ls); + if (ls->token != TK_eof) + err_token(ls, TK_eof); + pt = fs_finish(ls, ls->linenumber); + L->top--; /* Drop chunkname. */ + lua_assert(fs.prev == NULL); + lua_assert(ls->fs == NULL); + lua_assert(pt->sizeuv == 0); + return pt; +} + diff --git a/external/lua/luajit/src/src/lj_parse.h b/external/lua/luajit/src/src/lj_parse.h new file mode 100644 index 0000000000..558b4e2d01 --- /dev/null +++ b/external/lua/luajit/src/src/lj_parse.h @@ -0,0 +1,18 @@ +/* +** Lua parser (source code -> bytecode). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_PARSE_H +#define _LJ_PARSE_H + +#include "lj_obj.h" +#include "lj_lex.h" + +LJ_FUNC GCproto *lj_parse(LexState *ls); +LJ_FUNC GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t l); +#if LJ_HASFFI +LJ_FUNC void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd); +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_record.c b/external/lua/luajit/src/src/lj_record.c new file mode 100644 index 0000000000..7336e0ac14 --- /dev/null +++ b/external/lua/luajit/src/src/lj_record.c @@ -0,0 +1,2247 @@ +/* +** Trace recorder (bytecode -> SSA IR). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_record_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_meta.h" +#include "lj_frame.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#endif +#include "lj_bc.h" +#include "lj_ff.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_ircall.h" +#include "lj_iropt.h" +#include "lj_trace.h" +#include "lj_record.h" +#include "lj_ffrecord.h" +#include "lj_snap.h" +#include "lj_dispatch.h" +#include "lj_vm.h" + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) + +/* Pass IR on to next optimization in chain (FOLD). */ +#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) + +/* Emit raw IR without passing through optimizations. */ +#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) + +/* -- Sanity checks ------------------------------------------------------- */ + +#ifdef LUA_USE_ASSERT +/* Sanity check the whole IR -- sloooow. */ +static void rec_check_ir(jit_State *J) +{ + IRRef i, nins = J->cur.nins, nk = J->cur.nk; + lua_assert(nk <= REF_BIAS && nins >= REF_BIAS && nins < 65536); + for (i = nins-1; i >= nk; i--) { + IRIns *ir = IR(i); + uint32_t mode = lj_ir_mode[ir->o]; + IRRef op1 = ir->op1; + IRRef op2 = ir->op2; + switch (irm_op1(mode)) { + case IRMnone: lua_assert(op1 == 0); break; + case IRMref: lua_assert(op1 >= nk); + lua_assert(i >= REF_BIAS ? op1 < i : op1 > i); break; + case IRMlit: break; + case IRMcst: lua_assert(i < REF_BIAS); continue; + } + switch (irm_op2(mode)) { + case IRMnone: lua_assert(op2 == 0); break; + case IRMref: lua_assert(op2 >= nk); + lua_assert(i >= REF_BIAS ? op2 < i : op2 > i); break; + case IRMlit: break; + case IRMcst: lua_assert(0); break; + } + if (ir->prev) { + lua_assert(ir->prev >= nk); + lua_assert(i >= REF_BIAS ? ir->prev < i : ir->prev > i); + lua_assert(ir->o == IR_NOP || IR(ir->prev)->o == ir->o); + } + } +} + +/* Compare stack slots and frames of the recorder and the VM. */ +static void rec_check_slots(jit_State *J) +{ + BCReg s, nslots = J->baseslot + J->maxslot; + int32_t depth = 0; + cTValue *base = J->L->base - J->baseslot; + lua_assert(J->baseslot >= 1 && J->baseslot < LJ_MAX_JSLOTS); + lua_assert(J->baseslot == 1 || (J->slot[J->baseslot-1] & TREF_FRAME)); + lua_assert(nslots < LJ_MAX_JSLOTS); + for (s = 0; s < nslots; s++) { + TRef tr = J->slot[s]; + if (tr) { + cTValue *tv = &base[s]; + IRRef ref = tref_ref(tr); + IRIns *ir; + lua_assert(ref >= J->cur.nk && ref < J->cur.nins); + ir = IR(ref); + lua_assert(irt_t(ir->t) == tref_t(tr)); + if (s == 0) { + lua_assert(tref_isfunc(tr)); + } else if ((tr & TREF_FRAME)) { + GCfunc *fn = gco2func(frame_gc(tv)); + BCReg delta = (BCReg)(tv - frame_prev(tv)); + lua_assert(tref_isfunc(tr)); + if (tref_isk(tr)) lua_assert(fn == ir_kfunc(ir)); + lua_assert(s > delta ? (J->slot[s-delta] & TREF_FRAME) : (s == delta)); + depth++; + } else if ((tr & TREF_CONT)) { + lua_assert(ir_kptr(ir) == gcrefp(tv->gcr, void)); + lua_assert((J->slot[s+1] & TREF_FRAME)); + depth++; + } else { + if (tvisnumber(tv)) + lua_assert(tref_isnumber(tr)); /* Could be IRT_INT etc., too. */ + else + lua_assert(itype2irt(tv) == tref_type(tr)); + if (tref_isk(tr)) { /* Compare constants. */ + TValue tvk; + lj_ir_kvalue(J->L, &tvk, ir); + if (!(tvisnum(&tvk) && tvisnan(&tvk))) + lua_assert(lj_obj_equal(tv, &tvk)); + else + lua_assert(tvisnum(tv) && tvisnan(tv)); + } + } + } + } + lua_assert(J->framedepth == depth); +} +#endif + +/* -- Type handling and specialization ------------------------------------ */ + +/* Note: these functions return tagged references (TRef). */ + +/* Specialize a slot to a specific type. Note: slot can be negative! */ +static TRef sloadt(jit_State *J, int32_t slot, IRType t, int mode) +{ + /* Caller may set IRT_GUARD in t. */ + TRef ref = emitir_raw(IRT(IR_SLOAD, t), (int32_t)J->baseslot+slot, mode); + J->base[slot] = ref; + return ref; +} + +/* Specialize a slot to the runtime type. Note: slot can be negative! */ +static TRef sload(jit_State *J, int32_t slot) +{ + IRType t = itype2irt(&J->L->base[slot]); + TRef ref = emitir_raw(IRTG(IR_SLOAD, t), (int32_t)J->baseslot+slot, + IRSLOAD_TYPECHECK); + if (irtype_ispri(t)) ref = TREF_PRI(t); /* Canonicalize primitive refs. */ + J->base[slot] = ref; + return ref; +} + +/* Get TRef from slot. Load slot and specialize if not done already. */ +#define getslot(J, s) (J->base[(s)] ? J->base[(s)] : sload(J, (int32_t)(s))) + +/* Get TRef for current function. */ +static TRef getcurrf(jit_State *J) +{ + if (J->base[-1]) + return J->base[-1]; + lua_assert(J->baseslot == 1); + return sloadt(J, -1, IRT_FUNC, IRSLOAD_READONLY); +} + +/* Compare for raw object equality. +** Returns 0 if the objects are the same. +** Returns 1 if they are different, but the same type. +** Returns 2 for two different types. +** Comparisons between primitives always return 1 -- no caller cares about it. +*/ +int lj_record_objcmp(jit_State *J, TRef a, TRef b, cTValue *av, cTValue *bv) +{ + int diff = !lj_obj_equal(av, bv); + if (!tref_isk2(a, b)) { /* Shortcut, also handles primitives. */ + IRType ta = tref_isinteger(a) ? IRT_INT : tref_type(a); + IRType tb = tref_isinteger(b) ? IRT_INT : tref_type(b); + if (ta != tb) { + /* Widen mixed number/int comparisons to number/number comparison. */ + if (ta == IRT_INT && tb == IRT_NUM) { + a = emitir(IRTN(IR_CONV), a, IRCONV_NUM_INT); + ta = IRT_NUM; + } else if (ta == IRT_NUM && tb == IRT_INT) { + b = emitir(IRTN(IR_CONV), b, IRCONV_NUM_INT); + } else { + return 2; /* Two different types are never equal. */ + } + } + emitir(IRTG(diff ? IR_NE : IR_EQ, ta), a, b); + } + return diff; +} + +/* Constify a value. Returns 0 for non-representable object types. */ +TRef lj_record_constify(jit_State *J, cTValue *o) +{ + if (tvisgcv(o)) + return lj_ir_kgc(J, gcV(o), itype2irt(o)); + else if (tvisint(o)) + return lj_ir_kint(J, intV(o)); + else if (tvisnum(o)) + return lj_ir_knumint(J, numV(o)); + else if (tvisbool(o)) + return TREF_PRI(itype2irt(o)); + else + return 0; /* Can't represent lightuserdata (pointless). */ +} + +/* -- Record loop ops ----------------------------------------------------- */ + +/* Loop event. */ +typedef enum { + LOOPEV_LEAVE, /* Loop is left or not entered. */ + LOOPEV_ENTERLO, /* Loop is entered with a low iteration count left. */ + LOOPEV_ENTER /* Loop is entered. */ +} LoopEvent; + +/* Canonicalize slots: convert integers to numbers. */ +static void canonicalize_slots(jit_State *J) +{ + BCReg s; + if (LJ_DUALNUM) return; + for (s = J->baseslot+J->maxslot-1; s >= 1; s--) { + TRef tr = J->slot[s]; + if (tref_isinteger(tr)) { + IRIns *ir = IR(tref_ref(tr)); + if (!(ir->o == IR_SLOAD && (ir->op2 & IRSLOAD_READONLY))) + J->slot[s] = emitir(IRTN(IR_CONV), tr, IRCONV_NUM_INT); + } + } +} + +/* Stop recording. */ +static void rec_stop(jit_State *J, TraceLink linktype, TraceNo lnk) +{ + lj_trace_end(J); + J->cur.linktype = (uint8_t)linktype; + J->cur.link = (uint16_t)lnk; + /* Looping back at the same stack level? */ + if (lnk == J->cur.traceno && J->framedepth + J->retdepth == 0) { + if ((J->flags & JIT_F_OPT_LOOP)) /* Shall we try to create a loop? */ + goto nocanon; /* Do not canonicalize or we lose the narrowing. */ + if (J->cur.root) /* Otherwise ensure we always link to the root trace. */ + J->cur.link = J->cur.root; + } + canonicalize_slots(J); +nocanon: + /* Note: all loop ops must set J->pc to the following instruction! */ + lj_snap_add(J); /* Add loop snapshot. */ + J->needsnap = 0; + J->mergesnap = 1; /* In case recording continues. */ +} + +/* Search bytecode backwards for a int/num constant slot initializer. */ +static TRef find_kinit(jit_State *J, const BCIns *endpc, BCReg slot, IRType t) +{ + /* This algorithm is rather simplistic and assumes quite a bit about + ** how the bytecode is generated. It works fine for FORI initializers, + ** but it won't necessarily work in other cases (e.g. iterator arguments). + ** It doesn't do anything fancy, either (like backpropagating MOVs). + */ + const BCIns *pc, *startpc = proto_bc(J->pt); + for (pc = endpc-1; pc > startpc; pc--) { + BCIns ins = *pc; + BCOp op = bc_op(ins); + /* First try to find the last instruction that stores to this slot. */ + if (bcmode_a(op) == BCMbase && bc_a(ins) <= slot) { + return 0; /* Multiple results, e.g. from a CALL or KNIL. */ + } else if (bcmode_a(op) == BCMdst && bc_a(ins) == slot) { + if (op == BC_KSHORT || op == BC_KNUM) { /* Found const. initializer. */ + /* Now try to verify there's no forward jump across it. */ + const BCIns *kpc = pc; + for (; pc > startpc; pc--) + if (bc_op(*pc) == BC_JMP) { + const BCIns *target = pc+bc_j(*pc)+1; + if (target > kpc && target <= endpc) + return 0; /* Conditional assignment. */ + } + if (op == BC_KSHORT) { + int32_t k = (int32_t)(int16_t)bc_d(ins); + return t == IRT_INT ? lj_ir_kint(J, k) : lj_ir_knum(J, (lua_Number)k); + } else { + cTValue *tv = proto_knumtv(J->pt, bc_d(ins)); + if (t == IRT_INT) { + int32_t k = numberVint(tv); + if (tvisint(tv) || numV(tv) == (lua_Number)k) /* -0 is ok here. */ + return lj_ir_kint(J, k); + return 0; /* Type mismatch. */ + } else { + return lj_ir_knum(J, numberVnum(tv)); + } + } + } + return 0; /* Non-constant initializer. */ + } + } + return 0; /* No assignment to this slot found? */ +} + +/* Load and optionally convert a FORI argument from a slot. */ +static TRef fori_load(jit_State *J, BCReg slot, IRType t, int mode) +{ + int conv = (tvisint(&J->L->base[slot]) != (t==IRT_INT)) ? IRSLOAD_CONVERT : 0; + return sloadt(J, (int32_t)slot, + t + (((mode & IRSLOAD_TYPECHECK) || + (conv && t == IRT_INT && !(mode >> 16))) ? + IRT_GUARD : 0), + mode + conv); +} + +/* Peek before FORI to find a const initializer. Otherwise load from slot. */ +static TRef fori_arg(jit_State *J, const BCIns *fori, BCReg slot, + IRType t, int mode) +{ + TRef tr = J->base[slot]; + if (!tr) { + tr = find_kinit(J, fori, slot, t); + if (!tr) + tr = fori_load(J, slot, t, mode); + } + return tr; +} + +/* Return the direction of the FOR loop iterator. +** It's important to exactly reproduce the semantics of the interpreter. +*/ +static int rec_for_direction(cTValue *o) +{ + return (tvisint(o) ? intV(o) : (int32_t)o->u32.hi) >= 0; +} + +/* Simulate the runtime behavior of the FOR loop iterator. */ +static LoopEvent rec_for_iter(IROp *op, cTValue *o, int isforl) +{ + lua_Number stopv = numberVnum(&o[FORL_STOP]); + lua_Number idxv = numberVnum(&o[FORL_IDX]); + lua_Number stepv = numberVnum(&o[FORL_STEP]); + if (isforl) + idxv += stepv; + if (rec_for_direction(&o[FORL_STEP])) { + if (idxv <= stopv) { + *op = IR_LE; + return idxv + 2*stepv > stopv ? LOOPEV_ENTERLO : LOOPEV_ENTER; + } + *op = IR_GT; return LOOPEV_LEAVE; + } else { + if (stopv <= idxv) { + *op = IR_GE; + return idxv + 2*stepv < stopv ? LOOPEV_ENTERLO : LOOPEV_ENTER; + } + *op = IR_LT; return LOOPEV_LEAVE; + } +} + +/* Record checks for FOR loop overflow and step direction. */ +static void rec_for_check(jit_State *J, IRType t, int dir, + TRef stop, TRef step, int init) +{ + if (!tref_isk(step)) { + /* Non-constant step: need a guard for the direction. */ + TRef zero = (t == IRT_INT) ? lj_ir_kint(J, 0) : lj_ir_knum_zero(J); + emitir(IRTG(dir ? IR_GE : IR_LT, t), step, zero); + /* Add hoistable overflow checks for a narrowed FORL index. */ + if (init && t == IRT_INT) { + if (tref_isk(stop)) { + /* Constant stop: optimize check away or to a range check for step. */ + int32_t k = IR(tref_ref(stop))->i; + if (dir) { + if (k > 0) + emitir(IRTGI(IR_LE), step, lj_ir_kint(J, (int32_t)0x7fffffff-k)); + } else { + if (k < 0) + emitir(IRTGI(IR_GE), step, lj_ir_kint(J, (int32_t)0x80000000-k)); + } + } else { + /* Stop+step variable: need full overflow check. */ + TRef tr = emitir(IRTGI(IR_ADDOV), step, stop); + emitir(IRTI(IR_USE), tr, 0); /* ADDOV is weak. Avoid dead result. */ + } + } + } else if (init && t == IRT_INT && !tref_isk(stop)) { + /* Constant step: optimize overflow check to a range check for stop. */ + int32_t k = IR(tref_ref(step))->i; + k = (int32_t)(dir ? 0x7fffffff : 0x80000000) - k; + emitir(IRTGI(dir ? IR_LE : IR_GE), stop, lj_ir_kint(J, k)); + } +} + +/* Record a FORL instruction. */ +static void rec_for_loop(jit_State *J, const BCIns *fori, ScEvEntry *scev, + int init) +{ + BCReg ra = bc_a(*fori); + cTValue *tv = &J->L->base[ra]; + TRef idx = J->base[ra+FORL_IDX]; + IRType t = idx ? tref_type(idx) : + (init || LJ_DUALNUM) ? lj_opt_narrow_forl(J, tv) : IRT_NUM; + int mode = IRSLOAD_INHERIT + + ((!LJ_DUALNUM || tvisint(tv) == (t == IRT_INT)) ? IRSLOAD_READONLY : 0); + TRef stop = fori_arg(J, fori, ra+FORL_STOP, t, mode); + TRef step = fori_arg(J, fori, ra+FORL_STEP, t, mode); + int tc, dir = rec_for_direction(&tv[FORL_STEP]); + lua_assert(bc_op(*fori) == BC_FORI || bc_op(*fori) == BC_JFORI); + scev->t.irt = t; + scev->dir = dir; + scev->stop = tref_ref(stop); + scev->step = tref_ref(step); + rec_for_check(J, t, dir, stop, step, init); + scev->start = tref_ref(find_kinit(J, fori, ra+FORL_IDX, IRT_INT)); + tc = (LJ_DUALNUM && + !(scev->start && irref_isk(scev->stop) && irref_isk(scev->step) && + tvisint(&tv[FORL_IDX]) == (t == IRT_INT))) ? + IRSLOAD_TYPECHECK : 0; + if (tc) { + J->base[ra+FORL_STOP] = stop; + J->base[ra+FORL_STEP] = step; + } + if (!idx) + idx = fori_load(J, ra+FORL_IDX, t, + IRSLOAD_INHERIT + tc + (J->scev.start << 16)); + if (!init) + J->base[ra+FORL_IDX] = idx = emitir(IRT(IR_ADD, t), idx, step); + J->base[ra+FORL_EXT] = idx; + scev->idx = tref_ref(idx); + J->maxslot = ra+FORL_EXT+1; +} + +/* Record FORL/JFORL or FORI/JFORI. */ +static LoopEvent rec_for(jit_State *J, const BCIns *fori, int isforl) +{ + BCReg ra = bc_a(*fori); + TValue *tv = &J->L->base[ra]; + TRef *tr = &J->base[ra]; + IROp op; + LoopEvent ev; + TRef stop; + IRType t; + if (isforl) { /* Handle FORL/JFORL opcodes. */ + TRef idx = tr[FORL_IDX]; + if (tref_ref(idx) == J->scev.idx) { + t = J->scev.t.irt; + stop = J->scev.stop; + idx = emitir(IRT(IR_ADD, t), idx, J->scev.step); + tr[FORL_EXT] = tr[FORL_IDX] = idx; + } else { + ScEvEntry scev; + rec_for_loop(J, fori, &scev, 0); + t = scev.t.irt; + stop = scev.stop; + } + } else { /* Handle FORI/JFORI opcodes. */ + BCReg i; + lj_meta_for(J->L, tv); + t = (LJ_DUALNUM || tref_isint(tr[FORL_IDX])) ? lj_opt_narrow_forl(J, tv) : + IRT_NUM; + for (i = FORL_IDX; i <= FORL_STEP; i++) { + if (!tr[i]) sload(J, ra+i); + lua_assert(tref_isnumber_str(tr[i])); + if (tref_isstr(tr[i])) + tr[i] = emitir(IRTG(IR_STRTO, IRT_NUM), tr[i], 0); + if (t == IRT_INT) { + if (!tref_isinteger(tr[i])) + tr[i] = emitir(IRTGI(IR_CONV), tr[i], IRCONV_INT_NUM|IRCONV_CHECK); + } else { + if (!tref_isnum(tr[i])) + tr[i] = emitir(IRTN(IR_CONV), tr[i], IRCONV_NUM_INT); + } + } + tr[FORL_EXT] = tr[FORL_IDX]; + stop = tr[FORL_STOP]; + rec_for_check(J, t, rec_for_direction(&tv[FORL_STEP]), + stop, tr[FORL_STEP], 1); + } + + ev = rec_for_iter(&op, tv, isforl); + if (ev == LOOPEV_LEAVE) { + J->maxslot = ra+FORL_EXT+1; + J->pc = fori+1; + } else { + J->maxslot = ra; + J->pc = fori+bc_j(*fori)+1; + } + lj_snap_add(J); + + emitir(IRTG(op, t), tr[FORL_IDX], stop); + + if (ev == LOOPEV_LEAVE) { + J->maxslot = ra; + J->pc = fori+bc_j(*fori)+1; + } else { + J->maxslot = ra+FORL_EXT+1; + J->pc = fori+1; + } + J->needsnap = 1; + return ev; +} + +/* Record ITERL/JITERL. */ +static LoopEvent rec_iterl(jit_State *J, const BCIns iterins) +{ + BCReg ra = bc_a(iterins); + lua_assert(J->base[ra] != 0); + if (!tref_isnil(J->base[ra])) { /* Looping back? */ + J->base[ra-1] = J->base[ra]; /* Copy result of ITERC to control var. */ + J->maxslot = ra-1+bc_b(J->pc[-1]); + J->pc += bc_j(iterins)+1; + return LOOPEV_ENTER; + } else { + J->maxslot = ra-3; + J->pc++; + return LOOPEV_LEAVE; + } +} + +/* Record LOOP/JLOOP. Now, that was easy. */ +static LoopEvent rec_loop(jit_State *J, BCReg ra) +{ + if (ra < J->maxslot) J->maxslot = ra; + J->pc++; + return LOOPEV_ENTER; +} + +/* Check if a loop repeatedly failed to trace because it didn't loop back. */ +static int innerloopleft(jit_State *J, const BCIns *pc) +{ + ptrdiff_t i; + for (i = 0; i < PENALTY_SLOTS; i++) + if (mref(J->penalty[i].pc, const BCIns) == pc) { + if ((J->penalty[i].reason == LJ_TRERR_LLEAVE || + J->penalty[i].reason == LJ_TRERR_LINNER) && + J->penalty[i].val >= 2*PENALTY_MIN) + return 1; + break; + } + return 0; +} + +/* Handle the case when an interpreted loop op is hit. */ +static void rec_loop_interp(jit_State *J, const BCIns *pc, LoopEvent ev) +{ + if (J->parent == 0) { + if (pc == J->startpc && J->framedepth + J->retdepth == 0) { + /* Same loop? */ + if (ev == LOOPEV_LEAVE) /* Must loop back to form a root trace. */ + lj_trace_err(J, LJ_TRERR_LLEAVE); + rec_stop(J, LJ_TRLINK_LOOP, J->cur.traceno); /* Looping root trace. */ + } else if (ev != LOOPEV_LEAVE) { /* Entering inner loop? */ + /* It's usually better to abort here and wait until the inner loop + ** is traced. But if the inner loop repeatedly didn't loop back, + ** this indicates a low trip count. In this case try unrolling + ** an inner loop even in a root trace. But it's better to be a bit + ** more conservative here and only do it for very short loops. + */ + if (bc_j(*pc) != -1 && !innerloopleft(J, pc)) + lj_trace_err(J, LJ_TRERR_LINNER); /* Root trace hit an inner loop. */ + if ((ev != LOOPEV_ENTERLO && + J->loopref && J->cur.nins - J->loopref > 24) || --J->loopunroll < 0) + lj_trace_err(J, LJ_TRERR_LUNROLL); /* Limit loop unrolling. */ + J->loopref = J->cur.nins; + } + } else if (ev != LOOPEV_LEAVE) { /* Side trace enters an inner loop. */ + J->loopref = J->cur.nins; + if (--J->loopunroll < 0) + lj_trace_err(J, LJ_TRERR_LUNROLL); /* Limit loop unrolling. */ + } /* Side trace continues across a loop that's left or not entered. */ +} + +/* Handle the case when an already compiled loop op is hit. */ +static void rec_loop_jit(jit_State *J, TraceNo lnk, LoopEvent ev) +{ + if (J->parent == 0) { /* Root trace hit an inner loop. */ + /* Better let the inner loop spawn a side trace back here. */ + lj_trace_err(J, LJ_TRERR_LINNER); + } else if (ev != LOOPEV_LEAVE) { /* Side trace enters a compiled loop. */ + J->instunroll = 0; /* Cannot continue across a compiled loop op. */ + if (J->pc == J->startpc && J->framedepth + J->retdepth == 0) + rec_stop(J, LJ_TRLINK_LOOP, J->cur.traceno); /* Form an extra loop. */ + else + rec_stop(J, LJ_TRLINK_ROOT, lnk); /* Link to the loop. */ + } /* Side trace continues across a loop that's left or not entered. */ +} + +/* -- Record calls and returns -------------------------------------------- */ + +/* Specialize to the runtime value of the called function or its prototype. */ +static TRef rec_call_specialize(jit_State *J, GCfunc *fn, TRef tr) +{ + TRef kfunc; + if (isluafunc(fn)) { + GCproto *pt = funcproto(fn); + /* Too many closures created? Probably not a monomorphic function. */ + if (pt->flags >= PROTO_CLC_POLY) { /* Specialize to prototype instead. */ + TRef trpt = emitir(IRT(IR_FLOAD, IRT_P32), tr, IRFL_FUNC_PC); + emitir(IRTG(IR_EQ, IRT_P32), trpt, lj_ir_kptr(J, proto_bc(pt))); + (void)lj_ir_kgc(J, obj2gco(pt), IRT_PROTO); /* Prevent GC of proto. */ + return tr; + } + } + /* Otherwise specialize to the function (closure) value itself. */ + kfunc = lj_ir_kfunc(J, fn); + emitir(IRTG(IR_EQ, IRT_FUNC), tr, kfunc); + return kfunc; +} + +/* Record call setup. */ +static void rec_call_setup(jit_State *J, BCReg func, ptrdiff_t nargs) +{ + RecordIndex ix; + TValue *functv = &J->L->base[func]; + TRef *fbase = &J->base[func]; + ptrdiff_t i; + for (i = 0; i <= nargs; i++) + (void)getslot(J, func+i); /* Ensure func and all args have a reference. */ + if (!tref_isfunc(fbase[0])) { /* Resolve __call metamethod. */ + ix.tab = fbase[0]; + copyTV(J->L, &ix.tabv, functv); + if (!lj_record_mm_lookup(J, &ix, MM_call) || !tref_isfunc(ix.mobj)) + lj_trace_err(J, LJ_TRERR_NOMM); + for (i = ++nargs; i > 0; i--) /* Shift arguments up. */ + fbase[i] = fbase[i-1]; + fbase[0] = ix.mobj; /* Replace function. */ + functv = &ix.mobjv; + } + fbase[0] = TREF_FRAME | rec_call_specialize(J, funcV(functv), fbase[0]); + J->maxslot = (BCReg)nargs; +} + +/* Record call. */ +void lj_record_call(jit_State *J, BCReg func, ptrdiff_t nargs) +{ + rec_call_setup(J, func, nargs); + /* Bump frame. */ + J->framedepth++; + J->base += func+1; + J->baseslot += func+1; +} + +/* Record tail call. */ +void lj_record_tailcall(jit_State *J, BCReg func, ptrdiff_t nargs) +{ + rec_call_setup(J, func, nargs); + if (frame_isvarg(J->L->base - 1)) { + BCReg cbase = (BCReg)frame_delta(J->L->base - 1); + if (--J->framedepth < 0) + lj_trace_err(J, LJ_TRERR_NYIRETL); + J->baseslot -= (BCReg)cbase; + J->base -= cbase; + func += cbase; + } + /* Move func + args down. */ + memmove(&J->base[-1], &J->base[func], sizeof(TRef)*(J->maxslot+1)); + /* Note: the new TREF_FRAME is now at J->base[-1] (even for slot #0). */ + /* Tailcalls can form a loop, so count towards the loop unroll limit. */ + if (++J->tailcalled > J->loopunroll) + lj_trace_err(J, LJ_TRERR_LUNROLL); +} + +/* Check unroll limits for down-recursion. */ +static int check_downrec_unroll(jit_State *J, GCproto *pt) +{ + IRRef ptref; + for (ptref = J->chain[IR_KGC]; ptref; ptref = IR(ptref)->prev) + if (ir_kgc(IR(ptref)) == obj2gco(pt)) { + int count = 0; + IRRef ref; + for (ref = J->chain[IR_RETF]; ref; ref = IR(ref)->prev) + if (IR(ref)->op1 == ptref) + count++; + if (count) { + if (J->pc == J->startpc) { + if (count + J->tailcalled > J->param[JIT_P_recunroll]) + return 1; + } else { + lj_trace_err(J, LJ_TRERR_DOWNREC); + } + } + } + return 0; +} + +/* Record return. */ +void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults) +{ + TValue *frame = J->L->base - 1; + ptrdiff_t i; + for (i = 0; i < gotresults; i++) + (void)getslot(J, rbase+i); /* Ensure all results have a reference. */ + while (frame_ispcall(frame)) { /* Immediately resolve pcall() returns. */ + BCReg cbase = (BCReg)frame_delta(frame); + if (--J->framedepth < 0) + lj_trace_err(J, LJ_TRERR_NYIRETL); + lua_assert(J->baseslot > 1); + gotresults++; + rbase += cbase; + J->baseslot -= (BCReg)cbase; + J->base -= cbase; + J->base[--rbase] = TREF_TRUE; /* Prepend true to results. */ + frame = frame_prevd(frame); + } + /* Return to lower frame via interpreter for unhandled cases. */ + if (J->framedepth == 0 && J->pt && bc_isret(bc_op(*J->pc)) && + (!frame_islua(frame) || + (J->parent == 0 && !bc_isret(bc_op(J->cur.startins))))) { + /* NYI: specialize to frame type and return directly, not via RET*. */ + for (i = -1; i < (ptrdiff_t)rbase; i++) + J->base[i] = 0; /* Purge dead slots. */ + J->maxslot = rbase + (BCReg)gotresults; + rec_stop(J, LJ_TRLINK_RETURN, 0); /* Return to interpreter. */ + return; + } + if (frame_isvarg(frame)) { + BCReg cbase = (BCReg)frame_delta(frame); + if (--J->framedepth < 0) /* NYI: return of vararg func to lower frame. */ + lj_trace_err(J, LJ_TRERR_NYIRETL); + lua_assert(J->baseslot > 1); + rbase += cbase; + J->baseslot -= (BCReg)cbase; + J->base -= cbase; + frame = frame_prevd(frame); + } + if (frame_islua(frame)) { /* Return to Lua frame. */ + BCIns callins = *(frame_pc(frame)-1); + ptrdiff_t nresults = bc_b(callins) ? (ptrdiff_t)bc_b(callins)-1 :gotresults; + BCReg cbase = bc_a(callins); + GCproto *pt = funcproto(frame_func(frame - (cbase+1))); + if (J->framedepth == 0 && J->pt && frame == J->L->base - 1) { + if (check_downrec_unroll(J, pt)) { + J->maxslot = (BCReg)(rbase + gotresults); + lj_snap_purge(J); + rec_stop(J, LJ_TRLINK_DOWNREC, J->cur.traceno); /* Down-recursion. */ + return; + } + lj_snap_add(J); + } + for (i = 0; i < nresults; i++) /* Adjust results. */ + J->base[i-1] = i < gotresults ? J->base[rbase+i] : TREF_NIL; + J->maxslot = cbase+(BCReg)nresults; + if (J->framedepth > 0) { /* Return to a frame that is part of the trace. */ + J->framedepth--; + lua_assert(J->baseslot > cbase+1); + J->baseslot -= cbase+1; + J->base -= cbase+1; + } else if (J->parent == 0 && !bc_isret(bc_op(J->cur.startins))) { + /* Return to lower frame would leave the loop in a root trace. */ + lj_trace_err(J, LJ_TRERR_LLEAVE); + } else { /* Return to lower frame. Guard for the target we return to. */ + TRef trpt = lj_ir_kgc(J, obj2gco(pt), IRT_PROTO); + TRef trpc = lj_ir_kptr(J, (void *)frame_pc(frame)); + emitir(IRTG(IR_RETF, IRT_P32), trpt, trpc); + J->retdepth++; + J->needsnap = 1; + lua_assert(J->baseslot == 1); + /* Shift result slots up and clear the slots of the new frame below. */ + memmove(J->base + cbase, J->base-1, sizeof(TRef)*nresults); + memset(J->base-1, 0, sizeof(TRef)*(cbase+1)); + } + } else if (frame_iscont(frame)) { /* Return to continuation frame. */ + ASMFunction cont = frame_contf(frame); + BCReg cbase = (BCReg)frame_delta(frame); + if ((J->framedepth -= 2) < 0) + lj_trace_err(J, LJ_TRERR_NYIRETL); + J->baseslot -= (BCReg)cbase; + J->base -= cbase; + J->maxslot = cbase-2; + if (cont == lj_cont_ra) { + /* Copy result to destination slot. */ + BCReg dst = bc_a(*(frame_contpc(frame)-1)); + J->base[dst] = gotresults ? J->base[cbase+rbase] : TREF_NIL; + if (dst >= J->maxslot) J->maxslot = dst+1; + } else if (cont == lj_cont_nop) { + /* Nothing to do here. */ + } else if (cont == lj_cont_cat) { + lua_assert(0); + } else { + /* Result type already specialized. */ + lua_assert(cont == lj_cont_condf || cont == lj_cont_condt); + } + } else { + lj_trace_err(J, LJ_TRERR_NYIRETL); /* NYI: handle return to C frame. */ + } + lua_assert(J->baseslot >= 1); +} + +/* -- Metamethod handling ------------------------------------------------- */ + +/* Prepare to record call to metamethod. */ +static BCReg rec_mm_prep(jit_State *J, ASMFunction cont) +{ + BCReg s, top = curr_proto(J->L)->framesize; + TRef trcont; + setcont(&J->L->base[top], cont); +#if LJ_64 + trcont = lj_ir_kptr(J, (void *)((int64_t)cont - (int64_t)lj_vm_asm_begin)); +#else + trcont = lj_ir_kptr(J, (void *)cont); +#endif + J->base[top] = trcont | TREF_CONT; + J->framedepth++; + for (s = J->maxslot; s < top; s++) + J->base[s] = 0; /* Clear frame gap to avoid resurrecting previous refs. */ + return top+1; +} + +/* Record metamethod lookup. */ +int lj_record_mm_lookup(jit_State *J, RecordIndex *ix, MMS mm) +{ + RecordIndex mix; + GCtab *mt; + if (tref_istab(ix->tab)) { + mt = tabref(tabV(&ix->tabv)->metatable); + mix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_TAB_META); + } else if (tref_isudata(ix->tab)) { + int udtype = udataV(&ix->tabv)->udtype; + mt = tabref(udataV(&ix->tabv)->metatable); + /* The metatables of special userdata objects are treated as immutable. */ + if (udtype != UDTYPE_USERDATA) { + cTValue *mo; + if (LJ_HASFFI && udtype == UDTYPE_FFI_CLIB) { + /* Specialize to the C library namespace object. */ + emitir(IRTG(IR_EQ, IRT_P32), ix->tab, lj_ir_kptr(J, udataV(&ix->tabv))); + } else { + /* Specialize to the type of userdata. */ + TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), ix->tab, IRFL_UDATA_UDTYPE); + emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, udtype)); + } + immutable_mt: + mo = lj_tab_getstr(mt, mmname_str(J2G(J), mm)); + if (!mo || tvisnil(mo)) + return 0; /* No metamethod. */ + /* Treat metamethod or index table as immutable, too. */ + if (!(tvisfunc(mo) || tvistab(mo))) + lj_trace_err(J, LJ_TRERR_BADTYPE); + copyTV(J->L, &ix->mobjv, mo); + ix->mobj = lj_ir_kgc(J, gcV(mo), tvisfunc(mo) ? IRT_FUNC : IRT_TAB); + ix->mtv = mt; + ix->mt = TREF_NIL; /* Dummy value for comparison semantics. */ + return 1; /* Got metamethod or index table. */ + } + mix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_UDATA_META); + } else { + /* Specialize to base metatable. Must flush mcode in lua_setmetatable(). */ + mt = tabref(basemt_obj(J2G(J), &ix->tabv)); + if (mt == NULL) { + ix->mt = TREF_NIL; + return 0; /* No metamethod. */ + } + /* The cdata metatable is treated as immutable. */ + if (LJ_HASFFI && tref_iscdata(ix->tab)) goto immutable_mt; + ix->mt = mix.tab = lj_ir_ktab(J, mt); + goto nocheck; + } + ix->mt = mt ? mix.tab : TREF_NIL; + emitir(IRTG(mt ? IR_NE : IR_EQ, IRT_TAB), mix.tab, lj_ir_knull(J, IRT_TAB)); +nocheck: + if (mt) { + GCstr *mmstr = mmname_str(J2G(J), mm); + cTValue *mo = lj_tab_getstr(mt, mmstr); + if (mo && !tvisnil(mo)) + copyTV(J->L, &ix->mobjv, mo); + ix->mtv = mt; + settabV(J->L, &mix.tabv, mt); + setstrV(J->L, &mix.keyv, mmstr); + mix.key = lj_ir_kstr(J, mmstr); + mix.val = 0; + mix.idxchain = 0; + ix->mobj = lj_record_idx(J, &mix); + return !tref_isnil(ix->mobj); /* 1 if metamethod found, 0 if not. */ + } + return 0; /* No metamethod. */ +} + +/* Record call to arithmetic metamethod. */ +static TRef rec_mm_arith(jit_State *J, RecordIndex *ix, MMS mm) +{ + /* Set up metamethod call first to save ix->tab and ix->tabv. */ + BCReg func = rec_mm_prep(J, lj_cont_ra); + TRef *base = J->base + func; + TValue *basev = J->L->base + func; + base[1] = ix->tab; base[2] = ix->key; + copyTV(J->L, basev+1, &ix->tabv); + copyTV(J->L, basev+2, &ix->keyv); + if (!lj_record_mm_lookup(J, ix, mm)) { /* Lookup mm on 1st operand. */ + if (mm != MM_unm) { + ix->tab = ix->key; + copyTV(J->L, &ix->tabv, &ix->keyv); + if (lj_record_mm_lookup(J, ix, mm)) /* Lookup mm on 2nd operand. */ + goto ok; + } + lj_trace_err(J, LJ_TRERR_NOMM); + } +ok: + base[0] = ix->mobj; + copyTV(J->L, basev+0, &ix->mobjv); + lj_record_call(J, func, 2); + return 0; /* No result yet. */ +} + +/* Record call to __len metamethod. */ +static TRef rec_mm_len(jit_State *J, TRef tr, TValue *tv) +{ + RecordIndex ix; + ix.tab = tr; + copyTV(J->L, &ix.tabv, tv); + if (lj_record_mm_lookup(J, &ix, MM_len)) { + BCReg func = rec_mm_prep(J, lj_cont_ra); + TRef *base = J->base + func; + TValue *basev = J->L->base + func; + base[0] = ix.mobj; copyTV(J->L, basev+0, &ix.mobjv); + base[1] = tr; copyTV(J->L, basev+1, tv); +#if LJ_52 + base[2] = tr; copyTV(J->L, basev+2, tv); +#else + base[2] = TREF_NIL; setnilV(basev+2); +#endif + lj_record_call(J, func, 2); + } else { + if (LJ_52 && tref_istab(tr)) + return lj_ir_call(J, IRCALL_lj_tab_len, tr); + lj_trace_err(J, LJ_TRERR_NOMM); + } + return 0; /* No result yet. */ +} + +/* Call a comparison metamethod. */ +static void rec_mm_callcomp(jit_State *J, RecordIndex *ix, int op) +{ + BCReg func = rec_mm_prep(J, (op&1) ? lj_cont_condf : lj_cont_condt); + TRef *base = J->base + func; + TValue *tv = J->L->base + func; + base[0] = ix->mobj; base[1] = ix->val; base[2] = ix->key; + copyTV(J->L, tv+0, &ix->mobjv); + copyTV(J->L, tv+1, &ix->valv); + copyTV(J->L, tv+2, &ix->keyv); + lj_record_call(J, func, 2); +} + +/* Record call to equality comparison metamethod (for tab and udata only). */ +static void rec_mm_equal(jit_State *J, RecordIndex *ix, int op) +{ + ix->tab = ix->val; + copyTV(J->L, &ix->tabv, &ix->valv); + if (lj_record_mm_lookup(J, ix, MM_eq)) { /* Lookup mm on 1st operand. */ + cTValue *bv; + TRef mo1 = ix->mobj; + TValue mo1v; + copyTV(J->L, &mo1v, &ix->mobjv); + /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */ + bv = &ix->keyv; + if (tvistab(bv) && tabref(tabV(bv)->metatable) == ix->mtv) { + TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_TAB_META); + emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); + } else if (tvisudata(bv) && tabref(udataV(bv)->metatable) == ix->mtv) { + TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_UDATA_META); + emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); + } else { /* Lookup metamethod on 2nd operand and compare both. */ + ix->tab = ix->key; + copyTV(J->L, &ix->tabv, bv); + if (!lj_record_mm_lookup(J, ix, MM_eq) || + lj_record_objcmp(J, mo1, ix->mobj, &mo1v, &ix->mobjv)) + return; + } + rec_mm_callcomp(J, ix, op); + } +} + +/* Record call to ordered comparison metamethods (for arbitrary objects). */ +static void rec_mm_comp(jit_State *J, RecordIndex *ix, int op) +{ + ix->tab = ix->val; + copyTV(J->L, &ix->tabv, &ix->valv); + while (1) { + MMS mm = (op & 2) ? MM_le : MM_lt; /* Try __le + __lt or only __lt. */ +#if LJ_52 + if (!lj_record_mm_lookup(J, ix, mm)) { /* Lookup mm on 1st operand. */ + ix->tab = ix->key; + copyTV(J->L, &ix->tabv, &ix->keyv); + if (!lj_record_mm_lookup(J, ix, mm)) /* Lookup mm on 2nd operand. */ + goto nomatch; + } + rec_mm_callcomp(J, ix, op); + return; +#else + if (lj_record_mm_lookup(J, ix, mm)) { /* Lookup mm on 1st operand. */ + cTValue *bv; + TRef mo1 = ix->mobj; + TValue mo1v; + copyTV(J->L, &mo1v, &ix->mobjv); + /* Avoid the 2nd lookup and the objcmp if the metatables are equal. */ + bv = &ix->keyv; + if (tvistab(bv) && tabref(tabV(bv)->metatable) == ix->mtv) { + TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_TAB_META); + emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); + } else if (tvisudata(bv) && tabref(udataV(bv)->metatable) == ix->mtv) { + TRef mt2 = emitir(IRT(IR_FLOAD, IRT_TAB), ix->key, IRFL_UDATA_META); + emitir(IRTG(IR_EQ, IRT_TAB), mt2, ix->mt); + } else { /* Lookup metamethod on 2nd operand and compare both. */ + ix->tab = ix->key; + copyTV(J->L, &ix->tabv, bv); + if (!lj_record_mm_lookup(J, ix, mm) || + lj_record_objcmp(J, mo1, ix->mobj, &mo1v, &ix->mobjv)) + goto nomatch; + } + rec_mm_callcomp(J, ix, op); + return; + } +#endif + nomatch: + /* Lookup failed. Retry with __lt and swapped operands. */ + if (!(op & 2)) break; /* Already at __lt. Interpreter will throw. */ + ix->tab = ix->key; ix->key = ix->val; ix->val = ix->tab; + copyTV(J->L, &ix->tabv, &ix->keyv); + copyTV(J->L, &ix->keyv, &ix->valv); + copyTV(J->L, &ix->valv, &ix->tabv); + op ^= 3; + } +} + +#if LJ_HASFFI +/* Setup call to cdata comparison metamethod. */ +static void rec_mm_comp_cdata(jit_State *J, RecordIndex *ix, int op, MMS mm) +{ + lj_snap_add(J); + if (tref_iscdata(ix->val)) { + ix->tab = ix->val; + copyTV(J->L, &ix->tabv, &ix->valv); + } else { + lua_assert(tref_iscdata(ix->key)); + ix->tab = ix->key; + copyTV(J->L, &ix->tabv, &ix->keyv); + } + lj_record_mm_lookup(J, ix, mm); + rec_mm_callcomp(J, ix, op); +} +#endif + +/* -- Indexed access ------------------------------------------------------ */ + +/* Record bounds-check. */ +static void rec_idx_abc(jit_State *J, TRef asizeref, TRef ikey, uint32_t asize) +{ + /* Try to emit invariant bounds checks. */ + if ((J->flags & (JIT_F_OPT_LOOP|JIT_F_OPT_ABC)) == + (JIT_F_OPT_LOOP|JIT_F_OPT_ABC)) { + IRRef ref = tref_ref(ikey); + IRIns *ir = IR(ref); + int32_t ofs = 0; + IRRef ofsref = 0; + /* Handle constant offsets. */ + if (ir->o == IR_ADD && irref_isk(ir->op2)) { + ofsref = ir->op2; + ofs = IR(ofsref)->i; + ref = ir->op1; + ir = IR(ref); + } + /* Got scalar evolution analysis results for this reference? */ + if (ref == J->scev.idx) { + int32_t stop; + lua_assert(irt_isint(J->scev.t) && ir->o == IR_SLOAD); + stop = numberVint(&(J->L->base - J->baseslot)[ir->op1 + FORL_STOP]); + /* Runtime value for stop of loop is within bounds? */ + if ((int64_t)stop + ofs < (int64_t)asize) { + /* Emit invariant bounds check for stop. */ + emitir(IRTG(IR_ABC, IRT_P32), asizeref, ofs == 0 ? J->scev.stop : + emitir(IRTI(IR_ADD), J->scev.stop, ofsref)); + /* Emit invariant bounds check for start, if not const or negative. */ + if (!(J->scev.dir && J->scev.start && + (int64_t)IR(J->scev.start)->i + ofs >= 0)) + emitir(IRTG(IR_ABC, IRT_P32), asizeref, ikey); + return; + } + } + } + emitir(IRTGI(IR_ABC), asizeref, ikey); /* Emit regular bounds check. */ +} + +/* Record indexed key lookup. */ +static TRef rec_idx_key(jit_State *J, RecordIndex *ix) +{ + TRef key; + GCtab *t = tabV(&ix->tabv); + ix->oldv = lj_tab_get(J->L, t, &ix->keyv); /* Lookup previous value. */ + + /* Integer keys are looked up in the array part first. */ + key = ix->key; + if (tref_isnumber(key)) { + int32_t k = numberVint(&ix->keyv); + if (!tvisint(&ix->keyv) && numV(&ix->keyv) != (lua_Number)k) + k = LJ_MAX_ASIZE; + if ((MSize)k < LJ_MAX_ASIZE) { /* Potential array key? */ + TRef ikey = lj_opt_narrow_index(J, key); + TRef asizeref = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_ASIZE); + if ((MSize)k < t->asize) { /* Currently an array key? */ + TRef arrayref; + rec_idx_abc(J, asizeref, ikey, t->asize); + arrayref = emitir(IRT(IR_FLOAD, IRT_P32), ix->tab, IRFL_TAB_ARRAY); + return emitir(IRT(IR_AREF, IRT_P32), arrayref, ikey); + } else { /* Currently not in array (may be an array extension)? */ + emitir(IRTGI(IR_ULE), asizeref, ikey); /* Inv. bounds check. */ + if (k == 0 && tref_isk(key)) + key = lj_ir_knum_zero(J); /* Canonicalize 0 or +-0.0 to +0.0. */ + /* And continue with the hash lookup. */ + } + } else if (!tref_isk(key)) { + /* We can rule out const numbers which failed the integerness test + ** above. But all other numbers are potential array keys. + */ + if (t->asize == 0) { /* True sparse tables have an empty array part. */ + /* Guard that the array part stays empty. */ + TRef tmp = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_ASIZE); + emitir(IRTGI(IR_EQ), tmp, lj_ir_kint(J, 0)); + } else { + lj_trace_err(J, LJ_TRERR_NYITMIX); + } + } + } + + /* Otherwise the key is located in the hash part. */ + if (t->hmask == 0) { /* Shortcut for empty hash part. */ + /* Guard that the hash part stays empty. */ + TRef tmp = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_HMASK); + emitir(IRTGI(IR_EQ), tmp, lj_ir_kint(J, 0)); + return lj_ir_kkptr(J, niltvg(J2G(J))); + } + if (tref_isinteger(key)) /* Hash keys are based on numbers, not ints. */ + key = emitir(IRTN(IR_CONV), key, IRCONV_NUM_INT); + if (tref_isk(key)) { + /* Optimize lookup of constant hash keys. */ + MSize hslot = (MSize)((char *)ix->oldv - (char *)&noderef(t->node)[0].val); + if (t->hmask > 0 && hslot <= t->hmask*(MSize)sizeof(Node) && + hslot <= 65535*(MSize)sizeof(Node)) { + TRef node, kslot; + TRef hm = emitir(IRTI(IR_FLOAD), ix->tab, IRFL_TAB_HMASK); + emitir(IRTGI(IR_EQ), hm, lj_ir_kint(J, (int32_t)t->hmask)); + node = emitir(IRT(IR_FLOAD, IRT_P32), ix->tab, IRFL_TAB_NODE); + kslot = lj_ir_kslot(J, key, hslot / sizeof(Node)); + return emitir(IRTG(IR_HREFK, IRT_P32), node, kslot); + } + } + /* Fall back to a regular hash lookup. */ + return emitir(IRT(IR_HREF, IRT_P32), ix->tab, key); +} + +/* Determine whether a key is NOT one of the fast metamethod names. */ +static int nommstr(jit_State *J, TRef key) +{ + if (tref_isstr(key)) { + if (tref_isk(key)) { + GCstr *str = ir_kstr(IR(tref_ref(key))); + uint32_t mm; + for (mm = 0; mm <= MM_FAST; mm++) + if (mmname_str(J2G(J), mm) == str) + return 0; /* MUST be one the fast metamethod names. */ + } else { + return 0; /* Variable string key MAY be a metamethod name. */ + } + } + return 1; /* CANNOT be a metamethod name. */ +} + +/* Record indexed load/store. */ +TRef lj_record_idx(jit_State *J, RecordIndex *ix) +{ + TRef xref; + IROp xrefop, loadop; + cTValue *oldv; + + while (!tref_istab(ix->tab)) { /* Handle non-table lookup. */ + /* Never call raw lj_record_idx() on non-table. */ + lua_assert(ix->idxchain != 0); + if (!lj_record_mm_lookup(J, ix, ix->val ? MM_newindex : MM_index)) + lj_trace_err(J, LJ_TRERR_NOMM); + handlemm: + if (tref_isfunc(ix->mobj)) { /* Handle metamethod call. */ + BCReg func = rec_mm_prep(J, ix->val ? lj_cont_nop : lj_cont_ra); + TRef *base = J->base + func; + TValue *tv = J->L->base + func; + base[0] = ix->mobj; base[1] = ix->tab; base[2] = ix->key; + setfuncV(J->L, tv+0, funcV(&ix->mobjv)); + copyTV(J->L, tv+1, &ix->tabv); + copyTV(J->L, tv+2, &ix->keyv); + if (ix->val) { + base[3] = ix->val; + copyTV(J->L, tv+3, &ix->valv); + lj_record_call(J, func, 3); /* mobj(tab, key, val) */ + return 0; + } else { + lj_record_call(J, func, 2); /* res = mobj(tab, key) */ + return 0; /* No result yet. */ + } + } + /* Otherwise retry lookup with metaobject. */ + ix->tab = ix->mobj; + copyTV(J->L, &ix->tabv, &ix->mobjv); + if (--ix->idxchain == 0) + lj_trace_err(J, LJ_TRERR_IDXLOOP); + } + + /* First catch nil and NaN keys for tables. */ + if (tvisnil(&ix->keyv) || (tvisnum(&ix->keyv) && tvisnan(&ix->keyv))) { + if (ix->val) /* Better fail early. */ + lj_trace_err(J, LJ_TRERR_STORENN); + if (tref_isk(ix->key)) { + if (ix->idxchain && lj_record_mm_lookup(J, ix, MM_index)) + goto handlemm; + return TREF_NIL; + } + } + + /* Record the key lookup. */ + xref = rec_idx_key(J, ix); + xrefop = IR(tref_ref(xref))->o; + loadop = xrefop == IR_AREF ? IR_ALOAD : IR_HLOAD; + /* The lj_meta_tset() inconsistency is gone, but better play safe. */ + oldv = xrefop == IR_KKPTR ? (cTValue *)ir_kptr(IR(tref_ref(xref))) : ix->oldv; + + if (ix->val == 0) { /* Indexed load */ + IRType t = itype2irt(oldv); + TRef res; + if (oldv == niltvg(J2G(J))) { + emitir(IRTG(IR_EQ, IRT_P32), xref, lj_ir_kkptr(J, niltvg(J2G(J)))); + res = TREF_NIL; + } else { + res = emitir(IRTG(loadop, t), xref, 0); + } + if (t == IRT_NIL && ix->idxchain && lj_record_mm_lookup(J, ix, MM_index)) + goto handlemm; + if (irtype_ispri(t)) res = TREF_PRI(t); /* Canonicalize primitives. */ + return res; + } else { /* Indexed store. */ + GCtab *mt = tabref(tabV(&ix->tabv)->metatable); + int keybarrier = tref_isgcv(ix->key) && !tref_isnil(ix->val); + if (tvisnil(oldv)) { /* Previous value was nil? */ + /* Need to duplicate the hasmm check for the early guards. */ + int hasmm = 0; + if (ix->idxchain && mt) { + cTValue *mo = lj_tab_getstr(mt, mmname_str(J2G(J), MM_newindex)); + hasmm = mo && !tvisnil(mo); + } + if (hasmm) + emitir(IRTG(loadop, IRT_NIL), xref, 0); /* Guard for nil value. */ + else if (xrefop == IR_HREF) + emitir(IRTG(oldv == niltvg(J2G(J)) ? IR_EQ : IR_NE, IRT_P32), + xref, lj_ir_kkptr(J, niltvg(J2G(J)))); + if (ix->idxchain && lj_record_mm_lookup(J, ix, MM_newindex)) { + lua_assert(hasmm); + goto handlemm; + } + lua_assert(!hasmm); + if (oldv == niltvg(J2G(J))) { /* Need to insert a new key. */ + TRef key = ix->key; + if (tref_isinteger(key)) /* NEWREF needs a TValue as a key. */ + key = emitir(IRTN(IR_CONV), key, IRCONV_NUM_INT); + xref = emitir(IRT(IR_NEWREF, IRT_P32), ix->tab, key); + keybarrier = 0; /* NEWREF already takes care of the key barrier. */ + } + } else if (!lj_opt_fwd_wasnonnil(J, loadop, tref_ref(xref))) { + /* Cannot derive that the previous value was non-nil, must do checks. */ + if (xrefop == IR_HREF) /* Guard against store to niltv. */ + emitir(IRTG(IR_NE, IRT_P32), xref, lj_ir_kkptr(J, niltvg(J2G(J)))); + if (ix->idxchain) { /* Metamethod lookup required? */ + /* A check for NULL metatable is cheaper (hoistable) than a load. */ + if (!mt) { + TRef mtref = emitir(IRT(IR_FLOAD, IRT_TAB), ix->tab, IRFL_TAB_META); + emitir(IRTG(IR_EQ, IRT_TAB), mtref, lj_ir_knull(J, IRT_TAB)); + } else { + IRType t = itype2irt(oldv); + emitir(IRTG(loadop, t), xref, 0); /* Guard for non-nil value. */ + } + } + } else { + keybarrier = 0; /* Previous non-nil value kept the key alive. */ + } + /* Convert int to number before storing. */ + if (!LJ_DUALNUM && tref_isinteger(ix->val)) + ix->val = emitir(IRTN(IR_CONV), ix->val, IRCONV_NUM_INT); + emitir(IRT(loadop+IRDELTA_L2S, tref_type(ix->val)), xref, ix->val); + if (keybarrier || tref_isgcv(ix->val)) + emitir(IRT(IR_TBAR, IRT_NIL), ix->tab, 0); + /* Invalidate neg. metamethod cache for stores with certain string keys. */ + if (!nommstr(J, ix->key)) { + TRef fref = emitir(IRT(IR_FREF, IRT_P32), ix->tab, IRFL_TAB_NOMM); + emitir(IRT(IR_FSTORE, IRT_U8), fref, lj_ir_kint(J, 0)); + } + J->needsnap = 1; + return 0; + } +} + +/* -- Upvalue access ------------------------------------------------------ */ + +/* Check whether upvalue is immutable and ok to constify. */ +static int rec_upvalue_constify(jit_State *J, GCupval *uvp) +{ + if (uvp->immutable) { + cTValue *o = uvval(uvp); + /* Don't constify objects that may retain large amounts of memory. */ +#if LJ_HASFFI + if (tviscdata(o)) { + GCcdata *cd = cdataV(o); + if (!cdataisv(cd) && !(cd->marked & LJ_GC_CDATA_FIN)) { + CType *ct = ctype_raw(ctype_ctsG(J2G(J)), cd->ctypeid); + if (!ctype_hassize(ct->info) || ct->size <= 16) + return 1; + } + return 0; + } +#else + UNUSED(J); +#endif + if (!(tvistab(o) || tvisudata(o) || tvisthread(o))) + return 1; + } + return 0; +} + +/* Record upvalue load/store. */ +static TRef rec_upvalue(jit_State *J, uint32_t uv, TRef val) +{ + GCupval *uvp = &gcref(J->fn->l.uvptr[uv])->uv; + TRef fn = getcurrf(J); + IRRef uref; + int needbarrier = 0; + if (rec_upvalue_constify(J, uvp)) { /* Try to constify immutable upvalue. */ + TRef tr, kfunc; + lua_assert(val == 0); + if (!tref_isk(fn)) { /* Late specialization of current function. */ + if (J->pt->flags >= PROTO_CLC_POLY) + goto noconstify; + kfunc = lj_ir_kfunc(J, J->fn); + emitir(IRTG(IR_EQ, IRT_FUNC), fn, kfunc); + J->base[-1] = TREF_FRAME | kfunc; + fn = kfunc; + } + tr = lj_record_constify(J, uvval(uvp)); + if (tr) + return tr; + } +noconstify: + /* Note: this effectively limits LJ_MAX_UPVAL to 127. */ + uv = (uv << 8) | (hashrot(uvp->dhash, uvp->dhash + HASH_BIAS) & 0xff); + if (!uvp->closed) { + /* In current stack? */ + if (uvval(uvp) >= tvref(J->L->stack) && + uvval(uvp) < tvref(J->L->maxstack)) { + int32_t slot = (int32_t)(uvval(uvp) - (J->L->base - J->baseslot)); + if (slot >= 0) { /* Aliases an SSA slot? */ + slot -= (int32_t)J->baseslot; /* Note: slot number may be negative! */ + /* NYI: add IR to guard that it's still aliasing the same slot. */ + if (val == 0) { + return getslot(J, slot); + } else { + J->base[slot] = val; + if (slot >= (int32_t)J->maxslot) J->maxslot = (BCReg)(slot+1); + return 0; + } + } + } + uref = tref_ref(emitir(IRTG(IR_UREFO, IRT_P32), fn, uv)); + } else { + needbarrier = 1; + uref = tref_ref(emitir(IRTG(IR_UREFC, IRT_P32), fn, uv)); + } + if (val == 0) { /* Upvalue load */ + IRType t = itype2irt(uvval(uvp)); + TRef res = emitir(IRTG(IR_ULOAD, t), uref, 0); + if (irtype_ispri(t)) res = TREF_PRI(t); /* Canonicalize primitive refs. */ + return res; + } else { /* Upvalue store. */ + /* Convert int to number before storing. */ + if (!LJ_DUALNUM && tref_isinteger(val)) + val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT); + emitir(IRT(IR_USTORE, tref_type(val)), uref, val); + if (needbarrier && tref_isgcv(val)) + emitir(IRT(IR_OBAR, IRT_NIL), uref, val); + J->needsnap = 1; + return 0; + } +} + +/* -- Record calls to Lua functions --------------------------------------- */ + +/* Check unroll limits for calls. */ +static void check_call_unroll(jit_State *J, TraceNo lnk) +{ + cTValue *frame = J->L->base - 1; + void *pc = mref(frame_func(frame)->l.pc, void); + int32_t depth = J->framedepth; + int32_t count = 0; + if ((J->pt->flags & PROTO_VARARG)) depth--; /* Vararg frame still missing. */ + for (; depth > 0; depth--) { /* Count frames with same prototype. */ + frame = frame_prev(frame); + if (mref(frame_func(frame)->l.pc, void) == pc) + count++; + } + if (J->pc == J->startpc) { + if (count + J->tailcalled > J->param[JIT_P_recunroll]) { + J->pc++; + if (J->framedepth + J->retdepth == 0) + rec_stop(J, LJ_TRLINK_TAILREC, J->cur.traceno); /* Tail-recursion. */ + else + rec_stop(J, LJ_TRLINK_UPREC, J->cur.traceno); /* Up-recursion. */ + } + } else { + if (count > J->param[JIT_P_callunroll]) { + if (lnk) { /* Possible tail- or up-recursion. */ + lj_trace_flush(J, lnk); /* Flush trace that only returns. */ + /* Set a small, pseudo-random hotcount for a quick retry of JFUNC*. */ + hotcount_set(J2GG(J), J->pc+1, LJ_PRNG_BITS(J, 4)); + } + lj_trace_err(J, LJ_TRERR_CUNROLL); + } + } +} + +/* Record Lua function setup. */ +static void rec_func_setup(jit_State *J) +{ + GCproto *pt = J->pt; + BCReg s, numparams = pt->numparams; + if ((pt->flags & PROTO_NOJIT)) + lj_trace_err(J, LJ_TRERR_CJITOFF); + if (J->baseslot + pt->framesize >= LJ_MAX_JSLOTS) + lj_trace_err(J, LJ_TRERR_STACKOV); + /* Fill up missing parameters with nil. */ + for (s = J->maxslot; s < numparams; s++) + J->base[s] = TREF_NIL; + /* The remaining slots should never be read before they are written. */ + J->maxslot = numparams; +} + +/* Record Lua vararg function setup. */ +static void rec_func_vararg(jit_State *J) +{ + GCproto *pt = J->pt; + BCReg s, fixargs, vframe = J->maxslot+1; + lua_assert((pt->flags & PROTO_VARARG)); + if (J->baseslot + vframe + pt->framesize >= LJ_MAX_JSLOTS) + lj_trace_err(J, LJ_TRERR_STACKOV); + J->base[vframe-1] = J->base[-1]; /* Copy function up. */ + /* Copy fixarg slots up and set their original slots to nil. */ + fixargs = pt->numparams < J->maxslot ? pt->numparams : J->maxslot; + for (s = 0; s < fixargs; s++) { + J->base[vframe+s] = J->base[s]; + J->base[s] = TREF_NIL; + } + J->maxslot = fixargs; + J->framedepth++; + J->base += vframe; + J->baseslot += vframe; +} + +/* Record entry to a Lua function. */ +static void rec_func_lua(jit_State *J) +{ + rec_func_setup(J); + check_call_unroll(J, 0); +} + +/* Record entry to an already compiled function. */ +static void rec_func_jit(jit_State *J, TraceNo lnk) +{ + GCtrace *T; + rec_func_setup(J); + T = traceref(J, lnk); + if (T->linktype == LJ_TRLINK_RETURN) { /* Trace returns to interpreter? */ + check_call_unroll(J, lnk); + /* Temporarily unpatch JFUNC* to continue recording across function. */ + J->patchins = *J->pc; + J->patchpc = (BCIns *)J->pc; + *J->patchpc = T->startins; + return; + } + J->instunroll = 0; /* Cannot continue across a compiled function. */ + if (J->pc == J->startpc && J->framedepth + J->retdepth == 0) + rec_stop(J, LJ_TRLINK_TAILREC, J->cur.traceno); /* Extra tail-recursion. */ + else + rec_stop(J, LJ_TRLINK_ROOT, lnk); /* Link to the function. */ +} + +/* -- Vararg handling ----------------------------------------------------- */ + +/* Detect y = select(x, ...) idiom. */ +static int select_detect(jit_State *J) +{ + BCIns ins = J->pc[1]; + if (bc_op(ins) == BC_CALLM && bc_b(ins) == 2 && bc_c(ins) == 1) { + cTValue *func = &J->L->base[bc_a(ins)]; + if (tvisfunc(func) && funcV(func)->c.ffid == FF_select) + return 1; + } + return 0; +} + +/* Record vararg instruction. */ +static void rec_varg(jit_State *J, BCReg dst, ptrdiff_t nresults) +{ + int32_t numparams = J->pt->numparams; + ptrdiff_t nvararg = frame_delta(J->L->base-1) - numparams - 1; + lua_assert(frame_isvarg(J->L->base-1)); + if (J->framedepth > 0) { /* Simple case: varargs defined on-trace. */ + ptrdiff_t i; + if (nvararg < 0) nvararg = 0; + if (nresults == -1) { + nresults = nvararg; + J->maxslot = dst + (BCReg)nvararg; + } else if (dst + nresults > J->maxslot) { + J->maxslot = dst + (BCReg)nresults; + } + for (i = 0; i < nresults; i++) { + J->base[dst+i] = i < nvararg ? J->base[i - nvararg - 1] : TREF_NIL; + lua_assert(J->base[dst+i] != 0); + } + } else { /* Unknown number of varargs passed to trace. */ + TRef fr = emitir(IRTI(IR_SLOAD), 0, IRSLOAD_READONLY|IRSLOAD_FRAME); + int32_t frofs = 8*(1+numparams)+FRAME_VARG; + if (nresults >= 0) { /* Known fixed number of results. */ + ptrdiff_t i; + if (nvararg > 0) { + ptrdiff_t nload = nvararg >= nresults ? nresults : nvararg; + TRef vbase; + if (nvararg >= nresults) + emitir(IRTGI(IR_GE), fr, lj_ir_kint(J, frofs+8*(int32_t)nresults)); + else + emitir(IRTGI(IR_EQ), fr, lj_ir_kint(J, frame_ftsz(J->L->base-1))); + vbase = emitir(IRTI(IR_SUB), REF_BASE, fr); + vbase = emitir(IRT(IR_ADD, IRT_P32), vbase, lj_ir_kint(J, frofs-8)); + for (i = 0; i < nload; i++) { + IRType t = itype2irt(&J->L->base[i-1-nvararg]); + TRef aref = emitir(IRT(IR_AREF, IRT_P32), + vbase, lj_ir_kint(J, (int32_t)i)); + TRef tr = emitir(IRTG(IR_VLOAD, t), aref, 0); + if (irtype_ispri(t)) tr = TREF_PRI(t); /* Canonicalize primitives. */ + J->base[dst+i] = tr; + } + } else { + emitir(IRTGI(IR_LE), fr, lj_ir_kint(J, frofs)); + nvararg = 0; + } + for (i = nvararg; i < nresults; i++) + J->base[dst+i] = TREF_NIL; + if (dst + (BCReg)nresults > J->maxslot) + J->maxslot = dst + (BCReg)nresults; + } else if (select_detect(J)) { /* y = select(x, ...) */ + TRef tridx = J->base[dst-1]; + TRef tr = TREF_NIL; + ptrdiff_t idx = lj_ffrecord_select_mode(J, tridx, &J->L->base[dst-1]); + if (idx < 0) goto nyivarg; + if (idx != 0 && !tref_isinteger(tridx)) + tridx = emitir(IRTGI(IR_CONV), tridx, IRCONV_INT_NUM|IRCONV_INDEX); + if (idx != 0 && tref_isk(tridx)) { + emitir(IRTGI(idx <= nvararg ? IR_GE : IR_LT), + fr, lj_ir_kint(J, frofs+8*(int32_t)idx)); + frofs -= 8; /* Bias for 1-based index. */ + } else if (idx <= nvararg) { /* Compute size. */ + TRef tmp = emitir(IRTI(IR_ADD), fr, lj_ir_kint(J, -frofs)); + if (numparams) + emitir(IRTGI(IR_GE), tmp, lj_ir_kint(J, 0)); + tr = emitir(IRTI(IR_BSHR), tmp, lj_ir_kint(J, 3)); + if (idx != 0) { + tridx = emitir(IRTI(IR_ADD), tridx, lj_ir_kint(J, -1)); + rec_idx_abc(J, tr, tridx, (uint32_t)nvararg); + } + } else { + TRef tmp = lj_ir_kint(J, frofs); + if (idx != 0) { + TRef tmp2 = emitir(IRTI(IR_BSHL), tridx, lj_ir_kint(J, 3)); + tmp = emitir(IRTI(IR_ADD), tmp2, tmp); + } else { + tr = lj_ir_kint(J, 0); + } + emitir(IRTGI(IR_LT), fr, tmp); + } + if (idx != 0 && idx <= nvararg) { + IRType t; + TRef aref, vbase = emitir(IRTI(IR_SUB), REF_BASE, fr); + vbase = emitir(IRT(IR_ADD, IRT_P32), vbase, lj_ir_kint(J, frofs-8)); + t = itype2irt(&J->L->base[idx-2-nvararg]); + aref = emitir(IRT(IR_AREF, IRT_P32), vbase, tridx); + tr = emitir(IRTG(IR_VLOAD, t), aref, 0); + if (irtype_ispri(t)) tr = TREF_PRI(t); /* Canonicalize primitives. */ + } + J->base[dst-2] = tr; + J->maxslot = dst-1; + J->bcskip = 2; /* Skip CALLM + select. */ + } else { + nyivarg: + setintV(&J->errinfo, BC_VARG); + lj_trace_err_info(J, LJ_TRERR_NYIBC); + } + } +} + +/* -- Record allocations -------------------------------------------------- */ + +static TRef rec_tnew(jit_State *J, uint32_t ah) +{ + uint32_t asize = ah & 0x7ff; + uint32_t hbits = ah >> 11; + if (asize == 0x7ff) asize = 0x801; + return emitir(IRTG(IR_TNEW, IRT_TAB), asize, hbits); +} + +/* -- Record bytecode ops ------------------------------------------------- */ + +/* Prepare for comparison. */ +static void rec_comp_prep(jit_State *J) +{ + /* Prevent merging with snapshot #0 (GC exit) since we fixup the PC. */ + if (J->cur.nsnap == 1 && J->cur.snap[0].ref == J->cur.nins) + emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0); + lj_snap_add(J); +} + +/* Fixup comparison. */ +static void rec_comp_fixup(jit_State *J, const BCIns *pc, int cond) +{ + BCIns jmpins = pc[1]; + const BCIns *npc = pc + 2 + (cond ? bc_j(jmpins) : 0); + SnapShot *snap = &J->cur.snap[J->cur.nsnap-1]; + /* Set PC to opposite target to avoid re-recording the comp. in side trace. */ + J->cur.snapmap[snap->mapofs + snap->nent] = SNAP_MKPC(npc); + J->needsnap = 1; + if (bc_a(jmpins) < J->maxslot) J->maxslot = bc_a(jmpins); + lj_snap_shrink(J); /* Shrink last snapshot if possible. */ +} + +/* Record the next bytecode instruction (_before_ it's executed). */ +void lj_record_ins(jit_State *J) +{ + cTValue *lbase; + RecordIndex ix; + const BCIns *pc; + BCIns ins; + BCOp op; + TRef ra, rb, rc; + + /* Perform post-processing action before recording the next instruction. */ + if (LJ_UNLIKELY(J->postproc != LJ_POST_NONE)) { + switch (J->postproc) { + case LJ_POST_FIXCOMP: /* Fixup comparison. */ + pc = frame_pc(&J2G(J)->tmptv); + rec_comp_fixup(J, pc, (!tvistruecond(&J2G(J)->tmptv2) ^ (bc_op(*pc)&1))); + /* fallthrough */ + case LJ_POST_FIXGUARD: /* Fixup and emit pending guard. */ + case LJ_POST_FIXGUARDSNAP: /* Fixup and emit pending guard and snapshot. */ + if (!tvistruecond(&J2G(J)->tmptv2)) { + J->fold.ins.o ^= 1; /* Flip guard to opposite. */ + if (J->postproc == LJ_POST_FIXGUARDSNAP) { + SnapShot *snap = &J->cur.snap[J->cur.nsnap-1]; + J->cur.snapmap[snap->mapofs+snap->nent-1]--; /* False -> true. */ + } + } + lj_opt_fold(J); /* Emit pending guard. */ + /* fallthrough */ + case LJ_POST_FIXBOOL: + if (!tvistruecond(&J2G(J)->tmptv2)) { + BCReg s; + TValue *tv = J->L->base; + for (s = 0; s < J->maxslot; s++) /* Fixup stack slot (if any). */ + if (J->base[s] == TREF_TRUE && tvisfalse(&tv[s])) { + J->base[s] = TREF_FALSE; + break; + } + } + break; + case LJ_POST_FIXCONST: + { + BCReg s; + TValue *tv = J->L->base; + for (s = 0; s < J->maxslot; s++) /* Constify stack slots (if any). */ + if (J->base[s] == TREF_NIL && !tvisnil(&tv[s])) + J->base[s] = lj_record_constify(J, &tv[s]); + } + break; + case LJ_POST_FFRETRY: /* Suppress recording of retried fast function. */ + if (bc_op(*J->pc) >= BC__MAX) + return; + break; + default: lua_assert(0); break; + } + J->postproc = LJ_POST_NONE; + } + + /* Need snapshot before recording next bytecode (e.g. after a store). */ + if (J->needsnap) { + J->needsnap = 0; + lj_snap_purge(J); + lj_snap_add(J); + J->mergesnap = 1; + } + + /* Skip some bytecodes. */ + if (LJ_UNLIKELY(J->bcskip > 0)) { + J->bcskip--; + return; + } + + /* Record only closed loops for root traces. */ + pc = J->pc; + if (J->framedepth == 0 && + (MSize)((char *)pc - (char *)J->bc_min) >= J->bc_extent) + lj_trace_err(J, LJ_TRERR_LLEAVE); + +#ifdef LUA_USE_ASSERT + rec_check_slots(J); + rec_check_ir(J); +#endif + + /* Keep a copy of the runtime values of var/num/str operands. */ +#define rav (&ix.valv) +#define rbv (&ix.tabv) +#define rcv (&ix.keyv) + + lbase = J->L->base; + ins = *pc; + op = bc_op(ins); + ra = bc_a(ins); + ix.val = 0; + switch (bcmode_a(op)) { + case BCMvar: + copyTV(J->L, rav, &lbase[ra]); ix.val = ra = getslot(J, ra); break; + default: break; /* Handled later. */ + } + rb = bc_b(ins); + rc = bc_c(ins); + switch (bcmode_b(op)) { + case BCMnone: rb = 0; rc = bc_d(ins); break; /* Upgrade rc to 'rd'. */ + case BCMvar: + copyTV(J->L, rbv, &lbase[rb]); ix.tab = rb = getslot(J, rb); break; + default: break; /* Handled later. */ + } + switch (bcmode_c(op)) { + case BCMvar: + copyTV(J->L, rcv, &lbase[rc]); ix.key = rc = getslot(J, rc); break; + case BCMpri: setitype(rcv, ~rc); ix.key = rc = TREF_PRI(IRT_NIL+rc); break; + case BCMnum: { cTValue *tv = proto_knumtv(J->pt, rc); + copyTV(J->L, rcv, tv); ix.key = rc = tvisint(tv) ? lj_ir_kint(J, intV(tv)) : + lj_ir_knumint(J, numV(tv)); } break; + case BCMstr: { GCstr *s = gco2str(proto_kgc(J->pt, ~(ptrdiff_t)rc)); + setstrV(J->L, rcv, s); ix.key = rc = lj_ir_kstr(J, s); } break; + default: break; /* Handled later. */ + } + + switch (op) { + + /* -- Comparison ops ---------------------------------------------------- */ + + case BC_ISLT: case BC_ISGE: case BC_ISLE: case BC_ISGT: +#if LJ_HASFFI + if (tref_iscdata(ra) || tref_iscdata(rc)) { + rec_mm_comp_cdata(J, &ix, op, ((int)op & 2) ? MM_le : MM_lt); + break; + } +#endif + /* Emit nothing for two numeric or string consts. */ + if (!(tref_isk2(ra,rc) && tref_isnumber_str(ra) && tref_isnumber_str(rc))) { + IRType ta = tref_isinteger(ra) ? IRT_INT : tref_type(ra); + IRType tc = tref_isinteger(rc) ? IRT_INT : tref_type(rc); + int irop; + if (ta != tc) { + /* Widen mixed number/int comparisons to number/number comparison. */ + if (ta == IRT_INT && tc == IRT_NUM) { + ra = emitir(IRTN(IR_CONV), ra, IRCONV_NUM_INT); + ta = IRT_NUM; + } else if (ta == IRT_NUM && tc == IRT_INT) { + rc = emitir(IRTN(IR_CONV), rc, IRCONV_NUM_INT); + } else if (LJ_52) { + ta = IRT_NIL; /* Force metamethod for different types. */ + } else if (!((ta == IRT_FALSE || ta == IRT_TRUE) && + (tc == IRT_FALSE || tc == IRT_TRUE))) { + break; /* Interpreter will throw for two different types. */ + } + } + rec_comp_prep(J); + irop = (int)op - (int)BC_ISLT + (int)IR_LT; + if (ta == IRT_NUM) { + if ((irop & 1)) irop ^= 4; /* ISGE/ISGT are unordered. */ + if (!lj_ir_numcmp(numberVnum(rav), numberVnum(rcv), (IROp)irop)) + irop ^= 5; + } else if (ta == IRT_INT) { + if (!lj_ir_numcmp(numberVnum(rav), numberVnum(rcv), (IROp)irop)) + irop ^= 1; + } else if (ta == IRT_STR) { + if (!lj_ir_strcmp(strV(rav), strV(rcv), (IROp)irop)) irop ^= 1; + ra = lj_ir_call(J, IRCALL_lj_str_cmp, ra, rc); + rc = lj_ir_kint(J, 0); + ta = IRT_INT; + } else { + rec_mm_comp(J, &ix, (int)op); + break; + } + emitir(IRTG(irop, ta), ra, rc); + rec_comp_fixup(J, J->pc, ((int)op ^ irop) & 1); + } + break; + + case BC_ISEQV: case BC_ISNEV: + case BC_ISEQS: case BC_ISNES: + case BC_ISEQN: case BC_ISNEN: + case BC_ISEQP: case BC_ISNEP: +#if LJ_HASFFI + if (tref_iscdata(ra) || tref_iscdata(rc)) { + rec_mm_comp_cdata(J, &ix, op, MM_eq); + break; + } +#endif + /* Emit nothing for two non-table, non-udata consts. */ + if (!(tref_isk2(ra, rc) && !(tref_istab(ra) || tref_isudata(ra)))) { + int diff; + rec_comp_prep(J); + diff = lj_record_objcmp(J, ra, rc, rav, rcv); + if (diff == 2 || !(tref_istab(ra) || tref_isudata(ra))) + rec_comp_fixup(J, J->pc, ((int)op & 1) == !diff); + else if (diff == 1) /* Only check __eq if different, but same type. */ + rec_mm_equal(J, &ix, (int)op); + } + break; + + /* -- Unary test and copy ops ------------------------------------------- */ + + case BC_ISTC: case BC_ISFC: + if ((op & 1) == tref_istruecond(rc)) + rc = 0; /* Don't store if condition is not true. */ + /* fallthrough */ + case BC_IST: case BC_ISF: /* Type specialization suffices. */ + if (bc_a(pc[1]) < J->maxslot) + J->maxslot = bc_a(pc[1]); /* Shrink used slots. */ + break; + + /* -- Unary ops --------------------------------------------------------- */ + + case BC_NOT: + /* Type specialization already forces const result. */ + rc = tref_istruecond(rc) ? TREF_FALSE : TREF_TRUE; + break; + + case BC_LEN: + if (tref_isstr(rc)) + rc = emitir(IRTI(IR_FLOAD), rc, IRFL_STR_LEN); + else if (!LJ_52 && tref_istab(rc)) + rc = lj_ir_call(J, IRCALL_lj_tab_len, rc); + else + rc = rec_mm_len(J, rc, rcv); + break; + + /* -- Arithmetic ops ---------------------------------------------------- */ + + case BC_UNM: + if (tref_isnumber_str(rc)) { + rc = lj_opt_narrow_unm(J, rc, rcv); + } else { + ix.tab = rc; + copyTV(J->L, &ix.tabv, rcv); + rc = rec_mm_arith(J, &ix, MM_unm); + } + break; + + case BC_ADDNV: case BC_SUBNV: case BC_MULNV: case BC_DIVNV: case BC_MODNV: + /* Swap rb/rc and rbv/rcv. rav is temp. */ + ix.tab = rc; ix.key = rc = rb; rb = ix.tab; + copyTV(J->L, rav, rbv); + copyTV(J->L, rbv, rcv); + copyTV(J->L, rcv, rav); + if (op == BC_MODNV) + goto recmod; + /* fallthrough */ + case BC_ADDVN: case BC_SUBVN: case BC_MULVN: case BC_DIVVN: + case BC_ADDVV: case BC_SUBVV: case BC_MULVV: case BC_DIVVV: { + MMS mm = bcmode_mm(op); + if (tref_isnumber_str(rb) && tref_isnumber_str(rc)) + rc = lj_opt_narrow_arith(J, rb, rc, rbv, rcv, + (int)mm - (int)MM_add + (int)IR_ADD); + else + rc = rec_mm_arith(J, &ix, mm); + break; + } + + case BC_MODVN: case BC_MODVV: + recmod: + if (tref_isnumber_str(rb) && tref_isnumber_str(rc)) + rc = lj_opt_narrow_mod(J, rb, rc, rcv); + else + rc = rec_mm_arith(J, &ix, MM_mod); + break; + + case BC_POW: + if (tref_isnumber_str(rb) && tref_isnumber_str(rc)) + rc = lj_opt_narrow_pow(J, lj_ir_tonum(J, rb), rc, rcv); + else + rc = rec_mm_arith(J, &ix, MM_pow); + break; + + /* -- Constant and move ops --------------------------------------------- */ + + case BC_MOV: + /* Clear gap of method call to avoid resurrecting previous refs. */ + if (ra > J->maxslot) J->base[ra-1] = 0; + break; + case BC_KSTR: case BC_KNUM: case BC_KPRI: + break; + case BC_KSHORT: + rc = lj_ir_kint(J, (int32_t)(int16_t)rc); + break; + case BC_KNIL: + while (ra <= rc) + J->base[ra++] = TREF_NIL; + if (rc >= J->maxslot) J->maxslot = rc+1; + break; +#if LJ_HASFFI + case BC_KCDATA: + rc = lj_ir_kgc(J, proto_kgc(J->pt, ~(ptrdiff_t)rc), IRT_CDATA); + break; +#endif + + /* -- Upvalue and function ops ------------------------------------------ */ + + case BC_UGET: + rc = rec_upvalue(J, rc, 0); + break; + case BC_USETV: case BC_USETS: case BC_USETN: case BC_USETP: + rec_upvalue(J, ra, rc); + break; + + /* -- Table ops --------------------------------------------------------- */ + + case BC_GGET: case BC_GSET: + settabV(J->L, &ix.tabv, tabref(J->fn->l.env)); + ix.tab = emitir(IRT(IR_FLOAD, IRT_TAB), getcurrf(J), IRFL_FUNC_ENV); + ix.idxchain = LJ_MAX_IDXCHAIN; + rc = lj_record_idx(J, &ix); + break; + + case BC_TGETB: case BC_TSETB: + setintV(&ix.keyv, (int32_t)rc); + ix.key = lj_ir_kint(J, (int32_t)rc); + /* fallthrough */ + case BC_TGETV: case BC_TGETS: case BC_TSETV: case BC_TSETS: + ix.idxchain = LJ_MAX_IDXCHAIN; + rc = lj_record_idx(J, &ix); + break; + + case BC_TNEW: + rc = rec_tnew(J, rc); + break; + case BC_TDUP: + rc = emitir(IRTG(IR_TDUP, IRT_TAB), + lj_ir_ktab(J, gco2tab(proto_kgc(J->pt, ~(ptrdiff_t)rc))), 0); + break; + + /* -- Calls and vararg handling ----------------------------------------- */ + + case BC_ITERC: + J->base[ra] = getslot(J, ra-3); + J->base[ra+1] = getslot(J, ra-2); + J->base[ra+2] = getslot(J, ra-1); + { /* Do the actual copy now because lj_record_call needs the values. */ + TValue *b = &J->L->base[ra]; + copyTV(J->L, b, b-3); + copyTV(J->L, b+1, b-2); + copyTV(J->L, b+2, b-1); + } + lj_record_call(J, ra, (ptrdiff_t)rc-1); + break; + + /* L->top is set to L->base+ra+rc+NARGS-1+1. See lj_dispatch_ins(). */ + case BC_CALLM: + rc = (BCReg)(J->L->top - J->L->base) - ra; + /* fallthrough */ + case BC_CALL: + lj_record_call(J, ra, (ptrdiff_t)rc-1); + break; + + case BC_CALLMT: + rc = (BCReg)(J->L->top - J->L->base) - ra; + /* fallthrough */ + case BC_CALLT: + lj_record_tailcall(J, ra, (ptrdiff_t)rc-1); + break; + + case BC_VARG: + rec_varg(J, ra, (ptrdiff_t)rb-1); + break; + + /* -- Returns ----------------------------------------------------------- */ + + case BC_RETM: + /* L->top is set to L->base+ra+rc+NRESULTS-1, see lj_dispatch_ins(). */ + rc = (BCReg)(J->L->top - J->L->base) - ra + 1; + /* fallthrough */ + case BC_RET: case BC_RET0: case BC_RET1: + lj_record_ret(J, ra, (ptrdiff_t)rc-1); + break; + + /* -- Loops and branches ------------------------------------------------ */ + + case BC_FORI: + if (rec_for(J, pc, 0) != LOOPEV_LEAVE) + J->loopref = J->cur.nins; + break; + case BC_JFORI: + lua_assert(bc_op(pc[(ptrdiff_t)rc-BCBIAS_J]) == BC_JFORL); + if (rec_for(J, pc, 0) != LOOPEV_LEAVE) /* Link to existing loop. */ + rec_stop(J, LJ_TRLINK_ROOT, bc_d(pc[(ptrdiff_t)rc-BCBIAS_J])); + /* Continue tracing if the loop is not entered. */ + break; + + case BC_FORL: + rec_loop_interp(J, pc, rec_for(J, pc+((ptrdiff_t)rc-BCBIAS_J), 1)); + break; + case BC_ITERL: + rec_loop_interp(J, pc, rec_iterl(J, *pc)); + break; + case BC_LOOP: + rec_loop_interp(J, pc, rec_loop(J, ra)); + break; + + case BC_JFORL: + rec_loop_jit(J, rc, rec_for(J, pc+bc_j(traceref(J, rc)->startins), 1)); + break; + case BC_JITERL: + rec_loop_jit(J, rc, rec_iterl(J, traceref(J, rc)->startins)); + break; + case BC_JLOOP: + rec_loop_jit(J, rc, rec_loop(J, ra)); + break; + + case BC_IFORL: + case BC_IITERL: + case BC_ILOOP: + case BC_IFUNCF: + case BC_IFUNCV: + lj_trace_err(J, LJ_TRERR_BLACKL); + break; + + case BC_JMP: + if (ra < J->maxslot) + J->maxslot = ra; /* Shrink used slots. */ + break; + + /* -- Function headers -------------------------------------------------- */ + + case BC_FUNCF: + rec_func_lua(J); + break; + case BC_JFUNCF: + rec_func_jit(J, rc); + break; + + case BC_FUNCV: + rec_func_vararg(J); + rec_func_lua(J); + break; + case BC_JFUNCV: + lua_assert(0); /* Cannot happen. No hotcall counting for varag funcs. */ + break; + + case BC_FUNCC: + case BC_FUNCCW: + lj_ffrecord_func(J); + break; + + default: + if (op >= BC__MAX) { + lj_ffrecord_func(J); + break; + } + /* fallthrough */ + case BC_ITERN: + case BC_ISNEXT: + case BC_CAT: + case BC_UCLO: + case BC_FNEW: + case BC_TSETM: + setintV(&J->errinfo, (int32_t)op); + lj_trace_err_info(J, LJ_TRERR_NYIBC); + break; + } + + /* rc == 0 if we have no result yet, e.g. pending __index metamethod call. */ + if (bcmode_a(op) == BCMdst && rc) { + J->base[ra] = rc; + if (ra >= J->maxslot) J->maxslot = ra+1; + } + +#undef rav +#undef rbv +#undef rcv + + /* Limit the number of recorded IR instructions. */ + if (J->cur.nins > REF_FIRST+(IRRef)J->param[JIT_P_maxrecord]) + lj_trace_err(J, LJ_TRERR_TRACEOV); +} + +/* -- Recording setup ----------------------------------------------------- */ + +/* Setup recording for a root trace started by a hot loop. */ +static const BCIns *rec_setup_root(jit_State *J) +{ + /* Determine the next PC and the bytecode range for the loop. */ + const BCIns *pcj, *pc = J->pc; + BCIns ins = *pc; + BCReg ra = bc_a(ins); + switch (bc_op(ins)) { + case BC_FORL: + J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns); + pc += 1+bc_j(ins); + J->bc_min = pc; + break; + case BC_ITERL: + lua_assert(bc_op(pc[-1]) == BC_ITERC); + J->maxslot = ra + bc_b(pc[-1]) - 1; + J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns); + pc += 1+bc_j(ins); + lua_assert(bc_op(pc[-1]) == BC_JMP); + J->bc_min = pc; + break; + case BC_LOOP: + /* Only check BC range for real loops, but not for "repeat until true". */ + pcj = pc + bc_j(ins); + ins = *pcj; + if (bc_op(ins) == BC_JMP && bc_j(ins) < 0) { + J->bc_min = pcj+1 + bc_j(ins); + J->bc_extent = (MSize)(-bc_j(ins))*sizeof(BCIns); + } + J->maxslot = ra; + pc++; + break; + case BC_RET: + case BC_RET0: + case BC_RET1: + /* No bytecode range check for down-recursive root traces. */ + J->maxslot = ra + bc_d(ins); + break; + case BC_FUNCF: + /* No bytecode range check for root traces started by a hot call. */ + J->maxslot = J->pt->numparams; + pc++; + break; + default: + lua_assert(0); + break; + } + return pc; +} + +/* Setup for recording a new trace. */ +void lj_record_setup(jit_State *J) +{ + uint32_t i; + + /* Initialize state related to current trace. */ + memset(J->slot, 0, sizeof(J->slot)); + memset(J->chain, 0, sizeof(J->chain)); + memset(J->bpropcache, 0, sizeof(J->bpropcache)); + J->scev.idx = REF_NIL; + + J->baseslot = 1; /* Invoking function is at base[-1]. */ + J->base = J->slot + J->baseslot; + J->maxslot = 0; + J->framedepth = 0; + J->retdepth = 0; + + J->instunroll = J->param[JIT_P_instunroll]; + J->loopunroll = J->param[JIT_P_loopunroll]; + J->tailcalled = 0; + J->loopref = 0; + + J->bc_min = NULL; /* Means no limit. */ + J->bc_extent = ~(MSize)0; + + /* Emit instructions for fixed references. Also triggers initial IR alloc. */ + emitir_raw(IRT(IR_BASE, IRT_P32), J->parent, J->exitno); + for (i = 0; i <= 2; i++) { + IRIns *ir = IR(REF_NIL-i); + ir->i = 0; + ir->t.irt = (uint8_t)(IRT_NIL+i); + ir->o = IR_KPRI; + ir->prev = 0; + } + J->cur.nk = REF_TRUE; + + J->startpc = J->pc; + setmref(J->cur.startpc, J->pc); + if (J->parent) { /* Side trace. */ + GCtrace *T = traceref(J, J->parent); + TraceNo root = T->root ? T->root : J->parent; + J->cur.root = (uint16_t)root; + J->cur.startins = BCINS_AD(BC_JMP, 0, 0); + /* Check whether we could at least potentially form an extra loop. */ + if (J->exitno == 0 && T->snap[0].nent == 0) { + /* We can narrow a FORL for some side traces, too. */ + if (J->pc > proto_bc(J->pt) && bc_op(J->pc[-1]) == BC_JFORI && + bc_d(J->pc[bc_j(J->pc[-1])-1]) == root) { + lj_snap_add(J); + rec_for_loop(J, J->pc-1, &J->scev, 1); + goto sidecheck; + } + } else { + J->startpc = NULL; /* Prevent forming an extra loop. */ + } + lj_snap_replay(J, T); + sidecheck: + if (traceref(J, J->cur.root)->nchild >= J->param[JIT_P_maxside] || + T->snap[J->exitno].count >= J->param[JIT_P_hotexit] + + J->param[JIT_P_tryside]) { + rec_stop(J, LJ_TRLINK_INTERP, 0); + } + } else { /* Root trace. */ + J->cur.root = 0; + J->cur.startins = *J->pc; + J->pc = rec_setup_root(J); + /* Note: the loop instruction itself is recorded at the end and not + ** at the start! So snapshot #0 needs to point to the *next* instruction. + */ + lj_snap_add(J); + if (bc_op(J->cur.startins) == BC_FORL) + rec_for_loop(J, J->pc-1, &J->scev, 1); + if (1 + J->pt->framesize >= LJ_MAX_JSLOTS) + lj_trace_err(J, LJ_TRERR_STACKOV); + } +#ifdef LUAJIT_ENABLE_CHECKHOOK + /* Regularly check for instruction/line hooks from compiled code and + ** exit to the interpreter if the hooks are set. + ** + ** This is a compile-time option and disabled by default, since the + ** hook checks may be quite expensive in tight loops. + ** + ** Note this is only useful if hooks are *not* set most of the time. + ** Use this only if you want to *asynchronously* interrupt the execution. + ** + ** You can set the instruction hook via lua_sethook() with a count of 1 + ** from a signal handler or another native thread. Please have a look + ** at the first few functions in luajit.c for an example (Ctrl-C handler). + */ + { + TRef tr = emitir(IRT(IR_XLOAD, IRT_U8), + lj_ir_kptr(J, &J2G(J)->hookmask), IRXLOAD_VOLATILE); + tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (LUA_MASKLINE|LUA_MASKCOUNT))); + emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, 0)); + } +#endif +} + +#undef IR +#undef emitir_raw +#undef emitir + +#endif diff --git a/external/lua/luajit/src/src/lj_record.h b/external/lua/luajit/src/src/lj_record.h new file mode 100644 index 0000000000..287b26045e --- /dev/null +++ b/external/lua/luajit/src/src/lj_record.h @@ -0,0 +1,44 @@ +/* +** Trace recorder (bytecode -> SSA IR). +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_RECORD_H +#define _LJ_RECORD_H + +#include "lj_obj.h" +#include "lj_jit.h" + +#if LJ_HASJIT +/* Context for recording an indexed load/store. */ +typedef struct RecordIndex { + TValue tabv; /* Runtime value of table (or indexed object). */ + TValue keyv; /* Runtime value of key. */ + TValue valv; /* Runtime value of stored value. */ + TValue mobjv; /* Runtime value of metamethod object. */ + GCtab *mtv; /* Runtime value of metatable object. */ + cTValue *oldv; /* Runtime value of previously stored value. */ + TRef tab; /* Table (or indexed object) reference. */ + TRef key; /* Key reference. */ + TRef val; /* Value reference for a store or 0 for a load. */ + TRef mt; /* Metatable reference. */ + TRef mobj; /* Metamethod object reference. */ + int idxchain; /* Index indirections left or 0 for raw lookup. */ +} RecordIndex; + +LJ_FUNC int lj_record_objcmp(jit_State *J, TRef a, TRef b, + cTValue *av, cTValue *bv); +LJ_FUNC TRef lj_record_constify(jit_State *J, cTValue *o); + +LJ_FUNC void lj_record_call(jit_State *J, BCReg func, ptrdiff_t nargs); +LJ_FUNC void lj_record_tailcall(jit_State *J, BCReg func, ptrdiff_t nargs); +LJ_FUNC void lj_record_ret(jit_State *J, BCReg rbase, ptrdiff_t gotresults); + +LJ_FUNC int lj_record_mm_lookup(jit_State *J, RecordIndex *ix, MMS mm); +LJ_FUNC TRef lj_record_idx(jit_State *J, RecordIndex *ix); + +LJ_FUNC void lj_record_ins(jit_State *J); +LJ_FUNC void lj_record_setup(jit_State *J); +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_snap.c b/external/lua/luajit/src/src/lj_snap.c new file mode 100644 index 0000000000..aea6102c4c --- /dev/null +++ b/external/lua/luajit/src/src/lj_snap.c @@ -0,0 +1,860 @@ +/* +** Snapshot handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_snap_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_gc.h" +#include "lj_tab.h" +#include "lj_state.h" +#include "lj_frame.h" +#include "lj_bc.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_iropt.h" +#include "lj_trace.h" +#include "lj_snap.h" +#include "lj_target.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#include "lj_cdata.h" +#endif + +/* Some local macros to save typing. Undef'd at the end. */ +#define IR(ref) (&J->cur.ir[(ref)]) + +/* Pass IR on to next optimization in chain (FOLD). */ +#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J)) + +/* Emit raw IR without passing through optimizations. */ +#define emitir_raw(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_ir_emit(J)) + +/* -- Snapshot buffer allocation ------------------------------------------ */ + +/* Grow snapshot buffer. */ +void lj_snap_grow_buf_(jit_State *J, MSize need) +{ + MSize maxsnap = (MSize)J->param[JIT_P_maxsnap]; + if (need > maxsnap) + lj_trace_err(J, LJ_TRERR_SNAPOV); + lj_mem_growvec(J->L, J->snapbuf, J->sizesnap, maxsnap, SnapShot); + J->cur.snap = J->snapbuf; +} + +/* Grow snapshot map buffer. */ +void lj_snap_grow_map_(jit_State *J, MSize need) +{ + if (need < 2*J->sizesnapmap) + need = 2*J->sizesnapmap; + else if (need < 64) + need = 64; + J->snapmapbuf = (SnapEntry *)lj_mem_realloc(J->L, J->snapmapbuf, + J->sizesnapmap*sizeof(SnapEntry), need*sizeof(SnapEntry)); + J->cur.snapmap = J->snapmapbuf; + J->sizesnapmap = need; +} + +/* -- Snapshot generation ------------------------------------------------- */ + +/* Add all modified slots to the snapshot. */ +static MSize snapshot_slots(jit_State *J, SnapEntry *map, BCReg nslots) +{ + IRRef retf = J->chain[IR_RETF]; /* Limits SLOAD restore elimination. */ + BCReg s; + MSize n = 0; + for (s = 0; s < nslots; s++) { + TRef tr = J->slot[s]; + IRRef ref = tref_ref(tr); + if (ref) { + SnapEntry sn = SNAP_TR(s, tr); + IRIns *ir = IR(ref); + if (!(sn & (SNAP_CONT|SNAP_FRAME)) && + ir->o == IR_SLOAD && ir->op1 == s && ref > retf) { + /* No need to snapshot unmodified non-inherited slots. */ + if (!(ir->op2 & IRSLOAD_INHERIT)) + continue; + /* No need to restore readonly slots and unmodified non-parent slots. */ + if (!(LJ_DUALNUM && (ir->op2 & IRSLOAD_CONVERT)) && + (ir->op2 & (IRSLOAD_READONLY|IRSLOAD_PARENT)) != IRSLOAD_PARENT) + sn |= SNAP_NORESTORE; + } + if (LJ_SOFTFP && irt_isnum(ir->t)) + sn |= SNAP_SOFTFPNUM; + map[n++] = sn; + } + } + return n; +} + +/* Add frame links at the end of the snapshot. */ +static BCReg snapshot_framelinks(jit_State *J, SnapEntry *map) +{ + cTValue *frame = J->L->base - 1; + cTValue *lim = J->L->base - J->baseslot; + cTValue *ftop = frame + funcproto(frame_func(frame))->framesize; + MSize f = 0; + map[f++] = SNAP_MKPC(J->pc); /* The current PC is always the first entry. */ + while (frame > lim) { /* Backwards traversal of all frames above base. */ + if (frame_islua(frame)) { + map[f++] = SNAP_MKPC(frame_pc(frame)); + frame = frame_prevl(frame); + if (frame + funcproto(frame_func(frame))->framesize > ftop) + ftop = frame + funcproto(frame_func(frame))->framesize; + } else if (frame_iscont(frame)) { + map[f++] = SNAP_MKFTSZ(frame_ftsz(frame)); + map[f++] = SNAP_MKPC(frame_contpc(frame)); + frame = frame_prevd(frame); + } else { + lua_assert(!frame_isc(frame)); + map[f++] = SNAP_MKFTSZ(frame_ftsz(frame)); + frame = frame_prevd(frame); + } + } + lua_assert(f == (MSize)(1 + J->framedepth)); + return (BCReg)(ftop - lim); +} + +/* Take a snapshot of the current stack. */ +static void snapshot_stack(jit_State *J, SnapShot *snap, MSize nsnapmap) +{ + BCReg nslots = J->baseslot + J->maxslot; + MSize nent; + SnapEntry *p; + /* Conservative estimate. */ + lj_snap_grow_map(J, nsnapmap + nslots + (MSize)J->framedepth+1); + p = &J->cur.snapmap[nsnapmap]; + nent = snapshot_slots(J, p, nslots); + snap->topslot = (uint8_t)snapshot_framelinks(J, p + nent); + snap->mapofs = (uint16_t)nsnapmap; + snap->ref = (IRRef1)J->cur.nins; + snap->nent = (uint8_t)nent; + snap->nslots = (uint8_t)nslots; + snap->count = 0; + J->cur.nsnapmap = (uint16_t)(nsnapmap + nent + 1 + J->framedepth); +} + +/* Add or merge a snapshot. */ +void lj_snap_add(jit_State *J) +{ + MSize nsnap = J->cur.nsnap; + MSize nsnapmap = J->cur.nsnapmap; + /* Merge if no ins. inbetween or if requested and no guard inbetween. */ + if (J->mergesnap ? !irt_isguard(J->guardemit) : + (nsnap > 0 && J->cur.snap[nsnap-1].ref == J->cur.nins)) { + if (nsnap == 1) { /* But preserve snap #0 PC. */ + emitir_raw(IRT(IR_NOP, IRT_NIL), 0, 0); + goto nomerge; + } + nsnapmap = J->cur.snap[--nsnap].mapofs; + } else { + nomerge: + lj_snap_grow_buf(J, nsnap+1); + J->cur.nsnap = (uint16_t)(nsnap+1); + } + J->mergesnap = 0; + J->guardemit.irt = 0; + snapshot_stack(J, &J->cur.snap[nsnap], nsnapmap); +} + +/* -- Snapshot modification ----------------------------------------------- */ + +#define SNAP_USEDEF_SLOTS (LJ_MAX_JSLOTS+LJ_STACK_EXTRA) + +/* Find unused slots with reaching-definitions bytecode data-flow analysis. */ +static BCReg snap_usedef(jit_State *J, uint8_t *udf, + const BCIns *pc, BCReg maxslot) +{ + BCReg s; + GCobj *o; + + if (maxslot == 0) return 0; +#ifdef LUAJIT_USE_VALGRIND + /* Avoid errors for harmless reads beyond maxslot. */ + memset(udf, 1, SNAP_USEDEF_SLOTS); +#else + memset(udf, 1, maxslot); +#endif + + /* Treat open upvalues as used. */ + o = gcref(J->L->openupval); + while (o) { + if (uvval(gco2uv(o)) < J->L->base) break; + udf[uvval(gco2uv(o)) - J->L->base] = 0; + o = gcref(o->gch.nextgc); + } + +#define USE_SLOT(s) udf[(s)] &= ~1 +#define DEF_SLOT(s) udf[(s)] *= 3 + + /* Scan through following bytecode and check for uses/defs. */ + lua_assert(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc); + for (;;) { + BCIns ins = *pc++; + BCOp op = bc_op(ins); + switch (bcmode_b(op)) { + case BCMvar: USE_SLOT(bc_b(ins)); break; + default: break; + } + switch (bcmode_c(op)) { + case BCMvar: USE_SLOT(bc_c(ins)); break; + case BCMrbase: + lua_assert(op == BC_CAT); + for (s = bc_b(ins); s <= bc_c(ins); s++) USE_SLOT(s); + for (; s < maxslot; s++) DEF_SLOT(s); + break; + case BCMjump: + handle_jump: { + BCReg minslot = bc_a(ins); + if (op >= BC_FORI && op <= BC_JFORL) minslot += FORL_EXT; + else if (op >= BC_ITERL && op <= BC_JITERL) minslot += bc_b(pc[-2])-1; + else if (op == BC_UCLO) { pc += bc_j(ins); break; } + for (s = minslot; s < maxslot; s++) DEF_SLOT(s); + return minslot < maxslot ? minslot : maxslot; + } + case BCMlit: + if (op == BC_JFORL || op == BC_JITERL || op == BC_JLOOP) { + goto handle_jump; + } else if (bc_isret(op)) { + BCReg top = op == BC_RETM ? maxslot : (bc_a(ins) + bc_d(ins)-1); + for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s); + for (; s < top; s++) USE_SLOT(s); + for (; s < maxslot; s++) DEF_SLOT(s); + return 0; + } + break; + case BCMfunc: return maxslot; /* NYI: will abort, anyway. */ + default: break; + } + switch (bcmode_a(op)) { + case BCMvar: USE_SLOT(bc_a(ins)); break; + case BCMdst: + if (!(op == BC_ISTC || op == BC_ISFC)) DEF_SLOT(bc_a(ins)); + break; + case BCMbase: + if (op >= BC_CALLM && op <= BC_VARG) { + BCReg top = (op == BC_CALLM || op == BC_CALLMT || bc_c(ins) == 0) ? + maxslot : (bc_a(ins) + bc_c(ins)); + s = bc_a(ins) - ((op == BC_ITERC || op == BC_ITERN) ? 3 : 0); + for (; s < top; s++) USE_SLOT(s); + for (; s < maxslot; s++) DEF_SLOT(s); + if (op == BC_CALLT || op == BC_CALLMT) { + for (s = 0; s < bc_a(ins); s++) DEF_SLOT(s); + return 0; + } + } else if (op == BC_KNIL) { + for (s = bc_a(ins); s <= bc_d(ins); s++) DEF_SLOT(s); + } else if (op == BC_TSETM) { + for (s = bc_a(ins)-1; s < maxslot; s++) USE_SLOT(s); + } + break; + default: break; + } + lua_assert(pc >= proto_bc(J->pt) && pc < proto_bc(J->pt) + J->pt->sizebc); + } + +#undef USE_SLOT +#undef DEF_SLOT + + return 0; /* unreachable */ +} + +/* Purge dead slots before the next snapshot. */ +void lj_snap_purge(jit_State *J) +{ + uint8_t udf[SNAP_USEDEF_SLOTS]; + BCReg maxslot = J->maxslot; + BCReg s = snap_usedef(J, udf, J->pc, maxslot); + for (; s < maxslot; s++) + if (udf[s] != 0) + J->base[s] = 0; /* Purge dead slots. */ +} + +/* Shrink last snapshot. */ +void lj_snap_shrink(jit_State *J) +{ + SnapShot *snap = &J->cur.snap[J->cur.nsnap-1]; + SnapEntry *map = &J->cur.snapmap[snap->mapofs]; + MSize n, m, nlim, nent = snap->nent; + uint8_t udf[SNAP_USEDEF_SLOTS]; + BCReg maxslot = J->maxslot; + BCReg minslot = snap_usedef(J, udf, snap_pc(map[nent]), maxslot); + BCReg baseslot = J->baseslot; + maxslot += baseslot; + minslot += baseslot; + snap->nslots = (uint8_t)maxslot; + for (n = m = 0; n < nent; n++) { /* Remove unused slots from snapshot. */ + BCReg s = snap_slot(map[n]); + if (s < minslot || (s < maxslot && udf[s-baseslot] == 0)) + map[m++] = map[n]; /* Only copy used slots. */ + } + snap->nent = (uint8_t)m; + nlim = J->cur.nsnapmap - snap->mapofs - 1; + while (n <= nlim) map[m++] = map[n++]; /* Move PC + frame links down. */ + J->cur.nsnapmap = (uint16_t)(snap->mapofs + m); /* Free up space in map. */ +} + +/* -- Snapshot access ----------------------------------------------------- */ + +/* Initialize a Bloom Filter with all renamed refs. +** There are very few renames (often none), so the filter has +** very few bits set. This makes it suitable for negative filtering. +*/ +static BloomFilter snap_renamefilter(GCtrace *T, SnapNo lim) +{ + BloomFilter rfilt = 0; + IRIns *ir; + for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--) + if (ir->op2 <= lim) + bloomset(rfilt, ir->op1); + return rfilt; +} + +/* Process matching renames to find the original RegSP. */ +static RegSP snap_renameref(GCtrace *T, SnapNo lim, IRRef ref, RegSP rs) +{ + IRIns *ir; + for (ir = &T->ir[T->nins-1]; ir->o == IR_RENAME; ir--) + if (ir->op1 == ref && ir->op2 <= lim) + rs = ir->prev; + return rs; +} + +/* Copy RegSP from parent snapshot to the parent links of the IR. */ +IRIns *lj_snap_regspmap(GCtrace *T, SnapNo snapno, IRIns *ir) +{ + SnapShot *snap = &T->snap[snapno]; + SnapEntry *map = &T->snapmap[snap->mapofs]; + BloomFilter rfilt = snap_renamefilter(T, snapno); + MSize n = 0; + IRRef ref = 0; + for ( ; ; ir++) { + uint32_t rs; + if (ir->o == IR_SLOAD) { + if (!(ir->op2 & IRSLOAD_PARENT)) break; + for ( ; ; n++) { + lua_assert(n < snap->nent); + if (snap_slot(map[n]) == ir->op1) { + ref = snap_ref(map[n++]); + break; + } + } + } else if (LJ_SOFTFP && ir->o == IR_HIOP) { + ref++; + } else if (ir->o == IR_PVAL) { + ref = ir->op1 + REF_BIAS; + } else { + break; + } + rs = T->ir[ref].prev; + if (bloomtest(rfilt, ref)) + rs = snap_renameref(T, snapno, ref, rs); + ir->prev = (uint16_t)rs; + lua_assert(regsp_used(rs)); + } + return ir; +} + +/* -- Snapshot replay ----------------------------------------------------- */ + +/* Replay constant from parent trace. */ +static TRef snap_replay_const(jit_State *J, IRIns *ir) +{ + /* Only have to deal with constants that can occur in stack slots. */ + switch ((IROp)ir->o) { + case IR_KPRI: return TREF_PRI(irt_type(ir->t)); + case IR_KINT: return lj_ir_kint(J, ir->i); + case IR_KGC: return lj_ir_kgc(J, ir_kgc(ir), irt_t(ir->t)); + case IR_KNUM: return lj_ir_k64(J, IR_KNUM, ir_knum(ir)); + case IR_KINT64: return lj_ir_k64(J, IR_KINT64, ir_kint64(ir)); + case IR_KPTR: return lj_ir_kptr(J, ir_kptr(ir)); /* Continuation. */ + default: lua_assert(0); return TREF_NIL; break; + } +} + +/* De-duplicate parent reference. */ +static TRef snap_dedup(jit_State *J, SnapEntry *map, MSize nmax, IRRef ref) +{ + MSize j; + for (j = 0; j < nmax; j++) + if (snap_ref(map[j]) == ref) + return J->slot[snap_slot(map[j])] & ~(SNAP_CONT|SNAP_FRAME); + return 0; +} + +/* Emit parent reference with de-duplication. */ +static TRef snap_pref(jit_State *J, GCtrace *T, SnapEntry *map, MSize nmax, + BloomFilter seen, IRRef ref) +{ + IRIns *ir = &T->ir[ref]; + TRef tr; + if (irref_isk(ref)) + tr = snap_replay_const(J, ir); + else if (!regsp_used(ir->prev)) + tr = 0; + else if (!bloomtest(seen, ref) || (tr = snap_dedup(J, map, nmax, ref)) == 0) + tr = emitir(IRT(IR_PVAL, irt_type(ir->t)), ref - REF_BIAS, 0); + return tr; +} + +/* Check whether a sunk store corresponds to an allocation. Slow path. */ +static int snap_sunk_store2(jit_State *J, IRIns *ira, IRIns *irs) +{ + if (irs->o == IR_ASTORE || irs->o == IR_HSTORE || + irs->o == IR_FSTORE || irs->o == IR_XSTORE) { + IRIns *irk = IR(irs->op1); + if (irk->o == IR_AREF || irk->o == IR_HREFK) + irk = IR(irk->op1); + return (IR(irk->op1) == ira); + } + return 0; +} + +/* Check whether a sunk store corresponds to an allocation. Fast path. */ +static LJ_AINLINE int snap_sunk_store(jit_State *J, IRIns *ira, IRIns *irs) +{ + if (irs->s != 255) + return (ira + irs->s == irs); /* Fast check. */ + return snap_sunk_store2(J, ira, irs); +} + +/* Replay snapshot state to setup side trace. */ +void lj_snap_replay(jit_State *J, GCtrace *T) +{ + SnapShot *snap = &T->snap[J->exitno]; + SnapEntry *map = &T->snapmap[snap->mapofs]; + MSize n, nent = snap->nent; + BloomFilter seen = 0; + int pass23 = 0; + J->framedepth = 0; + /* Emit IR for slots inherited from parent snapshot. */ + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + BCReg s = snap_slot(sn); + IRRef ref = snap_ref(sn); + IRIns *ir = &T->ir[ref]; + TRef tr; + /* The bloom filter avoids O(nent^2) overhead for de-duping slots. */ + if (bloomtest(seen, ref) && (tr = snap_dedup(J, map, n, ref)) != 0) + goto setslot; + bloomset(seen, ref); + if (irref_isk(ref)) { + tr = snap_replay_const(J, ir); + } else if (!regsp_used(ir->prev)) { + pass23 = 1; + lua_assert(s != 0); + tr = s; + } else { + IRType t = irt_type(ir->t); + uint32_t mode = IRSLOAD_INHERIT|IRSLOAD_PARENT; + if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM)) t = IRT_NUM; + if (ir->o == IR_SLOAD) mode |= (ir->op2 & IRSLOAD_READONLY); + tr = emitir_raw(IRT(IR_SLOAD, t), s, mode); + } + setslot: + J->slot[s] = tr | (sn&(SNAP_CONT|SNAP_FRAME)); /* Same as TREF_* flags. */ + J->framedepth += ((sn & (SNAP_CONT|SNAP_FRAME)) && s); + if ((sn & SNAP_FRAME)) + J->baseslot = s+1; + } + if (pass23) { + IRIns *irlast = &T->ir[snap->ref]; + pass23 = 0; + /* Emit dependent PVALs. */ + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + IRRef refp = snap_ref(sn); + IRIns *ir = &T->ir[refp]; + if (regsp_reg(ir->r) == RID_SUNK) { + if (J->slot[snap_slot(sn)] != snap_slot(sn)) continue; + pass23 = 1; + lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP || + ir->o == IR_CNEW || ir->o == IR_CNEWI); + if (ir->op1 >= T->nk) snap_pref(J, T, map, nent, seen, ir->op1); + if (ir->op2 >= T->nk) snap_pref(J, T, map, nent, seen, ir->op2); + if (LJ_HASFFI && ir->o == IR_CNEWI) { + if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP) + snap_pref(J, T, map, nent, seen, (ir+1)->op2); + } else { + IRIns *irs; + for (irs = ir+1; irs < irlast; irs++) + if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) { + if (snap_pref(J, T, map, nent, seen, irs->op2) == 0) + snap_pref(J, T, map, nent, seen, T->ir[irs->op2].op1); + else if ((LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) && + irs+1 < irlast && (irs+1)->o == IR_HIOP) + snap_pref(J, T, map, nent, seen, (irs+1)->op2); + } + } + } else if (!irref_isk(refp) && !regsp_used(ir->prev)) { + lua_assert(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT); + J->slot[snap_slot(sn)] = snap_pref(J, T, map, nent, seen, ir->op1); + } + } + /* Replay sunk instructions. */ + for (n = 0; pass23 && n < nent; n++) { + SnapEntry sn = map[n]; + IRRef refp = snap_ref(sn); + IRIns *ir = &T->ir[refp]; + if (regsp_reg(ir->r) == RID_SUNK) { + TRef op1, op2; + if (J->slot[snap_slot(sn)] != snap_slot(sn)) { /* De-dup allocs. */ + J->slot[snap_slot(sn)] = J->slot[J->slot[snap_slot(sn)]]; + continue; + } + op1 = ir->op1; + if (op1 >= T->nk) op1 = snap_pref(J, T, map, nent, seen, op1); + op2 = ir->op2; + if (op2 >= T->nk) op2 = snap_pref(J, T, map, nent, seen, op2); + if (LJ_HASFFI && ir->o == IR_CNEWI) { + if (LJ_32 && refp+1 < T->nins && (ir+1)->o == IR_HIOP) { + lj_needsplit(J); /* Emit joining HIOP. */ + op2 = emitir_raw(IRT(IR_HIOP, IRT_I64), op2, + snap_pref(J, T, map, nent, seen, (ir+1)->op2)); + } + J->slot[snap_slot(sn)] = emitir(ir->ot, op1, op2); + } else { + IRIns *irs; + TRef tr = emitir(ir->ot, op1, op2); + J->slot[snap_slot(sn)] = tr; + for (irs = ir+1; irs < irlast; irs++) + if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) { + IRIns *irr = &T->ir[irs->op1]; + TRef val, key = irr->op2, tmp = tr; + if (irr->o != IR_FREF) { + IRIns *irk = &T->ir[key]; + if (irr->o == IR_HREFK) + key = lj_ir_kslot(J, snap_replay_const(J, &T->ir[irk->op1]), + irk->op2); + else + key = snap_replay_const(J, irk); + if (irr->o == IR_HREFK || irr->o == IR_AREF) { + IRIns *irf = &T->ir[irr->op1]; + tmp = emitir(irf->ot, tmp, irf->op2); + } + } + tmp = emitir(irr->ot, tmp, key); + val = snap_pref(J, T, map, nent, seen, irs->op2); + if (val == 0) { + IRIns *irc = &T->ir[irs->op2]; + lua_assert(irc->o == IR_CONV && irc->op2 == IRCONV_NUM_INT); + val = snap_pref(J, T, map, nent, seen, irc->op1); + val = emitir(IRTN(IR_CONV), val, IRCONV_NUM_INT); + } else if ((LJ_SOFTFP || (LJ_32 && LJ_HASFFI)) && + irs+1 < irlast && (irs+1)->o == IR_HIOP) { + IRType t = IRT_I64; + if (LJ_SOFTFP && irt_type((irs+1)->t) == IRT_SOFTFP) + t = IRT_NUM; + lj_needsplit(J); + if (irref_isk(irs->op2) && irref_isk((irs+1)->op2)) { + uint64_t k = (uint32_t)T->ir[irs->op2].i + + ((uint64_t)T->ir[(irs+1)->op2].i << 32); + val = lj_ir_k64(J, t == IRT_I64 ? IR_KINT64 : IR_KNUM, + lj_ir_k64_find(J, k)); + } else { + val = emitir_raw(IRT(IR_HIOP, t), val, + snap_pref(J, T, map, nent, seen, (irs+1)->op2)); + } + tmp = emitir(IRT(irs->o, t), tmp, val); + continue; + } + tmp = emitir(irs->ot, tmp, val); + } + } + } + } + } + J->base = J->slot + J->baseslot; + J->maxslot = snap->nslots - J->baseslot; + lj_snap_add(J); + if (pass23) /* Need explicit GC step _after_ initial snapshot. */ + emitir_raw(IRTG(IR_GCSTEP, IRT_NIL), 0, 0); +} + +/* -- Snapshot restore ---------------------------------------------------- */ + +static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex, + SnapNo snapno, BloomFilter rfilt, + IRIns *ir, TValue *o); + +/* Restore a value from the trace exit state. */ +static void snap_restoreval(jit_State *J, GCtrace *T, ExitState *ex, + SnapNo snapno, BloomFilter rfilt, + IRRef ref, TValue *o) +{ + IRIns *ir = &T->ir[ref]; + IRType1 t = ir->t; + RegSP rs = ir->prev; + if (irref_isk(ref)) { /* Restore constant slot. */ + lj_ir_kvalue(J->L, o, ir); + return; + } + if (LJ_UNLIKELY(bloomtest(rfilt, ref))) + rs = snap_renameref(T, snapno, ref, rs); + if (ra_hasspill(regsp_spill(rs))) { /* Restore from spill slot. */ + int32_t *sps = &ex->spill[regsp_spill(rs)]; + if (irt_isinteger(t)) { + setintV(o, *sps); +#if !LJ_SOFTFP + } else if (irt_isnum(t)) { + o->u64 = *(uint64_t *)sps; +#endif + } else if (LJ_64 && irt_islightud(t)) { + /* 64 bit lightuserdata which may escape already has the tag bits. */ + o->u64 = *(uint64_t *)sps; + } else { + lua_assert(!irt_ispri(t)); /* PRI refs never have a spill slot. */ + setgcrefi(o->gcr, *sps); + setitype(o, irt_toitype(t)); + } + } else { /* Restore from register. */ + Reg r = regsp_reg(rs); + if (ra_noreg(r)) { + lua_assert(ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT); + snap_restoreval(J, T, ex, snapno, rfilt, ir->op1, o); + if (LJ_DUALNUM) setnumV(o, (lua_Number)intV(o)); + return; + } else if (irt_isinteger(t)) { + setintV(o, (int32_t)ex->gpr[r-RID_MIN_GPR]); +#if !LJ_SOFTFP + } else if (irt_isnum(t)) { + setnumV(o, ex->fpr[r-RID_MIN_FPR]); +#endif + } else if (LJ_64 && irt_islightud(t)) { + /* 64 bit lightuserdata which may escape already has the tag bits. */ + o->u64 = ex->gpr[r-RID_MIN_GPR]; + } else { + if (!irt_ispri(t)) + setgcrefi(o->gcr, ex->gpr[r-RID_MIN_GPR]); + setitype(o, irt_toitype(t)); + } + } +} + +#if LJ_HASFFI +/* Restore raw data from the trace exit state. */ +static void snap_restoredata(GCtrace *T, ExitState *ex, + SnapNo snapno, BloomFilter rfilt, + IRRef ref, void *dst, CTSize sz) +{ + IRIns *ir = &T->ir[ref]; + RegSP rs = ir->prev; + int32_t *src; + uint64_t tmp; + if (irref_isk(ref)) { + if (ir->o == IR_KNUM || ir->o == IR_KINT64) { + src = mref(ir->ptr, int32_t); + } else if (sz == 8) { + tmp = (uint64_t)(uint32_t)ir->i; + src = (int32_t *)&tmp; + } else { + src = &ir->i; + } + } else { + if (LJ_UNLIKELY(bloomtest(rfilt, ref))) + rs = snap_renameref(T, snapno, ref, rs); + if (ra_hasspill(regsp_spill(rs))) { + src = &ex->spill[regsp_spill(rs)]; + if (sz == 8 && !irt_is64(ir->t)) { + tmp = (uint64_t)(uint32_t)*src; + src = (int32_t *)&tmp; + } + } else { + Reg r = regsp_reg(rs); + if (ra_noreg(r)) { + /* Note: this assumes CNEWI is never used for SOFTFP split numbers. */ + lua_assert(sz == 8 && ir->o == IR_CONV && ir->op2 == IRCONV_NUM_INT); + snap_restoredata(T, ex, snapno, rfilt, ir->op1, dst, 4); + *(lua_Number *)dst = (lua_Number)*(int32_t *)dst; + return; + } + src = (int32_t *)&ex->gpr[r-RID_MIN_GPR]; +#if !LJ_SOFTFP + if (r >= RID_MAX_GPR) { + src = (int32_t *)&ex->fpr[r-RID_MIN_FPR]; +#if LJ_TARGET_PPC + if (sz == 4) { /* PPC FPRs are always doubles. */ + *(float *)dst = (float)*(double *)src; + return; + } +#else + if (LJ_BE && sz == 4) src++; +#endif + } +#endif + } + } + lua_assert(sz == 1 || sz == 2 || sz == 4 || sz == 8); + if (sz == 4) *(int32_t *)dst = *src; + else if (sz == 8) *(int64_t *)dst = *(int64_t *)src; + else if (sz == 1) *(int8_t *)dst = (int8_t)*src; + else *(int16_t *)dst = (int16_t)*src; +} +#endif + +/* Unsink allocation from the trace exit state. Unsink sunk stores. */ +static void snap_unsink(jit_State *J, GCtrace *T, ExitState *ex, + SnapNo snapno, BloomFilter rfilt, + IRIns *ir, TValue *o) +{ + lua_assert(ir->o == IR_TNEW || ir->o == IR_TDUP || + ir->o == IR_CNEW || ir->o == IR_CNEWI); +#if LJ_HASFFI + if (ir->o == IR_CNEW || ir->o == IR_CNEWI) { + CTState *cts = ctype_ctsG(J2G(J)); + CTypeID id = (CTypeID)T->ir[ir->op1].i; + CTSize sz = lj_ctype_size(cts, id); + GCcdata *cd = lj_cdata_new(cts, id, sz); + setcdataV(J->L, o, cd); + if (ir->o == IR_CNEWI) { + uint8_t *p = (uint8_t *)cdataptr(cd); + lua_assert(sz == 4 || sz == 8); + if (LJ_32 && sz == 8 && ir+1 < T->ir + T->nins && (ir+1)->o == IR_HIOP) { + snap_restoredata(T, ex, snapno, rfilt, (ir+1)->op2, LJ_LE?p+4:p, 4); + if (LJ_BE) p += 4; + sz = 4; + } + snap_restoredata(T, ex, snapno, rfilt, ir->op2, p, sz); + } else { + IRIns *irs, *irlast = &T->ir[T->snap[snapno].ref]; + for (irs = ir+1; irs < irlast; irs++) + if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) { + IRIns *iro = &T->ir[T->ir[irs->op1].op2]; + uint8_t *p = (uint8_t *)cd; + CTSize szs; + lua_assert(irs->o == IR_XSTORE && T->ir[irs->op1].o == IR_ADD); + lua_assert(iro->o == IR_KINT || iro->o == IR_KINT64); + if (irt_is64(irs->t)) szs = 8; + else if (irt_isi8(irs->t) || irt_isu8(irs->t)) szs = 1; + else if (irt_isi16(irs->t) || irt_isu16(irs->t)) szs = 2; + else szs = 4; + if (LJ_64 && iro->o == IR_KINT64) + p += (int64_t)ir_k64(iro)->u64; + else + p += iro->i; + lua_assert(p >= (uint8_t *)cdataptr(cd) && + p + szs <= (uint8_t *)cdataptr(cd) + sz); + if (LJ_32 && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) { + lua_assert(szs == 4); + snap_restoredata(T, ex, snapno, rfilt, (irs+1)->op2, LJ_LE?p+4:p,4); + if (LJ_BE) p += 4; + } + snap_restoredata(T, ex, snapno, rfilt, irs->op2, p, szs); + } + } + } else +#endif + { + IRIns *irs, *irlast; + GCtab *t = ir->o == IR_TNEW ? lj_tab_new(J->L, ir->op1, ir->op2) : + lj_tab_dup(J->L, ir_ktab(&T->ir[ir->op1])); + settabV(J->L, o, t); + irlast = &T->ir[T->snap[snapno].ref]; + for (irs = ir+1; irs < irlast; irs++) + if (irs->r == RID_SINK && snap_sunk_store(J, ir, irs)) { + IRIns *irk = &T->ir[irs->op1]; + TValue tmp, *val; + lua_assert(irs->o == IR_ASTORE || irs->o == IR_HSTORE || + irs->o == IR_FSTORE); + if (irk->o == IR_FREF) { + lua_assert(irk->op2 == IRFL_TAB_META); + snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, &tmp); + /* NOBARRIER: The table is new (marked white). */ + setgcref(t->metatable, obj2gco(tabV(&tmp))); + } else { + irk = &T->ir[irk->op2]; + if (irk->o == IR_KSLOT) irk = &T->ir[irk->op1]; + lj_ir_kvalue(J->L, &tmp, irk); + val = lj_tab_set(J->L, t, &tmp); + /* NOBARRIER: The table is new (marked white). */ + snap_restoreval(J, T, ex, snapno, rfilt, irs->op2, val); + if (LJ_SOFTFP && irs+1 < T->ir + T->nins && (irs+1)->o == IR_HIOP) { + snap_restoreval(J, T, ex, snapno, rfilt, (irs+1)->op2, &tmp); + val->u32.hi = tmp.u32.lo; + } + } + } + } +} + +/* Restore interpreter state from exit state with the help of a snapshot. */ +const BCIns *lj_snap_restore(jit_State *J, void *exptr) +{ + ExitState *ex = (ExitState *)exptr; + SnapNo snapno = J->exitno; /* For now, snapno == exitno. */ + GCtrace *T = traceref(J, J->parent); + SnapShot *snap = &T->snap[snapno]; + MSize n, nent = snap->nent; + SnapEntry *map = &T->snapmap[snap->mapofs]; + SnapEntry *flinks = &T->snapmap[snap_nextofs(T, snap)-1]; + int32_t ftsz0; + TValue *frame; + BloomFilter rfilt = snap_renamefilter(T, snapno); + const BCIns *pc = snap_pc(map[nent]); + lua_State *L = J->L; + + /* Set interpreter PC to the next PC to get correct error messages. */ + setcframe_pc(cframe_raw(L->cframe), pc+1); + + /* Make sure the stack is big enough for the slots from the snapshot. */ + if (LJ_UNLIKELY(L->base + snap->topslot >= tvref(L->maxstack))) { + L->top = curr_topL(L); + lj_state_growstack(L, snap->topslot - curr_proto(L)->framesize); + } + + /* Fill stack slots with data from the registers and spill slots. */ + frame = L->base-1; + ftsz0 = frame_ftsz(frame); /* Preserve link to previous frame in slot #0. */ + for (n = 0; n < nent; n++) { + SnapEntry sn = map[n]; + if (!(sn & SNAP_NORESTORE)) { + TValue *o = &frame[snap_slot(sn)]; + IRRef ref = snap_ref(sn); + IRIns *ir = &T->ir[ref]; + if (ir->r == RID_SUNK) { + MSize j; + for (j = 0; j < n; j++) + if (snap_ref(map[j]) == ref) { /* De-duplicate sunk allocations. */ + copyTV(L, o, &frame[snap_slot(map[j])]); + goto dupslot; + } + snap_unsink(J, T, ex, snapno, rfilt, ir, o); + dupslot: + continue; + } + snap_restoreval(J, T, ex, snapno, rfilt, ref, o); + if (LJ_SOFTFP && (sn & SNAP_SOFTFPNUM) && tvisint(o)) { + TValue tmp; + snap_restoreval(J, T, ex, snapno, rfilt, ref+1, &tmp); + o->u32.hi = tmp.u32.lo; + } else if ((sn & (SNAP_CONT|SNAP_FRAME))) { + /* Overwrite tag with frame link. */ + o->fr.tp.ftsz = snap_slot(sn) != 0 ? (int32_t)*flinks-- : ftsz0; + L->base = o+1; + } + } + } + lua_assert(map + nent == flinks); + + /* Compute current stack top. */ + switch (bc_op(*pc)) { + case BC_CALLM: case BC_CALLMT: case BC_RETM: case BC_TSETM: + L->top = frame + snap->nslots; + break; + default: + L->top = curr_topL(L); + break; + } + return pc; +} + +#undef IR +#undef emitir_raw +#undef emitir + +#endif diff --git a/external/lua/luajit/src/src/lj_snap.h b/external/lua/luajit/src/src/lj_snap.h new file mode 100644 index 0000000000..aff97e5e4d --- /dev/null +++ b/external/lua/luajit/src/src/lj_snap.h @@ -0,0 +1,34 @@ +/* +** Snapshot handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_SNAP_H +#define _LJ_SNAP_H + +#include "lj_obj.h" +#include "lj_jit.h" + +#if LJ_HASJIT +LJ_FUNC void lj_snap_add(jit_State *J); +LJ_FUNC void lj_snap_purge(jit_State *J); +LJ_FUNC void lj_snap_shrink(jit_State *J); +LJ_FUNC IRIns *lj_snap_regspmap(GCtrace *T, SnapNo snapno, IRIns *ir); +LJ_FUNC void lj_snap_replay(jit_State *J, GCtrace *T); +LJ_FUNC const BCIns *lj_snap_restore(jit_State *J, void *exptr); +LJ_FUNC void lj_snap_grow_buf_(jit_State *J, MSize need); +LJ_FUNC void lj_snap_grow_map_(jit_State *J, MSize need); + +static LJ_AINLINE void lj_snap_grow_buf(jit_State *J, MSize need) +{ + if (LJ_UNLIKELY(need > J->sizesnap)) lj_snap_grow_buf_(J, need); +} + +static LJ_AINLINE void lj_snap_grow_map(jit_State *J, MSize need) +{ + if (LJ_UNLIKELY(need > J->sizesnapmap)) lj_snap_grow_map_(J, need); +} + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_state.c b/external/lua/luajit/src/src/lj_state.c new file mode 100644 index 0000000000..8c53d37f1f --- /dev/null +++ b/external/lua/luajit/src/src/lj_state.c @@ -0,0 +1,287 @@ +/* +** State and stack handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lj_state_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_func.h" +#include "lj_meta.h" +#include "lj_state.h" +#include "lj_frame.h" +#if LJ_HASFFI +#include "lj_ctype.h" +#endif +#include "lj_trace.h" +#include "lj_dispatch.h" +#include "lj_vm.h" +#include "lj_lex.h" +#include "lj_alloc.h" + +/* -- Stack handling ------------------------------------------------------ */ + +/* Stack sizes. */ +#define LJ_STACK_MIN LUA_MINSTACK /* Min. stack size. */ +#define LJ_STACK_MAX LUAI_MAXSTACK /* Max. stack size. */ +#define LJ_STACK_START (2*LJ_STACK_MIN) /* Starting stack size. */ +#define LJ_STACK_MAXEX (LJ_STACK_MAX + 1 + LJ_STACK_EXTRA) + +/* Explanation of LJ_STACK_EXTRA: +** +** Calls to metamethods store their arguments beyond the current top +** without checking for the stack limit. This avoids stack resizes which +** would invalidate passed TValue pointers. The stack check is performed +** later by the function header. This can safely resize the stack or raise +** an error. Thus we need some extra slots beyond the current stack limit. +** +** Most metamethods need 4 slots above top (cont, mobj, arg1, arg2) plus +** one extra slot if mobj is not a function. Only lj_meta_tset needs 5 +** slots above top, but then mobj is always a function. So we can get by +** with 5 extra slots. +*/ + +/* Resize stack slots and adjust pointers in state. */ +static void resizestack(lua_State *L, MSize n) +{ + TValue *st, *oldst = tvref(L->stack); + ptrdiff_t delta; + MSize oldsize = L->stacksize; + MSize realsize = n + 1 + LJ_STACK_EXTRA; + GCobj *up; + lua_assert((MSize)(tvref(L->maxstack)-oldst)==L->stacksize-LJ_STACK_EXTRA-1); + st = (TValue *)lj_mem_realloc(L, tvref(L->stack), + (MSize)(L->stacksize*sizeof(TValue)), + (MSize)(realsize*sizeof(TValue))); + setmref(L->stack, st); + delta = (char *)st - (char *)oldst; + setmref(L->maxstack, st + n); + while (oldsize < realsize) /* Clear new slots. */ + setnilV(st + oldsize++); + L->stacksize = realsize; + L->base = (TValue *)((char *)L->base + delta); + L->top = (TValue *)((char *)L->top + delta); + for (up = gcref(L->openupval); up != NULL; up = gcnext(up)) + setmref(gco2uv(up)->v, (TValue *)((char *)uvval(gco2uv(up)) + delta)); + if (obj2gco(L) == gcref(G(L)->jit_L)) + setmref(G(L)->jit_base, mref(G(L)->jit_base, char) + delta); +} + +/* Relimit stack after error, in case the limit was overdrawn. */ +void lj_state_relimitstack(lua_State *L) +{ + if (L->stacksize > LJ_STACK_MAXEX && L->top-tvref(L->stack) < LJ_STACK_MAX-1) + resizestack(L, LJ_STACK_MAX); +} + +/* Try to shrink the stack (called from GC). */ +void lj_state_shrinkstack(lua_State *L, MSize used) +{ + if (L->stacksize > LJ_STACK_MAXEX) + return; /* Avoid stack shrinking while handling stack overflow. */ + if (4*used < L->stacksize && + 2*(LJ_STACK_START+LJ_STACK_EXTRA) < L->stacksize && + obj2gco(L) != gcref(G(L)->jit_L)) /* Don't shrink stack of live trace. */ + resizestack(L, L->stacksize >> 1); +} + +/* Try to grow stack. */ +void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need) +{ + MSize n; + if (L->stacksize > LJ_STACK_MAXEX) /* Overflow while handling overflow? */ + lj_err_throw(L, LUA_ERRERR); + n = L->stacksize + need; + if (n > LJ_STACK_MAX) { + n += 2*LUA_MINSTACK; + } else if (n < 2*L->stacksize) { + n = 2*L->stacksize; + if (n >= LJ_STACK_MAX) + n = LJ_STACK_MAX; + } + resizestack(L, n); + if (L->stacksize > LJ_STACK_MAXEX) + lj_err_msg(L, LJ_ERR_STKOV); +} + +void LJ_FASTCALL lj_state_growstack1(lua_State *L) +{ + lj_state_growstack(L, 1); +} + +/* Allocate basic stack for new state. */ +static void stack_init(lua_State *L1, lua_State *L) +{ + TValue *stend, *st = lj_mem_newvec(L, LJ_STACK_START+LJ_STACK_EXTRA, TValue); + setmref(L1->stack, st); + L1->stacksize = LJ_STACK_START + LJ_STACK_EXTRA; + stend = st + L1->stacksize; + setmref(L1->maxstack, stend - LJ_STACK_EXTRA - 1); + L1->base = L1->top = st+1; + setthreadV(L1, st, L1); /* Needed for curr_funcisL() on empty stack. */ + while (st < stend) /* Clear new slots. */ + setnilV(st++); +} + +/* -- State handling ------------------------------------------------------ */ + +/* Open parts that may cause memory-allocation errors. */ +static TValue *cpluaopen(lua_State *L, lua_CFunction dummy, void *ud) +{ + global_State *g = G(L); + UNUSED(dummy); + UNUSED(ud); + stack_init(L, L); + /* NOBARRIER: State initialization, all objects are white. */ + setgcref(L->env, obj2gco(lj_tab_new(L, 0, LJ_MIN_GLOBAL))); + settabV(L, registry(L), lj_tab_new(L, 0, LJ_MIN_REGISTRY)); + lj_str_resize(L, LJ_MIN_STRTAB-1); + lj_meta_init(L); + lj_lex_init(L); + fixstring(lj_err_str(L, LJ_ERR_ERRMEM)); /* Preallocate memory error msg. */ + g->gc.threshold = 4*g->gc.total; + lj_trace_initstate(g); + return NULL; +} + +static void close_state(lua_State *L) +{ + global_State *g = G(L); + lj_func_closeuv(L, tvref(L->stack)); + lj_gc_freeall(g); + lua_assert(gcref(g->gc.root) == obj2gco(L)); + lua_assert(g->strnum == 0); + lj_trace_freestate(g); +#if LJ_HASFFI + lj_ctype_freestate(g); +#endif + lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef); + lj_str_freebuf(g, &g->tmpbuf); + lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); + lua_assert(g->gc.total == sizeof(GG_State)); +#ifndef LUAJIT_USE_SYSMALLOC + if (g->allocf == lj_alloc_f) + lj_alloc_destroy(g->allocd); + else +#endif + g->allocf(g->allocd, G2GG(g), sizeof(GG_State), 0); +} + +#if LJ_64 +lua_State *lj_state_newstate(lua_Alloc f, void *ud) +#else +LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) +#endif +{ + GG_State *GG = (GG_State *)f(ud, NULL, 0, sizeof(GG_State)); + lua_State *L = &GG->L; + global_State *g = &GG->g; + if (GG == NULL || !checkptr32(GG)) return NULL; + memset(GG, 0, sizeof(GG_State)); + L->gct = ~LJ_TTHREAD; + L->marked = LJ_GC_WHITE0 | LJ_GC_FIXED | LJ_GC_SFIXED; /* Prevent free. */ + L->dummy_ffid = FF_C; + setmref(L->glref, g); + g->gc.currentwhite = LJ_GC_WHITE0 | LJ_GC_FIXED; + g->strempty.marked = LJ_GC_WHITE0; + g->strempty.gct = ~LJ_TSTR; + g->allocf = f; + g->allocd = ud; + setgcref(g->mainthref, obj2gco(L)); + setgcref(g->uvhead.prev, obj2gco(&g->uvhead)); + setgcref(g->uvhead.next, obj2gco(&g->uvhead)); + g->strmask = ~(MSize)0; + setnilV(registry(L)); + setnilV(&g->nilnode.val); + setnilV(&g->nilnode.key); + setmref(g->nilnode.freetop, &g->nilnode); + lj_str_initbuf(&g->tmpbuf); + g->gc.state = GCSpause; + setgcref(g->gc.root, obj2gco(L)); + setmref(g->gc.sweep, &g->gc.root); + g->gc.total = sizeof(GG_State); + g->gc.pause = LUAI_GCPAUSE; + g->gc.stepmul = LUAI_GCMUL; + lj_dispatch_init((GG_State *)L); + L->status = LUA_ERRERR+1; /* Avoid touching the stack upon memory error. */ + if (lj_vm_cpcall(L, NULL, NULL, cpluaopen) != 0) { + /* Memory allocation error: free partial state. */ + close_state(L); + return NULL; + } + L->status = 0; + return L; +} + +static TValue *cpfinalize(lua_State *L, lua_CFunction dummy, void *ud) +{ + UNUSED(dummy); + UNUSED(ud); + lj_gc_finalize_cdata(L); + lj_gc_finalize_udata(L); + /* Frame pop omitted. */ + return NULL; +} + +LUA_API void lua_close(lua_State *L) +{ + global_State *g = G(L); + int i; + L = mainthread(g); /* Only the main thread can be closed. */ + lj_func_closeuv(L, tvref(L->stack)); + lj_gc_separateudata(g, 1); /* Separate udata which have GC metamethods. */ +#if LJ_HASJIT + G2J(g)->flags &= ~JIT_F_ON; + G2J(g)->state = LJ_TRACE_IDLE; + lj_dispatch_update(g); +#endif + for (i = 0;;) { + hook_enter(g); + L->status = 0; + L->cframe = NULL; + L->base = L->top = tvref(L->stack) + 1; + if (lj_vm_cpcall(L, NULL, NULL, cpfinalize) == 0) { + if (++i >= 10) break; + lj_gc_separateudata(g, 1); /* Separate udata again. */ + if (gcref(g->gc.mmudata) == NULL) /* Until nothing is left to do. */ + break; + } + } + close_state(L); +} + +lua_State *lj_state_new(lua_State *L) +{ + lua_State *L1 = lj_mem_newobj(L, lua_State); + L1->gct = ~LJ_TTHREAD; + L1->dummy_ffid = FF_C; + L1->status = 0; + L1->stacksize = 0; + setmref(L1->stack, NULL); + L1->cframe = NULL; + /* NOBARRIER: The lua_State is new (marked white). */ + setgcrefnull(L1->openupval); + setmrefr(L1->glref, L->glref); + setgcrefr(L1->env, L->env); + stack_init(L1, L); /* init stack */ + lua_assert(iswhite(obj2gco(L1))); + return L1; +} + +void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L) +{ + lua_assert(L != mainthread(g)); + lj_func_closeuv(L, tvref(L->stack)); + lua_assert(gcref(L->openupval) == NULL); + lj_mem_freevec(g, tvref(L->stack), L->stacksize, TValue); + lj_mem_freet(g, L); +} + diff --git a/external/lua/luajit/src/src/lj_state.h b/external/lua/luajit/src/src/lj_state.h new file mode 100644 index 0000000000..527f054efa --- /dev/null +++ b/external/lua/luajit/src/src/lj_state.h @@ -0,0 +1,35 @@ +/* +** State and stack handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_STATE_H +#define _LJ_STATE_H + +#include "lj_obj.h" + +#define incr_top(L) \ + (++L->top >= tvref(L->maxstack) && (lj_state_growstack1(L), 0)) + +#define savestack(L, p) ((char *)(p) - mref(L->stack, char)) +#define restorestack(L, n) ((TValue *)(mref(L->stack, char) + (n))) + +LJ_FUNC void lj_state_relimitstack(lua_State *L); +LJ_FUNC void lj_state_shrinkstack(lua_State *L, MSize used); +LJ_FUNCA void LJ_FASTCALL lj_state_growstack(lua_State *L, MSize need); +LJ_FUNC void LJ_FASTCALL lj_state_growstack1(lua_State *L); + +static LJ_AINLINE void lj_state_checkstack(lua_State *L, MSize need) +{ + if ((mref(L->maxstack, char) - (char *)L->top) <= + (ptrdiff_t)need*(ptrdiff_t)sizeof(TValue)) + lj_state_growstack(L, need); +} + +LJ_FUNC lua_State *lj_state_new(lua_State *L); +LJ_FUNC void LJ_FASTCALL lj_state_free(global_State *g, lua_State *L); +#if LJ_64 +LJ_FUNC lua_State *lj_state_newstate(lua_Alloc f, void *ud); +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_str.c b/external/lua/luajit/src/src/lj_str.c new file mode 100644 index 0000000000..6548ee4d38 --- /dev/null +++ b/external/lua/luajit/src/src/lj_str.c @@ -0,0 +1,339 @@ +/* +** String handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#include + +#define lj_str_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_str.h" +#include "lj_state.h" +#include "lj_char.h" + +/* -- String interning ---------------------------------------------------- */ + +/* Ordered compare of strings. Assumes string data is 4-byte aligned. */ +int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b) +{ + MSize i, n = a->len > b->len ? b->len : a->len; + for (i = 0; i < n; i += 4) { + /* Note: innocuous access up to end of string + 3. */ + uint32_t va = *(const uint32_t *)(strdata(a)+i); + uint32_t vb = *(const uint32_t *)(strdata(b)+i); + if (va != vb) { +#if LJ_LE + va = lj_bswap(va); vb = lj_bswap(vb); +#endif + i -= n; + if ((int32_t)i >= -3) { + va >>= 32+(i<<3); vb >>= 32+(i<<3); + if (va == vb) break; + } + return va < vb ? -1 : 1; + } + } + return (int32_t)(a->len - b->len); +} + +/* Fast string data comparison. Caveat: unaligned access to 1st string! */ +static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len) +{ + MSize i = 0; + lua_assert(len > 0); + lua_assert((((uintptr_t)a+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4); + do { /* Note: innocuous access up to end of string + 3. */ + uint32_t v = lj_getu32(a+i) ^ *(const uint32_t *)(b+i); + if (v) { + i -= len; +#if LJ_LE + return (int32_t)i >= -3 ? (v << (32+(i<<3))) : 1; +#else + return (int32_t)i >= -3 ? (v >> (32+(i<<3))) : 1; +#endif + } + i += 4; + } while (i < len); + return 0; +} + +/* Resize the string hash table (grow and shrink). */ +void lj_str_resize(lua_State *L, MSize newmask) +{ + global_State *g = G(L); + GCRef *newhash; + MSize i; + if (g->gc.state == GCSsweepstring || newmask >= LJ_MAX_STRTAB-1) + return; /* No resizing during GC traversal or if already too big. */ + newhash = lj_mem_newvec(L, newmask+1, GCRef); + memset(newhash, 0, (newmask+1)*sizeof(GCRef)); + for (i = g->strmask; i != ~(MSize)0; i--) { /* Rehash old table. */ + GCobj *p = gcref(g->strhash[i]); + while (p) { /* Follow each hash chain and reinsert all strings. */ + MSize h = gco2str(p)->hash & newmask; + GCobj *next = gcnext(p); + /* NOBARRIER: The string table is a GC root. */ + setgcrefr(p->gch.nextgc, newhash[h]); + setgcref(newhash[h], p); + p = next; + } + } + lj_mem_freevec(g, g->strhash, g->strmask+1, GCRef); + g->strmask = newmask; + g->strhash = newhash; +} + +/* Intern a string and return string object. */ +GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx) +{ + global_State *g; + GCstr *s; + GCobj *o; + MSize len = (MSize)lenx; + MSize a, b, h = len; + if (lenx >= LJ_MAX_STR) + lj_err_msg(L, LJ_ERR_STROV); + g = G(L); + /* Compute string hash. Constants taken from lookup3 hash by Bob Jenkins. */ + if (len >= 4) { /* Caveat: unaligned access! */ + a = lj_getu32(str); + h ^= lj_getu32(str+len-4); + b = lj_getu32(str+(len>>1)-2); + h ^= b; h -= lj_rol(b, 14); + b += lj_getu32(str+(len>>2)-1); + } else if (len > 0) { + a = *(const uint8_t *)str; + h ^= *(const uint8_t *)(str+len-1); + b = *(const uint8_t *)(str+(len>>1)); + h ^= b; h -= lj_rol(b, 14); + } else { + return &g->strempty; + } + a ^= h; a -= lj_rol(h, 11); + b ^= a; b -= lj_rol(a, 25); + h ^= b; h -= lj_rol(b, 16); + /* Check if the string has already been interned. */ + o = gcref(g->strhash[h & g->strmask]); + if (LJ_LIKELY((((uintptr_t)str+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4)) { + while (o != NULL) { + GCstr *sx = gco2str(o); + if (sx->len == len && str_fastcmp(str, strdata(sx), len) == 0) { + /* Resurrect if dead. Can only happen with fixstring() (keywords). */ + if (isdead(g, o)) flipwhite(o); + return sx; /* Return existing string. */ + } + o = gcnext(o); + } + } else { /* Slow path: end of string is too close to a page boundary. */ + while (o != NULL) { + GCstr *sx = gco2str(o); + if (sx->len == len && memcmp(str, strdata(sx), len) == 0) { + /* Resurrect if dead. Can only happen with fixstring() (keywords). */ + if (isdead(g, o)) flipwhite(o); + return sx; /* Return existing string. */ + } + o = gcnext(o); + } + } + /* Nope, create a new string. */ + s = lj_mem_newt(L, sizeof(GCstr)+len+1, GCstr); + newwhite(g, s); + s->gct = ~LJ_TSTR; + s->len = len; + s->hash = h; + s->reserved = 0; + memcpy(strdatawr(s), str, len); + strdatawr(s)[len] = '\0'; /* Zero-terminate string. */ + /* Add it to string hash table. */ + h &= g->strmask; + s->nextgc = g->strhash[h]; + /* NOBARRIER: The string table is a GC root. */ + setgcref(g->strhash[h], obj2gco(s)); + if (g->strnum++ > g->strmask) /* Allow a 100% load factor. */ + lj_str_resize(L, (g->strmask<<1)+1); /* Grow string table. */ + return s; /* Return newly interned string. */ +} + +void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s) +{ + g->strnum--; + lj_mem_free(g, s, sizestring(s)); +} + +/* -- Type conversions ---------------------------------------------------- */ + +/* Print number to buffer. Canonicalizes non-finite values. */ +size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o) +{ + if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */ + lua_Number n = o->n; +#if __BIONIC__ + if (tvismzero(o)) { s[0] = '-'; s[1] = '0'; return 2; } +#endif + return (size_t)lua_number2str(s, n); + } else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) { + s[0] = 'n'; s[1] = 'a'; s[2] = 'n'; return 3; + } else if ((o->u32.hi & 0x80000000) == 0) { + s[0] = 'i'; s[1] = 'n'; s[2] = 'f'; return 3; + } else { + s[0] = '-'; s[1] = 'i'; s[2] = 'n'; s[3] = 'f'; return 4; + } +} + +/* Print integer to buffer. Returns pointer to start. */ +char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k) +{ + uint32_t u = (uint32_t)(k < 0 ? -k : k); + p += 1+10; + do { *--p = (char)('0' + u % 10); } while (u /= 10); + if (k < 0) *--p = '-'; + return p; +} + +/* Convert number to string. */ +GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np) +{ + char buf[LJ_STR_NUMBUF]; + size_t len = lj_str_bufnum(buf, (TValue *)np); + return lj_str_new(L, buf, len); +} + +/* Convert integer to string. */ +GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k) +{ + char s[1+10]; + char *p = lj_str_bufint(s, k); + return lj_str_new(L, p, (size_t)(s+sizeof(s)-p)); +} + +GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o) +{ + return tvisint(o) ? lj_str_fromint(L, intV(o)) : lj_str_fromnum(L, &o->n); +} + +/* -- String formatting --------------------------------------------------- */ + +static void addstr(lua_State *L, SBuf *sb, const char *str, MSize len) +{ + char *p; + MSize i; + if (sb->n + len > sb->sz) { + MSize sz = sb->sz * 2; + while (sb->n + len > sz) sz = sz * 2; + lj_str_resizebuf(L, sb, sz); + } + p = sb->buf + sb->n; + sb->n += len; + for (i = 0; i < len; i++) p[i] = str[i]; +} + +static void addchar(lua_State *L, SBuf *sb, int c) +{ + if (sb->n + 1 > sb->sz) { + MSize sz = sb->sz * 2; + lj_str_resizebuf(L, sb, sz); + } + sb->buf[sb->n++] = (char)c; +} + +/* Push formatted message as a string object to Lua stack. va_list variant. */ +const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) +{ + SBuf *sb = &G(L)->tmpbuf; + lj_str_needbuf(L, sb, (MSize)strlen(fmt)); + lj_str_resetbuf(sb); + for (;;) { + const char *e = strchr(fmt, '%'); + if (e == NULL) break; + addstr(L, sb, fmt, (MSize)(e-fmt)); + /* This function only handles %s, %c, %d, %f and %p formats. */ + switch (e[1]) { + case 's': { + const char *s = va_arg(argp, char *); + if (s == NULL) s = "(null)"; + addstr(L, sb, s, (MSize)strlen(s)); + break; + } + case 'c': + addchar(L, sb, va_arg(argp, int)); + break; + case 'd': { + char buf[LJ_STR_INTBUF]; + char *p = lj_str_bufint(buf, va_arg(argp, int32_t)); + addstr(L, sb, p, (MSize)(buf+LJ_STR_INTBUF-p)); + break; + } + case 'f': { + char buf[LJ_STR_NUMBUF]; + TValue tv; + MSize len; + tv.n = (lua_Number)(va_arg(argp, LUAI_UACNUMBER)); + len = (MSize)lj_str_bufnum(buf, &tv); + addstr(L, sb, buf, len); + break; + } + case 'p': { +#define FMTP_CHARS (2*sizeof(ptrdiff_t)) + char buf[2+FMTP_CHARS]; + ptrdiff_t p = (ptrdiff_t)(va_arg(argp, void *)); + ptrdiff_t i, lasti = 2+FMTP_CHARS; + if (p == 0) { + addstr(L, sb, "NULL", 4); + break; + } +#if LJ_64 + /* Shorten output for 64 bit pointers. */ + lasti = 2+2*4+((p >> 32) ? 2+2*(lj_fls((uint32_t)(p >> 32))>>3) : 0); +#endif + buf[0] = '0'; + buf[1] = 'x'; + for (i = lasti-1; i >= 2; i--, p >>= 4) + buf[i] = "0123456789abcdef"[(p & 15)]; + addstr(L, sb, buf, (MSize)lasti); + break; + } + case '%': + addchar(L, sb, '%'); + break; + default: + addchar(L, sb, '%'); + addchar(L, sb, e[1]); + break; + } + fmt = e+2; + } + addstr(L, sb, fmt, (MSize)strlen(fmt)); + setstrV(L, L->top, lj_str_new(L, sb->buf, sb->n)); + incr_top(L); + return strVdata(L->top - 1); +} + +/* Push formatted message as a string object to Lua stack. Vararg variant. */ +const char *lj_str_pushf(lua_State *L, const char *fmt, ...) +{ + const char *msg; + va_list argp; + va_start(argp, fmt); + msg = lj_str_pushvf(L, fmt, argp); + va_end(argp); + return msg; +} + +/* -- Buffer handling ----------------------------------------------------- */ + +char *lj_str_needbuf(lua_State *L, SBuf *sb, MSize sz) +{ + if (sz > sb->sz) { + if (sz < LJ_MIN_SBUF) sz = LJ_MIN_SBUF; + lj_str_resizebuf(L, sb, sz); + } + return sb->buf; +} + diff --git a/external/lua/luajit/src/src/lj_str.h b/external/lua/luajit/src/src/lj_str.h new file mode 100644 index 0000000000..3aa036625f --- /dev/null +++ b/external/lua/luajit/src/src/lj_str.h @@ -0,0 +1,50 @@ +/* +** String handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_STR_H +#define _LJ_STR_H + +#include + +#include "lj_obj.h" + +/* String interning. */ +LJ_FUNC int32_t LJ_FASTCALL lj_str_cmp(GCstr *a, GCstr *b); +LJ_FUNC void lj_str_resize(lua_State *L, MSize newmask); +LJ_FUNCA GCstr *lj_str_new(lua_State *L, const char *str, size_t len); +LJ_FUNC void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s); + +#define lj_str_newz(L, s) (lj_str_new(L, s, strlen(s))) +#define lj_str_newlit(L, s) (lj_str_new(L, "" s, sizeof(s)-1)) + +/* Type conversions. */ +LJ_FUNC size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o); +LJ_FUNC char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k); +LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np); +LJ_FUNC GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k); +LJ_FUNCA GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o); + +#define LJ_STR_INTBUF (1+10) +#define LJ_STR_NUMBUF LUAI_MAXNUMBER2STR + +/* String formatting. */ +LJ_FUNC const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp); +LJ_FUNC const char *lj_str_pushf(lua_State *L, const char *fmt, ...) +#if defined(__GNUC__) + __attribute__ ((format (printf, 2, 3))) +#endif + ; + +/* Resizable string buffers. Struct definition in lj_obj.h. */ +LJ_FUNC char *lj_str_needbuf(lua_State *L, SBuf *sb, MSize sz); + +#define lj_str_initbuf(sb) ((sb)->buf = NULL, (sb)->sz = 0) +#define lj_str_resetbuf(sb) ((sb)->n = 0) +#define lj_str_resizebuf(L, sb, size) \ + ((sb)->buf = (char *)lj_mem_realloc(L, (sb)->buf, (sb)->sz, (size)), \ + (sb)->sz = (size)) +#define lj_str_freebuf(g, sb) lj_mem_free(g, (void *)(sb)->buf, (sb)->sz) + +#endif diff --git a/external/lua/luajit/src/src/lj_strscan.c b/external/lua/luajit/src/src/lj_strscan.c new file mode 100644 index 0000000000..8d41a3e20c --- /dev/null +++ b/external/lua/luajit/src/src/lj_strscan.c @@ -0,0 +1,496 @@ +/* +** String scanning. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include + +#define lj_strscan_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_char.h" +#include "lj_strscan.h" + +/* -- Scanning numbers ---------------------------------------------------- */ + +/* +** Rationale for the builtin string to number conversion library: +** +** It removes a dependency on libc's strtod(), which is a true portability +** nightmare. Mainly due to the plethora of supported OS and toolchain +** combinations. Sadly, the various implementations +** a) are often buggy, incomplete (no hex floats) and/or imprecise, +** b) sometimes crash or hang on certain inputs, +** c) return non-standard NaNs that need to be filtered out, and +** d) fail if the locale-specific decimal separator is not a dot, +** which can only be fixed with atrocious workarounds. +** +** Also, most of the strtod() implementations are hopelessly bloated, +** which is not just an I-cache hog, but a problem for static linkage +** on embedded systems, too. +** +** OTOH the builtin conversion function is very compact. Even though it +** does a lot more, like parsing long longs, octal or imaginary numbers +** and returning the result in different formats: +** a) It needs less than 3 KB (!) of machine code (on x64 with -Os), +** b) it doesn't perform any dynamic allocation and, +** c) it needs only around 600 bytes of stack space. +** +** The builtin function is faster than strtod() for typical inputs, e.g. +** "123", "1.5" or "1e6". Arguably, it's slower for very large exponents, +** which are not very common (this could be fixed, if needed). +** +** And most importantly, the builtin function is equally precise on all +** platforms. It correctly converts and rounds any input to a double. +** If this is not the case, please send a bug report -- but PLEASE verify +** that the implementation you're comparing to is not the culprit! +** +** The implementation quickly pre-scans the entire string first and +** handles simple integers on-the-fly. Otherwise, it dispatches to the +** base-specific parser. Hex and octal is straightforward. +** +** Decimal to binary conversion uses a fixed-length circular buffer in +** base 100. Some simple cases are handled directly. For other cases, the +** number in the buffer is up-scaled or down-scaled until the integer part +** is in the proper range. Then the integer part is rounded and converted +** to a double which is finally rescaled to the result. Denormals need +** special treatment to prevent incorrect 'double rounding'. +*/ + +/* Definitions for circular decimal digit buffer (base 100 = 2 digits/byte). */ +#define STRSCAN_DIG 1024 +#define STRSCAN_MAXDIG 800 /* 772 + extra are sufficient. */ +#define STRSCAN_DDIG (STRSCAN_DIG/2) +#define STRSCAN_DMASK (STRSCAN_DDIG-1) + +/* Helpers for circular buffer. */ +#define DNEXT(a) (((a)+1) & STRSCAN_DMASK) +#define DPREV(a) (((a)-1) & STRSCAN_DMASK) +#define DLEN(lo, hi) ((int32_t)(((lo)-(hi)) & STRSCAN_DMASK)) + +#define casecmp(c, k) (((c) | 0x20) == k) + +/* Final conversion to double. */ +static void strscan_double(uint64_t x, TValue *o, int32_t ex2, int32_t neg) +{ + double n; + + /* Avoid double rounding for denormals. */ + if (LJ_UNLIKELY(ex2 <= -1075 && x != 0)) { + /* NYI: all of this generates way too much code on 32 bit CPUs. */ +#if defined(__GNUC__) && LJ_64 + int32_t b = (int32_t)(__builtin_clzll(x)^63); +#else + int32_t b = (x>>32) ? 32+(int32_t)lj_fls((uint32_t)(x>>32)) : + (int32_t)lj_fls((uint32_t)x); +#endif + if ((int32_t)b + ex2 <= -1023 && (int32_t)b + ex2 >= -1075) { + uint64_t rb = (uint64_t)1 << (-1075-ex2); + if ((x & rb) && ((x & (rb+rb+rb-1)))) x += rb+rb; + x = (x & ~(rb+rb-1)); + } + } + + /* Convert to double using a signed int64_t conversion, then rescale. */ + lua_assert((int64_t)x >= 0); + n = (double)(int64_t)x; + if (neg) n = -n; + if (ex2) n = ldexp(n, ex2); + o->n = n; +} + +/* Parse hexadecimal number. */ +static StrScanFmt strscan_hex(const uint8_t *p, TValue *o, + StrScanFmt fmt, uint32_t opt, + int32_t ex2, int32_t neg, uint32_t dig) +{ + uint64_t x = 0; + uint32_t i; + + /* Scan hex digits. */ + for (i = dig > 16 ? 16 : dig ; i; i--, p++) { + uint32_t d = (*p != '.' ? *p : *++p); if (d > '9') d += 9; + x = (x << 4) + (d & 15); + } + + /* Summarize rounding-effect of excess digits. */ + for (i = 16; i < dig; i++, p++) + x |= ((*p != '.' ? *p : *++p) != '0'), ex2 += 4; + + /* Format-specific handling. */ + switch (fmt) { + case STRSCAN_INT: + if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) { + o->i = neg ? -(int32_t)x : (int32_t)x; + return STRSCAN_INT; /* Fast path for 32 bit integers. */ + } + if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; break; } + /* fallthrough */ + case STRSCAN_U32: + if (dig > 8) return STRSCAN_ERROR; + o->i = neg ? -(int32_t)x : (int32_t)x; + return STRSCAN_U32; + case STRSCAN_I64: + case STRSCAN_U64: + if (dig > 16) return STRSCAN_ERROR; + o->u64 = neg ? (uint64_t)-(int64_t)x : x; + return fmt; + default: + break; + } + + /* Reduce range then convert to double. */ + if ((x & U64x(c0000000,0000000))) { x = (x >> 2) | (x & 3); ex2 += 2; } + strscan_double(x, o, ex2, neg); + return fmt; +} + +/* Parse octal number. */ +static StrScanFmt strscan_oct(const uint8_t *p, TValue *o, + StrScanFmt fmt, int32_t neg, uint32_t dig) +{ + uint64_t x = 0; + + /* Scan octal digits. */ + if (dig > 22 || (dig == 22 && *p > '1')) return STRSCAN_ERROR; + while (dig-- > 0) { + if (!(*p >= '0' && *p <= '7')) return STRSCAN_ERROR; + x = (x << 3) + (*p++ & 7); + } + + /* Format-specific handling. */ + switch (fmt) { + case STRSCAN_INT: + if (x >= 0x80000000u+neg) fmt = STRSCAN_U32; + /* fallthrough */ + case STRSCAN_U32: + if ((x >> 32)) return STRSCAN_ERROR; + o->i = neg ? -(int32_t)x : (int32_t)x; + break; + default: + case STRSCAN_I64: + case STRSCAN_U64: + o->u64 = neg ? (uint64_t)-(int64_t)x : x; + break; + } + return fmt; +} + +/* Parse decimal number. */ +static StrScanFmt strscan_dec(const uint8_t *p, TValue *o, + StrScanFmt fmt, uint32_t opt, + int32_t ex10, int32_t neg, uint32_t dig) +{ + uint8_t xi[STRSCAN_DDIG], *xip = xi; + + if (dig) { + uint32_t i = dig; + if (i > STRSCAN_MAXDIG) { + ex10 -= (int32_t)(i - STRSCAN_MAXDIG); + i = STRSCAN_MAXDIG; + } + /* Scan unaligned leading digit. */ + if (((ex10^i) & 1)) + *xip++ = ((*p != '.' ? *p : *++p) & 15), i--, p++; + /* Scan aligned double-digits. */ + for ( ; i > 1; i -= 2) { + uint32_t d = 10 * ((*p != '.' ? *p : *++p) & 15); p++; + *xip++ = d + ((*p != '.' ? *p : *++p) & 15); p++; + } + /* Scan and realign trailing digit. */ + if (i) *xip++ = 10 * ((*p != '.' ? *p : *++p) & 15), ex10--, p++; + + /* Summarize rounding-effect of excess digits. */ + if (dig > STRSCAN_MAXDIG) { + do { + if ((*p != '.' ? *p : *++p) != '0') { xip[-1] |= 1; break; } + p++; + } while (--dig > STRSCAN_MAXDIG); + dig = STRSCAN_MAXDIG; + } else { /* Simplify exponent. */ + while (ex10 > 0 && dig <= 18) *xip++ = 0, ex10 -= 2, dig += 2; + } + } else { /* Only got zeros. */ + ex10 = 0; + xi[0] = 0; + } + + /* Fast path for numbers in integer format (but handles e.g. 1e6, too). */ + if (dig <= 20 && ex10 == 0) { + uint8_t *xis; + uint64_t x = xi[0]; + double n; + for (xis = xi+1; xis < xip; xis++) x = x * 100 + *xis; + if (!(dig == 20 && (xi[0] > 18 || (int64_t)x >= 0))) { /* No overflow? */ + /* Format-specific handling. */ + switch (fmt) { + case STRSCAN_INT: + if (!(opt & STRSCAN_OPT_TONUM) && x < 0x80000000u+neg) { + o->i = neg ? -(int32_t)x : (int32_t)x; + return STRSCAN_INT; /* Fast path for 32 bit integers. */ + } + if (!(opt & STRSCAN_OPT_C)) { fmt = STRSCAN_NUM; goto plainnumber; } + /* fallthrough */ + case STRSCAN_U32: + if ((x >> 32) != 0) return STRSCAN_ERROR; + o->i = neg ? -(int32_t)x : (int32_t)x; + return STRSCAN_U32; + case STRSCAN_I64: + case STRSCAN_U64: + o->u64 = neg ? (uint64_t)-(int64_t)x : x; + return fmt; + default: + plainnumber: /* Fast path for plain numbers < 2^63. */ + if ((int64_t)x < 0) break; + n = (double)(int64_t)x; + if (neg) n = -n; + o->n = n; + return fmt; + } + } + } + + /* Slow non-integer path. */ + if (fmt == STRSCAN_INT) { + if ((opt & STRSCAN_OPT_C)) return STRSCAN_ERROR; + fmt = STRSCAN_NUM; + } else if (fmt > STRSCAN_INT) { + return STRSCAN_ERROR; + } + { + uint32_t hi = 0, lo = (uint32_t)(xip-xi); + int32_t ex2 = 0, idig = (int32_t)lo + (ex10 >> 1); + + lua_assert(lo > 0 && (ex10 & 1) == 0); + + /* Handle simple overflow/underflow. */ + if (idig > 310/2) { if (neg) setminfV(o); else setpinfV(o); return fmt; } + else if (idig < -326/2) { o->n = neg ? -0.0 : 0.0; return fmt; } + + /* Scale up until we have at least 17 or 18 integer part digits. */ + while (idig < 9 && idig < DLEN(lo, hi)) { + uint32_t i, cy = 0; + ex2 -= 6; + for (i = DPREV(lo); ; i = DPREV(i)) { + uint32_t d = (xi[i] << 6) + cy; + cy = (((d >> 2) * 5243) >> 17); d = d - cy * 100; /* Div/mod 100. */ + xi[i] = (uint8_t)d; + if (i == hi) break; + if (d == 0 && i == DPREV(lo)) lo = i; + } + if (cy) { + if (xi[DPREV(lo)] == 0) lo = DPREV(lo); + else if (hi == lo) { lo = DPREV(lo); xi[DPREV(lo)] |= xi[lo]; } + hi = DPREV(hi); xi[hi] = (uint8_t)cy; idig++; + } + } + + /* Scale down until no more than 17 or 18 integer part digits remain. */ + while (idig > 9) { + uint32_t i, cy = 0; + ex2 += 6; + for (i = hi; i != lo; i = DNEXT(i)) { + cy += xi[i]; + xi[i] = (cy >> 6); + cy = 100 * (cy & 0x3f); + if (xi[i] == 0 && i == hi) hi = DNEXT(hi), idig--; + } + while (cy) { + if (hi == lo) { xi[DPREV(lo)] |= 1; break; } + xi[lo] = (cy >> 6); lo = DNEXT(lo); + cy = 100 * (cy & 0x3f); + } + } + + /* Collect integer part digits and convert to rescaled double. */ + { + uint64_t x = xi[hi]; + uint32_t i; + for (i = DNEXT(hi); --idig > 0 && i != lo; i = DNEXT(i)) + x = x * 100 + xi[i]; + if (i == lo) { + while (--idig >= 0) x = x * 100; + } else { /* Gather round bit from remaining digits. */ + x <<= 1; ex2--; + do { + if (xi[i]) { x |= 1; break; } + i = DNEXT(i); + } while (i != lo); + } + strscan_double(x, o, ex2, neg); + } + } + return fmt; +} + +/* Scan string containing a number. Returns format. Returns value in o. */ +StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt) +{ + int32_t neg = 0; + + /* Remove leading space, parse sign and non-numbers. */ + if (LJ_UNLIKELY(!lj_char_isdigit(*p))) { + while (lj_char_isspace(*p)) p++; + if (*p == '+' || *p == '-') neg = (*p++ == '-'); + if (LJ_UNLIKELY(*p >= 'A')) { /* Parse "inf", "infinity" or "nan". */ + TValue tmp; + setnanV(&tmp); + if (casecmp(p[0],'i') && casecmp(p[1],'n') && casecmp(p[2],'f')) { + if (neg) setminfV(&tmp); else setpinfV(&tmp); + p += 3; + if (casecmp(p[0],'i') && casecmp(p[1],'n') && casecmp(p[2],'i') && + casecmp(p[3],'t') && casecmp(p[4],'y')) p += 5; + } else if (casecmp(p[0],'n') && casecmp(p[1],'a') && casecmp(p[2],'n')) { + p += 3; + } + while (lj_char_isspace(*p)) p++; + if (*p) return STRSCAN_ERROR; + o->u64 = tmp.u64; + return STRSCAN_NUM; + } + } + + /* Parse regular number. */ + { + StrScanFmt fmt = STRSCAN_INT; + int cmask = LJ_CHAR_DIGIT; + int base = (opt & STRSCAN_OPT_C) && *p == '0' ? 0 : 10; + const uint8_t *sp, *dp = NULL; + uint32_t dig = 0, hasdig = 0, x = 0; + int32_t ex = 0; + + /* Determine base and skip leading zeros. */ + if (LJ_UNLIKELY(*p <= '0')) { + if (*p == '0' && casecmp(p[1], 'x')) + base = 16, cmask = LJ_CHAR_XDIGIT, p += 2; + for ( ; ; p++) { + if (*p == '0') { + hasdig = 1; + } else if (*p == '.') { + if (dp) return STRSCAN_ERROR; + dp = p; + } else { + break; + } + } + } + + /* Preliminary digit and decimal point scan. */ + for (sp = p; ; p++) { + if (LJ_LIKELY(lj_char_isa(*p, cmask))) { + x = x * 10 + (*p & 15); /* For fast path below. */ + dig++; + } else if (*p == '.') { + if (dp) return STRSCAN_ERROR; + dp = p; + } else { + break; + } + } + if (!(hasdig | dig)) return STRSCAN_ERROR; + + /* Handle decimal point. */ + if (dp) { + fmt = STRSCAN_NUM; + if (dig) { + ex = (int32_t)(dp-(p-1)); dp = p-1; + while (ex < 0 && *dp-- == '0') ex++, dig--; /* Skip trailing zeros. */ + if (base == 16) ex *= 4; + } + } + + /* Parse exponent. */ + if (casecmp(*p, (uint32_t)(base == 16 ? 'p' : 'e'))) { + uint32_t xx; + int negx = 0; + fmt = STRSCAN_NUM; p++; + if (*p == '+' || *p == '-') negx = (*p++ == '-'); + if (!lj_char_isdigit(*p)) return STRSCAN_ERROR; + xx = (*p++ & 15); + while (lj_char_isdigit(*p)) { + if (xx < 65536) xx = xx * 10 + (*p & 15); + p++; + } + ex += negx ? -(int32_t)xx : (int32_t)xx; + } + + /* Parse suffix. */ + if (*p) { + /* I (IMAG), U (U32), LL (I64), ULL/LLU (U64), L (long), UL/LU (ulong). */ + /* NYI: f (float). Not needed until cp_number() handles non-integers. */ + if (casecmp(*p, 'i')) { + if (!(opt & STRSCAN_OPT_IMAG)) return STRSCAN_ERROR; + p++; fmt = STRSCAN_IMAG; + } else if (fmt == STRSCAN_INT) { + if (casecmp(*p, 'u')) p++, fmt = STRSCAN_U32; + if (casecmp(*p, 'l')) { + p++; + if (casecmp(*p, 'l')) p++, fmt += STRSCAN_I64 - STRSCAN_INT; + else if (!(opt & STRSCAN_OPT_C)) return STRSCAN_ERROR; + else if (sizeof(long) == 8) fmt += STRSCAN_I64 - STRSCAN_INT; + } + if (casecmp(*p, 'u') && (fmt == STRSCAN_INT || fmt == STRSCAN_I64)) + p++, fmt += STRSCAN_U32 - STRSCAN_INT; + if ((fmt == STRSCAN_U32 && !(opt & STRSCAN_OPT_C)) || + (fmt >= STRSCAN_I64 && !(opt & STRSCAN_OPT_LL))) + return STRSCAN_ERROR; + } + while (lj_char_isspace(*p)) p++; + if (*p) return STRSCAN_ERROR; + } + + /* Fast path for decimal 32 bit integers. */ + if (fmt == STRSCAN_INT && base == 10 && + (dig < 10 || (dig == 10 && *sp <= '2' && x < 0x80000000u+neg))) { + int32_t y = neg ? -(int32_t)x : (int32_t)x; + if ((opt & STRSCAN_OPT_TONUM)) { + o->n = (double)y; + return STRSCAN_NUM; + } else { + o->i = y; + return STRSCAN_INT; + } + } + + /* Dispatch to base-specific parser. */ + if (base == 0 && !(fmt == STRSCAN_NUM || fmt == STRSCAN_IMAG)) + return strscan_oct(sp, o, fmt, neg, dig); + if (base == 16) + fmt = strscan_hex(sp, o, fmt, opt, ex, neg, dig); + else + fmt = strscan_dec(sp, o, fmt, opt, ex, neg, dig); + + /* Try to convert number to integer, if requested. */ + if (fmt == STRSCAN_NUM && (opt & STRSCAN_OPT_TOINT)) { + double n = o->n; + int32_t i = lj_num2int(n); + if (n == (lua_Number)i) { o->i = i; return STRSCAN_INT; } + } + return fmt; + } +} + +int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o) +{ + StrScanFmt fmt = lj_strscan_scan((const uint8_t *)strdata(str), o, + STRSCAN_OPT_TONUM); + lua_assert(fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM); + return (fmt != STRSCAN_ERROR); +} + +#if LJ_DUALNUM +int LJ_FASTCALL lj_strscan_number(GCstr *str, TValue *o) +{ + StrScanFmt fmt = lj_strscan_scan((const uint8_t *)strdata(str), o, + STRSCAN_OPT_TOINT); + lua_assert(fmt == STRSCAN_ERROR || fmt == STRSCAN_NUM || fmt == STRSCAN_INT); + if (fmt == STRSCAN_INT) setitype(o, LJ_TISNUM); + return (fmt != STRSCAN_ERROR); +} +#endif + +#undef DNEXT +#undef DPREV +#undef DLEN + diff --git a/external/lua/luajit/src/src/lj_strscan.h b/external/lua/luajit/src/src/lj_strscan.h new file mode 100644 index 0000000000..9557d67382 --- /dev/null +++ b/external/lua/luajit/src/src/lj_strscan.h @@ -0,0 +1,39 @@ +/* +** String scanning. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_STRSCAN_H +#define _LJ_STRSCAN_H + +#include "lj_obj.h" + +/* Options for accepted/returned formats. */ +#define STRSCAN_OPT_TOINT 0x01 /* Convert to int32_t, if possible. */ +#define STRSCAN_OPT_TONUM 0x02 /* Always convert to double. */ +#define STRSCAN_OPT_IMAG 0x04 +#define STRSCAN_OPT_LL 0x08 +#define STRSCAN_OPT_C 0x10 + +/* Returned format. */ +typedef enum { + STRSCAN_ERROR, + STRSCAN_NUM, STRSCAN_IMAG, + STRSCAN_INT, STRSCAN_U32, STRSCAN_I64, STRSCAN_U64, +} StrScanFmt; + +LJ_FUNC StrScanFmt lj_strscan_scan(const uint8_t *p, TValue *o, uint32_t opt); +LJ_FUNC int LJ_FASTCALL lj_strscan_num(GCstr *str, TValue *o); +#if LJ_DUALNUM +LJ_FUNC int LJ_FASTCALL lj_strscan_number(GCstr *str, TValue *o); +#else +#define lj_strscan_number(s, o) lj_strscan_num((s), (o)) +#endif + +/* Check for number or convert string to number/int in-place (!). */ +static LJ_AINLINE int lj_strscan_numberobj(TValue *o) +{ + return tvisnumber(o) || (tvisstr(o) && lj_strscan_number(strV(o), o)); +} + +#endif diff --git a/external/lua/luajit/src/src/lj_tab.c b/external/lua/luajit/src/src/lj_tab.c new file mode 100644 index 0000000000..ccad1f6872 --- /dev/null +++ b/external/lua/luajit/src/src/lj_tab.c @@ -0,0 +1,624 @@ +/* +** Table handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#define lj_tab_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_tab.h" + +/* -- Object hashing ------------------------------------------------------ */ + +/* Hash values are masked with the table hash mask and used as an index. */ +static LJ_AINLINE Node *hashmask(const GCtab *t, uint32_t hash) +{ + Node *n = noderef(t->node); + return &n[hash & t->hmask]; +} + +/* String hashes are precomputed when they are interned. */ +#define hashstr(t, s) hashmask(t, (s)->hash) + +#define hashlohi(t, lo, hi) hashmask((t), hashrot((lo), (hi))) +#define hashnum(t, o) hashlohi((t), (o)->u32.lo, ((o)->u32.hi << 1)) +#define hashptr(t, p) hashlohi((t), u32ptr(p), u32ptr(p) + HASH_BIAS) +#define hashgcref(t, r) hashlohi((t), gcrefu(r), gcrefu(r) + HASH_BIAS) + +/* Hash an arbitrary key and return its anchor position in the hash table. */ +static Node *hashkey(const GCtab *t, cTValue *key) +{ + lua_assert(!tvisint(key)); + if (tvisstr(key)) + return hashstr(t, strV(key)); + else if (tvisnum(key)) + return hashnum(t, key); + else if (tvisbool(key)) + return hashmask(t, boolV(key)); + else + return hashgcref(t, key->gcr); + /* Only hash 32 bits of lightuserdata on a 64 bit CPU. Good enough? */ +} + +/* -- Table creation and destruction -------------------------------------- */ + +/* Create new hash part for table. */ +static LJ_AINLINE void newhpart(lua_State *L, GCtab *t, uint32_t hbits) +{ + uint32_t hsize; + Node *node; + lua_assert(hbits != 0); + if (hbits > LJ_MAX_HBITS) + lj_err_msg(L, LJ_ERR_TABOV); + hsize = 1u << hbits; + node = lj_mem_newvec(L, hsize, Node); + setmref(node->freetop, &node[hsize]); + setmref(t->node, node); + t->hmask = hsize-1; +} + +/* +** Q: Why all of these copies of t->hmask, t->node etc. to local variables? +** A: Because alias analysis for C is _really_ tough. +** Even state-of-the-art C compilers won't produce good code without this. +*/ + +/* Clear hash part of table. */ +static LJ_AINLINE void clearhpart(GCtab *t) +{ + uint32_t i, hmask = t->hmask; + Node *node = noderef(t->node); + lua_assert(t->hmask != 0); + for (i = 0; i <= hmask; i++) { + Node *n = &node[i]; + setmref(n->next, NULL); + setnilV(&n->key); + setnilV(&n->val); + } +} + +/* Clear array part of table. */ +static LJ_AINLINE void clearapart(GCtab *t) +{ + uint32_t i, asize = t->asize; + TValue *array = tvref(t->array); + for (i = 0; i < asize; i++) + setnilV(&array[i]); +} + +/* Create a new table. Note: the slots are not initialized (yet). */ +static GCtab *newtab(lua_State *L, uint32_t asize, uint32_t hbits) +{ + GCtab *t; + /* First try to colocate the array part. */ + if (LJ_MAX_COLOSIZE != 0 && asize > 0 && asize <= LJ_MAX_COLOSIZE) { + lua_assert((sizeof(GCtab) & 7) == 0); + t = (GCtab *)lj_mem_newgco(L, sizetabcolo(asize)); + t->gct = ~LJ_TTAB; + t->nomm = (uint8_t)~0; + t->colo = (int8_t)asize; + setmref(t->array, (TValue *)((char *)t + sizeof(GCtab))); + setgcrefnull(t->metatable); + t->asize = asize; + t->hmask = 0; + setmref(t->node, &G(L)->nilnode); + } else { /* Otherwise separately allocate the array part. */ + t = lj_mem_newobj(L, GCtab); + t->gct = ~LJ_TTAB; + t->nomm = (uint8_t)~0; + t->colo = 0; + setmref(t->array, NULL); + setgcrefnull(t->metatable); + t->asize = 0; /* In case the array allocation fails. */ + t->hmask = 0; + setmref(t->node, &G(L)->nilnode); + if (asize > 0) { + if (asize > LJ_MAX_ASIZE) + lj_err_msg(L, LJ_ERR_TABOV); + setmref(t->array, lj_mem_newvec(L, asize, TValue)); + t->asize = asize; + } + } + if (hbits) + newhpart(L, t, hbits); + return t; +} + +/* Create a new table. +** +** IMPORTANT NOTE: The API differs from lua_createtable()! +** +** The array size is non-inclusive. E.g. asize=128 creates array slots +** for 0..127, but not for 128. If you need slots 1..128, pass asize=129 +** (slot 0 is wasted in this case). +** +** The hash size is given in hash bits. hbits=0 means no hash part. +** hbits=1 creates 2 hash slots, hbits=2 creates 4 hash slots and so on. +*/ +GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits) +{ + GCtab *t = newtab(L, asize, hbits); + clearapart(t); + if (t->hmask > 0) clearhpart(t); + return t; +} + +#if LJ_HASJIT +GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize) +{ + GCtab *t = newtab(L, ahsize & 0xffffff, ahsize >> 24); + clearapart(t); + if (t->hmask > 0) clearhpart(t); + return t; +} +#endif + +/* Duplicate a table. */ +GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt) +{ + GCtab *t; + uint32_t asize, hmask; + t = newtab(L, kt->asize, kt->hmask > 0 ? lj_fls(kt->hmask)+1 : 0); + lua_assert(kt->asize == t->asize && kt->hmask == t->hmask); + t->nomm = 0; /* Keys with metamethod names may be present. */ + asize = kt->asize; + if (asize > 0) { + TValue *array = tvref(t->array); + TValue *karray = tvref(kt->array); + if (asize < 64) { /* An inlined loop beats memcpy for < 512 bytes. */ + uint32_t i; + for (i = 0; i < asize; i++) + copyTV(L, &array[i], &karray[i]); + } else { + memcpy(array, karray, asize*sizeof(TValue)); + } + } + hmask = kt->hmask; + if (hmask > 0) { + uint32_t i; + Node *node = noderef(t->node); + Node *knode = noderef(kt->node); + ptrdiff_t d = (char *)node - (char *)knode; + setmref(node->freetop, (Node *)((char *)noderef(knode->freetop) + d)); + for (i = 0; i <= hmask; i++) { + Node *kn = &knode[i]; + Node *n = &node[i]; + Node *next = nextnode(kn); + /* Don't use copyTV here, since it asserts on a copy of a dead key. */ + n->val = kn->val; n->key = kn->key; + setmref(n->next, next == NULL? next : (Node *)((char *)next + d)); + } + } + return t; +} + +/* Free a table. */ +void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t) +{ + if (t->hmask > 0) + lj_mem_freevec(g, noderef(t->node), t->hmask+1, Node); + if (t->asize > 0 && LJ_MAX_COLOSIZE != 0 && t->colo <= 0) + lj_mem_freevec(g, tvref(t->array), t->asize, TValue); + if (LJ_MAX_COLOSIZE != 0 && t->colo) + lj_mem_free(g, t, sizetabcolo((uint32_t)t->colo & 0x7f)); + else + lj_mem_freet(g, t); +} + +/* -- Table resizing ------------------------------------------------------ */ + +/* Resize a table to fit the new array/hash part sizes. */ +static void resizetab(lua_State *L, GCtab *t, uint32_t asize, uint32_t hbits) +{ + Node *oldnode = noderef(t->node); + uint32_t oldasize = t->asize; + uint32_t oldhmask = t->hmask; + if (asize > oldasize) { /* Array part grows? */ + TValue *array; + uint32_t i; + if (asize > LJ_MAX_ASIZE) + lj_err_msg(L, LJ_ERR_TABOV); + if (LJ_MAX_COLOSIZE != 0 && t->colo > 0) { + /* A colocated array must be separated and copied. */ + TValue *oarray = tvref(t->array); + array = lj_mem_newvec(L, asize, TValue); + t->colo = (int8_t)(t->colo | 0x80); /* Mark as separated (colo < 0). */ + for (i = 0; i < oldasize; i++) + copyTV(L, &array[i], &oarray[i]); + } else { + array = (TValue *)lj_mem_realloc(L, tvref(t->array), + oldasize*sizeof(TValue), asize*sizeof(TValue)); + } + setmref(t->array, array); + t->asize = asize; + for (i = oldasize; i < asize; i++) /* Clear newly allocated slots. */ + setnilV(&array[i]); + } + /* Create new (empty) hash part. */ + if (hbits) { + newhpart(L, t, hbits); + clearhpart(t); + } else { + global_State *g = G(L); + setmref(t->node, &g->nilnode); + t->hmask = 0; + } + if (asize < oldasize) { /* Array part shrinks? */ + TValue *array = tvref(t->array); + uint32_t i; + t->asize = asize; /* Note: This 'shrinks' even colocated arrays. */ + for (i = asize; i < oldasize; i++) /* Reinsert old array values. */ + if (!tvisnil(&array[i])) + copyTV(L, lj_tab_setinth(L, t, (int32_t)i), &array[i]); + /* Physically shrink only separated arrays. */ + if (LJ_MAX_COLOSIZE != 0 && t->colo <= 0) + setmref(t->array, lj_mem_realloc(L, array, + oldasize*sizeof(TValue), asize*sizeof(TValue))); + } + if (oldhmask > 0) { /* Reinsert pairs from old hash part. */ + global_State *g; + uint32_t i; + for (i = 0; i <= oldhmask; i++) { + Node *n = &oldnode[i]; + if (!tvisnil(&n->val)) + copyTV(L, lj_tab_set(L, t, &n->key), &n->val); + } + g = G(L); + lj_mem_freevec(g, oldnode, oldhmask+1, Node); + } +} + +static uint32_t countint(cTValue *key, uint32_t *bins) +{ + lua_assert(!tvisint(key)); + if (tvisnum(key)) { + lua_Number nk = numV(key); + int32_t k = lj_num2int(nk); + if ((uint32_t)k < LJ_MAX_ASIZE && nk == (lua_Number)k) { + bins[(k > 2 ? lj_fls((uint32_t)(k-1)) : 0)]++; + return 1; + } + } + return 0; +} + +static uint32_t countarray(const GCtab *t, uint32_t *bins) +{ + uint32_t na, b, i; + if (t->asize == 0) return 0; + for (na = i = b = 0; b < LJ_MAX_ABITS; b++) { + uint32_t n, top = 2u << b; + TValue *array; + if (top >= t->asize) { + top = t->asize-1; + if (i > top) + break; + } + array = tvref(t->array); + for (n = 0; i <= top; i++) + if (!tvisnil(&array[i])) + n++; + bins[b] += n; + na += n; + } + return na; +} + +static uint32_t counthash(const GCtab *t, uint32_t *bins, uint32_t *narray) +{ + uint32_t total, na, i, hmask = t->hmask; + Node *node = noderef(t->node); + for (total = na = 0, i = 0; i <= hmask; i++) { + Node *n = &node[i]; + if (!tvisnil(&n->val)) { + na += countint(&n->key, bins); + total++; + } + } + *narray += na; + return total; +} + +static uint32_t bestasize(uint32_t bins[], uint32_t *narray) +{ + uint32_t b, sum, na = 0, sz = 0, nn = *narray; + for (b = 0, sum = 0; 2*nn > (1u< 0 && 2*(sum += bins[b]) > (1u<hmask > 0 ? lj_fls(t->hmask)+1 : 0); +} + +/* -- Table getters ------------------------------------------------------- */ + +cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key) +{ + TValue k; + Node *n; + k.n = (lua_Number)key; + n = hashnum(t, &k); + do { + if (tvisnum(&n->key) && n->key.n == k.n) + return &n->val; + } while ((n = nextnode(n))); + return NULL; +} + +cTValue *lj_tab_getstr(GCtab *t, GCstr *key) +{ + Node *n = hashstr(t, key); + do { + if (tvisstr(&n->key) && strV(&n->key) == key) + return &n->val; + } while ((n = nextnode(n))); + return NULL; +} + +cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key) +{ + if (tvisstr(key)) { + cTValue *tv = lj_tab_getstr(t, strV(key)); + if (tv) + return tv; + } else if (tvisint(key)) { + cTValue *tv = lj_tab_getint(t, intV(key)); + if (tv) + return tv; + } else if (tvisnum(key)) { + lua_Number nk = numV(key); + int32_t k = lj_num2int(nk); + if (nk == (lua_Number)k) { + cTValue *tv = lj_tab_getint(t, k); + if (tv) + return tv; + } else { + goto genlookup; /* Else use the generic lookup. */ + } + } else if (!tvisnil(key)) { + Node *n; + genlookup: + n = hashkey(t, key); + do { + if (lj_obj_equal(&n->key, key)) + return &n->val; + } while ((n = nextnode(n))); + } + return niltv(L); +} + +/* -- Table setters ------------------------------------------------------- */ + +/* Insert new key. Use Brent's variation to optimize the chain length. */ +TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key) +{ + Node *n = hashkey(t, key); + if (!tvisnil(&n->val) || t->hmask == 0) { + Node *nodebase = noderef(t->node); + Node *collide, *freenode = noderef(nodebase->freetop); + lua_assert(freenode >= nodebase && freenode <= nodebase+t->hmask+1); + do { + if (freenode == nodebase) { /* No free node found? */ + rehashtab(L, t, key); /* Rehash table. */ + return lj_tab_set(L, t, key); /* Retry key insertion. */ + } + } while (!tvisnil(&(--freenode)->key)); + setmref(nodebase->freetop, freenode); + lua_assert(freenode != &G(L)->nilnode); + collide = hashkey(t, &n->key); + if (collide != n) { /* Colliding node not the main node? */ + while (noderef(collide->next) != n) /* Find predecessor. */ + collide = nextnode(collide); + setmref(collide->next, freenode); /* Relink chain. */ + /* Copy colliding node into free node and free main node. */ + freenode->val = n->val; + freenode->key = n->key; + freenode->next = n->next; + setmref(n->next, NULL); + setnilV(&n->val); + /* Rechain pseudo-resurrected string keys with colliding hashes. */ + while (nextnode(freenode)) { + Node *nn = nextnode(freenode); + if (tvisstr(&nn->key) && !tvisnil(&nn->val) && + hashstr(t, strV(&nn->key)) == n) { + freenode->next = nn->next; + nn->next = n->next; + setmref(n->next, nn); + } else { + freenode = nn; + } + } + } else { /* Otherwise use free node. */ + setmrefr(freenode->next, n->next); /* Insert into chain. */ + setmref(n->next, freenode); + n = freenode; + } + } + n->key.u64 = key->u64; + if (LJ_UNLIKELY(tvismzero(&n->key))) + n->key.u64 = 0; + lj_gc_anybarriert(L, t); + lua_assert(tvisnil(&n->val)); + return &n->val; +} + +TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key) +{ + TValue k; + Node *n; + k.n = (lua_Number)key; + n = hashnum(t, &k); + do { + if (tvisnum(&n->key) && n->key.n == k.n) + return &n->val; + } while ((n = nextnode(n))); + return lj_tab_newkey(L, t, &k); +} + +TValue *lj_tab_setstr(lua_State *L, GCtab *t, GCstr *key) +{ + TValue k; + Node *n = hashstr(t, key); + do { + if (tvisstr(&n->key) && strV(&n->key) == key) + return &n->val; + } while ((n = nextnode(n))); + setstrV(L, &k, key); + return lj_tab_newkey(L, t, &k); +} + +TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key) +{ + Node *n; + t->nomm = 0; /* Invalidate negative metamethod cache. */ + if (tvisstr(key)) { + return lj_tab_setstr(L, t, strV(key)); + } else if (tvisint(key)) { + return lj_tab_setint(L, t, intV(key)); + } else if (tvisnum(key)) { + lua_Number nk = numV(key); + int32_t k = lj_num2int(nk); + if (nk == (lua_Number)k) + return lj_tab_setint(L, t, k); + if (tvisnan(key)) + lj_err_msg(L, LJ_ERR_NANIDX); + /* Else use the generic lookup. */ + } else if (tvisnil(key)) { + lj_err_msg(L, LJ_ERR_NILIDX); + } + n = hashkey(t, key); + do { + if (lj_obj_equal(&n->key, key)) + return &n->val; + } while ((n = nextnode(n))); + return lj_tab_newkey(L, t, key); +} + +/* -- Table traversal ----------------------------------------------------- */ + +/* Get the traversal index of a key. */ +static uint32_t keyindex(lua_State *L, GCtab *t, cTValue *key) +{ + TValue tmp; + if (tvisint(key)) { + int32_t k = intV(key); + if ((uint32_t)k < t->asize) + return (uint32_t)k; /* Array key indexes: [0..t->asize-1] */ + setnumV(&tmp, (lua_Number)k); + key = &tmp; + } else if (tvisnum(key)) { + lua_Number nk = numV(key); + int32_t k = lj_num2int(nk); + if ((uint32_t)k < t->asize && nk == (lua_Number)k) + return (uint32_t)k; /* Array key indexes: [0..t->asize-1] */ + } + if (!tvisnil(key)) { + Node *n = hashkey(t, key); + do { + if (lj_obj_equal(&n->key, key)) + return t->asize + (uint32_t)(n - noderef(t->node)); + /* Hash key indexes: [t->asize..t->asize+t->nmask] */ + } while ((n = nextnode(n))); + if (key->u32.hi == 0xfffe7fff) /* ITERN was despecialized while running. */ + return key->u32.lo - 1; + lj_err_msg(L, LJ_ERR_NEXTIDX); + return 0; /* unreachable */ + } + return ~0u; /* A nil key starts the traversal. */ +} + +/* Advance to the next step in a table traversal. */ +int lj_tab_next(lua_State *L, GCtab *t, TValue *key) +{ + uint32_t i = keyindex(L, t, key); /* Find predecessor key index. */ + for (i++; i < t->asize; i++) /* First traverse the array keys. */ + if (!tvisnil(arrayslot(t, i))) { + setintV(key, i); + copyTV(L, key+1, arrayslot(t, i)); + return 1; + } + for (i -= t->asize; i <= t->hmask; i++) { /* Then traverse the hash keys. */ + Node *n = &noderef(t->node)[i]; + if (!tvisnil(&n->val)) { + copyTV(L, key, &n->key); + copyTV(L, key+1, &n->val); + return 1; + } + } + return 0; /* End of traversal. */ +} + +/* -- Table length calculation -------------------------------------------- */ + +static MSize unbound_search(GCtab *t, MSize j) +{ + cTValue *tv; + MSize i = j; /* i is zero or a present index */ + j++; + /* find `i' and `j' such that i is present and j is not */ + while ((tv = lj_tab_getint(t, (int32_t)j)) && !tvisnil(tv)) { + i = j; + j *= 2; + if (j > (MSize)(INT_MAX-2)) { /* overflow? */ + /* table was built with bad purposes: resort to linear search */ + i = 1; + while ((tv = lj_tab_getint(t, (int32_t)i)) && !tvisnil(tv)) i++; + return i - 1; + } + } + /* now do a binary search between them */ + while (j - i > 1) { + MSize m = (i+j)/2; + cTValue *tvb = lj_tab_getint(t, (int32_t)m); + if (tvb && !tvisnil(tvb)) i = m; else j = m; + } + return i; +} + +/* +** Try to find a boundary in table `t'. A `boundary' is an integer index +** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil). +*/ +MSize LJ_FASTCALL lj_tab_len(GCtab *t) +{ + MSize j = (MSize)t->asize; + if (j > 1 && tvisnil(arrayslot(t, j-1))) { + MSize i = 1; + while (j - i > 1) { + MSize m = (i+j)/2; + if (tvisnil(arrayslot(t, m-1))) j = m; else i = m; + } + return i-1; + } + if (j) j--; + if (t->hmask <= 0) + return j; + return unbound_search(t, j); +} + diff --git a/external/lua/luajit/src/src/lj_tab.h b/external/lua/luajit/src/src/lj_tab.h new file mode 100644 index 0000000000..2787caa0d6 --- /dev/null +++ b/external/lua/luajit/src/src/lj_tab.h @@ -0,0 +1,67 @@ +/* +** Table handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_TAB_H +#define _LJ_TAB_H + +#include "lj_obj.h" + +/* Hash constants. Tuned using a brute force search. */ +#define HASH_BIAS (-0x04c11db7) +#define HASH_ROT1 14 +#define HASH_ROT2 5 +#define HASH_ROT3 13 + +/* Scramble the bits of numbers and pointers. */ +static LJ_AINLINE uint32_t hashrot(uint32_t lo, uint32_t hi) +{ +#if LJ_TARGET_X86ORX64 + /* Prefer variant that compiles well for a 2-operand CPU. */ + lo ^= hi; hi = lj_rol(hi, HASH_ROT1); + lo -= hi; hi = lj_rol(hi, HASH_ROT2); + hi ^= lo; hi -= lj_rol(lo, HASH_ROT3); +#else + lo ^= hi; + lo = lo - lj_rol(hi, HASH_ROT1); + hi = lo ^ lj_rol(hi, HASH_ROT1 + HASH_ROT2); + hi = hi - lj_rol(lo, HASH_ROT3); +#endif + return hi; +} + +#define hsize2hbits(s) ((s) ? ((s)==1 ? 1 : 1+lj_fls((uint32_t)((s)-1))) : 0) + +LJ_FUNCA GCtab *lj_tab_new(lua_State *L, uint32_t asize, uint32_t hbits); +#if LJ_HASJIT +LJ_FUNC GCtab * LJ_FASTCALL lj_tab_new1(lua_State *L, uint32_t ahsize); +#endif +LJ_FUNCA GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt); +LJ_FUNC void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t); +LJ_FUNCA void lj_tab_reasize(lua_State *L, GCtab *t, uint32_t nasize); + +/* Caveat: all getters except lj_tab_get() can return NULL! */ + +LJ_FUNCA cTValue * LJ_FASTCALL lj_tab_getinth(GCtab *t, int32_t key); +LJ_FUNC cTValue *lj_tab_getstr(GCtab *t, GCstr *key); +LJ_FUNCA cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key); + +/* Caveat: all setters require a write barrier for the stored value. */ + +LJ_FUNCA TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key); +LJ_FUNC TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key); +LJ_FUNC TValue *lj_tab_setstr(lua_State *L, GCtab *t, GCstr *key); +LJ_FUNC TValue *lj_tab_set(lua_State *L, GCtab *t, cTValue *key); + +#define inarray(t, key) ((MSize)(key) < (MSize)(t)->asize) +#define arrayslot(t, i) (&tvref((t)->array)[(i)]) +#define lj_tab_getint(t, key) \ + (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_getinth((t), (key))) +#define lj_tab_setint(L, t, key) \ + (inarray((t), (key)) ? arrayslot((t), (key)) : lj_tab_setinth(L, (t), (key))) + +LJ_FUNCA int lj_tab_next(lua_State *L, GCtab *t, TValue *key); +LJ_FUNCA MSize LJ_FASTCALL lj_tab_len(GCtab *t); + +#endif diff --git a/external/lua/luajit/src/src/lj_target.h b/external/lua/luajit/src/src/lj_target.h new file mode 100644 index 0000000000..eed69d1236 --- /dev/null +++ b/external/lua/luajit/src/src/lj_target.h @@ -0,0 +1,162 @@ +/* +** Definitions for target CPU. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_TARGET_H +#define _LJ_TARGET_H + +#include "lj_def.h" +#include "lj_arch.h" + +/* -- Registers and spill slots ------------------------------------------- */ + +/* Register type (uint8_t in ir->r). */ +typedef uint32_t Reg; + +/* The hi-bit is NOT set for an allocated register. This means the value +** can be directly used without masking. The hi-bit is set for a register +** allocation hint or for RID_INIT, RID_SINK or RID_SUNK. +*/ +#define RID_NONE 0x80 +#define RID_MASK 0x7f +#define RID_INIT (RID_NONE|RID_MASK) +#define RID_SINK (RID_INIT-1) +#define RID_SUNK (RID_INIT-2) + +#define ra_noreg(r) ((r) & RID_NONE) +#define ra_hasreg(r) (!((r) & RID_NONE)) + +/* The ra_hashint() macro assumes a previous test for ra_noreg(). */ +#define ra_hashint(r) ((r) < RID_SUNK) +#define ra_gethint(r) ((Reg)((r) & RID_MASK)) +#define ra_sethint(rr, r) rr = (uint8_t)((r)|RID_NONE) +#define ra_samehint(r1, r2) (ra_gethint((r1)^(r2)) == 0) + +/* Spill slot 0 means no spill slot has been allocated. */ +#define SPS_NONE 0 + +#define ra_hasspill(s) ((s) != SPS_NONE) + +/* Combined register and spill slot (uint16_t in ir->prev). */ +typedef uint32_t RegSP; + +#define REGSP(r, s) ((r) + ((s) << 8)) +#define REGSP_HINT(r) ((r)|RID_NONE) +#define REGSP_INIT REGSP(RID_INIT, 0) + +#define regsp_reg(rs) ((rs) & 255) +#define regsp_spill(rs) ((rs) >> 8) +#define regsp_used(rs) \ + (((rs) & ~REGSP(RID_MASK, 0)) != REGSP(RID_NONE, 0)) + +/* -- Register sets ------------------------------------------------------- */ + +/* Bitset for registers. 32 registers suffice for most architectures. +** Note that one set holds bits for both GPRs and FPRs. +*/ +#if LJ_TARGET_PPC || LJ_TARGET_MIPS +typedef uint64_t RegSet; +#else +typedef uint32_t RegSet; +#endif + +#define RID2RSET(r) (((RegSet)1) << (r)) +#define RSET_EMPTY ((RegSet)0) +#define RSET_RANGE(lo, hi) ((RID2RSET((hi)-(lo))-1) << (lo)) + +#define rset_test(rs, r) ((int)((rs) >> (r)) & 1) +#define rset_set(rs, r) (rs |= RID2RSET(r)) +#define rset_clear(rs, r) (rs &= ~RID2RSET(r)) +#define rset_exclude(rs, r) (rs & ~RID2RSET(r)) +#if LJ_TARGET_PPC || LJ_TARGET_MIPS +#define rset_picktop(rs) ((Reg)(__builtin_clzll(rs)^63)) +#define rset_pickbot(rs) ((Reg)__builtin_ctzll(rs)) +#else +#define rset_picktop(rs) ((Reg)lj_fls(rs)) +#define rset_pickbot(rs) ((Reg)lj_ffs(rs)) +#endif + +/* -- Register allocation cost -------------------------------------------- */ + +/* The register allocation heuristic keeps track of the cost for allocating +** a specific register: +** +** A free register (obviously) has a cost of 0 and a 1-bit in the free mask. +** +** An already allocated register has the (non-zero) IR reference in the lowest +** bits and the result of a blended cost-model in the higher bits. +** +** The allocator first checks the free mask for a hit. Otherwise an (unrolled) +** linear search for the minimum cost is used. The search doesn't need to +** keep track of the position of the minimum, which makes it very fast. +** The lowest bits of the minimum cost show the desired IR reference whose +** register is the one to evict. +** +** Without the cost-model this degenerates to the standard heuristics for +** (reverse) linear-scan register allocation. Since code generation is done +** in reverse, a live interval extends from the last use to the first def. +** For an SSA IR the IR reference is the first (and only) def and thus +** trivially marks the end of the interval. The LSRA heuristics says to pick +** the register whose live interval has the furthest extent, i.e. the lowest +** IR reference in our case. +** +** A cost-model should take into account other factors, like spill-cost and +** restore- or rematerialization-cost, which depend on the kind of instruction. +** E.g. constants have zero spill costs, variant instructions have higher +** costs than invariants and PHIs should preferably never be spilled. +** +** Here's a first cut at simple, but effective blended cost-model for R-LSRA: +** - Due to careful design of the IR, constants already have lower IR +** references than invariants and invariants have lower IR references +** than variants. +** - The cost in the upper 16 bits is the sum of the IR reference and a +** weighted score. The score currently only takes into account whether +** the IRT_ISPHI bit is set in the instruction type. +** - The PHI weight is the minimum distance (in IR instructions) a PHI +** reference has to be further apart from a non-PHI reference to be spilled. +** - It should be a power of two (for speed) and must be between 2 and 32768. +** Good values for the PHI weight seem to be between 40 and 150. +** - Further study is required. +*/ +#define REGCOST_PHI_WEIGHT 64 + +/* Cost for allocating a specific register. */ +typedef uint32_t RegCost; + +/* Note: assumes 16 bit IRRef1. */ +#define REGCOST(cost, ref) ((RegCost)(ref) + ((RegCost)(cost) << 16)) +#define regcost_ref(rc) ((IRRef1)(rc)) + +#define REGCOST_T(t) \ + ((RegCost)((t)&IRT_ISPHI) * (((RegCost)(REGCOST_PHI_WEIGHT)<<16)/IRT_ISPHI)) +#define REGCOST_REF_T(ref, t) (REGCOST((ref), (ref)) + REGCOST_T((t))) + +/* -- Target-specific definitions ----------------------------------------- */ + +#if LJ_TARGET_X86ORX64 +#include "lj_target_x86.h" +#elif LJ_TARGET_ARM +#include "lj_target_arm.h" +#elif LJ_TARGET_PPC +#include "lj_target_ppc.h" +#elif LJ_TARGET_MIPS +#include "lj_target_mips.h" +#else +#error "Missing include for target CPU" +#endif + +#ifdef EXITSTUBS_PER_GROUP +/* Return the address of an exit stub. */ +static LJ_AINLINE char *exitstub_addr_(char **group, uint32_t exitno) +{ + lua_assert(group[exitno / EXITSTUBS_PER_GROUP] != NULL); + return (char *)group[exitno / EXITSTUBS_PER_GROUP] + + EXITSTUB_SPACING*(exitno % EXITSTUBS_PER_GROUP); +} +/* Avoid dependence on lj_jit.h if only including lj_target.h. */ +#define exitstub_addr(J, exitno) \ + ((MCode *)exitstub_addr_((char **)((J)->exitstubgroup), (exitno))) +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_target_arm.h b/external/lua/luajit/src/src/lj_target_arm.h new file mode 100644 index 0000000000..bec55772b1 --- /dev/null +++ b/external/lua/luajit/src/src/lj_target_arm.h @@ -0,0 +1,274 @@ +/* +** Definitions for ARM CPUs. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_TARGET_ARM_H +#define _LJ_TARGET_ARM_H + +/* -- Registers IDs ------------------------------------------------------- */ + +#define GPRDEF(_) \ + _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \ + _(R8) _(R9) _(R10) _(R11) _(R12) _(SP) _(LR) _(PC) +#if LJ_SOFTFP +#define FPRDEF(_) +#else +#define FPRDEF(_) \ + _(D0) _(D1) _(D2) _(D3) _(D4) _(D5) _(D6) _(D7) \ + _(D8) _(D9) _(D10) _(D11) _(D12) _(D13) _(D14) _(D15) +#endif +#define VRIDDEF(_) + +#define RIDENUM(name) RID_##name, + +enum { + GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ + FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ + RID_MAX, + RID_TMP = RID_LR, + + /* Calling conventions. */ + RID_RET = RID_R0, + RID_RETLO = RID_R0, + RID_RETHI = RID_R1, +#if LJ_SOFTFP + RID_FPRET = RID_R0, +#else + RID_FPRET = RID_D0, +#endif + + /* These definitions must match with the *.dasc file(s): */ + RID_BASE = RID_R9, /* Interpreter BASE. */ + RID_LPC = RID_R6, /* Interpreter PC. */ + RID_DISPATCH = RID_R7, /* Interpreter DISPATCH table. */ + RID_LREG = RID_R8, /* Interpreter L. */ + + /* Register ranges [min, max) and number of registers. */ + RID_MIN_GPR = RID_R0, + RID_MAX_GPR = RID_PC+1, + RID_MIN_FPR = RID_MAX_GPR, +#if LJ_SOFTFP + RID_MAX_FPR = RID_MIN_FPR, +#else + RID_MAX_FPR = RID_D15+1, +#endif + RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, + RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR +}; + +#define RID_NUM_KREF RID_NUM_GPR +#define RID_MIN_KREF RID_R0 + +/* -- Register sets ------------------------------------------------------- */ + +/* Make use of all registers, except sp, lr and pc. */ +#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_R12+1)) +#define RSET_GPREVEN \ + (RID2RSET(RID_R0)|RID2RSET(RID_R2)|RID2RSET(RID_R4)|RID2RSET(RID_R6)| \ + RID2RSET(RID_R8)|RID2RSET(RID_R10)) +#define RSET_GPRODD \ + (RID2RSET(RID_R1)|RID2RSET(RID_R3)|RID2RSET(RID_R5)|RID2RSET(RID_R7)| \ + RID2RSET(RID_R9)|RID2RSET(RID_R11)) +#if LJ_SOFTFP +#define RSET_FPR 0 +#else +#define RSET_FPR (RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)) +#endif +#define RSET_ALL (RSET_GPR|RSET_FPR) +#define RSET_INIT RSET_ALL + +/* ABI-specific register sets. lr is an implicit scratch register. */ +#define RSET_SCRATCH_GPR_ (RSET_RANGE(RID_R0, RID_R3+1)|RID2RSET(RID_R12)) +#ifdef __APPLE__ +#define RSET_SCRATCH_GPR (RSET_SCRATCH_GPR_|RID2RSET(RID_R9)) +#else +#define RSET_SCRATCH_GPR RSET_SCRATCH_GPR_ +#endif +#if LJ_SOFTFP +#define RSET_SCRATCH_FPR 0 +#else +#define RSET_SCRATCH_FPR (RSET_RANGE(RID_D0, RID_D7+1)) +#endif +#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) +#define REGARG_FIRSTGPR RID_R0 +#define REGARG_LASTGPR RID_R3 +#define REGARG_NUMGPR 4 +#if LJ_ABI_SOFTFP +#define REGARG_FIRSTFPR 0 +#define REGARG_LASTFPR 0 +#define REGARG_NUMFPR 0 +#else +#define REGARG_FIRSTFPR RID_D0 +#define REGARG_LASTFPR RID_D7 +#define REGARG_NUMFPR 8 +#endif + +/* -- Spill slots --------------------------------------------------------- */ + +/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. +** +** SPS_FIXED: Available fixed spill slots in interpreter frame. +** This definition must match with the *.dasc file(s). +** +** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots. +*/ +#define SPS_FIXED 2 +#define SPS_FIRST 2 + +#define SPOFS_TMP 0 + +#define sps_scale(slot) (4 * (int32_t)(slot)) +#define sps_align(slot) (((slot) - SPS_FIXED + 1) & ~1) + +/* -- Exit state ---------------------------------------------------------- */ + +/* This definition must match with the *.dasc file(s). */ +typedef struct { +#if !LJ_SOFTFP + lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ +#endif + int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ + int32_t spill[256]; /* Spill slots. */ +} ExitState; + +/* PC after instruction that caused an exit. Used to find the trace number. */ +#define EXITSTATE_PCREG RID_PC +/* Highest exit + 1 indicates stack check. */ +#define EXITSTATE_CHECKEXIT 1 + +#define EXITSTUB_SPACING 4 +#define EXITSTUBS_PER_GROUP 32 + +/* -- Instructions -------------------------------------------------------- */ + +/* Instruction fields. */ +#define ARMF_CC(ai, cc) (((ai) ^ ARMI_CCAL) | ((cc) << 28)) +#define ARMF_N(r) ((r) << 16) +#define ARMF_D(r) ((r) << 12) +#define ARMF_S(r) ((r) << 8) +#define ARMF_M(r) (r) +#define ARMF_SH(sh, n) (((sh) << 5) | ((n) << 7)) +#define ARMF_RSH(sh, r) (0x10 | ((sh) << 5) | ARMF_S(r)) + +typedef enum ARMIns { + ARMI_CCAL = 0xe0000000, + ARMI_S = 0x000100000, + ARMI_K12 = 0x02000000, + ARMI_KNEG = 0x00200000, + ARMI_LS_W = 0x00200000, + ARMI_LS_U = 0x00800000, + ARMI_LS_P = 0x01000000, + ARMI_LS_R = 0x02000000, + ARMI_LSX_I = 0x00400000, + + ARMI_AND = 0xe0000000, + ARMI_EOR = 0xe0200000, + ARMI_SUB = 0xe0400000, + ARMI_RSB = 0xe0600000, + ARMI_ADD = 0xe0800000, + ARMI_ADC = 0xe0a00000, + ARMI_SBC = 0xe0c00000, + ARMI_RSC = 0xe0e00000, + ARMI_TST = 0xe1100000, + ARMI_TEQ = 0xe1300000, + ARMI_CMP = 0xe1500000, + ARMI_CMN = 0xe1700000, + ARMI_ORR = 0xe1800000, + ARMI_MOV = 0xe1a00000, + ARMI_BIC = 0xe1c00000, + ARMI_MVN = 0xe1e00000, + + ARMI_NOP = 0xe1a00000, + + ARMI_MUL = 0xe0000090, + ARMI_SMULL = 0xe0c00090, + + ARMI_LDR = 0xe4100000, + ARMI_LDRB = 0xe4500000, + ARMI_LDRH = 0xe01000b0, + ARMI_LDRSB = 0xe01000d0, + ARMI_LDRSH = 0xe01000f0, + ARMI_LDRD = 0xe00000d0, + ARMI_STR = 0xe4000000, + ARMI_STRB = 0xe4400000, + ARMI_STRH = 0xe00000b0, + ARMI_STRD = 0xe00000f0, + ARMI_PUSH = 0xe92d0000, + + ARMI_B = 0xea000000, + ARMI_BL = 0xeb000000, + ARMI_BLX = 0xfa000000, + ARMI_BLXr = 0xe12fff30, + + /* ARMv6 */ + ARMI_REV = 0xe6bf0f30, + ARMI_SXTB = 0xe6af0070, + ARMI_SXTH = 0xe6bf0070, + ARMI_UXTB = 0xe6ef0070, + ARMI_UXTH = 0xe6ff0070, + + /* ARMv6T2 */ + ARMI_MOVW = 0xe3000000, + ARMI_MOVT = 0xe3400000, + + /* VFP */ + ARMI_VMOV_D = 0xeeb00b40, + ARMI_VMOV_S = 0xeeb00a40, + ARMI_VMOVI_D = 0xeeb00b00, + + ARMI_VMOV_R_S = 0xee100a10, + ARMI_VMOV_S_R = 0xee000a10, + ARMI_VMOV_RR_D = 0xec500b10, + ARMI_VMOV_D_RR = 0xec400b10, + + ARMI_VADD_D = 0xee300b00, + ARMI_VSUB_D = 0xee300b40, + ARMI_VMUL_D = 0xee200b00, + ARMI_VMLA_D = 0xee000b00, + ARMI_VMLS_D = 0xee000b40, + ARMI_VNMLS_D = 0xee100b00, + ARMI_VDIV_D = 0xee800b00, + + ARMI_VABS_D = 0xeeb00bc0, + ARMI_VNEG_D = 0xeeb10b40, + ARMI_VSQRT_D = 0xeeb10bc0, + + ARMI_VCMP_D = 0xeeb40b40, + ARMI_VCMPZ_D = 0xeeb50b40, + + ARMI_VMRS = 0xeef1fa10, + + ARMI_VCVT_S32_F32 = 0xeebd0ac0, + ARMI_VCVT_S32_F64 = 0xeebd0bc0, + ARMI_VCVT_U32_F32 = 0xeebc0ac0, + ARMI_VCVT_U32_F64 = 0xeebc0bc0, + ARMI_VCVTR_S32_F32 = 0xeebd0a40, + ARMI_VCVTR_S32_F64 = 0xeebd0b40, + ARMI_VCVTR_U32_F32 = 0xeebc0a40, + ARMI_VCVTR_U32_F64 = 0xeebc0b40, + ARMI_VCVT_F32_S32 = 0xeeb80ac0, + ARMI_VCVT_F64_S32 = 0xeeb80bc0, + ARMI_VCVT_F32_U32 = 0xeeb80a40, + ARMI_VCVT_F64_U32 = 0xeeb80b40, + ARMI_VCVT_F32_F64 = 0xeeb70bc0, + ARMI_VCVT_F64_F32 = 0xeeb70ac0, + + ARMI_VLDR_S = 0xed100a00, + ARMI_VLDR_D = 0xed100b00, + ARMI_VSTR_S = 0xed000a00, + ARMI_VSTR_D = 0xed000b00, +} ARMIns; + +typedef enum ARMShift { + ARMSH_LSL, ARMSH_LSR, ARMSH_ASR, ARMSH_ROR +} ARMShift; + +/* ARM condition codes. */ +typedef enum ARMCC { + CC_EQ, CC_NE, CC_CS, CC_CC, CC_MI, CC_PL, CC_VS, CC_VC, + CC_HI, CC_LS, CC_GE, CC_LT, CC_GT, CC_LE, CC_AL, + CC_HS = CC_CS, CC_LO = CC_CC +} ARMCC; + +#endif diff --git a/external/lua/luajit/src/src/lj_target_mips.h b/external/lua/luajit/src/src/lj_target_mips.h new file mode 100644 index 0000000000..36f46c8301 --- /dev/null +++ b/external/lua/luajit/src/src/lj_target_mips.h @@ -0,0 +1,257 @@ +/* +** Definitions for MIPS CPUs. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_TARGET_MIPS_H +#define _LJ_TARGET_MIPS_H + +/* -- Registers IDs ------------------------------------------------------- */ + +#define GPRDEF(_) \ + _(R0) _(R1) _(R2) _(R3) _(R4) _(R5) _(R6) _(R7) \ + _(R8) _(R9) _(R10) _(R11) _(R12) _(R13) _(R14) _(R15) \ + _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \ + _(R24) _(R25) _(SYS1) _(SYS2) _(R28) _(SP) _(R30) _(RA) +#define FPRDEF(_) \ + _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \ + _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \ + _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \ + _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31) +#define VRIDDEF(_) + +#define RIDENUM(name) RID_##name, + +enum { + GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ + FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ + RID_MAX, + RID_ZERO = RID_R0, + RID_TMP = RID_RA, + + /* Calling conventions. */ + RID_RET = RID_R2, +#if LJ_LE + RID_RETHI = RID_R3, + RID_RETLO = RID_R2, +#else + RID_RETHI = RID_R2, + RID_RETLO = RID_R3, +#endif + RID_FPRET = RID_F0, + RID_CFUNCADDR = RID_R25, + + /* These definitions must match with the *.dasc file(s): */ + RID_BASE = RID_R16, /* Interpreter BASE. */ + RID_LPC = RID_R18, /* Interpreter PC. */ + RID_DISPATCH = RID_R19, /* Interpreter DISPATCH table. */ + RID_LREG = RID_R20, /* Interpreter L. */ + RID_JGL = RID_R30, /* On-trace: global_State + 32768. */ + + /* Register ranges [min, max) and number of registers. */ + RID_MIN_GPR = RID_R0, + RID_MAX_GPR = RID_RA+1, + RID_MIN_FPR = RID_F0, + RID_MAX_FPR = RID_F31+1, + RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, + RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR /* Only even regs are used. */ +}; + +#define RID_NUM_KREF RID_NUM_GPR +#define RID_MIN_KREF RID_R0 + +/* -- Register sets ------------------------------------------------------- */ + +/* Make use of all registers, except ZERO, TMP, SP, SYS1, SYS2 and JGL. */ +#define RSET_FIXED \ + (RID2RSET(RID_ZERO)|RID2RSET(RID_TMP)|RID2RSET(RID_SP)|\ + RID2RSET(RID_SYS1)|RID2RSET(RID_SYS2)|RID2RSET(RID_JGL)) +#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED) +#define RSET_FPR \ + (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\ + RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\ + RID2RSET(RID_F16)|RID2RSET(RID_F18)|RID2RSET(RID_F20)|RID2RSET(RID_F22)|\ + RID2RSET(RID_F24)|RID2RSET(RID_F26)|RID2RSET(RID_F28)|RID2RSET(RID_F30)) +#define RSET_ALL (RSET_GPR|RSET_FPR) +#define RSET_INIT RSET_ALL + +#define RSET_SCRATCH_GPR \ + (RSET_RANGE(RID_R1, RID_R15+1)|\ + RID2RSET(RID_R24)|RID2RSET(RID_R25)|RID2RSET(RID_R28)) +#define RSET_SCRATCH_FPR \ + (RID2RSET(RID_F0)|RID2RSET(RID_F2)|RID2RSET(RID_F4)|RID2RSET(RID_F6)|\ + RID2RSET(RID_F8)|RID2RSET(RID_F10)|RID2RSET(RID_F12)|RID2RSET(RID_F14)|\ + RID2RSET(RID_F16)|RID2RSET(RID_F18)) +#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) +#define REGARG_FIRSTGPR RID_R4 +#define REGARG_LASTGPR RID_R7 +#define REGARG_NUMGPR 4 +#define REGARG_FIRSTFPR RID_F12 +#define REGARG_LASTFPR RID_F14 +#define REGARG_NUMFPR 2 + +/* -- Spill slots --------------------------------------------------------- */ + +/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. +** +** SPS_FIXED: Available fixed spill slots in interpreter frame. +** This definition must match with the *.dasc file(s). +** +** SPS_FIRST: First spill slot for general use. +*/ +#define SPS_FIXED 5 +#define SPS_FIRST 4 + +#define SPOFS_TMP 0 + +#define sps_scale(slot) (4 * (int32_t)(slot)) +#define sps_align(slot) (((slot) - SPS_FIXED + 1) & ~1) + +/* -- Exit state ---------------------------------------------------------- */ + +/* This definition must match with the *.dasc file(s). */ +typedef struct { + lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ + int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ + int32_t spill[256]; /* Spill slots. */ +} ExitState; + +/* Highest exit + 1 indicates stack check. */ +#define EXITSTATE_CHECKEXIT 1 + +/* Return the address of a per-trace exit stub. */ +static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p) +{ + while (*p == 0x00000000) p++; /* Skip MIPSI_NOP. */ + return p; +} +/* Avoid dependence on lj_jit.h if only including lj_target.h. */ +#define exitstub_trace_addr(T, exitno) \ + exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode)) + +/* -- Instructions -------------------------------------------------------- */ + +/* Instruction fields. */ +#define MIPSF_S(r) ((r) << 21) +#define MIPSF_T(r) ((r) << 16) +#define MIPSF_D(r) ((r) << 11) +#define MIPSF_R(r) ((r) << 21) +#define MIPSF_H(r) ((r) << 16) +#define MIPSF_G(r) ((r) << 11) +#define MIPSF_F(r) ((r) << 6) +#define MIPSF_A(n) ((n) << 6) +#define MIPSF_M(n) ((n) << 11) + +typedef enum MIPSIns { + /* Integer instructions. */ + MIPSI_MOVE = 0x00000021, + MIPSI_NOP = 0x00000000, + + MIPSI_LI = 0x24000000, + MIPSI_LU = 0x34000000, + MIPSI_LUI = 0x3c000000, + + MIPSI_ADDIU = 0x24000000, + MIPSI_ANDI = 0x30000000, + MIPSI_ORI = 0x34000000, + MIPSI_XORI = 0x38000000, + MIPSI_SLTI = 0x28000000, + MIPSI_SLTIU = 0x2c000000, + + MIPSI_ADDU = 0x00000021, + MIPSI_SUBU = 0x00000023, + MIPSI_MUL = 0x70000002, + MIPSI_AND = 0x00000024, + MIPSI_OR = 0x00000025, + MIPSI_XOR = 0x00000026, + MIPSI_NOR = 0x00000027, + MIPSI_SLT = 0x0000002a, + MIPSI_SLTU = 0x0000002b, + MIPSI_MOVZ = 0x0000000a, + MIPSI_MOVN = 0x0000000b, + + MIPSI_SLL = 0x00000000, + MIPSI_SRL = 0x00000002, + MIPSI_SRA = 0x00000003, + MIPSI_ROTR = 0x00200002, /* MIPS32R2 */ + MIPSI_SLLV = 0x00000004, + MIPSI_SRLV = 0x00000006, + MIPSI_SRAV = 0x00000007, + MIPSI_ROTRV = 0x00000046, /* MIPS32R2 */ + + MIPSI_SEB = 0x7c000420, /* MIPS32R2 */ + MIPSI_SEH = 0x7c000620, /* MIPS32R2 */ + MIPSI_WSBH = 0x7c0000a0, /* MIPS32R2 */ + + MIPSI_B = 0x10000000, + MIPSI_J = 0x08000000, + MIPSI_JAL = 0x0c000000, + MIPSI_JR = 0x00000008, + MIPSI_JALR = 0x0000f809, + + MIPSI_BEQ = 0x10000000, + MIPSI_BNE = 0x14000000, + MIPSI_BLEZ = 0x18000000, + MIPSI_BGTZ = 0x1c000000, + MIPSI_BLTZ = 0x04000000, + MIPSI_BGEZ = 0x04010000, + + /* Load/store instructions. */ + MIPSI_LW = 0x8c000000, + MIPSI_SW = 0xac000000, + MIPSI_LB = 0x80000000, + MIPSI_SB = 0xa0000000, + MIPSI_LH = 0x84000000, + MIPSI_SH = 0xa4000000, + MIPSI_LBU = 0x90000000, + MIPSI_LHU = 0x94000000, + MIPSI_LWC1 = 0xc4000000, + MIPSI_SWC1 = 0xe4000000, + MIPSI_LDC1 = 0xd4000000, + MIPSI_SDC1 = 0xf4000000, + + /* FP instructions. */ + MIPSI_MOV_S = 0x46000006, + MIPSI_MOV_D = 0x46200006, + MIPSI_MOVT_D = 0x46210011, + MIPSI_MOVF_D = 0x46200011, + + MIPSI_ABS_D = 0x46200005, + MIPSI_NEG_D = 0x46200007, + + MIPSI_ADD_D = 0x46200000, + MIPSI_SUB_D = 0x46200001, + MIPSI_MUL_D = 0x46200002, + MIPSI_DIV_D = 0x46200003, + MIPSI_SQRT_D = 0x46200004, + + MIPSI_ADD_S = 0x46000000, + MIPSI_SUB_S = 0x46000001, + + MIPSI_CVT_D_S = 0x46000021, + MIPSI_CVT_W_S = 0x46000024, + MIPSI_CVT_S_D = 0x46200020, + MIPSI_CVT_W_D = 0x46200024, + MIPSI_CVT_S_W = 0x46800020, + MIPSI_CVT_D_W = 0x46800021, + + MIPSI_TRUNC_W_S = 0x4600000d, + MIPSI_TRUNC_W_D = 0x4620000d, + MIPSI_FLOOR_W_S = 0x4600000f, + MIPSI_FLOOR_W_D = 0x4620000f, + + MIPSI_MFC1 = 0x44000000, + MIPSI_MTC1 = 0x44800000, + + MIPSI_BC1F = 0x45000000, + MIPSI_BC1T = 0x45010000, + + MIPSI_C_EQ_D = 0x46200032, + MIPSI_C_OLT_D = 0x46200034, + MIPSI_C_ULT_D = 0x46200035, + MIPSI_C_OLE_D = 0x46200036, + MIPSI_C_ULE_D = 0x46200037, + +} MIPSIns; + +#endif diff --git a/external/lua/luajit/src/src/lj_target_ppc.h b/external/lua/luajit/src/src/lj_target_ppc.h new file mode 100644 index 0000000000..4e95c3a414 --- /dev/null +++ b/external/lua/luajit/src/src/lj_target_ppc.h @@ -0,0 +1,280 @@ +/* +** Definitions for PPC CPUs. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_TARGET_PPC_H +#define _LJ_TARGET_PPC_H + +/* -- Registers IDs ------------------------------------------------------- */ + +#define GPRDEF(_) \ + _(R0) _(SP) _(SYS1) _(R3) _(R4) _(R5) _(R6) _(R7) \ + _(R8) _(R9) _(R10) _(R11) _(R12) _(SYS2) _(R14) _(R15) \ + _(R16) _(R17) _(R18) _(R19) _(R20) _(R21) _(R22) _(R23) \ + _(R24) _(R25) _(R26) _(R27) _(R28) _(R29) _(R30) _(R31) +#define FPRDEF(_) \ + _(F0) _(F1) _(F2) _(F3) _(F4) _(F5) _(F6) _(F7) \ + _(F8) _(F9) _(F10) _(F11) _(F12) _(F13) _(F14) _(F15) \ + _(F16) _(F17) _(F18) _(F19) _(F20) _(F21) _(F22) _(F23) \ + _(F24) _(F25) _(F26) _(F27) _(F28) _(F29) _(F30) _(F31) +#define VRIDDEF(_) + +#define RIDENUM(name) RID_##name, + +enum { + GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ + FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ + RID_MAX, + RID_TMP = RID_R0, + + /* Calling conventions. */ + RID_RET = RID_R3, + RID_RETHI = RID_R3, + RID_RETLO = RID_R4, + RID_FPRET = RID_F1, + + /* These definitions must match with the *.dasc file(s): */ + RID_BASE = RID_R14, /* Interpreter BASE. */ + RID_LPC = RID_R16, /* Interpreter PC. */ + RID_DISPATCH = RID_R17, /* Interpreter DISPATCH table. */ + RID_LREG = RID_R18, /* Interpreter L. */ + RID_JGL = RID_R31, /* On-trace: global_State + 32768. */ + + /* Register ranges [min, max) and number of registers. */ + RID_MIN_GPR = RID_R0, + RID_MAX_GPR = RID_R31+1, + RID_MIN_FPR = RID_F0, + RID_MAX_FPR = RID_F31+1, + RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, + RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR +}; + +#define RID_NUM_KREF RID_NUM_GPR +#define RID_MIN_KREF RID_R0 + +/* -- Register sets ------------------------------------------------------- */ + +/* Make use of all registers, except TMP, SP, SYS1, SYS2 and JGL. */ +#define RSET_FIXED \ + (RID2RSET(RID_TMP)|RID2RSET(RID_SP)|RID2RSET(RID_SYS1)|\ + RID2RSET(RID_SYS2)|RID2RSET(RID_JGL)) +#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR) - RSET_FIXED) +#define RSET_FPR RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR) +#define RSET_ALL (RSET_GPR|RSET_FPR) +#define RSET_INIT RSET_ALL + +#define RSET_SCRATCH_GPR (RSET_RANGE(RID_R3, RID_R12+1)) +#define RSET_SCRATCH_FPR (RSET_RANGE(RID_F0, RID_F13+1)) +#define RSET_SCRATCH (RSET_SCRATCH_GPR|RSET_SCRATCH_FPR) +#define REGARG_FIRSTGPR RID_R3 +#define REGARG_LASTGPR RID_R10 +#define REGARG_NUMGPR 8 +#define REGARG_FIRSTFPR RID_F1 +#define REGARG_LASTFPR RID_F8 +#define REGARG_NUMFPR 8 + +/* -- Spill slots --------------------------------------------------------- */ + +/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. +** +** SPS_FIXED: Available fixed spill slots in interpreter frame. +** This definition must match with the *.dasc file(s). +** +** SPS_FIRST: First spill slot for general use. +** [sp+12] tmplo word \ +** [sp+ 8] tmphi word / tmp dword, parameter area for callee +** [sp+ 4] tmpw, LR of callee +** [sp+ 0] stack chain +*/ +#define SPS_FIXED 7 +#define SPS_FIRST 4 + +/* Stack offsets for temporary slots. Used for FP<->int conversions etc. */ +#define SPOFS_TMPW 4 +#define SPOFS_TMP 8 +#define SPOFS_TMPHI 8 +#define SPOFS_TMPLO 12 + +#define sps_scale(slot) (4 * (int32_t)(slot)) +#define sps_align(slot) (((slot) - SPS_FIXED + 3) & ~3) + +/* -- Exit state ---------------------------------------------------------- */ + +/* This definition must match with the *.dasc file(s). */ +typedef struct { + lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ + int32_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ + int32_t spill[256]; /* Spill slots. */ +} ExitState; + +/* Highest exit + 1 indicates stack check. */ +#define EXITSTATE_CHECKEXIT 1 + +/* Return the address of a per-trace exit stub. */ +static LJ_AINLINE uint32_t *exitstub_trace_addr_(uint32_t *p, uint32_t exitno) +{ + while (*p == 0x60000000) p++; /* Skip PPCI_NOP. */ + return p + 3 + exitno; +} +/* Avoid dependence on lj_jit.h if only including lj_target.h. */ +#define exitstub_trace_addr(T, exitno) \ + exitstub_trace_addr_((MCode *)((char *)(T)->mcode + (T)->szmcode), (exitno)) + +/* -- Instructions -------------------------------------------------------- */ + +/* Instruction fields. */ +#define PPCF_CC(cc) ((((cc) & 3) << 16) | (((cc) & 4) << 22)) +#define PPCF_T(r) ((r) << 21) +#define PPCF_A(r) ((r) << 16) +#define PPCF_B(r) ((r) << 11) +#define PPCF_C(r) ((r) << 6) +#define PPCF_MB(n) ((n) << 6) +#define PPCF_ME(n) ((n) << 1) +#define PPCF_Y 0x00200000 +#define PPCF_DOT 0x00000001 + +typedef enum PPCIns { + /* Integer instructions. */ + PPCI_MR = 0x7c000378, + PPCI_NOP = 0x60000000, + + PPCI_LI = 0x38000000, + PPCI_LIS = 0x3c000000, + + PPCI_ADD = 0x7c000214, + PPCI_ADDC = 0x7c000014, + PPCI_ADDO = 0x7c000614, + PPCI_ADDE = 0x7c000114, + PPCI_ADDZE = 0x7c000194, + PPCI_ADDME = 0x7c0001d4, + PPCI_ADDI = 0x38000000, + PPCI_ADDIS = 0x3c000000, + PPCI_ADDIC = 0x30000000, + PPCI_ADDICDOT = 0x34000000, + + PPCI_SUBF = 0x7c000050, + PPCI_SUBFC = 0x7c000010, + PPCI_SUBFO = 0x7c000450, + PPCI_SUBFE = 0x7c000110, + PPCI_SUBFZE = 0x7c000190, + PPCI_SUBFME = 0x7c0001d0, + PPCI_SUBFIC = 0x20000000, + + PPCI_NEG = 0x7c0000d0, + + PPCI_AND = 0x7c000038, + PPCI_ANDC = 0x7c000078, + PPCI_NAND = 0x7c0003b8, + PPCI_ANDIDOT = 0x70000000, + PPCI_ANDISDOT = 0x74000000, + + PPCI_OR = 0x7c000378, + PPCI_NOR = 0x7c0000f8, + PPCI_ORI = 0x60000000, + PPCI_ORIS = 0x64000000, + + PPCI_XOR = 0x7c000278, + PPCI_EQV = 0x7c000238, + PPCI_XORI = 0x68000000, + PPCI_XORIS = 0x6c000000, + + PPCI_CMPW = 0x7c000000, + PPCI_CMPLW = 0x7c000040, + PPCI_CMPWI = 0x2c000000, + PPCI_CMPLWI = 0x28000000, + + PPCI_MULLW = 0x7c0001d6, + PPCI_MULLI = 0x1c000000, + PPCI_MULLWO = 0x7c0005d6, + + PPCI_EXTSB = 0x7c000774, + PPCI_EXTSH = 0x7c000734, + + PPCI_SLW = 0x7c000030, + PPCI_SRW = 0x7c000430, + PPCI_SRAW = 0x7c000630, + PPCI_SRAWI = 0x7c000670, + + PPCI_RLWNM = 0x5c000000, + PPCI_RLWINM = 0x54000000, + PPCI_RLWIMI = 0x50000000, + + PPCI_B = 0x48000000, + PPCI_BL = 0x48000001, + PPCI_BC = 0x40800000, + PPCI_BCL = 0x40800001, + PPCI_BCTR = 0x4e800420, + PPCI_BCTRL = 0x4e800421, + + PPCI_CRANDC = 0x4c000102, + PPCI_CRXOR = 0x4c000182, + PPCI_CRAND = 0x4c000202, + PPCI_CREQV = 0x4c000242, + PPCI_CRORC = 0x4c000342, + PPCI_CROR = 0x4c000382, + + PPCI_MFLR = 0x7c0802a6, + PPCI_MTCTR = 0x7c0903a6, + + PPCI_MCRXR = 0x7c000400, + + /* Load/store instructions. */ + PPCI_LWZ = 0x80000000, + PPCI_LBZ = 0x88000000, + PPCI_STW = 0x90000000, + PPCI_STB = 0x98000000, + PPCI_LHZ = 0xa0000000, + PPCI_LHA = 0xa8000000, + PPCI_STH = 0xb0000000, + + PPCI_STWU = 0x94000000, + + PPCI_LFS = 0xc0000000, + PPCI_LFD = 0xc8000000, + PPCI_STFS = 0xd0000000, + PPCI_STFD = 0xd8000000, + + PPCI_LWZX = 0x7c00002e, + PPCI_LBZX = 0x7c0000ae, + PPCI_STWX = 0x7c00012e, + PPCI_STBX = 0x7c0001ae, + PPCI_LHZX = 0x7c00022e, + PPCI_LHAX = 0x7c0002ae, + PPCI_STHX = 0x7c00032e, + + PPCI_LWBRX = 0x7c00042c, + PPCI_STWBRX = 0x7c00052c, + + PPCI_LFSX = 0x7c00042e, + PPCI_LFDX = 0x7c0004ae, + PPCI_STFSX = 0x7c00052e, + PPCI_STFDX = 0x7c0005ae, + + /* FP instructions. */ + PPCI_FMR = 0xfc000090, + PPCI_FNEG = 0xfc000050, + PPCI_FABS = 0xfc000210, + + PPCI_FRSP = 0xfc000018, + PPCI_FCTIWZ = 0xfc00001e, + + PPCI_FADD = 0xfc00002a, + PPCI_FSUB = 0xfc000028, + PPCI_FMUL = 0xfc000032, + PPCI_FDIV = 0xfc000024, + PPCI_FSQRT = 0xfc00002c, + + PPCI_FMADD = 0xfc00003a, + PPCI_FMSUB = 0xfc000038, + PPCI_FNMSUB = 0xfc00003c, + + PPCI_FCMPU = 0xfc000000, + PPCI_FSEL = 0xfc00002e, +} PPCIns; + +typedef enum PPCCC { + CC_GE, CC_LE, CC_NE, CC_NS, CC_LT, CC_GT, CC_EQ, CC_SO +} PPCCC; + +#endif diff --git a/external/lua/luajit/src/src/lj_target_x86.h b/external/lua/luajit/src/src/lj_target_x86.h new file mode 100644 index 0000000000..84b0871da0 --- /dev/null +++ b/external/lua/luajit/src/src/lj_target_x86.h @@ -0,0 +1,342 @@ +/* +** Definitions for x86 and x64 CPUs. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_TARGET_X86_H +#define _LJ_TARGET_X86_H + +/* -- Registers IDs ------------------------------------------------------- */ + +#if LJ_64 +#define GPRDEF(_) \ + _(EAX) _(ECX) _(EDX) _(EBX) _(ESP) _(EBP) _(ESI) _(EDI) \ + _(R8D) _(R9D) _(R10D) _(R11D) _(R12D) _(R13D) _(R14D) _(R15D) +#define FPRDEF(_) \ + _(XMM0) _(XMM1) _(XMM2) _(XMM3) _(XMM4) _(XMM5) _(XMM6) _(XMM7) \ + _(XMM8) _(XMM9) _(XMM10) _(XMM11) _(XMM12) _(XMM13) _(XMM14) _(XMM15) +#else +#define GPRDEF(_) \ + _(EAX) _(ECX) _(EDX) _(EBX) _(ESP) _(EBP) _(ESI) _(EDI) +#define FPRDEF(_) \ + _(XMM0) _(XMM1) _(XMM2) _(XMM3) _(XMM4) _(XMM5) _(XMM6) _(XMM7) +#endif +#define VRIDDEF(_) \ + _(MRM) + +#define RIDENUM(name) RID_##name, + +enum { + GPRDEF(RIDENUM) /* General-purpose registers (GPRs). */ + FPRDEF(RIDENUM) /* Floating-point registers (FPRs). */ + RID_MAX, + RID_MRM = RID_MAX, /* Pseudo-id for ModRM operand. */ + + /* Calling conventions. */ + RID_RET = RID_EAX, +#if LJ_64 + RID_FPRET = RID_XMM0, +#else + RID_RETLO = RID_EAX, + RID_RETHI = RID_EDX, +#endif + + /* These definitions must match with the *.dasc file(s): */ + RID_BASE = RID_EDX, /* Interpreter BASE. */ +#if LJ_64 && !LJ_ABI_WIN + RID_LPC = RID_EBX, /* Interpreter PC. */ + RID_DISPATCH = RID_R14D, /* Interpreter DISPATCH table. */ +#else + RID_LPC = RID_ESI, /* Interpreter PC. */ + RID_DISPATCH = RID_EBX, /* Interpreter DISPATCH table. */ +#endif + + /* Register ranges [min, max) and number of registers. */ + RID_MIN_GPR = RID_EAX, + RID_MIN_FPR = RID_XMM0, + RID_MAX_GPR = RID_MIN_FPR, + RID_MAX_FPR = RID_MAX, + RID_NUM_GPR = RID_MAX_GPR - RID_MIN_GPR, + RID_NUM_FPR = RID_MAX_FPR - RID_MIN_FPR, +}; + +/* -- Register sets ------------------------------------------------------- */ + +/* Make use of all registers, except the stack pointer. */ +#define RSET_GPR (RSET_RANGE(RID_MIN_GPR, RID_MAX_GPR)-RID2RSET(RID_ESP)) +#define RSET_FPR (RSET_RANGE(RID_MIN_FPR, RID_MAX_FPR)) +#define RSET_ALL (RSET_GPR|RSET_FPR) +#define RSET_INIT RSET_ALL + +#if LJ_64 +/* Note: this requires the use of FORCE_REX! */ +#define RSET_GPR8 RSET_GPR +#else +#define RSET_GPR8 (RSET_RANGE(RID_EAX, RID_EBX+1)) +#endif + +/* ABI-specific register sets. */ +#define RSET_ACD (RID2RSET(RID_EAX)|RID2RSET(RID_ECX)|RID2RSET(RID_EDX)) +#if LJ_64 +#if LJ_ABI_WIN +/* Windows x64 ABI. */ +#define RSET_SCRATCH \ + (RSET_ACD|RSET_RANGE(RID_R8D, RID_R11D+1)|RSET_RANGE(RID_XMM0, RID_XMM5+1)) +#define REGARG_GPRS \ + (RID_ECX|((RID_EDX|((RID_R8D|(RID_R9D<<5))<<5))<<5)) +#define REGARG_NUMGPR 4 +#define REGARG_NUMFPR 4 +#define REGARG_FIRSTFPR RID_XMM0 +#define REGARG_LASTFPR RID_XMM3 +#define STACKARG_OFS (4*8) +#else +/* The rest of the civilized x64 world has a common ABI. */ +#define RSET_SCRATCH \ + (RSET_ACD|RSET_RANGE(RID_ESI, RID_R11D+1)|RSET_FPR) +#define REGARG_GPRS \ + (RID_EDI|((RID_ESI|((RID_EDX|((RID_ECX|((RID_R8D|(RID_R9D \ + <<5))<<5))<<5))<<5))<<5)) +#define REGARG_NUMGPR 6 +#define REGARG_NUMFPR 8 +#define REGARG_FIRSTFPR RID_XMM0 +#define REGARG_LASTFPR RID_XMM7 +#define STACKARG_OFS 0 +#endif +#else +/* Common x86 ABI. */ +#define RSET_SCRATCH (RSET_ACD|RSET_FPR) +#define REGARG_GPRS (RID_ECX|(RID_EDX<<5)) /* Fastcall only. */ +#define REGARG_NUMGPR 2 /* Fastcall only. */ +#define REGARG_NUMFPR 0 +#define STACKARG_OFS 0 +#endif + +#if LJ_64 +/* Prefer the low 8 regs of each type to reduce REX prefixes. */ +#undef rset_picktop +#define rset_picktop(rs) (lj_fls(lj_bswap(rs)) ^ 0x18) +#endif + +/* -- Spill slots --------------------------------------------------------- */ + +/* Spill slots are 32 bit wide. An even/odd pair is used for FPRs. +** +** SPS_FIXED: Available fixed spill slots in interpreter frame. +** This definition must match with the *.dasc file(s). +** +** SPS_FIRST: First spill slot for general use. Reserve min. two 32 bit slots. +*/ +#if LJ_64 +#if LJ_ABI_WIN +#define SPS_FIXED (4*2) +#define SPS_FIRST (4*2) /* Don't use callee register save area. */ +#else +#define SPS_FIXED 4 +#define SPS_FIRST 2 +#endif +#else +#define SPS_FIXED 6 +#define SPS_FIRST 2 +#endif + +#define SPOFS_TMP 0 + +#define sps_scale(slot) (4 * (int32_t)(slot)) +#define sps_align(slot) (((slot) - SPS_FIXED + 3) & ~3) + +/* -- Exit state ---------------------------------------------------------- */ + +/* This definition must match with the *.dasc file(s). */ +typedef struct { + lua_Number fpr[RID_NUM_FPR]; /* Floating-point registers. */ + intptr_t gpr[RID_NUM_GPR]; /* General-purpose registers. */ + int32_t spill[256]; /* Spill slots. */ +} ExitState; + +/* Limited by the range of a short fwd jump (127): (2+2)*(32-1)-2 = 122. */ +#define EXITSTUB_SPACING (2+2) +#define EXITSTUBS_PER_GROUP 32 + +/* -- x86 ModRM operand encoding ------------------------------------------ */ + +typedef enum { + XM_OFS0 = 0x00, XM_OFS8 = 0x40, XM_OFS32 = 0x80, XM_REG = 0xc0, + XM_SCALE1 = 0x00, XM_SCALE2 = 0x40, XM_SCALE4 = 0x80, XM_SCALE8 = 0xc0, + XM_MASK = 0xc0 +} x86Mode; + +/* Structure to hold variable ModRM operand. */ +typedef struct { + int32_t ofs; /* Offset. */ + uint8_t base; /* Base register or RID_NONE. */ + uint8_t idx; /* Index register or RID_NONE. */ + uint8_t scale; /* Index scale (XM_SCALE1 .. XM_SCALE8). */ +} x86ModRM; + +/* -- Opcodes ------------------------------------------------------------- */ + +/* Macros to construct variable-length x86 opcodes. -(len+1) is in LSB. */ +#define XO_(o) ((uint32_t)(0x0000fe + (0x##o<<24))) +#define XO_FPU(a,b) ((uint32_t)(0x00fd + (0x##a<<16)+(0x##b<<24))) +#define XO_0f(o) ((uint32_t)(0x0f00fd + (0x##o<<24))) +#define XO_66(o) ((uint32_t)(0x6600fd + (0x##o<<24))) +#define XO_660f(o) ((uint32_t)(0x0f66fc + (0x##o<<24))) +#define XO_f20f(o) ((uint32_t)(0x0ff2fc + (0x##o<<24))) +#define XO_f30f(o) ((uint32_t)(0x0ff3fc + (0x##o<<24))) + +/* This list of x86 opcodes is not intended to be complete. Opcodes are only +** included when needed. Take a look at DynASM or jit.dis_x86 to see the +** whole mess. +*/ +typedef enum { + /* Fixed length opcodes. XI_* prefix. */ + XI_NOP = 0x90, + XI_XCHGa = 0x90, + XI_CALL = 0xe8, + XI_JMP = 0xe9, + XI_JMPs = 0xeb, + XI_PUSH = 0x50, /* Really 50+r. */ + XI_JCCs = 0x70, /* Really 7x. */ + XI_JCCn = 0x80, /* Really 0f8x. */ + XI_LEA = 0x8d, + XI_MOVrib = 0xb0, /* Really b0+r. */ + XI_MOVri = 0xb8, /* Really b8+r. */ + XI_ARITHib = 0x80, + XI_ARITHi = 0x81, + XI_ARITHi8 = 0x83, + XI_PUSHi8 = 0x6a, + XI_TESTb = 0x84, + XI_TEST = 0x85, + XI_MOVmi = 0xc7, + XI_GROUP5 = 0xff, + + /* Note: little-endian byte-order! */ + XI_FLDZ = 0xeed9, + XI_FLD1 = 0xe8d9, + XI_FLDLG2 = 0xecd9, + XI_FLDLN2 = 0xedd9, + XI_FDUP = 0xc0d9, /* Really fld st0. */ + XI_FPOP = 0xd8dd, /* Really fstp st0. */ + XI_FPOP1 = 0xd9dd, /* Really fstp st1. */ + XI_FRNDINT = 0xfcd9, + XI_FSIN = 0xfed9, + XI_FCOS = 0xffd9, + XI_FPTAN = 0xf2d9, + XI_FPATAN = 0xf3d9, + XI_FSCALE = 0xfdd9, + XI_FYL2X = 0xf1d9, + + /* Variable-length opcodes. XO_* prefix. */ + XO_MOV = XO_(8b), + XO_MOVto = XO_(89), + XO_MOVtow = XO_66(89), + XO_MOVtob = XO_(88), + XO_MOVmi = XO_(c7), + XO_MOVmib = XO_(c6), + XO_LEA = XO_(8d), + XO_ARITHib = XO_(80), + XO_ARITHi = XO_(81), + XO_ARITHi8 = XO_(83), + XO_ARITHiw8 = XO_66(83), + XO_SHIFTi = XO_(c1), + XO_SHIFT1 = XO_(d1), + XO_SHIFTcl = XO_(d3), + XO_IMUL = XO_0f(af), + XO_IMULi = XO_(69), + XO_IMULi8 = XO_(6b), + XO_CMP = XO_(3b), + XO_TESTb = XO_(84), + XO_TEST = XO_(85), + XO_GROUP3b = XO_(f6), + XO_GROUP3 = XO_(f7), + XO_GROUP5b = XO_(fe), + XO_GROUP5 = XO_(ff), + XO_MOVZXb = XO_0f(b6), + XO_MOVZXw = XO_0f(b7), + XO_MOVSXb = XO_0f(be), + XO_MOVSXw = XO_0f(bf), + XO_MOVSXd = XO_(63), + XO_BSWAP = XO_0f(c8), + XO_CMOV = XO_0f(40), + + XO_MOVSD = XO_f20f(10), + XO_MOVSDto = XO_f20f(11), + XO_MOVSS = XO_f30f(10), + XO_MOVSSto = XO_f30f(11), + XO_MOVLPD = XO_660f(12), + XO_MOVAPS = XO_0f(28), + XO_XORPS = XO_0f(57), + XO_ANDPS = XO_0f(54), + XO_ADDSD = XO_f20f(58), + XO_SUBSD = XO_f20f(5c), + XO_MULSD = XO_f20f(59), + XO_DIVSD = XO_f20f(5e), + XO_SQRTSD = XO_f20f(51), + XO_MINSD = XO_f20f(5d), + XO_MAXSD = XO_f20f(5f), + XO_ROUNDSD = 0x0b3a0ffc, /* Really 66 0f 3a 0b. See asm_fpmath. */ + XO_UCOMISD = XO_660f(2e), + XO_CVTSI2SD = XO_f20f(2a), + XO_CVTSD2SI = XO_f20f(2d), + XO_CVTTSD2SI= XO_f20f(2c), + XO_CVTSI2SS = XO_f30f(2a), + XO_CVTSS2SI = XO_f30f(2d), + XO_CVTTSS2SI= XO_f30f(2c), + XO_CVTSS2SD = XO_f30f(5a), + XO_CVTSD2SS = XO_f20f(5a), + XO_ADDSS = XO_f30f(58), + XO_MOVD = XO_660f(6e), + XO_MOVDto = XO_660f(7e), + + XO_FLDd = XO_(d9), XOg_FLDd = 0, + XO_FLDq = XO_(dd), XOg_FLDq = 0, + XO_FILDd = XO_(db), XOg_FILDd = 0, + XO_FILDq = XO_(df), XOg_FILDq = 5, + XO_FSTPd = XO_(d9), XOg_FSTPd = 3, + XO_FSTPq = XO_(dd), XOg_FSTPq = 3, + XO_FISTPq = XO_(df), XOg_FISTPq = 7, + XO_FISTTPq = XO_(dd), XOg_FISTTPq = 1, + XO_FADDq = XO_(dc), XOg_FADDq = 0, + XO_FLDCW = XO_(d9), XOg_FLDCW = 5, + XO_FNSTCW = XO_(d9), XOg_FNSTCW = 7 +} x86Op; + +/* x86 opcode groups. */ +typedef uint32_t x86Group; + +#define XG_(i8, i, g) ((x86Group)(((i8) << 16) + ((i) << 8) + (g))) +#define XG_ARITHi(g) XG_(XI_ARITHi8, XI_ARITHi, g) +#define XG_TOXOi(xg) ((x86Op)(0x000000fe + (((xg)<<16) & 0xff000000))) +#define XG_TOXOi8(xg) ((x86Op)(0x000000fe + (((xg)<<8) & 0xff000000))) + +#define XO_ARITH(a) ((x86Op)(0x030000fe + ((a)<<27))) +#define XO_ARITHw(a) ((x86Op)(0x036600fd + ((a)<<27))) + +typedef enum { + XOg_ADD, XOg_OR, XOg_ADC, XOg_SBB, XOg_AND, XOg_SUB, XOg_XOR, XOg_CMP, + XOg_X_IMUL +} x86Arith; + +typedef enum { + XOg_ROL, XOg_ROR, XOg_RCL, XOg_RCR, XOg_SHL, XOg_SHR, XOg_SAL, XOg_SAR +} x86Shift; + +typedef enum { + XOg_TEST, XOg_TEST_, XOg_NOT, XOg_NEG, XOg_MUL, XOg_IMUL, XOg_DIV, XOg_IDIV +} x86Group3; + +typedef enum { + XOg_INC, XOg_DEC, XOg_CALL, XOg_CALLfar, XOg_JMP, XOg_JMPfar, XOg_PUSH +} x86Group5; + +/* x86 condition codes. */ +typedef enum { + CC_O, CC_NO, CC_B, CC_NB, CC_E, CC_NE, CC_BE, CC_NBE, + CC_S, CC_NS, CC_P, CC_NP, CC_L, CC_NL, CC_LE, CC_NLE, + CC_C = CC_B, CC_NAE = CC_C, CC_NC = CC_NB, CC_AE = CC_NB, + CC_Z = CC_E, CC_NZ = CC_NE, CC_NA = CC_BE, CC_A = CC_NBE, + CC_PE = CC_P, CC_PO = CC_NP, CC_NGE = CC_L, CC_GE = CC_NL, + CC_NG = CC_LE, CC_G = CC_NLE +} x86CC; + +#endif diff --git a/external/lua/luajit/src/src/lj_trace.c b/external/lua/luajit/src/src/lj_trace.c new file mode 100644 index 0000000000..c70fc247ec --- /dev/null +++ b/external/lua/luajit/src/src/lj_trace.c @@ -0,0 +1,815 @@ +/* +** Trace management. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_trace_c +#define LUA_CORE + +#include "lj_obj.h" + +#if LJ_HASJIT + +#include "lj_gc.h" +#include "lj_err.h" +#include "lj_debug.h" +#include "lj_str.h" +#include "lj_frame.h" +#include "lj_state.h" +#include "lj_bc.h" +#include "lj_ir.h" +#include "lj_jit.h" +#include "lj_iropt.h" +#include "lj_mcode.h" +#include "lj_trace.h" +#include "lj_snap.h" +#include "lj_gdbjit.h" +#include "lj_record.h" +#include "lj_asm.h" +#include "lj_dispatch.h" +#include "lj_vm.h" +#include "lj_vmevent.h" +#include "lj_target.h" + +/* -- Error handling ------------------------------------------------------ */ + +/* Synchronous abort with error message. */ +void lj_trace_err(jit_State *J, TraceError e) +{ + setnilV(&J->errinfo); /* No error info. */ + setintV(J->L->top++, (int32_t)e); + lj_err_throw(J->L, LUA_ERRRUN); +} + +/* Synchronous abort with error message and error info. */ +void lj_trace_err_info(jit_State *J, TraceError e) +{ + setintV(J->L->top++, (int32_t)e); + lj_err_throw(J->L, LUA_ERRRUN); +} + +/* -- Trace management ---------------------------------------------------- */ + +/* The current trace is first assembled in J->cur. The variable length +** arrays point to shared, growable buffers (J->irbuf etc.). When trace +** recording ends successfully, the current trace and its data structures +** are copied to a new (compact) GCtrace object. +*/ + +/* Find a free trace number. */ +static TraceNo trace_findfree(jit_State *J) +{ + MSize osz, lim; + if (J->freetrace == 0) + J->freetrace = 1; + for (; J->freetrace < J->sizetrace; J->freetrace++) + if (traceref(J, J->freetrace) == NULL) + return J->freetrace++; + /* Need to grow trace array. */ + lim = (MSize)J->param[JIT_P_maxtrace] + 1; + if (lim < 2) lim = 2; else if (lim > 65535) lim = 65535; + osz = J->sizetrace; + if (osz >= lim) + return 0; /* Too many traces. */ + lj_mem_growvec(J->L, J->trace, J->sizetrace, lim, GCRef); + for (; osz < J->sizetrace; osz++) + setgcrefnull(J->trace[osz]); + return J->freetrace; +} + +#define TRACE_APPENDVEC(field, szfield, tp) \ + T->field = (tp *)p; \ + memcpy(p, J->cur.field, J->cur.szfield*sizeof(tp)); \ + p += J->cur.szfield*sizeof(tp); + +#ifdef LUAJIT_USE_PERFTOOLS +/* +** Create symbol table of JIT-compiled code. For use with Linux perf tools. +** Example usage: +** perf record -f -e cycles luajit test.lua +** perf report -s symbol +** rm perf.data /tmp/perf-*.map +*/ +#include +#include + +static void perftools_addtrace(GCtrace *T) +{ + static FILE *fp; + GCproto *pt = &gcref(T->startpt)->pt; + const BCIns *startpc = mref(T->startpc, const BCIns); + const char *name = proto_chunknamestr(pt); + BCLine lineno; + if (name[0] == '@' || name[0] == '=') + name++; + else + name = "(string)"; + lua_assert(startpc >= proto_bc(pt) && startpc < proto_bc(pt) + pt->sizebc); + lineno = lj_debug_line(pt, proto_bcpos(pt, startpc)); + if (!fp) { + char fname[40]; + sprintf(fname, "/tmp/perf-%d.map", getpid()); + if (!(fp = fopen(fname, "w"))) return; + setlinebuf(fp); + } + fprintf(fp, "%lx %x TRACE_%d::%s:%u\n", + (long)T->mcode, T->szmcode, T->traceno, name, lineno); +} +#endif + +/* Save current trace by copying and compacting it. */ +static void trace_save(jit_State *J) +{ + size_t sztr = ((sizeof(GCtrace)+7)&~7); + size_t szins = (J->cur.nins-J->cur.nk)*sizeof(IRIns); + size_t sz = sztr + szins + + J->cur.nsnap*sizeof(SnapShot) + + J->cur.nsnapmap*sizeof(SnapEntry); + GCtrace *T = lj_mem_newt(J->L, (MSize)sz, GCtrace); + char *p = (char *)T + sztr; + memcpy(T, &J->cur, sizeof(GCtrace)); + setgcrefr(T->nextgc, J2G(J)->gc.root); + setgcrefp(J2G(J)->gc.root, T); + newwhite(J2G(J), T); + T->gct = ~LJ_TTRACE; + T->ir = (IRIns *)p - J->cur.nk; + memcpy(p, J->cur.ir+J->cur.nk, szins); + p += szins; + TRACE_APPENDVEC(snap, nsnap, SnapShot) + TRACE_APPENDVEC(snapmap, nsnapmap, SnapEntry) + J->cur.traceno = 0; + setgcrefp(J->trace[T->traceno], T); + lj_gc_barriertrace(J2G(J), T->traceno); + lj_gdbjit_addtrace(J, T); +#ifdef LUAJIT_USE_PERFTOOLS + perftools_addtrace(T); +#endif +} + +void LJ_FASTCALL lj_trace_free(global_State *g, GCtrace *T) +{ + jit_State *J = G2J(g); + if (T->traceno) { + lj_gdbjit_deltrace(J, T); + if (T->traceno < J->freetrace) + J->freetrace = T->traceno; + setgcrefnull(J->trace[T->traceno]); + } + lj_mem_free(g, T, + ((sizeof(GCtrace)+7)&~7) + (T->nins-T->nk)*sizeof(IRIns) + + T->nsnap*sizeof(SnapShot) + T->nsnapmap*sizeof(SnapEntry)); +} + +/* Re-enable compiling a prototype by unpatching any modified bytecode. */ +void lj_trace_reenableproto(GCproto *pt) +{ + if ((pt->flags & PROTO_ILOOP)) { + BCIns *bc = proto_bc(pt); + BCPos i, sizebc = pt->sizebc;; + pt->flags &= ~PROTO_ILOOP; + if (bc_op(bc[0]) == BC_IFUNCF) + setbc_op(&bc[0], BC_FUNCF); + for (i = 1; i < sizebc; i++) { + BCOp op = bc_op(bc[i]); + if (op == BC_IFORL || op == BC_IITERL || op == BC_ILOOP) + setbc_op(&bc[i], (int)op+(int)BC_LOOP-(int)BC_ILOOP); + } + } +} + +/* Unpatch the bytecode modified by a root trace. */ +static void trace_unpatch(jit_State *J, GCtrace *T) +{ + BCOp op = bc_op(T->startins); + BCIns *pc = mref(T->startpc, BCIns); + UNUSED(J); + if (op == BC_JMP) + return; /* No need to unpatch branches in parent traces (yet). */ + switch (bc_op(*pc)) { + case BC_JFORL: + lua_assert(traceref(J, bc_d(*pc)) == T); + *pc = T->startins; + pc += bc_j(T->startins); + lua_assert(bc_op(*pc) == BC_JFORI); + setbc_op(pc, BC_FORI); + break; + case BC_JITERL: + case BC_JLOOP: + lua_assert(op == BC_ITERL || op == BC_LOOP || bc_isret(op)); + *pc = T->startins; + break; + case BC_JMP: + lua_assert(op == BC_ITERL); + pc += bc_j(*pc)+2; + if (bc_op(*pc) == BC_JITERL) { + lua_assert(traceref(J, bc_d(*pc)) == T); + *pc = T->startins; + } + break; + case BC_JFUNCF: + lua_assert(op == BC_FUNCF); + *pc = T->startins; + break; + default: /* Already unpatched. */ + break; + } +} + +/* Flush a root trace. */ +static void trace_flushroot(jit_State *J, GCtrace *T) +{ + GCproto *pt = &gcref(T->startpt)->pt; + lua_assert(T->root == 0 && pt != NULL); + /* First unpatch any modified bytecode. */ + trace_unpatch(J, T); + /* Unlink root trace from chain anchored in prototype. */ + if (pt->trace == T->traceno) { /* Trace is first in chain. Easy. */ + pt->trace = T->nextroot; + } else if (pt->trace) { /* Otherwise search in chain of root traces. */ + GCtrace *T2 = traceref(J, pt->trace); + if (T2) { + for (; T2->nextroot; T2 = traceref(J, T2->nextroot)) + if (T2->nextroot == T->traceno) { + T2->nextroot = T->nextroot; /* Unlink from chain. */ + break; + } + } + } +} + +/* Flush a trace. Only root traces are considered. */ +void lj_trace_flush(jit_State *J, TraceNo traceno) +{ + if (traceno > 0 && traceno < J->sizetrace) { + GCtrace *T = traceref(J, traceno); + if (T && T->root == 0) + trace_flushroot(J, T); + } +} + +/* Flush all traces associated with a prototype. */ +void lj_trace_flushproto(global_State *g, GCproto *pt) +{ + while (pt->trace != 0) + trace_flushroot(G2J(g), traceref(G2J(g), pt->trace)); +} + +/* Flush all traces. */ +int lj_trace_flushall(lua_State *L) +{ + jit_State *J = L2J(L); + ptrdiff_t i; + if ((J2G(J)->hookmask & HOOK_GC)) + return 1; + for (i = (ptrdiff_t)J->sizetrace-1; i > 0; i--) { + GCtrace *T = traceref(J, i); + if (T) { + if (T->root == 0) + trace_flushroot(J, T); + lj_gdbjit_deltrace(J, T); + T->traceno = 0; + setgcrefnull(J->trace[i]); + } + } + J->cur.traceno = 0; + J->freetrace = 0; + /* Clear penalty cache. */ + memset(J->penalty, 0, sizeof(J->penalty)); + /* Free the whole machine code and invalidate all exit stub groups. */ + lj_mcode_free(J); + memset(J->exitstubgroup, 0, sizeof(J->exitstubgroup)); + lj_vmevent_send(L, TRACE, + setstrV(L, L->top++, lj_str_newlit(L, "flush")); + ); + return 0; +} + +/* Initialize JIT compiler state. */ +void lj_trace_initstate(global_State *g) +{ + jit_State *J = G2J(g); + TValue *tv; + /* Initialize SIMD constants. */ + tv = LJ_KSIMD(J, LJ_KSIMD_ABS); + tv[0].u64 = U64x(7fffffff,ffffffff); + tv[1].u64 = U64x(7fffffff,ffffffff); + tv = LJ_KSIMD(J, LJ_KSIMD_NEG); + tv[0].u64 = U64x(80000000,00000000); + tv[1].u64 = U64x(80000000,00000000); +} + +/* Free everything associated with the JIT compiler state. */ +void lj_trace_freestate(global_State *g) +{ + jit_State *J = G2J(g); +#ifdef LUA_USE_ASSERT + { /* This assumes all traces have already been freed. */ + ptrdiff_t i; + for (i = 1; i < (ptrdiff_t)J->sizetrace; i++) + lua_assert(i == (ptrdiff_t)J->cur.traceno || traceref(J, i) == NULL); + } +#endif + lj_mcode_free(J); + lj_ir_k64_freeall(J); + lj_mem_freevec(g, J->snapmapbuf, J->sizesnapmap, SnapEntry); + lj_mem_freevec(g, J->snapbuf, J->sizesnap, SnapShot); + lj_mem_freevec(g, J->irbuf + J->irbotlim, J->irtoplim - J->irbotlim, IRIns); + lj_mem_freevec(g, J->trace, J->sizetrace, GCRef); +} + +/* -- Penalties and blacklisting ------------------------------------------ */ + +/* Blacklist a bytecode instruction. */ +static void blacklist_pc(GCproto *pt, BCIns *pc) +{ + setbc_op(pc, (int)bc_op(*pc)+(int)BC_ILOOP-(int)BC_LOOP); + pt->flags |= PROTO_ILOOP; +} + +/* Penalize a bytecode instruction. */ +static void penalty_pc(jit_State *J, GCproto *pt, BCIns *pc, TraceError e) +{ + uint32_t i, val = PENALTY_MIN; + for (i = 0; i < PENALTY_SLOTS; i++) + if (mref(J->penalty[i].pc, const BCIns) == pc) { /* Cache slot found? */ + /* First try to bump its hotcount several times. */ + val = ((uint32_t)J->penalty[i].val << 1) + + LJ_PRNG_BITS(J, PENALTY_RNDBITS); + if (val > PENALTY_MAX) { + blacklist_pc(pt, pc); /* Blacklist it, if that didn't help. */ + return; + } + goto setpenalty; + } + /* Assign a new penalty cache slot. */ + i = J->penaltyslot; + J->penaltyslot = (J->penaltyslot + 1) & (PENALTY_SLOTS-1); + setmref(J->penalty[i].pc, pc); +setpenalty: + J->penalty[i].val = (uint16_t)val; + J->penalty[i].reason = e; + hotcount_set(J2GG(J), pc+1, val); +} + +/* -- Trace compiler state machine ---------------------------------------- */ + +/* Start tracing. */ +static void trace_start(jit_State *J) +{ + lua_State *L; + TraceNo traceno; + + if ((J->pt->flags & PROTO_NOJIT)) { /* JIT disabled for this proto? */ + if (J->parent == 0) { + /* Lazy bytecode patching to disable hotcount events. */ + lua_assert(bc_op(*J->pc) == BC_FORL || bc_op(*J->pc) == BC_ITERL || + bc_op(*J->pc) == BC_LOOP || bc_op(*J->pc) == BC_FUNCF); + setbc_op(J->pc, (int)bc_op(*J->pc)+(int)BC_ILOOP-(int)BC_LOOP); + J->pt->flags |= PROTO_ILOOP; + } + J->state = LJ_TRACE_IDLE; /* Silently ignored. */ + return; + } + + /* Get a new trace number. */ + traceno = trace_findfree(J); + if (LJ_UNLIKELY(traceno == 0)) { /* No free trace? */ + lua_assert((J2G(J)->hookmask & HOOK_GC) == 0); + lj_trace_flushall(J->L); + J->state = LJ_TRACE_IDLE; /* Silently ignored. */ + return; + } + setgcrefp(J->trace[traceno], &J->cur); + + /* Setup enough of the current trace to be able to send the vmevent. */ + memset(&J->cur, 0, sizeof(GCtrace)); + J->cur.traceno = traceno; + J->cur.nins = J->cur.nk = REF_BASE; + J->cur.ir = J->irbuf; + J->cur.snap = J->snapbuf; + J->cur.snapmap = J->snapmapbuf; + J->mergesnap = 0; + J->needsnap = 0; + J->bcskip = 0; + J->guardemit.irt = 0; + J->postproc = LJ_POST_NONE; + lj_resetsplit(J); + setgcref(J->cur.startpt, obj2gco(J->pt)); + + L = J->L; + lj_vmevent_send(L, TRACE, + setstrV(L, L->top++, lj_str_newlit(L, "start")); + setintV(L->top++, traceno); + setfuncV(L, L->top++, J->fn); + setintV(L->top++, proto_bcpos(J->pt, J->pc)); + if (J->parent) { + setintV(L->top++, J->parent); + setintV(L->top++, J->exitno); + } + ); + lj_record_setup(J); +} + +/* Stop tracing. */ +static void trace_stop(jit_State *J) +{ + BCIns *pc = mref(J->cur.startpc, BCIns); + BCOp op = bc_op(J->cur.startins); + GCproto *pt = &gcref(J->cur.startpt)->pt; + TraceNo traceno = J->cur.traceno; + lua_State *L; + + switch (op) { + case BC_FORL: + setbc_op(pc+bc_j(J->cur.startins), BC_JFORI); /* Patch FORI, too. */ + /* fallthrough */ + case BC_LOOP: + case BC_ITERL: + case BC_FUNCF: + /* Patch bytecode of starting instruction in root trace. */ + setbc_op(pc, (int)op+(int)BC_JLOOP-(int)BC_LOOP); + setbc_d(pc, traceno); + addroot: + /* Add to root trace chain in prototype. */ + J->cur.nextroot = pt->trace; + pt->trace = (TraceNo1)traceno; + break; + case BC_RET: + case BC_RET0: + case BC_RET1: + *pc = BCINS_AD(BC_JLOOP, J->cur.snap[0].nslots, traceno); + goto addroot; + case BC_JMP: + /* Patch exit branch in parent to side trace entry. */ + lua_assert(J->parent != 0 && J->cur.root != 0); + lj_asm_patchexit(J, traceref(J, J->parent), J->exitno, J->cur.mcode); + /* Avoid compiling a side trace twice (stack resizing uses parent exit). */ + traceref(J, J->parent)->snap[J->exitno].count = SNAPCOUNT_DONE; + /* Add to side trace chain in root trace. */ + { + GCtrace *root = traceref(J, J->cur.root); + root->nchild++; + J->cur.nextside = root->nextside; + root->nextside = (TraceNo1)traceno; + } + break; + default: + lua_assert(0); + break; + } + + /* Commit new mcode only after all patching is done. */ + lj_mcode_commit(J, J->cur.mcode); + J->postproc = LJ_POST_NONE; + trace_save(J); + + L = J->L; + lj_vmevent_send(L, TRACE, + setstrV(L, L->top++, lj_str_newlit(L, "stop")); + setintV(L->top++, traceno); + ); +} + +/* Start a new root trace for down-recursion. */ +static int trace_downrec(jit_State *J) +{ + /* Restart recording at the return instruction. */ + lua_assert(J->pt != NULL); + lua_assert(bc_isret(bc_op(*J->pc))); + if (bc_op(*J->pc) == BC_RETM) + return 0; /* NYI: down-recursion with RETM. */ + J->parent = 0; + J->exitno = 0; + J->state = LJ_TRACE_RECORD; + trace_start(J); + return 1; +} + +/* Abort tracing. */ +static int trace_abort(jit_State *J) +{ + lua_State *L = J->L; + TraceError e = LJ_TRERR_RECERR; + TraceNo traceno; + + J->postproc = LJ_POST_NONE; + lj_mcode_abort(J); + if (tvisnumber(L->top-1)) + e = (TraceError)numberVint(L->top-1); + if (e == LJ_TRERR_MCODELM) { + L->top--; /* Remove error object */ + J->state = LJ_TRACE_ASM; + return 1; /* Retry ASM with new MCode area. */ + } + /* Penalize or blacklist starting bytecode instruction. */ + if (J->parent == 0 && !bc_isret(bc_op(J->cur.startins))) + penalty_pc(J, &gcref(J->cur.startpt)->pt, mref(J->cur.startpc, BCIns), e); + + /* Is there anything to abort? */ + traceno = J->cur.traceno; + if (traceno) { + ptrdiff_t errobj = savestack(L, L->top-1); /* Stack may be resized. */ + J->cur.link = 0; + J->cur.linktype = LJ_TRLINK_NONE; + lj_vmevent_send(L, TRACE, + TValue *frame; + const BCIns *pc; + GCfunc *fn; + setstrV(L, L->top++, lj_str_newlit(L, "abort")); + setintV(L->top++, traceno); + /* Find original Lua function call to generate a better error message. */ + frame = J->L->base-1; + pc = J->pc; + while (!isluafunc(frame_func(frame))) { + pc = (frame_iscont(frame) ? frame_contpc(frame) : frame_pc(frame)) - 1; + frame = frame_prev(frame); + } + fn = frame_func(frame); + setfuncV(L, L->top++, fn); + setintV(L->top++, proto_bcpos(funcproto(fn), pc)); + copyTV(L, L->top++, restorestack(L, errobj)); + copyTV(L, L->top++, &J->errinfo); + ); + /* Drop aborted trace after the vmevent (which may still access it). */ + setgcrefnull(J->trace[traceno]); + if (traceno < J->freetrace) + J->freetrace = traceno; + J->cur.traceno = 0; + } + L->top--; /* Remove error object */ + if (e == LJ_TRERR_DOWNREC) + return trace_downrec(J); + else if (e == LJ_TRERR_MCODEAL) + lj_trace_flushall(L); + return 0; +} + +/* Perform pending re-patch of a bytecode instruction. */ +static LJ_AINLINE void trace_pendpatch(jit_State *J, int force) +{ + if (LJ_UNLIKELY(J->patchpc)) { + if (force || J->bcskip == 0) { + *J->patchpc = J->patchins; + J->patchpc = NULL; + } else { + J->bcskip = 0; + } + } +} + +/* State machine for the trace compiler. Protected callback. */ +static TValue *trace_state(lua_State *L, lua_CFunction dummy, void *ud) +{ + jit_State *J = (jit_State *)ud; + UNUSED(dummy); + do { + retry: + switch (J->state) { + case LJ_TRACE_START: + J->state = LJ_TRACE_RECORD; /* trace_start() may change state. */ + trace_start(J); + lj_dispatch_update(J2G(J)); + break; + + case LJ_TRACE_RECORD: + trace_pendpatch(J, 0); + setvmstate(J2G(J), RECORD); + lj_vmevent_send_(L, RECORD, + /* Save/restore tmptv state for trace recorder. */ + TValue savetv = J2G(J)->tmptv; + TValue savetv2 = J2G(J)->tmptv2; + setintV(L->top++, J->cur.traceno); + setfuncV(L, L->top++, J->fn); + setintV(L->top++, J->pt ? (int32_t)proto_bcpos(J->pt, J->pc) : -1); + setintV(L->top++, J->framedepth); + , + J2G(J)->tmptv = savetv; + J2G(J)->tmptv2 = savetv2; + ); + lj_record_ins(J); + break; + + case LJ_TRACE_END: + trace_pendpatch(J, 1); + J->loopref = 0; + if ((J->flags & JIT_F_OPT_LOOP) && + J->cur.link == J->cur.traceno && J->framedepth + J->retdepth == 0) { + setvmstate(J2G(J), OPT); + lj_opt_dce(J); + if (lj_opt_loop(J)) { /* Loop optimization failed? */ + J->cur.link = 0; + J->cur.linktype = LJ_TRLINK_NONE; + J->loopref = J->cur.nins; + J->state = LJ_TRACE_RECORD; /* Try to continue recording. */ + break; + } + J->loopref = J->chain[IR_LOOP]; /* Needed by assembler. */ + } + lj_opt_split(J); + lj_opt_sink(J); + J->state = LJ_TRACE_ASM; + break; + + case LJ_TRACE_ASM: + setvmstate(J2G(J), ASM); + lj_asm_trace(J, &J->cur); + trace_stop(J); + setvmstate(J2G(J), INTERP); + J->state = LJ_TRACE_IDLE; + lj_dispatch_update(J2G(J)); + return NULL; + + default: /* Trace aborted asynchronously. */ + setintV(L->top++, (int32_t)LJ_TRERR_RECERR); + /* fallthrough */ + case LJ_TRACE_ERR: + trace_pendpatch(J, 1); + if (trace_abort(J)) + goto retry; + setvmstate(J2G(J), INTERP); + J->state = LJ_TRACE_IDLE; + lj_dispatch_update(J2G(J)); + return NULL; + } + } while (J->state > LJ_TRACE_RECORD); + return NULL; +} + +/* -- Event handling ------------------------------------------------------ */ + +/* A bytecode instruction is about to be executed. Record it. */ +void lj_trace_ins(jit_State *J, const BCIns *pc) +{ + /* Note: J->L must already be set. pc is the true bytecode PC here. */ + J->pc = pc; + J->fn = curr_func(J->L); + J->pt = isluafunc(J->fn) ? funcproto(J->fn) : NULL; + while (lj_vm_cpcall(J->L, NULL, (void *)J, trace_state) != 0) + J->state = LJ_TRACE_ERR; +} + +/* A hotcount triggered. Start recording a root trace. */ +void LJ_FASTCALL lj_trace_hot(jit_State *J, const BCIns *pc) +{ + /* Note: pc is the interpreter bytecode PC here. It's offset by 1. */ + ERRNO_SAVE + /* Reset hotcount. */ + hotcount_set(J2GG(J), pc, J->param[JIT_P_hotloop]*HOTCOUNT_LOOP); + /* Only start a new trace if not recording or inside __gc call or vmevent. */ + if (J->state == LJ_TRACE_IDLE && + !(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT))) { + J->parent = 0; /* Root trace. */ + J->exitno = 0; + J->state = LJ_TRACE_START; + lj_trace_ins(J, pc-1); + } + ERRNO_RESTORE +} + +/* Check for a hot side exit. If yes, start recording a side trace. */ +static void trace_hotside(jit_State *J, const BCIns *pc) +{ + SnapShot *snap = &traceref(J, J->parent)->snap[J->exitno]; + if (!(J2G(J)->hookmask & (HOOK_GC|HOOK_VMEVENT)) && + snap->count != SNAPCOUNT_DONE && + ++snap->count >= J->param[JIT_P_hotexit]) { + lua_assert(J->state == LJ_TRACE_IDLE); + /* J->parent is non-zero for a side trace. */ + J->state = LJ_TRACE_START; + lj_trace_ins(J, pc); + } +} + +/* Tiny struct to pass data to protected call. */ +typedef struct ExitDataCP { + jit_State *J; + void *exptr; /* Pointer to exit state. */ + const BCIns *pc; /* Restart interpreter at this PC. */ +} ExitDataCP; + +/* Need to protect lj_snap_restore because it may throw. */ +static TValue *trace_exit_cp(lua_State *L, lua_CFunction dummy, void *ud) +{ + ExitDataCP *exd = (ExitDataCP *)ud; + cframe_errfunc(L->cframe) = -1; /* Inherit error function. */ + exd->pc = lj_snap_restore(exd->J, exd->exptr); + UNUSED(dummy); + return NULL; +} + +#ifndef LUAJIT_DISABLE_VMEVENT +/* Push all registers from exit state. */ +static void trace_exit_regs(lua_State *L, ExitState *ex) +{ + int32_t i; + setintV(L->top++, RID_NUM_GPR); + setintV(L->top++, RID_NUM_FPR); + for (i = 0; i < RID_NUM_GPR; i++) { + if (sizeof(ex->gpr[i]) == sizeof(int32_t)) + setintV(L->top++, (int32_t)ex->gpr[i]); + else + setnumV(L->top++, (lua_Number)ex->gpr[i]); + } +#if !LJ_SOFTFP + for (i = 0; i < RID_NUM_FPR; i++) { + setnumV(L->top, ex->fpr[i]); + if (LJ_UNLIKELY(tvisnan(L->top))) + setnanV(L->top); + L->top++; + } +#endif +} +#endif + +#ifdef EXITSTATE_PCREG +/* Determine trace number from pc of exit instruction. */ +static TraceNo trace_exit_find(jit_State *J, MCode *pc) +{ + TraceNo traceno; + for (traceno = 1; traceno < J->sizetrace; traceno++) { + GCtrace *T = traceref(J, traceno); + if (T && pc >= T->mcode && pc < (MCode *)((char *)T->mcode + T->szmcode)) + return traceno; + } + lua_assert(0); + return 0; +} +#endif + +/* A trace exited. Restore interpreter state. */ +int LJ_FASTCALL lj_trace_exit(jit_State *J, void *exptr) +{ + ERRNO_SAVE + lua_State *L = J->L; + ExitState *ex = (ExitState *)exptr; + ExitDataCP exd; + int errcode; + const BCIns *pc; + void *cf; + GCtrace *T; +#ifdef EXITSTATE_PCREG + J->parent = trace_exit_find(J, (MCode *)(intptr_t)ex->gpr[EXITSTATE_PCREG]); +#endif + T = traceref(J, J->parent); UNUSED(T); +#ifdef EXITSTATE_CHECKEXIT + if (J->exitno == T->nsnap) { /* Treat stack check like a parent exit. */ + lua_assert(T->root != 0); + J->exitno = T->ir[REF_BASE].op2; + J->parent = T->ir[REF_BASE].op1; + T = traceref(J, J->parent); + } +#endif + lua_assert(T != NULL && J->exitno < T->nsnap); + exd.J = J; + exd.exptr = exptr; + errcode = lj_vm_cpcall(L, NULL, &exd, trace_exit_cp); + if (errcode) + return -errcode; /* Return negated error code. */ + + lj_vmevent_send(L, TEXIT, + lj_state_checkstack(L, 4+RID_NUM_GPR+RID_NUM_FPR+LUA_MINSTACK); + setintV(L->top++, J->parent); + setintV(L->top++, J->exitno); + trace_exit_regs(L, ex); + ); + + pc = exd.pc; + cf = cframe_raw(L->cframe); + setcframe_pc(cf, pc); + if (G(L)->gc.state == GCSatomic || G(L)->gc.state == GCSfinalize) { + if (!(G(L)->hookmask & HOOK_GC)) + lj_gc_step(L); /* Exited because of GC: drive GC forward. */ + } else { + trace_hotside(J, pc); + } + if (bc_op(*pc) == BC_JLOOP) { + BCIns *retpc = &traceref(J, bc_d(*pc))->startins; + if (bc_isret(bc_op(*retpc))) { + if (J->state == LJ_TRACE_RECORD) { + J->patchins = *pc; + J->patchpc = (BCIns *)pc; + *J->patchpc = *retpc; + J->bcskip = 1; + } else { + pc = retpc; + setcframe_pc(cf, pc); + } + } + } + /* Return MULTRES or 0. */ + ERRNO_RESTORE + switch (bc_op(*pc)) { + case BC_CALLM: case BC_CALLMT: + return (int)((BCReg)(L->top - L->base) - bc_a(*pc) - bc_c(*pc)); + case BC_RETM: + return (int)((BCReg)(L->top - L->base) + 1 - bc_a(*pc) - bc_d(*pc)); + case BC_TSETM: + return (int)((BCReg)(L->top - L->base) + 1 - bc_a(*pc)); + default: + if (bc_op(*pc) >= BC_FUNCF) + return (int)((BCReg)(L->top - L->base) + 1); + return 0; + } +} + +#endif diff --git a/external/lua/luajit/src/src/lj_trace.h b/external/lua/luajit/src/src/lj_trace.h new file mode 100644 index 0000000000..e30d3d599e --- /dev/null +++ b/external/lua/luajit/src/src/lj_trace.h @@ -0,0 +1,53 @@ +/* +** Trace management. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_TRACE_H +#define _LJ_TRACE_H + +#include "lj_obj.h" + +#if LJ_HASJIT +#include "lj_jit.h" +#include "lj_dispatch.h" + +/* Trace errors. */ +typedef enum { +#define TREDEF(name, msg) LJ_TRERR_##name, +#include "lj_traceerr.h" + LJ_TRERR__MAX +} TraceError; + +LJ_FUNC_NORET void lj_trace_err(jit_State *J, TraceError e); +LJ_FUNC_NORET void lj_trace_err_info(jit_State *J, TraceError e); + +/* Trace management. */ +LJ_FUNC void LJ_FASTCALL lj_trace_free(global_State *g, GCtrace *T); +LJ_FUNC void lj_trace_reenableproto(GCproto *pt); +LJ_FUNC void lj_trace_flushproto(global_State *g, GCproto *pt); +LJ_FUNC void lj_trace_flush(jit_State *J, TraceNo traceno); +LJ_FUNC int lj_trace_flushall(lua_State *L); +LJ_FUNC void lj_trace_initstate(global_State *g); +LJ_FUNC void lj_trace_freestate(global_State *g); + +/* Event handling. */ +LJ_FUNC void lj_trace_ins(jit_State *J, const BCIns *pc); +LJ_FUNCA void LJ_FASTCALL lj_trace_hot(jit_State *J, const BCIns *pc); +LJ_FUNCA int LJ_FASTCALL lj_trace_exit(jit_State *J, void *exptr); + +/* Signal asynchronous abort of trace or end of trace. */ +#define lj_trace_abort(g) (G2J(g)->state &= ~LJ_TRACE_ACTIVE) +#define lj_trace_end(J) (J->state = LJ_TRACE_END) + +#else + +#define lj_trace_flushall(L) (UNUSED(L), 0) +#define lj_trace_initstate(g) UNUSED(g) +#define lj_trace_freestate(g) UNUSED(g) +#define lj_trace_abort(g) UNUSED(g) +#define lj_trace_end(J) UNUSED(J) + +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_traceerr.h b/external/lua/luajit/src/src/lj_traceerr.h new file mode 100644 index 0000000000..2ef4ad63e7 --- /dev/null +++ b/external/lua/luajit/src/src/lj_traceerr.h @@ -0,0 +1,61 @@ +/* +** Trace compiler error messages. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* This file may be included multiple times with different TREDEF macros. */ + +/* Recording. */ +TREDEF(RECERR, "error thrown or hook called during recording") +TREDEF(TRACEOV, "trace too long") +TREDEF(STACKOV, "trace too deep") +TREDEF(SNAPOV, "too many snapshots") +TREDEF(BLACKL, "blacklisted") +TREDEF(NYIBC, "NYI: bytecode %d") + +/* Recording loop ops. */ +TREDEF(LLEAVE, "leaving loop in root trace") +TREDEF(LINNER, "inner loop in root trace") +TREDEF(LUNROLL, "loop unroll limit reached") + +/* Recording calls/returns. */ +TREDEF(BADTYPE, "bad argument type") +TREDEF(CJITOFF, "call to JIT-disabled function") +TREDEF(CUNROLL, "call unroll limit reached") +TREDEF(DOWNREC, "down-recursion, restarting") +TREDEF(NYICF, "NYI: C function %p") +TREDEF(NYIFF, "NYI: FastFunc %s") +TREDEF(NYIFFU, "NYI: unsupported variant of FastFunc %s") +TREDEF(NYIRETL, "NYI: return to lower frame") + +/* Recording indexed load/store. */ +TREDEF(STORENN, "store with nil or NaN key") +TREDEF(NOMM, "missing metamethod") +TREDEF(IDXLOOP, "looping index lookup") +TREDEF(NYITMIX, "NYI: mixed sparse/dense table") + +/* Recording C data operations. */ +TREDEF(NOCACHE, "symbol not in cache") +TREDEF(NYICONV, "NYI: unsupported C type conversion") +TREDEF(NYICALL, "NYI: unsupported C function type") + +/* Optimizations. */ +TREDEF(GFAIL, "guard would always fail") +TREDEF(PHIOV, "too many PHIs") +TREDEF(TYPEINS, "persistent type instability") + +/* Assembler. */ +TREDEF(MCODEAL, "failed to allocate mcode memory") +TREDEF(MCODEOV, "machine code too long") +TREDEF(MCODELM, "hit mcode limit (retrying)") +TREDEF(SPILLOV, "too many spill slots") +TREDEF(BADRA, "inconsistent register allocation") +TREDEF(NYIIR, "NYI: cannot assemble IR instruction %d") +TREDEF(NYIPHI, "NYI: PHI shuffling too complex") +TREDEF(NYICOAL, "NYI: register coalescing too complex") + +#undef TREDEF + +/* Detecting unused error messages: + awk -F, '/^TREDEF/ { gsub(/TREDEF./, ""); printf "grep -q LJ_TRERR_%s *.[ch] || echo %s\n", $1, $1}' lj_traceerr.h | sh +*/ diff --git a/external/lua/luajit/src/src/lj_udata.c b/external/lua/luajit/src/src/lj_udata.c new file mode 100644 index 0000000000..df5e7f30bf --- /dev/null +++ b/external/lua/luajit/src/src/lj_udata.c @@ -0,0 +1,34 @@ +/* +** Userdata handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_udata_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_gc.h" +#include "lj_udata.h" + +GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env) +{ + GCudata *ud = lj_mem_newt(L, sizeof(GCudata) + sz, GCudata); + global_State *g = G(L); + newwhite(g, ud); /* Not finalized. */ + ud->gct = ~LJ_TUDATA; + ud->udtype = UDTYPE_USERDATA; + ud->len = sz; + /* NOBARRIER: The GCudata is new (marked white). */ + setgcrefnull(ud->metatable); + setgcref(ud->env, obj2gco(env)); + /* Chain to userdata list (after main thread). */ + setgcrefr(ud->nextgc, mainthread(g)->nextgc); + setgcref(mainthread(g)->nextgc, obj2gco(ud)); + return ud; +} + +void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud) +{ + lj_mem_free(g, ud, sizeudata(ud)); +} + diff --git a/external/lua/luajit/src/src/lj_udata.h b/external/lua/luajit/src/src/lj_udata.h new file mode 100644 index 0000000000..f62c02bcee --- /dev/null +++ b/external/lua/luajit/src/src/lj_udata.h @@ -0,0 +1,14 @@ +/* +** Userdata handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_UDATA_H +#define _LJ_UDATA_H + +#include "lj_obj.h" + +LJ_FUNC GCudata *lj_udata_new(lua_State *L, MSize sz, GCtab *env); +LJ_FUNC void LJ_FASTCALL lj_udata_free(global_State *g, GCudata *ud); + +#endif diff --git a/external/lua/luajit/src/src/lj_vm.h b/external/lua/luajit/src/src/lj_vm.h new file mode 100644 index 0000000000..c5d05de44d --- /dev/null +++ b/external/lua/luajit/src/src/lj_vm.h @@ -0,0 +1,116 @@ +/* +** Assembler VM interface definitions. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_VM_H +#define _LJ_VM_H + +#include "lj_obj.h" + +/* Entry points for ASM parts of VM. */ +LJ_ASMF void lj_vm_call(lua_State *L, TValue *base, int nres1); +LJ_ASMF int lj_vm_pcall(lua_State *L, TValue *base, int nres1, ptrdiff_t ef); +typedef TValue *(*lua_CPFunction)(lua_State *L, lua_CFunction func, void *ud); +LJ_ASMF int lj_vm_cpcall(lua_State *L, lua_CFunction func, void *ud, + lua_CPFunction cp); +LJ_ASMF int lj_vm_resume(lua_State *L, TValue *base, int nres1, ptrdiff_t ef); +LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_c(void *cframe, int errcode); +LJ_ASMF_NORET void LJ_FASTCALL lj_vm_unwind_ff(void *cframe); +LJ_ASMF void lj_vm_unwind_c_eh(void); +LJ_ASMF void lj_vm_unwind_ff_eh(void); +#if LJ_TARGET_X86ORX64 +LJ_ASMF void lj_vm_unwind_rethrow(void); +#endif + +/* Miscellaneous functions. */ +#if LJ_TARGET_X86ORX64 +LJ_ASMF int lj_vm_cpuid(uint32_t f, uint32_t res[4]); +#endif +#if LJ_TARGET_PPC +void lj_vm_cachesync(void *start, void *end); +#endif +LJ_ASMF double lj_vm_foldarith(double x, double y, int op); +#if LJ_HASJIT +LJ_ASMF double lj_vm_foldfpm(double x, int op); +#endif +#if !LJ_ARCH_HASFPU +/* Declared in lj_obj.h: LJ_ASMF int32_t lj_vm_tobit(double x); */ +#endif + +/* Dispatch targets for recording and hooks. */ +LJ_ASMF void lj_vm_record(void); +LJ_ASMF void lj_vm_inshook(void); +LJ_ASMF void lj_vm_rethook(void); +LJ_ASMF void lj_vm_callhook(void); + +/* Trace exit handling. */ +LJ_ASMF void lj_vm_exit_handler(void); +LJ_ASMF void lj_vm_exit_interp(void); + +/* Internal math helper functions. */ +#if LJ_TARGET_X86ORX64 || LJ_TARGET_PPC +#define lj_vm_floor floor +#define lj_vm_ceil ceil +#else +LJ_ASMF double lj_vm_floor(double); +LJ_ASMF double lj_vm_ceil(double); +#if LJ_TARGET_ARM +LJ_ASMF double lj_vm_floor_sf(double); +LJ_ASMF double lj_vm_ceil_sf(double); +#endif +#endif +#if defined(LUAJIT_NO_LOG2) || LJ_TARGET_X86ORX64 +LJ_ASMF double lj_vm_log2(double); +#else +#define lj_vm_log2 log2 +#endif + +#if LJ_HASJIT +#if LJ_TARGET_X86ORX64 +LJ_ASMF void lj_vm_floor_sse(void); +LJ_ASMF void lj_vm_ceil_sse(void); +LJ_ASMF void lj_vm_trunc_sse(void); +LJ_ASMF void lj_vm_exp_x87(void); +LJ_ASMF void lj_vm_exp2_x87(void); +LJ_ASMF void lj_vm_pow_sse(void); +LJ_ASMF void lj_vm_powi_sse(void); +#else +#if LJ_TARGET_PPC +#define lj_vm_trunc trunc +#else +LJ_ASMF double lj_vm_trunc(double); +#if LJ_TARGET_ARM +LJ_ASMF double lj_vm_trunc_sf(double); +#endif +#endif +LJ_ASMF double lj_vm_powi(double, int32_t); +#ifdef LUAJIT_NO_EXP2 +LJ_ASMF double lj_vm_exp2(double); +#else +#define lj_vm_exp2 exp2 +#endif +#endif +LJ_ASMF int32_t LJ_FASTCALL lj_vm_modi(int32_t, int32_t); +#if LJ_HASFFI +LJ_ASMF int lj_vm_errno(void); +#endif +#endif + +/* Continuations for metamethods. */ +LJ_ASMF void lj_cont_cat(void); /* Continue with concatenation. */ +LJ_ASMF void lj_cont_ra(void); /* Store result in RA from instruction. */ +LJ_ASMF void lj_cont_nop(void); /* Do nothing, just continue execution. */ +LJ_ASMF void lj_cont_condt(void); /* Branch if result is true. */ +LJ_ASMF void lj_cont_condf(void); /* Branch if result is false. */ +LJ_ASMF void lj_cont_hook(void); /* Continue from hook yield. */ + +enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */ + +/* Start of the ASM code. */ +LJ_ASMF char lj_vm_asm_begin[]; + +/* Bytecode offsets are relative to lj_vm_asm_begin. */ +#define makeasmfunc(ofs) ((ASMFunction)(lj_vm_asm_begin + (ofs))) + +#endif diff --git a/external/lua/luajit/src/src/lj_vmevent.c b/external/lua/luajit/src/src/lj_vmevent.c new file mode 100644 index 0000000000..21ad08feff --- /dev/null +++ b/external/lua/luajit/src/src/lj_vmevent.c @@ -0,0 +1,57 @@ +/* +** VM event handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#include + +#define lj_vmevent_c +#define LUA_CORE + +#include "lj_obj.h" +#include "lj_str.h" +#include "lj_tab.h" +#include "lj_state.h" +#include "lj_dispatch.h" +#include "lj_vm.h" +#include "lj_vmevent.h" + +ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev) +{ + global_State *g = G(L); + GCstr *s = lj_str_newlit(L, LJ_VMEVENTS_REGKEY); + cTValue *tv = lj_tab_getstr(tabV(registry(L)), s); + if (tvistab(tv)) { + int hash = VMEVENT_HASH(ev); + tv = lj_tab_getint(tabV(tv), hash); + if (tv && tvisfunc(tv)) { + lj_state_checkstack(L, LUA_MINSTACK); + setfuncV(L, L->top++, funcV(tv)); + return savestack(L, L->top); + } + } + g->vmevmask &= ~VMEVENT_MASK(ev); /* No handler: cache this fact. */ + return 0; +} + +void lj_vmevent_call(lua_State *L, ptrdiff_t argbase) +{ + global_State *g = G(L); + uint8_t oldmask = g->vmevmask; + uint8_t oldh = hook_save(g); + int status; + g->vmevmask = 0; /* Disable all events. */ + hook_vmevent(g); + status = lj_vm_pcall(L, restorestack(L, argbase), 0+1, 0); + if (LJ_UNLIKELY(status)) { + /* Really shouldn't use stderr here, but where else to complain? */ + L->top--; + fputs("VM handler failed: ", stderr); + fputs(tvisstr(L->top) ? strVdata(L->top) : "?", stderr); + fputc('\n', stderr); + } + hook_restore(g, oldh); + if (g->vmevmask != VMEVENT_NOCACHE) + g->vmevmask = oldmask; /* Restore event mask, but not if not modified. */ +} + diff --git a/external/lua/luajit/src/src/lj_vmevent.h b/external/lua/luajit/src/src/lj_vmevent.h new file mode 100644 index 0000000000..11dedb42f3 --- /dev/null +++ b/external/lua/luajit/src/src/lj_vmevent.h @@ -0,0 +1,59 @@ +/* +** VM event handling. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LJ_VMEVENT_H +#define _LJ_VMEVENT_H + +#include "lj_obj.h" + +/* Registry key for VM event handler table. */ +#define LJ_VMEVENTS_REGKEY "_VMEVENTS" +#define LJ_VMEVENTS_HSIZE 4 + +#define VMEVENT_MASK(ev) ((uint8_t)1 << ((int)(ev) & 7)) +#define VMEVENT_HASH(ev) ((int)(ev) & ~7) +#define VMEVENT_HASHIDX(h) ((int)(h) << 3) +#define VMEVENT_NOCACHE 255 + +#define VMEVENT_DEF(name, hash) \ + LJ_VMEVENT_##name##_, \ + LJ_VMEVENT_##name = ((LJ_VMEVENT_##name##_) & 7)|((hash) << 3) + +/* VM event IDs. */ +typedef enum { + VMEVENT_DEF(BC, 0x00003883), + VMEVENT_DEF(TRACE, 0xb2d91467), + VMEVENT_DEF(RECORD, 0x9284bf4f), + VMEVENT_DEF(TEXIT, 0xb29df2b0), + LJ_VMEVENT__MAX +} VMEvent; + +#ifdef LUAJIT_DISABLE_VMEVENT +#define lj_vmevent_send(L, ev, args) UNUSED(L) +#define lj_vmevent_send_(L, ev, args, post) UNUSED(L) +#else +#define lj_vmevent_send(L, ev, args) \ + if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \ + ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \ + if (argbase) { \ + args \ + lj_vmevent_call(L, argbase); \ + } \ + } +#define lj_vmevent_send_(L, ev, args, post) \ + if (G(L)->vmevmask & VMEVENT_MASK(LJ_VMEVENT_##ev)) { \ + ptrdiff_t argbase = lj_vmevent_prepare(L, LJ_VMEVENT_##ev); \ + if (argbase) { \ + args \ + lj_vmevent_call(L, argbase); \ + post \ + } \ + } + +LJ_FUNC ptrdiff_t lj_vmevent_prepare(lua_State *L, VMEvent ev); +LJ_FUNC void lj_vmevent_call(lua_State *L, ptrdiff_t argbase); +#endif + +#endif diff --git a/external/lua/luajit/src/src/lj_vmmath.c b/external/lua/luajit/src/src/lj_vmmath.c new file mode 100644 index 0000000000..aa6903033c --- /dev/null +++ b/external/lua/luajit/src/src/lj_vmmath.c @@ -0,0 +1,140 @@ +/* +** Math helper functions for assembler VM. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#define lj_vmmath_c +#define LUA_CORE + +#include +#include + +#include "lj_obj.h" +#include "lj_ir.h" +#include "lj_vm.h" + +/* -- Helper functions for generated machine code ------------------------- */ + +#if LJ_TARGET_X86ORX64 +/* Wrapper functions to avoid linker issues on OSX. */ +LJ_FUNCA double lj_vm_sinh(double x) { return sinh(x); } +LJ_FUNCA double lj_vm_cosh(double x) { return cosh(x); } +LJ_FUNCA double lj_vm_tanh(double x) { return tanh(x); } +#endif + +#if !LJ_TARGET_X86ORX64 +double lj_vm_foldarith(double x, double y, int op) +{ + switch (op) { + case IR_ADD - IR_ADD: return x+y; break; + case IR_SUB - IR_ADD: return x-y; break; + case IR_MUL - IR_ADD: return x*y; break; + case IR_DIV - IR_ADD: return x/y; break; + case IR_MOD - IR_ADD: return x-lj_vm_floor(x/y)*y; break; + case IR_POW - IR_ADD: return pow(x, y); break; + case IR_NEG - IR_ADD: return -x; break; + case IR_ABS - IR_ADD: return fabs(x); break; +#if LJ_HASJIT + case IR_ATAN2 - IR_ADD: return atan2(x, y); break; + case IR_LDEXP - IR_ADD: return ldexp(x, (int)y); break; + case IR_MIN - IR_ADD: return x > y ? y : x; break; + case IR_MAX - IR_ADD: return x < y ? y : x; break; +#endif + default: return x; + } +} +#endif + +#if LJ_HASJIT + +#ifdef LUAJIT_NO_LOG2 +double lj_vm_log2(double a) +{ + return log(a) * 1.4426950408889634074; +} +#endif + +#ifdef LUAJIT_NO_EXP2 +double lj_vm_exp2(double a) +{ + return exp(a * 0.6931471805599453); +} +#endif + +#if !(LJ_TARGET_ARM || LJ_TARGET_PPC) +int32_t LJ_FASTCALL lj_vm_modi(int32_t a, int32_t b) +{ + uint32_t y, ua, ub; + lua_assert(b != 0); /* This must be checked before using this function. */ + ua = a < 0 ? (uint32_t)-a : (uint32_t)a; + ub = b < 0 ? (uint32_t)-b : (uint32_t)b; + y = ua % ub; + if (y != 0 && (a^b) < 0) y = y - ub; + if (((int32_t)y^b) < 0) y = (uint32_t)-(int32_t)y; + return (int32_t)y; +} +#endif + +#if !LJ_TARGET_X86ORX64 +/* Unsigned x^k. */ +static double lj_vm_powui(double x, uint32_t k) +{ + double y; + lua_assert(k != 0); + for (; (k & 1) == 0; k >>= 1) x *= x; + y = x; + if ((k >>= 1) != 0) { + for (;;) { + x *= x; + if (k == 1) break; + if (k & 1) y *= x; + k >>= 1; + } + y *= x; + } + return y; +} + +/* Signed x^k. */ +double lj_vm_powi(double x, int32_t k) +{ + if (k > 1) + return lj_vm_powui(x, (uint32_t)k); + else if (k == 1) + return x; + else if (k == 0) + return 1.0; + else + return 1.0 / lj_vm_powui(x, (uint32_t)-k); +} + +/* Computes fpm(x) for extended math functions. */ +double lj_vm_foldfpm(double x, int fpm) +{ + switch (fpm) { + case IRFPM_FLOOR: return lj_vm_floor(x); + case IRFPM_CEIL: return lj_vm_ceil(x); + case IRFPM_TRUNC: return lj_vm_trunc(x); + case IRFPM_SQRT: return sqrt(x); + case IRFPM_EXP: return exp(x); + case IRFPM_EXP2: return lj_vm_exp2(x); + case IRFPM_LOG: return log(x); + case IRFPM_LOG2: return lj_vm_log2(x); + case IRFPM_LOG10: return log10(x); + case IRFPM_SIN: return sin(x); + case IRFPM_COS: return cos(x); + case IRFPM_TAN: return tan(x); + default: lua_assert(0); + } + return 0; +} +#endif + +#if LJ_HASFFI +int lj_vm_errno(void) +{ + return errno; +} +#endif + +#endif diff --git a/external/lua/luajit/src/src/ljamalg.c b/external/lua/luajit/src/src/ljamalg.c new file mode 100644 index 0000000000..962b313442 --- /dev/null +++ b/external/lua/luajit/src/src/ljamalg.c @@ -0,0 +1,93 @@ +/* +** LuaJIT core and libraries amalgamation. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +/* ++--------------------------------------------------------------------------+ +| WARNING: Compiling the amalgamation needs a lot of virtual memory | +| (around 200 MB with GCC 4.x)! If you don't have enough physical memory | +| your machine will start swapping to disk and the compile will not finish | +| within a reasonable amount of time. | +| So either compile on a bigger machine or use the non-amalgamated build. | ++--------------------------------------------------------------------------+ +*/ + +#define ljamalg_c +#define LUA_CORE + +/* To get the mremap prototype. Must be defined before any system includes. */ +#if defined(__linux__) && !defined(_GNU_SOURCE) +#define _GNU_SOURCE +#endif + +#ifndef WINVER +#define WINVER 0x0500 +#endif + +#include "lua.h" +#include "lauxlib.h" + +#include "lj_gc.c" +#include "lj_err.c" +#include "lj_char.c" +#include "lj_bc.c" +#include "lj_obj.c" +#include "lj_str.c" +#include "lj_tab.c" +#include "lj_func.c" +#include "lj_udata.c" +#include "lj_meta.c" +#include "lj_debug.c" +#include "lj_state.c" +#include "lj_dispatch.c" +#include "lj_vmevent.c" +#include "lj_vmmath.c" +#include "lj_strscan.c" +#include "lj_api.c" +#include "lj_lex.c" +#include "lj_parse.c" +#include "lj_bcread.c" +#include "lj_bcwrite.c" +#include "lj_load.c" +#include "lj_ctype.c" +#include "lj_cdata.c" +#include "lj_cconv.c" +#include "lj_ccall.c" +#include "lj_ccallback.c" +#include "lj_carith.c" +#include "lj_clib.c" +#include "lj_cparse.c" +#include "lj_lib.c" +#include "lj_ir.c" +#include "lj_opt_mem.c" +#include "lj_opt_fold.c" +#include "lj_opt_narrow.c" +#include "lj_opt_dce.c" +#include "lj_opt_loop.c" +#include "lj_opt_split.c" +#include "lj_opt_sink.c" +#include "lj_mcode.c" +#include "lj_snap.c" +#include "lj_record.c" +#include "lj_crecord.c" +#include "lj_ffrecord.c" +#include "lj_asm.c" +#include "lj_trace.c" +#include "lj_gdbjit.c" +#include "lj_alloc.c" + +#include "lib_aux.c" +#include "lib_base.c" +#include "lib_math.c" +#include "lib_string.c" +#include "lib_table.c" +#include "lib_io.c" +#include "lib_os.c" +#include "lib_package.c" +#include "lib_debug.c" +#include "lib_bit.c" +#include "lib_jit.c" +#include "lib_ffi.c" +#include "lib_init.c" + diff --git a/external/lua/luajit/src/src/lua.h b/external/lua/luajit/src/src/lua.h new file mode 100644 index 0000000000..c83fd3bbe7 --- /dev/null +++ b/external/lua/luajit/src/src/lua.h @@ -0,0 +1,393 @@ +/* +** $Id: lua.h,v 1.218.1.5 2008/08/06 13:30:12 roberto Exp $ +** Lua - An Extensible Extension Language +** Lua.org, PUC-Rio, Brazil (http://www.lua.org) +** See Copyright Notice at the end of this file +*/ + + +#ifndef lua_h +#define lua_h + +#include +#include + + +#include "luaconf.h" + + +#define LUA_VERSION "Lua 5.1" +#define LUA_RELEASE "Lua 5.1.4" +#define LUA_VERSION_NUM 501 +#define LUA_COPYRIGHT "Copyright (C) 1994-2008 Lua.org, PUC-Rio" +#define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" + + +/* mark for precompiled code (`Lua') */ +#define LUA_SIGNATURE "\033Lua" + +/* option for multiple returns in `lua_pcall' and `lua_call' */ +#define LUA_MULTRET (-1) + + +/* +** pseudo-indices +*/ +#define LUA_REGISTRYINDEX (-10000) +#define LUA_ENVIRONINDEX (-10001) +#define LUA_GLOBALSINDEX (-10002) +#define lua_upvalueindex(i) (LUA_GLOBALSINDEX-(i)) + + +/* thread status; 0 is OK */ +#define LUA_YIELD 1 +#define LUA_ERRRUN 2 +#define LUA_ERRSYNTAX 3 +#define LUA_ERRMEM 4 +#define LUA_ERRERR 5 + + +typedef struct lua_State lua_State; + +typedef int (*lua_CFunction) (lua_State *L); + + +/* +** functions that read/write blocks when loading/dumping Lua chunks +*/ +typedef const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz); + +typedef int (*lua_Writer) (lua_State *L, const void* p, size_t sz, void* ud); + + +/* +** prototype for memory-allocation functions +*/ +typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); + + +/* +** basic types +*/ +#define LUA_TNONE (-1) + +#define LUA_TNIL 0 +#define LUA_TBOOLEAN 1 +#define LUA_TLIGHTUSERDATA 2 +#define LUA_TNUMBER 3 +#define LUA_TSTRING 4 +#define LUA_TTABLE 5 +#define LUA_TFUNCTION 6 +#define LUA_TUSERDATA 7 +#define LUA_TTHREAD 8 + + + +/* minimum Lua stack available to a C function */ +#define LUA_MINSTACK 20 + + +/* +** generic extra include file +*/ +#if defined(LUA_USER_H) +#include LUA_USER_H +#endif + + +/* type of numbers in Lua */ +typedef LUA_NUMBER lua_Number; + + +/* type for integer functions */ +typedef LUA_INTEGER lua_Integer; + + + +/* +** state manipulation +*/ +LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud); +LUA_API void (lua_close) (lua_State *L); +LUA_API lua_State *(lua_newthread) (lua_State *L); + +LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf); + + +/* +** basic stack manipulation +*/ +LUA_API int (lua_gettop) (lua_State *L); +LUA_API void (lua_settop) (lua_State *L, int idx); +LUA_API void (lua_pushvalue) (lua_State *L, int idx); +LUA_API void (lua_remove) (lua_State *L, int idx); +LUA_API void (lua_insert) (lua_State *L, int idx); +LUA_API void (lua_replace) (lua_State *L, int idx); +LUA_API int (lua_checkstack) (lua_State *L, int sz); + +LUA_API void (lua_xmove) (lua_State *from, lua_State *to, int n); + + +/* +** access functions (stack -> C) +*/ + +LUA_API int (lua_isnumber) (lua_State *L, int idx); +LUA_API int (lua_isstring) (lua_State *L, int idx); +LUA_API int (lua_iscfunction) (lua_State *L, int idx); +LUA_API int (lua_isuserdata) (lua_State *L, int idx); +LUA_API int (lua_type) (lua_State *L, int idx); +LUA_API const char *(lua_typename) (lua_State *L, int tp); + +LUA_API int (lua_equal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_rawequal) (lua_State *L, int idx1, int idx2); +LUA_API int (lua_lessthan) (lua_State *L, int idx1, int idx2); + +LUA_API lua_Number (lua_tonumber) (lua_State *L, int idx); +LUA_API lua_Integer (lua_tointeger) (lua_State *L, int idx); +LUA_API int (lua_toboolean) (lua_State *L, int idx); +LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); +LUA_API size_t (lua_objlen) (lua_State *L, int idx); +LUA_API lua_CFunction (lua_tocfunction) (lua_State *L, int idx); +LUA_API void *(lua_touserdata) (lua_State *L, int idx); +LUA_API lua_State *(lua_tothread) (lua_State *L, int idx); +LUA_API const void *(lua_topointer) (lua_State *L, int idx); + + +/* +** push functions (C -> stack) +*/ +LUA_API void (lua_pushnil) (lua_State *L); +LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); +LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); +LUA_API void (lua_pushlstring) (lua_State *L, const char *s, size_t l); +LUA_API void (lua_pushstring) (lua_State *L, const char *s); +LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, + va_list argp); +LUA_API const char *(lua_pushfstring) (lua_State *L, const char *fmt, ...); +LUA_API void (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n); +LUA_API void (lua_pushboolean) (lua_State *L, int b); +LUA_API void (lua_pushlightuserdata) (lua_State *L, void *p); +LUA_API int (lua_pushthread) (lua_State *L); + + +/* +** get functions (Lua -> stack) +*/ +LUA_API void (lua_gettable) (lua_State *L, int idx); +LUA_API void (lua_getfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawget) (lua_State *L, int idx); +LUA_API void (lua_rawgeti) (lua_State *L, int idx, int n); +LUA_API void (lua_createtable) (lua_State *L, int narr, int nrec); +LUA_API void *(lua_newuserdata) (lua_State *L, size_t sz); +LUA_API int (lua_getmetatable) (lua_State *L, int objindex); +LUA_API void (lua_getfenv) (lua_State *L, int idx); + + +/* +** set functions (stack -> Lua) +*/ +LUA_API void (lua_settable) (lua_State *L, int idx); +LUA_API void (lua_setfield) (lua_State *L, int idx, const char *k); +LUA_API void (lua_rawset) (lua_State *L, int idx); +LUA_API void (lua_rawseti) (lua_State *L, int idx, int n); +LUA_API int (lua_setmetatable) (lua_State *L, int objindex); +LUA_API int (lua_setfenv) (lua_State *L, int idx); + + +/* +** `load' and `call' functions (load and run Lua code) +*/ +LUA_API void (lua_call) (lua_State *L, int nargs, int nresults); +LUA_API int (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc); +LUA_API int (lua_cpcall) (lua_State *L, lua_CFunction func, void *ud); +LUA_API int (lua_load) (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname); + +LUA_API int (lua_dump) (lua_State *L, lua_Writer writer, void *data); + + +/* +** coroutine functions +*/ +LUA_API int (lua_yield) (lua_State *L, int nresults); +LUA_API int (lua_resume) (lua_State *L, int narg); +LUA_API int (lua_status) (lua_State *L); + +/* +** garbage-collection function and options +*/ + +#define LUA_GCSTOP 0 +#define LUA_GCRESTART 1 +#define LUA_GCCOLLECT 2 +#define LUA_GCCOUNT 3 +#define LUA_GCCOUNTB 4 +#define LUA_GCSTEP 5 +#define LUA_GCSETPAUSE 6 +#define LUA_GCSETSTEPMUL 7 + +LUA_API int (lua_gc) (lua_State *L, int what, int data); + + +/* +** miscellaneous functions +*/ + +LUA_API int (lua_error) (lua_State *L); + +LUA_API int (lua_next) (lua_State *L, int idx); + +LUA_API void (lua_concat) (lua_State *L, int n); + +LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); +LUA_API void lua_setallocf (lua_State *L, lua_Alloc f, void *ud); + + + +/* +** =============================================================== +** some useful macros +** =============================================================== +*/ + +#define lua_pop(L,n) lua_settop(L, -(n)-1) + +#define lua_newtable(L) lua_createtable(L, 0, 0) + +#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n))) + +#define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) + +#define lua_strlen(L,i) lua_objlen(L, (i)) + +#define lua_isfunction(L,n) (lua_type(L, (n)) == LUA_TFUNCTION) +#define lua_istable(L,n) (lua_type(L, (n)) == LUA_TTABLE) +#define lua_islightuserdata(L,n) (lua_type(L, (n)) == LUA_TLIGHTUSERDATA) +#define lua_isnil(L,n) (lua_type(L, (n)) == LUA_TNIL) +#define lua_isboolean(L,n) (lua_type(L, (n)) == LUA_TBOOLEAN) +#define lua_isthread(L,n) (lua_type(L, (n)) == LUA_TTHREAD) +#define lua_isnone(L,n) (lua_type(L, (n)) == LUA_TNONE) +#define lua_isnoneornil(L, n) (lua_type(L, (n)) <= 0) + +#define lua_pushliteral(L, s) \ + lua_pushlstring(L, "" s, (sizeof(s)/sizeof(char))-1) + +#define lua_setglobal(L,s) lua_setfield(L, LUA_GLOBALSINDEX, (s)) +#define lua_getglobal(L,s) lua_getfield(L, LUA_GLOBALSINDEX, (s)) + +#define lua_tostring(L,i) lua_tolstring(L, (i), NULL) + + + +/* +** compatibility macros and functions +*/ + +#define lua_open() luaL_newstate() + +#define lua_getregistry(L) lua_pushvalue(L, LUA_REGISTRYINDEX) + +#define lua_getgccount(L) lua_gc(L, LUA_GCCOUNT, 0) + +#define lua_Chunkreader lua_Reader +#define lua_Chunkwriter lua_Writer + + +/* hack */ +LUA_API void lua_setlevel (lua_State *from, lua_State *to); + + +/* +** {====================================================================== +** Debug API +** ======================================================================= +*/ + + +/* +** Event codes +*/ +#define LUA_HOOKCALL 0 +#define LUA_HOOKRET 1 +#define LUA_HOOKLINE 2 +#define LUA_HOOKCOUNT 3 +#define LUA_HOOKTAILRET 4 + + +/* +** Event masks +*/ +#define LUA_MASKCALL (1 << LUA_HOOKCALL) +#define LUA_MASKRET (1 << LUA_HOOKRET) +#define LUA_MASKLINE (1 << LUA_HOOKLINE) +#define LUA_MASKCOUNT (1 << LUA_HOOKCOUNT) + +typedef struct lua_Debug lua_Debug; /* activation record */ + + +/* Functions to be called by the debuger in specific events */ +typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); + + +LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar); +LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); +LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); +LUA_API const char *lua_getupvalue (lua_State *L, int funcindex, int n); +LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n); +LUA_API int lua_sethook (lua_State *L, lua_Hook func, int mask, int count); +LUA_API lua_Hook lua_gethook (lua_State *L); +LUA_API int lua_gethookmask (lua_State *L); +LUA_API int lua_gethookcount (lua_State *L); + +/* From Lua 5.2. */ +LUA_API void *lua_upvalueid (lua_State *L, int idx, int n); +LUA_API void lua_upvaluejoin (lua_State *L, int idx1, int n1, int idx2, int n2); +LUA_API int lua_loadx (lua_State *L, lua_Reader reader, void *dt, + const char *chunkname, const char *mode); + + +struct lua_Debug { + int event; + const char *name; /* (n) */ + const char *namewhat; /* (n) `global', `local', `field', `method' */ + const char *what; /* (S) `Lua', `C', `main', `tail' */ + const char *source; /* (S) */ + int currentline; /* (l) */ + int nups; /* (u) number of upvalues */ + int linedefined; /* (S) */ + int lastlinedefined; /* (S) */ + char short_src[LUA_IDSIZE]; /* (S) */ + /* private part */ + int i_ci; /* active function */ +}; + +/* }====================================================================== */ + + +/****************************************************************************** +* Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +******************************************************************************/ + + +#endif diff --git a/external/lua/luajit/src/src/lua.hpp b/external/lua/luajit/src/src/lua.hpp new file mode 100644 index 0000000000..07e9002dc5 --- /dev/null +++ b/external/lua/luajit/src/src/lua.hpp @@ -0,0 +1,9 @@ +// C++ wrapper for LuaJIT header files. + +extern "C" { +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include "luajit.h" +} + diff --git a/external/lua/luajit/src/src/luaconf.h b/external/lua/luajit/src/src/luaconf.h new file mode 100644 index 0000000000..8e3a7aaa0c --- /dev/null +++ b/external/lua/luajit/src/src/luaconf.h @@ -0,0 +1,139 @@ +/* +** Configuration header. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef luaconf_h +#define luaconf_h + +#include +#include + +/* Default path for loading Lua and C modules with require(). */ +#if defined(_WIN32) +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_PATH_DEFAULT \ + ".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" +#define LUA_CPATH_DEFAULT \ + ".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll" +#else +/* +** Note to distribution maintainers: do NOT patch the following line! +** Please read ../doc/install.html#distro and pass PREFIX=/usr instead. +*/ +#define LUA_ROOT "/usr/local/" +#define LUA_LDIR LUA_ROOT "share/lua/5.1/" +#define LUA_CDIR LUA_ROOT "lib/lua/5.1/" +#ifdef LUA_XROOT +#define LUA_JDIR LUA_XROOT "share/luajit-2.0.1/" +#define LUA_XPATH \ + ";" LUA_XROOT "share/lua/5.1/?.lua;" LUA_XROOT "share/lua/5.1/?/init.lua" +#define LUA_XCPATH LUA_XROOT "lib/lua/5.1/?.so;" +#else +#define LUA_JDIR LUA_ROOT "share/luajit-2.0.1/" +#define LUA_XPATH +#define LUA_XCPATH +#endif +#define LUA_PATH_DEFAULT \ + "./?.lua;" LUA_JDIR"?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua" LUA_XPATH +#define LUA_CPATH_DEFAULT \ + "./?.so;" LUA_CDIR"?.so;" LUA_XCPATH LUA_CDIR"loadall.so" +#endif + +/* Environment variable names for path overrides and initialization code. */ +#define LUA_PATH "LUA_PATH" +#define LUA_CPATH "LUA_CPATH" +#define LUA_INIT "LUA_INIT" + +/* Special file system characters. */ +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif +#define LUA_PATHSEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXECDIR "!" +#define LUA_IGMARK "-" +#define LUA_PATH_CONFIG \ + LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" \ + LUA_EXECDIR "\n" LUA_IGMARK + +/* Quoting in error messages. */ +#define LUA_QL(x) "'" x "'" +#define LUA_QS LUA_QL("%s") + +/* Various tunables. */ +#define LUAI_MAXSTACK 65500 /* Max. # of stack slots for a thread (<64K). */ +#define LUAI_MAXCSTACK 8000 /* Max. # of stack slots for a C func (<10K). */ +#define LUAI_GCPAUSE 200 /* Pause GC until memory is at 200%. */ +#define LUAI_GCMUL 200 /* Run GC at 200% of allocation speed. */ +#define LUA_MAXCAPTURES 32 /* Max. pattern captures. */ + +/* Compatibility with older library function names. */ +#define LUA_COMPAT_MOD /* OLD: math.mod, NEW: math.fmod */ +#define LUA_COMPAT_GFIND /* OLD: string.gfind, NEW: string.gmatch */ + +/* Configuration for the frontend (the luajit executable). */ +#if defined(luajit_c) +#define LUA_PROGNAME "luajit" /* Fallback frontend name. */ +#define LUA_PROMPT "> " /* Interactive prompt. */ +#define LUA_PROMPT2 ">> " /* Continuation prompt. */ +#define LUA_MAXINPUT 512 /* Max. input line length. */ +#endif + +/* Note: changing the following defines breaks the Lua 5.1 ABI. */ +#define LUA_INTEGER ptrdiff_t +#define LUA_IDSIZE 60 /* Size of lua_Debug.short_src. */ +/* +** Size of lauxlib and io.* on-stack buffers. Weird workaround to avoid using +** unreasonable amounts of stack space, but still retain ABI compatibility. +** Blame Lua for depending on BUFSIZ in the ABI, blame **** for wrecking it. +*/ +#define LUAL_BUFFERSIZE (BUFSIZ > 16384 ? 8192 : BUFSIZ) + +/* The following defines are here only for compatibility with luaconf.h +** from the standard Lua distribution. They must not be changed for LuaJIT. +*/ +#define LUA_NUMBER_DOUBLE +#define LUA_NUMBER double +#define LUAI_UACNUMBER double +#define LUA_NUMBER_SCAN "%lf" +#define LUA_NUMBER_FMT "%.14g" +#define lua_number2str(s, n) sprintf((s), LUA_NUMBER_FMT, (n)) +#define LUAI_MAXNUMBER2STR 32 +#define LUA_INTFRMLEN "l" +#define LUA_INTFRM_T long + +/* Linkage of public API functions. */ +#if defined(LUA_BUILD_AS_DLL) +#if defined(LUA_CORE) || defined(LUA_LIB) +#define LUA_API __declspec(dllexport) +#else +#define LUA_API __declspec(dllimport) +#endif +#else +#define LUA_API extern +#endif + +#define LUALIB_API LUA_API + +/* Support for internal assertions. */ +#if defined(LUA_USE_ASSERT) || defined(LUA_USE_APICHECK) +#include +#endif +#ifdef LUA_USE_ASSERT +#define lua_assert(x) assert(x) +#endif +#ifdef LUA_USE_APICHECK +#define luai_apicheck(L, o) { (void)L; assert(o); } +#else +#define luai_apicheck(L, o) { (void)L; } +#endif + +#endif diff --git a/external/lua/luajit/src/src/luajit.c b/external/lua/luajit/src/src/luajit.c new file mode 100644 index 0000000000..37b0deb3cf --- /dev/null +++ b/external/lua/luajit/src/src/luajit.c @@ -0,0 +1,572 @@ +/* +** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +** +** Major portions taken verbatim or adapted from the Lua interpreter. +** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h +*/ + +#include +#include +#include + +#define luajit_c + +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include "luajit.h" + +#include "lj_arch.h" + +#if LJ_TARGET_POSIX +#include +#define lua_stdin_is_tty() isatty(0) +#elif LJ_TARGET_WINDOWS +#include +#ifdef __BORLANDC__ +#define lua_stdin_is_tty() isatty(_fileno(stdin)) +#else +#define lua_stdin_is_tty() _isatty(_fileno(stdin)) +#endif +#else +#define lua_stdin_is_tty() 1 +#endif + +#if !LJ_TARGET_CONSOLE +#include +#endif + +static lua_State *globalL = NULL; +static const char *progname = LUA_PROGNAME; + +#if !LJ_TARGET_CONSOLE +static void lstop(lua_State *L, lua_Debug *ar) +{ + (void)ar; /* unused arg. */ + lua_sethook(L, NULL, 0, 0); + /* Avoid luaL_error -- a C hook doesn't add an extra frame. */ + luaL_where(L, 0); + lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1)); + lua_error(L); +} + +static void laction(int i) +{ + signal(i, SIG_DFL); /* if another SIGINT happens before lstop, + terminate process (default action) */ + lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); +} +#endif + +static void print_usage(void) +{ + fprintf(stderr, + "usage: %s [options]... [script [args]...].\n" + "Available options are:\n" + " -e chunk Execute string " LUA_QL("chunk") ".\n" + " -l name Require library " LUA_QL("name") ".\n" + " -b ... Save or list bytecode.\n" + " -j cmd Perform LuaJIT control command.\n" + " -O[opt] Control LuaJIT optimizations.\n" + " -i Enter interactive mode after executing " LUA_QL("script") ".\n" + " -v Show version information.\n" + " -E Ignore environment variables.\n" + " -- Stop handling options.\n" + " - Execute stdin and stop handling options.\n" + , + progname); + fflush(stderr); +} + +static void l_message(const char *pname, const char *msg) +{ + if (pname) fprintf(stderr, "%s: ", pname); + fprintf(stderr, "%s\n", msg); + fflush(stderr); +} + +static int report(lua_State *L, int status) +{ + if (status && !lua_isnil(L, -1)) { + const char *msg = lua_tostring(L, -1); + if (msg == NULL) msg = "(error object is not a string)"; + l_message(progname, msg); + lua_pop(L, 1); + } + return status; +} + +static int traceback(lua_State *L) +{ + if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */ + if (lua_isnoneornil(L, 1) || + !luaL_callmeta(L, 1, "__tostring") || + !lua_isstring(L, -1)) + return 1; /* Return non-string error object. */ + lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */ + } + luaL_traceback(L, L, lua_tostring(L, 1), 1); + return 1; +} + +static int docall(lua_State *L, int narg, int clear) +{ + int status; + int base = lua_gettop(L) - narg; /* function index */ + lua_pushcfunction(L, traceback); /* push traceback function */ + lua_insert(L, base); /* put it under chunk and args */ +#if !LJ_TARGET_CONSOLE + signal(SIGINT, laction); +#endif + status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); +#if !LJ_TARGET_CONSOLE + signal(SIGINT, SIG_DFL); +#endif + lua_remove(L, base); /* remove traceback function */ + /* force a complete garbage collection in case of errors */ + if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); + return status; +} + +static void print_version(void) +{ + fputs(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n", stdout); +} + +static void print_jit_status(lua_State *L) +{ + int n; + const char *s; + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, -1, "jit"); /* Get jit.* module table. */ + lua_remove(L, -2); + lua_getfield(L, -1, "status"); + lua_remove(L, -2); + n = lua_gettop(L); + lua_call(L, 0, LUA_MULTRET); + fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout); + for (n++; (s = lua_tostring(L, n)); n++) { + putc(' ', stdout); + fputs(s, stdout); + } + putc('\n', stdout); +} + +static int getargs(lua_State *L, char **argv, int n) +{ + int narg; + int i; + int argc = 0; + while (argv[argc]) argc++; /* count total number of arguments */ + narg = argc - (n + 1); /* number of arguments to the script */ + luaL_checkstack(L, narg + 3, "too many arguments to script"); + for (i = n+1; i < argc; i++) + lua_pushstring(L, argv[i]); + lua_createtable(L, narg, n + 1); + for (i = 0; i < argc; i++) { + lua_pushstring(L, argv[i]); + lua_rawseti(L, -2, i - n); + } + return narg; +} + +static int dofile(lua_State *L, const char *name) +{ + int status = luaL_loadfile(L, name) || docall(L, 0, 1); + return report(L, status); +} + +static int dostring(lua_State *L, const char *s, const char *name) +{ + int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); + return report(L, status); +} + +static int dolibrary(lua_State *L, const char *name) +{ + lua_getglobal(L, "require"); + lua_pushstring(L, name); + return report(L, docall(L, 1, 1)); +} + +static void write_prompt(lua_State *L, int firstline) +{ + const char *p; + lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2"); + p = lua_tostring(L, -1); + if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2; + fputs(p, stdout); + fflush(stdout); + lua_pop(L, 1); /* remove global */ +} + +static int incomplete(lua_State *L, int status) +{ + if (status == LUA_ERRSYNTAX) { + size_t lmsg; + const char *msg = lua_tolstring(L, -1, &lmsg); + const char *tp = msg + lmsg - (sizeof(LUA_QL("")) - 1); + if (strstr(msg, LUA_QL("")) == tp) { + lua_pop(L, 1); + return 1; + } + } + return 0; /* else... */ +} + +static int pushline(lua_State *L, int firstline) +{ + char buf[LUA_MAXINPUT]; + write_prompt(L, firstline); + if (fgets(buf, LUA_MAXINPUT, stdin)) { + size_t len = strlen(buf); + if (len > 0 && buf[len-1] == '\n') + buf[len-1] = '\0'; + if (firstline && buf[0] == '=') + lua_pushfstring(L, "return %s", buf+1); + else + lua_pushstring(L, buf); + return 1; + } + return 0; +} + +static int loadline(lua_State *L) +{ + int status; + lua_settop(L, 0); + if (!pushline(L, 1)) + return -1; /* no input */ + for (;;) { /* repeat until gets a complete line */ + status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); + if (!incomplete(L, status)) break; /* cannot try to add lines? */ + if (!pushline(L, 0)) /* no more input? */ + return -1; + lua_pushliteral(L, "\n"); /* add a new line... */ + lua_insert(L, -2); /* ...between the two lines */ + lua_concat(L, 3); /* join them */ + } + lua_remove(L, 1); /* remove line */ + return status; +} + +static void dotty(lua_State *L) +{ + int status; + const char *oldprogname = progname; + progname = NULL; + while ((status = loadline(L)) != -1) { + if (status == 0) status = docall(L, 0, 0); + report(L, status); + if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */ + lua_getglobal(L, "print"); + lua_insert(L, 1); + if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0) + l_message(progname, + lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)", + lua_tostring(L, -1))); + } + } + lua_settop(L, 0); /* clear stack */ + fputs("\n", stdout); + fflush(stdout); + progname = oldprogname; +} + +static int handle_script(lua_State *L, char **argv, int n) +{ + int status; + const char *fname; + int narg = getargs(L, argv, n); /* collect arguments */ + lua_setglobal(L, "arg"); + fname = argv[n]; + if (strcmp(fname, "-") == 0 && strcmp(argv[n-1], "--") != 0) + fname = NULL; /* stdin */ + status = luaL_loadfile(L, fname); + lua_insert(L, -(narg+1)); + if (status == 0) + status = docall(L, narg, 0); + else + lua_pop(L, narg); + return report(L, status); +} + +/* Load add-on module. */ +static int loadjitmodule(lua_State *L) +{ + lua_getglobal(L, "require"); + lua_pushliteral(L, "jit."); + lua_pushvalue(L, -3); + lua_concat(L, 2); + if (lua_pcall(L, 1, 1, 0)) { + const char *msg = lua_tostring(L, -1); + if (msg && !strncmp(msg, "module ", 7)) { + err: + l_message(progname, + "unknown luaJIT command or jit.* modules not installed"); + return 1; + } else { + return report(L, 1); + } + } + lua_getfield(L, -1, "start"); + if (lua_isnil(L, -1)) goto err; + lua_remove(L, -2); /* Drop module table. */ + return 0; +} + +/* Run command with options. */ +static int runcmdopt(lua_State *L, const char *opt) +{ + int narg = 0; + if (opt && *opt) { + for (;;) { /* Split arguments. */ + const char *p = strchr(opt, ','); + narg++; + if (!p) break; + if (p == opt) + lua_pushnil(L); + else + lua_pushlstring(L, opt, (size_t)(p - opt)); + opt = p + 1; + } + if (*opt) + lua_pushstring(L, opt); + else + lua_pushnil(L); + } + return report(L, lua_pcall(L, narg, 0, 0)); +} + +/* JIT engine control command: try jit library first or load add-on module. */ +static int dojitcmd(lua_State *L, const char *cmd) +{ + const char *opt = strchr(cmd, '='); + lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd)); + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, -1, "jit"); /* Get jit.* module table. */ + lua_remove(L, -2); + lua_pushvalue(L, -2); + lua_gettable(L, -2); /* Lookup library function. */ + if (!lua_isfunction(L, -1)) { + lua_pop(L, 2); /* Drop non-function and jit.* table, keep module name. */ + if (loadjitmodule(L)) + return 1; + } else { + lua_remove(L, -2); /* Drop jit.* table. */ + } + lua_remove(L, -2); /* Drop module name. */ + return runcmdopt(L, opt ? opt+1 : opt); +} + +/* Optimization flags. */ +static int dojitopt(lua_State *L, const char *opt) +{ + lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); + lua_getfield(L, -1, "jit.opt"); /* Get jit.opt.* module table. */ + lua_remove(L, -2); + lua_getfield(L, -1, "start"); + lua_remove(L, -2); + return runcmdopt(L, opt); +} + +/* Save or list bytecode. */ +static int dobytecode(lua_State *L, char **argv) +{ + int narg = 0; + lua_pushliteral(L, "bcsave"); + if (loadjitmodule(L)) + return 1; + if (argv[0][2]) { + narg++; + argv[0][1] = '-'; + lua_pushstring(L, argv[0]+1); + } + for (argv++; *argv != NULL; narg++, argv++) + lua_pushstring(L, *argv); + return report(L, lua_pcall(L, narg, 0, 0)); +} + +/* check that argument has no extra characters at the end */ +#define notail(x) {if ((x)[2] != '\0') return -1;} + +#define FLAGS_INTERACTIVE 1 +#define FLAGS_VERSION 2 +#define FLAGS_EXEC 4 +#define FLAGS_OPTION 8 +#define FLAGS_NOENV 16 + +static int collectargs(char **argv, int *flags) +{ + int i; + for (i = 1; argv[i] != NULL; i++) { + if (argv[i][0] != '-') /* Not an option? */ + return i; + switch (argv[i][1]) { /* Check option. */ + case '-': + notail(argv[i]); + return (argv[i+1] != NULL ? i+1 : 0); + case '\0': + return i; + case 'i': + notail(argv[i]); + *flags |= FLAGS_INTERACTIVE; + /* fallthrough */ + case 'v': + notail(argv[i]); + *flags |= FLAGS_VERSION; + break; + case 'e': + *flags |= FLAGS_EXEC; + case 'j': /* LuaJIT extension */ + case 'l': + *flags |= FLAGS_OPTION; + if (argv[i][2] == '\0') { + i++; + if (argv[i] == NULL) return -1; + } + break; + case 'O': break; /* LuaJIT extension */ + case 'b': /* LuaJIT extension */ + if (*flags) return -1; + *flags |= FLAGS_EXEC; + return 0; + case 'E': + *flags |= FLAGS_NOENV; + break; + default: return -1; /* invalid option */ + } + } + return 0; +} + +static int runargs(lua_State *L, char **argv, int n) +{ + int i; + for (i = 1; i < n; i++) { + if (argv[i] == NULL) continue; + lua_assert(argv[i][0] == '-'); + switch (argv[i][1]) { /* option */ + case 'e': { + const char *chunk = argv[i] + 2; + if (*chunk == '\0') chunk = argv[++i]; + lua_assert(chunk != NULL); + if (dostring(L, chunk, "=(command line)") != 0) + return 1; + break; + } + case 'l': { + const char *filename = argv[i] + 2; + if (*filename == '\0') filename = argv[++i]; + lua_assert(filename != NULL); + if (dolibrary(L, filename)) + return 1; /* stop if file fails */ + break; + } + case 'j': { /* LuaJIT extension */ + const char *cmd = argv[i] + 2; + if (*cmd == '\0') cmd = argv[++i]; + lua_assert(cmd != NULL); + if (dojitcmd(L, cmd)) + return 1; + break; + } + case 'O': /* LuaJIT extension */ + if (dojitopt(L, argv[i] + 2)) + return 1; + break; + case 'b': /* LuaJIT extension */ + return dobytecode(L, argv+i); + default: break; + } + } + return 0; +} + +static int handle_luainit(lua_State *L) +{ +#if LJ_TARGET_CONSOLE + const char *init = NULL; +#else + const char *init = getenv(LUA_INIT); +#endif + if (init == NULL) + return 0; /* status OK */ + else if (init[0] == '@') + return dofile(L, init+1); + else + return dostring(L, init, "=" LUA_INIT); +} + +struct Smain { + char **argv; + int argc; + int status; +}; + +static int pmain(lua_State *L) +{ + struct Smain *s = (struct Smain *)lua_touserdata(L, 1); + char **argv = s->argv; + int script; + int flags = 0; + globalL = L; + if (argv[0] && argv[0][0]) progname = argv[0]; + LUAJIT_VERSION_SYM(); /* linker-enforced version check */ + script = collectargs(argv, &flags); + if (script < 0) { /* invalid args? */ + print_usage(); + s->status = 1; + return 0; + } + if ((flags & FLAGS_NOENV)) { + lua_pushboolean(L, 1); + lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); + } + lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */ + luaL_openlibs(L); /* open libraries */ + lua_gc(L, LUA_GCRESTART, -1); + if (!(flags & FLAGS_NOENV)) { + s->status = handle_luainit(L); + if (s->status != 0) return 0; + } + if ((flags & FLAGS_VERSION)) print_version(); + s->status = runargs(L, argv, (script > 0) ? script : s->argc); + if (s->status != 0) return 0; + if (script) { + s->status = handle_script(L, argv, script); + if (s->status != 0) return 0; + } + if ((flags & FLAGS_INTERACTIVE)) { + print_jit_status(L); + dotty(L); + } else if (script == 0 && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) { + if (lua_stdin_is_tty()) { + print_version(); + print_jit_status(L); + dotty(L); + } else { + dofile(L, NULL); /* executes stdin as a file */ + } + } + return 0; +} + +int main(int argc, char **argv) +{ + int status; + struct Smain s; + lua_State *L = lua_open(); /* create state */ + if (L == NULL) { + l_message(argv[0], "cannot create state: not enough memory"); + return EXIT_FAILURE; + } + s.argc = argc; + s.argv = argv; + status = lua_cpcall(L, pmain, &s); + report(L, status); + lua_close(L); + return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS; +} + diff --git a/external/lua/luajit/src/src/luajit.h b/external/lua/luajit/src/src/luajit.h new file mode 100644 index 0000000000..ed39d014d5 --- /dev/null +++ b/external/lua/luajit/src/src/luajit.h @@ -0,0 +1,70 @@ +/* +** LuaJIT -- a Just-In-Time Compiler for Lua. http://luajit.org/ +** +** Copyright (C) 2005-2013 Mike Pall. All rights reserved. +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] +*/ + +#ifndef _LUAJIT_H +#define _LUAJIT_H + +#include "lua.h" + +#define LUAJIT_VERSION "LuaJIT 2.0.1" +#define LUAJIT_VERSION_NUM 20001 /* Version 2.0.1 = 02.00.01. */ +#define LUAJIT_VERSION_SYM luaJIT_version_2_0_1 +#define LUAJIT_COPYRIGHT "Copyright (C) 2005-2013 Mike Pall" +#define LUAJIT_URL "http://luajit.org/" + +/* Modes for luaJIT_setmode. */ +#define LUAJIT_MODE_MASK 0x00ff + +enum { + LUAJIT_MODE_ENGINE, /* Set mode for whole JIT engine. */ + LUAJIT_MODE_DEBUG, /* Set debug mode (idx = level). */ + + LUAJIT_MODE_FUNC, /* Change mode for a function. */ + LUAJIT_MODE_ALLFUNC, /* Recurse into subroutine protos. */ + LUAJIT_MODE_ALLSUBFUNC, /* Change only the subroutines. */ + + LUAJIT_MODE_TRACE, /* Flush a compiled trace. */ + + LUAJIT_MODE_WRAPCFUNC = 0x10, /* Set wrapper mode for C function calls. */ + + LUAJIT_MODE_MAX +}; + +/* Flags or'ed in to the mode. */ +#define LUAJIT_MODE_OFF 0x0000 /* Turn feature off. */ +#define LUAJIT_MODE_ON 0x0100 /* Turn feature on. */ +#define LUAJIT_MODE_FLUSH 0x0200 /* Flush JIT-compiled code. */ + +/* LuaJIT public C API. */ + +/* Control the JIT engine. */ +LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode); + +/* Enforce (dynamic) linker error for version mismatches. Call from main. */ +LUA_API void LUAJIT_VERSION_SYM(void); + +#endif diff --git a/external/lua/luajit/src/src/lualib.h b/external/lua/luajit/src/src/lualib.h new file mode 100644 index 0000000000..18c6234da7 --- /dev/null +++ b/external/lua/luajit/src/src/lualib.h @@ -0,0 +1,43 @@ +/* +** Standard library header. +** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +*/ + +#ifndef _LUALIB_H +#define _LUALIB_H + +#include "lua.h" + +#define LUA_FILEHANDLE "FILE*" + +#define LUA_COLIBNAME "coroutine" +#define LUA_MATHLIBNAME "math" +#define LUA_STRLIBNAME "string" +#define LUA_TABLIBNAME "table" +#define LUA_IOLIBNAME "io" +#define LUA_OSLIBNAME "os" +#define LUA_LOADLIBNAME "package" +#define LUA_DBLIBNAME "debug" +#define LUA_BITLIBNAME "bit" +#define LUA_JITLIBNAME "jit" +#define LUA_FFILIBNAME "ffi" + +LUALIB_API int luaopen_base(lua_State *L); +LUALIB_API int luaopen_math(lua_State *L); +LUALIB_API int luaopen_string(lua_State *L); +LUALIB_API int luaopen_table(lua_State *L); +LUALIB_API int luaopen_io(lua_State *L); +LUALIB_API int luaopen_os(lua_State *L); +LUALIB_API int luaopen_package(lua_State *L); +LUALIB_API int luaopen_debug(lua_State *L); +LUALIB_API int luaopen_bit(lua_State *L); +LUALIB_API int luaopen_jit(lua_State *L); +LUALIB_API int luaopen_ffi(lua_State *L); + +LUALIB_API void luaL_openlibs(lua_State *L); + +#ifndef lua_assert +#define lua_assert(x) ((void)0) +#endif + +#endif diff --git a/external/lua/luajit/src/src/msvcbuild.bat b/external/lua/luajit/src/src/msvcbuild.bat new file mode 100644 index 0000000000..c69c6b33db --- /dev/null +++ b/external/lua/luajit/src/src/msvcbuild.bat @@ -0,0 +1,111 @@ +@rem Script to build LuaJIT with MSVC. +@rem Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h +@rem +@rem Either open a "Visual Studio .NET Command Prompt" +@rem (Note that the Express Edition does not contain an x64 compiler) +@rem -or- +@rem Open a "Windows SDK Command Shell" and set the compiler environment: +@rem setenv /release /x86 +@rem -or- +@rem setenv /release /x64 +@rem +@rem Then cd to this directory and run this script. + +@if not defined INCLUDE goto :FAIL + +@setlocal +@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE +@set LJLINK=link /nologo +@set LJMT=mt /nologo +@set LJLIB=lib /nologo +@set DASMDIR=..\dynasm +@set DASM=%DASMDIR%\dynasm.lua +@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c + +%LJCOMPILE% host\minilua.c +@if errorlevel 1 goto :BAD +%LJLINK% /out:minilua.exe minilua.obj +@if errorlevel 1 goto :BAD +if exist minilua.exe.manifest^ + %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe + +@set DASMFLAGS=-D WIN -D JIT -D FFI -D P64 +@set LJARCH=x64 +@minilua +@if errorlevel 8 goto :X64 +@set DASMFLAGS=-D WIN -D JIT -D FFI +@set LJARCH=x86 +:X64 +minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_x86.dasc +@if errorlevel 1 goto :BAD + +%LJCOMPILE% /I "." /I %DASMDIR% host\buildvm*.c +@if errorlevel 1 goto :BAD +%LJLINK% /out:buildvm.exe buildvm*.obj +@if errorlevel 1 goto :BAD +if exist buildvm.exe.manifest^ + %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe + +buildvm -m peobj -o lj_vm.obj +@if errorlevel 1 goto :BAD +buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m libdef -o lj_libdef.h %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m recdef -o lj_recdef.h %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m folddef -o lj_folddef.h lj_opt_fold.c +@if errorlevel 1 goto :BAD + +@if "%1" neq "debug" goto :NODEBUG +@shift +@set LJCOMPILE=%LJCOMPILE% /Zi +@set LJLINK=%LJLINK% /debug +:NODEBUG +@if "%1"=="amalg" goto :AMALGDLL +@if "%1"=="static" goto :STATIC +%LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c +@if errorlevel 1 goto :BAD +%LJLINK% /DLL /out:lua51.dll lj_*.obj lib_*.obj +@if errorlevel 1 goto :BAD +@goto :MTDLL +:STATIC +%LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c +@if errorlevel 1 goto :BAD +%LJLIB% /OUT:lua51.lib lj_*.obj lib_*.obj +@if errorlevel 1 goto :BAD +@goto :MTDLL +:AMALGDLL +%LJCOMPILE% /DLUA_BUILD_AS_DLL ljamalg.c +@if errorlevel 1 goto :BAD +%LJLINK% /DLL /out:lua51.dll ljamalg.obj lj_vm.obj +@if errorlevel 1 goto :BAD +:MTDLL +if exist lua51.dll.manifest^ + %LJMT% -manifest lua51.dll.manifest -outputresource:lua51.dll;2 + +%LJCOMPILE% luajit.c +@if errorlevel 1 goto :BAD +%LJLINK% /out:luajit.exe luajit.obj lua51.lib +@if errorlevel 1 goto :BAD +if exist luajit.exe.manifest^ + %LJMT% -manifest luajit.exe.manifest -outputresource:luajit.exe + +@del *.obj *.manifest minilua.exe buildvm.exe +@echo. +@echo === Successfully built LuaJIT for Windows/%LJARCH% === + +@goto :END +:BAD +@echo. +@echo ******************************************************* +@echo *** Build FAILED -- Please check the error messages *** +@echo ******************************************************* +@goto :END +:FAIL +@echo You must open a "Visual Studio .NET Command Prompt" to run this script +:END diff --git a/external/lua/luajit/src/src/vm_arm.dasc.REMOVED.git-id b/external/lua/luajit/src/src/vm_arm.dasc.REMOVED.git-id new file mode 100644 index 0000000000..c75b510e75 --- /dev/null +++ b/external/lua/luajit/src/src/vm_arm.dasc.REMOVED.git-id @@ -0,0 +1 @@ +114416a46a0f941844710ba9e0a7a67e40aa89fc \ No newline at end of file diff --git a/external/lua/luajit/src/src/vm_mips.dasc.REMOVED.git-id b/external/lua/luajit/src/src/vm_mips.dasc.REMOVED.git-id new file mode 100644 index 0000000000..d95d3ef750 --- /dev/null +++ b/external/lua/luajit/src/src/vm_mips.dasc.REMOVED.git-id @@ -0,0 +1 @@ +a81dbeebf71bd5e3db84a5aef10d7294190c0a38 \ No newline at end of file diff --git a/external/lua/luajit/src/src/vm_ppc.dasc.REMOVED.git-id b/external/lua/luajit/src/src/vm_ppc.dasc.REMOVED.git-id new file mode 100644 index 0000000000..8ccd6bb33f --- /dev/null +++ b/external/lua/luajit/src/src/vm_ppc.dasc.REMOVED.git-id @@ -0,0 +1 @@ +685ea5180baa7de783d684306de74dce021689d2 \ No newline at end of file diff --git a/external/lua/luajit/src/src/vm_ppcspe.dasc.REMOVED.git-id b/external/lua/luajit/src/src/vm_ppcspe.dasc.REMOVED.git-id new file mode 100644 index 0000000000..430371bd2d --- /dev/null +++ b/external/lua/luajit/src/src/vm_ppcspe.dasc.REMOVED.git-id @@ -0,0 +1 @@ +4fabc02f10e9a6d736f29440bbbc777568e1cc40 \ No newline at end of file diff --git a/external/lua/luajit/src/src/vm_x86.dasc.REMOVED.git-id b/external/lua/luajit/src/src/vm_x86.dasc.REMOVED.git-id new file mode 100644 index 0000000000..e446a1b271 --- /dev/null +++ b/external/lua/luajit/src/src/vm_x86.dasc.REMOVED.git-id @@ -0,0 +1 @@ +b4674e2ba508a96f267cc5e55b5deb92402763e6 \ No newline at end of file diff --git a/external/lua/luajit/src/src/xedkbuild.bat b/external/lua/luajit/src/src/xedkbuild.bat new file mode 100644 index 0000000000..bcc01e069a --- /dev/null +++ b/external/lua/luajit/src/src/xedkbuild.bat @@ -0,0 +1,92 @@ +@rem Script to build LuaJIT with the Xbox 360 SDK. +@rem Donated to the public domain. +@rem +@rem Open a "Visual Studio .NET Command Prompt" (32 bit host compiler) +@rem Then cd to this directory and run this script. + +@if not defined INCLUDE goto :FAIL +@if not defined XEDK goto :FAIL + +@setlocal +@rem ---- Host compiler ---- +@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE +@set LJLINK=link /nologo +@set LJMT=mt /nologo +@set DASMDIR=..\dynasm +@set DASM=%DASMDIR%\dynasm.lua +@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c + +%LJCOMPILE% host\minilua.c +@if errorlevel 1 goto :BAD +%LJLINK% /out:minilua.exe minilua.obj +@if errorlevel 1 goto :BAD +if exist minilua.exe.manifest^ + %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe + +@rem Error out for 64 bit host compiler +@minilua +@if errorlevel 8 goto :FAIL + +@set DASMFLAGS=-D GPR64 -D FRAME32 -D PPE -D SQRT -D DUALNUM +minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h vm_ppc.dasc +@if errorlevel 1 goto :BAD + +%LJCOMPILE% /I "." /I %DASMDIR% /D_XBOX_VER=200 /DLUAJIT_TARGET=LUAJIT_ARCH_PPC host\buildvm*.c +@if errorlevel 1 goto :BAD +%LJLINK% /out:buildvm.exe buildvm*.obj +@if errorlevel 1 goto :BAD +if exist buildvm.exe.manifest^ + %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe + +buildvm -m peobj -o lj_vm.obj +@if errorlevel 1 goto :BAD +buildvm -m bcdef -o lj_bcdef.h %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m ffdef -o lj_ffdef.h %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m libdef -o lj_libdef.h %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m recdef -o lj_recdef.h %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB% +@if errorlevel 1 goto :BAD +buildvm -m folddef -o lj_folddef.h lj_opt_fold.c +@if errorlevel 1 goto :BAD + +@rem ---- Cross compiler ---- +@set LJCOMPILE="%XEDK%\bin\win32\cl" /nologo /c /MT /O2 /W3 /GF /Gm- /GR- /GS- /Gy /openmp- /D_CRT_SECURE_NO_DEPRECATE /DNDEBUG /D_XBOX /D_LIB /DLUAJIT_USE_SYSMALLOC +@set LJLIB="%XEDK%\bin\win32\lib" /nologo +@set INCLUDE="%XEDK%\include\xbox" + +@if "%1" neq "debug" goto :NODEBUG +@shift +@set LJCOMPILE="%LJCOMPILE%" /Zi +:NODEBUG +@if "%1"=="amalg" goto :AMALG +%LJCOMPILE% /DLUA_BUILD_AS_DLL lj_*.c lib_*.c +@if errorlevel 1 goto :BAD +%LJLIB% /OUT:luajit20.lib lj_*.obj lib_*.obj +@if errorlevel 1 goto :BAD +@goto :NOAMALG +:AMALG +%LJCOMPILE% /DLUA_BUILD_AS_DLL ljamalg.c +@if errorlevel 1 goto :BAD +%LJLIB% /OUT:luajit20.lib ljamalg.obj lj_vm.obj +@if errorlevel 1 goto :BAD +:NOAMALG + +@del *.obj *.manifest minilua.exe buildvm.exe +@echo. +@echo === Successfully built LuaJIT for Xbox 360 === + +@goto :END +:BAD +@echo. +@echo ******************************************************* +@echo *** Build FAILED -- Please check the error messages *** +@echo ******************************************************* +@goto :END +:FAIL +@echo To run this script you must open a "Visual Studio .NET Command Prompt" +@echo (32 bit host compiler). The Xbox 360 SDK must be installed, too. +:END diff --git a/external/lua/luajit/src/v2.0.1_hotfix1.patch b/external/lua/luajit/src/v2.0.1_hotfix1.patch new file mode 100644 index 0000000000..0601cd9845 --- /dev/null +++ b/external/lua/luajit/src/v2.0.1_hotfix1.patch @@ -0,0 +1,103 @@ +Add missing GC steps for io.* functions. +MIPS: Fix cache flush/sync for JIT-compiled code jump area. +ARM: Fix cache flush/sync for exit stubs of JIT-compiled code. +Fix MSVC intrinsics for older versions. +Fix memory access check for fast string interning. + +--- a/src/lib_io.c ++++ b/src/lib_io.c +@@ -17,6 +17,7 @@ + #include "lualib.h" + + #include "lj_obj.h" ++#include "lj_gc.h" + #include "lj_err.h" + #include "lj_str.h" + #include "lj_state.h" +@@ -152,6 +153,7 @@ static int io_file_readline(lua_State *L, FILE *fp, MSize chop) + if (n >= m - 64) m += m; + } + setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); ++ lj_gc_check(L); + return (int)ok; + } + +@@ -163,6 +165,7 @@ static void io_file_readall(lua_State *L, FILE *fp) + n += (MSize)fread(buf+n, 1, m-n, fp); + if (n != m) { + setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); ++ lj_gc_check(L); + return; + } + } +@@ -174,6 +177,7 @@ static int io_file_readlen(lua_State *L, FILE *fp, MSize m) + char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, m); + MSize n = (MSize)fread(buf, 1, m, fp); + setstrV(L, L->top++, lj_str_new(L, buf, (size_t)n)); ++ lj_gc_check(L); + return (n > 0 || m == 0); + } else { + int c = getc(fp); +--- a/src/lj_asm_arm.h ++++ b/src/lj_asm_arm.h +@@ -91,6 +91,7 @@ static MCode *asm_exitstub_gen(ASMState *as, ExitNo group) + *mxp++ = group*EXITSTUBS_PER_GROUP; + for (i = 0; i < EXITSTUBS_PER_GROUP; i++) + *mxp++ = ARMI_B|((-6-i)&0x00ffffffu); ++ lj_mcode_sync(as->mcbot, mxp); + lj_mcode_commitbot(as->J, mxp); + as->mcbot = mxp; + as->mclim = as->mcbot + MCLIM_REDZONE; +--- a/src/lj_asm_mips.h ++++ b/src/lj_asm_mips.h +@@ -71,6 +71,7 @@ static void asm_sparejump_setup(ASMState *as) + memset(mxp+2, 0, MIPS_SPAREJUMP*8); + mxp += MIPS_SPAREJUMP*2; + lua_assert(mxp < as->mctop); ++ lj_mcode_sync(as->mcbot, mxp); + lj_mcode_commitbot(as->J, mxp); + as->mcbot = mxp; + as->mclim = as->mcbot + MCLIM_REDZONE; +--- a/src/lj_def.h ++++ b/src/lj_def.h +@@ -243,17 +243,17 @@ static LJ_AINLINE uint32_t lj_getu32(const void *p) + #endif + + #ifdef _M_PPC +-#pragma intrinsic(_CountLeadingZeros) + unsigned int _CountLeadingZeros(long); ++#pragma intrinsic(_CountLeadingZeros) + static LJ_AINLINE uint32_t lj_fls(uint32_t x) + { + return _CountLeadingZeros(x) ^ 31; + } + #else +-#pragma intrinsic(_BitScanForward) +-#pragma intrinsic(_BitScanReverse) + unsigned char _BitScanForward(uint32_t *, unsigned long); + unsigned char _BitScanReverse(uint32_t *, unsigned long); ++#pragma intrinsic(_BitScanForward) ++#pragma intrinsic(_BitScanReverse) + + static LJ_AINLINE uint32_t lj_ffs(uint32_t x) + { +--- a/src/lj_str.c ++++ b/src/lj_str.c +@@ -48,7 +48,7 @@ static LJ_AINLINE int str_fastcmp(const char *a, const char *b, MSize len) + { + MSize i = 0; + lua_assert(len > 0); +- lua_assert((((uintptr_t)a + len) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4); ++ lua_assert((((uintptr_t)a+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4); + do { /* Note: innocuous access up to end of string + 3. */ + uint32_t v = lj_getu32(a+i) ^ *(const uint32_t *)(b+i); + if (v) { +@@ -121,7 +121,7 @@ GCstr *lj_str_new(lua_State *L, const char *str, size_t lenx) + h ^= b; h -= lj_rol(b, 16); + /* Check if the string has already been interned. */ + o = gcref(g->strhash[h & g->strmask]); +- if (LJ_LIKELY((((uintptr_t)str + len) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4)) { ++ if (LJ_LIKELY((((uintptr_t)str+len-1) & (LJ_PAGESIZE-1)) <= LJ_PAGESIZE-4)) { + while (o != NULL) { + GCstr *sx = gco2str(o); + if (sx->len == len && str_fastcmp(str, strdata(sx), len) == 0) { diff --git a/external/lua/luasocket/auxiliar.c b/external/lua/luasocket/auxiliar.c new file mode 100644 index 0000000000..de625e9d74 --- /dev/null +++ b/external/lua/luasocket/auxiliar.c @@ -0,0 +1,158 @@ +/*=========================================================================*\ +* Auxiliar routines for class hierarchy manipulation +* LuaSocket toolkit +\*=========================================================================*/ +#include +#include + +#include "auxiliar.h" + +/*=========================================================================*\ +* Exported functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Initializes the module +\*-------------------------------------------------------------------------*/ +int auxiliar_open(lua_State *L) { + (void) L; + return 0; +} + +/*-------------------------------------------------------------------------*\ +* Creates a new class with given methods +* Methods whose names start with __ are passed directly to the metatable. +\*-------------------------------------------------------------------------*/ +void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func) { + luaL_newmetatable(L, classname); /* mt */ + /* create __index table to place methods */ + lua_pushstring(L, "__index"); /* mt,"__index" */ + lua_newtable(L); /* mt,"__index",it */ + /* put class name into class metatable */ + lua_pushstring(L, "class"); /* mt,"__index",it,"class" */ + lua_pushstring(L, classname); /* mt,"__index",it,"class",classname */ + lua_rawset(L, -3); /* mt,"__index",it */ + /* pass all methods that start with _ to the metatable, and all others + * to the index table */ + for (; func->name; func++) { /* mt,"__index",it */ + lua_pushstring(L, func->name); + lua_pushcfunction(L, func->func); + lua_rawset(L, func->name[0] == '_' ? -5: -3); + } + lua_rawset(L, -3); /* mt */ + lua_pop(L, 1); +} + +/*-------------------------------------------------------------------------*\ +* Prints the value of a class in a nice way +\*-------------------------------------------------------------------------*/ +int auxiliar_tostring(lua_State *L) { + char buf[32]; + if (!lua_getmetatable(L, 1)) goto error; + lua_pushstring(L, "__index"); + lua_gettable(L, -2); + if (!lua_istable(L, -1)) goto error; + lua_pushstring(L, "class"); + lua_gettable(L, -2); + if (!lua_isstring(L, -1)) goto error; + sprintf(buf, "%p", lua_touserdata(L, 1)); + lua_pushfstring(L, "%s: %s", lua_tostring(L, -1), buf); + return 1; +error: + lua_pushstring(L, "invalid object passed to 'auxiliar.c:__tostring'"); + lua_error(L); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Insert class into group +\*-------------------------------------------------------------------------*/ +void auxiliar_add2group(lua_State *L, const char *classname, const char *groupname) { + luaL_getmetatable(L, classname); + lua_pushstring(L, groupname); + lua_pushboolean(L, 1); + lua_rawset(L, -3); + lua_pop(L, 1); +} + +/*-------------------------------------------------------------------------*\ +* Make sure argument is a boolean +\*-------------------------------------------------------------------------*/ +int auxiliar_checkboolean(lua_State *L, int objidx) { + if (!lua_isboolean(L, objidx)) + auxiliar_typeerror(L, objidx, lua_typename(L, LUA_TBOOLEAN)); + return lua_toboolean(L, objidx); +} + +/*-------------------------------------------------------------------------*\ +* Return userdata pointer if object belongs to a given class, abort with +* error otherwise +\*-------------------------------------------------------------------------*/ +void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx) { + void *data = auxiliar_getclassudata(L, classname, objidx); + if (!data) { + char msg[45]; + sprintf(msg, "%.35s expected", classname); + luaL_argerror(L, objidx, msg); + } + return data; +} + +/*-------------------------------------------------------------------------*\ +* Return userdata pointer if object belongs to a given group, abort with +* error otherwise +\*-------------------------------------------------------------------------*/ +void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx) { + void *data = auxiliar_getgroupudata(L, groupname, objidx); + if (!data) { + char msg[45]; + sprintf(msg, "%.35s expected", groupname); + luaL_argerror(L, objidx, msg); + } + return data; +} + +/*-------------------------------------------------------------------------*\ +* Set object class +\*-------------------------------------------------------------------------*/ +void auxiliar_setclass(lua_State *L, const char *classname, int objidx) { + luaL_getmetatable(L, classname); + if (objidx < 0) objidx--; + lua_setmetatable(L, objidx); +} + +/*-------------------------------------------------------------------------*\ +* Get a userdata pointer if object belongs to a given group. Return NULL +* otherwise +\*-------------------------------------------------------------------------*/ +void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx) { + if (!lua_getmetatable(L, objidx)) + return NULL; + lua_pushstring(L, groupname); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) { + lua_pop(L, 2); + return NULL; + } else { + lua_pop(L, 2); + return lua_touserdata(L, objidx); + } +} + +/*-------------------------------------------------------------------------*\ +* Get a userdata pointer if object belongs to a given class. Return NULL +* otherwise +\*-------------------------------------------------------------------------*/ +void *auxiliar_getclassudata(lua_State *L, const char *classname, int objidx) { + return luaL_checkudata(L, objidx, classname); +} + +/*-------------------------------------------------------------------------*\ +* Throws error when argument does not have correct type. +* Used to be part of lauxlib in Lua 5.1, was dropped from 5.2. +\*-------------------------------------------------------------------------*/ +int auxiliar_typeerror (lua_State *L, int narg, const char *tname) { + const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, + luaL_typename(L, narg)); + return luaL_argerror(L, narg, msg); +} + diff --git a/external/lua/luasocket/auxiliar.h b/external/lua/luasocket/auxiliar.h new file mode 100644 index 0000000000..ea9901333d --- /dev/null +++ b/external/lua/luasocket/auxiliar.h @@ -0,0 +1,47 @@ +#ifndef AUXILIAR_H +#define AUXILIAR_H +/*=========================================================================*\ +* Auxiliar routines for class hierarchy manipulation +* LuaSocket toolkit (but completely independent of other LuaSocket modules) +* +* A LuaSocket class is a name associated with Lua metatables. A LuaSocket +* group is a name associated with a class. A class can belong to any number +* of groups. This module provides the functionality to: +* +* - create new classes +* - add classes to groups +* - set the class of objects +* - check if an object belongs to a given class or group +* - get the userdata associated to objects +* - print objects in a pretty way +* +* LuaSocket class names follow the convention {}. Modules +* can define any number of classes and groups. The module tcp.c, for +* example, defines the classes tcp{master}, tcp{client} and tcp{server} and +* the groups tcp{client,server} and tcp{any}. Module functions can then +* perform type-checking on their arguments by either class or group. +* +* LuaSocket metatables define the __index metamethod as being a table. This +* table has one field for each method supported by the class, and a field +* "class" with the class name. +* +* The mapping from class name to the corresponding metatable and the +* reverse mapping are done using lauxlib. +\*=========================================================================*/ + +#include "lua.h" +#include "lauxlib.h" + +int auxiliar_open(lua_State *L); +void auxiliar_newclass(lua_State *L, const char *classname, luaL_Reg *func); +void auxiliar_add2group(lua_State *L, const char *classname, const char *group); +void auxiliar_setclass(lua_State *L, const char *classname, int objidx); +void *auxiliar_checkclass(lua_State *L, const char *classname, int objidx); +void *auxiliar_checkgroup(lua_State *L, const char *groupname, int objidx); +void *auxiliar_getclassudata(lua_State *L, const char *groupname, int objidx); +void *auxiliar_getgroupudata(lua_State *L, const char *groupname, int objidx); +int auxiliar_checkboolean(lua_State *L, int objidx); +int auxiliar_tostring(lua_State *L); +int auxiliar_typeerror(lua_State *L, int narg, const char *tname); + +#endif /* AUXILIAR_H */ diff --git a/external/lua/luasocket/except.c b/external/lua/luasocket/except.c new file mode 100644 index 0000000000..1d1ade0ee0 --- /dev/null +++ b/external/lua/luasocket/except.c @@ -0,0 +1,97 @@ +/*=========================================================================*\ +* Simple exception support +* LuaSocket toolkit +\*=========================================================================*/ +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "except.h" + +/*=========================================================================*\ +* Internal function prototypes. +\*=========================================================================*/ +static int global_protect(lua_State *L); +static int global_newtry(lua_State *L); +static int protected_(lua_State *L); +static int finalize(lua_State *L); +static int do_nothing(lua_State *L); + +/* except functions */ +static luaL_Reg func[] = { + {"newtry", global_newtry}, + {"protect", global_protect}, + {NULL, NULL} +}; + +/*-------------------------------------------------------------------------*\ +* Try factory +\*-------------------------------------------------------------------------*/ +static void wrap(lua_State *L) { + lua_newtable(L); + lua_pushnumber(L, 1); + lua_pushvalue(L, -3); + lua_settable(L, -3); + lua_insert(L, -2); + lua_pop(L, 1); +} + +static int finalize(lua_State *L) { + if (!lua_toboolean(L, 1)) { + lua_pushvalue(L, lua_upvalueindex(1)); + lua_pcall(L, 0, 0, 0); + lua_settop(L, 2); + wrap(L); + lua_error(L); + return 0; + } else return lua_gettop(L); +} + +static int do_nothing(lua_State *L) { + (void) L; + return 0; +} + +static int global_newtry(lua_State *L) { + lua_settop(L, 1); + if (lua_isnil(L, 1)) lua_pushcfunction(L, do_nothing); + lua_pushcclosure(L, finalize, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Protect factory +\*-------------------------------------------------------------------------*/ +static int unwrap(lua_State *L) { + if (lua_istable(L, -1)) { + lua_pushnumber(L, 1); + lua_gettable(L, -2); + lua_pushnil(L); + lua_insert(L, -2); + return 1; + } else return 0; +} + +static int protected_(lua_State *L) { + lua_pushvalue(L, lua_upvalueindex(1)); + lua_insert(L, 1); + if (lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0) != 0) { + if (unwrap(L)) return 2; + else lua_error(L); + return 0; + } else return lua_gettop(L); +} + +static int global_protect(lua_State *L) { + lua_pushcclosure(L, protected_, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Init module +\*-------------------------------------------------------------------------*/ +int except_open(lua_State *L) { + luaL_openlib(L, NULL, func, 0); + return 0; +} diff --git a/external/lua/luasocket/except.h b/external/lua/luasocket/except.h new file mode 100644 index 0000000000..1e7a2455e7 --- /dev/null +++ b/external/lua/luasocket/except.h @@ -0,0 +1,33 @@ +#ifndef EXCEPT_H +#define EXCEPT_H +/*=========================================================================*\ +* Exception control +* LuaSocket toolkit (but completely independent from other modules) +* +* This provides support for simple exceptions in Lua. During the +* development of the HTTP/FTP/SMTP support, it became aparent that +* error checking was taking a substantial amount of the coding. These +* function greatly simplify the task of checking errors. +* +* The main idea is that functions should return nil as its first return +* value when it finds an error, and return an error message (or value) +* following nil. In case of success, as long as the first value is not nil, +* the other values don't matter. +* +* The idea is to nest function calls with the "try" function. This function +* checks the first value, and calls "error" on the second if the first is +* nil. Otherwise, it returns all values it received. +* +* The protect function returns a new function that behaves exactly like the +* function it receives, but the new function doesn't throw exceptions: it +* returns nil followed by the error message instead. +* +* With these two function, it's easy to write functions that throw +* exceptions on error, but that don't interrupt the user script. +\*=========================================================================*/ + +#include "lua.h" + +int except_open(lua_State *L); + +#endif diff --git a/external/lua/luasocket/inet.c b/external/lua/luasocket/inet.c new file mode 100644 index 0000000000..16f709387c --- /dev/null +++ b/external/lua/luasocket/inet.c @@ -0,0 +1,528 @@ +/*=========================================================================*\ +* Internet domain functions +* LuaSocket toolkit +\*=========================================================================*/ +#include +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "inet.h" + +/*=========================================================================*\ +* Internal function prototypes. +\*=========================================================================*/ +static int inet_global_toip(lua_State *L); +static int inet_global_getaddrinfo(lua_State *L); +static int inet_global_tohostname(lua_State *L); +static int inet_global_getnameinfo(lua_State *L); +static void inet_pushresolved(lua_State *L, struct hostent *hp); +static int inet_global_gethostname(lua_State *L); + +/* DNS functions */ +static luaL_Reg func[] = { + { "toip", inet_global_toip}, + { "getaddrinfo", inet_global_getaddrinfo}, + { "tohostname", inet_global_tohostname}, + { "getnameinfo", inet_global_getnameinfo}, + { "gethostname", inet_global_gethostname}, + { NULL, NULL} +}; + +#ifdef _WINDOWS_ +/****luodx patch for windows xp start**/ +char* win32xp_inet_ntop(int family, PVOID src, char* dest, size_t length) +{ + char* result = inet_ntoa(*(IN_ADDR*)src); + if (result != NULL){ + strcpy(dest, result); + } + return result; +} + +int win32xp_inet_pton(int family, const char* string, PVOID dest) { + return inet_aton(string, (IN_ADDR*)dest); +} +/****luodx patch for windows xp end**/ +#endif + + +/*=========================================================================*\ +* Exported functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +int inet_open(lua_State *L) +{ + lua_pushstring(L, "dns"); + lua_newtable(L); + luaL_openlib(L, NULL, func, 0); + lua_settable(L, -3); + return 0; +} + +/*=========================================================================*\ +* Global Lua functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Returns all information provided by the resolver given a host name +* or ip address +\*-------------------------------------------------------------------------*/ +static int inet_gethost(const char *address, struct hostent **hp) { + struct in_addr addr; + if (inet_aton(address, &addr)) + return socket_gethostbyaddr((char *) &addr, sizeof(addr), hp); + else + return socket_gethostbyname(address, hp); +} + +/*-------------------------------------------------------------------------*\ +* Returns all information provided by the resolver given a host name +* or ip address +\*-------------------------------------------------------------------------*/ +static int inet_global_tohostname(lua_State *L) { + const char *address = luaL_checkstring(L, 1); + struct hostent *hp = NULL; + int err = inet_gethost(address, &hp); + if (err != IO_DONE) { + lua_pushnil(L); + lua_pushstring(L, socket_hoststrerror(err)); + return 2; + } + lua_pushstring(L, hp->h_name); + inet_pushresolved(L, hp); + return 2; +} + +static int inet_global_getnameinfo(lua_State *L) { + int i, ret; + char host[1024]; + char serv[32]; + struct addrinfo hints; + struct addrinfo *resolved, *iter; + const char *node = luaL_optstring(L, 1, NULL); + const char *service = luaL_optstring(L, 2, NULL); + + if (!(node || service)) + luaL_error(L, "You have to specify a hostname, a service, or both"); + + memset(&hints, 0, sizeof(hints)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_family = PF_UNSPEC; + + /* getaddrinfo must get a node and a service argument */ + ret = getaddrinfo(node ? node : "127.0.0.1", service ? service : "7", + &hints, &resolved); + if (ret != 0) { + lua_pushnil(L); + lua_pushstring(L, socket_gaistrerror(ret)); + return 2; + } + + lua_newtable(L); + for (i = 1, iter = resolved; iter; i++, iter = iter->ai_next) { + getnameinfo(iter->ai_addr, (socklen_t) iter->ai_addrlen, host, + node ? (socklen_t) sizeof(host) : 0, serv, service ? (socklen_t) sizeof(serv) : 0, 0); + + if (node) { + lua_pushnumber(L, i); + lua_pushstring(L, host); + lua_settable(L, -3); + } + } + freeaddrinfo(resolved); + + if (service) { + lua_pushstring(L, serv); + return 2; + } else { + return 1; + } +} + +/*-------------------------------------------------------------------------*\ +* Returns all information provided by the resolver given a host name +* or ip address +\*-------------------------------------------------------------------------*/ +static int inet_global_toip(lua_State *L) +{ + const char *address = luaL_checkstring(L, 1); + struct hostent *hp = NULL; + int err = inet_gethost(address, &hp); + if (err != IO_DONE) { + lua_pushnil(L); + lua_pushstring(L, socket_hoststrerror(err)); + return 2; + } + lua_pushstring(L, inet_ntoa(*((struct in_addr *) hp->h_addr))); + inet_pushresolved(L, hp); + return 2; +} + +int inet_optfamily(lua_State* L, int narg, const char* def) +{ + static const char* optname[] = { "unspec", "inet", "inet6", NULL }; + static int optvalue[] = { PF_UNSPEC, PF_INET, PF_INET6, 0 }; + + return optvalue[luaL_checkoption(L, narg, def, optname)]; +} + +int inet_optsocktype(lua_State* L, int narg, const char* def) +{ + static const char* optname[] = { "stream", "dgram", NULL }; + static int optvalue[] = { SOCK_STREAM, SOCK_DGRAM, 0 }; + + return optvalue[luaL_checkoption(L, narg, def, optname)]; +} + +static int inet_global_getaddrinfo(lua_State *L) +{ + const char *hostname = luaL_checkstring(L, 1); + struct addrinfo *iterator = NULL, *resolved = NULL; + struct addrinfo hints; + int i = 1, ret = 0; + memset(&hints, 0, sizeof(hints)); + hints.ai_socktype = SOCK_STREAM; + hints.ai_family = PF_UNSPEC; + ret = getaddrinfo(hostname, NULL, &hints, &resolved); + if (ret != 0) { + lua_pushnil(L); + lua_pushstring(L, socket_gaistrerror(ret)); + return 2; + } + lua_newtable(L); + for (iterator = resolved; iterator; iterator = iterator->ai_next) { + char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; + getnameinfo(iterator->ai_addr, (socklen_t) iterator->ai_addrlen, hbuf, + (socklen_t) sizeof(hbuf), sbuf, 0, NI_NUMERICHOST); + lua_pushnumber(L, i); + lua_newtable(L); + switch (iterator->ai_family) { + case AF_INET: + lua_pushliteral(L, "family"); + lua_pushliteral(L, "inet"); + lua_settable(L, -3); + break; + case AF_INET6: + lua_pushliteral(L, "family"); + lua_pushliteral(L, "inet6"); + lua_settable(L, -3); + break;; + } + lua_pushliteral(L, "addr"); + lua_pushstring(L, hbuf); + lua_settable(L, -3); + lua_settable(L, -3); + i++; + } + freeaddrinfo(resolved); + return 1; +} + + +/*-------------------------------------------------------------------------*\ +* Gets the host name +\*-------------------------------------------------------------------------*/ +static int inet_global_gethostname(lua_State *L) +{ + char name[257]; + name[256] = '\0'; + if (gethostname(name, 256) < 0) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(errno)); + return 2; + } else { + lua_pushstring(L, name); + return 1; + } +} + + + +/*=========================================================================*\ +* Lua methods +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Retrieves socket peer name +\*-------------------------------------------------------------------------*/ +int inet_meth_getpeername(lua_State *L, p_socket ps, int family) +{ + switch (family) { + case PF_INET: { + struct sockaddr_in peer; + socklen_t peer_len = sizeof(peer); + char name[INET_ADDRSTRLEN]; + if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(errno)); + return 2; + } else { + my_inet_ntop(family, &peer.sin_addr, name, sizeof(name)); + lua_pushstring(L, name); + lua_pushnumber(L, ntohs(peer.sin_port)); + lua_pushliteral(L, "inet"); + return 3; + } + } + case PF_INET6: { + struct sockaddr_in6 peer; + socklen_t peer_len = sizeof(peer); + char name[INET6_ADDRSTRLEN]; + if (getpeername(*ps, (SA *) &peer, &peer_len) < 0) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(errno)); + return 2; + } else { + my_inet_ntop(family, &peer.sin6_addr, name, sizeof(name)); + lua_pushstring(L, name); + lua_pushnumber(L, ntohs(peer.sin6_port)); + lua_pushliteral(L, "inet6"); + return 3; + } + } + default: + lua_pushnil(L); + lua_pushfstring(L, "unknown family %d", family); + return 2; + } +} + +/*-------------------------------------------------------------------------*\ +* Retrieves socket local name +\*-------------------------------------------------------------------------*/ +int inet_meth_getsockname(lua_State *L, p_socket ps, int family) +{ + switch (family) { + case PF_INET: { + struct sockaddr_in local; + socklen_t local_len = sizeof(local); + char name[INET_ADDRSTRLEN]; + if (getsockname(*ps, (SA *) &local, &local_len) < 0) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(errno)); + return 2; + } else { + my_inet_ntop(family, &local.sin_addr, name, sizeof(name)); + lua_pushstring(L, name); + lua_pushnumber(L, ntohs(local.sin_port)); + lua_pushliteral(L, "inet"); + return 3; + } + } + case PF_INET6: { + struct sockaddr_in6 local; + socklen_t local_len = sizeof(local); + char name[INET6_ADDRSTRLEN]; + if (getsockname(*ps, (SA *) &local, &local_len) < 0) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(errno)); + return 2; + } else { + my_inet_ntop(family, &local.sin6_addr, name, sizeof(name)); + lua_pushstring(L, name); + lua_pushnumber(L, ntohs(local.sin6_port)); + lua_pushliteral(L, "inet6"); + return 3; + } + } + default: + lua_pushnil(L); + lua_pushfstring(L, "unknown family %d", family); + return 2; + } +} + +/*=========================================================================*\ +* Internal functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Passes all resolver information to Lua as a table +\*-------------------------------------------------------------------------*/ +static void inet_pushresolved(lua_State *L, struct hostent *hp) +{ + char **alias; + struct in_addr **addr; + int i, resolved; + lua_newtable(L); resolved = lua_gettop(L); + lua_pushstring(L, "name"); + lua_pushstring(L, hp->h_name); + lua_settable(L, resolved); + lua_pushstring(L, "ip"); + lua_pushstring(L, "alias"); + i = 1; + alias = hp->h_aliases; + lua_newtable(L); + if (alias) { + while (*alias) { + lua_pushnumber(L, i); + lua_pushstring(L, *alias); + lua_settable(L, -3); + i++; alias++; + } + } + lua_settable(L, resolved); + i = 1; + lua_newtable(L); + addr = (struct in_addr **) hp->h_addr_list; + if (addr) { + while (*addr) { + lua_pushnumber(L, i); + lua_pushstring(L, inet_ntoa(**addr)); + lua_settable(L, -3); + i++; addr++; + } + } + lua_settable(L, resolved); +} + +/*-------------------------------------------------------------------------*\ +* Tries to create a new inet socket +\*-------------------------------------------------------------------------*/ +const char *inet_trycreate(p_socket ps, int family, int type) { + return socket_strerror(socket_create(ps, family, type, 0)); +} + +/*-------------------------------------------------------------------------*\ +* "Disconnects" a DGRAM socket +\*-------------------------------------------------------------------------*/ +const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm) +{ + switch (family) { + case PF_INET: { + struct sockaddr_in sin; + memset((char *) &sin, 0, sizeof(sin)); + sin.sin_family = AF_UNSPEC; + sin.sin_addr.s_addr = INADDR_ANY; + return socket_strerror(socket_connect(ps, (SA *) &sin, + sizeof(sin), tm)); + } + case PF_INET6: { + struct sockaddr_in6 sin6; + struct in6_addr addrany = IN6ADDR_ANY_INIT; + memset((char *) &sin6, 0, sizeof(sin6)); + sin6.sin6_family = AF_UNSPEC; +fprintf(stderr, "disconnecting\n"); + sin6.sin6_addr = addrany; + return socket_strerror(socket_connect(ps, (SA *) &sin6, + sizeof(sin6), tm)); + } + } + return NULL; +} + +/*-------------------------------------------------------------------------*\ +* Tries to connect to remote address (address, port) +\*-------------------------------------------------------------------------*/ +const char *inet_tryconnect(p_socket ps, const char *address, + const char *serv, p_timeout tm, struct addrinfo *connecthints) +{ + struct addrinfo *iterator = NULL, *resolved = NULL; + const char *err = NULL; + /* try resolving */ + err = socket_gaistrerror(getaddrinfo(address, serv, + connecthints, &resolved)); + if (err != NULL) { + if (resolved) freeaddrinfo(resolved); + return err; + } + for (iterator = resolved; iterator; iterator = iterator->ai_next) { + timeout_markstart(tm); + /* try connecting to remote address */ + err = socket_strerror(socket_connect(ps, (SA *) iterator->ai_addr, + (socklen_t) iterator->ai_addrlen, tm)); + /* if success, break out of loop */ + if (err == NULL) break; + } + freeaddrinfo(resolved); + /* here, if err is set, we failed */ + return err; +} + +/*-------------------------------------------------------------------------*\ +* Tries to accept a socket +\*-------------------------------------------------------------------------*/ +const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm) +{ + socklen_t len; + t_sockaddr_storage addr; + if (family == PF_INET6) { + len = sizeof(struct sockaddr_in6); + } else { + len = sizeof(struct sockaddr_in); + } + return socket_strerror(socket_accept(server, client, (SA *) &addr, &len, tm)); +} + +/*-------------------------------------------------------------------------*\ +* Tries to bind socket to (address, port) +\*-------------------------------------------------------------------------*/ +const char *inet_trybind(p_socket ps, const char *address, const char *serv, + struct addrinfo *bindhints) +{ + struct addrinfo *iterator = NULL, *resolved = NULL; + const char *err = NULL; + t_socket sock = *ps; + /* try resolving */ + err = socket_gaistrerror(getaddrinfo(address, serv, bindhints, &resolved)); + if (err) { + if (resolved) freeaddrinfo(resolved); + return err; + } + /* iterate over resolved addresses until one is good */ + for (iterator = resolved; iterator; iterator = iterator->ai_next) { + if(sock == SOCKET_INVALID) { + err = socket_strerror(socket_create(&sock, iterator->ai_family, + iterator->ai_socktype, iterator->ai_protocol)); + if(err) + continue; + } + /* try binding to local address */ + err = socket_strerror(socket_bind(&sock, + (SA *) iterator->ai_addr, + (socklen_t) iterator->ai_addrlen)); + + /* keep trying unless bind succeeded */ + if (err) { + if(sock != *ps) + socket_destroy(&sock); + } else { + /* remember what we connected to, particularly the family */ + *bindhints = *iterator; + break; + } + } + /* cleanup and return error */ + freeaddrinfo(resolved); + *ps = sock; + return err; +} + +/*-------------------------------------------------------------------------*\ +* Some systems do not provide this so that we provide our own. It's not +* marvelously fast, but it works just fine. +\*-------------------------------------------------------------------------*/ +#ifdef INET_ATON +int inet_aton(const char *cp, struct in_addr *inp) +{ + unsigned int a = 0, b = 0, c = 0, d = 0; + int n = 0, r; + unsigned long int addr = 0; + r = sscanf(cp, "%u.%u.%u.%u%n", &a, &b, &c, &d, &n); + if (r == 0 || n == 0) return 0; + cp += n; + if (*cp) return 0; + if (a > 255 || b > 255 || c > 255 || d > 255) return 0; + if (inp) { + addr += a; addr <<= 8; + addr += b; addr <<= 8; + addr += c; addr <<= 8; + addr += d; + inp->s_addr = htonl(addr); + } + return 1; +} +#endif + + diff --git a/external/lua/luasocket/inet.h b/external/lua/luasocket/inet.h new file mode 100644 index 0000000000..9971002103 --- /dev/null +++ b/external/lua/luasocket/inet.h @@ -0,0 +1,56 @@ +#ifndef INET_H +#define INET_H +/*=========================================================================*\ +* Internet domain functions +* LuaSocket toolkit +* +* This module implements the creation and connection of internet domain +* sockets, on top of the socket.h interface, and the interface of with the +* resolver. +* +* The function inet_aton is provided for the platforms where it is not +* available. The module also implements the interface of the internet +* getpeername and getsockname functions as seen by Lua programs. +* +* The Lua functions toip and tohostname are also implemented here. +\*=========================================================================*/ +#include "lua.h" +#include "socket.h" +#include "timeout.h" + +#ifdef _WIN32 +#define INET_ATON +#endif + +int inet_open(lua_State *L); + +const char *inet_trycreate(p_socket ps, int family, int type); +const char *inet_tryconnect(p_socket ps, const char *address, + const char *serv, p_timeout tm, struct addrinfo *connecthints); +const char *inet_trybind(p_socket ps, const char *address, const char *serv, + struct addrinfo *bindhints); +const char *inet_trydisconnect(p_socket ps, int family, p_timeout tm); +const char *inet_tryaccept(p_socket server, int family, p_socket client, p_timeout tm); + +int inet_meth_getpeername(lua_State *L, p_socket ps, int family); +int inet_meth_getsockname(lua_State *L, p_socket ps, int family); + +int inet_optfamily(lua_State* L, int narg, const char* def); +int inet_optsocktype(lua_State* L, int narg, const char* def); + +#ifdef INET_ATON +int inet_aton(const char *cp, struct in_addr *inp); +#endif + +#ifndef _WINDOWS_ +#define my_inet_ntop(a,b,c,d) inet_ntop(a,b,c,d) +#define my_inet_pton(a,b,c) inet_pton(a,b,c) +#else +int win32xp_inet_pton(int family, const char* string, PVOID dest); +char* win32xp_inet_ntop(int family, PVOID src, char* dest, size_t length); +#define my_inet_ntop(a,b,c,d) win32xp_inet_ntop(a,b,c,d) +#define my_inet_pton(a,b,c) win32xp_inet_pton(a,b,c) +#endif + + +#endif /* INET_H */ diff --git a/external/lua/luasocket/luasocket.c b/external/lua/luasocket/luasocket.c new file mode 100644 index 0000000000..c86a3e86b0 --- /dev/null +++ b/external/lua/luasocket/luasocket.c @@ -0,0 +1,116 @@ +/*=========================================================================*\ +* LuaSocket toolkit +* Networking support for the Lua language +* Diego Nehab +* 26/11/1999 +* +* This library is part of an effort to progressively increase the network +* connectivity of the Lua language. The Lua interface to networking +* functions follows the Sockets API closely, trying to simplify all tasks +* involved in setting up both client and server connections. The provided +* IO routines, however, follow the Lua style, being very similar to the +* standard Lua read and write functions. +\*=========================================================================*/ + +/*=========================================================================*\ +* Standard include files +\*=========================================================================*/ +#include "lua.h" +#include "lauxlib.h" + +#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501) +#include "compat-5.1.h" +#endif + +/*=========================================================================*\ +* LuaSocket includes +\*=========================================================================*/ +#include "luasocket.h" +#include "auxiliar.h" +#include "except.h" +#include "timeout.h" +#include "luasocket_buffer.h" +#include "inet.h" +#include "tcp.h" +#include "udp.h" +#include "select.h" + +/*-------------------------------------------------------------------------*\ +* Internal function prototypes +\*-------------------------------------------------------------------------*/ +static int global_skip(lua_State *L); +static int global_unload(lua_State *L); +static int base_open(lua_State *L); + +/*-------------------------------------------------------------------------*\ +* Modules and functions +\*-------------------------------------------------------------------------*/ +static const luaL_Reg mod[] = { + {"auxiliar", auxiliar_open}, + {"except", except_open}, + {"timeout", timeout_open}, + {"buffer", buffer_open}, + {"inet", inet_open}, + {"tcp", tcp_open}, + {"udp", udp_open}, + {"select", select_open}, + {NULL, NULL} +}; + +static luaL_Reg func[] = { + {"skip", global_skip}, + {"__unload", global_unload}, + {NULL, NULL} +}; + +/*-------------------------------------------------------------------------*\ +* Skip a few arguments +\*-------------------------------------------------------------------------*/ +static int global_skip(lua_State *L) { + int amount = luaL_checkint(L, 1); + int ret = lua_gettop(L) - amount - 1; + return ret >= 0 ? ret : 0; +} + +/*-------------------------------------------------------------------------*\ +* Unloads the library +\*-------------------------------------------------------------------------*/ +static int global_unload(lua_State *L) { + (void) L; + socket_close(); + return 0; +} + +/*-------------------------------------------------------------------------*\ +* Setup basic stuff. +\*-------------------------------------------------------------------------*/ +static int base_open(lua_State *L) { + if (socket_open()) { + /* export functions (and leave namespace table on top of stack) */ + luaL_openlib(L, "socket", func, 0); +#ifdef LUASOCKET_DEBUG + lua_pushstring(L, "_DEBUG"); + lua_pushboolean(L, 1); + lua_rawset(L, -3); +#endif + /* make version string available to scripts */ + lua_pushstring(L, "_VERSION"); + lua_pushstring(L, LUASOCKET_VERSION); + lua_rawset(L, -3); + return 1; + } else { + lua_pushstring(L, "unable to initialize library"); + lua_error(L); + return 0; + } +} + +/*-------------------------------------------------------------------------*\ +* Initializes all library modules. +\*-------------------------------------------------------------------------*/ +LUASOCKET_API int luaopen_socket_core(lua_State *L) { + int i; + base_open(L); + for (i = 0; mod[i].name; i++) mod[i].func(L); + return 1; +} diff --git a/external/lua/luasocket/luasocket.h b/external/lua/luasocket/luasocket.h new file mode 100644 index 0000000000..f2ca3c159e --- /dev/null +++ b/external/lua/luasocket/luasocket.h @@ -0,0 +1,34 @@ +#ifndef LUASOCKET_H +#define LUASOCKET_H +/*=========================================================================*\ +* LuaSocket toolkit +* Networking support for the Lua language +* Diego Nehab +* 9/11/1999 +\*=========================================================================*/ +#include "lua.h" + +/*-------------------------------------------------------------------------*\ +* Current socket library version +\*-------------------------------------------------------------------------*/ +#define LUASOCKET_VERSION "LuaSocket 2.1-rc1" +#define LUASOCKET_COPYRIGHT "Copyright (C) 1999-2012 Diego Nehab" +#define LUASOCKET_AUTHORS "Diego Nehab" + +/*-------------------------------------------------------------------------*\ +* This macro prefixes all exported API functions +\*-------------------------------------------------------------------------*/ +#ifndef LUASOCKET_API +#define LUASOCKET_API extern +#endif + +#if LUA_VERSION_NUM > 501 & !( defined LUA_COMPAT_MODULE) +# error Lua 5.2 requires LUA_COMPAT_MODULE defined for luaL_openlib +#endif + +/*-------------------------------------------------------------------------*\ +* Initializes the library. +\*-------------------------------------------------------------------------*/ +LUASOCKET_API int luaopen_socket_core(lua_State *L); + +#endif /* LUASOCKET_H */ diff --git a/external/lua/luasocket/luasocket_buffer.c b/external/lua/luasocket/luasocket_buffer.c new file mode 100644 index 0000000000..13140bccc0 --- /dev/null +++ b/external/lua/luasocket/luasocket_buffer.c @@ -0,0 +1,276 @@ +/*=========================================================================*\ +* Input/Output interface for Lua programs +* LuaSocket toolkit +\*=========================================================================*/ +#include "lua.h" +#include "lauxlib.h" + +#include "luasocket_buffer.h" + +/*=========================================================================*\ +* Internal function prototypes +\*=========================================================================*/ +static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b); +static int recvline(p_buffer buf, luaL_Buffer *b); +static int recvall(p_buffer buf, luaL_Buffer *b); +static int buffer_get(p_buffer buf, const char **data, size_t *count); +static void buffer_skip(p_buffer buf, size_t count); +static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent); + +/* min and max macros */ +#ifndef MIN +#define MIN(x, y) ((x) < (y) ? x : y) +#endif +#ifndef MAX +#define MAX(x, y) ((x) > (y) ? x : y) +#endif + +/*=========================================================================*\ +* Exported functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +int buffer_open(lua_State *L) { + (void) L; + return 0; +} + +/*-------------------------------------------------------------------------*\ +* Initializes C structure +\*-------------------------------------------------------------------------*/ +void buffer_init(p_buffer buf, p_io io, p_timeout tm) { + buf->first = buf->last = 0; + buf->io = io; + buf->tm = tm; + buf->received = buf->sent = 0; + buf->birthday = timeout_gettime(); +} + +/*-------------------------------------------------------------------------*\ +* object:getstats() interface +\*-------------------------------------------------------------------------*/ +int buffer_meth_getstats(lua_State *L, p_buffer buf) { + lua_pushnumber(L, (lua_Number) buf->received); + lua_pushnumber(L, (lua_Number) buf->sent); + lua_pushnumber(L, timeout_gettime() - buf->birthday); + return 3; +} + +/*-------------------------------------------------------------------------*\ +* object:setstats() interface +\*-------------------------------------------------------------------------*/ +int buffer_meth_setstats(lua_State *L, p_buffer buf) { + buf->received = (long) luaL_optnumber(L, 2, (lua_Number) buf->received); + buf->sent = (long) luaL_optnumber(L, 3, (lua_Number) buf->sent); + if (lua_isnumber(L, 4)) buf->birthday = timeout_gettime() - lua_tonumber(L, 4); + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* object:send() interface +\*-------------------------------------------------------------------------*/ +int buffer_meth_send(lua_State *L, p_buffer buf) { + int top = lua_gettop(L); + int err = IO_DONE; + size_t size = 0, sent = 0; + const char *data = luaL_checklstring(L, 2, &size); + long start = (long) luaL_optnumber(L, 3, 1); + long end = (long) luaL_optnumber(L, 4, -1); +#ifdef LUASOCKET_DEBUG + p_timeout tm = timeout_markstart(buf->tm); +#endif + if (start < 0) start = (long) (size+start+1); + if (end < 0) end = (long) (size+end+1); + if (start < 1) start = (long) 1; + if (end > (long) size) end = (long) size; + if (start <= end) err = sendraw(buf, data+start-1, end-start+1, &sent); + /* check if there was an error */ + if (err != IO_DONE) { + lua_pushnil(L); + lua_pushstring(L, buf->io->error(buf->io->ctx, err)); + lua_pushnumber(L, (lua_Number) (sent+start-1)); + } else { + lua_pushnumber(L, (lua_Number) (sent+start-1)); + lua_pushnil(L); + lua_pushnil(L); + } +#ifdef LUASOCKET_DEBUG + /* push time elapsed during operation as the last return value */ + lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm)); +#endif + return lua_gettop(L) - top; +} + +/*-------------------------------------------------------------------------*\ +* object:receive() interface +\*-------------------------------------------------------------------------*/ +int buffer_meth_receive(lua_State *L, p_buffer buf) { + int err = IO_DONE, top = lua_gettop(L); + luaL_Buffer b; + size_t size; + const char *part = luaL_optlstring(L, 3, "", &size); +#ifdef LUASOCKET_DEBUG + p_timeout tm = timeout_markstart(buf->tm); +#endif + /* initialize buffer with optional extra prefix + * (useful for concatenating previous partial results) */ + luaL_buffinit(L, &b); + luaL_addlstring(&b, part, size); + /* receive new patterns */ + if (!lua_isnumber(L, 2)) { + const char *p= luaL_optstring(L, 2, "*l"); + if (p[0] == '*' && p[1] == 'l') err = recvline(buf, &b); + else if (p[0] == '*' && p[1] == 'a') err = recvall(buf, &b); + else luaL_argcheck(L, 0, 2, "invalid receive pattern"); + /* get a fixed number of bytes (minus what was already partially + * received) */ + } else { + double n = lua_tonumber(L, 2); + size_t wanted = (size_t) n; + luaL_argcheck(L, n >= 0, 2, "invalid receive pattern"); + if (size == 0 || wanted > size) + err = recvraw(buf, wanted-size, &b); + } + /* check if there was an error */ + if (err != IO_DONE) { + /* we can't push anyting in the stack before pushing the + * contents of the buffer. this is the reason for the complication */ + luaL_pushresult(&b); + lua_pushstring(L, buf->io->error(buf->io->ctx, err)); + lua_pushvalue(L, -2); + lua_pushnil(L); + lua_replace(L, -4); + } else { + luaL_pushresult(&b); + lua_pushnil(L); + lua_pushnil(L); + } +#ifdef LUASOCKET_DEBUG + /* push time elapsed during operation as the last return value */ + lua_pushnumber(L, timeout_gettime() - timeout_getstart(tm)); +#endif + return lua_gettop(L) - top; +} + +/*-------------------------------------------------------------------------*\ +* Determines if there is any data in the read buffer +\*-------------------------------------------------------------------------*/ +int buffer_isempty(p_buffer buf) { + return buf->first >= buf->last; +} + +/*=========================================================================*\ +* Internal functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Sends a block of data (unbuffered) +\*-------------------------------------------------------------------------*/ +#define STEPSIZE 8192 +static int sendraw(p_buffer buf, const char *data, size_t count, size_t *sent) { + p_io io = buf->io; + p_timeout tm = buf->tm; + size_t total = 0; + int err = IO_DONE; + while (total < count && err == IO_DONE) { + size_t done = 0; + size_t step = (count-total <= STEPSIZE)? count-total: STEPSIZE; + err = io->send(io->ctx, data+total, step, &done, tm); + total += done; + } + *sent = total; + buf->sent += total; + return err; +} + +/*-------------------------------------------------------------------------*\ +* Reads a fixed number of bytes (buffered) +\*-------------------------------------------------------------------------*/ +static int recvraw(p_buffer buf, size_t wanted, luaL_Buffer *b) { + int err = IO_DONE; + size_t total = 0; + while (err == IO_DONE) { + size_t count; const char *data; + err = buffer_get(buf, &data, &count); + count = MIN(count, wanted - total); + luaL_addlstring(b, data, count); + buffer_skip(buf, count); + total += count; + if (total >= wanted) break; + } + return err; +} + +/*-------------------------------------------------------------------------*\ +* Reads everything until the connection is closed (buffered) +\*-------------------------------------------------------------------------*/ +static int recvall(p_buffer buf, luaL_Buffer *b) { + int err = IO_DONE; + size_t total = 0; + while (err == IO_DONE) { + const char *data; size_t count; + err = buffer_get(buf, &data, &count); + total += count; + luaL_addlstring(b, data, count); + buffer_skip(buf, count); + } + if (err == IO_CLOSED) { + if (total > 0) return IO_DONE; + else return IO_CLOSED; + } else return err; +} + +/*-------------------------------------------------------------------------*\ +* Reads a line terminated by a CR LF pair or just by a LF. The CR and LF +* are not returned by the function and are discarded from the buffer +\*-------------------------------------------------------------------------*/ +static int recvline(p_buffer buf, luaL_Buffer *b) { + int err = IO_DONE; + while (err == IO_DONE) { + size_t count, pos; const char *data; + err = buffer_get(buf, &data, &count); + pos = 0; + while (pos < count && data[pos] != '\n') { + /* we ignore all \r's */ + if (data[pos] != '\r') luaL_addchar(b, data[pos]); + pos++; + } + if (pos < count) { /* found '\n' */ + buffer_skip(buf, pos+1); /* skip '\n' too */ + break; /* we are done */ + } else /* reached the end of the buffer */ + buffer_skip(buf, pos); + } + return err; +} + +/*-------------------------------------------------------------------------*\ +* Skips a given number of bytes from read buffer. No data is read from the +* transport layer +\*-------------------------------------------------------------------------*/ +static void buffer_skip(p_buffer buf, size_t count) { + buf->received += count; + buf->first += count; + if (buffer_isempty(buf)) + buf->first = buf->last = 0; +} + +/*-------------------------------------------------------------------------*\ +* Return any data available in buffer, or get more data from transport layer +* if buffer is empty +\*-------------------------------------------------------------------------*/ +static int buffer_get(p_buffer buf, const char **data, size_t *count) { + int err = IO_DONE; + p_io io = buf->io; + p_timeout tm = buf->tm; + if (buffer_isempty(buf)) { + size_t got; + err = io->recv(io->ctx, buf->data, BUF_SIZE, &got, tm); + buf->first = 0; + buf->last = got; + } + *count = buf->last - buf->first; + *data = buf->data + buf->first; + return err; +} diff --git a/external/lua/luasocket/luasocket_buffer.h b/external/lua/luasocket/luasocket_buffer.h new file mode 100644 index 0000000000..2456cd386b --- /dev/null +++ b/external/lua/luasocket/luasocket_buffer.h @@ -0,0 +1,45 @@ +#ifndef BUF_H +#define BUF_H +/*=========================================================================*\ +* Input/Output interface for Lua programs +* LuaSocket toolkit +* +* Line patterns require buffering. Reading one character at a time involves +* too many system calls and is very slow. This module implements the +* LuaSocket interface for input/output on connected objects, as seen by +* Lua programs. +* +* Input is buffered. Output is *not* buffered because there was no simple +* way of making sure the buffered output data would ever be sent. +* +* The module is built on top of the I/O abstraction defined in io.h and the +* timeout management is done with the timeout.h interface. +\*=========================================================================*/ +#include "lua.h" + +#include "luasocket_io.h" +#include "timeout.h" + +/* buffer size in bytes */ +#define BUF_SIZE 8192 + +/* buffer control structure */ +typedef struct t_buffer_ { + double birthday; /* throttle support info: creation time, */ + size_t sent, received; /* bytes sent, and bytes received */ + p_io io; /* IO driver used for this buffer */ + p_timeout tm; /* timeout management for this buffer */ + size_t first, last; /* index of first and last bytes of stored data */ + char data[BUF_SIZE]; /* storage space for buffer data */ +} t_buffer; +typedef t_buffer *p_buffer; + +int buffer_open(lua_State *L); +void buffer_init(p_buffer buf, p_io io, p_timeout tm); +int buffer_meth_send(lua_State *L, p_buffer buf); +int buffer_meth_receive(lua_State *L, p_buffer buf); +int buffer_meth_getstats(lua_State *L, p_buffer buf); +int buffer_meth_setstats(lua_State *L, p_buffer buf); +int buffer_isempty(p_buffer buf); + +#endif /* BUF_H */ diff --git a/external/lua/luasocket/luasocket_io.c b/external/lua/luasocket/luasocket_io.c new file mode 100644 index 0000000000..74722a5abf --- /dev/null +++ b/external/lua/luasocket/luasocket_io.c @@ -0,0 +1,30 @@ +/*=========================================================================*\ +* Input/Output abstraction +* LuaSocket toolkit +\*=========================================================================*/ +#include "luasocket_io.h" + +/*=========================================================================*\ +* Exported functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Initializes C structure +\*-------------------------------------------------------------------------*/ +void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx) { + io->send = send; + io->recv = recv; + io->error = error; + io->ctx = ctx; +} + +/*-------------------------------------------------------------------------*\ +* I/O error strings +\*-------------------------------------------------------------------------*/ +const char *io_strerror(int err) { + switch (err) { + case IO_DONE: return NULL; + case IO_CLOSED: return "closed"; + case IO_TIMEOUT: return "timeout"; + default: return "unknown error"; + } +} diff --git a/external/lua/luasocket/luasocket_io.h b/external/lua/luasocket/luasocket_io.h new file mode 100644 index 0000000000..8cca08a860 --- /dev/null +++ b/external/lua/luasocket/luasocket_io.h @@ -0,0 +1,65 @@ +#ifndef IO_H +#define IO_H +/*=========================================================================*\ +* Input/Output abstraction +* LuaSocket toolkit +* +* This module defines the interface that LuaSocket expects from the +* transport layer for streamed input/output. The idea is that if any +* transport implements this interface, then the buffer.c functions +* automatically work on it. +* +* The module socket.h implements this interface, and thus the module tcp.h +* is very simple. +\*=========================================================================*/ +#include +#include "lua.h" + +#include "timeout.h" + +/* IO error codes */ +enum { + IO_DONE = 0, /* operation completed successfully */ + IO_TIMEOUT = -1, /* operation timed out */ + IO_CLOSED = -2, /* the connection has been closed */ + IO_UNKNOWN = -3 +}; + +/* interface to error message function */ +typedef const char *(*p_error) ( + void *ctx, /* context needed by send */ + int err /* error code */ +); + +/* interface to send function */ +typedef int (*p_send) ( + void *ctx, /* context needed by send */ + const char *data, /* pointer to buffer with data to send */ + size_t count, /* number of bytes to send from buffer */ + size_t *sent, /* number of bytes sent uppon return */ + p_timeout tm /* timeout control */ +); + +/* interface to recv function */ +typedef int (*p_recv) ( + void *ctx, /* context needed by recv */ + char *data, /* pointer to buffer where data will be writen */ + size_t count, /* number of bytes to receive into buffer */ + size_t *got, /* number of bytes received uppon return */ + p_timeout tm /* timeout control */ +); + +/* IO driver definition */ +typedef struct t_io_ { + void *ctx; /* context needed by send/recv */ + p_send send; /* send function pointer */ + p_recv recv; /* receive function pointer */ + p_error error; /* strerror function */ +} t_io; +typedef t_io *p_io; + +void io_init(p_io io, p_send send, p_recv recv, p_error error, void *ctx); +const char *io_strerror(int err); + +#endif /* IO_H */ + diff --git a/external/lua/luasocket/mime.c b/external/lua/luasocket/mime.c new file mode 100644 index 0000000000..dddd3d66ea --- /dev/null +++ b/external/lua/luasocket/mime.c @@ -0,0 +1,723 @@ +/*=========================================================================*\ +* MIME support functions +* LuaSocket toolkit +\*=========================================================================*/ +#include + +#include "lua.h" +#include "lauxlib.h" + +#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501) +#include "compat-5.1.h" +#endif + +#include "mime.h" + +/*=========================================================================*\ +* Don't want to trust escape character constants +\*=========================================================================*/ +typedef unsigned char UC; +static const char CRLF[] = "\r\n"; +static const char EQCRLF[] = "=\r\n"; + +/*=========================================================================*\ +* Internal function prototypes. +\*=========================================================================*/ +static int mime_global_wrp(lua_State *L); +static int mime_global_b64(lua_State *L); +static int mime_global_unb64(lua_State *L); +static int mime_global_qp(lua_State *L); +static int mime_global_unqp(lua_State *L); +static int mime_global_qpwrp(lua_State *L); +static int mime_global_eol(lua_State *L); +static int mime_global_dot(lua_State *L); + +static size_t dot(int c, size_t state, luaL_Buffer *buffer); +static void b64setup(UC *base); +static size_t b64encode(UC c, UC *input, size_t size, luaL_Buffer *buffer); +static size_t b64pad(const UC *input, size_t size, luaL_Buffer *buffer); +static size_t b64decode(UC c, UC *input, size_t size, luaL_Buffer *buffer); + +static void qpsetup(UC *class, UC *unbase); +static void qpquote(UC c, luaL_Buffer *buffer); +static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer); +static size_t qpencode(UC c, UC *input, size_t size, + const char *marker, luaL_Buffer *buffer); +static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer); + +/* code support functions */ +static luaL_Reg func[] = { + { "dot", mime_global_dot }, + { "b64", mime_global_b64 }, + { "eol", mime_global_eol }, + { "qp", mime_global_qp }, + { "qpwrp", mime_global_qpwrp }, + { "unb64", mime_global_unb64 }, + { "unqp", mime_global_unqp }, + { "wrp", mime_global_wrp }, + { NULL, NULL } +}; + +/*-------------------------------------------------------------------------*\ +* Quoted-printable globals +\*-------------------------------------------------------------------------*/ +static UC qpclass[256]; +static UC qpbase[] = "0123456789ABCDEF"; +static UC qpunbase[256]; +enum {QP_PLAIN, QP_QUOTED, QP_CR, QP_IF_LAST}; + +/*-------------------------------------------------------------------------*\ +* Base64 globals +\*-------------------------------------------------------------------------*/ +static const UC b64base[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static UC b64unbase[256]; + +/*=========================================================================*\ +* Exported functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +MIME_API int luaopen_mime_core(lua_State *L) +{ + luaL_openlib(L, "mime", func, 0); + /* make version string available to scripts */ + lua_pushstring(L, "_VERSION"); + lua_pushstring(L, MIME_VERSION); + lua_rawset(L, -3); + /* initialize lookup tables */ + qpsetup(qpclass, qpunbase); + b64setup(b64unbase); + return 1; +} + +/*=========================================================================*\ +* Global Lua functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Incrementaly breaks a string into lines. The string can have CRLF breaks. +* A, n = wrp(l, B, length) +* A is a copy of B, broken into lines of at most 'length' bytes. +* 'l' is how many bytes are left for the first line of B. +* 'n' is the number of bytes left in the last line of A. +\*-------------------------------------------------------------------------*/ +static int mime_global_wrp(lua_State *L) +{ + size_t size = 0; + int left = (int) luaL_checknumber(L, 1); + const UC *input = (UC *) luaL_optlstring(L, 2, NULL, &size); + const UC *last = input + size; + int length = (int) luaL_optnumber(L, 3, 76); + luaL_Buffer buffer; + /* end of input black-hole */ + if (!input) { + /* if last line has not been terminated, add a line break */ + if (left < length) lua_pushstring(L, CRLF); + /* otherwise, we are done */ + else lua_pushnil(L); + lua_pushnumber(L, length); + return 2; + } + luaL_buffinit(L, &buffer); + while (input < last) { + switch (*input) { + case '\r': + break; + case '\n': + luaL_addstring(&buffer, CRLF); + left = length; + break; + default: + if (left <= 0) { + left = length; + luaL_addstring(&buffer, CRLF); + } + luaL_addchar(&buffer, *input); + left--; + break; + } + input++; + } + luaL_pushresult(&buffer); + lua_pushnumber(L, left); + return 2; +} + +/*-------------------------------------------------------------------------*\ +* Fill base64 decode map. +\*-------------------------------------------------------------------------*/ +static void b64setup(UC *unbase) +{ + int i; + for (i = 0; i <= 255; i++) unbase[i] = (UC) 255; + for (i = 0; i < 64; i++) unbase[b64base[i]] = (UC) i; + unbase['='] = 0; +} + +/*-------------------------------------------------------------------------*\ +* Acumulates bytes in input buffer until 3 bytes are available. +* Translate the 3 bytes into Base64 form and append to buffer. +* Returns new number of bytes in buffer. +\*-------------------------------------------------------------------------*/ +static size_t b64encode(UC c, UC *input, size_t size, + luaL_Buffer *buffer) +{ + input[size++] = c; + if (size == 3) { + UC code[4]; + unsigned long value = 0; + value += input[0]; value <<= 8; + value += input[1]; value <<= 8; + value += input[2]; + code[3] = b64base[value & 0x3f]; value >>= 6; + code[2] = b64base[value & 0x3f]; value >>= 6; + code[1] = b64base[value & 0x3f]; value >>= 6; + code[0] = b64base[value]; + luaL_addlstring(buffer, (char *) code, 4); + size = 0; + } + return size; +} + +/*-------------------------------------------------------------------------*\ +* Encodes the Base64 last 1 or 2 bytes and adds padding '=' +* Result, if any, is appended to buffer. +* Returns 0. +\*-------------------------------------------------------------------------*/ +static size_t b64pad(const UC *input, size_t size, + luaL_Buffer *buffer) +{ + unsigned long value = 0; + UC code[4] = {'=', '=', '=', '='}; + switch (size) { + case 1: + value = input[0] << 4; + code[1] = b64base[value & 0x3f]; value >>= 6; + code[0] = b64base[value]; + luaL_addlstring(buffer, (char *) code, 4); + break; + case 2: + value = input[0]; value <<= 8; + value |= input[1]; value <<= 2; + code[2] = b64base[value & 0x3f]; value >>= 6; + code[1] = b64base[value & 0x3f]; value >>= 6; + code[0] = b64base[value]; + luaL_addlstring(buffer, (char *) code, 4); + break; + default: + break; + } + return 0; +} + +/*-------------------------------------------------------------------------*\ +* Acumulates bytes in input buffer until 4 bytes are available. +* Translate the 4 bytes from Base64 form and append to buffer. +* Returns new number of bytes in buffer. +\*-------------------------------------------------------------------------*/ +static size_t b64decode(UC c, UC *input, size_t size, + luaL_Buffer *buffer) +{ + /* ignore invalid characters */ + if (b64unbase[c] > 64) return size; + input[size++] = c; + /* decode atom */ + if (size == 4) { + UC decoded[3]; + int valid, value = 0; + value = b64unbase[input[0]]; value <<= 6; + value |= b64unbase[input[1]]; value <<= 6; + value |= b64unbase[input[2]]; value <<= 6; + value |= b64unbase[input[3]]; + decoded[2] = (UC) (value & 0xff); value >>= 8; + decoded[1] = (UC) (value & 0xff); value >>= 8; + decoded[0] = (UC) value; + /* take care of paddding */ + valid = (input[2] == '=') ? 1 : (input[3] == '=') ? 2 : 3; + luaL_addlstring(buffer, (char *) decoded, valid); + return 0; + /* need more data */ + } else return size; +} + +/*-------------------------------------------------------------------------*\ +* Incrementally applies the Base64 transfer content encoding to a string +* A, B = b64(C, D) +* A is the encoded version of the largest prefix of C .. D that is +* divisible by 3. B has the remaining bytes of C .. D, *without* encoding. +* The easiest thing would be to concatenate the two strings and +* encode the result, but we can't afford that or Lua would dupplicate +* every chunk we received. +\*-------------------------------------------------------------------------*/ +static int mime_global_b64(lua_State *L) +{ + UC atom[3]; + size_t isize = 0, asize = 0; + const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize); + const UC *last = input + isize; + luaL_Buffer buffer; + /* end-of-input blackhole */ + if (!input) { + lua_pushnil(L); + lua_pushnil(L); + return 2; + } + /* make sure we don't confuse buffer stuff with arguments */ + lua_settop(L, 2); + /* process first part of the input */ + luaL_buffinit(L, &buffer); + while (input < last) + asize = b64encode(*input++, atom, asize, &buffer); + input = (UC *) luaL_optlstring(L, 2, NULL, &isize); + /* if second part is nil, we are done */ + if (!input) { + size_t osize = 0; + asize = b64pad(atom, asize, &buffer); + luaL_pushresult(&buffer); + /* if the output is empty and the input is nil, return nil */ + lua_tolstring(L, -1, &osize); + if (osize == 0) lua_pushnil(L); + lua_pushnil(L); + return 2; + } + /* otherwise process the second part */ + last = input + isize; + while (input < last) + asize = b64encode(*input++, atom, asize, &buffer); + luaL_pushresult(&buffer); + lua_pushlstring(L, (char *) atom, asize); + return 2; +} + +/*-------------------------------------------------------------------------*\ +* Incrementally removes the Base64 transfer content encoding from a string +* A, B = b64(C, D) +* A is the encoded version of the largest prefix of C .. D that is +* divisible by 4. B has the remaining bytes of C .. D, *without* encoding. +\*-------------------------------------------------------------------------*/ +static int mime_global_unb64(lua_State *L) +{ + UC atom[4]; + size_t isize = 0, asize = 0; + const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize); + const UC *last = input + isize; + luaL_Buffer buffer; + /* end-of-input blackhole */ + if (!input) { + lua_pushnil(L); + lua_pushnil(L); + return 2; + } + /* make sure we don't confuse buffer stuff with arguments */ + lua_settop(L, 2); + /* process first part of the input */ + luaL_buffinit(L, &buffer); + while (input < last) + asize = b64decode(*input++, atom, asize, &buffer); + input = (UC *) luaL_optlstring(L, 2, NULL, &isize); + /* if second is nil, we are done */ + if (!input) { + size_t osize = 0; + luaL_pushresult(&buffer); + /* if the output is empty and the input is nil, return nil */ + lua_tolstring(L, -1, &osize); + if (osize == 0) lua_pushnil(L); + lua_pushnil(L); + return 2; + } + /* otherwise, process the rest of the input */ + last = input + isize; + while (input < last) + asize = b64decode(*input++, atom, asize, &buffer); + luaL_pushresult(&buffer); + lua_pushlstring(L, (char *) atom, asize); + return 2; +} + +/*-------------------------------------------------------------------------*\ +* Quoted-printable encoding scheme +* all (except CRLF in text) can be =XX +* CLRL in not text must be =XX=XX +* 33 through 60 inclusive can be plain +* 62 through 126 inclusive can be plain +* 9 and 32 can be plain, unless in the end of a line, where must be =XX +* encoded lines must be no longer than 76 not counting CRLF +* soft line-break are =CRLF +* To encode one byte, we need to see the next two. +* Worst case is when we see a space, and wonder if a CRLF is comming +\*-------------------------------------------------------------------------*/ +/*-------------------------------------------------------------------------*\ +* Split quoted-printable characters into classes +* Precompute reverse map for encoding +\*-------------------------------------------------------------------------*/ +static void qpsetup(UC *cl, UC *unbase) +{ + int i; + for (i = 0; i < 256; i++) cl[i] = QP_QUOTED; + for (i = 33; i <= 60; i++) cl[i] = QP_PLAIN; + for (i = 62; i <= 126; i++) cl[i] = QP_PLAIN; + cl['\t'] = QP_IF_LAST; + cl[' '] = QP_IF_LAST; + cl['\r'] = QP_CR; + for (i = 0; i < 256; i++) unbase[i] = 255; + unbase['0'] = 0; unbase['1'] = 1; unbase['2'] = 2; + unbase['3'] = 3; unbase['4'] = 4; unbase['5'] = 5; + unbase['6'] = 6; unbase['7'] = 7; unbase['8'] = 8; + unbase['9'] = 9; unbase['A'] = 10; unbase['a'] = 10; + unbase['B'] = 11; unbase['b'] = 11; unbase['C'] = 12; + unbase['c'] = 12; unbase['D'] = 13; unbase['d'] = 13; + unbase['E'] = 14; unbase['e'] = 14; unbase['F'] = 15; + unbase['f'] = 15; +} + +/*-------------------------------------------------------------------------*\ +* Output one character in form =XX +\*-------------------------------------------------------------------------*/ +static void qpquote(UC c, luaL_Buffer *buffer) +{ + luaL_addchar(buffer, '='); + luaL_addchar(buffer, qpbase[c >> 4]); + luaL_addchar(buffer, qpbase[c & 0x0F]); +} + +/*-------------------------------------------------------------------------*\ +* Accumulate characters until we are sure about how to deal with them. +* Once we are sure, output to the buffer, in the correct form. +\*-------------------------------------------------------------------------*/ +static size_t qpencode(UC c, UC *input, size_t size, + const char *marker, luaL_Buffer *buffer) +{ + input[size++] = c; + /* deal with all characters we can have */ + while (size > 0) { + switch (qpclass[input[0]]) { + /* might be the CR of a CRLF sequence */ + case QP_CR: + if (size < 2) return size; + if (input[1] == '\n') { + luaL_addstring(buffer, marker); + return 0; + } else qpquote(input[0], buffer); + break; + /* might be a space and that has to be quoted if last in line */ + case QP_IF_LAST: + if (size < 3) return size; + /* if it is the last, quote it and we are done */ + if (input[1] == '\r' && input[2] == '\n') { + qpquote(input[0], buffer); + luaL_addstring(buffer, marker); + return 0; + } else luaL_addchar(buffer, input[0]); + break; + /* might have to be quoted always */ + case QP_QUOTED: + qpquote(input[0], buffer); + break; + /* might never have to be quoted */ + default: + luaL_addchar(buffer, input[0]); + break; + } + input[0] = input[1]; input[1] = input[2]; + size--; + } + return 0; +} + +/*-------------------------------------------------------------------------*\ +* Deal with the final characters +\*-------------------------------------------------------------------------*/ +static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer) +{ + size_t i; + for (i = 0; i < size; i++) { + if (qpclass[input[i]] == QP_PLAIN) luaL_addchar(buffer, input[i]); + else qpquote(input[i], buffer); + } + if (size > 0) luaL_addstring(buffer, EQCRLF); + return 0; +} + +/*-------------------------------------------------------------------------*\ +* Incrementally converts a string to quoted-printable +* A, B = qp(C, D, marker) +* Marker is the text to be used to replace CRLF sequences found in A. +* A is the encoded version of the largest prefix of C .. D that +* can be encoded without doubts. +* B has the remaining bytes of C .. D, *without* encoding. +\*-------------------------------------------------------------------------*/ +static int mime_global_qp(lua_State *L) +{ + + size_t asize = 0, isize = 0; + UC atom[3]; + const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize); + const UC *last = input + isize; + const char *marker = luaL_optstring(L, 3, CRLF); + luaL_Buffer buffer; + /* end-of-input blackhole */ + if (!input) { + lua_pushnil(L); + lua_pushnil(L); + return 2; + } + /* make sure we don't confuse buffer stuff with arguments */ + lua_settop(L, 3); + /* process first part of input */ + luaL_buffinit(L, &buffer); + while (input < last) + asize = qpencode(*input++, atom, asize, marker, &buffer); + input = (UC *) luaL_optlstring(L, 2, NULL, &isize); + /* if second part is nil, we are done */ + if (!input) { + asize = qppad(atom, asize, &buffer); + luaL_pushresult(&buffer); + if (!(*lua_tostring(L, -1))) lua_pushnil(L); + lua_pushnil(L); + return 2; + } + /* otherwise process rest of input */ + last = input + isize; + while (input < last) + asize = qpencode(*input++, atom, asize, marker, &buffer); + luaL_pushresult(&buffer); + lua_pushlstring(L, (char *) atom, asize); + return 2; +} + +/*-------------------------------------------------------------------------*\ +* Accumulate characters until we are sure about how to deal with them. +* Once we are sure, output the to the buffer, in the correct form. +\*-------------------------------------------------------------------------*/ +static size_t qpdecode(UC c, UC *input, size_t size, luaL_Buffer *buffer) { + int d; + input[size++] = c; + /* deal with all characters we can deal */ + switch (input[0]) { + /* if we have an escape character */ + case '=': + if (size < 3) return size; + /* eliminate soft line break */ + if (input[1] == '\r' && input[2] == '\n') return 0; + /* decode quoted representation */ + c = qpunbase[input[1]]; d = qpunbase[input[2]]; + /* if it is an invalid, do not decode */ + if (c > 15 || d > 15) luaL_addlstring(buffer, (char *)input, 3); + else luaL_addchar(buffer, (char) ((c << 4) + d)); + return 0; + case '\r': + if (size < 2) return size; + if (input[1] == '\n') luaL_addlstring(buffer, (char *)input, 2); + return 0; + default: + if (input[0] == '\t' || (input[0] > 31 && input[0] < 127)) + luaL_addchar(buffer, input[0]); + return 0; + } +} + +/*-------------------------------------------------------------------------*\ +* Incrementally decodes a string in quoted-printable +* A, B = qp(C, D) +* A is the decoded version of the largest prefix of C .. D that +* can be decoded without doubts. +* B has the remaining bytes of C .. D, *without* decoding. +\*-------------------------------------------------------------------------*/ +static int mime_global_unqp(lua_State *L) +{ + size_t asize = 0, isize = 0; + UC atom[3]; + const UC *input = (UC *) luaL_optlstring(L, 1, NULL, &isize); + const UC *last = input + isize; + luaL_Buffer buffer; + /* end-of-input blackhole */ + if (!input) { + lua_pushnil(L); + lua_pushnil(L); + return 2; + } + /* make sure we don't confuse buffer stuff with arguments */ + lua_settop(L, 2); + /* process first part of input */ + luaL_buffinit(L, &buffer); + while (input < last) + asize = qpdecode(*input++, atom, asize, &buffer); + input = (UC *) luaL_optlstring(L, 2, NULL, &isize); + /* if second part is nil, we are done */ + if (!input) { + luaL_pushresult(&buffer); + if (!(*lua_tostring(L, -1))) lua_pushnil(L); + lua_pushnil(L); + return 2; + } + /* otherwise process rest of input */ + last = input + isize; + while (input < last) + asize = qpdecode(*input++, atom, asize, &buffer); + luaL_pushresult(&buffer); + lua_pushlstring(L, (char *) atom, asize); + return 2; +} + +/*-------------------------------------------------------------------------*\ +* Incrementally breaks a quoted-printed string into lines +* A, n = qpwrp(l, B, length) +* A is a copy of B, broken into lines of at most 'length' bytes. +* 'l' is how many bytes are left for the first line of B. +* 'n' is the number of bytes left in the last line of A. +* There are two complications: lines can't be broken in the middle +* of an encoded =XX, and there might be line breaks already +\*-------------------------------------------------------------------------*/ +static int mime_global_qpwrp(lua_State *L) +{ + size_t size = 0; + int left = (int) luaL_checknumber(L, 1); + const UC *input = (UC *) luaL_optlstring(L, 2, NULL, &size); + const UC *last = input + size; + int length = (int) luaL_optnumber(L, 3, 76); + luaL_Buffer buffer; + /* end-of-input blackhole */ + if (!input) { + if (left < length) lua_pushstring(L, EQCRLF); + else lua_pushnil(L); + lua_pushnumber(L, length); + return 2; + } + /* process all input */ + luaL_buffinit(L, &buffer); + while (input < last) { + switch (*input) { + case '\r': + break; + case '\n': + left = length; + luaL_addstring(&buffer, CRLF); + break; + case '=': + if (left <= 3) { + left = length; + luaL_addstring(&buffer, EQCRLF); + } + luaL_addchar(&buffer, *input); + left--; + break; + default: + if (left <= 1) { + left = length; + luaL_addstring(&buffer, EQCRLF); + } + luaL_addchar(&buffer, *input); + left--; + break; + } + input++; + } + luaL_pushresult(&buffer); + lua_pushnumber(L, left); + return 2; +} + +/*-------------------------------------------------------------------------*\ +* Here is what we do: \n, and \r are considered candidates for line +* break. We issue *one* new line marker if any of them is seen alone, or +* followed by a different one. That is, \n\n and \r\r will issue two +* end of line markers each, but \r\n, \n\r etc will only issue *one* +* marker. This covers Mac OS, Mac OS X, VMS, Unix and DOS, as well as +* probably other more obscure conventions. +* +* c is the current character being processed +* last is the previous character +\*-------------------------------------------------------------------------*/ +#define eolcandidate(c) (c == '\r' || c == '\n') +static int eolprocess(int c, int last, const char *marker, + luaL_Buffer *buffer) +{ + if (eolcandidate(c)) { + if (eolcandidate(last)) { + if (c == last) luaL_addstring(buffer, marker); + return 0; + } else { + luaL_addstring(buffer, marker); + return c; + } + } else { + luaL_addchar(buffer, (char) c); + return 0; + } +} + +/*-------------------------------------------------------------------------*\ +* Converts a string to uniform EOL convention. +* A, n = eol(o, B, marker) +* A is the converted version of the largest prefix of B that can be +* converted unambiguously. 'o' is the context returned by the previous +* call. 'n' is the new context. +\*-------------------------------------------------------------------------*/ +static int mime_global_eol(lua_State *L) +{ + int ctx = luaL_checkint(L, 1); + size_t isize = 0; + const char *input = luaL_optlstring(L, 2, NULL, &isize); + const char *last = input + isize; + const char *marker = luaL_optstring(L, 3, CRLF); + luaL_Buffer buffer; + luaL_buffinit(L, &buffer); + /* end of input blackhole */ + if (!input) { + lua_pushnil(L); + lua_pushnumber(L, 0); + return 2; + } + /* process all input */ + while (input < last) + ctx = eolprocess(*input++, ctx, marker, &buffer); + luaL_pushresult(&buffer); + lua_pushnumber(L, ctx); + return 2; +} + +/*-------------------------------------------------------------------------*\ +* Takes one byte and stuff it if needed. +\*-------------------------------------------------------------------------*/ +static size_t dot(int c, size_t state, luaL_Buffer *buffer) +{ + luaL_addchar(buffer, (char) c); + switch (c) { + case '\r': + return 1; + case '\n': + return (state == 1)? 2: 0; + case '.': + if (state == 2) + luaL_addchar(buffer, '.'); + default: + return 0; + } +} + +/*-------------------------------------------------------------------------*\ +* Incrementally applies smtp stuffing to a string +* A, n = dot(l, D) +\*-------------------------------------------------------------------------*/ +static int mime_global_dot(lua_State *L) +{ + size_t isize = 0, state = (size_t) luaL_checknumber(L, 1); + const char *input = luaL_optlstring(L, 2, NULL, &isize); + const char *last = input + isize; + luaL_Buffer buffer; + /* end-of-input blackhole */ + if (!input) { + lua_pushnil(L); + lua_pushnumber(L, 2); + return 2; + } + /* process all input */ + luaL_buffinit(L, &buffer); + while (input < last) + state = dot(*input++, state, &buffer); + luaL_pushresult(&buffer); + lua_pushnumber(L, (lua_Number) state); + return 2; +} + diff --git a/external/lua/luasocket/mime.h b/external/lua/luasocket/mime.h new file mode 100644 index 0000000000..150e7adf91 --- /dev/null +++ b/external/lua/luasocket/mime.h @@ -0,0 +1,29 @@ +#ifndef MIME_H +#define MIME_H +/*=========================================================================*\ +* Core MIME support +* LuaSocket toolkit +* +* This module provides functions to implement transfer content encodings +* and formatting conforming to RFC 2045. It is used by mime.lua, which +* provide a higher level interface to this functionality. +\*=========================================================================*/ +#include "lua.h" + +/*-------------------------------------------------------------------------*\ +* Current MIME library version +\*-------------------------------------------------------------------------*/ +#define MIME_VERSION "MIME 1.0.3-rc1" +#define MIME_COPYRIGHT "Copyright (C) 2004-2012 Diego Nehab" +#define MIME_AUTHORS "Diego Nehab" + +/*-------------------------------------------------------------------------*\ +* This macro prefixes all exported API functions +\*-------------------------------------------------------------------------*/ +#ifndef MIME_API +#define MIME_API extern +#endif + +MIME_API int luaopen_mime_core(lua_State *L); + +#endif /* MIME_H */ diff --git a/external/lua/luasocket/options.c b/external/lua/luasocket/options.c new file mode 100644 index 0000000000..6cae7eeb92 --- /dev/null +++ b/external/lua/luasocket/options.c @@ -0,0 +1,262 @@ +/*=========================================================================*\ +* Common option interface +* LuaSocket toolkit +\*=========================================================================*/ +#include + +#include "lauxlib.h" + +#include "auxiliar.h" +#include "options.h" +#include "inet.h" + +/*=========================================================================*\ +* Internal functions prototypes +\*=========================================================================*/ +static int opt_setmembership(lua_State *L, p_socket ps, int level, int name); +static int opt_setboolean(lua_State *L, p_socket ps, int level, int name); +static int opt_getboolean(lua_State *L, p_socket ps, int level, int name); +static int opt_set(lua_State *L, p_socket ps, int level, int name, + void *val, int len); +static int opt_get(lua_State *L, p_socket ps, int level, int name, + void *val, int* len); + +/*=========================================================================*\ +* Exported functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Calls appropriate option handler +\*-------------------------------------------------------------------------*/ +int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps) +{ + const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ + while (opt->name && strcmp(name, opt->name)) + opt++; + if (!opt->func) { + char msg[45]; + sprintf(msg, "unsupported option `%.35s'", name); + luaL_argerror(L, 2, msg); + } + return opt->func(L, ps); +} + +int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps) +{ + const char *name = luaL_checkstring(L, 2); /* obj, name, ... */ + while (opt->name && strcmp(name, opt->name)) + opt++; + if (!opt->func) { + char msg[45]; + sprintf(msg, "unsupported option `%.35s'", name); + luaL_argerror(L, 2, msg); + } + return opt->func(L, ps); +} + +/* enables reuse of local address */ +int opt_set_reuseaddr(lua_State *L, p_socket ps) +{ + return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); +} + +int opt_get_reuseaddr(lua_State *L, p_socket ps) +{ + return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEADDR); +} + +/* enables reuse of local port */ +int opt_set_reuseport(lua_State *L, p_socket ps) +{ + return opt_setboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); +} + +int opt_get_reuseport(lua_State *L, p_socket ps) +{ + return opt_getboolean(L, ps, SOL_SOCKET, SO_REUSEPORT); +} + +/* disables the Naggle algorithm */ +int opt_set_tcp_nodelay(lua_State *L, p_socket ps) +{ + return opt_setboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); +} + +int opt_get_tcp_nodelay(lua_State *L, p_socket ps) +{ + return opt_getboolean(L, ps, IPPROTO_TCP, TCP_NODELAY); +} + +int opt_set_keepalive(lua_State *L, p_socket ps) +{ + return opt_setboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); +} + +int opt_get_keepalive(lua_State *L, p_socket ps) +{ + return opt_getboolean(L, ps, SOL_SOCKET, SO_KEEPALIVE); +} + +int opt_set_dontroute(lua_State *L, p_socket ps) +{ + return opt_setboolean(L, ps, SOL_SOCKET, SO_DONTROUTE); +} + +int opt_set_broadcast(lua_State *L, p_socket ps) +{ + return opt_setboolean(L, ps, SOL_SOCKET, SO_BROADCAST); +} + +int opt_set_ip_multicast_loop(lua_State *L, p_socket ps) +{ + return opt_setboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); +} + +int opt_get_ip_multicast_loop(lua_State *L, p_socket ps) +{ + return opt_getboolean(L, ps, IPPROTO_IP, IP_MULTICAST_LOOP); +} + +int opt_set_linger(lua_State *L, p_socket ps) +{ + struct linger li; /* obj, name, table */ + if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); + lua_pushstring(L, "on"); + lua_gettable(L, 3); + if (!lua_isboolean(L, -1)) + luaL_argerror(L, 3, "boolean 'on' field expected"); + li.l_onoff = (u_short) lua_toboolean(L, -1); + lua_pushstring(L, "timeout"); + lua_gettable(L, 3); + if (!lua_isnumber(L, -1)) + luaL_argerror(L, 3, "number 'timeout' field expected"); + li.l_linger = (u_short) lua_tonumber(L, -1); + return opt_set(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, sizeof(li)); +} + +int opt_get_linger(lua_State *L, p_socket ps) +{ + struct linger li; /* obj, name */ + int len = sizeof(li); + int err = opt_get(L, ps, SOL_SOCKET, SO_LINGER, (char *) &li, &len); + if (err) + return err; + lua_newtable(L); + lua_pushboolean(L, li.l_onoff); + lua_setfield(L, -2, "on"); + lua_pushinteger(L, li.l_linger); + lua_setfield(L, -2, "timeout"); + return 1; +} + +int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps) +{ + int val = (int) luaL_checknumber(L, 3); /* obj, name, int */ + return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_TTL, + (char *) &val, sizeof(val)); +} + +int opt_set_ip_multicast_if(lua_State *L, p_socket ps) +{ + const char *address = luaL_checkstring(L, 3); /* obj, name, ip */ + struct in_addr val; + val.s_addr = htonl(INADDR_ANY); + if (strcmp(address, "*") && !inet_aton(address, &val)) + luaL_argerror(L, 3, "ip expected"); + return opt_set(L, ps, IPPROTO_IP, IP_MULTICAST_IF, + (char *) &val, sizeof(val)); +} + +int opt_get_ip_multicast_if(lua_State *L, p_socket ps) +{ + struct in_addr val; + socklen_t len = sizeof(val); + if (getsockopt(*ps, IPPROTO_IP, IP_MULTICAST_IF, (char *) &val, &len) < 0) { + lua_pushnil(L); + lua_pushstring(L, "getsockopt failed"); + return 2; + } + lua_pushstring(L, inet_ntoa(val)); + return 1; +} + +int opt_set_ip_add_membership(lua_State *L, p_socket ps) +{ + return opt_setmembership(L, ps, IPPROTO_IP, IP_ADD_MEMBERSHIP); +} + +int opt_set_ip_drop_membersip(lua_State *L, p_socket ps) +{ + return opt_setmembership(L, ps, IPPROTO_IP, IP_DROP_MEMBERSHIP); +} + +int opt_set_ip6_v6only(lua_State *L, p_socket ps) +{ + return opt_setboolean(L, ps, IPPROTO_IPV6, IPV6_V6ONLY); +} + +/*=========================================================================*\ +* Auxiliar functions +\*=========================================================================*/ +static int opt_setmembership(lua_State *L, p_socket ps, int level, int name) +{ + struct ip_mreq val; /* obj, name, table */ + if (!lua_istable(L, 3)) auxiliar_typeerror(L,3,lua_typename(L, LUA_TTABLE)); + lua_pushstring(L, "multiaddr"); + lua_gettable(L, 3); + if (!lua_isstring(L, -1)) + luaL_argerror(L, 3, "string 'multiaddr' field expected"); + if (!inet_aton(lua_tostring(L, -1), &val.imr_multiaddr)) + luaL_argerror(L, 3, "invalid 'multiaddr' ip address"); + lua_pushstring(L, "interface"); + lua_gettable(L, 3); + if (!lua_isstring(L, -1)) + luaL_argerror(L, 3, "string 'interface' field expected"); + val.imr_interface.s_addr = htonl(INADDR_ANY); + if (strcmp(lua_tostring(L, -1), "*") && + !inet_aton(lua_tostring(L, -1), &val.imr_interface)) + luaL_argerror(L, 3, "invalid 'interface' ip address"); + return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); +} + +static +int opt_get(lua_State *L, p_socket ps, int level, int name, void *val, int* len) +{ + socklen_t socklen = *len; + if (getsockopt(*ps, level, name, (char *) val, &socklen) < 0) { + lua_pushnil(L); + lua_pushstring(L, "getsockopt failed"); + return 2; + } + *len = socklen; + return 0; +} + +static +int opt_set(lua_State *L, p_socket ps, int level, int name, void *val, int len) +{ + if (setsockopt(*ps, level, name, (char *) val, len) < 0) { + lua_pushnil(L); + lua_pushstring(L, "setsockopt failed"); + return 2; + } + lua_pushnumber(L, 1); + return 1; +} + +static int opt_getboolean(lua_State *L, p_socket ps, int level, int name) +{ + int val = 0; + int len = sizeof(val); + int err = opt_get(L, ps, level, name, (char *) &val, &len); + if (err) + return err; + lua_pushboolean(L, val); + return 1; +} + +static int opt_setboolean(lua_State *L, p_socket ps, int level, int name) +{ + int val = auxiliar_checkboolean(L, 3); /* obj, name, bool */ + return opt_set(L, ps, level, name, (char *) &val, sizeof(val)); +} + diff --git a/external/lua/luasocket/options.h b/external/lua/luasocket/options.h new file mode 100644 index 0000000000..55447f78f4 --- /dev/null +++ b/external/lua/luasocket/options.h @@ -0,0 +1,50 @@ +#ifndef OPTIONS_H +#define OPTIONS_H +/*=========================================================================*\ +* Common option interface +* LuaSocket toolkit +* +* This module provides a common interface to socket options, used mainly by +* modules UDP and TCP. +\*=========================================================================*/ + +#include "lua.h" +#include "socket.h" + +/* option registry */ +typedef struct t_opt { + const char *name; + int (*func)(lua_State *L, p_socket ps); +} t_opt; +typedef t_opt *p_opt; + +/* supported options for setoption */ +int opt_set_dontroute(lua_State *L, p_socket ps); +int opt_set_broadcast(lua_State *L, p_socket ps); +int opt_set_reuseaddr(lua_State *L, p_socket ps); +int opt_set_tcp_nodelay(lua_State *L, p_socket ps); +int opt_set_keepalive(lua_State *L, p_socket ps); +int opt_set_linger(lua_State *L, p_socket ps); +int opt_set_reuseaddr(lua_State *L, p_socket ps); +int opt_set_reuseport(lua_State *L, p_socket ps); +int opt_set_ip_multicast_if(lua_State *L, p_socket ps); +int opt_set_ip_multicast_ttl(lua_State *L, p_socket ps); +int opt_set_ip_multicast_loop(lua_State *L, p_socket ps); +int opt_set_ip_add_membership(lua_State *L, p_socket ps); +int opt_set_ip_drop_membersip(lua_State *L, p_socket ps); +int opt_set_ip6_v6only(lua_State *L, p_socket ps); + +/* supported options for getoption */ +int opt_get_reuseaddr(lua_State *L, p_socket ps); +int opt_get_tcp_nodelay(lua_State *L, p_socket ps); +int opt_get_keepalive(lua_State *L, p_socket ps); +int opt_get_linger(lua_State *L, p_socket ps); +int opt_get_reuseaddr(lua_State *L, p_socket ps); +int opt_get_ip_multicast_loop(lua_State *L, p_socket ps); +int opt_get_ip_multicast_if(lua_State *L, p_socket ps); + +/* invokes the appropriate option handler */ +int opt_meth_setoption(lua_State *L, p_opt opt, p_socket ps); +int opt_meth_getoption(lua_State *L, p_opt opt, p_socket ps); + +#endif diff --git a/external/lua/luasocket/select.c b/external/lua/luasocket/select.c new file mode 100644 index 0000000000..51fb198611 --- /dev/null +++ b/external/lua/luasocket/select.c @@ -0,0 +1,216 @@ +/*=========================================================================*\ +* Select implementation +* LuaSocket toolkit +\*=========================================================================*/ +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "socket.h" +#include "timeout.h" +#include "select.h" + +/*=========================================================================*\ +* Internal function prototypes. +\*=========================================================================*/ +static t_socket getfd(lua_State *L); +static int dirty(lua_State *L); +static void collect_fd(lua_State *L, int tab, int itab, + fd_set *set, t_socket *max_fd); +static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set); +static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, + int itab, int tab, int start); +static void make_assoc(lua_State *L, int tab); +static int global_select(lua_State *L); + +/* functions in library namespace */ +static luaL_Reg func[] = { + {"select", global_select}, + {NULL, NULL} +}; + +/*=========================================================================*\ +* Exported functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +int select_open(lua_State *L) { + lua_pushstring(L, "_SETSIZE"); + lua_pushnumber(L, FD_SETSIZE); + lua_rawset(L, -3); + luaL_openlib(L, NULL, func, 0); + return 0; +} + +/*=========================================================================*\ +* Global Lua functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Waits for a set of sockets until a condition is met or timeout. +\*-------------------------------------------------------------------------*/ +static int global_select(lua_State *L) { + int rtab, wtab, itab, ret, ndirty; + t_socket max_fd = SOCKET_INVALID; + fd_set rset, wset; + t_timeout tm; + double t = luaL_optnumber(L, 3, -1); + FD_ZERO(&rset); FD_ZERO(&wset); + lua_settop(L, 3); + lua_newtable(L); itab = lua_gettop(L); + lua_newtable(L); rtab = lua_gettop(L); + lua_newtable(L); wtab = lua_gettop(L); + collect_fd(L, 1, itab, &rset, &max_fd); + collect_fd(L, 2, itab, &wset, &max_fd); + ndirty = check_dirty(L, 1, rtab, &rset); + t = ndirty > 0? 0.0: t; + timeout_init(&tm, t, -1); + timeout_markstart(&tm); + ret = socket_select(max_fd+1, &rset, &wset, NULL, &tm); + if (ret > 0 || ndirty > 0) { + return_fd(L, &rset, max_fd+1, itab, rtab, ndirty); + return_fd(L, &wset, max_fd+1, itab, wtab, 0); + make_assoc(L, rtab); + make_assoc(L, wtab); + return 2; + } else if (ret == 0) { + lua_pushstring(L, "timeout"); + return 3; + } else { + luaL_error(L, "select failed"); + return 3; + } +} + +/*=========================================================================*\ +* Internal functions +\*=========================================================================*/ +static t_socket getfd(lua_State *L) { + t_socket fd = SOCKET_INVALID; + lua_pushstring(L, "getfd"); + lua_gettable(L, -2); + if (!lua_isnil(L, -1)) { + lua_pushvalue(L, -2); + lua_call(L, 1, 1); + if (lua_isnumber(L, -1)) { + double numfd = lua_tonumber(L, -1); + fd = (numfd >= 0.0)? (t_socket) numfd: SOCKET_INVALID; + } + } + lua_pop(L, 1); + return fd; +} + +static int dirty(lua_State *L) { + int is = 0; + lua_pushstring(L, "dirty"); + lua_gettable(L, -2); + if (!lua_isnil(L, -1)) { + lua_pushvalue(L, -2); + lua_call(L, 1, 1); + is = lua_toboolean(L, -1); + } + lua_pop(L, 1); + return is; +} + +static void collect_fd(lua_State *L, int tab, int itab, + fd_set *set, t_socket *max_fd) { + int i = 1, n = 0; + /* nil is the same as an empty table */ + if (lua_isnil(L, tab)) return; + /* otherwise we need it to be a table */ + luaL_checktype(L, tab, LUA_TTABLE); + for ( ;; ) { + t_socket fd; + lua_pushnumber(L, i); + lua_gettable(L, tab); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + break; + } + /* getfd figures out if this is a socket */ + fd = getfd(L); + if (fd != SOCKET_INVALID) { + /* make sure we don't overflow the fd_set */ +#ifdef _WIN32 + if (n >= FD_SETSIZE) + luaL_argerror(L, tab, "too many sockets"); +#else + if (fd >= FD_SETSIZE) + luaL_argerror(L, tab, "descriptor too large for set size"); +#endif + FD_SET(fd, set); + n++; + /* keep track of the largest descriptor so far */ + if (*max_fd == SOCKET_INVALID || *max_fd < fd) + *max_fd = fd; + /* make sure we can map back from descriptor to the object */ + lua_pushnumber(L, (lua_Number) fd); + lua_pushvalue(L, -2); + lua_settable(L, itab); + } + lua_pop(L, 1); + i = i + 1; + } +} + +static int check_dirty(lua_State *L, int tab, int dtab, fd_set *set) { + int ndirty = 0, i = 1; + if (lua_isnil(L, tab)) + return 0; + for ( ;; ) { + t_socket fd; + lua_pushnumber(L, i); + lua_gettable(L, tab); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + break; + } + fd = getfd(L); + if (fd != SOCKET_INVALID && dirty(L)) { + lua_pushnumber(L, ++ndirty); + lua_pushvalue(L, -2); + lua_settable(L, dtab); + FD_CLR(fd, set); + } + lua_pop(L, 1); + i = i + 1; + } + return ndirty; +} + +static void return_fd(lua_State *L, fd_set *set, t_socket max_fd, + int itab, int tab, int start) { + t_socket fd; + for (fd = 0; fd < max_fd; fd++) { + if (FD_ISSET(fd, set)) { + lua_pushnumber(L, ++start); + lua_pushnumber(L, (lua_Number) fd); + lua_gettable(L, itab); + lua_settable(L, tab); + } + } +} + +static void make_assoc(lua_State *L, int tab) { + int i = 1, atab; + lua_newtable(L); atab = lua_gettop(L); + for ( ;; ) { + lua_pushnumber(L, i); + lua_gettable(L, tab); + if (!lua_isnil(L, -1)) { + lua_pushnumber(L, i); + lua_pushvalue(L, -2); + lua_settable(L, atab); + lua_pushnumber(L, i); + lua_settable(L, atab); + } else { + lua_pop(L, 1); + break; + } + i = i+1; + } +} + diff --git a/external/lua/luasocket/select.h b/external/lua/luasocket/select.h new file mode 100644 index 0000000000..8750200395 --- /dev/null +++ b/external/lua/luasocket/select.h @@ -0,0 +1,15 @@ +#ifndef SELECT_H +#define SELECT_H +/*=========================================================================*\ +* Select implementation +* LuaSocket toolkit +* +* Each object that can be passed to the select function has to export +* method getfd() which returns the descriptor to be passed to the +* underlying select function. Another method, dirty(), should return +* true if there is data ready for reading (required for buffered input). +\*=========================================================================*/ + +int select_open(lua_State *L); + +#endif /* SELECT_H */ diff --git a/external/lua/luasocket/serial.c b/external/lua/luasocket/serial.c new file mode 100644 index 0000000000..5b09e76830 --- /dev/null +++ b/external/lua/luasocket/serial.c @@ -0,0 +1,183 @@ +/*=========================================================================*\ +* Serial stream +* LuaSocket toolkit +\*=========================================================================*/ +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "auxiliar.h" +#include "socket.h" +#include "options.h" +#include "unix.h" +#include + +/* +Reuses userdata definition from unix.h, since it is useful for all +stream-like objects. + +If we stored the serial path for use in error messages or userdata +printing, we might need our own userdata definition. + +Group usage is semi-inherited from unix.c, but unnecessary since we +have only one object type. +*/ + +/*=========================================================================*\ +* Internal function prototypes +\*=========================================================================*/ +static int global_create(lua_State *L); +static int meth_send(lua_State *L); +static int meth_receive(lua_State *L); +static int meth_close(lua_State *L); +static int meth_settimeout(lua_State *L); +static int meth_getfd(lua_State *L); +static int meth_setfd(lua_State *L); +static int meth_dirty(lua_State *L); +static int meth_getstats(lua_State *L); +static int meth_setstats(lua_State *L); + +/* serial object methods */ +static luaL_Reg serial_methods[] = { + {"__gc", meth_close}, + {"__tostring", auxiliar_tostring}, + {"close", meth_close}, + {"dirty", meth_dirty}, + {"getfd", meth_getfd}, + {"getstats", meth_getstats}, + {"setstats", meth_setstats}, + {"receive", meth_receive}, + {"send", meth_send}, + {"setfd", meth_setfd}, + {"settimeout", meth_settimeout}, + {NULL, NULL} +}; + +/* our socket creation function */ +static luaL_Reg func[] = { + {"serial", global_create}, + {NULL, NULL} +}; + + +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +LUASOCKET_API int luaopen_socket_serial(lua_State *L) { + /* create classes */ + auxiliar_newclass(L, "serial{client}", serial_methods); + /* create class groups */ + auxiliar_add2group(L, "serial{client}", "serial{any}"); + /* make sure the function ends up in the package table */ + luaL_openlib(L, "socket", func, 0); + /* return the function instead of the 'socket' table */ + lua_pushstring(L, "serial"); + lua_gettable(L, -2); + return 1; +} + +/*=========================================================================*\ +* Lua methods +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Just call buffered IO methods +\*-------------------------------------------------------------------------*/ +static int meth_send(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); + return buffer_meth_send(L, &un->buf); +} + +static int meth_receive(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); + return buffer_meth_receive(L, &un->buf); +} + +static int meth_getstats(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); + return buffer_meth_getstats(L, &un->buf); +} + +static int meth_setstats(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkclass(L, "serial{client}", 1); + return buffer_meth_setstats(L, &un->buf); +} + +/*-------------------------------------------------------------------------*\ +* Select support methods +\*-------------------------------------------------------------------------*/ +static int meth_getfd(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); + lua_pushnumber(L, (int) un->sock); + return 1; +} + +/* this is very dangerous, but can be handy for those that are brave enough */ +static int meth_setfd(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); + un->sock = (t_socket) luaL_checknumber(L, 2); + return 0; +} + +static int meth_dirty(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); + lua_pushboolean(L, !buffer_isempty(&un->buf)); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Closes socket used by object +\*-------------------------------------------------------------------------*/ +static int meth_close(lua_State *L) +{ + p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); + socket_destroy(&un->sock); + lua_pushnumber(L, 1); + return 1; +} + + +/*-------------------------------------------------------------------------*\ +* Just call tm methods +\*-------------------------------------------------------------------------*/ +static int meth_settimeout(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "serial{any}", 1); + return timeout_meth_settimeout(L, &un->tm); +} + +/*=========================================================================*\ +* Library functions +\*=========================================================================*/ + + +/*-------------------------------------------------------------------------*\ +* Creates a serial object +\*-------------------------------------------------------------------------*/ +static int global_create(lua_State *L) { + const char* path = luaL_checkstring(L, 1); + + /* allocate unix object */ + p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); + + /* open serial device */ + t_socket sock = open(path, O_NOCTTY|O_RDWR); + + /*printf("open %s on %d\n", path, sock);*/ + + if (sock < 0) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(errno)); + lua_pushnumber(L, errno); + return 3; + } + /* set its type as client object */ + auxiliar_setclass(L, "serial{client}", -1); + /* initialize remaining structure fields */ + socket_setnonblocking(&sock); + un->sock = sock; + io_init(&un->io, (p_send) socket_write, (p_recv) socket_read, + (p_error) socket_ioerror, &un->sock); + timeout_init(&un->tm, -1, -1); + buffer_init(&un->buf, &un->io, &un->tm); + return 1; +} diff --git a/external/lua/luasocket/socket.h b/external/lua/luasocket/socket.h new file mode 100644 index 0000000000..03fa186b8a --- /dev/null +++ b/external/lua/luasocket/socket.h @@ -0,0 +1,78 @@ +#ifndef SOCKET_H +#define SOCKET_H +/*=========================================================================*\ +* Socket compatibilization module +* LuaSocket toolkit +* +* BSD Sockets and WinSock are similar, but there are a few irritating +* differences. Also, not all *nix platforms behave the same. This module +* (and the associated usocket.h and wsocket.h) factor these differences and +* creates a interface compatible with the io.h module. +\*=========================================================================*/ +#include "luasocket_io.h" + +/*=========================================================================*\ +* Platform specific compatibilization +\*=========================================================================*/ +#ifdef _WIN32 +#include "wsocket.h" +#else +#include "usocket.h" +#endif + +/*=========================================================================*\ +* The connect and accept functions accept a timeout and their +* implementations are somewhat complicated. We chose to move +* the timeout control into this module for these functions in +* order to simplify the modules that use them. +\*=========================================================================*/ +#include "timeout.h" + +/* we are lazy... */ +typedef struct sockaddr SA; + +/*=========================================================================*\ +* Functions bellow implement a comfortable platform independent +* interface to sockets +\*=========================================================================*/ +int socket_open(void); +int socket_close(void); +void socket_destroy(p_socket ps); +void socket_shutdown(p_socket ps, int how); +int socket_sendto(p_socket ps, const char *data, size_t count, + size_t *sent, SA *addr, socklen_t addr_len, p_timeout tm); +int socket_recvfrom(p_socket ps, char *data, size_t count, + size_t *got, SA *addr, socklen_t *addr_len, p_timeout tm); + +void socket_setnonblocking(p_socket ps); +void socket_setblocking(p_socket ps); + +int socket_waitfd(p_socket ps, int sw, p_timeout tm); +int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, + p_timeout tm); + +int socket_connect(p_socket ps, SA *addr, socklen_t addr_len, p_timeout tm); +int socket_create(p_socket ps, int domain, int type, int protocol); +int socket_bind(p_socket ps, SA *addr, socklen_t addr_len); +int socket_listen(p_socket ps, int backlog); +int socket_accept(p_socket ps, p_socket pa, SA *addr, + socklen_t *addr_len, p_timeout tm); + +const char *socket_hoststrerror(int err); +const char *socket_gaistrerror(int err); +const char *socket_strerror(int err); + +/* these are perfect to use with the io abstraction module + and the buffered input module */ +int socket_send(p_socket ps, const char *data, size_t count, + size_t *sent, p_timeout tm); +int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm); +int socket_write(p_socket ps, const char *data, size_t count, + size_t *sent, p_timeout tm); +int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm); +const char *socket_ioerror(p_socket ps, int err); + +int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp); +int socket_gethostbyname(const char *addr, struct hostent **hp); + +#endif /* SOCKET_H */ diff --git a/external/lua/luasocket/socket_scripts.c.REMOVED.git-id b/external/lua/luasocket/socket_scripts.c.REMOVED.git-id new file mode 100644 index 0000000000..f8ab743b49 --- /dev/null +++ b/external/lua/luasocket/socket_scripts.c.REMOVED.git-id @@ -0,0 +1 @@ +e79358cdf4b02c66a50e684543e505002bdbcc91 \ No newline at end of file diff --git a/external/lua/luasocket/socket_scripts.h b/external/lua/luasocket/socket_scripts.h new file mode 100644 index 0000000000..939fcb2d03 --- /dev/null +++ b/external/lua/luasocket/socket_scripts.h @@ -0,0 +1,32 @@ + +/* socket_scripts.h */ + +#ifndef __LUA_MODULES_E58C07CB2FBAF5B178B7E1836DC22849_H_ +#define __LUA_MODULES_E58C07CB2FBAF5B178B7E1836DC22849_H_ + +#if __cplusplus +extern "C" { +#endif + +#include "lua.h" + +void luaopen_socket_scripts(lua_State* L); + +/* +int luaopen_lua_m_ltn12(lua_State* L); +int luaopen_lua_m_mime(lua_State* L); +int luaopen_lua_m_socket_ftp(lua_State* L); +int luaopen_lua_m_socket_headers(lua_State* L); +int luaopen_lua_m_socket_http(lua_State* L); +int luaopen_lua_m_socket_mbox(lua_State* L); +int luaopen_lua_m_socket_smtp(lua_State* L); +int luaopen_lua_m_socket_tp(lua_State* L); +int luaopen_lua_m_socket_url(lua_State* L); +int luaopen_lua_m_socket(lua_State* L); +*/ + +#if __cplusplus +} +#endif + +#endif /* __LUA_MODULES_E58C07CB2FBAF5B178B7E1836DC22849_H_ */ diff --git a/external/lua/luasocket/tcp.c b/external/lua/luasocket/tcp.c new file mode 100644 index 0000000000..6734dc0b49 --- /dev/null +++ b/external/lua/luasocket/tcp.c @@ -0,0 +1,475 @@ +/*=========================================================================*\ +* TCP object +* LuaSocket toolkit +\*=========================================================================*/ +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "auxiliar.h" +#include "socket.h" +#include "inet.h" +#include "options.h" +#include "tcp.h" + +/*=========================================================================*\ +* Internal function prototypes +\*=========================================================================*/ +static int global_create(lua_State *L); +static int global_create6(lua_State *L); +static int global_connect(lua_State *L); +static int meth_connect(lua_State *L); +static int meth_listen(lua_State *L); +static int meth_getfamily(lua_State *L); +static int meth_bind(lua_State *L); +static int meth_send(lua_State *L); +static int meth_getstats(lua_State *L); +static int meth_setstats(lua_State *L); +static int meth_getsockname(lua_State *L); +static int meth_getpeername(lua_State *L); +static int meth_shutdown(lua_State *L); +static int meth_receive(lua_State *L); +static int meth_accept(lua_State *L); +static int meth_close(lua_State *L); +static int meth_getoption(lua_State *L); +static int meth_setoption(lua_State *L); +static int meth_settimeout(lua_State *L); +static int meth_getfd(lua_State *L); +static int meth_setfd(lua_State *L); +static int meth_dirty(lua_State *L); + +/* tcp object methods */ +static luaL_Reg tcp_methods[] = { + {"__gc", meth_close}, + {"__tostring", auxiliar_tostring}, + {"accept", meth_accept}, + {"bind", meth_bind}, + {"close", meth_close}, + {"connect", meth_connect}, + {"dirty", meth_dirty}, + {"getfamily", meth_getfamily}, + {"getfd", meth_getfd}, + {"getoption", meth_getoption}, + {"getpeername", meth_getpeername}, + {"getsockname", meth_getsockname}, + {"getstats", meth_getstats}, + {"setstats", meth_setstats}, + {"listen", meth_listen}, + {"receive", meth_receive}, + {"send", meth_send}, + {"setfd", meth_setfd}, + {"setoption", meth_setoption}, + {"setpeername", meth_connect}, + {"setsockname", meth_bind}, + {"settimeout", meth_settimeout}, + {"shutdown", meth_shutdown}, + {NULL, NULL} +}; + +/* socket option handlers */ +static t_opt optget[] = { + {"keepalive", opt_get_keepalive}, + {"reuseaddr", opt_get_reuseaddr}, + {"tcp-nodelay", opt_get_tcp_nodelay}, + {"linger", opt_get_linger}, + {NULL, NULL} +}; + +static t_opt optset[] = { + {"keepalive", opt_set_keepalive}, + {"reuseaddr", opt_set_reuseaddr}, + {"tcp-nodelay", opt_set_tcp_nodelay}, + {"ipv6-v6only", opt_set_ip6_v6only}, + {"linger", opt_set_linger}, + {NULL, NULL} +}; + +/* functions in library namespace */ +static luaL_Reg func[] = { + {"tcp", global_create}, + {"tcp6", global_create6}, + {"connect", global_connect}, + {NULL, NULL} +}; + +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +int tcp_open(lua_State *L) +{ + /* create classes */ + auxiliar_newclass(L, "tcp{master}", tcp_methods); + auxiliar_newclass(L, "tcp{client}", tcp_methods); + auxiliar_newclass(L, "tcp{server}", tcp_methods); + /* create class groups */ + auxiliar_add2group(L, "tcp{master}", "tcp{any}"); + auxiliar_add2group(L, "tcp{client}", "tcp{any}"); + auxiliar_add2group(L, "tcp{server}", "tcp{any}"); + /* define library functions */ + luaL_openlib(L, NULL, func, 0); + return 0; +} + +/*=========================================================================*\ +* Lua methods +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Just call buffered IO methods +\*-------------------------------------------------------------------------*/ +static int meth_send(lua_State *L) { + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); + return buffer_meth_send(L, &tcp->buf); +} + +static int meth_receive(lua_State *L) { + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); + return buffer_meth_receive(L, &tcp->buf); +} + +static int meth_getstats(lua_State *L) { + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); + return buffer_meth_getstats(L, &tcp->buf); +} + +static int meth_setstats(lua_State *L) { + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); + return buffer_meth_setstats(L, &tcp->buf); +} + +/*-------------------------------------------------------------------------*\ +* Just call option handler +\*-------------------------------------------------------------------------*/ +static int meth_getoption(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + return opt_meth_getoption(L, optget, &tcp->sock); +} + +static int meth_setoption(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + return opt_meth_setoption(L, optset, &tcp->sock); +} + +/*-------------------------------------------------------------------------*\ +* Select support methods +\*-------------------------------------------------------------------------*/ +static int meth_getfd(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + lua_pushnumber(L, (int) tcp->sock); + return 1; +} + +/* this is very dangerous, but can be handy for those that are brave enough */ +static int meth_setfd(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + tcp->sock = (t_socket) luaL_checknumber(L, 2); + return 0; +} + +static int meth_dirty(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + lua_pushboolean(L, !buffer_isempty(&tcp->buf)); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Waits for and returns a client object attempting connection to the +* server object +\*-------------------------------------------------------------------------*/ +static int meth_accept(lua_State *L) +{ + p_tcp server = (p_tcp) auxiliar_checkclass(L, "tcp{server}", 1); + p_timeout tm = timeout_markstart(&server->tm); + t_socket sock; + const char *err = inet_tryaccept(&server->sock, server->family, &sock, tm); + /* if successful, push client socket */ + if (err == NULL) { + p_tcp clnt = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); + auxiliar_setclass(L, "tcp{client}", -1); + /* initialize structure fields */ + memset(clnt, 0, sizeof(t_tcp)); + socket_setnonblocking(&sock); + clnt->sock = sock; + io_init(&clnt->io, (p_send) socket_send, (p_recv) socket_recv, + (p_error) socket_ioerror, &clnt->sock); + timeout_init(&clnt->tm, -1, -1); + buffer_init(&clnt->buf, &clnt->io, &clnt->tm); + clnt->family = server->family; + return 1; + } else { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } +} + +/*-------------------------------------------------------------------------*\ +* Binds an object to an address +\*-------------------------------------------------------------------------*/ +static int meth_bind(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1); + const char *address = luaL_checkstring(L, 2); + const char *port = luaL_checkstring(L, 3); + const char *err; + struct addrinfo bindhints; + memset(&bindhints, 0, sizeof(bindhints)); + bindhints.ai_socktype = SOCK_STREAM; + bindhints.ai_family = tcp->family; + bindhints.ai_flags = AI_PASSIVE; + address = strcmp(address, "*")? address: NULL; + err = inet_trybind(&tcp->sock, address, port, &bindhints); + if (err) { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Turns a master tcp object into a client object. +\*-------------------------------------------------------------------------*/ +static int meth_connect(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + const char *address = luaL_checkstring(L, 2); + const char *port = luaL_checkstring(L, 3); + struct addrinfo connecthints; + const char *err; + memset(&connecthints, 0, sizeof(connecthints)); + connecthints.ai_socktype = SOCK_STREAM; + /* make sure we try to connect only to the same family */ + connecthints.ai_family = tcp->family; + timeout_markstart(&tcp->tm); + err = inet_tryconnect(&tcp->sock, address, port, &tcp->tm, &connecthints); + /* have to set the class even if it failed due to non-blocking connects */ + auxiliar_setclass(L, "tcp{client}", 1); + if (err) { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Closes socket used by object +\*-------------------------------------------------------------------------*/ +static int meth_close(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + socket_destroy(&tcp->sock); + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Returns family as string +\*-------------------------------------------------------------------------*/ +static int meth_getfamily(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + if (tcp->family == PF_INET6) { + lua_pushliteral(L, "inet6"); + return 1; + } else { + lua_pushliteral(L, "inet4"); + return 1; + } +} + +/*-------------------------------------------------------------------------*\ +* Puts the sockt in listen mode +\*-------------------------------------------------------------------------*/ +static int meth_listen(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1); + int backlog = (int) luaL_optnumber(L, 2, 32); + int err = socket_listen(&tcp->sock, backlog); + if (err != IO_DONE) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(err)); + return 2; + } + /* turn master object into a server object */ + auxiliar_setclass(L, "tcp{server}", 1); + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Shuts the connection down partially +\*-------------------------------------------------------------------------*/ +static int meth_shutdown(lua_State *L) +{ + /* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */ + static const char* methods[] = { "receive", "send", "both", NULL }; + p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{client}", 1); + int how = luaL_checkoption(L, 2, "both", methods); + socket_shutdown(&tcp->sock, how); + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Just call inet methods +\*-------------------------------------------------------------------------*/ +static int meth_getpeername(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + return inet_meth_getpeername(L, &tcp->sock, tcp->family); +} + +static int meth_getsockname(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + return inet_meth_getsockname(L, &tcp->sock, tcp->family); +} + +/*-------------------------------------------------------------------------*\ +* Just call tm methods +\*-------------------------------------------------------------------------*/ +static int meth_settimeout(lua_State *L) +{ + p_tcp tcp = (p_tcp) auxiliar_checkgroup(L, "tcp{any}", 1); + return timeout_meth_settimeout(L, &tcp->tm); +} + +/*=========================================================================*\ +* Library functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Creates a master tcp object +\*-------------------------------------------------------------------------*/ +static int tcp_create(lua_State *L, int family) { + t_socket sock; + const char *err = inet_trycreate(&sock, family, SOCK_STREAM); + /* try to allocate a system socket */ + if (!err) { + /* allocate tcp object */ + p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); + memset(tcp, 0, sizeof(t_tcp)); + /* set its type as master object */ + auxiliar_setclass(L, "tcp{master}", -1); + /* initialize remaining structure fields */ + socket_setnonblocking(&sock); + if (family == PF_INET6) { + int yes = 1; + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, + (void *)&yes, sizeof(yes)); + } + tcp->sock = sock; + io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, + (p_error) socket_ioerror, &tcp->sock); + timeout_init(&tcp->tm, -1, -1); + buffer_init(&tcp->buf, &tcp->io, &tcp->tm); + tcp->family = family; + return 1; + } else { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } +} + +static int global_create(lua_State *L) { + return tcp_create(L, AF_INET); +} + +static int global_create6(lua_State *L) { + return tcp_create(L, AF_INET6); +} + +static const char *tryconnect6(const char *remoteaddr, const char *remoteserv, + struct addrinfo *connecthints, p_tcp tcp) { + struct addrinfo *iterator = NULL, *resolved = NULL; + const char *err = NULL; + /* try resolving */ + err = socket_gaistrerror(getaddrinfo(remoteaddr, remoteserv, + connecthints, &resolved)); + if (err != NULL) { + if (resolved) freeaddrinfo(resolved); + return err; + } + /* iterate over all returned addresses trying to connect */ + for (iterator = resolved; iterator; iterator = iterator->ai_next) { + p_timeout tm = timeout_markstart(&tcp->tm); + /* create new socket if one wasn't created by the bind stage */ + if (tcp->sock == SOCKET_INVALID) { + err = socket_strerror(socket_create(&tcp->sock, + iterator->ai_family, iterator->ai_socktype, + iterator->ai_protocol)); + if (err != NULL) { + freeaddrinfo(resolved); + return err; + } + tcp->family = iterator->ai_family; + /* all sockets initially non-blocking */ + socket_setnonblocking(&tcp->sock); + } + /* finally try connecting to remote address */ + err = socket_strerror(socket_connect(&tcp->sock, + (SA *) iterator->ai_addr, + (socklen_t) iterator->ai_addrlen, tm)); + /* if success, break out of loop */ + if (err == NULL) break; + } + + freeaddrinfo(resolved); + /* here, if err is set, we failed */ + return err; +} + +static int global_connect(lua_State *L) { + const char *remoteaddr = luaL_checkstring(L, 1); + const char *remoteserv = luaL_checkstring(L, 2); + const char *localaddr = luaL_optstring(L, 3, NULL); + const char *localserv = luaL_optstring(L, 4, "0"); + int family = inet_optfamily(L, 5, "unspec"); + p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); + struct addrinfo bindhints, connecthints; + const char *err = NULL; + /* initialize tcp structure */ + memset(tcp, 0, sizeof(t_tcp)); + io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, + (p_error) socket_ioerror, &tcp->sock); + timeout_init(&tcp->tm, -1, -1); + buffer_init(&tcp->buf, &tcp->io, &tcp->tm); + tcp->sock = SOCKET_INVALID; + /* allow user to pick local address and port */ + memset(&bindhints, 0, sizeof(bindhints)); + bindhints.ai_socktype = SOCK_STREAM; + bindhints.ai_family = family; + bindhints.ai_flags = AI_PASSIVE; + if (localaddr) { + err = inet_trybind(&tcp->sock, localaddr, localserv, &bindhints); + if (err) { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } + tcp->family = bindhints.ai_family; + } + /* try to connect to remote address and port */ + memset(&connecthints, 0, sizeof(connecthints)); + connecthints.ai_socktype = SOCK_STREAM; + /* make sure we try to connect only to the same family */ + connecthints.ai_family = bindhints.ai_family; + err = tryconnect6(remoteaddr, remoteserv, &connecthints, tcp); + if (err) { + socket_destroy(&tcp->sock); + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } + auxiliar_setclass(L, "tcp{client}", -1); + return 1; +} diff --git a/external/lua/luasocket/tcp.h b/external/lua/luasocket/tcp.h new file mode 100644 index 0000000000..a8b9b6f2cf --- /dev/null +++ b/external/lua/luasocket/tcp.h @@ -0,0 +1,35 @@ +#ifndef TCP_H +#define TCP_H +/*=========================================================================*\ +* TCP object +* LuaSocket toolkit +* +* The tcp.h module is basicly a glue that puts together modules buffer.h, +* timeout.h socket.h and inet.h to provide the LuaSocket TCP (AF_INET, +* SOCK_STREAM) support. +* +* Three classes are defined: master, client and server. The master class is +* a newly created tcp object, that has not been bound or connected. Server +* objects are tcp objects bound to some local address. Client objects are +* tcp objects either connected to some address or returned by the accept +* method of a server object. +\*=========================================================================*/ +#include "lua.h" + +#include "luasocket_buffer.h" +#include "timeout.h" +#include "socket.h" + +typedef struct t_tcp_ { + t_socket sock; + t_io io; + t_buffer buf; + t_timeout tm; + int family; +} t_tcp; + +typedef t_tcp *p_tcp; + +int tcp_open(lua_State *L); + +#endif /* TCP_H */ diff --git a/external/lua/luasocket/timeout.c b/external/lua/luasocket/timeout.c new file mode 100644 index 0000000000..c7354b52f3 --- /dev/null +++ b/external/lua/luasocket/timeout.c @@ -0,0 +1,217 @@ +/*=========================================================================*\ +* Timeout management functions +* LuaSocket toolkit +\*=========================================================================*/ +#include +#include +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "auxiliar.h" +#include "timeout.h" + +#ifdef _WIN32 +#include +#else +#include +#include +#endif + +/* min and max macros */ +#ifndef MIN +#define MIN(x, y) ((x) < (y) ? x : y) +#endif +#ifndef MAX +#define MAX(x, y) ((x) > (y) ? x : y) +#endif + +/*=========================================================================*\ +* Internal function prototypes +\*=========================================================================*/ +static int timeout_lua_gettime(lua_State *L); +static int timeout_lua_sleep(lua_State *L); + +static luaL_Reg func[] = { + { "gettime", timeout_lua_gettime }, + { "sleep", timeout_lua_sleep }, + { NULL, NULL } +}; + +/*=========================================================================*\ +* Exported functions. +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Initialize structure +\*-------------------------------------------------------------------------*/ +void timeout_init(p_timeout tm, double block, double total) { + tm->block = block; + tm->total = total; +} + +/*-------------------------------------------------------------------------*\ +* Determines how much time we have left for the next system call, +* if the previous call was successful +* Input +* tm: timeout control structure +* Returns +* the number of ms left or -1 if there is no time limit +\*-------------------------------------------------------------------------*/ +double timeout_get(p_timeout tm) { + if (tm->block < 0.0 && tm->total < 0.0) { + return -1; + } else if (tm->block < 0.0) { + double t = tm->total - timeout_gettime() + tm->start; + return MAX(t, 0.0); + } else if (tm->total < 0.0) { + return tm->block; + } else { + double t = tm->total - timeout_gettime() + tm->start; + return MIN(tm->block, MAX(t, 0.0)); + } +} + +/*-------------------------------------------------------------------------*\ +* Returns time since start of operation +* Input +* tm: timeout control structure +* Returns +* start field of structure +\*-------------------------------------------------------------------------*/ +double timeout_getstart(p_timeout tm) { + return tm->start; +} + +/*-------------------------------------------------------------------------*\ +* Determines how much time we have left for the next system call, +* if the previous call was a failure +* Input +* tm: timeout control structure +* Returns +* the number of ms left or -1 if there is no time limit +\*-------------------------------------------------------------------------*/ +double timeout_getretry(p_timeout tm) { + if (tm->block < 0.0 && tm->total < 0.0) { + return -1; + } else if (tm->block < 0.0) { + double t = tm->total - timeout_gettime() + tm->start; + return MAX(t, 0.0); + } else if (tm->total < 0.0) { + double t = tm->block - timeout_gettime() + tm->start; + return MAX(t, 0.0); + } else { + double t = tm->total - timeout_gettime() + tm->start; + return MIN(tm->block, MAX(t, 0.0)); + } +} + +/*-------------------------------------------------------------------------*\ +* Marks the operation start time in structure +* Input +* tm: timeout control structure +\*-------------------------------------------------------------------------*/ +p_timeout timeout_markstart(p_timeout tm) { + tm->start = timeout_gettime(); + return tm; +} + +/*-------------------------------------------------------------------------*\ +* Gets time in s, relative to January 1, 1970 (UTC) +* Returns +* time in s. +\*-------------------------------------------------------------------------*/ +#ifdef _WIN32 +double timeout_gettime(void) { + FILETIME ft; + double t; + GetSystemTimeAsFileTime(&ft); + /* Windows file time (time since January 1, 1601 (UTC)) */ + t = ft.dwLowDateTime/1.0e7 + ft.dwHighDateTime*(4294967296.0/1.0e7); + /* convert to Unix Epoch time (time since January 1, 1970 (UTC)) */ + return (t - 11644473600.0); +} +#else +double timeout_gettime(void) { + struct timeval v; + gettimeofday(&v, (struct timezone *) NULL); + /* Unix Epoch time (time since January 1, 1970 (UTC)) */ + return v.tv_sec + v.tv_usec/1.0e6; +} +#endif + +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +int timeout_open(lua_State *L) { + luaL_openlib(L, NULL, func, 0); + return 0; +} + +/*-------------------------------------------------------------------------*\ +* Sets timeout values for IO operations +* Lua Input: base, time [, mode] +* time: time out value in seconds +* mode: "b" for block timeout, "t" for total timeout. (default: b) +\*-------------------------------------------------------------------------*/ +int timeout_meth_settimeout(lua_State *L, p_timeout tm) { + double t = luaL_optnumber(L, 2, -1); + const char *mode = luaL_optstring(L, 3, "b"); + switch (*mode) { + case 'b': + tm->block = t; + break; + case 'r': case 't': + tm->total = t; + break; + default: + luaL_argcheck(L, 0, 3, "invalid timeout mode"); + break; + } + lua_pushnumber(L, 1); + return 1; +} + +/*=========================================================================*\ +* Test support functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Returns the time the system has been up, in secconds. +\*-------------------------------------------------------------------------*/ +static int timeout_lua_gettime(lua_State *L) +{ + lua_pushnumber(L, timeout_gettime()); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Sleep for n seconds. +\*-------------------------------------------------------------------------*/ +#ifdef _WIN32 +int timeout_lua_sleep(lua_State *L) +{ + double n = luaL_checknumber(L, 1); + if (n < 0.0) n = 0.0; + if (n < DBL_MAX/1000.0) n *= 1000.0; + if (n > INT_MAX) n = INT_MAX; + Sleep((int)n); + return 0; +} +#else +int timeout_lua_sleep(lua_State *L) +{ + double n = luaL_checknumber(L, 1); + struct timespec t, r; + if (n < 0.0) n = 0.0; + if (n > INT_MAX) n = INT_MAX; + t.tv_sec = (int) n; + n -= t.tv_sec; + t.tv_nsec = (int) (n * 1000000000); + if (t.tv_nsec >= 1000000000) t.tv_nsec = 999999999; + while (nanosleep(&t, &r) != 0) { + t.tv_sec = r.tv_sec; + t.tv_nsec = r.tv_nsec; + } + return 0; +} +#endif diff --git a/external/lua/luasocket/timeout.h b/external/lua/luasocket/timeout.h new file mode 100644 index 0000000000..6715ca70a1 --- /dev/null +++ b/external/lua/luasocket/timeout.h @@ -0,0 +1,28 @@ +#ifndef TIMEOUT_H +#define TIMEOUT_H +/*=========================================================================*\ +* Timeout management functions +* LuaSocket toolkit +\*=========================================================================*/ +#include "lua.h" + +/* timeout control structure */ +typedef struct t_timeout_ { + double block; /* maximum time for blocking calls */ + double total; /* total number of miliseconds for operation */ + double start; /* time of start of operation */ +} t_timeout; +typedef t_timeout *p_timeout; + +int timeout_open(lua_State *L); +void timeout_init(p_timeout tm, double block, double total); +double timeout_get(p_timeout tm); +double timeout_getretry(p_timeout tm); +p_timeout timeout_markstart(p_timeout tm); +double timeout_getstart(p_timeout tm); +double timeout_gettime(void); +int timeout_meth_settimeout(lua_State *L, p_timeout tm); + +#define timeout_iszero(tm) ((tm)->block == 0.0) + +#endif /* TIMEOUT_H */ diff --git a/external/lua/luasocket/udp.c b/external/lua/luasocket/udp.c new file mode 100644 index 0000000000..c51bd9ddd2 --- /dev/null +++ b/external/lua/luasocket/udp.c @@ -0,0 +1,468 @@ +/*=========================================================================*\ +* UDP object +* LuaSocket toolkit +\*=========================================================================*/ +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "auxiliar.h" +#include "socket.h" +#include "inet.h" +#include "options.h" +#include "udp.h" + +/* min and max macros */ +#ifndef MIN +#define MIN(x, y) ((x) < (y) ? x : y) +#endif +#ifndef MAX +#define MAX(x, y) ((x) > (y) ? x : y) +#endif + +/*=========================================================================*\ +* Internal function prototypes +\*=========================================================================*/ +static int global_create(lua_State *L); +static int global_create6(lua_State *L); +static int meth_send(lua_State *L); +static int meth_sendto(lua_State *L); +static int meth_receive(lua_State *L); +static int meth_receivefrom(lua_State *L); +static int meth_getfamily(lua_State *L); +static int meth_getsockname(lua_State *L); +static int meth_getpeername(lua_State *L); +static int meth_setsockname(lua_State *L); +static int meth_setpeername(lua_State *L); +static int meth_close(lua_State *L); +static int meth_setoption(lua_State *L); +static int meth_getoption(lua_State *L); +static int meth_settimeout(lua_State *L); +static int meth_getfd(lua_State *L); +static int meth_setfd(lua_State *L); +static int meth_dirty(lua_State *L); + +/* udp object methods */ +static luaL_Reg udp_methods[] = { + {"__gc", meth_close}, + {"__tostring", auxiliar_tostring}, + {"close", meth_close}, + {"dirty", meth_dirty}, + {"getfamily", meth_getfamily}, + {"getfd", meth_getfd}, + {"getpeername", meth_getpeername}, + {"getsockname", meth_getsockname}, + {"receive", meth_receive}, + {"receivefrom", meth_receivefrom}, + {"send", meth_send}, + {"sendto", meth_sendto}, + {"setfd", meth_setfd}, + {"setoption", meth_setoption}, + {"getoption", meth_getoption}, + {"setpeername", meth_setpeername}, + {"setsockname", meth_setsockname}, + {"settimeout", meth_settimeout}, + {NULL, NULL} +}; + +/* socket options for setoption */ +static t_opt optset[] = { + {"dontroute", opt_set_dontroute}, + {"broadcast", opt_set_broadcast}, + {"reuseaddr", opt_set_reuseaddr}, + {"reuseport", opt_set_reuseport}, + {"ip-multicast-if", opt_set_ip_multicast_if}, + {"ip-multicast-ttl", opt_set_ip_multicast_ttl}, + {"ip-multicast-loop", opt_set_ip_multicast_loop}, + {"ip-add-membership", opt_set_ip_add_membership}, + {"ip-drop-membership", opt_set_ip_drop_membersip}, + {"ipv6-v6only", opt_set_ip6_v6only}, + {NULL, NULL} +}; + +/* socket options for getoption */ +static t_opt optget[] = { + {"ip-multicast-if", opt_get_ip_multicast_if}, + {"ip-multicast-loop", opt_get_ip_multicast_loop}, + {NULL, NULL} +}; + +/* functions in library namespace */ +static luaL_Reg func[] = { + {"udp", global_create}, + {"udp6", global_create6}, + {NULL, NULL} +}; + +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +int udp_open(lua_State *L) +{ + /* create classes */ + auxiliar_newclass(L, "udp{connected}", udp_methods); + auxiliar_newclass(L, "udp{unconnected}", udp_methods); + /* create class groups */ + auxiliar_add2group(L, "udp{connected}", "udp{any}"); + auxiliar_add2group(L, "udp{unconnected}", "udp{any}"); + auxiliar_add2group(L, "udp{connected}", "select{able}"); + auxiliar_add2group(L, "udp{unconnected}", "select{able}"); + /* define library functions */ + luaL_openlib(L, NULL, func, 0); + return 0; +} + +/*=========================================================================*\ +* Lua methods +\*=========================================================================*/ +const char *udp_strerror(int err) { + /* a 'closed' error on an unconnected means the target address was not + * accepted by the transport layer */ + if (err == IO_CLOSED) return "refused"; + else return socket_strerror(err); +} + +/*-------------------------------------------------------------------------*\ +* Send data through connected udp socket +\*-------------------------------------------------------------------------*/ +static int meth_send(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1); + p_timeout tm = &udp->tm; + size_t count, sent = 0; + int err; + const char *data = luaL_checklstring(L, 2, &count); + timeout_markstart(tm); + err = socket_send(&udp->sock, data, count, &sent, tm); + if (err != IO_DONE) { + lua_pushnil(L); + lua_pushstring(L, udp_strerror(err)); + return 2; + } + lua_pushnumber(L, (lua_Number) sent); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Send data through unconnected udp socket +\*-------------------------------------------------------------------------*/ +static int meth_sendto(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); + size_t count, sent = 0; + const char *data = luaL_checklstring(L, 2, &count); + const char *ip = luaL_checkstring(L, 3); + unsigned short port = (unsigned short) luaL_checknumber(L, 4); + p_timeout tm = &udp->tm; + int err; + switch (udp->family) { + case PF_INET: { + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + if (!my_inet_pton(AF_INET, ip, &addr.sin_addr)) + luaL_argerror(L, 3, "invalid ip address"); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); + timeout_markstart(tm); + err = socket_sendto(&udp->sock, data, count, &sent, + (SA *) &addr, sizeof(addr), tm); + break; + } + case PF_INET6: { + struct sockaddr_in6 addr; + memset(&addr, 0, sizeof(addr)); + if (!my_inet_pton(AF_INET6, ip, &addr.sin6_addr)) + luaL_argerror(L, 3, "invalid ip address"); + addr.sin6_family = AF_INET6; + addr.sin6_port = htons(port); + timeout_markstart(tm); + err = socket_sendto(&udp->sock, data, count, &sent, + (SA *) &addr, sizeof(addr), tm); + break; + } + default: + lua_pushnil(L); + lua_pushfstring(L, "unknown family %d", udp->family); + return 2; + } + if (err != IO_DONE) { + lua_pushnil(L); + lua_pushstring(L, udp_strerror(err)); + return 2; + } + lua_pushnumber(L, (lua_Number) sent); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Receives data from a UDP socket +\*-------------------------------------------------------------------------*/ +static int meth_receive(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + char buffer[UDP_DATAGRAMSIZE]; + size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); + int err; + p_timeout tm = &udp->tm; + count = MIN(count, sizeof(buffer)); + timeout_markstart(tm); + err = socket_recv(&udp->sock, buffer, count, &got, tm); + /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ + if (err == IO_CLOSED) + err = IO_DONE; + if (err != IO_DONE) { + lua_pushnil(L); + lua_pushstring(L, udp_strerror(err)); + return 2; + } + lua_pushlstring(L, buffer, got); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Receives data and sender from a UDP socket +\*-------------------------------------------------------------------------*/ +static int meth_receivefrom(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); + char buffer[UDP_DATAGRAMSIZE]; + size_t got, count = (size_t) luaL_optnumber(L, 2, sizeof(buffer)); + int err; + p_timeout tm = &udp->tm; + timeout_markstart(tm); + count = MIN(count, sizeof(buffer)); + switch (udp->family) { + case PF_INET: { + struct sockaddr_in addr; + socklen_t addr_len = sizeof(addr); + err = socket_recvfrom(&udp->sock, buffer, count, &got, + (SA *) &addr, &addr_len, tm); + /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ + if (err == IO_CLOSED) + err = IO_DONE; + if (err == IO_DONE) { + char addrstr[INET_ADDRSTRLEN]; + lua_pushlstring(L, buffer, got); + if (!my_inet_ntop(AF_INET, &addr.sin_addr, + addrstr, sizeof(addrstr))) { + lua_pushnil(L); + lua_pushstring(L, "invalid source address"); + return 2; + } + lua_pushstring(L, addrstr); + lua_pushnumber(L, ntohs(addr.sin_port)); + return 3; + } + break; + } + case PF_INET6: { + struct sockaddr_in6 addr; + socklen_t addr_len = sizeof(addr); + err = socket_recvfrom(&udp->sock, buffer, count, &got, + (SA *) &addr, &addr_len, tm); + /* Unlike TCP, recv() of zero is not closed, but a zero-length packet. */ + if (err == IO_CLOSED) + err = IO_DONE; + if (err == IO_DONE) { + char addrstr[INET6_ADDRSTRLEN]; + lua_pushlstring(L, buffer, got); + if (!my_inet_ntop(AF_INET6, &addr.sin6_addr, + addrstr, sizeof(addrstr))) { + lua_pushnil(L); + lua_pushstring(L, "invalid source address"); + return 2; + } + lua_pushstring(L, addrstr); + lua_pushnumber(L, ntohs(addr.sin6_port)); + return 3; + } + break; + } + default: + lua_pushnil(L); + lua_pushfstring(L, "unknown family %d", udp->family); + return 2; + } + lua_pushnil(L); + lua_pushstring(L, udp_strerror(err)); + return 2; +} + +/*-------------------------------------------------------------------------*\ +* Returns family as string +\*-------------------------------------------------------------------------*/ +static int meth_getfamily(lua_State *L) +{ + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + if (udp->family == PF_INET6) { + lua_pushliteral(L, "inet6"); + return 1; + } else { + lua_pushliteral(L, "inet4"); + return 1; + } +} + +/*-------------------------------------------------------------------------*\ +* Select support methods +\*-------------------------------------------------------------------------*/ +static int meth_getfd(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + lua_pushnumber(L, (int) udp->sock); + return 1; +} + +/* this is very dangerous, but can be handy for those that are brave enough */ +static int meth_setfd(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + udp->sock = (t_socket) luaL_checknumber(L, 2); + return 0; +} + +static int meth_dirty(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + (void) udp; + lua_pushboolean(L, 0); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Just call inet methods +\*-------------------------------------------------------------------------*/ +static int meth_getpeername(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{connected}", 1); + return inet_meth_getpeername(L, &udp->sock, udp->family); +} + +static int meth_getsockname(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + return inet_meth_getsockname(L, &udp->sock, udp->family); +} + +/*-------------------------------------------------------------------------*\ +* Just call option handler +\*-------------------------------------------------------------------------*/ +static int meth_setoption(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + return opt_meth_setoption(L, optset, &udp->sock); +} + +/*-------------------------------------------------------------------------*\ +* Just call option handler +\*-------------------------------------------------------------------------*/ +static int meth_getoption(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + return opt_meth_getoption(L, optget, &udp->sock); +} + +/*-------------------------------------------------------------------------*\ +* Just call tm methods +\*-------------------------------------------------------------------------*/ +static int meth_settimeout(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + return timeout_meth_settimeout(L, &udp->tm); +} + +/*-------------------------------------------------------------------------*\ +* Turns a master udp object into a client object. +\*-------------------------------------------------------------------------*/ +static int meth_setpeername(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + p_timeout tm = &udp->tm; + const char *address = luaL_checkstring(L, 2); + int connecting = strcmp(address, "*"); + const char *port = connecting? luaL_checkstring(L, 3): "0"; + struct addrinfo connecthints; + const char *err; + memset(&connecthints, 0, sizeof(connecthints)); + connecthints.ai_socktype = SOCK_DGRAM; + /* make sure we try to connect only to the same family */ + connecthints.ai_family = udp->family; + if (connecting) { + err = inet_tryconnect(&udp->sock, address, port, tm, &connecthints); + if (err) { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } + auxiliar_setclass(L, "udp{connected}", 1); + } else { + /* we ignore possible errors because Mac OS X always + * returns EAFNOSUPPORT */ + inet_trydisconnect(&udp->sock, udp->family, tm); + auxiliar_setclass(L, "udp{unconnected}", 1); + } + /* change class to connected or unconnected depending on address */ + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Closes socket used by object +\*-------------------------------------------------------------------------*/ +static int meth_close(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkgroup(L, "udp{any}", 1); + socket_destroy(&udp->sock); + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Turns a master object into a server object +\*-------------------------------------------------------------------------*/ +static int meth_setsockname(lua_State *L) { + p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1); + const char *address = luaL_checkstring(L, 2); + const char *port = luaL_checkstring(L, 3); + const char *err; + struct addrinfo bindhints; + memset(&bindhints, 0, sizeof(bindhints)); + bindhints.ai_socktype = SOCK_DGRAM; + bindhints.ai_family = udp->family; + bindhints.ai_flags = AI_PASSIVE; + err = inet_trybind(&udp->sock, address, port, &bindhints); + if (err) { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } + lua_pushnumber(L, 1); + return 1; +} + +/*=========================================================================*\ +* Library functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Creates a master udp object +\*-------------------------------------------------------------------------*/ +static int udp_create(lua_State *L, int family) { + t_socket sock; + const char *err = inet_trycreate(&sock, family, SOCK_DGRAM); + /* try to allocate a system socket */ + if (!err) { + /* allocate udp object */ + p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); + auxiliar_setclass(L, "udp{unconnected}", -1); + /* initialize remaining structure fields */ + socket_setnonblocking(&sock); + if (family == PF_INET6) { + int yes = 1; + setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, + (void *)&yes, sizeof(yes)); + } + udp->sock = sock; + timeout_init(&udp->tm, -1, -1); + udp->family = family; + return 1; + } else { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } +} + +static int global_create(lua_State *L) { + return udp_create(L, AF_INET); +} + +static int global_create6(lua_State *L) { + return udp_create(L, AF_INET6); +} diff --git a/external/lua/luasocket/udp.h b/external/lua/luasocket/udp.h new file mode 100644 index 0000000000..2b831a5371 --- /dev/null +++ b/external/lua/luasocket/udp.h @@ -0,0 +1,32 @@ +#ifndef UDP_H +#define UDP_H +/*=========================================================================*\ +* UDP object +* LuaSocket toolkit +* +* The udp.h module provides LuaSocket with support for UDP protocol +* (AF_INET, SOCK_DGRAM). +* +* Two classes are defined: connected and unconnected. UDP objects are +* originally unconnected. They can be "connected" to a given address +* with a call to the setpeername function. The same function can be used to +* break the connection. +\*=========================================================================*/ +#include "lua.h" + +#include "timeout.h" +#include "socket.h" + +/* can't be larger than wsocket.c MAXCHUNK!!! */ +#define UDP_DATAGRAMSIZE 8192 + +typedef struct t_udp_ { + t_socket sock; + t_timeout tm; + int family; +} t_udp; +typedef t_udp *p_udp; + +int udp_open(lua_State *L); + +#endif /* UDP_H */ diff --git a/external/lua/luasocket/unix.c b/external/lua/luasocket/unix.c new file mode 100644 index 0000000000..28d4cc7b84 --- /dev/null +++ b/external/lua/luasocket/unix.c @@ -0,0 +1,340 @@ +/*=========================================================================*\ +* Unix domain socket +* LuaSocket toolkit +\*=========================================================================*/ +#include + +#include "lua.h" +#include "lauxlib.h" + +#include "auxiliar.h" +#include "socket.h" +#include "options.h" +#include "unix.h" +#include + +/*=========================================================================*\ +* Internal function prototypes +\*=========================================================================*/ +static int global_create(lua_State *L); +static int meth_connect(lua_State *L); +static int meth_listen(lua_State *L); +static int meth_bind(lua_State *L); +static int meth_send(lua_State *L); +static int meth_shutdown(lua_State *L); +static int meth_receive(lua_State *L); +static int meth_accept(lua_State *L); +static int meth_close(lua_State *L); +static int meth_setoption(lua_State *L); +static int meth_settimeout(lua_State *L); +static int meth_getfd(lua_State *L); +static int meth_setfd(lua_State *L); +static int meth_dirty(lua_State *L); +static int meth_getstats(lua_State *L); +static int meth_setstats(lua_State *L); + +static const char *unix_tryconnect(p_unix un, const char *path); +static const char *unix_trybind(p_unix un, const char *path); + +/* unix object methods */ +static luaL_Reg unix_methods[] = { + {"__gc", meth_close}, + {"__tostring", auxiliar_tostring}, + {"accept", meth_accept}, + {"bind", meth_bind}, + {"close", meth_close}, + {"connect", meth_connect}, + {"dirty", meth_dirty}, + {"getfd", meth_getfd}, + {"getstats", meth_getstats}, + {"setstats", meth_setstats}, + {"listen", meth_listen}, + {"receive", meth_receive}, + {"send", meth_send}, + {"setfd", meth_setfd}, + {"setoption", meth_setoption}, + {"setpeername", meth_connect}, + {"setsockname", meth_bind}, + {"settimeout", meth_settimeout}, + {"shutdown", meth_shutdown}, + {NULL, NULL} +}; + +/* socket option handlers */ +static t_opt optset[] = { + {"keepalive", opt_set_keepalive}, + {"reuseaddr", opt_set_reuseaddr}, + {"linger", opt_set_linger}, + {NULL, NULL} +}; + +/* our socket creation function */ +static luaL_Reg func[] = { + {"unix", global_create}, + {NULL, NULL} +}; + + +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +int luaopen_socket_unix(lua_State *L) { + /* create classes */ + auxiliar_newclass(L, "unix{master}", unix_methods); + auxiliar_newclass(L, "unix{client}", unix_methods); + auxiliar_newclass(L, "unix{server}", unix_methods); + /* create class groups */ + auxiliar_add2group(L, "unix{master}", "unix{any}"); + auxiliar_add2group(L, "unix{client}", "unix{any}"); + auxiliar_add2group(L, "unix{server}", "unix{any}"); + /* make sure the function ends up in the package table */ + luaL_openlib(L, "socket", func, 0); + /* return the function instead of the 'socket' table */ + lua_pushstring(L, "unix"); + lua_gettable(L, -2); + return 1; +} + +/*=========================================================================*\ +* Lua methods +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Just call buffered IO methods +\*-------------------------------------------------------------------------*/ +static int meth_send(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); + return buffer_meth_send(L, &un->buf); +} + +static int meth_receive(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); + return buffer_meth_receive(L, &un->buf); +} + +static int meth_getstats(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); + return buffer_meth_getstats(L, &un->buf); +} + +static int meth_setstats(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); + return buffer_meth_setstats(L, &un->buf); +} + +/*-------------------------------------------------------------------------*\ +* Just call option handler +\*-------------------------------------------------------------------------*/ +static int meth_setoption(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); + return opt_meth_setoption(L, optset, &un->sock); +} + +/*-------------------------------------------------------------------------*\ +* Select support methods +\*-------------------------------------------------------------------------*/ +static int meth_getfd(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); + lua_pushnumber(L, (int) un->sock); + return 1; +} + +/* this is very dangerous, but can be handy for those that are brave enough */ +static int meth_setfd(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); + un->sock = (t_socket) luaL_checknumber(L, 2); + return 0; +} + +static int meth_dirty(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); + lua_pushboolean(L, !buffer_isempty(&un->buf)); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Waits for and returns a client object attempting connection to the +* server object +\*-------------------------------------------------------------------------*/ +static int meth_accept(lua_State *L) { + p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1); + p_timeout tm = timeout_markstart(&server->tm); + t_socket sock; + int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); + /* if successful, push client socket */ + if (err == IO_DONE) { + p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix)); + auxiliar_setclass(L, "unix{client}", -1); + /* initialize structure fields */ + socket_setnonblocking(&sock); + clnt->sock = sock; + io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv, + (p_error) socket_ioerror, &clnt->sock); + timeout_init(&clnt->tm, -1, -1); + buffer_init(&clnt->buf, &clnt->io, &clnt->tm); + return 1; + } else { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(err)); + return 2; + } +} + +/*-------------------------------------------------------------------------*\ +* Binds an object to an address +\*-------------------------------------------------------------------------*/ +static const char *unix_trybind(p_unix un, const char *path) { + struct sockaddr_un local; + size_t len = strlen(path); + int err; + if (len >= sizeof(local.sun_path)) return "path too long"; + memset(&local, 0, sizeof(local)); + strcpy(local.sun_path, path); + local.sun_family = AF_UNIX; +#ifdef UNIX_HAS_SUN_LEN + local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) + + len + 1; + err = socket_bind(&un->sock, (SA *) &local, local.sun_len); + +#else + err = socket_bind(&un->sock, (SA *) &local, + (socklen_t)(sizeof(local.sun_family) + len)); +#endif + if (err != IO_DONE) socket_destroy(&un->sock); + return socket_strerror(err); +} + +static int meth_bind(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); + const char *path = luaL_checkstring(L, 2); + const char *err = unix_trybind(un, path); + if (err) { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Turns a master unix object into a client object. +\*-------------------------------------------------------------------------*/ +static const char *unix_tryconnect(p_unix un, const char *path) +{ + struct sockaddr_un remote; + int err; + size_t len = strlen(path); + if (len >= sizeof(remote.sun_path)) return "path too long"; + memset(&remote, 0, sizeof(remote)); + strcpy(remote.sun_path, path); + remote.sun_family = AF_UNIX; + timeout_markstart(&un->tm); +#ifdef UNIX_HAS_SUN_LEN + remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) + + len + 1; + err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); +#else + err = socket_connect(&un->sock, (SA *) &remote, + (socklen_t)(sizeof(remote.sun_family) + len), &un->tm); +#endif + if (err != IO_DONE) socket_destroy(&un->sock); + return socket_strerror(err); +} + +static int meth_connect(lua_State *L) +{ + p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); + const char *path = luaL_checkstring(L, 2); + const char *err = unix_tryconnect(un, path); + if (err) { + lua_pushnil(L); + lua_pushstring(L, err); + return 2; + } + /* turn master object into a client object */ + auxiliar_setclass(L, "unix{client}", 1); + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Closes socket used by object +\*-------------------------------------------------------------------------*/ +static int meth_close(lua_State *L) +{ + p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); + socket_destroy(&un->sock); + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Puts the sockt in listen mode +\*-------------------------------------------------------------------------*/ +static int meth_listen(lua_State *L) +{ + p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); + int backlog = (int) luaL_optnumber(L, 2, 32); + int err = socket_listen(&un->sock, backlog); + if (err != IO_DONE) { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(err)); + return 2; + } + /* turn master object into a server object */ + auxiliar_setclass(L, "unix{server}", 1); + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Shuts the connection down partially +\*-------------------------------------------------------------------------*/ +static int meth_shutdown(lua_State *L) +{ + /* SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, so we can use method index directly */ + static const char* methods[] = { "receive", "send", "both", NULL }; + p_unix tcp = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); + int how = luaL_checkoption(L, 2, "both", methods); + socket_shutdown(&tcp->sock, how); + lua_pushnumber(L, 1); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Just call tm methods +\*-------------------------------------------------------------------------*/ +static int meth_settimeout(lua_State *L) { + p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); + return timeout_meth_settimeout(L, &un->tm); +} + +/*=========================================================================*\ +* Library functions +\*=========================================================================*/ +/*-------------------------------------------------------------------------*\ +* Creates a master unix object +\*-------------------------------------------------------------------------*/ +static int global_create(lua_State *L) { + t_socket sock; + int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0); + /* try to allocate a system socket */ + if (err == IO_DONE) { + /* allocate unix object */ + p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); + /* set its type as master object */ + auxiliar_setclass(L, "unix{master}", -1); + /* initialize remaining structure fields */ + socket_setnonblocking(&sock); + un->sock = sock; + io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv, + (p_error) socket_ioerror, &un->sock); + timeout_init(&un->tm, -1, -1); + buffer_init(&un->buf, &un->io, &un->tm); + return 1; + } else { + lua_pushnil(L); + lua_pushstring(L, socket_strerror(err)); + return 2; + } +} diff --git a/external/lua/luasocket/unix.h b/external/lua/luasocket/unix.h new file mode 100644 index 0000000000..86a961b747 --- /dev/null +++ b/external/lua/luasocket/unix.h @@ -0,0 +1,26 @@ +#ifndef UNIX_H +#define UNIX_H +/*=========================================================================*\ +* Unix domain object +* LuaSocket toolkit +* +* This module is just an example of how to extend LuaSocket with a new +* domain. +\*=========================================================================*/ +#include "lua.h" +#include "luasocket.h" +#include "luasocket_buffer.h" +#include "timeout.h" +#include "socket.h" + +typedef struct t_unix_ { + t_socket sock; + t_io io; + t_buffer buf; + t_timeout tm; +} t_unix; +typedef t_unix *p_unix; + +LUASOCKET_API int luaopen_socket_unix(lua_State *L); + +#endif /* UNIX_H */ diff --git a/external/lua/luasocket/usocket.c b/external/lua/luasocket/usocket.c new file mode 100644 index 0000000000..096ecd00b6 --- /dev/null +++ b/external/lua/luasocket/usocket.c @@ -0,0 +1,449 @@ +/*=========================================================================*\ +* Socket compatibilization module for Unix +* LuaSocket toolkit +* +* The code is now interrupt-safe. +* The penalty of calling select to avoid busy-wait is only paid when +* the I/O call fail in the first place. +\*=========================================================================*/ +#include +#include + +#include "socket.h" + +/*-------------------------------------------------------------------------*\ +* Wait for readable/writable/connected socket with timeout +\*-------------------------------------------------------------------------*/ +#ifndef SOCKET_SELECT +#include + +#define WAITFD_R POLLIN +#define WAITFD_W POLLOUT +#define WAITFD_C (POLLIN|POLLOUT) +int socket_waitfd(p_socket ps, int sw, p_timeout tm) { + int ret; + struct pollfd pfd; + pfd.fd = *ps; + pfd.events = sw; + pfd.revents = 0; + if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ + do { + int t = (int)(timeout_getretry(tm)*1e3); + ret = poll(&pfd, 1, t >= 0? t: -1); + } while (ret == -1 && errno == EINTR); + if (ret == -1) return errno; + if (ret == 0) return IO_TIMEOUT; + if (sw == WAITFD_C && (pfd.revents & (POLLIN|POLLERR))) return IO_CLOSED; + return IO_DONE; +} +#else + +#define WAITFD_R 1 +#define WAITFD_W 2 +#define WAITFD_C (WAITFD_R|WAITFD_W) + +int socket_waitfd(p_socket ps, int sw, p_timeout tm) { + int ret; + fd_set rfds, wfds, *rp, *wp; + struct timeval tv, *tp; + double t; + if (*ps >= FD_SETSIZE) return EINVAL; + if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ + do { + /* must set bits within loop, because select may have modifed them */ + rp = wp = NULL; + if (sw & WAITFD_R) { FD_ZERO(&rfds); FD_SET(*ps, &rfds); rp = &rfds; } + if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } + t = timeout_getretry(tm); + tp = NULL; + if (t >= 0.0) { + tv.tv_sec = (int)t; + tv.tv_usec = (int)((t-tv.tv_sec)*1.0e6); + tp = &tv; + } + ret = select(*ps+1, rp, wp, NULL, tp); + } while (ret == -1 && errno == EINTR); + if (ret == -1) return errno; + if (ret == 0) return IO_TIMEOUT; + if (sw == WAITFD_C && FD_ISSET(*ps, &rfds)) return IO_CLOSED; + return IO_DONE; +} +#endif + + +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +int socket_open(void) { + /* instals a handler to ignore sigpipe or it will crash us */ + signal(SIGPIPE, SIG_IGN); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Close module +\*-------------------------------------------------------------------------*/ +int socket_close(void) { + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Close and inutilize socket +\*-------------------------------------------------------------------------*/ +void socket_destroy(p_socket ps) { + if (*ps != SOCKET_INVALID) { + socket_setblocking(ps); + close(*ps); + *ps = SOCKET_INVALID; + } +} + +/*-------------------------------------------------------------------------*\ +* Select with timeout control +\*-------------------------------------------------------------------------*/ +int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, + p_timeout tm) { + int ret; + do { + struct timeval tv; + double t = timeout_getretry(tm); + tv.tv_sec = (int) t; + tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); + /* timeout = 0 means no wait */ + ret = select(n, rfds, wfds, efds, t >= 0.0 ? &tv: NULL); + } while (ret < 0 && errno == EINTR); + return ret; +} + +/*-------------------------------------------------------------------------*\ +* Creates and sets up a socket +\*-------------------------------------------------------------------------*/ +int socket_create(p_socket ps, int domain, int type, int protocol) { + *ps = socket(domain, type, protocol); + if (*ps != SOCKET_INVALID) return IO_DONE; + else return errno; +} + +/*-------------------------------------------------------------------------*\ +* Binds or returns error message +\*-------------------------------------------------------------------------*/ +int socket_bind(p_socket ps, SA *addr, socklen_t len) { + int err = IO_DONE; + socket_setblocking(ps); + if (bind(*ps, addr, len) < 0) err = errno; + socket_setnonblocking(ps); + return err; +} + +/*-------------------------------------------------------------------------*\ +* +\*-------------------------------------------------------------------------*/ +int socket_listen(p_socket ps, int backlog) { + int err = IO_DONE; + socket_setblocking(ps); + if (listen(*ps, backlog)) err = errno; + socket_setnonblocking(ps); + return err; +} + +/*-------------------------------------------------------------------------*\ +* +\*-------------------------------------------------------------------------*/ +void socket_shutdown(p_socket ps, int how) { + socket_setblocking(ps); + shutdown(*ps, how); + socket_setnonblocking(ps); +} + +/*-------------------------------------------------------------------------*\ +* Connects or returns error message +\*-------------------------------------------------------------------------*/ +int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { + int err; + /* avoid calling on closed sockets */ + if (*ps == SOCKET_INVALID) return IO_CLOSED; + /* call connect until done or failed without being interrupted */ + do if (connect(*ps, addr, len) == 0) return IO_DONE; + while ((err = errno) == EINTR); + /* if connection failed immediately, return error code */ + if (err != EINPROGRESS && err != EAGAIN) return err; + /* zero timeout case optimization */ + if (timeout_iszero(tm)) return IO_TIMEOUT; + /* wait until we have the result of the connection attempt or timeout */ + err = socket_waitfd(ps, WAITFD_C, tm); + if (err == IO_CLOSED) { + if (recv(*ps, (char *) &err, 0, 0) == 0) return IO_DONE; + else return errno; + } else return err; +} + +/*-------------------------------------------------------------------------*\ +* Accept with timeout +\*-------------------------------------------------------------------------*/ +int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, p_timeout tm) { + if (*ps == SOCKET_INVALID) return IO_CLOSED; + for ( ;; ) { + int err; + if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE; + err = errno; + if (err == EINTR) continue; + if (err != EAGAIN && err != ECONNABORTED) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + } + /* can't reach here */ + return IO_UNKNOWN; +} + +/*-------------------------------------------------------------------------*\ +* Send with timeout +\*-------------------------------------------------------------------------*/ +int socket_send(p_socket ps, const char *data, size_t count, + size_t *sent, p_timeout tm) +{ + int err; + *sent = 0; + /* avoid making system calls on closed sockets */ + if (*ps == SOCKET_INVALID) return IO_CLOSED; + /* loop until we send something or we give up on error */ + for ( ;; ) { + long put = (long) send(*ps, data, count, 0); + /* if we sent anything, we are done */ + if (put >= 0) { + *sent = put; + return IO_DONE; + } + err = errno; + /* EPIPE means the connection was closed */ + if (err == EPIPE) return IO_CLOSED; + /* we call was interrupted, just try again */ + if (err == EINTR) continue; + /* if failed fatal reason, report error */ + if (err != EAGAIN) return err; + /* wait until we can send something or we timeout */ + if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; + } + /* can't reach here */ + return IO_UNKNOWN; +} + +/*-------------------------------------------------------------------------*\ +* Sendto with timeout +\*-------------------------------------------------------------------------*/ +int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, + SA *addr, socklen_t len, p_timeout tm) +{ + int err; + *sent = 0; + if (*ps == SOCKET_INVALID) return IO_CLOSED; + for ( ;; ) { + long put = (long) sendto(*ps, data, count, 0, addr, len); + if (put >= 0) { + *sent = put; + return IO_DONE; + } + err = errno; + if (err == EPIPE) return IO_CLOSED; + if (err == EINTR) continue; + if (err != EAGAIN) return err; + if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; + } + return IO_UNKNOWN; +} + +/*-------------------------------------------------------------------------*\ +* Receive with timeout +\*-------------------------------------------------------------------------*/ +int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { + int err; + *got = 0; + if (*ps == SOCKET_INVALID) return IO_CLOSED; + for ( ;; ) { + long taken = (long) recv(*ps, data, count, 0); + if (taken > 0) { + *got = taken; + return IO_DONE; + } + err = errno; + if (taken == 0) return IO_CLOSED; + if (err == EINTR) continue; + if (err != EAGAIN) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + } + return IO_UNKNOWN; +} + +/*-------------------------------------------------------------------------*\ +* Recvfrom with timeout +\*-------------------------------------------------------------------------*/ +int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, + SA *addr, socklen_t *len, p_timeout tm) { + int err; + *got = 0; + if (*ps == SOCKET_INVALID) return IO_CLOSED; + for ( ;; ) { + long taken = (long) recvfrom(*ps, data, count, 0, addr, len); + if (taken > 0) { + *got = taken; + return IO_DONE; + } + err = errno; + if (taken == 0) return IO_CLOSED; + if (err == EINTR) continue; + if (err != EAGAIN) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + } + return IO_UNKNOWN; +} + + +/*-------------------------------------------------------------------------*\ +* Write with timeout +* +* socket_read and socket_write are cut-n-paste of socket_send and socket_recv, +* with send/recv replaced with write/read. We can't just use write/read +* in the socket version, because behaviour when size is zero is different. +\*-------------------------------------------------------------------------*/ +int socket_write(p_socket ps, const char *data, size_t count, + size_t *sent, p_timeout tm) +{ + int err; + *sent = 0; + /* avoid making system calls on closed sockets */ + if (*ps == SOCKET_INVALID) return IO_CLOSED; + /* loop until we send something or we give up on error */ + for ( ;; ) { + long put = (long) write(*ps, data, count); + /* if we sent anything, we are done */ + if (put >= 0) { + *sent = put; + return IO_DONE; + } + err = errno; + /* EPIPE means the connection was closed */ + if (err == EPIPE) return IO_CLOSED; + /* we call was interrupted, just try again */ + if (err == EINTR) continue; + /* if failed fatal reason, report error */ + if (err != EAGAIN) return err; + /* wait until we can send something or we timeout */ + if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; + } + /* can't reach here */ + return IO_UNKNOWN; +} + +/*-------------------------------------------------------------------------*\ +* Read with timeout +* See note for socket_write +\*-------------------------------------------------------------------------*/ +int socket_read(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { + int err; + *got = 0; + if (*ps == SOCKET_INVALID) return IO_CLOSED; + for ( ;; ) { + long taken = (long) read(*ps, data, count); + if (taken > 0) { + *got = taken; + return IO_DONE; + } + err = errno; + if (taken == 0) return IO_CLOSED; + if (err == EINTR) continue; + if (err != EAGAIN) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + } + return IO_UNKNOWN; +} + +/*-------------------------------------------------------------------------*\ +* Put socket into blocking mode +\*-------------------------------------------------------------------------*/ +void socket_setblocking(p_socket ps) { + int flags = fcntl(*ps, F_GETFL, 0); + flags &= (~(O_NONBLOCK)); + fcntl(*ps, F_SETFL, flags); +} + +/*-------------------------------------------------------------------------*\ +* Put socket into non-blocking mode +\*-------------------------------------------------------------------------*/ +void socket_setnonblocking(p_socket ps) { + int flags = fcntl(*ps, F_GETFL, 0); + flags |= O_NONBLOCK; + fcntl(*ps, F_SETFL, flags); +} + +/*-------------------------------------------------------------------------*\ +* DNS helpers +\*-------------------------------------------------------------------------*/ +int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { + *hp = gethostbyaddr(addr, len, AF_INET); + if (*hp) return IO_DONE; + else if (h_errno) return h_errno; + else if (errno) return errno; + else return IO_UNKNOWN; +} + +int socket_gethostbyname(const char *addr, struct hostent **hp) { + *hp = gethostbyname(addr); + if (*hp) return IO_DONE; + else if (h_errno) return h_errno; + else if (errno) return errno; + else return IO_UNKNOWN; +} + +/*-------------------------------------------------------------------------*\ +* Error translation functions +* Make sure important error messages are standard +\*-------------------------------------------------------------------------*/ +const char *socket_hoststrerror(int err) { + if (err <= 0) return io_strerror(err); + switch (err) { + case HOST_NOT_FOUND: return "host not found"; + default: return hstrerror(err); + } +} + +const char *socket_strerror(int err) { + if (err <= 0) return io_strerror(err); + switch (err) { + case EADDRINUSE: return "address already in use"; + case EISCONN: return "already connected"; + case EACCES: return "permission denied"; + case ECONNREFUSED: return "connection refused"; + case ECONNABORTED: return "closed"; + case ECONNRESET: return "closed"; + case ETIMEDOUT: return "timeout"; + default: return strerror(err); + } +} + +const char *socket_ioerror(p_socket ps, int err) { + (void) ps; + return socket_strerror(err); +} + +const char *socket_gaistrerror(int err) { + if (err == 0) return NULL; + switch (err) { + case EAI_AGAIN: return "temporary failure in name resolution"; + case EAI_BADFLAGS: return "invalid value for ai_flags"; +#ifdef EAI_BADHINTS + case EAI_BADHINTS: return "invalid value for hints"; +#endif + case EAI_FAIL: return "non-recoverable failure in name resolution"; + case EAI_FAMILY: return "ai_family not supported"; + case EAI_MEMORY: return "memory allocation failure"; + case EAI_NONAME: + return "host or service not provided, or not known"; + case EAI_OVERFLOW: return "argument buffer overflow"; +#ifdef EAI_PROTOCOL + case EAI_PROTOCOL: return "resolved protocol is unknown"; +#endif + case EAI_SERVICE: return "service not supported for socket type"; + case EAI_SOCKTYPE: return "ai_socktype not supported"; + case EAI_SYSTEM: return strerror(errno); + default: return gai_strerror(err); + } +} + diff --git a/external/lua/luasocket/usocket.h b/external/lua/luasocket/usocket.h new file mode 100644 index 0000000000..8b3241bf3c --- /dev/null +++ b/external/lua/luasocket/usocket.h @@ -0,0 +1,43 @@ +#ifndef USOCKET_H +#define USOCKET_H +/*=========================================================================*\ +* Socket compatibilization module for Unix +* LuaSocket toolkit +\*=========================================================================*/ + +/*=========================================================================*\ +* BSD include files +\*=========================================================================*/ +/* error codes */ +#include +/* close function */ +#include +/* fnctnl function and associated constants */ +#include +/* struct sockaddr */ +#include +/* socket function */ +#include +/* struct timeval */ +#include +/* gethostbyname and gethostbyaddr functions */ +#include +/* sigpipe handling */ +#include +/* IP stuff*/ +#include +#include +/* TCP options (nagle algorithm disable) */ +#include + +#ifndef SO_REUSEPORT +#define SO_REUSEPORT SO_REUSEADDR +#endif + +typedef int t_socket; +typedef t_socket *p_socket; +typedef struct sockaddr_storage t_sockaddr_storage; + +#define SOCKET_INVALID (-1) + +#endif /* USOCKET_H */ diff --git a/external/lua/luasocket/wsocket.c b/external/lua/luasocket/wsocket.c new file mode 100644 index 0000000000..19b3f11b37 --- /dev/null +++ b/external/lua/luasocket/wsocket.c @@ -0,0 +1,413 @@ +/*=========================================================================*\ +* Socket compatibilization module for Win32 +* LuaSocket toolkit +* +* The penalty of calling select to avoid busy-wait is only paid when +* the I/O call fail in the first place. +\*=========================================================================*/ +#include + +#include "socket.h" + +/* WinSock doesn't have a strerror... */ +static const char *wstrerror(int err); + +/*-------------------------------------------------------------------------*\ +* Initializes module +\*-------------------------------------------------------------------------*/ +int socket_open(void) { + WSADATA wsaData; + WORD wVersionRequested = MAKEWORD(2, 0); + int err = WSAStartup(wVersionRequested, &wsaData ); + if (err != 0) return 0; + if ((LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) && + (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)) { + WSACleanup(); + return 0; + } + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Close module +\*-------------------------------------------------------------------------*/ +int socket_close(void) { + WSACleanup(); + return 1; +} + +/*-------------------------------------------------------------------------*\ +* Wait for readable/writable/connected socket with timeout +\*-------------------------------------------------------------------------*/ +#define WAITFD_R 1 +#define WAITFD_W 2 +#define WAITFD_E 4 +#define WAITFD_C (WAITFD_E|WAITFD_W) + +int socket_waitfd(p_socket ps, int sw, p_timeout tm) { + int ret; + fd_set rfds, wfds, efds, *rp = NULL, *wp = NULL, *ep = NULL; + struct timeval tv, *tp = NULL; + double t; + if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ + if (sw & WAITFD_R) { + FD_ZERO(&rfds); + FD_SET(*ps, &rfds); + rp = &rfds; + } + if (sw & WAITFD_W) { FD_ZERO(&wfds); FD_SET(*ps, &wfds); wp = &wfds; } + if (sw & WAITFD_C) { FD_ZERO(&efds); FD_SET(*ps, &efds); ep = &efds; } + if ((t = timeout_get(tm)) >= 0.0) { + tv.tv_sec = (int) t; + tv.tv_usec = (int) ((t-tv.tv_sec)*1.0e6); + tp = &tv; + } + ret = select(0, rp, wp, ep, tp); + if (ret == -1) return WSAGetLastError(); + if (ret == 0) return IO_TIMEOUT; + if (sw == WAITFD_C && FD_ISSET(*ps, &efds)) return IO_CLOSED; + return IO_DONE; +} + +/*-------------------------------------------------------------------------*\ +* Select with int timeout in ms +\*-------------------------------------------------------------------------*/ +int socket_select(t_socket n, fd_set *rfds, fd_set *wfds, fd_set *efds, + p_timeout tm) { + struct timeval tv; + double t = timeout_get(tm); + tv.tv_sec = (int) t; + tv.tv_usec = (int) ((t - tv.tv_sec) * 1.0e6); + if (n <= 0) { + Sleep((DWORD) (1000*t)); + return 0; + } else return select(0, rfds, wfds, efds, t >= 0.0? &tv: NULL); +} + +/*-------------------------------------------------------------------------*\ +* Close and inutilize socket +\*-------------------------------------------------------------------------*/ +void socket_destroy(p_socket ps) { + if (*ps != SOCKET_INVALID) { + socket_setblocking(ps); /* close can take a long time on WIN32 */ + closesocket(*ps); + *ps = SOCKET_INVALID; + } +} + +/*-------------------------------------------------------------------------*\ +* +\*-------------------------------------------------------------------------*/ +void socket_shutdown(p_socket ps, int how) { + socket_setblocking(ps); + shutdown(*ps, how); + socket_setnonblocking(ps); +} + +/*-------------------------------------------------------------------------*\ +* Creates and sets up a socket +\*-------------------------------------------------------------------------*/ +int socket_create(p_socket ps, int domain, int type, int protocol) { + *ps = socket(domain, type, protocol); + if (*ps != SOCKET_INVALID) return IO_DONE; + else return WSAGetLastError(); +} + +/*-------------------------------------------------------------------------*\ +* Connects or returns error message +\*-------------------------------------------------------------------------*/ +int socket_connect(p_socket ps, SA *addr, socklen_t len, p_timeout tm) { + int err; + /* don't call on closed socket */ + if (*ps == SOCKET_INVALID) return IO_CLOSED; + /* ask system to connect */ + if (connect(*ps, addr, len) == 0) return IO_DONE; + /* make sure the system is trying to connect */ + err = WSAGetLastError(); + if (err != WSAEWOULDBLOCK && err != WSAEINPROGRESS) return err; + /* zero timeout case optimization */ + if (timeout_iszero(tm)) return IO_TIMEOUT; + /* we wait until something happens */ + err = socket_waitfd(ps, WAITFD_C, tm); + if (err == IO_CLOSED) { + int len = sizeof(err); + /* give windows time to set the error (yes, disgusting) */ + Sleep(10); + /* find out why we failed */ + getsockopt(*ps, SOL_SOCKET, SO_ERROR, (char *)&err, &len); + /* we KNOW there was an error. if 'why' is 0, we will return + * "unknown error", but it's not really our fault */ + return err > 0? err: IO_UNKNOWN; + } else return err; + +} + +/*-------------------------------------------------------------------------*\ +* Binds or returns error message +\*-------------------------------------------------------------------------*/ +int socket_bind(p_socket ps, SA *addr, socklen_t len) { + int err = IO_DONE; + socket_setblocking(ps); + if (bind(*ps, addr, len) < 0) err = WSAGetLastError(); + socket_setnonblocking(ps); + return err; +} + +/*-------------------------------------------------------------------------*\ +* +\*-------------------------------------------------------------------------*/ +int socket_listen(p_socket ps, int backlog) { + int err = IO_DONE; + socket_setblocking(ps); + if (listen(*ps, backlog) < 0) err = WSAGetLastError(); + socket_setnonblocking(ps); + return err; +} + +/*-------------------------------------------------------------------------*\ +* Accept with timeout +\*-------------------------------------------------------------------------*/ +int socket_accept(p_socket ps, p_socket pa, SA *addr, socklen_t *len, + p_timeout tm) { + if (*ps == SOCKET_INVALID) return IO_CLOSED; + for ( ;; ) { + int err; + /* try to get client socket */ + if ((*pa = accept(*ps, addr, len)) != SOCKET_INVALID) return IO_DONE; + /* find out why we failed */ + err = WSAGetLastError(); + /* if we failed because there was no connectoin, keep trying */ + if (err != WSAEWOULDBLOCK && err != WSAECONNABORTED) return err; + /* call select to avoid busy wait */ + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + } +} + +/*-------------------------------------------------------------------------*\ +* Send with timeout +* On windows, if you try to send 10MB, the OS will buffer EVERYTHING +* this can take an awful lot of time and we will end up blocked. +* Therefore, whoever calls this function should not pass a huge buffer. +\*-------------------------------------------------------------------------*/ +int socket_send(p_socket ps, const char *data, size_t count, + size_t *sent, p_timeout tm) +{ + int err; + *sent = 0; + /* avoid making system calls on closed sockets */ + if (*ps == SOCKET_INVALID) return IO_CLOSED; + /* loop until we send something or we give up on error */ + for ( ;; ) { + /* try to send something */ + int put = send(*ps, data, (int) count, 0); + /* if we sent something, we are done */ + if (put > 0) { + *sent = put; + return IO_DONE; + } + /* deal with failure */ + err = WSAGetLastError(); + /* we can only proceed if there was no serious error */ + if (err != WSAEWOULDBLOCK) return err; + /* avoid busy wait */ + if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; + } +} + +/*-------------------------------------------------------------------------*\ +* Sendto with timeout +\*-------------------------------------------------------------------------*/ +int socket_sendto(p_socket ps, const char *data, size_t count, size_t *sent, + SA *addr, socklen_t len, p_timeout tm) +{ + int err; + *sent = 0; + if (*ps == SOCKET_INVALID) return IO_CLOSED; + for ( ;; ) { + int put = sendto(*ps, data, (int) count, 0, addr, len); + if (put > 0) { + *sent = put; + return IO_DONE; + } + err = WSAGetLastError(); + if (err != WSAEWOULDBLOCK) return err; + if ((err = socket_waitfd(ps, WAITFD_W, tm)) != IO_DONE) return err; + } +} + +/*-------------------------------------------------------------------------*\ +* Receive with timeout +\*-------------------------------------------------------------------------*/ +int socket_recv(p_socket ps, char *data, size_t count, size_t *got, p_timeout tm) { + int err; + *got = 0; + if (*ps == SOCKET_INVALID) return IO_CLOSED; + for ( ;; ) { + int taken = recv(*ps, data, (int) count, 0); + if (taken > 0) { + *got = taken; + return IO_DONE; + } + if (taken == 0) return IO_CLOSED; + err = WSAGetLastError(); + if (err != WSAEWOULDBLOCK) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + } +} + +/*-------------------------------------------------------------------------*\ +* Recvfrom with timeout +\*-------------------------------------------------------------------------*/ +int socket_recvfrom(p_socket ps, char *data, size_t count, size_t *got, + SA *addr, socklen_t *len, p_timeout tm) { + int err; + *got = 0; + if (*ps == SOCKET_INVALID) return IO_CLOSED; + for ( ;; ) { + int taken = recvfrom(*ps, data, (int) count, 0, addr, len); + if (taken > 0) { + *got = taken; + return IO_DONE; + } + if (taken == 0) return IO_CLOSED; + err = WSAGetLastError(); + if (err != WSAEWOULDBLOCK) return err; + if ((err = socket_waitfd(ps, WAITFD_R, tm)) != IO_DONE) return err; + } +} + +/*-------------------------------------------------------------------------*\ +* Put socket into blocking mode +\*-------------------------------------------------------------------------*/ +void socket_setblocking(p_socket ps) { + u_long argp = 0; + ioctlsocket(*ps, FIONBIO, &argp); +} + +/*-------------------------------------------------------------------------*\ +* Put socket into non-blocking mode +\*-------------------------------------------------------------------------*/ +void socket_setnonblocking(p_socket ps) { + u_long argp = 1; + ioctlsocket(*ps, FIONBIO, &argp); +} + +/*-------------------------------------------------------------------------*\ +* DNS helpers +\*-------------------------------------------------------------------------*/ +int socket_gethostbyaddr(const char *addr, socklen_t len, struct hostent **hp) { + *hp = gethostbyaddr(addr, len, AF_INET); + if (*hp) return IO_DONE; + else return WSAGetLastError(); +} + +int socket_gethostbyname(const char *addr, struct hostent **hp) { + *hp = gethostbyname(addr); + if (*hp) return IO_DONE; + else return WSAGetLastError(); +} + +/*-------------------------------------------------------------------------*\ +* Error translation functions +\*-------------------------------------------------------------------------*/ +const char *socket_hoststrerror(int err) { + if (err <= 0) return io_strerror(err); + switch (err) { + case WSAHOST_NOT_FOUND: return "host not found"; + default: return wstrerror(err); + } +} + +const char *socket_strerror(int err) { + if (err <= 0) return io_strerror(err); + switch (err) { + case WSAEADDRINUSE: return "address already in use"; + case WSAECONNREFUSED: return "connection refused"; + case WSAEISCONN: return "already connected"; + case WSAEACCES: return "permission denied"; + case WSAECONNABORTED: return "closed"; + case WSAECONNRESET: return "closed"; + case WSAETIMEDOUT: return "timeout"; + default: return wstrerror(err); + } +} + +const char *socket_ioerror(p_socket ps, int err) { + (void) ps; + return socket_strerror(err); +} + +static const char *wstrerror(int err) { + switch (err) { + case WSAEINTR: return "Interrupted function call"; + case WSAEACCES: return "Permission denied"; + case WSAEFAULT: return "Bad address"; + case WSAEINVAL: return "Invalid argument"; + case WSAEMFILE: return "Too many open files"; + case WSAEWOULDBLOCK: return "Resource temporarily unavailable"; + case WSAEINPROGRESS: return "Operation now in progress"; + case WSAEALREADY: return "Operation already in progress"; + case WSAENOTSOCK: return "Socket operation on nonsocket"; + case WSAEDESTADDRREQ: return "Destination address required"; + case WSAEMSGSIZE: return "Message too long"; + case WSAEPROTOTYPE: return "Protocol wrong type for socket"; + case WSAENOPROTOOPT: return "Bad protocol option"; + case WSAEPROTONOSUPPORT: return "Protocol not supported"; + case WSAESOCKTNOSUPPORT: return "Socket type not supported"; + case WSAEOPNOTSUPP: return "Operation not supported"; + case WSAEPFNOSUPPORT: return "Protocol family not supported"; + case WSAEAFNOSUPPORT: + return "Address family not supported by protocol family"; + case WSAEADDRINUSE: return "Address already in use"; + case WSAEADDRNOTAVAIL: return "Cannot assign requested address"; + case WSAENETDOWN: return "Network is down"; + case WSAENETUNREACH: return "Network is unreachable"; + case WSAENETRESET: return "Network dropped connection on reset"; + case WSAECONNABORTED: return "Software caused connection abort"; + case WSAECONNRESET: return "Connection reset by peer"; + case WSAENOBUFS: return "No buffer space available"; + case WSAEISCONN: return "Socket is already connected"; + case WSAENOTCONN: return "Socket is not connected"; + case WSAESHUTDOWN: return "Cannot send after socket shutdown"; + case WSAETIMEDOUT: return "Connection timed out"; + case WSAECONNREFUSED: return "Connection refused"; + case WSAEHOSTDOWN: return "Host is down"; + case WSAEHOSTUNREACH: return "No route to host"; + case WSAEPROCLIM: return "Too many processes"; + case WSASYSNOTREADY: return "Network subsystem is unavailable"; + case WSAVERNOTSUPPORTED: return "Winsock.dll version out of range"; + case WSANOTINITIALISED: + return "Successful WSAStartup not yet performed"; + case WSAEDISCON: return "Graceful shutdown in progress"; + case WSAHOST_NOT_FOUND: return "Host not found"; + case WSATRY_AGAIN: return "Nonauthoritative host not found"; + case WSANO_RECOVERY: return "Nonrecoverable name lookup error"; + case WSANO_DATA: return "Valid name, no data record of requested type"; + default: return "Unknown error"; + } +} + +const char *socket_gaistrerror(int err) { + if (err == 0) return NULL; + switch (err) { + case EAI_AGAIN: return "temporary failure in name resolution"; + case EAI_BADFLAGS: return "invalid value for ai_flags"; +#ifdef EAI_BADHINTS + case EAI_BADHINTS: return "invalid value for hints"; +#endif + case EAI_FAIL: return "non-recoverable failure in name resolution"; + case EAI_FAMILY: return "ai_family not supported"; + case EAI_MEMORY: return "memory allocation failure"; + case EAI_NONAME: + return "host or service not provided, or not known"; +// case EAI_OVERFLOW: return "argument buffer overflow"; +#ifdef EAI_PROTOCOL + case EAI_PROTOCOL: return "resolved protocol is unknown"; +#endif + case EAI_SERVICE: return "service not supported for socket type"; + case EAI_SOCKTYPE: return "ai_socktype not supported"; +// case EAI_SYSTEM: return strerror(errno); + default: return gai_strerrorA(err); + } +} + diff --git a/external/lua/luasocket/wsocket.h b/external/lua/luasocket/wsocket.h new file mode 100644 index 0000000000..0783b00d84 --- /dev/null +++ b/external/lua/luasocket/wsocket.h @@ -0,0 +1,25 @@ +#ifndef WSOCKET_H +#define WSOCKET_H +/*=========================================================================*\ +* Socket compatibilization module for Win32 +* LuaSocket toolkit +\*=========================================================================*/ + +/*=========================================================================*\ +* WinSock include files +\*=========================================================================*/ +#include +#include + +typedef int socklen_t; +typedef SOCKADDR_STORAGE t_sockaddr_storage; +typedef SOCKET t_socket; +typedef t_socket *p_socket; + +#define SOCKET_INVALID (INVALID_SOCKET) + +#ifndef SO_REUSEPORT +#define SO_REUSEPORT SO_REUSEADDR +#endif + +#endif /* WSOCKET_H */ diff --git a/external/lua/tolua/CMakeLists.txt b/external/lua/tolua/CMakeLists.txt new file mode 100644 index 0000000000..ccede5366b --- /dev/null +++ b/external/lua/tolua/CMakeLists.txt @@ -0,0 +1,22 @@ +set(TOLUA_SRC + tolua_event.c + tolua_is.c + tolua_map.c + tolua_push.c + tolua_to.c +) + +include_directories( + ../lua +) + + +add_library(tolua STATIC + ${TOLUA_SRC} +) + +set_target_properties(tolua + PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" +) diff --git a/external/lua/tolua/tolua++.h b/external/lua/tolua/tolua++.h new file mode 100644 index 0000000000..173aec08ad --- /dev/null +++ b/external/lua/tolua/tolua++.h @@ -0,0 +1,192 @@ +/* tolua +** Support code for Lua bindings. +** Written by Waldemar Celes +** TeCGraf/PUC-Rio +** Apr 2003 +** $Id: $ +*/ + +/* This code is free software; you can redistribute it and/or modify it. +** The software provided hereunder is on an "as is" basis, and +** the author has no obligation to provide maintenance, support, updates, +** enhancements, or modifications. +*/ + + +#ifndef TOLUA_H +#define TOLUA_H + +#ifndef TOLUA_API +#define TOLUA_API extern +#endif + +#define TOLUA_VERSION "tolua++-1.0.93" + +#ifdef __cplusplus +extern "C" { +#endif + +#define tolua_pushcppstring(x,y) tolua_pushstring(x,y.c_str()) +#define tolua_iscppstring tolua_isstring + +#define tolua_iscppstringarray tolua_isstringarray +#define tolua_pushfieldcppstring(L,lo,idx,s) tolua_pushfieldstring(L, lo, idx, s.c_str()) + +#ifndef TEMPLATE_BIND +#define TEMPLATE_BIND(p) +#endif + +#define TOLUA_TEMPLATE_BIND(p) + +#define TOLUA_PROTECTED_DESTRUCTOR +#define TOLUA_PROPERTY_TYPE(p) + +#define TOLUA_VALUE_ROOT "tolua_value_root" + +typedef int lua_Object; + +#include "lua.h" +#include "lauxlib.h" + +struct tolua_Error +{ + int index; + int array; + const char* type; +}; +typedef struct tolua_Error tolua_Error; + +#define TOLUA_NOPEER LUA_REGISTRYINDEX /* for lua 5.1 */ + +TOLUA_API const char* tolua_typename (lua_State* L, int lo); +TOLUA_API void tolua_error (lua_State* L, const char* msg, tolua_Error* err); +TOLUA_API int tolua_isnoobj (lua_State* L, int lo, tolua_Error* err); +TOLUA_API int tolua_isvalue (lua_State* L, int lo, int def, tolua_Error* err); +TOLUA_API int tolua_isvaluenil (lua_State* L, int lo, tolua_Error* err); +TOLUA_API int tolua_isboolean (lua_State* L, int lo, int def, tolua_Error* err); +TOLUA_API int tolua_isnumber (lua_State* L, int lo, int def, tolua_Error* err); +TOLUA_API int tolua_isstring (lua_State* L, int lo, int def, tolua_Error* err); +TOLUA_API int tolua_istable (lua_State* L, int lo, int def, tolua_Error* err); +TOLUA_API int tolua_isusertable (lua_State* L, int lo, const char* type, int def, tolua_Error* err); +TOLUA_API int tolua_isuserdata (lua_State* L, int lo, int def, tolua_Error* err); +TOLUA_API int tolua_isusertype (lua_State* L, int lo, const char* type, int def, tolua_Error* err); +TOLUA_API int tolua_isvaluearray +(lua_State* L, int lo, int dim, int def, tolua_Error* err); +TOLUA_API int tolua_isbooleanarray +(lua_State* L, int lo, int dim, int def, tolua_Error* err); +TOLUA_API int tolua_isnumberarray +(lua_State* L, int lo, int dim, int def, tolua_Error* err); +TOLUA_API int tolua_isstringarray +(lua_State* L, int lo, int dim, int def, tolua_Error* err); +TOLUA_API int tolua_istablearray +(lua_State* L, int lo, int dim, int def, tolua_Error* err); +TOLUA_API int tolua_isuserdataarray +(lua_State* L, int lo, int dim, int def, tolua_Error* err); +TOLUA_API int tolua_isusertypearray +(lua_State* L, int lo, const char* type, int dim, int def, tolua_Error* err); + +TOLUA_API void tolua_open (lua_State* L); + +TOLUA_API void* tolua_copy (lua_State* L, void* value, unsigned int size); +TOLUA_API int tolua_register_gc (lua_State* L, int lo); +TOLUA_API int tolua_default_collect (lua_State* tolua_S); + +TOLUA_API void tolua_usertype (lua_State* L, const char* type); +TOLUA_API void tolua_beginmodule (lua_State* L, const char* name); +TOLUA_API void tolua_endmodule (lua_State* L); +TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar); +TOLUA_API void tolua_class (lua_State* L, const char* name, const char* base); +TOLUA_API void tolua_cclass (lua_State* L, const char* lname, const char* name, const char* base, lua_CFunction col); +TOLUA_API void tolua_function (lua_State* L, const char* name, lua_CFunction func); +TOLUA_API void tolua_constant (lua_State* L, const char* name, lua_Number value); +TOLUA_API void tolua_variable (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set); +TOLUA_API void tolua_array (lua_State* L,const char* name, lua_CFunction get, lua_CFunction set); + +/* TOLUA_API void tolua_set_call_event(lua_State* L, lua_CFunction func, char* type); */ +TOLUA_API void tolua_addbase(lua_State* L, char* name, char* base); + +TOLUA_API void tolua_pushvalue (lua_State* L, int lo); +TOLUA_API void tolua_pushboolean (lua_State* L, int value); +TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value); +TOLUA_API void tolua_pushstring (lua_State* L, const char* value); +TOLUA_API void tolua_pushuserdata (lua_State* L, void* value); +TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type); +TOLUA_API void tolua_pushusertype_and_takeownership(lua_State* L, void* value, const char* type); +TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v); +TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v); +TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v); +TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v); +TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v); +TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type); +TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type); + +TOLUA_API void tolua_pushusertype_and_addtoroot (lua_State* L, void* value, const char* type); +TOLUA_API void tolua_add_value_to_root (lua_State* L,void* value); +TOLUA_API void tolua_remove_value_from_root (lua_State* L, void* value); + +TOLUA_API lua_Number tolua_tonumber (lua_State* L, int narg, lua_Number def); +TOLUA_API const char* tolua_tostring (lua_State* L, int narg, const char* def); +TOLUA_API void* tolua_touserdata (lua_State* L, int narg, void* def); +TOLUA_API void* tolua_tousertype (lua_State* L, int narg, void* def); +TOLUA_API int tolua_tovalue (lua_State* L, int narg, int def); +TOLUA_API int tolua_toboolean (lua_State* L, int narg, int def); +TOLUA_API lua_Number tolua_tofieldnumber (lua_State* L, int lo, int index, lua_Number def); +TOLUA_API const char* tolua_tofieldstring (lua_State* L, int lo, int index, const char* def); +TOLUA_API void* tolua_tofielduserdata (lua_State* L, int lo, int index, void* def); +TOLUA_API void* tolua_tofieldusertype (lua_State* L, int lo, int index, void* def); +TOLUA_API int tolua_tofieldvalue (lua_State* L, int lo, int index, int def); +TOLUA_API int tolua_getfieldboolean (lua_State* L, int lo, int index, int def); + +TOLUA_API void tolua_dobuffer(lua_State* L, char* B, unsigned int size, const char* name); + +TOLUA_API int class_gc_event (lua_State* L); + +#ifdef __cplusplus +static inline const char* tolua_tocppstring (lua_State* L, int narg, const char* def) { + + const char* s = tolua_tostring(L, narg, def); + return s?s:""; +}; + +static inline const char* tolua_tofieldcppstring (lua_State* L, int lo, int index, const char* def) { + + const char* s = tolua_tofieldstring(L, lo, index, def); + return s?s:""; +}; + +#else +#define tolua_tocppstring tolua_tostring +#define tolua_tofieldcppstring tolua_tofieldstring +#endif + +TOLUA_API int tolua_fast_isa(lua_State *L, int mt_indexa, int mt_indexb, int super_index); + +#ifndef Mtolua_new +#define Mtolua_new(EXP) new EXP +#endif + +#ifndef Mtolua_delete +#define Mtolua_delete(EXP) delete EXP +#endif + +#ifndef Mtolua_new_dim +#define Mtolua_new_dim(EXP, len) new EXP[len] +#endif + +#ifndef Mtolua_delete_dim +#define Mtolua_delete_dim(EXP) delete [] EXP +#endif + +#ifndef tolua_outside +#define tolua_outside +#endif + +#ifndef tolua_owned +#define tolua_owned +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/external/lua/tolua/tolua_event.c b/external/lua/tolua/tolua_event.c new file mode 100644 index 0000000000..bb09628017 --- /dev/null +++ b/external/lua/tolua/tolua_event.c @@ -0,0 +1,536 @@ +/* tolua: event functions +** Support code for Lua bindings. +** Written by Waldemar Celes +** TeCGraf/PUC-Rio +** Apr 2003 +** $Id: $ +*/ + +/* This code is free software; you can redistribute it and/or modify it. +** The software provided hereunder is on an "as is" basis, and +** the author has no obligation to provide maintenance, support, updates, +** enhancements, or modifications. +*/ + +#include + +#include "tolua++.h" + +/* Store at ubox + * It stores, creating the corresponding table if needed, + * the pair key/value in the corresponding ubox table +*/ +static void storeatubox (lua_State* L, int lo) +{ +#ifdef LUA_VERSION_NUM + lua_getfenv(L, lo); + if (lua_rawequal(L, -1, TOLUA_NOPEER)) { + lua_pop(L, 1); + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setfenv(L, lo); /* stack: k,v,table */ + }; + lua_insert(L, -3); + lua_settable(L, -3); /* on lua 5.1, we trade the "tolua_peers" lookup for a settable call */ + lua_pop(L, 1); +#else + /* stack: key value (to be stored) */ + lua_pushstring(L,"tolua_peers"); + lua_rawget(L,LUA_REGISTRYINDEX); /* stack: k v ubox */ + lua_pushvalue(L,lo); + lua_rawget(L,-2); /* stack: k v ubox ubox[u] */ + if (!lua_istable(L,-1)) + { + lua_pop(L,1); /* stack: k v ubox */ + lua_newtable(L); /* stack: k v ubox table */ + lua_pushvalue(L,1); + lua_pushvalue(L,-2); /* stack: k v ubox table u table */ + lua_rawset(L,-4); /* stack: k v ubox ubox[u]=table */ + } + lua_insert(L,-4); /* put table before k */ + lua_pop(L,1); /* pop ubox */ + lua_rawset(L,-3); /* store at table */ + lua_pop(L,1); /* pop ubox[u] */ +#endif +} + +/* Module index function +*/ +static int module_index_event (lua_State* L) +{ + lua_pushstring(L,".get"); + lua_rawget(L,-3); + if (lua_istable(L,-1)) + { + lua_pushvalue(L,2); /* key */ + lua_rawget(L,-2); + if (lua_iscfunction(L,-1)) + { + lua_call(L,0,1); + return 1; + } + else if (lua_istable(L,-1)) + return 1; + } + /* call old index meta event */ + if (lua_getmetatable(L,1)) + { + lua_pushstring(L,"__index"); + lua_rawget(L,-2); + lua_pushvalue(L,1); + lua_pushvalue(L,2); + if (lua_isfunction(L,-1)) + { + lua_call(L,2,1); + return 1; + } + else if (lua_istable(L,-1)) + { + lua_gettable(L,-3); + return 1; + } + } + lua_pushnil(L); + return 1; +} + +/* Module newindex function +*/ +static int module_newindex_event (lua_State* L) +{ + lua_pushstring(L,".set"); + lua_rawget(L,-4); + if (lua_istable(L,-1)) + { + lua_pushvalue(L,2); /* key */ + lua_rawget(L,-2); + if (lua_iscfunction(L,-1)) + { + lua_pushvalue(L,1); /* only to be compatible with non-static vars */ + lua_pushvalue(L,3); /* value */ + lua_call(L,2,0); + return 0; + } + } + /* call old newindex meta event */ + if (lua_getmetatable(L,1) && lua_getmetatable(L,-1)) + { + lua_pushstring(L,"__newindex"); + lua_rawget(L,-2); + if (lua_isfunction(L,-1)) + { + lua_pushvalue(L,1); + lua_pushvalue(L,2); + lua_pushvalue(L,3); + lua_call(L,3,0); + } + } + lua_settop(L,3); + lua_rawset(L,-3); + return 0; +} + +/* Class index function + * If the object is a userdata (ie, an object), it searches the field in + * the alternative table stored in the corresponding "ubox" table. +*/ +static int class_index_event (lua_State* L) +{ + int t = lua_type(L,1); + if (t == LUA_TUSERDATA) + { + /* Access alternative table */ +#ifdef LUA_VERSION_NUM /* new macro on version 5.1 */ + lua_getfenv(L,1); + if (!lua_rawequal(L, -1, TOLUA_NOPEER)) { + lua_pushvalue(L, 2); /* key */ + lua_gettable(L, -2); /* on lua 5.1, we trade the "tolua_peers" lookup for a gettable call */ + if (!lua_isnil(L, -1)) + return 1; + }; +#else + lua_pushstring(L,"tolua_peers"); + lua_rawget(L,LUA_REGISTRYINDEX); /* stack: obj key ubox */ + lua_pushvalue(L,1); + lua_rawget(L,-2); /* stack: obj key ubox ubox[u] */ + if (lua_istable(L,-1)) + { + lua_pushvalue(L,2); /* key */ + lua_rawget(L,-2); /* stack: obj key ubox ubox[u] value */ + if (!lua_isnil(L,-1)) + return 1; + } +#endif + lua_settop(L,2); /* stack: obj key */ + /* Try metatables */ + lua_pushvalue(L,1); /* stack: obj key obj */ + while (lua_getmetatable(L,-1)) + { /* stack: obj key obj mt */ + lua_remove(L,-2); /* stack: obj key mt */ + if (lua_isnumber(L,2)) /* check if key is a numeric value */ + { + /* try operator[] */ + lua_pushstring(L,".geti"); + lua_rawget(L,-2); /* stack: obj key mt func */ + if (lua_isfunction(L,-1)) + { + lua_pushvalue(L,1); + lua_pushvalue(L,2); + lua_call(L,2,1); + return 1; + } + } + else + { + lua_pushvalue(L,2); /* stack: obj key mt key */ + lua_rawget(L,-2); /* stack: obj key mt value */ + if (!lua_isnil(L,-1)) + return 1; + else + lua_pop(L,1); + /* try C/C++ variable */ + lua_pushstring(L,".get"); + lua_rawget(L,-2); /* stack: obj key mt tget */ + if (lua_istable(L,-1)) + { + lua_pushvalue(L,2); + lua_rawget(L,-2); /* stack: obj key mt value */ + if (lua_iscfunction(L,-1)) + { + lua_pushvalue(L,1); + lua_pushvalue(L,2); + lua_call(L,2,1); + return 1; + } + else if (lua_istable(L,-1)) + { + /* deal with array: create table to be returned and cache it in ubox */ + void* u = *((void**)lua_touserdata(L,1)); + lua_newtable(L); /* stack: obj key mt value table */ + lua_pushstring(L,".self"); + lua_pushlightuserdata(L,u); + lua_rawset(L,-3); /* store usertype in ".self" */ + lua_insert(L,-2); /* stack: obj key mt table value */ + lua_setmetatable(L,-2); /* set stored value as metatable */ + lua_pushvalue(L,-1); /* stack: obj key met table table */ + lua_pushvalue(L,2); /* stack: obj key mt table table key */ + lua_insert(L,-2); /* stack: obj key mt table key table */ + storeatubox(L,1); /* stack: obj key mt table */ + return 1; + } + } + } + lua_settop(L,3); + } + lua_pushnil(L); + return 1; + } + else if (t== LUA_TTABLE) + { + module_index_event(L); + return 1; + } + lua_pushnil(L); + return 1; +} + +/* Newindex function + * It first searches for a C/C++ varaible to be set. + * Then, it either stores it in the alternative ubox table (in the case it is + * an object) or in the own table (that represents the class or module). +*/ +static int class_newindex_event (lua_State* L) +{ + int t = lua_type(L,1); + if (t == LUA_TUSERDATA) + { + /* Try accessing a C/C++ variable to be set */ + lua_getmetatable(L,1); + while (lua_istable(L,-1)) /* stack: t k v mt */ + { + if (lua_isnumber(L,2)) /* check if key is a numeric value */ + { + /* try operator[] */ + lua_pushstring(L,".seti"); + lua_rawget(L,-2); /* stack: obj key mt func */ + if (lua_isfunction(L,-1)) + { + lua_pushvalue(L,1); + lua_pushvalue(L,2); + lua_pushvalue(L,3); + lua_call(L,3,0); + return 0; + } + } + else + { + lua_pushstring(L,".set"); + lua_rawget(L,-2); /* stack: t k v mt tset */ + if (lua_istable(L,-1)) + { + lua_pushvalue(L,2); + lua_rawget(L,-2); /* stack: t k v mt tset func */ + if (lua_iscfunction(L,-1)) + { + lua_pushvalue(L,1); + lua_pushvalue(L,3); + lua_call(L,2,0); + return 0; + } + lua_pop(L,1); /* stack: t k v mt tset */ + } + lua_pop(L,1); /* stack: t k v mt */ + if (!lua_getmetatable(L,-1)) /* stack: t k v mt mt */ + lua_pushnil(L); + lua_remove(L,-2); /* stack: t k v mt */ + } + } + lua_settop(L,3); /* stack: t k v */ + + /* then, store as a new field */ + storeatubox(L,1); + } + else if (t== LUA_TTABLE) + { + module_newindex_event(L); + } + return 0; +} + +static int class_call_event(lua_State* L) { + + if (lua_istable(L, 1)) { + lua_pushstring(L, ".call"); + lua_rawget(L, 1); + if (lua_isfunction(L, -1)) { + + lua_insert(L, 1); + lua_call(L, lua_gettop(L)-1, 1); + + return 1; + }; + }; + tolua_error(L,"Attempt to call a non-callable object.",NULL); + return 0; +}; + +static int do_operator (lua_State* L, const char* op) +{ + if (lua_isuserdata(L,1)) + { + /* Try metatables */ + lua_pushvalue(L,1); /* stack: op1 op2 */ + while (lua_getmetatable(L,-1)) + { /* stack: op1 op2 op1 mt */ + lua_remove(L,-2); /* stack: op1 op2 mt */ + lua_pushstring(L,op); /* stack: op1 op2 mt key */ + lua_rawget(L,-2); /* stack: obj key mt func */ + if (lua_isfunction(L,-1)) + { + lua_pushvalue(L,1); + lua_pushvalue(L,2); + lua_call(L,2,1); + return 1; + } + lua_settop(L,3); + } + } + tolua_error(L,"Attempt to perform operation on an invalid operand",NULL); + return 0; +} + +static int class_add_event (lua_State* L) +{ + return do_operator(L,".add"); +} + +int class_sub_event (lua_State* L) +{ + return do_operator(L,".sub"); +} + +static int class_mul_event (lua_State* L) +{ + return do_operator(L,".mul"); +} + +static int class_div_event (lua_State* L) +{ + return do_operator(L,".div"); +} + +static int class_lt_event (lua_State* L) +{ + return do_operator(L,".lt"); +} + +static int class_le_event (lua_State* L) +{ + return do_operator(L,".le"); +} + +static int class_eq_event (lua_State* L) +{ + /* copying code from do_operator here to return false when no operator is found */ + if (lua_isuserdata(L,1)) + { + /* Try metatables */ + lua_pushvalue(L,1); /* stack: op1 op2 */ + while (lua_getmetatable(L,-1)) + { /* stack: op1 op2 op1 mt */ + lua_remove(L,-2); /* stack: op1 op2 mt */ + lua_pushstring(L,".eq"); /* stack: op1 op2 mt key */ + lua_rawget(L,-2); /* stack: obj key mt func */ + if (lua_isfunction(L,-1)) + { + lua_pushvalue(L,1); + lua_pushvalue(L,2); + lua_call(L,2,1); + return 1; + } + lua_settop(L,3); + } + } + + lua_settop(L, 3); + lua_pushboolean(L, 0); + return 1; +} + +/* +static int class_gc_event (lua_State* L) +{ + void* u = *((void**)lua_touserdata(L,1)); + fprintf(stderr, "collecting: looking at %p\n", u); + lua_pushstring(L,"tolua_gc"); + lua_rawget(L,LUA_REGISTRYINDEX); + lua_pushlightuserdata(L,u); + lua_rawget(L,-2); + if (lua_isfunction(L,-1)) + { + lua_pushvalue(L,1); + lua_call(L,1,0); + lua_pushlightuserdata(L,u); + lua_pushnil(L); + lua_rawset(L,-3); + } + lua_pop(L,2); + return 0; +} +*/ +TOLUA_API int class_gc_event (lua_State* L) +{ + void* u = *((void**)lua_touserdata(L,1)); + int top; + /*fprintf(stderr, "collecting: looking at %p\n", u);*/ + /* + lua_pushstring(L,"tolua_gc"); + lua_rawget(L,LUA_REGISTRYINDEX); + */ + lua_pushvalue(L, lua_upvalueindex(1)); + lua_pushlightuserdata(L,u); + lua_rawget(L,-2); /* stack: gc umt */ + lua_getmetatable(L,1); /* stack: gc umt mt */ + /*fprintf(stderr, "checking type\n");*/ + top = lua_gettop(L); + if (tolua_fast_isa(L,top,top-1, lua_upvalueindex(2))) /* make sure we collect correct type */ + { + /*fprintf(stderr, "Found type!\n");*/ + /* get gc function */ + lua_pushliteral(L,".collector"); + lua_rawget(L,-2); /* stack: gc umt mt collector */ + if (lua_isfunction(L,-1)) { + /*fprintf(stderr, "Found .collector!\n");*/ + } + else { + lua_pop(L,1); + /*fprintf(stderr, "Using default cleanup\n");*/ + lua_pushcfunction(L,tolua_default_collect); + } + + lua_pushvalue(L,1); /* stack: gc umt mt collector u */ + lua_call(L,1,0); + + lua_pushlightuserdata(L,u); /* stack: gc umt mt u */ + lua_pushnil(L); /* stack: gc umt mt u nil */ + lua_rawset(L,-5); /* stack: gc umt mt */ + } + lua_pop(L,3); + return 0; +} + + +/* Register module events + * It expects the metatable on the top of the stack +*/ +TOLUA_API void tolua_moduleevents (lua_State* L) +{ + lua_pushstring(L,"__index"); + lua_pushcfunction(L,module_index_event); + lua_rawset(L,-3); + lua_pushstring(L,"__newindex"); + lua_pushcfunction(L,module_newindex_event); + lua_rawset(L,-3); +} + +/* Check if the object on the top has a module metatable +*/ +TOLUA_API int tolua_ismodulemetatable (lua_State* L) +{ + int r = 0; + if (lua_getmetatable(L,-1)) + { + lua_pushstring(L,"__index"); + lua_rawget(L,-2); + r = (lua_tocfunction(L,-1) == module_index_event); + lua_pop(L,2); + } + return r; +} + +/* Register class events + * It expects the metatable on the top of the stack +*/ +TOLUA_API void tolua_classevents (lua_State* L) +{ + lua_pushstring(L,"__index"); + lua_pushcfunction(L,class_index_event); + lua_rawset(L,-3); + lua_pushstring(L,"__newindex"); + lua_pushcfunction(L,class_newindex_event); + lua_rawset(L,-3); + + lua_pushstring(L,"__add"); + lua_pushcfunction(L,class_add_event); + lua_rawset(L,-3); + lua_pushstring(L,"__sub"); + lua_pushcfunction(L,class_sub_event); + lua_rawset(L,-3); + lua_pushstring(L,"__mul"); + lua_pushcfunction(L,class_mul_event); + lua_rawset(L,-3); + lua_pushstring(L,"__div"); + lua_pushcfunction(L,class_div_event); + lua_rawset(L,-3); + + lua_pushstring(L,"__lt"); + lua_pushcfunction(L,class_lt_event); + lua_rawset(L,-3); + lua_pushstring(L,"__le"); + lua_pushcfunction(L,class_le_event); + lua_rawset(L,-3); + lua_pushstring(L,"__eq"); + lua_pushcfunction(L,class_eq_event); + lua_rawset(L,-3); + + lua_pushstring(L,"__call"); + lua_pushcfunction(L,class_call_event); + lua_rawset(L,-3); + + lua_pushstring(L,"__gc"); + lua_pushstring(L, "tolua_gc_event"); + lua_rawget(L, LUA_REGISTRYINDEX); + /*lua_pushcfunction(L,class_gc_event);*/ + lua_rawset(L,-3); +} + diff --git a/external/lua/tolua/tolua_event.h b/external/lua/tolua/tolua_event.h new file mode 100644 index 0000000000..898f33dfcb --- /dev/null +++ b/external/lua/tolua/tolua_event.h @@ -0,0 +1,24 @@ +/* tolua: event functions +** Support code for Lua bindings. +** Written by Waldemar Celes +** TeCGraf/PUC-Rio +** Apr 2003 +** $Id: $ +*/ + +/* This code is free software; you can redistribute it and/or modify it. +** The software provided hereunder is on an "as is" basis, and +** the author has no obligation to provide maintenance, support, updates, +** enhancements, or modifications. +*/ + +#ifndef TOLUA_EVENT_H +#define TOLUA_EVENT_H + +#include "tolua++.h" + +TOLUA_API void tolua_moduleevents (lua_State* L); +TOLUA_API int tolua_ismodulemetatable (lua_State* L); +TOLUA_API void tolua_classevents (lua_State* L); + +#endif diff --git a/external/lua/tolua/tolua_is.c b/external/lua/tolua/tolua_is.c new file mode 100644 index 0000000000..8d395ae18e --- /dev/null +++ b/external/lua/tolua/tolua_is.c @@ -0,0 +1,621 @@ +/* tolua: functions to check types. +** Support code for Lua bindings. +** Written by Waldemar Celes +** TeCGraf/PUC-Rio +** Apr 2003 +** $Id: $ +*/ + +/* This code is free software; you can redistribute it and/or modify it. +** The software provided hereunder is on an "as is" basis, and +** the author has no obligation to provide maintenance, support, updates, +** enhancements, or modifications. +*/ + +#include "tolua++.h" +#include "lauxlib.h" + +#include +#include + +/* a fast check if a is b, without parameter validation + i.e. if b is equal to a or a superclass of a. */ +TOLUA_API int tolua_fast_isa(lua_State *L, int mt_indexa, int mt_indexb, int super_index) +{ + int result; + if (lua_rawequal(L,mt_indexa,mt_indexb)) + result = 1; + else + { + if (super_index) { + lua_pushvalue(L, super_index); + } else { + lua_pushliteral(L,"tolua_super"); + lua_rawget(L,LUA_REGISTRYINDEX); /* stack: super */ + }; + lua_pushvalue(L,mt_indexa); /* stack: super mta */ + lua_rawget(L,-2); /* stack: super super[mta] */ + lua_pushvalue(L,mt_indexb); /* stack: super super[mta] mtb */ + lua_rawget(L,LUA_REGISTRYINDEX); /* stack: super super[mta] typenameB */ + lua_rawget(L,-2); /* stack: super super[mta] bool */ + result = lua_toboolean(L,-1); + lua_pop(L,3); + } + return result; +} + +/* Push and returns the corresponding object typename */ +TOLUA_API const char* tolua_typename (lua_State* L, int lo) +{ + int tag = lua_type(L,lo); + if (tag == LUA_TNONE) + lua_pushstring(L,"[no object]"); + else if (tag != LUA_TUSERDATA && tag != LUA_TTABLE) + lua_pushstring(L,lua_typename(L,tag)); + else if (tag == LUA_TUSERDATA) + { + if (!lua_getmetatable(L,lo)) + lua_pushstring(L,lua_typename(L,tag)); + else + { + lua_rawget(L,LUA_REGISTRYINDEX); + if (!lua_isstring(L,-1)) + { + lua_pop(L,1); + lua_pushstring(L,"[undefined]"); + } + } + } + else /* is table */ + { + lua_pushvalue(L,lo); + lua_rawget(L,LUA_REGISTRYINDEX); + if (!lua_isstring(L,-1)) + { + lua_pop(L,1); + lua_pushstring(L,"table"); + } + else + { + lua_pushstring(L,"class "); + lua_insert(L,-2); + lua_concat(L,2); + } + } + return lua_tostring(L,-1); +} + +TOLUA_API void tolua_error (lua_State* L, const char* msg, tolua_Error* err) +{ + if (msg[0] == '#') + { + const char* expected = err->type; + const char* provided = tolua_typename(L,err->index); + if (msg[1]=='f') + { + int narg = err->index; + if (err->array) + luaL_error(L,"%s\n argument #%d is array of '%s'; array of '%s' expected.\n", + msg+2,narg,provided,expected); + else + luaL_error(L,"%s\n argument #%d is '%s'; '%s' expected.\n", + msg+2,narg,provided,expected); + } + else if (msg[1]=='v') + { + if (err->array) + luaL_error(L,"%s\n value is array of '%s'; array of '%s' expected.\n", + msg+2,provided,expected); + else + luaL_error(L,"%s\n value is '%s'; '%s' expected.\n", + msg+2,provided,expected); + } + } + else + luaL_error(L,msg); +} + +/* the equivalent of lua_is* for usertable */ +static int lua_isusertable (lua_State* L, int lo, const char* type) +{ + int r = 0; + if (lo < 0) lo = lua_gettop(L)+lo+1; + lua_pushvalue(L,lo); + lua_rawget(L,LUA_REGISTRYINDEX); /* get registry[t] */ + if (lua_isstring(L,-1)) + { + r = strcmp(lua_tostring(L,-1),type)==0; + if (!r) + { + /* try const */ + lua_pushstring(L,"const "); + lua_insert(L,-2); + lua_concat(L,2); + r = lua_isstring(L,-1) && strcmp(lua_tostring(L,-1),type)==0; + } + } + lua_pop(L, 1); + return r; +} + +int push_table_instance(lua_State* L, int lo) { + + if (lua_istable(L, lo)) { + + lua_pushstring(L, ".c_instance"); + lua_gettable(L, lo); + if (lua_isuserdata(L, -1)) { + + lua_replace(L, lo); + return 1; + } else { + + lua_pop(L, 1); + return 0; + }; + } else { + return 0; + }; + + return 0; +}; + +/* the equivalent of lua_is* for usertype */ +int lua_isusertype (lua_State* L, int lo, const char* type) +{ + if (!lua_isuserdata(L,lo)) { + if (!push_table_instance(L, lo)) { + return 0; + }; + }; + { + /* check if it is of the same type */ + int r; + const char *tn; + if (lua_getmetatable(L,lo)) /* if metatable? */ + { + lua_rawget(L,LUA_REGISTRYINDEX); /* get registry[mt] */ + tn = lua_tostring(L,-1); + r = tn && (strcmp(tn,type) == 0); + lua_pop(L, 1); + if (r) + return 1; + else + { + /* check if it is a specialized class */ + lua_pushstring(L,"tolua_super"); + lua_rawget(L,LUA_REGISTRYINDEX); /* get super */ + lua_getmetatable(L,lo); + lua_rawget(L,-2); /* get super[mt] */ + if (lua_istable(L,-1)) + { + int b; + lua_pushstring(L,type); + lua_rawget(L,-2); /* get super[mt][type] */ + b = lua_toboolean(L,-1); + lua_pop(L,3); + if (b) + return 1; + } + } + } + } + return 0; +} + +TOLUA_API int tolua_isnoobj (lua_State* L, int lo, tolua_Error* err) +{ + if (lua_gettop(L)index = lo; + err->array = 0; + err->type = "[no object]"; + return 0; +} + +TOLUA_API int tolua_isboolean (lua_State* L, int lo, int def, tolua_Error* err) +{ + if (def && lua_gettop(L)index = lo; + err->array = 0; + err->type = "boolean"; + return 0; +} + +TOLUA_API int tolua_isnumber (lua_State* L, int lo, int def, tolua_Error* err) +{ + if (def && lua_gettop(L)index = lo; + err->array = 0; + err->type = "number"; + return 0; +} + +TOLUA_API int tolua_isstring (lua_State* L, int lo, int def, tolua_Error* err) +{ + if (def && lua_gettop(L)index = lo; + err->array = 0; + err->type = "string"; + return 0; +} + +TOLUA_API int tolua_istable (lua_State* L, int lo, int def, tolua_Error* err) +{ + if (def && lua_gettop(L)index = lo; + err->array = 0; + err->type = "table"; + return 0; +} + +TOLUA_API int tolua_isusertable (lua_State* L, int lo, const char* type, int def, tolua_Error* err) +{ + if (def && lua_gettop(L)index = lo; + err->array = 0; + err->type = type; + return 0; +} + + +TOLUA_API int tolua_isuserdata (lua_State* L, int lo, int def, tolua_Error* err) +{ + if (def && lua_gettop(L)index = lo; + err->array = 0; + err->type = "userdata"; + return 0; +} + +TOLUA_API int tolua_isvaluenil (lua_State* L, int lo, tolua_Error* err) { + + if (lua_gettop(L)index = lo; + err->array = 0; + err->type = "value"; + return 1; +}; + +TOLUA_API int tolua_isvalue (lua_State* L, int lo, int def, tolua_Error* err) +{ + if (def || abs(lo)<=lua_gettop(L)) /* any valid index */ + return 1; + err->index = lo; + err->array = 0; + err->type = "value"; + return 0; +} + +TOLUA_API int tolua_isusertype (lua_State* L, int lo, const char* type, int def, tolua_Error* err) +{ + if (def && lua_gettop(L)index = lo; + err->array = 0; + err->type = type; + return 0; +} + +TOLUA_API int tolua_isvaluearray +(lua_State* L, int lo, int dim, int def, tolua_Error* err) +{ + if (!tolua_istable(L,lo,def,err)) + return 0; + else + return 1; +} + +TOLUA_API int tolua_isbooleanarray +(lua_State* L, int lo, int dim, int def, tolua_Error* err) +{ + if (!tolua_istable(L,lo,def,err)) + return 0; + else + { + int i; + for (i=1; i<=dim; ++i) + { + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!(lua_isnil(L,-1) || lua_isboolean(L,-1)) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "boolean"; + return 0; + } + lua_pop(L,1); + } + } + return 1; +} + +TOLUA_API int tolua_isnumberarray +(lua_State* L, int lo, int dim, int def, tolua_Error* err) +{ + if (!tolua_istable(L,lo,def,err)) + return 0; + else + { + int i; + for (i=1; i<=dim; ++i) + { + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!lua_isnumber(L,-1) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "number"; + return 0; + } + lua_pop(L,1); + } + } + return 1; +} + +TOLUA_API int tolua_isstringarray +(lua_State* L, int lo, int dim, int def, tolua_Error* err) +{ + if (!tolua_istable(L,lo,def,err)) + return 0; + else + { + int i; + for (i=1; i<=dim; ++i) + { + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!(lua_isnil(L,-1) || lua_isstring(L,-1)) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "string"; + return 0; + } + lua_pop(L,1); + } + } + return 1; +} + +TOLUA_API int tolua_istablearray +(lua_State* L, int lo, int dim, int def, tolua_Error* err) +{ + if (!tolua_istable(L,lo,def,err)) + return 0; + else + { + int i; + for (i=1; i<=dim; ++i) + { + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (! lua_istable(L,-1) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "table"; + return 0; + } + lua_pop(L,1); + } + } + return 1; +} + +TOLUA_API int tolua_isuserdataarray +(lua_State* L, int lo, int dim, int def, tolua_Error* err) +{ + if (!tolua_istable(L,lo,def,err)) + return 0; + else + { + int i; + for (i=1; i<=dim; ++i) + { + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "userdata"; + return 0; + } + lua_pop(L,1); + } + } + return 1; +} + +TOLUA_API int tolua_isusertypearray +(lua_State* L, int lo, const char* type, int dim, int def, tolua_Error* err) +{ + if (!tolua_istable(L,lo,def,err)) + return 0; + else + { + int i; + for (i=1; i<=dim; ++i) + { + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->type = type; + err->array = 1; + return 0; + } + lua_pop(L,1); + } + } + return 1; +} + +#if 0 +int tolua_isbooleanfield +(lua_State* L, int lo, int i, int def, tolua_Error* err) +{ + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!(lua_isnil(L,-1) || lua_isboolean(L,-1)) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "boolean"; + return 0; + } + lua_pop(L,1); + return 1; +} + +int tolua_isnumberfield +(lua_State* L, int lo, int i, int def, tolua_Error* err) +{ + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!lua_isnumber(L,-1) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "number"; + return 0; + } + lua_pop(L,1); + return 1; +} + +int tolua_isstringfield +(lua_State* L, int lo, int i, int def, tolua_Error* err) +{ + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!(lua_isnil(L,-1) || lua_isstring(L,-1)) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "string"; + return 0; + } + lua_pop(L,1); + return 1; +} + +int tolua_istablefield +(lua_State* L, int lo, int i, int def, tolua_Error* err) +{ + lua_pushnumber(L,i+1); + lua_gettable(L,lo); + if (! lua_istable(L,-1) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "table"; + return 0; + } + lua_pop(L,1); +} + +int tolua_isusertablefield +(lua_State* L, int lo, const char* type, int i, int def, tolua_Error* err) +{ + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (! lua_isusertable(L,-1,type) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = type; + return 0; + } + lua_pop(L,1); + return 1; +} + +int tolua_isuserdatafield +(lua_State* L, int lo, int i, int def, tolua_Error* err) +{ + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!(lua_isnil(L,-1) || lua_isuserdata(L,-1)) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->array = 1; + err->type = "userdata"; + return 0; + } + lua_pop(L,1); + return 1; +} + +int tolua_isusertypefield +(lua_State* L, int lo, const char* type, int i, int def, tolua_Error* err) +{ + lua_pushnumber(L,i); + lua_gettable(L,lo); + if (!(lua_isnil(L,-1) || lua_isusertype(L,-1,type)) && + !(def && lua_isnil(L,-1)) + ) + { + err->index = lo; + err->type = type; + err->array = 1; + return 0; + } + lua_pop(L,1); + return 1; +} + +#endif diff --git a/external/lua/tolua/tolua_map.c b/external/lua/tolua/tolua_map.c new file mode 100644 index 0000000000..5528ef6cbe --- /dev/null +++ b/external/lua/tolua/tolua_map.c @@ -0,0 +1,742 @@ +/* tolua: functions to map features +** Support code for Lua bindings. +** Written by Waldemar Celes +** TeCGraf/PUC-Rio +** Apr 2003 +** $Id: $ +*/ + +/* This code is free software; you can redistribute it and/or modify it. +** The software provided hereunder is on an "as is" basis, and +** the author has no obligation to provide maintenance, support, updates, +** enhancements, or modifications. +*/ + +#include "tolua++.h" +#include "tolua_event.h" +#include "lauxlib.h" + +#include +#include +#include +#include + + +/* Create metatable + * Create and register new metatable +*/ +static int tolua_newmetatable (lua_State* L, const char* name) +{ + int r = luaL_newmetatable(L,name); + +#ifdef LUA_VERSION_NUM /* only lua 5.1 */ + if (r) { + lua_pushvalue(L, -1); + lua_pushstring(L, name); + lua_settable(L, LUA_REGISTRYINDEX); /* reg[mt] = type_name */ + }; +#endif + + if (r) + tolua_classevents(L); /* set meta events */ + lua_pop(L,1); + return r; +} + +/* Map super classes + * It sets 'name' as being also a 'base', mapping all super classes of 'base' in 'name' +*/ +static void mapsuper (lua_State* L, const char* name, const char* base) +{ + /* push registry.super */ + lua_pushstring(L,"tolua_super"); + lua_rawget(L,LUA_REGISTRYINDEX); /* stack: super */ + luaL_getmetatable(L,name); /* stack: super mt */ + lua_rawget(L,-2); /* stack: super table */ + if (lua_isnil(L,-1)) + { + /* create table */ + lua_pop(L,1); + lua_newtable(L); /* stack: super table */ + luaL_getmetatable(L,name); /* stack: super table mt */ + lua_pushvalue(L,-2); /* stack: super table mt table */ + lua_rawset(L,-4); /* stack: super table */ + } + + /* set base as super class */ + lua_pushstring(L,base); + lua_pushboolean(L,1); + lua_rawset(L,-3); /* stack: super table */ + + /* set all super class of base as super class of name */ + luaL_getmetatable(L,base); /* stack: super table base_mt */ + lua_rawget(L,-3); /* stack: super table base_table */ + if (lua_istable(L,-1)) + { + /* traverse base table */ + lua_pushnil(L); /* first key */ + while (lua_next(L,-2) != 0) + { + /* stack: ... base_table key value */ + lua_pushvalue(L,-2); /* stack: ... base_table key value key */ + lua_insert(L,-2); /* stack: ... base_table key key value */ + lua_rawset(L,-5); /* stack: ... base_table key */ + } + } + lua_pop(L,3); /* stack: */ +} + +/* creates a 'tolua_ubox' table for base clases, and +// expects the metatable and base metatable on the stack */ +static void set_ubox(lua_State* L) { + + /* mt basemt */ + if (!lua_isnil(L, -1)) { + lua_pushstring(L, "tolua_ubox"); + lua_rawget(L,-2); + } else { + lua_pushnil(L); + }; + /* mt basemt base_ubox */ + if (!lua_isnil(L,-1)) { + lua_pushstring(L, "tolua_ubox"); + lua_insert(L, -2); + /* mt basemt key ubox */ + lua_rawset(L,-4); + /* (mt with ubox) basemt */ + } else { + /* mt basemt nil */ + lua_pop(L, 1); + lua_pushstring(L,"tolua_ubox"); + lua_newtable(L); + /* make weak value metatable for ubox table to allow userdata to be + garbage-collected */ + lua_newtable(L); + lua_pushliteral(L, "__mode"); + lua_pushliteral(L, "v"); + lua_rawset(L, -3); /* stack: string ubox mt */ + lua_setmetatable(L, -2); /* stack:mt basemt string ubox */ + lua_rawset(L,-4); + }; + +}; + +/* Map inheritance + * It sets 'name' as derived from 'base' by setting 'base' as metatable of 'name' +*/ +static void mapinheritance (lua_State* L, const char* name, const char* base) +{ + /* set metatable inheritance */ + luaL_getmetatable(L,name); + + if (base && *base) + luaL_getmetatable(L,base); + else { + + if (lua_getmetatable(L, -1)) { /* already has a mt, we don't overwrite it */ + lua_pop(L, 2); + return; + }; + luaL_getmetatable(L,"tolua_commonclass"); + }; + + set_ubox(L); + + lua_setmetatable(L,-2); + lua_pop(L,1); +} + +/* Object type +*/ +static int tolua_bnd_type (lua_State* L) +{ + tolua_typename(L,lua_gettop(L)); + return 1; +} + +/* Take ownership +*/ +static int tolua_bnd_takeownership (lua_State* L) +{ + int success = 0; + if (lua_isuserdata(L,1)) + { + if (lua_getmetatable(L,1)) /* if metatable? */ + { + lua_pop(L,1); /* clear metatable off stack */ + /* force garbage collection to avoid C to reuse a to-be-collected address */ +#ifdef LUA_VERSION_NUM + lua_gc(L, LUA_GCCOLLECT, 0); +#else + lua_setgcthreshold(L,0); +#endif + + success = tolua_register_gc(L,1); + } + } + lua_pushboolean(L,success!=0); + return 1; +} + +/* Release ownership +*/ +static int tolua_bnd_releaseownership (lua_State* L) +{ + int done = 0; + if (lua_isuserdata(L,1)) + { + void* u = *((void**)lua_touserdata(L,1)); + /* force garbage collection to avoid releasing a to-be-collected address */ +#ifdef LUA_VERSION_NUM + lua_gc(L, LUA_GCCOLLECT, 0); +#else + lua_setgcthreshold(L,0); +#endif + lua_pushstring(L,"tolua_gc"); + lua_rawget(L,LUA_REGISTRYINDEX); + lua_pushlightuserdata(L,u); + lua_rawget(L,-2); + lua_getmetatable(L,1); + if (lua_rawequal(L,-1,-2)) /* check that we are releasing the correct type */ + { + lua_pushlightuserdata(L,u); + lua_pushnil(L); + lua_rawset(L,-5); + done = 1; + } + } + lua_pushboolean(L,done!=0); + return 1; +} + +/* Type casting +*/ +int tolua_bnd_cast (lua_State* L) +{ + + /* // old code + void* v = tolua_tousertype(L,1,NULL); + const char* s = tolua_tostring(L,2,NULL); + if (v && s) + tolua_pushusertype(L,v,s); + else + lua_pushnil(L); + return 1; + */ + + void* v; + const char* s; + if (lua_islightuserdata(L, 1)) { + v = tolua_touserdata(L, 1, NULL); + } else { + v = tolua_tousertype(L, 1, 0); + }; + + s = tolua_tostring(L,2,NULL); + if (v && s) + tolua_pushusertype(L,v,s); + else + lua_pushnil(L); + return 1; +} + +/* Test userdata is null +*/ +static int tolua_bnd_isnulluserdata (lua_State* L) { + void **ud = (void**)lua_touserdata(L, -1); + tolua_pushboolean(L, ud == NULL || *ud == NULL); + return 1; +} + +/* Inheritance +*/ +static int tolua_bnd_inherit (lua_State* L) { + + /* stack: lua object, c object */ + lua_pushstring(L, ".c_instance"); + lua_pushvalue(L, -2); + lua_rawset(L, -4); + /* l_obj[".c_instance"] = c_obj */ + + return 0; +}; + +#ifdef LUA_VERSION_NUM /* lua 5.1 */ +static int tolua_bnd_setpeer(lua_State* L) { + + /* stack: userdata, table */ + if (!lua_isuserdata(L, -2)) { + lua_pushstring(L, "Invalid argument #1 to setpeer: userdata expected."); + lua_error(L); + }; + + if (lua_isnil(L, -1)) { + + lua_pop(L, 1); + lua_pushvalue(L, TOLUA_NOPEER); + }; + lua_setfenv(L, -2); + + return 0; +}; + +static int tolua_bnd_getpeer(lua_State* L) { + + /* stack: userdata */ + lua_getfenv(L, -1); + if (lua_rawequal(L, -1, TOLUA_NOPEER)) { + lua_pop(L, 1); + lua_pushnil(L); + }; + return 1; +}; +#endif + +/* static int class_gc_event (lua_State* L); */ + +TOLUA_API void tolua_open (lua_State* L) +{ + int top = lua_gettop(L); + lua_pushstring(L,"tolua_opened"); + lua_rawget(L,LUA_REGISTRYINDEX); + if (!lua_isboolean(L,-1)) + { + lua_pushstring(L,"tolua_opened"); + lua_pushboolean(L,1); + lua_rawset(L,LUA_REGISTRYINDEX); + + // create value root table + lua_pushstring(L, TOLUA_VALUE_ROOT); + lua_newtable(L); + lua_rawset(L, LUA_REGISTRYINDEX); + +#ifndef LUA_VERSION_NUM /* only prior to lua 5.1 */ + /* create peer object table */ + lua_pushstring(L, "tolua_peers"); + lua_newtable(L); + /* make weak key metatable for peers indexed by userdata object */ + lua_newtable(L); + lua_pushliteral(L, "__mode"); + lua_pushliteral(L, "k"); + lua_rawset(L, -3); /* stack: string peers mt */ + lua_setmetatable(L, -2); /* stack: string peers */ + lua_rawset(L,LUA_REGISTRYINDEX); +#endif + + /* create object ptr -> udata mapping table */ + lua_pushstring(L,"tolua_ubox"); + lua_newtable(L); + /* make weak value metatable for ubox table to allow userdata to be + garbage-collected */ + lua_newtable(L); + lua_pushliteral(L, "__mode"); + lua_pushliteral(L, "v"); + lua_rawset(L, -3); /* stack: string ubox mt */ + lua_setmetatable(L, -2); /* stack: string ubox */ + lua_rawset(L,LUA_REGISTRYINDEX); + +// /* create object ptr -> class type mapping table */ +// lua_pushstring(L, "tolua_ptr2type"); +// lua_newtable(L); +// lua_rawset(L, LUA_REGISTRYINDEX); + + lua_pushstring(L,"tolua_super"); + lua_newtable(L); + lua_rawset(L,LUA_REGISTRYINDEX); + lua_pushstring(L,"tolua_gc"); + lua_newtable(L); + lua_rawset(L,LUA_REGISTRYINDEX); + + /* create gc_event closure */ + lua_pushstring(L, "tolua_gc_event"); + lua_pushstring(L, "tolua_gc"); + lua_rawget(L, LUA_REGISTRYINDEX); + lua_pushstring(L, "tolua_super"); + lua_rawget(L, LUA_REGISTRYINDEX); + lua_pushcclosure(L, class_gc_event, 2); + lua_rawset(L, LUA_REGISTRYINDEX); + + tolua_newmetatable(L,"tolua_commonclass"); + + tolua_module(L,NULL,0); + tolua_beginmodule(L,NULL); + tolua_module(L,"tolua",0); + tolua_beginmodule(L,"tolua"); + tolua_function(L,"type",tolua_bnd_type); + tolua_function(L,"takeownership",tolua_bnd_takeownership); + tolua_function(L,"releaseownership",tolua_bnd_releaseownership); + tolua_function(L,"cast",tolua_bnd_cast); + tolua_function(L,"isnull",tolua_bnd_isnulluserdata); + tolua_function(L,"inherit", tolua_bnd_inherit); +#ifdef LUA_VERSION_NUM /* lua 5.1 */ + tolua_function(L, "setpeer", tolua_bnd_setpeer); + tolua_function(L, "getpeer", tolua_bnd_getpeer); +#endif + + tolua_endmodule(L); + tolua_endmodule(L); + } + lua_settop(L,top); +} + +/* Copy a C object +*/ +TOLUA_API void* tolua_copy (lua_State* L, void* value, unsigned int size) +{ + void* clone = (void*)malloc(size); + if (clone) + memcpy(clone,value,size); + else + tolua_error(L,"insuficient memory",NULL); + return clone; +} + +/* Default collect function +*/ +TOLUA_API int tolua_default_collect (lua_State* tolua_S) +{ + void* self = tolua_tousertype(tolua_S,1,0); + free(self); + return 0; +} + +/* Do clone +*/ +TOLUA_API int tolua_register_gc (lua_State* L, int lo) +{ + int success = 1; + void *value = *(void **)lua_touserdata(L,lo); + lua_pushstring(L,"tolua_gc"); + lua_rawget(L,LUA_REGISTRYINDEX); + lua_pushlightuserdata(L,value); + lua_rawget(L,-2); + if (!lua_isnil(L,-1)) /* make sure that object is not already owned */ + success = 0; + else + { + lua_pushlightuserdata(L,value); + lua_getmetatable(L,lo); + lua_rawset(L,-4); + } + lua_pop(L,2); + return success; +} + +/* Register a usertype + * It creates the correspoding metatable in the registry, for both 'type' and 'const type'. + * It maps 'const type' as being also a 'type' +*/ +TOLUA_API void tolua_usertype (lua_State* L, const char* type) +{ + char ctype[128] = "const "; + strncat(ctype,type,120); + + /* create both metatables */ + if (tolua_newmetatable(L,ctype) && tolua_newmetatable(L,type)) + mapsuper(L,type,ctype); /* 'type' is also a 'const type' */ +} + + +/* Begin module + * It pushes the module (or class) table on the stack +*/ +TOLUA_API void tolua_beginmodule (lua_State* L, const char* name) +{ + if (name) + { + lua_pushstring(L,name); + lua_rawget(L,-2); + } + else + lua_pushvalue(L,LUA_GLOBALSINDEX); +} + +/* End module + * It pops the module (or class) from the stack +*/ +TOLUA_API void tolua_endmodule (lua_State* L) +{ + lua_pop(L,1); +} + +/* Map module + * It creates a new module +*/ +#if 1 +TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar) +{ + if (name) + { + /* tolua module */ + lua_pushstring(L,name); + lua_rawget(L,-2); + if (!lua_istable(L,-1)) /* check if module already exists */ + { + lua_pop(L,1); + lua_newtable(L); + lua_pushstring(L,name); + lua_pushvalue(L,-2); + lua_rawset(L,-4); /* assing module into module */ + } + } + else + { + /* global table */ + lua_pushvalue(L,LUA_GLOBALSINDEX); + } + if (hasvar) + { + if (!tolua_ismodulemetatable(L)) /* check if it already has a module metatable */ + { + /* create metatable to get/set C/C++ variable */ + lua_newtable(L); + tolua_moduleevents(L); + if (lua_getmetatable(L,-2)) + lua_setmetatable(L,-2); /* set old metatable as metatable of metatable */ + lua_setmetatable(L,-2); + } + } + lua_pop(L,1); /* pop module */ +} +#else +TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar) +{ + if (name) + { + /* tolua module */ + lua_pushstring(L,name); + lua_newtable(L); + } + else + { + /* global table */ + lua_pushvalue(L,LUA_GLOBALSINDEX); + } + if (hasvar) + { + /* create metatable to get/set C/C++ variable */ + lua_newtable(L); + tolua_moduleevents(L); + if (lua_getmetatable(L,-2)) + lua_setmetatable(L,-2); /* set old metatable as metatable of metatable */ + lua_setmetatable(L,-2); + } + if (name) + lua_rawset(L,-3); /* assing module into module */ + else + lua_pop(L,1); /* pop global table */ +} +#endif + +static void push_collector(lua_State* L, const char* type, lua_CFunction col) { + + /* push collector function, but only if it's not NULL, or if there's no + collector already */ + if (!col) return; + luaL_getmetatable(L,type); + lua_pushstring(L,".collector"); + /* + if (!col) { + lua_pushvalue(L, -1); + lua_rawget(L, -3); + if (!lua_isnil(L, -1)) { + lua_pop(L, 3); + return; + }; + lua_pop(L, 1); + }; + // */ + lua_pushcfunction(L,col); + + lua_rawset(L,-3); + lua_pop(L, 1); +}; + +/* Map C class + * It maps a C class, setting the appropriate inheritance and super classes. +*/ +TOLUA_API void tolua_cclass (lua_State* L, const char* lname, const char* name, const char* base, lua_CFunction col) +{ + char cname[128] = "const "; + char cbase[128] = "const "; + strncat(cname,name,120); + strncat(cbase,base,120); + + mapinheritance(L,name,base); + mapinheritance(L,cname,name); + + mapsuper(L,cname,cbase); + mapsuper(L,name,base); + + lua_pushstring(L,lname); + + push_collector(L, name, col); + /* + luaL_getmetatable(L,name); + lua_pushstring(L,".collector"); + lua_pushcfunction(L,col); + + lua_rawset(L,-3); + */ + + luaL_getmetatable(L,name); + lua_rawset(L,-3); /* assign class metatable to module */ + + /* now we also need to store the collector table for the const + instances of the class */ + push_collector(L, cname, col); + /* + luaL_getmetatable(L,cname); + lua_pushstring(L,".collector"); + lua_pushcfunction(L,col); + lua_rawset(L,-3); + lua_pop(L,1); + */ + + +} + +/* Add base + * It adds additional base classes to a class (for multiple inheritance) + * (not for now) + */ +TOLUA_API void tolua_addbase(lua_State* L, char* name, char* base) { + + char cname[128] = "const "; + char cbase[128] = "const "; + strncat(cname,name,120); + strncat(cbase,base,120); + + mapsuper(L,cname,cbase); + mapsuper(L,name,base); +}; + + +/* Map function + * It assigns a function into the current module (or class) +*/ +TOLUA_API void tolua_function (lua_State* L, const char* name, lua_CFunction func) +{ + lua_pushstring(L,name); + lua_pushcfunction(L,func); + lua_rawset(L,-3); +} + +/* sets the __call event for the class (expects the class' main table on top) */ +/* never really worked :( +TOLUA_API void tolua_set_call_event(lua_State* L, lua_CFunction func, char* type) { + + lua_getmetatable(L, -1); + //luaL_getmetatable(L, type); + lua_pushstring(L,"__call"); + lua_pushcfunction(L,func); + lua_rawset(L,-3); + lua_pop(L, 1); +}; +*/ + +/* Map constant number + * It assigns a constant number into the current module (or class) +*/ +TOLUA_API void tolua_constant (lua_State* L, const char* name, lua_Number value) +{ + lua_pushstring(L,name); + tolua_pushnumber(L,value); + lua_rawset(L,-3); +} + + +/* Map variable + * It assigns a variable into the current module (or class) +*/ +TOLUA_API void tolua_variable (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set) +{ + /* get func */ + lua_pushstring(L,".get"); + lua_rawget(L,-2); + if (!lua_istable(L,-1)) + { + /* create .get table, leaving it at the top */ + lua_pop(L,1); + lua_newtable(L); + lua_pushstring(L,".get"); + lua_pushvalue(L,-2); + lua_rawset(L,-4); + } + lua_pushstring(L,name); + lua_pushcfunction(L,get); + lua_rawset(L,-3); /* store variable */ + lua_pop(L,1); /* pop .get table */ + + /* set func */ + if (set) + { + lua_pushstring(L,".set"); + lua_rawget(L,-2); + if (!lua_istable(L,-1)) + { + /* create .set table, leaving it at the top */ + lua_pop(L,1); + lua_newtable(L); + lua_pushstring(L,".set"); + lua_pushvalue(L,-2); + lua_rawset(L,-4); + } + lua_pushstring(L,name); + lua_pushcfunction(L,set); + lua_rawset(L,-3); /* store variable */ + lua_pop(L,1); /* pop .set table */ + } +} + +/* Access const array + * It reports an error when trying to write into a const array +*/ +static int const_array (lua_State* L) +{ + luaL_error(L,"value of const array cannot be changed"); + return 0; +} + +/* Map an array + * It assigns an array into the current module (or class) +*/ +TOLUA_API void tolua_array (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set) +{ + lua_pushstring(L,".get"); + lua_rawget(L,-2); + if (!lua_istable(L,-1)) + { + /* create .get table, leaving it at the top */ + lua_pop(L,1); + lua_newtable(L); + lua_pushstring(L,".get"); + lua_pushvalue(L,-2); + lua_rawset(L,-4); + } + lua_pushstring(L,name); + + lua_newtable(L); /* create array metatable */ + lua_pushvalue(L,-1); + lua_setmetatable(L,-2); /* set the own table as metatable (for modules) */ + lua_pushstring(L,"__index"); + lua_pushcfunction(L,get); + lua_rawset(L,-3); + lua_pushstring(L,"__newindex"); + lua_pushcfunction(L,set?set:const_array); + lua_rawset(L,-3); + + lua_rawset(L,-3); /* store variable */ + lua_pop(L,1); /* pop .get table */ +} + + +TOLUA_API void tolua_dobuffer(lua_State* L, char* B, unsigned int size, const char* name) { + +#ifdef LUA_VERSION_NUM /* lua 5.1 */ + if (!luaL_loadbuffer(L, B, size, name)) lua_pcall(L, 0, 0, 0); +#else + lua_dobuffer(L, B, size, name); +#endif +}; + diff --git a/external/lua/tolua/tolua_push.c b/external/lua/tolua/tolua_push.c new file mode 100644 index 0000000000..8d9ee7b33a --- /dev/null +++ b/external/lua/tolua/tolua_push.c @@ -0,0 +1,217 @@ +/* tolua: functions to push C values. +** Support code for Lua bindings. +** Written by Waldemar Celes +** TeCGraf/PUC-Rio +** Apr 2003 +** $Id: $ +*/ + +/* This code is free software; you can redistribute it and/or modify it. +** The software provided hereunder is on an "as is" basis, and +** the author has no obligation to provide maintenance, support, updates, +** enhancements, or modifications. +*/ + +#include "tolua++.h" +#include "lauxlib.h" + +#include + +void tolua_pushusertype_internal (lua_State* L, void* value, const char* type, int addToRoot) +{ + if (value == NULL) + lua_pushnil(L); + else + { + luaL_getmetatable(L, type); /* stack: mt */ + if (lua_isnil(L, -1)) { /* NOT FOUND metatable */ + lua_pop(L, 1); + return; + } + lua_pushstring(L,"tolua_ubox"); + lua_rawget(L,-2); /* stack: mt ubox */ + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + lua_pushstring(L, "tolua_ubox"); + lua_rawget(L, LUA_REGISTRYINDEX); + }; + + lua_pushlightuserdata(L,value); /* stack: mt ubox key */ + lua_rawget(L,-2); /* stack: mt ubox ubox[value] */ + + if (lua_isnil(L,-1)) + { + lua_pop(L,1); /* stack: mt ubox */ + lua_pushlightuserdata(L,value); + *(void**)lua_newuserdata(L,sizeof(void *)) = value; /* stack: mt ubox value newud */ + lua_pushvalue(L,-1); /* stack: mt ubox value newud newud */ + lua_insert(L,-4); /* stack: mt newud ubox value newud */ + lua_rawset(L,-3); /* ubox[value] = newud, stack: mt newud ubox */ + lua_pop(L,1); /* stack: mt newud */ + /*luaL_getmetatable(L,type);*/ + lua_pushvalue(L, -2); /* stack: mt newud mt */ + lua_setmetatable(L,-2); /* update mt, stack: mt newud */ + +#ifdef LUA_VERSION_NUM + lua_pushvalue(L, TOLUA_NOPEER); /* stack: mt newud peer */ + lua_setfenv(L, -2); /* stack: mt newud */ +#endif + } + else + { + /* check the need of updating the metatable to a more specialized class */ + lua_insert(L,-2); /* stack: mt ubox[u] ubox */ + lua_pop(L,1); /* stack: mt ubox[u] */ + lua_pushstring(L,"tolua_super"); + lua_rawget(L,LUA_REGISTRYINDEX); /* stack: mt ubox[u] super */ + lua_getmetatable(L,-2); /* stack: mt ubox[u] super mt */ + lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] */ + if (lua_istable(L,-1)) + { + lua_pushstring(L,type); /* stack: mt ubox[u] super super[mt] type */ + lua_rawget(L,-2); /* stack: mt ubox[u] super super[mt] flag */ + if (lua_toboolean(L,-1) == 1) /* if true */ + { + lua_pop(L,3); /* mt ubox[u]*/ + lua_remove(L, -2); + return; + } + } + /* type represents a more specilized type */ + /*luaL_getmetatable(L,type); // stack: mt ubox[u] super super[mt] flag mt */ + lua_pushvalue(L, -5); /* stack: mt ubox[u] super super[mt] flag mt */ + lua_setmetatable(L,-5); /* stack: mt ubox[u] super super[mt] flag */ + lua_pop(L,3); /* stack: mt ubox[u] */ + } + lua_remove(L, -2); /* stack: ubox[u]*/ + + if (0 != addToRoot) + { + lua_pushvalue(L, -1); + tolua_add_value_to_root(L, value); + } + } +} + +TOLUA_API void tolua_pushvalue (lua_State* L, int lo) +{ + lua_pushvalue(L,lo); +} + +TOLUA_API void tolua_pushboolean (lua_State* L, int value) +{ + lua_pushboolean(L,value); +} + +TOLUA_API void tolua_pushnumber (lua_State* L, lua_Number value) +{ + lua_pushnumber(L,value); +} + +TOLUA_API void tolua_pushstring (lua_State* L, const char* value) +{ + if (value == NULL) + lua_pushnil(L); + else + lua_pushstring(L,value); +} + +TOLUA_API void tolua_pushuserdata (lua_State* L, void* value) +{ + if (value == NULL) + lua_pushnil(L); + else + lua_pushlightuserdata(L,value); +} + +TOLUA_API void tolua_pushusertype (lua_State* L, void* value, const char* type) +{ + tolua_pushusertype_internal(L, value, type, 0); +} + +TOLUA_API void tolua_pushusertype_and_addtoroot (lua_State* L, void* value, const char* type) +{ + tolua_pushusertype_internal(L, value, type, 1); +} + +TOLUA_API void tolua_pushusertype_and_takeownership (lua_State* L, void* value, const char* type) +{ + tolua_pushusertype(L,value,type); + tolua_register_gc(L,lua_gettop(L)); +} + +TOLUA_API void tolua_add_value_to_root(lua_State* L, void* ptr) +{ + + lua_pushstring(L, TOLUA_VALUE_ROOT); + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: value root */ + lua_insert(L, -2); /* stack: root value */ + lua_pushlightuserdata(L, ptr); /* stack: root value ptr */ + lua_insert(L, -2); /* stack: root ptr value */ + lua_rawset(L, -3); /* root[ptr] = value, stack: root */ + lua_pop(L, 1); /* stack: - */ +} + + +TOLUA_API void tolua_remove_value_from_root (lua_State* L, void* ptr) +{ + lua_pushstring(L, TOLUA_VALUE_ROOT); + lua_rawget(L, LUA_REGISTRYINDEX); /* stack: root */ + lua_pushlightuserdata(L, ptr); /* stack: root ptr */ + + lua_pushnil(L); /* stack: root ptr nil */ + lua_rawset(L, -3); /* root[ptr] = nil, stack: root */ + lua_pop(L, 1); +} + +TOLUA_API void tolua_pushfieldvalue (lua_State* L, int lo, int index, int v) +{ + lua_pushnumber(L,index); + lua_pushvalue(L,v); + lua_settable(L,lo); +} + +TOLUA_API void tolua_pushfieldboolean (lua_State* L, int lo, int index, int v) +{ + lua_pushnumber(L,index); + lua_pushboolean(L,v); + lua_settable(L,lo); +} + + +TOLUA_API void tolua_pushfieldnumber (lua_State* L, int lo, int index, lua_Number v) +{ + lua_pushnumber(L,index); + tolua_pushnumber(L,v); + lua_settable(L,lo); +} + +TOLUA_API void tolua_pushfieldstring (lua_State* L, int lo, int index, const char* v) +{ + lua_pushnumber(L,index); + tolua_pushstring(L,v); + lua_settable(L,lo); +} + +TOLUA_API void tolua_pushfielduserdata (lua_State* L, int lo, int index, void* v) +{ + lua_pushnumber(L,index); + tolua_pushuserdata(L,v); + lua_settable(L,lo); +} + +TOLUA_API void tolua_pushfieldusertype (lua_State* L, int lo, int index, void* v, const char* type) +{ + lua_pushnumber(L,index); + tolua_pushusertype(L,v,type); + lua_settable(L,lo); +} + +TOLUA_API void tolua_pushfieldusertype_and_takeownership (lua_State* L, int lo, int index, void* v, const char* type) +{ + lua_pushnumber(L,index); + tolua_pushusertype(L,v,type); + tolua_register_gc(L,lua_gettop(L)); + lua_settable(L,lo); +} + diff --git a/external/lua/tolua/tolua_to.c b/external/lua/tolua/tolua_to.c new file mode 100644 index 0000000000..455f4d83a1 --- /dev/null +++ b/external/lua/tolua/tolua_to.c @@ -0,0 +1,133 @@ +/* tolua: funcitons to convert to C types +** Support code for Lua bindings. +** Written by Waldemar Celes +** TeCGraf/PUC-Rio +** Apr 2003 +** $Id: $ +*/ + +/* This code is free software; you can redistribute it and/or modify it. +** The software provided hereunder is on an "as is" basis, and +** the author has no obligation to provide maintenance, support, updates, +** enhancements, or modifications. +*/ + +#include "tolua++.h" + +#include +#include + +TOLUA_API lua_Number tolua_tonumber (lua_State* L, int narg, lua_Number def) +{ + return lua_gettop(L)getOpenGLView(); + if(!glview) { + glview = GLView::createWithRect("Test Lua", Rect(0,0,900,640)); + director->setOpenGLView(glview); + } + + // turn on display FPS + director->setDisplayStats(true); + + // set FPS. the default value is 1.0/60 if you don't call this + director->setAnimationInterval(1.0 / 60); + + auto screenSize = glview->getFrameSize(); + + auto designSize = Size(480, 320); + + auto pFileUtils = FileUtils::getInstance(); + + if (screenSize.height > 320) + { + auto resourceSize = Size(960, 640); + std::vector searchPaths; + searchPaths.push_back("hd"); + pFileUtils->setSearchPaths(searchPaths); + director->setContentScaleFactor(resourceSize.height/designSize.height); + } + + glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::FIXED_HEIGHT); + + // register lua engine + LuaEngine* pEngine = LuaEngine::getInstance(); + ScriptEngineManager::getInstance()->setScriptEngine(pEngine); + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC) + LuaStack* stack = pEngine->getLuaStack(); + register_assetsmanager_test_sample(stack->getLuaState()); +#endif + + std::string resPrefix(""); + + std::vector searchPaths = pFileUtils->getSearchPaths(); + searchPaths.insert(searchPaths.begin(), resPrefix); + searchPaths.insert(searchPaths.begin(), resPrefix + "Images"); + searchPaths.insert(searchPaths.begin(), resPrefix + "cocosbuilderRes"); + if (screenSize.height > 320) + { + searchPaths.insert(searchPaths.begin(), resPrefix + "hd"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/Images"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/ArmatureComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/AttributeComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/BackgroundComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/EffectComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/LoadSceneEdtiorFileTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/ParticleComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/SpriteComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/TmxMapComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/UIComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "hd/scenetest/TriggerTest"); + } + else + { + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/ArmatureComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/AttributeComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/BackgroundComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/EffectComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/LoadSceneEdtiorFileTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/ParticleComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/SpriteComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/TmxMapComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/UIComponentTest"); + searchPaths.insert(searchPaths.begin(), resPrefix + "scenetest/TriggerTest"); + } + + + FileUtils::getInstance()->setSearchPaths(searchPaths); + + pEngine->executeScriptFile("src/controller.lua"); + + return true; +} + +// This function will be called when the app is inactive. When comes a phone call,it's be invoked too +void AppDelegate::applicationDidEnterBackground() +{ + Director::getInstance()->stopAnimation(); + + SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); +} + +// this function will be called when the app is active again +void AppDelegate::applicationWillEnterForeground() +{ + Director::getInstance()->startAnimation(); + + SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); +} diff --git a/samples/lua-tests/project/Classes/AppDelegate.h b/samples/lua-tests/project/Classes/AppDelegate.h new file mode 100644 index 0000000000..5cf478d052 --- /dev/null +++ b/samples/lua-tests/project/Classes/AppDelegate.h @@ -0,0 +1,38 @@ +#ifndef __APP_DELEGATE_H__ +#define __APP_DELEGATE_H__ + +#include "cocos2d.h" + +/** +@brief The cocos2d Application. + +The reason for implement as private inheritance is to hide some interface call by Director. +*/ +class AppDelegate : private cocos2d::Application +{ +public: + AppDelegate(); + virtual ~AppDelegate(); + + /** + @brief Implement Director and Scene init code here. + @return true Initialize success, app continue. + @return false Initialize failed, app terminate. + */ + virtual bool applicationDidFinishLaunching(); + + /** + @brief The function be called when the application enter background + @param the pointer of the application + */ + virtual void applicationDidEnterBackground(); + + /** + @brief The function be called when the application enter foreground + @param the pointer of the application + */ + virtual void applicationWillEnterForeground(); +}; + +#endif // __APP_DELEGATE_H__ + diff --git a/samples/lua-tests/project/Classes/lua_assetsmanager_test_sample.cpp b/samples/lua-tests/project/Classes/lua_assetsmanager_test_sample.cpp new file mode 100644 index 0000000000..f76157f0d4 --- /dev/null +++ b/samples/lua-tests/project/Classes/lua_assetsmanager_test_sample.cpp @@ -0,0 +1,154 @@ +#include "lua_assetsmanager_test_sample.h" + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua_fix.h" +#ifdef __cplusplus +} +#endif + +#include "cocos2d.h" +#include "extensions/cocos-ext.h" + +#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) +#include +#include +#endif + +USING_NS_CC; +USING_NS_CC_EXT; + + +static int lua_cocos2dx_createDownloadDir(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = lua_gettop(L); + + if (0 == argc) + { + std::string pathToSave = FileUtils::getInstance()->getWritablePath(); + pathToSave += "tmpdir"; + +#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) + DIR *pDir = NULL; + + pDir = opendir (pathToSave.c_str()); + if (! pDir) + { + mkdir(pathToSave.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); + } +#else + if ((GetFileAttributesA(pathToSave.c_str())) == INVALID_FILE_ATTRIBUTES) + { + CreateDirectoryA(pathToSave.c_str(), 0); + } +#endif + tolua_pushstring(L, pathToSave.c_str()); + return 1; + } + + CCLOG("'createDownloadDir' function wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; +} + +static int lua_cocos2dx_deleteDownloadDir(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = lua_gettop(L); + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(L, 1, 0, &tolua_err)) goto tolua_lerror; +#endif + std::string pathToSave = tolua_tostring(L, 1, ""); + +#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) + std::string command = "rm -r "; + // Path may include space. + command += "\"" + pathToSave + "\""; + system(command.c_str()); +#else + std::string command = "rd /s /q "; + // Path may include space. + command += "\"" + pathToSave + "\""; + system(command.c_str()); +#endif + return 0; + } + + CCLOG("'resetDownloadDir' function wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'resetDownloadDir'.",&tolua_err); + return 0; +#endif +} + +static int lua_cocos2dx_addSearchPath(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = lua_gettop(L); + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + + if (2 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!tolua_isstring(L, 1, 0, &tolua_err) || + !tolua_isboolean(L, 2, 0, &tolua_err)) + goto tolua_lerror; +#endif + std::string pathToSave = tolua_tostring(L, 1, ""); + bool before = tolua_toboolean(L, 2, 0); + std::vector searchPaths = FileUtils::getInstance()->getSearchPaths(); + if (before) + { + searchPaths.insert(searchPaths.begin(), pathToSave); + } + else + { + searchPaths.push_back(pathToSave); + } + + FileUtils::getInstance()->setSearchPaths(searchPaths); + + return 0; + } + CCLOG("'addSearchPath' function wrong number of arguments: %d, was expecting %d\n", argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addSearchPath'.",&tolua_err); + return 0; +#endif +} + +int register_assetsmanager_test_sample(lua_State* L) +{ + tolua_open(L); + tolua_module(L, NULL, 0); + tolua_beginmodule(L, NULL); + tolua_function(L, "createDownloadDir", lua_cocos2dx_createDownloadDir); + tolua_function(L, "deleteDownloadDir", lua_cocos2dx_deleteDownloadDir); + tolua_function(L, "addSearchPath", lua_cocos2dx_addSearchPath); + tolua_endmodule(L); + return 0; +} diff --git a/samples/lua-tests/project/Classes/lua_assetsmanager_test_sample.h b/samples/lua-tests/project/Classes/lua_assetsmanager_test_sample.h new file mode 100644 index 0000000000..967593de71 --- /dev/null +++ b/samples/lua-tests/project/Classes/lua_assetsmanager_test_sample.h @@ -0,0 +1,17 @@ +#ifndef COCOS2DX_COCOS_SCRIPTING_LUA_BINDINGS_LUA_ASSETSMANAGER_TEST_SAMPLE_H +#define COCOS2DX_COCOS_SCRIPTING_LUA_BINDINGS_LUA_ASSETSMANAGER_TEST_SAMPLE_H + +#ifdef __cplusplus +extern "C" { +#endif +#include "tolua++.h" +#ifdef __cplusplus +} +#endif + +/** + * The apis which are bound in this file are temporary for the assetsmanager test sample.After the completion of some systems like fileutils,these apis will be deprecated + */ +TOLUA_API int register_assetsmanager_test_sample(lua_State* tolua_S); + +#endif // #ifndef COCOS2DX_COCOS_SCRIPTING_LUA_BINDINGS_LUA_ASSETSMANAGER_TEST_SAMPLE_H diff --git a/samples/lua-tests/project/proj.android/.classpath b/samples/lua-tests/project/proj.android/.classpath new file mode 100644 index 0000000000..0b08408342 --- /dev/null +++ b/samples/lua-tests/project/proj.android/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/samples/lua-tests/project/proj.android/.project b/samples/lua-tests/project/proj.android/.project new file mode 100644 index 0000000000..fac9edb71e --- /dev/null +++ b/samples/lua-tests/project/proj.android/.project @@ -0,0 +1,50 @@ + + + TestLua + + + + + + com.android.ide.eclipse.adt.ResourceManagerBuilder + + + + + com.android.ide.eclipse.adt.PreCompilerBuilder + + + + + org.eclipse.jdt.core.javabuilder + + + + + com.android.ide.eclipse.adt.ApkBuilder + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + com.android.ide.eclipse.adt.AndroidNature + org.eclipse.jdt.core.javanature + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + + + Resources + 2 + PARENT-1-PROJECT_LOC/Resources + + + diff --git a/samples/lua-tests/project/proj.android/AndroidManifest.xml b/samples/lua-tests/project/proj.android/AndroidManifest.xml new file mode 100644 index 0000000000..430e935659 --- /dev/null +++ b/samples/lua-tests/project/proj.android/AndroidManifest.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/lua-tests/project/proj.android/ant.properties b/samples/lua-tests/project/proj.android/ant.properties new file mode 100644 index 0000000000..b0971e891e --- /dev/null +++ b/samples/lua-tests/project/proj.android/ant.properties @@ -0,0 +1,17 @@ +# This file is used to override default values used by the Ant build system. +# +# This file must be checked into Version Control Systems, as it is +# integral to the build system of your project. + +# This file is only used by the Ant script. + +# You can use this to override default values such as +# 'source.dir' for the location of your java source folder and +# 'out.dir' for the location of your output folder. + +# You can also use it define how the release builds are signed by declaring +# the following properties: +# 'key.store' for the location of your keystore and +# 'key.alias' for the name of the key to use. +# The password will be asked during the build when you use the 'release' target. + diff --git a/samples/lua-tests/project/proj.android/build.xml b/samples/lua-tests/project/proj.android/build.xml new file mode 100644 index 0000000000..0d99a1f0fc --- /dev/null +++ b/samples/lua-tests/project/proj.android/build.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/lua-tests/project/proj.android/jni/Android.mk b/samples/lua-tests/project/proj.android/jni/Android.mk new file mode 100644 index 0000000000..f98f613ab6 --- /dev/null +++ b/samples/lua-tests/project/proj.android/jni/Android.mk @@ -0,0 +1,22 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := testlua_shared + +LOCAL_MODULE_FILENAME := libtestlua + +LOCAL_SRC_FILES += testlua/main.cpp \ + ../../Classes/AppDelegate.cpp \ + ../../Classes/lua_assetsmanager_test_sample.cpp + + +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../Classes + +LOCAL_STATIC_LIBRARIES := curl_static_prebuilt + +LOCAL_WHOLE_STATIC_LIBRARIES := cocos_lua_static + +include $(BUILD_SHARED_LIBRARY) + +$(call import-module,scripting/lua-bindings) diff --git a/samples/lua-tests/project/proj.android/jni/Application.mk b/samples/lua-tests/project/proj.android/jni/Application.mk new file mode 100644 index 0000000000..3961feba34 --- /dev/null +++ b/samples/lua-tests/project/proj.android/jni/Application.mk @@ -0,0 +1,8 @@ +APP_STL := gnustl_static + +# add -Wno-literal-suffix to avoid warning: warning: invalid suffix on literal; C++11 requires a space between literal and identifier [-Wliteral-suffix] +# in NDK_ROOT/arch-arm/usr/include/sys/cdefs_elf.h:35:28: when using ndk-r9 +APP_CPPFLAGS := -frtti -DCOCOS2D_DEBUG=1 -std=c++11 -Wno-literal-suffix -fsigned-char + +APP_CPPFLAGS += -fexceptions + diff --git a/samples/lua-tests/project/proj.android/jni/testlua/main.cpp b/samples/lua-tests/project/proj.android/jni/testlua/main.cpp new file mode 100644 index 0000000000..a8b36b317d --- /dev/null +++ b/samples/lua-tests/project/proj.android/jni/testlua/main.cpp @@ -0,0 +1,16 @@ +#include "AppDelegate.h" +#include "cocos2d.h" +#include "CCEventType.h" +#include "platform/android/jni/JniHelper.h" +#include +#include + +#define LOG_TAG "main" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) + +using namespace cocos2d; + +void cocos_android_app_init (struct android_app* app) { + LOGD("cocos_android_app_init"); + AppDelegate *pAppDelegate = new AppDelegate(); +} diff --git a/samples/lua-tests/project/proj.android/proguard-project.txt b/samples/lua-tests/project/proj.android/proguard-project.txt new file mode 100644 index 0000000000..f2fe1559a2 --- /dev/null +++ b/samples/lua-tests/project/proj.android/proguard-project.txt @@ -0,0 +1,20 @@ +# To enable ProGuard in your project, edit project.properties +# to define the proguard.config property as described in that file. +# +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in ${sdk.dir}/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the ProGuard +# include property in project.properties. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/samples/lua-tests/project/proj.android/project.properties b/samples/lua-tests/project/proj.android/project.properties new file mode 100644 index 0000000000..0a6dc6664d --- /dev/null +++ b/samples/lua-tests/project/proj.android/project.properties @@ -0,0 +1,13 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system use, +# "ant.properties", and override values to adapt the script to your +# project structure. + +# Project target. +target=android-10 + +android.library.reference.1=../../../../cocos/2d/platform/android/java diff --git a/samples/lua-tests/project/proj.android/res/values/strings.xml b/samples/lua-tests/project/proj.android/res/values/strings.xml new file mode 100644 index 0000000000..0ad2cc48ff --- /dev/null +++ b/samples/lua-tests/project/proj.android/res/values/strings.xml @@ -0,0 +1,4 @@ + + + TestLua + diff --git a/samples/lua-tests/project/proj.android/src/com/cocos2dx/sample/LuaJavaBridgeTest/LuaJavaBridgeTest.java b/samples/lua-tests/project/proj.android/src/com/cocos2dx/sample/LuaJavaBridgeTest/LuaJavaBridgeTest.java new file mode 100644 index 0000000000..969fb7be47 --- /dev/null +++ b/samples/lua-tests/project/proj.android/src/com/cocos2dx/sample/LuaJavaBridgeTest/LuaJavaBridgeTest.java @@ -0,0 +1,38 @@ +/**************************************************************************** +Copyright (c) 2010-2012 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ****************************************************************************/ +package com.cocos2dx.sample.LuaJavaBridgeTest; + +import org.cocos2dx.lib.Cocos2dxLuaJavaBridge; + +public class LuaJavaBridgeTest +{ + public static int addTwoNumbers(final int num1,final int num2){ + return num1 + num2; + } + + public static void callbackLua(final String tipInfo,final int luaFunc){ + Cocos2dxLuaJavaBridge.callLuaFunctionWithString(luaFunc, "success"); + Cocos2dxLuaJavaBridge.releaseLuaFunction(luaFunc); + } +} diff --git a/samples/lua-tests/project/proj.android/src/org/cocos2dx/testlua/Cocos2dxActivity.java b/samples/lua-tests/project/proj.android/src/org/cocos2dx/testlua/Cocos2dxActivity.java new file mode 100644 index 0000000000..c5eac04f8c --- /dev/null +++ b/samples/lua-tests/project/proj.android/src/org/cocos2dx/testlua/Cocos2dxActivity.java @@ -0,0 +1,32 @@ +package org.cocos2dx.testlua; + +import android.app.NativeActivity; +import android.os.Bundle; + +public class Cocos2dxActivity extends NativeActivity{ + + @Override + protected void onCreate(Bundle savedInstanceState) { + // TODO Auto-generated method stub + super.onCreate(savedInstanceState); + + //For supports translucency + + //1.change "attribs" in cocos\2d\platform\android\nativeactivity.cpp + /*const EGLint attribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + //EGL_BLUE_SIZE, 5, -->delete + //EGL_GREEN_SIZE, 6, -->delete + //EGL_RED_SIZE, 5, -->delete + EGL_BUFFER_SIZE, 32, //-->new field + EGL_DEPTH_SIZE, 16, + EGL_STENCIL_SIZE, 8, + EGL_NONE + };*/ + + //2.Set the format of window + // getWindow().setFormat(PixelFormat.TRANSLUCENT); + + } +} diff --git a/samples/lua-tests/project/proj.ios_mac/ios/AppController.h b/samples/lua-tests/project/proj.ios_mac/ios/AppController.h new file mode 100644 index 0000000000..3d51064ca0 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/AppController.h @@ -0,0 +1,33 @@ +/**************************************************************************** + Copyright (c) 2010 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +@class RootViewController; + +@interface AppController : NSObject { + UIWindow *window; + RootViewController *viewController; +} + +@end + diff --git a/samples/lua-tests/project/proj.ios_mac/ios/AppController.mm b/samples/lua-tests/project/proj.ios_mac/ios/AppController.mm new file mode 100644 index 0000000000..c7d5d8312f --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/AppController.mm @@ -0,0 +1,142 @@ +/**************************************************************************** + Copyright (c) 2010 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +#import +#import "AppController.h" +#import "cocos2d.h" +#import "CCEAGLView.h" +#import "AppDelegate.h" + +#import "RootViewController.h" + +@implementation AppController + +#pragma mark - +#pragma mark Application lifecycle + +// cocos2d application instance +static AppDelegate s_sharedApplication; + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + + // Override point for customization after application launch. + + // Add the view controller's view to the window and display. + window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; + CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds] + pixelFormat: kEAGLColorFormatRGBA8 + depthFormat: GL_DEPTH24_STENCIL8_OES + preserveBackbuffer: NO + sharegroup: nil + multiSampling: NO + numberOfSamples: 0 ]; + + [eaglView setMultipleTouchEnabled:YES]; + + // Use RootViewController manage CCEAGLView + viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; + viewController.wantsFullScreenLayout = YES; + viewController.view = eaglView; + + // Set RootViewController to window + if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0) + { + // warning: addSubView doesn't work on iOS6 + [window addSubview: viewController.view]; + } + else + { + // use this method on ios6 + [window setRootViewController:viewController]; + } + + [window makeKeyAndVisible]; + + [[UIApplication sharedApplication] setStatusBarHidden: YES]; + + + // IMPORTANT: Setting the GLView should be done after creating the RootViewController + cocos2d::GLView *glview = cocos2d::GLView::createWithEAGLView(eaglView); + cocos2d::Director::getInstance()->setOpenGLView(glview); + + cocos2d::Application::getInstance()->run(); + return YES; +} + + +- (void)applicationWillResignActive:(UIApplication *)application { + /* + Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. + */ + cocos2d::Director::getInstance()->pause(); +} + +- (void)applicationDidBecomeActive:(UIApplication *)application { + /* + Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. + */ + cocos2d::Director::getInstance()->resume(); +} + +- (void)applicationDidEnterBackground:(UIApplication *)application { + /* + Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + If your application supports background execution, called instead of applicationWillTerminate: when the user quits. + */ + cocos2d::Application::getInstance()->applicationDidEnterBackground(); +} + +- (void)applicationWillEnterForeground:(UIApplication *)application { + /* + Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background. + */ + cocos2d::Application::getInstance()->applicationWillEnterForeground(); +} + +- (void)applicationWillTerminate:(UIApplication *)application { + /* + Called when the application is about to terminate. + See also applicationDidEnterBackground:. + */ +} + + +#pragma mark - +#pragma mark Memory management + +- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { + /* + Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later. + */ + cocos2d::Director::getInstance()->purgeCachedData(); +} + + +- (void)dealloc { + [super dealloc]; +} + + +@end + diff --git a/samples/lua-tests/project/proj.ios_mac/ios/Default-568h@2x.png.REMOVED.git-id b/samples/lua-tests/project/proj.ios_mac/ios/Default-568h@2x.png.REMOVED.git-id new file mode 100644 index 0000000000..8f5838f3a8 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/Default-568h@2x.png.REMOVED.git-id @@ -0,0 +1 @@ +66c6d1cead373b45218424f6a82f370897e443e4 \ No newline at end of file diff --git a/samples/lua-tests/project/proj.ios_mac/ios/Default@2x.png.REMOVED.git-id b/samples/lua-tests/project/proj.ios_mac/ios/Default@2x.png.REMOVED.git-id new file mode 100644 index 0000000000..8843505b20 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/Default@2x.png.REMOVED.git-id @@ -0,0 +1 @@ +84689888a14a2123d2b39f7f2f61be8c15207479 \ No newline at end of file diff --git a/samples/lua-tests/project/proj.ios_mac/ios/LuaObjectCBridgeTest.h b/samples/lua-tests/project/proj.ios_mac/ios/LuaObjectCBridgeTest.h new file mode 100644 index 0000000000..882dab3ed7 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/LuaObjectCBridgeTest.h @@ -0,0 +1,20 @@ +#ifndef COCOS2DX_SAMPLES_TESTLUA_PROJ_IOS_LUAOBJECTCBRIDGETEST_H +#define COCOS2DX_SAMPLES_TESTLUA_PROJ_IOS_LUAOBJECTCBRIDGETEST_H +#import + +@interface LuaObjectCBridgeTest : NSObject { + int _scriptHandler; +} ++ (LuaObjectCBridgeTest*) getInstance; ++ (void) destroyInstance; + ++ (void) registerScriptHandler:(NSDictionary *)dict; ++ (void) unregisterScriptHandler; + ++ (int) addTwoNumbers:(NSDictionary *)dict; ++ (void) callbackScriptHandler; + +- (void) init; +@end + +#endif // COCOS2DX_SAMPLES_TESTLUA_PROJ_IOS_LUAOBJECTCBRIDGETEST_H diff --git a/samples/lua-tests/project/proj.ios_mac/ios/LuaObjectCBridgeTest.mm b/samples/lua-tests/project/proj.ios_mac/ios/LuaObjectCBridgeTest.mm new file mode 100644 index 0000000000..624e338206 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/LuaObjectCBridgeTest.mm @@ -0,0 +1,81 @@ +#import "LuaObjectCBridgeTest.h" + +#include "cocos2d.h" +#include "CCLuaEngine.h" +#include "CCLuaBridge.h" + +using namespace cocos2d; + +@implementation LuaObjectCBridgeTest + +static LuaObjectCBridgeTest* s_instance = nil; + ++ (LuaObjectCBridgeTest*) getInstance +{ + if (!s_instance) + { + s_instance = [LuaObjectCBridgeTest alloc]; + [s_instance init]; + } + + return s_instance; +} + ++ (void) destroyInstance +{ + [s_instance release]; +} + +- (void) setScriptHandler:(int)scriptHandler +{ + if (_scriptHandler) + { + LuaBridge::releaseLuaFunctionById(_scriptHandler); + _scriptHandler = 0; + } + _scriptHandler = scriptHandler; +} + +- (int) getScriptHandler +{ + return _scriptHandler; +} + + ++(void) registerScriptHandler:(NSDictionary *)dict +{ + [[LuaObjectCBridgeTest getInstance] setScriptHandler:[[dict objectForKey:@"scriptHandler"] intValue]]; +} + + ++ (void) unregisterScriptHandler +{ + [[LuaObjectCBridgeTest getInstance] setScriptHandler:0]; +} + ++ (int) addTwoNumbers:(NSDictionary *)dict +{ + int num1 = [[dict objectForKey:@"num1"] intValue]; + int num2 = [[dict objectForKey:@"num2"] intValue]; + + return num1 + num2; +} + ++ (void) callbackScriptHandler +{ + int scriptHandler = [[LuaObjectCBridgeTest getInstance] getScriptHandler]; + if (scriptHandler) + { + LuaBridge::pushLuaFunctionById(scriptHandler); + LuaStack *stack = LuaBridge::getStack(); + stack->pushString("success"); + stack->executeFunction(1); + } +} + +- (void)init +{ + _scriptHandler = 0; +} + +@end diff --git a/samples/lua-tests/project/proj.ios_mac/ios/RootViewController.h b/samples/lua-tests/project/proj.ios_mac/ios/RootViewController.h new file mode 100644 index 0000000000..11dfc4bf88 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/RootViewController.h @@ -0,0 +1,33 @@ +/**************************************************************************** + Copyright (c) 2010-2011 cocos2d-x.org + Copyright (c) 2010 Ricardo Quesada + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#import + + +@interface RootViewController : UIViewController { + +} +- (BOOL)prefersStatusBarHidden; +@end diff --git a/samples/lua-tests/project/proj.ios_mac/ios/RootViewController.mm b/samples/lua-tests/project/proj.ios_mac/ios/RootViewController.mm new file mode 100644 index 0000000000..a00da00584 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/RootViewController.mm @@ -0,0 +1,96 @@ +/**************************************************************************** + Copyright (c) 2010-2011 cocos2d-x.org + Copyright (c) 2010 Ricardo Quesada + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#import "RootViewController.h" + + +@implementation RootViewController + +/* + // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. +- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { + if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { + // Custom initialization + } + return self; +} +*/ + +/* +// Implement loadView to create a view hierarchy programmatically, without using a nib. +- (void)loadView { +} +*/ + +/* +// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. +- (void)viewDidLoad { + [super viewDidLoad]; +} + +*/ +// Override to allow orientations other than the default portrait orientation. +// This method is deprecated on ios6 +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { + return UIInterfaceOrientationIsLandscape( interfaceOrientation ); +} + +// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead +- (NSUInteger) supportedInterfaceOrientations{ +#ifdef __IPHONE_6_0 + return UIInterfaceOrientationMaskAllButUpsideDown; +#endif +} + +- (BOOL) shouldAutorotate { + return YES; +} + +//fix not hide status on ios7 +- (BOOL)prefersStatusBarHidden +{ + return YES; +} + +- (void)didReceiveMemoryWarning { + // Releases the view if it doesn't have a superview. + [super didReceiveMemoryWarning]; + + // Release any cached data, images, etc that aren't in use. +} + +- (void)viewDidUnload { + [super viewDidUnload]; + // Release any retained subviews of the main view. + // e.g. self.myOutlet = nil; +} + + +- (void)dealloc { + [super dealloc]; +} + + +@end diff --git a/samples/lua-tests/project/proj.ios_mac/ios/TestLua_Prefix.pch b/samples/lua-tests/project/proj.ios_mac/ios/TestLua_Prefix.pch new file mode 100644 index 0000000000..4c4fc38a96 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/TestLua_Prefix.pch @@ -0,0 +1,8 @@ +// +// Prefix header for all source files of the 'TestLua' target in the 'TestLua' project +// + +#ifdef __OBJC__ + #import + #import +#endif diff --git a/samples/lua-tests/project/proj.ios_mac/ios/main.m b/samples/lua-tests/project/proj.ios_mac/ios/main.m new file mode 100644 index 0000000000..b657da1334 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/ios/main.m @@ -0,0 +1,15 @@ +// +// main.m +// TestLua +// +// Copyright __MyCompanyName__ 2011. All rights reserved. +// + +#import + +int main(int argc, char *argv[]) { + NSAutoreleasePool *pool = [NSAutoreleasePool new]; + int retVal = UIApplicationMain(argc, argv, nil, @"AppController"); + [pool release]; + return retVal; +} diff --git a/samples/lua-tests/project/proj.ios_mac/mac/Icon.icns.REMOVED.git-id b/samples/lua-tests/project/proj.ios_mac/mac/Icon.icns.REMOVED.git-id new file mode 100644 index 0000000000..9874ec6979 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/mac/Icon.icns.REMOVED.git-id @@ -0,0 +1 @@ +3d09e8fb4f4ca1c1ae7ab0a6948db592c7c3d9a0 \ No newline at end of file diff --git a/samples/lua-tests/project/proj.ios_mac/mac/LuaObjectCBridgeTest.h b/samples/lua-tests/project/proj.ios_mac/mac/LuaObjectCBridgeTest.h new file mode 100644 index 0000000000..882dab3ed7 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/mac/LuaObjectCBridgeTest.h @@ -0,0 +1,20 @@ +#ifndef COCOS2DX_SAMPLES_TESTLUA_PROJ_IOS_LUAOBJECTCBRIDGETEST_H +#define COCOS2DX_SAMPLES_TESTLUA_PROJ_IOS_LUAOBJECTCBRIDGETEST_H +#import + +@interface LuaObjectCBridgeTest : NSObject { + int _scriptHandler; +} ++ (LuaObjectCBridgeTest*) getInstance; ++ (void) destroyInstance; + ++ (void) registerScriptHandler:(NSDictionary *)dict; ++ (void) unregisterScriptHandler; + ++ (int) addTwoNumbers:(NSDictionary *)dict; ++ (void) callbackScriptHandler; + +- (void) init; +@end + +#endif // COCOS2DX_SAMPLES_TESTLUA_PROJ_IOS_LUAOBJECTCBRIDGETEST_H diff --git a/samples/lua-tests/project/proj.ios_mac/mac/LuaObjectCBridgeTest.mm b/samples/lua-tests/project/proj.ios_mac/mac/LuaObjectCBridgeTest.mm new file mode 100644 index 0000000000..624e338206 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/mac/LuaObjectCBridgeTest.mm @@ -0,0 +1,81 @@ +#import "LuaObjectCBridgeTest.h" + +#include "cocos2d.h" +#include "CCLuaEngine.h" +#include "CCLuaBridge.h" + +using namespace cocos2d; + +@implementation LuaObjectCBridgeTest + +static LuaObjectCBridgeTest* s_instance = nil; + ++ (LuaObjectCBridgeTest*) getInstance +{ + if (!s_instance) + { + s_instance = [LuaObjectCBridgeTest alloc]; + [s_instance init]; + } + + return s_instance; +} + ++ (void) destroyInstance +{ + [s_instance release]; +} + +- (void) setScriptHandler:(int)scriptHandler +{ + if (_scriptHandler) + { + LuaBridge::releaseLuaFunctionById(_scriptHandler); + _scriptHandler = 0; + } + _scriptHandler = scriptHandler; +} + +- (int) getScriptHandler +{ + return _scriptHandler; +} + + ++(void) registerScriptHandler:(NSDictionary *)dict +{ + [[LuaObjectCBridgeTest getInstance] setScriptHandler:[[dict objectForKey:@"scriptHandler"] intValue]]; +} + + ++ (void) unregisterScriptHandler +{ + [[LuaObjectCBridgeTest getInstance] setScriptHandler:0]; +} + ++ (int) addTwoNumbers:(NSDictionary *)dict +{ + int num1 = [[dict objectForKey:@"num1"] intValue]; + int num2 = [[dict objectForKey:@"num2"] intValue]; + + return num1 + num2; +} + ++ (void) callbackScriptHandler +{ + int scriptHandler = [[LuaObjectCBridgeTest getInstance] getScriptHandler]; + if (scriptHandler) + { + LuaBridge::pushLuaFunctionById(scriptHandler); + LuaStack *stack = LuaBridge::getStack(); + stack->pushString("success"); + stack->executeFunction(1); + } +} + +- (void)init +{ + _scriptHandler = 0; +} + +@end diff --git a/samples/lua-tests/project/proj.ios_mac/mac/TestLua_Prefix.pch b/samples/lua-tests/project/proj.ios_mac/mac/TestLua_Prefix.pch new file mode 100644 index 0000000000..46c36a7e99 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/mac/TestLua_Prefix.pch @@ -0,0 +1,7 @@ +// +// Prefix header for all source files of the 'Paralaxer' target in the 'Paralaxer' project +// + +#ifdef __OBJC__ + #import +#endif diff --git a/samples/lua-tests/project/proj.ios_mac/mac/en.lproj/InfoPlist.strings b/samples/lua-tests/project/proj.ios_mac/mac/en.lproj/InfoPlist.strings new file mode 100644 index 0000000000..477b28ff8f --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/mac/en.lproj/InfoPlist.strings @@ -0,0 +1,2 @@ +/* Localized versions of Info.plist keys */ + diff --git a/samples/lua-tests/project/proj.ios_mac/mac/en.lproj/MainMenu.xib b/samples/lua-tests/project/proj.ios_mac/mac/en.lproj/MainMenu.xib new file mode 100644 index 0000000000..9f99439250 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/mac/en.lproj/MainMenu.xib @@ -0,0 +1,812 @@ + + + + 1060 + 10K549 + 1938 + 1038.36 + 461.00 + + com.apple.InterfaceBuilder.CocoaPlugin + 1938 + + + YES + NSMenuItem + NSCustomObject + NSMenu + + + YES + com.apple.InterfaceBuilder.CocoaPlugin + + + PluginDependencyRecalculationVersion + + + + YES + + NSApplication + + + FirstResponder + + + NSApplication + + + AMainMenu + + YES + + + HelloCpp + + 1048576 + 2147483647 + + NSImage + NSMenuCheckmark + + + NSImage + NSMenuMixedState + + submenuAction: + + HelloCpp + + YES + + + About HelloCpp + + 2147483647 + + + + + + YES + YES + + + 1048576 + 2147483647 + + + + + + Preferences… + , + 1048576 + 2147483647 + + + + + + YES + YES + + + 1048576 + 2147483647 + + + + + + Services + + 1048576 + 2147483647 + + + submenuAction: + + Services + + YES + + _NSServicesMenu + + + + + YES + YES + + + 1048576 + 2147483647 + + + + + + Hide HelloCpp + h + 1048576 + 2147483647 + + + + + + Hide Others + h + 1572864 + 2147483647 + + + + + + Show All + + 1048576 + 2147483647 + + + + + + YES + YES + + + 1048576 + 2147483647 + + + + + + Quit HelloCpp + q + 1048576 + 2147483647 + + + + + _NSAppleMenu + + + + + View + + 1048576 + 2147483647 + + + submenuAction: + + View + + YES + + + Toggle Fullscreen + f + 1048576 + 2147483647 + + + + + + + + + Window + + 1048576 + 2147483647 + + + submenuAction: + + Window + + YES + + + Minimize + m + 1048576 + 2147483647 + + + + + + Zoom + + 1048576 + 2147483647 + + + + + + YES + YES + + + 1048576 + 2147483647 + + + + + + Bring All to Front + + 1048576 + 2147483647 + + + + + _NSWindowsMenu + + + + + Help + + 2147483647 + + + submenuAction: + + Help + + YES + + + HelloCpp Help + ? + 1048576 + 2147483647 + + + + + _NSHelpMenu + + + + _NSMainMenu + + + AppController + + + NSFontManager + + + + + YES + + + terminate: + + + + 449 + + + + orderFrontStandardAboutPanel: + + + + 142 + + + + delegate + + + + 495 + + + + performMiniaturize: + + + + 37 + + + + arrangeInFront: + + + + 39 + + + + performZoom: + + + + 240 + + + + hide: + + + + 367 + + + + hideOtherApplications: + + + + 368 + + + + unhideAllApplications: + + + + 370 + + + + showHelp: + + + + 493 + + + + toggleFullScreen: + + + + 537 + + + + + YES + + 0 + + YES + + + + + + -2 + + + File's Owner + + + -1 + + + First Responder + + + -3 + + + Application + + + 29 + + + YES + + + + + + + + + 19 + + + YES + + + + + + 56 + + + YES + + + + + + 57 + + + YES + + + + + + + + + + + + + + + + 58 + + + + + 134 + + + + + 150 + + + + + 136 + + + + + 144 + + + + + 129 + + + + + 143 + + + + + 236 + + + + + 131 + + + YES + + + + + + 149 + + + + + 145 + + + + + 130 + + + + + 24 + + + YES + + + + + + + + + 92 + + + + + 5 + + + + + 239 + + + + + 23 + + + + + 295 + + + YES + + + + + + 296 + + + YES + + + + + + 420 + + + + + 490 + + + YES + + + + + + 491 + + + YES + + + + + + 492 + + + + + 494 + + + + + 536 + + + + + + + YES + + YES + -1.IBPluginDependency + -2.IBPluginDependency + -3.IBPluginDependency + 129.IBPluginDependency + 130.IBPluginDependency + 131.IBPluginDependency + 134.IBPluginDependency + 136.IBPluginDependency + 143.IBPluginDependency + 144.IBPluginDependency + 145.IBPluginDependency + 149.IBPluginDependency + 150.IBPluginDependency + 19.IBPluginDependency + 23.IBPluginDependency + 236.IBPluginDependency + 239.IBPluginDependency + 24.IBPluginDependency + 29.IBPluginDependency + 295.IBPluginDependency + 296.IBPluginDependency + 420.IBPluginDependency + 490.IBPluginDependency + 491.IBPluginDependency + 492.IBPluginDependency + 494.IBPluginDependency + 5.IBPluginDependency + 536.IBPluginDependency + 56.IBPluginDependency + 57.IBPluginDependency + 58.IBPluginDependency + 92.IBPluginDependency + + + YES + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + + + + YES + + + + + + YES + + + + + 541 + + + + YES + + AppController + NSObject + + YES + + YES + exitFullScreen: + toggleFullScreen: + + + YES + id + id + + + + YES + + YES + exitFullScreen: + toggleFullScreen: + + + YES + + exitFullScreen: + id + + + toggleFullScreen: + id + + + + + YES + + YES + glView + window + + + YES + EAGLView + Window + + + + YES + + YES + glView + window + + + YES + + glView + EAGLView + + + window + Window + + + + + IBProjectSource + ./Classes/AppController.h + + + + EAGLView + NSOpenGLView + + IBProjectSource + ./Classes/EAGLView.h + + + + + 0 + IBCocoaFramework + + com.apple.InterfaceBuilder.CocoaPlugin.InterfaceBuilder3 + + + YES + 3 + + YES + + YES + NSMenuCheckmark + NSMenuMixedState + + + YES + {9, 8} + {7, 2} + + + + diff --git a/samples/lua-tests/project/proj.ios_mac/mac/main.cpp b/samples/lua-tests/project/proj.ios_mac/mac/main.cpp new file mode 100644 index 0000000000..1814d16c15 --- /dev/null +++ b/samples/lua-tests/project/proj.ios_mac/mac/main.cpp @@ -0,0 +1,33 @@ +/**************************************************************************** + Copyright (c) 2010 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "AppDelegate.h" + +USING_NS_CC; + +int main(int argc, char *argv[]) +{ + AppDelegate app; + return Application::getInstance()->run(); +} diff --git a/samples/lua-tests/project/proj.linux/main.cpp b/samples/lua-tests/project/proj.linux/main.cpp new file mode 100644 index 0000000000..e420889600 --- /dev/null +++ b/samples/lua-tests/project/proj.linux/main.cpp @@ -0,0 +1,16 @@ +#include "../Classes/AppDelegate.h" +#include "cocos2d.h" + +#include +#include +#include +#include + +USING_NS_CC; + +int main(int argc, char **argv) +{ + // create the application instance + AppDelegate app; + return Application::getInstance()->run(); +} diff --git a/samples/lua-tests/project/proj.win32/TestLua.rc b/samples/lua-tests/project/proj.win32/TestLua.rc new file mode 100644 index 0000000000..8b155d0f9a --- /dev/null +++ b/samples/lua-tests/project/proj.win32/TestLua.rc @@ -0,0 +1,86 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#define APSTUDIO_HIDDEN_SYMBOLS +#include "windows.h" +#undef APSTUDIO_HIDDEN_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (U.S.) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +#ifdef _WIN32 +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) +#endif //_WIN32 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +#endif // APSTUDIO_INVOKED + +///////////////////////////////////////////////////////////////////////////// +// +// Icon +// + +// Icon with lowest ID value placed first to ensure application icon +// remains consistent on all systems. +IDR_MAINFRAME ICON "res\\TestLua.ico" + +///////////////////////////////////////////////////////////////////////////// +// +// Version +// + +VS_VERSION_INFO VERSIONINFO + FILEVERSION 1,0,0,1 + PRODUCTVERSION 1,0,0,1 + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS 0x1L +#else + FILEFLAGS 0x0L +#endif + FILEOS 0x4L + FILETYPE 0x2L + FILESUBTYPE 0x0L +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904B0" + BEGIN + VALUE "CompanyName", "\0" + VALUE "FileDescription", "TestLua Module\0" + VALUE "FileVersion", "1, 0, 0, 1\0" + VALUE "InternalName", "TestLua\0" + VALUE "LegalCopyright", "Copyright \0" + VALUE "OriginalFilename", "TestLua.exe\0" + VALUE "ProductName", "TestLua Module\0" + VALUE "ProductVersion", "1, 0, 0, 1\0" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409, 0x04B0 + END +END + +///////////////////////////////////////////////////////////////////////////// +#endif // !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) diff --git a/samples/lua-tests/project/proj.win32/TestLua.win32.vcxproj b/samples/lua-tests/project/proj.win32/TestLua.win32.vcxproj new file mode 100644 index 0000000000..0cdffe39e5 --- /dev/null +++ b/samples/lua-tests/project/proj.win32/TestLua.win32.vcxproj @@ -0,0 +1,224 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {4E6A7A0E-DDD8-4BAA-8B22-C964069364ED} + TestLua + + + + Application + true + Unicode + v100 + v110 + v110_xp + + + Application + false + Unicode + v100 + v110 + v110_xp + + + + + + + + + + + + + + + + + $(SolutionDir)$(Configuration).win32\ + $(Configuration).win32\ + true + $(IncludePath) + $(SourcePath); + AllRules.ruleset + + + $(SolutionDir)$(Configuration).win32\ + $(Configuration).win32\ + false + + + $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\lib;$(LibraryPath) + + + $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\lib;$(LibraryPath) + + + + $(ProjectDir)..\Classes;$(EngineRoot)cocos\scripting\lua-bindings\auto;$(EngineRoot)cocos\scripting\lua-bindings\manual;$(EngineRoot)cocos\audio\include;$(EngineRoot)external\lua\luajit\include;$(EngineRoot)external\lua\tolua;$(EngineRoot)external\lua;$(EngineRoot)external\chipmunk\include\chipmunk;$(EngineRoot)extensions;$(EngineRoot);%(AdditionalIncludeDirectories) + + + Level3 + MultiThreadedDebugDLL + false + OldStyle + EnableFastChecks + Disabled + WIN32;_WINDOWS;STRICT;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGS_DEBUG;COCOS2D_DEBUG=1;%(PreprocessorDefinitions) + 4267;4251;4244;%(DisableSpecificWarnings) + true + + + Windows + MachineX86 + true + $(OutDir);%(AdditionalLibraryDirectories) + libcurl_imp.lib;lua51.lib;websockets.lib;%(AdditionalDependencies) + + + 0x0409 + $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;$(IntDir);%(AdditionalIncludeDirectories) + _DEBUG;%(PreprocessorDefinitions) + + + false + Win32 + _DEBUG;%(PreprocessorDefinitions) + TestLua.h + TestLua_i.c + TestLua_p.c + true + $(IntDir)/TestLua.tlb + + + + + if not exist "$(OutDir)" mkdir "$(OutDir)" +if exist "$(OutDir)\test-lua-res" rd /s /q "$(OutDir)\test-lua-res" +mkdir "$(OutDir)\test-lua-res" +mkdir "$(OutDir)\test-lua-res\src" +xcopy "$(ProjectDir)..\..\..\..\cocos\scripting\lua-bindings\script" "$(OutDir)\test-lua-res" /e /Y +xcopy "$(ProjectDir)..\..\src" "$(OutDir)\test-lua-res\src" /e /Y +xcopy "$(ProjectDir)..\..\..\cpp-tests\Resources" "$(OutDir)\test-lua-res" /e /Y +xcopy "$(ProjectDir)..\..\res" "$(OutDir)\test-lua-res" /e /Y + copy files + + + if not exist "$(OutDir)" mkdir "$(OutDir)" +xcopy /Y /Q "$(EngineRoot)external\websockets\prebuilt\win32\*.*" "$(OutDir)" + + + + + $(ProjectDir)..\Classes;$(EngineRoot)cocos\scripting\lua-bindings\auto;$(EngineRoot)cocos\scripting\lua-bindings\manual;$(EngineRoot)cocos\audio\include;$(EngineRoot)external\lua\luajit\include;$(EngineRoot)external\lua\tolua;$(EngineRoot)external\lua;$(EngineRoot)external\chipmunk\include\chipmunk;$(EngineRoot)extensions;$(EngineRoot);%(AdditionalIncludeDirectories) + + + Level3 + MultiThreadedDLL + + + ProgramDatabase + WIN32;_WINDOWS;STRICT;_CRT_SECURE_NO_WARNINGS;_SCL_SECURE_NO_WARNINGSNDEBUG;%(PreprocessorDefinitions) + 4267;4251;4244;%(DisableSpecificWarnings) + true + + + Windows + MachineX86 + $(OutDir);%(AdditionalLibraryDirectories) + libcurl_imp.lib;lua51.lib;websockets.lib;%(AdditionalDependencies) + true + + + 0x0409 + $(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v7.1A\include;$(IntDir);%(AdditionalIncludeDirectories) + NDEBUG;%(PreprocessorDefinitions) + + + false + Win32 + NDEBUG;%(PreprocessorDefinitions) + TestLua.h + TestLua_i.c + TestLua_p.c + true + $(IntDir)/TestLua.tlb + + + + + if not exist "$(OutDir)" mkdir "$(OutDir)" +if exist "$(OutDir)\test-lua-res" rd /s /q "$(OutDir)\test-lua-res" +mkdir "$(OutDir)\test-lua-res" +mkdir "$(OutDir)\test-lua-res\src" +xcopy "$(ProjectDir)..\..\..\..\cocos\scripting\lua-bindings\script" "$(OutDir)\test-lua-res" /e /Y +xcopy "$(ProjectDir)..\..\src" "$(OutDir)\test-lua-res\src" /e /Y +xcopy "$(ProjectDir)..\..\..\cpp-tests\Resources" "$(OutDir)\test-lua-res" /e /Y +xcopy "$(ProjectDir)..\..\res" "$(OutDir)\test-lua-res" /e /Y + copy files + + + if not exist "$(OutDir)" mkdir "$(OutDir)" +xcopy /Y /Q "$(EngineRoot)external\websockets\prebuilt\win32\*.*" "$(OutDir)" + + + + + + + + + + + + + + + {ddc3e27f-004d-4dd4-9dd3-931a013d2159} + + + {98a51ba8-fc3a-415b-ac8f-8c7bd464e93e} + + + {f8edd7fa-9a51-4e80-baeb-860825d2eac6} + + + {811c0dab-7b96-4bd3-a154-b7572b58e4ab} + + + {b57cf53f-2e49-4031-9822-047cc0e6bde2} + + + {b7c2a162-dec9-4418-972e-240ab3cbfcae} + + + {7e06e92c-537a-442b-9e4a-4761c84f8a1a} + + + {df2638c0-8128-4847-867c-6eafe3dee7b5} + + + {632a8f38-d0f0-4d22-86b3-d69f5e6bf63a} + + + {21b2c324-891f-48ea-ad1a-5ae13de12e28} + + + {207bc7a9-ccf1-4f2f-a04d-45f72242ae25} + + + + + + \ No newline at end of file diff --git a/samples/lua-tests/project/proj.win32/TestLua.win32.vcxproj.filters b/samples/lua-tests/project/proj.win32/TestLua.win32.vcxproj.filters new file mode 100644 index 0000000000..12f4b9ee7c --- /dev/null +++ b/samples/lua-tests/project/proj.win32/TestLua.win32.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {fc5cb953-2953-4968-83b3-39e3ff951754} + + + {037a9a02-b906-4cc5-ad98-304acd4e25ee} + + + + + Classes + + + win32 + + + Classes + + + + + Classes + + + win32 + + + Classes + + + \ No newline at end of file diff --git a/samples/lua-tests/project/proj.win32/TestLua.win32.vcxproj.user b/samples/lua-tests/project/proj.win32/TestLua.win32.vcxproj.user new file mode 100644 index 0000000000..63179a676c --- /dev/null +++ b/samples/lua-tests/project/proj.win32/TestLua.win32.vcxproj.user @@ -0,0 +1,10 @@ + + + + false + $(OutDir)/test-lua-res + $(OutDir)/test-lua-res + WindowsLocalDebugger + WindowsLocalDebugger + + \ No newline at end of file diff --git a/samples/lua-tests/project/proj.win32/main.cpp b/samples/lua-tests/project/proj.win32/main.cpp new file mode 100644 index 0000000000..eb96bb538f --- /dev/null +++ b/samples/lua-tests/project/proj.win32/main.cpp @@ -0,0 +1,35 @@ +#include "main.h" +#include "AppDelegate.h" +#include "cocos2d.h" + +USING_NS_CC; + +// uncomment below line, open debug console +#define USE_WIN32_CONSOLE + +int APIENTRY _tWinMain(HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPTSTR lpCmdLine, + int nCmdShow) +{ + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(lpCmdLine); + +#ifdef USE_WIN32_CONSOLE + AllocConsole(); + freopen("CONIN$", "r", stdin); + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); +#endif + + // create the application instance + AppDelegate app; + + int ret = Application::getInstance()->run(); + +#ifdef USE_WIN32_CONSOLE + FreeConsole(); +#endif + + return ret; +} diff --git a/samples/lua-tests/project/proj.win32/main.h b/samples/lua-tests/project/proj.win32/main.h new file mode 100644 index 0000000000..d756fd1e57 --- /dev/null +++ b/samples/lua-tests/project/proj.win32/main.h @@ -0,0 +1,10 @@ +#ifndef __MAIN_H__ +#define __MAIN_H__ + +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers + +// Windows Header Files: +#include +#include + +#endif // __WINMAIN_H__ diff --git a/samples/lua-tests/project/proj.win32/res/TestLua.ico b/samples/lua-tests/project/proj.win32/res/TestLua.ico new file mode 100644 index 0000000000..feaf932a74 Binary files /dev/null and b/samples/lua-tests/project/proj.win32/res/TestLua.ico differ diff --git a/samples/lua-tests/project/proj.win32/resource.h b/samples/lua-tests/project/proj.win32/resource.h new file mode 100644 index 0000000000..b64c0b240f --- /dev/null +++ b/samples/lua-tests/project/proj.win32/resource.h @@ -0,0 +1,20 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by TestLua.RC +// + +#define IDS_PROJNAME 100 +#define IDR_TESTLUA 100 + +#define ID_FILE_NEW_WINDOW 32771 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 201 +#define _APS_NEXT_CONTROL_VALUE 1000 +#define _APS_NEXT_SYMED_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 32775 +#endif +#endif diff --git a/samples/lua-tests/res/cocosbuilderRes/ccb/flower.jpg.REMOVED.git-id b/samples/lua-tests/res/cocosbuilderRes/ccb/flower.jpg.REMOVED.git-id new file mode 100644 index 0000000000..60dddf3fb7 --- /dev/null +++ b/samples/lua-tests/res/cocosbuilderRes/ccb/flower.jpg.REMOVED.git-id @@ -0,0 +1 @@ +38e3a5fabac55b5649842cb35930af7594f093ef \ No newline at end of file diff --git a/samples/lua-tests/res/cocosbuilderRes/ccb/gem-0.wav.REMOVED.git-id b/samples/lua-tests/res/cocosbuilderRes/ccb/gem-0.wav.REMOVED.git-id new file mode 100644 index 0000000000..f339009735 --- /dev/null +++ b/samples/lua-tests/res/cocosbuilderRes/ccb/gem-0.wav.REMOVED.git-id @@ -0,0 +1 @@ +13382b4a2fc6364d425aedd849893d8de79f72fb \ No newline at end of file diff --git a/samples/lua-tests/res/cocosbuilderRes/ccb/gem-1.wav.REMOVED.git-id b/samples/lua-tests/res/cocosbuilderRes/ccb/gem-1.wav.REMOVED.git-id new file mode 100644 index 0000000000..cb48f3aa14 --- /dev/null +++ b/samples/lua-tests/res/cocosbuilderRes/ccb/gem-1.wav.REMOVED.git-id @@ -0,0 +1 @@ +5a60849ce6d5a81e3d62587a6651eb1a7ed7d488 \ No newline at end of file diff --git a/samples/lua-tests/res/cocosbuilderRes/ccb/markerfelt24shadow.fnt.REMOVED.git-id b/samples/lua-tests/res/cocosbuilderRes/ccb/markerfelt24shadow.fnt.REMOVED.git-id new file mode 100644 index 0000000000..4b87d9cc98 --- /dev/null +++ b/samples/lua-tests/res/cocosbuilderRes/ccb/markerfelt24shadow.fnt.REMOVED.git-id @@ -0,0 +1 @@ +2685fbcab626d939a8e2ea65b05a8a82beb90afa \ No newline at end of file diff --git a/samples/lua-tests/src/AccelerometerTest/AccelerometerTest.lua b/samples/lua-tests/src/AccelerometerTest/AccelerometerTest.lua new file mode 100644 index 0000000000..76aeb3a008 --- /dev/null +++ b/samples/lua-tests/src/AccelerometerTest/AccelerometerTest.lua @@ -0,0 +1,73 @@ +local function AccelerometerMainLayer() + + local function title() + return "AccelerometerTest" + end + local layer = cc.Layer:create() + + local function onEnter() + layer:setAccelerometerEnabled(true) + + local label = cc.LabelTTF:create(title(), "Arial", 32) + layer:addChild(label, 1) + label:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 50) ) + + local ball = cc.Sprite:create("Images/ball.png") + ball:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y)) + layer:addChild(ball) + + local function accelerometerListener(event,x,y,z,timestamp) + local target = event:getCurrentTarget() + local ballSize = target:getContentSize() + local ptNowX,ptNowY = target:getPosition() + ptNowX = ptNowX + x * 9.81 + ptNowY = ptNowY + y * 9.81 + + local minX = math.floor(VisibleRect:left().x + ballSize.width / 2.0) + local maxX = math.floor(VisibleRect:right().x - ballSize.width / 2.0) + if ptNowX < minX then + ptNowX = minX + elseif ptNowX > maxX then + ptNowX = maxX + end + + local minY = math.floor(VisibleRect:bottom().y + ballSize.height / 2.0) + local maxY = math.floor(VisibleRect:top().y - ballSize.height / 2.0) + if ptNowY < minY then + ptNowY = minY + elseif ptNowY > maxY then + ptNowY = maxY + end + + target:setPosition(cc.p(ptNowX , ptNowY)) + end + + local listerner = cc.EventListenerAcceleration:create(accelerometerListener) + layer:getEventDispatcher():addEventListenerWithSceneGraphPriority(listerner,ball) + end + + local function onExit() + layer:setAccelerometerEnabled(false) + end + + local function onNodeEvent(event) + if "enter" == event then + onEnter() + elseif "exit" == event then + onExit() + end + end + + layer:registerScriptHandler(onNodeEvent) + + return layer +end + + +function AccelerometerMain() + cclog("AccelerometerMain") + local scene = cc.Scene:create() + scene:addChild(AccelerometerMainLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/ActionManagerTest/ActionManagerTest.lua b/samples/lua-tests/src/ActionManagerTest/ActionManagerTest.lua new file mode 100644 index 0000000000..5d3941c3bf --- /dev/null +++ b/samples/lua-tests/src/ActionManagerTest/ActionManagerTest.lua @@ -0,0 +1,194 @@ +local kTagNode = 0 +local kTagGrossini = 1 +local kTagSequence = 2 +local scheduler = cc.Director:getInstance():getScheduler() +-------------------------------------------------------------------- +-- +-- Test1 +-- +-------------------------------------------------------------------- + +local function CrashTest() + local ret = createTestLayer("Test 1. Should not crash") + + local child = cc.Sprite:create(s_pPathGrossini) + child:setPosition( 200,200 ) + ret:addChild(child, 1) + + --Sum of all action's duration is 1.5 second. + child:runAction(cc.RotateBy:create(1.5, 90)) + child:runAction(cc.Sequence:create(cc.DelayTime:create(1.4),cc.FadeOut:create(1.1))) + + local function removeThis() + ret:getParent():removeChild(ret, true) + Helper.nextAction() + end + + --After 1.5 second, self will be removed. + ret:runAction( cc.Sequence:create(cc.DelayTime:create(1.4),cc.CallFunc:create(removeThis))) + return ret +end + + +-------------------------------------------------------------------- +-- +-- LogicTest +-- +-------------------------------------------------------------------- +local function LogicTest() + local ret = createTestLayer("Logic test") + local grossini = cc.Sprite:create(s_pPathGrossini) + ret:addChild(grossini, 0, 2) + grossini:setPosition(200,200) + local function bugMe(node) + node:stopAllActions() --After this stop next action not working, if remove this stop everything is working + node:runAction(cc.ScaleTo:create(2, 2)) + end + grossini:runAction( cc.Sequence:create(cc.MoveBy:create(1, cc.p(150,0)) ,cc.CallFunc:create(bugMe))) + return ret +end + +-------------------------------------------------------------------- +-- +-- PauseTest +-- +-------------------------------------------------------------------- + +local function PauseTest() + local ret = createTestLayer("Pause Test") + local schedulerEntry = nil + local function unpause(dt) + scheduler:unscheduleScriptEntry(schedulerEntry) + schedulerEntry = nil + local node = ret:getChildByTag( kTagGrossini ) + local pDirector = cc.Director:getInstance() + pDirector:getActionManager():resumeTarget(node) + end + + local function onNodeEvent(event) + if event == "enter" then + local s = cc.Director:getInstance():getWinSize() + local l = cc.LabelTTF:create("After 3 seconds grossini should move", "Thonburi", 16) + ret:addChild(l) + l:setPosition( cc.p(s.width / 2, 245) ) + + local grossini = cc.Sprite:create(s_pPathGrossini) + ret:addChild(grossini, 0, kTagGrossini) + grossini:setPosition(cc.p(200,200)) + + local action = cc.MoveBy:create(1, cc.p(150,0)) + + local pDirector = cc.Director:getInstance() + pDirector:getActionManager():addAction(action, grossini, true) + + schedulerEntry = scheduler:scheduleScriptFunc(unpause, 3.0, false) + + elseif event == "exit" then + if schedulerEntry ~= nil then + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + end + + ret:registerScriptHandler(onNodeEvent) + return ret +end + +-------------------------------------------------------------------- +-- +-- RemoveTest +-- +-------------------------------------------------------------------- +local function RemoveTest() + local ret = createTestLayer("Remove Test") + local l = cc.LabelTTF:create("Should not crash", "Thonburi", 16) + local s = cc.Director:getInstance():getWinSize() + ret:addChild(l) + l:setPosition( cc.p(s.width / 2, 245)) + + local pMove = cc.MoveBy:create(2, cc.p(200, 0)) + local function stopAction() + local pSprite = ret:getChildByTag(kTagGrossini) + pSprite:stopActionByTag(kTagSequence) + end + + local callfunc = cc.CallFunc:create(stopAction) + local pSequence = cc.Sequence:create(pMove,callfunc) + pSequence:setTag(kTagSequence) + + local pChild = cc.Sprite:create(s_pPathGrossini) + pChild:setPosition( 200, 200 ) + + ret:addChild(pChild, 1, kTagGrossini) + pChild:runAction(pSequence) + return ret +end + + +-------------------------------------------------------------------- +-- +-- ResumeTest +-- +-------------------------------------------------------------------- +local function ResumeTest() + local ret = createTestLayer("Resume Test") + + local schedulerEntry = nil + local function resumeGrossini(time) + scheduler:unscheduleScriptEntry(schedulerEntry) + schedulerEntry = nil + local pGrossini = ret:getChildByTag(kTagGrossini) + local pDirector = cc.Director:getInstance() + pDirector:getActionManager():resumeTarget(pGrossini) + end + + + local function onNodeEvent(event) + if event == "enter" then + local l = cc.LabelTTF:create("Grossini only rotate/scale in 3 seconds", "Thonburi", 16) + ret:addChild(l) + local s = cc.Director:getInstance():getWinSize() + l:setPosition( s.width / 2, 245) + + local pGrossini = cc.Sprite:create(s_pPathGrossini) + ret:addChild(pGrossini, 0, kTagGrossini) + pGrossini:setPosition(200,200) + + pGrossini:runAction(cc.ScaleBy:create(2, 2)) + + local pDirector = cc.Director:getInstance() + pDirector:getActionManager():pauseTarget(pGrossini) + pGrossini:runAction(cc.RotateBy:create(2, 360)) + + schedulerEntry = scheduler:scheduleScriptFunc(resumeGrossini, 3.0, false) + elseif event == "exit" then + if schedulerEntry ~= nil then + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + + +function ActionManagerTestMain() + cclog("ActionManagerTestMain") + Helper.index = 1 + cc.Director:getInstance():setDepthTest(true) + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + CrashTest, + LogicTest, + PauseTest, + RemoveTest, + ResumeTest + } + scene:addChild(CrashTest()) + scene:addChild(CreateBackMenuItem()) + return scene +end + diff --git a/samples/lua-tests/src/ActionsEaseTest/ActionsEaseTest.lua b/samples/lua-tests/src/ActionsEaseTest/ActionsEaseTest.lua new file mode 100644 index 0000000000..7ceb78c0e7 --- /dev/null +++ b/samples/lua-tests/src/ActionsEaseTest/ActionsEaseTest.lua @@ -0,0 +1,468 @@ +local kTagAction1 = 1 +local kTagAction2 = 2 +local kTagSlider = 1 + +local s = cc.Director:getInstance():getWinSize() +local scheduler = cc.Director:getInstance():getScheduler() + +local function createSimpleMoveBy() + return cc.MoveBy:create(3, cc.p(s.width - 130, 0)) +end + +local function createSimpleDelayTime() + return cc.DelayTime:create(0.25) +end + +local function positionForTwo() + grossini:setPosition(cc.p(60, s.height * 1 / 5)) + tamara:setPosition(cc.p(60, s.height * 4 / 5)) + kathia:setVisible(false) +end + +local function getBaseLayer() + local layer = cc.Layer:create() + + grossini = cc.Sprite:create(s_pPathGrossini) + tamara = cc.Sprite:create(s_pPathSister1) + kathia = cc.Sprite:create(s_pPathSister2) + + layer:addChild(grossini, 3) + layer:addChild(kathia, 2) + layer:addChild(tamara, 1) + + grossini:setPosition(cc.p(60, s.height * 1 / 5)) + kathia:setPosition(cc.p(60, s.height * 2.5 / 5)) + tamara:setPosition(cc.p(60, s.height * 4 / 5)) + + Helper.initWithLayer(layer) + + return layer +end + +----------------------------------- +-- SpriteEase +----------------------------------- +local SpriteEase_entry = nil + +local function testStopAction(dt) + scheduler:unscheduleScriptEntry(SpriteEase_entry) + tamara:stopActionByTag(1) + kathia:stopActionByTag(1) + grossini:stopActionByTag(1) +end + +local function SpriteEase_onEnterOrExit(tag) + if tag == "enter" then + SpriteEase_entry = scheduler:scheduleScriptFunc(testStopAction, 6.25, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(SpriteEase_entry) + end +end + +local function SpriteEase() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease_in = cc.EaseIn:create(createSimpleMoveBy(), 2.5) + local move_ease_in_back = move_ease_in:reverse() + + local move_ease_out = cc.EaseOut:create(createSimpleMoveBy(), 2.5) + local move_ease_out_back = move_ease_out:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) + local seq2 = cc.Sequence:create(move_ease_in,createSimpleDelayTime(),move_ease_in_back,createSimpleDelayTime()) + local seq3 = cc.Sequence:create(move_ease_out,createSimpleDelayTime(),move_ease_out_back,createSimpleDelayTime()) + + local a2 = grossini:runAction(cc.RepeatForever:create(seq1)) + a2:setTag(1) + local a1 = tamara:runAction(cc.RepeatForever:create(seq2)) + a1:setTag(1) + local a = kathia:runAction(cc.RepeatForever:create(seq3)) + a:setTag(1) + + layer:registerScriptHandler(SpriteEase_onEnterOrExit) + + Helper.titleLabel:setString("EaseIn - EaseOut - Stop") + return layer +end + +----------------------------------- +-- SpriteEaseInOut +----------------------------------- +local function SpriteEaseInOut() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + + local move_ease_inout1 = cc.EaseInOut:create(createSimpleMoveBy(), 0.65) + local move_ease_inout_back1 = move_ease_inout1:reverse() + + local move_ease_inout2 = cc.EaseInOut:create(createSimpleMoveBy(), 1.35) + local move_ease_inout_back2 = move_ease_inout2:reverse() + + local move_ease_inout3 = cc.EaseInOut:create(createSimpleMoveBy(), 1.0) + local move_ease_inout_back3 = move_ease_inout3:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move_ease_inout1,delay,move_ease_inout_back1,createSimpleDelayTime()) + local seq2 = cc.Sequence:create(move_ease_inout2,createSimpleDelayTime(),move_ease_inout_back2,createSimpleDelayTime()) + local seq3 = cc.Sequence:create(move_ease_inout3, createSimpleDelayTime(), move_ease_inout_back3, createSimpleDelayTime() ) + + tamara:runAction(cc.RepeatForever:create(seq1)) + kathia:runAction(cc.RepeatForever:create(seq2)) + grossini:runAction(cc.RepeatForever:create(seq3)) + + Helper.titleLabel:setString("EaseInOut and rates") + return layer +end + +----------------------------------- +-- SpriteEaseExponential +----------------------------------- +local function SpriteEaseExponential() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease_in = cc.EaseExponentialIn:create(createSimpleMoveBy()) + local move_ease_in_back = move_ease_in:reverse() + + local move_ease_out = cc.EaseExponentialOut:create(createSimpleMoveBy()) + local move_ease_out_back = move_ease_out:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) + local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) + local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime()) + + grossini:runAction(cc.RepeatForever:create(seq1)) + tamara:runAction(cc.RepeatForever:create(seq2)) + kathia:runAction(cc.RepeatForever:create(seq3)) + + Helper.titleLabel:setString("ExpIn - ExpOut actions") + return layer +end + +----------------------------------- +-- SpriteEaseExponentialInOut +----------------------------------- +local function SpriteEaseExponentialInOut() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease = cc.EaseExponentialInOut:create(createSimpleMoveBy()) + local move_ease_back = move_ease:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) + local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime() ) + + positionForTwo() + + grossini:runAction(cc.RepeatForever:create(seq1)) + tamara:runAction(cc.RepeatForever:create(seq2)) + + Helper.titleLabel:setString("EaseExponentialInOut action") + return layer +end + +----------------------------------- +-- SpriteEaseSine +----------------------------------- +local function SpriteEaseSine() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease_in = cc.EaseSineIn:create(createSimpleMoveBy()) + local move_ease_in_back = move_ease_in:reverse() + + local move_ease_out = cc.EaseSineOut:create(createSimpleMoveBy()) + local move_ease_out_back = move_ease_out:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() ) + local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) + local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back,createSimpleDelayTime()) + + grossini:runAction(cc.RepeatForever:create(seq1)) + tamara:runAction(cc.RepeatForever:create(seq2)) + kathia:runAction(cc.RepeatForever:create(seq3)) + + Helper.titleLabel:setString("EaseSineIn - EaseSineOut") + return layer +end + +----------------------------------- +-- SpriteEaseSineInOut +----------------------------------- +local function SpriteEaseSineInOut() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease = cc.EaseSineInOut:create(createSimpleMoveBy()) + local move_ease_back = move_ease:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) + local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime()) + + positionForTwo() + + grossini:runAction(cc.RepeatForever:create(seq1)) + tamara:runAction(cc.RepeatForever:create(seq2)) + + Helper.titleLabel:setString("EaseSineInOut action") + return layer +end + +----------------------------------- +-- SpriteEaseElastic +----------------------------------- +local function SpriteEaseElastic() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease_in = cc.EaseElasticIn:create(createSimpleMoveBy()) + local move_ease_in_back = move_ease_in:reverse() + + local move_ease_out = cc.EaseElasticOut:create(createSimpleMoveBy()) + local move_ease_out_back = move_ease_out:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() ) + local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) + local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime()) + + grossini:runAction(cc.RepeatForever:create(seq1)) + tamara:runAction(cc.RepeatForever:create(seq2)) + kathia:runAction(cc.RepeatForever:create(seq3)) + + Helper.titleLabel:setString("Elastic In - Out actions") + return layer +end + +----------------------------------- +-- SpriteEaseElasticInOut +----------------------------------- +local function SpriteEaseElasticInOut() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + + local move_ease_inout1 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.3) + local move_ease_inout_back1 = move_ease_inout1:reverse() + + local move_ease_inout2 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.45) + local move_ease_inout_back2 = move_ease_inout2:reverse() + + local move_ease_inout3 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.6) + local move_ease_inout_back3 = move_ease_inout3:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move_ease_inout1, delay, move_ease_inout_back1, createSimpleDelayTime()) + local seq2 = cc.Sequence:create(move_ease_inout2, createSimpleDelayTime(), move_ease_inout_back2, createSimpleDelayTime()) + local seq3 = cc.Sequence:create(move_ease_inout3, createSimpleDelayTime(), move_ease_inout_back3, createSimpleDelayTime()) + + tamara:runAction(cc.RepeatForever:create(seq1)) + kathia:runAction(cc.RepeatForever:create(seq2)) + grossini:runAction(cc.RepeatForever:create(seq3)) + + Helper.titleLabel:setString("EaseElasticInOut action") + return layer +end + +----------------------------------- +-- SpriteEaseBounce +----------------------------------- +local function SpriteEaseBounce() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease_in = cc.EaseBounceIn:create(createSimpleMoveBy()) + local move_ease_in_back = move_ease_in:reverse() + + local move_ease_out = cc.EaseBounceOut:create(createSimpleMoveBy()) + local move_ease_out_back = move_ease_out:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() ) + local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) + local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime()) + + grossini:runAction(cc.RepeatForever:create(seq1)) + tamara:runAction(cc.RepeatForever:create(seq2)) + kathia:runAction(cc.RepeatForever:create(seq3)) + + Helper.titleLabel:setString("Bounce In - Out actions") + return layer +end + +----------------------------------- +-- SpriteEaseBounceInOut +----------------------------------- +local function SpriteEaseBounceInOut() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease = cc.EaseBounceInOut:create(createSimpleMoveBy()) + local move_ease_back = move_ease:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) + local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime()) + + positionForTwo() + + grossini:runAction(cc.RepeatForever:create(seq1)) + tamara:runAction(cc.RepeatForever:create(seq2)) + + Helper.titleLabel:setString("EaseBounceInOut action") + return layer +end + +----------------------------------- +-- SpriteEaseBack +----------------------------------- +local function SpriteEaseBack() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease_in = cc.EaseBackIn:create(createSimpleMoveBy()) + local move_ease_in_back = move_ease_in:reverse() + + local move_ease_out = cc.EaseBackOut:create(createSimpleMoveBy()) + local move_ease_out_back = move_ease_out:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) + local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime()) + local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime()) + + grossini:runAction(cc.RepeatForever:create(seq1)) + tamara:runAction(cc.RepeatForever:create(seq2)) + kathia:runAction(cc.RepeatForever:create(seq3)) + + Helper.titleLabel:setString("Back In - Out actions") + return layer +end + +----------------------------------- +-- SpriteEaseBackInOut +----------------------------------- +local function SpriteEaseBackInOut() + local layer = getBaseLayer() + + local move = createSimpleMoveBy() + local move_back = move:reverse() + + local move_ease = cc.EaseBackInOut:create(createSimpleMoveBy()) + local move_ease_back = move_ease:reverse() + + local delay = createSimpleDelayTime() + local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime()) + local seq2 = cc.Sequence:create(move_ease,createSimpleDelayTime(), move_ease_back, createSimpleDelayTime()) + + positionForTwo() + + grossini:runAction(cc.RepeatForever:create(seq1)) + tamara:runAction(cc.RepeatForever:create(seq2)) + + Helper.titleLabel:setString("EaseBackInOut action") + return layer +end + +----------------------------------- +-- SpeedTest +----------------------------------- +local SpeedTest_entry = nil +local SpeedTest_action1 = nil +local SpeedTest_action2 = nil +local SpeedTest_action3 = nil + +local function altertime(dt) + SpeedTest_action1:setSpeed(math.random() * 2) + SpeedTest_action2:setSpeed(math.random() * 2) + SpeedTest_action3:setSpeed(math.random() * 2) +end + +local function SpeedTest_onEnterOrExit(tag) + if tag == "enter" then + SpeedTest_entry = scheduler:scheduleScriptFunc(altertime, 1.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(SpeedTest_entry) + end +end + +local function SpeedTest() + local layer = getBaseLayer() + + local jump1 = cc.JumpBy:create(4, cc.p(- s.width + 80, 0), 100, 4) + local jump2 = jump1:reverse() + local rot1 = cc.RotateBy:create(4, 360 * 2) + local rot2 = rot1:reverse() + + local seq3_1 = cc.Sequence:create(jump2, jump1) + local seq3_2 = cc.Sequence:create(rot1, rot2) + + local spawn = cc.Spawn:create(seq3_1, seq3_2) + SpeedTest_action1 = cc.Speed:create(cc.RepeatForever:create(spawn), 1.0) + + local spawn2 = tolua.cast(spawn:clone(), "cc.Spawn") + SpeedTest_action2 = cc.Speed:create(cc.RepeatForever:create(spawn2), 1.0) + + local spawn3 = tolua.cast(spawn:clone(), "cc.Spawn") + SpeedTest_action3 = cc.Speed:create(cc.RepeatForever:create(spawn3), 1.0) + + grossini:runAction(SpeedTest_action2) + tamara:runAction(SpeedTest_action3) + kathia:runAction(SpeedTest_action1) + + layer:registerScriptHandler(SpeedTest_onEnterOrExit) + + Helper.titleLabel:setString("Speed action") + return layer +end + +function EaseActionsTest() + local scene = cc.Scene:create() + cclog("EaseActionsTest") + + Helper.createFunctionTable = { + SpriteEase, + SpriteEaseInOut, + SpriteEaseExponential, + SpriteEaseExponentialInOut, + SpriteEaseSine, + SpriteEaseSineInOut, + SpriteEaseElastic, + SpriteEaseElasticInOut, + SpriteEaseBounce, + SpriteEaseBounceInOut, + SpriteEaseBack, + SpriteEaseBackInOut, + SpeedTest + } + + scene:addChild(SpriteEase()) + scene:addChild(CreateBackMenuItem()) + + return scene +end diff --git a/samples/lua-tests/src/ActionsProgressTest/ActionsProgressTest.lua b/samples/lua-tests/src/ActionsProgressTest/ActionsProgressTest.lua new file mode 100644 index 0000000000..8d53dcd301 --- /dev/null +++ b/samples/lua-tests/src/ActionsProgressTest/ActionsProgressTest.lua @@ -0,0 +1,298 @@ + +local s = cc.Director:getInstance():getWinSize() + +------------------------------------ +-- SpriteProgressToRadial +------------------------------------ +local function SpriteProgressToRadial() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local to1 = cc.ProgressTo:create(2, 100) + local to2 = cc.ProgressTo:create(2, 100) + + local left = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister1)) + left:setType(cc.PROGRESS_TIMER_TYPE_RADIAL) + left:setPosition(cc.p(100, s.height / 2)) + left:runAction(cc.RepeatForever:create(to1)) + layer:addChild(left) + + local right = cc.ProgressTimer:create(cc.Sprite:create(s_pPathBlock)) + right:setType(cc.PROGRESS_TIMER_TYPE_RADIAL) + -- Makes the ridial CCW + right:setReverseDirection(true) + right:setPosition(cc.p(s.width - 100, s.height / 2)) + right:runAction(cc.RepeatForever:create(to2)) + layer:addChild(right) + + Helper.subtitleLabel:setString("ProgressTo Radial") + return layer +end + +------------------------------------ +-- SpriteProgressToHorizontal +------------------------------------ +local function SpriteProgressToHorizontal() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local to1 = cc.ProgressTo:create(2, 100) + local to2 = cc.ProgressTo:create(2, 100) + + local left = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister1)) + left:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the left since the midpoint is 0 for the x + left:setMidpoint(cc.p(0, 0)) + -- Setup for a horizontal bar since the bar change rate is 0 for y meaning no vertical change + left:setBarChangeRate(cc.p(1, 0)) + left:setPosition(cc.p(100, s.height / 2)) + left:runAction(cc.RepeatForever:create(to1)) + layer:addChild(left) + + local right = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister2)) + right:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the left since the midpoint is 1 for the x + right:setMidpoint(cc.p(1, 0)) + -- Setup for a horizontal bar since the bar change rate is 0 for y meaning no vertical change + right:setBarChangeRate(cc.p(1, 0)) + right:setPosition(cc.p(s.width - 100, s.height / 2)) + right:runAction(cc.RepeatForever:create(to2)) + layer:addChild(right) + + Helper.subtitleLabel:setString("ProgressTo Horizontal") + return layer +end + +------------------------------------ +-- SpriteProgressToVertical +------------------------------------ +local function SpriteProgressToVertical() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local to1 = cc.ProgressTo:create(2, 100) + local to2 = cc.ProgressTo:create(2, 100) + + local left = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister1)) + left:setType(cc.PROGRESS_TIMER_TYPE_BAR) + + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + left:setMidpoint(cc.p(0,0)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + left:setBarChangeRate(cc.p(0, 1)) + left:setPosition(cc.p(100, s.height / 2)) + left:runAction(cc.RepeatForever:create(to1)) + layer:addChild(left) + + local right = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister2)) + right:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + right:setMidpoint(cc.p(0, 1)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + right:setBarChangeRate(cc.p(0, 1)) + right:setPosition(cc.p(s.width - 100, s.height / 2)) + right:runAction(cc.RepeatForever:create(to2)) + layer:addChild(right) + + Helper.subtitleLabel:setString("ProgressTo Vertical") + return layer +end + +------------------------------------ +-- SpriteProgressToRadialMidpointChanged +------------------------------------ +local function SpriteProgressToRadialMidpointChanged() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local action = cc.ProgressTo:create(2, 100) + + -- Our image on the left should be a radial progress indicator, clockwise + local left = cc.ProgressTimer:create(cc.Sprite:create(s_pPathBlock)) + left:setType(cc.PROGRESS_TIMER_TYPE_RADIAL) + left:setMidpoint(cc.p(0.25, 0.75)) + left:setPosition(cc.p(100, s.height / 2)) + left:runAction(cc.RepeatForever:create(cc.ProgressTo:create(2, 100))) + layer:addChild(left) + + -- Our image on the left should be a radial progress indicator, counter clockwise + local right = cc.ProgressTimer:create(cc.Sprite:create(s_pPathBlock)) + right:setType(cc.PROGRESS_TIMER_TYPE_RADIAL) + right:setMidpoint(cc.p(0.75, 0.25)) + + --[[ + Note the reverse property (default=NO) is only added to the right image. That's how + we get a counter clockwise progress. + ]] + right:setPosition(cc.p(s.width - 100, s.height / 2)) + right:runAction(cc.RepeatForever:create(cc.ProgressTo:create(2, 100))) + layer:addChild(right) + + Helper.subtitleLabel:setString("Radial w/ Different Midpoints") + return layer +end + +------------------------------------ +-- SpriteProgressBarVarious +------------------------------------ +local function SpriteProgressBarVarious() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local to = cc.ProgressTo:create(2, 100) + + local left = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister1)) + left:setType(cc.PROGRESS_TIMER_TYPE_BAR) + + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + left:setMidpoint(cc.p(0.5, 0.5)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + left:setBarChangeRate(cc.p(1, 0)) + left:setPosition(cc.p(100, s.height / 2)) + left:runAction(cc.RepeatForever:create(cc.ProgressTo:create(2, 100))) + layer:addChild(left) + + local middle = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister2)) + middle:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + middle:setMidpoint(cc.p(0.5, 0.5)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + middle:setBarChangeRate(cc.p(1, 1)) + middle:setPosition(cc.p(s.width/2, s.height/2)) + middle:runAction(cc.RepeatForever:create(cc.ProgressTo:create(2, 100))) + layer:addChild(middle) + + local right = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister2)) + right:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + right:setMidpoint(cc.p(0.5, 0.5)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + right:setBarChangeRate(cc.p(0, 1)) + right:setPosition(cc.p(s.width-100, s.height/2)) + right:runAction(cc.RepeatForever:create(cc.ProgressTo:create(2, 100))) + layer:addChild(right) + + Helper.subtitleLabel:setString("ProgressTo Bar Mid") + return layer +end + +------------------------------------ +-- SpriteProgressBarTintAndFade +------------------------------------ +local function SpriteProgressBarTintAndFade() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local to = cc.ProgressTo:create(6, 100) + local tint = cc.Sequence:create(cc.TintTo:create(1, 255, 0, 0), cc.TintTo:create(1, 0, 255, 0), cc.TintTo:create(1, 0, 0, 255)) + local fade = cc.Sequence:create(cc.FadeTo:create(1.0, 0),cc.FadeTo:create(1.0, 255)) + local left = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister1)) + left:setType(cc.PROGRESS_TIMER_TYPE_BAR) + + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + left:setMidpoint(cc.p(0.5, 0.5)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + left:setBarChangeRate(cc.p(1, 0)) + left:setPosition(cc.p(100, s.height / 2)) + left:runAction(cc.RepeatForever:create(cc.ProgressTo:create(6, 100))) + left:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.TintTo:create(1, 255, 0, 0), cc.TintTo:create(1, 0, 255, 0), cc.TintTo:create(1, 0, 0, 255)))) + layer:addChild(left) + + left:addChild(cc.LabelTTF:create("Tint", "Marker Felt", 20.0)) + + local middle = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister2)) + middle:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + middle:setMidpoint(cc.p(0.5, 0.5)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + middle:setBarChangeRate(cc.p(1, 1)) + middle:setPosition(cc.p(s.width / 2, s.height / 2)) + middle:runAction(cc.RepeatForever:create(cc.ProgressTo:create(6, 100))) + + local fade2 = cc.Sequence:create(cc.FadeTo:create(1.0, 0), cc.FadeTo:create(1.0, 255)) + middle:runAction(cc.RepeatForever:create(fade2)) + layer:addChild(middle) + + middle:addChild(cc.LabelTTF:create("Fade", "Marker Felt", 20.0)) + + local right = cc.ProgressTimer:create(cc.Sprite:create(s_pPathSister2)) + right:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + right:setMidpoint(cc.p(0.5, 0.5)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + right:setBarChangeRate(cc.p(0, 1)) + right:setPosition(cc.p(s.width - 100, s.height / 2)) + right:runAction(cc.RepeatForever:create(cc.ProgressTo:create(6, 100))) + right:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.TintTo:create(1, 255, 0, 0), cc.TintTo:create(1, 0, 255, 0), cc.TintTo:create(1, 0, 0, 255)))) + right:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.FadeTo:create(1.0, 0), cc.FadeTo:create(1.0, 255)))) + layer:addChild(right) + + right:addChild(cc.LabelTTF:create("Tint and Fade", "Marker Felt", 20.0)) + + Helper.subtitleLabel:setString("ProgressTo Bar Mid") + return layer +end + +------------------------------------ +-- SpriteProgressWithSpriteFrame +------------------------------------ +local function SpriteProgressWithSpriteFrame() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local to = cc.ProgressTo:create(6, 100) + + cc.SpriteFrameCache:getInstance():addSpriteFrames("zwoptex/grossini.plist") + + local left = cc.ProgressTimer:create(cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")) + left:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + left:setMidpoint(cc.p(0.5, 0.5)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + left:setBarChangeRate(cc.p(1, 0)) + left:setPosition(cc.p(100, s.height / 2)) + left:runAction(cc.RepeatForever:create(cc.ProgressTo:create(6, 100))) + layer:addChild(left) + + local middle = cc.ProgressTimer:create(cc.Sprite:createWithSpriteFrameName("grossini_dance_02.png")) + middle:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + middle:setMidpoint(cc.p(0.5, 0.5)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + middle:setBarChangeRate(cc.p(1, 1)) + middle:setPosition(cc.p(s.width / 2, s.height / 2)) + middle:runAction(cc.RepeatForever:create(cc.ProgressTo:create(6, 100))) + layer:addChild(middle) + + local right = cc.ProgressTimer:create(cc.Sprite:createWithSpriteFrameName("grossini_dance_03.png")) + right:setType(cc.PROGRESS_TIMER_TYPE_BAR) + -- Setup for a bar starting from the bottom since the midpoint is 0 for the y + right:setMidpoint(cc.p(0.5, 0.5)) + -- Setup for a vertical bar since the bar change rate is 0 for x meaning no horizontal change + right:setBarChangeRate(cc.p(0, 1)) + right:setPosition(cc.p(s.width - 100, s.height / 2)) + right:runAction(cc.RepeatForever:create(cc.ProgressTo:create(6, 100))) + layer:addChild(right) + + Helper.subtitleLabel:setString("Progress With Sprite Frame") + return layer +end + +function ProgressActionsTest() + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + SpriteProgressToRadial, + SpriteProgressToHorizontal, + SpriteProgressToVertical, + SpriteProgressToRadialMidpointChanged, + SpriteProgressBarVarious, + SpriteProgressBarTintAndFade, + SpriteProgressWithSpriteFrame + } + + scene:addChild(SpriteProgressToRadial()) + scene:addChild(CreateBackMenuItem()) + + cc.Director:getInstance():replaceScene(scene) +end diff --git a/samples/lua-tests/src/ActionsTest/ActionsTest.lua b/samples/lua-tests/src/ActionsTest/ActionsTest.lua new file mode 100644 index 0000000000..9b70078388 --- /dev/null +++ b/samples/lua-tests/src/ActionsTest/ActionsTest.lua @@ -0,0 +1,1281 @@ +local size = cc.Director:getInstance():getWinSize() + +local function initWithLayer(layer) + grossini = cc.Sprite:create(s_pPathGrossini) + tamara = cc.Sprite:create(s_pPathSister1) + kathia = cc.Sprite:create(s_pPathSister2) + + layer:addChild(grossini, 1) + layer:addChild(tamara, 2) + layer:addChild(kathia, 3) + + grossini:setPosition(cc.p(size.width / 2, size.height / 3)) + tamara:setPosition(cc.p(size.width / 2, 2 * size.height / 3)) + kathia:setPosition(cc.p(size.width / 2, size.height / 2)) + + Helper.initWithLayer(layer) +end + + +local function centerSprites(numberOfSprites) + if numberOfSprites == 0 then + tamara:setVisible(false) + kathia:setVisible(false) + grossini:setVisible(false) + elseif numberOfSprites == 1 then + tamara:setVisible(false) + kathia:setVisible(false) + grossini:setPosition(cc.p(size.width / 2, size.height / 2)) + elseif numberOfSprites == 2 then + kathia:setPosition(cc.p(size.width / 3, size.height / 2)) + tamara:setPosition(cc.p(2 * size.width / 3, size.height / 2)) + grossini:setVisible(false) + elseif numberOfSprites == 3 then + grossini:setPosition(cc.p(size.width / 2, size.height / 2)) + tamara:setPosition(cc.p(size.width / 4, size.height / 2)) + kathia:setPosition(cc.p(3 * size.width / 4, size.height / 2)) + end +end + +local function alignSpritesLeft(numberOfSprites) + if numberOfSprites == 1 then + tamara:setVisible(false) + kathia:setVisible(false) + grossini:setPosition(cc.p(60, size.height / 2)) + elseif numberOfSprites == 2 then + kathia:setPosition(cc.p(60, size.height / 3)) + tamara:setPosition(cc.p(60, 2 * size.height / 3)) + grossini:setVisible(false) + elseif numberOfSprites == 3 then + grossini:setPosition(cc.p(60, size.height / 2)) + tamara:setPosition(cc.p(60, 2 * size.height / 3)) + kathia:setPosition(cc.p(60, size.height / 3)) + end +end + + +-------------------------------------- +-- ActionManual +-------------------------------------- +local function ActionManual() + local layer = cc.Layer:create() + initWithLayer(layer) + + tamara:setScaleX(2.5) + tamara:setScaleY(-1.0) + tamara:setPosition(cc.p(100, 70)) + tamara:setOpacity(128) + + grossini:setRotation(120) + grossini:setPosition(cc.p(size.width / 2, size.height / 2)) + grossini:setColor(cc.c3b(255, 0, 0)) + + kathia:setPosition(cc.p(size.width - 100, size.height / 2)) + kathia:setColor(cc.c3b(0, 0, 255)) + + Helper.subtitleLabel:setString("Manual Transformation") + return layer +end + +-------------------------------------- +-- ActionMove +-------------------------------------- +local function ActionMove() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(3) + local actionBy = cc.MoveBy:create(2, cc.p(80, 80)) + local actionByBack = actionBy:reverse() + + tamara:runAction(cc.MoveTo:create(2, cc.p(size.width - 40, size.height - 40))) + grossini:runAction(cc.Sequence:create(actionBy, actionByBack)) + kathia:runAction(cc.MoveTo:create(1, cc.p(40, 40))) + + Helper.subtitleLabel:setString("MoveTo / MoveBy") + return layer +end + +-------------------------------------- +-- ActionScale +-------------------------------------- +local function ActionScale() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(3) + + local actionTo = cc.ScaleTo:create(2.0, 0.5) + local actionBy = cc.ScaleBy:create(2.0, 1.0, 10.0) + local actionBy2 = cc.ScaleBy:create(2.0, 5.0, 1.0) + + grossini:runAction(actionTo) + tamara:runAction(cc.Sequence:create(actionBy, actionBy:reverse())) + kathia:runAction(cc.Sequence:create(actionBy2, actionBy2:reverse())) + + Helper.subtitleLabel:setString("ScaleTo / ScaleBy") + return layer +end + +-------------------------------------- +-- ActionRotate +-------------------------------------- +local function ActionRotate() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(3) + + local actionTo = cc.RotateTo:create( 2, 45) + local actionTo2 = cc.RotateTo:create( 2, -45) + local actionTo0 = cc.RotateTo:create(2 , 0) + tamara:runAction(cc.Sequence:create(actionTo, actionTo0)) + + local actionBy = cc.RotateBy:create(2 , 360) + local actionByBack = actionBy:reverse() + grossini:runAction(cc.Sequence:create(actionBy, actionByBack)) + + local action0Retain = cc.RotateTo:create(2 , 0) + + kathia:runAction(cc.Sequence:create(actionTo2, action0Retain)) + + Helper.subtitleLabel:setString("RotateTo / RotateBy") + return layer +end + +-------------------------------------- +-- ActionSkew +-------------------------------------- +local function ActionSkew() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(3) + + local actionTo = cc.SkewTo:create(2, 37.2, -37.2) + local actionToBack = cc.SkewTo:create(2, 0, 0) + local actionBy = cc.SkewBy:create(2, 0.0, -90.0) + local actionBy2 = cc.SkewBy:create(2, 45.0, 45.0) + local actionByBack = actionBy:reverse() + + tamara:runAction(cc.Sequence:create(actionTo, actionToBack)) + grossini:runAction(cc.Sequence:create(actionBy, actionByBack)) + kathia:runAction(cc.Sequence:create(actionBy2, actionBy2:reverse())) + + Helper.subtitleLabel:setString("SkewTo / SkewBy") + return layer +end + +--ActionRotationalSkewVSStandardSkew +local function ActionRotationalSkewVSStandardSkew() + + local layer = cc.Layer:create() + initWithLayer(layer) + + tamara:removeFromParent(true); + grossini:removeFromParent(true); + kathia:removeFromParent(true); + + local s = cc.Director:getInstance():getWinSize(); + local boxSize = cc.size(100.0, 100.0); + local box = cc.LayerColor:create(cc.c4b(255,255,0,255)); + box:setAnchorPoint(cc.p(0.5,0.5)); + box:setContentSize( boxSize ); + box:ignoreAnchorPointForPosition(false); + box:setPosition(cc.p(s.width/2, s.height - 100 - box:getContentSize().height/2)); + layer:addChild(box); + local label = cc.LabelTTF:create("Standard cocos2d Skew", "Marker Felt", 16); + label:setPosition(cc.p(s.width/2, s.height - 100 + label:getContentSize().height)); + layer:addChild(label); + local actionTo = cc.SkewBy:create(2, 360, 0); + local actionToBack = cc.SkewBy:create(2, -360, 0); + local seq = cc.Sequence:create(actionTo, actionToBack) + + box:runAction(seq); + + box = cc.LayerColor:create(cc.c4b(255,255,0,255)); + box:setAnchorPoint(cc.p(0.5,0.5)); + box:setContentSize(boxSize); + box:ignoreAnchorPointForPosition(false); + box:setPosition(cc.p(s.width/2, s.height - 250 - box:getContentSize().height/2)); + layer:addChild(box); + label = cc.LabelTTF:create("Rotational Skew", "Marker Felt", 16); + label:setPosition(cc.p(s.width/2, s.height - 250 + label:getContentSize().height/2)); + layer:addChild(label); + local actionTo2 = cc.RotateBy:create(2, 360); + local actionToBack2 = cc.RotateBy:create(2, -360); + seq = cc.Sequence:create(actionTo2, actionToBack2) + box:runAction(seq); + + Helper.subtitleLabel:setString("Skew Comparison") + return layer; +end + +-------------------------------------- +-- ActionSkewRotate +-------------------------------------- +local function ActionSkewRotate() + local layer = cc.Layer:create() + initWithLayer(layer) + + tamara:removeFromParent(true) + grossini:removeFromParent(true) + kathia:removeFromParent(true) + + local boxSize = cc.size(100.0, 100.0) + + local box = cc.LayerColor:create(cc.c4b(255, 255, 0, 255)) + box:setAnchorPoint(cc.p(0, 0)) + box:setPosition(190, 110) + box:setContentSize(boxSize) + + local markrside = 10.0 + local uL = cc.LayerColor:create(cc.c4b(255, 0, 0, 255)) + box:addChild(uL) + uL:setContentSize(cc.size(markrside, markrside)) + uL:setPosition(0, boxSize.height - markrside) + uL:setAnchorPoint(cc.p(0, 0)) + + local uR = cc.LayerColor:create(cc.c4b(0, 0, 255, 255)) + box:addChild(uR) + uR:setContentSize(cc.size(markrside, markrside)) + uR:setPosition(boxSize.width - markrside, boxSize.height - markrside) + uR:setAnchorPoint(cc.p(0, 0)) + layer:addChild(box) + + local actionTo = cc.SkewTo:create(2, 0, 2) + local rotateTo = cc.RotateTo:create(2, 61.0) + local actionScaleTo = cc.ScaleTo:create(2, -0.44, 0.47) + + local actionScaleToBack = cc.ScaleTo:create(2, 1.0, 1.0) + local rotateToBack = cc.RotateTo:create(2, 0) + local actionToBack = cc.SkewTo:create(2, 0, 0) + + box:runAction(cc.Sequence:create(actionTo, actionToBack)) + box:runAction(cc.Sequence:create(rotateTo, rotateToBack)) + box:runAction(cc.Sequence:create(actionScaleTo, actionScaleToBack)) + + Helper.subtitleLabel:setString("Skew + Rotate + Scale") + return layer +end + +-------------------------------------- +-- ActionJump +-------------------------------------- +local function ActionJump() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(3) + + local actionTo = cc.JumpTo:create(2, cc.p(300,300), 50, 4) + local actionBy = cc.JumpBy:create(2, cc.p(300,0), 50, 4) + local actionUp = cc.JumpBy:create(2, cc.p(0,0), 80, 4) + local actionByBack = actionBy:reverse() + + tamara:runAction(actionTo) + grossini:runAction(cc.Sequence:create(actionBy, actionByBack)) + kathia:runAction(cc.RepeatForever:create(actionUp)) + + Helper.subtitleLabel:setString("JumpTo / JumpBy") + return layer +end + +-------------------------------------- +-- ActionCardinalSpline +-------------------------------------- +local function ActionCardinalSpline() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(2) + local array = { + cc.p(0, 0), + cc.p(size.width / 2 - 30, 0), + cc.p(size.width / 2 - 30, size.height - 80), + cc.p(0, size.height - 80), + cc.p(0, 0), + } + local action = cc.CardinalSplineBy:create(3, array, 0) + local reverse = action:reverse() + local seq = cc.Sequence:create(action, reverse) + + tamara:setPosition(cc.p(50, 50)) + tamara:runAction(seq) + + local action2 = cc.CardinalSplineBy:create(3, array, 1) + local reverse2 = action2:reverse() + local seq2 = cc.Sequence:create(action2, reverse2) + + kathia:setPosition(cc.p(size.width / 2, 50)) + kathia:runAction(seq2) +--[[ + local function drawCardinalSpline() + kmGLPushMatrix() + kmGLTranslatef(50, 50, 0) + cc.DrawPrimitives.drawCardinalSpline(array, 0, 100) + kmGLPopMatrix() + + kmGLPushMatrix() + kmGLTranslatef(size.width / 2, 50, 0) + cc.DrawPrimitives.drawCardinalSpline(array, 1, 100) + kmGLPopMatrix() + end + + array:retain() + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(size.width, size.height)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + glNode:registerScriptDrawHandler(drawCardinalSpline) + layer:addChild(glNode,-10) + glNode:setPosition( size.width / 2, size.height / 2) +]]-- + Helper.titleLabel:setString("CardinalSplineBy / CardinalSplineAt") + Helper.subtitleLabel:setString("Cardinal Spline paths.\nTesting different tensions for one array") + return layer +end + +-------------------------------------- +-- ActionCatmullRom +-------------------------------------- +local function ActionCatmullRom() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(2) + + tamara:setPosition(cc.p(50, 50)) + local array = { + cc.p(0, 0), + cc.p(80, 80), + cc.p(size.width - 80, 80), + cc.p(size.width - 80, size.height - 80), + cc.p(80, size.height - 80), + cc.p(80, 80), + cc.p(size.width / 2, size.height / 2), + } + + local action = cc.CatmullRomBy:create(3, array) + local reverse = action:reverse() + local seq = cc.Sequence:create(action, reverse) + tamara:runAction(seq) + + local array2 = { + cc.p(size.width / 2, 30), + cc.p(size.width -80, 30), + cc.p(size.width - 80, size.height - 80), + cc.p(size.width / 2, size.height - 80), + cc.p(size.width / 2, 30), + } + + local action2 = cc.CatmullRomTo:create(3, array2) + local reverse2 = action2:reverse() + local seq2 = cc.Sequence:create(action2, reverse2) + kathia:runAction(seq2) +--[[ + local function drawCatmullRom() + kmGLPushMatrix() + kmGLTranslatef(50, 50, 0) + cc.DrawPrimitives.drawCatmullRom(array, 50) + kmGLPopMatrix() + + cc.DrawPrimitives.drawCatmullRom(array2,50) + end + + array:retain() + array2:retain() + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(size.width, size.height)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + glNode:registerScriptDrawHandler(drawCatmullRom) + layer:addChild(glNode,-10) + glNode:setPosition( size.width / 2, size.height / 2) + ]]-- + + Helper.titleLabel:setString("CatmullRomBy / CatmullRomTo") + Helper.subtitleLabel:setString("Catmull Rom spline paths. Testing reverse too") + return layer +end + +-------------------------------------- +-- ActionBezier +-------------------------------------- +local function ActionBezier() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(3) + + -- sprite 1 + --[[ + local bezier = ccBezierConfig() + bezier.controlPoint_1 = cc.p(0, size.height / 2) + bezier.controlPoint_2 = cc.p(300, - size.height / 2) + bezier.endPosition = cc.p(300, 100) + ]]-- + local bezier = { + cc.p(0, size.height / 2), + cc.p(300, - size.height / 2), + cc.p(300, 100), + } + local bezierForward = cc.BezierBy:create(3, bezier) + local bezierBack = bezierForward:reverse() + local rep = cc.RepeatForever:create(cc.Sequence:create(bezierForward, bezierBack)) + + -- sprite 2 + tamara:setPosition(cc.p(80,160)) + --[[ + local bezier2 = ccBezierConfig() + bezier2.controlPoint_1 = cc.p(100, size.height / 2) + bezier2.controlPoint_2 = cc.p(200, - size.height / 2) + bezier2.endPosition = cc.p(240, 160) + ]]-- + local bezier2 ={ + cc.p(100, size.height / 2), + cc.p(200, - size.height / 2), + cc.p(240, 160) + } + + local bezierTo1 = cc.BezierTo:create(2, bezier2) + + -- sprite 3 + kathia:setPosition(cc.p(400,160)) + local bezierTo2 = cc.BezierTo:create(2, bezier2) + + grossini:runAction(rep) + tamara:runAction(bezierTo1) + kathia:runAction(bezierTo2) + + Helper.subtitleLabel:setString("BezierTo / BezierBy") + return layer +end + +-------------------------------------- +-- ActionBlink +-------------------------------------- +local function ActionBlink() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(2) + + local action1 = cc.Blink:create(2, 10) + local action2 = cc.Blink:create(2, 5) + + tamara:runAction(action1) + kathia:runAction(action2) + + Helper.subtitleLabel:setString("Blink") + + return layer +end + +-------------------------------------- +-- ActionFade +-------------------------------------- +local function ActionFade() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(2) + + tamara:setOpacity(0) + local action1 = cc.FadeIn:create(1) + local action1Back = action1:reverse() + + local action2 = cc.FadeOut:create(1) + local action2Back = action2:reverse() + + tamara:runAction(cc.Sequence:create( action1, action1Back)) + kathia:runAction(cc.Sequence:create( action2, action2Back)) + + Helper.subtitleLabel:setString("FadeIn / FadeOut") + + return layer +end + +-------------------------------------- +-- ActionTint +-------------------------------------- +local function ActionTint() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(2) + + local action1 = cc.TintTo:create(2, 255, 0, 255) + local action2 = cc.TintBy:create(2, -127, -255, -127) + local action2Back = action2:reverse() + + tamara:runAction(action1) + kathia:runAction(cc.Sequence:create(action2, action2Back)) + + Helper.subtitleLabel:setString("TintTo / TintBy") + + return layer +end + +-------------------------------------- +-- ActionAnimate +-------------------------------------- +local function ActionAnimate() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(3) + + local animation = cc.Animation:create() + local number, name + for i = 1, 14 do + if i < 10 then + number = "0"..i + else + number = i + end + name = "Images/grossini_dance_"..number..".png" + animation:addSpriteFrameWithFile(name) + end + -- should last 2.8 seconds. And there are 14 frames. + animation:setDelayPerUnit(2.8 / 14.0) + animation:setRestoreOriginalFrame(true) + + local action = cc.Animate:create(animation) + grossini:runAction(cc.Sequence:create(action, action:reverse())) + + local cache = cc.AnimationCache:getInstance() + cache:addAnimations("animations/animations-2.plist") + local animation2 = cache:getAnimation("dance_1") + + local action2 = cc.Animate:create(animation2) + tamara:runAction(cc.Sequence:create(action2, action2:reverse())) + + local animation3 = animation2:clone() + -- problem + tolua.cast(animation3,"cc.Animation"):setLoops(4) + + local action3 = cc.Animate:create(animation3) + kathia:runAction(action3) + + Helper.titleLabel:setString("Animation") + Helper.subtitleLabel:setString("Center: Manual animation. Border: using file format animation") + + return layer +end + +-------------------------------------- +-- ActionSequence +-------------------------------------- +local function ActionSequence() + local layer = cc.Layer:create() + initWithLayer(layer) + + alignSpritesLeft(1) + + local action = cc.Sequence:create( + cc.MoveBy:create(2, cc.p(240,0)), + cc.RotateBy:create(2, 540)) + + grossini:runAction(action) + + Helper.subtitleLabel:setString("Sequence: Move + Rotate") + + return layer +end + +-------------------------------------- +-- ActionSequence2 +-------------------------------------- +local actionSequenceLayer = nil + +local function ActionSequenceCallback1() + local label = cc.LabelTTF:create("callback 1 called", "Marker Felt", 16) + label:setPosition(size.width / 4, size.height / 2) + + actionSequenceLayer:addChild(label) +end + +local function ActionSequenceCallback2(sender) + local label = cc.LabelTTF:create("callback 2 called", "Marker Felt", 16) + label:setPosition(cc.p(size.width / 4 * 2, size.height / 2)) + + actionSequenceLayer:addChild(label) +end + +local function ActionSequenceCallback3(sender) + local label = cc.LabelTTF:create("callback 3 called", "Marker Felt", 16) + label:setPosition(cc.p(size.width / 4 * 3, size.height / 2)) + + actionSequenceLayer:addChild(label) +end + +local function ActionSequence2() + actionSequenceLayer = cc.Layer:create() + initWithLayer(actionSequenceLayer) + + alignSpritesLeft(1) + + grossini:setVisible(false) + local action = cc.Sequence:create(cc.Place:create(cc.p(200,200)),cc.Show:create(),cc.MoveBy:create(1, cc.p(100,0)), cc.CallFunc:create(ActionSequenceCallback1),cc.CallFunc:create(ActionSequenceCallback2),cc.CallFunc:create(ActionSequenceCallback3)) + + grossini:runAction(action) + + Helper.subtitleLabel:setString("Sequence of InstantActions") + return actionSequenceLayer +end + +-------------------------------------- +-- ActionSpawn +-------------------------------------- +local function ActionSpawn() + local layer = cc.Layer:create() + initWithLayer(layer) + + alignSpritesLeft(1) + + local action = cc.Spawn:create( + cc.JumpBy:create(2, cc.p(300,0), 50, 4), + cc.RotateBy:create( 2, 720)) + + grossini:runAction(action) + + Helper.subtitleLabel:setString("Spawn: Jump + Rotate") + + return layer +end + +-------------------------------------- +-- ActionReverse +-------------------------------------- +local function ActionReverse() + local layer = cc.Layer:create() + initWithLayer(layer) + + alignSpritesLeft(1) + + local jump = cc.JumpBy:create(2, cc.p(300,0), 50, 4) + local action = cc.Sequence:create(jump, jump:reverse()) + + grossini:runAction(action) + + Helper.subtitleLabel:setString("Reverse an action") + + return layer +end + +-------------------------------------- +-- ActionDelaytime +-------------------------------------- +local function ActionDelaytime() + local layer = cc.Layer:create() + initWithLayer(layer) + + alignSpritesLeft(1) + + local move = cc.MoveBy:create(1, cc.p(150,0)) + local action = cc.Sequence:create(move, cc.DelayTime:create(2), move) + + grossini:runAction(action) + + Helper.subtitleLabel:setString("DelayTime: m + delay + m") + return layer +end + +-------------------------------------- +-- ActionRepeat +-------------------------------------- +local function ActionRepeat() + local layer = cc.Layer:create() + initWithLayer(layer) + + alignSpritesLeft(2) + + local a1 = cc.MoveBy:create(1, cc.p(150,0)) + local action1 = cc.Repeat:create(cc.Sequence:create(cc.Place:create(cc.p(60,60)), a1), 3) + + local a2 = cc.MoveBy:create(1, cc.p(150,0)) + local action2 = cc.RepeatForever:create(cc.Sequence:create(a2, a1:reverse())) + + kathia:runAction(action1) + tamara:runAction(action2) + + Helper.subtitleLabel:setString("Repeat / RepeatForever actions") + return layer +end + +-------------------------------------- +-- ActionRepeatForever +-------------------------------------- +local function repeatForever(sender) + local repeatAction = cc.RepeatForever:create(cc.RotateBy:create(1.0, 360)) + + sender:runAction(repeatAction) +end + +local function ActionRepeatForever() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(1) + + local action = cc.Sequence:create( + cc.DelayTime:create(1), + cc.CallFunc:create(repeatForever) ) + + grossini:runAction(action) + + Helper.subtitleLabel:setString("CallFuncN + RepeatForever") + return layer +end + +-------------------------------------- +-- ActionRotateToRepeat +-------------------------------------- +local function ActionRotateToRepeat() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(2) + + local act1 = cc.RotateTo:create(1, 90) + local act2 = cc.RotateTo:create(1, 0) + local seq = cc.Sequence:create(act1, act2) + local rep1 = cc.RepeatForever:create(seq) + local rep2 = cc.Repeat:create(tolua.cast(seq:clone(), "cc.Sequence"), 10) + + tamara:runAction(rep1) + kathia:runAction(rep2) + + Helper.subtitleLabel:setString("Repeat/RepeatForever + RotateTo") + + return layer +end + +-------------------------------------- +-- ActionRotateJerk +-------------------------------------- +local function ActionRotateJerk() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(2) + + local seq = cc.Sequence:create( + cc.RotateTo:create(0.5, -20), + cc.RotateTo:create(0.5, 20)) + + local rep1 = cc.Repeat:create(seq, 10) + + local seq2 = cc.Sequence:create( + cc.RotateTo:create(0.5, -20), + cc.RotateTo:create(0.5, 20)) + + local rep2 = cc.RepeatForever:create(seq2) + + tamara:runAction(rep1) + kathia:runAction(rep2) + + Helper.subtitleLabel:setString("RepeatForever / Repeat + Rotate") + return layer +end + +-------------------------------------- +-- ActionCallFunc +-------------------------------------- +local callFuncLayer = nil + +local function CallFucnCallback1() + local label = cc.LabelTTF:create("callback 1 called", "Marker Felt", 16) + label:setPosition(size.width / 4, size.height / 2) + + callFuncLayer:addChild(label) +end + +local function CallFucnCallback2(sender) + local label = cc.LabelTTF:create("callback 2 called", "Marker Felt", 16) + label:setPosition(size.width / 4 * 2, size.height / 2) + + callFuncLayer:addChild(label) +end + +local function CallFucnCallback3(sender) + local label = cc.LabelTTF:create("callback 3 called", "Marker Felt", 16) + label:setPosition(size.width / 4 * 3, size.height / 2) + + callFuncLayer:addChild(label) +end + +local function ActionCallFunc() + callFuncLayer = cc.Layer:create() + initWithLayer(callFuncLayer) + + centerSprites(3) + + local action = cc.Sequence:create( + cc.MoveBy:create(2, cc.p(200,0)), + cc.CallFunc:create(CallFucnCallback1) ) + local action2 = cc.Sequence:create(cc.ScaleBy:create(2, 2),cc.FadeOut:create(2),cc.CallFunc:create(CallFucnCallback2)) + local action3 = cc.Sequence:create(cc.RotateBy:create(3 , 360),cc.FadeOut:create(2),cc.CallFunc:create(CallFucnCallback3)) + + grossini:runAction(action) + tamara:runAction(action2) + kathia:runAction(action3) + + Helper.subtitleLabel:setString("Callbacks: CallFunc and friends") + return callFuncLayer +end + +-------------------------------------- +-- ActionCallFuncND * +-- problem: the current luaEngine doesn't support +-- passing more than one param to lua script +-------------------------------------- +local function ActionCallFuncND() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(1) + + local function doRemoveFromParentAndCleanup(sender,table) + grossini:removeFromParentAndCleanup(table[1]) + end + + local action = cc.Sequence:create( + cc.MoveBy:create(2, cc.p(200,0)), + cc.CallFunc:create(doRemoveFromParentAndCleanup,{true})) + + grossini:runAction(action) + + Helper.titleLabel:setString("CallFuncND + auto remove") + Helper.subtitleLabel:setString("CallFuncND + removeFromParent. Grossini dissapears in 2s") + return layer +end + +-------------------------------------- +-- ActionReverseSequence +-------------------------------------- +local function ActionReverseSequence() + local layer = cc.Layer:create() + initWithLayer(layer) + + alignSpritesLeft(1) + + local move1 = cc.MoveBy:create(1, cc.p(250,0)) + local move2 = cc.MoveBy:create(1, cc.p(0,50)) + local seq = cc.Sequence:create(move1, move2, move1:reverse()) + local action = cc.Sequence:create(seq, seq:reverse()) + + grossini:runAction(action) + + Helper.subtitleLabel:setString("Reverse a sequence") + return layer +end + +-------------------------------------- +-- ActionReverseSequence2 +-------------------------------------- +local function ActionReverseSequence2() + local layer = cc.Layer:create() + initWithLayer(layer) + + alignSpritesLeft(2) + + -- Test: + -- Sequence should work both with IntervalAction and InstantActions + local move1 = cc.MoveBy:create(1, cc.p(250,0)) + local move2 = cc.MoveBy:create(1, cc.p(0,50)) + local tog1 = cc.ToggleVisibility:create() + local tog2 = cc.ToggleVisibility:create() + local seq = cc.Sequence:create(move1, tog1, move2, tog2, move1:reverse()) + local action = cc.Repeat:create(cc.Sequence:create(seq, seq:reverse()), 3) + + -- Test: + -- Also test that the reverse of Hide is Show, and vice-versa + kathia:runAction(action) + + local move_tamara = cc.MoveBy:create(1, cc.p(100,0)) + local move_tamara2 = cc.MoveBy:create(1, cc.p(50,0)) + local hide = cc.Hide:create() + + local seq_tamara = cc.Sequence:create(move_tamara, hide, move_tamara2) + local seq_back = seq_tamara:reverse() + tamara:runAction(cc.Sequence:create(seq_tamara, seq_back)) + + Helper.subtitleLabel:setString("Reverse a sequence2") + return layer +end + +-------------------------------------- +-- ActionOrbit +-------------------------------------- +local function ActionOrbit() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(3) + + local orbit1 = cc.OrbitCamera:create(2,1, 0, 0, 180, 0, 0) + local action1 = cc.Sequence:create(orbit1, orbit1:reverse()) + + local orbit2 = cc.OrbitCamera:create(2,1, 0, 0, 180, -45, 0) + local action2 = cc.Sequence:create(orbit2, orbit2:reverse()) + + local orbit3 = cc.OrbitCamera:create(2,1, 0, 0, 180, 90, 0) + local action3 = cc.Sequence:create(orbit3, orbit3:reverse()) + + kathia:runAction(cc.RepeatForever:create(action1)) + tamara:runAction(cc.RepeatForever:create(action2)) + grossini:runAction(cc.RepeatForever:create(action3)) + + local move = cc.MoveBy:create(3, cc.p(100,-100)) + local move_back = move:reverse() + local seq = cc.Sequence:create(move, move_back) + local rfe = cc.RepeatForever:create(seq) + kathia:runAction(rfe) + tamara:runAction(tolua.cast(rfe:clone(), "cc.ActionInterval")) + grossini:runAction(tolua.cast(rfe:clone(), "cc.ActionInterval")) + + + Helper.subtitleLabel:setString("OrbitCamera action") + return layer +end + +-------------------------------------- +-- ActionFollow +-------------------------------------- +local function ActionFollow() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(1) + + grossini:setPosition(cc.p(-200, size.height / 2)) + local move = cc.MoveBy:create(2, cc.p(size.width * 3, 0)) + local move_back = move:reverse() + local seq = cc.Sequence:create(move, move_back) + local rep = cc.RepeatForever:create(seq) + + grossini:runAction(rep) + + layer:runAction(cc.Follow:create(grossini, cc.rect(0, 0, size.width * 2 - 100, size.height))) + + local function draw() + local winSize = cc.Director:getInstance():getWinSize() + local x = winSize.width * 2 - 100 + local y = winSize.height + local vertices = { cc.p(5, 5), cc.p(x - 5, 5), cc.p(x - 5,y - 5), cc.p(5,y - 5) } + cc.DrawPrimitives.drawPoly(vertices, 4, true) + end + + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(size.width, size.height)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + glNode:registerScriptDrawHandler(draw) + layer:addChild(glNode,-10) + glNode:setPosition( size.width / 2, size.height / 2) + + Helper.subtitleLabel:setString("Follow action") + return layer +end + +-------------------------------------- +-- ActionTargeted +-------------------------------------- +local function ActionTargeted() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(2) + + local jump1 = cc.JumpBy:create(2, cc.p(0, 0), 100, 3) + local jump2 = cc.JumpBy:create(2, cc.p(0, 0), 100, 3) + local rot1 = cc.RotateBy:create(1, 360) + local rot2 = cc.RotateBy:create(1, 360) + + local t1 = cc.TargetedAction:create(kathia, jump2) + local t2 = cc.TargetedAction:create(kathia, rot2) + local seq = cc.Sequence:create(jump1, t1, rot1, t2) + local always = cc.RepeatForever:create(seq) + + tamara:runAction(always) + + Helper.titleLabel:setString("ActionTargeted") + Helper.subtitleLabel:setString("Action that runs on another target. Useful for sequences") + return layer +end + +-------------------------------------- +-- PauseResumeActions * +-- problem: schedule feature is constructing +-------------------------------------- +local pausedTargets = nil +local PauseResumeActions_pauseEntry = nil +local PauseResumeActions_resumeEntry = nil + +local function ActionPause(dt) + cclog("Pausing") + + local scheduler = cc.Director:getInstance():getScheduler() + scheduler:unscheduleScriptEntry(PauseResumeActions_pauseEntry) + + local director = cc.Director:getInstance() + pausedTargets = director:getActionManager():pauseAllRunningActions() +end + +local function ActionResume(dt) + cclog("Resuming") + + local scheduler = cc.Director:getInstance():getScheduler() + scheduler:unscheduleScriptEntry(PauseResumeActions_resumeEntry) + + local director = cc.Director:getInstance() + if pausedTargets ~= nil then + -- problem: will crash here. Try fixing me! + director:getActionManager():resumeTargets(pausedTargets) + end +end + +local function PauseResumeActions_onEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + PauseResumeActions_pauseEntry = scheduler:scheduleScriptFunc(ActionPause, 3, false) + PauseResumeActions_resumeEntry = scheduler:scheduleScriptFunc(ActionResume, 5, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(PauseResumeActions_pauseEntry) + scheduler:unscheduleScriptEntry(PauseResumeActions_resumeEntry) + end +end + +local function PauseResumeActions() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(2) + + tamara:runAction(cc.RepeatForever:create(cc.RotateBy:create(3, 360))) + kathia:runAction(cc.RepeatForever:create(cc.RotateBy:create(3, 360))) + + layer:registerScriptHandler(PauseResumeActions_onEnterOrExit) + + Helper.titleLabel:setString("PauseResumeActions") + Helper.subtitleLabel:setString("All actions pause at 3s and resume at 5s") + return layer +end + +-------------------------------------- +-- ActionIssue1305 +-------------------------------------- +local spriteTmp = nil +local Issue1305_entry = nil +local Issue1305_layer = nil + +local function Issue1305_log(sender) + cclog("This message SHALL ONLY appear when the sprite is added to the scene, NOT BEFORE") +end + +local function addSprite(dt) + local scheduler = cc.Director:getInstance():getScheduler() + scheduler:unscheduleScriptEntry(Issue1305_entry) + + spriteTmp:setPosition(cc.p(250, 250)) + Issue1305_layer:addChild(spriteTmp) +end + +local function Issue1305_onEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + Issue1305_entry = scheduler:scheduleScriptFunc(addSprite, 2, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(Issue1305_entry) + end +end + +local function ActionIssue1305() + Issue1305_layer = cc.Layer:create() + initWithLayer(Issue1305_layer) + + centerSprites(0) + + spriteTmp = cc.Sprite:create("Images/grossini.png") + spriteTmp:runAction(cc.CallFunc:create(Issue1305_log)) + + Issue1305_layer:registerScriptHandler(Issue1305_onEnterOrExit) + + Helper.titleLabel:setString("Issue 1305") + Helper.subtitleLabel:setString("In two seconds you should see a message on the console. NOT BEFORE.") + return Issue1305_layer +end + +-------------------------------------- +-- ActionIssue1305_2 +-------------------------------------- +local function Issue1305_2_log1() + cclog("1st block") +end + +local function Issue1305_2_log2() + cclog("2nd block") +end + +local function Issue1305_2_log3() + cclog("3rd block") +end + +local function Issue1305_2_log4() + cclog("4th block") +end + +local function ActionIssue1305_2() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(0) + + local spr = cc.Sprite:create("Images/grossini.png") + spr:setPosition(cc.p(200,200)) + layer:addChild(spr) + + local act1 = cc.MoveBy:create(2 ,cc.p(0, 100)) + local act2 = cc.CallFunc:create(Issue1305_2_log1) + local act3 = cc.MoveBy:create(2, cc.p(0, -100)) + local act4 = cc.CallFunc:create(Issue1305_2_log2) + local act5 = cc.MoveBy:create(2, cc.p(100, -100)) + local act6 = cc.CallFunc:create(Issue1305_2_log3) + local act7 = cc.MoveBy:create(2, cc.p(-100, 0)) + local act8 = cc.CallFunc:create(Issue1305_2_log4) + local actF = cc.Sequence:create(act1, act2, act3, act4, act5, act6, act7, act8) + + cc.Director:getInstance():getActionManager():addAction(actF ,spr, false) + + Helper.titleLabel:setString("Issue 1305 #2") + Helper.subtitleLabel:setString("See console. You should only see one message for each block") + return layer +end + +-------------------------------------- +-- ActionIssue1288 +-------------------------------------- +local function ActionIssue1288() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(0) + + local spr = cc.Sprite:create("Images/grossini.png") + spr:setPosition(cc.p(100, 100)) + layer:addChild(spr) + + local act1 = cc.MoveBy:create(0.5, cc.p(100, 0)) + local act2 = act1:reverse() + local act3 = cc.Sequence:create(act1, act2) + local act4 = cc.Repeat:create(act3, 2) + + spr:runAction(act4) + + Helper.titleLabel:setString("Issue 1288") + Helper.subtitleLabel:setString("Sprite should end at the position where it started.") + return layer +end + +-------------------------------------- +-- ActionIssue1288_2 +-------------------------------------- +local function ActionIssue1288_2() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(0) + + local spr = cc.Sprite:create("Images/grossini.png") + spr:setPosition(cc.p(100, 100)) + layer:addChild(spr) + + local act1 = cc.MoveBy:create(0.5, cc.p(100, 0)) + spr:runAction(cc.Repeat:create(act1, 1)) + + Helper.titleLabel:setString("Issue 1288 #2") + Helper.subtitleLabel:setString("Sprite should move 100 pixels, and stay there") + return layer +end + +-------------------------------------- +-- ActionIssue1327 +-------------------------------------- +local function logSprRotation(sender) + cclog(""..sender:getRotation()) +end + +local function ActionIssue1327() + local layer = cc.Layer:create() + initWithLayer(layer) + + centerSprites(0) + + local spr = cc.Sprite:create("Images/grossini.png") + spr:setPosition(cc.p(100, 100)) + layer:addChild(spr) + + local act1 = cc.CallFunc:create(logSprRotation) + local act2 = cc.RotateBy:create(0.25, 45) + local act3 = cc.CallFunc:create(logSprRotation) + local act4 = cc.RotateBy:create(0.25, 45) + local act5 = cc.CallFunc:create(logSprRotation) + local act6 = cc.RotateBy:create(0.25, 45) + local act7 = cc.CallFunc:create(logSprRotation) + local act8 = cc.RotateBy:create(0.25, 45) + local act9 = cc.CallFunc:create(logSprRotation) + spr:runAction(cc.Sequence:create(act1, act2, act3, act4, act5, act6, act7,act8, act9)) + + Helper.titleLabel:setString("Issue 1327") + Helper.subtitleLabel:setString("See console: You should see: 0, 45, 90, 135, 180") + return layer +end + +function ActionsTest() + cclog("ActionsTest") + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + ActionManual, + ActionMove, + ActionScale, + ActionRotate, + ActionSkew, + ActionRotationalSkewVSStandardSkew, + ActionSkewRotate, + ActionJump, + ActionCardinalSpline, + ActionCatmullRom, + ActionBezier, + ActionBlink, + ActionFade, + ActionTint, + ActionAnimate, + ActionSequence, + ActionSequence2, + ActionSpawn, + ActionReverse, + ActionDelaytime, + ActionRepeat, + ActionRepeatForever, + ActionRotateToRepeat, + ActionRotateJerk, + ActionCallFunc, + ActionCallFuncND, + ActionReverseSequence, + ActionReverseSequence2, + ActionOrbit, + ActionFollow, + ActionTargeted, + PauseResumeActions, + ActionIssue1305, + ActionIssue1305_2, + ActionIssue1288, + ActionIssue1288_2, + ActionIssue1327 + } + + scene:addChild(ActionManual()) + scene:addChild(CreateBackMenuItem()) + + return scene +end diff --git a/samples/lua-tests/src/AssetsManagerTest/AssetsManagerModule.lua b/samples/lua-tests/src/AssetsManagerTest/AssetsManagerModule.lua new file mode 100644 index 0000000000..ab9b3874a6 --- /dev/null +++ b/samples/lua-tests/src/AssetsManagerTest/AssetsManagerModule.lua @@ -0,0 +1,43 @@ +local AssetManagerModule = {} + +function AssetManagerModule.newScene(backfunc) + + local winSize = cc.Director:getInstance():getWinSize() + + local newScene = cc.Scene:create() + local layer = cc.Layer:create() + + local function backToUpdate() + local scene = backfunc() + if scene ~= nil then + cc.Director:getInstance():replaceScene(scene) + end + end + + --Create BackMneu + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local backMenuItem = cc.MenuItemFont:create("Back") + backMenuItem:setPosition(cc.p(VisibleRect:rightBottom().x - 50, VisibleRect:rightBottom().y + 25)) + backMenuItem:registerScriptTapHandler(backToUpdate) + + local backMenu = cc.Menu:create() + backMenu:setPosition(0, 0) + backMenu:addChild(backMenuItem) + layer:addChild(backMenu,6) + + local helloLabel = cc.LabelTTF:create("Hello World", "Arial", 38) + helloLabel:setPosition(cc.p(winSize.width / 2, winSize.height - 40)) + layer:addChild(helloLabel, 5) + + local sprite = cc.Sprite:create("background.png") + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + sprite:setPosition(cc.p(winSize.width / 2, winSize.height / 2)) + layer:addChild(sprite, 0) + + newScene:addChild(layer) + cc.Director:getInstance():replaceScene(newScene) +end + + +return AssetManagerModule diff --git a/samples/lua-tests/src/AssetsManagerTest/AssetsManagerTest.lua b/samples/lua-tests/src/AssetsManagerTest/AssetsManagerTest.lua new file mode 100644 index 0000000000..83e7c33d3c --- /dev/null +++ b/samples/lua-tests/src/AssetsManagerTest/AssetsManagerTest.lua @@ -0,0 +1,158 @@ +local targetPlatform = cc.Application:getInstance():getTargetPlatform() + +local lineSpace = 40 +local itemTagBasic = 1000 +local menuItemNames = +{ + "enter", + "reset", + "update", +} + +local winSize = cc.Director:getInstance():getWinSize() + +local function updateLayer() + local layer = cc.Layer:create() + + local support = false + if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) + or (cc.PLATFORM_OS_WINDOWS == targetPlatform) or (cc.PLATFORM_OS_ANDROID == targetPlatform) + or (cc.PLATFORM_OS_MAC == targetPlatform) then + support = true + end + + if not support then + print("Platform is not supported!") + return layer + end + + local isUpdateItemClicked = false + local assetsManager = nil + local pathToSave = "" + + local menu = cc.Menu:create() + menu:setPosition(cc.p(0, 0)) + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + + local progressLable = cc.LabelTTF:create("","Arial",30) + progressLable:setPosition(cc.p(140,50)) + layer:addChild(progressLable) + + pathToSave = createDownloadDir() + + local function onError(errorCode) + if errorCode == cc.ASSETSMANAGER_NO_NEW_VERSION then + progressLable:setString("no new version") + elseif errorCode == cc.ASSETSMANAGER_NETWORK then + progressLable:setString("network error") + end + end + + local function onProgress( percent ) + local progress = string.format("downloading %d%%",percent) + progressLable:setString(progress) + end + + local function onSuccess() + progressLable:setString("downloading ok") + end + + local function getAssetsManager() + if nil == assetsManager then + assetsManager = cc.AssetsManager:new("https://raw.github.com/samuele3hu/AssetsManagerTest/master/package.zip", + "https://raw.github.com/samuele3hu/AssetsManagerTest/master/version", + pathToSave) + assetsManager:retain() + assetsManager:setDelegate(onError, cc.ASSETSMANAGER_PROTOCOL_ERROR ) + assetsManager:setDelegate(onProgress, cc.ASSETSMANAGER_PROTOCOL_PROGRESS) + assetsManager:setDelegate(onSuccess, cc.ASSETSMANAGER_PROTOCOL_SUCCESS ) + assetsManager:setConnectionTimeout(3) + end + + return assetsManager + end + + local function update(sender) + progressLable:setString("") + + getAssetsManager():update() + end + + local function reset(sender) + progressLable:setString("") + + deleteDownloadDir(pathToSave) + + getAssetsManager():deleteVersion() + + createDownloadDir() + end + + local function reloadModule( moduleName ) + + package.loaded[moduleName] = nil + + return require(moduleName) + end + + local function enter(sender) + + if not isUpdateItemClicked then + addSearchPath(pathToSave,true) + end + + assetsManagerModule = reloadModule("src/AssetsManagerTest/AssetsManagerModule") + + assetsManagerModule.newScene(AssetsManagerTestMain) + end + + local callbackFuncs = + { + enter, + reset, + update, + } + + local function menuCallback(tag, menuItem) + local scene = nil + local nIdx = menuItem:getLocalZOrder() - itemTagBasic + local ExtensionsTestScene = CreateExtensionsTestScene(nIdx) + if nil ~= ExtensionsTestScene then + cc.Director:getInstance():replaceScene(ExtensionsTestScene) + end + end + + for i = 1, table.getn(menuItemNames) do + local item = cc.MenuItemFont:create(menuItemNames[i]) + item:registerScriptTapHandler(callbackFuncs[i]) + item:setPosition(winSize.width / 2, winSize.height - i * lineSpace) + if not support then + item:setEnabled(false) + end + menu:addChild(item, itemTagBasic + i) + end + + local function onNodeEvent(msgName) + if nil ~= assetsManager then + assetsManager:release() + assetsManager = nil + end + end + + layer:registerScriptHandler(onNodeEvent) + + layer:addChild(menu) + + return layer +end + +------------------------------------- +-- AssetsManager Test +------------------------------------- +function AssetsManagerTestMain() + local scene = cc.Scene:create() + scene:addChild(updateLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/BugsTest/BugsTest.lua b/samples/lua-tests/src/BugsTest/BugsTest.lua new file mode 100644 index 0000000000..822f7be3ad --- /dev/null +++ b/samples/lua-tests/src/BugsTest/BugsTest.lua @@ -0,0 +1,644 @@ +local MAX_COUNT = 9 +local LINE_SPACE = 40 +local kItemTagBasic = 5432 + +local Winsize = cc.Director:getInstance():getWinSize() + +local testNames = { + "Bug-350", + "Bug-422", + "Bug-458", + "Bug-624", + "Bug-886", + "Bug-899", + "Bug-914", + "Bug-1159", + "Bug-1174" +} + +local function CreateBugsTestBackMenuItem(pLayer) + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local pMenuItemFont = cc.MenuItemFont:create("Back") + pMenuItemFont:setPosition(cc.p(VisibleRect:rightBottom().x - 50, VisibleRect:rightBottom().y + 25)) + local function menuCallback() + local pScene = BugsTestMain() + if pScene ~= nil then + cc.Director:getInstance():replaceScene(pScene) + end + end + pMenuItemFont:registerScriptTapHandler(menuCallback) + local pMenu = cc.Menu:create() + pMenu:addChild(pMenuItemFont) + pMenu:setPosition(cc.p(0, 0)) + pLayer:addChild(pMenu) +end + +--BugTest350 +local function BugTest350() + local pLayer = cc.Layer:create() + local pBackground = cc.Sprite:create("Hello.png") + pBackground:setPosition(cc.p(Winsize.width/2, Winsize.height/2)) + pLayer:addChild(pBackground) + return pLayer +end + +--BugTest422 +local nLocalTag = 0 +local function BugTest422() + local function reset(pResetLayer) + nLocalTag = nLocalTag + 1 + local pNode = pResetLayer:getChildByTag(nLocalTag - 1) + if nil ~= pNode then + --userdata per + local strLog = "Menu:"..nLocalTag + print(strLog) + end + + pResetLayer:removeChild(pNode, false) + + local function menuCallback(tag,pMenuItem) + if nil ~= pMenuItem then + local pCallbackLayer = pMenuItem:getParent():getParent() + reset(pCallbackLayer) + end + end + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local pMenuItem1 = cc.MenuItemFont:create("One") + pMenuItem1:registerScriptTapHandler(menuCallback) + local pMenuItem2 = cc.MenuItemFont:create("Two") + pMenuItem2:registerScriptTapHandler(menuCallback) + local pMenu = cc.Menu:create(pMenuItem1, pMenuItem2) + pMenu:alignItemsVertically() + local fX = math.random() * 50 + local fY = math.random() * 50 + local menuPosX ,menuPosY = pMenu:getPosition() + pMenu:setPosition(cc.p(menuPosX + fX,menuPosY + fY)) + pResetLayer:addChild(pMenu,0,nLocalTag) + end + + local pLayer = cc.Layer:create() + reset(pLayer) + return pLayer +end + +--BugTest458 +local nColorFlag = 0 +local function BugTest458() + local pLayer = cc.Layer:create() + + local function InitQuestionContainerSprite(pSprite) + --Add label + local pLabel = cc.LabelTTF:create("Answer 1", "Arial", 12) + pLabel:setTag(100) + + --Add the background + local pCorner = cc.Sprite:create("Images/bugs/corner.png") + local nWidth = Winsize.width * 0.9 - (pCorner:getContentSize().width * 2) + local nHeight = Winsize.height * 0.15 - (pCorner:getContentSize().height * 2) + local pColorLayer = cc.LayerColor:create(cc.c4b(255, 255, 255, 255 * .75), nWidth, nHeight) + pColorLayer:setPosition(cc.p(-nWidth / 2, -nHeight / 2)) + --First button is blue,Second is red,Used for testing - change later + if (0 == nColorFlag) then + pLabel:setColor(cc.c3b(0,0,255)) + else + print("Color changed") + pLabel:setColor(cc.c3b(255,0,0)) + end + nColorFlag = nColorFlag + 1 + pSprite:addChild(pColorLayer) + + pCorner:setPosition(cc.p(-(nWidth / 2 + pCorner:getContentSize().width / 2), -(nHeight / 2 + pCorner:getContentSize().height / 2))) + pSprite:addChild(pCorner) + + local nX,nY = pCorner:getPosition() + local pCorner2 = cc.Sprite:create("Images/bugs/corner.png") + pCorner2:setPosition(cc.p(-nX, nY)) + pCorner2:setFlipX(true) + pSprite:addChild(pCorner2) + + local pCorner3 = cc.Sprite:create("Images/bugs/corner.png") + pCorner3:setPosition(cc.p(nX, -nY)) + pCorner3:setFlipY(true) + pSprite:addChild(pCorner3) + + local pCorner4 = cc.Sprite:create("Images/bugs/corner.png") + pCorner4:setPosition(cc.p(-nX, -nY)) + pCorner4:setFlipX(true) + pCorner4:setFlipY(true) + pSprite:addChild(pCorner4) + + local pEdge = cc.Sprite:create("Images/bugs/edge.png") + pEdge:setScaleX(nWidth) + pEdge:setPosition(cc.p(nX + (pCorner:getContentSize().width / 2) + (nWidth / 2), nY)) + pSprite:addChild(pEdge) + + local pEdge2 = cc.Sprite:create("Images/bugs/edge.png") + pEdge2:setScaleX(nWidth) + pEdge2:setPosition(cc.p(nX + (pCorner:getContentSize().width / 2) + (nWidth / 2), -nY)) + pEdge2:setFlipY(true) + pSprite:addChild(pEdge2) + + local pEdge3 = cc.Sprite:create("Images/bugs/edge.png") + pEdge3:setRotation(90) + pEdge3:setScaleX(nHeight) + pEdge3:setPosition(cc.p(nX, nY + (pCorner:getContentSize().height / 2) + (nHeight / 2))) + pSprite:addChild(pEdge3) + + local pEdge4 = cc.Sprite:create("Images/bugs/edge.png") + pEdge4:setRotation(270) + pEdge4:setScaleX(nHeight) + pEdge4:setPosition(cc.p(-nX, nY + (pCorner:getContentSize().height / 2) + (nHeight / 2))) + pSprite:addChild(pEdge4) + + pSprite:addChild(pLabel) + end + + local pQuestion1 = cc.Sprite:create() + InitQuestionContainerSprite(pQuestion1) + local pQuestion2 = cc.Sprite:create() + InitQuestionContainerSprite(pQuestion2) + + local function menuCallback() + print("Selected") + end + local pMenuItemSprite = cc.MenuItemSprite:create(pQuestion1,pQuestion2) + pMenuItemSprite:registerScriptTapHandler(menuCallback) + local pLayerColor1 = cc.LayerColor:create(cc.c4b(0,0,255,255), 100, 100) +-- question->release() +-- question2->release() + + local pLayerColor2 = cc.LayerColor:create(cc.c4b(255,0,0,255), 100, 100) + local pMenuItemSprite2 = cc.MenuItemSprite:create(pLayerColor1, pLayerColor2) + pMenuItemSprite2:registerScriptTapHandler(menuCallback) + local pMenu = cc.Menu:create(pMenuItemSprite, pMenuItemSprite2) + pMenu:alignItemsVerticallyWithPadding(100) + pMenu:setPosition(cc.p(Winsize.width / 2, Winsize.height / 2)) + + -- add the label as a child to this Layer + pLayer:addChild(pMenu) + + return pLayer +end + +--BugTest624 +local BugTest624_entry = nil +local BugTest624_2_entry = nil + +local function BugTest624() + local pLayer = cc.Layer:create() + + local pLabel = cc.LabelTTF:create("Layer1", "Marker Felt", 36) + pLabel:setPosition(cc.p(Winsize.width / 2, Winsize.height / 2)) + pLayer:addChild(pLabel) + pLayer:setAccelerometerEnabled(true) +-- schedule(schedule_selector(Bug624Layer::switchLayer), 5.0f) + + local function BugTest624_SwitchLayer() + local scheduler = cc.Director:getInstance():getScheduler() + scheduler:unscheduleScriptEntry(BugTest624_entry) + + local pScene = cc.Scene:create() + local pNewPlayer = BugTest624_2() + CreateBugsTestBackMenuItem(pNewPlayer) + pScene:addChild(pNewPlayer) + cc.Director:getInstance():replaceScene(cc.TransitionFade:create(2.0, pScene, cc.c3b(255,255,255))) + end + + + local function BugTest624_OnEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + BugTest624_entry = scheduler:scheduleScriptFunc(BugTest624_SwitchLayer, 5.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(BugTest624_entry) + end + end + pLayer:registerScriptHandler(BugTest624_OnEnterOrExit) + + local function didAccelerate(x,y,z,timestamp) + print("Layer1 accel") + end + pLayer:registerScriptAccelerateHandler(didAccelerate) + + return pLayer +end + +function BugTest624_2() + local pLayer = cc.Layer:create() + + local pLabel = cc.LabelTTF:create("Layer2", "Marker Felt", 36) + pLabel:setPosition(cc.p(Winsize.width / 2, Winsize.height / 2)) + pLayer:addChild(pLabel) + pLayer:setAccelerometerEnabled(true) + + local function BugTest624_2_SwitchLayer() + local scheduler = cc.Director:getInstance():getScheduler() + scheduler:unscheduleScriptEntry(BugTest624_2_entry) + + local pScene = cc.Scene:create() + local pNewPlayer = BugTest624() + CreateBugsTestBackMenuItem(pNewPlayer) + pScene:addChild(pNewPlayer) + cc.Director:getInstance():replaceScene(cc.TransitionFade:create(2.0, pScene, cc.c3b(255,0,0))) + end + + local function BugTest624_2_OnEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + BugTest624_2_entry = scheduler:scheduleScriptFunc(BugTest624_2_SwitchLayer, 5.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(BugTest624_2_entry) + end + end + pLayer:registerScriptHandler(BugTest624_2_OnEnterOrExit) + + local function didAccelerate(x,y,z,timestamp) + print("Layer2 accel") + end + + pLayer:registerScriptAccelerateHandler(didAccelerate) + return pLayer +end + +--BugTest886 +local function BugTest886() + local pLayer = cc.Layer:create() + + local pSprite1 = cc.Sprite:create("Images/bugs/bug886.jpg") + pSprite1:setAnchorPoint(cc.p(0, 0)) + pSprite1:setPosition(cc.p(0, 0)) + pSprite1:setScaleX(0.6) + pLayer:addChild(pSprite1) + + local pSprite2 = cc.Sprite:create("Images/bugs/bug886.jpg") + pSprite2:setAnchorPoint(cc.p(0, 0)) + pSprite2:setScaleX(0.6) + pSprite2:setPosition(cc.p(pSprite1:getContentSize().width * 0.6 + 10, 0)) + pLayer:addChild(pSprite2) + + return pLayer +end + +--BugTest899 +local function BugTest899() + local pLayer = cc.Layer:create() + + local pBg = cc.Sprite:create("Images/bugs/RetinaDisplay.jpg") + pLayer:addChild(pBg,0) + pBg:setAnchorPoint(cc.p(0, 0)) + + return pLayer +end + +--BugTest914 +local function BugTest914() + local layer = cc.Layer:create() + + local layerColor = nil + + for i = 0, 4 do + layerColor = cc.LayerColor:create(cc.c4b(i*20, i*20, i*20,255)) + layerColor:setContentSize(cc.size(i*100, i*100)) + layerColor:setPosition(cc.p(Winsize.width/2, Winsize.height/2)) + layerColor:setAnchorPoint(cc.p(0.5, 0.5)) + layerColor:ignoreAnchorPointForPosition(false) + layer:addChild(layerColor, -1-i) + end + + --create and initialize a Label + local function restart() + local scene = cc.Scene:create() + local newLayer = BugTest914() + CreateBugsTestBackMenuItem(newLayer) + scene:addChild(newLayer) + cc.Director:getInstance():replaceScene(scene) + end + + local label = cc.LabelTTF:create("Hello World", "Marker Felt", 64) + --position the label on the center of the screen + label:setPosition(cc.p( Winsize.width /2 , Winsize.height/2 )) + layer:addChild(label) + + local item1 = cc.MenuItemFont:create("restart") + item1:registerScriptTapHandler(restart) + + local menu = cc.Menu:create() + menu:addChild(item1) + menu:alignItemsVertically() + menu:setPosition(cc.p(Winsize.width/2, 100)) + layer:addChild(menu) + + -- handling touch events + local function onTouchMoved(touches, event) + local count = table.getn(touches) + print("Number of touches: ",count) + end + local function onTouchBegan(touches, event) + onTouchMoved(touches, event) + end + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCHES_BEGAN ) + listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + return layer +end + +--BugTest1159 +local function BugTest1159() + local pLayer = cc.Layer:create() + + cc.Director:getInstance():setDepthTest(true) + + local background = cc.LayerColor:create(cc.c4b(255, 0, 255, 255)) + pLayer:addChild(background) + + local sprite_a = cc.LayerColor:create(cc.c4b(255, 0, 0, 255), 700, 700) + sprite_a:setAnchorPoint(cc.p(0.5, 0.5)) + sprite_a:ignoreAnchorPointForPosition(false) + sprite_a:setPosition(cc.p(0.0, Winsize.height/2)) + pLayer:addChild(sprite_a) + local seq = cc.Sequence:create(cc.MoveTo:create(1.0, cc.p(1024.0, 384.0)), cc.MoveTo:create(1.0, cc.p(0.0, 384.0))) + sprite_a:runAction(cc.RepeatForever:create(seq)) + + local sprite_b = cc.LayerColor:create(cc.c4b(0, 0, 255, 255), 400, 400) + sprite_b:setAnchorPoint(cc.p(0.5, 0.5)) + sprite_b:ignoreAnchorPointForPosition(false) + sprite_b:setPosition(cc.p(Winsize.width/2, Winsize.height/2)) + pLayer:addChild(sprite_b) + + local function menuCallback() + local pScene = cc.Scene:create() + local pLayer = BugTest1159() + CreateBugsTestBackMenuItem(pLayer) + pScene:addChild(pLayer) + cc.Director:getInstance():replaceScene(cc.TransitionPageTurn:create(1.0, pScene, false)) + end + local label = cc.MenuItemLabel:create(cc.LabelTTF:create("Flip Me", "Helvetica", 24)) + label:registerScriptTapHandler(menuCallback) + local menu = cc.Menu:create() + menu:addChild(label) + menu:setPosition(cc.p(Winsize.width - 200.0, 50.0)) + pLayer:addChild(menu) + + local function onNodeEvent(event) + if event == "exit" then + --[[ + if schedulerEntry ~= nil then + scheduler:unscheduleScriptEntry(schedulerEntry) + end + ]]-- + cc.Director:getInstance():setDepthTest(false) + end + end + + pLayer:registerScriptHandler(onNodeEvent) + + return pLayer +end + +--BugTest1174 +local function BugTest1174() + local pLayer = cc.Layer:create() + + local function check_for_error(p1,p2,p3,p4,s,t) + local p4_p3 = cc.pSub(p4,p3) + local p4_p3_t = cc.pMul(p4_p3,t) + local hitp1 = cc.pAdd(p3,p4_p3_t) + + local p2_p1 = cc.pSub(p2,p1) + local p2_p1_s = cc.pMul(p2_p1,s) + local hitp2 = cc.pAdd(p1,p2_p1_s) + + if math.abs(hitp1.x - hitp2.x ) > 0.1 or math.abs(hitp1.y - hitp2.y) > 0.1 then + local strErr = "ERROR: ("..hitp1.x..","..hitp1.y..") != ("..hitp2.x..","..hitp2.y..")" + print(strErr) + return 1 + end + return 0 + end + local A,B,C,D,p1,p2,p3,p4 + local s = 0.0 + local t = 0.0 + local err = 0 + local ok = 0 + local bRet = false + + print("Test1 - Start") + local i = 0 + for i = 0, 9999 do + --[[ + A|b + ----- + c|d + ]]-- + local ax = math.random() * -5000 + local ay = math.random() * 5000 + --[[ + a|b + ----- + c|D + ]]-- + local dx = math.random() * 5000 + local dy = math.random() * -5000 + + --[[ + a|B + ----- + c|d + ]]-- + local bx = math.random() * 5000 + local by = math.random() * 5000 + + --[[ + a|b + ----- + C|d + ]]-- + local cx = math.random() * -5000 + local cy = math.random() * -5000 + + A = cc.p(ax,ay) + B = cc.p(bx,by) + C = cc.p(cx,cy) + D = cc.p(dx,dy) + + bRet,s,t = cc.pIsLineIntersect( A, D, B, C, s, t) + if true == bRet then + if 1 == check_for_error(A,D,B,C,s,t) then + err = err + 1 + else + ok = ok + 1 + end + end + end + local strLog = "Test1 - End. OK="..ok..", Err="..err + print(strLog) + -------- + --Test 2 + -------- + print("Test2 - Start") + + p1 = cc.p(220,480) + p2 = cc.p(304,325) + p3 = cc.p(264,416) + p4 = cc.p(186,416) + s = 0.0 + t = 0.0 + bRet,s,t = cc.pIsLineIntersect( p1, p2, p3, p4, s, t) + if true == bRet then + check_for_error(p1, p2, p3, p4, s, t) + end + print("Test2 - End") + + ------ + --Test 3 + ------ + print("Test3 - Start") + + ok=0 + err=0 + for i = 0 , 9999 do + -- A | b + -- ----- + -- c | d + local ax = math.random() * -500 + local ay = math.random() * 500 + p1 = cc.p(ax,ay) + -- a | b + -- ----- + -- c | D + local dx = math.random() * 500 + local dy = math.random() * -500 + p2 = cc.p(dx,dy) + + ------- + + local y = ay - ((ay - dy) / 2.0) + + -- a | b + -- ----- + -- C | d + local cx = math.random() * -500 + p3 = cc.p(cx,y) + + -- a | B + -- ----- + -- c | d + local bx = math.random() * 500 + p4 = cc.p(bx,y) + + s = 0.0 + t = 0.0 + bRet,s,t = cc.pIsLineIntersect(p1, p2, p3, p4, s, t) + if true == bRet then + if 1 == check_for_error(p1, p2, p3, p4, s,t ) then + err = err + 1 + else + ok = ok + 1 + end + end + end + strLog = "Test3 - End. OK="..ok..", Err="..err + print(strLog) + + return pLayer +end + +local CreateBugsTestTable = { + BugTest350, + BugTest422, + BugTest458, + BugTest624, + BugTest886, + BugTest899, + BugTest914, + BugTest1159, + BugTest1174, +} + +local function CreateBugsTestScene(nBugNo) + local pNewscene = cc.Scene:create() + local pLayer = CreateBugsTestTable[nBugNo]() + CreateBugsTestBackMenuItem(pLayer) + pNewscene:addChild(pLayer) + cc.Director:getInstance():replaceScene(pNewscene) +--pLayer:autorelease() +end + + +local function BugsTestMainLayer() + local ret = cc.Layer:create() + + --menu callback + local function menuCallback(tag, pMenuItem) + local nIdx = pMenuItem:getLocalZOrder() - kItemTagBasic + local BugTestScene = CreateBugsTestScene(nIdx) + if nil ~= testScene then + cc.Director:getInstance():replaceScene(testScene) + end + end + + -- add menu items for tests + local pItemMenu = cc.Menu:create() + local nTestCount = table.getn(testNames) + + local i = 1 + for i = 1, nTestCount do + local label = cc.LabelTTF:create(testNames[i], "Arial", 24) + local pMenuItem = cc.MenuItemLabel:create(label) + pMenuItem:registerScriptTapHandler(menuCallback) + pItemMenu:addChild(pMenuItem, i + kItemTagBasic) + pMenuItem:setPosition( cc.p( VisibleRect:center().x, (VisibleRect:top().y - i * LINE_SPACE) )) + end + + pItemMenu:setPosition(cc.p(0, 0)) + ret:addChild(pItemMenu) + + -- handling touch events + local ptBeginPos = {x = 0, y = 0} + local ptCurPos = {x = 0, y = 0} + + -- handling touch events + local function onTouchMoved(touches, event) + local touchLocation = touches[1]:getLocation() + local nMoveY = touchLocation.y - ptBeginPos.y + local curPosx, curPosy = pItemMenu:getPosition() + local nextPosy = curPosy + nMoveY + if nextPosy < 0 then + pItemMenu:setPosition(0, 0) + return + end + + if nextPosy > ((nTestCount + 1) * LINE_SPACE - Winsize.height) then + pItemMenu:setPosition(0, ((nTestCount + 1) * LINE_SPACE - Winsize.height)) + return + end + + pItemMenu:setPosition(curPosx, nextPosy) + ptBeginPos = touchLocation + ptCurPos = {x = curPosx, y = nextPosy} + end + local function onTouchBegan(touches, event) + ptBeginPos = touches[1]:getLocation() + end + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCHES_BEGAN ) + listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + + local eventDispatcher = ret:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ret) + + return ret +end +function BugsTestMain() + cclog("BugsTestMain") + local scene = cc.Scene:create() + scene:addChild(BugsTestMainLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/ClickAndMoveTest/ClickAndMoveTest.lua b/samples/lua-tests/src/ClickAndMoveTest/ClickAndMoveTest.lua new file mode 100644 index 0000000000..87f49208b7 --- /dev/null +++ b/samples/lua-tests/src/ClickAndMoveTest/ClickAndMoveTest.lua @@ -0,0 +1,69 @@ +local size = cc.Director:getInstance():getWinSize() +local layer = nil +local kTagSprite = 1 + +local function initWithLayer() + local sprite = cc.Sprite:create(s_pPathGrossini) + + local bgLayer = cc.LayerColor:create(cc.c4b(255,255,0,255)) + layer:addChild(bgLayer, -1) + + layer:addChild(sprite, 0, kTagSprite) + sprite:setPosition(cc.p(20,150)) + + sprite:runAction(cc.JumpTo:create(4, cc.p(300,48), 100, 4)) + + bgLayer:runAction(cc.RepeatForever:create(cc.Sequence:create( + cc.FadeIn:create(1), + cc.FadeOut:create(1)))) + + local function onTouchBegan(touch, event) + return true + end + + local function onTouchEnded(touch, event) + + local location = touch:getLocation() + + local s = layer:getChildByTag(kTagSprite) + s:stopAllActions() + s:runAction(cc.MoveTo:create(1, cc.p(location.x, location.y))) + local posX, posY = s:getPosition() + local o = location.x - posX + local a = location.y - posY + local at = math.atan(o / a) / math.pi * 180.0 + + if a < 0 then + if o < 0 then + at = 180 + math.abs(at) + else + at = 180 - math.abs(at) + end + end + s:runAction(cc.RotateTo:create(1, at)) + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + return layer +end + +-------------------------------- +-- Click And Move Test +-------------------------------- +function ClickAndMoveTest() + cclog("ClickAndMoveTest") + local scene = cc.Scene:create() + layer = cc.Layer:create() + + initWithLayer() + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + + + return scene +end diff --git a/samples/lua-tests/src/CocoStudioTest/CocoStudioArmatureTest/CocoStudioArmatureTest.lua b/samples/lua-tests/src/CocoStudioTest/CocoStudioArmatureTest/CocoStudioArmatureTest.lua new file mode 100644 index 0000000000..adca6996a5 --- /dev/null +++ b/samples/lua-tests/src/CocoStudioTest/CocoStudioArmatureTest/CocoStudioArmatureTest.lua @@ -0,0 +1,1260 @@ +local itemTagBasic = 1000 +local armaturePerformanceTag = 20000 +local frameEventActionTag = 10000 +local winSize = cc.Director:getInstance():getWinSize() +local scheduler = cc.Director:getInstance():getScheduler() +local ArmatureTestIndex = +{ + TEST_ASYNCHRONOUS_LOADING = 1, + TEST_DIRECT_LOADING = 2, + TEST_COCOSTUDIO_WITH_SKELETON = 3, + TEST_DRAGON_BONES_2_0 = 4, + TEST_PERFORMANCE = 5, + TEST_CHANGE_ZORDER = 6, + TEST_ANIMATION_EVENT = 7, + TEST_FRAME_EVENT = 8, + TEST_PARTICLE_DISPLAY = 9, + TEST_USE_DIFFERENT_PICTURE = 10, + TEST_ANCHORPOINT = 11, + TEST_ARMATURE_NESTING = 12, + TEST_ARMATURE_NESTING_2 = 13, +} +local armatureSceneIdx = ArmatureTestIndex.TEST_ASYNCHRONOUS_LOADING + +local ArmatureTestScene = class("ArmatureTestScene") +ArmatureTestScene.__index = ArmatureTestScene + +function ArmatureTestScene.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, ArmatureTestScene) + return target +end + +function ArmatureTestScene:runThisTest() + armatureSceneIdx = ArmatureTestIndex.TEST_ASYNCHRONOUS_LOADING + self:addChild(restartArmatureTest()) +end + +function ArmatureTestScene.create() + local scene = ArmatureTestScene.extend(cc.Scene:create()) + local bg = cc.Sprite:create("armature/bg.jpg") + bg:setPosition(VisibleRect:center()) + + local scaleX = VisibleRect:getVisibleRect().width / bg:getContentSize().width + local scaleY = VisibleRect:getVisibleRect().height / bg:getContentSize().height + + bg:setScaleX(scaleX) + bg:setScaleY(scaleY) + + scene:addChild(bg) + return scene +end + +function ArmatureTestScene.toMainMenuCallback() + ccs.ArmatureDataManager:purgeArmatureSystem() +end + +local ArmatureTestLayer = class("ArmatureTestLayer") +ArmatureTestLayer.__index = ArmatureTestLayer +ArmatureTestLayer._backItem = nil +ArmatureTestLayer._restarItem = nil +ArmatureTestLayer._nextItem = nil + +function ArmatureTestLayer:onEnter() + +end + +function ArmatureTestLayer.title(idx) + if ArmatureTestIndex.TEST_ASYNCHRONOUS_LOADING == idx then + return "Test Asynchronous Loading" + elseif ArmatureTestIndex.TEST_DIRECT_LOADING == idx then + return "Test Direct Loading" + elseif ArmatureTestIndex.TEST_COCOSTUDIO_WITH_SKELETON == idx then + return "Test Export From CocoStudio With Skeleton Effect" + elseif ArmatureTestIndex.TEST_DRAGON_BONES_2_0 == idx then + return "Test Export From DragonBones version 2.0" + elseif ArmatureTestIndex.TEST_PERFORMANCE == idx then + return "Test Performance" + elseif ArmatureTestIndex.TEST_CHANGE_ZORDER == idx then + return "Test Change ZOrder Of Different Armature" + elseif ArmatureTestIndex.TEST_ANIMATION_EVENT == idx then + return "Test Armature Animation Event" + elseif ArmatureTestIndex.TEST_FRAME_EVENT == idx then + return "Test Frame Event" + elseif ArmatureTestIndex.TEST_PARTICLE_DISPLAY == idx then + return "Test Particle Display" + elseif ArmatureTestIndex.TEST_USE_DIFFERENT_PICTURE == idx then + return "Test One Armature Use Different Picture" + elseif ArmatureTestIndex.TEST_ANCHORPOINT == idx then + return "Test Set AnchorPoint" + elseif ArmatureTestIndex.TEST_ARMATURE_NESTING == idx then + return "Test Armature Nesting" + elseif ArmatureTestIndex.TEST_ARMATURE_NESTING_2 == idx then + return "Test Armature Nesting 2" + end +end + +function ArmatureTestLayer.subTitle(idx) + if ArmatureTestIndex.TEST_ASYNCHRONOUS_LOADING == idx then + return "current percent :" + elseif ArmatureTestIndex.TEST_PERFORMANCE == idx then + return "Current Armature Count : " + elseif ArmatureTestIndex.TEST_PARTICLE_DISPLAY == idx then + return "Touch to change animation" + elseif ArmatureTestIndex.TEST_USE_DIFFERENT_PICTURE == idx then + return "weapon and armature are in different picture" + elseif ArmatureTestIndex.TEST_ARMATURE_NESTING_2 == idx then + return "Move to a mount and press the ChangeMount Button." + else + return "" + end +end + +function ArmatureTestLayer.create() + local layer = ArmatureTestLayer.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + layer:creatTitleAndSubTitle(armatureSceneIdx) + end + + return layer +end + +function ArmatureTestLayer.backCallback() + local newScene = ArmatureTestScene.create() + newScene:addChild(backArmatureTest()) + cc.Director:getInstance():replaceScene(newScene) +end + +function ArmatureTestLayer.restartCallback() + local newScene = ArmatureTestScene.create() + newScene:addChild(restartArmatureTest()) + cc.Director:getInstance():replaceScene(newScene) +end + +function ArmatureTestLayer.nextCallback() + local newScene = ArmatureTestScene.create() + newScene:addChild(nextArmatureTest()) + cc.Director:getInstance():replaceScene(newScene) +end + +function ArmatureTestLayer:createMenu() + local menu = cc.Menu:create() + + self._backItem = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + self._backItem:registerScriptTapHandler(self.backCallback) + menu:addChild(self._backItem,itemTagBasic) + self._restarItem = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + self._restarItem:registerScriptTapHandler(self.restartCallback) + menu:addChild(self._restarItem,itemTagBasic) + self._nextItem = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + menu:addChild(self._nextItem,itemTagBasic) + self._nextItem:registerScriptTapHandler(self.nextCallback) + + local size = cc.Director:getInstance():getWinSize() + self._backItem:setPosition(cc.p(size.width / 2 - self._restarItem:getContentSize().width * 2, self._restarItem:getContentSize().height / 2)) + self._restarItem:setPosition(cc.p(size.width / 2, self._restarItem:getContentSize().height / 2)) + self._nextItem:setPosition(cc.p(size.width / 2 + self._restarItem:getContentSize().width * 2, self._restarItem:getContentSize().height / 2)) + + menu:setPosition(cc.p(0, 0)) + + self:addChild(menu) +end + +function ArmatureTestLayer.toExtensionMenu() + ccs.ArmatureDataManager:destroyInstance() + local scene = CocoStudioTestMain() + if scene ~= nil then + cc.Director:getInstance():replaceScene(scene) + end +end + +function ArmatureTestLayer:createToExtensionMenu() + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local menuItemFont = cc.MenuItemFont:create("Back") + menuItemFont:setPosition(cc.p(VisibleRect:rightBottom().x - 50, VisibleRect:rightBottom().y + 25)) + menuItemFont:registerScriptTapHandler(ArmatureTestLayer.toExtensionMenu) + + local backMenu = cc.Menu:create() + backMenu:addChild(menuItemFont) + backMenu:setPosition(cc.p(0, 0)) + self:addChild(backMenu,10) +end + +function ArmatureTestLayer:creatTitleAndSubTitle(idx) + local title = cc.LabelTTF:create(ArmatureTestLayer.title(idx),"Arial",18) + title:setColor(cc.c3b(0,0,0)) + self:addChild(title, 1, 10000) + title:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 30)) + local subTitle = nil + if "" ~= ArmatureTestLayer.subTitle(idx) then + local subTitle = cc.LabelTTF:create(ArmatureTestLayer.subTitle(idx), "Arial", 18) + subTitle:setColor(cc.c3b(0,0,0)) + self:addChild(subTitle, 1, 10001) + subTitle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 60) ) + end +end + +local TestAsynchronousLoading = class("TestAsynchronousLoading",ArmatureTestLayer) +TestAsynchronousLoading.__index = TestAsynchronousLoading + +function TestAsynchronousLoading.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestAsynchronousLoading) + return target +end + +function TestAsynchronousLoading:onEnter() + self._backItem:setEnabled(false) + self._restarItem:setEnabled(false) + self._nextItem:setEnabled(false) + + local title = cc.LabelTTF:create(ArmatureTestLayer.title(1),"Arial",18) + title:setColor(cc.c3b(0,0,0)) + self:addChild(title, 1, 10000) + title:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 30)) + local subTitle = nil + if "" ~= ArmatureTestLayer.subTitle(1) then + local subInfo = ArmatureTestLayer.subTitle(1) .. 0.0 + local subTitle = cc.LabelTTF:create(subInfo, "Arial", 18) + subTitle:setColor(cc.c3b(0,0,0)) + self:addChild(subTitle, 1, 10001) + subTitle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 60) ) + end + + local function dataLoaded(percent) + local label = self:getChildByTag(10001) + if nil ~= label then + local subInfo = ArmatureTestLayer.subTitle(1) .. (percent * 100) + label:setString(subInfo) + end + if percent >= 1 and nil ~= self._backItem and nil ~= self._restarItem and nil ~= self._nextItem then + self._backItem:setEnabled(true) + self._restarItem:setEnabled(true) + self._nextItem:setEnabled(true) + end + end + + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/knight.png", "armature/knight.plist", "armature/knight.xml", dataLoaded) + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/weapon.png", "armature/weapon.plist", "armature/weapon.xml", dataLoaded) + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/robot.png", "armature/robot.plist", "armature/robot.xml", dataLoaded) + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/cyborg.png", "armature/cyborg.plist", "armature/cyborg.xml", dataLoaded) + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/Dragon.png", "armature/Dragon.plist", "armature/Dragon.xml", dataLoaded) + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/Cowboy.ExportJson", dataLoaded) + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/hero.ExportJson", dataLoaded) + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/horse.ExportJson", dataLoaded) + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/bear.ExportJson", dataLoaded) + ccs.ArmatureDataManager:getInstance():addArmatureFileInfoAsync("armature/HeroAnimation.ExportJson", dataLoaded) + +end + +function TestAsynchronousLoading.restartCallback() + ccs.ArmatureDataManager:destoryInstance() + local newScene = ArmatureTestScene.create() + newScene:addChild(restartArmatureTest()) + cc.Director:getInstance():replaceScene(newScene) +end + +function TestAsynchronousLoading.create() + local layer = TestAsynchronousLoading.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local TestDirectLoading = class("TestDirectLoading",ArmatureTestLayer) +TestDirectLoading.__index = TestDirectLoading + +function TestDirectLoading.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestDirectLoading) + return target +end + +function TestDirectLoading:onEnter() + ccs.ArmatureDataManager:getInstance():removeArmatureFileInfo("armature/bear.ExportJson") + ccs.ArmatureDataManager:getInstance():addArmatureFileInfo("armature/bear.ExportJson") + local armature = ccs.Armature:create("bear") + armature:getAnimation():playWithIndex(0) + armature:setPosition(cc.p(VisibleRect:center())) + self:addChild(armature) +end + +function TestDirectLoading.create() + local layer = TestDirectLoading.extend(cc.Layer:create()) + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + + layer:creatTitleAndSubTitle(armatureSceneIdx) + end + return layer +end + + +local TestCSWithSkeleton = class("TestCSWithSkeleton",ArmatureTestLayer) +TestCSWithSkeleton.__index = TestCSWithSkeleton + +function TestCSWithSkeleton.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestCSWithSkeleton) + return target +end + +function TestCSWithSkeleton:onEnter() + local armature = ccs.Armature:create("Cowboy") + armature:getAnimation():playWithIndex(0) + armature:setScale(0.2) + armature:setAnchorPoint(cc.p(0.5, 0.5)) + armature:setPosition(cc.p(winSize.width / 2, winSize.height / 2)) + self:addChild(armature) +end + +function TestCSWithSkeleton.create() + local layer = TestCSWithSkeleton.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + layer:creatTitleAndSubTitle(armatureSceneIdx) + end + + return layer +end + +local TestDragonBones20 = class("TestDragonBones20",ArmatureTestLayer) +TestDragonBones20.__index = TestDragonBones20 + +function TestDragonBones20.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestDragonBones20) + return target +end + +function TestDragonBones20:onEnter() + local armature = ccs.Armature:create("Dragon") + armature:getAnimation():playWithIndex(1) + armature:getAnimation():setSpeedScale(0.4) + armature:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y * 0.3)) + armature:setScale(0.6) + self:addChild(armature) +end + +function TestDragonBones20.create() + local layer = TestDragonBones20.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + layer:creatTitleAndSubTitle(armatureSceneIdx) + end + return layer +end + +local TestPerformance = class("TestPerformance",ArmatureTestLayer) +TestPerformance.__index = TestPerformance +TestPerformance._armatureCount = 0 +TestPerformance._frames = 0 +TestPerformance._times = 0 +TestPerformance._lastTimes = 0 +TestPerformance._generated = false + +function TestPerformance.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestPerformance) + return target +end + +function TestPerformance:refreshTitle() + local subTitleInfo = ArmatureTestLayer.subTitle(5) .. self._armatureCount + local label = tolua.cast(self:getChildByTag(10001),"cc.LabelTTF") + label:setString(subTitleInfo) +end + +function TestPerformance:addArmatureToParent(armature) + self:addChild(armature, 0, armaturePerformanceTag + self._armatureCount) +end + +function TestPerformance:removeArmatureFromParent(tag) + self:removeChildByTag(armaturePerformanceTag + self._armatureCount, true) +end + +function TestPerformance:addArmature(num) + for i = 1, num do + self._armatureCount = self._armatureCount + 1 + local armature = ccs.Armature:create("Knight_f/Knight") + armature:getAnimation():playWithIndex(0) + armature:setPosition(50 + self._armatureCount * 2, 150) + armature:setScale(0.6) + self:addArmatureToParent(armature) + end + + self:refreshTitle() +end + +function TestPerformance:onEnter() + + local function onIncrease(sender) + self:addArmature(20) + end + + local function onDecrease(sender) + if self._armatureCount == 0 then + return + end + + for i = 1, 20 do + self:removeArmatureFromParent(armaturePerformanceTag + self._armatureCount) + self._armatureCount = self._armatureCount - 1 + self:refreshTitle() + end + end + + cc.MenuItemFont:setFontSize(65) + local decrease = cc.MenuItemFont:create(" - ") + decrease:setColor(cc.c3b(0,200,20)) + decrease:registerScriptTapHandler(onDecrease) + + local increase = cc.MenuItemFont:create(" + ") + increase:setColor(cc.c3b(0,200,20)) + increase:registerScriptTapHandler(onIncrease) + + local menu = cc.Menu:create(decrease, increase ) + menu:alignItemsHorizontally() + menu:setPosition(cc.p(VisibleRect:getVisibleRect().width/2, VisibleRect:getVisibleRect().height-100)) + self:addChild(menu, 10000) + + self._armatureCount = 0 + self._frames = 0 + self._times = 0 + self._lastTimes = 0 + self._generated = false + + self:addArmature(100) +end + +function TestPerformance.create() + local layer = TestPerformance.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + return layer +end + +local TestPerformanceBatchNode = class("TestPerformanceBatchNode",TestPerformance) +TestPerformanceBatchNode.__index = TestPerformanceBatchNode +TestPerformanceBatchNode._batchNode = nil + +function TestPerformanceBatchNode.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestPerformanceBatchNode) + return target +end + +function TestPerformanceBatchNode:addArmatureToParent(armature) + self._batchNode:addChild(armature, 0, armaturePerformanceTag + self._armatureCount) +end + +function TestPerformanceBatchNode:removeArmatureFromParent(tag) + self._batchNode:removeChildByTag(armaturePerformanceTag + self._armatureCount, true) +end + +function TestPerformanceBatchNode:onEnter() + self._batchNode = ccs.BatchNode:create() + self:addChild(self._batchNode) + + local function onIncrease(sender) + self:addArmature(20) + end + + local function onDecrease(sender) + if self._armatureCount == 0 then + return + end + + for i = 1, 20 do + self:removeArmatureFromParent(armaturePerformanceTag + self._armatureCount) + self._armatureCount = self._armatureCount - 1 + self:refreshTitle() + end + end + + cc.MenuItemFont:setFontSize(65) + local decrease = cc.MenuItemFont:create(" - ") + decrease:setColor(cc.c3b(0,200,20)) + decrease:registerScriptTapHandler(onDecrease) + + local increase = cc.MenuItemFont:create(" + ") + increase:setColor(cc.c3b(0,200,20)) + increase:registerScriptTapHandler(onIncrease) + + local menu = cc.Menu:create(decrease, increase ) + menu:alignItemsHorizontally() + menu:setPosition(cc.p(VisibleRect:getVisibleRect().width/2, VisibleRect:getVisibleRect().height-100)) + self:addChild(menu, 10000) + + self._armatureCount = 0 + self._frames = 0 + self._times = 0 + self._lastTimes = 0 + self._generated = false + + self:addArmature(100) +end + +function TestPerformanceBatchNode.create() + local layer = TestPerformanceBatchNode.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + return layer +end + +local TestChangeZorder = class("TestChangeZorder",ArmatureTestLayer) +TestChangeZorder.__index = TestChangeZorder +TestChangeZorder.currentTag = -1 + +function TestChangeZorder.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestChangeZorder) + return target +end + +function TestChangeZorder:onEnter() + self.currentTag = -1 + + local armature = ccs.Armature:create("Knight_f/Knight") + armature:getAnimation():playWithIndex(0) + armature:setPosition(cc.p(winSize.width / 2, winSize.height / 2 - 100 )) + armature:setScale(0.6) + self.currentTag = self.currentTag + 1 + self:addChild(armature, self.currentTag, self.currentTag) + + armature = ccs.Armature:create("Cowboy") + armature:getAnimation():playWithIndex(0) + armature:setScale(0.24) + armature:setPosition(cc.p(winSize.width / 2, winSize.height / 2 - 100)) + self.currentTag = self.currentTag + 1 + self:addChild(armature, self.currentTag, self.currentTag) + + armature = ccs.Armature:create("Dragon") + armature:getAnimation():playWithIndex(0) + armature:setPosition(cc.p(winSize.width / 2, winSize.height / 2 - 100)) + armature:setScale(0.6) + self.currentTag = self.currentTag + 1 + self:addChild(armature, self.currentTag, self.currentTag) + + local function changeZorder(dt) + local node = self:getChildByTag(self.currentTag) + node:setLocalZOrder(math.random(0,1) * 3) + self.currentTag = (self.currentTag + 1) % 3 + end + + schedule(self,changeZorder, 1) +end + +function TestChangeZorder.create() + local layer = TestChangeZorder.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + return layer +end + +--UNDO callback +local TestAnimationEvent = class("TestAnimationEvent",ArmatureTestLayer) +TestAnimationEvent.__index = TestAnimationEvent + +function TestAnimationEvent.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestAnimationEvent) + return target +end + +function TestAnimationEvent:onEnter() + local armature = ccs.Armature:create("Cowboy") + armature:getAnimation():play("Fire") + armature:setScaleX(-0.24) + armature:setScaleY(0.24) + armature:setPosition(cc.p(VisibleRect:left().x + 50, VisibleRect:left().y)) + + local function callback1() + armature:runAction(cc.ScaleTo:create(0.3, 0.24, 0.24)) + armature:getAnimation():play("FireMax", 10) + end + + local function callback2() + armature:runAction(cc.ScaleTo:create(0.3, -0.24, 0.24)) + armature:getAnimation():play("Fire", 10) + end + + local function animationEvent(armatureBack,movementType,movementID) + local id = movementID + if movementType == ccs.MovementEventType.loopComplete then + if id == "Fire" then + local actionToRight = cc.MoveTo:create(2, cc.p(VisibleRect:right().x - 50, VisibleRect:right().y)) + armatureBack:stopAllActions() + armatureBack:runAction(cc.Sequence:create(actionToRight,cc.CallFunc:create(callback1))) + armatureBack:getAnimation():play("Walk") + elseif id == "FireMax" then + local actionToLeft = cc.MoveTo:create(2, cc.p(VisibleRect:left().x + 50, VisibleRect:left().y)) + armatureBack:stopAllActions() + armatureBack:runAction(cc.Sequence:create(actionToLeft, cc.CallFunc:create(callback2))) + armatureBack:getAnimation():play("Walk") + end + end + end + + armature:getAnimation():setMovementEventCallFunc(animationEvent) + + self:addChild(armature) +end + +function TestAnimationEvent.create() + local layer = TestAnimationEvent.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + return layer +end + +local TestFrameEvent = class("TestFrameEvent",ArmatureTestLayer) +TestFrameEvent.__index = TestFrameEvent + +function TestFrameEvent.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestFrameEvent) + return target +end + +function TestFrameEvent:onEnter() + + local gridNode = cc.NodeGrid:create() + + local armature = ccs.Armature:create("HeroAnimation") + armature:getAnimation():play("attack") + armature:getAnimation():setSpeedScale(0.5) + armature:setPosition(cc.p(VisibleRect:center().x - 50, VisibleRect:center().y -100)) + + local function onFrameEvent( bone,evt,originFrameIndex,currentFrameIndex) + if (not gridNode:getActionByTag(frameEventActionTag)) or (not gridNode:getActionByTag(frameEventActionTag):isDone()) then + gridNode:stopAllActions() + + local action = cc.ShatteredTiles3D:create(0.2, cc.size(16,12), 5, false) + action:setTag(frameEventActionTag) + gridNode:runAction(action) + end + end + + armature:getAnimation():setFrameEventCallFunc(onFrameEvent) + gridNode:addChild(armature) + + self:addChild(gridNode) + + local function checkAction(dt) + if gridNode:getNumberOfRunningActions() == 0 and gridNode:getGrid() ~= nil then + gridNode:setGrid(nil) + end + end + + self:scheduleUpdateWithPriorityLua(checkAction,0) + + local function onNodeEvent(tag) + if "exit" == tag then + self:unscheduleUpdate() + end + end +end + +function TestFrameEvent.create() + local layer = TestFrameEvent.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + return layer +end + +local TestParticleDisplay = class("TestParticleDisplay",ArmatureTestLayer) +TestParticleDisplay.__index = TestParticleDisplay +TestParticleDisplay.animationID = 0 +TestParticleDisplay.armature = nil + +function TestParticleDisplay.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestParticleDisplay) + return target +end + +function TestParticleDisplay:onEnter() + self.animationID = 0 + + self.armature = ccs.Armature:create("robot") + self.armature:getAnimation():playWithIndex(0) + self.armature:setPosition(VisibleRect:center()) + self.armature:setScale(0.48) + self:addChild(self.armature) + + local p1 = cc.ParticleSystemQuad:create("Particles/SmallSun.plist") + local p2 = cc.ParticleSystemQuad:create("Particles/SmallSun.plist") + + local bone = ccs.Bone:create("p1") + bone:addDisplay(p1, 0) + bone:changeDisplayWithIndex(0, true) + bone:setIgnoreMovementBoneData(true) + bone:setLocalZOrder(100) + bone:setScale(1.2) + self.armature:addBone(bone, "bady-a3") + + bone = ccs.Bone:create("p2") + bone:addDisplay(p2, 0) + bone:changeDisplayWithIndex(0, true) + bone:setIgnoreMovementBoneData(true) + bone:setLocalZOrder(100) + bone:setScale(1.2) + self.armature:addBone(bone, "bady-a30") + + -- handling touch events + local function onTouchEnded(touches, event) + self.animationID = (self.animationID + 1) % self.armature:getAnimation():getMovementCount() + self.armature:getAnimation():playWithIndex(self.animationID) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCHES_ENDED ) + + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self) +end + +function TestParticleDisplay.create() + local layer = TestParticleDisplay.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local TestUseMutiplePicture = class("TestUseMutiplePicture",ArmatureTestLayer) +TestUseMutiplePicture.__index = TestUseMutiplePicture +TestUseMutiplePicture.displayIndex = 0 +TestUseMutiplePicture.armature = nil + +function TestUseMutiplePicture.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestUseMutiplePicture) + return target +end + +function TestUseMutiplePicture:onEnter() + self.displayIndex = 1 + + self.armature = ccs.Armature:create("Knight_f/Knight") + self.armature:getAnimation():playWithIndex(0) + self.armature:setPosition(cc.p(VisibleRect:left().x + 70, VisibleRect:left().y)) + self.armature:setScale(1.2) + self:addChild(self.armature) + + local weapon = + { + "weapon_f-sword.png", + "weapon_f-sword2.png", + "weapon_f-sword3.png", + "weapon_f-sword4.png", + "weapon_f-sword5.png", + "weapon_f-knife.png", + "weapon_f-hammer.png", + } + + local i = 1 + for i = 1,table.getn(weapon) do + local skin = ccs.Skin:createWithSpriteFrameName(weapon[i]) + self.armature:getBone("weapon"):addDisplay(skin, i - 1) + end + + -- handling touch events + local function onTouchEnded(touches, event) + self.displayIndex = (self.displayIndex + 1) % (table.getn(weapon) - 1) + self.armature:getBone("weapon"):changeDisplayWithIndex(self.displayIndex, true) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCHES_ENDED ) + + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self) +end + +function TestUseMutiplePicture.create() + local layer = TestUseMutiplePicture.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local TestAnchorPoint = class("TestAnchorPoint",ArmatureTestLayer) +TestAnchorPoint.__index = TestAnchorPoint + +function TestAnchorPoint.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestAnchorPoint) + return target +end + +function TestAnchorPoint:onEnter() + local i = 1 + for i = 1 , 5 do + local armature = ccs.Armature:create("Cowboy") + armature:getAnimation():playWithIndex(0) + armature:setPosition(VisibleRect:center()) + armature:setScale(0.2) + self:addChild(armature, 0, i - 1) + end + + self:getChildByTag(0):setAnchorPoint(cc.p(0,0)) + self:getChildByTag(1):setAnchorPoint(cc.p(0,1)) + self:getChildByTag(2):setAnchorPoint(cc.p(1,0)) + self:getChildByTag(3):setAnchorPoint(cc.p(1,1)) + self:getChildByTag(4):setAnchorPoint(cc.p(0.5,0.5)) +end + +function TestAnchorPoint.create() + local layer = TestAnchorPoint.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local TestArmatureNesting = class("TestArmatureNesting",ArmatureTestLayer) +TestArmatureNesting.__index = TestArmatureNesting +TestArmatureNesting.weaponIndex = 0 +TestArmatureNesting.armature = nil + +function TestArmatureNesting.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestArmatureNesting) + return target +end + +function TestArmatureNesting:onEnter() + self.weaponIndex = 0 + + self.armature = ccs.Armature:create("cyborg") + self.armature:getAnimation():playWithIndex(1) + self.armature:setPosition(VisibleRect:center()) + self.armature:setScale(1.2) + self.armature:getAnimation():setSpeedScale(0.4) + self:addChild(self.armature) + + -- handling touch events + local function onTouchEnded(touches, event) + self.weaponIndex = (self.weaponIndex + 1) % 4 + self.armature:getBone("armInside"):getChildArmature():getAnimation():playWithIndex(self.weaponIndex) + self.armature:getBone("armOutside"):getChildArmature():getAnimation():playWithIndex(self.weaponIndex) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCHES_ENDED ) + + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self) +end + +function TestArmatureNesting.create() + local layer = TestArmatureNesting.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local Hero = class("Hero") +Hero.__index = Hero +Hero._mount = nil +Hero._layer = nil + +function Hero.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, Hero) + return target +end + +function Hero:changeMount(armature) + if nil == armature then + --note + self:retain() + + self:playWithIndex(0) + self._mount:getBone("hero"):removeDisplay(0) + self._mount:stopAllActions() + self:setPosition(self._mount:getPosition()) + self._layer:addChild(self) + self:release() + self._mount = armature + else + self._mount = armature + self:retain() + self:removeFromParent(false) + local bone = armature:getBone("hero") + bone:addDisplay(self, 0) + bone:changeDisplayWithIndex(0, true) + bone:setIgnoreMovementBoneData(true) + self:setPosition(cc.p(0,0)) + self:playWithIndex(1) + self:setScale(1) + self:release() + end +end + +function Hero:playWithIndex(index) + self:getAnimation():playWithIndex(index) + if nil ~= self._mount then + self._mount:getAnimation():playWithIndex(index) + end +end + +function Hero.create(name) + local hero = Hero.extend(ccs.Armature:create(name)) + return hero +end + + + +local TestArmatureNesting2 = class("TestArmatureNesting",ArmatureTestLayer) +TestArmatureNesting2.__index = TestArmatureNesting2 +TestArmatureNesting2._hero = nil +TestArmatureNesting2._horse = nil +TestArmatureNesting2._horse2 = nil +TestArmatureNesting2._bear = nil +TestArmatureNesting2._touchedMenu = nil + +function TestArmatureNesting2.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TestArmatureNesting2) + return target +end + +function TestArmatureNesting2:onEnter() + -- handling touch events + local function onTouchEnded(touches, event) + local location = touches[1]:getLocation() + local armature = self._hero._mount and self._hero._mount or self._hero + if location.x < armature:getPositionX() then + armature:setScaleX(-1) + else + armature:setScaleX(1) + end + + local move = cc.MoveTo:create(2, location) + armature:stopAllActions() + armature:runAction(cc.Sequence:create(move)) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCHES_ENDED ) + + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self) + + local function changeMountCallback(sender) + self._hero:stopAllActions() + + if nil ~= self._hero._mount then + self._hero:changeMount(nil) + else + if cc.pGetDistance(cc.p(self._hero:getPosition()),cc.p(self._horse:getPosition())) < 20 then + self._hero:changeMount(self._horse) + elseif cc.pGetDistance(cc.p(self._hero:getPosition()),cc.p(self._horse2:getPosition())) < 20 then + self._hero:changeMount(self._horse2) + elseif cc.pGetDistance(cc.p(self._hero:getPosition()),cc.p(self._bear:getPosition())) < 30 then + self._hero:changeMount(self._bear) + end + end + end + + self._touchedMenu = false + local label = cc.LabelTTF:create("Change Mount", "Arial", 20) + local menuItem = cc.MenuItemLabel:create(label) + menuItem:registerScriptTapHandler(changeMountCallback) + local menu = cc.Menu:create(menuItem) + menu:setPosition(cc.p(0,0)) + menuItem:setPosition( cc.p( VisibleRect:right().x - 67, VisibleRect:bottom().y + 50) ) + self:addChild(menu, 2) + + self._hero = Hero.create("hero") + self._hero._layer = self + self._hero:playWithIndex(0) + self._hero:setPosition(cc.p(VisibleRect:left().x + 20, VisibleRect:left().y)) + self:addChild(self._hero) + + self._horse = self:createMount("horse", VisibleRect:center()) + + self._horse2 = self:createMount("horse", cc.p(120, 200)) + self._horse2:setOpacity(200) + + self._bear = self:createMount("bear", cc.p(300,70)) +end + +function TestArmatureNesting2:createMount(name,pt) + local armature = ccs.Armature:create(name) + armature:getAnimation():playWithIndex(0) + armature:setPosition(pt) + self:addChild(armature) + return armature +end + +function TestArmatureNesting2.create() + local layer = TestArmatureNesting2.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:createToExtensionMenu() + layer:creatTitleAndSubTitle(armatureSceneIdx) + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + + +local armatureSceneArr = +{ + TestAsynchronousLoading.create, + TestDirectLoading.create, + TestCSWithSkeleton.create, + TestDragonBones20.create, + TestPerformance.create, + --TestPerformanceBatchNode.create, + TestChangeZorder.create, + TestAnimationEvent.create, + TestFrameEvent.create, + TestParticleDisplay.create, + TestUseMutiplePicture.create, + TestAnchorPoint.create, + TestArmatureNesting.create, + TestArmatureNesting2.create, +} + +function nextArmatureTest() + armatureSceneIdx = armatureSceneIdx + 1 + armatureSceneIdx = armatureSceneIdx % table.getn(armatureSceneArr) + if 0 == armatureSceneIdx then + armatureSceneIdx = table.getn(armatureSceneArr) + end + return armatureSceneArr[armatureSceneIdx]() +end + +function backArmatureTest() + armatureSceneIdx = armatureSceneIdx - 1 + if armatureSceneIdx <= 0 then + armatureSceneIdx = armatureSceneIdx + table.getn(armatureSceneArr) + end + + return armatureSceneArr[armatureSceneIdx]() +end + +function restartArmatureTest() + return armatureSceneArr[armatureSceneIdx]() +end + +local function addFileInfo() + +end + +function runArmatureTestScene() + local scene = ArmatureTestScene.create() + scene:runThisTest() + cc.Director:getInstance():replaceScene(scene) +end diff --git a/samples/lua-tests/src/CocoStudioTest/CocoStudioGUITest/CocoStudioGUITest.lua.REMOVED.git-id b/samples/lua-tests/src/CocoStudioTest/CocoStudioGUITest/CocoStudioGUITest.lua.REMOVED.git-id new file mode 100644 index 0000000000..0279040f69 --- /dev/null +++ b/samples/lua-tests/src/CocoStudioTest/CocoStudioGUITest/CocoStudioGUITest.lua.REMOVED.git-id @@ -0,0 +1 @@ +09ed3c488f6d182685c416c291f8f325fb5cc2d2 \ No newline at end of file diff --git a/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/CocoStudioSceneTest.lua b/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/CocoStudioSceneTest.lua new file mode 100644 index 0000000000..292352189a --- /dev/null +++ b/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/CocoStudioSceneTest.lua @@ -0,0 +1,751 @@ +require "src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts" +require "src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/cons" +require "src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/eventDef" + +local itemTagBasic = 1000 + +local sceneEditorTestIdx = 1 + +local SceneEditorTestLayer = class("SceneEditorTestLayer") +SceneEditorTestLayer.__index = SceneEditorTestLayer +SceneEditorTestLayer._backItem = nil +SceneEditorTestLayer._restarItem = nil +SceneEditorTestLayer._nextItem = nil +SceneEditorTestLayer.title = +{ + "loadSceneEdtiorFile Test", + "Sprite Component Test", + "Armature Component Test", + "UI Component Test", + "TmxMap Component Test", + "Particle Component Test", + "Effect Component Test", + "Background Component Test", + "Attribute Component Test", + "Trigger Test", +} + +function SceneEditorTestLayer.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, SceneEditorTestLayer) + return target +end + + +function SceneEditorTestLayer:onEnter() + +end + +function SceneEditorTestLayer:onExit() + ccs.TriggerMng.destroyInstance() + ccs.ArmatureDataManager:destroyInstance() + ccs.SceneReader:destroyInstance() + ccs.ActionManagerEx:destroyInstance() + ccs.GUIReader:destroyInstance() +end + +function SceneEditorTestLayer.create() + local layer = SceneEditorTestLayer.extend(cc.Layer:create()) + if nil ~= layer then + layer:createTitle() + layer:createMenu() + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + end + end + layer:registerScriptHandler(onNodeEvent) + end +end + +function SceneEditorTestLayer:createTitle() + local title = cc.LabelTTF:create(self.title[sceneEditorTestIdx],"Arial",18) + title:setColor(cc.c3b(255, 255, 255)) + self:addChild(title, 1, 10000) + title:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 30)) +end + +function SceneEditorTestLayer:createMenu() + local menu = cc.Menu:create() + + local function pre() + local scene = cc.Scene:create() + scene:addChild(backSceneEditorTest()) + cc.Director:getInstance():replaceScene(scene) + end + + local function restart() + local scene = cc.Scene:create() + scene:addChild(restartSceneEditorTest()) + cc.Director:getInstance():replaceScene(scene) + end + + local function next() + local scene = cc.Scene:create() + scene:addChild(nextSceneEditorTest()) + cc.Director:getInstance():replaceScene(scene) + end + + self._backItem = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + self._backItem:registerScriptTapHandler(pre) + self._restarItem = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + self._restarItem:registerScriptTapHandler(restart) + self._nextItem = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + self._nextItem:registerScriptTapHandler(next) + + + + local size = cc.Director:getInstance():getWinSize() + self._backItem:setPosition(cc.p(size.width / 2 - self._restarItem:getContentSize().width * 2, self._restarItem:getContentSize().height / 2)) + self._restarItem:setPosition(cc.p(size.width / 2, self._restarItem:getContentSize().height / 2)) + self._nextItem:setPosition(cc.p(size.width / 2 + self._restarItem:getContentSize().width * 2, self._restarItem:getContentSize().height / 2)) + + menu:addChild(self._backItem,itemTagBasic) + menu:addChild(self._restarItem,itemTagBasic) + menu:addChild(self._nextItem,itemTagBasic) + menu:setPosition(cc.p(0,0)) + self:addChild(menu, 100) + + local function back() + local scene = CocoStudioTestMain() + if nil ~= scene then + cc.Director:getInstance():replaceScene(scene) + end + end + + local backMenu = cc.Menu:create() + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local menuItemFont = cc.MenuItemFont:create("Back") + menuItemFont:setPosition(cc.p(VisibleRect:rightBottom().x - 50, VisibleRect:rightBottom().y + 25)) + menuItemFont:registerScriptTapHandler(back) + + backMenu:addChild(menuItemFont) + backMenu:setPosition(cc.p(0, 0)) + self:addChild(backMenu,10) +end + +local LoadSceneEdtiorFileTest = class("LoadSceneEdtiorFileTest",SceneEditorTestLayer) +LoadSceneEdtiorFileTest.__index = LoadSceneEdtiorFileTest + +function LoadSceneEdtiorFileTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, LoadSceneEdtiorFileTest) + return target +end + +function LoadSceneEdtiorFileTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/LoadSceneEdtiorFileTest/FishJoy2.json") + return node +end + +function LoadSceneEdtiorFileTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + end + +end + +function LoadSceneEdtiorFileTest.create() + local layer = LoadSceneEdtiorFileTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local SpriteComponentTest = class("SpriteComponentTest",SceneEditorTestLayer) +SpriteComponentTest.__index = SpriteComponentTest + +function SpriteComponentTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, SpriteComponentTest) + return target +end + +function SpriteComponentTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/SpriteComponentTest/SpriteComponentTest.json") + if nil ~= node then + local action1 = cc.Blink:create(2, 10) + local action2 = cc.Blink:create(2, 5) + + local sister1 = tolua.cast(node:getChildByTag(10003):getComponent("CCSprite"),"ccs.ComRender") + sister1:getNode():runAction(action1) + local sister2 = tolua.cast(node:getChildByTag(10004):getComponent("CCSprite"),"ccs.ComRender") + sister2:getNode():runAction(action2) + end + + return node +end + +function SpriteComponentTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + end + +end + +function SpriteComponentTest.create() + local layer = SpriteComponentTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local ArmatureComponentTest = class("ArmatureComponentTest",SceneEditorTestLayer) +ArmatureComponentTest.__index = ArmatureComponentTest + +function ArmatureComponentTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, ArmatureComponentTest) + return target +end + +function ArmatureComponentTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/ArmatureComponentTest/ArmatureComponentTest.json") + if nil ~= node then + local blowFish = tolua.cast(node:getChildByTag(10007):getComponent("CCArmature"),"ccs.ComRender") + blowFish:getNode():runAction(cc.MoveBy:create(10.0, cc.p(-1000.0, 0))) + + local butterflyfish = tolua.cast(node:getChildByTag(10008):getComponent("CCArmature"),"ccs.ComRender") + butterflyfish:getNode():runAction(CCMoveBy:create(10.0, cc.p(-1000.0, 0))) + end + + return node +end + +function ArmatureComponentTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + end + +end + +function ArmatureComponentTest.create() + local layer = ArmatureComponentTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local UIComponentTest = class("UIComponentTest",SceneEditorTestLayer) +UIComponentTest.__index = UIComponentTest + +function UIComponentTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, UIComponentTest) + return target +end + +function UIComponentTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/UIComponentTest/UIComponentTest.json") + if nil ~= node then + local render = tolua.cast(node:getChildByTag(10025):getComponent("GUIComponent"),"ccs.ComRender") + local widget = tolua.cast(render:getNode(), "ccui.Widget") + local button = tolua.cast(widget:getChildByName("Button_156"),"ccui.Button") + local function onTouch(sender, eventType) + if eventType == ccui.TouchEventType.began then + local blowFish = tolua.cast(node:getChildByTag(10010):getComponent("CCArmature"), "ccs.ComRender") + blowFish:getNode():runAction(cc.MoveBy:create(10.0, cc.p(-1000.0, 0))) + + local butterflyfish = tolua.cast(node:getChildByTag(10011):getComponent("CCArmature"), "ccs.ComRender") + butterflyfish:getNode():runAction(cc.MoveBy:create(10.0, cc.p(-1000.0, 0))) + end + end + + button:addTouchEventListener(onTouch) + end + + return node +end + +function UIComponentTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + end + +end + +function UIComponentTest.create() + local layer = UIComponentTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local TmxMapComponentTest = class("TmxMapComponentTest",SceneEditorTestLayer) +TmxMapComponentTest.__index = TmxMapComponentTest + +function TmxMapComponentTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TmxMapComponentTest) + return target +end + +function TmxMapComponentTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/TmxMapComponentTest/TmxMapComponentTest.json") + if nil ~= node then + local tmxMap = tolua.cast(node:getChildByTag(10015):getComponent("CCTMXTiledMap"),"ccs.ComRender") + local actionTo = cc.SkewTo:create(2, 0.0, 2.0) + local rotateTo = cc.RotateTo:create(2, 61.0) + local actionScaleTo = cc.ScaleTo:create(2, -0.44, 0.47) + + local actionScaleToBack = cc.ScaleTo:create(2, 1.0, 1.0) + local rotateToBack = cc.RotateTo:create(2, 0) + local actionToBack = cc.SkewTo:create(2, 0, 0) + + tmxMap:getNode():runAction(cc.Sequence:create(actionTo, actionToBack)) + tmxMap:getNode():runAction(cc.Sequence:create(rotateTo, rotateToBack)) + tmxMap:getNode():runAction(cc.Sequence:create(actionScaleTo, actionScaleToBack)) + end + + return node +end + +function TmxMapComponentTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + end + +end + +function TmxMapComponentTest.create() + local layer = TmxMapComponentTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local ParticleComponentTest = class("ParticleComponentTest",SceneEditorTestLayer) +ParticleComponentTest.__index = ParticleComponentTest + +function ParticleComponentTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, ParticleComponentTest) + return target +end + +function ParticleComponentTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/ParticleComponentTest/ParticleComponentTest.json") + if nil ~= node then + local particle = tolua.cast(node:getChildByTag(10020):getComponent("CCParticleSystemQuad"),"ccs.ComRender") + local jump = cc.JumpBy:create(5, cc.p(-500,0), 50, 4) + local action = cc.Sequence:create( jump, jump:reverse()) + particle:getNode():runAction(action) + end + + return node +end + +function ParticleComponentTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + end + +end + +function ParticleComponentTest.create() + local layer = ParticleComponentTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local EffectComponentTest = class("EffectComponentTest",SceneEditorTestLayer) +EffectComponentTest.__index = EffectComponentTest + +function EffectComponentTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, EffectComponentTest) + return target +end + +function EffectComponentTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/EffectComponentTest/EffectComponentTest.json") + if nil ~= node then + local render = tolua.cast(node:getChildByTag(10015):getComponent("CCArmature"),"ccs.ComRender") + local armature = tolua.cast(render:getNode(),"ccs.Armature") + local function animationEvent(armatureBack,movementType,movementID) + local id = movementID + if movementType == ccs.MovementEventType.loopComplete then + if id == "Fire" then + local audio = tolua.cast(node:getChildByTag(10015):getComponent("CCComAudio"), "ccs.ComAudio") + audio:playEffect() + end + end + end + + armature:getAnimation():setMovementEventCallFunc(animationEvent) + end + + return node +end + +function EffectComponentTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + end + +end + +function EffectComponentTest.create() + local layer = EffectComponentTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local BackgroundComponentTest = class("BackgroundComponentTest",SceneEditorTestLayer) +BackgroundComponentTest.__index = BackgroundComponentTest + +function BackgroundComponentTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, BackgroundComponentTest) + return target +end + +function BackgroundComponentTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/BackgroundComponentTest/BackgroundComponentTest.json") + if nil ~= node then + local audio = tolua.cast(node:getComponent("CCBackgroundAudio"),"ccs.ComAudio") + audio:playBackgroundMusic() + end + + return node +end + +function BackgroundComponentTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + end + +end + +function BackgroundComponentTest.create() + local layer = BackgroundComponentTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local AttributeComponentTest = class("AttributeComponentTest",SceneEditorTestLayer) +AttributeComponentTest.__index = AttributeComponentTest + +function AttributeComponentTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, AttributeComponentTest) + return target +end + +function AttributeComponentTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/AttributeComponentTest/AttributeComponentTest.json") + local attribute = tolua.cast(node:getChildByTag(10015):getComponent("CCComAttribute"), "ccs.ComAttribute") + print(string.format("Name: %s, HP: %f, MP: %f", attribute:getString("name"), attribute:getFloat("maxHP"), attribute:getFloat("maxMP"))) + return node +end + +function AttributeComponentTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + end + +end + +function AttributeComponentTest.create() + local layer = AttributeComponentTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local TriggerTest = class("TriggerTest",SceneEditorTestLayer) +TriggerTest.__index = TriggerTest +TriggerTest._touchListener = nil + +function TriggerTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TriggerTest) + return target +end + +function TriggerTest:createGameScene() + local node = ccs.SceneReader:getInstance():createNodeWithSceneFile("scenetest/TriggerTest/TriggerTest.json") + + return node +end + +function TriggerTest:onTouchBegan(touch,event) + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_TOUCHBEGAN) + return true +end + +function TriggerTest:onTouchMoved(touch,event) + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_TOUCHMOVED) +end + +function TriggerTest:onTouchEnded(touch,event) + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_TOUCHENDED) +end + +function TriggerTest:onTouchCancelled(touch,event) + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_TOUCHCANCELLED) +end + +function TriggerTest:onEnter() + self:createMenu() + self:createTitle() + local root = self:createGameScene() + if nil ~= root then + self:addChild(root, 0, 1) + self._touchListener = nil + local listener = cc.EventListenerTouchOneByOne:create() + listener:setSwallowTouches(true) + listener:registerScriptHandler(self.onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener:registerScriptHandler(self.onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) + listener:registerScriptHandler(self.onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) + listener:registerScriptHandler(self.onTouchCancelled,cc.Handler.EVENT_TOUCH_CANCELLED ) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self) + self._touchListener = listener + local function update(dt) + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_UPDATESCENE) + end + self:scheduleUpdateWithPriorityLua(update,0) + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_ENTERSCENE) + end + +end + +function TriggerTest:onExit() + ccs.sendTriggerEvent(triggerEventDef.TRIGGEREVENT_LEAVESCENE) + self:unscheduleUpdate() + local dispatcher = self:getEventDispatcher() + dispatcher:removeEventListener(self._touchListener) + ccs.TriggerMng.destroyInstance() + ccs.ArmatureDataManager:destroyInstance() + ccs.SceneReader:destroyInstance() + ccs.ActionManagerEx:destroyInstance() + ccs.GUIReader:destroyInstance() +end + +function TriggerTest.create() + local layer = TriggerTest.extend(cc.Layer:create()) + + if nil ~= layer then + local function onNodeEvent(event) + if "enter" == event then + layer:onEnter() + elseif "exit" == event then + layer:onExit() + end + end + layer:registerScriptHandler(onNodeEvent) + end + + return layer +end + +local createSceneEditorTest = +{ + LoadSceneEdtiorFileTest.create, + SpriteComponentTest.create, + ArmatureComponentTest.create, + UIComponentTest.create, + TmxMapComponentTest.create, + ParticleComponentTest.create, + EffectComponentTest.create, + BackgroundComponentTest.create, + AttributeComponentTest.create, + TriggerTest.create, +} + +function nextSceneEditorTest( ) + sceneEditorTestIdx = sceneEditorTestIdx + 1 + sceneEditorTestIdx = sceneEditorTestIdx % table.getn(createSceneEditorTest) + + if 0 == sceneEditorTestIdx then + sceneEditorTestIdx = table.getn(createSceneEditorTest) + end + + return createSceneEditorTest[sceneEditorTestIdx]() +end + +function backSceneEditorTest() + sceneEditorTestIdx = sceneEditorTestIdx - 1 + if sceneEditorTestIdx <= 0 then + sceneEditorTestIdx = sceneEditorTestIdx + table.getn(createSceneEditorTest) + end + + return createSceneEditorTest[sceneEditorTestIdx]() +end + +function restartSceneEditorTest() + return createSceneEditorTest[sceneEditorTestIdx]() +end + +function runCocosSceneTestScene() + local scene = cc.Scene:create() + sceneEditorTestIdx = 1 + scene:addChild(restartSceneEditorTest()) + cc.Director:getInstance():replaceScene(scene) +end diff --git a/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua b/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua new file mode 100644 index 0000000000..ef7b828cbf --- /dev/null +++ b/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/acts.lua @@ -0,0 +1,172 @@ +require "CocoStudio" + +local TMoveBy = class("TMoveBy") +TMoveBy._tag = -1 +TMoveBy._duration = 0 +TMoveBy._x = 0 +TMoveBy._y = 0 +TMoveBy._reverse = false +function TMoveBy:ctor() + self._tag = -1 + self._duration = 0.0 + self._x = 0 + self._y = 0 + self._reverse = false +end + +function TMoveBy:init() + return true +end + +function TMoveBy:done() + local node = ccs.SceneReader:getInstance():getNodeByTag(self._tag) + if nil == node then + return + end + local actionBy = cc.MoveBy:create(self._duration, cc.p(self._x, self._y)) + if nil == actionBy then + return + end + + if true == self._reverse then + local actionByBack = actionBy:reverse() + node:runAction(cc.Sequence:create(actionBy, actionByBack)) + else + node:runAction(actionBy) + end +end + +function TMoveBy:serialize(value) + local dataItems = value["dataitems"] + if nil ~= dataItems then + local count = table.getn(dataItems) + for i = 1, count do + local subDict = dataItems[i] + local key = subDict["key"] + if key == "Tag" then + self._tag = subDict["value"] + elseif key == "Duration" then + self._duration = subDict["value"] + elseif key == "x" then + self._x = subDict["value"] + elseif key == "y" then + self._y = subDict["value"] + elseif key == "IsReverse" then + self._reverse = subDict["value"] + end + end + end +end + +function TMoveBy:removeAll() + local node = ccs.SceneReader:getInstance():getNodeByTag(self._tag) + node:getActionManager():removeAllActions() + print("TMoveBy::removeAll") +end + +local TScaleTo = class("TScaleTo") +TScaleTo._tag = -1 +TScaleTo._duration = 0 +TScaleTo._scaleX = 0 +TScaleTo._scaleY = 0 + +function TScaleTo:ctor() + self._tag = -1 + self._duration = 0 + self._scaleX = 0 + self._scaleY = 0 +end + +function TScaleTo:init() + return true +end + +function TScaleTo:done() + local node = ccs.SceneReader:getInstance():getNodeByTag(self._tag) + if nil == node then + return + end + + local actionTo = cc.ScaleTo:create(self._duration, self._scaleX, self._scaleY) + if nil == actionTo then + return + end + + node:runAction(actionTo) +end + +function TScaleTo:serialize(value) + local dataItems = value["dataitems"] + if nil ~= dataItems then + local count = table.getn(dataItems) + for i = 1, count do + local subDict = dataItems[i] + local key = subDict["key"] + if key == "Tag" then + self._tag = subDict["value"] + elseif key == "Duration" then + self._duration = subDict["value"] + elseif key == "ScaleX" then + self._scaleX = subDict["value"] + elseif key == "ScaleY" then + self._scaleY = subDict["value"] + end + end + end +end + +function TScaleTo:removeAll() + print("TScaleTo::removeAll") +end + + +local TriggerState = class("TriggerState") +TriggerState._id = -1 +TriggerState._state = 0 + +function TriggerState:ctor() + self._id = -1 + self._state = 0 +end + +function TriggerState:init() + return true +end + +function TriggerState:done() + local obj = ccs.TriggerMng.getInstance():getTriggerObj(self._id) + if nil ~= obj then + if self._state == 0 then + obj:setEnable(false) + elseif self._state == 1 then + obj:setEnable(true) + elseif self._state == 2 then + ccs.TriggerMng.getInstance():removeTriggerObj(self._id) + end + end +end + +function TriggerState:serialize(value) + local dataItems = value["dataitems"] + if nil ~= dataItems then + local count = table.getn(dataItems) + for i = 1, count do + local subDict = dataItems[i] + local key = subDict["key"] + if key == "ID" then + self._id = subDict["value"] + elseif key == "State" then + self._state = subDict["value"] + end + end + end +end + +function TriggerState:removeAll() + print("TriggerState::removeAll") +end + + +ccs.registerTriggerClass("TScaleTo",TScaleTo.new) +ccs.registerTriggerClass("TMoveBy",TMoveBy.new) +ccs.registerTriggerClass("TriggerState",TriggerState.new) diff --git a/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/cons.lua b/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/cons.lua new file mode 100644 index 0000000000..2f6b10c038 --- /dev/null +++ b/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/cons.lua @@ -0,0 +1,55 @@ +require ("CocoStudio") + +local NodeInRect = class("NodeInRect") +NodeInRect._tag = -1 +NodeInRect._origin = nil +NodeInRect._size = nil + +function NodeInRect:ctor() + self._tag = -1 + self._origin = nil + self._size = nil + self._origin = cc.p(0, 0) + self._size = cc.size(0, 0) +end + +function NodeInRect:init() + return true +end + +function NodeInRect:detect() + local node = ccs.SceneReader:getInstance():getNodeByTag(self._tag) + if nil ~= node and math.abs(node:getPositionX() - self._origin.x) <= self._size.width and math.abs(node:getPositionY() - self._origin.y) <= self._size.height then + return true + end + + return false +end + +function NodeInRect:serialize(value) + local dataItems = value["dataitems"] + if nil ~= dataItems then + local count = table.getn(dataItems) + for i = 1, count do + local subDict = dataItems[i] + local key = subDict["key"] + if key == "Tag" then + self._tag = subDict["value"] + elseif key == "originX" then + self._origin.x = subDict["value"] + elseif key == "originY" then + self._origin.y = subDict["value"] + elseif key == "sizeWidth" then + self._size.width = subDict["value"] + elseif key == "sizeHeight" then + self._size.height = subDict["value"] + end + end + end +end + +function NodeInRect:removeAll() + print("NodeInRect::removeAll") +end + +ccs.registerTriggerClass("NodeInRect",NodeInRect.new) diff --git a/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/eventDef.lua b/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/eventDef.lua new file mode 100644 index 0000000000..a973745b38 --- /dev/null +++ b/samples/lua-tests/src/CocoStudioTest/CocoStudioSceneTest/TriggerCode/eventDef.lua @@ -0,0 +1,12 @@ + +triggerEventDef = +{ + TRIGGEREVENT_ENTERSCENE = 0, + TRIGGEREVENT_LEAVESCENE = 1, + TRIGGEREVENT_INITSCENE = 2, + TRIGGEREVENT_UPDATESCENE = 3, + TRIGGEREVENT_TOUCHBEGAN = 4, + TRIGGEREVENT_TOUCHMOVED = 5, + TRIGGEREVENT_TOUCHENDED = 6, + TRIGGEREVENT_TOUCHCANCELLED = 7, +} diff --git a/samples/lua-tests/src/CocoStudioTest/CocoStudioTest.lua b/samples/lua-tests/src/CocoStudioTest/CocoStudioTest.lua new file mode 100644 index 0000000000..510d9af64a --- /dev/null +++ b/samples/lua-tests/src/CocoStudioTest/CocoStudioTest.lua @@ -0,0 +1,111 @@ +require "src/CocoStudioTest/CocoStudioGUITest/CocoStudioGUITest" +require "src/CocoStudioTest/CocoStudioSceneTest/CocoStudioSceneTest" +require "src/CocoStudioTest/CocoStudioArmatureTest/CocoStudioArmatureTest" + +local LINE_SPACE = 40 +local ITEM_TAG_BASIC = 1000 + +local cocoStudioTestItemNames = +{ + { + itemTitle = "CocoStudioArmatureTest", + testScene = function () + runArmatureTestScene() + end + }, + + { + itemTitle = "CocoStudioGUITest", + testScene = function () + runCocosGUITestScene() + end + }, + + { + itemTitle = "CocoStudioSceneTest", + testScene = function () + runCocosSceneTestScene() + end + }, +} + +local CocoStudioTestScene = class("CocoStudioTestScene") +CocoStudioTestScene.__index = CocoStudioTestScene + +function CocoStudioTestScene.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, CocoStudioTestScene) + return target +end + +function CocoStudioTestScene:runThisTest() + + --armatureSceneIdx = ArmatureTestIndex.TEST_COCOSTUDIO_WITH_SKELETON + --self:addChild(restartArmatureTest()) +end + +function CocoStudioTestScene.create() + local scene = CocoStudioTestScene.extend(cc.Scene:create()) + return scene +end + +local CocoStudioTestLayer = class("CocoStudioTestLayer") +CocoStudioTestLayer.__index = CocoStudioTestLayer + +function CocoStudioTestLayer.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, CocoStudioTestLayer) + return target +end + +function CocoStudioTestLayer.onMenuCallback(tag,sender) + local index = sender:getLocalZOrder() - ITEM_TAG_BASIC + cocoStudioTestItemNames[index].testScene() +end + +function CocoStudioTestLayer:createMenu() + + local winSize = cc.Director:getInstance():getWinSize() + + local menu = cc.Menu:create() + menu:setPosition(cc.p(0,0)) + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + + for i = 1, table.getn(cocoStudioTestItemNames) do + local menuItem = cc.MenuItemFont:create(cocoStudioTestItemNames[i].itemTitle) + menuItem:setPosition(cc.p(winSize.width / 2, winSize.height - (i + 1) * LINE_SPACE)) + menuItem:registerScriptTapHandler(CocoStudioTestLayer.onMenuCallback) + menu:addChild(menuItem, ITEM_TAG_BASIC + i) + end + + self:addChild(menu) +end + +function CocoStudioTestLayer.create() + local layer = CocoStudioTestLayer.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + end + return layer +end + +------------------------------------- +--CocoStudio Test +------------------------------------- +function CocoStudioTestMain() + local newScene = CocoStudioTestScene.create() + newScene:addChild(CreateBackMenuItem()) + newScene:addChild(CocoStudioTestLayer.create()) + newScene:runThisTest() + return newScene +end diff --git a/samples/lua-tests/src/CocosDenshionTest/CocosDenshionTest.lua b/samples/lua-tests/src/CocosDenshionTest/CocosDenshionTest.lua new file mode 100644 index 0000000000..5cc281471a --- /dev/null +++ b/samples/lua-tests/src/CocosDenshionTest/CocosDenshionTest.lua @@ -0,0 +1,210 @@ +require "AudioEngine" +local EFFECT_FILE = "effect1.wav" +local MUSIC_FILE = "background.mp3" + +local LINE_SPACE = 40 + +local function CocosDenshionTest() + local ret = cc.Layer:create() + local m_pItmeMenu = nil + local m_tBeginPos = cc.p(0, 0) + local m_nSoundId = 0 + + local testItems = { + "play background music", + "stop background music", + "pause background music", + "resume background music", + "rewind background music", + "is background music playing", + "play effect", + "play effect repeatly", + "stop effect", + "unload effect", + "add background music volume", + "sub background music volume", + "add effects volume", + "sub effects volume", + "pause effect", + "resume effect", + "pause all effects", + "resume all effects", + "stop all effects" + } + + local function menuCallback(tag, pMenuItem) + local nIdx = pMenuItem:getLocalZOrder() - 10000 + -- play background music + if nIdx == 0 then + AudioEngine.playMusic(MUSIC_FILE, true) + elseif nIdx == 1 then + -- stop background music + AudioEngine.stopMusic() + elseif nIdx == 2 then + -- pause background music + AudioEngine.pauseMusic() + elseif nIdx == 3 then + -- resume background music + AudioEngine.resumeMusic() + -- rewind background music + elseif nIdx == 4 then + AudioEngine.rewindMusic() + elseif nIdx == 5 then + -- is background music playing + if AudioEngine.isMusicPlaying () then + cclog("background music is playing") + else + cclog("background music is not playing") + end + elseif nIdx == 6 then + -- play effect + m_nSoundId = AudioEngine.playEffect(EFFECT_FILE) + elseif nIdx == 7 then + -- play effect + m_nSoundId = AudioEngine.playEffect(EFFECT_FILE, true) + elseif nIdx == 8 then + -- stop effect + AudioEngine.stopEffect(m_nSoundId) + elseif nIdx == 9 then + -- unload effect + AudioEngine.unloadEffect(EFFECT_FILE) + elseif nIdx == 10 then + -- add bakcground music volume + AudioEngine.setMusicVolume(AudioEngine.getMusicVolume() + 0.1) + elseif nIdx == 11 then + -- sub backgroud music volume + AudioEngine.setMusicVolume(AudioEngine.getMusicVolume() - 0.1) + elseif nIdx == 12 then + -- add effects volume + AudioEngine.setEffectsVolume(AudioEngine.getEffectsVolume() + 0.1) + elseif nIdx == 13 then + -- sub effects volume + AudioEngine.setEffectsVolume(AudioEngine.getEffectsVolume() - 0.1) + elseif nIdx == 14 then + AudioEngine.pauseEffect(m_nSoundId) + elseif nIdx == 15 then + AudioEngine.resumeEffect(m_nSoundId) + elseif nIdx == 16 then + AudioEngine.pauseAllEffects() + elseif nIdx == 17 then + AudioEngine.resumeAllEffects() + elseif nIdx == 18 then + AudioEngine.stopAllEffects() + end + end + -- add menu items for tests + m_pItmeMenu = cc.Menu:create() + + m_nTestCount = table.getn(testItems) + local i = 1 + for i = 1, m_nTestCount do + local label = cc.LabelTTF:create(testItems[i], "Arial", 24) + local pMenuItem = cc.MenuItemLabel:create(label) + pMenuItem:registerScriptTapHandler(menuCallback) + m_pItmeMenu:addChild(pMenuItem, i + 10000 -1) + pMenuItem:setPosition( cc.p( VisibleRect:center().x, (VisibleRect:top().y - i * LINE_SPACE) )) + end + + m_pItmeMenu:setContentSize(cc.size(VisibleRect:getVisibleRect().width, (m_nTestCount + 1) * LINE_SPACE)) + m_pItmeMenu:setPosition(cc.p(0, 0)) + ret:addChild(m_pItmeMenu) + + -- preload background music and effect + AudioEngine.preloadMusic( MUSIC_FILE ) + AudioEngine.preloadEffect( EFFECT_FILE ) + + -- set default volume + AudioEngine.setEffectsVolume(0.5) + AudioEngine.setMusicVolume(0.5) + + local function onNodeEvent(event) + if event == "enter" then + + elseif event == "exit" then + --SimpleAudioEngine:getInstance():endToLua() + end + end + + ret:registerScriptHandler(onNodeEvent) + + local prev = {x = 0, y = 0} + local function onTouchEvent(eventType, x, y) + if eventType == "began" then + prev.x = x + prev.y = y + m_tBeginPos = cc.p(x, y) + return true + elseif eventType == "moved" then + local touchLocation = cc.p(x, y) + local nMoveY = touchLocation.y - m_tBeginPos.y + local curPosX, curPosY = m_pItmeMenu:getPosition() + local curPos = cc.p(curPosX, curPosY) + local nextPos = cc.p(curPos.x, curPos.y + nMoveY) + + if nextPos.y < 0.0 then + m_pItmeMenu:setPosition(cc.p(0, 0)) + end + + if nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height) then + m_pItmeMenu:setPosition(cc.p(0, ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height))) + end + + m_pItmeMenu:setPosition(nextPos) + m_tBeginPos.x = touchLocation.x + m_tBeginPos.y = touchLocation.y + + prev.x = x + prev.y = y + end + end + + local function onTouchBegan(touch, event) + local location = touch:getLocation() + prev.x = location.x + prev.y = location.y + m_tBeginPos = location + return true + end + + local function onTouchMoved(touch, event) + local location = touch:getLocation() + local touchLocation = location + local nMoveY = touchLocation.y - m_tBeginPos.y + local curPosX, curPosY = m_pItmeMenu:getPosition() + local curPos = cc.p(curPosX, curPosY) + local nextPos = cc.p(curPos.x, curPos.y + nMoveY) + + if nextPos.y < 0.0 then + m_pItmeMenu:setPosition(cc.p(0, 0)) + end + + if nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height) then + m_pItmeMenu:setPosition(cc.p(0, ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height))) + end + + m_pItmeMenu:setPosition(nextPos) + m_tBeginPos.x = touchLocation.x + m_tBeginPos.y = touchLocation.y + + prev.x = location.x + prev.y = location.y + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:setSwallowTouches(true) + + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) + local eventDispatcher = ret:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ret) + + return ret +end + +function CocosDenshionTestMain() + cclog("CocosDenshionTestMain") + local scene = cc.Scene:create() + scene:addChild(CocosDenshionTest()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/CurrentLanguageTest/CurrentLanguageTest.lua b/samples/lua-tests/src/CurrentLanguageTest/CurrentLanguageTest.lua new file mode 100644 index 0000000000..b89f7da164 --- /dev/null +++ b/samples/lua-tests/src/CurrentLanguageTest/CurrentLanguageTest.lua @@ -0,0 +1,47 @@ +local function CurrentLanguageTest() + local ret = cc.Layer:create() + local label = cc.LabelTTF:create("Current language Test", "Arial", 28) + ret:addChild(label, 0) + label:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y-50) ) + + local labelLanguage = cc.LabelTTF:create("", "Arial", 20) + labelLanguage:setPosition(VisibleRect:center()) + + local currentLanguageType = cc.Application:getInstance():getCurrentLanguage() + + if currentLanguageType == cc.LANGUAGE_ENGLISH then + labelLanguage:setString("current language is English") + elseif currentLanguageType == cc.LANGUAGE_CHINESE then + labelLanguage:setString("current language is Chinese") + elseif currentLanguageType == cc.LANGUAGE_FRENCH then + labelLanguage:setString("current language is French") + elseif currentLanguageType == cc.LANGUAGE_GERMAN then + labelLanguage:setString("current language is German") + elseif currentLanguageType == cc.LANGUAGE_ITALIAN then + labelLanguage:setString("current language is Italian") + elseif currentLanguageType == cc.LANGUAGE_RUSSIAN then + labelLanguage:setString("current language is Russian") + elseif currentLanguageType == cc.LANGUAGE_SPANISH then + labelLanguage:setString("current language is Spanish") + elseif currentLanguageType == cc.LANGUAGE_KOREAN then + labelLanguage:setString("current language is Korean") + elseif currentLanguageType == cc.LANGUAGE_JAPANESE then + labelLanguage:setString("current language is Japanese") + elseif currentLanguageType == cc.LANGUAGE_HUNGARIAN then + labelLanguage:setString("current language is Hungarian") + elseif currentLanguageType == cc.LANGUAGE_PORTUGUESE then + labelLanguage:setString("current language is Portuguese") + elseif currentLanguageType == cc.LANGUAGE_ARABIC then + labelLanguage:setString("current language is Arabic") + end + ret:addChild(labelLanguage) + return ret +end + +function CurrentLanguageTestMain() + local scene = cc.Scene:create() + local pLayer = CurrentLanguageTest() + scene:addChild(pLayer) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/DrawPrimitivesTest/DrawPrimitivesTest.lua b/samples/lua-tests/src/DrawPrimitivesTest/DrawPrimitivesTest.lua new file mode 100644 index 0000000000..a2884fb64d --- /dev/null +++ b/samples/lua-tests/src/DrawPrimitivesTest/DrawPrimitivesTest.lua @@ -0,0 +1,237 @@ + + +local function drawPrimitivesMainLayer() + local kItemTagBasic = 1000 + local testCount = 2 + local maxCases = testCount + local curCase = 0 + local size = cc.Director:getInstance():getWinSize() + local curLayer = nil + + local function orderCallbackMenu() + local function backCallback() + curCase = curCase - 1 + if curCase < 0 then + curCase = curCase + maxCases + end + showCurrentTest() + end + + local function restartCallback() + showCurrentTest() + end + + local function nextCallback() + curCase = curCase + 1 + curCase = curCase % maxCases + showCurrentTest() + end + + local ordercallbackmenu = cc.Menu:create() + local size = cc.Director:getInstance():getWinSize() + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + item1:registerScriptTapHandler(backCallback) + ordercallbackmenu:addChild(item1,kItemTagBasic) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + item2:registerScriptTapHandler(restartCallback) + ordercallbackmenu:addChild(item2,kItemTagBasic) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + ordercallbackmenu:addChild(item3,kItemTagBasic) + item3:registerScriptTapHandler(nextCallback) + + item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + + ordercallbackmenu:setPosition(cc.p(0, 0)) + + return ordercallbackmenu + end + + local function GetTitle() + if 0 == curCase then + return "Draw primitives" + elseif 1 == curCase then + return "Test DrawNode" + end + end + + local function GetSubTitle() + if 0 == curCase then + return "Drawing Primitives by call gl funtions" + elseif 1 == curCase then + return "Testing DrawNode - batched draws. Concave polygons are BROKEN" + end + end + + local function InitTitle(layer) + --Title + local lableTitle = cc.LabelTTF:create(GetTitle(), "Arial", 40) + layer:addChild(lableTitle, 15) + lableTitle:setPosition(cc.p(size.width / 2, size.height - 32)) + lableTitle:setColor(cc.c3b(255, 255, 40)) + --SubTitle + local subLabelTitle = cc.LabelTTF:create(GetSubTitle(), "Thonburi", 16) + layer:addChild(subLabelTitle, 15) + subLabelTitle:setPosition(cc.p(size.width / 2, size.height - 80)) + end + + local function createDrawPrimitivesEffect() + local layer = cc.Layer:create() + + InitTitle(layer) + + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(size.width, size.height)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + + local function primitivesDraw() + cc.DrawPrimitives.drawLine(VisibleRect:leftBottom(), VisibleRect:rightTop() ) + + gl.lineWidth( 5.0 ) + cc.DrawPrimitives.drawColor4B(255,0,0,255) + cc.DrawPrimitives.drawLine( VisibleRect:leftTop(), VisibleRect:rightBottom() ) + + + + cc.DrawPrimitives.setPointSize(64) + cc.DrawPrimitives.drawColor4B(0, 0, 255, 128) + cc.DrawPrimitives.drawPoint(VisibleRect:center()) + + local points = {cc.p(60,60), cc.p(70,70), cc.p(60,70), cc.p(70,60) } + cc.DrawPrimitives.setPointSize(4) + cc.DrawPrimitives.drawColor4B(0,255,255,255) + cc.DrawPrimitives.drawPoints(points,4) + + gl.lineWidth(16) + cc.DrawPrimitives.drawColor4B(0, 255, 0, 255) + cc.DrawPrimitives.drawCircle( VisibleRect:center(), 100, 0, 10, false) + + gl.lineWidth(2) + cc.DrawPrimitives.drawColor4B(0, 255, 255, 255) + cc.DrawPrimitives.drawCircle( VisibleRect:center(), 50, math.pi / 2, 50, true) + + gl.lineWidth(2) + cc.DrawPrimitives.drawColor4B(255, 0, 255, 255) + cc.DrawPrimitives.drawSolidCircle( cc.p(VisibleRect:center().x + 140 ,VisibleRect:center().y), 40, math.rad(90), 50, 1.0, 1.0) + + gl.lineWidth(10) + cc.DrawPrimitives.drawColor4B(255, 255, 0, 255) + local yellowPoints = { cc.p(0,0), cc.p(50,50), cc.p(100,50), cc.p(100,100), cc.p(50,100)} + cc.DrawPrimitives.drawPoly( yellowPoints, 5, false) + + gl.lineWidth(1) + local filledVertices = { cc.p(0,120), cc.p(50,120), cc.p(50,170), cc.p(25,200), cc.p(0,170) } + cc.DrawPrimitives.drawSolidPoly(filledVertices, 5, cc.c4f(0.5, 0.5, 1, 1)) + + gl.lineWidth(2) + cc.DrawPrimitives.drawColor4B(255, 0, 255, 255) + local closePoints= { cc.p(30,130), cc.p(30,230), cc.p(50,200) } + cc.DrawPrimitives.drawPoly( closePoints, 3, true) + + cc.DrawPrimitives.drawQuadBezier(VisibleRect:leftTop(), VisibleRect:center(), VisibleRect:rightTop(), 50) + + cc.DrawPrimitives.drawCubicBezier(VisibleRect:center(), cc.p(VisibleRect:center().x + 30, VisibleRect:center().y + 50), cc.p(VisibleRect:center().x + 60,VisibleRect:center().y - 50), VisibleRect:right(), 100) + + local solidvertices = {cc.p(60,160), cc.p(70,190), cc.p(100,190), cc.p(90,160)} + cc.DrawPrimitives.drawSolidPoly( solidvertices, 4, cc.c4f(1, 1, 0, 1) ) + + local array = { + cc.p(0, 0), + cc.p(size.width / 2 - 30, 0), + cc.p(size.width / 2 - 30, size.height - 80), + cc.p(0, size.height - 80), + cc.p(0, 0), + } + cc.DrawPrimitives.drawCatmullRom( array, 5) + + cc.DrawPrimitives.drawCardinalSpline( array, 0,100) + + gl.lineWidth(1) + cc.DrawPrimitives.drawColor4B(255,255,255,255) + cc.DrawPrimitives.setPointSize(1) + end + + glNode:registerScriptDrawHandler(primitivesDraw) + layer:addChild(glNode,-10) + glNode:setPosition( size.width / 2, size.height / 2) + + return layer + end + + local function createDrawNodeTest() + local layer = cc.Layer:create() + + InitTitle(layer) + + local draw = cc.DrawNode:create() + layer:addChild(draw, 10) + + --Draw 10 circles + for i=1, 10 do + draw:drawDot(cc.p(size.width/2, size.height/2), 10*(10-i), cc.c4f(math.random(0,1), math.random(0,1), math.random(0,1), 1)) + end + + --Draw polygons + points = { cc.p(size.height/4, 0), cc.p(size.width, size.height / 5), cc.p(size.width / 3 * 2, size.height) } + draw:drawPolygon(points, table.getn(points), cc.c4f(1,0,0,0.5), 4, cc.c4f(0,0,1,1)) + + local o = 80 + local w = 20 + local h = 50 + local star1 = { cc.p( o + w, o - h), cc.p(o + w * 2, o), cc.p(o + w * 2 + h, o + w), cc.p(o + w * 2, o + w * 2) } + + draw:drawPolygon(star1, table.getn(star1), cc.c4f(1,0,0,0.5), 1, cc.c4f(0,0,1,1)) + + o = 180 + w = 20 + h = 50 + local star2 = { + cc.p(o, o), cc.p(o + w, o - h), cc.p(o + w * 2, o), --lower spike + cc.p(o + w * 2 + h, o + w ), cc.p(o + w * 2, o + w * 2), --right spike + cc.p(o + w, o + w * 2 + h), cc.p(o, o + w * 2), --top spike + cc.p(o - h, o + w), --left spike + }; + + draw:drawPolygon(star2, table.getn(star2), cc.c4f(1,0,0,0.5), 1, cc.c4f(0,0,1,1)) + + draw:drawSegment(cc.p(20,size.height), cc.p(20,size.height/2), 10, cc.c4f(0, 1, 0, 1)) + + draw:drawSegment(cc.p(10,size.height/2), cc.p(size.width/2, size.height/2), 40, cc.c4f(1, 0, 1, 0.5)) + + return layer + end + + local function createLayerByCurCase(curCase) + if 0 == curCase then + return createDrawPrimitivesEffect() + elseif 1 == curCase then + return createDrawNodeTest() + end + end + + function showCurrentTest() + local curScene = cc.Scene:create() + if nil ~= curScene then + curLayer = createLayerByCurCase(curCase) + if nil ~= curLayer then + curScene:addChild(curLayer) + curLayer:addChild(orderCallbackMenu(),15) + curScene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene(curScene) + end + end + end + + curLayer = createLayerByCurCase(curCase) + curLayer:addChild(orderCallbackMenu(),15) + return curLayer +end + +function DrawPrimitivesTest() + local scene = cc.Scene:create() + scene:addChild(drawPrimitivesMainLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end + diff --git a/samples/lua-tests/src/EffectsAdvancedTest/EffectsAdvancedTest.lua b/samples/lua-tests/src/EffectsAdvancedTest/EffectsAdvancedTest.lua new file mode 100644 index 0000000000..863044cd94 --- /dev/null +++ b/samples/lua-tests/src/EffectsAdvancedTest/EffectsAdvancedTest.lua @@ -0,0 +1,263 @@ +local kTagTextLayer = 1 +local kTagSprite1 = 1 +local kTagSprite2 = 2 +local kTagBackground = 1 +local kTagLabel = 2 + +local originCreateLayer = createTestLayer + +local function createTestLayer(title, subtitle) + local ret = originCreateLayer(title, subtitle) + local bgNode = cc.NodeGrid:create() + bgNode:setAnchorPoint(cc.p(0.5,0.5)) + ret:addChild(bgNode,0,kTagBackground) + + local bg = cc.Sprite:create("Images/background3.png") + bg:setPosition( VisibleRect:center()) + bgNode:addChild(bg) + + local target1 = cc.NodeGrid:create() + target1:setAnchorPoint(cc.p(0.5,0.5)) + local grossini = cc.Sprite:create("Images/grossinis_sister2.png") + target1:addChild(grossini) + bgNode:addChild(target1,1,kTagSprite1) + target1:setPosition(cc.p(VisibleRect:left().x+VisibleRect:getVisibleRect().width/3.0, VisibleRect:bottom().y+ 200) ) + local sc = cc.ScaleBy:create(2, 5) + local sc_back = sc:reverse() + target1:runAction( cc.RepeatForever:create(cc.Sequence:create(sc, sc_back))) + + local target2 = cc.NodeGrid:create() + target2:setAnchorPoint(cc.p(0.5,0.5)) + local tamara = cc.Sprite:create("Images/grossinis_sister1.png") + target2:addChild(tamara) + bgNode:addChild(target2,1,kTagSprite2) + target2:setPosition( cc.p(VisibleRect:left().x+2*VisibleRect:getVisibleRect().width/3.0,VisibleRect:bottom().y+200) ) + local sc2 = cc.ScaleBy:create(2, 5) + local sc2_back = sc2:reverse() + target2:runAction( cc.RepeatForever:create(cc.Sequence:create(sc2, sc2_back))) + + return ret +end + + +-------------------------------------------------------------------- +-- +-- Effect1 +-- +-------------------------------------------------------------------- +local function Effect1() + local ret = createTestLayer("Lens + Waves3d and OrbitCamera") + local target = ret:getChildByTag(kTagBackground) + + -- To reuse a grid the grid size and the grid type must be the same. + -- in this case: + -- Lens3D is Grid3D and it's size is (15,10) + -- Waves3D is Grid3D and it's size is (15,10) + + local size = cc.Director:getInstance():getWinSize() + local lens = cc.Lens3D:create(0.0, cc.size(15,10), cc.p(size.width/2,size.height/2), 240) + local waves = cc.Waves3D:create(10, cc.size(15,10), 18, 15) + + local reuse = cc.ReuseGrid:create(1) + local delay = cc.DelayTime:create(8) + + -- local orbit = cc.OrbitCamera:create(5, 1, 2, 0, 180, 0, -90) + -- local orbit_back = orbit:reverse() + -- target:runAction( cc.RepeatForever:create( cc.Sequence:create(orbit, orbit_back))) + target:runAction( cc.Sequence:create(lens, delay, reuse, waves)) + return ret +end + +-------------------------------------------------------------------- +-- +-- Effect2 +-- +-------------------------------------------------------------------- +local function Effect2() + local ret = createTestLayer("ShakyTiles + ShuffleTiles + TurnOffTiles") + local target = ret:getChildByTag(kTagBackground) + + -- To reuse a grid the grid size and the grid type must be the same. + -- in this case: + -- ShakyTiles is TiledGrid3D and it's size is (15,10) + -- Shuffletiles is TiledGrid3D and it's size is (15,10) + -- TurnOfftiles is TiledGrid3D and it's size is (15,10) + local shaky = cc.ShakyTiles3D:create(5, cc.size(15,10), 4, false) + local shuffle = cc.ShuffleTiles:create(0, cc.size(15,10), 3) + local turnoff = cc.TurnOffTiles:create(0, cc.size(15,10), 3) + local turnon = turnoff:reverse() + + -- reuse 2 times: + -- 1 for shuffle + -- 2 for turn off + -- turnon tiles will use a new grid + local reuse = cc.ReuseGrid:create(2) + + local delay = cc.DelayTime:create(1) + + target:runAction(cc.Sequence:create(shaky, delay ,reuse, shuffle, tolua.cast(delay:clone(), "cc.Action"), turnoff, turnon)) + return ret +end + +-------------------------------------------------------------------- +-- +-- Effect3 +-- +-------------------------------------------------------------------- +local function Effect3() + local ret = createTestLayer("Effects on 2 sprites") + local bg = ret:getChildByTag(kTagBackground) + local target1 = bg:getChildByTag(kTagSprite1) + local target2 = bg:getChildByTag(kTagSprite2) + + local waves = cc.Waves:create(5, cc.size(15,10), 5, 20, true, false) + local shaky = cc.Shaky3D:create(5, cc.size(15,10), 4, false) + + target1:runAction( cc.RepeatForever:create( waves ) ) + target2:runAction( cc.RepeatForever:create( shaky ) ) + + -- moving background. Testing issue #244 + local move = cc.MoveBy:create(3, cc.p(200,0) ) + bg:runAction(cc.RepeatForever:create( cc.Sequence:create(move, move:reverse()))) + return ret +end + +-------------------------------------------------------------------- +-- +-- Effect4 +-- +-------------------------------------------------------------------- + +-- class Lens3DTarget : public cc.Node + +-- public: +-- virtual void setPosition(const CCPoint& var) + +-- m_pLens3D:setPosition(var) +-- end + +-- virtual const CCPoint& getPosition() + +-- return m_pLens3D:getPosition() +-- end + +-- static Lens3DTarget* create(cc.Lens3D* pAction) + +-- Lens3DTarget* pRet = new Lens3DTarget() +-- pRet:m_pLens3D = pAction +-- pRet:autorelease() +-- return pRet +-- end +-- private: + +-- Lens3DTarget() +-- : m_pLens3D(NULL) +-- {} + +-- cc.Lens3D* m_pLens3D +-- end + +local function Effect4() + local ret = createTestLayer("Jumpy Lens3D") + local lens = cc.Lens3D:create(10, cc.size(32,24), cc.p(100,180), 150) + local move = cc.JumpBy:create(5, cc.p(380,0), 100, 4) + local move_back = move:reverse() + local seq = cc.Sequence:create( move, move_back) + + -- /* In cocos2d-iphone, the type of action's target is 'id', so it supports using the instance of 'cc.Lens3D' as its target. + -- While in cocos2d-x, the target of action only supports cc.Node or its subclass, + -- so we make an encapsulation for cc.Lens3D to achieve that. + -- */ + + local director = cc.Director:getInstance() + -- local pTarget = Lens3DTarget:create(lens) + -- -- Please make sure the target been added to its parent. + -- ret:addChild(pTarget) + + -- director:getActionManager():addAction(seq, pTarget, false) + local bg = ret:getChildByTag(kTagBackground) + bg:runAction( lens ) + return ret +end + +-------------------------------------------------------------------- +-- +-- Effect5 +-- +-------------------------------------------------------------------- +local function Effect5() + local ret = createTestLayer("Test Stop-Copy-Restar") + + local effect = cc.Liquid:create(2, cc.size(32,24), 1, 20) + + local stopEffect = cc.Sequence:create(effect, cc.DelayTime:create(2), cc.StopGrid:create()) + local bg = ret:getChildByTag(kTagBackground) + bg:runAction(stopEffect) + local function onNodeEvent(event) + if event == "exit" then + cc.Director:getInstance():setProjection(cc.DIRECTOR_PROJECTION_3D) + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +-------------------------------------------------------------------- +-- +-- Issue631 +-- +-------------------------------------------------------------------- +local function Issue631() + local ret = createTestLayer("Testing Opacity", + "Effect image should be 100% opaque. Testing issue #631") + + local effect = cc.Sequence:create(cc.DelayTime:create(2.0),cc.Shaky3D:create(5.0, cc.size(5, 5), 16, false)) + + -- cleanup + local bg = ret:getChildByTag(kTagBackground) + ret:removeChild(bg, true) + + -- background + local layer = cc.LayerColor:create( cc.c4b(255,0,0,255) ) + ret:addChild(layer, -10) + local sprite = cc.Sprite:create("Images/grossini.png") + sprite:setPosition( cc.p(50,80) ) + layer:addChild(sprite, 10) + + -- foreground + local layer2BaseGrid = cc.NodeGrid:create() + local layer2 = cc.LayerColor:create(cc.c4b( 0, 255,0,255 ) ) + local fog = cc.Sprite:create("Images/Fog.png") + + --local bf = BlendFunc() + --bf.src = GL_SRC_ALPHA + --bf.dst = GL_ONE_MINUS_SRC_ALPHA + + fog:setBlendFunc(gl.SRC_ALPHA , gl.ONE_MINUS_SRC_ALPHA ) + layer2:addChild(fog, 1) + ret:addChild(layer2BaseGrid, 1) + layer2BaseGrid:addChild(layer2) + + layer2BaseGrid:runAction( cc.RepeatForever:create(effect) ) + return ret +end + +function EffectAdvancedTestMain() + cclog("EffectAdvancedTestMain") + Helper.index = 1 + + local scene = cc.Scene:create() + Helper.createFunctionTable = { + Effect3, + Effect2, + Effect1, + Effect4, + Effect5, + Issue631 + } + scene:addChild(Effect3()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/EffectsTest/EffectsName.lua b/samples/lua-tests/src/EffectsTest/EffectsName.lua new file mode 100644 index 0000000000..ffc3857058 --- /dev/null +++ b/samples/lua-tests/src/EffectsTest/EffectsName.lua @@ -0,0 +1,26 @@ + +EffectsList = +{ + [0] = "Shaky3D", + "Waves3D", + "FlipX3D", + "FlipY3D", + "Lens3D", + "Ripple3D", + "Liquid", + "Waves", + "Twirl", + "ShakyTiles3D", + "ShatteredTiles3D", + "ShuffleTiles", + "FadeOutTRTiles", + "FadeOutBLTiles", + "FadeOutUpTiles", + "FadeOutDownTiles", + "TurnOffTiles", + "WavesTiles3D", + "JumpTiles3D", + "SplitRows", + "SplitCols", + "PageTurn3D", +} diff --git a/samples/lua-tests/src/EffectsTest/EffectsTest.lua b/samples/lua-tests/src/EffectsTest/EffectsTest.lua new file mode 100644 index 0000000000..3696df33ee --- /dev/null +++ b/samples/lua-tests/src/EffectsTest/EffectsTest.lua @@ -0,0 +1,392 @@ +require "src/EffectsTest/EffectsName" + + +local ActionIdx = -1 +local size = cc.Director:getInstance():getWinSize() +local kTagTextLayer = 1 +local kTagBackground = 1 +local kTagLabel = 2 +local MAX_LAYER = 22 + +local testLayer = nil +local titleLabel = nil +local entry = nil +local gridNodeTarget = nil + +local function checkAnim(dt) + + if nil == gridNodeTarget then + return + end + + if gridNodeTarget:getNumberOfRunningActions() == 0 and nil ~=gridNodeTarget:getGrid() then + gridNodeTarget:setGrid(nil) + end +end + +local function onEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + entry = scheduler:scheduleScriptFunc(checkAnim, 0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(entry) + end +end + +local function backAction() + ActionIdx = ActionIdx - 1 + if ActionIdx < 0 then + ActionIdx = ActionIdx + MAX_LAYER + end + + return CreateEffectsTestLayer() +end + +local function restartAction() + return CreateEffectsTestLayer() +end + +local function nextAction() + ActionIdx = ActionIdx + 1 + ActionIdx = math.mod(ActionIdx, MAX_LAYER) + + return CreateEffectsTestLayer() +end + +local function backCallback(sender) + local scene = cc.Scene:create() + + scene:addChild(backAction()) + scene:addChild(CreateBackMenuItem()) + + cc.Director:getInstance():replaceScene(scene) +end + +local function restartCallback(sender) + local scene = cc.Scene:create() + + scene:addChild(restartAction()) + scene:addChild(CreateBackMenuItem()) + + cc.Director:getInstance():replaceScene(scene) +end + +local function nextCallback(sender) + local scene = cc.Scene:create() + + scene:addChild(nextAction()) + scene:addChild(CreateBackMenuItem()) + + cc.Director:getInstance():replaceScene(scene) +end + +-------------------------------------- +-- Shaky3DDemo +-------------------------------------- +local function Shaky3DDemo(t) + return cc.Shaky3D:create(t, cc.size(15,10), 5, false) +end + +-------------------------------------- +-- Waves3DDemo +-------------------------------------- +local function Waves3DDemo(t) + return cc.Waves3D:create(t, cc.size(15,10), 5, 40) +end + +-------------------------------------- +-- FlipX3DDemo +-------------------------------------- +local function FlipX3DDemo(t) + local flipx = cc.FlipX3D:create(t) + local flipx_back = flipx:reverse() + local delay = cc.DelayTime:create(2) + + return cc.Sequence:create(flipx, flipx_back, delay) +end + +-------------------------------------- +-- FlipY3DDemo +-------------------------------------- +local function FlipY3DDemo(t) + local flipy = cc.FlipY3D:create(t) + local flipy_back = flipy:reverse() + local delay = cc.DelayTime:create(2) + + return cc.Sequence:create(flipy, flipy_back, delay) +end + +-------------------------------------- +-- Lens3DDemo +-------------------------------------- +local function Lens3DDemo(t) + return cc.Lens3D:create(t, cc.size(15,10), cc.p(size.width/2,size.height/2), 240) +end + +-------------------------------------- +-- Ripple3DDemo +-------------------------------------- +local function Ripple3DDemo(t) + return cc.Ripple3D:create(t, cc.size(32,24), cc.p(size.width/2,size.height/2), 240, 4, 160) +end + +-------------------------------------- +-- LiquidDemo +-------------------------------------- +local function LiquidDemo(t) + return cc.Liquid:create(t, cc.size(16,12), 4, 20) +end + +-------------------------------------- +-- WavesDemo +-------------------------------------- +local function WavesDemo(t) + return cc.Waves:create(t, cc.size(16,12), 4, 20, true, true) +end + +-------------------------------------- +-- TwirlDemo +-------------------------------------- +local function TwirlDemo(t) + return cc.Twirl:create(t, cc.size(12,8), cc.p(size.width/2, size.height/2), 1, 2.5) +end + +-------------------------------------- +-- ShakyTiles3DDemo +-------------------------------------- +local function ShakyTiles3DDemo(t) + return cc.ShakyTiles3D:create(t, cc.size(16,12), 5, false) +end + +-------------------------------------- +-- ShatteredTiles3DDemo +-------------------------------------- +local function ShatteredTiles3DDemo(t) + return cc.ShatteredTiles3D:create(t, cc.size(16,12), 5, false) +end + +-------------------------------------- +-- ShuffleTilesDemo +-------------------------------------- +local function ShuffleTilesDemo(t) + local shuffle = cc.ShuffleTiles:create(t, cc.size(16,12), 25) + local shuffle_back = shuffle:reverse() + local delay = cc.DelayTime:create(2) + + return cc.Sequence:create(shuffle, shuffle_back, delay) +end + +-------------------------------------- +-- FadeOutTRTilesDemo +-------------------------------------- +local function FadeOutTRTilesDemo(t) + local fadeout = cc.FadeOutTRTiles:create(t, cc.size(16,12)) + local back = fadeout:reverse() + local delay = cc.DelayTime:create(0.5) + + return cc.Sequence:create(fadeout, back, delay) +end + +-------------------------------------- +-- FadeOutBLTilesDemo +-------------------------------------- +local function FadeOutBLTilesDemo(t) + local fadeout = cc.FadeOutBLTiles:create(t, cc.size(16,12)) + local back = fadeout:reverse() + local delay = cc.DelayTime:create(0.5) + + return cc.Sequence:create(fadeout, back, delay) +end + +-------------------------------------- +-- FadeOutUpTilesDemo +-------------------------------------- +local function FadeOutUpTilesDemo(t) + local fadeout = cc.FadeOutUpTiles:create(t, cc.size(16,12)) + local back = fadeout:reverse() + local delay = cc.DelayTime:create(0.5) + + return cc.Sequence:create(fadeout, back, delay) +end + +-------------------------------------- +-- FadeOutDownTilesDemo +-------------------------------------- +local function FadeOutDownTilesDemo(t) + local fadeout = cc.FadeOutDownTiles:create(t, cc.size(16,12)) + local back = fadeout:reverse() + local delay = cc.DelayTime:create(0.5) + + return cc.Sequence:create(fadeout, back, delay) +end + +-------------------------------------- +-- TurnOffTilesDemo +-------------------------------------- +local function TurnOffTilesDemo(t) + local fadeout = cc.TurnOffTiles:create(t, cc.size(48,32), 25) + local back = fadeout:reverse() + local delay = cc.DelayTime:create(0.5) + + return cc.Sequence:create(fadeout, back, delay) +end + +-------------------------------------- +-- WavesTiles3DDemo +-------------------------------------- +local function WavesTiles3DDemo(t) + return cc.WavesTiles3D:create(t, cc.size(15,10), 4, 120) +end + +-------------------------------------- +-- JumpTiles3DDemo +-------------------------------------- +local function JumpTiles3DDemo(t) + return cc.JumpTiles3D:create(t, cc.size(15,10), 2, 30) +end + +-------------------------------------- +-- SplitRowsDemo +-------------------------------------- +local function SplitRowsDemo(t) + return cc.SplitRows:create(t, 9) +end + +-------------------------------------- +-- SplitColsDemo +-------------------------------------- +local function SplitColsDemo(t) + return cc.SplitCols:create(t, 9) +end + +-------------------------------------- +-- PageTurn3DDemo +-------------------------------------- +local function PageTurn3DDemo(t) + cc.Director:getInstance():setDepthTest(true) + return cc.PageTurn3D:create(t, cc.size(15,10)) +end + +-------------------------------------- +-- Effects Test +-------------------------------------- +local function createEffect(idx, t) + cc.Director:getInstance():setDepthTest(false) + local action = nil + + if idx == 0 then + action = Shaky3DDemo(t) + elseif idx == 1 then + action = Waves3DDemo(t) + elseif idx == 2 then + action = FlipX3DDemo(t) + elseif idx == 3 then + action = FlipY3DDemo(t) + elseif idx == 4 then + action = Lens3DDemo(t) + elseif idx == 5 then + action = Ripple3DDemo(t) + elseif idx == 6 then + action = LiquidDemo(t) + elseif idx == 7 then + action = WavesDemo(t) + elseif idx == 8 then + action = TwirlDemo(t) + elseif idx == 9 then + action = ShakyTiles3DDemo(t) + elseif idx == 10 then + action = ShatteredTiles3DDemo(t) + elseif idx == 11 then + action = ShuffleTilesDemo(t) + elseif idx == 12 then + action = FadeOutTRTilesDemo(t) + elseif idx == 13 then + action = FadeOutBLTilesDemo(t) + elseif idx == 14 then + action = FadeOutUpTilesDemo(t) + elseif idx == 15 then + action = FadeOutDownTilesDemo(t) + elseif idx == 16 then + action = TurnOffTilesDemo(t) + elseif idx == 17 then + action = WavesTiles3DDemo(t) + elseif idx == 18 then + action = JumpTiles3DDemo(t) + elseif idx == 19 then + action = SplitRowsDemo(t) + elseif idx == 20 then + action = SplitColsDemo(t) + elseif idx == 21 then + action = PageTurn3DDemo(t) + end + + return action +end + +function CreateEffectsTestLayer() + testLayer = cc.LayerColor:create(cc.c4b(32,128,32,255)) + + + gridNodeTarget = cc.NodeGrid:create() + local effect = createEffect(ActionIdx, 3) + gridNodeTarget:runAction(effect) + testLayer:addChild(gridNodeTarget, 0, kTagBackground) + + local bg = cc.Sprite:create(s_back3) + gridNodeTarget:addChild(bg, 0) + bg:setPosition(VisibleRect:center()) + + local grossini = cc.Sprite:create(s_pPathSister2) + gridNodeTarget:addChild(grossini, 1) + grossini:setPosition( cc.p(VisibleRect:left().x+VisibleRect:getVisibleRect().width/3,VisibleRect:center().y) ) + local sc = cc.ScaleBy:create(2, 5) + local sc_back = sc:reverse() + grossini:runAction( cc.RepeatForever:create(cc.Sequence:create(sc, sc_back) ) ) + + local tamara = cc.Sprite:create(s_pPathSister1) + gridNodeTarget:addChild(tamara, 1) + tamara:setPosition( cc.p(VisibleRect:left().x+2*VisibleRect:getVisibleRect().width/3,VisibleRect:center().y) ) + local sc2 = cc.ScaleBy:create(2, 5) + local sc2_back = sc2:reverse() + tamara:runAction( cc.RepeatForever:create(cc.Sequence:create(sc2, sc2_back)) ) + + local x, y = size.width, size.height + + titleLabel = cc.LabelTTF:create(EffectsList[ActionIdx], "Marker Felt", 32) + titleLabel:setPosition(x / 2, y - 80) + testLayer:addChild(titleLabel) + titleLabel:setTag(kTagLabel) + + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + item1:registerScriptTapHandler(backCallback) + item2:registerScriptTapHandler(restartCallback) + item3:registerScriptTapHandler(nextCallback) + + local menu = cc.Menu:create() + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + + menu:setPosition(cc.p(0, 0)) + item1:setPosition(cc.p(size.width/2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(size.width/2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(size.width/2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + + testLayer:addChild(menu, 1) + + testLayer:registerScriptHandler(onEnterOrExit) + + return testLayer +end + +function EffectsTest() + local scene = cc.Scene:create() + + ActionIdx = -1 + scene:addChild(nextAction()) + scene:addChild(CreateBackMenuItem()) + + return scene +end diff --git a/samples/lua-tests/src/ExtensionTest/CocosBuilderTest.lua b/samples/lua-tests/src/ExtensionTest/CocosBuilderTest.lua new file mode 100644 index 0000000000..590ae907b6 --- /dev/null +++ b/samples/lua-tests/src/ExtensionTest/CocosBuilderTest.lua @@ -0,0 +1,319 @@ + +require "CCBReaderLoad" + +HelloCocosBuilderLayer = HelloCocosBuilderLayer or {} +ccb["HelloCocosBuilderLayer"] = HelloCocosBuilderLayer + +TestMenusLayer = TestMenusLayer or {} +ccb["TestMenusLayer"] = TestMenusLayer + +TestButtonsLayer = TestButtonsLayer or {} +ccb["TestButtonsLayer"] = TestButtonsLayer + +TestHeaderLayer = TestHeaderLayer or {} +ccb["TestHeaderLayer"] = TestHeaderLayer + +TestSpritesLayer = TestSpritesLayer or {} +ccb["TestSpritesLayer"] = TestSpritesLayer + +TestParticleSystemsLayer = TestParticleSystemsLayer or {} +ccb["TestParticleSystemsLayer"] = TestParticleSystemsLayer + +TestAnimationsLayer = TestAnimationsLayer or {} +ccb["TestAnimationsLayer"] = TestAnimationsLayer + +TestTimelineLayer = TestTimelineLayer or {} +ccb["TestTimelineLayer"] = TestTimelineLayer + +TestScrollViewsLayer = TestScrollViewsLayer or {} +ccb["TestScrollViewsLayer"] = TestScrollViewsLayer + +local function onMenuItemAClicked() + if nil ~= TestMenusLayer["mMenuItemStatusLabelBMFont"] then + local labelBmFt = tolua.cast(TestMenusLayer["mMenuItemStatusLabelBMFont"],"cc.LabelBMFont") + if nil ~= labelBmFt then + labelBmFt:setString("Menu Item A clicked."); + end + end +end + +local function onMenuItemBClicked() + if nil ~= TestMenusLayer["mMenuItemStatusLabelBMFont"] then + local labelBmFt = tolua.cast(TestMenusLayer["mMenuItemStatusLabelBMFont"],"cc.LabelBMFont") + if nil ~= labelBmFt then + labelBmFt:setString("Menu Item B clicked."); + end + end +end + +local function pressedC( ... ) + if nil ~= TestMenusLayer["mMenuItemStatusLabelBMFont"] then + local labelBmFt = tolua.cast(TestMenusLayer["mMenuItemStatusLabelBMFont"],"cc.LabelBMFont") + if nil ~= labelBmFt then + labelBmFt:setString("Menu Item C clicked."); + end + end +end +local function onMenuTestClicked() + cclog("CCBMenuTest"); + local scene = cc.Scene:create() + local proxy = cc.CCBProxy:create() + local node = CCBReaderLoad("cocosbuilderRes/ccb/ccb/TestMenus.ccbi",proxy,HelloCocosBuilderLayer) + local layer = tolua.cast(node,"cc.Layer") + if nil ~= HelloCocosBuilderLayer["mTestTitleLabelTTF"] then + local ccLabelTTF = tolua.cast(HelloCocosBuilderLayer["mTestTitleLabelTTF"],"cc.LabelTTF") + if nil ~= ccLabelTTF then + ccLabelTTF:setString("ccb/ccb/TestMenus.ccbi") + end + end + if nil ~= scene then + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():pushScene(cc.TransitionFade:create(0.5, scene, cc.c3b(0,0,0))); + end +end + +TestMenusLayer["onMenuItemAClicked"] = onMenuItemAClicked +TestMenusLayer["onMenuItemBClicked"] = onMenuItemBClicked +TestMenusLayer["pressedC:"] = pressedC + +local function onBackClicked() + cc.Director:getInstance():popScene(); +end + +TestHeaderLayer["onBackClicked"] = onBackClicked + +local function onSpriteTestClicked() + cclog("CCBSpriteTest"); + local scene = cc.Scene:create() + local proxy = cc.CCBProxy:create() + local node = CCBReaderLoad("cocosbuilderRes/ccb/ccb/TestSprites.ccbi",proxy,HelloCocosBuilderLayer) + local layer = tolua.cast(node,"cc.Layer") + if nil ~= HelloCocosBuilderLayer["mTestTitleLabelTTF"] then + local ccLabelTTF = tolua.cast(HelloCocosBuilderLayer["mTestTitleLabelTTF"],"cc.LabelTTF") + if nil ~= ccLabelTTF then + ccLabelTTF:setString("ccb/ccb/TestSprites.ccbi") + end + end + if nil ~= scene then + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():pushScene(cc.TransitionFade:create(0.5, scene, cc.c3b(0,0,0))); + end +end + +local function onButtonTestClicked() + cclog("CCBButtionTest"); + local scene = cc.Scene:create() + local proxy = cc.CCBProxy:create() + local node = CCBReaderLoad("cocosbuilderRes/ccb/ccb/TestButtons.ccbi",proxy,HelloCocosBuilderLayer) + local layer = tolua.cast(node,"cc.Layer") + if nil ~= HelloCocosBuilderLayer["mTestTitleLabelTTF"] then + local ccLabelTTF = tolua.cast(HelloCocosBuilderLayer["mTestTitleLabelTTF"],"cc.LabelTTF") + if nil ~= ccLabelTTF then + ccLabelTTF:setString("ccb/ccb/TestButtons.ccbi") + end + end + if nil ~= scene then + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():pushScene(cc.TransitionFade:create(0.5, scene, cc.c3b(0,0,0))); + end +end + +local function onCCControlButtonClicked(sender,controlEvent) + local labelTTF = tolua.cast(TestButtonsLayer["mCCControlEventLabel"],"cc.LabelBMFont") + + if nil == labelTTF then + return + end + + if controlEvent == cc.CONTROL_EVENTTYPE_TOUCH_DOWN then + labelTTF:setString("Touch Down.") + elseif controlEvent == cc.CONTROL_EVENTTYPE_DRAG_INSIDE then + labelTTF:setString("Touch Drag Inside.") + elseif controlEvent == cc.CONTROL_EVENTTYPE_DRAG_OUTSIDE then + labelTTF:setString("Touch Drag Outside.") + elseif controlEvent == cc.CONTROL_EVENTTYPE_DRAG_ENTER then + labelTTF:setString("Touch Drag Enter.") + elseif controlEvent == cc.CONTROL_EVENTTYPE_DRAG_EXIT then + labelTTF:setString("Touch Drag Exit.") + elseif controlEvent == cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE then + labelTTF:setString("Touch Up Inside.") + elseif controlEvent == cc.CONTROL_EVENTTYPE_TOUCH_UP_OUTSIDE then + labelTTF:setString("Touch Up Outside.") + elseif controlEvent == cc.CONTROL_EVENTTYPE_TOUCH_CANCEL then + labelTTF:setString("Touch Cancel.") + elseif controlEvent == cc.CONTROL_EVENT_VALUECHANGED then + labelTTF:setString("Value Changed.") + end +end + +TestButtonsLayer["onCCControlButtonClicked"] = onCCControlButtonClicked + + + +local function onAnimationsTestClicked() + cclog("CCBAnimationsTestTest"); + local scene = cc.Scene:create() + local proxy = cc.CCBProxy:create() + local node = CCBReaderLoad("cocosbuilderRes/ccb/ccb/TestAnimations.ccbi",proxy,HelloCocosBuilderLayer) + local layer = tolua.cast(node,"cc.Layer") + if nil ~= HelloCocosBuilderLayer["mTestTitleLabelTTF"] then + local ccLabelTTF = tolua.cast(HelloCocosBuilderLayer["mTestTitleLabelTTF"],"cc.LabelTTF") + if nil ~= ccLabelTTF then + ccLabelTTF:setString("ccb/ccb/TestAnimations.ccbi") + end + end + if nil ~= scene then + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():pushScene(cc.TransitionFade:create(0.5, scene, cc.c3b(0,0,0))); + end +end + +local function onParticleSystemTestClicked() + cclog("CCBParticleSystemTest"); + local scene = cc.Scene:create() + local proxy = cc.CCBProxy:create() + local node = CCBReaderLoad("cocosbuilderRes/ccb/ccb/TestParticleSystems.ccbi",proxy,HelloCocosBuilderLayer) + local layer = tolua.cast(node,"cc.Layer") + if nil ~= HelloCocosBuilderLayer["mTestTitleLabelTTF"] then + local ccLabelTTF = tolua.cast(HelloCocosBuilderLayer["mTestTitleLabelTTF"],"cc.LabelTTF") + if nil ~= ccLabelTTF then + ccLabelTTF:setString("ccb/ccb/TestParticleSystems.ccbi") + end + end + if nil ~= scene then + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():pushScene(cc.TransitionFade:create(0.5, scene, cc.c3b(0,0,0))); + end +end + +local function onCCControlButtonIdleClicked() + if nil ~= TestAnimationsLayer["mAnimationManager"] then + local animationMgr = tolua.cast(TestAnimationsLayer["mAnimationManager"],"cc.CCBAnimationManager") + if nil ~= animationMgr then + animationMgr:runAnimationsForSequenceNamedTweenDuration("Idle", 0.3) + end + end +end + +local function onCCControlButtonWaveClicked() + if nil ~= TestAnimationsLayer["mAnimationManager"] then + local animationMgr = tolua.cast(TestAnimationsLayer["mAnimationManager"],"cc.CCBAnimationManager") + if nil ~= animationMgr then + animationMgr:runAnimationsForSequenceNamedTweenDuration("Wave", 0.3) + end + end +end + +local function onCCControlButtonJumpClicked() + if nil ~= TestAnimationsLayer["mAnimationManager"] then + local animationMgr = tolua.cast(TestAnimationsLayer["mAnimationManager"],"cc.CCBAnimationManager") + if nil ~= animationMgr then + animationMgr:runAnimationsForSequenceNamedTweenDuration("Jump", 0.3) + end + end +end + +local function onCCControlButtonFunkyClicked() + if nil ~= TestAnimationsLayer["mAnimationManager"] then + local animationMgr = tolua.cast(TestAnimationsLayer["mAnimationManager"],"cc.CCBAnimationManager") + if nil ~= animationMgr then + animationMgr:runAnimationsForSequenceNamedTweenDuration("Funky", 0.3) + end + end +end + +TestAnimationsLayer["onCCControlButtonIdleClicked"] = onCCControlButtonIdleClicked +TestAnimationsLayer["onCCControlButtonWaveClicked"] = onCCControlButtonWaveClicked +TestAnimationsLayer["onCCControlButtonJumpClicked"] = onCCControlButtonJumpClicked +TestAnimationsLayer["onCCControlButtonFunkyClicked"] = onCCControlButtonFunkyClicked + + +local function onScrollViewTestClicked() + local scene = cc.Scene:create() + local proxy = cc.CCBProxy:create() + local node = CCBReaderLoad("cocosbuilderRes/ccb/ccb/TestScrollViews.ccbi",proxy,HelloCocosBuilderLayer) + local layer = tolua.cast(node,"cc.Layer") + if nil ~= HelloCocosBuilderLayer["mTestTitleLabelTTF"] then + local ccLabelTTF = tolua.cast(HelloCocosBuilderLayer["mTestTitleLabelTTF"],"cc.LabelTTF") + if nil ~= ccLabelTTF then + ccLabelTTF:setString("ccb/ccb/TestScrollViews.ccbi") + end + end + if nil ~= scene then + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():pushScene(cc.TransitionFade:create(0.5, scene, cc.c3b(0,0,0))); + end +end + +local function onTimelineCallbackSoundClicked() + cclog("CCBTimelineTest"); + local scene = cc.Scene:create() + local proxy = cc.CCBProxy:create() + local node = CCBReaderLoad("cocosbuilderRes/ccb/ccb/TestTimelineCallback.ccbi",proxy,HelloCocosBuilderLayer) + local layer = tolua.cast(node,"cc.Layer") + if nil ~= HelloCocosBuilderLayer["mTestTitleLabelTTF"] then + local ccLabelTTF = tolua.cast(HelloCocosBuilderLayer["mTestTitleLabelTTF"],"cc.LabelTTF") + if nil ~= ccLabelTTF then + ccLabelTTF:setString("ccb/ccb/TestTimelineCallback.ccbi") + end + end + if nil ~= scene then + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():pushScene(cc.TransitionFade:create(0.5, scene, cc.c3b(0,0,0))); + end +end + +function onCallback1() + if nil ~= TestTimelineLayer["helloLabel"] then + local ccLabelTTF = tolua.cast(TestTimelineLayer["helloLabel"],"cc.LabelTTF") + if nil ~= ccLabelTTF then + ccLabelTTF:runAction(cc.RotateBy:create(1, 360)) + ccLabelTTF:setString("Callback 1"); + end + end +end + +function onCallback2() + if nil ~= TestTimelineLayer["helloLabel"] then + local ccLabelTTF = tolua.cast(TestTimelineLayer["helloLabel"],"cc.LabelTTF") + if nil ~= ccLabelTTF then + ccLabelTTF:runAction(cc.RotateBy:create(2, 360)) + ccLabelTTF:setString("Callback 2"); + end + end +end +TestTimelineLayer["onCallback1"] = onCallback1 +TestTimelineLayer["onCallback2"] = onCallback2 + +HelloCocosBuilderLayer["onMenuTestClicked"] = onMenuTestClicked +HelloCocosBuilderLayer["onSpriteTestClicked"] = onSpriteTestClicked +HelloCocosBuilderLayer["onButtonTestClicked"] = onButtonTestClicked +HelloCocosBuilderLayer["onAnimationsTestClicked"] = onAnimationsTestClicked +HelloCocosBuilderLayer["onParticleSystemTestClicked"] = onParticleSystemTestClicked +HelloCocosBuilderLayer["onScrollViewTestClicked"] = onScrollViewTestClicked +HelloCocosBuilderLayer["onTimelineCallbackSoundClicked"] = onTimelineCallbackSoundClicked + + + +local function HelloCCBTestMainLayer() + print(type(cc.Scene)) + local proxy = cc.CCBProxy:create() + local node = CCBReaderLoad("cocosbuilderRes/ccb/HelloCocosBuilder.ccbi",proxy,HelloCocosBuilderLayer) + local layer = tolua.cast(node,"cc.Layer") + return layer +end + +function runCocosBuilder() + cclog("HelloCCBSceneTestMain") + local scene = cc.Scene:create() + scene:addChild(HelloCCBTestMainLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/ExtensionTest/ExtensionTest.lua b/samples/lua-tests/src/ExtensionTest/ExtensionTest.lua new file mode 100644 index 0000000000..4ce8ac495a --- /dev/null +++ b/samples/lua-tests/src/ExtensionTest/ExtensionTest.lua @@ -0,0 +1,1288 @@ +require "src/ExtensionTest/CocosBuilderTest" +require "src/ExtensionTest/WebProxyTest" + +local LINE_SPACE = 40 +local kItemTagBasic = 1000 + +local ExtensionTestEnum = +{ + TEST_NOTIFICATIONCENTER = 0, + TEST_CCCONTROLBUTTON = 1, + TEST_COCOSBUILDER = 2, + TEST_WEBSOCKET = 3, + TEST_EDITBOX = 4, + TEST_TABLEVIEW = 5, + TEST_SCROLLVIEW = 6, + TEST_MAX_COUNT = 7, +} + +local testsName = +{ + "NotificationCenterTest", + "CCControlButtonTest", + "CocosBuilderTest", + "WebSocketTest", + "EditBoxTest", + "TableViewTest", + "ScrollViewTest", +} + + +--Create toMainLayr MenuItem +function CreateExtensionsBasicLayerMenu(pMenu) + if nil == pMenu then + return + end + local function toMainLayer() + local pScene = ExtensionsTestMain() + if pScene ~= nil then + cc.Director:getInstance():replaceScene(pScene) + end + end + --Create BackMneu + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local pMenuItemFont = cc.MenuItemFont:create("Back") + pMenuItemFont:setPosition(cc.p(VisibleRect:rightBottom().x - 50, VisibleRect:rightBottom().y + 25)) + pMenuItemFont:registerScriptTapHandler(toMainLayer) + pMenu:addChild(pMenuItemFont) +end + +------------------------------------- +--Notification Center Test +------------------------------------- +local NotificationCenterParam = +{ + kTagLight = 100, + kTagConnect = 200, + + MSG_SWITCH_STATE = "SwitchState" +} +local function runNotificationCenterTest() + + local pNewScene = cc.Scene:create() + local pNewLayer = cc.Layer:create() + local function BaseInitSceneLayer(pLayer) + if nil == pLayer then + return + end + local s = cc.Director:getInstance():getWinSize() + + local function toggleSwitch(tag,menuItem) + local toggleItem = tolua.cast(menuItem,"cc.MenuItemToggle") + local nIndex = toggleItem:getSelectedIndex() + local selectedItem = toggleItem:getSelectedItem() + if 0 == nIndex then + selectedItem = nil + end + cc.NotificationCenter:getInstance():postNotification(NotificationCenterParam.MSG_SWITCH_STATE,selectedItem) + end + + local switchlabel1 = cc.LabelTTF:create("switch off", "Marker Felt", 26) + local switchlabel2 = cc.LabelTTF:create("switch on", "Marker Felt", 26) + local switchitem1 = cc.MenuItemLabel:create(switchlabel1) + local switchitem2 = cc.MenuItemLabel:create(switchlabel2) + local switchitem = cc.MenuItemToggle:create(switchitem1) + switchitem:addSubItem(switchitem2) + switchitem:registerScriptTapHandler(toggleSwitch) + --turn on + switchitem:setSelectedIndex(1) + local menu = cc.Menu:create() + menu:addChild(switchitem) + menu:setPosition(cc.p(s.width/2+100, s.height/2)) + pLayer:addChild(menu) + + local menuConnect = cc.Menu:create() + menuConnect:setPosition(cc.p(0,0)) + pLayer:addChild(menuConnect) + local i = 1 + local bSwitchOn = false + local bConnectArray = + { + false, + false, + false + } + + local lightArray = {} + + local function updateLightState() + for i = 1, 3 do + if bSwitchOn and bConnectArray[i] then + lightArray[i]:setOpacity(255) + else + lightArray[i]:setOpacity(50) + end + end + end + + local function switchStateChanged() + local nIndex = switchitem:getSelectedIndex() + + if 0 == nIndex then + bSwitchOn = false + else + bSwitchOn = true + end + updateLightState() + end + + local function setIsConnectToSwitch(pLight,bConnect,nIdx) + bConnectArray[nIdx] = bConnect + if bConnect then + cc.NotificationCenter:getInstance():registerScriptObserver(pLight, switchStateChanged,NotificationCenterParam.MSG_SWITCH_STATE) + else + cc.NotificationCenter:getInstance():unregisterScriptObserver(pLight,NotificationCenterParam.MSG_SWITCH_STATE) + end + updateLightState() + end + + + for i = 1, 3 do + lightArray[i] = cc.Sprite:create("Images/Pea.png") + lightArray[i]:setTag(NotificationCenterParam.kTagLight + i) + lightArray[i]:setPosition(cc.p(100, s.height / 4 * i) ) + pLayer:addChild(lightArray[i]) + + local connectlabel1 = cc.LabelTTF:create("not connected", "Marker Felt", 26) + + local connectlabel2 = cc.LabelTTF:create("connected", "Marker Felt", 26) + local connectitem1 = cc.MenuItemLabel:create(connectlabel1) + local connectitem2 = cc.MenuItemLabel:create(connectlabel2) + local connectitem = cc.MenuItemToggle:create(connectitem1) + connectitem:addSubItem(connectitem2) + + connectitem:setTag(NotificationCenterParam.kTagConnect+i) + + local function connectToSwitch(tag,menuItem) + local connectMenuitem = tolua.cast(menuItem,"cc.MenuItemToggle") + local bConnected = true + if connectMenuitem:getSelectedIndex() == 0 then + bConnected = false + end + local nIdx = connectMenuitem:getTag()-NotificationCenterParam.kTagConnect + setIsConnectToSwitch(lightArray[nIdx],bConnected,nIdx) + end + + connectitem:registerScriptTapHandler(connectToSwitch) + local nX,nY = lightArray[i]:getPosition() + connectitem:setPosition(cc.p(nX,nY+50)) + + menuConnect:addChild(connectitem, 0,connectitem:getTag()) + + if i == 2 then + connectitem:setSelectedIndex(1) + end + bConnectArray[i] = false + if 1 == connectitem:getSelectedIndex() then + bConnectArray[i] = true + end + end + + for i = 1, 3 do + setIsConnectToSwitch(lightArray[i],bConnectArray[i],i) + end + local toggleSelectIndex = switchitem:getSelectedIndex() + local toggleSelectedItem = switchitem:getSelectedItem() + if 0 == toggleSelectIndex then + toggleSelectedItem = nil + end + cc.NotificationCenter:getInstance():postNotification(NotificationCenterParam.MSG_SWITCH_STATE, toggleSelectedItem) + + --for testing removeAllObservers */ + local function doNothing() + end + cc.NotificationCenter:getInstance():registerScriptObserver(pNewLayer,doNothing, "random-observer1") + cc.NotificationCenter:getInstance():registerScriptObserver(pNewLayer,doNothing, "random-observer2") + cc.NotificationCenter:getInstance():registerScriptObserver(pNewLayer,doNothing, "random-observer3") + + local function CreateToMainMenu(pMenu) + if nil == pMenu then + return + end + local function toMainLayer() + local numObserversRemoved = cc.NotificationCenter:getInstance():removeAllObservers(pNewLayer) + if 3 ~= numObserversRemoved then + print("All observers were not removed!") + end + + for i = 1 , 3 do + if bConnectArray[i] then + cc.NotificationCenter:getInstance():unregisterScriptObserver(lightArray[i],NotificationCenterParam.MSG_SWITCH_STATE) + end + end + + local pScene = ExtensionsTestMain() + if pScene ~= nil then + cc.Director:getInstance():replaceScene(pScene) + end + end + --Create BackMneu + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local pMenuItemFont = cc.MenuItemFont:create("Back") + pMenuItemFont:setPosition(cc.p(VisibleRect:rightBottom().x - 50, VisibleRect:rightBottom().y + 25)) + pMenuItemFont:registerScriptTapHandler(toMainLayer) + pMenu:addChild(pMenuItemFont) + end + --Add Menu + local pToMainMenu = cc.Menu:create() + CreateToMainMenu(pToMainMenu) + pToMainMenu:setPosition(cc.p(0, 0)) + pLayer:addChild(pToMainMenu,10) + end + + BaseInitSceneLayer(pNewLayer) + pNewScene:addChild(pNewLayer) + + return pNewScene +end +------------------------------------- +-- Control Extensions Test +------------------------------------- +local ControlExtensionsTestEnum = +{ + kCCControlSliderTest = 0, + kCCControlColourPickerTest = 1, + kCCControlSwitchTest = 2, + kCCControlButtonTest_HelloVariableSize = 3, + kCCControlButtonTest_Event = 4, + kCCControlButtonTest_Styling = 5, + kCCControlPotentiometerTest = 6, + kCCControlStepperTest = 7, + kCCControlTestMax = 8 +} + +local ControlExtensionsTestArray = +{ + "CCControlSliderTest", + "ControlColourPickerTest", + "ControlSwitchTest", + "ControlButtonTest_HelloVariableSize", + "ControlButtonTest_Event", + "ControlButtonTest_Styling", + "ControlPotentiometerTest", + "CCControlStepperTest", +} +local function runCCControlTest() + local nMaxCases = ControlExtensionsTestEnum.kCCControlTestMax + --kCCControlSliderTest + local nCurCase = ControlExtensionsTestEnum.kCCControlSliderTest + local pSceneTitleLabel = nil + + local function GetSceneTitleLabel() + return pSceneTitleLabel + end + + local function SetSceneTitleLabel(pLabel) + pSceneTitleLabel = pLabel + end + + local function GetControlExtensionsTitle() + return ControlExtensionsTestArray[nCurCase + 1] + end + + local pNewScene = cc.Scene:create() + + local function CreateBasicMenu(pMenu) + if nil == pMenu then + return + end + + local function backCallback() + nCurCase = nCurCase - 1 + if nCurCase < 0 then + nCurCase = nCurCase + nMaxCases + end + CurrentControlScene() + end + + local function restartCallback() + CurrentControlScene() + end + + local function nextCallback() + nCurCase = nCurCase + 1 + --No check nMaxCases + nCurCase = nCurCase % nMaxCases + CurrentControlScene() + end + + local size = cc.Director:getInstance():getWinSize() + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + item1:registerScriptTapHandler(backCallback) + pMenu:addChild(item1,kItemTagBasic) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + item2:registerScriptTapHandler(restartCallback) + pMenu:addChild(item2,kItemTagBasic) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + pMenu:addChild(item3,kItemTagBasic) + item3:registerScriptTapHandler(nextCallback) + + local size = cc.Director:getInstance():getWinSize() + item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + + end + + local function BaseInitSceneLayer(pLayer,pStrTitle) + if nil == pLayer then + return + end + --Add Menu + local pToMainMenu = cc.Menu:create() + CreateExtensionsBasicLayerMenu(pToMainMenu) + pToMainMenu:setPosition(cc.p(0, 0)) + pLayer:addChild(pToMainMenu,10) + + --Add the generated background + local pBackground = cc.Sprite:create("extensions/background.png") + pBackground:setPosition(VisibleRect:center()) + pLayer:addChild(pBackground) + + --Add the ribbon + local pRibbon = cc.Scale9Sprite:create("extensions/ribbon.png", cc.rect(1, 1, 48, 55)) + pRibbon:setContentSize(cc.size(VisibleRect:getVisibleRect().width, 57)) + pRibbon:setPosition(cc.p(VisibleRect:center().x, VisibleRect:top().y - pRibbon:getContentSize().height / 2.0)) + pLayer:addChild(pRibbon) + + --Add the title + pSceneTitleLabel = cc.LabelTTF:create("Title", "Arial", 12) + pSceneTitleLabel:setPosition(cc.p (VisibleRect:center().x, VisibleRect:top().y - pSceneTitleLabel:getContentSize().height / 2 - 5)) + pLayer:addChild(pSceneTitleLabel, 1) + pSceneTitleLabel:setString(pStrTitle) + + local pOperateMenu = cc.Menu:create() + CreateBasicMenu(pOperateMenu) + pOperateMenu:setPosition(cc.p(0, 0)) + pLayer:addChild(pOperateMenu,1) + end + + local function InitSliderTest(pLayer) + if nil == pLayer then + return + end + + local screenSize = cc.Director:getInstance():getWinSize() + --Add a label in which the slider value will be displayed + local pDisplayValueLabel = cc.LabelTTF:create("Move the slider thumb!\nThe lower slider is restricted." ,"Marker Felt", 32) + pDisplayValueLabel:retain() + pDisplayValueLabel:setAnchorPoint(cc.p(0.5, -1.0)) + pDisplayValueLabel:setPosition(cc.p(screenSize.width / 1.7, screenSize.height / 2.0)) + pLayer:addChild(pDisplayValueLabel) + + local function valueChanged(pSender) + if nil == pSender or nil == pDisplayValueLabel then + return + end + local pControl = tolua.cast(pSender,"cc.ControlSlider") + local strFmt = nil + if pControl:getTag() == 1 then + strFmt = string.format("Upper slider value = %.02f",pControl:getValue()) + elseif pControl:getTag() == 2 then + strFmt = string.format("Lower slider value = %.02f",pControl:getValue()) + end + + if nil ~= strFmt then + pDisplayValueLabel:setString(strFmt) + end + end + --Add the slider + local pSlider = cc.ControlSlider:create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png") + pSlider:setAnchorPoint(cc.p(0.5, 1.0)) + pSlider:setMinimumValue(0.0) + pSlider:setMaximumValue(5.0) + pSlider:setPosition(cc.p(screenSize.width / 2.0, screenSize.height / 2.0 + 16)) + pSlider:setTag(1) + + --When the value of the slider will change, the given selector will be call + pSlider:registerControlEventHandler(valueChanged, cc.CONTROL_EVENTTYPE_VALUE_CHANGED) + + local pRestrictSlider = cc.ControlSlider:create("extensions/sliderTrack.png","extensions/sliderProgress.png" ,"extensions/sliderThumb.png") + pRestrictSlider:setAnchorPoint(cc.p(0.5, 1.0)) + pRestrictSlider:setMinimumValue(0.0) + pRestrictSlider:setMaximumValue(5.0) + pRestrictSlider:setMaximumAllowedValue(4.0) + pRestrictSlider:setMinimumAllowedValue(1.5) + pRestrictSlider:setValue(3.0) + pRestrictSlider:setPosition(cc.p(screenSize.width / 2.0, screenSize.height / 2.0 - 24)) + pRestrictSlider:setTag(2) + --same with restricted + pRestrictSlider:registerControlEventHandler(valueChanged, cc.CONTROL_EVENTTYPE_VALUE_CHANGED) + pLayer:addChild(pSlider) + pLayer:addChild(pRestrictSlider) + end + + --ColourPickerTest + local function InitColourPickerTest(pLayer) + if nil == pLayer then + return + end + local screenSize = cc.Director:getInstance():getWinSize() + local pColorLabel = nil + + local pNode = cc.Node:create() + pNode:setPosition(cc.p (screenSize.width / 2, screenSize.height / 2)) + pLayer:addChild(pNode, 1) + + local dLayer_width = 0 + + --Create the colour picker,pStrEventName not use + local function colourValueChanged(pSender) + if nil == pSender or nil == pColorLabel then + return + end + + local pPicker = tolua.cast(pSender,"cc.ControlColourPicker") + local strFmt = string.format("#%02X%02X%02X",pPicker:getColor().r, pPicker:getColor().g, pPicker:getColor().b) + pColorLabel:setString(strFmt) + end + local pColourPicker = cc.ControlColourPicker:create() + pColourPicker:setColor(cc.c3b(37, 46, 252)) + pColourPicker:setPosition(cc.p (pColourPicker:getContentSize().width / 2, 0)) + pColourPicker:registerControlEventHandler(colourValueChanged, cc.CONTROL_EVENTTYPE_VALUE_CHANGED) + pNode:addChild(pColourPicker) + + dLayer_width = dLayer_width + pColourPicker:getContentSize().width + + --Add the black background for the text + local pBackground = cc.Scale9Sprite:create("extensions/buttonBackground.png") + pBackground:setContentSize(cc.size(150, 50)) + pBackground:setPosition(cc.p(dLayer_width + pBackground:getContentSize().width / 2.0, 0)) + pNode:addChild(pBackground) + dLayer_width = dLayer_width + pBackground:getContentSize().width + + pColorLabel = cc.LabelTTF:create("#color", "Marker Felt", 30) + pColorLabel:retain() + pColorLabel:setPosition(pBackground:getPosition()) + pNode:addChild(pColorLabel) + + --Set the layer size + pNode:setContentSize(cc.size(dLayer_width, 0)) + pNode:setAnchorPoint(cc.p (0.5, 0.5)) + + --Update the color text + colourValueChanged(pColourPicker) + end + + --SwitchTest + local function InitSwitchTest(pLayer) + if nil == pLayer then + return + end + + local screenSize = cc.Director:getInstance():getWinSize() + + local pNode = cc.Node:create() + pNode:setPosition(cc.p (screenSize.width / 2, screenSize.height / 2)) + pLayer:addChild(pNode, 1) + + local dLayer_width = 0 + + --Add the black background for the text + local pBackground = cc.Scale9Sprite:create("extensions/buttonBackground.png") + pBackground:setContentSize(cc.size(80, 50)) + pBackground:setPosition(cc.p(dLayer_width + pBackground:getContentSize().width / 2.0, 0)) + pNode:addChild(pBackground) + dLayer_width = dLayer_width + pBackground:getContentSize().width + + local pDisplayValueLabel = cc.LabelTTF:create("#color" ,"Marker Felt" ,30) + pDisplayValueLabel:retain() + + pDisplayValueLabel:setPosition(pBackground:getPosition()) + pNode:addChild(pDisplayValueLabel) + + --Create the switch + local function valueChanged(pSender) + if nil == pDisplayValueLabel or nil == pSender then + return + end + + local pControl = tolua.cast(pSender,"cc.ControlSwitch") + if pControl:isOn() then + pDisplayValueLabel:setString("On") + else + pDisplayValueLabel:setString("Off") + end + end + local pSwitchControl = cc.ControlSwitch:create( + cc.Sprite:create("extensions/switch-mask.png"), + cc.Sprite:create("extensions/switch-on.png"), + cc.Sprite:create("extensions/switch-off.png"), + cc.Sprite:create("extensions/switch-thumb.png"), + cc.LabelTTF:create("On", "Arial-BoldMT", 16), + cc.LabelTTF:create("Off", "Arial-BoldMT", 16) + ) + pSwitchControl:setPosition(cc.p (dLayer_width + 10 + pSwitchControl:getContentSize().width / 2, 0)) + pNode:addChild(pSwitchControl) + pSwitchControl:registerControlEventHandler(valueChanged, cc.CONTROL_EVENTTYPE_VALUE_CHANGED) + + --Set the layer size + pNode:setContentSize(cc.size(dLayer_width, 0)) + pNode:setAnchorPoint(cc.p (0.5, 0.5)) + + --Update the value label + valueChanged(pSwitchControl) + end + + --Hvs:HelloVariableSize + local function HvsStandardButtonWithTitle(pStrTitle) + -- Creates and return a button with a default background and title color. + local pBackgroundButton = cc.Scale9Sprite:create("extensions/button.png") + local pBackgroundHighlightedButton = cc.Scale9Sprite:create("extensions/buttonHighlighted.png") + + pTitleButton = cc.LabelTTF:create(pStrTitle, "Marker Felt", 30) + + pTitleButton:setColor(cc.c3b(159, 168, 176)) + + local pButton = cc.ControlButton:create(pTitleButton, pBackgroundButton) + pButton:setBackgroundSpriteForState(pBackgroundHighlightedButton, cc.CONTROL_STATE_HIGH_LIGHTED ) + pButton:setTitleColorForState(cc.c3b(255,255,255), cc.CONTROL_STATE_HIGH_LIGHTED ) + + return pButton + end + + local function InitHelloVariableSize(pLayer) + if nil == pLayer then + return + end + + local screenSize = cc.Director:getInstance():getWinSize() + local strArray = {"Hello", "Variable", "Size", "!"} + + local pNode = cc.Node:create() + pLayer:addChild(pNode,1) + local dTotalWidth = 0 + local dHeight = 0 + local pObj = nil + + local i = 0 + local nLen = table.getn(strArray) + for i = 0, nLen - 1 do + --Creates a button with pLayer string as title + local pButton = HvsStandardButtonWithTitle(strArray[i + 1]) + pButton:setPosition(cc.p (dTotalWidth + pButton:getContentSize().width / 2, pButton:getContentSize().height / 2)) + pNode:addChild(pButton) + + --Compute the size of the layer + dHeight = pButton:getContentSize().height + dTotalWidth = dTotalWidth + pButton:getContentSize().width + end + + pNode:setAnchorPoint(cc.p (0.5, 0.5)) + pNode:setContentSize(cc.size(dTotalWidth, dHeight)) + pNode:setPosition(cc.p(screenSize.width / 2.0, screenSize.height / 2.0)) + + --Add the black background + local pBackground = cc.Scale9Sprite:create("extensions/buttonBackground.png") + pBackground:setContentSize(cc.size(dTotalWidth + 14, dHeight + 14)) + pBackground:setPosition(cc.p(screenSize.width / 2.0, screenSize.height / 2.0)) + pLayer:addChild(pBackground) + end + + local function StylingStandardButtonWithTitle(pStrTitle) + local pBackgroundButton = cc.Scale9Sprite:create("extensions/button.png") + pBackgroundButton:setPreferredSize(cc.size(45, 45)) + local pBackgroundHighlightedButton = cc.Scale9Sprite:create("extensions/buttonHighlighted.png") + pBackgroundHighlightedButton:setPreferredSize(cc.size(45, 45)) + + local pTitleButton = cc.LabelTTF:create(pStrTitle, "Marker Felt", 30) + + pTitleButton:setColor(cc.c3b(159, 168, 176)) + + local pButton = cc.ControlButton:create(pTitleButton, pBackgroundButton) + pButton:setBackgroundSpriteForState(pBackgroundHighlightedButton, cc.CONTROL_STATE_HIGH_LIGHTED ) + pButton:setTitleColorForState(cc.c3b(255,255,255), cc.CONTROL_STATE_HIGH_LIGHTED ) + + return pButton + end + + local function InitStyling(pLayer) + if nil == pLayer then + return + end + + local screenSize = cc.Director:getInstance():getWinSize() + + local pNode = cc.Node:create() + pLayer:addChild(pNode, 1) + + local nSpace = 10 + + local nMax_w = 0 + local nMax_h = 0 + local i = 0 + local j = 0 + for i = 0, 2 do + for j = 0, 2 do + --Add the buttons + local strFmt = string.format("%d",math.random(0,32767) % 30) + local pButton = StylingStandardButtonWithTitle(strFmt) + pButton:setAdjustBackgroundImage(false) + pButton:setPosition(cc.p (pButton:getContentSize().width / 2 + (pButton:getContentSize().width + nSpace) * i, + pButton:getContentSize().height / 2 + (pButton:getContentSize().height + nSpace) * j)) + + pNode:addChild(pButton) + + nMax_w = math.max(pButton:getContentSize().width * (i + 1) + nSpace * i, nMax_w) + nMax_h = math.max(pButton:getContentSize().height * (j + 1) + nSpace * j, nMax_h) + end + end + + + pNode:setAnchorPoint(cc.p (0.5, 0.5)) + pNode:setContentSize(cc.size(nMax_w, nMax_h)) + pNode:setPosition(cc.p(screenSize.width / 2.0, screenSize.height / 2.0)) + + --Add the black background + local pBackgroundButton = cc.Scale9Sprite:create("extensions/buttonBackground.png") + pBackgroundButton:setContentSize(cc.size(nMax_w + 14, nMax_h + 14)) + pBackgroundButton:setPosition(cc.p(screenSize.width / 2.0, screenSize.height / 2.0)) + pLayer:addChild(pBackgroundButton) + end + + local function InitButtonTestEvent(pLayer) + if nil == pLayer then + return + end + + local screenSize = cc.Director:getInstance():getWinSize() + + --Add a label in which the button events will be displayed + local pDisplayValueLabel = nil + pDisplayValueLabel = cc.LabelTTF:create("No Event", "Marker Felt", 32) + pDisplayValueLabel:setAnchorPoint(cc.p(0.5, -1)) + pDisplayValueLabel:setPosition(cc.p(screenSize.width / 2.0, screenSize.height / 2.0)) + pLayer:addChild(pDisplayValueLabel, 1) + + --Add the button + local pBackgroundButton = cc.Scale9Sprite:create("extensions/button.png") + local pBackgroundHighlightedButton = cc.Scale9Sprite:create("extensions/buttonHighlighted.png") + + local pTitleButtonLabel = cc.LabelTTF:create("Touch Me!", "Marker Felt", 30) + pTitleButtonLabel:setColor(cc.c3b(159, 168, 176)) + + local pControlButton = cc.ControlButton:create(pTitleButtonLabel, pBackgroundButton) + local function touchDownAction() + if nil == pDisplayValueLabel then + return + end + pDisplayValueLabel:setString("Touch Down" ) + end + + local function touchDragInsideAction() + if nil == pDisplayValueLabel then + return + end + pDisplayValueLabel:setString("Drag Inside") + end + + local function touchDragOutsideAction() + if nil == pDisplayValueLabel then + return + end + pDisplayValueLabel:setString("Drag Outside") + end + + local function touchDragEnterAction() + if nil == pDisplayValueLabel then + return + end + pDisplayValueLabel:setString("Drag Enter") + end + + local function touchDragExitAction() + if nil == pDisplayValueLabel then + return + end + pDisplayValueLabel:setString("Drag Exit") + end + + local function touchUpInsideAction() + if nil == pDisplayValueLabel then + return + end + pDisplayValueLabel:setString("Touch Up Inside.") + end + + local function touchUpOutsideAction() + if nil == pDisplayValueLabel then + return + end + pDisplayValueLabel:setString("Touch Up Outside.") + end + + local function touchCancelAction() + if nil == pDisplayValueLabel then + return + end + pDisplayValueLabel:setString("Touch Cancel") + end + + + + pControlButton:setBackgroundSpriteForState(pBackgroundHighlightedButton, cc.CONTROL_STATE_HIGH_LIGHTED ) + pControlButton:setTitleColorForState(cc.c3b(255, 255, 255), cc.CONTROL_STATE_HIGH_LIGHTED ) + pControlButton:setAnchorPoint(cc.p(0.5, 1)) + pControlButton:setPosition(cc.p(screenSize.width / 2.0, screenSize.height / 2.0)) + pControlButton:registerControlEventHandler(touchDownAction,cc.CONTROL_EVENTTYPE_TOUCH_DOWN ) + pControlButton:registerControlEventHandler(touchDragInsideAction,cc.CONTROL_EVENTTYPE_DRAG_INSIDE) + pControlButton:registerControlEventHandler(touchDragOutsideAction,cc.CONTROL_EVENTTYPE_DRAG_OUTSIDE) + pControlButton:registerControlEventHandler(touchDragEnterAction,cc.CONTROL_EVENTTYPE_DRAG_ENTER) + pControlButton:registerControlEventHandler(touchDragExitAction,cc.CONTROL_EVENTTYPE_DRAG_EXIT) + pControlButton:registerControlEventHandler(touchUpInsideAction,cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE) + pControlButton:registerControlEventHandler(touchUpOutsideAction,cc.CONTROL_EVENTTYPE_TOUCH_UP_OUTSIDE) + pControlButton:registerControlEventHandler(touchCancelAction,cc.CONTROL_EVENTTYPE_TOUCH_CANCEL) + pLayer:addChild(pControlButton, 1) + + --Add the black background + local pBackgroundButton = cc.Scale9Sprite:create("extensions/buttonBackground.png") + pBackgroundButton:setContentSize(cc.size(300, 170)) + pBackgroundButton:setPosition(cc.p(screenSize.width / 2.0, screenSize.height / 2.0)) + pLayer:addChild(pBackgroundButton) + end + --PotentiometerTest + local function InitPotentiometerTest(pLayer) + if nil == pLayer then + return + end + + local screenSize = cc.Director:getInstance():getWinSize() + + local pNode = cc.Node:create() + pNode:setPosition(cc.p (screenSize.width / 2, screenSize.height / 2)) + pLayer:addChild(pNode, 1) + + local dLayer_width = 0 + + -- Add the black background for the text + local pBackground = cc.Scale9Sprite:create("extensions/buttonBackground.png") + pBackground:setContentSize(cc.size(80, 50)) + pBackground:setPosition(cc.p(dLayer_width + pBackground:getContentSize().width / 2.0, 0)) + pNode:addChild(pBackground) + + dLayer_width = dLayer_width + pBackground:getContentSize().width + + local pDisplayValueLabel = cc.LabelTTF:create("", "HelveticaNeue-Bold", 30) + pDisplayValueLabel:setPosition(pBackground:getPosition()) + pNode:addChild(pDisplayValueLabel) + + -- Add the slider + local function valueChanged(pSender) + if nil == pSender then + return + end + + local pControl = tolua.cast(pSender,"cc.ControlPotentiometer") + local strFmt = string.format("%0.2f",pControl:getValue()) + pDisplayValueLabel:setString(strFmt ) + end + local pPotentiometer = cc.ControlPotentiometer:create("extensions/potentiometerTrack.png","extensions/potentiometerProgress.png" + ,"extensions/potentiometerButton.png") + pPotentiometer:setPosition(cc.p (dLayer_width + 10 + pPotentiometer:getContentSize().width / 2, 0)) + + -- When the value of the slider will change, the given selector will be call + pPotentiometer:registerControlEventHandler(valueChanged, cc.CONTROL_EVENTTYPE_VALUE_CHANGED) + + pNode:addChild(pPotentiometer) + + dLayer_width = dLayer_width + pPotentiometer:getContentSize().width + + -- Set the layer size + pNode:setContentSize(cc.size(dLayer_width, 0)) + pNode:setAnchorPoint(cc.p (0.5, 0.5)) + + -- Update the value label + valueChanged(pPotentiometer) + end + + local function InitStepperTest(pLayer) + if nil == pLayer then + return + end + + local screenSize = cc.Director:getInstance():getWinSize() + + local pNode = cc.Node:create() + pNode:setPosition(cc.p (screenSize.width / 2, screenSize.height / 2)) + pLayer:addChild(pNode, 1) + + local layer_width = 0 + + -- Add the black background for the text + local background = cc.Scale9Sprite:create("extensions/buttonBackground.png") + background:setContentSize(cc.size(100, 50)) + background:setPosition(cc.p(layer_width + background:getContentSize().width / 2.0, 0)) + pNode:addChild(background) + + local pDisplayValueLabel = cc.LabelTTF:create("0", "HelveticaNeue-Bold", 30) + + pDisplayValueLabel:setPosition(background:getPosition()) + pNode:addChild(pDisplayValueLabel) + + layer_width = layer_width + background:getContentSize().width + + local minusSprite = cc.Sprite:create("extensions/stepper-minus.png") + local plusSprite = cc.Sprite:create("extensions/stepper-plus.png") + + local function valueChanged(pSender) + if nil == pDisplayValueLabel or nil == pSender then + return + end + + local pControl = tolua.cast(pSender,"cc.ControlStepper") + local strFmt = string.format("%0.02f",pControl:getValue() ) + pDisplayValueLabel:setString(strFmt ) + end + local stepper = cc.ControlStepper:create(minusSprite, plusSprite) + stepper:setPosition(cc.p (layer_width + 10 + stepper:getContentSize().width / 2, 0)) + stepper:registerControlEventHandler(valueChanged, cc.CONTROL_EVENTTYPE_VALUE_CHANGED) + pNode:addChild(stepper) + + layer_width = layer_width + stepper:getContentSize().width + + -- Set the layer size + pNode:setContentSize(cc.size(layer_width, 0)) + pNode:setAnchorPoint(cc.p (0.5, 0.5)) + + -- Update the value label + valueChanged(stepper) + end + + local function InitSpecialSceneLayer(pLayer) + if ControlExtensionsTestEnum.kCCControlSliderTest == nCurCase then + InitSliderTest(pLayer) + elseif ControlExtensionsTestEnum.kCCControlColourPickerTest == nCurCase then + InitColourPickerTest(pLayer) + elseif ControlExtensionsTestEnum.kCCControlSwitchTest == nCurCase then + InitSwitchTest(pLayer) + elseif ControlExtensionsTestEnum.kCCControlButtonTest_HelloVariableSize == nCurCase then + InitHelloVariableSize(pLayer) + elseif ControlExtensionsTestEnum.kCCControlButtonTest_Event == nCurCase then + InitButtonTestEvent(pLayer) + elseif ControlExtensionsTestEnum.kCCControlButtonTest_Styling == nCurCase then + InitStyling(pLayer) + elseif ControlExtensionsTestEnum.kCCControlPotentiometerTest == nCurCase then + InitPotentiometerTest(pLayer) + elseif ControlExtensionsTestEnum.kCCControlStepperTest == nCurCase then + InitStepperTest(pLayer) + end + end + + function CurrentControlScene() + pNewScene = nil + pNewScene = cc.Scene:create() + local pNewLayer = cc.Layer:create() + BaseInitSceneLayer(pNewLayer,GetControlExtensionsTitle()) + InitSpecialSceneLayer(pNewLayer) + pNewScene:addChild(pNewLayer) + if nil ~= pNewScene then + cc.Director:getInstance():replaceScene(pNewScene) + end + + end + + local pNewLayer = cc.Layer:create() + BaseInitSceneLayer(pNewLayer,GetControlExtensionsTitle()) + InitSpecialSceneLayer(pNewLayer) + pNewScene:addChild(pNewLayer) + + return pNewScene +end + +local function runEditBoxTest() + local newScene = cc.Scene:create() + local newLayer = cc.Layer:create() + local visibleOrigin = cc.Director:getInstance():getVisibleOrigin() + local visibleSize = cc.Director:getInstance():getVisibleSize() + + local pBg = cc.Sprite:create("Images/HelloWorld.png") + pBg:setPosition(cc.p(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/2)) + newLayer:addChild(pBg) + + local TTFShowEditReturn = cc.LabelTTF:create("No edit control return!", "", 30) + TTFShowEditReturn:setPosition(cc.p(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y + visibleSize.height - 50)) + newLayer:addChild(TTFShowEditReturn) + + -- Back Menu + local pToMainMenu = cc.Menu:create() + CreateExtensionsBasicLayerMenu(pToMainMenu) + pToMainMenu:setPosition(cc.p(0, 0)) + newLayer:addChild(pToMainMenu,10) + + local editBoxSize = cc.size(visibleSize.width - 100, 60) + local EditName = nil + local EditPassword = nil + local EditEmail = nil + + local function editBoxTextEventHandle(strEventName,pSender) + local edit = tolua.cast(pSender,"cc.EditBox") + local strFmt + if strEventName == "began" then + strFmt = string.format("editBox %p DidBegin !", edit) + print(strFmt) + elseif strEventName == "ended" then + strFmt = string.format("editBox %p DidEnd !", edit) + print(strFmt) + elseif strEventName == "return" then + strFmt = string.format("editBox %p was returned !",edit) + if edit == EditName then + TTFShowEditReturn:setString("Name EditBox return !") + elseif edit == EditPassword then + TTFShowEditReturn:setString("Password EditBox return !") + elseif edit == EditEmail then + TTFShowEditReturn:setString("Email EditBox return !") + end + print(strFmt) + elseif strEventName == "changed" then + strFmt = string.format("editBox %p TextChanged, text: %s ", edit, edit:getText()) + print(strFmt) + end + end + -- top + EditName = cc.EditBox:create(editBoxSize, cc.Scale9Sprite:create("extensions/green_edit.png")) + EditName:setPosition(cc.p(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height*3/4)) + local targetPlatform = cc.Application:getInstance():getTargetPlatform() + if kTargetIphone == targetPlatform or kTargetIpad == targetPlatform then + EditName:setFontName("Paint Boy") + else + EditName:setFontName("fonts/Paint Boy.ttf") + end + EditName:setFontSize(25) + EditName:setFontColor(cc.c3b(255,0,0)) + EditName:setPlaceHolder("Name:") + EditName:setPlaceholderFontColor(cc.c3b(255,255,255)) + EditName:setMaxLength(8) + EditName:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE ) + --Handler + EditName:registerScriptEditBoxHandler(editBoxTextEventHandle) + newLayer:addChild(EditName) + + --middle + EditPassword = cc.EditBox:create(editBoxSize, cc.Scale9Sprite:create("extensions/orange_edit.png")) + EditPassword:setPosition(cc.p(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/2)) + if kTargetIphone == targetPlatform or kTargetIpad == targetPlatform then + EditPassword:setFont("American Typewriter", 30) + else + EditPassword:setFont("fonts/American Typewriter.ttf", 30) + end + + + EditPassword:setFontColor(cc.c3b(0,255,0)) + EditPassword:setPlaceHolder("Password:") + EditPassword:setMaxLength(6) + EditPassword:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD) + EditPassword:setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE) + EditPassword:registerScriptEditBoxHandler(editBoxTextEventHandle) + newLayer:addChild(EditPassword) + + --bottom + EditEmail = cc.EditBox:create(cc.size(editBoxSize.width, editBoxSize.height), cc.Scale9Sprite:create("extensions/yellow_edit.png")) + EditEmail:setPosition(cc.p(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/4)) + EditEmail:setAnchorPoint(cc.p(0.5, 1.0)) + EditEmail:setPlaceHolder("Email:") + EditEmail:setInputMode(cc.EDITBOX_INPUT_MODE_EMAILADDR) + EditEmail:registerScriptEditBoxHandler(editBoxTextEventHandle) + newLayer:addChild(EditEmail) + newLayer:setPosition(cc.p(10, 20)) + + + newScene:addChild(newLayer) + return newScene +end + +local TableViewTestLayer = class("TableViewTestLayer") +TableViewTestLayer.__index = TableViewTestLayer + +function TableViewTestLayer.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TableViewTestLayer) + return target +end + +function TableViewTestLayer.scrollViewDidScroll(view) + print("scrollViewDidScroll") +end + +function TableViewTestLayer.scrollViewDidZoom(view) + print("scrollViewDidZoom") +end + +function TableViewTestLayer.tableCellTouched(table,cell) + print("cell touched at index: " .. cell:getIdx()) +end + +function TableViewTestLayer.cellSizeForTable(table,idx) + return 60,60 +end + +function TableViewTestLayer.tableCellAtIndex(table, idx) + local strValue = string.format("%d",idx) + local cell = table:dequeueCell() + local label = nil + if nil == cell then + cell = cc.TableViewCell:new() + local sprite = cc.Sprite:create("Images/Icon.png") + sprite:setAnchorPoint(cc.p(0,0)) + sprite:setPosition(cc.p(0, 0)) + cell:addChild(sprite) + + label = cc.LabelTTF:create(strValue, "Helvetica", 20.0) + label:setPosition(cc.p(0,0)) + label:setAnchorPoint(cc.p(0,0)) + label:setTag(123) + cell:addChild(label) + else + label = tolua.cast(cell:getChildByTag(123),"cc.LabelTTF") + if nil ~= label then + label:setString(strValue) + end + end + + return cell +end + +function TableViewTestLayer.numberOfCellsInTableView(table) + return 25 +end + +function TableViewTestLayer:init() + + local winSize = cc.Director:getInstance():getWinSize() + + local tableView = cc.TableView:create(cc.size(600,60)) + tableView:setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL) + tableView:setPosition(cc.p(20, winSize.height / 2 - 150)) + tableView:setDelegate() + self:addChild(tableView) + --registerScriptHandler functions must be before the reloadData funtion + tableView:registerScriptHandler(TableViewTestLayer.numberOfCellsInTableView,cc.NUMBER_OF_CELLS_IN_TABLEVIEW) + tableView:registerScriptHandler(TableViewTestLayer.scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL) + tableView:registerScriptHandler(TableViewTestLayer.scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM) + tableView:registerScriptHandler(TableViewTestLayer.tableCellTouched,cc.TABLECELL_TOUCHED) + tableView:registerScriptHandler(TableViewTestLayer.cellSizeForTable,cc.TABLECELL_SIZE_FOR_INDEX) + tableView:registerScriptHandler(TableViewTestLayer.tableCellAtIndex,cc.TABLECELL_SIZE_AT_INDEX) + tableView:reloadData() + + tableView = cc.TableView:create(cc.size(60, 350)) + tableView:setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL) + tableView:setPosition(cc.p(winSize.width - 150, winSize.height / 2 - 150)) + tableView:setDelegate() + tableView:setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN) + self:addChild(tableView) + tableView:registerScriptHandler(TableViewTestLayer.scrollViewDidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL) + tableView:registerScriptHandler(TableViewTestLayer.scrollViewDidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM) + tableView:registerScriptHandler(TableViewTestLayer.tableCellTouched,cc.TABLECELL_TOUCHED) + tableView:registerScriptHandler(TableViewTestLayer.cellSizeForTable,cc.TABLECELL_SIZE_FOR_INDEX) + tableView:registerScriptHandler(TableViewTestLayer.tableCellAtIndex,cc.TABLECELL_SIZE_AT_INDEX) + tableView:registerScriptHandler(TableViewTestLayer.numberOfCellsInTableView,cc.NUMBER_OF_CELLS_IN_TABLEVIEW) + tableView:reloadData() + + -- Back Menu + local pToMainMenu = cc.Menu:create() + CreateExtensionsBasicLayerMenu(pToMainMenu) + pToMainMenu:setPosition(cc.p(0, 0)) + self:addChild(pToMainMenu,10) + + return true +end + +function TableViewTestLayer.create() + local layer = TableViewTestLayer.extend(cc.Layer:create()) + if nil ~= layer then + layer:init() + end + + return layer +end + +local function runTableViewTest() + local newScene = cc.Scene:create() + local newLayer = TableViewTestLayer.create() + newScene:addChild(newLayer) + return newScene +end + +local function runScrollViewTest() + local newScene = cc.Scene:create() + local newLayer = cc.Layer:create() + + -- Back Menu + local pToMainMenu = cc.Menu:create() + CreateExtensionsBasicLayerMenu(pToMainMenu) + pToMainMenu:setPosition(cc.p(0, 0)) + newLayer:addChild(pToMainMenu,10) + + local layerColor = cc.LayerColor:create(cc.c4b(128,64,0,255)) + newLayer:addChild(layerColor) + + local scrollView1 = cc.ScrollView:create() + local screenSize = cc.Director:getInstance():getWinSize() + local function scrollView1DidScroll() + print("scrollView1DidScroll") + end + local function scrollView1DidZoom() + print("scrollView1DidZoom") + end + if nil ~= scrollView1 then + scrollView1:setViewSize(cc.size(screenSize.width / 2,screenSize.height)) + scrollView1:setPosition(cc.p(0,0)) + scrollView1:setScale(1.0) + scrollView1:ignoreAnchorPointForPosition(true) + local flowersprite1 = cc.Sprite:create("ccb/flower.jpg") + if nil ~= flowersprite1 then + scrollView1:setContainer(flowersprite1) + scrollView1:updateInset() + end + scrollView1:setDirection(cc.SCROLLVIEW_DIRECTION_BOTH ) + scrollView1:setClippingToBounds(true) + scrollView1:setBounceable(true) + scrollView1:setDelegate() + scrollView1:registerScriptHandler(scrollView1DidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL) + scrollView1:registerScriptHandler(scrollView1DidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM) + end + newLayer:addChild(scrollView1) + + local scrollView2 = cc.ScrollView:create() + local function scrollView2DidScroll() + print("scrollView2DidScroll") + end + local function scrollView2DidZoom() + print("scrollView2DidZoom") + end + if nil ~= scrollView2 then + scrollView2:setViewSize(cc.size(screenSize.width / 2,screenSize.height)) + scrollView2:setPosition(cc.p(screenSize.width / 2,0)) + scrollView2:setScale(1.0) + scrollView2:ignoreAnchorPointForPosition(true) + local flowersprite2 = cc.Sprite:create("ccb/flower.jpg") + if nil ~= flowersprite2 then + scrollView2:setContainer(flowersprite2) + scrollView2:updateInset() + end + scrollView2:setDirection(cc.SCROLLVIEW_DIRECTION_BOTH ) + scrollView2:setClippingToBounds(true) + scrollView2:setBounceable(true) + scrollView2:setDelegate() + scrollView2:registerScriptHandler(scrollView2DidScroll,cc.SCROLLVIEW_SCRIPT_SCROLL) + scrollView2:registerScriptHandler(scrollView2DidZoom,cc.SCROLLVIEW_SCRIPT_ZOOM) + end + newLayer:addChild(scrollView2) + + newScene:addChild(newLayer) + return newScene +end + +local CreateExtensionsTestTable = +{ + runNotificationCenterTest, + runCCControlTest, + runCocosBuilder, + runWebSocketTest, + runEditBoxTest, + runTableViewTest, + runScrollViewTest, +} + + +local function ExtensionsMainLayer() + + local s = cc.Director:getInstance():getWinSize() + + local function CreateExtensionsTestScene(nPerformanceNo) + local pNewscene = CreateExtensionsTestTable[nPerformanceNo]() + return pNewscene + end + + local function menuCallback(tag, pMenuItem) + local scene = nil + local nIdx = pMenuItem:getLocalZOrder() - kItemTagBasic + local ExtensionsTestScene = CreateExtensionsTestScene(nIdx) + if nil ~= ExtensionsTestScene then + cc.Director:getInstance():replaceScene(ExtensionsTestScene) + end + end + + local layer = cc.Layer:create() + local menu = cc.Menu:create() + menu:setPosition(cc.p(0, 0)) + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local targetPlatform = cc.Application:getInstance():getTargetPlatform() + local bSupportWebSocket = false + if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or (cc.PLATFORM_OS_ANDROID == targetPlatform) or (cc.PLATFORM_OS_WINDOWS == targetPlatform) then + bSupportWebSocket = true + end + local bSupportEdit = false + if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or + (cc.PLATFORM_OS_ANDROID == targetPlatform) or (cc.PLATFORM_OS_WINDOWS == targetPlatform) or + (cc.PLATFORM_OS_MAC == targetPlatform) or (cc.PLATFORM_OS_TIZEN == targetPlatform) then + bSupportEdit = true + end + for i = 1, ExtensionTestEnum.TEST_MAX_COUNT do + local item = cc.MenuItemFont:create(testsName[i]) + item:registerScriptTapHandler(menuCallback) + item:setPosition(s.width / 2, s.height - i * LINE_SPACE) + menu:addChild(item, kItemTagBasic + i) + if ((i == ExtensionTestEnum.TEST_WEBSOCKET + 1) and (false == bSupportWebSocket)) + or ((i == ExtensionTestEnum.TEST_EDITBOX + 1) and (false == bSupportEdit)) + or (i == ExtensionTestEnum.TEST_NOTIFICATIONCENTER + 1)then + item:setEnabled(false) + end + end + + layer:addChild(menu) + + -- handling touch events + local beginPos = {x = 0, y = 0} + local function onTouchesBegan(touches, event) + beginPos = touches[1]:getLocation() + end + + local function onTouchesMoved(touches, event) + local location = touches[1]:getLocation() + + local nMoveY = location.y - beginPos.y + local curPosx, curPosy = menu:getPosition() + local nextPosy = curPosy + nMoveY + local winSize = cc.Director:getInstance():getWinSize() + if nextPosy < 0 then + menu:setPosition(0, 0) + return + end + + if nextPosy > ((ExtensionTestEnum.TEST_MAX_COUNT + 1) * LINE_SPACE - winSize.height) then + menu:setPosition(0, ((ExtensionTestEnum.TEST_MAX_COUNT + 1) * LINE_SPACE - winSize.height)) + return + end + + menu:setPosition(curPosx, nextPosy) + beginPos = {x = location.x, y = location.y} + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesBegan,cc.Handler.EVENT_TOUCHES_BEGAN ) + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCH_MOVED ) + + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + return layer +end + +------------------------------------- +-- Extensions Test +------------------------------------- +function ExtensionsTestMain() + local scene = cc.Scene:create() + + scene:addChild(ExtensionsMainLayer()) + scene:addChild(CreateBackMenuItem()) + + return scene +end diff --git a/samples/lua-tests/src/ExtensionTest/WebProxyTest.lua b/samples/lua-tests/src/ExtensionTest/WebProxyTest.lua new file mode 100644 index 0000000000..9a2c203ffd --- /dev/null +++ b/samples/lua-tests/src/ExtensionTest/WebProxyTest.lua @@ -0,0 +1,202 @@ + + local function WebSocketTestLayer() + local layer = cc.Layer:create() + local winSize = cc.Director:getInstance():getWinSize() + + local MARGIN = 40 + local SPACE = 35 + + local wsSendText = nil + local wsSendBinary = nil + local wsError = nil + local sendTextStatus = nil + local sendBinaryStatus = nil + local errorStatus = nil + local receiveTextTimes = 0 + local receiveBinaryTimes = 0 + + local label = cc.LabelTTF:create("WebSocket Test", "Arial", 28) + label:setPosition(cc.p( winSize.width / 2, winSize.height - MARGIN)) + layer:addChild(label, 0) + + local menuRequest = cc.Menu:create() + menuRequest:setPosition(cc.p(0, 0)) + layer:addChild(menuRequest) + + --Send Text + local function onMenuSendTextClicked() + if nil ~= wsSendText then + if cc.WEBSOCKET_STATE_OPEN == wsSendText:getReadyState() then + sendTextStatus:setString("Send Text WS is waiting...") + wsSendText:sendString("Hello WebSocket中文, I'm a text message.") + else + local warningStr = "send text websocket instance wasn't ready..." + print(warningStr) + sendTextStatus:setString(warningStr) + end + end + end + local labelSendText = cc.LabelTTF:create("Send Text", "Arial", 22) + local itemSendText = cc.MenuItemLabel:create(labelSendText) + itemSendText:registerScriptTapHandler(onMenuSendTextClicked) + itemSendText:setPosition(cc.p(winSize.width / 2, winSize.height - MARGIN - SPACE)) + menuRequest:addChild(itemSendText) + + --Send Binary + local function onMenuSendBinaryClicked() + if nil ~= wsSendBinary then + if cc.WEBSOCKET_STATE_OPEN == wsSendBinary:getReadyState() then + sendBinaryStatus:setString("Send Binary WS is waiting...") + wsSendBinary:sendString("Hello WebSocket中文--,\0 I'm\0 a\0 binary\0 message\0.") + else + local warningStr = "send binary websocket instance wasn't ready..." + sendBinaryStatus:setString(warningStr) + end + end + end + local labelSendBinary = cc.LabelTTF:create("Send Binary", "Arial", 22) + local itemSendBinary = cc.MenuItemLabel:create(labelSendBinary) + itemSendBinary:registerScriptTapHandler(onMenuSendBinaryClicked) + itemSendBinary:setPosition(cc.p(winSize.width / 2, winSize.height - MARGIN - 2 * SPACE)) + menuRequest:addChild(itemSendBinary) + + --Send Text Status Label + sendTextStatus = cc.LabelTTF:create("Send Text WS is waiting...", "Arial", 14,cc.size(160, 100),cc.VERTICAL_TEXT_ALIGNMENT_CENTER,cc.VERTICAL_TEXT_ALIGNMENT_TOP) + sendTextStatus:setAnchorPoint(cc.p(0, 0)) + sendTextStatus:setPosition(cc.p(0, 25)) + layer:addChild(sendTextStatus) + + --Send Binary Status Label + sendBinaryStatus = cc.LabelTTF:create("Send Binary WS is waiting...", "Arial", 14, cc.size(160, 100), cc.VERTICAL_TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_TOP) + sendBinaryStatus:setAnchorPoint(cc.p(0, 0)) + sendBinaryStatus:setPosition(cc.p(160, 25)) + layer:addChild(sendBinaryStatus) + + --Error Label + errorStatus = cc.LabelTTF:create("Error WS is waiting...", "Arial", 14, cc.size(160, 100), cc.VERTICAL_TEXT_ALIGNMENT_CENTER, cc.VERTICAL_TEXT_ALIGNMENT_TOP) + errorStatus:setAnchorPoint(cc.p(0, 0)) + errorStatus:setPosition(cc.p(320, 25)) + layer:addChild(errorStatus) + + local toMainMenu = cc.Menu:create() + CreateExtensionsBasicLayerMenu(toMainMenu) + toMainMenu:setPosition(cc.p(0, 0)) + layer:addChild(toMainMenu,10) + + wsSendText = cc.WebSocket:create("ws://echo.websocket.org") + wsSendBinary = cc.WebSocket:create("ws://echo.websocket.org") + wsError = cc.WebSocket:create("ws://invalid.url.com") + + local function wsSendTextOpen(strData) + sendTextStatus:setString("Send Text WS was opened.") + end + + local function wsSendTextMessage(strData) + receiveTextTimes= receiveTextTimes + 1 + local strInfo= "response text msg: "..strData..", "..receiveTextTimes + sendTextStatus:setString(strInfo) + end + + local function wsSendTextClose(strData) + print("_wsiSendText websocket instance closed.") + sendTextStatus = nil + wsSendText = nil + end + + local function wsSendTextError(strData) + print("sendText Error was fired") + end + + local function wsSendBinaryOpen(strData) + sendBinaryStatus:setString("Send Binary WS was opened.") + end + + local function wsSendBinaryMessage(paramTable) + local length = table.getn(paramTable) + local i = 1 + local strInfo = "response bin msg: " + for i = 1,length do + if 0 == paramTable[i] then + strInfo = strInfo.."\'\\0\'" + else + strInfo = strInfo..string.char(paramTable[i]) + end + end + receiveBinaryTimes = receiveBinaryTimes + 1 + strInfo = strInfo..receiveBinaryTimes + sendBinaryStatus:setString(strInfo) + end + + local function wsSendBinaryClose(strData) + print("_wsiSendBinary websocket instance closed.") + sendBinaryStatus = nil + wsSendBinary = nil + end + + local function wsSendBinaryError(strData) + print("sendBinary Error was fired") + end + + local function wsErrorOpen(strData) + end + + local function wsErrorMessage(strData) + + end + + local function wsErrorError(strData) + print("Error was fired") + errorStatus:setString("an error was fired") + end + + local function wsErrorClose(strData) + print("_wsiError websocket instance closed.") + errorStatus= nil + wsError = nil + end + + if nil ~= wsSendText then + wsSendText:registerScriptHandler(wsSendTextOpen,cc.WEBSOCKET_OPEN) + wsSendText:registerScriptHandler(wsSendTextMessage,cc.WEBSOCKET_MESSAGE) + wsSendText:registerScriptHandler(wsSendTextClose,cc.WEBSOCKET_CLOSE) + wsSendText:registerScriptHandler(wsSendTextError,cc.WEBSOCKET_ERROR) + end + + if nil ~= wsSendBinary then + wsSendBinary:registerScriptHandler(wsSendBinaryOpen,cc.WEBSOCKET_OPEN) + wsSendBinary:registerScriptHandler(wsSendBinaryMessage,cc.WEBSOCKET_MESSAGE) + wsSendBinary:registerScriptHandler(wsSendBinaryClose,cc.WEBSOCKET_CLOSE) + wsSendBinary:registerScriptHandler(wsSendBinaryError,cc.WEBSOCKET_ERROR) + end + + if nil ~= wsError then + wsError:registerScriptHandler(wsErrorOpen,cc.WEBSOCKET_OPEN) + wsError:registerScriptHandler(wsErrorMessage,cc.WEBSOCKET_MESSAGE) + wsError:registerScriptHandler(wsErrorClose,cc.WEBSOCKET_CLOSE) + wsError:registerScriptHandler(wsErrorError,cc.WEBSOCKET_ERROR) + end + + local function OnExit(strEventName) + if "exit" == strEventName then + if nil ~= wsSendText then + wsSendText:close() + end + if nil ~= wsSendBinary then + wsSendBinary:close() + end + if nil ~= wsError then + wsError:close() + end + end + end + + layer:registerScriptHandler(OnExit) + + return layer +end + + function runWebSocketTest() + local scene = cc.Scene:create() + scene:addChild(WebSocketTestLayer()) + return scene +end diff --git a/samples/lua-tests/src/FontTest/FontTest.lua b/samples/lua-tests/src/FontTest/FontTest.lua new file mode 100644 index 0000000000..7ec20ed132 --- /dev/null +++ b/samples/lua-tests/src/FontTest/FontTest.lua @@ -0,0 +1,113 @@ +local kTagLabel1 = 0 +local kTagLabel2 = 1 +local kTagLabel3 = 2 +local kTagLabel4 = 3 + +local fontList = { + "fonts/A Damn Mess.ttf", + "fonts/Abberancy.ttf", + "fonts/Abduction.ttf", + "fonts/Paint Boy.ttf", + "fonts/Schwarzwald Regular.ttf", + "fonts/Scissor Cuts.ttf" +} + +local fontCount = table.getn(fontList) +cclog("font count = "..fontCount) + +local vAlignIdx = 1 +local verticalAlignment = { + cc.VERTICAL_TEXT_ALIGNMENT_TOP, + cc.VERTICAL_TEXT_ALIGNMENT_CENTER, + cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM, +} + +local vAlignCount = table.getn(verticalAlignment) + +local function showFont(ret, pFont) + + cclog("vAlignIdx="..vAlignIdx) + local s = cc.Director:getInstance():getWinSize() + + local blockSize = cc.size(s.width/3, 200) + local fontSize = 26 + + ret:removeChildByTag(kTagLabel1, true) + ret:removeChildByTag(kTagLabel2, true) + ret:removeChildByTag(kTagLabel3, true) + ret:removeChildByTag(kTagLabel4, true) + + local top = cc.LabelTTF:create(pFont, pFont, 24) + local left = cc.LabelTTF:create("alignment left", pFont, fontSize, + blockSize, cc.TEXT_ALIGNMENT_LEFT, verticalAlignment[vAlignIdx]) + local center = cc.LabelTTF:create("alignment center", pFont, fontSize, + blockSize, cc.TEXT_ALIGNMENT_CENTER, verticalAlignment[vAlignIdx]) + local right = cc.LabelTTF:create("alignment right", pFont, fontSize, + blockSize, cc.TEXT_ALIGNMENT_RIGHT, verticalAlignment[vAlignIdx]) + + local leftColor = cc.LayerColor:create(cc.c4b(100, 100, 100, 255), blockSize.width, blockSize.height) + local centerColor = cc.LayerColor:create(cc.c4b(200, 100, 100, 255), blockSize.width, blockSize.height) + local rightColor = cc.LayerColor:create(cc.c4b(100, 100, 200, 255), blockSize.width, blockSize.height) + + leftColor:ignoreAnchorPointForPosition(false) + centerColor:ignoreAnchorPointForPosition(false) + rightColor:ignoreAnchorPointForPosition(false) + + top:setAnchorPoint(cc.p(0.5, 1)) + left:setAnchorPoint(cc.p(0,0.5)) + leftColor:setAnchorPoint(cc.p(0,0.5)) + center:setAnchorPoint(cc.p(0,0.5)) + centerColor:setAnchorPoint(cc.p(0,0.5)) + right:setAnchorPoint(cc.p(0,0.5)) + rightColor:setAnchorPoint(cc.p(0,0.5)) + + top:setPosition(cc.p(s.width/2,s.height-20)) + left:setPosition(cc.p(0,s.height/2)) + leftColor:setPosition(left:getPosition()) + center:setPosition(cc.p(blockSize.width, s.height/2)) + centerColor:setPosition(center:getPosition()) + right:setPosition(cc.p(blockSize.width*2, s.height/2)) + rightColor:setPosition(right:getPosition()) + + ret:addChild(leftColor, -1) + ret:addChild(left, 0, kTagLabel1) + ret:addChild(rightColor, -1) + ret:addChild(right, 0, kTagLabel2) + ret:addChild(centerColor, -1) + ret:addChild(center, 0, kTagLabel3) + ret:addChild(top, 0, kTagLabel4) +end + +local isFirst = true +local originCreateLayer = createTestLayer +local function createTestLayer() + if isFirst == false then + if Helper.index == 1 then + vAlignIdx = vAlignIdx % vAlignCount + 1 + end + else + isFirst = false + end + + local ret = originCreateLayer("") + showFont(ret, fontList[Helper.index]) + return ret +end + +function FontTestMain() + cclog("FontTestMain") + Helper.index = 1 + vAlignIdx = 1 + local scene = cc.Scene:create() + Helper.createFunctionTable = { + createTestLayer, + createTestLayer, + createTestLayer, + createTestLayer, + createTestLayer, + createTestLayer + } + scene:addChild(createTestLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/IntervalTest/IntervalTest.lua b/samples/lua-tests/src/IntervalTest/IntervalTest.lua new file mode 100644 index 0000000000..3f09c0bf0e --- /dev/null +++ b/samples/lua-tests/src/IntervalTest/IntervalTest.lua @@ -0,0 +1,136 @@ +local scheduler = cc.Director:getInstance():getScheduler() +local SID_STEP1 = 100 +local SID_STEP2 = 101 +local SID_STEP3 = 102 +local IDC_PAUSE = 200 + +local function IntervalLayer() + local ret = cc.Layer:create() + local m_time0 = 0 + local m_time1 = 0 + local m_time2 = 0 + local m_time3 = 0 + local m_time4 = 0 + + local s = cc.Director:getInstance():getWinSize() + -- sun + local sun = cc.ParticleSun:create() + sun:setTexture(cc.Director:getInstance():getTextureCache():addImage("Images/fire.png")) + sun:setPosition( cc.p(VisibleRect:rightTop().x-32,VisibleRect:rightTop().y-32) ) + + sun:setTotalParticles(130) + sun:setLife(0.6) + ret:addChild(sun) + + -- timers + m_label0 = cc.LabelBMFont:create("0", "fonts/bitmapFontTest4.fnt") + m_label1 = cc.LabelBMFont:create("0", "fonts/bitmapFontTest4.fnt") + m_label2 = cc.LabelBMFont:create("0", "fonts/bitmapFontTest4.fnt") + m_label3 = cc.LabelBMFont:create("0", "fonts/bitmapFontTest4.fnt") + m_label4 = cc.LabelBMFont:create("0", "fonts/bitmapFontTest4.fnt") + + local function update(dt) + m_time0 = m_time0 + dt + local str = string.format("%2.1f", m_time0) + m_label0:setString(str) + end + + ret:scheduleUpdateWithPriorityLua(update, 0) + + local function step1(dt) + m_time1 = m_time1 + dt + local str = string.format("%2.1f", m_time1) + m_label1:setString( str ) + end + + local function step2(dt) + m_time2 = m_time2 + dt + local str = string.format("%2.1f", m_time2) + m_label2:setString( str ) + end + + local function step3(dt) + m_time3 = m_time3 + dt + local str = string.format("%2.1f", m_time3) + m_label3:setString( str ) + end + + local function step4(dt) + m_time4 = m_time4 + dt + local str = string.format("%2.1f", m_time4) + m_label4:setString( str ) + end + + local schedulerEntry1 = nil + local schedulerEntry2 = nil + local schedulerEntry3 = nil + local schedulerEntry4 = nil + + local function onNodeEvent(event) + if event == "enter" then + schedulerEntry1 = scheduler:scheduleScriptFunc(step1, 0, false) + schedulerEntry2 = scheduler:scheduleScriptFunc(step2, 0, false) + schedulerEntry3 = scheduler:scheduleScriptFunc(step3, 1.0, false) + schedulerEntry4 = scheduler:scheduleScriptFunc(step4, 2.0, false) + elseif event == "exit" then + scheduler:unscheduleScriptEntry(schedulerEntry1) + scheduler:unscheduleScriptEntry(schedulerEntry2) + scheduler:unscheduleScriptEntry(schedulerEntry3) + scheduler:unscheduleScriptEntry(schedulerEntry4) + if cc.Director:getInstance():isPaused() then + cc.Director:getInstance():resume() + end + end + end + + ret:registerScriptHandler(onNodeEvent) + + + m_label0:setPosition(cc.p(s.width*1/6, s.height/2)) + m_label1:setPosition(cc.p(s.width*2/6, s.height/2)) + m_label2:setPosition(cc.p(s.width*3/6, s.height/2)) + m_label3:setPosition(cc.p(s.width*4/6, s.height/2)) + m_label4:setPosition(cc.p(s.width*5/6, s.height/2)) + + ret:addChild(m_label0) + ret:addChild(m_label1) + ret:addChild(m_label2) + ret:addChild(m_label3) + ret:addChild(m_label4) + + -- Sprite + local sprite = cc.Sprite:create(s_pPathGrossini) + sprite:setPosition( cc.p(VisibleRect:left().x + 40, VisibleRect:bottom().y + 50) ) + + local jump = cc.JumpBy:create(3, cc.p(s.width-80,0), 50, 4) + + ret:addChild(sprite) + sprite:runAction( cc.RepeatForever:create(cc.Sequence:create(jump, jump:reverse()))) + -- pause button + local item1 = cc.MenuItemFont:create("Pause") + local function onPause(tag, pSender) + if cc.Director:getInstance():isPaused() then + cc.Director:getInstance():resume() + else + cc.Director:getInstance():pause() + end + end + + item1:registerScriptTapHandler(onPause) + local menu = cc.Menu:create(item1) + menu:setPosition( cc.p(s.width/2, s.height-50) ) + + ret:addChild( menu ) + + return ret +end + + +function IntervalTestMain() + cclog("IntervalTestMain") + local scene = cc.Scene:create() + local layer = IntervalLayer() + scene:addChild(layer, 0) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/KeypadTest/KeypadTest.lua b/samples/lua-tests/src/KeypadTest/KeypadTest.lua new file mode 100644 index 0000000000..850a908225 --- /dev/null +++ b/samples/lua-tests/src/KeypadTest/KeypadTest.lua @@ -0,0 +1,49 @@ +local function KeypadMainLayer() + local layer = cc.Layer:create() + + local function onEnter() + print("come in") + local s = cc.Director:getInstance():getWinSize() + local label = cc.LabelTTF:create("Keypad Test", "Arial", 28) + layer:addChild(label, 0) + label:setPosition(cc.p(s.width/2, s.height-50)) + + local labelTip = cc.LabelTTF:create("Please press any key...", "Arial", 22) + labelTip:setPosition(cc.p(s.width / 2, s.height / 2)) + layer:addChild(labelTip, 0) + + local function onKeyReleased(keyCode, event) + local label = event:getCurrentTarget() + if keyCode == cc.KeyCode.KEY_BACKSPACE then + label:setString("BACK clicked!") + elseif keyCode == cc.KeyCode.KEY_MENU then + label:setString("MENU clicked!") + end + end + + local listener = cc.EventListenerKeyboard:create() + listener:registerScriptHandler(onKeyReleased, cc.Handler.EVENT_KEYBOARD_RELEASED ) + + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, labelTip) + end + + local function onNodeEvent(event) + if event == "enter" then + onEnter() + end + end + + layer:registerScriptHandler(onNodeEvent) + + return layer +end + + +function KeypadTestMain() + cclog("KeypadTestMain") + local scene = cc.Scene:create() + scene:addChild(KeypadMainLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/LabelTest/LabelTest.lua b/samples/lua-tests/src/LabelTest/LabelTest.lua new file mode 100644 index 0000000000..c92d347fcb --- /dev/null +++ b/samples/lua-tests/src/LabelTest/LabelTest.lua @@ -0,0 +1,1409 @@ +local size = cc.Director:getInstance():getWinSize() +local scheduler = cc.Director:getInstance():getScheduler() + +local kTagTileMap = 1 +local kTagSpriteManager = 1 +local kTagAnimation1 = 1 +local kTagBitmapAtlas1 = 1 +local kTagBitmapAtlas2 = 2 +local kTagBitmapAtlas3 = 3 +local kTagSprite1 = 0 +local kTagSprite2 = 1 +local kTagSprite3 = 2 +local kTagSprite4 = 3 +local kTagSprite5 = 4 +local kTagSprite6 = 5 +local kTagSprite7 = 6 +local kTagSprite8 = 7 + +-------------------------------------------------------------------- +-- +-- LabelAtlasTest +-- +-------------------------------------------------------------------- + +local LabelAtlasTest = {} +LabelAtlasTest.layer = nil +LabelAtlasTest.__index = LabelAtlasTest +local m_time = 0 + +function LabelAtlasTest.step(dt) + m_time = m_time + dt + local string = string.format("%2.2f Test", m_time) + + local label1_origin = LabelAtlasTest.layer:getChildByTag(kTagSprite1) + local label1 = tolua.cast(label1_origin, "cc.LabelAtlas") + label1:setString(string) -- + + local label2_origin = LabelAtlasTest.layer:getChildByTag(kTagSprite2) + local label2 = tolua.cast(label2_origin, "cc.LabelAtlas") + string = string.format("%d", m_time) + + label2:setString(string) +end + +function LabelAtlasTest.onNodeEvent(tag) + if tag == "exit" then + LabelAtlasTest.layer:unscheduleUpdate() + end +end + +function LabelAtlasTest.create() + m_time = 0 + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + LabelAtlasTest.layer = layer + + local label1 = cc.LabelAtlas:_create("123 Test", "fonts/tuffy_bold_italic-charmap.plist") + layer:addChild(label1, 0, kTagSprite1) + label1:setPosition( cc.p(10,100) ) + label1:setOpacity( 200 ) + + local label2 = cc.LabelAtlas:_create("0123456789", "fonts/tuffy_bold_italic-charmap.plist") + layer:addChild(label2, 0, kTagSprite2) + label2:setPosition( cc.p(10,200) ) + label2:setOpacity( 32 ) + + layer:scheduleUpdateWithPriorityLua(LabelAtlasTest.step, 0) + + Helper.titleLabel:setString("LabelAtlas") + Helper.subtitleLabel:setString("Updating label should be fast") + + layer:registerScriptHandler(LabelAtlasTest.onNodeEvent) + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelAtlasColorTest +-- +-------------------------------------------------------------------- + +local LabelAtlasColorTest = {} +LabelAtlasColorTest.layer = nil +LabelAtlasColorTest.__index = LabelAtlasColorTest +local m_time = 0 + +function LabelAtlasColorTest.step(dt) + m_time = m_time + dt + local string = string.format("%2.2f Test", m_time) + local label1_origin = LabelAtlasColorTest.layer:getChildByTag(kTagSprite1) + local label1 = tolua.cast(label1_origin, "cc.LabelAtlas") + label1:setString(string) + + local label2_origin = LabelAtlasColorTest.layer:getChildByTag(kTagSprite2) + local label2 = tolua.cast(label2_origin, "cc.LabelAtlas") + string = string.format("%d", m_time) + + label2:setString(string) +end + +function LabelAtlasColorTest.actionFinishCallback() + cclog("Action finished") +end + +function LabelAtlasColorTest.onNodeEvent(tag) + if tag == "exit" then + LabelAtlasColorTest.layer:unscheduleUpdate() + end +end + +function LabelAtlasColorTest.create() + m_time = 0 + + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + LabelAtlasColorTest.layer = layer + + local label1 = cc.LabelAtlas:_create("123 Test", "fonts/tuffy_bold_italic-charmap.plist") + layer:addChild(label1, 0, kTagSprite1) + label1:setPosition( cc.p(10,100) ) + label1:setOpacity( 200 ) + + local label2 = cc.LabelAtlas:_create("0123456789", "fonts/tuffy_bold_italic-charmap.plist") + layer:addChild(label2, 0, kTagSprite2) + label2:setPosition( cc.p(10,200) ) + label2:setColor(cc.c3b(255, 0, 0)) + + local fade = cc.FadeOut:create(1.0) + local fade_in = fade:reverse() + + local cb = cc.CallFunc:create(LabelAtlasColorTest.actionFinishCallback) + + local seq = cc.Sequence:create(fade, fade_in, cb) + local repeatAction = cc.RepeatForever:create( seq ) + label2:runAction( repeatAction ) + + layer:registerScriptHandler(LabelAtlasColorTest.onNodeEvent) + layer:scheduleUpdateWithPriorityLua(LabelAtlasColorTest.step, 0) + + Helper.titleLabel:setString("LabelAtlas") + Helper.subtitleLabel:setString("Opacity + Color should work at the same time") + + return layer +end + +-------------------------------------------------------------------- +-- +-- Atlas3 +-- +-- Use any of these editors to generate BMFonts: +-- http://glyphdesigner.71squared.com/ (Commercial, Mac OS X) +-- http://www.n4te.com/hiero/hiero.jnlp (Free, Java) +-- http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) +-- http://www.angelcode.com/products/bmfont/ (Free, Windows only) +-- +-------------------------------------------------------------------- +local Atlas3 = {} +Atlas3.layer = nil +Atlas3.__index = Atlas3 + +function Atlas3.onNodeEvent(tag) + if tag == "exit" then + Atlas3.layer:unscheduleUpdate() + end +end + +function Atlas3.create() + cclog("Atlas3.create") + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Atlas3.layer = layer + + m_time = 0 + + local col = cc.LayerColor:create( cc.c4b(128,128,128,255) ) + layer:addChild(col, -10) + + local label1 = cc.LabelBMFont:create("Test", "fonts/bitmapFontTest2.fnt") + + -- testing anchors + label1:setAnchorPoint( cc.p(0,0) ) + layer:addChild(label1, 0, kTagBitmapAtlas1) + + local fade = cc.FadeOut:create(1.0) + local fade_in = fade:reverse() + + local seq = cc.Sequence:create(fade,fade_in) + local repeatAction = cc.RepeatForever:create(seq) + label1:runAction(repeatAction) + + --VERY IMPORTANT + --color and opacity work OK because bitmapFontAltas2 loads a BMP image (not a PNG image) + --If you want to use both opacity and color, it is recommended to use NON premultiplied images like BMP images + --Of course, you can also tell XCode not to compress PNG images, but I think it doesn't work as expected + local label2 = cc.LabelBMFont:create("Test", "fonts/bitmapFontTest2.fnt") + + -- testing anchors + label2:setAnchorPoint( cc.p(0.5, 0.5) ) + label2:setColor(cc.c3b(255, 0, 0 )) + layer:addChild(label2, 0, kTagBitmapAtlas2) + + label2:runAction( tolua.cast(repeatAction:clone(), "cc.Action") ) + + local label3 = cc.LabelBMFont:create("Test", "fonts/bitmapFontTest2.fnt") + -- testing anchors + label3:setAnchorPoint( cc.p(1,1) ) + layer:addChild(label3, 0, kTagBitmapAtlas3) + + label1:setPosition( VisibleRect:leftBottom() ) + label2:setPosition( VisibleRect:center() ) + label3:setPosition( VisibleRect:rightTop() ) + + layer:registerScriptHandler(Atlas3.onNodeEvent) + layer:scheduleUpdateWithPriorityLua(Atlas3.step, 0) + + Helper.titleLabel:setString( "LabelBMFont" ) + Helper.subtitleLabel:setString( "Testing alignment. Testing opacity + tint" ) + + return layer +end + +function Atlas3.step(dt) + m_time = m_time + dt + local string = string.format("%2.2f Test j", m_time) + + local label1 = tolua.cast(Atlas3.layer:getChildByTag(kTagBitmapAtlas1), "cc.LabelBMFont") + label1:setString(string) + + local label2 = tolua.cast(Atlas3.layer:getChildByTag(kTagBitmapAtlas2), "cc.LabelBMFont") + label2:setString(string) + + local label3 = tolua.cast(Atlas3.layer:getChildByTag(kTagBitmapAtlas3), "cc.LabelBMFont") + label3:setString(string) +end + +-------------------------------------------------------------------- +-- +-- Atlas4 +-- +-- Use any of these editors to generate BMFonts: +-- http:--glyphdesigner.71squared.com/ (Commercial, Mac OS X) +-- http:--www.n4te.com/hiero/hiero.jnlp (Free, Java) +-- http:--slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) +-- http:--www.angelcode.com/products/bmfont/ (Free, Windows only) +-- +-------------------------------------------------------------------- +local Atlas4 = {} +Atlas4.layer = nil +Atlas4.__index = Atlas4 +Atlas4.stepEntry = nil + +function Atlas4.onNodeEvent(tag) + if tag == "enter" then + Atlas4.stepEntry = scheduler:scheduleScriptFunc(Atlas4.step, 0.1, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(Atlas4.stepEntry) + end +end + +function Atlas4.create() + cclog("Atlas4.create") + m_time = 0 + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Atlas4.layer = layer + + -- Upper Label + local label = cc.LabelBMFont:create("Bitmap Font Atlas", "fonts/bitmapFontTest.fnt") + layer:addChild(label) + + local s = cc.Director:getInstance():getWinSize() + + label:setPosition( cc.p(s.width/2, s.height/2) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + + local BChar = label:getChildByTag(0) + local FChar = label:getChildByTag(7) + local AChar = label:getChildByTag(12) + + + local rotate = cc.RotateBy:create(2, 360) + local rot_4ever = cc.RepeatForever:create(rotate) + + local scale = cc.ScaleBy:create(2, 1.5) + local scale_back = scale:reverse() + + local scale_seq = cc.Sequence:create(scale, scale_back) + local scale_4ever = cc.RepeatForever:create(scale_seq) + + local jump = cc.JumpBy:create(0.5, cc.p(0, 0), 60, 1) + local jump_4ever = cc.RepeatForever:create(jump) + + local fade_out = cc.FadeOut:create(1) + local fade_in = cc.FadeIn:create(1) + + local seq = cc.Sequence:create(fade_out, fade_in) + local fade_4ever = cc.RepeatForever:create(seq) + + BChar:runAction(rot_4ever) + BChar:runAction(scale_4ever) + FChar:runAction(jump_4ever) + AChar:runAction(fade_4ever) + + + -- Bottom Label + local label2 = cc.LabelBMFont:create("00.0", "fonts/bitmapFontTest.fnt") + layer:addChild(label2, 0, kTagBitmapAtlas2) + label2:setPosition( cc.p(s.width/2.0, 80) ) + + local lastChar = label2:getChildByTag(3) + lastChar:runAction(tolua.cast( rot_4ever:clone(), "cc.Action" )) + + layer:registerScriptHandler(Atlas4.onNodeEvent) + + Helper.titleLabel:setString("LabelBMFont") + Helper.subtitleLabel:setString( "Using fonts as cc.Sprite objects. Some characters should rotate.") + return layer +end + +function Atlas4.draw() + local s = cc.Director:getInstance():getWinSize() + cc.DrawPrimitives.drawLine( cc.p(0, s.height/2), cc.p(s.width, s.height/2) ) + cc.DrawPrimitives.drawLine( cc.p(s.width/2, 0), cc.p(s.width/2, s.height) ) +end + +function Atlas4.step(dt) + m_time = m_time + dt + + local string = string.format("%04.1f", m_time) + + local label1 = tolua.cast(Atlas4.layer:getChildByTag(kTagBitmapAtlas2), "cc.LabelBMFont") + label1:setString(string) +end + + +-------------------------------------------------------------------- +-- +-- Atlas5 +-- +-- Use any of these editors to generate BMFonts: +-- http:--glyphdesigner.71squared.com/ (Commercial, Mac OS X) +-- http:--www.n4te.com/hiero/hiero.jnlp (Free, Java) +-- http:--slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) +-- http:--www.angelcode.com/products/bmfont/ (Free, Windows only) +-- +-------------------------------------------------------------------- + +local Atlas5 = {} +Atlas5.layer = nil +function Atlas5:create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Atlas5.layer = layer + + local label = cc.LabelBMFont:create("abcdefg", "fonts/bitmapFontTest4.fnt") + layer:addChild(label) + + local s = cc.Director:getInstance():getWinSize() + + label:setPosition( cc.p(s.width/2, s.height/2) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + Helper.titleLabel:setString("LabelBMFont") + Helper.subtitleLabel:setString("Testing padding") + return layer +end + +-------------------------------------------------------------------- +-- +-- Atlas6 +-- +-- Use any of these editors to generate BMFonts: +-- http:--glyphdesigner.71squared.com/ (Commercial, Mac OS X) +-- http:--www.n4te.com/hiero/hiero.jnlp (Free, Java) +-- http:--slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) +-- http:--www.angelcode.com/products/bmfont/ (Free, Windows only) +-- +-------------------------------------------------------------------- +local Atlas6 = {} +Atlas6.layer = nil + +function Atlas6:create() + cclog("Atlas6:create") + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Atlas6.layer = layer + + local s = cc.Director:getInstance():getWinSize() + local label = cc.LabelBMFont:create("FaFeFiFoFu", "fonts/bitmapFontTest5.fnt") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, s.height/2+50) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + label = cc.LabelBMFont:create("fafefifofu", "fonts/bitmapFontTest5.fnt") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, s.height/2) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + label = cc.LabelBMFont:create("aeiou", "fonts/bitmapFontTest5.fnt") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, s.height/2-50) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + Helper.titleLabel:setString("LabelBMFont") + Helper.subtitleLabel:setString("Rendering should be OK. Testing offset") + return layer +end + +-------------------------------------------------------------------- +-- +-- AtlasBitmapColor +-- +-- Use any of these editors to generate BMFonts: +-- http:--glyphdesigner.71squared.com/ (Commercial, Mac OS X) +-- http:--www.n4te.com/hiero/hiero.jnlp (Free, Java) +-- http:--slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) +-- http:--www.angelcode.com/products/bmfont/ (Free, Windows only) +-- +-------------------------------------------------------------------- +local AtlasBitmapColor = { layer= nil } +function AtlasBitmapColor:create() + local layer = cc.Layer:create() + AtlasBitmapColor.layer = layer + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local label = cc.LabelBMFont:create("Blue", "fonts/bitmapFontTest5.fnt") + label:setColor( cc.c3b(0, 0, 255 )) + layer:addChild(label) + label:setPosition( cc.p(s.width/2, s.height/4) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + label = cc.LabelBMFont:create("Red", "fonts/bitmapFontTest5.fnt") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, 2*s.height/4) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + label:setColor( cc.c3b(255, 0, 0) ) + + label = cc.LabelBMFont:create("G", "fonts/bitmapFontTest5.fnt") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, 3*s.height/4) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + label:setColor( cc.c3b(0, 255, 0 )) + label:setString("Green") + + Helper.titleLabel:setString("LabelBMFont") + Helper.subtitleLabel:setString("Testing color") + return layer +end + +-------------------------------------------------------------------- +-- +-- AtlasFastBitmap +-- +-- Use any of these editors to generate BMFonts: +-- http:--glyphdesigner.71squared.com/ (Commercial, Mac OS X) +-- http:--www.n4te.com/hiero/hiero.jnlp (Free, Java) +-- http:--slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) +-- http:--www.angelcode.com/products/bmfont/ (Free, Windows only) +-- +-------------------------------------------------------------------- + +local AtlasFastBitmap = { layer = nil } +function AtlasFastBitmap:create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + AtlasFastBitmap.layer = layer + + math.randomseed(os.time()) + -- Upper Label + local i = 0 + for i = 0, 100, 1 do + local str = string.format("-%d-", i) + local label = cc.LabelBMFont:create(str, "fonts/bitmapFontTest.fnt") + layer:addChild(label) + + local s = cc.Director:getInstance():getWinSize() + + local p = cc.p( math.random() * s.width, math.random() * s.height) + label:setPosition( p ) + label:setAnchorPoint(cc.p(0.5, 0.5)) + end + + Helper.titleLabel:setString("LabelBMFont") + Helper.subtitleLabel:setString("Creating several cc.LabelBMFont with the same .fnt file should be fast") + return layer +end + +-------------------------------------------------------------------- +-- +-- BitmapFontMultiLine +-- +-- Use any of these editors to generate BMFonts: +-- http:--glyphdesigner.71squared.com/ (Commercial, Mac OS X) +-- http:--www.n4te.com/hiero/hiero.jnlp (Free, Java) +-- http:--slick.cokeandcode.com/demos/hiero.jnlp (Free, Java) +-- http:--www.angelcode.com/products/bmfont/ (Free, Windows only) +-- +-------------------------------------------------------------------- +local BitmapFontMultiLine = {} + +function BitmapFontMultiLine:create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + local s = nil + + -- Left + local label1 = cc.LabelBMFont:create(" Multi line\nLeft", "fonts/bitmapFontTest3.fnt") + label1:setAnchorPoint(cc.p(0,0)) + layer:addChild(label1, 0, kTagBitmapAtlas1) + + s = label1:getContentSize() + cclog("content size: %.2fx%.2f", s.width, s.height) + + + -- Center + local label2 = cc.LabelBMFont:create("Multi line\nCenter", "fonts/bitmapFontTest3.fnt") + label2:setAnchorPoint(cc.p(0.5, 0.5)) + layer:addChild(label2, 0, kTagBitmapAtlas2) + + s= label2:getContentSize() + cclog("content size: %.2fx%.2f", s.width, s.height) + + -- right + local label3 = cc.LabelBMFont:create("Multi line\nRight\nThree lines Three", "fonts/bitmapFontTest3.fnt") + label3:setAnchorPoint(cc.p(1, 1)) + layer:addChild(label3, 0, kTagBitmapAtlas3) + + s = label3:getContentSize() + cclog("content size: %.2fx%.2f", s.width, s.height) + + label1:setPosition(VisibleRect:leftBottom()) + label2:setPosition(VisibleRect:center()) + label3:setPosition(VisibleRect:rightTop()) + Helper.titleLabel:setString("LabelBMFont") + Helper.subtitleLabel:setString("Multiline + anchor point") + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelsEmpty +-- +-------------------------------------------------------------------- + +local LabelsEmpty = {} +LabelsEmpty.layer = nil +LabelsEmpty.setEmpty = false +LabelsEmpty.updateEntry = nil + +function LabelsEmpty.onNodeEvent(tag) + if tag == "enter" then + LabelsEmpty.updateEntry = scheduler:scheduleScriptFunc(LabelsEmpty.updateStrings, 1.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(LabelsEmpty.updateEntry) + end +end + + +function LabelsEmpty.create() + cclog("LabelsEmpty.create") + + local layer = cc.Layer:create() + LabelsEmpty.layer = layer + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + -- cc.LabelBMFont + local label1 = cc.LabelBMFont:create("", "fonts/bitmapFontTest3.fnt") + layer:addChild(label1, 0, kTagBitmapAtlas1) + label1:setPosition(cc.p(s.width/2, s.height-100)) + + -- cc.LabelTTF + local label2 = cc.LabelTTF:create("", "Arial", 24) + layer:addChild(label2, 0, kTagBitmapAtlas2) + label2:setPosition(cc.p(s.width/2, s.height/2)) + + -- cc.LabelAtlas + local label3 = cc.LabelAtlas:_create("", "fonts/tuffy_bold_italic-charmap.png", 48, 64, string.byte(" ")) + layer:addChild(label3, 0, kTagBitmapAtlas3) + label3:setPosition(cc.p(s.width/2, 0+100)) + + layer:registerScriptHandler(LabelsEmpty.onNodeEvent) + + LabelsEmpty.setEmpty = false + Helper.titleLabel:setString("Testing empty labels") + Helper.subtitleLabel:setString("3 empty labels: LabelAtlas, LabelTTF and LabelBMFont") + return layer +end + +function LabelsEmpty.updateStrings(dt) + local label1 = tolua.cast(LabelsEmpty.layer:getChildByTag(kTagBitmapAtlas1), "cc.LabelBMFont") + local label2 = tolua.cast(LabelsEmpty.layer:getChildByTag(kTagBitmapAtlas2), "cc.LabelTTF") + local label3 = tolua.cast(LabelsEmpty.layer:getChildByTag(kTagBitmapAtlas3), "cc.LabelAtlas") + + if( LabelsEmpty.setEmpty == false) then + label1:setString("not empty") + label2:setString("not empty") + label3:setString("hi") + LabelsEmpty.setEmpty = true + else + label1:setString("") + label2:setString("") + label3:setString("") + LabelsEmpty.setEmpty = false + end +end + +-------------------------------------------------------------------- +-- +-- LabelBMFontHD +-- +-------------------------------------------------------------------- +local LabelBMFontHD = { + +} + +function LabelBMFontHD.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + -- cc.LabelBMFont + local label1 = cc.LabelBMFont:create("TESTING RETINA DISPLAY", "fonts/konqa32.fnt") + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/2)) + + Helper.titleLabel:setString("Testing Retina Display BMFont") + Helper.subtitleLabel:setString("loading arista16 or arista16-hd") + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelAtlasHD +-- +-------------------------------------------------------------------- +local LabelAtlasHD = {} +function LabelAtlasHD.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + -- cc.LabelBMFont + local label1 = cc.LabelAtlas:_create("TESTING RETINA DISPLAY", "fonts/larabie-16.plist") + label1:setAnchorPoint(cc.p(0.5, 0.5)) + + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/2)) + + Helper.titleLabel:setString("LabelAtlas with Retina Display") + Helper.subtitleLabel:setString("loading larabie-16 / larabie-16-hd") + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelGlyphDesigner +-- +-------------------------------------------------------------------- + +local LabelGlyphDesigner = {} +function LabelGlyphDesigner.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local colorlayer = cc.LayerColor:create(cc.c4b(128,128,128,255)) + layer:addChild(colorlayer, -10) + + -- cc.LabelBMFont + local label1 = cc.LabelBMFont:create("Testing Glyph Designer", "fonts/futura-48.fnt") + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/2)) + Helper.titleLabel:setString("Testing Glyph Designer") + Helper.subtitleLabel:setString("You should see a font with shawdows and outline") + + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelTTFTest +-- +-------------------------------------------------------------------- + +local LabelTTFTest = { + _layer = nil, + _plabel = nil, + _eHorizAlign = cc.TEXT_ALIGNMENT_LEFT, + _eVertAlign = cc.VERTICAL_TEXT_ALIGNMENT_TOP +} + +function LabelTTFTest.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + LabelTTFTest._layer = layer + LabelTTFTest._plabel = nil + LabelTTFTest._eHorizAlign = cc.TEXT_ALIGNMENT_LEFT + LabelTTFTest._eVertAlign = cc.VERTICAL_TEXT_ALIGNMENT_TOP + + local blockSize = cc.size(200, 160) + local s = cc.Director:getInstance():getWinSize() + + local colorLayer = cc.LayerColor:create(cc.c4b(100, 100, 100, 255), blockSize.width, blockSize.height) + colorLayer:setAnchorPoint(cc.p(0,0)) + colorLayer:setPosition(cc.p((s.width - blockSize.width) / 2, (s.height - blockSize.height) / 2)) + + layer:addChild(colorLayer) + + cc.MenuItemFont:setFontSize(30) + local item1 = cc.MenuItemFont:create("Left") + item1:registerScriptTapHandler(LabelTTFTest.setAlignmentLeft) + local item2 = cc.MenuItemFont:create("Center") + item2:registerScriptTapHandler(LabelTTFTest.setAlignmentCenter) + local item3 = cc.MenuItemFont:create("Right") + item3:registerScriptTapHandler(LabelTTFTest.setAlignmentRight) + + local menu = cc.Menu:create() + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + menu:alignItemsVerticallyWithPadding(4) + menu:setPosition(cc.p(50, s.height / 2 - 20)) + layer:addChild(menu) + + menu = cc.Menu:create() + + item1 = cc.MenuItemFont:create("Top") + item1:registerScriptTapHandler(LabelTTFTest.setAlignmentTop) + item2 = cc.MenuItemFont:create("Middle") + item2:registerScriptTapHandler(LabelTTFTest.setAlignmentMiddle) + item3 = cc.MenuItemFont:create("Bottom") + item3:registerScriptTapHandler(LabelTTFTest.setAlignmentBottom) + + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + + menu:alignItemsVerticallyWithPadding(4) + menu:setPosition(cc.p(s.width - 50, s.height / 2 - 20)) + layer:addChild(menu) + + LabelTTFTest.updateAlignment() + + layer:registerScriptHandler(LabelTTFTest.onNodeEvent) + + Helper.titleLabel:setString("Testing cc.LabelTTF") + Helper.subtitleLabel:setString("Select the buttons on the sides to change alignment") + + return layer +end + +function LabelTTFTest.onNodeEvent(tag) + if tag == "exit" then + if LabelTTFTest._plabel ~= nil then + LabelTTFTest._plabel:release() + end + end +end + +function LabelTTFTest.updateAlignment() + local blockSize = cc.size(200, 160) + local s = cc.Director:getInstance():getWinSize() + + if LabelTTFTest._plabel ~= nil then + LabelTTFTest._plabel:removeFromParent(true) + LabelTTFTest._plabel:release() + end + + LabelTTFTest._plabel = cc.LabelTTF:create(LabelTTFTest.getCurrentAlignment(), "Marker Felt", 32, + blockSize, LabelTTFTest._eHorizAlign, LabelTTFTest._eVertAlign) + LabelTTFTest._plabel:retain() + + LabelTTFTest._plabel:setAnchorPoint(cc.p(0,0)) + LabelTTFTest._plabel:setPosition(cc.p((s.width - blockSize.width) / 2, (s.height - blockSize.height)/2 )) + + LabelTTFTest._layer:addChild(LabelTTFTest._plabel) +end + +function LabelTTFTest.setAlignmentLeft(pSender) + LabelTTFTest._eHorizAlign = cc.TEXT_ALIGNMENT_LEFT + LabelTTFTest.updateAlignment() +end + +function LabelTTFTest.setAlignmentCenter(pSender) + LabelTTFTest._eHorizAlign = cc.TEXT_ALIGNMENT_CENTER + LabelTTFTest.updateAlignment() +end + +function LabelTTFTest.setAlignmentRight(pSender) + LabelTTFTest._eHorizAlign = cc.TEXT_ALIGNMENT_RIGHT + LabelTTFTest.updateAlignment() +end + +function LabelTTFTest.setAlignmentTop(pSender) + LabelTTFTest._eVertAlign = cc.VERTICAL_TEXT_ALIGNMENT_TOP + LabelTTFTest.updateAlignment() +end + +function LabelTTFTest.setAlignmentMiddle(pSender) + LabelTTFTest._eVertAlign = cc.VERTICAL_TEXT_ALIGNMENT_CENTER + LabelTTFTest.updateAlignment() +end + +function LabelTTFTest.setAlignmentBottom(pSender) + LabelTTFTest._eVertAlign = cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM + LabelTTFTest.updateAlignment() +end + +function LabelTTFTest.getCurrentAlignment() + local vertical = nil + local horizontal = nil + if LabelTTFTest._eVertAlign == cc.VERTICAL_TEXT_ALIGNMENT_TOP then + vertical = "Top" + elseif LabelTTFTest._eVertAlign == cc.VERTICAL_TEXT_ALIGNMENT_CENTER then + vertical = "Middle" + elseif LabelTTFTest._eVertAlign == cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM then + vertical = "Bottom" + end + + if LabelTTFTest._eHorizAlign == cc.TEXT_ALIGNMENT_LEFT then + horizontal = "Left" + elseif LabelTTFTest._eHorizAlign == cc.TEXT_ALIGNMENT_CENTER then + horizontal = "Center" + elseif LabelTTFTest._eHorizAlign == cc.TEXT_ALIGNMENT_RIGHT then + horizontal = "Right" + end + + return string.format("Alignment %s %s", vertical, horizontal) +end + +-------------------------------------------------------------------- +-- +-- Atlas1 +-- +-------------------------------------------------------------------- +--Atlas1:Atlas1() +--{ +-- m_textureAtlas = cc.TextureAtlas:create(s_AtlasTest, 3); m_textureAtlas:retain(); +-- +-- cc.size s = cc.Director:getInstance():getWinSize(); +-- +-- -- +-- -- Notice: u,v tex coordinates are inverted +-- -- +-- V3F_C4B_T2F_Quad quads[] = +-- { +-- { +-- {{0,0,0},cc.c4b(0,0,255,255),{0.0f,1.0f},}, -- bottom left +-- {{s.width,0,0},cc.c4b(0,0,255,0),{1.0f,1.0f},}, -- bottom right +-- {{0,s.height,0},cc.c4b(0,0,255,0),{0.0f,0.0f},}, -- top left +-- {{s.width,s.height,0},{0,0,255,255},{1.0f,0.0f},}, -- top right +-- }, +-- { +-- {{40,40,0},cc.c4b(255,255,255,255),{0.0f,0.2f},}, -- bottom left +-- {{120,80,0},cc.c4b(255,0,0,255),{0.5f,0.2f},}, -- bottom right +-- {{40,160,0},cc.c4b(255,255,255,255),{0.0f,0.0f},}, -- top left +-- {{160,160,0},cc.c4b(0,255,0,255),{0.5f,0.0f},}, -- top right +-- }, +-- +-- { +-- {{s.width/2,40,0},cc.c4b(255,0,0,255),{0.0f,1.0f},}, -- bottom left +-- {{s.width,40,0},cc.c4b(0,255,0,255),{1.0f,1.0f},}, -- bottom right +-- {{s.width/2-50,200,0},cc.c4b(0,0,255,255),{0.0f,0.0f},}, -- top left +-- {{s.width,100,0},cc.c4b(255,255,0,255),{1.0f,0.0f},}, -- top right +-- }, +-- +-- }; +-- +-- +-- for( int i=0;i<3;i++) +-- { +-- m_textureAtlas:updateQuad(&quads[i], i); +-- } +--} +-- +--Atlas1:~Atlas1() +--{ +-- m_textureAtlas:release(); +--} +-- +--void Atlas1:draw() +--{ +-- -- GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY +-- -- GL_TEXTURE_2D +-- +-- m_textureAtlas:drawQuads(); +-- +---- [textureAtlas drawNumberOfQuads:3]; +-- +--} +-- +--std:string Atlas1:title() +--{ +-- return "TextureAtlas"; +--} +-- +--std:string Atlas1:subtitle() +--{ +-- return "Manual creation of cc.TextureAtlas"; +--} + +local LabelTTFMultiline = { + layer = nil +} + +function LabelTTFMultiline.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local center = cc.LabelTTF:create("word wrap \"testing\" (bla0) bla1 'bla2' [bla3] (bla4) {bla5} {bla6} [bla7] (bla8) [bla9] 'bla0' \"bla1\"", + "Paint Boy", + 32, + cc.size(s.width/2,200), + cc.TEXT_ALIGNMENT_CENTER, + cc.VERTICAL_TEXT_ALIGNMENT_TOP) + + center:setPosition(cc.p(s.width / 2, 150)) + + layer:addChild(center) + Helper.titleLabel:setString("Testing cc.LabelTTF Word Wrap") + Helper.subtitleLabel:setString("Word wrap using cc.LabelTTF and a custom TTF font") + + return layer +end + +local LabelTTFChinese = {} + +function LabelTTFChinese.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + local size = cc.Director:getInstance():getWinSize() + local pLable = cc.LabelTTF:create("中国", "Marker Felt", 30) + pLable:setPosition(cc.p(size.width / 2, size.height /2)) + layer:addChild(pLable) + Helper.titleLabel:setString("Testing cc.LabelTTF with Chinese character") + return layer +end + +local LabelBMFontChinese = {} +function LabelBMFontChinese.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local size = cc.Director:getInstance():getWinSize() + local pLable = cc.LabelBMFont:create("中国", "fonts/bitmapFontChinese.fnt") + pLable:setPosition(cc.p(size.width / 2, size.height /2)) + layer:addChild(pLable) + + Helper.titleLabel:setString("Testing cc.LabelBMFont with Chinese character") + return layer +end + +-- BitmapFontMultiLineAlignment + +local LongSentencesExample = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." +local LineBreaksExample = "Lorem ipsum dolor\nsit amet\nconsectetur adipisicing elit\nblah\nblah" +local MixedExample = "ABC\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt\nDEF" + +local ArrowsMax = 0.95 +local ArrowsMin = 0.7 + +local LeftAlign = 0 +local CenterAlign = 1 +local RightAlign = 2 + +local LongSentences = 0 +local LineBreaks = 1 +local Mixed = 2 + +local alignmentItemPadding = 50 +local menuItemPaddingCenter = 50 + +local BitmapFontMultiLineAlignment = { + _pLabelShouldRetain = nil, + _pArrowsBarShouldRetain = nil, + _pArrowsShouldRetain = nil, + _pLastSentenceItem = nil, + _drag = false, +} + +function BitmapFontMultiLineAlignment.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + -- ask director the the window size + local size = cc.Director:getInstance():getWinSize() + + -- create and initialize a Label + BitmapFontMultiLineAlignment._pLabelShouldRetain = cc.LabelBMFont:create(LongSentencesExample, "fonts/markerFelt.fnt", size.width/1.5, cc.TEXT_ALIGNMENT_CENTER) + BitmapFontMultiLineAlignment._pLabelShouldRetain:retain() + + BitmapFontMultiLineAlignment._pArrowsBarShouldRetain = cc.Sprite:create("Images/arrowsBar.png") + BitmapFontMultiLineAlignment._pArrowsBarShouldRetain:retain() + BitmapFontMultiLineAlignment._pArrowsShouldRetain = cc.Sprite:create("Images/arrows.png") + BitmapFontMultiLineAlignment._pArrowsShouldRetain:retain() + + cc.MenuItemFont:setFontSize(20) + local longSentences = cc.MenuItemFont:create("Long Flowing Sentences") + longSentences:registerScriptTapHandler(BitmapFontMultiLineAlignment.stringChanged) + local lineBreaks = cc.MenuItemFont:create("Short Sentences With Intentional Line Breaks") + lineBreaks:registerScriptTapHandler(BitmapFontMultiLineAlignment.stringChanged) + local mixed = cc.MenuItemFont:create("Long Sentences Mixed With Intentional Line Breaks") + mixed:registerScriptTapHandler(BitmapFontMultiLineAlignment.stringChanged) + local stringMenu = cc.Menu:create() + stringMenu:addChild(longSentences) + stringMenu:addChild(lineBreaks) + stringMenu:addChild(mixed) + stringMenu:alignItemsVertically() + + longSentences:setColor(cc.c3b(255, 0, 0)) + BitmapFontMultiLineAlignment._pLastSentenceItem = longSentences + + longSentences:setTag(LongSentences) + lineBreaks:setTag(LineBreaks) + mixed:setTag(Mixed) + + cc.MenuItemFont:setFontSize(30) + + local left = cc.MenuItemFont:create("Left") + left:registerScriptTapHandler(BitmapFontMultiLineAlignment.alignmentChanged) + local center = cc.MenuItemFont:create("Center") + center:registerScriptTapHandler(BitmapFontMultiLineAlignment.alignmentChanged) + local right = cc.MenuItemFont:create("Right") + right:registerScriptTapHandler(BitmapFontMultiLineAlignment.alignmentChanged) + + local alignmentMenu = cc.Menu:create() + alignmentMenu:addChild(left) + alignmentMenu:addChild(center) + alignmentMenu:addChild(right) + + alignmentMenu:alignItemsHorizontallyWithPadding(alignmentItemPadding) + + center:setColor(cc.c3b(255, 0, 0)) + BitmapFontMultiLineAlignment._pLastAlignmentItem = center + left:setTag(LeftAlign) + center:setTag(CenterAlign) + right:setTag(RightAlign) + + -- position the label on the center of the screen + BitmapFontMultiLineAlignment._pLabelShouldRetain:setPosition(cc.p(size.width/2, size.height/2)) + + BitmapFontMultiLineAlignment._pArrowsBarShouldRetain:setVisible(false) + + local arrowsWidth = (ArrowsMax - ArrowsMin) * size.width + BitmapFontMultiLineAlignment._pArrowsBarShouldRetain:setScaleX(arrowsWidth / BitmapFontMultiLineAlignment._pArrowsBarShouldRetain:getContentSize().width) + BitmapFontMultiLineAlignment._pArrowsBarShouldRetain:setPosition( ((ArrowsMax + ArrowsMin) / 2) * size.width, BitmapFontMultiLineAlignment._pLabelShouldRetain:getPositionY() ) + + BitmapFontMultiLineAlignment.snapArrowsToEdge() + + stringMenu:setPosition(cc.p(size.width/2, size.height - menuItemPaddingCenter)) + alignmentMenu:setPosition(cc.p(size.width/2, menuItemPaddingCenter+15)) + + layer:addChild(BitmapFontMultiLineAlignment._pLabelShouldRetain) + layer:addChild(BitmapFontMultiLineAlignment._pArrowsBarShouldRetain) + layer:addChild(BitmapFontMultiLineAlignment._pArrowsShouldRetain) + layer:addChild(stringMenu) + layer:addChild(alignmentMenu) + layer:registerScriptHandler(BitmapFontMultiLineAlignment.onNodeEvent) + + local function onTouchesBegan(touches, event) + local location = touches[1]:getLocationInView() + if cc.rectContainsPoint(BitmapFontMultiLineAlignment._pArrowsShouldRetain:getBoundingBox(), location) then + BitmapFontMultiLineAlignment._drag = true + BitmapFontMultiLineAlignment._pArrowsBarShouldRetain:setVisible(true) + end + end + + local function onTouchesMoved(touches, event) + if BitmapFontMultiLine._drag == false then + return + end + local location = touches[1]:getLocationInView() + local winSize = cc.Director:getInstance():getWinSize() + BitmapFontMultiLineAlignment._pArrowsShouldRetain:setPosition( + math.max(math.min(location.x, ArrowsMax*winSize.width), ArrowsMin*winSize.width), + BitmapFontMultiLineAlignment._pArrowsShouldRetain:getPositionY()) + + local labelWidth = math.abs(BitmapFontMultiLineAlignment._pArrowsShouldRetain:getPositionX() - BitmapFontMultiLineAlignment._pLabelShouldRetain:getPositionX()) * 2 + + BitmapFontMultiLineAlignment._pLabelShouldRetain:setWidth(labelWidth) + end + + local function onTouchesEnded(touch, event) + BitmapFontMultiLineAlignment._drag = false + BitmapFontMultiLineAlignment.snapArrowsToEdge() + BitmapFontMultiLineAlignment._pArrowsBarShouldRetain:setVisible(false) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesBegan,cc.Handler.EVENT_TOUCHES_BEGAN ) + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + listener:registerScriptHandler(onTouchesEnded,cc.Handler.EVENT_TOUCHES_ENDED ) + + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + return layer +end + +function BitmapFontMultiLineAlignment.onNodeEvent(tag) + if tag == "onexit" then + BitmapFontMultiLineAlignment._pLabelShouldRetain:release() + BitmapFontMultiLineAlignment._pArrowsBarShouldRetain:release() + BitmapFontMultiLineAlignment._pArrowsShouldRetain:release() + end +end + + + +function BitmapFontMultiLineAlignment.stringChanged(tag, sender) + local item = tolua.cast(sender, "cc.MenuItemFont") + item:setColor(cc.c3b(255, 0, 0)) + BitmapFontMultiLineAlignment._pLastAlignmentItem:setColor(cc.c3b(255, 255, 255)) + BitmapFontMultiLineAlignment._pLastAlignmentItem = item + + if item:getTag() == LongSentences then + BitmapFontMultiLineAlignment._pLabelShouldRetain:setString(LongSentencesExample) + elseif item:getTag() == LineBreaks then + BitmapFontMultiLineAlignment._pLabelShouldRetain:setString(LineBreaksExample) + elseif item:getTag() == Mixed then + BitmapFontMultiLineAlignment._pLabelShouldRetain:setString(MixedExample) + end + + BitmapFontMultiLineAlignment.snapArrowsToEdge() +end + +function BitmapFontMultiLineAlignment.alignmentChanged(tag, sender) + -- cclog("BitmapFontMultiLineAlignment.alignmentChanged, tag:"..tag) + local item = tolua.cast(sender, "cc.MenuItemFont") + item:setColor(cc.c3b(255, 0, 0)) + BitmapFontMultiLineAlignment._pLastAlignmentItem:setColor(cc.c3b(255, 255, 255)) + BitmapFontMultiLineAlignment._pLastAlignmentItem = item + + if tag == LeftAlign then + cclog("LeftAlign") + BitmapFontMultiLineAlignment._pLabelShouldRetain:setAlignment(cc.TEXT_ALIGNMENT_LEFT) + elseif tag == CenterAlign then + BitmapFontMultiLineAlignment._pLabelShouldRetain:setAlignment(cc.TEXT_ALIGNMENT_CENTER) + elseif tag == RightAlign then + BitmapFontMultiLineAlignment._pLabelShouldRetain:setAlignment(cc.TEXT_ALIGNMENT_RIGHT) + end + + BitmapFontMultiLineAlignment.snapArrowsToEdge() +end + +function BitmapFontMultiLineAlignment.snapArrowsToEdge() + BitmapFontMultiLineAlignment._pArrowsShouldRetain:setPosition( + BitmapFontMultiLineAlignment._pLabelShouldRetain:getPositionX() + BitmapFontMultiLineAlignment._pLabelShouldRetain:getContentSize().width/2, BitmapFontMultiLineAlignment._pLabelShouldRetain:getPositionY() + ) +end + +--/ LabelTTFA8Test + +local LabelTTFA8Test = {} + +function LabelTTFA8Test.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local colorlayer = cc.LayerColor:create(cc.c4b(128, 128, 128, 255)) + layer:addChild(colorlayer, -10) + + -- cc.LabelBMFont + local label1 = cc.LabelTTF:create("Testing A8 Format", "Marker Felt", 48) + layer:addChild(label1) + label1:setColor(cc.c3b(255, 0, 0)) + label1:setPosition(cc.p(s.width/2, s.height/2)) + + local fadeOut = cc.FadeOut:create(2) + local fadeIn = cc.FadeIn:create(2) + + local seq = cc.Sequence:create(fadeOut, fadeIn) + local forever = cc.RepeatForever:create(seq) + label1:runAction(forever) + + Helper.titleLabel:setString("Testing A8 Format") + Helper.subtitleLabel:setString("RED label, fading In and Out in the center of the screen") + return layer +end + +--/ BMFontOneAtlas +local BMFontOneAtlas = {} +function BMFontOneAtlas.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local label1 = cc.LabelBMFont:create("This is Helvetica", "fonts/helvetica-32.fnt", cc.LABEL_AUTOMATIC_WIDTH, cc.TEXT_ALIGNMENT_LEFT, cc.p(0, 0)) + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/3*2)) + + local label2 = cc.LabelBMFont:create("And this is Geneva", "fonts/geneva-32.fnt", cc.LABEL_AUTOMATIC_WIDTH, cc.TEXT_ALIGNMENT_LEFT, cc.p(0, 128)) + layer:addChild(label2) + label2:setPosition(cc.p(s.width/2, s.height/3*1)) + Helper.titleLabel:setString("LabelBMFont with one texture") + Helper.subtitleLabel:setString("Using 2 .fnt definitions that share the same texture atlas.") + return layer +end + +--/ BMFontUnicode +local BMFontUnicode = {} +function BMFontUnicode.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("LabelBMFont with Unicode support") + Helper.subtitleLabel:setString("You should see 3 differnt labels: In Spanish, Chinese and Korean") + + local s = cc.Director:getInstance():getWinSize() + + local label1 = cc.LabelBMFont:create("Buen día", "fonts/arial-unicode-26.fnt", 200, cc.TEXT_ALIGNMENT_LEFT) + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/4*3)) + + local label2 = cc.LabelBMFont:create("美好的一天", "fonts/arial-unicode-26.fnt") + layer:addChild(label2) + label2:setPosition(cc.p(s.width/2, s.height/4*2)) + + local label3 = cc.LabelBMFont:create("良い一日を", "fonts/arial-unicode-26.fnt") + layer:addChild(label3) + label3:setPosition(cc.p(s.width/2, s.height/4*1)) + + return layer +end + +--BMFontInit + +local BMFontInit = {} +function BMFontInit.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("LabelBMFont init") + Helper.subtitleLabel:setString("Test for support of init method without parameters.") + + local s = cc.Director:getInstance():getWinSize() + local bmFont = cc.LabelBMFont:create() + --cc.LabelBMFont* bmFont = [cc.LabelBMFont create:@"Foo" fntFile:@"arial-unicode-26.fnt"] + bmFont:setFntFile("fonts/helvetica-32.fnt") + bmFont:setString("It is working!") + layer:addChild(bmFont) + bmFont:setPosition(cc.p(s.width/2,s.height/4*2)) + return layer +end + + +-- TTFFontInit + +local TTFFontInit = {} +function TTFFontInit.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("LabelTTF init") + Helper.subtitleLabel:setString("Test for support of init method without parameters.") + + local s = cc.Director:getInstance():getWinSize() + + local font = cc.LabelTTF:create() + font:setFontName("Marker Felt") + font:setFontSize(48) + font:setString("It is working!") + layer:addChild(font) + font:setPosition(cc.p(s.width/2,s.height/4*2)) + return layer +end + +-- Issue1343 + +local Issue1343 = {} +function Issue1343.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("Issue 1343") + Helper.subtitleLabel:setString("You should see: ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.,'") + + local s = cc.Director:getInstance():getWinSize() + + local bmFont = cc.LabelBMFont:create() + bmFont:setFntFile("fonts/font-issue1343.fnt") + bmFont:setString("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.,'") + layer:addChild(bmFont) + bmFont:setScale(0.3) + + bmFont:setPosition(cc.p(s.width/2,s.height/4*2)) + return layer +end + +local LabelBMFontBounds = {} +function LabelBMFontBounds.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("Testing LabelBMFont Bounds") + Helper.subtitleLabel:setString("You should see string enclosed by a box") + + local s = cc.Director:getInstance():getWinSize() + + local colorlayer = cc.LayerColor:create(cc.c4b(128,128,128,255)) + layer:addChild(colorlayer, -10) + + -- cc.LabelBMFont + local label1 = cc.LabelBMFont:create("Testing Glyph Designer", "fonts/boundsTestFont.fnt") + + + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/2)) + return layer +end + + +function LabelBMFontBounds.draw() + -- cc.size labelSize = label1:getContentSize() + -- cc.size origin = cc.Director:getInstance():getWinSize() + + -- origin.width = origin.width / 2 - (labelSize.width / 2) + -- origin.height = origin.height / 2 - (labelSize.height / 2) + + -- cc.p vertices[4]= + + -- cc.p(origin.width, origin.height), + -- cc.p(labelSize.width + origin.width, origin.height), + -- cc.p(labelSize.width + origin.width, labelSize.height + origin.height), + -- cc.p(origin.width, labelSize.height + origin.height) + -- end + -- ccDrawPoly(vertices, 4, true) +end + +-------------------------------------------------------------------- +-- +-- LabelTTFAlignment +-- +-------------------------------------------------------------------- +local LabelTTFAlignment = {} +function LabelTTFAlignment.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("LabelTTF alignment") + Helper.subtitleLabel:setString("Tests alignment values") + + local s = cc.Director:getInstance():getWinSize() + + local ttf0 = cc.LabelTTF:create("Alignment 0\nnew line", "Helvetica", 12, + cc.size(256, 32), cc.TEXT_ALIGNMENT_LEFT) + ttf0:setPosition(cc.p(s.width/2,(s.height/6)*2)) + ttf0:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(ttf0) + + local ttf1 = cc.LabelTTF:create("Alignment 1\nnew line", "Helvetica", 12, + cc.size(245, 32), cc.TEXT_ALIGNMENT_CENTER) + ttf1:setPosition(cc.p(s.width/2,(s.height/6)*3)) + ttf1:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(ttf1) + + local ttf2 = cc.LabelTTF:create("Alignment 2\nnew line", "Helvetica", 12, + cc.size(245, 32), cc.TEXT_ALIGNMENT_RIGHT) + ttf2:setPosition(cc.p(s.width/2,(s.height/6)*4)) + ttf2:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(ttf2) + return layer +end + +function LabelTest() + cclog("LabelTest") + m_time = 0 + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + LabelAtlasTest.create, + LabelAtlasColorTest.create, + Atlas3.create, + Atlas4.create, + Atlas5.create, + Atlas6.create, + AtlasBitmapColor.create, + AtlasFastBitmap.create, + BitmapFontMultiLine.create, + LabelsEmpty.create, + LabelBMFontHD.create, + LabelAtlasHD.create, + LabelGlyphDesigner.create, + -- Atlas1, + LabelTTFTest.create, + LabelTTFMultiline.create, + LabelTTFChinese.create, + LabelBMFontChinese.create, + BitmapFontMultiLineAlignment.create, + LabelTTFA8Test.create, + BMFontOneAtlas.create, + BMFontUnicode.create, + BMFontInit.create, + TTFFontInit.create, + Issue1343.create, + LabelTTFAlignment.create, + LabelBMFontBounds.create + } + scene:addChild(LabelAtlasTest.create()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/LabelTestNew/LabelTestNew.lua b/samples/lua-tests/src/LabelTestNew/LabelTestNew.lua new file mode 100644 index 0000000000..7b24b2d865 --- /dev/null +++ b/samples/lua-tests/src/LabelTestNew/LabelTestNew.lua @@ -0,0 +1,1337 @@ +local size = cc.Director:getInstance():getWinSize() +local scheduler = cc.Director:getInstance():getScheduler() + +local kTagTileMap = 1 +local kTagSpriteManager = 1 +local kTagAnimation1 = 1 +local kTagBitmapAtlas1 = 1 +local kTagBitmapAtlas2 = 2 +local kTagBitmapAtlas3 = 3 +local kTagSprite1 = 0 +local kTagSprite2 = 1 +local kTagSprite3 = 2 +local kTagSprite4 = 3 +local kTagSprite5 = 4 +local kTagSprite6 = 5 +local kTagSprite7 = 6 +local kTagSprite8 = 7 + +-------------------------------------------------------------------- +-- +-- LabelFNTColorAndOpacity +-- +-------------------------------------------------------------------- +local LabelFNTColorAndOpacity = {} +LabelFNTColorAndOpacity.layer = nil +LabelFNTColorAndOpacity.__index = LabelFNTColorAndOpacity + +function LabelFNTColorAndOpacity.onNodeEvent(tag) + if tag == "exit" then + LabelFNTColorAndOpacity.layer:unscheduleUpdate() + end +end + +function LabelFNTColorAndOpacity.create() + cclog("LabelFNTColorAndOpacity.create") + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + LabelFNTColorAndOpacity.layer = layer + + m_time = 0 + + local col = cc.LayerColor:create( cc.c4b(128,128,128,255) ) + layer:addChild(col, -10) + + local label1 = cc.Label:createWithBMFont("fonts/bitmapFontTest2.fnt", "Test") + + -- testing anchors + label1:setAnchorPoint( cc.p(0,0) ) + layer:addChild(label1, 0, kTagBitmapAtlas1) + + local fade = cc.FadeOut:create(1.0) + local fade_in = fade:reverse() + + local seq = cc.Sequence:create(fade,fade_in) + local repeatAction = cc.RepeatForever:create(seq) + label1:runAction(repeatAction) + + local label2 = cc.Label:createWithBMFont("fonts/bitmapFontTest2.fnt", "Test") + -- testing anchors + label2:setAnchorPoint( cc.p(0.5, 0.5) ) + label2:setColor(cc.c3b(255, 0, 0 )) + layer:addChild(label2, 0, kTagBitmapAtlas2) + + label2:runAction( tolua.cast(repeatAction:clone(), "cc.Action") ) + + local label3 = cc.Label:createWithBMFont("fonts/bitmapFontTest2.fnt", "Test") + -- testing anchors + label3:setAnchorPoint( cc.p(1,1) ) + layer:addChild(label3, 0, kTagBitmapAtlas3) + + label1:setPosition( VisibleRect:leftBottom() ) + label2:setPosition( VisibleRect:center() ) + label3:setPosition( VisibleRect:rightTop() ) + + layer:registerScriptHandler(LabelFNTColorAndOpacity.onNodeEvent) + layer:scheduleUpdateWithPriorityLua(LabelFNTColorAndOpacity.step, 0) + + Helper.titleLabel:setString( "New Label + .FNT file" ) + Helper.subtitleLabel:setString( "Testing opacity + tint" ) + + return layer +end + +function LabelFNTColorAndOpacity.step(dt) + m_time = m_time + dt + local string = string.format("%2.2f Test j", m_time) + + local label1 = tolua.cast(LabelFNTColorAndOpacity.layer:getChildByTag(kTagBitmapAtlas1), "cc.Label") + label1:setString(string) + + local label2 = tolua.cast(LabelFNTColorAndOpacity.layer:getChildByTag(kTagBitmapAtlas2), "cc.Label") + label2:setString(string) + + local label3 = tolua.cast(LabelFNTColorAndOpacity.layer:getChildByTag(kTagBitmapAtlas3), "cc.Label") + label3:setString(string) +end + +-------------------------------------------------------------------- +-- +-- LabelFNTSpriteActions +-- +-------------------------------------------------------------------- +local LabelFNTSpriteActions = {} +LabelFNTSpriteActions.layer = nil +LabelFNTSpriteActions.__index = LabelFNTSpriteActions +LabelFNTSpriteActions.stepEntry = nil + +function LabelFNTSpriteActions.onNodeEvent(tag) + if tag == "enter" then + LabelFNTSpriteActions.stepEntry = scheduler:scheduleScriptFunc(LabelFNTSpriteActions.step, 0.1, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(LabelFNTSpriteActions.stepEntry) + end +end + +function LabelFNTSpriteActions.create() + cclog("LabelFNTSpriteActions.create") + m_time = 0 + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + LabelFNTSpriteActions.layer = layer + + -- Upper Label + local label = cc.Label:createWithBMFont("fonts/bitmapFontTest.fnt", "Bitmap Font Atlas") + layer:addChild(label) + + local s = cc.Director:getInstance():getWinSize() + + label:setPosition( cc.p(s.width/2, s.height/2) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + + local BChar = label:getLetter(0) + local FChar = label:getLetter(7) + local AChar = label:getLetter(12) + + + local rotate = cc.RotateBy:create(2, 360) + local rot_4ever = cc.RepeatForever:create(rotate) + + local scale = cc.ScaleBy:create(2, 1.5) + local scale_back = scale:reverse() + + local scale_seq = cc.Sequence:create(scale, scale_back) + local scale_4ever = cc.RepeatForever:create(scale_seq) + + local jump = cc.JumpBy:create(0.5, cc.p(0, 0), 60, 1) + local jump_4ever = cc.RepeatForever:create(jump) + + local fade_out = cc.FadeOut:create(1) + local fade_in = cc.FadeIn:create(1) + + local seq = cc.Sequence:create(fade_out, fade_in) + local fade_4ever = cc.RepeatForever:create(seq) + + BChar:runAction(rot_4ever) + BChar:runAction(scale_4ever) + FChar:runAction(jump_4ever) + AChar:runAction(fade_4ever) + + + -- Bottom Label + local label2 = cc.Label:createWithBMFont("fonts/bitmapFontTest.fnt", "00.0") + layer:addChild(label2, 0, kTagBitmapAtlas2) + label2:setPosition( cc.p(s.width/2.0, 80) ) + + local lastChar = label2:getLetter(3) + lastChar:runAction(tolua.cast( rot_4ever:clone(), "cc.Action" )) + + layer:registerScriptHandler(LabelFNTSpriteActions.onNodeEvent) + + Helper.titleLabel:setString("New Label + .FNT file") + Helper.subtitleLabel:setString( "Using fonts as Sprite objects. Some characters should rotate.") + return layer +end + +function LabelFNTSpriteActions.draw() + local s = cc.Director:getInstance():getWinSize() + cc.DrawPrimitives.drawLine( cc.p(0, s.height/2), cc.p(s.width, s.height/2) ) + cc.DrawPrimitives.drawLine( cc.p(s.width/2, 0), cc.p(s.width/2, s.height) ) +end + +function LabelFNTSpriteActions.step(dt) + m_time = m_time + dt + + local string = string.format("%04.1f", m_time) + + local label1 = LabelFNTSpriteActions.layer:getChildByTag(kTagBitmapAtlas2) + label1:setString(string) +end + + +-------------------------------------------------------------------- +-- +-- LabelFNTPadding +-- +-------------------------------------------------------------------- + +local LabelFNTPadding = {} +LabelFNTPadding.layer = nil +function LabelFNTPadding:create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + LabelFNTPadding.layer = layer + + local label = cc.Label:createWithBMFont("fonts/bitmapFontTest4.fnt", "abcdefg") + layer:addChild(label) + + local s = cc.Director:getInstance():getWinSize() + + label:setPosition( cc.p(s.width/2, s.height/2) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + Helper.titleLabel:setString("New Label + .FNT file") + Helper.subtitleLabel:setString("Testing padding") + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelFNTOffset +-- +-------------------------------------------------------------------- +local LabelFNTOffset = {} +LabelFNTOffset.layer = nil + +function LabelFNTOffset:create() + cclog("LabelFNTOffset:create") + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + LabelFNTOffset.layer = layer + + local s = cc.Director:getInstance():getWinSize() + local label = cc.Label:createWithBMFont("fonts/bitmapFontTest5.fnt", "FaFeFiFoFu") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, s.height/2+50) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + label = cc.Label:createWithBMFont("fonts/bitmapFontTest5.fnt", "fafefifofu") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, s.height/2) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + label = cc.Label:createWithBMFont("fonts/bitmapFontTest5.fnt", "aeiou") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, s.height/2-50) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + Helper.titleLabel:setString("New Label + .FNT file") + Helper.subtitleLabel:setString("Rendering should be OK. Testing offset") + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelFNTColor +-- +-------------------------------------------------------------------- +local LabelFNTColor = { layer= nil } +function LabelFNTColor:create() + local layer = cc.Layer:create() + LabelFNTColor.layer = layer + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local label = cc.Label:createWithBMFont("fonts/bitmapFontTest5.fnt", "Blue") + label:setColor( cc.c3b(0, 0, 255 )) + layer:addChild(label) + label:setPosition( cc.p(s.width/2, s.height/4) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + + label = cc.Label:createWithBMFont("fonts/bitmapFontTest5.fnt", "Red") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, 2*s.height/4) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + label:setColor( cc.c3b(255, 0, 0) ) + + label = cc.Label:createWithBMFont("fonts/bitmapFontTest5.fnt", "Green") + layer:addChild(label) + label:setPosition( cc.p(s.width/2, 3*s.height/4) ) + label:setAnchorPoint( cc.p(0.5, 0.5) ) + label:setColor( cc.c3b(0, 255, 0 )) + label:setString("Green") + + Helper.titleLabel:setString("New Label + .FNT file") + Helper.subtitleLabel:setString("Testing color") + return layer +end + + +-------------------------------------------------------------------- +-- +-- LabelTTFColor +-- +-------------------------------------------------------------------- +local LabelTTFColor = { layer= nil } +function LabelTTFColor:create() + local layer = cc.Layer:create() + LabelTTFColor.layer = layer + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + local ttfConfig = {} + ttfConfig.fontFilePath="fonts/arial.ttf" + ttfConfig.fontSize=35 + + local label1 = cc.Label:createWithTTF(ttfConfig,"Green", cc.VERTICAL_TEXT_ALIGNMENT_CENTER, s.width) + label1:setColor( cc.c3b(0, 255, 0 )) + layer:addChild(label1) + label1:setPosition( cc.p(s.width/2, s.height/5 * 1.5) ) + label1:setAnchorPoint( cc.p(0.5, 0.5) ) + + local label2 = cc.Label:createWithTTF(ttfConfig, "Red", cc.VERTICAL_TEXT_ALIGNMENT_CENTER, s.width) + layer:addChild(label2) + label2:setPosition( cc.p(s.width/2, s.height/5 * 2.0) ) + label2:setAnchorPoint( cc.p(0.5, 0.5) ) + label2:setColor( cc.c3b(255, 0, 0) ) + + local label3 = cc.Label:createWithTTF(ttfConfig, "Blue", cc.VERTICAL_TEXT_ALIGNMENT_CENTER, s.width) + layer:addChild(label3) + label3:setPosition( cc.p(s.width/2, s.height/5 * 2.5) ) + label3:setAnchorPoint( cc.p(0.5, 0.5) ) + label3:setColor( cc.c3b(0, 0, 255 )) + + Helper.titleLabel:setString("New Label + .TTF") + Helper.subtitleLabel:setString("Uses the new Label with TTF. Testing Color") + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelFNTHundredLabels +-- +-------------------------------------------------------------------- + +local LabelFNTHundredLabels = { layer = nil } +function LabelFNTHundredLabels:create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + LabelFNTHundredLabels.layer = layer + + math.randomseed(os.time()) + -- Upper Label + local i = 0 + for i = 0, 100, 1 do + local str = string.format("-%d-", i) + local label = cc.Label:createWithBMFont("fonts/bitmapFontTest.fnt", str) + layer:addChild(label) + + local s = cc.Director:getInstance():getWinSize() + + local p = cc.p( math.random() * s.width, math.random() * s.height) + label:setPosition( p ) + label:setAnchorPoint(cc.p(0.5, 0.5)) + end + + Helper.titleLabel:setString("New Label + .FNT file") + Helper.subtitleLabel:setString("Creating several Labels using the same FNT file should be fast") + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelFNTMultiLine +-- +-- +-------------------------------------------------------------------- +local LabelFNTMultiLine = {} + +function LabelFNTMultiLine:create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + local s = nil + + -- Left + local label1 = cc.Label:createWithBMFont("fonts/bitmapFontTest3.fnt", " Multi line\nLeft") + label1:setAnchorPoint(cc.p(0,0)) + layer:addChild(label1, 0, kTagBitmapAtlas1) + + s = label1:getContentSize() + cclog("content size: %.2fx%.2f", s.width, s.height) + + + -- Center + local label2 = cc.Label:createWithBMFont("fonts/bitmapFontTest3.fnt", "Multi line\nCenter") + label2:setAnchorPoint(cc.p(0.5, 0.5)) + layer:addChild(label2, 0, kTagBitmapAtlas2) + + s= label2:getContentSize() + cclog("content size: %.2fx%.2f", s.width, s.height) + + -- right + local label3 = cc.Label:createWithBMFont("fonts/bitmapFontTest3.fnt", "Multi line\nRight\nThree lines Three") + label3:setAnchorPoint(cc.p(1, 1)) + layer:addChild(label3, 0, kTagBitmapAtlas3) + + s = label3:getContentSize() + cclog("content size: %.2fx%.2f", s.width, s.height) + + label1:setPosition(VisibleRect:leftBottom()) + label2:setPosition(VisibleRect:center()) + label3:setPosition(VisibleRect:rightTop()) + Helper.titleLabel:setString("New Label + .FNT file") + Helper.subtitleLabel:setString("Multiline + anchor point") + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelFNTandTTFEmpty +-- +-------------------------------------------------------------------- + +local LabelFNTandTTFEmpty = {} +LabelFNTandTTFEmpty.layer = nil +LabelFNTandTTFEmpty.setEmpty = false +LabelFNTandTTFEmpty.updateEntry = nil + +function LabelFNTandTTFEmpty.onNodeEvent(tag) + if tag == "enter" then + LabelFNTandTTFEmpty.updateEntry = scheduler:scheduleScriptFunc(LabelFNTandTTFEmpty.updateStrings, 1.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(LabelFNTandTTFEmpty.updateEntry) + end +end + + +function LabelFNTandTTFEmpty.create() + cclog("LabelFNTandTTFEmpty.create") + + local layer = cc.Layer:create() + LabelFNTandTTFEmpty.layer = layer + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + -- cc.LabelBMFont + local label1 = cc.Label:createWithBMFont("fonts/bitmapFontTest3.fnt", "", cc.TEXT_ALIGNMENT_CENTER,s.width) + layer:addChild(label1, 0, kTagBitmapAtlas1) + label1:setPosition(cc.p(s.width/2, s.height-100)) + + -- cc.LabelTTF + local ttfConfig = {} + ttfConfig.fontFilePath = "fonts/arial.ttf" + ttfConfig.fontSize = 48 + local label2 = cc.Label:createWithTTF(ttfConfig, "", cc.TEXT_ALIGNMENT_CENTER, s.width) + layer:addChild(label2, 0, kTagBitmapAtlas2) + label2:setAnchorPoint(cc.p(0.5, 0.5)) + label2:setPosition(cc.p(s.width/2, s.height/2)) + + layer:registerScriptHandler(LabelFNTandTTFEmpty.onNodeEvent) + + LabelFNTandTTFEmpty.setEmpty = false + Helper.titleLabel:setString("New Label : .FNT file & .TTF file") + Helper.subtitleLabel:setString("2 empty labels: new Label + .FNT and new Label + .TTF") + return layer +end + +function LabelFNTandTTFEmpty.updateStrings(dt) + local label1 = LabelFNTandTTFEmpty.layer:getChildByTag(kTagBitmapAtlas1) + local label2 = LabelFNTandTTFEmpty.layer:getChildByTag(kTagBitmapAtlas2) + + if( LabelFNTandTTFEmpty.setEmpty == false) then + label1:setString("not empty") + label2:setString("not empty") + LabelFNTandTTFEmpty.setEmpty = true + else + label1:setString("") + label2:setString("") + LabelFNTandTTFEmpty.setEmpty = false + end +end + +-------------------------------------------------------------------- +-- +-- LabelFNTRetina +-- +-------------------------------------------------------------------- +local LabelFNTRetina = { + +} + +function LabelFNTRetina.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + -- cc.LabelBMFont + local label1 = cc.Label:createWithBMFont("fonts/konqa32.fnt", "TESTING RETINA DISPLAY") + label1:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/2)) + + Helper.titleLabel:setString("New Label + .FNT file") + Helper.subtitleLabel:setString("loading arista16 or arista16-hd") + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelFNTGlyphDesigner +-- +-------------------------------------------------------------------- +local LabelFNTGlyphDesigner = {} +function LabelFNTGlyphDesigner.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local label1 = cc.Label:createWithBMFont("fonts/futura-48.fnt", "TESTING RETINA DISPLAY") + label1:setAnchorPoint(cc.p(0.5, 0.5)) + + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/2)) + + Helper.titleLabel:setString("New Label + .FNT file") + Helper.subtitleLabel:setString("Testing Glyph Designer: you should see a font with shawdows and outline") + return layer +end + +--/ LabelTTFUnicodeChinese +local LabelTTFUnicodeChinese = {} +function LabelTTFUnicodeChinese.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + .TTF file Chinese") + Helper.subtitleLabel:setString("Testing new Label + TTF with Chinese character") + + local s = cc.Director:getInstance():getWinSize() + local ttfConfig = {} + ttfConfig.fontFilePath="fonts/wt021.ttf" + ttfConfig.fontSize=55 + ttfConfig.glyphs=cc.GLYPHCOLLECTION_CUSTOM + ttfConfig.customGlyphs="美好的一天啊" + + local label1 = cc.Label:createWithTTF(ttfConfig,"美好的一天啊", cc.TEXT_ALIGNMENT_CENTER, s.width) + label1:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/2)) + + return layer +end + +------------ +-- LabelFNTUnicodeChinese +------------ + +local LabelFNTUnicodeChinese = {} +function LabelFNTUnicodeChinese.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + local size = cc.Director:getInstance():getWinSize() + local lable = cc.Label:createWithBMFont("fonts/bitmapFontChinese.fnt", "中国") + lable:setAnchorPoint(cc.p(0.5,0.5)) + lable:setPosition(cc.p(size.width / 2, size.height /2)) + layer:addChild(lable) + + Helper.titleLabel:setString("New Label + .FNT file Chinese") + Helper.subtitleLabel:setString("Testing new Label + FNT with Chinese character") + + return layer +end + +-- LabelFNTMultiLineAlignment + +local LongSentencesExample = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." +local LineBreaksExample = "Lorem ipsum dolor\nsit amet\nconsectetur adipisicing elit\nblah\nblah" +local MixedExample = "ABC\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt\nDEF" + +local ArrowsMax = 0.95 +local ArrowsMin = 0.7 + +local LeftAlign = 0 +local CenterAlign = 1 +local RightAlign = 2 + +local LongSentences = 0 +local LineBreaks = 1 +local Mixed = 2 + +local alignmentItemPadding = 50 +local menuItemPaddingCenter = 50 + +local LabelFNTMultiLineAlignment = { + _pLabelShouldRetain = nil, + _pArrowsBarShouldRetain = nil, + _pArrowsShouldRetain = nil, + _pLastSentenceItem = nil, + _drag = false, +} + +function LabelFNTMultiLineAlignment.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + -- ask director the the window size + local size = cc.Director:getInstance():getWinSize() + + -- create and initialize a Label + LabelFNTMultiLineAlignment._pLabelShouldRetain = cc.Label:createWithBMFont("fonts/markerFelt.fnt", LongSentencesExample, cc.TEXT_ALIGNMENT_CENTER, size.width/1.5) + LabelFNTMultiLineAlignment._pLabelShouldRetain:setAnchorPoint(cc.p(0.5, 0.5)) + LabelFNTMultiLineAlignment._pLabelShouldRetain:retain() + + LabelFNTMultiLineAlignment._pArrowsBarShouldRetain = cc.Sprite:create("Images/arrowsBar.png") + LabelFNTMultiLineAlignment._pArrowsBarShouldRetain:retain() + LabelFNTMultiLineAlignment._pArrowsShouldRetain = cc.Sprite:create("Images/arrows.png") + LabelFNTMultiLineAlignment._pArrowsShouldRetain:retain() + + cc.MenuItemFont:setFontSize(20) + local longSentences = cc.MenuItemFont:create("Long Flowing Sentences") + longSentences:registerScriptTapHandler(LabelFNTMultiLineAlignment.stringChanged) + local lineBreaks = cc.MenuItemFont:create("Short Sentences With Intentional Line Breaks") + lineBreaks:registerScriptTapHandler(LabelFNTMultiLineAlignment.stringChanged) + local mixed = cc.MenuItemFont:create("Long Sentences Mixed With Intentional Line Breaks") + mixed:registerScriptTapHandler(LabelFNTMultiLineAlignment.stringChanged) + local stringMenu = cc.Menu:create() + stringMenu:addChild(longSentences) + stringMenu:addChild(lineBreaks) + stringMenu:addChild(mixed) + stringMenu:alignItemsVertically() + + longSentences:setColor(cc.c3b(255, 0, 0)) + LabelFNTMultiLineAlignment._pLastSentenceItem = longSentences + + longSentences:setTag(LongSentences) + lineBreaks:setTag(LineBreaks) + mixed:setTag(Mixed) + + cc.MenuItemFont:setFontSize(30) + + local left = cc.MenuItemFont:create("Left") + left:registerScriptTapHandler(LabelFNTMultiLineAlignment.alignmentChanged) + local center = cc.MenuItemFont:create("Center") + center:registerScriptTapHandler(LabelFNTMultiLineAlignment.alignmentChanged) + local right = cc.MenuItemFont:create("Right") + right:registerScriptTapHandler(LabelFNTMultiLineAlignment.alignmentChanged) + + local alignmentMenu = cc.Menu:create() + alignmentMenu:addChild(left) + alignmentMenu:addChild(center) + alignmentMenu:addChild(right) + + alignmentMenu:alignItemsHorizontallyWithPadding(alignmentItemPadding) + + center:setColor(cc.c3b(255, 0, 0)) + LabelFNTMultiLineAlignment._pLastAlignmentItem = center + left:setTag(LeftAlign) + center:setTag(CenterAlign) + right:setTag(RightAlign) + + -- position the label on the center of the screen + LabelFNTMultiLineAlignment._pLabelShouldRetain:setPosition(cc.p(size.width/2, size.height/2)) + + LabelFNTMultiLineAlignment._pArrowsBarShouldRetain:setVisible(false) + + local arrowsWidth = (ArrowsMax - ArrowsMin) * size.width + LabelFNTMultiLineAlignment._pArrowsBarShouldRetain:setScaleX(arrowsWidth / LabelFNTMultiLineAlignment._pArrowsBarShouldRetain:getContentSize().width) + LabelFNTMultiLineAlignment._pArrowsBarShouldRetain:setPosition( ((ArrowsMax + ArrowsMin) / 2) * size.width, LabelFNTMultiLineAlignment._pLabelShouldRetain:getPositionY() ) + + LabelFNTMultiLineAlignment.snapArrowsToEdge() + + stringMenu:setPosition(cc.p(size.width/2, size.height - menuItemPaddingCenter)) + alignmentMenu:setPosition(cc.p(size.width/2, menuItemPaddingCenter+15)) + + layer:addChild(LabelFNTMultiLineAlignment._pLabelShouldRetain) + layer:addChild(LabelFNTMultiLineAlignment._pArrowsBarShouldRetain) + layer:addChild(LabelFNTMultiLineAlignment._pArrowsShouldRetain) + layer:addChild(stringMenu) + layer:addChild(alignmentMenu) + layer:registerScriptHandler(LabelFNTMultiLineAlignment.onNodeEvent) + + local function onTouchesBegan(touches, event) + local location = touches[1]:getLocationInView() + if cc.rectContainsPoint(LabelFNTMultiLineAlignment._pArrowsShouldRetain:getBoundingBox(), cc.p(location.x, location.y)) then + LabelFNTMultiLineAlignment._drag = true + LabelFNTMultiLineAlignment._pArrowsBarShouldRetain:setVisible(true) + end + end + + local function onTouchesMoved(touches, event) + if LabelFNTMultiLineAlignment._drag == false then + return + end + + local winSize = cc.Director:getInstance():getWinSize() + local location = touches[1]:getLocationInView() + + LabelFNTMultiLineAlignment._pArrowsShouldRetain:setPosition( + math.max(math.min(location.x, ArrowsMax*winSize.width), ArrowsMin*winSize.width), + LabelFNTMultiLineAlignment._pArrowsShouldRetain:getPositionY()) + + local labelWidth = math.abs(LabelFNTMultiLineAlignment._pArrowsShouldRetain:getPositionX() - LabelFNTMultiLineAlignment._pLabelShouldRetain:getPositionX()) * 2 + + LabelFNTMultiLineAlignment._pLabelShouldRetain:setMaxLineWidth(labelWidth) + end + + local function onTouchesEnded(touch, event) + LabelFNTMultiLineAlignment._drag = false + LabelFNTMultiLineAlignment.snapArrowsToEdge() + LabelFNTMultiLineAlignment._pArrowsBarShouldRetain:setVisible(false) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesBegan,cc.Handler.EVENT_TOUCHES_BEGAN ) + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + listener:registerScriptHandler(onTouchesEnded,cc.Handler.EVENT_TOUCHES_ENDED ) + + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + return layer +end + +function LabelFNTMultiLineAlignment.onNodeEvent(tag) + if tag == "onexit" then + LabelFNTMultiLineAlignment._pLabelShouldRetain:release() + LabelFNTMultiLineAlignment._pArrowsBarShouldRetain:release() + LabelFNTMultiLineAlignment._pArrowsShouldRetain:release() + end +end + + + +function LabelFNTMultiLineAlignment.stringChanged(tag, sender) + local item = tolua.cast(sender, "cc.MenuItemFont") + item:setColor(cc.c3b(255, 0, 0)) + LabelFNTMultiLineAlignment._pLastAlignmentItem:setColor(cc.c3b(255, 255, 255)) + LabelFNTMultiLineAlignment._pLastAlignmentItem = item + + if item:getTag() == LongSentences then + LabelFNTMultiLineAlignment._pLabelShouldRetain:setString(LongSentencesExample) + elseif item:getTag() == LineBreaks then + LabelFNTMultiLineAlignment._pLabelShouldRetain:setString(LineBreaksExample) + elseif item:getTag() == Mixed then + LabelFNTMultiLineAlignment._pLabelShouldRetain:setString(MixedExample) + end + + LabelFNTMultiLineAlignment.snapArrowsToEdge() +end + +function LabelFNTMultiLineAlignment.alignmentChanged(tag, sender) + -- cclog("LabelFNTMultiLineAlignment.alignmentChanged, tag:"..tag) + local item = tolua.cast(sender, "cc.MenuItemFont") + item:setColor(cc.c3b(255, 0, 0)) + LabelFNTMultiLineAlignment._pLastAlignmentItem:setColor(cc.c3b(255, 255, 255)) + LabelFNTMultiLineAlignment._pLastAlignmentItem = item + + if tag == LeftAlign then + cclog("LeftAlign") + LabelFNTMultiLineAlignment._pLabelShouldRetain:setAlignment(cc.TEXT_ALIGNMENT_LEFT) + elseif tag == CenterAlign then + LabelFNTMultiLineAlignment._pLabelShouldRetain:setAlignment(cc.TEXT_ALIGNMENT_CENTER) + elseif tag == RightAlign then + LabelFNTMultiLineAlignment._pLabelShouldRetain:setAlignment(cc.TEXT_ALIGNMENT_RIGHT) + end + + LabelFNTMultiLineAlignment.snapArrowsToEdge() +end + +function LabelFNTMultiLineAlignment.snapArrowsToEdge() + LabelFNTMultiLineAlignment._pArrowsShouldRetain:setPosition( + LabelFNTMultiLineAlignment._pLabelShouldRetain:getPositionX() + LabelFNTMultiLineAlignment._pLabelShouldRetain:getContentSize().width/2, LabelFNTMultiLineAlignment._pLabelShouldRetain:getPositionY() + ) +end + +------------------------------------ +----- LabelFNTUNICODELanguages +------------------------------------ +local LabelFNTUNICODELanguages = {} +function LabelFNTUNICODELanguages.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + .FNT + UNICODE") + Helper.subtitleLabel:setString("You should see 3 differnt labels:\nIn Spanish, Chinese, and Japanese") + + local s = cc.Director:getInstance():getWinSize() + + local label1 = cc.Label:createWithBMFont("fonts/arial-unicode-26.fnt", "Buen día", cc.TEXT_ALIGNMENT_CENTER, 200) + label1:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/4*3)) + + local label2 = cc.Label:createWithBMFont("fonts/arial-unicode-26.fnt", "美好的一天") + label2:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(label2) + label2:setPosition(cc.p(s.width/2, s.height/4*2)) + + local label3 = cc.Label:createWithBMFont("fonts/arial-unicode-26.fnt", "良い一日を") + label3:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(label3) + label3:setPosition(cc.p(s.width/2, s.height/4*1)) + + return layer +end + +---------------------------------------------------------- +----- LabelTTFAlignmentNew +---------------------------------------------------------- +local LabelTTFAlignmentNew = {} +function LabelTTFAlignmentNew.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + TTF") + Helper.subtitleLabel:setString("Tests alignment values") + + local s = cc.Director:getInstance():getWinSize() + local ttfConfig = {} + ttfConfig.fontFilePath="fonts/tahoma.ttf" + ttfConfig.fontSize=32 + local ttf0 = cc.Label:createWithTTF(ttfConfig, "Alignment 0\nnew line", cc.TEXT_ALIGNMENT_LEFT) + ttf0:setPosition(cc.p(s.width/2,(s.height/6)*2 - 30)) + ttf0:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(ttf0) + + local ttf1 = cc.Label:createWithTTF(ttfConfig, "Alignment 1\nnew line", cc.TEXT_ALIGNMENT_CENTER) + ttf1:setPosition(cc.p(s.width/2,(s.height/6)*3 - 30)) + ttf1:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(ttf1) + + local ttf2 = cc.Label:createWithTTF(ttfConfig, "Alignment 2\nnew line", cc.TEXT_ALIGNMENT_RIGHT) + ttf2:setPosition(cc.p(s.width/2,(s.height/6)*4 - 30)) + ttf2:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(ttf2) + + return layer +end + + +------------------------------------- +----- LabelTTFUnicodeNew +------------------------------------- +local LabelTTFUnicodeNew = {} +function LabelTTFUnicodeNew.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + TTF unicode") + Helper.subtitleLabel:setString("Uses the new Label with TTF. Testing unicode") + + local s = cc.Director:getInstance():getWinSize() + local vStep = s.height/9 + local vSize = s.height + + local ttfConfig = {} + ttfConfig.fontFilePath="fonts/arial.ttf" + ttfConfig.fontSize=45 + ttfConfig.glyphs=cc.GLYPHCOLLECTION_ASCII + + local label1 = cc.Label:createWithTTF(ttfConfig,"Buen día, ¿cómo te llamas?", cc.TEXT_ALIGNMENT_CENTER, s.width) + label1:setAnchorPoint(cc.p(0.5,0.5)) + label1:setPosition(cc.p(s.width/2, vSize - vStep * 4.5)) + layer:addChild(label1) + + local label2 = cc.Label:createWithTTF(ttfConfig, "In welcher Straße haben Sie gelebt?", cc.TEXT_ALIGNMENT_CENTER, s.width) + label2:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(label2) + label2:setPosition(cc.p(s.width/2, vSize - vStep * 5.5)) + + ttfConfig.fontFilePath = "fonts/wt021.ttf" + ttfConfig.glyphs = cc.GLYPHCOLLECTION_CUSTOM + ttfConfig.customGlyphs = "美好的一天" + local label3 = cc.Label:createWithTTF(ttfConfig, "美好的一天", cc.TEXT_ALIGNMENT_CENTER, s.width) + label3:setAnchorPoint(cc.p(0.5,0.5)) + layer:addChild(label3) + label3:setPosition(cc.p(s.width/2, vSize - vStep * 6.5)) + + return layer +end + + +---------------------------------- +-- +--LabelFNTBounds +-- +---------------------------------- +local LabelFNTBounds = {} +function LabelFNTBounds.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + .FNT + Bounds") + Helper.subtitleLabel:setString("You should see string enclosed by a box") + + local s = cc.Director:getInstance():getWinSize() + + local colorlayer = cc.LayerColor:create(cc.c4b(128,128,128,255)) + layer:addChild(colorlayer, -10) + + -- cc.LabelBMFont + local label1 = cc.Label:createWithBMFont("fonts/boundsTestFont.fnt", "Testing Glyph Designer", cc.TEXT_ALIGNMENT_CENTER, s.width) + label1:setAnchorPoint(cc.p(0.5, 0.5)) + + layer:addChild(label1) + label1:setPosition(cc.p(s.width/2, s.height/2)) + return layer +end + +function LabelFNTBounds.draw() + -- cc.size labelSize = label1:getContentSize() + -- cc.size origin = cc.Director:getInstance():getWinSize() + + -- origin.width = origin.width / 2 - (labelSize.width / 2) + -- origin.height = origin.height / 2 - (labelSize.height / 2) + + -- cc.p vertices[4]= + + -- cc.p(origin.width, origin.height), + -- cc.p(labelSize.width + origin.width, origin.height), + -- cc.p(labelSize.width + origin.width, labelSize.height + origin.height), + -- cc.p(origin.width, labelSize.height + origin.height) + -- end + -- ccDrawPoly(vertices, 4, true) +end + + +-------------------------------------------------------- +----- LabelTTFLongLineWrapping +-------------------------------------------------------- +local LabelTTFLongLineWrapping = {} +function LabelTTFLongLineWrapping.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + .TTF") + Helper.subtitleLabel:setString("Uses the new Label with TTF. Testing auto-wrapping") + + local s = cc.Director:getInstance():getWinSize() + local ttfConfig = {} + ttfConfig.fontFilePath = "fonts/arial.ttf" + ttfConfig.fontSize = 28 + local label1 = cc.Label:createWithTTF(ttfConfig, LongSentencesExample, cc.TEXT_ALIGNMENT_LEFT, s.width) + label1:setAnchorPoint(cc.p(0.5,1.0)) + label1:setPosition(cc.p(s.width/2, s.height/2)) + layer:addChild(label1) + return layer +end + +-------------------------------------------------------------------- +-- +-- LabelTTFDynamicAlignment +-- +-------------------------------------------------------------------- + +local LabelTTFDynamicAlignment = { + _layer = nil, + _label = nil, + _eHorizAlign = cc.TEXT_ALIGNMENT_LEFT, +} + +function LabelTTFDynamicAlignment.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + LabelTTFDynamicAlignment._layer = layer + LabelTTFDynamicAlignment._label = nil + LabelTTFDynamicAlignment._eHorizAlign = cc.TEXT_ALIGNMENT_LEFT + + local s = cc.Director:getInstance():getWinSize() + local ttfConfig = {} + ttfConfig.fontFilePath = "fonts/arial.ttf" + ttfConfig.fontSize = 45 + LabelTTFDynamicAlignment._label = cc.Label:createWithTTF(ttfConfig, LongSentencesExample, cc.TEXT_ALIGNMENT_CENTER, s.width) + LabelTTFDynamicAlignment._label:setPosition( cc.p(s.width/2, s.height/2) ) + LabelTTFDynamicAlignment._label:setAnchorPoint(cc.p(0.5, 0.5)) + layer:addChild(LabelTTFDynamicAlignment._label) + + cc.MenuItemFont:setFontSize(30) + local item1 = cc.MenuItemFont:create("Left") + item1:registerScriptTapHandler(LabelTTFDynamicAlignment.setAlignmentLeft) + local item2 = cc.MenuItemFont:create("Center") + item2:registerScriptTapHandler(LabelTTFDynamicAlignment.setAlignmentCenter) + local item3 = cc.MenuItemFont:create("Right") + item3:registerScriptTapHandler(LabelTTFDynamicAlignment.setAlignmentRight) + + local menu = cc.Menu:create() + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + menu:alignItemsVerticallyWithPadding(4) + menu:setPosition(cc.p(50, s.height / 4)) + layer:addChild(menu) + + Helper.titleLabel:setString("New Label + .TTF") + Helper.subtitleLabel:setString("Uses the new Label with TTF. Testing alignment") + + LabelTTFDynamicAlignment.updateAlignment() + + return layer +end + +function LabelTTFDynamicAlignment.updateAlignment() + local s = cc.Director:getInstance():getWinSize() + LabelTTFDynamicAlignment._label:setAlignment(LabelTTFDynamicAlignment._eHorizAlign) +end + +function LabelTTFDynamicAlignment.setAlignmentLeft(pSender) + LabelTTFDynamicAlignment._eHorizAlign = cc.TEXT_ALIGNMENT_LEFT + LabelTTFDynamicAlignment.updateAlignment() +end + +function LabelTTFDynamicAlignment.setAlignmentCenter(pSender) + LabelTTFDynamicAlignment._eHorizAlign = cc.TEXT_ALIGNMENT_CENTER + LabelTTFDynamicAlignment.updateAlignment() +end + +function LabelTTFDynamicAlignment.setAlignmentRight(pSender) + LabelTTFDynamicAlignment._eHorizAlign = cc.TEXT_ALIGNMENT_RIGHT + LabelTTFDynamicAlignment.updateAlignment() +end + + +-------------------------------------------------------- +----- LabelTTFCJKWrappingTest +-------------------------------------------------------- +local LabelTTFCJKWrappingTest = {} +function LabelTTFCJKWrappingTest.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + .TTF") + Helper.subtitleLabel:setString( + "New Label with CJK + ASCII characters\n" + .. "Characters should stay in the correct position") + + local size = cc.Director:getInstance():getVisibleSize() + local ttfConfig = {} + ttfConfig.fontFilePath = "fonts/wt021.ttf" + ttfConfig.fontSize = 50 + ttfConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC + ttfConfig.customGlyphs = nil + ttfConfig.distanceFieldEnabled = true + + local drawNode = cc.DrawNode:create() + drawNode:setAnchorPoint(cc.p(0, 0)) + layer:addChild(drawNode) + drawNode:drawSegment( + cc.p(size.width * 0.1, size.height * 0.8), + cc.p(size.width * 0.1, 0), 1, cc.c4f(1, 0, 0, 1)) + drawNode:drawSegment( + cc.p(size.width * 0.85, size.height * 0.8), + cc.p(size.width * 0.85, 0), 1, cc.c4f(1, 0, 0, 1)) + + local label1 = cc.Label:createWithTTF( + ttfConfig, "你好,Cocos2d-x v3的New Label。", + cc.TEXT_ALIGNMENT_LEFT, size.width * 0.75) + label1:setColor(cc.c3b(128, 255, 255)) + label1:setPosition(cc.p(size.width * 0.1, size.height * 0.6)) + label1:setAnchorPoint(cc.p(0, 0.5)) + layer:addChild(label1) + + local label2 = cc.Label:createWithTTF( + ttfConfig, "早上好,Cocos2d-x v3的New Label。", + cc.TEXT_ALIGNMENT_LEFT, size.width * 0.75) + label2:setColor(cc.c3b(255, 128, 255)) + label2:setPosition(cc.p(size.width * 0.1, size.height * 0.4)) + label2:setAnchorPoint(cc.p(0, 0.5)) + layer:addChild(label2) + + local label3 = cc.Label:createWithTTF( + ttfConfig, "美好的一天啊美好的一天啊美好的一天啊", + cc.TEXT_ALIGNMENT_LEFT, size.width * 0.75) + label3:setColor(cc.c3b(255, 255, 128)) + label3:setPosition(cc.p(size.width * 0.1, size.height * 0.2)) + label3:setAnchorPoint(cc.p(0, 0.5)) + layer:addChild(label3) + + return layer +end + + +-------------------------------------------------------- +----- LabelTTFFontsTestNew +-------------------------------------------------------- +local LabelTTFFontsTestNew = {} +function LabelTTFFontsTestNew.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + TTF") + Helper.subtitleLabel:setString("") + + local s = cc.Director:getInstance():getWinSize() + + local ttfPaths = + { + "fonts/A Damn Mess.ttf", + "fonts/Abberancy.ttf", + "fonts/Abduction.ttf", + "fonts/American Typewriter.ttf", + "fonts/Paint Boy.ttf", + "fonts/Schwarzwald Regular.ttf", + "fonts/Scissor Cuts.ttf", + } + local ttfConfig = {} + ttfConfig.fontFilePath = ttfPaths[0] + ttfConfig.fontSize = 40 + for i=1, table.getn(ttfPaths) do + ttfConfig.fontFilePath = ttfPaths[i] + local label = cc.Label:createWithTTF( ttfConfig, ttfPaths[i], cc.TEXT_ALIGNMENT_CENTER, 0) + if nil ~= label then + label:setPosition( cc.p(s.width/2, ((s.height * 0.6)/table.getn(ttfPaths) * (i -1)) + (s.height/5))) + layer:addChild(label) + + label:setAnchorPoint(cc.p(0.5, 0.5)) + else + print(string.format("ERROR: Cannot load: %s",ttfPaths[i])) + end + end + + return layer +end + + +-------------------------------------------------------- +----- LabelBMFontTestNew +-------------------------------------------------------- +local LabelBMFontTestNew = {} +function LabelBMFontTestNew.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + FNT") + Helper.subtitleLabel:setString("Uses the new Label with .FNT file") + + local s = cc.Director:getInstance():getWinSize() + + local label1 = cc.Label:createWithBMFont("fonts/bitmapFontTest2.fnt", "Hello World, this is testing the new Label using fnt file", cc.TEXT_ALIGNMENT_CENTER, s.width) + label1:setAnchorPoint(cc.p(0.5,0.5)) + label1:setPosition(cc.p(s.width/2, s.height/2)) + layer:addChild(label1) + return layer +end + + +-------------------------------------------------------- +----- LabelTTFDistanceField +-------------------------------------------------------- +local LabelTTFDistanceField = {} +function LabelTTFDistanceField.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + .TTF") + Helper.subtitleLabel:setString("Testing rendering base on DistanceField") + + local s = cc.Director:getInstance():getWinSize() + local ttfConfig = {} + ttfConfig.fontFilePath = "fonts/arial.ttf" + ttfConfig.fontSize = 80 + ttfConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC + ttfConfig.customGlyphs = nil + ttfConfig.distanceFieldEnabled = true + + local label1 = cc.Label:createWithTTF(ttfConfig,"Distance Field",cc.TEXT_ALIGNMENT_CENTER,s.width) + label1:setAnchorPoint(cc.p(0.5,0.5)) + label1:setPosition(cc.p(s.width/2, s.height/2)) + label1:setColor( cc.c3b(0, 255, 0) ) + local action = cc.Sequence:create(cc.DelayTime:create(1.0), + cc.ScaleTo:create(6.0,5.0,5.0), + cc.ScaleTo:create(6.0,1.0,1.0)) + label1:runAction(cc.RepeatForever:create(action)) + layer:addChild(label1) + + local label2 = cc.Label:createWithTTF(ttfConfig,"Distance Field",cc.TEXT_ALIGNMENT_CENTER,s.width) + label2:setPosition( cc.p(s.width/2, s.height/5) ) + label2:setColor( cc.c3b(255, 0, 0)) + label2:setAnchorPoint(cc.p(0.5, 0.5)) + layer:addChild(label2) + + return layer +end + +-------------------------------------------------------- +----- LabelOutlineAndGlowTest +-------------------------------------------------------- +local LabelOutlineAndGlowTest = {} +function LabelOutlineAndGlowTest.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + .TTF") + Helper.subtitleLabel:setString("Testing outline and glow of label") + + local s = cc.Director:getInstance():getWinSize() + + local col = cc.LayerColor:create( cc.c4b(200, 191, 231, 255)) + layer:addChild(col) + + local ttfConfig = {} + ttfConfig.fontFilePath = "fonts/arial.ttf" + ttfConfig.fontSize = 80 + ttfConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC + ttfConfig.customGlyphs = nil + ttfConfig.distanceFieldEnabled = true + ttfConfig.outlineSize = 0 + + local label1 = cc.Label:createWithTTF(ttfConfig,"Glow",cc.TEXT_ALIGNMENT_CENTER,s.width) + label1:setAnchorPoint(cc.p(0.5,0.5)) + label1:setPosition(cc.p(s.width/2, s.height * 0.7)) + label1:setColor( cc.c3b(0, 255, 0) ) + label1:enableGlow(cc.c3b(255, 255, 0)) + layer:addChild(label1) + + ttfConfig.outlineSize = 1 + local label2 = cc.Label:createWithTTF(ttfConfig,"Outline",cc.TEXT_ALIGNMENT_CENTER,s.width) + label2:setPosition( cc.p(s.width/2, s.height * 0.6)) + label2:setColor( cc.c3b(255, 0, 0)) + label2:setAnchorPoint(cc.p(0.5, 0.5)) + label2:enableOutline(cc.c4b(0,0,255,255)) + layer:addChild(label2) + + ttfConfig.outlineSize = 2 + local label3 = cc.Label:createWithTTF(ttfConfig,"Outline",cc.TEXT_ALIGNMENT_CENTER,s.width) + label3:setPosition( cc.p(s.width/2, s.height * 0.48)) + label3:setColor( cc.c3b(255, 0, 0)) + label3:setAnchorPoint(cc.p(0.5, 0.5)) + label3:enableOutline(cc.c4b(0,0,255,255)) + layer:addChild(label3) + + return layer +end + + +-------------------------------------------------------------------- +-- +-- LabelCharMapTest +-- +-------------------------------------------------------------------- +local LabelCharMapTest = {} + +function LabelCharMapTest.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + char map file") + Helper.subtitleLabel:setString("Updating label should be fast.") + + time = 0 + + local label1 = cc.Label:createWithCharMap("fonts/tuffy_bold_italic-charmap.plist") + layer:addChild(label1, 0, kTagSprite1) + label1:setPosition( cc.p(10,100) ) + label1:setOpacity( 200 ) + + local label2 = cc.Label:createWithCharMap("fonts/tuffy_bold_italic-charmap.plist") + layer:addChild(label2, 0, kTagSprite2) + label2:setPosition( cc.p(10,160) ) + label2:setOpacity( 32 ) + + local label3 = cc.Label:createWithCharMap("fonts/tuffy_bold_italic-charmap.png", 48, 64, 32)--32 means Space key + label3:setString("123 Test") + layer:addChild(label3, 0, kTagSprite3) + label3:setPosition(cc.p(10,220)) + + local function step(dt) + time = time + dt + local info = string.format("%2.2f Test", time) + + local label1 = layer:getChildByTag(kTagSprite1) + label1:setString(info) + + local label2 = layer:getChildByTag(kTagSprite2) + info = string.format("%d",time) + label2:setString(info) + end + + layer:scheduleUpdateWithPriorityLua(step, 0) + + function onNodeEvent(tag) + if tag == "exit" then + layer:unscheduleUpdate() + end + end + + layer:registerScriptHandler(onNodeEvent) + + return layer +end + + +-------------------------------------------------------- +----- LabelCrashTest +-------------------------------------------------------- +local LabelCrashTest = {} +function LabelCrashTest.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Helper.titleLabel:setString("New Label + .TTF") + Helper.subtitleLabel:setString("Testing rendering base on DistanceField") + + local ttfConfig = {} + ttfConfig.fontFilePath = "fonts/arial.ttf" + ttfConfig.fontSize = 80 + ttfConfig.glyphs = cc.GLYPHCOLLECTION_DYNAMIC + ttfConfig.customGlyphs = nil + ttfConfig.distanceFieldEnabled = true + local s = cc.Director:getInstance():getWinSize() + local label1 = cc.Label:createWithTTF(ttfConfig,"Test崩溃123", cc.TEXT_ALIGNMENT_CENTER, s.width) + label1:setPosition( cc.p(s.width/2, s.height/2) ) + label1:setAnchorPoint(cc.p(0.5, 0.5)) + layer:addChild(label1) + + return layer +end + +------------ +function LabelTestNew() + cclog("LabelTestNew") + m_time = 0 + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + LabelFNTColorAndOpacity.create, + LabelFNTSpriteActions.create, + LabelFNTPadding.create, + LabelFNTOffset.create, + LabelFNTColor.create, + LabelTTFColor.create, + LabelFNTHundredLabels.create, + LabelFNTMultiLine.create, + LabelFNTandTTFEmpty.create, + LabelFNTRetina.create, + LabelFNTGlyphDesigner.create, + LabelTTFUnicodeChinese.create, + LabelFNTUnicodeChinese.create, + LabelFNTMultiLineAlignment.create, + LabelFNTUNICODELanguages.create, + LabelTTFUnicodeNew.create, + LabelTTFAlignmentNew.create, + LabelFNTBounds.create, + LabelTTFLongLineWrapping.create, + LabelTTFDynamicAlignment.create, + LabelTTFCJKWrappingTest.create, + LabelTTFFontsTestNew.create, + LabelBMFontTestNew.create, + LabelTTFDistanceField.create, + LabelOutlineAndGlowTest.create, + LabelCharMapTest.create, + LabelCrashTest.create, + } + scene:addChild(LabelFNTColorAndOpacity.create()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/LayerTest/LayerTest.lua b/samples/lua-tests/src/LayerTest/LayerTest.lua new file mode 100644 index 0000000000..7fce8fdc99 --- /dev/null +++ b/samples/lua-tests/src/LayerTest/LayerTest.lua @@ -0,0 +1,651 @@ +local scheduler = cc.Director:getInstance():getScheduler() +local kTagLayer = 1 + +local function createLayerDemoLayer(title, subtitle) + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + local titleStr = title == nil and "No title" or title + local subTitleStr = subtitle == nil and "" or subtitle + Helper.titleLabel:setString(titleStr) + Helper.subtitleLabel:setString(subTitleStr) + + -- local prev = {x = 0, y = 0} + -- local function onTouchEvent(eventType, x, y) + -- if eventType == "began" then + -- prev.x = x + -- prev.y = y + -- return true + -- elseif eventType == "moved" then + -- local node = layer:getChildByTag(kTagTileMap) + -- local newX = node:getPositionX() + -- local newY = node:getPositionY() + -- local diffX = x - prev.x + -- local diffY = y - prev.y + + -- node:setPosition( cc.p.__add(cc.p(newX, newY), cc.p(diffX, diffY)) ) + -- prev.x = x + -- prev.y = y + -- end + -- end + -- layer:setTouchEnabled(true) + -- layer:registerScriptTouchHandler(onTouchEvent) + return layer +end + +--#pragma mark - Cascading support extensions + +local function setEnableRecursiveCascading(node, enable) + if node == nil then + -- cclog("node == nil, return directly") + return + end + + if node.setCascadeColorEnabled ~= nil and node.setCascadeOpacityEnabled ~= nil then + node:setCascadeColorEnabled(enable) + node:setCascadeOpacityEnabled(enable) + end + + local obj = nil + local children = node:getChildren() + if children == nil then + -- cclog("children is nil") + print("children is nil") + return + end + + local i = 0 + local len = table.getn(children) + for i = 0, len-1, 1 do + setEnableRecursiveCascading(children[i + 1], enable) + end +end + +-- LayerTestCascadingOpacityA +local function LayerTestCascadingOpacityA() + local ret = createLayerDemoLayer("Layer: cascading opacity") + local s = cc.Director:getInstance():getWinSize() + local layer1 = cc.Layer:create() + + local sister1 = cc.Sprite:create("Images/grossinis_sister1.png") + local sister2 = cc.Sprite:create("Images/grossinis_sister2.png") + local label = cc.LabelBMFont:create("Test", "fonts/bitmapFontTest.fnt") + + layer1:addChild(sister1) + layer1:addChild(sister2) + layer1:addChild(label) + ret:addChild( layer1, 0, kTagLayer) + + sister1:setPosition( cc.p( s.width*1/3, s.height/2)) + sister2:setPosition( cc.p( s.width*2/3, s.height/2)) + label:setPosition( cc.p( s.width/2, s.height/2)) + + layer1:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.FadeTo:create(4, 0), + cc.FadeTo:create(4, 255), + cc.DelayTime:create(1) + ))) + + + sister1:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.FadeTo:create(2, 0), + cc.FadeTo:create(2, 255), + cc.FadeTo:create(2, 0), + cc.FadeTo:create(2, 255), + cc.DelayTime:create(1) + ))) + + -- Enable cascading in scene + setEnableRecursiveCascading(ret, true) + return ret +end + +-- LayerTestCascadingOpacityB +local function LayerTestCascadingOpacityB() + local ret = createLayerDemoLayer("CCLayerColor: cascading opacity") + + local s = cc.Director:getInstance():getWinSize() + local layer1 = cc.LayerColor:create(cc.c4b(192, 0, 0, 255), s.width, s.height/2) + layer1:setCascadeColorEnabled(false) + + layer1:setPosition( cc.p(0, s.height/2)) + + local sister1 = cc.Sprite:create("Images/grossinis_sister1.png") + local sister2 = cc.Sprite:create("Images/grossinis_sister2.png") + local label = cc.LabelBMFont:create("Test", "fonts/bitmapFontTest.fnt") + + layer1:addChild(sister1) + layer1:addChild(sister2) + layer1:addChild(label) + ret:addChild( layer1, 0, kTagLayer) + + sister1:setPosition( cc.p( s.width*1/3, 0)) + sister2:setPosition( cc.p( s.width*2/3, 0)) + label:setPosition( cc.p( s.width/2, 0)) + + layer1:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.FadeTo:create(4, 0),cc.FadeTo:create(4, 255),cc.DelayTime:create(1)))) + + sister1:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.FadeTo:create(2, 0),cc.FadeTo:create(2, 255),cc.FadeTo:create(2, 0),cc.FadeTo:create(2, 255),cc.DelayTime:create(1)))) + + -- Enable cascading in scene + setEnableRecursiveCascading(ret, true) + return ret +end + +-- LayerTestCascadingOpacityC +local function LayerTestCascadingOpacityC() + local ret = createLayerDemoLayer("LayerColor: non-cascading opacity") + + local s = cc.Director:getInstance():getWinSize() + local layer1 = cc.LayerColor:create(cc.c4b(192, 0, 0, 255), s.width, s.height/2) + layer1:setCascadeColorEnabled(false) + layer1:setCascadeOpacityEnabled(false) + + layer1:setPosition( cc.p(0, s.height/2)) + + local sister1 = cc.Sprite:create("Images/grossinis_sister1.png") + local sister2 = cc.Sprite:create("Images/grossinis_sister2.png") + local label = cc.LabelBMFont:create("Test", "fonts/bitmapFontTest.fnt") + + layer1:addChild(sister1) + layer1:addChild(sister2) + layer1:addChild(label) + ret:addChild( layer1, 0, kTagLayer) + + sister1:setPosition( cc.p( s.width*1/3, 0)) + sister2:setPosition( cc.p( s.width*2/3, 0)) + label:setPosition( cc.p( s.width/2, 0)) + + layer1:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.FadeTo:create(4, 0), + cc.FadeTo:create(4, 255),cc.DelayTime:create(1)))) + + sister1:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.FadeTo:create(2, 0),cc.FadeTo:create(2, 255), + cc.FadeTo:create(2, 0),cc.FadeTo:create(2, 255),cc.DelayTime:create(1)))) + return ret +end + +--#pragma mark Example LayerTestCascadingColor + +-- LayerTestCascadingColorA +local function LayerTestCascadingColorA() + local ret = createLayerDemoLayer("Layer: cascading color") + + local s = cc.Director:getInstance():getWinSize() + local layer1 = cc.Layer:create() + + local sister1 = cc.Sprite:create("Images/grossinis_sister1.png") + local sister2 = cc.Sprite:create("Images/grossinis_sister2.png") + local label = cc.LabelBMFont:create("Test", "fonts/bitmapFontTest.fnt") + + layer1:addChild(sister1) + layer1:addChild(sister2) + layer1:addChild(label) + ret:addChild( layer1, 0, kTagLayer) + + sister1:setPosition( cc.p( s.width*1/3, s.height/2)) + sister2:setPosition( cc.p( s.width*2/3, s.height/2)) + label:setPosition( cc.p( s.width/2, s.height/2)) + + layer1:runAction( + cc.RepeatForever:create( + cc.Sequence:create(cc.TintTo:create(6, 255, 0, 255), + cc.TintTo:create(6, 255, 255, 255), + cc.DelayTime:create(1) + ))) + + + sister1:runAction( + cc.RepeatForever:create( + cc.Sequence:create(cc.TintTo:create(2, 255, 255, 0), + cc.TintTo:create(2, 255, 255, 255), + cc.TintTo:create(2, 0, 255, 255), + cc.TintTo:create(2, 255, 255, 255), + cc.TintTo:create(2, 255, 0, 255), + cc.TintTo:create(2, 255, 255, 255), + cc.DelayTime:create(1) + ))) + + -- Enable cascading in scene + setEnableRecursiveCascading(ret, true) + return ret +end + +-- LayerTestCascadingColorB +local function LayerTestCascadingColorB() + local ret = createLayerDemoLayer("LayerColor: cascading color") + + local s = cc.Director:getInstance():getWinSize() + local layer1 = cc.LayerColor:create(cc.c4b(255, 255, 255, 255), s.width, s.height/2) + + layer1:setPosition( cc.p(0, s.height/2)) + + local sister1 = cc.Sprite:create("Images/grossinis_sister1.png") + local sister2 = cc.Sprite:create("Images/grossinis_sister2.png") + local label = cc.LabelBMFont:create("Test", "fonts/bitmapFontTest.fnt") + + layer1:addChild(sister1) + layer1:addChild(sister2) + layer1:addChild(label) + ret:addChild( layer1, 0, kTagLayer) + + sister1:setPosition( cc.p( s.width*1/3, 0)) + sister2:setPosition( cc.p( s.width*2/3, 0)) + label:setPosition( cc.p( s.width/2, 0)) + + layer1:runAction( + cc.RepeatForever:create( + cc.Sequence:create(cc.TintTo:create(6, 255, 0, 255), + cc.TintTo:create(6, 255, 255, 255), + cc.DelayTime:create(1) + ))) + + sister1:runAction( + cc.RepeatForever:create( + cc.Sequence:create(cc.TintTo:create(2, 255, 255, 0), + cc.TintTo:create(2, 255, 255, 255), + cc.TintTo:create(2, 0, 255, 255), + cc.TintTo:create(2, 255, 255, 255), + cc.TintTo:create(2, 255, 0, 255), + cc.TintTo:create(2, 255, 255, 255), + cc.DelayTime:create(1) + ))) + + -- Enable cascading in scene + setEnableRecursiveCascading(ret, true) + return ret +end + +-- LayerTestCascadingColorC +local function LayerTestCascadingColorC() + local ret = createLayerDemoLayer("LayerColor: non-cascading color") + + local s = cc.Director:getInstance():getWinSize() + local layer1 = cc.LayerColor:create(cc.c4b(255, 255, 255, 255), s.width, s.height/2) + layer1:setCascadeColorEnabled(false) + layer1:setPosition( cc.p(0, s.height/2)) + + local sister1 = cc.Sprite:create("Images/grossinis_sister1.png") + local sister2 = cc.Sprite:create("Images/grossinis_sister2.png") + local label = cc.LabelBMFont:create("Test", "fonts/bitmapFontTest.fnt") + + layer1:addChild(sister1) + layer1:addChild(sister2) + layer1:addChild(label) + ret:addChild( layer1, 0, kTagLayer) + + sister1:setPosition( cc.p( s.width*1/3, 0)) + sister2:setPosition( cc.p( s.width*2/3, 0)) + label:setPosition( cc.p( s.width/2, 0)) + + layer1:runAction( + cc.RepeatForever:create( + cc.Sequence:create(cc.TintTo:create(6, 255, 0, 255), + cc.TintTo:create(6, 255, 255, 255), + cc.DelayTime:create(1) + ))) + + sister1:runAction( + cc.RepeatForever:create( + cc.Sequence:create(cc.TintTo:create(2, 255, 255, 0), + cc.TintTo:create(2, 255, 255, 255), + cc.TintTo:create(2, 0, 255, 255), + cc.TintTo:create(2, 255, 255, 255), + cc.TintTo:create(2, 255, 0, 255), + cc.TintTo:create(2, 255, 255, 255), + cc.DelayTime:create(1) + ))) + return ret +end + +-------------------------------------------------------------------- +-- +-- LayerTest1 +-- +-------------------------------------------------------------------- +local function LayerTest1() + local ret = createLayerDemoLayer("ColorLayer resize (tap & move)") + + local s = cc.Director:getInstance():getWinSize() + local layer = cc.LayerColor:create( cc.c4b(0xFF, 0x00, 0x00, 0x80), 200, 200) + + layer:ignoreAnchorPointForPosition(false) + layer:setPosition( cc.p(s.width/2, s.height/2) ) + ret:addChild(layer, 1, kTagLayer) + + local function updateSize(x, y) + local s = cc.Director:getInstance():getWinSize() + + local newSize = cc.size( math.abs(x - s.width/2)*2, math.abs(y - s.height/2)*2) + + local l = tolua.cast(ret:getChildByTag(kTagLayer), "cc.LayerColor") + + l:setContentSize( newSize ) + end + + local function onTouchesMoved(touches, event) + local touchLocation = touches[1]:getLocation() + + updateSize(touchLocation.x, touchLocation.y) + end + + local function onTouchesBegan(touches, event) + onTouchesMoved(touches, event) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesBegan,cc.Handler.EVENT_TOUCHES_BEGAN ) + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + + local eventDispatcher = ret:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ret) + + return ret +end + +-------------------------------------------------------------------- +-- +-- LayerTest2 +-- +-------------------------------------------------------------------- +local function LayerTest2() + local ret = createLayerDemoLayer("ColorLayer: fade and tint") + + local s = cc.Director:getInstance():getWinSize() + local layer1 = cc.LayerColor:create( cc.c4b(255, 255, 0, 80), 100, 300) + layer1:setPosition(cc.p(s.width/3, s.height/2)) + layer1:ignoreAnchorPointForPosition(false) + ret:addChild(layer1, 1) + + local layer2 = cc.LayerColor:create( cc.c4b(0, 0, 255, 255), 100, 300) + layer2:setPosition(cc.p((s.width/3)*2, s.height/2)) + layer2:ignoreAnchorPointForPosition(false) + ret:addChild(layer2, 1) + + local actionTint = cc.TintBy:create(2, -255, -127, 0) + local actionTintBack = actionTint:reverse() + local seq1 = cc.Sequence:create(actionTint,actionTintBack) + layer1:runAction(seq1) + + local actionFade = cc.FadeOut:create(2.0) + local actionFadeBack = actionFade:reverse() + local seq2 = cc.Sequence:create(actionFade,actionFadeBack) + layer2:runAction(seq2) + + return ret +end + +-------------------------------------------------------------------- +-- +-- LayerTestBlend +-- +-------------------------------------------------------------------- + +local function LayerTestBlend() + local ret = createLayerDemoLayer("ColorLayer: blend") + local s = cc.Director:getInstance():getWinSize() + local layer1 = cc.LayerColor:create( cc.c4b(255, 255, 255, 80) ) + + local sister1 = cc.Sprite:create(s_pPathSister1) + local sister2 = cc.Sprite:create(s_pPathSister2) + + ret:addChild(sister1) + ret:addChild(sister2) + ret:addChild(layer1, 100, kTagLayer) + + sister1:setPosition( cc.p( s.width*1/3, s.height/2) ) + sister2:setPosition( cc.p( s.width*2/3, s.height/2) ) + + local blend = true + + local function newBlend(dt) + local layer = tolua.cast(ret:getChildByTag(kTagLayer), "cc.LayerColor") + + local src = 0 + local dst = 0 + + if blend then + src = gl.SRC_ALPHA + dst = gl.ONE_MINUS_SRC_ALPHA + else + src = gl.ONE_MINUS_DST_COLOR + dst = gl.ZERO + end + + layer:setBlendFunc(src, dst) + blend = not blend + end + + + local schedulerEntry = nil + local function onNodeEvent(event) + if event == "enter" then + schedulerEntry = scheduler:scheduleScriptFunc(newBlend, 1.0, false) + elseif event == "exit" then + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + + ret:registerScriptHandler(onNodeEvent) + return ret +end + +-------------------------------------------------------------------- +-- +-- LayerGradient +-- +-------------------------------------------------------------------- +local function LayerGradient() + local ret = createLayerDemoLayer("LayerGradient", "Touch the screen and move your finger") + local layer1 = cc.LayerGradient:create(cc.c4b(255,0,0,255), cc.c4b(0,255,0,255), cc.p(0.9, 0.9)) + ret:addChild(layer1, 0, kTagLayer) + + local label1 = cc.LabelTTF:create("Compressed Interpolation: Enabled", "Marker Felt", 26) + local label2 = cc.LabelTTF:create("Compressed Interpolation: Disabled", "Marker Felt", 26) + local item1 = cc.MenuItemLabel:create(label1) + local item2 = cc.MenuItemLabel:create(label2) + local item = cc.MenuItemToggle:create(item1) + item:addSubItem(item2) + + local function toggleItem(sender) + -- cclog("toggleItem") + local gradient = tolua.cast(ret:getChildByTag(kTagLayer), "cc.LayerGradient") + gradient:setCompressedInterpolation(not gradient:isCompressedInterpolation()) + end + + item:registerScriptTapHandler(toggleItem) + + local menu = cc.Menu:create(item) + ret:addChild(menu) + local s = cc.Director:getInstance():getWinSize() + menu:setPosition(cc.p(s.width / 2, 100)) + + local function onTouchesMoved(touches, event) + local s = cc.Director:getInstance():getWinSize() + local start = touches[1]:getLocation() + local movingPos = cc.p(s.width/2,s.height/2) + local diff = cc.p(movingPos.x - start.x, movingPos.y - start.y) + diff = cc.pNormalize(diff) + + local gradient = tolua.cast(ret:getChildByTag(1), "cc.LayerGradient") + gradient:setVector(diff) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + + local eventDispatcher = ret:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ret) + + return ret +end + + +-- LayerIgnoreAnchorPointPos + +local kLayerIgnoreAnchorPoint = 1000 + +local function LayerIgnoreAnchorPointPos() + local ret = createLayerDemoLayer("IgnoreAnchorPoint - Position", "Ignoring Anchor Point for position") + + local s = cc.Director:getInstance():getWinSize() + + local l = cc.LayerColor:create(cc.c4b(255, 0, 0, 255), 150, 150) + + l:setAnchorPoint(cc.p(0.5, 0.5)) + l:setPosition(cc.p( s.width/2, s.height/2)) + + local move = cc.MoveBy:create(2, cc.p(100,2)) + local back = tolua.cast(move:reverse(), "cc.MoveBy") + local seq = cc.Sequence:create(move, back) + l:runAction(cc.RepeatForever:create(seq)) + ret:addChild(l, 0, kLayerIgnoreAnchorPoint) + + local child = cc.Sprite:create("Images/grossini.png") + l:addChild(child) + local lsize = l:getContentSize() + child:setPosition(cc.p(lsize.width/2, lsize.height/2)) + + local function onToggle(pObject) + local pLayer = ret:getChildByTag(kLayerIgnoreAnchorPoint) + local ignore = pLayer:isIgnoreAnchorPointForPosition() + pLayer:ignoreAnchorPointForPosition(not ignore) + end + + local item = cc.MenuItemFont:create("Toggle ignore anchor point") + item:registerScriptTapHandler(onToggle) + + local menu = cc.Menu:create(item) + ret:addChild(menu) + + menu:setPosition(cc.p(s.width/2, s.height/2)) + return ret +end + +-- LayerIgnoreAnchorPointRot + +local function LayerIgnoreAnchorPointRot() + local ret = createLayerDemoLayer("IgnoreAnchorPoint - Rotation", "Ignoring Anchor Point for rotations") + + local s = cc.Director:getInstance():getWinSize() + + local l = cc.LayerColor:create(cc.c4b(255, 0, 0, 255), 200, 200) + + l:setAnchorPoint(cc.p(0.5, 0.5)) + l:setPosition(cc.p( s.width/2, s.height/2)) + + ret:addChild(l, 0, kLayerIgnoreAnchorPoint) + + local rot = cc.RotateBy:create(2, 360) + l:runAction(cc.RepeatForever:create(rot)) + + + local child = cc.Sprite:create("Images/grossini.png") + l:addChild(child) + local lsize = l:getContentSize() + child:setPosition(cc.p(lsize.width/2, lsize.height/2)) + + local function onToggle(pObject) + local pLayer = ret:getChildByTag(kLayerIgnoreAnchorPoint) + local ignore = pLayer:isIgnoreAnchorPointForPosition() + pLayer:ignoreAnchorPointForPosition(not ignore) + end + + local item = cc.MenuItemFont:create("Toogle ignore anchor point") + item:registerScriptTapHandler(onToggle) + + local menu = cc.Menu:create(item) + ret:addChild(menu) + + menu:setPosition(cc.p(s.width/2, s.height/2)) + return ret +end + +-- LayerIgnoreAnchorPointScale +local function LayerIgnoreAnchorPointScale() + local ret = createLayerDemoLayer("IgnoreAnchorPoint - Scale", "Ignoring Anchor Point for scale") + local s = cc.Director:getInstance():getWinSize() + + local l = cc.LayerColor:create(cc.c4b(255, 0, 0, 255), 200, 200) + + l:setAnchorPoint(cc.p(0.5, 1.0)) + l:setPosition(cc.p( s.width/2, s.height/2)) + + + local scale = cc.ScaleBy:create(2, 2) + local back = tolua.cast(scale:reverse(), "cc.ScaleBy") + local seq = cc.Sequence:create(scale, back) + + l:runAction(cc.RepeatForever:create(seq)) + + ret:addChild(l, 0, kLayerIgnoreAnchorPoint) + + local child = cc.Sprite:create("Images/grossini.png") + l:addChild(child) + local lsize = l:getContentSize() + child:setPosition(cc.p(lsize.width/2, lsize.height/2)) + + local function onToggle(pObject) + local pLayer = ret:getChildByTag(kLayerIgnoreAnchorPoint) + local ignore = pLayer:isIgnoreAnchorPointForPosition() + pLayer:ignoreAnchorPointForPosition(not ignore) + return ret + end + + local item = cc.MenuItemFont:create("Toogle ignore anchor point") + item:registerScriptTapHandler(onToggle) + + local menu = cc.Menu:create(item) + ret:addChild(menu) + + menu:setPosition(cc.p(s.width/2, s.height/2)) + return ret +end + + +local function LayerExtendedBlendOpacityTest() + local ret = createLayerDemoLayer("Extended Blend & Opacity", "You should see 3 layers") + local layer1 = cc.LayerGradient:create(cc.c4b(255, 0, 0, 255), cc.c4b(255, 0, 255, 255)) + layer1:setContentSize(cc.size(80, 80)) + layer1:setPosition(cc.p(50,50)) + ret:addChild(layer1) + + local layer2 = cc.LayerGradient:create(cc.c4b(0, 0, 0, 127), cc.c4b(255, 255, 255, 127)) + layer2:setContentSize(cc.size(80, 80)) + layer2:setPosition(cc.p(100,90)) + ret:addChild(layer2) + + local layer3 = cc.LayerGradient:create() + layer3:setContentSize(cc.size(80, 80)) + layer3:setPosition(cc.p(150,140)) + layer3:setStartColor(cc.c3b(255, 0, 0)) + layer3:setEndColor(cc.c3b(255, 0, 255)) + layer3:setStartOpacity(255) + layer3:setEndOpacity(255) + layer3:setBlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) + ret:addChild(layer3) + return ret +end + +function LayerTestMain() + cclog("LayerTestMain") + Helper.index = 1 + cc.Director:getInstance():setDepthTest(true) + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + LayerTestCascadingOpacityA, + LayerTestCascadingOpacityB, + LayerTestCascadingOpacityC, + LayerTestCascadingColorA, + LayerTestCascadingColorB, + LayerTestCascadingColorC, + LayerTest1, + LayerTest2, + LayerTestBlend, + LayerGradient, + LayerIgnoreAnchorPointPos, + LayerIgnoreAnchorPointRot, + LayerIgnoreAnchorPointScale, + LayerExtendedBlendOpacityTest + } + scene:addChild(LayerTestCascadingOpacityA()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/LuaBridgeTest/LuaBridgeTest.lua b/samples/lua-tests/src/LuaBridgeTest/LuaBridgeTest.lua new file mode 100644 index 0000000000..6fea800fee --- /dev/null +++ b/samples/lua-tests/src/LuaBridgeTest/LuaBridgeTest.lua @@ -0,0 +1,162 @@ +local targetPlatform = cc.Application:getInstance():getTargetPlatform() + +local LINE_SPACE = 40 +local itemTagBasic = 1000 +local LuaBridgeTestsName = +{ + "LuaJavaBridge", + "LuaObjectCBridge", +} + +local s = cc.Director:getInstance():getWinSize() + +local function LuaBridgeLayer() + local layer = cc.Layer:create() + local menu = cc.Menu:create() + menu:setPosition(cc.p(0, 0)) + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + + local supportObjectCBridge = false + if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or (cc.PLATFORM_OS_MAC == targetPlatform) then + supportObjectCBridge = true + end + + local supportJavaBridge = false + if (cc.PLATFORM_OS_ANDROID == targetPlatform) then + supportJavaBridge = true + end + + local function createMenuToBridgeScene() + local menuToBridgeScene = cc.Menu:create() + local function toBridgeScene() + local scene = LuaBridgeMainTest() + if scene ~= nil then + cc.Director:getInstance():replaceScene(scene) + end + end + --Create BackMneu + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local menuItemFont = cc.MenuItemFont:create("Back") + menuItemFont:setPosition(cc.p(VisibleRect:rightBottom().x - 50, VisibleRect:rightBottom().y + 25)) + menuItemFont:registerScriptTapHandler(toBridgeScene) + menuToBridgeScene:addChild(menuItemFont) + menuToBridgeScene:setPosition(cc.p(0, 0)) + return menuToBridgeScene + end + + local function newLuaJavaBridge() + local newScene = cc.Scene:create() + local titleLabel = cc.LabelTTF:create("", "Arial", 28) + newScene:addChild(titleLabel, 1) + titleLabel:setPosition(s.width / 2, s.height - 50) + titleLabel:setString("LuaJavaBridge Test") + + subtitleLabel = cc.LabelTTF:create("", "Thonburi", 16) + newScene:addChild(subtitleLabel, 1) + subtitleLabel:setPosition(s.width / 2, s.height - 80) + subtitleLabel:setString("See the console.") + if (cc.PLATFORM_OS_ANDROID == targetPlatform) then + local args = { 2 , 3} + local sigs = "(II)I" + local luaj = require "luaj" + local className = "com/cocos2dx/sample/LuaJavaBridgeTest/LuaJavaBridgeTest" + local ok,ret = luaj.callStaticMethod(className,"addTwoNumbers",args,sigs) + if not ok then + print("luaj error:", ret) + else + print("The ret is:", ret) + end + + local function callbackLua(param) + if "success" == param then + print("java call back success") + end + end + args = { "callbacklua", callbackLua } + sigs = "(Ljava/lang/String;I)V" + ok = luaj.callStaticMethod(className,"callbackLua",args,sigs) + if not ok then + print("call callback error") + end + end + return newScene + end + + local function newLuaObjectCBridge() + local newScene = cc.Scene:create() + local titleLabel = cc.LabelTTF:create("", "Arial", 28) + newScene:addChild(titleLabel, 1) + titleLabel:setPosition(s.width / 2, s.height - 50) + titleLabel:setString("LuaObjectCBridge Test") + + subtitleLabel = cc.LabelTTF:create("", "Thonburi", 16) + newScene:addChild(subtitleLabel, 1) + subtitleLabel:setPosition(s.width / 2, s.height - 80) + subtitleLabel:setString("See the console.") + if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or (cc.PLATFORM_OS_MAC == targetPlatform) then + local args = { num1 = 2 , num2 = 3 } + local luaoc = require "luaoc" + local className = "LuaObjectCBridgeTest" + local ok,ret = luaoc.callStaticMethod(className,"addTwoNumbers",args) + if not ok then + Director:getInstance():resume() + else + print("The ret is:", ret) + end + + local function callback(param) + if "success" == param then + print("object c call back success") + end + end + luaoc.callStaticMethod(className,"registerScriptHandler", {scriptHandler = callback } ) + luaoc.callStaticMethod(className,"callbackScriptHandler") + end + return newScene + end + + local function newLuaBridgeScene(idx) + if 1 == idx then + return newLuaJavaBridge() + elseif 2 == idx then + return newLuaObjectCBridge() + end + end + + local function menuCallback(tag, pMenuItem) + local scene = nil + local nIdx = pMenuItem:getLocalZOrder() - itemTagBasic + local newScene = newLuaBridgeScene(nIdx) + if nil ~= newScene then + newScene:addChild(createMenuToBridgeScene(),10) + cc.Director:getInstance():replaceScene(newScene) + end + end + + for i = 1, table.getn(LuaBridgeTestsName) do + local item = cc.MenuItemFont:create(LuaBridgeTestsName[i]) + item:registerScriptTapHandler(menuCallback) + item:setPosition(s.width / 2, s.height - i * LINE_SPACE) + menu:addChild(item, itemTagBasic + i) + if ((i == 1) and (false == supportJavaBridge)) + or ((i == 2) and (false == supportObjectCBridge)) then + item:setEnabled(false) + end + end + + layer:addChild(menu) + + return layer +end + +------------------------------------- +-- LuaBridge Test +------------------------------------- +function LuaBridgeMainTest() + local scene = cc.Scene:create() + scene:addChild(LuaBridgeLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/MenuTest/MenuTest.lua b/samples/lua-tests/src/MenuTest/MenuTest.lua new file mode 100644 index 0000000000..5c1c78c42a --- /dev/null +++ b/samples/lua-tests/src/MenuTest/MenuTest.lua @@ -0,0 +1,594 @@ +local kTagMenu = 1 +local kTagMenu0 = 0 +local kTagMenu1 = 1 + +local MID_CALLBACK = 1000 +local MID_CALLBACK2 = 1001 +local MID_DISABLED = 1002 +local MID_ENABLE = 1003 +local MID_CONFIG = 1004 +local MID_QUIT = 1005 +local MID_OPACITY = 1006 +local MID_ALIGN = 1007 +local MID_CALLBACK3 = 1008 +local MID_BACKCALLBACK = 1009 + +-------------------------------------------------------------------- +-- +-- MenuLayerMainMenu +-- +-------------------------------------------------------------------- +local function MenuLayerMainMenu() + local m_disabledItem = nil + + local ret = cc.Layer:create() + + -- Font Item + local spriteNormal = cc.Sprite:create(s_MenuItem, cc.rect(0,23*2,115,23)) + local spriteSelected = cc.Sprite:create(s_MenuItem, cc.rect(0,23*1,115,23)) + local spriteDisabled = cc.Sprite:create(s_MenuItem, cc.rect(0,23*0,115,23)) + + local item1 = cc.MenuItemSprite:create(spriteNormal, spriteSelected, spriteDisabled) + + local function menuCallback(sender) + cclog("menuCallback...") + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(1) + end + + item1:registerScriptTapHandler(menuCallback) + -- Image Item + local function menuCallback2(sender) + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(2) + end + + local item2 = cc.MenuItemImage:create(s_SendScore, s_PressSendScore) + item2:registerScriptTapHandler(menuCallback2) + + + local schedulerEntry = nil + local scheduler = cc.Director:getInstance():getScheduler() + local function allowTouches(dt) + local pDirector = cc.Director:getInstance() + --pDirector:getTouchDispatcher():setPriority(cc.MENU_HANDLER_PRIORITY +1, ret) + if nil ~= schedulerEntry then + scheduler:unscheduleScriptEntry(schedulerEntry) + schedulerEntry = nil + end + cclog("TOUCHES ALLOWED AGAIN") + end + + + local function menuCallbackDisabled(sender) + -- hijack all touch events for 5 seconds + local pDirector = cc.Director:getInstance() + --pDirector:getTouchDispatcher():setPriority(cc.MENU_HANDLER_PRIORITY -1, ret) + schedulerEntry = scheduler:scheduleScriptFunc(allowTouches, 5, false) + cclog("TOUCHES DISABLED FOR 5 SECONDS") + end + + -- Label Item (LabelAtlas) + local labelAtlas = cc.LabelAtlas:_create("0123456789", "fonts/labelatlas.png", 16, 24, string.byte('.')) + local item3 = cc.MenuItemLabel:create(labelAtlas) + item3:registerScriptTapHandler(menuCallbackDisabled) + item3:setDisabledColor( cc.c3b(32,32,64) ) + item3:setColor( cc.c3b(200,200,255) ) + + local function menuCallbackEnable(sender) + m_disabledItem:setEnabled(not m_disabledItem:isEnabled() ) + end + + -- Font Item + local item4 = cc.MenuItemFont:create("I toggle enable items") + item4:registerScriptTapHandler(menuCallbackEnable) + + item4:setFontSizeObj(20) + cc.MenuItemFont:setFontName("Marker Felt") + + local function menuCallbackConfig(sender) + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(3) + end + + -- Label Item (cc.LabelBMFont) + local label = cc.LabelBMFont:create("configuration", "fonts/bitmapFontTest3.fnt") + local item5 = cc.MenuItemLabel:create(label) + item5:registerScriptTapHandler(menuCallbackConfig) + + -- Testing issue #500 + item5:setScale( 0.8 ) + + -- Events + cc.MenuItemFont:setFontName("Marker Felt") + local function menuCallbackBugsTest(pSender) + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(4) + end + + -- Bugs Item + local item6 = cc.MenuItemFont:create("Bugs") + item6:registerScriptTapHandler(menuCallbackBugsTest) + + local function onQuit(sender) + cclog("onQuit item is clicked.") + end + + -- Font Item + local item7 = cc.MenuItemFont:create("Quit") + item7:registerScriptTapHandler(onQuit) + + local function menuMovingCallback(pSender) + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(5) + end + + local item8 = cc.MenuItemFont:create("Remove menu item when moving") + item8:registerScriptTapHandler(menuMovingCallback) + + local color_action = cc.TintBy:create(0.5, 0, -255, -255) + local color_back = color_action:reverse() + local seq = cc.Sequence:create(color_action, color_back) + item7:runAction(cc.RepeatForever:create(seq)) + + local menu = cc.Menu:create() + + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + menu:addChild(item4) + menu:addChild(item5) + menu:addChild(item6) + menu:addChild(item7) + menu:addChild(item8) + + menu:alignItemsVertically() + + -- elastic effect + local s = cc.Director:getInstance():getWinSize() + + local i = 0 + local child = nil + local pArray = menu:getChildren() + local len = table.getn(pArray) + local pObject = nil + for i = 0, len-1 do + pObject = pArray[i + 1] + if pObject == nil then + break + end + child = tolua.cast(pObject, "cc.Node") + local dstPointX, dstPointY = child:getPosition() + local offset = s.width/2 + 50 + if i % 2 == 0 then + offset = 0-offset + end + child:setPosition( cc.p( dstPointX + offset, dstPointY) ) + child:runAction( cc.EaseElasticOut:create(cc.MoveBy:create(2, cc.p(dstPointX - offset,0)), 0.35) ) + end + + m_disabledItem = item3 + item3:retain() + + m_disabledItem:setEnabled( false ) + + ret:addChild(menu) + menu:setPosition(cc.p(s.width/2, s.height/2)) + +-- local schedulerEntry = nil + local function onNodeEvent(event) + if event == "exit" then + if (schedulerEntry ~= nil) then + scheduler:unscheduleScriptEntry(schedulerEntry) + end + if m_disabledItem ~= nil then +-- m_disabledItem:release() + end + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +-------------------------------------------------------------------- +-- +-- MenuLayer2 +-- +-------------------------------------------------------------------- +local function MenuLayer2() + local ret = cc.Layer:create() + local m_centeredMenu = nil + local m_alignedH = false + + local function alignMenusH() + local i = 0 + for i=0, 1 do + local menu = tolua.cast(ret:getChildByTag(100+i), "cc.Menu") + menu:setPosition( m_centeredMenu ) + if i==0 then + -- TIP: if no padding, padding = 5 + menu:alignItemsHorizontally() + local x, y = menu:getPosition() + menu:setPosition( cc.pAdd(cc.p(x, y), cc.p(0,30)) ) + else + -- TIP: but padding is configurable + menu:alignItemsHorizontallyWithPadding(40) + local x, y = menu:getPosition() + menu:setPosition( cc.pSub(cc.p(x, y), cc.p(0,30)) ) + end + end + end + + local function alignMenusV() + local i = 0 + for i=0, 1 do + local menu = tolua.cast(ret:getChildByTag(100+i), "cc.Menu") + menu:setPosition( m_centeredMenu ) + if i==0 then + -- TIP: if no padding, padding = 5 + menu:alignItemsVertically() + local x, y = menu:getPosition() + menu:setPosition( cc.pAdd(cc.p(x, y), cc.p(100,0)) ) + else + -- TIP: but padding is configurable + menu:alignItemsVerticallyWithPadding(40) + local x, y = menu:getPosition() + menu:setPosition( cc.pSub(cc.p(x, y), cc.p(100,0)) ) + end + end + end + + local function menuCallback(sender) + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(0) + end + + local function menuCallbackOpacity(tag, sender) + local menu = tolua.cast(sender:getParent(), "cc.Menu") + local opacity = menu:getOpacity() + if opacity == 128 then + menu:setOpacity(255) + else + menu:setOpacity(128) + end + end + + local function menuCallbackAlign(sender) + m_alignedH = not m_alignedH + + if m_alignedH then + alignMenusH() + else + alignMenusV() + end + end + + local i = 0 + for i=0, 1 do + local item1 = cc.MenuItemImage:create(s_PlayNormal, s_PlaySelect) + item1:registerScriptTapHandler(menuCallback) + + local item2 = cc.MenuItemImage:create(s_HighNormal, s_HighSelect) + item2:registerScriptTapHandler(menuCallbackOpacity) + + local item3 = cc.MenuItemImage:create(s_AboutNormal, s_AboutSelect) + item3:registerScriptTapHandler(menuCallbackAlign) + + item1:setScaleX( 1.5 ) + item2:setScaleX( 0.5 ) + item3:setScaleX( 0.5 ) + + local menu = cc.Menu:create() + + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + + local s = cc.Director:getInstance():getWinSize() + menu:setPosition(cc.p(s.width/2, s.height/2)) + + menu:setTag( kTagMenu ) + + ret:addChild(menu, 0, 100+i) + local x, y = menu:getPosition() + m_centeredMenu = cc.p(x, y) + end + + m_alignedH = true + alignMenusH() + + return ret +end + +-------------------------------------------------------------------- +-- +-- MenuLayer3 +-- +-------------------------------------------------------------------- +local function MenuLayer3() + local m_disabledItem = nil + local ret = cc.Layer:create() + local function menuCallback(sender) + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(0) + end + + local function menuCallback2(sender) + cclog("Label clicked. Toogling AtlasSprite") + m_disabledItem:setEnabled( not m_disabledItem:isEnabled() ) + m_disabledItem:stopAllActions() + end + + local function menuCallback3(sender) + cclog("MenuItemSprite clicked") + end + + cc.MenuItemFont:setFontName("Marker Felt") + cc.MenuItemFont:setFontSize(28) + + local label = cc.LabelBMFont:create("Enable AtlasItem", "fonts/bitmapFontTest3.fnt") + local item1 = cc.MenuItemLabel:create(label) + item1:registerScriptTapHandler(menuCallback2) + + local item2 = cc.MenuItemFont:create("--- Go Back ---") + item2:registerScriptTapHandler(menuCallback) + + local spriteNormal = cc.Sprite:create(s_MenuItem, cc.rect(0,23*2,115,23)) + local spriteSelected = cc.Sprite:create(s_MenuItem, cc.rect(0,23*1,115,23)) + local spriteDisabled = cc.Sprite:create(s_MenuItem, cc.rect(0,23*0,115,23)) + + + local item3 = cc.MenuItemSprite:create(spriteNormal, spriteSelected, spriteDisabled) + item3:registerScriptTapHandler(menuCallback3) + m_disabledItem = item3 + item3:retain() + m_disabledItem:setEnabled( false ) + + local menu = cc.Menu:create() + + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + + menu:setPosition( cc.p(0,0) ) + + local s = cc.Director:getInstance():getWinSize() + + item1:setPosition( cc.p(s.width/2 - 150, s.height/2) ) + item2:setPosition( cc.p(s.width/2 - 200, s.height/2) ) + item3:setPosition( cc.p(s.width/2, s.height/2 - 100) ) + + local jump = cc.JumpBy:create(3, cc.p(400,0), 50, 4) + item2:runAction( cc.RepeatForever:create(cc.Sequence:create( jump, jump:reverse()))) + + local spin1 = cc.RotateBy:create(3, 360) + local spin2 = tolua.cast(spin1:clone(), "cc.ActionInterval") + local spin3 = tolua.cast(spin1:clone(), "cc.ActionInterval") + + item1:runAction( cc.RepeatForever:create(spin1) ) + item2:runAction( cc.RepeatForever:create(spin2) ) + item3:runAction( cc.RepeatForever:create(spin3) ) + + ret:addChild( menu ) + + menu:setPosition(cc.p(0,0)) + + local function onNodeEvent(event) + if event == "exit" then + if m_disabledItem ~= nil then + --m_disabledItem:release() + end + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + + +-------------------------------------------------------------------- +-- +-- MenuLayer4 +-- +-------------------------------------------------------------------- +local function MenuLayer4() + local ret = cc.Layer:create() + cc.MenuItemFont:setFontName("American Typewriter") + cc.MenuItemFont:setFontSize(18) + local title1 = cc.MenuItemFont:create("Sound") + title1:setEnabled(false) + cc.MenuItemFont:setFontName( "Marker Felt" ) + cc.MenuItemFont:setFontSize(34) + local item1 = cc.MenuItemToggle:create(cc.MenuItemFont:create( "On" )) + + local function menuCallback(tag, sender) + cclog("selected item: tag: %d, index:%d", tag, tolua.cast(sender, "cc.MenuItemToggle"):getSelectedIndex() ) + end + + local function backCallback(tag, sender) + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(0) + end + + item1:registerScriptTapHandler(menuCallback) + item1:addSubItem(cc.MenuItemFont:create( "Off")) + + cc.MenuItemFont:setFontName( "American Typewriter" ) + cc.MenuItemFont:setFontSize(18) + local title2 = cc.MenuItemFont:create( "Music" ) + title2:setEnabled(false) + cc.MenuItemFont:setFontName( "Marker Felt" ) + cc.MenuItemFont:setFontSize(34) + local item2 = cc.MenuItemToggle:create(cc.MenuItemFont:create( "On" )) + item2:registerScriptTapHandler(menuCallback) + item2:addSubItem(cc.MenuItemFont:create( "Off")) + + cc.MenuItemFont:setFontName( "American Typewriter" ) + cc.MenuItemFont:setFontSize(18) + local title3 = cc.MenuItemFont:create( "Quality" ) + title3:setEnabled( false ) + cc.MenuItemFont:setFontName( "Marker Felt" ) + cc.MenuItemFont:setFontSize(34) + local item3 = cc.MenuItemToggle:create(cc.MenuItemFont:create( "High" )) + item3:registerScriptTapHandler(menuCallback) + item3:addSubItem(cc.MenuItemFont:create( "Low" )) + + cc.MenuItemFont:setFontName( "American Typewriter" ) + cc.MenuItemFont:setFontSize(18) + local title4 = cc.MenuItemFont:create( "Orientation" ) + title4:setEnabled(false) + cc.MenuItemFont:setFontName( "Marker Felt" ) + cc.MenuItemFont:setFontSize(34) + local item4 = cc.MenuItemToggle:create(cc.MenuItemFont:create( "Off"), + cc.MenuItemFont:create( "33%" ), + cc.MenuItemFont:create( "66%" ), + cc.MenuItemFont:create( "100%")) + item4:registerScriptTapHandler(menuCallback) + + -- you can change the one of the items by doing this + item4:setSelectedIndex( 2 ) + + cc.MenuItemFont:setFontName( "Marker Felt" ) + cc.MenuItemFont:setFontSize( 34 ) + + local label = cc.LabelBMFont:create( "go back", "fonts/bitmapFontTest3.fnt" ) + local back = cc.MenuItemLabel:create(label) + back:registerScriptTapHandler(backCallback) + + local menu = cc.Menu:create() + + menu:addChild(title1) + menu:addChild(title2) + menu:addChild(item1 ) + menu:addChild(item2 ) + menu:addChild(title3) + menu:addChild(title4) + menu:addChild(item3 ) + menu:addChild(item4 ) + menu:addChild(back ) + + menu:alignItemsInColumns(2, 2, 2, 2, 1) + + ret:addChild(menu) + + local s = cc.Director:getInstance():getWinSize() + menu:setPosition(cc.p(s.width/2, s.height/2)) + return ret +end + +-- BugsTest +local function BugsTest() + local ret = cc.Layer:create() + local function issue1410MenuCallback(tag, pSender) + local menu = tolua.cast(pSender:getParent(), "cc.Menu") + menu:setEnabled(false) + menu:setEnabled(true) + cclog("NO CRASHES") + end + + local function issue1410v2MenuCallback(tag, pSender) + local menu = tolua.cast(pSender:getParent(), "cc.Menu") + menu:setEnabled(true) + menu:setEnabled(false) + cclog("NO CRASHES. AND MENU SHOULD STOP WORKING") + end + + local function backMenuCallback(tag, pSender) + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(0) + end + + + local issue1410 = cc.MenuItemFont:create("Issue 1410") + issue1410:registerScriptTapHandler(issue1410MenuCallback) + local issue1410_2 = cc.MenuItemFont:create("Issue 1410 #2") + issue1410_2:registerScriptTapHandler(issue1410v2MenuCallback) + local back = cc.MenuItemFont:create("Back") + back:registerScriptTapHandler(backMenuCallback) + + local menu = cc.Menu:create() + menu:addChild(issue1410) + menu:addChild(issue1410_2) + menu:addChild(back) + ret:addChild(menu) + menu:alignItemsVertically() + + local s = cc.Director:getInstance():getWinSize() + menu:setPosition(cc.p(s.width/2, s.height/2)) + return ret +end + + +local function RemoveMenuItemWhenMove() + local ret = cc.Layer:create() + local s = cc.Director:getInstance():getWinSize() + + local label = cc.LabelTTF:create("click item and move, should not crash", "Arial", 20) + label:setPosition(cc.p(s.width/2, s.height - 30)) + ret:addChild(label) + + local item = cc.MenuItemFont:create("item 1") + item:retain() + + local back = cc.MenuItemFont:create("go back") + local function goBack(tag, pSender) + tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(0) + end + + back:registerScriptTapHandler(goBack) + + local menu = cc.Menu:create() + menu:addChild(item) + menu:addChild(back) + + ret:addChild(menu) + menu:alignItemsVertically() + + menu:setPosition(cc.p(s.width/2, s.height/2)) + + local function onTouchBegan(touch, event) + return true + end + + local function onTouchMoved(touch, event) + if item ~= nil then + item:removeFromParent(true) + --item:release() + --item = nil + end + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) + local eventDispatcher = ret:getEventDispatcher() + eventDispatcher:addEventListenerWithFixedPriority(listener, -129) + + local function onNodeEvent(event) + if event == "exit" then + ret:getEventDispatcher():removeEventListener(listener) + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +function MenuTestMain() + cclog("MenuTestMain") + local scene = cc.Scene:create() + + local pLayer1 = MenuLayerMainMenu() + local pLayer2 = MenuLayer2() + + local pLayer3 = MenuLayer3() + local pLayer4 = MenuLayer4() + local pLayer5 = BugsTest() + local pLayer6 = RemoveMenuItemWhenMove() + + local layer = cc.LayerMultiplex:create(pLayer1, + pLayer2, + pLayer3, + pLayer4, + pLayer5, + pLayer6 ) + + scene:addChild(layer, 0) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/MotionStreakTest/MotionStreakTest.lua b/samples/lua-tests/src/MotionStreakTest/MotionStreakTest.lua new file mode 100644 index 0000000000..79db81c8c7 --- /dev/null +++ b/samples/lua-tests/src/MotionStreakTest/MotionStreakTest.lua @@ -0,0 +1,181 @@ + +local streak = nil +local titleLabel = nil +local subtitleLabel = nil + +local s = cc.Director:getInstance():getWinSize() +local scheduler = cc.Director:getInstance():getScheduler() + +local firstTick = nil + +local function modeCallback(sender) + fastMode = streak:isFastMode() + if fastMode == true then + streak:setFastMode(false) + else + streak:setFastMode(true) + end +end + +local function getBaseLayer() + local layer = cc.Layer:create() + + local itemMode = cc.MenuItemToggle:create(cc.MenuItemFont:create("Use High Quality Mode")) + itemMode:addSubItem(cc.MenuItemFont:create("Use Fast Mode")) + itemMode:registerScriptTapHandler(modeCallback) + local menuMode = cc.Menu:create() + menuMode:addChild(itemMode) + menuMode:setPosition(cc.p(s.width / 2, s.height / 4)) + layer:addChild(menuMode) + + Helper.initWithLayer(layer) + + return layer +end + +----------------------------------- +-- MotionStreakTest1 +----------------------------------- +local root = nil +local target = nil + +local MotionStreakTest1_entry = nil + +local function MotionStreakTest1_update(dt) + if firstTick == true then + firstTick = false + return + end + + streak:setPosition(target:convertToWorldSpace(cc.p(0, 0))) +end + +local function MotionStreakTest1_onEnterOrExit(tag) + if tag == "enter" then + MotionStreakTest1_entry = scheduler:scheduleScriptFunc(MotionStreakTest1_update, 0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(MotionStreakTest1_entry) + end +end + +local function MotionStreakTest1() + local layer = getBaseLayer() + + root = cc.Sprite:create(s_pPathR1) + layer:addChild(root, 1) + root:setPosition(cc.p(s.width / 2, s.height / 2)) + + target = cc.Sprite:create(s_pPathR1) + root:addChild(target) + target:setPosition(cc.p(s.width / 4, 0)) + + streak = cc.MotionStreak:create(2, 3, 32, cc.c3b(0, 255, 0), s_streak) + layer:addChild(streak) + + local a1 = cc.RotateBy:create(2, 360) + + local action1 = cc.RepeatForever:create(a1) + local motion = cc.MoveBy:create(2, cc.p(100,0)) + root:runAction(cc.RepeatForever:create(cc.Sequence:create(motion, motion:reverse()))) + root:runAction(action1) + + local colorAction = cc.RepeatForever:create(cc.Sequence:create(cc.TintTo:create(0.2, 255, 0, 0), + cc.TintTo:create(0.2, 0, 255, 0), + cc.TintTo:create(0.2, 0, 0, 255), + cc.TintTo:create(0.2, 0, 255, 255), + cc.TintTo:create(0.2, 255, 255, 0), + cc.TintTo:create(0.2, 255, 255, 255))) + + streak:runAction(colorAction) + + firstTick = true + layer:registerScriptHandler(MotionStreakTest1_onEnterOrExit) + + Helper.titleLabel:setString("MotionStreak test 1") + return layer +end + +----------------------------------- +-- MotionStreakTest2 +----------------------------------- +local function MotionStreakTest2() + local layer = getBaseLayer() + + streak = cc.MotionStreak:create(3, 3, 64, cc.c3b(255, 255, 255), s_streak) + layer:addChild(streak) + + streak:setPosition(cc.p(s.width / 2, s.height / 2)) + + local function onTouchesMoved(touches, event) + streak:setPosition( touches[1]:getLocation() ) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + Helper.titleLabel:setString("MotionStreak test") + + return layer +end + +----------------------------------- +-- Issue1358 +----------------------------------- +local Issue1358_entry = nil + +local center = nil +local fAngle = nil +local fRadius = nil + +local function Issue1358_update(dt) + if firstTick == true then + firstTick = false + return + end + fAngle = fAngle + 1.0 + streak:setPosition( + cc.p(center.x + math.cos(fAngle / 180 * math.pi) * fRadius, center.y + math.sin(fAngle/ 180 * math.pi) * fRadius)) +end + +local function Issue1358_onEnterOrExit(tag) + if tag == "enter" then + Issue1358_entry = scheduler:scheduleScriptFunc(Issue1358_update, 0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(Issue1358_entry) + end +end + +local function Issue1358() + local layer = getBaseLayer() + + streak = cc.MotionStreak:create(2.0, 1.0, 50.0, cc.c3b(255, 255, 0), "Images/Icon.png") + layer:addChild(streak) + + center = cc.p(s.width / 2, s.height / 2) + fRadius = s.width / 3 + fAngle = 0 + + firstTick = true + layer:registerScriptHandler(Issue1358_onEnterOrExit) + + Helper.titleLabel:setString("Issue 1358") + Helper.subtitleLabel:setString("The tail should use the texture") + return layer +end + +function MotionStreakTest() + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + MotionStreakTest1, + MotionStreakTest2, + Issue1358 + } + + scene:addChild(MotionStreakTest1()) + scene:addChild(CreateBackMenuItem()) + + return scene +end diff --git a/samples/lua-tests/src/NewEventDispatcherTest/NewEventDispatcherTest.lua b/samples/lua-tests/src/NewEventDispatcherTest/NewEventDispatcherTest.lua new file mode 100644 index 0000000000..f86249bb21 --- /dev/null +++ b/samples/lua-tests/src/NewEventDispatcherTest/NewEventDispatcherTest.lua @@ -0,0 +1,1009 @@ +local itemTagBasic = 1000 + +local testArray = +{ + TouchableSprite = 1, + FixedPriority = 2, + RemoveListenerWhenDispatching = 3, + CustomEvent = 4, + LabelKeyboardEvent = 5, + SpriteAccelerationEvent = 6, + RemoveAndRetainNode = 7, + RemoveListenerAfterAdding = 8, +} + +local curLayerIdx = testArray.TouchableSprite + +local EventDispatcherScene = class("EventDispatcherScene") +EventDispatcherScene.__index = EventDispatcherScene + +function EventDispatcherScene.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, EventDispatcherScene) + return target +end + +function EventDispatcherScene.create() + local scene = EventDispatcherScene.extend(cc.Scene:create()) + return scene +end + + +local EventDispatcherTestDemo = class("EventDispatcherTestDemo") +EventDispatcherTestDemo.__index = EventDispatcherTestDemo +EventDispatcherTestDemo._backItem = nil +EventDispatcherTestDemo._restarItem = nil +EventDispatcherTestDemo._nextItem = nil + +function EventDispatcherTestDemo:onEnter() + +end + +function EventDispatcherTestDemo.title(idx) + if testArray.TouchableSprite == idx then + return "Touchable Sprite Test" + elseif testArray.FixedPriority == idx then + return "Fixed priority test" + elseif testArray.RemoveListenerWhenDispatching == idx then + return "Add and remove listener\n when dispatching event" + elseif testArray.CustomEvent == idx then + return "Send custom event" + elseif testArray.LabelKeyboardEvent == idx then + return "Label Receives Keyboard Event" + elseif testArray.SpriteAccelerationEvent == idx then + return "Sprite Receives Acceleration Event" + elseif testArray.RemoveAndRetainNode == idx then + return "RemoveAndRetainNodeTest" + elseif testArray.RemoveListenerAfterAdding == idx then + return "RemoveListenerAfterAddingTest" + end +end + +function EventDispatcherTestDemo.subTitle(idx) + if testArray.TouchableSprite == idx then + return "Please drag the blocks" + elseif testArray.FixedPriority == idx then + return "Fixed Priority, Blue: 30, Red: 20, Yellow: 10\n The lower value the higher priority will be." + elseif testArray.RemoveListenerWhenDispatching == idx then + return "" + elseif testArray.CustomEvent == idx then + return "" + elseif testArray.LabelKeyboardEvent == idx then + return "Please click keyboard\n(Only available on Desktop and Android)" + elseif testArray.SpriteAccelerationEvent == idx then + return "Please move your device\n(Only available on mobile)" + elseif testArray.RemoveAndRetainNode == idx then + return "Sprite should be removed after 5s, add to scene again after 5s" + elseif testArray.RemoveListenerAfterAdding == idx then + return "Should not crash!" + end +end + +function EventDispatcherTestDemo.create() + local layer = EventDispatcherTestDemo.extend(cc.Layer:create()) + + if nil ~= layer then + layer:createMenu() + layer:onEnter() + layer:creatTitleAndSubTitle(curLayerIdx) + end + + return layer +end + +function EventDispatcherTestDemo.backCallback() + local newScene = EventDispatcherScene.create() + newScene:addChild(backEventDispatcherTest()) + newScene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene(newScene) +end + +function EventDispatcherTestDemo.restartCallback() + local newScene = EventDispatcherScene.create() + newScene:addChild(restartEventDispatcherTest()) + newScene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene(newScene) +end + +function EventDispatcherTestDemo.nextCallback() + local newScene = EventDispatcherScene.create() + newScene:addChild(nextEventDispatcherTest()) + newScene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene(newScene) +end + +function EventDispatcherTestDemo:createMenu() + local menu = cc.Menu:create() + + self._backItem = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + self._backItem:registerScriptTapHandler(self.backCallback) + menu:addChild(self._backItem,itemTagBasic) + self._restarItem = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + self._restarItem:registerScriptTapHandler(self.restartCallback) + menu:addChild(self._restarItem,itemTagBasic) + self._nextItem = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + menu:addChild(self._nextItem,itemTagBasic) + self._nextItem:registerScriptTapHandler(self.nextCallback) + + local size = cc.Director:getInstance():getWinSize() + self._backItem:setPosition(cc.p(size.width / 2 - self._restarItem:getContentSize().width * 2, self._restarItem:getContentSize().height / 2)) + self._restarItem:setPosition(cc.p(size.width / 2, self._restarItem:getContentSize().height / 2)) + self._nextItem:setPosition(cc.p(size.width / 2 + self._restarItem:getContentSize().width * 2, self._restarItem:getContentSize().height / 2)) + + menu:setPosition(cc.p(0, 0)) + + self:addChild(menu) +end + +function EventDispatcherTestDemo:creatTitleAndSubTitle(idx) + local title = cc.LabelTTF:create(EventDispatcherTestDemo.title(idx),"Arial",18) + title:setColor(cc.c3b(128,128,0)) + self:addChild(title, 1, 10000) + title:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 30)) + local subTitle = nil + if "" ~= EventDispatcherTestDemo.subTitle(idx) then + local subTitle = cc.LabelTTF:create(EventDispatcherTestDemo.subTitle(idx), "Arial", 18) + subTitle:setColor(cc.c3b(128,128,0)) + self:addChild(subTitle, 1, 10001) + subTitle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 60) ) + end +end + + +local TouchableSpriteTest = class("TouchableSpriteTest",EventDispatcherTestDemo) +TouchableSpriteTest.__index = TouchableSpriteTest + +function TouchableSpriteTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TouchableSpriteTest) + return target +end + +function TouchableSpriteTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local sprite1 = cc.Sprite:create("Images/CyanSquare.png") + sprite1:setPosition(cc.p(origin.x + size.width/2 - 80, origin.y + size.height/2 + 80)) + self:addChild(sprite1, 10) + + local sprite2 = cc.Sprite:create("Images/MagentaSquare.png") + sprite2:setPosition(cc.p(origin.x + size.width/2, origin.x + size.height/2)) + self:addChild(sprite2, 20) + + local sprite3 = cc.Sprite:create("Images/YellowSquare.png") + sprite3:setPosition(cc.p(0, 0)) + sprite2:addChild(sprite3, 1) + + local function onTouchBegan(touch, event) + local target = tolua.cast(event:getCurrentTarget(),"cc.Sprite") + + local locationInNode = target:convertToNodeSpace(touch:getLocation()) + local s = target:getContentSize() + local rect = cc.rect(0, 0, s.width, s.height) + + if cc.rectContainsPoint(rect, locationInNode) then + print(string.format("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y)) + target:setOpacity(180) + return true + end + return false + end + + local function onTouchMoved(touch, event) + local target = tolua.cast(event:getCurrentTarget(), "cc.Sprite") + local posX,posY = target:getPosition() + local delta = touch:getDelta() + target:setPosition(cc.p(posX + delta.x, posY + delta.y)) + end + + local function onTouchEnded(touch, event) + local target = tolua.cast(event:getCurrentTarget(), "cc.Sprite") + print("sprite onTouchesEnded..") + target:setOpacity(255) + if target == sprite2 then + sprite1:setLocalZOrder(100) + elseif target == sprite1 then + sprite1:setLocalZOrder(0) + end + end + + local listener1 = cc.EventListenerTouchOneByOne:create() + listener1:setSwallowTouches(true) + listener1:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener1:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) + listener1:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, sprite1) + + local listener2 = listener1:clone() + listener2:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN) + listener2:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED) + listener2:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) + eventDispatcher:addEventListenerWithSceneGraphPriority(listener2, sprite2) + + + local listener3 = listener1:clone() + listener3:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener3:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) + listener3:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) + eventDispatcher:addEventListenerWithSceneGraphPriority(listener3, sprite3) + + local function removeAllTouchItem(tag, sender) + sender:setString("Only Next item could be clicked") + eventDispatcher:removeEventListenersForType(cc.EVENT_TOUCH_ONE_BY_ONE) + + local nextMenuItem = cc.MenuItemFont:create("Next") + nextMenuItem:setFontSizeObj(16) + nextMenuItem:setPosition(cc.p(VisibleRect:right().x - 100, VisibleRect:right().y - 30)) + nextMenuItem:registerScriptTapHandler(self.nextCallback) + + local menu2 = cc.Menu:create(nextMenuItem) + menu2:setPosition(cc.p(0, 0)) + menu2:setAnchorPoint(cc.p(0, 0)) + self:addChild(menu2) + end + + local menuItem = cc.MenuItemFont:create("Remove All Touch Listeners") + menuItem:setFontSizeObj(16) + menuItem:setPosition(cc.p(VisibleRect:right().x - 100, VisibleRect:right().y)) + menuItem:registerScriptTapHandler(removeAllTouchItem) + + local menu = cc.Menu:create(menuItem) + menu:setPosition(cc.p(0, 0)) + menu:setAnchorPoint(cc.p(0, 0)) + self:addChild(menu) +end + +function TouchableSpriteTest.create() + local layer = TouchableSpriteTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local TouchableSpriteWithFixedPriority = class("TouchableSpriteWithFixedPriority") +TouchableSpriteWithFixedPriority.__index = TouchableSpriteWithFixedPriority +TouchableSpriteWithFixedPriority._listener = nil +TouchableSpriteWithFixedPriority._fixedPriority = 0 +TouchableSpriteWithFixedPriority._useNodePriority = false + +function TouchableSpriteWithFixedPriority.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, TouchableSpriteWithFixedPriority) + return target +end + +function TouchableSpriteWithFixedPriority:onEnter() + local function onTouchBegan(touch, event) + local locationInNode = self:convertToNodeSpace(touch:getLocation()) + local s = self:getContentSize() + local rect = cc.rect(0, 0, s.width, s.height) + + if cc.rectContainsPoint(rect, locationInNode) then + self:setColor(cc.c3b(255, 0, 0)) + return true + end + + return false + end + + local function onTouchMoved(touch, event) + + end + + local function onTouchEnded(touch, event) + self:setColor(cc.c3b(255, 255, 255)) + end + + local listener = cc.EventListenerTouchOneByOne:create() + self._listener = listener + listener:setSwallowTouches(true) + + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) + listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) + + local eventDispatcher = self:getEventDispatcher() + if self._useNodePriority then + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self) + else + eventDispatcher:addEventListenerWithFixedPriority(listener,self._fixedPriority) + end +end + +function TouchableSpriteWithFixedPriority:onExit() + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:removeEventListener(self._listener) +end + +function TouchableSpriteWithFixedPriority:setPriority(fixedPriority) + self._fixedPriority = fixedPriority + self._useNodePriority = false +end + +function TouchableSpriteWithFixedPriority:setPriorityWithThis(useNodePriority) + self._fixedPriority = 0 + self._useNodePriority = useNodePriority +end + +function TouchableSpriteWithFixedPriority.create() + local touchableSprite = TouchableSpriteWithFixedPriority.extend(cc.Sprite:create()) + + local function onNodeEvent(event) + if event == "enter" then + touchableSprite:onEnter() + elseif event == "exit" then + touchableSprite:onExit() + end + end + + touchableSprite:registerScriptHandler(onNodeEvent) + return touchableSprite +end + +local FixedPriorityTest = class("FixedPriorityTest",EventDispatcherTestDemo) +FixedPriorityTest.__index = FixedPriorityTest + +function FixedPriorityTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, FixedPriorityTest) + return target +end + +function FixedPriorityTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local sprite1 = TouchableSpriteWithFixedPriority.create() + sprite1:setTexture("Images/CyanSquare.png") + sprite1:setPriority(30) + sprite1:setPosition(cc.p(origin.x + size.width/2 - 80, origin.y + size.height/2 + 40)) + self:addChild(sprite1, 10) + + local sprite2 = TouchableSpriteWithFixedPriority.create() + sprite2:setTexture("Images/MagentaSquare.png") + sprite2:setPriority(20) + sprite2:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2) ) + self:addChild(sprite2, 20) + + local sprite3 = TouchableSpriteWithFixedPriority:create() + sprite3:setTexture("Images/YellowSquare.png") + sprite3:setPriority(10) + sprite3:setPosition(cc.p(0, 0)) + sprite2:addChild(sprite3, 1) +end + +function FixedPriorityTest.create() + local layer = FixedPriorityTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local RemoveListenerWhenDispatchingTest = class("RemoveListenerWhenDispatchingTest",EventDispatcherTestDemo) +RemoveListenerWhenDispatchingTest.__index = RemoveListenerWhenDispatchingTest + +function RemoveListenerWhenDispatchingTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, RemoveListenerWhenDispatchingTest) + return target +end + +function RemoveListenerWhenDispatchingTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local sprite1 = cc.Sprite:create("Images/CyanSquare.png") + sprite1:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2)) + self:addChild(sprite1, 10) + + local function onTouchBegan(touch, event) + local locationInNode = sprite1:convertToNodeSpace(touch:getLocation()) + local s = sprite1:getContentSize() + local rect = cc.rect(0, 0, s.width, s.height) + + if cc.rectContainsPoint(rect, locationInNode) then + sprite1:setColor(cc.c3b(255, 0, 0)) + return true + end + + return false + end + + local function onTouchEnded(touch, event) + sprite1:setColor(cc.c3b(255, 255, 255)) + end + + local listener1 = cc.EventListenerTouchOneByOne:create() + listener1:setSwallowTouches(true) + self:setUserObject(listener1) + + listener1:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener1:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) + + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, sprite1) + + local statusLabel = cc.LabelTTF:create("The sprite could be touched!", "", 20) + statusLabel:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height - 90)) + self:addChild(statusLabel) + + local enable = true + local function toggleCallback(tag, sender) + if enable then + eventDispatcher:removeEventListener(listener1) + statusLabel:setString("The sprite could not be touched!") + enable = false + else + eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, sprite1) + statusLabel:setString("The sprite could be touched!") + enable = true + end + end + + local toggleItem = cc.MenuItemToggle:create(cc.MenuItemFont:create("Enabled"), cc.MenuItemFont:create("Disabled")) + toggleItem:setPosition(cc.p(origin.x + size.width/2, origin.y + 80)) + toggleItem:registerScriptTapHandler(toggleCallback) + local menu = cc.Menu:create(toggleItem) + menu:setPosition(cc.p(0, 0)) + menu:setAnchorPoint(cc.p(0, 0)) + self:addChild(menu, -1) +end + + +function RemoveListenerWhenDispatchingTest.create() + local layer = RemoveListenerWhenDispatchingTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local CustomEventTest = class("CustomEventTest",EventDispatcherTestDemo) +CustomEventTest.__index = CustomEventTest +CustomEventTest._listener1 = nil +CustomEventTest._listener2 = nil + +function CustomEventTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, CustomEventTest) + return target +end + +function CustomEventTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + local count1 = 0 + local count2 = 0 + + cc.MenuItemFont:setFontSize(20) + + local statusLabel1 = cc.LabelTTF:create("No custom event 1 received!", "", 20) + statusLabel1:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height-90 )) + self:addChild(statusLabel1) + + local function eventCustomListener1(event) + local str = "Custom event 1 received, "..event._usedata.." times" + statusLabel1:setString(str) + end + + local listener1 = cc.EventListenerCustom:create("game_custom_event1",eventCustomListener1) + self._listener1 = listener1 + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithFixedPriority(listener1, 1) + + local function sendCallback1(tag, sender) + count1 = count1 + 1 + + local event = cc.EventCustom:new("game_custom_event1") + event._usedata = string.format("%d",count1) + eventDispatcher:dispatchEvent(event) + end + local sendItem1 = cc.MenuItemFont:create("Send Custom Event 1") + sendItem1:registerScriptTapHandler(sendCallback1) + sendItem1:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2)) + + local statusLabel2 = cc.LabelTTF:create("No custom event 2 received!", "", 20) + statusLabel2:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height-120 )) + self:addChild(statusLabel2) + + local function eventCustomListener2(event) + local str = "Custom event 2 received, "..event._usedata.." times" + statusLabel2:setString(str) + end + + local listener2 = cc.EventListenerCustom:create("game_custom_event2",eventCustomListener2) + CustomEventTest._listener2 = listener2 + eventDispatcher:addEventListenerWithFixedPriority(listener2, 1) + + local function sendCallback2(tag, sender) + count2 = count2 + 1 + + local event = cc.EventCustom:new("game_custom_event2") + event._usedata = string.format("%d",count2) + eventDispatcher:dispatchEvent(event) + end + local sendItem2 = cc.MenuItemFont:create("Send Custom Event 2") + sendItem2:registerScriptTapHandler(sendCallback2) + sendItem2:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2 - 40)) + + local menu = cc.Menu:create(sendItem1, sendItem2) + menu:setPosition(cc.p(0, 0)) + menu:setAnchorPoint(cc.p(0, 0)) + self:addChild(menu, -1) +end + +function CustomEventTest:onExit() + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:removeEventListener(self._listener1) + eventDispatcher:removeEventListener(self._listener2) +end + +function CustomEventTest.create() + local layer = CustomEventTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + layer:onExit() + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + + + +local LabelKeyboardEventTest = class("LabelKeyboardEventTest",EventDispatcherTestDemo) +LabelKeyboardEventTest.__index = LabelKeyboardEventTest + +function LabelKeyboardEventTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, LabelKeyboardEventTest) + return target +end + +function LabelKeyboardEventTest:onEnter() + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local statusLabel = cc.LabelTTF:create("No keyboard event received!", "", 20) + statusLabel:setPosition(cc.p(origin.x + size.width/2,origin.y + size.height/2)) + self:addChild(statusLabel) + + local function onKeyPressed(keyCode, event) + local buf = string.format("Key %s was pressed!",string.char(keyCode)) + local label = event:getCurrentTarget() + label:setString(buf) + end + + local function onKeyReleased(keyCode, event) + local buf = string.format("Key %s was released!",string.char(keyCode)) + local label = event:getCurrentTarget() + label:setString(buf) + end + + local listener = cc.EventListenerKeyboard:create() + listener:registerScriptHandler(onKeyPressed, cc.Handler.EVENT_KEYBOARD_PRESSED ) + listener:registerScriptHandler(onKeyReleased, cc.Handler.EVENT_KEYBOARD_RELEASED ) + + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, statusLabel) +end + + +function LabelKeyboardEventTest.create() + local layer = LabelKeyboardEventTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local SpriteAccelerationEventTest = class("SpriteAccelerationEventTest",EventDispatcherTestDemo) +SpriteAccelerationEventTest.__index = SpriteAccelerationEventTest + +function SpriteAccelerationEventTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, SpriteAccelerationEventTest) + return target +end + +function SpriteAccelerationEventTest:onEnter() + self:setAccelerometerEnabled(true) + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + local sprite = cc.Sprite:create("Images/ball.png") + sprite:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y)) + self:addChild(sprite) + + local function accelerometerListener(event,x,y,z,timestamp) + local target = event:getCurrentTarget() + local ballSize = target:getContentSize() + local ptNowX,ptNowY = target:getPosition() + ptNowX = ptNowX + x * 9.81 + ptNowY = ptNowY + y * 9.81 + + local minX = math.floor(VisibleRect:left().x + ballSize.width / 2.0) + local maxX = math.floor(VisibleRect:right().x - ballSize.width / 2.0) + if ptNowX < minX then + ptNowX = minX + elseif ptNowX > maxX then + ptNowX = maxX + end + + local minY = math.floor(VisibleRect:bottom().y + ballSize.height / 2.0) + local maxY = math.floor(VisibleRect:top().y - ballSize.height / 2.0) + if ptNowY < minY then + ptNowY = minY + elseif ptNowY > maxY then + ptNowY = maxY + end + + target:setPosition(cc.p(ptNowX , ptNowY)) + end + + local listerner = cc.EventListenerAcceleration:create(accelerometerListener) + + self:getEventDispatcher():addEventListenerWithSceneGraphPriority(listerner,sprite) +end + +function SpriteAccelerationEventTest:onExit() + self:setAccelerometerEnabled(false) +end + +function SpriteAccelerationEventTest.create() + local layer = SpriteAccelerationEventTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + layer:onExit() + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + + +local RemoveAndRetainNodeTest = class("RemoveAndRetainNodeTest",EventDispatcherTestDemo) +RemoveAndRetainNodeTest.__index = RemoveAndRetainNodeTest +RemoveAndRetainNodeTest._spriteSaved = false +RemoveAndRetainNodeTest._sprite = nil + +function RemoveAndRetainNodeTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, RemoveAndRetainNodeTest) + return target +end + +function RemoveAndRetainNodeTest:onEnter() + self._spriteSaved = false + local origin = cc.Director:getInstance():getVisibleOrigin() + local size = cc.Director:getInstance():getVisibleSize() + + local sprite = cc.Sprite:create("Images/CyanSquare.png") + sprite:setPosition(cc.p(origin.x + size.width/2, origin.y + size.height/2)) + self._sprite = sprite + self:addChild(sprite, 10) + + local function onTouchBegan(touch,event) + local target = event:getCurrentTarget() + local locationInNode = target:convertToNodeSpace(touch:getLocation()) + local s = target:getContentSize() + local rect = cc.rect(0, 0, s.width, s.height) + + if cc.rectContainsPoint(rect, locationInNode) then + print(string.format("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y)) + target:setOpacity(180) + return true + end + return false + end + + local function onTouchMoved(touch,event) + local target = event:getCurrentTarget() + local posX,posY = target:getPosition() + local delta = touch:getDelta() + target:setPosition(cc.p(posX + delta.x, posY + delta.y)) + end + + local function onTouchEnded(touch,event) + local target = event:getCurrentTarget() + print("sprite onTouchesEnded.. ") + target:setOpacity(255) + end + + local listener1 = cc.EventListenerTouchOneByOne:create() + listener1:setSwallowTouches(true) + listener1:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN) + listener1:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED) + listener1:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED) + self:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener1, sprite) + + local function retainSprite() + self._spriteSaved = true + self._sprite:retain() + self._sprite:removeFromParent() + end + + local function releaseSprite() + self._spriteSaved = false + self:addChild(self._sprite) + self._sprite:release() + end + + self:runAction( cc.Sequence:create(cc.DelayTime:create(5.0), + cc.CallFunc:create(retainSprite), + cc.DelayTime:create(5.0), + cc.CallFunc:create(releaseSprite) + )) + +end + +function RemoveAndRetainNodeTest:onExit() + if self._spriteSaved then + self._sprite:release() + end +end + +function RemoveAndRetainNodeTest.create() + local layer = RemoveAndRetainNodeTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + layer:onExit() + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + +local RemoveListenerAfterAddingTest = class("RemoveListenerAfterAddingTest",EventDispatcherTestDemo) +RemoveListenerAfterAddingTest.__index = RemoveListenerAfterAddingTest + + +function RemoveListenerAfterAddingTest.extend(target) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, RemoveListenerAfterAddingTest) + return target +end + +function RemoveListenerAfterAddingTest:onEnter() + + local eventDispatcher = self:getEventDispatcher() + + local function item1Callback(tag, sender) + + local function onTouchBegan(touch, event) + CCASSERT(false, "Should not come here!") + return true + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + eventDispatcher:addEventListenerWithFixedPriority(listener, -1) + eventDispatcher:removeEventListener(listener) + end + + local item1 = cc.MenuItemFont:create("Click Me 1") + item1:registerScriptTapHandler(item1Callback) + item1:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y + 80)) + + local function addNextButton() + + local function nextButtonCallback(tag, sender) + self.restartCallback() + end + + local nextButton = cc.MenuItemFont:create("Please Click Me To Reset!") + nextButton:registerScriptTapHandler(nextButtonCallback) + nextButton:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y - 40)) + + local menu = cc.Menu:create(nextButton) + menu:setPosition(VisibleRect:leftBottom()) + menu:setAnchorPoint(cc.p(0,0)) + self:addChild(menu) + end + + local function item2Callback( tag, sender ) + + local function onTouchBegan(touch, event) + print("Should not come here!") + return true + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN) + + eventDispatcher:addEventListenerWithFixedPriority(listener, -1) + eventDispatcher:removeEventListeners(cc.EVENT_TOUCH_ONE_BY_ONE) + + addNextButton() + + end + + local item2 = cc.MenuItemFont:create("Click Me 2") + item2:registerScriptTapHandler(item2Callback) + item2:setPosition(cc.p(VisibleRect:center().x, VisibleRect:center().y + 40)) + + local function item3Callback( tag, sender ) + + local function onTouchBegan(touch, event) + print("Should not come here!") + return true + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN) + + eventDispatcher:addEventListenerWithFixedPriority(listener, -1) + eventDispatcher:removeAllEventListeners() + + addNextButton() + + end + + local item3 = cc.MenuItemFont:create("Click Me 3") + item3:registerScriptTapHandler(item3Callback) + item3:setPosition(VisibleRect:center()) + + local menu = cc.Menu:create(item1, item2, item3) + menu:setPosition(VisibleRect:leftBottom()) + menu:setAnchorPoint(cc.p(0,0)) + + self:addChild(menu) +end + +function RemoveListenerAfterAddingTest:onExit() + +end + +function RemoveListenerAfterAddingTest.create() + local layer = RemoveListenerAfterAddingTest.extend(cc.Layer:create()) + + local function onNodeEvent(event) + if event == "enter" then + layer:onEnter() + elseif event == "exit" then + layer:onExit() + end + end + + layer:createMenu() + layer:creatTitleAndSubTitle(curLayerIdx) + layer:registerScriptHandler(onNodeEvent) + return layer +end + + +local createFunction = +{ + TouchableSpriteTest.create, + FixedPriorityTest.create, + RemoveListenerWhenDispatchingTest.create, + CustomEventTest.create, + LabelKeyboardEventTest.create, + SpriteAccelerationEventTest.create, + RemoveAndRetainNodeTest.create, + RemoveListenerAfterAddingTest.create, +} + +function nextEventDispatcherTest() + curLayerIdx = curLayerIdx + 1 + curLayerIdx = curLayerIdx % table.getn(createFunction) + if 0 == curLayerIdx then + curLayerIdx = table.getn(createFunction) + end + return createFunction[curLayerIdx]() +end + +function backEventDispatcherTest() + curLayerIdx = curLayerIdx - 1 + if curLayerIdx <= 0 then + curLayerIdx = curLayerIdx + table.getn(createFunction) + end + + return createFunction[curLayerIdx]() +end + +function restartEventDispatcherTest() + return createFunction[curLayerIdx]() +end + + +function NewEventDispatcherTest() + local scene = EventDispatcherScene.create() + curLayerIdx = testArray.TouchableSprite + scene:addChild(restartEventDispatcherTest()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/NodeTest/NodeTest.lua b/samples/lua-tests/src/NodeTest/NodeTest.lua new file mode 100644 index 0000000000..6cf01b65e6 --- /dev/null +++ b/samples/lua-tests/src/NodeTest/NodeTest.lua @@ -0,0 +1,708 @@ +local kTagSprite1 = 1 +local kTagSprite2 = 2 +local kTagSprite3 = 3 +local kTagSlider = 4 + +local s = cc.Director:getInstance():getWinSize() +local scheduler = cc.Director:getInstance():getScheduler() + +local function getBaseLayer() + local layer = cc.Layer:create() + + Helper.initWithLayer(layer) + + return layer +end + +----------------------------------- +-- CameraCenterTest +----------------------------------- +local function CameraCenterTest() + local layer = getBaseLayer() + + -- LEFT-TOP + local sprite = cc.Sprite:create("Images/white-512x512.png") + layer:addChild(sprite, 0) + sprite:setPosition(cc.p(s.width / 5 * 1, s.height / 5 * 1)) + sprite:setColor(cc.c3b(255, 0, 0)) + sprite:setTextureRect(cc.rect(0, 0, 120, 50)) + local orbit = cc.OrbitCamera:create(10, 1, 0, 0, 360, 0, 0) + sprite:runAction(cc.RepeatForever:create(orbit)) + -- [sprite setAnchorPoint: cc.p(0,1)) + + -- LEFT-BOTTOM + sprite = cc.Sprite:create("Images/white-512x512.png") + layer:addChild(sprite, 0, 40) + sprite:setPosition(cc.p(s.width / 5 * 1, s.height / 5 * 4)) + sprite:setColor(cc.c3b(0, 0, 255)) + sprite:setTextureRect(cc.rect(0, 0, 120, 50)) + orbit = cc.OrbitCamera:create(10, 1, 0, 0, 360, 0, 0) + sprite:runAction(cc.RepeatForever:create(orbit)) + + -- RIGHT-TOP + sprite = cc.Sprite:create("Images/white-512x512.png") + layer:addChild(sprite, 0) + sprite:setPosition(cc.p(s.width / 5 * 4, s.height / 5 * 1)) + sprite:setColor(cc.c3b(255, 255, 0)) + sprite:setTextureRect(cc.rect(0, 0, 120, 50)) + orbit = cc.OrbitCamera:create(10, 1, 0, 0, 360, 0, 0) + sprite:runAction(cc.RepeatForever:create(orbit)) + + -- RIGHT-BOTTOM + sprite = cc.Sprite:create("Images/white-512x512.png") + layer:addChild(sprite, 0, 40) + sprite:setPosition(cc.p(s.width / 5 * 4, s.height / 5 * 4)) + sprite:setColor(cc.c3b(0, 255, 0)) + sprite:setTextureRect(cc.rect(0, 0, 120, 50)) + orbit = cc.OrbitCamera:create(10, 1, 0, 0, 360, 0, 0) + sprite:runAction(cc.RepeatForever:create(orbit)) + + -- CENTER + sprite = cc.Sprite:create("Images/white-512x512.png") + layer:addChild(sprite, 0, 40) + sprite:setPosition(cc.p(s.width / 2, s.height / 2)) + sprite:setColor(cc.c3b(255, 255, 255)) + sprite:setTextureRect(cc.rect(0, 0, 120, 50)) + orbit = cc.OrbitCamera:create(10, 1, 0, 0, 360, 0, 0) + sprite:runAction(cc.RepeatForever:create(orbit)) + + Helper.titleLabel:setString("Camera Center test") + Helper.subtitleLabel:setString("Sprites should rotate at the same speed") + return layer +end + +----------------------------------- +-- Test2 +----------------------------------- +local function Test2() + local layer = getBaseLayer() + + local sp1 = cc.Sprite:create(s_pPathSister1) + local sp2 = cc.Sprite:create(s_pPathSister2) + local sp3 = cc.Sprite:create(s_pPathSister1) + local sp4 = cc.Sprite:create(s_pPathSister2) + + sp1:setPosition(cc.p(100, s.height /2)) + sp2:setPosition(cc.p(380, s.height /2)) + layer:addChild(sp1) + layer:addChild(sp2) + + sp3:setScale(0.25) + sp4:setScale(0.25) + sp1:addChild(sp3) + sp2:addChild(sp4) + + local a1 = cc.RotateBy:create(2, 360) + local a2 = cc.ScaleBy:create(2, 2) + + local action1 = cc.RepeatForever:create(cc.Sequence:create(a1, a2, a2:reverse())) + local action2 = cc.RepeatForever:create(cc.Sequence:create(a1:clone(), a2:clone(), a2:reverse())) + + sp2:setAnchorPoint(cc.p(0,0)) + + sp1:runAction(action1) + sp2:runAction(action2) + + Helper.titleLabel:setString("anchorPoint and children") + return layer +end + +----------------------------------- +-- Test4 +----------------------------------- +local Test4_layer = nil +local Test4_delay2Entry = nil +local Test4_delay4Entry = nil + +local function delay2(dt) + node = Test4_layer:getChildByTag(2) + local action1 = cc.RotateBy:create(1, 360) + node:runAction(action1) +end + +local function delay4(dt) + scheduler:unscheduleScriptEntry(Test4_delay4Entry) + Test4_layer:removeChildByTag(3, false) +end + +local function Test4_onEnterOrExit(tag) + if tag == "enter" then + Test4_delay2Entry = scheduler:scheduleScriptFunc(delay2, 2.0, false) + Test4_delay4Entry = scheduler:scheduleScriptFunc(delay4, 4.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(Test4_delay2Entry) + scheduler:unscheduleScriptEntry(Test4_delay4Entry) + end +end + +local function Test4() + Test4_layer = getBaseLayer() + + local sp1 = cc.Sprite:create(s_pPathSister1) + local sp2 = cc.Sprite:create(s_pPathSister2) + sp1:setPosition(cc.p(100, 160)) + sp2:setPosition(cc.p(380, 160)) + + Test4_layer:addChild(sp1, 0, 2) + Test4_layer:addChild(sp2, 0, 3) + + Test4_layer:registerScriptHandler(Test4_onEnterOrExit) + + Helper.titleLabel:setString("tags") + return Test4_layer +end + +----------------------------------- +-- Test5 +----------------------------------- +local Test5_entry = nil +local Test5_layer = nil + +local function Test5_addAndRemove(dt) + local sp1 = Test5_layer:getChildByTag(kTagSprite1) + local sp2 = Test5_layer:getChildByTag(kTagSprite2) + + sp1:retain() + sp2:retain() + + Test5_layer:removeChild(sp1, false) + Test5_layer:removeChild(sp2, true) + + Test5_layer:addChild(sp1, 0, kTagSprite1) + Test5_layer:addChild(sp2, 0, kTagSprite2) + + sp1:release() + sp2:release() +end + +local function Test5_onEnterOrExit(tag) + if tag == "enter" then + Test5_entry = scheduler:scheduleScriptFunc(Test5_addAndRemove, 2.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(Test5_entry) + end +end + +local function Test5() + Test5_layer = getBaseLayer() + + local sp1 = cc.Sprite:create(s_pPathSister1) + local sp2 = cc.Sprite:create(s_pPathSister2) + + sp1:setPosition(cc.p(100, 160)) + sp2:setPosition(cc.p(380, 160)) + + local rot = cc.RotateBy:create(2, 360) + local rot_back = rot:reverse() + local forever = cc.RepeatForever:create(cc.Sequence:create(rot, rot_back)) + + local rot2 = cc.RotateBy:create(2, 360) + local rot2_back = rot2:reverse() + local forever2 = cc.RepeatForever:create(cc.Sequence:create(rot2, rot2_back)) + + forever:setTag(101) + forever2:setTag(102) + + Test5_layer:addChild(sp1, 0, kTagSprite1) + Test5_layer:addChild(sp2, 0, kTagSprite2) + + sp1:runAction(forever) + sp2:runAction(forever2) + + Test5_layer:registerScriptHandler(Test5_onEnterOrExit) + + Helper.titleLabel:setString("remove and cleanup") + return Test5_layer +end + +----------------------------------- +-- Test6 +----------------------------------- +local Test6_entry = nil +local Test6_layer = nil + +local function Test6_addAndRemove(dt) + local sp1 = Test6_layer:getChildByTag(kTagSprite1) + local sp2 = Test6_layer:getChildByTag(kTagSprite2) + + sp1:retain() + sp2:retain() + + Test6_layer:removeChild(sp1, false) + Test6_layer:removeChild(sp2, true) + + Test6_layer:addChild(sp1, 0, kTagSprite1) + Test6_layer:addChild(sp2, 0, kTagSprite2) + + sp1:release() + sp2:release() +end + +local function Test6_onEnterOrExit(tag) + if tag == "enter" then + Test6_entry = scheduler:scheduleScriptFunc(Test6_addAndRemove, 2.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(Test6_entry) + end +end + +local function Test6() + Test6_layer = getBaseLayer() + + local sp1 = cc.Sprite:create(s_pPathSister1) + local sp11 = cc.Sprite:create(s_pPathSister1) + + local sp2 = cc.Sprite:create(s_pPathSister2) + local sp21 = cc.Sprite:create(s_pPathSister2) + + sp1:setPosition(cc.p(100, 160)) + sp2:setPosition(cc.p(380, 160)) + + local rot = cc.RotateBy:create(2, 360) + local rot_back = rot:reverse() + local forever1 = cc.RepeatForever:create(cc.Sequence:create(rot, rot_back)) + local forever11 = tolua.cast(forever1:clone(), "cc.RepeatForever") + + local forever2 = tolua.cast(forever1:clone(), "cc.RepeatForever") + local forever21 = tolua.cast(forever1:clone(), "cc.RepeatForever") + + Test6_layer:addChild(sp1, 0, kTagSprite1) + sp1:addChild(sp11) + Test6_layer:addChild(sp2, 0, kTagSprite2) + sp2:addChild(sp21) + + sp1:runAction(forever1) + sp11:runAction(forever11) + sp2:runAction(forever2) + sp21:runAction(forever21) + + Test6_layer:registerScriptHandler(Test6_onEnterOrExit) + + Helper.titleLabel:setString("remove/cleanup with children") + return Test6_layer +end + +----------------------------------- +-- StressTest1 +----------------------------------- +local StressTest1_entry = nil +local StressTest1_layer = nil + +local function removeMe(node) + local parent = StressTest1_layer:getParent() + parent:removeChild(node, true) + Helper.nextAction() +end + +local function shouldNotCrash(dt) + scheduler:unscheduleScriptEntry(StressTest1_entry) + + -- if the node has timers, it crashes + local explosion = cc.ParticleSun:create() + explosion:setTexture(cc.Director:getInstance():getTextureCache():addImage("Images/fire.png")) + + explosion:setPosition(s.width / 2, s.height / 2) + + StressTest1_layer:setAnchorPoint(cc.p(0, 0)) + local callFunc = cc.CallFunc:create(removeMe) + StressTest1_layer:runAction(cc.Sequence:create(cc.RotateBy:create(2, 360), callFunc)) + + StressTest1_layer:addChild(explosion) +end + +local function StressTest1_onEnterOrExit(tag) + if tag == "enter" then + StressTest1_entry = scheduler:scheduleScriptFunc(shouldNotCrash, 1.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(StressTest1_entry) + end +end + +local function StressTest1() + StressTest1_layer = getBaseLayer() + + sp1 = cc.Sprite:create(s_pPathSister1) + StressTest1_layer:addChild(sp1, 0, kTagSprite1) + + sp1:setPosition(cc.p(s.width / 2, s.height / 2)) + + StressTest1_layer:registerScriptHandler(StressTest1_onEnterOrExit) + + Helper.titleLabel:setString("stress test #1: no crashes") + return StressTest1_layer +end + +----------------------------------- +-- StressTest2 +----------------------------------- +local StressTest2_entry = nil +local StressTest2_layer = nil + +local function shouldNotLeak(dt) + scheduler:unscheduleScriptEntry(StressTest2_entry) + + local sublayer = StressTest2_layer:getChildByTag(kTagSprite1) + sublayer:removeAllChildrenWithCleanup(true) +end + +local function StressTest2_onEnterOrExit(tag) + if tag == "enter" then + StressTest2_entry = scheduler:scheduleScriptFunc(shouldNotLeak, 6.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(StressTest2_entry) + end +end + +local function StressTest2() + StressTest2_layer = getBaseLayer() + + sublayer = cc.Layer:create() + + local sp1 = cc.Sprite:create(s_pPathSister1) + sp1:setPosition(cc.p(80, s.height / 2)) + + local move = cc.MoveBy:create(3, cc.p(350, 0)) + local move_ease_inout3 = cc.EaseInOut:create(cc.MoveBy:create(3, cc.p(350, 0)), 2.0) + local move_ease_inout_back3 = move_ease_inout3:reverse() + local seq3 = cc.Sequence:create(move_ease_inout3, move_ease_inout_back3) + sp1:runAction(cc.RepeatForever:create(seq3)) + sublayer:addChild(sp1, 1) + + local fire = cc.ParticleFire:create() + fire:setTexture(cc.Director:getInstance():getTextureCache():addImage("Images/fire.png")) + fire = tolua.cast(fire, "cc.Node") + fire:setPosition(80, s.height / 2 - 50) + + local copy_seq3 = tolua.cast(seq3:clone(), "cc.Sequence") + fire:runAction(cc.RepeatForever:create(copy_seq3)) + sublayer:addChild(fire, 2) + + StressTest2_layer:registerScriptHandler(StressTest2_onEnterOrExit) + + StressTest2_layer:addChild(sublayer, 0, kTagSprite1) + + Helper.titleLabel:setString("stress test #2: no leaks") + return StressTest2_layer +end + +----------------------------------- +-- NodeToWorld +----------------------------------- +local function NodeToWorld() + local layer = getBaseLayer() + + -- This code tests that nodeToParent works OK: + -- - It tests different anchor Points + -- - It tests different children anchor points + + local back = cc.Sprite:create(s_back3) + layer:addChild(back, -10) + back:setAnchorPoint(cc.p(0, 0)) + local backSize = back:getContentSize() + + local item = cc.MenuItemImage:create(s_PlayNormal, s_PlaySelect) + local menu = cc.Menu:create() + menu:addChild(item) + menu:alignItemsVertically() + menu:setPosition(cc.p(backSize.width / 2, backSize.height / 2)) + back:addChild(menu) + + local rot = cc.RotateBy:create(5, 360) + local fe = cc.RepeatForever:create(rot) + item:runAction(fe) + + local move = cc.MoveBy:create(3, cc.p(200, 0)) + local move_back = move:reverse() + local seq = cc.Sequence:create(move, move_back) + local fe2 = cc.RepeatForever:create(seq) + back:runAction(fe2) + + Helper.titleLabel:setString("nodeToParent transform") + return layer +end + +----------------------------------- +-- CameraOrbitTest +----------------------------------- +local function CameraOrbitTest_onEnterOrExit(tag) + if tag == "enter" then + cc.Director:getInstance():setProjection(cc.DIRECTOR_PROJECTION3_D) + elseif tag == "exit" then + cc.Director:getInstance():setProjection(cc.DIRECTOR_PROJECTION2_D) + end +end + +local function CameraOrbitTest() + local layer = getBaseLayer() + + local p = cc.Sprite:create(s_back3) + layer:addChild(p, 0) + p:setPosition(cc.p(s.width / 2, s.height / 2)) + p:setOpacity(128) + + -- LEFT + local s = p:getContentSize() + local sprite = cc.Sprite:create(s_pPathGrossini) + sprite:setScale(0.5) + p:addChild(sprite, 0) + sprite:setPosition(cc.p(s.width / 4 * 1, s.height / 2)) + local orbit = cc.OrbitCamera:create(2, 1, 0, 0, 360, 0, 0) + sprite:runAction(cc.RepeatForever:create(orbit)) + + -- CENTER + sprite = cc.Sprite:create(s_pPathGrossini) + sprite:setScale(1.0) + p:addChild(sprite, 0) + sprite:setPosition(cc.p(s.width / 4 * 2, s.height / 2)) + orbit = cc.OrbitCamera:create(2, 1, 0, 0, 360, 45, 0) + sprite:runAction(cc.RepeatForever:create(orbit)) + + -- RIGHT + sprite = cc.Sprite:create(s_pPathGrossini) + sprite:setScale(2.0) + p:addChild(sprite, 0) + sprite:setPosition(cc.p(s.width / 4 * 3, s.height / 2)) + local ss = sprite:getContentSize() + orbit = cc.OrbitCamera:create(2, 1, 0, 0, 360, 90, -45) + sprite:runAction(cc.RepeatForever:create(orbit)) + + -- PARENT + orbit = cc.OrbitCamera:create(10, 1, 0, 0, 360, 0, 90) + p:runAction(cc.RepeatForever:create(orbit)) + + layer:setScale(1) + + Helper.titleLabel:setString("Camera Orbit test") + return layer +end + +----------------------------------- +-- CameraZoomTest +----------------------------------- +local z = 0 +local CameraZoomTest_layer = nil +local CameraZoomTest_entry = nil + +local function CameraZoomTest_update(dt) + z = z + dt * 100 + + local sprite = CameraZoomTest_layer:getChildByTag(20) + -- local cam = sprite:getCamera() + -- cam:setEye(0, 0, z) + + sprite = CameraZoomTest_layer:getChildByTag(40) + -- cam = sprite:getCamera() + -- cam:setEye(0, 0, -z) +end + +local function CameraZoomTest_onEnterOrExit(tag) + if tag == "enter" then + cc.Director:getInstance():setProjection(cc.DIRECTOR_PROJECTION3_D) + CameraZoomTest_entry = scheduler:scheduleScriptFunc(CameraZoomTest_update, 0.0, false) + elseif tag == "exit" then + cc.Director:getInstance():setProjection(cc.DIRECTOR_PROJECTION2_D) + scheduler:unscheduleScriptEntry(CameraZoomTest_entry) + end +end + +local function CameraZoomTest() + CameraZoomTest_layer = getBaseLayer() + + z = 0 + + -- LEFT + local sprite = cc.Sprite:create(s_pPathGrossini) + CameraZoomTest_layer:addChild(sprite, 0) + sprite:setPosition(cc.p(s.width / 4 * 1, s.height / 2)) + -- local cam = sprite:getCamera() + -- cam:setEye(0, 0, 415 / 2) + -- cam:setCenter(0, 0, 0) + + -- CENTER + sprite = cc.Sprite:create(s_pPathGrossini) + CameraZoomTest_layer:addChild(sprite, 0, 40) + sprite:setPosition(cc.p(s.width / 4 * 2, s.height / 2)) + + -- RIGHT + sprite = cc.Sprite:create(s_pPathGrossini) + CameraZoomTest_layer:addChild(sprite, 0, 20) + sprite:setPosition(cc.p(s.width / 4 * 3, s.height / 2)) + + CameraZoomTest_layer:scheduleUpdateWithPriorityLua(CameraZoomTest_update, 0) + CameraZoomTest_layer:registerScriptHandler(CameraZoomTest_onEnterOrExit) + + Helper.titleLabel:setString("Camera Zoom test") + return CameraZoomTest_layer +end + +----------------------------------- +-- ConvertToNode +----------------------------------- +local ConvertToNode_layer = nil + +local function ConvertToNode() + ConvertToNode_layer = getBaseLayer() + + local rotate = cc.RotateBy:create(10, 360) + local action = cc.RepeatForever:create(rotate) + for i = 0, 2 do + local sprite = cc.Sprite:create("Images/grossini.png") + sprite:setPosition(cc.p(s.width / 4 * (i + 1), s.height / 2)) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale(0.25) + point:setPosition(sprite:getPosition()) + ConvertToNode_layer:addChild(point, 10, 100 + i) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0, 0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + elseif i == 2 then + sprite:setAnchorPoint(cc.p(1,1)) + end + + point:setPosition(sprite:getPosition()) + + local copy = tolua.cast(action:clone(), "cc.RepeatForever") + sprite:runAction(copy) + ConvertToNode_layer:addChild(sprite, i) + end + + local function onTouchesEnded(touches, event) + local count = table.getn(touches) + for i = 1, count do + local location = touches[i]:getLocation() + for j = 1,3 do + local node = ConvertToNode_layer:getChildByTag(100 + i - 1) + local p1, p2 + p1 = node:convertToNodeSpaceAR(location) + p2 = node:convertToNodeSpace(location) + + cclog("AR: x=" .. p1.x .. ", y=" .. p1.y .. " -- Not AR: x=" .. p2.x .. ", y=" .. p2.y) + end + end + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesEnded,cc.Handler.EVENT_TOUCHES_ENDED ) + local eventDispatcher = ConvertToNode_layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ConvertToNode_layer) + + Helper.titleLabel:setString("Convert To Node Space") + Helper.subtitleLabel:setString("testing convertToNodeSpace / AR. Touch and see console") + return ConvertToNode_layer +end + +----------------------------------- +-- NodeOpaqueTest +----------------------------------- +local function NodeOpaqueTest() + local layer = getBaseLayer() + + for i = 0, 49 do + local background = cc.Sprite:create("Images/background1.png") + background:setBlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) + background:setAnchorPoint(cc.p(0, 0)) + layer:addChild(background) + end + + Helper.titleLabel:setString("Node Opaque Test") + Helper.subtitleLabel:setString("Node rendered with GL_BLEND disabled") + return layer +end + +----------------------------------- +-- NodeNonOpaqueTest +----------------------------------- +local function NodeNonOpaqueTest() + local layer = getBaseLayer() + + for i = 0, 49 do + background = cc.Sprite:create("Images/background1.jpg") + background:setBlendFunc(gl.ONE, gl.ZERO) + background:setAnchorPoint(cc.p(0, 0)) + layer:addChild(background) + end + Helper.titleLabel:setString("Node Non Opaque Test") + Helper.subtitleLabel:setString("Node rendered with GL_BLEND enabled") + return layer +end + +----------------------------------- +-- NodeGlobalZValueTest +----------------------------------- +local function NodeGlobalZValueTest() + local layer = getBaseLayer() + Helper.titleLabel:setString("Global Z Value") + Helper.subtitleLabel:setString("Center Sprite should change go from foreground to background") + + + local s = cc.Director:getInstance():getWinSize() + local zOrderSprite = nil + for i = 1,9 do + local parent = cc.Node:create() + local sprite = nil + if i == 5 then + sprite = cc.Sprite:create("Images/grossinis_sister2.png") + sprite:setGlobalZOrder(-1) + zOrderSprite = sprite + else + sprite = cc.Sprite:create("Images/grossinis_sister1.png") + end + parent:addChild(sprite) + layer:addChild(parent) + + local w = sprite:getContentSize().width + sprite:setPosition(s.width/2 - w*0.7*(i - 6),s.height / 2) + end + + local accum = 0 + + local function update(dt) + accum = accum + dt + if accum > 1 then + local z = zOrderSprite:getGlobalZOrder() + zOrderSprite:setGlobalZOrder(-z) + accum = 0 + end + end + + local function onNodeEvent(tag) + if tag == "exit" then + layer:unscheduleUpdate() + end + end + layer:scheduleUpdateWithPriorityLua(update,0) + layer:registerScriptHandler(onNodeEvent) + + return layer +end + + + +function CocosNodeTest() + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + CameraCenterTest, + Test2, + Test4, + Test5, + Test6, + StressTest1, + StressTest2, + NodeToWorld, + CameraOrbitTest, + CameraZoomTest, + ConvertToNode, + NodeOpaqueTest, + NodeNonOpaqueTest, + NodeGlobalZValueTest, + } + + scene:addChild(CameraCenterTest()) + scene:addChild(CreateBackMenuItem()) + + return scene +end diff --git a/samples/lua-tests/src/OpenGLTest/OpenGLTest.lua b/samples/lua-tests/src/OpenGLTest/OpenGLTest.lua new file mode 100644 index 0000000000..3c9885c82d --- /dev/null +++ b/samples/lua-tests/src/OpenGLTest/OpenGLTest.lua @@ -0,0 +1,1177 @@ +require "OpenglConstants" +require "Cocos2dConstants" +require "Opengl" +local function OpenGLTestMainLayer() + local kItemTagBasic = 1000 + local testCount = 16 + local maxCases = testCount + local curCase = 0 + local accum = 0 + local labelBMFont = nil + local size = cc.Director:getInstance():getWinSize() + local curLayer = nil + local schedulEntry = nil + local function OrderCallbackMenu() + local function backCallback() + curCase = curCase - 1 + if curCase < 0 then + curCase = curCase + maxCases + end + ShowCurrentTest() + end + + local function restartCallback() + ShowCurrentTest() + end + + local function nextCallback() + curCase = curCase + 1 + curCase = curCase % maxCases + ShowCurrentTest() + end + + local ordercallbackmenu = cc.Menu:create() + local size = cc.Director:getInstance():getWinSize() + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + item1:registerScriptTapHandler(backCallback) + ordercallbackmenu:addChild(item1,kItemTagBasic) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + item2:registerScriptTapHandler(restartCallback) + ordercallbackmenu:addChild(item2,kItemTagBasic) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + ordercallbackmenu:addChild(item3,kItemTagBasic) + item3:registerScriptTapHandler(nextCallback) + + item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + + ordercallbackmenu:setPosition(cc.p(0, 0)) + + return ordercallbackmenu + end + + local function GetTitle() + if 0 == curCase then + return "Shader Retro Effect" + elseif 1 == curCase then + return "Shader Monjori Test" + elseif 2 == curCase then + return "Shader Mandelbrot Test" + elseif 3 == curCase then + return "Shader Heart Test" + elseif 4 == curCase then + return "Shader Plasma Test" + elseif 5 == curCase then + return "Shader Flower Test" + elseif 6 == curCase then + return "Shader Julia Test" + elseif 7 == curCase then + return "gl.getActive***" + elseif 8 == curCase then + return "TexImage2DTest" + elseif 9 == curCase then + return "GetSupportedExtensionsTest" + elseif 10 == curCase then + return "gl.ReadPixels()" + elseif 11 == curCase then + return "gl.clear(gl.COLOR_BUFFER_BIT)" + elseif 12 == curCase then + return "GLNode + WebGL API" + elseif 13 == curCase then + return "GLNode + cocos2d API" + elseif 14 == curCase then + return "GLTexParamterTest" + elseif 15 == curCase then + return "GLGetUniformTest" + end + end + + local function GetSubTitle() + if 0 == curCase then + return "Should see moving colors, and a sin effect on the letters" + elseif 1 == curCase then + return "Monjori plane deformations" + elseif 2 == curCase then + return "Mandelbrot shader with Zoom" + elseif 3 == curCase then + return "You should see a heart in the center" + elseif 4 == curCase then + return "You should see a plasma in the center" + elseif 5 == curCase then + return "You should see a moving Flower in the center" + elseif 6 == curCase then + return "You should see Julia effect" + elseif 7 == curCase then + return "Tests gl.getActiveUniform / getActiveAttrib. See console" + elseif 8 == curCase then + return "Testing Texture creation" + elseif 9 == curCase then + return "See console for the supported GL extensions" + elseif 10 == curCase then + return "Tests ReadPixels. See console" + elseif 11 == curCase then + return "Testing gl.clear() with cc.GLNode" + elseif 12 == curCase then + return "blue background with a red triangle in the middle" + elseif 13 == curCase then + return "blue background with a red triangle in the middle" + elseif 14 == curCase then + return "tests texParameter()" + elseif 15 == curCase then + return "tests texParameter()" + end + end + + local function InitTitle(layer) + --Title + local lableTitle = cc.LabelTTF:create(GetTitle(), "Arial", 40) + layer:addChild(lableTitle, 15) + lableTitle:setPosition(cc.p(size.width/2, size.height-32)) + lableTitle:setColor(cc.c3b(255,255,40)) + --SubTitle + local subLabelTitle = cc.LabelTTF:create(GetSubTitle(), "Thonburi", 16) + layer:addChild(subLabelTitle, 15) + subLabelTitle:setPosition(cc.p(size.width/2, size.height-80)) + end + + local function updateRetroEffect(fTime) + if nil == labelBMFont then + return + end + accum = accum + fTime + local children = labelBMFont:getChildren() + if nil == children then + return + end + local i = 0 + local len = table.getn(children) + for i= 0 ,len - 1 do + local child = tolua.cast(children[i + 1], "cc.Sprite") + local oldPosX,oldPosY = child:getPosition() + child:setPosition(oldPosX,math.sin(accum * 2 + i / 2.0) * 20) + local scaleY = math.sin(accum * 2 + i / 2.0 + 0.707) + child:setScaleY(scaleY) + end + end + + local function createShaderRetroEffect() + local RetroEffectlayer = cc.Layer:create() + InitTitle(RetroEffectlayer) + local program = cc.GLProgram:create("Shaders/example_ColorBars.vsh", "Shaders/example_ColorBars.fsh") + program:bindAttribLocation(cc.ATTRIBUTE_NAME_POSITION, cc.VERTEX_ATTRIB_POSITION) + program:bindAttribLocation(cc.ATTRIBUTE_NAME_TEX_COORD, cc.VERTEX_ATTRIB_TEX_COORDS) + program:link() + program:updateUniforms() + + label = cc.LabelBMFont:create("RETRO EFFECT","fonts/west_england-64.fnt") + label:setShaderProgram( program ) + + label:setPosition(size.width/2, size.height/2) + RetroEffectlayer:addChild(label) + labelBMFont = label + RetroEffectlayer:scheduleUpdateWithPriorityLua(updateRetroEffect,0) + return RetroEffectlayer + end + + local function createShaderMajoriTest() + local uniformCenter = 0 + local uniformResolution = 0 + local time = 0 + local squareVertexPositionBuffer = {} + local majorLayer = cc.Layer:create() + + InitTitle(majorLayer) + --loadShaderVertex + local shader = cc.GLProgram:create("Shaders/example_Monjori.vsh", "Shaders/example_Monjori.fsh") + + shader:bindAttribLocation("aVertex", cc.VERTEX_ATTRIB_POSITION) + shader:link() + shader:updateUniforms() + + local program = shader:getProgram() + + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(256,256)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + uniformCenter = gl.getUniformLocation(program,"center") + uniformResolution = gl.getUniformLocation( program, "resolution") + glNode:setShaderProgram(shader) + + local function initBuffer() + squareVertexPositionBuffer = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer) + local vertices = { 256,256,0,256,256,0,0,0} + gl.bufferData(gl.ARRAY_BUFFER,8,vertices,gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + local function updateMajori(fTime) + time = time + fTime + end + + local function majoriDraw() + if nil ~= shader then + shader:use() + shader:setUniformsForBuiltins() + --Uniforms + shader:setUniformLocationF32( uniformCenter, size.width/2, size.height/2) + shader:setUniformLocationF32( uniformResolution, 256, 256) + + gl.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION) + + --Draw fullscreen Square + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.TRIANGLE_STRIP,0,4) + gl.bindBuffer(gl.ARRAY_BUFFER,0) + end + end + initBuffer() + majorLayer:scheduleUpdateWithPriorityLua(updateMajori,0) + glNode:registerScriptDrawHandler(majoriDraw) + time = 0 + majorLayer:addChild(glNode,-10) + glNode:setPosition( size.width/2, size.height/2) + return majorLayer + end + + local function createShaderMandelbrotTest() + local uniformCenter = 0 + local uniformResolution = 0 + local time = 0 + local squareVertexPositionBuffer = {} + local mandelbrotLayer = cc.Layer:create() + + InitTitle(mandelbrotLayer) + --loadShaderVertex + local shader = cc.GLProgram:create("Shaders/example_Mandelbrot.vsh", "Shaders/example_Mandelbrot.fsh") + + shader:bindAttribLocation("aVertex", 0) + shader:link() + shader:updateUniforms() + + local program = shader:getProgram() + + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(256,256)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + uniformCenter = gl.getUniformLocation(program,"center") + uniformResolution = gl.getUniformLocation( program, "resolution") + glNode:setShaderProgram(shader) + + local function initBuffer() + squareVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + local vertices = { 256,256,0,256,256,0,0,0} + gl.bufferData(gl.ARRAY_BUFFER,8,vertices,gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + local function updateMandelbrot(fTime) + time = time + fTime + end + + local function mandelbrotDraw() + if nil ~= shader then + shader:use() + shader:setUniformsForBuiltins() + --Uniforms + shader:setUniformLocationF32( uniformCenter, size.width/2, size.height/2) + shader:setUniformLocationF32( uniformResolution, 256, 256) + + gl.glEnableVertexAttribs(0x1) + + --Draw fullscreen Square + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.TRIANGLE_STRIP,0,4) + gl.bindBuffer(gl.ARRAY_BUFFER,0) + end + end + initBuffer() + mandelbrotLayer:scheduleUpdateWithPriorityLua(updateMandelbrot,0) + glNode:registerScriptDrawHandler(mandelbrotDraw) + time = 0 + mandelbrotLayer:addChild(glNode,-10) + glNode:setPosition( size.width/2, size.height/2) + return mandelbrotLayer + end + + local function createShaderHeartTest() + local uniformCenter = 0 + local uniformResolution = 0 + local time = 0 + local squareVertexPositionBuffer = {} + local heartLayer = cc.Layer:create() + + InitTitle(heartLayer) + --loadShaderVertex + local shader = cc.GLProgram:create("Shaders/example_Heart.vsh", "Shaders/example_Heart.fsh") + + shader:bindAttribLocation("aVertex", 0) + shader:link() + shader:updateUniforms() + + local program = shader:getProgram() + + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(256,256)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + uniformCenter = gl.getUniformLocation(program,"center") + uniformResolution = gl.getUniformLocation( program, "resolution") + glNode:setShaderProgram(shader) + + local function initBuffer() + squareVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + local vertices = { 256,256,0,256,256,0,0,0} + gl.bufferData(gl.ARRAY_BUFFER,8,vertices,gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + local function updateHeart(fTime) + time = time + fTime + end + + local function heartDraw() + if nil ~= shader then + shader:use() + shader:setUniformsForBuiltins() + --Uniforms + shader:setUniformLocationF32( uniformCenter, size.width/2, size.height/2) + shader:setUniformLocationF32( uniformResolution, 256, 256) + + gl.glEnableVertexAttribs(0x1) + + --Draw fullscreen Square + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.TRIANGLE_STRIP,0,4) + gl.bindBuffer(gl.ARRAY_BUFFER,0) + end + end + initBuffer() + heartLayer:scheduleUpdateWithPriorityLua(updateHeart,0) + glNode:registerScriptDrawHandler(heartDraw) + time = 0 + heartLayer:addChild(glNode,-10) + glNode:setPosition( size.width/2, size.height/2) + return heartLayer + end + + local function createShaderPlasmaTest() + local uniformCenter = 0 + local uniformResolution = 0 + local time = 0 + local squareVertexPositionBuffer = {} + local plasmaLayer = cc.Layer:create() + + InitTitle(plasmaLayer) + --loadShaderVertex + local shader = cc.GLProgram:create("Shaders/example_Plasma.vsh", "Shaders/example_Plasma.fsh") + + shader:bindAttribLocation("aVertex", 0) + shader:link() + shader:updateUniforms() + + local program = shader:getProgram() + + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(256,256)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + uniformCenter = gl.getUniformLocation(program,"center") + uniformResolution = gl.getUniformLocation( program, "resolution") + glNode:setShaderProgram(shader) + + local function initBuffer() + squareVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + local vertices = { 256,256,0,256,256,0,0,0} + gl.bufferData(gl.ARRAY_BUFFER,8,vertices,gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + local function updatePlasma(fTime) + time = time + fTime + end + + local function plasmaDraw() + if nil ~= shader then + shader:use() + shader:setUniformsForBuiltins() + --Uniforms + shader:setUniformLocationF32( uniformCenter, size.width/2, size.height/2) + shader:setUniformLocationF32( uniformResolution, 256, 256) + + gl.glEnableVertexAttribs(0x1) + + --Draw fullscreen Square + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.TRIANGLE_STRIP,0,4) + gl.bindBuffer(gl.ARRAY_BUFFER,0) + end + end + initBuffer() + plasmaLayer:scheduleUpdateWithPriorityLua(updatePlasma,0) + glNode:registerScriptDrawHandler(plasmaDraw) + time = 0 + plasmaLayer:addChild(glNode,-10) + glNode:setPosition( size.width/2, size.height/2) + return plasmaLayer + end + + local function createShaderFlowerTest() + local uniformCenter = 0 + local uniformResolution = 0 + local time = 0 + local squareVertexPositionBuffer = {} + local flowerLayer = cc.Layer:create() + + InitTitle(flowerLayer) + --loadShaderVertex + local shader = cc.GLProgram:create("Shaders/example_Flower.vsh", "Shaders/example_Flower.fsh") + + shader:bindAttribLocation("aVertex", 0) + shader:link() + shader:updateUniforms() + + local program = shader:getProgram() + + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(256,256)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + uniformCenter = gl.getUniformLocation(program,"center") + uniformResolution = gl.getUniformLocation( program, "resolution") + glNode:setShaderProgram(shader) + + local function initBuffer() + squareVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + local vertices = { 256,256,0,256,256,0,0,0} + gl.bufferData(gl.ARRAY_BUFFER,8,vertices,gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + local function updateFlower(fTime) + time = time + fTime + end + + local function flowerDraw() + if nil ~= shader then + shader:use() + shader:setUniformsForBuiltins() + --Uniforms + shader:setUniformLocationF32( uniformCenter, size.width/2, size.height/2) + shader:setUniformLocationF32( uniformResolution, 256, 256) + + gl.glEnableVertexAttribs(0x1) + + --Draw fullscreen Square + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.TRIANGLE_STRIP,0,4) + gl.bindBuffer(gl.ARRAY_BUFFER,0) + end + end + initBuffer() + flowerLayer:scheduleUpdateWithPriorityLua(updateFlower,0) + glNode:registerScriptDrawHandler(flowerDraw) + time = 0 + flowerLayer:addChild(glNode,-10) + glNode:setPosition( size.width/2, size.height/2) + return flowerLayer + end + + local function createShaderJuliaTest() + local uniformCenter = 0 + local uniformResolution = 0 + local time = 0 + local squareVertexPositionBuffer = {} + local juliaLayer = cc.Layer:create() + + InitTitle(juliaLayer) + --loadShaderVertex + local shader = cc.GLProgram:create("Shaders/example_Julia.vsh", "Shaders/example_Julia.fsh") + + shader:bindAttribLocation("aVertex", 0) + shader:link() + shader:updateUniforms() + + local program = shader:getProgram() + + local glNode = gl.glNodeCreate() + glNode:setContentSize(cc.size(256,256)) + glNode:setAnchorPoint(cc.p(0.5, 0.5)) + uniformCenter = gl.getUniformLocation(program,"center") + uniformResolution = gl.getUniformLocation( program, "resolution") + glNode:setShaderProgram(shader) + + local function initBuffer() + squareVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + local vertices = { 256,256,0,256,256,0,0,0} + gl.bufferData(gl.ARRAY_BUFFER,8,vertices,gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + local function updateJulia(fTime) + time = time + fTime + end + + local function juliaDraw() + if nil ~= shader then + shader:use() + shader:setUniformsForBuiltins() + --Uniforms + shader:setUniformLocationF32( uniformCenter, size.width/2, size.height/2) + shader:setUniformLocationF32( uniformResolution, 256, 256) + + gl.glEnableVertexAttribs(0x1) + + --Draw fullscreen Square + gl.bindBuffer(gl.ARRAY_BUFFER,squareVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + gl.drawArrays(gl.TRIANGLE_STRIP,0,4) + gl.bindBuffer(gl.ARRAY_BUFFER,0) + end + end + initBuffer() + juliaLayer:scheduleUpdateWithPriorityLua(updateJulia,0) + glNode:registerScriptDrawHandler(juliaDraw) + time = 0 + juliaLayer:addChild(glNode,-10) + glNode:setPosition( size.width/2, size.height/2) + return juliaLayer + end + + local function createGLGetActiveTest() + local glGetActiveLayer = cc.Layer:create() + InitTitle(glGetActiveLayer) + local sprite = cc.Sprite:create("Images/grossini.png") + sprite:setPosition( size.width/2, size.height/2) + glGetActiveLayer:addChild(sprite) + local glNode = gl.glNodeCreate() + glGetActiveLayer:addChild(glNode,-10) + local scheduler = cc.Director:getInstance():getScheduler() + + local function getCurrentResult() + local var = {} + local glProgam = tolua.cast(sprite:getShaderProgram(),"cc.GLProgram") + if nil ~= glProgam then + local p = glProgam:getProgram() + local aaSize,aaType,aaName = gl.getActiveAttrib(p,0) + local strFmt = "size:"..aaSize.." type:"..aaType.." name:"..aaName + print(strFmt) + local auSize,auType,auName = gl.getActiveUniform(p,0) + strFmt = "size:"..auSize.." type:"..auType.." name:"..auName + print(strFmt) + local shadersTable = gl.getAttachedShaders(p) + if type(shadersTable) == "table" then + local count = table.getn(shadersTable) + local i = 1 + strFmt = "" + for i=1, count do + strFmt = strFmt..shadersTable[i].." " + end + print(strFmt) + end + + if nil ~= schedulEntry then + scheduler:unscheduleScriptEntry(schedulEntry) + end + end + end + + if nil ~= schedulEntry then + scheduler:unscheduleScriptEntry(schedulEntry) + end + schedulEntry = scheduler:scheduleScriptFunc(getCurrentResult, 0.5, false) + + return glGetActiveLayer + end + + --Have problem + local function createTexImage2DTest() + local texture = {} + local squareVertexPositionBuffer = {} + local squareVertexTextureBuffer = {} + local texImage2dLayer = cc.Layer:create() + InitTitle(texImage2dLayer) + local glNode = gl.glNodeCreate() + texImage2dLayer:addChild(glNode, 10) + glNode:setPosition(size.width/2, size.height/2) + glNode:setContentSize(cc.size(128,128)) + glNode:setAnchorPoint(cc.p(0.5,0.5)) + local shaderCache = cc.ShaderCache:getInstance() + local shader = shaderCache:getProgram("ShaderPositionTexture") + local function initGL() + texture.texture_id = gl.createTexture() + gl.bindTexture(gl.TEXTURE_2D,texture.texture_id ) + local pixels = {} + local i = 1 + while i <= 4096 do + pixels[i] = math.floor(i / 4) + i = i + 1 + pixels[i] = math.floor(i / 16) + i = i + 1 + pixels[i] = math.floor(i / 8) + i = i + 1 + pixels[i] = 255 + i = i + 1 + end + + gl._texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 32, 32, 0, gl.RGBA, gl.UNSIGNED_BYTE, table.getn(pixels),pixels) + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) + gl.bindTexture(gl.TEXTURE_2D, 0) + + --Square + squareVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer.buffer_id) + local vertices = { 128, 128, 0, 128, 128, 0, 0, 0 } + gl.bufferData(gl.ARRAY_BUFFER,table.getn(vertices),vertices,gl.STATIC_DRAW) + + squareVertexTextureBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureBuffer.buffer_id) + local texcoords = { 1, 1, 0, 1, 1, 0, 0, 0 } + gl.bufferData(gl.ARRAY_BUFFER,table.getn(texcoords),texcoords,gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER,0) + end + + local function TexImage2DDraw() + if nil ~= shader then + shader:use() + shader:setUniformsForBuiltins() + + gl.bindTexture(gl.TEXTURE_2D, texture.texture_id) + gl.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_TEX_COORDS or cc.VERTEX_ATTRIB_FLAG_POSITION) + + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION,2,gl.FLOAT,false,0,0) + + + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexTextureBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS,2,gl.FLOAT,false,0,0) + + gl.drawArrays(gl.TRIANGLE_STRIP,0,4) + + gl.bindTexture(gl.TEXTURE_2D,0) + gl.bindBuffer(gl.ARRAY_BUFFER,0) + end + end + + initGL() + glNode:registerScriptDrawHandler(TexImage2DDraw) + return texImage2dLayer + end + + local function CreateSupportedExtensionsLayer() + local extensionsLayer = cc.Layer:create() + InitTitle(extensionsLayer) + local glNode = gl.glNodeCreate() + extensionsLayer:addChild(glNode,-10) + local supportExtensions = gl.getSupportedExtensions() + if type(supportExtensions) ~= "table" then + print("error:return value not table") + return + end + local count = table.getn(supportExtensions) + local i = 1 + for i=1,count do + print(supportExtensions[i]) + end + + return extensionsLayer + end + + local function CreateReadPixelsTest() + local readPixelsLayer = cc.Layer:create() + InitTitle(readPixelsLayer) + local glNode = gl.glNodeCreate() + readPixelsLayer:addChild(glNode,-10) + + local x = size.width + local y = size.height + + local blue = cc.LayerColor:create(cc.c4b(0, 0, 255, 255)) + local red = cc.LayerColor:create(cc.c4b(255, 0, 0, 255)) + local green = cc.LayerColor:create(cc.c4b(0, 255, 0, 255)) + local white = cc.LayerColor:create(cc.c4b(255, 255, 255, 255)) + + blue:setScale(0.5) + blue:setPosition(-x / 4, -y / 4) + + red:setScale(0.5) + red:setPosition(x / 4, -y / 4) + + green:setScale(0.5) + green:setPosition(-x / 4, y / 4) + + white:setScale(0.5) + white:setPosition(x / 4, y / 4) + + readPixelsLayer:addChild(blue,10) + readPixelsLayer:addChild(white,11) + readPixelsLayer:addChild(green,12) + readPixelsLayer:addChild(red,13) + + local scheduler = cc.Director:getInstance():getScheduler() + + local function getCurrentResult() + local x = size.width + local y = size.height + + local pixelCount = 4 + local i = 1 + local strFmt = "" + --blue + local bPixels = gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelCount) + for i=1,pixelCount do + local strTmp = string.format("%d:%d ",i,bPixels[i]) + strFmt = strFmt .. strTmp + end + print(strFmt) + strFmt = "" + --red + local rPixels = gl.readPixels(x-1, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelCount) + for i=1,pixelCount do + local strTmp = string.format("%d:%d ",i,rPixels[i]) + strFmt = strFmt .. strTmp + end + print(strFmt) + strFmt = "" + --green + local gPixels = gl.readPixels(0, y-1, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelCount) + for i=1,pixelCount do + local strTmp = string.format("%d:%d ",i,gPixels[i]) + strFmt = strFmt .. strTmp + end + print(strFmt) + strFmt = "" + --white + local wPixels = gl.readPixels(x-1, y-1, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixelCount) + for i=1,pixelCount do + local strTmp = string.format("%d:%d ",i,wPixels[i]) + strFmt = strFmt .. strTmp + end + print(strFmt) + + if nil ~= schedulEntry then + scheduler:unscheduleScriptEntry(schedulEntry) + end + end + + if nil ~= schedulEntry then + scheduler:unscheduleScriptEntry(schedulEntry) + end + + schedulEntry = scheduler:scheduleScriptFunc(getCurrentResult, 0.5, false) + + return readPixelsLayer + end + + local function createClearTest() + local clearLayer = cc.Layer:create() + InitTitle(clearLayer) + + local blue = cc.LayerColor:create(cc.c4b(0, 0, 255, 255)) + clearLayer:addChild( blue, 1 ) + + local glNode = gl.glNodeCreate() + clearLayer:addChild(glNode,10) + --gl.init() + local scheduler = cc.Director:getInstance():getScheduler() + + local function clearDraw() + gl.clear(gl.COLOR_BUFFER_BIT) + end + + local function getCurrentResult() + + local pixels = gl.readPixels(math.floor(size.width/2), math.floor(size.height/2), 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, 4) + local strFmt = "" + for i=1,4 do + local strTmp = string.format("%d:%d ",i,pixels[i]) + strFmt = strFmt .. strTmp + end + print(strFmt) + if nil ~= schedulEntry then + scheduler:unscheduleScriptEntry(schedulEntry) + end + end + glNode:setPosition( size.width/2, size.height/2 ) + glNode:registerScriptDrawHandler(clearDraw) + + if nil ~= schedulEntry then + scheduler:unscheduleScriptEntry(schedulEntry) + end + + schedulEntry = scheduler:scheduleScriptFunc(getCurrentResult, 0.5, false) + return clearLayer + end + + local function createNodeWebGLAPITest() + local nodeWebGLAPILayer = cc.Layer:create() + InitTitle(nodeWebGLAPILayer) + local glNode = gl.glNodeCreate() + nodeWebGLAPILayer:addChild(glNode,10) + local shaderProgram = {} + local triangleVertexPositionBuffer = {} + local triangleVertexColorBuffer = {} + local squareVertexPositionBuffer = {} + local squareVertexColorBuffer = {} + local pMatrix = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1} + local mvMatrix = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1} + + local vsh = "\n".."attribute vec3 aVertexPosition;\n".."attribute vec4 aVertexColor;\n".. + "uniform mat4 uMVMatrix;\n".."uniform mat4 uPMatrix;\n".."varying vec4 vColor;\n".. + "void main(void) {\n".." gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);\n".." vColor = aVertexColor;\n".. + "}\n" + + local fsh = "\n".. + "#ifdef GL_ES\n".. + "precision mediump float;\n".. + "#endif\n".. + "varying vec4 vColor;\n".. + "void main(void) {\n".. + " gl_FragColor = vColor;\n".. + "}\n" + + local function compileShader(source,type) + local shader + if type == "fragment" then + shader = gl.createShader(gl.FRAGMENT_SHADER) + else + shader = gl.createShader(gl.VERTEX_SHADER) + end + gl.shaderSource(shader,source) + gl.compileShader(shader) + local ret = gl.getShaderParameter(shader,gl.COMPILE_STATUS) + if not ret then + --throw + print("Could not compile "..type.." shader") + end + return shader + end + + local function initBuffers() + triangleVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer.buffer_id) + local vertices = { + 0.0, 1.0, 0.0, + -1.0, -1.0, 0.0, + 1.0, -1.0, 0.0 + } + gl.bufferData(gl.ARRAY_BUFFER, 9, vertices,gl.STATIC_DRAW) + triangleVertexPositionBuffer.itemSize = 3 + triangleVertexPositionBuffer.numItems = 3 + + triangleVertexColorBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer.buffer_id) + local colors = { + 1.0, 0.0, 0.0, 1.0, + 1.0, 0.0, 0.0, 1.0, + 1.0, 0.0, 0.0, 1.0 + } + gl.bufferData(gl.ARRAY_BUFFER, 12, colors , gl.STATIC_DRAW) + triangleVertexColorBuffer.itemSize = 4 + triangleVertexColorBuffer.numItems = 3 + + squareVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer.buffer_id) + vertices = { + 1.0, 1.0, 0.0, + -1.0, 1.0, 0.0, + 1.0, -1.0, 0.0, + -1.0, -1.0, 0.0 + } + gl.bufferData(gl.ARRAY_BUFFER, 12, vertices,gl.STATIC_DRAW) + squareVertexPositionBuffer.itemSize = 3 + squareVertexPositionBuffer.numItems = 4 + + squareVertexColorBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexColorBuffer.buffer_id) + colors = { + 0.0, 0.0, 1.0, 1.0, + 0.0, 0.0, 1.0, 1.0, + 0.0, 0.0, 1.0, 1.0, + 0.0, 0.0, 1.0, 1.0 + } + gl.bufferData(gl.ARRAY_BUFFER, 16,colors, gl.STATIC_DRAW) + squareVertexColorBuffer.itemSize = 4 + squareVertexColorBuffer.numItems = 4 + + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + local function setMatrixUniforms() + gl.uniformMatrix4fv(shaderProgram.pMatrixUniform,false,table.getn(pMatrix), pMatrix) + gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform,false,table.getn(mvMatrix),mvMatrix) + end + + local function nodeWebGLDraw() + gl.useProgram(shaderProgram.program_id) + gl.uniformMatrix4fv(shaderProgram.pMatrixUniform,false,table.getn(pMatrix),pMatrix) + gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform,false,table.getn(mvMatrix),mvMatrix) + + gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute) + gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute) + + --Draw + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0) + + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexColorBuffer.buffer_id) + gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, squareVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0) + + setMatrixUniforms() + + gl.drawArrays(gl.TRIANGLE_STRIP, 0, squareVertexPositionBuffer.numItems) + + --DrawArray + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0) + + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer.buffer_id) + gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, triangleVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0) + + gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems) + + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + local fshader = compileShader(fsh, 'fragment') + local vshader = compileShader(vsh, 'vertex') + shaderProgram.program_id = gl.createProgram() + gl.attachShader(shaderProgram.program_id,vshader) + gl.attachShader(shaderProgram.program_id,fshader) + gl.linkProgram(shaderProgram.program_id) + if not gl.getProgramParameter(shaderProgram.program_id, gl.LINK_STATUS) then + --throw + print("Could not initialise shaders") + end + gl.useProgram(shaderProgram.program_id) + + shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram.program_id,"aVertexPosition") + gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute) + + shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram.program_id,"aVertexColor") + gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute) + + shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram.program_id, "uPMatrix") + shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram.program_id, "uMVMatrix") + + initBuffers() + + glNode:registerScriptDrawHandler(nodeWebGLDraw) + + return nodeWebGLAPILayer + end + + local function createGLNodeCCAPITest() + local nodeCCAPILayer = cc.Layer:create() + InitTitle(nodeCCAPILayer) + local glNode = gl.glNodeCreate() + nodeCCAPILayer:addChild(glNode,10) + local shader = cc.ShaderCache:getInstance():getProgram("ShaderPositionColor") + local triangleVertexPositionBuffer = {} + local triangleVertexColorBuffer = {} + local squareVertexPositionBuffer = {} + local squareVertexColorBuffer = {} + + local function initBuffers() + triangleVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer.buffer_id) + local vertices = { + size.width / 2, size.height, + 0, 0, + size.width, 0 + } + gl.bufferData(gl.ARRAY_BUFFER, table.getn(vertices), vertices, gl.STATIC_DRAW) + + triangleVertexColorBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer.buffer_id) + local colors = { + 1.0, 0.0, 0.0, 1.0, + 1.0, 0.0, 0.0, 1.0, + 1.0, 0.0, 0.0, 1.0 + } + gl.bufferData(gl.ARRAY_BUFFER, table.getn(colors),colors, gl.STATIC_DRAW) + + --Square + squareVertexPositionBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer.buffer_id) + vertices = { + size.width, size.height, + 0, size.height, + size.width, 0, + 0, 0 + } + gl.bufferData(gl.ARRAY_BUFFER, table.getn(vertices), vertices, gl.STATIC_DRAW) + + squareVertexColorBuffer.buffer_id = gl.createBuffer() + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexColorBuffer.buffer_id) + colors = { + 0.0, 0.0, 1.0, 1.0, + 0.0, 0.0, 1.0, 1.0, + 0.0, 0.0, 1.0, 1.0, + 0.0, 0.0, 1.0, 1.0 + }; + gl.bufferData(gl.ARRAY_BUFFER, table.getn(colors), colors, gl.STATIC_DRAW) + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + end + + local function GLNodeCCAPIDraw() + shader:use() + shader:setUniformsForBuiltins() + gl.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_COLOR or cc.VERTEX_ATTRIB_FLAG_POSITION) + + -- + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + + gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexColorBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, gl.FLOAT, false, 0, 0) + + gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4) + + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, gl.FLOAT, false, 0, 0) + + gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer.buffer_id) + gl.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, gl.FLOAT, false, 0, 0) + + gl.drawArrays(gl.TRIANGLE_STRIP, 0, 3) + + gl.bindBuffer(gl.ARRAY_BUFFER, 0) + + end + + initBuffers() + glNode:registerScriptDrawHandler(GLNodeCCAPIDraw) + + return nodeCCAPILayer + end + + local function createGLTexParamterTest() + local glTexParamLayer = cc.Layer:create() + InitTitle(glTexParamLayer) + local glNode = gl.glNodeCreate() + glTexParamLayer:addChild(glNode,10) + local function getTexValues() + gl.bindTexture(gl.TEXTURE_2D, 0) + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) + gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE ) + gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE ) + + local mag = gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER) + local min = gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER) + local w_s = gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S) + local w_t = gl.getTexParameter(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S) + local strFmt = string.format("%d %d %d %d",mag,min,w_s,w_t) + print(strFmt) + end + getTexValues() + return glTexParamLayer + end + + local function createGetUniformTest() + local getUniformLayer = cc.Layer:create() + InitTitle(getUniformLayer) + local glNode = gl.glNodeCreate() + getUniformLayer:addChild(glNode,10) + local pMatrix = {1,2,3,4, 4,3,2,1, 1,2,4,8, 1.1,1.2,1.3,1.4} + + local function runTest() + local shader = cc.ShaderCache:getInstance():getProgram("ShaderPositionTextureColor") + local program = shader:getProgram() + + shader:use() + + local loc = gl.getUniformLocation( program, "CC_MVPMatrix") + + gl.uniformMatrix4fv(loc, false, table.getn(pMatrix), pMatrix) + + local uniformTable = gl.getUniform( program, loc ) + local count = table.getn(uniformTable) + local strFmt = "" + for i=1,count do + local strTmp = string.format("%d: %.16f ",i - 1, uniformTable[i]) + strFmt = strFmt..strTmp + end + print(strFmt) + end + + runTest() + + return getUniformLayer + end + + local function createLayerByCurCase(curCase) + if 0 == curCase then + return createShaderRetroEffect() + elseif 1 == curCase then + return createShaderMajoriTest() + elseif 2 == curCase then + return createShaderMandelbrotTest() + elseif 3 == curCase then + return createShaderHeartTest() + elseif 4 == curCase then + return createShaderPlasmaTest() + elseif 5 == curCase then + return createShaderFlowerTest() + elseif 6 == curCase then + return createShaderJuliaTest() + elseif 7 == curCase then + return createGLGetActiveTest() + elseif 8 == curCase then + return createTexImage2DTest() + elseif 9 == curCase then + return CreateSupportedExtensionsLayer() + elseif 10 == curCase then + return CreateReadPixelsTest() + elseif 11 == curCase then + return createClearTest() + elseif 12 == curCase then + return createNodeWebGLAPITest() + elseif 13 == curCase then + return createGLNodeCCAPITest() + elseif 14 == curCase then + return createGLTexParamterTest() + elseif 15 == curCase then + return createGetUniformTest() + end + end + + function ShowCurrentTest() + local curScene = cc.Scene:create() + if nil ~= curScene then + if nil ~= curLayer then + local scheduler = cc.Director:getInstance():getScheduler() + if nil ~= schedulEntry then + scheduler:unscheduleScriptEntry(schedulEntry) + end + end + curLayer = createLayerByCurCase(curCase) + if nil ~= curLayer then + curScene:addChild(curLayer) + curLayer:addChild(OrderCallbackMenu(),15) + curScene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene(curScene) + end + end + end + curLayer = createLayerByCurCase(curCase) + curLayer:addChild(OrderCallbackMenu(),15) + return curLayer +end + +function OpenGLTestMain() + local scene = cc.Scene:create() + scene:addChild(OpenGLTestMainLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/ParallaxTest/ParallaxTest.lua b/samples/lua-tests/src/ParallaxTest/ParallaxTest.lua new file mode 100644 index 0000000000..af6d1c160f --- /dev/null +++ b/samples/lua-tests/src/ParallaxTest/ParallaxTest.lua @@ -0,0 +1,158 @@ +local kTagNode = 0 +local kTagGrossini = 1 + +local function createParallaxLayer(title, subtitle) + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + local titleStr = title == nil and "No title" or title + local subTitleStr = subtitle == nil and "" or subtitle + Helper.titleLabel:setString(titleStr) + Helper.subtitleLabel:setString(subTitleStr) + return layer +end + +-------------------------------------------------------------------- +-- +-- Parallax1 +-- +-------------------------------------------------------------------- + +local function Parallax1() + local ret = createParallaxLayer("Parallax: parent and 3 children") + -- Top Layer, a simple image + local cocosImage = cc.Sprite:create(s_Power) + -- scale the image (optional) + cocosImage:setScale( 2.5 ) + -- change the transform anchor point to 0,0 (optional) + cocosImage:setAnchorPoint( cc.p(0,0) ) + + + -- Middle layer: a Tile map atlas + local tilemap = cc.TileMapAtlas:create(s_TilesPng, s_LevelMapTga, 16, 16) + tilemap:releaseMap() + + -- change the transform anchor to 0,0 (optional) + tilemap:setAnchorPoint( cc.p(0, 0) ) + + -- Anti Aliased images + tilemap:getTexture():setAntiAliasTexParameters() + + + -- background layer: another image + local background = cc.Sprite:create(s_back) + -- scale the image (optional) + background:setScale( 1.5 ) + -- change the transform anchor point (optional) + background:setAnchorPoint( cc.p(0,0) ) + + + -- create a void node, a parent node + local voidNode = cc.ParallaxNode:create() + + -- NOW add the 3 layers to the 'void' node + + -- background image is moved at a ratio of 0.4x, 0.5y + voidNode:addChild(background, -1, cc.p(0.4,0.5), cc.p(0,0)) + + -- tiles are moved at a ratio of 2.2x, 1.0y + voidNode:addChild(tilemap, 1, cc.p(2.2,1.0), cc.p(0,-200) ) + + -- top image is moved at a ratio of 3.0x, 2.5y + voidNode:addChild(cocosImage, 2, cc.p(3.0,2.5), cc.p(200,800) ) + + + -- now create some actions that will move the 'void' node + -- and the children of the 'void' node will move at different + -- speed, thus, simulation the 3D environment + local goUp = cc.MoveBy:create(4, cc.p(0,-500) ) + local goDown = goUp:reverse() + local go = cc.MoveBy:create(8, cc.p(-1000,0) ) + local goBack = go:reverse() + local seq = cc.Sequence:create(goUp, go, goDown, goBack) + voidNode:runAction( (cc.RepeatForever:create(seq) )) + + ret:addChild( voidNode ) + return ret +end + +-------------------------------------------------------------------- +-- +-- Parallax2 +-- +-------------------------------------------------------------------- + +local function Parallax2() + local ret = createParallaxLayer("Parallax: drag screen") + + -- Top Layer, a simple image + local cocosImage = cc.Sprite:create(s_Power) + -- scale the image (optional) + cocosImage:setScale( 2.5 ) + -- change the transform anchor point to 0,0 (optional) + cocosImage:setAnchorPoint( cc.p(0,0) ) + + + -- Middle layer: a Tile map atlas + local tilemap = cc.TileMapAtlas:create(s_TilesPng, s_LevelMapTga, 16, 16) + tilemap:releaseMap() + + -- change the transform anchor to 0,0 (optional) + tilemap:setAnchorPoint( cc.p(0, 0) ) + + -- Anti Aliased images + tilemap:getTexture():setAntiAliasTexParameters() + + + -- background layer: another image + local background = cc.Sprite:create(s_back) + -- scale the image (optional) + background:setScale( 1.5 ) + -- change the transform anchor point (optional) + background:setAnchorPoint( cc.p(0,0) ) + + + -- create a void node, a parent node + local voidNode = cc.ParallaxNode:create() + + -- NOW add the 3 layers to the 'void' node + + -- background image is moved at a ratio of 0.4x, 0.5y + voidNode:addChild(background, -1, cc.p(0.4,0.5), cc.p(0, 0)) + + -- tiles are moved at a ratio of 1.0, 1.0y + voidNode:addChild(tilemap, 1, cc.p(1.0,1.0), cc.p(0,-200) ) + + -- top image is moved at a ratio of 3.0x, 2.5y + voidNode:addChild( cocosImage, 2, cc.p(3.0,2.5), cc.p(200,1000) ) + ret:addChild(voidNode, 0, kTagNode) + + local function onTouchesMoved(touches, event) + local diff = touches[1]:getDelta() + + local node = ret:getChildByTag(kTagNode) + local currentPosX, currentPosY = node:getPosition() + node:setPosition(cc.p(currentPosX + diff.x, currentPosY + diff.y)) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + local eventDispatcher = ret:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ret) + + return ret +end + +function ParallaxTestMain() + cclog("ParallaxMain") + Helper.index = 1 + cc.Director:getInstance():setDepthTest(true) + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + Parallax1, + Parallax2 + } + scene:addChild(Parallax1()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/ParticleTest/ParticleTest.lua b/samples/lua-tests/src/ParticleTest/ParticleTest.lua new file mode 100644 index 0000000000..4cece01d69 --- /dev/null +++ b/samples/lua-tests/src/ParticleTest/ParticleTest.lua @@ -0,0 +1,1497 @@ + +local SceneIdx = -1 +local MAX_LAYER = 42 + +local emitter = nil +local background = nil + +local labelAtlas = nil +local titleLabel = nil +local subtitleLabel = nil +local baseLayer_entry = nil + +local s = cc.Director:getInstance():getWinSize() + +local function backAction() + SceneIdx = SceneIdx - 1 + if SceneIdx < 0 then + SceneIdx = SceneIdx + MAX_LAYER + end + + return CreateParticleLayer() +end + +local function restartAction() + return CreateParticleLayer() +end + +local function nextAction() + SceneIdx = SceneIdx + 1 + SceneIdx = math.mod(SceneIdx, MAX_LAYER) + + return CreateParticleLayer() +end + +local function backCallback(sender) + local scene = cc.Scene:create() + + scene:addChild(backAction()) + scene:addChild(CreateBackMenuItem()) + + cc.Director:getInstance():replaceScene(scene) +end + +local function restartCallback(sender) + local scene = cc.Scene:create() + + scene:addChild(restartAction()) + scene:addChild(CreateBackMenuItem()) + + cc.Director:getInstance():replaceScene(scene) +end + +local function nextCallback(sender) + local scene = cc.Scene:create() + + scene:addChild(nextAction()) + scene:addChild(CreateBackMenuItem()) + + cc.Director:getInstance():replaceScene(scene) +end + +local function toggleCallback(sender) + if emitter ~= nil then + if emitter:getPositionType() == cc.POSITION_TYPE_GROUPED then + emitter:setPositionType(cc.POSITION_TYPE_FREE) + elseif emitter:getPositionType() == cc.POSITION_TYPE_FREE then + emitter:setPositionType(cc.POSITION_TYPE_RELATIVE) + elseif emitter:getPositionType() == cc.POSITION_TYPE_RELATIVE then + emitter:setPositionType(cc.POSITION_TYPE_GROUPED) + end + end +end + +local function setEmitterPosition() + if emitter ~= nil then + emitter:setPosition(s.width / 2, s.height / 2) + end +end + +local function update(dt) + if emitter ~= nil then + local str = "" .. emitter:getParticleCount() +-- labelAtlas:setString("" .. str) + end +end + +local function baseLayer_onEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + baseLayer_entry = scheduler:scheduleScriptFunc(update, 0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(baseLayer_entry) + end +end + +local function getBaseLayer() + local layer = cc.LayerColor:create(cc.c4b(127,127,127,255)) + + emitter = nil + + titleLabel = cc.LabelTTF:create("", "Arial", 28) + layer:addChild(titleLabel, 100, 1000) + titleLabel:setPosition(s.width / 2, s.height - 50) + + subtitleLabel = cc.LabelTTF:create("", "Arial", 16) + layer:addChild(subtitleLabel, 100) + subtitleLabel:setPosition(s.width / 2, s.height - 80) + + + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + local item4 = cc.MenuItemToggle:create(cc.MenuItemFont:create("Free Movement")) + item4:addSubItem(cc.MenuItemFont:create("Relative Movement")) + item4:addSubItem(cc.MenuItemFont:create("Grouped Movement")) + item1:registerScriptTapHandler(backCallback) + item2:registerScriptTapHandler(restartCallback) + item3:registerScriptTapHandler(nextCallback) + item4:registerScriptTapHandler(toggleCallback) + + local menu = cc.Menu:create() + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + menu:addChild(item4) + + menu:setPosition(cc.p(0, 0)) + item1:setPosition(cc.p(s.width/2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(s.width/2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(s.width/2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item4:setPosition(cc.p(0, 100)) + item4:setAnchorPoint(cc.p(0, 0)) + + layer:addChild(menu, 100) + + labelAtlas = cc.LabelAtlas:_create("0000", "fps_images.png", 12, 32, string.byte('.')) + layer:addChild(labelAtlas, 100) + labelAtlas:setPosition(cc.p(s.width - 66, 50)) + + -- moving background + background = cc.Sprite:create(s_back3) + layer:addChild(background, 5) + background:setPosition(cc.p(s.width / 2, s.height - 180)) + + local move = cc.MoveBy:create(4, cc.p(300, 0)) + local move_back = move:reverse() + local seq = cc.Sequence:create(move, move_back) + background:runAction(cc.RepeatForever:create(seq)) + + local function onTouchesEnded(touches, event) + local location = touches[1]:getLocation() + local pos = cc.p(0, 0) + if background ~= nil then + pos = background:convertToWorldSpace(cc.p(0, 0)) + end + + if emitter ~= nil then + local newPos = cc.pSub(location, pos) + emitter:setPosition(newPos.x, newPos.y) + end + end + + local function onTouchesBegan(touches, event) + onTouchesEnded(touches, event); + end + + local function onTouchesMoved(touches, event) + onTouchesEnded(touches, event); + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesBegan,cc.Handler.EVENT_TOUCHES_BEGAN ) + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + listener:registerScriptHandler(onTouchesEnded,cc.Handler.EVENT_TOUCHES_ENDED ) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + layer:registerScriptHandler(baseLayer_onEnterOrExit) + + return layer +end + +--------------------------------- +-- ParticleReorder +--------------------------------- +local ParticleReorder_Order = nil +local ParticleReorder_entry = nil +local ParticleReorder_layer = nil + +local function reorderParticles(dt) + update(dt) + + for i = 0, 1 do + local parent = ParticleReorder_layer:getChildByTag(1000 + i) + local child1 = parent:getChildByTag(1) + local child2 = parent:getChildByTag(2) + local child3 = parent:getChildByTag(3) + + if math.mod(ParticleReorder_Order, 3) == 0 then + parent:reorderChild(child1, 1) + parent:reorderChild(child2, 2) + parent:reorderChild(child3, 3) + + elseif math.mod(ParticleReorder_Order, 3) == 1 then + parent:reorderChild(child1, 3) + parent:reorderChild(child2, 1) + parent:reorderChild(child3, 2) + + elseif math.mod(ParticleReorder_Order, 3) == 2 then + parent:reorderChild(child1, 2) + parent:reorderChild(child2, 3) + parent:reorderChild(child3, 1) + end + end + + ParticleReorder_Order = ParticleReorder_Order + 1 +end + +local function ParticleReorder_onEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + ParticleReorder_entry = scheduler:scheduleScriptFunc(reorderParticles, 1.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(ParticleReorder_entry) + end +end + +local function ParticleReorder() + ParticleReorder_layer = getBaseLayer() + + ParticleReorder_Order = 0 + ParticleReorder_layer:setColor(cc.c3b(0, 0, 0)) + ParticleReorder_layer:removeChild(background, true) + background = nil + + local ignore = cc.ParticleSystemQuad:create("Particles/SmallSun.plist") + local parent1 = cc.Node:create() + local parent2 = cc.ParticleBatchNode:createWithTexture(ignore:getTexture()) + ignore:unscheduleUpdate() + + for i = 0, 1 do + local parent = nil + if i == 0 then + parent = parent1 + else + parent = parent2 + end + + local emitter1 = cc.ParticleSystemQuad:create("Particles/SmallSun.plist") + emitter1:setStartColor(cc.c4f(1,0,0,1)) + emitter1:setBlendAdditive(false) + local emitter2 = cc.ParticleSystemQuad:create("Particles/SmallSun.plist") + emitter2:setStartColor(cc.c4f(0,1,0,1)) + emitter2:setBlendAdditive(false) + local emitter3 = cc.ParticleSystemQuad:create("Particles/SmallSun.plist") + emitter3:setStartColor(cc.c4f(0,0,1,1)) + emitter3:setBlendAdditive(false) + + local neg = nil + if i == 0 then + neg = 1 + else + neg = -1 + end + + emitter1:setPosition(cc.p( s.width / 2 - 30, s.height / 2 + 60 * neg)) + emitter2:setPosition(cc.p( s.width / 2, s.height / 2 + 60 * neg)) + emitter3:setPosition(cc.p( s.width / 2 + 30, s.height / 2 + 60 * neg)) + + parent:addChild(emitter1, 0, 1) + parent:addChild(emitter2, 0, 2) + parent:addChild(emitter3, 0, 3) + + ParticleReorder_layer:addChild(parent, 10, 1000 + i) + end + + ParticleReorder_layer:registerScriptHandler(ParticleReorder_onEnterOrExit) + + titleLabel:setString("Reordering particles") + subtitleLabel:setString("Reordering particles with and without batches batches") + return ParticleReorder_layer +end + +--------------------------------- +-- ParticleBatchHybrid +--------------------------------- +local ParticleBatchHybrid_entry = nil +local ParticleBatchHybrid_parent1 = nil +local ParticleBatchHybrid_parent2 = nil + +local function switchRender(dt) + update(dt) + + local cond = (emitter:getBatchNode() ~= nil) + emitter:removeFromParent(false) + local str = "Particle: Using new parent: " + local newParent = nil + if cond == true then + newParent = ParticleBatchHybrid_parent2 + str = str .. "Node" + else + newParent = ParticleBatchHybrid_parent1 + str = str .. "ParticleBatchNode" + end + newParent:addChild(emitter) + cclog(str) +end + +local function ParticleBatchHybrid_onEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + ParticleBatchHybrid_entry = scheduler:scheduleScriptFunc(switchRender, 2.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(ParticleBatchHybrid_entry) + emitter:release() + end +end + +local function ParticleBatchHybrid() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + emitter = cc.ParticleSystemQuad:create("Particles/LavaFlow.plist") + emitter:retain() + local batch = cc.ParticleBatchNode:createWithTexture(emitter:getTexture()) + batch:addChild(emitter) + layer:addChild(batch, 10) + + local node = cc.Node:create() + layer:addChild(node) + ParticleBatchHybrid_parent1 = batch + ParticleBatchHybrid_parent2 = node + + layer:registerScriptHandler(ParticleBatchHybrid_onEnterOrExit) + + titleLabel:setString("Particle Batch") + subtitleLabel:setString("Hybrid: batched and non batched every 2 seconds") + return layer +end + +--------------------------------- +-- ParticleBatchMultipleEmitters +--------------------------------- +local function ParticleBatchMultipleEmitters() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + local emitter1 = cc.ParticleSystemQuad:create("Particles/LavaFlow.plist") + emitter1:setStartColor(cc.c4f(1,0,0,1)) + local emitter2 = cc.ParticleSystemQuad:create("Particles/LavaFlow.plist") + emitter2:setStartColor(cc.c4f(0,1,0,1)) + local emitter3 = cc.ParticleSystemQuad:create("Particles/LavaFlow.plist") + emitter3:setStartColor(cc.c4f(0,0,1,1)) + + emitter1:setPosition(cc.p(s.width / 1.25, s.height / 1.25)) + emitter2:setPosition(cc.p(s.width / 2, s.height / 2)) + emitter3:setPosition(cc.p(s.width / 4, s.height / 4)) + + local batch = cc.ParticleBatchNode:createWithTexture(emitter1:getTexture()) + + batch:addChild(emitter1, 0) + batch:addChild(emitter2, 0) + batch:addChild(emitter3, 0) + + layer:addChild(batch, 10) + + titleLabel:setString("Particle Batch") + subtitleLabel:setString("Multiple emitters. One Batch") + return layer +end + +--------------------------------- +-- DemoFlower +--------------------------------- +local function DemoFlower() + local layer = getBaseLayer() + + emitter = cc.ParticleFlower:create() + -- emitter:retain() + background:addChild(emitter, 10) + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_stars1)) + + setEmitterPosition() + + titleLabel:setString("ParticleFlower") + return layer +end + +--------------------------------- +-- DemoGalaxy +--------------------------------- +local function DemoGalaxy() + local layer = getBaseLayer() + + emitter = cc.ParticleGalaxy:create() + -- emitter:retain() + background:addChild(emitter, 10) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + + setEmitterPosition() + + titleLabel:setString("ParticleGalaxy") + return layer +end + +--------------------------------- +-- DemoFirework +--------------------------------- +local function DemoFirework() + local layer = getBaseLayer() + + emitter = cc.ParticleFireworks:create() + -- emitter:retain() + background:addChild(emitter, 10) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_stars1)) + + setEmitterPosition() + + titleLabel:setString("ParticleFireworks") + return layer +end + +--------------------------------- +-- DemoSpiral +--------------------------------- +local function DemoSpiral() + local layer = getBaseLayer() + + emitter = cc.ParticleSpiral:create() + -- emitter:retain() + background:addChild(emitter, 10) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + + setEmitterPosition() + + titleLabel:setString("ParticleSpiral") + return layer +end + +--------------------------------- +-- DemoSun +--------------------------------- +local function DemoSun() + local layer = getBaseLayer() + + emitter = cc.ParticleSun:create() + -- emitter:retain() + background:addChild(emitter, 10) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + + setEmitterPosition() + + titleLabel:setString("ParticleSun") + return layer +end + +--------------------------------- +-- DemoMeteor +--------------------------------- +local function DemoMeteor() + local layer = getBaseLayer() + + emitter = cc.ParticleMeteor:create() + -- emitter:retain() + background:addChild(emitter, 10) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + + setEmitterPosition() + + titleLabel:setString("ParticleMeteor") + return layer +end + +--------------------------------- +-- DemoFire +--------------------------------- +local function DemoFire() + local layer = getBaseLayer() + + emitter = cc.ParticleFire:create() + -- emitter:retain() + background:addChild(emitter, 10) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + local pos_x, pos_y = emitter:getPosition() + emitter:setPosition(pos_x, 100) + + titleLabel:setString("ParticleFire") + return layer +end + +--------------------------------- +-- DemoSmoke +--------------------------------- +local function DemoSmoke() + local layer = getBaseLayer() + + emitter = cc.ParticleSmoke:create() + -- emitter:retain() + background:addChild(emitter, 10) + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + local pos_x, pos_y = emitter:getPosition() + emitter:setPosition(pos_x, 100) + + setEmitterPosition() + + titleLabel:setString("ParticleSmoke") + return layer +end + +--------------------------------- +-- DemoExplosion +--------------------------------- +local function DemoExplosion() + local layer = getBaseLayer() + + emitter = cc.ParticleExplosion:create() + -- emitter:retain() + background:addChild(emitter, 10) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_stars1)) + + emitter:setAutoRemoveOnFinish(true) + + setEmitterPosition() + + titleLabel:setString("ParticleExplosion") + return layer +end + +--------------------------------- +-- DemoSnow +--------------------------------- +local function DemoSnow() + local layer = getBaseLayer() + + emitter = cc.ParticleSnow:create() + -- emitter:retain() + background:addChild(emitter, 10) + local pos_x, pos_y = emitter:getPosition() + emitter:setPosition(pos_x, pos_y - 110) + emitter:setLife(3) + emitter:setLifeVar(1) + + -- gravity + emitter:setGravity(cc.p(0, -10)) + + -- speed of particles + emitter:setSpeed(130) + emitter:setSpeedVar(30) + + local startColor = emitter:getStartColor() + startColor.r = 0.9 + startColor.g = 0.9 + startColor.b = 0.9 + emitter:setStartColor(startColor) + + local startColorVar = emitter:getStartColorVar() + startColorVar.b = 0.1 + emitter:setStartColorVar(startColorVar) + + emitter:setEmissionRate(emitter:getTotalParticles() / emitter:getLife()) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_snow)) + + setEmitterPosition() + + titleLabel:setString("ParticleSnow") + return layer +end + +--------------------------------- +-- DemoRain +--------------------------------- +local function DemoRain() + local layer = getBaseLayer() + + emitter = cc.ParticleRain:create() + -- emitter:retain() + background:addChild(emitter, 10) + local pos_x, pos_y = emitter:getPosition() + emitter:setPosition(pos_x, pos_y - 100) + emitter:setLife(4) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + + setEmitterPosition() + + titleLabel:setString("ParticleRain") + return layer +end + +--------------------------------- +-- DemoBigFlower +--------------------------------- +local function DemoBigFlower() + local layer = getBaseLayer() + + emitter = cc.ParticleSystemQuad:createWithTotalParticles(50) + + background:addChild(emitter, 10) + ----emitter:release() -- win32 : use this line or remove this line and use autorelease() + emitter:setTexture( cc.Director:getInstance():getTextureCache():addImage(s_stars1) ) + emitter:setDuration(-1) + + -- gravity + emitter:setGravity(cc.p(0, 0)) + + -- angle + emitter:setAngle(90) + emitter:setAngleVar(360) + + -- speed of particles + emitter:setSpeed(160) + emitter:setSpeedVar(20) + + -- radial + emitter:setRadialAccel(-120) + emitter:setRadialAccelVar(0) + + -- tagential + emitter:setTangentialAccel(30) + emitter:setTangentialAccelVar(0) + + -- emitter position + emitter:setPosition(160, 240) + emitter:setPosVar(cc.p(0, 0)) + + -- life of particles + emitter:setLife(4) + emitter:setLifeVar(1) + + -- spin of particles + emitter:setStartSpin(0) + emitter:setStartSizeVar(0) + emitter:setEndSpin(0) + emitter:setEndSpinVar(0) + + -- color of particles + emitter:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setStartColorVar(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + emitter:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + emitter:setStartSize(80.0) + emitter:setStartSizeVar(40.0) + emitter:setEndSize(cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE ) + + -- emits per second + emitter:setEmissionRate(emitter:getTotalParticles() / emitter:getLife()) + + -- additive + emitter:setBlendAdditive(true) + + setEmitterPosition() + + titleLabel:setString("ParticleBigFlower") + return layer +end + +--------------------------------- +-- DemoRotFlower +--------------------------------- +local function DemoRotFlower() + local layer = getBaseLayer() + + emitter = cc.ParticleSystemQuad:createWithTotalParticles(300) + + background:addChild(emitter, 10) + ----emitter:release() -- win32 : Remove this line + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_stars2)) + + -- duration + emitter:setDuration(-1) + + -- gravity + emitter:setGravity(cc.p(0, 0)) + + -- angle + emitter:setAngle(90) + emitter:setAngleVar(360) + + -- speed of particles + emitter:setSpeed(160) + emitter:setSpeedVar(20) + + -- radial + emitter:setRadialAccel(-120) + emitter:setRadialAccelVar(0) + + -- tagential + emitter:setTangentialAccel(30) + emitter:setTangentialAccelVar(0) + + -- emitter position + emitter:setPosition(160, 240) + emitter:setPosVar(cc.p(0, 0)) + + -- life of particles + emitter:setLife(3) + emitter:setLifeVar(1) + + -- spin of particles + emitter:setStartSpin(0) + emitter:setStartSpinVar(0) + emitter:setEndSpin(0) + emitter:setEndSpinVar(2000) + + -- color of particles + emitter:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setStartColorVar(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + emitter:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + emitter:setStartSize(30.0) + emitter:setStartSizeVar(0) + emitter:setEndSize(cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE ) + + -- emits per second + emitter:setEmissionRate(emitter:getTotalParticles() / emitter:getLife()) + + -- additive + emitter:setBlendAdditive(false) + + setEmitterPosition() + + titleLabel:setString("ParticleRotFlower") + return layer +end + +--------------------------------- +-- DemoModernArt +--------------------------------- +local function DemoModernArt() + local layer = getBaseLayer() + + emitter = cc.ParticleSystemQuad:createWithTotalParticles(1000) + + background:addChild(emitter, 10) + ----emitter:release() + + -- duration + emitter:setDuration(-1) + + -- gravity + emitter:setGravity(cc.p(0,0)) + + -- angle + emitter:setAngle(0) + emitter:setAngleVar(360) + + -- radial + emitter:setRadialAccel(70) + emitter:setRadialAccelVar(10) + + -- tagential + emitter:setTangentialAccel(80) + emitter:setTangentialAccelVar(0) + + -- speed of particles + emitter:setSpeed(50) + emitter:setSpeedVar(10) + + -- emitter position + emitter:setPosition(s.width / 2, s.height / 2) + emitter:setPosVar(cc.p(0, 0)) + + -- life of particles + emitter:setLife(2.0) + emitter:setLifeVar(0.3) + + -- emits per frame + emitter:setEmissionRate(emitter:getTotalParticles() / emitter:getLife()) + + -- color of particles + emitter:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setStartColorVar(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + emitter:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + emitter:setStartSize(1.0) + emitter:setStartSizeVar(1.0) + emitter:setEndSize(32.0) + emitter:setEndSizeVar(8.0) + + -- texture + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + + -- additive + emitter:setBlendAdditive(false) + + setEmitterPosition() + + titleLabel:setString("Varying size") + return layer +end + +--------------------------------- +-- DemoRing +--------------------------------- +local function DemoRing() + local layer = getBaseLayer() + + emitter = cc.ParticleFlower:create() + -- emitter:retain() + + background:addChild(emitter, 10) + + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_stars1)) + emitter:setLifeVar(0) + emitter:setLife(10) + emitter:setSpeed(100) + emitter:setSpeedVar(0) + emitter:setEmissionRate(10000) + + setEmitterPosition() + + titleLabel:setString("Ring Demo") + return layer +end + +--------------------------------- +-- ParallaxParticle +--------------------------------- +local function ParallaxParticle() + local layer = getBaseLayer() + + background:getParent():removeChild(background, true) + background = nil + + local p = cc.ParallaxNode:create() + layer:addChild(p, 5) + + local p1 = cc.Sprite:create(s_back3) + local p2 = cc.Sprite:create(s_back3) + + p:addChild(p1, 1, cc.p(0.5, 1), cc.p(0, 250)) + p:addChild(p2, 2, cc.p(1.5, 1), cc.p(0, 50)) + + emitter = cc.ParticleFlower:create() + -- emitter:retain() + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + + p1:addChild(emitter, 10) + emitter:setPosition(250, 200) + + local par = cc.ParticleSun:create() + p2:addChild(par, 10) + par:setTexture(cc.Director:getInstance():getTextureCache():addImage(s_fire)) + + local move = cc.MoveBy:create(4, cc.p(300,0)) + local move_back = move:reverse() + local seq = cc.Sequence:create(move, move_back) + p:runAction(cc.RepeatForever:create(seq)) + + titleLabel:setString("Parallax + Particles") + return layer +end + +--------------------------------- +-- DemoParticleFromFile +--------------------------------- +local function DemoParticleFromFile(name) + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + local filename = "Particles/" .. name .. ".plist" + emitter = cc.ParticleSystemQuad:create(filename) + layer:addChild(emitter, 10) + + setEmitterPosition() + + titleLabel:setString(name) + return layer +end + +--------------------------------- +-- RadiusMode1 +--------------------------------- +local function RadiusMode1() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + emitter = cc.ParticleSystemQuad:createWithTotalParticles(200) + layer:addChild(emitter, 10) + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage("Images/stars-grayscale.png")) + + -- duration + emitter:setDuration(cc.PARTICLE_DURATION_INFINITY) + + -- radius mode + emitter:setEmitterMode(cc.PARTICLE_MODE_RADIUS) + + -- radius mode: start and end radius in pixels + emitter:setStartRadius(0) + emitter:setStartRadiusVar(0) + emitter:setEndRadius(160) + emitter:setEndRadiusVar(0) + + -- radius mode: degrees per second + emitter:setRotatePerSecond(180) + emitter:setRotatePerSecondVar(0) + + + -- angle + emitter:setAngle(90) + emitter:setAngleVar(0) + + -- emitter position + emitter:setPosition(s.width / 2, s.height / 2) + emitter:setPosVar(cc.p(0, 0)) + + -- life of particles + emitter:setLife(5) + emitter:setLifeVar(0) + + -- spin of particles + emitter:setStartSpin(0) + emitter:setStartSpinVar(0) + emitter:setEndSpin(0) + emitter:setEndSpinVar(0) + + -- color of particles + emitter:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setStartColorVar(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + emitter:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + emitter:setStartSize(32) + emitter:setStartSizeVar(0) + emitter:setEndSize(cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE ) + + -- emits per second + emitter:setEmissionRate(emitter:getTotalParticles() / emitter:getLife()) + + -- additive + emitter:setBlendAdditive(false) + + titleLabel:setString("Radius Mode: Spiral") + return layer +end + +--------------------------------- +-- RadiusMode2 +--------------------------------- +local function RadiusMode2() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + emitter = cc.ParticleSystemQuad:createWithTotalParticles(200) + layer:addChild(emitter, 10) + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage("Images/stars-grayscale.png")) + + -- duration + emitter:setDuration(cc.PARTICLE_DURATION_INFINITY ) + + -- radius mode + emitter:setEmitterMode(cc.PARTICLE_MODE_RADIUS) + + -- radius mode: start and end radius in pixels + emitter:setStartRadius(100) + emitter:setStartRadiusVar(0) + emitter:setEndRadius(cc.PARTICLE_START_RADIUS_EQUAL_TO_END_RADIUS) + emitter:setEndRadiusVar(0) + + -- radius mode: degrees per second + emitter:setRotatePerSecond(45) + emitter:setRotatePerSecondVar(0) + + -- angle + emitter:setAngle(90) + emitter:setAngleVar(0) + + -- emitter position + emitter:setPosition(s.width / 2, s.height / 2) + emitter:setPosVar(cc.p(0, 0)) + + -- life of particles + emitter:setLife(4) + emitter:setLifeVar(0) + + -- spin of particles + emitter:setStartSpin(0) + emitter:setStartSpinVar(0) + emitter:setEndSpin(0) + emitter:setEndSpinVar(0) + + -- color of particles + emitter:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setStartColorVar(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + emitter:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + emitter:setStartSize(32) + emitter:setStartSizeVar(0) + emitter:setEndSize(cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE) + + -- emits per second + emitter:setEmissionRate(emitter:getTotalParticles() / emitter:getLife()) + + -- additive + emitter:setBlendAdditive(false) + + titleLabel:setString("Radius Mode: Semi Circle") + return layer +end + +--------------------------------- +-- Issue704 +--------------------------------- +local function Issue704() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + emitter = cc.ParticleSystemQuad:createWithTotalParticles(100) + layer:addChild(emitter, 10) + emitter:setTexture(cc.Director:getInstance():getTextureCache():addImage("Images/fire.png")) + + -- duration + emitter:setDuration(cc.PARTICLE_DURATION_INFINITY) + + -- radius mode + emitter:setEmitterMode(cc.PARTICLE_MODE_RADIUS) + + -- radius mode: start and end radius in pixels + emitter:setStartRadius(50) + emitter:setStartRadiusVar(0) + emitter:setEndRadius(cc.PARTICLE_START_RADIUS_EQUAL_TO_END_RADIUS ) + emitter:setEndRadiusVar(0) + + -- radius mode: degrees per second + emitter:setRotatePerSecond(0) + emitter:setRotatePerSecondVar(0) + + + -- angle + emitter:setAngle(90) + emitter:setAngleVar(0) + + -- emitter position + emitter:setPosition(s.width / 2, s.height / 2) + emitter:setPosVar(cc.p(0, 0)) + + -- life of particles + emitter:setLife(5) + emitter:setLifeVar(0) + + -- spin of particles + emitter:setStartSpin(0) + emitter:setStartSpinVar(0) + emitter:setEndSpin(0) + emitter:setEndSpinVar(0) + + -- color of particles + emitter:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setStartColorVar(cc.c4f(0.5, 0.5, 0.5, 1.0)) + emitter:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + emitter:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + emitter:setStartSize(16) + emitter:setStartSizeVar(0) + emitter:setEndSize(cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE) + + -- emits per second + emitter:setEmissionRate(emitter:getTotalParticles() / emitter:getLife()) + + -- additive + emitter:setBlendAdditive(false) + + local rot = cc.RotateBy:create(16, 360) + emitter:runAction(cc.RepeatForever:create(rot)) + + titleLabel:setString("Issue 704. Free + Rot") + subtitleLabel:setString("Emitted particles should not rotate") + return layer +end + +--------------------------------- +-- Issue870 +--------------------------------- +local Issue870_index = nil +local Issue870_entry = nil + +local function updateQuads(dt) + update(dt) + + Issue870_index = math.mod(Issue870_index + 1, 4) + local rect = cc.rect(Issue870_index * 32, 0, 32, 32) + emitter:setTextureWithRect(emitter:getTexture(), rect) +end + +local function Issue870_onEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + Issue870_entry = scheduler:scheduleScriptFunc(updateQuads, 2.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(Issue870_entry) + end +end + +local function Issue870() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + local system = cc.ParticleSystemQuad:create("Particles/SpinningPeas.plist") + system:setTextureWithRect(cc.Director:getInstance():getTextureCache():addImage("Images/particles.png"), cc.rect(0,0,32,32)) + layer:addChild(system, 10) + emitter = system + + Issue870_index = 0 + layer:registerScriptHandler(Issue870_onEnterOrExit) + + titleLabel:setString("Issue 870. SubRect") + subtitleLabel:setString("Every 2 seconds the particle should change") + return layer +end + +--------------------------------- +-- MultipleParticleSystems +--------------------------------- +local function MultipleParticleSystems() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + cc.Director:getInstance():getTextureCache():addImage("Images/particles.png") + + for i = 0, 4 do + local particleSystem = cc.ParticleSystemQuad:create("Particles/SpinningPeas.plist") + particleSystem:setPosition(i * 50, i * 50) + particleSystem:setPositionType(cc.POSITION_TYPE_GROUPED ) + layer:addChild(particleSystem) + end + + emitter = nil + + titleLabel:setString("Multiple particle systems") + subtitleLabel:setString("v1.1 test: FPS should be lower than next test") + return layer +end + +--------------------------------- +-- MultipleParticleSystemsBatched +--------------------------------- +local function MultipleParticleSystemsBatched() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + local batchNode = cc.ParticleBatchNode:createWithTexture(nil, 3000) + layer:addChild(batchNode, 1, 2) + + for i = 0, 4 do + local particleSystem = cc.ParticleSystemQuad:create("Particles/SpinningPeas.plist") + particleSystem:setPositionType(cc.POSITION_TYPE_GROUPED ) + particleSystem:setPosition(i * 50, i * 50) + batchNode:setTexture(particleSystem:getTexture()) + batchNode:addChild(particleSystem) + end + + emitter = nil + + titleLabel:setString("Multiple particle systems batched") + subtitleLabel:setString("v1.1 test: should perform better than previous test") + return layer +end + +--------------------------------- +-- AddAndDeleteParticleSystems +--------------------------------- +local AddAndDeleteParticleSystems_entry = nil +local AddAndDeleteParticleSystems_batchNode = nil + +local function removeSystem(dt) + update(dt) + + local ChildrenCount = table.getn(AddAndDeleteParticleSystems_batchNode:getChildren()) + if ChildrenCount > 0 then + cclog("remove random system") + local rand = math.mod(math.random(1, 999999), ChildrenCount - 1) + AddAndDeleteParticleSystems_batchNode:removeChild(AddAndDeleteParticleSystems_batchNode:getChildren()[(rand)], true) + + --add new + local particleSystem = cc.ParticleSystemQuad:create("Particles/Spiral.plist") + particleSystem:setPositionType(cc.POSITION_TYPE_GROUPED ) + particleSystem:setTotalParticles(200) + + particleSystem:setPosition(math.mod(math.random(1, 999999), 300) ,math.mod(math.random(1, 999999), 400)) + + cclog("add a new system") + local randZ = math.floor(math.random() * 100) + AddAndDeleteParticleSystems_batchNode:addChild(particleSystem, randZ, -1) + end +end + +local function AddAndDeleteParticleSystems_onEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + AddAndDeleteParticleSystems_entry = scheduler:scheduleScriptFunc(removeSystem, 2.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(AddAndDeleteParticleSystems_entry) + end +end + +local function AddAndDeleteParticleSystems() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + --adds the texture inside the plist to the texture cache + AddAndDeleteParticleSystems_batchNode = cc.ParticleBatchNode:createWithTexture(nil, 16000) + + layer:addChild(AddAndDeleteParticleSystems_batchNode, 1, 2) + + for i = 0, 5 do + local particleSystem = cc.ParticleSystemQuad:create("Particles/Spiral.plist") + AddAndDeleteParticleSystems_batchNode:setTexture(particleSystem:getTexture()) + + particleSystem:setPositionType(cc.POSITION_TYPE_GROUPED ) + particleSystem:setTotalParticles(200) + + particleSystem:setPosition(i * 15 + 100, i * 15 + 100) + + local randZ = math.floor(math.random() * 100) + AddAndDeleteParticleSystems_batchNode:addChild(particleSystem, randZ, -1) + end + + layer:registerScriptHandler(AddAndDeleteParticleSystems_onEnterOrExit) + emitter = nil + + titleLabel:setString("Add and remove Particle System") + subtitleLabel:setString("v1.1 test: every 2 sec 1 system disappear, 1 appears") + return layer +end + +--------------------------------- +-- ReorderParticleSystems * +-- problem +--------------------------------- +local ReorderParticleSystems_entry = nil +local ReorderParticleSystems_batchNode = nil + +local function reorderSystem(dt) + update(dt) + + local childArray = ReorderParticleSystems_batchNode:getChildren() + local random = math.random(1,table.getn(childArray)) + local child = childArray[random] + -- problem: there's no getLocalZOrder() for cc.Object + -- ReorderParticleSystems_batchNode:reorderChild(child, child:getLocalZOrder() - 1) + ReorderParticleSystems_batchNode:reorderChild(child, math.random(0, 99999)) +end + +local function ReorderParticleSystems_onEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + ReorderParticleSystems_entry = scheduler:scheduleScriptFunc(reorderSystem, 2.0, false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(ReorderParticleSystems_entry) + end +end + +local function ReorderParticleSystems() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background ,true) + background = nil + + ReorderParticleSystems_batchNode = cc.ParticleBatchNode:create("Images/stars-grayscale.png", 3000) + + layer:addChild(ReorderParticleSystems_batchNode, 1, 2) + + for i = 0, 2 do + local particleSystem = cc.ParticleSystemQuad:createWithTotalParticles(200) + particleSystem:setTexture(ReorderParticleSystems_batchNode:getTexture()) + + -- duration + particleSystem:setDuration(cc.PARTICLE_DURATION_INFINITY) + + -- radius mode + particleSystem:setEmitterMode(cc.PARTICLE_MODE_RADIUS) + + -- radius mode: 100 pixels from center + particleSystem:setStartRadius(100) + particleSystem:setStartRadiusVar(0) + particleSystem:setEndRadius(cc.PARTICLE_START_RADIUS_EQUAL_TO_END_RADIUS ) + particleSystem:setEndRadiusVar(0) -- not used when start == end + + -- radius mode: degrees per second + -- 45 * 4 seconds of life = 180 degrees + particleSystem:setRotatePerSecond(45) + particleSystem:setRotatePerSecondVar(0) + + + -- angle + particleSystem:setAngle(90) + particleSystem:setAngleVar(0) + + -- emitter position + particleSystem:setPosVar(cc.p(0, 0)) + + -- life of particles + particleSystem:setLife(4) + particleSystem:setLifeVar(0) + + -- spin of particles + particleSystem:setStartSpin(0) + particleSystem:setStartSpinVar(0) + particleSystem:setEndSpin(0) + particleSystem:setEndSpinVar(0) + + -- color of particles + local startColor = cc.c4f(0, 0, 0, 1) + if i == 0 then startColor.r = 1 + elseif i == 1 then startColor.g = 1 + elseif i == 2 then startColor.b = 1 + end + + particleSystem:setStartColor(startColor) + particleSystem:setStartColorVar(cc.c4f(0, 0, 0, 0)) + + local endColor = startColor + particleSystem:setEndColor(endColor) + particleSystem:setEndColorVar(cc.c4f(0, 0, 0, 0)) + + -- size, in pixels + particleSystem:setStartSize(32) + particleSystem:setStartSizeVar(0) + particleSystem:setEndSize(cc.PARTICLE_START_SIZE_EQUAL_TO_END_SIZE) + + -- emits per second + particleSystem:setEmissionRate(particleSystem:getTotalParticles() / particleSystem:getLife()) + + -- additive + particleSystem:setPosition(i * 10 + 120, 200) + + ReorderParticleSystems_batchNode:addChild(particleSystem) + particleSystem:setPositionType(cc.POSITION_TYPE_FREE ) + + --particleSystem:release() + end + + layer:registerScriptHandler(ReorderParticleSystems_onEnterOrExit) + emitter = nil + + titleLabel:setString("reorder systems") + subtitleLabel:setString("changes every 2 seconds") + return layer +end + +--------------------------------- +-- PremultipliedAlphaTest +--------------------------------- +local function PremultipliedAlphaTest() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 255)) + layer:removeChild(background, true) + background = nil + + emitter = cc.ParticleSystemQuad:create("Particles/BoilingFoam.plist") + + emitter:setBlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) + + --assert(emitter:getOpacityModifyRGB(), "Particle texture does not have premultiplied alpha, test is useless") + + emitter:setStartColor(cc.c4f(1, 1, 1, 1)) + emitter:setEndColor(cc.c4f(1, 1, 1, 0)) + emitter:setStartColorVar(cc.c4f(0, 0, 0, 0)) + emitter:setEndColorVar(cc.c4f(0, 0, 0, 0)) + + layer:addChild(emitter, 10) + + titleLabel:setString("premultiplied alpha") + subtitleLabel:setString("no black halo, particles should fade out") + return layer +end + +--------------------------------- +-- PremultipliedAlphaTest2 +--------------------------------- +local function PremultipliedAlphaTest2() + local layer = getBaseLayer() + + layer:setColor(cc.c3b(0, 0, 0)) + layer:removeChild(background, true) + background = nil + + emitter = cc.ParticleSystemQuad:create("Particles/TestPremultipliedAlpha.plist") + layer:addChild(emitter ,10) + + titleLabel:setString("premultiplied alpha 2") + subtitleLabel:setString("Arrows should be faded") + return layer +end + +--------------------------------- +-- Particle Test +--------------------------------- +function CreateParticleLayer() + if SceneIdx == 0 then return ParticleReorder() + elseif SceneIdx == 1 then return ParticleBatchHybrid() + elseif SceneIdx == 2 then return ParticleBatchMultipleEmitters() + elseif SceneIdx == 3 then return DemoFlower() + elseif SceneIdx == 4 then return DemoGalaxy() + elseif SceneIdx == 5 then return DemoFirework() + elseif SceneIdx == 6 then return DemoSpiral() + elseif SceneIdx == 7 then return DemoSun() + elseif SceneIdx == 8 then return DemoMeteor() + elseif SceneIdx == 9 then return DemoFire() + elseif SceneIdx == 10 then return DemoSmoke() + elseif SceneIdx == 11 then return DemoExplosion() + elseif SceneIdx == 12 then return DemoSnow() + elseif SceneIdx == 13 then return DemoRain() + elseif SceneIdx == 14 then return DemoBigFlower() + elseif SceneIdx == 15 then return DemoRotFlower() + elseif SceneIdx == 16 then return DemoModernArt() + elseif SceneIdx == 17 then return DemoRing() + elseif SceneIdx == 18 then return ParallaxParticle() + elseif SceneIdx == 19 then return DemoParticleFromFile("BoilingFoam") + elseif SceneIdx == 20 then return DemoParticleFromFile("BurstPipe") + elseif SceneIdx == 21 then return DemoParticleFromFile("Comet") + elseif SceneIdx == 22 then return DemoParticleFromFile("debian") + elseif SceneIdx == 23 then return DemoParticleFromFile("ExplodingRing") + elseif SceneIdx == 24 then return DemoParticleFromFile("LavaFlow") + elseif SceneIdx == 25 then return DemoParticleFromFile("SpinningPeas") + elseif SceneIdx == 26 then return DemoParticleFromFile("SpookyPeas") + elseif SceneIdx == 27 then return DemoParticleFromFile("Upsidedown") + elseif SceneIdx == 28 then return DemoParticleFromFile("Flower") + elseif SceneIdx == 29 then return DemoParticleFromFile("Spiral") + elseif SceneIdx == 30 then return DemoParticleFromFile("Galaxy") + elseif SceneIdx == 31 then return DemoParticleFromFile("Phoenix") + elseif SceneIdx == 32 then return DemoParticleFromFile("lines") + elseif SceneIdx == 33 then return RadiusMode1() + elseif SceneIdx == 34 then return RadiusMode2() + elseif SceneIdx == 35 then return Issue704() + elseif SceneIdx == 36 then return Issue870() + --elseif SceneIdx == 36 then return Issue1201() + -- v1.1 tests + elseif SceneIdx == 37 then return MultipleParticleSystems() + elseif SceneIdx == 38 then return MultipleParticleSystemsBatched() + elseif SceneIdx == 39 then return AddAndDeleteParticleSystems() + elseif SceneIdx == 40 then return ReorderParticleSystems() + elseif SceneIdx == 41 then return PremultipliedAlphaTest() + elseif SceneIdx == 42 then return PremultipliedAlphaTest2() + end +end + +function ParticleTest() + cclog("ParticleTest") + local scene = cc.Scene:create() + + SceneIdx = -1 + scene:addChild(nextAction()) + scene:addChild(CreateBackMenuItem()) + + return scene +end diff --git a/samples/lua-tests/src/PerformanceTest/PerformanceSpriteTest.lua b/samples/lua-tests/src/PerformanceTest/PerformanceSpriteTest.lua new file mode 100644 index 0000000000..a2707f5a51 --- /dev/null +++ b/samples/lua-tests/src/PerformanceTest/PerformanceSpriteTest.lua @@ -0,0 +1,550 @@ +local kMaxNodes = 50000 +local kBasicZOrder = 10 +local kNodesIncrease = 250 +local TEST_COUNT = 7 + +local s = cc.Director:getInstance():getWinSize() + +----------------------------------- +-- For test functions +----------------------------------- +local function performanceActions(sprite) + sprite:setPosition(cc.p(math.mod(math.random(1, 99999999), s.width), math.mod(math.random(1, 99999999), s.height))) + + local period = 0.5 + math.mod(math.random(1, 99999999), 1000) / 500.0 + local rot = cc.RotateBy:create(period, 360.0 * math.random()) + local rot = cc.RotateBy:create(period, 360.0 * math.random()) + local permanentRotation = cc.RepeatForever:create(cc.Sequence:createWithTwoActions(rot, rot:reverse())) + sprite:runAction(permanentRotation) + + local growDuration = 0.5 + math.mod(math.random(1, 99999999), 1000) / 500.0 + local grow = cc.ScaleBy:create(growDuration, 0.5, 0.5) + local permanentScaleLoop = cc.RepeatForever:create(cc.Sequence:createWithTwoActions(grow, grow:reverse())) + sprite:runAction(permanentScaleLoop) +end + +local function performanceActions20(sprite) + if math.random() < 0.2 then + sprite:setPosition(cc.p(math.mod(math.random(1, 99999999), s.width), math.mod(math.random(1, 99999999), s.height))) + else + sprite:setPosition(cc.p(-1000, -1000)) + end + + local period = 0.5 + math.mod(math.random(1, 99999999), 1000) / 500.0 + local rot = cc.RotateBy:create(period, 360.0 * math.random()) + local permanentRotation = cc.RepeatForever:create(cc.Sequence:createWithTwoActions(rot, rot:reverse())) + sprite:runAction(permanentRotation) + + local growDuration = 0.5 + math.mod(math.random(1, 99999999), 1000) / 500.0 + local grow = cc.ScaleBy:create(growDuration, 0.5, 0.5) + local permanentScaleLoop = cc.RepeatForever:create(cc.Sequence:createWithTwoActions(grow, grow:reverse())) + sprite:runAction(permanentScaleLoop) +end + +local function performanceRotationScale(sprite) + sprite:setPosition(cc.p(math.mod(math.random(1, 99999999), s.width), math.mod(math.random(1, 99999999), s.height))) + sprite:setRotation(math.random() * 360) + sprite:setScale(math.random() * 2) +end + +local function performancePosition(sprite) + sprite:setPosition(cc.p(math.mod(math.random(1, 99999999), s.width), math.mod(math.random(1, 99999999), s.height))) +end + +local function performanceout20(sprite) + if math.random() < 0.2 then + sprite:setPosition(cc.p(math.mod(math.random(1, 99999999), s.width), math.mod(math.random(1, 99999999), s.height))) + else + sprite:setPosition(cc.p(-1000, -1000)) + end +end + +local function performanceOut100(sprite) + sprite:setPosition(cc.p( -1000, -1000)) +end + +local function performanceScale(sprite) + sprite:setPosition(cc.p(math.mod(math.random(1, 99999999), s.width), math.mod(math.random(1, 99999999), s.height))) + sprite:setScale(math.random() * 100 / 50) +end + +----------------------------------- +-- Subtest +----------------------------------- +local subtestNumber = 1 +local batchNode = nil -- cc.SpriteBatchNode +local parent = nil -- cc.Node + +local function initWithSubTest(nSubTest, p) + subtestNumber = nSubTest + parent = p + batchNode = nil + + local mgr = cc.Director:getInstance():getTextureCache() + -- remove all texture + mgr:removeTexture(mgr:addImage("Images/grossinis_sister1.png")) + mgr:removeTexture(mgr:addImage("Images/grossini_dance_atlas.png")) + mgr:removeTexture(mgr:addImage("Images/spritesheet1.png")) + + if subtestNumber == 2 then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + batchNode = cc.SpriteBatchNode:create("Images/grossinis_sister1.png", 100) + p:addChild(batchNode, 0) + elseif subtestNumber == 3 then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + batchNode = cc.SpriteBatchNode:create("Images/grossinis_sister1.png", 100) + p:addChild(batchNode, 0) + elseif subtestNumber == 5 then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + batchNode = cc.SpriteBatchNode:create("Images/grossini_dance_atlas.png", 100) + p:addChild(batchNode, 0) + elseif subtestNumber == 6 then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + batchNode = cc.SpriteBatchNode:create("Images/grossini_dance_atlas.png", 100) + p:addChild(batchNode, 0) + elseif subtestNumber == 8 then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + batchNode = cc.SpriteBatchNode:create("Images/spritesheet1.png", 100) + p:addChild(batchNode, 0) + elseif subtestNumber == 9 then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + batchNode = cc.SpriteBatchNode:create("Images/spritesheet1.png", 100) + p:addChild(batchNode, 0) + end + + -- todo + if batchNode ~= nil then + batchNode:retain() + end + + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE_PIXELFORMAT_DEFAULT) +end + +local function createSpriteWithTag(tag) + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + + local sprite = nil + if subtestNumber == 1 then + sprite = cc.Sprite:create("Images/grossinis_sister1.png") + parent:addChild(sprite, -1, tag + 100) + elseif subtestNumber == 2 then + sprite = cc.Sprite:createWithTexture(batchNode:getTexture(), cc.rect(0, 0, 52, 139)) + batchNode:addChild(sprite, 0, tag + 100) + elseif subtestNumber == 3 then + sprite = cc.Sprite:createWithTexture(batchNode:getTexture(), cc.rect(0, 0, 52, 139)) + batchNode:addChild(sprite, 0, tag + 100) + elseif subtestNumber == 4 then + local idx = math.floor((math.random() * 1400 / 100)) + 1 + local num + if idx < 10 then + num = "0" .. idx + else + num = idx + end + local str = "Images/grossini_dance_" .. num .. ".png" + sprite = cc.Sprite:create(str) + parent:addChild(sprite, -1, tag + 100) + elseif subtestNumber == 5 then + local y, x + local r = math.floor(math.random() * 1400 / 100) + y = math.floor(r / 5) + x = math.mod(r, 5) + x = x * 85 + y = y * 121 + sprite = cc.Sprite:createWithTexture(batchNode:getTexture(), cc.rect(x, y, 85, 121)) + batchNode:addChild(sprite, 0, tag + 100) + elseif subtestNumber == 6 then + local y, x + local r = math.floor(math.random() * 1400 / 100) + y = math.floor(r / 5) + x = math.mod(r, 5) + x = x * 85 + y = y * 121 + sprite = cc.Sprite:createWithTexture(batchNode:getTexture(), cc.rect(x, y, 85, 121)) + batchNode:addChild(sprite, 0, tag + 100) + elseif subtestNumber == 7 then + local y, x + local r = math.floor(math.random() * 6400 / 100) + y = math.floor(r / 8) + x = math.mod(r, 8) + local str = "Images/sprites_test/sprite-"..x.."-"..y..".png" + sprite = cc.Sprite:create(str) + parent:addChild(sprite, -1, tag + 100) + elseif subtestNumber == 8 then + local y, x + local r = math.floor(math.random() * 6400 / 100) + y = math.floor(r / 8) + x = math.mod(r, 8) + x = x * 32 + y = y * 32 + sprite = cc.Sprite:createWithTexture(batchNode:getTexture(), cc.rect(x, y, 32, 32)) + batchNode:addChild(sprite, 0, tag + 100) + elseif subtestNumber == 9 then + local y, x + local r = math.floor(math.random() * 6400 / 100) + y = math.floor(r / 8) + x = math.mod(r, 8) + x = x * 32 + y = y * 32 + sprite = cc.Sprite:createWithTexture(batchNode:getTexture(), cc.rect(x, y, 32, 32)) + batchNode:addChild(sprite, 0, tag + 100) + end + + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE_PIXELFORMAT_DEFAULT) + + return sprite +end + +local function removeByTag(tag) + if subtestNumber == 1 then + parent:removeChildByTag(tag + 100, true) + elseif subtestNumber == 4 then + parent:removeChildByTag(tag + 100, true) + elseif subtestNumber == 7 then + parent:removeChildByTag(tag + 100, true) + else + batchNode:removeChildAtIndex(tag, true) + end +end + +----------------------------------- +-- PerformBasicLayer +----------------------------------- +local curCase = 0 +local maxCases = 7 + +local function showThisTest() + local scene = CreateSpriteTestScene() + cc.Director:getInstance():replaceScene(scene) +end + +local function backCallback(sender) + subtestNumber = 1 + curCase = curCase - 1 + if curCase < 0 then + curCase = curCase + maxCases + end + showThisTest() +end + +local function restartCallback(sender) + subtestNumber = 1 + showThisTest() +end + +local function nextCallback(sender) + subtestNumber = 1 + curCase = curCase + 1 + curCase = math.mod(curCase, maxCases) + showThisTest() +end + +local function toPerformanceMainLayer(sender) + cc.Director:getInstance():replaceScene(PerformanceTest()) +end + +local function initWithLayer(layer, controlMenuVisible) + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local mainItem = cc.MenuItemFont:create("Back") + mainItem:registerScriptTapHandler(toPerformanceMainLayer) + mainItem:setPosition(s.width - 50, 25) + local menu = cc.Menu:create() + menu:addChild(mainItem) + menu:setPosition(cc.p(0, 0)) + + if controlMenuVisible == true then + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + item1:registerScriptTapHandler(backCallback) + item2:registerScriptTapHandler(restartCallback) + item3:registerScriptTapHandler(nextCallback) + item1:setPosition(s.width / 2 - 100, 30) + item2:setPosition(s.width / 2, 30) + item3:setPosition(s.width / 2 + 100, 30) + + menu:addChild(item1, kItemTagBasic) + menu:addChild(item2, kItemTagBasic) + menu:addChild(item3, kItemTagBasic) + end + layer:addChild(menu) +end + +----------------------------------- +-- SpriteMainScene +----------------------------------- +local lastRenderedCount = nil +local quantityNodes = nil + +local infoLabel = nil +local titleLabel = nil + +local function testNCallback(tag) + subtestNumber = tag - kBasicZOrder + showThisTest() +end + +local function updateNodes() + if quantityNodes ~= lastRenderedCount then + local str = quantityNodes .. " nodes" + infoLabel:setString(str) + lastRenderedCount = quantityNodes + end +end + +local function onDecrease(sender) + if quantityNodes <= 0 then + return + end + + for i = 0, kNodesIncrease - 1 do + quantityNodes = quantityNodes - 1 + removeByTag(quantityNodes) + end + updateNodes() +end + +local function onIncrease(sender) + if quantityNodes >= kMaxNodes then + return + end + + for i = 0, kNodesIncrease - 1 do + local sprite = createSpriteWithTag(quantityNodes) + + if curCase == 0 then + doPerformSpriteTest1(sprite) + elseif curCase == 1 then + doPerformSpriteTest2(sprite) + elseif curCase == 2 then + doPerformSpriteTest3(sprite) + elseif curCase == 3 then + doPerformSpriteTest4(sprite) + elseif curCase == 4 then + doPerformSpriteTest5(sprite) + elseif curCase == 5 then + doPerformSpriteTest6(sprite) + elseif curCase == 6 then + doPerformSpriteTest7(sprite) + end + + quantityNodes = quantityNodes + 1 + end + + updateNodes() +end + +local function initWithMainTest(scene, asubtest, nNodes) + subtestNumber = asubtest + initWithSubTest(asubtest, scene) + + lastRenderedCount = 0 + quantityNodes = 0 + + cc.MenuItemFont:setFontSize(65) + local decrease = cc.MenuItemFont:create(" - ") + decrease:registerScriptTapHandler(onDecrease) + decrease:setColor(cc.c3b(0, 200, 20)) + local increase = cc.MenuItemFont:create(" + ") + increase:registerScriptTapHandler(onIncrease) + increase:setColor(cc.c3b(0, 200, 20)) + + local menu = cc.Menu:create() + menu:addChild(decrease) + menu:addChild(increase) + menu:alignItemsHorizontally() + menu:setPosition(s.width / 2, s.height - 65) + scene:addChild(menu, 1) + + infoLabel = cc.LabelTTF:create("0 nodes", "Marker Felt", 30) + infoLabel:setColor(cc.c3b(0, 200, 20)) + infoLabel:setPosition(s.width / 2, s.height - 90) + scene:addChild(infoLabel, 1) + + maxCases = TEST_COUNT + + -- Sub Tests + cc.MenuItemFont:setFontSize(32) + subMenu = cc.Menu:create() + for i = 1, 9 do + local str = i .. " " + local itemFont = cc.MenuItemFont:create(str) + itemFont:registerScriptTapHandler(testNCallback) + --itemFont:setTag(i) + subMenu:addChild(itemFont, kBasicZOrder + i, kBasicZOrder + i) + + if i <= 3 then + itemFont:setColor(cc.c3b(200, 20, 20)) + elseif i <= 6 then + itemFont:setColor(cc.c3b(0, 200, 20)) + else + itemFont:setColor(cc.c3b(0, 20, 200)) + end + end + + subMenu:alignItemsHorizontally() + subMenu:setPosition(cc.p(s.width / 2, 80)) + scene:addChild(subMenu, 1) + + -- add title label + titleLabel = cc.LabelTTF:create("No title", "Arial", 40) + scene:addChild(titleLabel, 1) + titleLabel:setPosition(s.width / 2, s.height - 32) + titleLabel:setColor(cc.c3b(255, 255, 40)) + + while quantityNodes < nNodes do + onIncrease() + end +end + +----------------------------------- +-- SpritePerformTest1 +----------------------------------- +function doPerformSpriteTest1(sprite) + performancePosition(sprite) +end + +local function SpriteTestLayer1() + local layer = cc.Layer:create() + initWithLayer(layer, true) + + local str = "A (" .. subtestNumber .. ") position" + titleLabel:setString(str) + + return layer +end + +----------------------------------- +-- SpritePerformTest2 +----------------------------------- +function doPerformSpriteTest2(sprite) + performanceScale(sprite) +end + +local function SpriteTestLayer2() + local layer = cc.Layer:create() + initWithLayer(layer, true) + + local str = "B (" .. subtestNumber .. ") scale" + titleLabel:setString(str) + + return layer +end + +----------------------------------- +-- SpritePerformTest3 +----------------------------------- +function doPerformSpriteTest3(sprite) + performanceRotationScale(sprite) +end + +local function SpriteTestLayer3() + local layer = cc.Layer:create() + initWithLayer(layer, true) + + local str = "C (" .. subtestNumber .. ") scale + rot" + titleLabel:setString(str) + + return layer +end + +----------------------------------- +-- SpritePerformTest4 +----------------------------------- +function doPerformSpriteTest4(sprite) + performanceOut100(sprite) +end + +local function SpriteTestLayer4() + local layer = cc.Layer:create() + initWithLayer(layer, true) + + local str = "D (" .. subtestNumber .. ") 100% out" + titleLabel:setString(str) + + return layer +end + +----------------------------------- +-- SpritePerformTest5 +----------------------------------- +function doPerformSpriteTest5(sprite) + performanceout20(sprite) +end + +local function SpriteTestLayer5() + local layer = cc.Layer:create() + initWithLayer(layer, true) + + local str = "E (" .. subtestNumber .. ") 80% out" + titleLabel:setString(str) + + return layer +end + +----------------------------------- +-- SpritePerformTest6 +----------------------------------- +function doPerformSpriteTest6(sprite) + performanceActions(sprite) +end + +local function SpriteTestLayer6() + local layer = cc.Layer:create() + initWithLayer(layer, true) + + local str = "F (" .. subtestNumber .. ") actions" + titleLabel:setString(str) + + return layer +end + +----------------------------------- +-- SpritePerformTest7 +----------------------------------- +function doPerformSpriteTest7(sprite) + performanceActions20(sprite) +end + +local function SpriteTestLayer7() + local layer = cc.Layer:create() + initWithLayer(layer, true) + + local str = "G (" .. subtestNumber .. ") actions 80% out" + titleLabel:setString(str) + + return layer +end + +----------------------------------- +-- PerformanceSpriteTest +----------------------------------- +function CreateSpriteTestScene() + local scene = cc.Scene:create() + initWithMainTest(scene, subtestNumber, kNodesIncrease) + + if curCase == 0 then + scene:addChild(SpriteTestLayer1()) + elseif curCase == 1 then + scene:addChild(SpriteTestLayer2()) + elseif curCase == 2 then + scene:addChild(SpriteTestLayer3()) + elseif curCase == 3 then + scene:addChild(SpriteTestLayer4()) + elseif curCase == 4 then + scene:addChild(SpriteTestLayer5()) + elseif curCase == 5 then + scene:addChild(SpriteTestLayer6()) + elseif curCase == 6 then + scene:addChild(SpriteTestLayer7()) + end + + return scene +end + +function PerformanceSpriteTest() + curCase = 0 + + return CreateSpriteTestScene() +end diff --git a/samples/lua-tests/src/PerformanceTest/PerformanceTest.lua b/samples/lua-tests/src/PerformanceTest/PerformanceTest.lua new file mode 100644 index 0000000000..531a72ab20 --- /dev/null +++ b/samples/lua-tests/src/PerformanceTest/PerformanceTest.lua @@ -0,0 +1,1928 @@ +require "src/PerformanceTest/PerformanceSpriteTest" + +local MAX_COUNT = 6 +local LINE_SPACE = 40 +local kItemTagBasic = 1000 + +local testsName = +{ + "PerformanceNodeChildrenTest", + "PerformanceParticleTest", + "PerformanceSpriteTest", + "PerformanceTextureTest", + "PerformanceTouchesTest", + "PerformanceFuncRelateWithTable", +} + +local s = cc.Director:getInstance():getWinSize() + +--Create toMainLayr MenuItem +function CreatePerfomBasicLayerMenu(pMenu) + if nil == pMenu then + return + end + local function toMainLayer() + local pScene = PerformanceTestMain() + if pScene ~= nil then + cc.Director:getInstance():replaceScene(pScene) + end + end + --Create BackMneu + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + local pMenuItemFont = cc.MenuItemFont:create("Back") + pMenuItemFont:setPosition(cc.p(VisibleRect:rightBottom().x - 50, VisibleRect:rightBottom().y + 25)) + pMenuItemFont:registerScriptTapHandler(toMainLayer) + pMenu:addChild(pMenuItemFont) +end + +---------------------------------- +--PerformanceNodeChildrenTest +---------------------------------- +local NodeChildrenTestParam = +{ + kTagInfoLayer = 1, + kTagMainLayer = 2, + kTagLabelAtlas = 3, + kTagBase = 20000, + TEST_COUNT = 4, + kMaxNodes = 15000, + kNodesIncrease = 500, +} + +local function runNodeChildrenTest() + --NodeChildrenMainScene + local nLastRenderedCount = 0 + local nQuantityOfNodes = 0 + local nCurrentQuantityOfNodes = 0 + --IterateSpriteSheet + local pBatchNode = nil + + --BasiceLayer param + local bControlMenuVisible = false + local nMaxCases = 0 + local nCurCase = 0 + + local pNewscene = cc.Scene:create() + + local function GetTitle() + if 0 == nCurCase then + return "B - Iterate SpriteSheet" + elseif 1 == nCurCase then + return "C - Add to spritesheet" + elseif 2 == nCurCase then + return "D - Del from spritesheet" + elseif 3 == nCurCase then + return "E - Reorder from spritesheet" + end + end + + local function GetSubTitle() + if 0 == nCurCase then + return "Iterate children using C Array API. See console" + elseif 1 == nCurCase then + return "Adds %10 of total sprites with random z. See console" + elseif 2 == nCurCase then + return "Remove %10 of total sprites placed randomly. See console" + elseif 3 == nCurCase then + return "Reorder %10 of total sprites placed randomly. See console" + end + end + + local function GetProfilerName() + if 0 == nCurCase then + return "iter c-array" + elseif 1 == nCurCase then + return "add sprites" + elseif 2 == nCurCase then + return "remove sprites" + elseif 3 == nCurCase then + return "reorder sprites" + end + end + + local function CreateBasicLayerMenuItem(pMenu,bMenuVisible,nMaxCasesNum,nCurCaseIndex) + if nil ~= pMenu then + bControlMenuVisible = bMenuVisible + nMaxCases = nMaxCasesNum + nCurCase = nCurCaseIndex + if true == bControlMenuVisible then + local function backCallback() + nCurCase = nCurCase - 1 + if nCurCase < 0 then + nCurCase = nCurCase + nMaxCases + end + ShowCurrentTest() + end + + local function restartCallback() + ShowCurrentTest() + end + + local function nextCallback() + nCurCase = nCurCase + 1 + --No check nMaxCases + nCurCase = nCurCase % nMaxCases + ShowCurrentTest() + end + + local size = cc.Director:getInstance():getWinSize() + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + item1:registerScriptTapHandler(backCallback) + pMenu:addChild(item1,kItemTagBasic) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + item2:registerScriptTapHandler(restartCallback) + pMenu:addChild(item2,kItemTagBasic) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + pMenu:addChild(item3,kItemTagBasic) + item3:registerScriptTapHandler(nextCallback) + + local size = cc.Director:getInstance():getWinSize() + item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + end + end + end + + local function updateQuantityLabel() + if nQuantityOfNodes ~= nLastRenderedCount then + -- local pInfoLabel = pNewscene:getChildByTag(NodeChildrenTestParam.kTagInfoLayer) + local pInfoLabel = tolua.cast(pNewscene:getChildByTag(NodeChildrenTestParam.kTagInfoLayer), "cc.LabelTTF") + local strNode = nQuantityOfNodes.." nodes" + pInfoLabel:setString(strNode) + nLastRenderedCount = nQuantityOfNodes + end + end + + local function IterateSpriteSheetCArrayUpdate(t) + if nil == pBatchNode then + return + end + local pChildren = pBatchNode:getChildren() + local pObject = nil + if nil == pChildren then + return + end + local i = 0 + local len = table.getn(pChildren) + for i = 0, len - 1, 1 do + local child = tolua.cast(pChildren[i + 1], "cc.Sprite") + child:setVisible(false) + end + end + + local function AddSpriteSheetUpdate(t) + if nil == pBatchNode then + return + end + + --15 percent + local nTotalToAdd = nCurrentQuantityOfNodes * 0.15 + local zs = {} + if nTotalToAdd > 0 then + local pSprites = {} + local i = 0 + for i = 0 , nTotalToAdd - 1 do + local pSprite = cc.Sprite:createWithTexture(pBatchNode:getTexture(), cc.rect(0,0,32,32)) + pSprites[i + 1] = pSprite + zs[i] = math.random(-1,1) * 50 + end + + for i = 0 , nTotalToAdd - 1 do + local pChild = tolua.cast(pSprites[i + 1],"cc.Node") + pBatchNode:addChild(pChild, zs[i], NodeChildrenTestParam.kTagBase + i) + end + + pBatchNode:sortAllChildren() + + for i = 0 , nTotalToAdd - 1 do + pBatchNode:removeChildByTag( NodeChildrenTestParam.kTagBase + i, true) + end + end + end + + local function RemoveSpriteSheetUpdate(t) + if nil == pBatchNode then + return + end + local nTotalToAdd = nCurrentQuantityOfNodes * 0.15 + if nTotalToAdd > 0 then + local pSprites = {} + -- Don't include the sprite creation time as part of the profiling + local i = 0 + for i = 0, nTotalToAdd - 1 do + local pSprite = cc.Sprite:createWithTexture(pBatchNode:getTexture(), cc.rect(0,0,32,32)) + pSprites[i + 1] = pSprite + end + -- add them with random Z (very important!) + for i=0, nTotalToAdd - 1 do + local pChild = tolua.cast(pSprites[i + 1],"cc.Node") + pBatchNode:addChild(pChild, math.random(-1,1) * 50, NodeChildrenTestParam.kTagBase + i) + end + + for i = 0, nTotalToAdd - 1 do + pBatchNode:removeChildByTag( NodeChildrenTestParam.kTagBase + i, true) + end + end + end + + local function ReorderSpriteSheetUpdate(t) + if nil == pBatchNode then + return + end + -- 15 percent + local nTotalToAdd = nCurrentQuantityOfNodes * 0.15 + + if nTotalToAdd > 0 then + local pSprites = {} + -- Don't include the sprite creation time as part of the profiling + local i = 0 + for i = 0,nTotalToAdd - 1 do + local pSprite = cc.Sprite:createWithTexture(pBatchNode:getTexture(), cc.rect(0,0,32,32)) + pSprites[i + 1] = pSprite + end + + --dd them with random Z (very important!) + for i = 0, nTotalToAdd - 1 do + local pChild = tolua.cast(pSprites[i + 1] ,"cc.Node") + pBatchNode:addChild(pChild, math.random(-1,1) * 50, NodeChildrenTestParam.kTagBase + i) + end + + pBatchNode:sortAllChildren() + + -- reorder them + for i = 0, nTotalToAdd - 1 do + local pNode = tolua.cast(pSprites[i + 1],"cc.Node") + pBatchNode:reorderChild(pNode, math.random(-1,1) * 50) + end + pBatchNode:sortAllChildren() + --remove them + for i = 0, nTotalToAdd - 1 do + pBatchNode:removeChildByTag( NodeChildrenTestParam.kTagBase+i, true) + end + end + end + + local function NodeChildrenScheduleUpdate() + if 0 == nCurCase then + pNewscene:scheduleUpdateWithPriorityLua(IterateSpriteSheetCArrayUpdate,0) + elseif 1 == nCurCase then + pNewscene:scheduleUpdateWithPriorityLua(AddSpriteSheetUpdate,0) + elseif 2 == nCurCase then + pNewscene:scheduleUpdateWithPriorityLua(RemoveSpriteSheetUpdate,0) + elseif 3 == nCurCase then + pNewscene:scheduleUpdateWithPriorityLua(ReorderSpriteSheetUpdate,0) + end + end + + local function updateQuantityOfNodes() + local s = cc.Director:getInstance():getWinSize() + --increase nodes + if( nCurrentQuantityOfNodes < nQuantityOfNodes ) then + local i = 0 + for i = 0,nQuantityOfNodes - nCurrentQuantityOfNodes - 1 do + local sprite = cc.Sprite:createWithTexture(pBatchNode:getTexture(), cc.rect(0, 0, 32, 32)) + pBatchNode:addChild(sprite) + sprite:setPosition(cc.p( math.random() * s.width, math.random() * s.height)) + if 0 ~= nCurCase then + sprite:setVisible(false) + end + end + elseif (nCurrentQuantityOfNodes > nQuantityOfNodes ) then + i = 0 + for i = 0, nCurrentQuantityOfNodes - nQuantityOfNodes - 1 do + local index = nCurrentQuantityOfNodes - i - 1 + pBatchNode:removeChildAtIndex(index, true) + end + end + + + nCurrentQuantityOfNodes = nQuantityOfNodes + end + + local function onDecrease() + nQuantityOfNodes = nQuantityOfNodes - NodeChildrenTestParam.kNodesIncrease + if nQuantityOfNodes < 0 then + nQuantityOfNodes = 0 + end + + updateQuantityLabel() + updateQuantityOfNodes() + end + + local function onIncrease() + nQuantityOfNodes = nQuantityOfNodes + NodeChildrenTestParam.kNodesIncrease + if nQuantityOfNodes > NodeChildrenTestParam.kMaxNodes then + nQuantityOfNodes = NodeChildrenTestParam.kMaxNodes + end + + updateQuantityLabel() + updateQuantityOfNodes() + end + + + local function SpecialInitWithQuantityOfNodes() + -- if 0 == nCurCase then + pBatchNode = cc.SpriteBatchNode:create("Images/spritesheet1.png") + pNewscene:addChild(pBatchNode) + --[[ + else + pBatchNode = cc.SpriteBatchNode:create("Images/spritesheet1.png") + pNewscene:addChild(pBatchNode) + end + ]]-- + end + + local function MainSceneInitWithQuantityOfNodes(nNodes) + local s = cc.Director:getInstance():getWinSize() + + --Title + local pLabel = cc.LabelTTF:create(GetTitle(), "Arial", 40) + pNewscene:addChild(pLabel, 1) + pLabel:setPosition(cc.p(s.width/2, s.height-32)) + pLabel:setColor(cc.c3b(255,255,40)) + + if (nil ~= GetSubTitle()) and ("" ~= GetSubTitle()) then + local pSubLabel = cc.LabelTTF:create(GetSubTitle(), "Thonburi", 16) + pNewscene:addChild(pSubLabel, 1) + pSubLabel:setPosition(cc.p(s.width/2, s.height-80)) + end + + nLastRenderedCount = 0 + nCurrentQuantityOfNodes = 0 + nQuantityOfNodes = nNodes + + --"+","-" Menu + cc.MenuItemFont:setFontSize(65) + local pDecrease = cc.MenuItemFont:create(" - ") + pDecrease:registerScriptTapHandler(onDecrease) + pDecrease:setColor(cc.c3b(0,200,20)) + local pIncrease = cc.MenuItemFont:create(" + ") + pIncrease:registerScriptTapHandler(onIncrease) + pIncrease:setColor(cc.c3b(0,200,20)) + + local pMenuAddOrSub = cc.Menu:create() + pMenuAddOrSub:addChild(pDecrease) + pMenuAddOrSub:addChild(pIncrease) + pMenuAddOrSub:alignItemsHorizontally() + pMenuAddOrSub:setPosition(cc.p(s.width/2, s.height/2+15)) + pNewscene:addChild(pMenuAddOrSub,1) + + --InfoLayer + local pInfoLabel = cc.LabelTTF:create("0 nodes", "Marker Felt", 30) + pInfoLabel:setColor(cc.c3b(0,200,20)) + pInfoLabel:setPosition(cc.p(s.width/2, s.height/2-15)) + pNewscene:addChild(pInfoLabel, 1, NodeChildrenTestParam.kTagInfoLayer) + + --NodeChildrenMenuLayer + local pNodeChildrenMenuLayer = cc.Layer:create() + local pNodeChildrenMenuMenu = cc.Menu:create() + CreatePerfomBasicLayerMenu(pNodeChildrenMenuMenu) + CreateBasicLayerMenuItem(pNodeChildrenMenuMenu,true,NodeChildrenTestParam.TEST_COUNT,nCurCase) + pNodeChildrenMenuMenu:setPosition(cc.p(0, 0)) + pNodeChildrenMenuLayer:addChild(pNodeChildrenMenuMenu) + pNewscene:addChild(pNodeChildrenMenuLayer) + + updateQuantityLabel() + updateQuantityOfNodes() + + end + + function ShowCurrentTest() + if nil ~= pNewscene then + pNewscene:unscheduleUpdate() + end + + pNewscene = cc.Scene:create() + if nil ~= pNewscene then + SpecialInitWithQuantityOfNodes() + MainSceneInitWithQuantityOfNodes(nQuantityOfNodes) +-- pNewscene:registerScriptHandler(onNodeEvent) + NodeChildrenScheduleUpdate() + cc.Director:getInstance():replaceScene(pNewscene) + end + end + + SpecialInitWithQuantityOfNodes() + MainSceneInitWithQuantityOfNodes(NodeChildrenTestParam.kNodesIncrease) +-- pNewscene:registerScriptHandler(onNodeEvent) + NodeChildrenScheduleUpdate() + + return pNewscene +end +---------------------------------- +--PerformanceParticleTest +---------------------------------- +local ParticleTestParam = +{ + kTagInfoLayer = 1, + kTagMainLayer = 2, + kTagParticleSystem = 3, + kTagLabelAtlas = 4, + kTagMenuLayer = 1000, + + TEST_COUNT = 4, + kMaxParticles = 14000, + kNodesIncrease = 500, + + kSubMenuBasicZOrder = 10 +} + +local function runParticleTest() + --PerformanceParticle param + local nLastRenderedCount = 0 + local nQuantityParticles = 0 + local nSubtestNumber = 0 + --BasiceLayer param + local bControlMenuVisible = false + local nMaxCases = 0 + local nCurCase = 0 + + local ScheduleSelector = nil + + local pNewScene = cc.Scene:create() + + local function GetTitle() + local strTitle = nil + if 0 == nCurCase then + strTitle = string.format("A (%d) size=4",nSubtestNumber) + elseif 1 == nCurCase then + strTitle = string.format("B (%d) size=8",nSubtestNumber) + elseif 2 == nCurCase then + strTitle = string.format("C (%d) size=32",nSubtestNumber) + elseif 3 == nCurCase then + strTitle = string.format("D (%d) size=64",nSubtestNumber) + end + return strTitle + end + + local function CreateBasicLayerMenuItem(pMenu,bMenuVisible,nMaxCasesNum,nCurCaseIndex) + if nil ~= pMenu then + bControlMenuVisible = bMenuVisible + nMaxCases = nMaxCasesNum + nCurCase = nCurCaseIndex + if true == bControlMenuVisible then + local function backCallback() + nCurCase = nCurCase - 1 + if nCurCase < 0 then + nCurCase = nCurCase + nMaxCases + end + ShowCurrentTest() + end + + local function restartCallback() + ShowCurrentTest() + end + + local function nextCallback() + nCurCase = nCurCase + 1 + --No check nMaxCases + nCurCase = nCurCase % nMaxCases + ShowCurrentTest() + end + + local size = cc.Director:getInstance():getWinSize() + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + item1:registerScriptTapHandler(backCallback) + pMenu:addChild(item1,kItemTagBasic) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + item2:registerScriptTapHandler(restartCallback) + pMenu:addChild(item2,kItemTagBasic) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + pMenu:addChild(item3,kItemTagBasic) + item3:registerScriptTapHandler(nextCallback) + + local size = cc.Director:getInstance():getWinSize() + item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + end + end + end + + local function TestNCallback(tag,pMenuItem) + local nIndex = pMenuItem:getLocalZOrder() - ParticleTestParam.kSubMenuBasicZOrder + nSubtestNumber = nIndex + ShowCurrentTest() + end + + local function UpdateQuantityLabel() + if nQuantityParticles ~= nLastRenderedCount then + local pInfoLabel = tolua.cast(pNewScene:getChildByTag(ParticleTestParam.kTagInfoLayer), "cc.LabelTTF") + local strInfo = string.format("%u particles", nQuantityParticles) + pInfoLabel:setString(strInfo) + + nLastRenderedCount = nQuantityParticles + end + end + + local function doTest() + local s = cc.Director:getInstance():getWinSize() + local pParticleSystem = tolua.cast(pNewScene:getChildByTag(ParticleTestParam.kTagParticleSystem),"cc.ParticleSystem") + if nil == pParticleSystem then + return + end + if 0 == nCurCase then + --duration + pParticleSystem:setDuration(-1) + + --gravity + pParticleSystem:setGravity(cc.p(0,-90)) + + --angle + pParticleSystem:setAngle(90) + pParticleSystem:setAngleVar(0) + + --radial + pParticleSystem:setRadialAccel(0) + pParticleSystem:setRadialAccelVar(0) + + -- speed of particles + pParticleSystem:setSpeed(180) + pParticleSystem:setSpeedVar(50) + + -- emitter position + pParticleSystem:setPosition(cc.p(s.width/2, 100)) + pParticleSystem:setPosVar(cc.p(s.width/2,0)) + + -- life of particles + pParticleSystem:setLife(2.0) + pParticleSystem:setLifeVar(1) + + --emits per frame + pParticleSystem:setEmissionRate(pParticleSystem:getTotalParticles() / pParticleSystem:getLife()) + + --color of particles + pParticleSystem:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + + pParticleSystem:setStartColorVar( cc.c4f(0.5, 0.5, 0.5, 1.0)) + + pParticleSystem:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + pParticleSystem:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + pParticleSystem:setEndSize(4.0) + pParticleSystem:setStartSize(4.0) + pParticleSystem:setEndSizeVar(0) + pParticleSystem:setStartSizeVar(0) + + -- additive + pParticleSystem:setBlendAdditive(false) + + elseif 1 == nCurCase then + --duration + pParticleSystem:setDuration(-1) + + --gravity + pParticleSystem:setGravity(cc.p(0,-90)) + + --angle + pParticleSystem:setAngle(90) + pParticleSystem:setAngleVar(0) + + --radial + pParticleSystem:setRadialAccel(0) + pParticleSystem:setRadialAccelVar(0) + + -- speed of particles + pParticleSystem:setSpeed(180) + pParticleSystem:setSpeedVar(50) + + -- emitter position + pParticleSystem:setPosition(cc.p(s.width/2, 100)) + pParticleSystem:setPosVar(cc.p(s.width/2,0)) + + -- life of particles + pParticleSystem:setLife(2.0) + pParticleSystem:setLifeVar(1) + + --emits per frame + pParticleSystem:setEmissionRate(pParticleSystem:getTotalParticles() / pParticleSystem:getLife()) + + --color of particles + pParticleSystem:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + + pParticleSystem:setStartColorVar( cc.c4f(0.5, 0.5, 0.5, 1.0)) + + pParticleSystem:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + pParticleSystem:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + pParticleSystem:setEndSize(8.0) + pParticleSystem:setStartSize(8.0) + pParticleSystem:setEndSizeVar(0) + pParticleSystem:setStartSizeVar(0) + + -- additive + pParticleSystem:setBlendAdditive(false) + elseif 2 == nCurCase then + --duration + pParticleSystem:setDuration(-1) + + --gravity + pParticleSystem:setGravity(cc.p(0,-90)) + + --angle + pParticleSystem:setAngle(90) + pParticleSystem:setAngleVar(0) + + --radial + pParticleSystem:setRadialAccel(0) + pParticleSystem:setRadialAccelVar(0) + + -- speed of particles + pParticleSystem:setSpeed(180) + pParticleSystem:setSpeedVar(50) + + -- emitter position + pParticleSystem:setPosition(cc.p(s.width/2, 100)) + pParticleSystem:setPosVar(cc.p(s.width/2,0)) + + -- life of particles + pParticleSystem:setLife(2.0) + pParticleSystem:setLifeVar(1) + + --emits per frame + pParticleSystem:setEmissionRate(pParticleSystem:getTotalParticles() / pParticleSystem:getLife()) + + --color of particles + pParticleSystem:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + + pParticleSystem:setStartColorVar( cc.c4f(0.5, 0.5, 0.5, 1.0)) + + pParticleSystem:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + pParticleSystem:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + pParticleSystem:setEndSize(32.0) + pParticleSystem:setStartSize(32.0) + pParticleSystem:setEndSizeVar(0) + pParticleSystem:setStartSizeVar(0) + + -- additive + pParticleSystem:setBlendAdditive(false) + elseif 3 == nCurCase then + --duration + pParticleSystem:setDuration(-1) + + --gravity + pParticleSystem:setGravity(cc.p(0,-90)) + + --angle + pParticleSystem:setAngle(90) + pParticleSystem:setAngleVar(0) + + --radial + pParticleSystem:setRadialAccel(0) + pParticleSystem:setRadialAccelVar(0) + + -- speed of particles + pParticleSystem:setSpeed(180) + pParticleSystem:setSpeedVar(50) + + -- emitter position + pParticleSystem:setPosition(cc.p(s.width/2, 100)) + pParticleSystem:setPosVar(cc.p(s.width/2,0)) + + -- life of particles + pParticleSystem:setLife(2.0) + pParticleSystem:setLifeVar(1) + + --emits per frame + pParticleSystem:setEmissionRate(pParticleSystem:getTotalParticles() / pParticleSystem:getLife()) + + --color of particles + pParticleSystem:setStartColor(cc.c4f(0.5, 0.5, 0.5, 1.0)) + + pParticleSystem:setStartColorVar( cc.c4f(0.5, 0.5, 0.5, 1.0)) + + pParticleSystem:setEndColor(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + pParticleSystem:setEndColorVar(cc.c4f(0.1, 0.1, 0.1, 0.2)) + + -- size, in pixels + pParticleSystem:setEndSize(64.0) + pParticleSystem:setStartSize(64.0) + pParticleSystem:setEndSizeVar(0) + pParticleSystem:setStartSizeVar(0) + + -- additive + pParticleSystem:setBlendAdditive(false) + end + end + + local function CreateParticleSystem() + local pParticleSystem = nil + --[[ + * Tests: + * 1: Point Particle System using 32-bit textures (PNG) + * 2: Point Particle System using 16-bit textures (PNG) + * 3: Point Particle System using 8-bit textures (PNG) + * 4: Point Particle System using 4-bit textures (PVRTC) + + * 5: Quad Particle System using 32-bit textures (PNG) + * 6: Quad Particle System using 16-bit textures (PNG) + * 7: Quad Particle System using 8-bit textures (PNG) + * 8: Quad Particle System using 4-bit textures (PVRTC) + ]]-- + pNewScene:removeChildByTag(ParticleTestParam.kTagParticleSystem, true) + + --remove the "fire.png" from the TextureCache cache. + local pTexture = cc.Director:getInstance():getTextureCache():addImage("Images/fire.png") + cc.Director:getInstance():getTextureCache():removeTexture(pTexture) + local pParticleSystem = cc.ParticleSystemQuad:createWithTotalParticles(nQuantityParticles) + if 1 == nSubtestNumber then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + elseif 2 == nSubtestNumber then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + elseif 3 == nSubtestNumber then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_A8) + elseif 4 == nSubtestNumber then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + elseif 5 == nSubtestNumber then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + elseif 6 == nSubtestNumber then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_A8) + else + pParticleSystem = nil + print("Shall not happen!") + end + + if nil ~= pParticleSystem then + pParticleSystem:setTexture(cc.Director:getInstance():getTextureCache():addImage("Images/fire.png")) + end + + pNewScene:addChild(pParticleSystem, 0, ParticleTestParam.kTagParticleSystem) + + doTest() + --restore the default pixel format + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + end + + local function step(t) + local pAtlas = tolua.cast(pNewScene:getChildByTag(ParticleTestParam.kTagLabelAtlas),"cc.LabelAtlas") + local pEmitter = tolua.cast(pNewScene:getChildByTag(ParticleTestParam.kTagParticleSystem),"cc.ParticleSystem") + local strInfo = string.format("%4d",pEmitter:getParticleCount()) + pAtlas:setString(strInfo) + end + + local function ScheduleFuncion() + local function OnEnterOrExit(tag) + local scheduler = cc.Director:getInstance():getScheduler() + if tag == "enter" then + ScheduleSelector = scheduler:scheduleScriptFunc(step,0,false) + elseif tag == "exit" then + scheduler:unscheduleScriptEntry(ScheduleSelector) + end + end + pNewScene:registerScriptHandler(OnEnterOrExit) + end + + local function onDecrease() + nQuantityParticles = nQuantityParticles - ParticleTestParam.kNodesIncrease + if nQuantityParticles < 0 then + nQuantityParticles = 0 + end + + UpdateQuantityLabel() + CreateParticleSystem() + end + + local function onIncrease() + nQuantityParticles = nQuantityParticles + ParticleTestParam.kNodesIncrease + if nQuantityParticles > ParticleTestParam.kMaxParticles then + nQuantityParticles = ParticleTestParam.kMaxParticles + end + + UpdateQuantityLabel() + CreateParticleSystem() + end + + local function InitWithSubTest(nSubtest,nParticles) + nSubtestNumber = nSubtest + local s = cc.Director:getInstance():getWinSize() + + nLastRenderedCount = 0 + nQuantityParticles = nParticles + + --"+","-" Menu + cc.MenuItemFont:setFontSize(65) + local pDecrease = cc.MenuItemFont:create(" - ") + pDecrease:registerScriptTapHandler(onDecrease) + pDecrease:setColor(cc.c3b(0,200,20)) + local pIncrease = cc.MenuItemFont:create(" + ") + pIncrease:registerScriptTapHandler(onIncrease) + pIncrease:setColor(cc.c3b(0,200,20)) + + local pMenuAddOrSub = cc.Menu:create() + pMenuAddOrSub:addChild(pDecrease) + pMenuAddOrSub:addChild(pIncrease) + pMenuAddOrSub:alignItemsHorizontally() + pMenuAddOrSub:setPosition(cc.p(s.width/2, s.height/2+15)) + pNewScene:addChild(pMenuAddOrSub,1) + + local pInfoLabel = cc.LabelTTF:create("0 nodes", "Marker Felt", 30) + pInfoLabel:setColor(cc.c3b(0,200,20)) + pInfoLabel:setPosition(cc.p(s.width/2, s.height - 90)) + pNewScene:addChild(pInfoLabel, 1, ParticleTestParam.kTagInfoLayer) + + --particles on stage + local pLabelAtlas = cc.LabelAtlas:_create("0000", "fps_images.png", 12, 32, string.byte('.')) + pNewScene:addChild(pLabelAtlas, 0, ParticleTestParam.kTagLabelAtlas) + pLabelAtlas:setPosition(cc.p(s.width-66,50)) + + --ParticleTestMenuLayer + local pParticleMenuLayer = cc.Layer:create() + local pParticleMenu = cc.Menu:create() + CreatePerfomBasicLayerMenu(pParticleMenu) + CreateBasicLayerMenuItem(pParticleMenu,true,ParticleTestParam.TEST_COUNT,nCurCase) + pParticleMenu:setPosition(cc.p(0, 0)) + pParticleMenuLayer:addChild(pParticleMenu) + pNewScene:addChild(pParticleMenuLayer) + + --Sub Tests + cc.MenuItemFont:setFontSize(40) + local pSubMenu = cc.Menu:create() + local i = 1 + for i = 1, 6 do + local strNum = string.format("%d ",i) + local pItemFont = cc.MenuItemFont:create(strNum) + pItemFont:registerScriptTapHandler(TestNCallback) + pSubMenu:addChild(pItemFont, i + ParticleTestParam.kSubMenuBasicZOrder) + if i <= 3 then + pItemFont:setColor(cc.c3b(200,20,20)) + else + pItemFont:setColor(cc.c3b(0,200,20)) + end + end + pSubMenu:alignItemsHorizontally() + pSubMenu:setPosition(cc.p(s.width/2, 80)) + pNewScene:addChild(pSubMenu, 2) + + local pLabel = cc.LabelTTF:create(GetTitle(), "Arial", 40) + pNewScene:addChild(pLabel, 1) + pLabel:setPosition(cc.p(s.width/2, s.height-32)) + pLabel:setColor(cc.c3b(255,255,40)) + + UpdateQuantityLabel() + CreateParticleSystem() + ScheduleFuncion() + end + + function ShowCurrentTest() + if nil ~= pNewScene then + cc.Director:getInstance():getScheduler():unscheduleScriptEntry(ScheduleSelector) + end + pNewScene = cc.Scene:create() + InitWithSubTest(nSubtestNumber,nQuantityParticles) + cc.Director:getInstance():replaceScene(pNewScene) + end + + + InitWithSubTest(1,ParticleTestParam.kNodesIncrease) + return pNewScene +end + +---------------------------------- +--PerformanceSpriteTest +---------------------------------- +local SpriteTestParam = +{ + kMaxNodes = 50000, + kNodesIncrease = 250, + TEST_COUNT = 7, + + kTagInfoLayer = 1, + kTagMainLayer = 2, + -- 50000 -- kMaxNodes + kTagMenuLayer = 50000 + 1000, + kInitNodes = 50, + kSubMenuBasicZOrder = 20, + + kRandMax = 32767 +} + +local function runSpriteTest() + --PerformanceParticle param + local nLastRenderedCount = 0 + local nQuantityNodes = 0 + local nSubtestNumber = 0 + --BasiceLayer param + local bControlMenuVisible = false + local nMaxCases = 0 + local nCurCase = 0 + + local ScheduleSelector = nil + + --SubTest + local pBatchNode = nil + + local function GetTitle() + local strTitle = nil + if 0 == nCurCase then + strTitle = string.format("A (%d) position",nSubtestNumber) + elseif 1 == nCurCase then + strTitle = string.format("B (%d) scale",nSubtestNumber) + elseif 2 == nCurCase then + strTitle = string.format("C (%d) scale + rot",nSubtestNumber) + elseif 3 == nCurCase then + strTitle = string.format("D (%d) 100%% out",nSubtestNumber) + elseif 4 == nCurCase then + strTitle = string.format("E (%d) 80%% out",nSubtestNumber) + elseif 5 == nCurCase then + strTitle = string.format("F (%d) actions",nSubtestNumber) + elseif 6 == nCurCase then + strTitle = string.format("G (%d) actions 80%% out",nSubtestNumber) + end + + return strTitle + end + + local pNewScene = cc.Scene:create() + + local function CreateBasicLayerMenuItem(pMenu,bMenuVisible,nMaxCasesNum,nCurCaseIndex) + if nil ~= pMenu then + bControlMenuVisible = bMenuVisible + nMaxCases = nMaxCasesNum + nCurCase = nCurCaseIndex + if true == bControlMenuVisible then + local function backCallback() + nCurCase = nCurCase - 1 + if nCurCase < 0 then + nCurCase = nCurCase + nMaxCases + end + ShowCurrentTest() + end + + local function restartCallback() + ShowCurrentTest() + end + + local function nextCallback() + nCurCase = nCurCase + 1 + --No check nMaxCases + nCurCase = nCurCase % nMaxCases + ShowCurrentTest() + end + + local size = cc.Director:getInstance():getWinSize() + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + item1:registerScriptTapHandler(backCallback) + pMenu:addChild(item1,kItemTagBasic) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + item2:registerScriptTapHandler(restartCallback) + pMenu:addChild(item2,kItemTagBasic) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + pMenu:addChild(item3,kItemTagBasic) + item3:registerScriptTapHandler(nextCallback) + + local size = cc.Director:getInstance():getWinSize() + item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + end + end + end + + local function UpdateNodes() + if nQuantityNodes ~= nLastRenderedCount then + local pInfoLabel = tolua.cast(pNewScene:getChildByTag(SpriteTestParam.kTagInfoLayer), "cc.LabelTTF") + local strInfo = string.format("%u nodes", nQuantityNodes) + pInfoLabel:setString(strInfo) + nLastRenderedCount = nQuantityNodes + end + end + + local function PerformancePosition(pSprite) + local size = cc.Director:getInstance():getWinSize() + pSprite:setPosition(cc.p((math.random(0,SpriteTestParam.kRandMax) % (size.width) ), (math.random(0,SpriteTestParam.kRandMax) % (size.height)))) + end + + local function PerformanceScale(pSprite) + local size = cc.Director:getInstance():getWinSize() + pSprite:setPosition(cc.p((math.random(0,SpriteTestParam.kRandMax) % (size.width) ), (math.random(0,SpriteTestParam.kRandMax) % (size.height)))) + pSprite:setScale(math.random() * 100 / 50) + end + + local function PerformanceRotationScale(pSprite) + local size = cc.Director:getInstance():getWinSize() + pSprite:setPosition(cc.p((math.random(0,SpriteTestParam.kRandMax) % (size.width) ), (math.random(0,SpriteTestParam.kRandMax) % (size.height)))) + pSprite:setRotation(math.random() * 360) + pSprite:setScale(math.random() * 2) + end + + local function PerformanceOut100(pSprite) + pSprite:setPosition(cc.p( -1000, -1000)) + end + + local function Performanceout20(pSprite) + local size = cc.Director:getInstance():getWinSize() + print("come in") + if math.random() < 0.2 then + pSprite:setPosition(cc.p((math.random(0,SpriteTestParam.kRandMax) % (size.width) ), (math.random(0,SpriteTestParam.kRandMax) % (size.height)))) + else + pSprite:setPosition(cc.p( -1000, -1000)) + end + end + + local function PerformanceActions(pSprite) + local size = cc.Director:getInstance():getWinSize() + pSprite:setPosition(cc.p((math.random(0,SpriteTestParam.kRandMax) % (size.width) ), (math.random(0,SpriteTestParam.kRandMax) % (size.height)))) + + local fPeriod = 0.5 + (math.random(0,SpriteTestParam.kRandMax) % 1000) / 500.0 + local pRot = cc.RotateBy:create(fPeriod, 360.0 * math.random() ) + local pRot_back = pRot:reverse() + local pPermanentRotation = cc.RepeatForever:create(cc.Sequence:create(pRot, pRot_back)) + pSprite:runAction(pPermanentRotation) + + local fGrowDuration = 0.5 + (math.random(0,SpriteTestParam.kRandMax) % 1000) / 500.0 + local pGrow = cc.ScaleBy:create(fGrowDuration, 0.5, 0.5) + local pPermanentScaleLoop = cc.RepeatForever:create(cc.Sequence:create(pGrow, pGrow:reverse())) + pSprite:runAction(pPermanentScaleLoop) + end + + local function PerformanceActions20(pSprite) + local size = cc.Director:getInstance():getWinSize() + + if math.random() < 0.2 then + pSprite:setPosition(cc.p((math.random(0,SpriteTestParam.kRandMax) % (size.width) ), (math.random(0,SpriteTestParam.kRandMax) % (size.height)))) + else + pSprite:setPosition(cc.p( -1000, -1000)) + end + + local pPeriod = 0.5 + (math.random(0,SpriteTestParam.kRandMax) % 1000) / 500.0 + local pRot = cc.RotateBy:create(pPeriod, 360.0 * math.random()) + local pRot_back = pRot:reverse() + local pPermanentRotation = cc.RepeatForever:create(cc.Sequence:create(pRot, pRot_back)) + pSprite:runAction(pPermanentRotation) + + + local fGrowDuration = 0.5 + (math.random(0,SpriteTestParam.kRandMax) % 1000) / 500.0 + local pGrow = cc.ScaleBy:create(fGrowDuration, 0.5, 0.5) + local pPermanentScaleLoop = cc.RepeatForever:create(cc.Sequence:create(pGrow, pGrow:reverse())) + pSprite:runAction(pPermanentScaleLoop) + + end + + local function CreateSpriteWithTag(nTag) + --create + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + local pSprite = nil + if 1 == nSubtestNumber then + pSprite = cc.Sprite:create("Images/grossinis_sister1.png") + pNewScene:addChild(pSprite, 0, nTag+100) + elseif 2 == nSubtestNumber or 3 == nSubtestNumber then + pSprite = cc.Sprite:createWithTexture(pBatchNode:getTexture(), cc.rect(0, 0, 52, 139)) + pBatchNode:addChild(pSprite, 0, nTag+100) + elseif 4 == nSubtestNumber then + local nIndex = math.floor((math.random() * 1400 / 100)) + 1 + local strPath = string.format("Images/grossini_dance_%02d.png", nIndex) + pSprite = cc.Sprite:create(strPath) + pNewScene:addChild(pSprite, 0, nTag+100) + elseif 5 == nSubtestNumber or 6 == nSubtestNumber then + local nY = 0 + local nX = 0 + local nR = math.floor(math.random() * 1400 / 100) + + nX = nR % 5 + nY = math.floor(nR / 5) + + nX = nX * 85 + nY = nY * 121 + pSprite = cc.Sprite:createWithTexture(pBatchNode:getTexture(), cc.rect(nX,nY,85,121)) + pBatchNode:addChild(pSprite, 0, nTag+100) + elseif 7 == nSubtestNumber then + local nX = 0 + local nY = 0 + local nR = math.floor(math.random() * 6400 / 100) + + nX = nR % 8 + nY = math.floor(nR / 8) + + local strPath = string.format("Images/sprites_test/sprite-%d-%d.png", nX, nY) + pSprite = cc.Sprite:create(strPath) + pNewScene:addChild(pSprite, 0, nTag+100) + elseif 8 == nSubtestNumber or 9 == nSubtestNumber then + local nX = 0 + local nY = 0 + local nR = math.floor(math.random() * 6400 / 100) + + nX = nR % 8 + nY = math.floor(nR / 8) + + nX = nX * 32 + nY = nY * 32 + pSprite = cc.Sprite:createWithTexture(pBatchNode:getTexture(), cc.rect(nX,nY,32,32)) + pBatchNode:addChild(pSprite, 0, nTag+100) + end + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE_PIXELFORMAT_DEFAULT ) + + return pSprite + end + + local function RemoveByTag(nTag) + if 1 == nSubtestNumber or 4 == nSubtestNumber or 7 == nSubtestNumber then + pNewScene:removeChildByTag(nTag + 100, true) + elseif 2 == nSubtestNumber or 3 == nSubtestNumber or 5 == nSubtestNumber or 6 == nSubtestNumber or 8 == nSubtestNumber or 9 == nSubtestNumber then + pBatchNode:removeChildAtIndex(nTag,true) + end + + end + + local function DoTest(pSprite) + if 0 == nCurCase then + PerformancePosition(pSprite) + elseif 1 == nCurCase then + PerformanceScale(pSprite) + elseif 2 == nCurCase then + PerformanceRotationScale(pSprite) + elseif 3 == nCurCase then + PerformanceOut100(pSprite) + elseif 4 == nCurCase then + Performanceout20(pSprite) + elseif 5 == nCurCase then + PerformanceActions(pSprite) + elseif 6 == nCurCase then + PerformanceActions20(pSprite) + end + end + + local function onDecrease() + if nQuantityNodes <= 0 then + return + end + + local i = 0 + for i = 0 , SpriteTestParam.kNodesIncrease - 1 do + nQuantityNodes = nQuantityNodes - 1 + RemoveByTag(nQuantityNodes) + end + UpdateNodes() + end + + local function onIncrease() + if nQuantityNodes >= SpriteTestParam.kMaxNodes then + return + end + + local i = 0 + for i = 0,SpriteTestParam.kNodesIncrease - 1 do + local pSprite = CreateSpriteWithTag(nQuantityNodes) + DoTest(pSprite) + nQuantityNodes = nQuantityNodes + 1 + end + UpdateNodes() + end + + local function TestNCallback(tag,pMenuItem) + local nIndex = pMenuItem:getLocalZOrder() - SpriteTestParam.kSubMenuBasicZOrder + nSubtestNumber = nIndex + ShowCurrentTest() + end + + local function InitWithSubTest(nSubTest) + pBatchNode = nil + --[[ + * Tests: + * 1: 1 (32-bit) PNG sprite of 52 x 139 + * 2: 1 (32-bit) PNG Batch Node using 1 sprite of 52 x 139 + * 3: 1 (16-bit) PNG Batch Node using 1 sprite of 52 x 139 + * 4: 1 (4-bit) PVRTC Batch Node using 1 sprite of 52 x 139 + + * 5: 14 (32-bit) PNG sprites of 85 x 121 each + * 6: 14 (32-bit) PNG Batch Node of 85 x 121 each + * 7: 14 (16-bit) PNG Batch Node of 85 x 121 each + * 8: 14 (4-bit) PVRTC Batch Node of 85 x 121 each + + * 9: 64 (32-bit) sprites of 32 x 32 each + *10: 64 (32-bit) PNG Batch Node of 32 x 32 each + *11: 64 (16-bit) PNG Batch Node of 32 x 32 each + *12: 64 (4-bit) PVRTC Batch Node of 32 x 32 each + ]]-- + --purge textures + local pMgr = cc.Director:getInstance():getTextureCache() + --[mgr removeAllTextures] + pMgr:removeTexture(pMgr:addImage("Images/grossinis_sister1.png")) + pMgr:removeTexture(pMgr:addImage("Images/grossini_dance_atlas.png")) + pMgr:removeTexture(pMgr:addImage("Images/spritesheet1.png")) + + if 1 == nSubTest or 4 == nSubTest or 7 == nSubTest then + elseif 2 == nSubTest then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + pBatchNode = cc.SpriteBatchNode:create("Images/grossinis_sister1.png", 100) + pNewScene:addChild(pBatchNode, 0) + elseif 3 == nSubTest then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + pBatchNode = cc.SpriteBatchNode:create("Images/grossinis_sister1.png", 100) + pNewScene:addChild(pBatchNode, 0) + elseif 5 == nSubTest then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + pBatchNode = cc.SpriteBatchNode:create("Images/grossini_dance_atlas.png", 100) + pNewScene:addChild(pBatchNode, 0) + elseif 6 == nSubTest then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + pBatchNode = cc.SpriteBatchNode:create("Images/grossini_dance_atlas.png", 100) + pNewScene:addChild(pBatchNode, 0) + elseif 8 == nSubTest then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + pBatchNode = cc.SpriteBatchNode:create("Images/spritesheet1.png", 100) + pNewScene:addChild(pBatchNode, 0) + elseif 9 == nSubTest then + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + pBatchNode = cc.SpriteBatchNode:create("Images/spritesheet1.png", 100) + pNewScene:addChild(pBatchNode, 0) + end + + if nil ~= pBatchNode then + pBatchNode:retain() + end + + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE_PIXELFORMAT_DEFAULT ) + end + + local function InitWithSpriteTest(nSubtest,nNodes) + nSubtestNumber = nSubtest + --about create subset + InitWithSubTest(nSubtest) + local s = cc.Director:getInstance():getWinSize() + + nLastRenderedCount = 0 + nQuantityNodes = 0 + + --"+","-" Menu + cc.MenuItemFont:setFontSize(65) + local pDecrease = cc.MenuItemFont:create(" - ") + pDecrease:registerScriptTapHandler(onDecrease) + pDecrease:setColor(cc.c3b(0,200,20)) + local pIncrease = cc.MenuItemFont:create(" + ") + pIncrease:registerScriptTapHandler(onIncrease) + pIncrease:setColor(cc.c3b(0,200,20)) + + local pMenuAddOrSub = cc.Menu:create() + pMenuAddOrSub:addChild(pDecrease) + pMenuAddOrSub:addChild(pIncrease) + pMenuAddOrSub:alignItemsHorizontally() + pMenuAddOrSub:setPosition(cc.p(s.width/2, s.height/2+15)) + pNewScene:addChild(pMenuAddOrSub,1) + + local pInfoLabel = cc.LabelTTF:create("0 nodes", "Marker Felt", 30) + pInfoLabel:setColor(cc.c3b(0,200,20)) + pInfoLabel:setPosition(cc.p(s.width/2, s.height - 90)) + pNewScene:addChild(pInfoLabel, 1, SpriteTestParam.kTagInfoLayer) + + --SpriteTestMenuLayer + local pSpriteMenuLayer = cc.Layer:create() + local pSpriteMenu = cc.Menu:create() + CreatePerfomBasicLayerMenu(pSpriteMenu) + CreateBasicLayerMenuItem(pSpriteMenu,true,SpriteTestParam.TEST_COUNT,nCurCase) + pSpriteMenu:setPosition(cc.p(0, 0)) + pSpriteMenuLayer:addChild(pSpriteMenu) + pNewScene:addChild(pSpriteMenuLayer,1,SpriteTestParam.kTagMenuLayer) + + + --Sub Tests + cc.MenuItemFont:setFontSize(40) + local pSubMenu = cc.Menu:create() + local i = 1 + for i = 1, 9 do + local strNum = string.format("%d ",i) + local pItemFont = cc.MenuItemFont:create(strNum) + pItemFont:registerScriptTapHandler(TestNCallback) + pSubMenu:addChild(pItemFont, i + SpriteTestParam.kSubMenuBasicZOrder) + if i <= 3 then + pItemFont:setColor(cc.c3b(200,20,20)) + elseif i <= 6 then + pItemFont:setColor(cc.c3b(0,200,20)) + else + pItemFont:setColor(cc.c3b(0,20,200)) + end + end + + + pSubMenu:alignItemsHorizontally() + pSubMenu:setPosition(cc.p(s.width/2, 80)) + pNewScene:addChild(pSubMenu, 2) + + local pLabel = cc.LabelTTF:create(GetTitle(), "Arial", 40) + pNewScene:addChild(pLabel, 1) + pLabel:setPosition(cc.p(s.width/2, s.height-32)) + pLabel:setColor(cc.c3b(255,255,40)) + while nQuantityNodes < nNodes do + onIncrease() + end + end + + function ShowCurrentTest() + pNewScene = cc.Scene:create() + InitWithSpriteTest(nSubtestNumber,nQuantityNodes) + cc.Director:getInstance():replaceScene(pNewScene) + end + + InitWithSpriteTest(1,SpriteTestParam.kInitNodes) + + return pNewScene +end + +---------------------------------- +--PerformanceTextureTest +---------------------------------- +local TextureTestParam = +{ + TEST_COUNT = 1, +} + +local function runTextureTest() + + local function GetTitle() + return "Texture Performance Test" + end + + local function GetSubtitle() + return "See console for results" + end + + local nTexCurCase = 0 + local pNewscene = cc.Scene:create() + local pLayer = cc.Layer:create() + local s = cc.Director:getInstance():getWinSize() + + local function PerformTestsPNG(strFileName) + local time + local pTexture = nil + local pCache = cc.Director:getInstance():getTextureCache() + + local pDefaultFormat = cc.Texture2D:getDefaultAlphaPixelFormat(); + + print("RGBA 8888") + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + pTexture = pCache:addImage(strFileName) + if nil ~= pTexture then + --os.time()--get secs,not micr sec + print("add sucess") + else + print(" ERROR") + end + pCache:removeTexture(pTexture) + + print("RGBA 4444") + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + --gettimeofday(&now, NULL) + pTexture = pCache:addImage(strFileName) + if nil ~= pTexture then + print("add sucess") + else + print(" ERROR") + end + pCache:removeTexture(pTexture) + + print("RGBA 5551") + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB5_A1) + --gettimeofday(&now, NULL) + pTexture = pCache:addImage(strFileName) + if nil ~= pTexture then + print("add sucess") + else + print(" ERROR") + end + pCache:removeTexture(pTexture) + + print("RGB 565") + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RG_B565) + -- gettimeofday(&now, NULL) + pTexture = pCache:addImage(strFileName) + if nil ~= pTexture then + --log(" ms:%f", calculateDeltaTime(&now) ) + print("add sucess") + else + print(" ERROR") + end + pCache:removeTexture(pTexture) + + cc.Texture2D:setDefaultAlphaPixelFormat(pDefaultFormat) + end + local function PerformTests() + print("--------") + print("--- PNG 128x128 ---") + PerformTestsPNG("Images/test_image.png") + + print("--- PNG 512x512 ---") + PerformTestsPNG("Images/texture512x512.png") + + print("EMPTY IMAGE") + print("--- PNG 1024x1024 ---") + PerformTestsPNG("Images/texture1024x1024.png") + + print("SPRITESHEET IMAGE") + print("--- PNG 1024x1024 ---") + PerformTestsPNG("Images/PlanetCute-1024x1024.png") + + print("LANDSCAPE IMAGE") + print("--- PNG 1024x1024 ---") + PerformTestsPNG("Images/landscape-1024x1024.png") + end + + local function InitTextureMenuLayer() + if nil == pLayer then + return + end + + --Title + local pLabel = cc.LabelTTF:create(GetTitle(), "Arial", 40) + pLayer:addChild(pLabel, 1) + pLabel:setPosition(cc.p(s.width/2, s.height-32)) + pLabel:setColor(cc.c3b(255,255,40)) + + --Subtitle + local pSubLabel = cc.LabelTTF:create(GetSubtitle(), "Thonburi", 16) + pLayer:addChild(pSubLabel, 1) + pSubLabel:setPosition(cc.p(s.width/2, s.height-80)) + + --menu + local pMenu = cc.Menu:create() + CreatePerfomBasicLayerMenu(pMenu) + pMenu:setPosition(cc.p(0, 0)) + pLayer:addChild(pMenu) + + PerformTests() + end + + InitTextureMenuLayer() + pNewscene:addChild(pLayer) + return pNewscene +end +---------------------------------- +--PerformanceTouchesTest +---------------------------------- +local TouchesTestParam = +{ + TEST_COUNT = 2, +} +local function runTouchesTest() + local nTouchCurCase = 0 + --PerformBasicLayer param + local bControlMenuVisible = false + local nMaxCases = 0 + local nCurCase = 0 + --TouchesMainScene param + local pClassLabel = nil + local nNumberOfTouchesB = 0 + local nNumberOfTouchesM = 0 + local nNumberOfTouchesE = 0 + local nNumberOfTouchesC = 0 + local fElapsedTime = 0.0 + + local s = cc.Director:getInstance():getWinSize() + local pNewscene = cc.Scene:create() + local pLayer = cc.Layer:create() + + local function GetTitle() + if 0 == nCurCase then + return "Targeted touches" + elseif 1 == nCurCase then + return "Standard touches" + end + end + + local function CreateBasicLayerMenuItem(pMenu,bMenuVisible,nMaxCasesNum,nCurCaseIndex) + if nil ~= pMenu then + bControlMenuVisible = bMenuVisible + nMaxCases = nMaxCasesNum + nCurCase = nCurCaseIndex + if true == bControlMenuVisible then + local function backCallback() + nCurCase = nCurCase - 1 + if nCurCase < 0 then + nCurCase = nCurCase + nMaxCases + end + ShowCurrentTest() + end + + local function restartCallback() + ShowCurrentTest() + end + + local function nextCallback() + nCurCase = nCurCase + 1 + --No check nMaxCases + nCurCase = nCurCase % nMaxCases + ShowCurrentTest() + end + + local size = cc.Director:getInstance():getWinSize() + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + item1:registerScriptTapHandler(backCallback) + pMenu:addChild(item1,kItemTagBasic) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + item2:registerScriptTapHandler(restartCallback) + pMenu:addChild(item2,kItemTagBasic) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + pMenu:addChild(item3,kItemTagBasic) + item3:registerScriptTapHandler(nextCallback) + + local size = cc.Director:getInstance():getWinSize() + item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + end + end + end + + local function update(fTime) + fElapsedTime = fElapsedTime + fTime + + if fElapsedTime > 1.0 then + local fFrameRateB = nNumberOfTouchesB / fElapsedTime + local fFrameRateM = nNumberOfTouchesM / fElapsedTime + local fFrameRateE = nNumberOfTouchesE / fElapsedTime + local fFrameRateC = nNumberOfTouchesC / fElapsedTime + fElapsedTime = 0 + nNumberOfTouchesB = 0 + nNumberOfTouchesM = 0 + nNumberOfTouchesE = 0 + nNumberOfTouchesC = 0 + + local strInfo = string.format("%.1f %.1f %.1f %.1f",fFrameRateB, fFrameRateM, fFrameRateE, fFrameRateC) + if nil ~= pClassLabel then + pClassLabel:setString(strInfo) + end + end + + end + + -- handling touch events + local function onTouchEnded(touch, event) + nNumberOfTouchesE = nNumberOfTouchesE + 1 + end + + local function onTouchBegan(touch, event) + nNumberOfTouchesB = nNumberOfTouchesB + 1 + end + + local function onTouchMoved(touch, event) + nNumberOfTouchesM = nNumberOfTouchesM + 1 + end + + local function onTouchCancelled(touch, event) + nNumberOfTouchesC = nNumberOfTouchesC + 1 + end + + + local function onTouchesEnded(touches, event) + nNumberOfTouchesE = nNumberOfTouchesE + table.getn(touches) + end + + local function onTouchesBegan(touches, event) + nNumberOfTouchesB = nNumberOfTouchesB + table.getn(touches) + end + + local function onTouchesMoved(touches, event) + nNumberOfTouchesM = nNumberOfTouchesM + table.getn(touches) + end + + local function onTouchesCancelled(touches, event) + nNumberOfTouchesC= nNumberOfTouchesC + table.getn(touches) + end + + local function InitLayer() + --menu + local pTouchesTestMenu = cc.Menu:create() + CreatePerfomBasicLayerMenu(pTouchesTestMenu) + CreateBasicLayerMenuItem(pTouchesTestMenu,true,TouchesTestParam.TEST_COUNT,nCurCase) + pTouchesTestMenu:setPosition(cc.p(0, 0)) + pLayer:addChild(pTouchesTestMenu) + + --Title + local pLabel = cc.LabelTTF:create(GetTitle(), "Arial", 40) + pLayer:addChild(pLabel, 1) + pLabel:setPosition(cc.p(s.width/2, s.height-32)) + pLabel:setColor(cc.c3b(255,255,40)) + + pLayer:scheduleUpdateWithPriorityLua(update,0) + + pClassLabel = cc.LabelBMFont:create("00.0", "fonts/arial16.fnt") + pClassLabel:setPosition(cc.p(s.width/2, s.height/2)) + pLayer:addChild(pClassLabel) + + fElapsedTime = 0.0 + nNumberOfTouchesB = 0 + nNumberOfTouchesM = 0 + nNumberOfTouchesE = 0 + nNumberOfTouchesC = 0 + + if 0 == nCurCase then + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) + listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) + listener:registerScriptHandler(onTouchCancelled,cc.Handler.EVENT_TOUCH_CANCELLED ) + local eventDispatcher = pLayer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, pLayer) + elseif 1 == nCurCase then + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesBegan,cc.Handler.EVENT_TOUCHES_BEGAN ) + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + listener:registerScriptHandler(onTouchesEnded,cc.Handler.EVENT_TOUCHES_ENDED ) + listener:registerScriptHandler(onTouchesCancelled,cc.Handler.EVENT_TOUCHES_CANCELLED ) + local eventDispatcher = pLayer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, pLayer) + end + end + + function ShowCurrentTest() + if nil ~= pLayer then + pLayer:unscheduleUpdate() + end + + pNewscene = cc.Scene:create() + + if nil ~= pNewscene then + pLayer = cc.Layer:create() + InitLayer() + pNewscene:addChild(pLayer) + cc.Director:getInstance():replaceScene(pNewscene) + end + end + + InitLayer() + pNewscene:addChild(pLayer) + return pNewscene +end + +local function runFuncRelateWithTable() + -- body + local newscene = cc.Scene:create() + local layer = cc.Layer:create() + local s = cc.Director:getInstance():getWinSize() + local scheduler = cc.Director:getInstance():getScheduler() + local scheduleEntryID = 0 + local quantityOfNodes = 10000 + local socket = require("socket") + local maxTime = 0.0 + local minTime = 99999 + local averageTime1 = 0.0 + local averageTime2 = 0.0 + local totalTime = 0.0 + local numberOfCalls = 0 + + local function GetTitle() + return "Func Releated Table Performance Test" + end + + local function GetSubtitle() + return "See console for results" + end + + local function initVar() + maxTime = 0.0 + minTime = 99999 + averageTime1 = 0.0 + averageTime2 = 0.0 + totalTime = 0.0 + numberOfCalls = 0 + end + + --Title + local title = cc.LabelTTF:create(GetTitle(), "Arial", 28) + layer:addChild(title, 1) + title:setPosition(cc.p(s.width/2, s.height-32)) + title:setColor(cc.c3b(255,255,40)) + --Subtitle + local subTitle = cc.LabelTTF:create(GetSubtitle(), "Thonburi", 16) + layer:addChild(subTitle, 1) + subTitle:setPosition(cc.p(s.width/2, s.height-80)) + + --"+","-" Menu + local function onDecrease() + quantityOfNodes = quantityOfNodes - 100 + if quantityOfNodes == 0 then + quantityOfNodes = 100 + end + local numLabel = tolua.cast(layer:getChildByTag(NodeChildrenTestParam.kTagInfoLayer), "cc.LabelTTF") + local strNum = string.format("%d", quantityOfNodes) + numLabel:setString(strNum) + end + + local function onIncrease() + quantityOfNodes = quantityOfNodes + 100 + local numLabel = tolua.cast(layer:getChildByTag(NodeChildrenTestParam.kTagInfoLayer), "cc.LabelTTF") + local strNum = string.format("%d", quantityOfNodes) + numLabel:setString(strNum) + end + + cc.MenuItemFont:setFontSize(65) + local decrease = cc.MenuItemFont:create(" - ") + decrease:registerScriptTapHandler(onDecrease) + decrease:setColor(cc.c3b(0,200,20)) + local increase = cc.MenuItemFont:create(" + ") + increase:registerScriptTapHandler(onIncrease) + increase:setColor(cc.c3b(0,200,20)) + + local menuAddOrSub = cc.Menu:create() + menuAddOrSub:addChild(decrease) + menuAddOrSub:addChild(increase) + menuAddOrSub:alignItemsHorizontally() + menuAddOrSub:setPosition(cc.p(s.width/2, s.height/2+15)) + layer:addChild(menuAddOrSub,1) + + --num + local numLabel = cc.LabelTTF:create("10000", "Marker Felt", 30) + numLabel:setColor(cc.c3b(0,200,20)) + numLabel:setPosition(cc.p(s.width/2, s.height/2-15)) + layer:addChild(numLabel, 1, NodeChildrenTestParam.kTagInfoLayer) + + --setPosition,getPosition,Point + cc.MenuItemFont:setFontSize(18) + local setPositionItem = cc.MenuItemFont:create("setPosition") + local getPositionItem = cc.MenuItemFont:create("getPosition") + local getAnchorPointItem = cc.MenuItemFont:create("getAnchorPoint") + local pointItem = cc.MenuItemFont:create("object") + local funcToggleItem = cc.MenuItemToggle:create(setPositionItem) + funcToggleItem:addSubItem(getPositionItem) + funcToggleItem:addSubItem(getAnchorPointItem) + funcToggleItem:addSubItem(pointItem) + funcToggleItem:setAnchorPoint(cc.p(0.0, 0.5)) + funcToggleItem:setPosition(cc.p(VisibleRect:left())) + local funcMenu = cc.Menu:create(funcToggleItem) + funcMenu:setPosition(cc.p(0, 0)) + layer:addChild(funcMenu) + + local testNode = cc.Node:create() + layer:addChild(testNode) + + local function step(dt) + print(string.format("push num: %d, avg1:%f, avg2:%f,min:%f, max:%f, total: %f, calls: %d",quantityOfNodes, averageTime1, averageTime2, minTime, maxTime, totalTime, numberOfCalls)) + end + + local function profileEnd(startTime) + local duration = socket.gettime() - startTime + totalTime = totalTime + duration + averageTime1 = (averageTime1 + duration) / 2 + averageTime2 = totalTime / numberOfCalls + + if maxTime < duration then + maxTime = duration + end + + if minTime > duration then + minTime = duration + end + end + + local function callSetPosition() + numberOfCalls = numberOfCalls + 1 + local startTime = socket.gettime() + for i=1,quantityOfNodes do + testNode:setPosition(cc.p(1,2)) + end + profileEnd(startTime) + end + + local function callGetPosition() + numberOfCalls = numberOfCalls + 1 + local startTime = socket.gettime() + for i=1,quantityOfNodes do + local x,y = testNode:getPosition() + end + profileEnd(startTime) + end + + local function callGetAnchorPoint() + numberOfCalls = numberOfCalls + 1 + local startTime = socket.gettime() + for i=1,quantityOfNodes do + local anchorPoint = testNode:getAnchorPoint() + end + profileEnd(startTime) + end + + local function callTableObject() + numberOfCalls = numberOfCalls + 1 + local startTime = socket.gettime() + for i=1,quantityOfNodes do + local pt = cc.p(1,2) + end + profileEnd(startTime) + end + + local function update(dt) + + local funcSelected = funcToggleItem:getSelectedIndex() + if 0 == funcSelected then + callSetPosition() + elseif 1 == funcSelected then + callGetPosition() + elseif 2 == funcSelected then + callGetAnchorPoint() + elseif 3 == funcSelected then + callTableObject() + end + end + + local function onNodeEvent(tag) + if tag == "exit" then + layer:unscheduleUpdate() + scheduler:unscheduleScriptEntry(scheduleEntryID) + end + end + + layer:registerScriptHandler(onNodeEvent) + + + local function startCallback() + initVar() + decrease:setEnabled(false) + increase:setEnabled(false) + funcToggleItem:setEnabled(false) + layer:unscheduleUpdate() + layer:scheduleUpdateWithPriorityLua(update, 0) + scheduler:unscheduleScriptEntry(scheduleEntryID) + scheduleEntryID = scheduler:scheduleScriptFunc(step,2,false) + end + + local function stopCallback() + decrease:setEnabled(true) + increase:setEnabled(true) + funcToggleItem:setEnabled(true) + layer:unscheduleUpdate() + scheduler:unscheduleScriptEntry(scheduleEntryID) + end + local startItem = cc.MenuItemFont:create("start") + startItem:registerScriptTapHandler(startCallback) + local stopItem = cc.MenuItemFont:create("stop") + stopItem:registerScriptTapHandler(stopCallback) + local startAndStop = cc.Menu:create(startItem,stopItem) + startAndStop:alignItemsVertically() + startAndStop:setPosition(VisibleRect:right().x - 50, VisibleRect:right().y) + layer:addChild(startAndStop) + + --back menu + local menu = cc.Menu:create() + CreatePerfomBasicLayerMenu(menu) + menu:setPosition(cc.p(0, 0)) + layer:addChild(menu) + + newscene:addChild(layer) + return newscene +end + + +------------------------ +-- +------------------------ +local CreatePerformancesTestTable = +{ + runNodeChildrenTest, + runParticleTest, + runSpriteTest, + runTextureTest, + runTouchesTest, + runFuncRelateWithTable, +} + +local function CreatePerformancesTestScene(nPerformanceNo) + local pNewscene = CreatePerformancesTestTable[nPerformanceNo]() + return pNewscene +end +local function menuCallback(tag, pMenuItem) + local scene = nil + local nIdx = pMenuItem:getLocalZOrder() - kItemTagBasic + local PerformanceTestScene = CreatePerformancesTestScene(nIdx) + if nil ~= PerformanceTestScene then + cc.Director:getInstance():replaceScene(PerformanceTestScene) + end +end + +local function PerformanceMainLayer() + local layer = cc.Layer:create() + + local menu = cc.Menu:create() + menu:setPosition(cc.p(0, 0)) + cc.MenuItemFont:setFontName("Arial") + cc.MenuItemFont:setFontSize(24) + for i = 1, MAX_COUNT do + local item = cc.MenuItemFont:create(testsName[i]) + item:registerScriptTapHandler(menuCallback) + item:setPosition(s.width / 2, s.height - i * LINE_SPACE) + menu:addChild(item, kItemTagBasic + i) + end + + layer:addChild(menu) + + return layer +end + +------------------------------------- +-- Performance Test +------------------------------------- +function PerformanceTestMain() + local scene = cc.Scene:create() + + scene:addChild(PerformanceMainLayer()) + scene:addChild(CreateBackMenuItem()) + + return scene +end diff --git a/samples/lua-tests/src/PhysicsTest/PhysicsTest.lua b/samples/lua-tests/src/PhysicsTest/PhysicsTest.lua new file mode 100644 index 0000000000..409a8a28a4 --- /dev/null +++ b/samples/lua-tests/src/PhysicsTest/PhysicsTest.lua @@ -0,0 +1,1368 @@ +local size = cc.Director:getInstance():getWinSize() +local MATERIAL_DEFAULT = cc.PhysicsMaterial(0.1, 0.5, 0.5) +local curLayer = nil +local STATIC_COLOR = cc.c4f(1.0, 0.0, 0.0, 1.0) +local DRAG_BODYS_TAG = 0x80 + +local function range(from, to, step) + step = step or 1 + return function(_, lastvalue) + local nextvalue = lastvalue + step + if step > 0 and nextvalue <= to or step < 0 and nextvalue >= to or + step == 0 + then + return nextvalue + end + end, nil, from - step +end + +-- bit operation + +bit = bit or {} +bit.data32 = {} +for i=1,32 do + bit.data32[i]=2^(32-i) +end + +function bit._b2d(arg) + local nr=0 + for i=1,32 do + if arg[i] ==1 then + nr=nr+bit.data32[i] + end + end + return nr +end + +function bit._d2b(arg) + arg = arg >= 0 and arg or (0xFFFFFFFF + arg + 1) + local tr={} + for i=1,32 do + if arg >= bit.data32[i] then + tr[i]=1 + arg=arg-bit.data32[i] + else + tr[i]=0 + end + end + return tr +end + +function bit._and(a,b) + local op1=bit._d2b(a) + local op2=bit._d2b(b) + local r={} + + for i=1,32 do + if op1[i]==1 and op2[i]==1 then + r[i]=1 + else + r[i]=0 + end + end + return bit._b2d(r) + +end + +function bit._rshift(a,n) + local op1=bit._d2b(a) + n = n <= 32 and n or 32 + n = n >= 0 and n or 0 + + for i=32, n+1, -1 do + op1[i] = op1[i-n] + end + for i=1, n do + op1[i] = 0 + end + + return bit._b2d(op1) +end + +function bit._not(a) + local op1=bit._d2b(a) + local r={} + + for i=1,32 do + if op1[i]==1 then + r[i]=0 + else + r[i]=1 + end + end + return bit._b2d(r) +end + +bit.band = bit.band or bit._and +bit.rshift = bit.rshift or bit._rshift +bit.bnot = bit.bnot or bit._not + +-- bit operation end + +local function initWithLayer(layer, callback) + curLayer = layer + layer.spriteTexture = cc.SpriteBatchNode:create("Images/grossini_dance_atlas.png", 100):getTexture() + + local debug = false + local function toggleDebugCallback(sender) + debug = not debug + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setDebugDrawMask(debug and cc.PhysicsWorld.DEBUGDRAW_ALL or cc.PhysicsWorld.DEBUGDRAW_NONE) + end + + layer.toggleDebug = function(self) toggleDebugCallback(nil) end; + cc.MenuItemFont:setFontSize(18) + local item = cc.MenuItemFont:create("Toogle debug") + item:registerScriptTapHandler(toggleDebugCallback) + local menu = cc.Menu:create(item) + layer:addChild(menu) + menu:setPosition(size.width - 50, size.height - 10) + Helper.initWithLayer(layer) + + local function onNodeEvent(event) + if "enter" == event then + callback() + end + end + layer:registerScriptHandler(onNodeEvent) +end + +local function addGrossiniAtPosition(layer, p, scale) + scale = scale or 1.0 + + local posx = math.random() * 200.0 + local posy = math.random() * 100.0 + posx = (math.floor(posx) % 4) * 85 + posy = (math.floor(posy) % 3) * 121 + + local sp = cc.Sprite:createWithTexture(layer.spriteTexture, cc.rect(posx, posy, 85, 121)) + sp:setScale(scale) + sp:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(48.0*scale, 108.0*scale))) + layer:addChild(sp) + sp:setPosition(p) + return sp +end + +local function onTouchBegan(touch, event) + local location = touch:getLocation() + local arr = cc.Director:getInstance():getRunningScene():getPhysicsWorld():getShapes(location) + + local body + for _, obj in ipairs(arr) do + if bit.band(obj:getBody():getTag(), DRAG_BODYS_TAG) ~= 0 then + body = obj:getBody(); + break; + end + end + + if body then + local mouse = cc.Node:create(); + mouse:setPhysicsBody(cc.PhysicsBody:create(PHYSICS_INFINITY, PHYSICS_INFINITY)); + mouse:getPhysicsBody():setDynamic(false); + mouse:setPosition(location); + curLayer:addChild(mouse); + local joint = cc.PhysicsJointPin:construct(mouse:getPhysicsBody(), body, location); + joint:setMaxForce(5000.0 * body:getMass()); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint); + touch.mouse = mouse + + return true; + end + + return false; +end + +local function onTouchMoved(touch, event) + if touch.mouse then + touch.mouse:setPosition(touch:getLocation()); + end +end + +local function onTouchEnded(touch, event) + if touch.mouse then + curLayer:removeChild(touch.mouse) + touch.mouse = nil + end +end + +local function makeBall(layer, point, radius, material) + material = material or MATERIAL_DEFAULT + + local ball + if layer.ball then + ball = cc.Sprite:createWithTexture(layer.ball:getTexture()) + else + ball = cc.Sprite:create("Images/ball.png") + end + + ball:setScale(0.13 * radius) + + local body = cc.PhysicsBody:createCircle(radius, material) + ball:setPhysicsBody(body) + ball:setPosition(point) + + return ball +end + +local function makeBox(point, size, color, material) + material = material or MATERIAL_DEFAULT + + local yellow = false; + if color == 0 then + yellow = math.random() > 0.5; + else + yellow = color == 1; + end + + local box = yellow and cc.Sprite:create("Images/YellowSquare.png") or cc.Sprite:create("Images/CyanSquare.png"); + + box:setScaleX(size.width/100.0); + box:setScaleY(size.height/100.0); + + local body = cc.PhysicsBody:createBox(size, material); + box:setPhysicsBody(body); + box:setPosition(cc.p(point.x, point.y)); + + return box; +end + +local function makeTriangle(point, size, color, material) + material = material or MATERIAL_DEFAULT + + local yellow = false; + if color == 0 then + yellow = math.random() > 0.5; + else + yellow = color == 1; + end + local triangle = yellow and cc.Sprite:create("Images/YellowTriangle.png") or cc.Sprite:create("Images/CyanTriangle.png"); + + if size.height == 0 then + triangle:setScale(size.width/100.0); + else + triangle:setScaleX(size.width/50.0) + triangle:setScaleY(size.height/43.5) + end + + vers = { cc.p(0, size.height/2), cc.p(size.width/2, -size.height/2), cc.p(-size.width/2, -size.height/2)}; + + local body = cc.PhysicsBody:createPolygon(vers, material); + triangle:setPhysicsBody(body); + triangle:setPosition(point); + + return triangle; +end + +local function PhysicsDemoClickAdd() + local layer = cc.Layer:create() + local function onEnter() + local function onTouchEnded(touch, event) + local location = touch:getLocation(); + addGrossiniAtPosition(layer, location) + end + + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(function() return true end, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + addGrossiniAtPosition(layer, VisibleRect:center()) + + local node = cc.Node:create() + node:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height))) + node:setPosition(VisibleRect:center()) + layer:addChild(node) + end + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Grossini") + Helper.subtitleLabel:setString("multi touch to add grossini") + + return layer +end + +local function PhysicsDemoLogoSmash() + local layer = cc.Layer:create() + + local function onEnter() + local logo_width = 188.0 + local logo_height = 35.0 + local logo_raw_length = 24.0 + local logo_image = { + 15,-16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-64,15,63,-32,-2,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,31,-64,15,127,-125,-1,-128,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,127,-64,15,127,15,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-2, + 31,-1,-64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,-64,0,-4,63,-1,-32,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,1,-1,-64,15,-8,127,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 1,-1,-64,0,-8,-15,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-31,-1,-64,15,-8,-32, + -1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,-15,-1,-64,9,-15,-32,-1,-32,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,31,-15,-1,-64,0,-15,-32,-1,-32,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,63,-7,-1,-64,9,-29,-32,127,-61,-16,63,15,-61,-1,-8,31,-16,15,-8,126,7,-31, + -8,31,-65,-7,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, + -4,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-2,63,-8,31,-4,-1,15,-13, + -2,63,-1,-3,-1,-64,9,-29,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, + -2,63,-33,-1,-1,-32,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, + -1,63,-33,-1,-1,-16,9,-25,-32,0,7,-8,127,-97,-25,-1,-1,63,-4,63,-4,-1,15,-13, + -1,63,-49,-1,-1,-8,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, + -1,-65,-49,-1,-1,-4,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, + -1,-65,-57,-1,-1,-2,9,-57,-32,0,7,-8,127,-97,-25,-8,-1,63,-2,127,-4,-1,15,-13, + -1,-1,-57,-1,-1,-1,9,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, + -1,-61,-1,-1,-1,-119,-57,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, + -1,-61,-1,-1,-1,-55,-49,-32,0,7,-1,-1,-97,-25,-8,-1,63,-1,-1,-4,-1,15,-13,-1, + -1,-63,-1,-1,-1,-23,-49,-32,127,-57,-1,-1,-97,-25,-1,-1,63,-1,-1,-4,-1,15,-13, + -1,-1,-63,-1,-1,-1,-16,-49,-32,-1,-25,-1,-1,-97,-25,-1,-1,63,-33,-5,-4,-1,15, + -13,-1,-1,-64,-1,-9,-1,-7,-49,-32,-1,-25,-8,127,-97,-25,-1,-1,63,-33,-5,-4,-1, + 15,-13,-1,-1,-64,-1,-13,-1,-32,-49,-32,-1,-25,-8,127,-97,-25,-1,-2,63,-49,-13, + -4,-1,15,-13,-1,-1,-64,127,-7,-1,-119,-17,-15,-1,-25,-8,127,-97,-25,-1,-2,63, + -49,-13,-4,-1,15,-13,-3,-1,-64,127,-8,-2,15,-17,-1,-1,-25,-8,127,-97,-25,-1, + -8,63,-49,-13,-4,-1,15,-13,-3,-1,-64,63,-4,120,0,-17,-1,-1,-25,-8,127,-97,-25, + -8,0,63,-57,-29,-4,-1,15,-13,-4,-1,-64,63,-4,0,15,-17,-1,-1,-25,-8,127,-97, + -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,-1,-64,31,-2,0,0,103,-1,-1,-57,-8,127,-97, + -25,-8,0,63,-57,-29,-4,-1,-1,-13,-4,127,-64,31,-2,0,15,103,-1,-1,-57,-8,127, + -97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,127,-64,15,-8,0,0,55,-1,-1,-121,-8, + 127,-97,-25,-8,0,63,-61,-61,-4,127,-1,-29,-4,63,-64,15,-32,0,0,23,-1,-2,3,-16, + 63,15,-61,-16,0,31,-127,-127,-8,31,-1,-127,-8,31,-128,7,-128,0,0 + }; + + local function get_pixel(x, y) + return bit.band(bit.rshift(logo_image[bit.rshift(x, 3) + y*logo_raw_length + 1], bit.band(bit.bnot(x), 0x07)), 1) + end + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setUpdateRate(5.0); + + layer.ball = cc.SpriteBatchNode:create("Images/ball.png", #logo_image); + layer:addChild(layer.ball); + for y in range(0, logo_height-1) do + for x in range(0, logo_width-1) do + if get_pixel(x, y) == 1 then + local x_jitter = 0.05*math.random(); + local y_jitter = 0.05*math.random(); + + local ball = makeBall(layer, cc.p(2*(x - logo_width/2 + x_jitter) + VisibleRect:getVisibleRect().width/2, + 2*(logo_height-y + y_jitter) + VisibleRect:getVisibleRect().height/2 - logo_height/2), + 0.95, cc.PhysicsMaterial(0.01, 0.0, 0.0)); + + ball:getPhysicsBody():setMass(1.0); + ball:getPhysicsBody():setMoment(PHYSICS_INFINITY); + + layer.ball:addChild(ball); + end + end + end + + local bullet = makeBall(layer, cc.p(400, 0), 10, cc.PhysicsMaterial(PHYSICS_INFINITY, 0, 0)); + bullet:getPhysicsBody():setVelocity(cc.p(200, 0)); + bullet:setPosition(cc.p(-500, VisibleRect:getVisibleRect().height/2)) + layer.ball:addChild(bullet); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Logo Smash") + + return layer +end + +local function PhysicsDemoJoints() + local layer = cc.Layer:create() + local function onEnter() + layer:toggleDebug(); + + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + local width = (VisibleRect:getVisibleRect().width - 10) / 4; + local height = (VisibleRect:getVisibleRect().height - 50) / 4; + + local node = cc.Node:create(); + local box = cc.PhysicsBody:create(); + node:setPhysicsBody(box); + box:setDynamic(false); + node:setPosition(cc.p(0, 0)); + layer:addChild(node); + + local scene = cc.Director:getInstance():getRunningScene(); + for i in range(0, 3) do + for j in range(0, 3) do + local offset = cc.p(VisibleRect:leftBottom().x + 5 + j * width + width/2, VisibleRect:leftBottom().y + 50 + i * height + height/2); + box:addShape(cc.PhysicsShapeEdgeBox:create(cc.size(width, height), cc.PHYSICSSHAPE_MATERIAL_DEFAULT, 1, offset)); + local index = i*4 + j + if index == 0 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBall(layer, cc.p(offset.x + 30, offset.y), 10); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 1 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointFixed:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), offset); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 2 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointDistance:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0)); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 3 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 30.0, 60.0); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 4 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointSpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(0, 0), cc.p(0, 0), 500.0, 0.3); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 5 then + local sp1 = makeBall(layer, cc.p(offset.x - 30, offset.y), 10); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + local joint = cc.PhysicsJointGroove:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), cc.p(30, 15), cc.p(30, -15), cc.p(-30, 0)) + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 6 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointRotarySpring:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 3000.0, 60.0); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 7 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointRotaryLimit:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 8 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointRatchet:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, math.pi/2); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 9 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointGear:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), 0.0, 2.0); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + elseif index == 10 then + local sp1 = makeBox(cc.p(offset.x - 30, offset.y), cc.size(30, 10)); + sp1:getPhysicsBody():setTag(DRAG_BODYS_TAG); + local sp2 = makeBox(cc.p(offset.x + 30, offset.y), cc.size(30, 10)); + sp2:getPhysicsBody():setTag(DRAG_BODYS_TAG); + + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp1:getPhysicsBody(), box, cc.p(sp1:getPosition()))); + scene:getPhysicsWorld():addJoint(cc.PhysicsJointPin:construct(sp2:getPhysicsBody(), box, cc.p(sp2:getPosition()))); + local joint = cc.PhysicsJointMotor:construct(sp1:getPhysicsBody(), sp2:getPhysicsBody(), math.pi/2); + scene:getPhysicsWorld():addJoint(joint); + + layer:addChild(sp1); + layer:addChild(sp2); + end + end + end + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Joints") + return layer +end + +local function PhysicsDemoPyramidStack() + local layer = cc.Layer:create() + + local function onEnter() + local touchListener = cc.EventListenerTouchOneByOne:create(); + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN); + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED); + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + + local node = cc.Node:create(); + node:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50))); + layer:addChild(node); + + local ball = cc.Sprite:create("Images/ball.png"); + ball:setScale(1); + ball:setPhysicsBody(cc.PhysicsBody:createCircle(10)); + ball:getPhysicsBody():setTag(DRAG_BODYS_TAG); + ball:setPosition(cc.p(VisibleRect:bottom().x, VisibleRect:bottom().y + 60)); + layer:addChild(ball); + + for i in range(0, 13) do + for j in range(0, i) do + local x = VisibleRect:bottom().x + (i/2 - j) * 11 + local y = VisibleRect:bottom().y + (14 - i) * 23 + 100 + local sp = addGrossiniAtPosition(layer, cc.p(x, y), 0.2); + sp:getPhysicsBody():setTag(DRAG_BODYS_TAG); + end + end + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Pyramid Stack") + + return layer +end + +local function PhysicsDemoRayCast() + local layer = cc.Layer:create() + + local function onEnter() + local function onTouchEnded(touch, event) + local location = touch:getLocation(); + + local r = math.random(3); + if r ==1 then + layer:addChild(makeBall(layer, location, 5 + math.random()*10)); + elseif r == 2 then + layer:addChild(makeBox(location, cc.size(10 + math.random()*15, 10 + math.random()*15))); + elseif r == 3 then + layer:addChild(makeTriangle(location, cc.size(10 + math.random()*20, 10 + math.random()*20))); + end + end + + local touchListener = cc.EventListenerTouchOneByOne:create(); + touchListener:registerScriptHandler(function() return true end, cc.Handler.EVENT_TOUCH_BEGAN); + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0,0)); + + local node = cc.DrawNode:create(); + node:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50))) + node:drawSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50), 1, STATIC_COLOR); + layer:addChild(node); + + local mode = 0 + cc.MenuItemFont:setFontSize(18); + local item = cc.MenuItemFont:create("Toogle debugChange Mode(any)") + local function changeModeCallback(sender) + mode = (mode + 1) % 3; + + if mode == 0 then + item:setString("Change Mode(any)"); + elseif mode == 1 then + item:setString("Change Mode(nearest)"); + elseif mode == 2 then + item:setString("Change Mode(multiple)"); + end + end + + item:registerScriptTapHandler(changeModeCallback) + + local menu = cc.Menu:create(item); + layer:addChild(menu); + menu:setPosition(cc.p(VisibleRect:left().x+100, VisibleRect:top().y-10)); + + local angle = 0 + local drawNode = nil + local function update(delta) + local L = 150.0; + local point1 = VisibleRect:center() + local d = cc.p(L * math.cos(angle), L * math.sin(angle)); + local point2 = cc.p(point1.x + d.x, point1.y + d.y) + + if drawNode then layer:removeChild(drawNode); end + drawNode = cc.DrawNode:create(); + if mode == 0 then + local point3 = cc.p(point2.x, point2.y) + local function func(world, info) + point3 = info.contact + return false + end + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2); + drawNode:drawSegment(point1, point3, 1, STATIC_COLOR); + + if point2.x ~= point3.x or point2.y ~= point3.y then + drawNode:drawDot(point3, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)); + end + layer:addChild(drawNode); + elseif mode == 1 then + local point3 = cc.p(point2.x, point2.y) + local friction = 1.0; + local function func(world, info) + if friction > info.fraction then + point3 = info.contact; + friction = info.fraction; + end + return true; + end + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2); + drawNode:drawSegment(point1, point3, 1, STATIC_COLOR); + + if point2.x ~= point3.x or point2.y ~= point3.y then + drawNode:drawDot(point3, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)); + end + layer:addChild(drawNode); + elseif mode == 2 then + local points = {} + + local function func(world, info) + points[#points + 1] = info.contact; + return true; + end + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(func, point1, point2); + drawNode:drawSegment(point1, point2, 1, STATIC_COLOR); + + for _, p in ipairs(points) do + drawNode:drawDot(p, 2, cc.c4f(1.0, 1.0, 1.0, 1.0)); + end + + layer:addChild(drawNode); + end + + angle = angle + 0.25 * math.pi / 180.0; + + end + + layer:scheduleUpdateWithPriorityLua(update, 0); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Ray Cast") + + return layer +end + +local function PhysicsDemoOneWayPlatform() + local layer = cc.Layer:create() + local function onEnter() + + local touchListener = cc.EventListenerTouchOneByOne:create(); + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN); + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED); + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + + local ground = cc.Node:create(); + ground:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50))); + layer:addChild(ground); + + local platform = makeBox(VisibleRect:center(), cc.size(200, 50)); + platform:getPhysicsBody():setDynamic(false); + platform:getPhysicsBody():setContactTestBitmask(1); + layer:addChild(platform); + + local ball = makeBall(layer, cc.p(VisibleRect:center().x, VisibleRect:center().y - 50), 20); + ball:getPhysicsBody():setVelocity(cc.p(0, 150)); + ball:getPhysicsBody():setTag(DRAG_BODYS_TAG); + ball:getPhysicsBody():setMass(1.0); + ball:getPhysicsBody():setContactTestBitmask(1); + layer:addChild(ball); + + local function onContactBegin(contact) + return contact:getContactData().normal.y < 0; + end + local contactListener = cc.EventListenerPhysicsContactWithBodies:create(platform:getPhysicsBody(), ball:getPhysicsBody()); + contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN); + eventDispatcher:addEventListenerWithSceneGraphPriority(contactListener, layer); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("One Way Platform") + + return layer +end + +local function PhysicsDemoActions() + local layer = cc.Layer:create() + local function onEnter() + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + local node = cc.Node:create(); + node:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height))); + node:setPosition(VisibleRect:center()); + layer:addChild(node); + + local sp1 = addGrossiniAtPosition(layer, VisibleRect:center()); + local sp2 = addGrossiniAtPosition(layer, cc.p(VisibleRect:left().x + 50, VisibleRect:left().y)); + local sp3 = addGrossiniAtPosition(layer, cc.p(VisibleRect:right().x - 20, VisibleRect:right().y)); + local sp4 = addGrossiniAtPosition(layer, cc.p(VisibleRect:leftTop().x + 50, VisibleRect:leftTop().y-50)); + sp4:getPhysicsBody():setGravityEnable(false); + + + local actionTo = cc.JumpTo:create(2, cc.p(100,100), 50, 4); + local actionBy = cc.JumpBy:create(2, cc.p(300,0), 50, 4); + local actionUp = cc.JumpBy:create(2, cc.p(0,50), 80, 4); + local actionByBack = actionBy:reverse(); + + sp1:runAction(cc.RepeatForever:create(actionUp)); + sp2:runAction(cc.RepeatForever:create(cc.Sequence:create(actionBy, actionByBack))); + sp3:runAction(actionTo); + sp4:runAction(cc.RepeatForever:create(cc.Sequence:create(actionBy:clone(), actionByBack:clone()))); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Actions") + + return layer +end + +local function PhysicsDemoPump() + local layer = cc.Layer:create() + local function onEnter() + layer:toggleDebug(); + + local distance = 0.0; + local rotationV = 0.0; + local function onTouchBeganEx(touch, event) + onTouchBegan(touch, event) + distance = touch:getLocation().x - VisibleRect:center().x; + return true; + end + + local function onTouchMovedEx(touch, event) + onTouchMoved(touch, event); + distance = touch:getLocation().x - VisibleRect:center().x; + end + + local function onTouchEndedEx(touch, event) + onTouchEnded(touch, event) + distance = 0; + end + + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBeganEx, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchMovedEx, cc.Handler.EVENT_TOUCH_MOVED) + touchListener:registerScriptHandler(onTouchEndedEx, cc.Handler.EVENT_TOUCH_ENDED) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + local function update() + for _, body in ipairs(cc.Director:getInstance():getRunningScene():getPhysicsWorld():getAllBodies()) do + if body:getTag() == DRAG_BODYS_TAG and body:getPosition().y < 0.0 then + body:getNode():setPosition(cc.p(VisibleRect:leftTop().x + 75, VisibleRect:leftTop().y + math.random() * 90, 0)); + body:setVelocity(cc.p(0, 0)); + end + end + + local gear = cc.Director:getInstance():getRunningScene():getPhysicsWorld():getBody(1); + if gear then + if distance ~= 0.0 then + rotationV = rotationV + distance/2500.0; + end + if rotationV > 30 then rotationV = 30.0 end + if rotationV < -30 then rotationV = -30.0 end + + gear:setAngularVelocity(rotationV); + rotationV = rotationV*0.995; + end + end + + layer:scheduleUpdateWithPriorityLua(update, 0); + + local node = cc.Node:create(); + local body = cc.PhysicsBody:create(); + body:setDynamic(false); + + local staticMaterial = cc.PhysicsMaterial(cc.PHYSICS_INFINITY, 0, 0.5); + body:addShape(cc.PhysicsShapeEdgeSegment:create( + cc.p(VisibleRect:leftTop().x + 50, VisibleRect:leftTop().y), + cc.p(VisibleRect:leftTop().x + 50, VisibleRect:leftTop().y-130), staticMaterial, 2.0)); + body:addShape(cc.PhysicsShapeEdgeSegment:create( + cc.p(VisibleRect:leftTop().x + 190, VisibleRect:leftTop().y), + cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-50), staticMaterial, 2.0)); + body:addShape(cc.PhysicsShapeEdgeSegment:create( + cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-50), + cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-90), staticMaterial, 2.0)); + body:addShape(cc.PhysicsShapeEdgeSegment:create( + cc.p(VisibleRect:leftTop().x + 50, VisibleRect:leftTop().y-130), + cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-145), staticMaterial, 2.0)); + body:addShape(cc.PhysicsShapeEdgeSegment:create( + cc.p(VisibleRect:leftTop().x + 100, VisibleRect:leftTop().y-145), + cc.p(VisibleRect:leftBottom().x + 100, VisibleRect:leftBottom().y + 80), staticMaterial, 2.0)); + body:addShape(cc.PhysicsShapeEdgeSegment:create( + cc.p(VisibleRect:leftTop().x + 150, VisibleRect:leftTop().y-80), + cc.p(VisibleRect:leftBottom().x + 150, VisibleRect:leftBottom().y + 80), staticMaterial, 2.0)); + body:addShape(cc.PhysicsShapeEdgeSegment:create( + cc.p(VisibleRect:leftTop().x + 150, VisibleRect:leftTop().y-80), + cc.p(VisibleRect:rightTop().x -100, VisibleRect:rightTop().y-150), staticMaterial, 2.0)); + + body:setCategoryBitmask(1); + + for _ in range(1, 6) do + local ball = makeBall(layer, cc.p(VisibleRect:leftTop().x + 75 + math.random() * 90, VisibleRect:leftTop().y), 22, cc.PhysicsMaterial(0.05, 0.0, 0.1)); + ball:getPhysicsBody():setTag(DRAG_BODYS_TAG); + layer:addChild(ball); + end + + node:setPhysicsBody(body); + layer:addChild(node); + + local vec = + { + cc.p(VisibleRect:leftTop().x + 102, VisibleRect:leftTop().y-148), + cc.p(VisibleRect:leftTop().x + 148, VisibleRect:leftTop().y-161), + cc.p(VisibleRect:leftBottom().x + 148, VisibleRect:leftBottom().y + 20), + cc.p(VisibleRect:leftBottom().x + 102, VisibleRect:leftBottom().y + 20) + }; + + local world = cc.Director:getInstance():getRunningScene():getPhysicsWorld(); + + -- small gear + local sgear = cc.Node:create(); + local sgearB = cc.PhysicsBody:createCircle(44); + sgear:setPhysicsBody(sgearB); + sgear:setPosition(cc.p(VisibleRect:leftBottom().x + 125, VisibleRect:leftBottom().y)); + layer:addChild(sgear); + sgearB:setCategoryBitmask(4); + sgearB:setCollisionBitmask(4); + sgearB:setTag(1); + world:addJoint(cc.PhysicsJointPin:construct(body, sgearB, sgearB:getPosition())); + + + -- big gear + local bgear = cc.Node:create(); + local bgearB = cc.PhysicsBody:createCircle(100); + bgear:setPhysicsBody(bgearB); + bgear:setPosition(cc.p(VisibleRect:leftBottom().x + 275, VisibleRect:leftBottom().y)); + layer:addChild(bgear); + bgearB:setCategoryBitmask(4); + world:addJoint(cc.PhysicsJointPin:construct(body, bgearB, bgearB:getPosition())); + + + -- pump + local pump = cc.Node:create(); + local center = cc.PhysicsShape:getPolyonCenter(vec); + pump:setPosition(center); + local pumpB = cc.PhysicsBody:createPolygon(vec, cc.PHYSICSBODY_MATERIAL_DEFAULT, cc.p(-center.x, -center.y)); + pump:setPhysicsBody(pumpB); + layer:addChild(pump); + pumpB:setCategoryBitmask(2); + pumpB:setGravityEnable(false); + world:addJoint(cc.PhysicsJointDistance:construct(pumpB, sgearB, cc.p(0, 0), cc.p(0, -44))); + + -- plugger + local seg = {cc.p(VisibleRect:leftTop().x + 75, VisibleRect:leftTop().y-120), cc.p(VisibleRect:leftBottom().x + 75, VisibleRect:leftBottom().y-100)}; + local segCenter = cc.p((seg[2].x + seg[1].x)/2, (seg[2].y + seg[1].y)/2); + seg[2] = cc.p(seg[2].x - segCenter.x, seg[2].y - segCenter.y); + seg[1] = cc.p(seg[1].x - segCenter.x, seg[1].y - segCenter.y); + local plugger = cc.Node:create(); + local pluggerB = cc.PhysicsBody:createEdgeSegment(seg[1], seg[2], cc.PhysicsMaterial(0.01, 0.0, 0.5), 20); + pluggerB:setDynamic(true); + pluggerB:setMass(30); + pluggerB:setMoment(100000); + plugger:setPhysicsBody(pluggerB); + plugger:setPosition(segCenter); + layer:addChild(plugger); + pluggerB:setCategoryBitmask(2); + sgearB:setCollisionBitmask(5); + world:addJoint(cc.PhysicsJointPin:construct(body, pluggerB, cc.p(VisibleRect:leftBottom().x + 75, VisibleRect:leftBottom().y-90))); + world:addJoint(cc.PhysicsJointDistance:construct(pluggerB, sgearB, + pluggerB:world2Local(cc.p(VisibleRect:leftBottom().x + 75, VisibleRect:leftBottom().y)), cc.p(44, 0))); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Pump") + Helper.subtitleLabel:setString("touch screen on left or right") + + return layer +end + +local function PhysicsDemoSlice() + local layer = cc.Layer:create() + local function onEnter() + layer:toggleDebug() + local sliceTag = 1; + + local function clipPoly(shape, normal, distance) + local body = shape:getBody(); + local count = shape:getPointsCount(); + local points = {} + + local j = count - 1 + for i in range(0, count-1) do + local a = body:local2World(shape:getPoint(j)); + local aDist = cc.pDot(a, normal) - distance; + + if aDist < 0.0 then + points[#points + 1] = a; + end + + local b = body:local2World(shape:getPoint(i)); + local bDist = cc.pDot(b, normal) - distance; + + if aDist*bDist < 0.0 then + local t = math.abs(aDist)/(math.abs(aDist) + math.abs(bDist)); + points[#points + 1] = cc.pLerp(a, b, t); + end + j = i + end + + local center = cc.PhysicsShape:getPolyonCenter(points); + local node = cc.Node:create(); + local polyon = cc.PhysicsBody:createPolygon(points, cc.PHYSICSBODY_MATERIAL_DEFAULT, cc.p(-center.x, -center.y)); + node:setPosition(center); + node:setPhysicsBody(polyon); + polyon:setVelocity(body:getVelocityAtWorldPoint(center)); + polyon:setAngularVelocity(body:getAngularVelocity()); + polyon.tag = sliceTag; + layer:addChild(node); + end + + + local function slice(world, info) + if info.shape:getBody().tag ~= sliceTag then + return true; + end + + if not info.shape:containsPoint(info.start) and not info.shape:containsPoint(info.ended) then + local normal = cc.p(info.ended.x - info.start.x, info.ended.y - info.start.y); + normal = cc.pNormalize(cc.pPerp(normal)); + local dist = cc.pDot(info.start, normal); + + clipPoly(info.shape, normal, dist); + clipPoly(info.shape, cc.p(-normal.x, -normal.y), -dist); + + info.shape:getBody():removeFromWorld(); + end + return true; + end + + local function onTouchEnded(touch, event) + cc.Director:getInstance():getRunningScene():getPhysicsWorld():rayCast(slice, touch:getStartLocation(), touch:getLocation()); + end + + local touchListener = cc.EventListenerTouchOneByOne:create(); + touchListener:registerScriptHandler(function() return true end, cc.Handler.EVENT_TOUCH_BEGAN); + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED); + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer); + + local ground = cc.Node:create(); + ground:setPhysicsBody(cc.PhysicsBody:createEdgeSegment(cc.p(VisibleRect:leftBottom().x, VisibleRect:leftBottom().y + 50), + cc.p(VisibleRect:rightBottom().x, VisibleRect:rightBottom().y + 50))); + layer:addChild(ground); + + local box = cc.Node:create(); + local points = {cc.p(-100, -100), cc.p(-100, 100), cc.p(100, 100), cc.p(100, -100)}; + box:setPhysicsBody(cc.PhysicsBody:createPolygon(points)); + box:setPosition(VisibleRect:center()); + box:getPhysicsBody().tag = sliceTag; + layer:addChild(box); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Slice") + Helper.subtitleLabel:setString("click and drag to slice up the block") + + return layer +end + + +local function PhysicsDemoBug3988() + local layer = cc.Layer:create() + local function onEnter() + layer:toggleDebug(); + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)); + + local ball = cc.Sprite:create("Images/YellowSquare.png"); + ball:setPosition(cc.p(VisibleRect:center().x-100, VisibleRect:center().y)); + ball:setRotation(30.0); + layer:addChild(ball); + + local physicsBall = makeBox(cc.p(VisibleRect:center().x+100, VisibleRect:center().y), cc.size(100, 100)); + physicsBall:setRotation(30.0); + layer:addChild(physicsBall); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Bug3988") + Helper.subtitleLabel:setString("All the Rectangles should have same rotation angle") + + return layer +end + +local function PhysicsContactTest() + local layer = cc.Layer:create() + local function onEnter() + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)); + local s = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); + + layer.yellowBoxNum = 50; + layer.blueBoxNum = 50; + layer.yellowTriangleNum = 50; + layer.blueTriangleNum = 50; + + local function onContactBegin(contact) + local a = contact:getShapeA():getBody(); + local b = contact:getShapeB():getBody(); + local body = (a:getCategoryBitmask() == 4 or a:getCategoryBitmask() == 8) and a or b; + + assert(body:getCategoryBitmask() == 4 or body:getCategoryBitmask() == 8, "physics contact fail"); + + return true; + end + + local function resetTest() + layer:removeChildByTag(10); + local root = cc.Node:create(); + root:setTag(10); + layer:addChild(root); + + local s = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); + + local label = cc.LabelTTF:create(tostring(layer.yellowBoxNum), "Arial", 32); + root:addChild(label, 1); + label:setPosition(cc.p(s.width/2, s.height-50)); + + label = cc.LabelTTF:create(tostring(layer.blueBoxNum), "Arial", 32); + root:addChild(label, 1); + label:setPosition(cc.p(s.width/2, s.height-90)); + + label = cc.LabelTTF:create(tostring(layer.yellowTriangleNum), "Arial", 32); + root:addChild(label, 1); + label:setPosition(cc.p(s.width/2, s.height-130)); + + label = cc.LabelTTF:create(tostring(layer.blueTriangleNum), "Arial", 32); + root:addChild(label, 1); + label:setPosition(cc.p(s.width/2, s.height-170)); + + local wall = cc.Node:create(); + wall:setPhysicsBody(cc.PhysicsBody:createEdgeBox(s, cc.PhysicsMaterial(0.1, 1, 0.0))); + wall:setPosition(VisibleRect:center()); + root:addChild(wall); + + -- yellow box, will collide with itself and blue box. + for i = 1, layer.yellowBoxNum do + local size = cc.size(10 + math.random()*10, 10 + math.random()*10); + local winSize = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); + local position = cc.p(winSize.width - size.width, winSize.height - size.height); + position.x = position.x * math.random(); + position.y = position.y * math.random(); + position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, VisibleRect:leftBottom().y + position.y + size.height/2); + local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200); + local box = makeBox(position, size, 1, cc.PhysicsMaterial(0.1, 1, 0.0)); + box:getPhysicsBody():setVelocity(velocity); + box:getPhysicsBody():setCategoryBitmask(1); -- 0001 + box:getPhysicsBody():setContactTestBitmask(4); -- 0100 + box:getPhysicsBody():setCollisionBitmask(3); -- 0011 + root:addChild(box); + end + + -- blue box, will collide with blue box. + for i = 1, layer.blueBoxNum do + local size = cc.size(10 + math.random()*10, 10 + math.random()*10); + local winSize = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); + local position = cc.p(winSize.width - size.width, winSize.height - size.height); + position.x = position.x * math.random(); + position.y = position.y * math.random(); + position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, VisibleRect:leftBottom().y + position.y + size.height/2); + local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200); + local box = makeBox(position, size, 2, cc.PhysicsMaterial(0.1, 1, 0.0)); + box:getPhysicsBody():setVelocity(velocity); + box:getPhysicsBody():setCategoryBitmask(2); -- 0010 + box:getPhysicsBody():setContactTestBitmask(8); -- 1000 + box:getPhysicsBody():setCollisionBitmask(1); -- 0001 + root:addChild(box); + end + + -- yellow triangle, will collide with itself and blue box. + for i = 1, layer.yellowTriangleNum do + local size = cc.size(10 + math.random()*10, 10 + math.random()*10); + local winSize = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); + local position = cc.p(winSize.width - size.width, winSize.height - size.height); + position.x = position.x * math.random(); + position.y = position.y * math.random(); + position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, VisibleRect:leftBottom().y + position.y + size.height/2); + local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200); + local triangle = makeTriangle(position, size, 1, cc.PhysicsMaterial(0.1, 1, 0.0)); + triangle:getPhysicsBody():setVelocity(velocity); + triangle:getPhysicsBody():setCategoryBitmask(4); -- 0100 + triangle:getPhysicsBody():setContactTestBitmask(1); -- 0001 + triangle:getPhysicsBody():setCollisionBitmask(6); -- 0110 + root:addChild(triangle); + end + + -- blue triangle, will collide with yellow box. + for i = 1, layer.blueTriangleNum do + local size = cc.size(10 + math.random()*10, 10 + math.random()*10); + local winSize = cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height); + local position = cc.p(winSize.width - size.width, winSize.height - size.height); + position.x = position.x * math.random(); + position.y = position.y * math.random(); + position = cc.p(VisibleRect:leftBottom().x + position.x + size.width/2, VisibleRect:leftBottom().y + position.y + size.height/2); + local velocity = cc.p((math.random() - 0.5)*200, (math.random() - 0.5)*200); + local triangle = makeTriangle(position, size, 2, cc.PhysicsMaterial(0.1, 1, 0.0)); + triangle:getPhysicsBody():setVelocity(velocity); + triangle:getPhysicsBody():setCategoryBitmask(8); -- 1000 + triangle:getPhysicsBody():setContactTestBitmask(2); -- 0010 + triangle:getPhysicsBody():setCollisionBitmask(1); -- 0001 + root:addChild(triangle); + end + end + + local function onDecrease(tag, sender) + if tag == 1 then + if layer.yellowBoxNum > 0 then layer.yellowBoxNum = layer.yellowBoxNum - 50 end + elseif tag == 2 then + if layer.blueBoxNum > 0 then layer.blueBoxNum = layer.blueBoxNum - 50 end + elseif tag == 3 then + if layer.yellowTriangleNum > 0 then layer.yellowTriangleNum = layer.yellowTriangleNum - 50 end + elseif tag == 4 then + if layer.blueTriangleNum > 0 then layer.blueTriangleNum = layer.blueTriangleNum - 50 end + end + + resetTest() + end + + local function onIncrease(tag, sender) + if tag == 1 then + layer.yellowBoxNum = layer.yellowBoxNum + 50 + elseif tag == 2 then + layer.blueBoxNum = layer.blueBoxNum + 50 + elseif tag == 3 then + layer.yellowTriangleNum = layer.yellowTriangleNum + 50 + elseif tag == 4 then + layer.blueTriangleNum = layer.blueTriangleNum + 50 + end + + resetTest(); + end + + cc.MenuItemFont:setFontSize(65); + local decrease1 = cc.MenuItemFont:create(" - "); + decrease1:setColor(cc.c3b(0,200,20)); + local increase1 = cc.MenuItemFont:create(" + "); + increase1:setColor(cc.c3b(0,200,20)); + decrease1:setTag(1); + increase1:setTag(1); + decrease1:registerScriptTapHandler(onDecrease); + increase1:registerScriptTapHandler(onIncrease); + + local menu1 = cc.Menu:create(decrease1, increase1); + menu1:alignItemsHorizontally(); + menu1:setPosition(cc.p(s.width/2, s.height-50)); + layer:addChild(menu1, 1); + + local label = cc.LabelTTF:create("yellow box", "Arial", 32); + layer:addChild(label, 1); + label:setPosition(cc.p(s.width/2 - 150, s.height-50)); + + local decrease2 = cc.MenuItemFont:create(" - "); + decrease2:setColor(cc.c3b(0,200,20)); + local increase2 = cc.MenuItemFont:create(" + "); + increase2:setColor(cc.c3b(0,200,20)); + decrease2:setTag(2); + increase2:setTag(2); + decrease2:registerScriptTapHandler(onDecrease); + increase2:registerScriptTapHandler(onIncrease); + + local menu2 = cc.Menu:create(decrease2, increase2); + menu2:alignItemsHorizontally(); + menu2:setPosition(cc.p(s.width/2, s.height-90)); + layer:addChild(menu2, 1); + + label = cc.LabelTTF:create("blue box", "Arial", 32); + layer:addChild(label, 1); + label:setPosition(cc.p(s.width/2 - 150, s.height-90)); + + local decrease3 = cc.MenuItemFont:create(" - "); + decrease3:setColor(cc.c3b(0,200,20)); + local increase3 = cc.MenuItemFont:create(" + "); + increase3:setColor(cc.c3b(0,200,20)); + decrease3:setTag(3); + increase3:setTag(3); + decrease3:registerScriptTapHandler(onDecrease); + increase3:registerScriptTapHandler(onIncrease); + + local menu3 = cc.Menu:create(decrease3, increase3); + menu3:alignItemsHorizontally(); + menu3:setPosition(cc.p(s.width/2, s.height-130)); + layer:addChild(menu3, 1); + + label = cc.LabelTTF:create("yellow triangle", "Arial", 32); + layer:addChild(label, 1); + label:setPosition(cc.p(s.width/2 - 150, s.height-130)); + + local decrease4 = cc.MenuItemFont:create(" - "); + decrease4:setColor(cc.c3b(0,200,20)); + local increase4 = cc.MenuItemFont:create(" + "); + increase4:setColor(cc.c3b(0,200,20)); + decrease4:setTag(4); + increase4:setTag(4); + decrease4:registerScriptTapHandler(onDecrease); + increase4:registerScriptTapHandler(onIncrease); + + local menu4 = cc.Menu:create(decrease4, increase4); + menu4:alignItemsHorizontally(); + menu4:setPosition(cc.p(s.width/2, s.height-170)); + layer:addChild(menu4, 1); + + label = cc.LabelTTF:create("blue triangle", "Arial", 32); + layer:addChild(label, 1); + label:setPosition(cc.p(s.width/2 - 150, s.height-170)); + + + local contactListener = cc.EventListenerPhysicsContact:create(); + contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN); + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(contactListener, layer); + + resetTest(); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Contact Test") + + return layer +end + +local function PhysicsPositionRotationTest() + local layer = cc.Layer:create() + local function onEnter() + layer:toggleDebug() + + cc.Director:getInstance():getRunningScene():getPhysicsWorld():setGravity(cc.p(0, 0)); + + local touchListener = cc.EventListenerTouchOneByOne:create() + touchListener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) + touchListener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED) + touchListener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(touchListener, layer) + + local wall = cc.Node:create(); + wall:setPhysicsBody(cc.PhysicsBody:createEdgeBox(cc.size(VisibleRect:getVisibleRect().width, VisibleRect:getVisibleRect().height))); + wall:setPosition(VisibleRect:center()); + layer:addChild(wall); + + -- anchor test + local anchorNode = cc.Sprite:create("Images/YellowSquare.png"); + anchorNode:setAnchorPoint(cc.p(0.1, 0.9)); + anchorNode:setPosition(100, 100); + anchorNode:setScale(0.25); + anchorNode:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(anchorNode:getContentSize().width*anchorNode:getScale(), anchorNode:getContentSize().height*anchorNode:getScale()))); + anchorNode:getPhysicsBody():setTag(DRAG_BODYS_TAG); + layer:addChild(anchorNode); + + --parent test + local parent = cc.Sprite:create("Images/YellowSquare.png"); + parent:setPosition(200, 100); + parent:setScale(0.25); + parent:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(anchorNode:getContentSize().width*anchorNode:getScale(), anchorNode:getContentSize().height*anchorNode:getScale()))); + parent:getPhysicsBody():setTag(DRAG_BODYS_TAG); + layer:addChild(parent); + + local leftBall = cc.Sprite:create("Images/ball.png"); + leftBall:setPosition(-30, 0); + leftBall:setScale(2); + leftBall:setPhysicsBody(cc.PhysicsBody:createCircle(leftBall:getContentSize().width/4)); + leftBall:getPhysicsBody():setTag(DRAG_BODYS_TAG); + parent:addChild(leftBall); + + -- offset position rotation test + local offsetPosNode = cc.Sprite:create("Images/YellowSquare.png"); + offsetPosNode:setPosition(100, 200); + offsetPosNode:setPhysicsBody(cc.PhysicsBody:createBox(cc.size(offsetPosNode:getContentSize().width/2, offsetPosNode:getContentSize().height/2))); + offsetPosNode:getPhysicsBody():setPositionOffset(cc.p(-offsetPosNode:getContentSize().width/2, -offsetPosNode:getContentSize().height/2)); + offsetPosNode:getPhysicsBody():setRotationOffset(45); + offsetPosNode:getPhysicsBody():setTag(DRAG_BODYS_TAG); + layer:addChild(offsetPosNode); + end + + initWithLayer(layer, onEnter) + Helper.titleLabel:setString("Position/Rotation Test") + + return layer +end + +function PhysicsTest() + cclog("PhysicsTest") + local scene = cc.Scene:createWithPhysics() + + + Helper.usePhysics = true + Helper.createFunctionTable = { + PhysicsDemoLogoSmash, + PhysicsDemoPyramidStack, + PhysicsDemoClickAdd, + PhysicsDemoRayCast, + PhysicsDemoJoints, + PhysicsDemoActions, + PhysicsDemoPump, + PhysicsDemoOneWayPlatform, + PhysicsDemoSlice, + PhysicsDemoBug3988, + PhysicsContactTest, + PhysicsPositionRotationTest, + } + + scene:addChild(Helper.createFunctionTable[1]()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/RenderTextureTest/RenderTextureTest.lua b/samples/lua-tests/src/RenderTextureTest/RenderTextureTest.lua new file mode 100644 index 0000000000..d44a4a677a --- /dev/null +++ b/samples/lua-tests/src/RenderTextureTest/RenderTextureTest.lua @@ -0,0 +1,616 @@ +-- Test #1 by Jason Booth (slipster216) +-- Test #3 by David Deaco (ddeaco) + +--/** +-- * Impelmentation of RenderTextureSave +--*/ +local function RenderTextureSave() + local ret = createTestLayer("Touch the screen", + "Press 'Save Image' to create an snapshot of the render texture") + local s = cc.Director:getInstance():getWinSize() + local target = nil + local counter = 0 + local brushes = {} + local function clearImage(tag, pSender) + target:clear(math.random(), math.random(), math.random(), math.random()) + end + + local function saveImage(tag, pSender) + local png = string.format("image-%d.png", counter) + local jpg = string.format("image-%d.jpg", counter) + + target:saveToFile(png, cc.IMAGE_FORMAT_PNG) + target:saveToFile(jpg, cc.IMAGE_FORMAT_JPEG) + + local pImage = target:newImage() + + local tex = cc.Director:getInstance():getTextureCache():addUIImage(pImage, png) + + pImage:release() + + local sprite = cc.Sprite:createWithTexture(tex) + + sprite:setScale(0.3) + ret:addChild(sprite) + sprite:setPosition(cc.p(40, 40)) + sprite:setRotation(counter * 3) + + cclog("Image saved %s and %s", png, jpg) + counter = counter + 1 + end + + local function onNodeEvent(event) + if event == "exit" then + target:release() + cc.Director:getInstance():getTextureCache():removeUnusedTextures() + end + end + + ret:registerScriptHandler(onNodeEvent) + + -- create a render texture, this is what we are going to draw into + target = cc.RenderTexture:create(s.width, s.height, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + target:retain() + target:setPosition(cc.p(s.width / 2, s.height / 2)) + + -- note that the render texture is a cc.Node, and contains a sprite of its texture for convience, + -- so we can just parent it to the scene like any other cc.Node + ret:addChild(target, -1) + + local function onTouchesMoved(touches, event) + local start = touches[1]:getLocation() + local ended = touches[1]:getPreviousLocation() + + target:begin() + + local distance = cc.pGetDistance(start, ended) + if distance > 1 then + brushes = {} + local d = distance + local i = 0 + + for i = 0,d -1 do + -- create a brush image to draw into the texture with + local sprite = cc.Sprite:create("Images/fire.png") + sprite:setColor(cc.c3b(255, 0, 0)) + sprite:setOpacity(20) + brushes[i + 1] = sprite + end + + for i = 0,d -1 do + local difx = ended.x - start.x + local dify = ended.y - start.y + local delta = i / distance + brushes[i + 1]:setPosition(cc.p(start.x + (difx * delta), start.y + (dify * delta))) + brushes[i + 1]:setRotation(math.random(0, 359)) + local r = math.random(0, 49) / 50.0 + 0.25 + brushes[i + 1]:setScale(r) + + -- Use cc.RANDOM_0_1() will cause error when loading libtests.so on android, I don't know why. + brushes[i + 1]:setColor(cc.c3b(math.random(0, 126) + 128, 255, 255)) + -- Call visit to draw the brush, don't call draw.. + brushes[i + 1]:visit() + end + end + + -- finish drawing and return context back to the screen + target:endToLua() + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + local eventDispatcher = ret:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ret) + -- Save Image menu + cc.MenuItemFont:setFontSize(16) + local item1 = cc.MenuItemFont:create("Save Image") + item1:registerScriptTapHandler(saveImage) + local item2 = cc.MenuItemFont:create("Clear") + item2:registerScriptTapHandler(clearImage) + local menu = cc.Menu:create(item1, item2) + ret:addChild(menu) + menu:alignItemsVertically() + menu:setPosition(cc.p(VisibleRect:rightTop().x - 80, VisibleRect:rightTop().y - 30)) + return ret +end + + + +--/** +-- * Impelmentation of RenderTextureIssue937 +--*/ + +-- local function RenderTextureIssue937() + +-- /* +-- * 1 2 +-- * A: A1 A2 +-- * +-- * B: B1 B2 +-- * +-- * A1: premulti sprite +-- * A2: premulti render +-- * +-- * B1: non-premulti sprite +-- * B2: non-premulti render +-- */ +-- local background = cc.LayerColor:create(cc.c4b(200,200,200,255)) +-- addChild(background) + +-- local spr_premulti = cc.Sprite:create("Images/fire.png") +-- spr_premulti:setPosition(cc.p(16,48)) + +-- local spr_nonpremulti = cc.Sprite:create("Images/fire.png") +-- spr_nonpremulti:setPosition(cc.p(16,16)) + + + + +-- /* A2 & B2 setup */ +-- local rend = cc.RenderTexture:create(32, 64, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + +-- if (NULL == rend) + +-- return +-- end + +-- -- It's possible to modify the RenderTexture blending function by +-- -- [[rend sprite] setBlendFunc:(BlendFunc) GL_ONE, GL_ONE_MINUS_SRC_ALPHAend] + +-- rend:begin() +-- spr_premulti:visit() +-- spr_nonpremulti:visit() +-- rend:end() + +-- local s = cc.Director:getInstance():getWinSize() + +-- --/* A1: setup */ +-- spr_premulti:setPosition(cc.p(s.width/2-16, s.height/2+16)) +-- --/* B1: setup */ +-- spr_nonpremulti:setPosition(cc.p(s.width/2-16, s.height/2-16)) + +-- rend:setPosition(cc.p(s.width/2+16, s.height/2)) + +-- addChild(spr_nonpremulti) +-- addChild(spr_premulti) +-- addChild(rend) +-- end + +-- local function title() + +-- return "Testing issue #937" +-- end + +-- local function subtitle() + +-- return "All images should be equal..." +-- end + +-- local function runThisTest() + +-- local pLayer = nextTestCase() +-- addChild(pLayer) + +-- cc.Director:getInstance():replaceScene(this) +-- end + +-- --/** +-- -- * Impelmentation of RenderTextureZbuffer +-- --*/ + +-- local function RenderTextureZbuffer() + +-- this:setTouchEnabled(true) +-- local size = cc.Director:getInstance():getWinSize() +-- local label = cc.LabelTTF:create("vertexZ = 50", "Marker Felt", 64) +-- label:setPosition(cc.p(size.width / 2, size.height * 0.25)) +-- this:addChild(label) + +-- local label2 = cc.LabelTTF:create("vertexZ = 0", "Marker Felt", 64) +-- label2:setPosition(cc.p(size.width / 2, size.height * 0.5)) +-- this:addChild(label2) + +-- local label3 = cc.LabelTTF:create("vertexZ = -50", "Marker Felt", 64) +-- label3:setPosition(cc.p(size.width / 2, size.height * 0.75)) +-- this:addChild(label3) + +-- label:setVertexZ(50) +-- label2:setVertexZ(0) +-- label3:setVertexZ(-50) + +-- cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("Images/bugs/circle.plist") +-- mgr = cc.SpriteBatchNode:create("Images/bugs/circle.png", 9) +-- this:addChild(mgr) +-- sp1 = cc.Sprite:createWithSpriteFrameName("circle.png") +-- sp2 = cc.Sprite:createWithSpriteFrameName("circle.png") +-- sp3 = cc.Sprite:createWithSpriteFrameName("circle.png") +-- sp4 = cc.Sprite:createWithSpriteFrameName("circle.png") +-- sp5 = cc.Sprite:createWithSpriteFrameName("circle.png") +-- sp6 = cc.Sprite:createWithSpriteFrameName("circle.png") +-- sp7 = cc.Sprite:createWithSpriteFrameName("circle.png") +-- sp8 = cc.Sprite:createWithSpriteFrameName("circle.png") +-- sp9 = cc.Sprite:createWithSpriteFrameName("circle.png") + +-- mgr:addChild(sp1, 9) +-- mgr:addChild(sp2, 8) +-- mgr:addChild(sp3, 7) +-- mgr:addChild(sp4, 6) +-- mgr:addChild(sp5, 5) +-- mgr:addChild(sp6, 4) +-- mgr:addChild(sp7, 3) +-- mgr:addChild(sp8, 2) +-- mgr:addChild(sp9, 1) + +-- sp1:setVertexZ(400) +-- sp2:setVertexZ(300) +-- sp3:setVertexZ(200) +-- sp4:setVertexZ(100) +-- sp5:setVertexZ(0) +-- sp6:setVertexZ(-100) +-- sp7:setVertexZ(-200) +-- sp8:setVertexZ(-300) +-- sp9:setVertexZ(-400) + +-- sp9:setScale(2) +-- sp9:setColor(cc.c3b::YELLOW) +-- end + +-- local function title() + +-- return "Testing Z Buffer in Render Texture" +-- end + +-- local function subtitle() + +-- return "Touch screen. It should be green" +-- end + +-- local function ccTouchesBegan(cocos2d:cc.Set *touches, cocos2d:cc.Event *event) + +-- cc.SetIterator iter +-- cc.Touch *touch +-- for (iter = touches:begin() iter != touches:end() ++iter) + +-- touch = (cc.Touch *)(*iter) +-- local location = touch:getLocation() + +-- sp1:setPosition(location) +-- sp2:setPosition(location) +-- sp3:setPosition(location) +-- sp4:setPosition(location) +-- sp5:setPosition(location) +-- sp6:setPosition(location) +-- sp7:setPosition(location) +-- sp8:setPosition(location) +-- sp9:setPosition(location) +-- end +-- end + +-- local function ccTouchesMoved(cc.const std::vector& touches, cc.Event* event) + +-- cc.SetIterator iter +-- cc.Touch *touch +-- for (iter = touches:begin() iter != touches:end() ++iter) + +-- touch = (cc.Touch *)(*iter) +-- local location = touch:getLocation() + +-- sp1:setPosition(location) +-- sp2:setPosition(location) +-- sp3:setPosition(location) +-- sp4:setPosition(location) +-- sp5:setPosition(location) +-- sp6:setPosition(location) +-- sp7:setPosition(location) +-- sp8:setPosition(location) +-- sp9:setPosition(location) +-- end +-- end + +-- local function ccTouchesEnded(cc.const std::vector& touches, cc.Event* event) + +-- this:renderScreenShot() +-- end + +-- local function renderScreenShot() + +-- local texture = cc.RenderTexture:create(512, 512) +-- if (NULL == texture) + +-- return +-- end +-- texture:setAnchorPoint(cc.p(0, 0)) +-- texture:begin() + +-- this:visit() + +-- texture:end() + +-- local sprite = cc.Sprite:createWithTexture(texture:getSprite():getTexture()) + +-- sprite:setPosition(cc.p(256, 256)) +-- sprite:setOpacity(182) +-- sprite:setFlipY(1) +-- this:addChild(sprite, 999999) +-- sprite:setColor(cc.c3b::GREEN) + +-- sprite:runAction(cc.Sequence:create(cc.FadeTo:create(2, 0), +-- cc.Hide:create(), +-- NULL)) +-- end + +-- -- RenderTextureTestDepthStencil + +-- local function RenderTextureTestDepthStencil() + +-- local s = cc.Director:getInstance():getWinSize() + +-- local sprite = cc.Sprite:create("Images/fire.png") +-- sprite:setPosition(cc.p(s.width * 0.25, 0)) +-- sprite:setScale(10) +-- local rend = cc.RenderTexture:create(s.width, s.height, kcc.Texture2DPixelFormat_RGBA4444, GL_DEPTH24_STENCIL8) + +-- glStencilMask(0xFF) +-- rend:beginWithClear(0, 0, 0, 0, 0, 0) + +-- --! mark sprite quad into stencil buffer +-- glEnable(GL_STENCIL_TEST) +-- glStencilFunc(GL_ALWAYS, 1, 0xFF) +-- glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE) +-- glColorMask(0, 0, 0, 1) +-- sprite:visit() + +-- --! move sprite half width and height, and draw only where not marked +-- sprite:setPosition(cc.p__add(sprite:getPosition(), cc.p__mul(cc.p(sprite:getContentSize().width * sprite:getScale(), sprite:getContentSize().height * sprite:getScale()), 0.5))) +-- glStencilFunc(GL_NOTEQUAL, 1, 0xFF) +-- glColorMask(1, 1, 1, 1) +-- sprite:visit() + +-- rend:end() + +-- glDisable(GL_STENCIL_TEST) + +-- rend:setPosition(cc.p(s.width * 0.5, s.height * 0.5)) + +-- this:addChild(rend) +-- end + +-- local function title() + +-- return "Testing depthStencil attachment" +-- end + +-- local function subtitle() + +-- return "Circle should be missing 1/4 of its region" +-- end + +-- -- RenderTextureTest +-- local function RenderTextureTargetNode() + +-- /* +-- * 1 2 +-- * A: A1 A2 +-- * +-- * B: B1 B2 +-- * +-- * A1: premulti sprite +-- * A2: premulti render +-- * +-- * B1: non-premulti sprite +-- * B2: non-premulti render +-- */ +-- local background = cc.LayerColor:create(cc.c4b(40,40,40,255)) +-- addChild(background) + +-- -- sprite 1 +-- sprite1 = cc.Sprite:create("Images/fire.png") + +-- -- sprite 2 +-- sprite2 = cc.Sprite:create("Images/fire_rgba8888.pvr") + +-- local s = cc.Director:getInstance():getWinSize() + +-- /* Create the render texture */ +-- local renderTexture = cc.RenderTexture:create(s.width, s.height, kcc.Texture2DPixelFormat_RGBA4444) +-- this:renderTexture = renderTexture + +-- renderTexture:setPosition(cc.p(s.width/2, s.height/2)) +-- -- [renderTexture setPosition:cc.p(s.width, s.height)] +-- -- renderTexture.scale = 2 + +-- /* add the sprites to the render texture */ +-- renderTexture:addChild(sprite1) +-- renderTexture:addChild(sprite2) +-- renderTexture:setClearColor(cc.c4f(0, 0, 0, 0)) +-- renderTexture:setClearFlags(GL_COLOR_BUFFER_BIT) + +-- /* add the render texture to the scene */ +-- addChild(renderTexture) + +-- renderTexture:setAutoDraw(true) + +-- scheduleUpdate() + +-- -- Toggle clear on / off +-- local item = cc.MenuItemFont:create("Clear On/Off", this, menu_selector(RenderTextureTargetNode:touched)) +-- local menu = cc.Menu:create(item, NULL) +-- addChild(menu) + +-- menu:setPosition(cc.p(s.width/2, s.height/2)) +-- end + +-- local function touched(cc.Object* sender) + +-- if (renderTexture:getClearFlags() == 0) + +-- renderTexture:setClearFlags(GL_COLOR_BUFFER_BIT) +-- end +-- else + +-- renderTexture:setClearFlags(0) +-- renderTexture:setClearColor(cc.c4f( cc.RANDOM_0_1(), cc.RANDOM_0_1(), cc.RANDOM_0_1(), 1)) +-- end +-- end + +-- local function update(float dt) + +-- static float time = 0 +-- float r = 80 +-- sprite1:setPosition(cc.p(cosf(time * 2) * r, sinf(time * 2) * r)) +-- sprite2:setPosition(cc.p(sinf(time * 2) * r, cosf(time * 2) * r)) + +-- time += dt +-- end + +-- local function title() + +-- return "Testing Render Target Node" +-- end + +-- local function subtitle() + +-- return "Sprites should be equal and move with each frame" +-- end + +-- -- SpriteRenderTextureBug + +-- local function SimpleSprite() : rt(NULL) {} + +-- local function SimpleSprite* SpriteRenderTextureBug:SimpleSprite:create(const char* filename, const cc.rect &rect) + +-- SimpleSprite *sprite = new SimpleSprite() +-- if (sprite && sprite:initWithFile(filename, rect)) + +-- sprite:autorelease() +-- end +-- else + +-- cc._SAFE_DELETE(sprite) +-- end + +-- return sprite +-- end + +-- local function SimpleSprite:draw() + +-- if (rt == NULL) + +-- local s = cc.Director:getInstance():getWinSize() +-- rt = new cc.RenderTexture() +-- rt:initWithWidthAndHeight(s.width, s.height, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) +-- end +-- rt:beginWithClear(0.0, 0.0, 0.0, 1.0) +-- rt:end() + +-- cc._NODE_DRAW_SETUP() + +-- BlendFunc blend = getBlendFunc() +-- ccGLBlendFunc(blend.src, blend.dst) + +-- ccGLBindTexture2D(getTexture():getName()) + +-- -- +-- -- Attributes +-- -- + +-- ccGLEnableVertexAttribs(kcc.VertexAttribFlag_PosColorTex) + +-- #define kQuadSize sizeof(m_sQuad.bl) +-- long offset = (long)&m_sQuad + +-- -- vertex +-- int diff = offsetof( V3F_C4B_T2F, vertices) +-- glVertexAttribPointer(kcc.VertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff)) + +-- -- texCoods +-- diff = offsetof( V3F_C4B_T2F, texCoords) +-- glVertexAttribPointer(kcc.VertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff)) + +-- -- color +-- diff = offsetof( V3F_C4B_T2F, colors) +-- glVertexAttribPointer(kcc.VertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff)) + +-- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4) +-- end + +-- local function SpriteRenderTextureBug() + +-- setTouchEnabled(true) + +-- local s = cc.Director:getInstance():getWinSize() +-- addNewSpriteWithCoords(cc.p(s.width/2, s.height/2)) +-- end + +-- local function SimpleSprite* SpriteRenderTextureBug:addNewSpriteWithCoords(const cc.p& p) + +-- int idx = cc.RANDOM_0_1() * 1400 / 100 +-- int x = (idx%5) * 85 +-- int y = (idx/5) * 121 + +-- SpriteRenderTextureBug:SimpleSprite *sprite = SpriteRenderTextureBug:SimpleSprite:create("Images/grossini_dance_atlas.png", +-- cc.rect(x,y,85,121)) +-- addChild(sprite) + +-- sprite:setPosition(p) + +-- local action = NULL +-- float rd = cc.RANDOM_0_1() + +-- if (rd < 0.20) +-- action = cc.ScaleBy:create(3, 2) +-- else if (rd < 0.40) +-- action = cc.RotateBy:create(3, 360) +-- else if (rd < 0.60) +-- action = cc.Blink:create(1, 3) +-- else if (rd < 0.8 ) +-- action = cc.TintBy:create(2, 0, -255, -255) +-- else +-- action = cc.FadeOut:create(2) + +-- local action_back = action:reverse() +-- local seq = cc.Sequence:create(action, action_back, NULL) + +-- sprite:runAction(cc.RepeatForever:create(seq)) + +-- --return sprite +-- return NULL +-- end + +-- local function ccTouchesEnded(cc.const std::vector& touches, cc.Event* event) + +-- cc.SetIterator iter = touches:begin() +-- for( iter != touches:end() ++iter) + +-- local location = ((cc.Touch*)(*iter)):getLocation() +-- addNewSpriteWithCoords(location) +-- end +-- end + +-- local function title() + +-- return "SpriteRenderTextureBug" +-- end + +-- local function subtitle() + +-- return "Touch the screen. Sprite should appear on under the touch" +-- end + +function RenderTextureTestMain() + cclog("RenderTextureTestMain") + Helper.index = 1 + local scene = cc.Scene:create() + Helper.createFunctionTable = { + + RenderTextureSave, + -- RenderTextureIssue937, + -- RenderTextureZbuffer, + -- RenderTextureTestDepthStencil, + -- RenderTextureTargetNode, + -- SpriteRenderTextureBug + } + scene:addChild(RenderTextureSave()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/RotateWorldTest/RotateWorldTest.lua b/samples/lua-tests/src/RotateWorldTest/RotateWorldTest.lua new file mode 100644 index 0000000000..e196832723 --- /dev/null +++ b/samples/lua-tests/src/RotateWorldTest/RotateWorldTest.lua @@ -0,0 +1,123 @@ + +local size = cc.Director:getInstance():getWinSize() + +local function CreateSpriteLayer() + local layer = cc.Layer:create() + + local x, y + x = size.width + y = size.height + + local sprite = cc.Sprite:create(s_pPathGrossini) + local spriteSister1 = cc.Sprite:create(s_pPathSister1) + local spriteSister2 = cc.Sprite:create(s_pPathSister2) + + sprite:setScale(1.5) + spriteSister1:setScale(1.5) + spriteSister2:setScale(1.5) + + sprite:setPosition(cc.p(x / 2, y / 2)) + spriteSister1:setPosition(cc.p(40, y / 2)) + spriteSister2:setPosition(cc.p(x - 40, y / 2)) + + layer:addChild(sprite) + layer:addChild(spriteSister1) + layer:addChild(spriteSister2) + + local rot = cc.RotateBy:create(16, -3600) + sprite:runAction(rot) + + local jump1 = cc.JumpBy:create(4, cc.p(-400, 0), 100, 4) + local jump2 = jump1:reverse() + + local rot1 = cc.RotateBy:create(4, 360 * 2) + local rot2 = rot1:reverse() + + local jump3 = cc.JumpBy:create(4, cc.p(-400, 0), 100, 4) + local jump4 = jump3:reverse() + local rot3 = cc.RotateBy:create(4, 360 * 2) + local rot4 = rot3:reverse() + + spriteSister1:runAction(cc.Repeat:create(cc.Sequence:create(jump2, jump1), 5)) + spriteSister2:runAction(cc.Repeat:create(cc.Sequence:create(jump3, jump4), 5)) + + spriteSister1:runAction(cc.Repeat:create(cc.Sequence:create(rot1, rot2), 5)) + spriteSister2:runAction(cc.Repeat:create(cc.Sequence:create(rot4, rot3), 5)) + + return layer +end + +local function CreateTestLayer() + local layer = cc.Layer:create() + + local x, y + x = size.width + y = size.height + + local label = cc.LabelTTF:create("cocos2d", "Tahoma", 64) + label:setPosition(x / 2, y / 2) + layer:addChild(label) + + return layer +end + +local function CreateRotateWorldLayer() + local layer = cc.Layer:create() + + local x, y + x = size.width + y = size.height + + local blue = cc.LayerColor:create(cc.c4b(0,0,255,255)) + local red = cc.LayerColor:create(cc.c4b(255,0,0,255)) + local green = cc.LayerColor:create(cc.c4b(0,255,0,255)) + local white = cc.LayerColor:create(cc.c4b(255,255,255,255)) + + blue:setScale(0.5) + blue:setPosition(cc.p(- x / 4, - y / 4)) + blue:addChild(CreateSpriteLayer()) + + red:setScale(0.5) + red:setPosition(cc.p(x / 4, - y / 4)) + + green:setScale(0.5) + green:setPosition(cc.p(- x / 4, y / 4)) + green:addChild(CreateTestLayer()) + + white:setScale(0.5) + white:setPosition(cc.p(x / 4, y / 4)) + white:ignoreAnchorPointForPosition(false) + white:setPosition(cc.p(x / 4 * 3, y / 4 * 3)) + + layer:addChild(blue, -1) + layer:addChild(white) + layer:addChild(green) + layer:addChild(red) + + local rot = cc.RotateBy:create(8, 720) + local rot1 = cc.RotateBy:create(8, 720) + local rot2 = cc.RotateBy:create(8, 720) + local rot3 = cc.RotateBy:create(8, 720) + + blue:runAction(rot) + red:runAction(rot1) + green:runAction(rot2) + white:runAction(rot3) + + return layer +end + +-------------------------------- +-- Rotate World Test +-------------------------------- +function RotateWorldTest() + cclog("RotateWorldTest") + local scene = cc.Scene:create() + local layer = CreateRotateWorldLayer() + + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + scene:runAction(cc.RotateBy:create(4, -360)) + + return scene +end diff --git a/samples/lua-tests/src/SceneTest/SceneTest.lua b/samples/lua-tests/src/SceneTest/SceneTest.lua new file mode 100644 index 0000000000..6621cc1797 --- /dev/null +++ b/samples/lua-tests/src/SceneTest/SceneTest.lua @@ -0,0 +1,180 @@ +local MID_PUSHSCENE = 100 +local MID_PUSHSCENETRAN = 101 +local MID_QUIT = 102 +local MID_REPLACESCENE = 103 +local MID_REPLACESCENETRAN = 104 +local MID_GOBACK = 105 + +local SceneTestLayer1 = nil +local SceneTestLayer2 = nil +local SceneTestLayer3 = nil + +-------------------------------------------------------------------- +-- +-- SceneTestLayer1 +-- +-------------------------------------------------------------------- + +SceneTestLayer1 = function() + local ret = cc.Layer:create() + + local function onPushScene(tag, pSender) + local scene = cc.Scene:create() + local layer = SceneTestLayer2() + scene:addChild(layer, 0) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():pushScene( scene ) + end + + local function onPushSceneTran(tag, pSender) + local scene = cc.Scene:create() + local layer = SceneTestLayer2() + scene:addChild(layer, 0) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():pushScene( cc.TransitionSlideInT:create(1, scene) ) + end + + + local function onQuit(tag, pSender) + cclog("onQuit") + end + + local item1 = cc.MenuItemFont:create( "Test pushScene") + item1:registerScriptTapHandler(onPushScene) + local item2 = cc.MenuItemFont:create( "Test pushScene w/transition") + item2:registerScriptTapHandler(onPushSceneTran) + local item3 = cc.MenuItemFont:create( "Quit") + item3:registerScriptTapHandler(onQuit) + + local menu = cc.Menu:create(item1, item2, item3) + menu:alignItemsVertically() + + ret:addChild( menu ) + + local s = cc.Director:getInstance():getWinSize() + local sprite = cc.Sprite:create(s_pPathGrossini) + ret:addChild(sprite) + sprite:setPosition( cc.p(s.width-40, s.height/2) ) + local rotate = cc.RotateBy:create(2, 360) + local repeatAction = cc.RepeatForever:create(rotate) + sprite:runAction(repeatAction) + + local function onNodeEvent(event) + if event == "enter" then + cclog("SceneTestLayer1#onEnter") + elseif event == "enterTransitionFinish" then + cclog("SceneTestLayer1#onEnterTransitionDidFinish") + end + end + + ret:registerScriptHandler(onNodeEvent) + return ret +end + +-------------------------------------------------------------------- +-- +-- SceneTestLayer2 +-- +-------------------------------------------------------------------- + +SceneTestLayer2 = function() + local ret = cc.Layer:create() + local m_timeCounter = 0 + + local function onGoBack(tag, pSender) + cc.Director:getInstance():popScene() + end + + local function onReplaceScene(tag, pSender) + local scene = cc.Scene:create() + local layer = SceneTestLayer3() + scene:addChild(layer, 0) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene( scene ) + end + + + local function onReplaceSceneTran(tag, pSender) + local scene = cc.Scene:create() + local layer = SceneTestLayer3() + scene:addChild(layer, 0) + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene( cc.TransitionFlipX:create(2, scene) ) + end + + local item1 = cc.MenuItemFont:create( "replaceScene") + item1:registerScriptTapHandler(onReplaceScene) + local item2 = cc.MenuItemFont:create( "replaceScene w/transition") + item2:registerScriptTapHandler(onReplaceSceneTran) + local item3 = cc.MenuItemFont:create( "Go Back") + item3:registerScriptTapHandler(onGoBack) + local menu = cc.Menu:create(item1, item2, item3) + menu:alignItemsVertically() + + ret:addChild( menu ) + + local s = cc.Director:getInstance():getWinSize() + local sprite = cc.Sprite:create(s_pPathGrossini) + ret:addChild(sprite) + sprite:setPosition( cc.p(s.width-40, s.height/2) ) + local rotate = cc.RotateBy:create(2, 360) + local repeat_action = cc.RepeatForever:create(rotate) + sprite:runAction(repeat_action) + + return ret +end + +-------------------------------------------------------------------- +-- +-- SceneTestLayer3 +-- +-------------------------------------------------------------------- + +SceneTestLayer3 = function() + local ret = cc.LayerColor:create(cc.c4b(0,0,255,255)) + local s = cc.Director:getInstance():getWinSize() + + local function item0Clicked(tag, pSender) + local newScene = cc.Scene:create() + newScene:addChild(SceneTestLayer3()) + cc.Director:getInstance():pushScene(cc.TransitionFade:create(0.5, newScene, cc.c3b(0,255,255))) + end + + local function item1Clicked(tag, pSender) + cc.Director:getInstance():popScene() + end + + local function item2Clicked(tag, pSender) + cc.Director:getInstance():popToRootScene() + end + + local item0 = cc.MenuItemFont:create("Touch to pushScene (self)") + item0:registerScriptTapHandler(item0Clicked) + local item1 = cc.MenuItemFont:create("Touch to popScene") + item1:registerScriptTapHandler(item1Clicked) + local item2 = cc.MenuItemFont:create("Touch to popToRootScene") + item2:registerScriptTapHandler(item2Clicked) + + local menu = cc.Menu:create(item0, item1, item2) + ret:addChild(menu) + menu:alignItemsVertically() + + local sprite = cc.Sprite:create(s_pPathGrossini) + ret:addChild(sprite) + sprite:setPosition( cc.p(s.width/2, 40) ) + local rotate = cc.RotateBy:create(2, 360) + local repeatAction = cc.RepeatForever:create(rotate) + sprite:runAction(repeatAction) + return ret +end + + + +function SceneTestMain() + cclog("SceneTestMain") + local scene = cc.Scene:create() + local layer = SceneTestLayer1() + scene:addChild(layer, 0) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/SpineTest/SpineTest.lua b/samples/lua-tests/src/SpineTest/SpineTest.lua new file mode 100644 index 0000000000..3e93a7c975 --- /dev/null +++ b/samples/lua-tests/src/SpineTest/SpineTest.lua @@ -0,0 +1,82 @@ +local SpineTestLayer1 = nil + + +-------------------------------------------------------------------- +-- +-- SpineTestLayer1 +-- +-------------------------------------------------------------------- + +SpineTestLayer1 = function() + local ret = cc.Layer:create() + + local s = cc.Director:getInstance():getWinSize() + local spineboy + + local addSpineBoy = function() + spineboy = sp.SkeletonAnimation:create(s_pPathSpineBoyJson, s_pPathSpineBoyAtlas, 0.8) + ret:addChild(spineboy) + spineboy:setPosition(cc.p(s.width/2, 20)) + spineboy:setMix("walk", "jump", 0.2) + spineboy:setMix("jump", "walk", 0.4) + spineboy:setTimeScale(0.3) + spineboy:setDebugBones(true) + spineboy:setAnimation(0, 'walk', false) + spineboy:addAnimation(0, 'jump', false) + spineboy:addAnimation(0, 'walk', true) + spineboy:addAnimation(0, 'jump', true, 4) + + spineboy:registerSpineEventHandler(function(event) + if event.type == 'start' then + print(string.format("[spine] %d start: %s", + event.trackIndex, + event.animation)) + elseif event.type == 'end' then + print(string.format("[spine] %d end: %s", + event.trackIndex, + event.animation)) + elseif event.type == 'complete' then + print(string.format("[spine] %d complete: %s, %d", + event.trackIndex, + event.animation, + event.loopCount)) + elseif event.type == 'event' then + print(string.format("[spine] %d event: %s, %s: %d, %f, %s", + event.trackIndex, + event.animation, + event.eventData.name, + event.eventData.intValue, + event.eventData.floatValue, + event.eventData.stringValue)) + end + end) + + spineboy:runAction(cc.RepeatForever:create(cc.Sequence:create(cc.FadeOut:create(1), + cc.FadeIn:create(1), + cc.DelayTime:create(5)))) + + + end + + local function onNodeEvent(event) + if event == "enter" then + cclog("SpineTestLayer1#onEnter") + addSpineBoy() + elseif event == "enterTransitionFinish" then + cclog("SceneTestLayer1#onEnterTransitionDidFinish") + end + end + + ret:registerScriptHandler(onNodeEvent) + return ret +end + + +function SpineTestMain() + cclog("SpineTestMain") + local scene = cc.Scene:create() + local layer = SpineTestLayer1() + scene:addChild(layer, 0) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/SpriteTest/SpriteTest.lua b/samples/lua-tests/src/SpriteTest/SpriteTest.lua new file mode 100644 index 0000000000..29c4dae6aa --- /dev/null +++ b/samples/lua-tests/src/SpriteTest/SpriteTest.lua @@ -0,0 +1,1588 @@ +local size = cc.Director:getInstance():getWinSize() +local kTagTileMap = 1 +local kTagSpriteBatchNode = 1 +local kTagNode = 2 +local kTagAnimation1 = 1 +local kTagSpriteLeft = 2 +local kTagSpriteRight = 2 + +local kTagSprite1 = 0 +local kTagSprite2 = 1 +local kTagSprite3 = 2 +local kTagSprite4 = 3 +local kTagSprite5 = 4 +local kTagSprite6 = 5 +local kTagSprite7 = 6 +local kTagSprite8 = 7 + +local testLayer = nil +local entry = nil + +-------------------------------------------- +---- test sprite1 +-------------------------------------------- +local Sprite1 = {} +Sprite1.__index = Sprite1 + +function Sprite1.addNewSpriteWithCoords(layer, point) + local idx = math.floor(math.random() * 1400 / 100) + local x = math.floor(math.mod(idx,5) * 85) + local y = math.floor(idx / 5 * 121) + + local sprite = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(x,y,85,121) ) + layer:addChild( sprite ) + + sprite:setPosition( cc.p(point.x, point.y) ) + + local action = nil + local random = math.random() + cclog("random = " .. random) + if( random < 0.20 ) then + action = cc.ScaleBy:create(3, 2) + elseif(random < 0.40) then + action = cc.RotateBy:create(3, 360) + elseif( random < 0.60) then + action = cc.Blink:create(1, 3) + elseif( random < 0.8 ) then + action = cc.TintBy:create(2, 0, -255, -255) + else + action = cc.FadeOut:create(2) + end + + local action_back = action:reverse() + local seq = cc.Sequence:create( action, action_back) + + sprite:runAction( cc.RepeatForever:create(seq) ) +end + +function Sprite1.onTouchesEnd(touches, event) + for i = 1,table.getn(touches) do + local location = touches[i]:getLocation() + Sprite1.addNewSpriteWithCoords(Helper.currentLayer, location) + end +end + +function Sprite1.create() + cclog("sprite1") + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + Sprite1.addNewSpriteWithCoords(layer, cc.p(size.width/2, size.height/2)) + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(Sprite1.onTouchesEnd,cc.Handler.EVENT_TOUCHES_ENDED ) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + Helper.titleLabel:setString("Sprite (tap screen)") + + return layer +end + +-------------------------------------------- +---- test SpriteBatchNode +-------------------------------------------- +local SpriteBatchNode1 = {} +SpriteBatchNode1.__index = SpriteBatchNode1 + +function SpriteBatchNode1.addNewSpriteWithCoords(layer, point) + local BatchNode = layer:getChildByTag( kTagSpriteBatchNode ) + local idx = math.floor(math.random() * 1400 / 100) + local x = math.floor(math.mod(idx,5) * 85) + local y = math.floor(idx / 5 * 121) + + local sprite = cc.Sprite:createWithTexture(BatchNode:getTexture(), cc.rect(x,y,85,121) ) + layer:addChild( sprite ) + + sprite:setPosition( cc.p(point.x, point.y) ) + + local action = nil + local random = math.random() + cclog("random = " .. random) + if( random < 0.20 ) then + action = cc.ScaleBy:create(3, 2) + elseif(random < 0.40) then + action = cc.RotateBy:create(3, 360) + elseif( random < 0.60) then + action = cc.Blink:create(1, 3) + elseif( random < 0.8 ) then + action = cc.TintBy:create(2, 0, -255, -255) + else + action = cc.FadeOut:create(2) + end + + local action_back = action:reverse() + local seq = cc.Sequence:create( action, action_back) + + sprite:runAction( cc.RepeatForever:create(seq) ) +end + +function SpriteBatchNode1.onTouchesEnd(touches,event) + for i = 1,table.getn(touches) do + local location = touches[i]:getLocation() + SpriteBatchNode1.addNewSpriteWithCoords(Helper.currentLayer, location) + end +end + +function SpriteBatchNode1.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + local BatchNode = cc.SpriteBatchNode:create("Images/grossini_dance_atlas.png", 50) + layer:addChild(BatchNode, 0, kTagSpriteBatchNode) + + SpriteBatchNode1.addNewSpriteWithCoords(layer, cc.p(size.width/2, size.height/2)) + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(Sprite1.onTouchesEnd,cc.Handler.EVENT_TOUCHES_ENDED ) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + Helper.titleLabel:setString("SpriteBatchNode (tap screen)") + + return layer +end + +-------------------------------------- +---- SpriteColorOpacity +-------------------------------------- +local SpriteColorOpacity = {} +SpriteColorOpacity.__index = SpriteColorOpacity +SpriteColorOpacity.entry = nil + +function SpriteColorOpacity.setLayerSprite(layer) + local sprite1 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*0, 121*1, 85, 121)) + local sprite2 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*1, 121*1, 85, 121)) + local sprite3 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*2, 121*1, 85, 121)) + local sprite4 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*3, 121*1, 85, 121)) + + local sprite5 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*0, 121*1, 85, 121)) + local sprite6 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*1, 121*1, 85, 121)) + local sprite7 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*2, 121*1, 85, 121)) + local sprite8 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*3, 121*1, 85, 121)) + + local s = cc.Director:getInstance():getWinSize() + sprite1:setPosition( cc.p( (s.width/5)*1, (s.height/3)*1) ) + sprite2:setPosition( cc.p( (s.width/5)*2, (s.height/3)*1) ) + sprite3:setPosition( cc.p( (s.width/5)*3, (s.height/3)*1) ) + sprite4:setPosition( cc.p( (s.width/5)*4, (s.height/3)*1) ) + sprite5:setPosition( cc.p( (s.width/5)*1, (s.height/3)*2) ) + sprite6:setPosition( cc.p( (s.width/5)*2, (s.height/3)*2) ) + sprite7:setPosition( cc.p( (s.width/5)*3, (s.height/3)*2) ) + sprite8:setPosition( cc.p( (s.width/5)*4, (s.height/3)*2) ) + + local action = cc.FadeIn:create(2) + local action_back = action:reverse() + local fade = cc.RepeatForever:create( cc.Sequence:create( action, action_back) ) + + local tintred = cc.TintBy:create(2, 0, -255, -255) + local tintred_back = tintred:reverse() + local red = cc.RepeatForever:create( cc.Sequence:create( tintred, tintred_back) ) + + local tintgreen = cc.TintBy:create(2, -255, 0, -255) + local tintgreen_back = tintgreen:reverse() + local green = cc.RepeatForever:create( cc.Sequence:create( tintgreen, tintgreen_back) ) + + local tintblue = cc.TintBy:create(2, -255, -255, 0) + local tintblue_back = tintblue:reverse() + local blue = cc.RepeatForever:create( cc.Sequence:create( tintblue, tintblue_back) ) + + sprite5:runAction(red) + sprite6:runAction(green) + sprite7:runAction(blue) + sprite8:runAction(fade) + + layer:addChild(sprite1, 0, kTagSprite1) + layer:addChild(sprite2, 0, kTagSprite2) + layer:addChild(sprite3, 0, kTagSprite3) + layer:addChild(sprite4, 0, kTagSprite4) + layer:addChild(sprite5, 0, kTagSprite5) + layer:addChild(sprite6, 0, kTagSprite6) + layer:addChild(sprite7, 0, kTagSprite7) + layer:addChild(sprite8, 0, kTagSprite8) + + layer:registerScriptHandler(SpriteColorOpacity.onEnterOrExit) +end + +function SpriteColorOpacity.onEnterOrExit(tag) + if tag == "enter" then + SpriteColorOpacity.onEnter() + elseif tag == "exit" then + SpriteColorOpacity.onExit() + end +end + +function SpriteColorOpacity.removeAndAddSprite(dt) + local sprite = Helper.currentLayer:getChildByTag(kTagSprite5) + sprite:retain() + + Helper.currentLayer:removeChild(sprite, false) + Helper.currentLayer:addChild(sprite, 0, kTagSprite5) + + sprite:release() +end + +function SpriteColorOpacity.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + SpriteColorOpacity.setLayerSprite(layer) + layer:registerScriptHandler(SpriteColorOpacity.onEnterOrExit) + Helper.titleLabel:setString("Sprite: Color & Opacity") + + return layer +end + +-------------------------------------- +---- SpriteFrameTest +-------------------------------------- +local SpriteFrameTest = {} +SpriteFrameTest.__index = SpriteFrameTest +SpriteFrameTest.entry = nil +SpriteFrameTest.m_pSprite1 = nil +SpriteFrameTest.m_pSprite2 = nil +SpriteFrameTest.m_nCounter = 0 + +function SpriteFrameTest.onEnter() + local s = cc.Director:getInstance():getWinSize() + local cache = cc.SpriteFrameCache:getInstance() + + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + cache:addSpriteFrames("animations/grossini_blue.plist", "animations/grossini_blue.png") + + SpriteFrameTest.m_pSprite1 = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + SpriteFrameTest.m_pSprite1:setPosition( cc.p( s.width/2-80, s.height/2) ) + + local spritebatch = cc.SpriteBatchNode:create("animations/grossini.png") + spritebatch:addChild(SpriteFrameTest.m_pSprite1) + Helper.currentLayer:addChild(spritebatch) + + local animFrames = {} + for i = 1,14 do + local frame = cache:getSpriteFrame( string.format("grossini_dance_%02d.png", i) ) + animFrames[i] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + SpriteFrameTest.m_pSprite1:runAction( cc.RepeatForever:create( cc.Animate:create(animation) ) ) + + SpriteFrameTest.m_pSprite1:setFlipX(false) + SpriteFrameTest.m_pSprite1:setFlipY(false) + + SpriteFrameTest.m_pSprite2 = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + SpriteFrameTest.m_pSprite2:setPosition( cc.p( s.width/2 + 80, s.height/2) ) + Helper.currentLayer:addChild(SpriteFrameTest.m_pSprite2) + + local moreFrames = {} + for i = 1,14 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_gray_%02d.png",i)) + moreFrames[i] = frame + end + + for i = 1,4 do + local frame = cache:getSpriteFrame(string.format("grossini_blue_%02d.png",i)) + moreFrames[14 + i] = frame + end + + --contact + for i = 1, 14 do + moreFrames[18 + i] = animFrames[i] + end + + + local animMixed = cc.Animation:createWithSpriteFrames(moreFrames, 0.3) + + SpriteFrameTest.m_pSprite2:runAction(cc.RepeatForever:create( cc.Animate:create(animMixed) ) ) + + SpriteFrameTest.m_pSprite2:setFlipX(false) + SpriteFrameTest.m_pSprite2:setFlipY(false) + + + performWithDelay(Helper.currentLayer,SpriteFrameTest.startIn05Secs, 0.5) + SpriteFrameTest.m_nCounter = 0 +end + +function SpriteFrameTest.onExit() + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + cache:removeSpriteFramesFromFile("animations/grossini_blue.plist") +end + +function SpriteFrameTest.startIn05Secs(dt) + schedule(Helper.currentLayer,SpriteFrameTest.flipSprites, 1) +end + +function SpriteFrameTest.flipSprites(dt) + SpriteFrameTest.m_nCounter = SpriteFrameTest.m_nCounter + 1 + + local fx = false + local fy = false + local i = math.mod(SpriteFrameTest.m_nCounter, 4) + + if(i == 0) then + fx = false + fy = false + elseif i == 1 then + fx = true + fy = false + elseif i == 2 then + fx = false + fy = true + elseif i == 3 then + fx = true + fy = true + end + + SpriteFrameTest.m_pSprite1:setFlipX(fx) + SpriteFrameTest.m_pSprite1:setFlipY(fy) + SpriteFrameTest.m_pSprite2:setFlipX(fx) + SpriteFrameTest.m_pSprite2:setFlipY(fy) +end + + +function SpriteFrameTest.onEnterOrExit(tag) + if tag == "enter" then + SpriteFrameTest.onEnter() + elseif tag == "exit" then + SpriteFrameTest.onExit() + end +end + +function SpriteFrameTest.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + layer:registerScriptHandler(SpriteFrameTest.onEnterOrExit) + Helper.titleLabel:setString("Sprite vs. SpriteBatchNode animation") + + return layer +end + +--------------------------------------------------------------------- +-- +-- SpriteFrameAliasNameTest +-- +-------------------------------------------------------------------- +local SpriteFrameAliasNameTest = {} +SpriteFrameAliasNameTest.__index = SpriteFrameAliasNameTest + +function SpriteFrameAliasNameTest.onEnter() + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini-aliases.plist", "animations/grossini-aliases.png") + + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width * 0.5, s.height * 0.5)) + + local spriteBatch = cc.SpriteBatchNode:create("animations/grossini-aliases.png") + cclog("spriteBatch = " .. tostring(tolua.isnull(spriteBatch))) + cclog("sprite = " .. tostring(tolua.isnull(sprite))) + spriteBatch:addChild(sprite) + Helper.currentLayer:addChild(spriteBatch) + + local animFrames = {} + for i = 1,14 do + local frame = cache:getSpriteFrame(string.format("dance_%02d", i)) + animFrames[i] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + -- 14 frames * 1sec = 14 seconds + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) +end + +function SpriteFrameAliasNameTest.onExit() + cc.SpriteFrameCache:getInstance():removeSpriteFramesFromFile("animations/grossini-aliases.plist") +end + +function SpriteFrameAliasNameTest.onEnterOrExit(tag) + if tag == "enter" then + SpriteFrameAliasNameTest.onEnter() + elseif tag == "exit" then + SpriteFrameAliasNameTest.onExit() + end +end + +function SpriteFrameAliasNameTest.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + layer:registerScriptHandler(SpriteFrameAliasNameTest.onEnterOrExit) + Helper.titleLabel:setString("SpriteFrame Alias Name") + Helper.subtitleLabel:setString("SpriteFrames are obtained using the alias name") + + return layer +end + +--------------------------------------------------------------------- +-- +-- SpriteAnchorPoint +-- +-------------------------------------------------------------------- +local SpriteAnchorPoint = {} +SpriteAnchorPoint.__index = SpriteAnchorPoint + +function SpriteAnchorPoint.initLayer(layer) + local s = cc.Director:getInstance():getWinSize() + + local rotate = cc.RotateBy:create(10, 360) + local action = cc.RepeatForever:create(rotate) + + for i = 0, 2 do + local sprite = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*i, 121*1, 85, 121) ) + sprite:setPosition( cc.p( s.width/4*(i+1), s.height/2) ) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale( 0.25 ) + point:setPosition( sprite:getPosition() ) + layer:addChild(point, 10) + + if i == 0 then + sprite:setAnchorPoint( cc.p(0, 0) ) + elseif i == 1 then + sprite:setAnchorPoint( cc.p(0.5, 0.5) ) + elseif i == 2 then + sprite:setAnchorPoint( cc.p(1,1) ) + end + point:setPosition( sprite:getPosition() ) + + local copy = tolua.cast(action:clone(), "cc.Action") + sprite:runAction(copy) + layer:addChild(sprite, i) + end + + return layer +end + +function SpriteAnchorPoint.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + layer = SpriteAnchorPoint.initLayer(layer) + Helper.titleLabel:setString("Sprite: anchor point") + Helper.subtitleLabel:setString("") + return layer +end + +-------------------------------------------------------------------- +-- SpriteBatchNodeAnchorPoint +-------------------------------------------------------------------- +local SpriteBatchNodeAnchorPoint = {} +SpriteBatchNodeAnchorPoint.__index = SpriteBatchNodeAnchorPoint + +function SpriteBatchNodeAnchorPoint.initLayer(layer) + -- small capacity. Testing resizing. + -- Don't use capacity=1 in your real game. It is expensive to resize the capacity + local batch = cc.SpriteBatchNode:create("Images/grossini_dance_atlas.png", 1) + layer:addChild(batch, 0, kTagSpriteBatchNode) + + local s = cc.Director:getInstance():getWinSize() + + local rotate = cc.RotateBy:create(10, 360) + local action = cc.RepeatForever:create(rotate) + for i=0,2 do + local sprite = cc.Sprite:createWithTexture(batch:getTexture(), cc.rect(85*i, 121*1, 85, 121)) + sprite:setPosition( cc.p( s.width/4*(i+1), s.height/2) ) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale( 0.25 ) + point:setPosition( cc.p(sprite:getPosition()) ) + layer:addChild(point, 1) + + if i == 0 then + sprite:setAnchorPoint( cc.p(0,0) ) + elseif i == 1 then + sprite:setAnchorPoint( cc.p(0.5, 0.5) ) + elseif i == 2 then + sprite:setAnchorPoint( cc.p(1,1) ) + end + + point:setPosition( cc.p(sprite:getPosition()) ) + + local copy = tolua.cast(action:clone(), "cc.Action") + sprite:runAction(copy) + batch:addChild(sprite, i) + end + + return layer +end + +function SpriteBatchNodeAnchorPoint.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + + layer = SpriteBatchNodeAnchorPoint.initLayer(layer) + Helper.titleLabel:setString("SpriteBatchNode: anchor point") + Helper.subtitleLabel:setString("") + + return layer +end + +-------------------------------------------------------------------- +-- +-- SpriteOffsetAnchorRotation +-- +-------------------------------------------------------------------- +local SpriteOffsetAnchorRotation = {} +SpriteOffsetAnchorRotation.__index = SpriteOffsetAnchorRotation + +function SpriteOffsetAnchorRotation.initLayer(layer) + local s = cc.Director:getInstance():getWinSize() + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + for i=0,2 do + -- + -- Animation using Sprite batch + -- + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p( s.width/4*(i+1), s.height/2)) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale( 0.25 ) + point:setPosition( sprite:getPosition() ) + layer:addChild(point, 1) + + if i == 0 then + sprite:setAnchorPoint( cc.p(0, 0) ) + elseif i == 1 then + sprite:setAnchorPoint( cc.p(0.5, 0.5) ) + elseif i == 2 then + sprite:setAnchorPoint( cc.p(1,1) ) + end + + point:setPosition( cc.p(sprite:getPosition()) ) + + local animFrames = {} + + for i = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png",(i+1))) + animFrames[i + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create( cc.Animate:create(animation) ) ) + sprite:runAction(cc.RepeatForever:create(cc.RotateBy:create(10, 360) ) ) + + layer:addChild(sprite, 0) + end + + return layer +end + +function SpriteOffsetAnchorRotation.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteOffsetAnchorRotation.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteOffsetAnchorRotation.eventHandler) + Helper.initWithLayer(layer) + + layer = SpriteOffsetAnchorRotation.initLayer(layer) + Helper.titleLabel:setString("Sprite offset + anchor + rot") + Helper.subtitleLabel:setString("") + + return layer +end + +-------------------------------------------------------------------- +-- +-- SpriteBatchNodeOffsetAnchorRotation +-- +-------------------------------------------------------------------- +local SpriteBatchNodeOffsetAnchorRotation = {} +SpriteBatchNodeOffsetAnchorRotation.__index = SpriteBatchNodeOffsetAnchorRotation + +function SpriteBatchNodeOffsetAnchorRotation.initLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + local spritebatch = cc.SpriteBatchNode:create("animations/grossini.png") + cclog("1") + layer:addChild(spritebatch) + + cclog("2") + for i = 0,2 do + -- + -- Animation using Sprite BatchNode + -- + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition( cc.p( s.width/4*(i+1), s.height/2)) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale( 0.25 ) + point:setPosition( cc.p(sprite:getPosition()) ) + layer:addChild(point, 200) + + if i == 0 then + sprite:setAnchorPoint( cc.p(0,0) ) + elseif i == 1 then + sprite:setAnchorPoint( cc.p(0.5, 0.5) ) + elseif i == 2 then + sprite:setAnchorPoint( cc.p(1,1) ) + end + + point:setPosition( cc.p(sprite:getPosition()) ) + + local animFrames = {} + for k = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png",(k+1))) + animFrames[k + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create( cc.Animate:create(animation) )) + sprite:runAction(cc.RepeatForever:create(cc.RotateBy:create(10, 360) )) + spritebatch:addChild(sprite, i) + end + + return layer +end + +function SpriteBatchNodeOffsetAnchorRotation.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteBatchNodeOffsetAnchorRotation.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteBatchNodeOffsetAnchorRotation.eventHandler) + Helper.initWithLayer(layer) + + layer = SpriteBatchNodeOffsetAnchorRotation.initLayer(layer) + Helper.titleLabel:setString("SpriteBatchNode offset + anchor + rot") + Helper.subtitleLabel:setString("") + + return layer +end + +-------------------------------------------------------------------- +-- +-- SpriteOffsetAnchorScale +-- +-------------------------------------------------------------------- +local SpriteOffsetAnchorScale = {} +SpriteOffsetAnchorScale.__index = SpriteOffsetAnchorScale + +function SpriteOffsetAnchorScale.initLayer(layer) + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + for i = 0,2 do + -- + -- Animation using Sprite BatchNode + -- + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition( cc.p( s.width/4*(i+1), s.height/2) ) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale( 0.25 ) + point:setPosition( sprite:getPosition() ) + layer:addChild(point, 1) + + if i == 0 then + sprite:setAnchorPoint( cc.p(0, 0) ) + elseif i == 1 then + sprite:setAnchorPoint( cc.p(0.5, 0.5) ) + elseif i == 2 then + sprite:setAnchorPoint( cc.p(1,1) ) + end + + point:setPosition( cc.p(sprite:getPosition()) ) + + local animFrames = {} + + for i = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png",(i+1))) + animFrames[i + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create( cc.Animate:create(animation) )) + + local scale = cc.ScaleBy:create(2, 2) + local scale_back = scale:reverse() + local seq_scale = cc.Sequence:create(scale, scale_back) + sprite:runAction(cc.RepeatForever:create(seq_scale)) + + layer:addChild(sprite, 0) + end + + return layer +end + +function SpriteOffsetAnchorScale.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteOffsetAnchorScale.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteOffsetAnchorScale.eventHandler) + Helper.initWithLayer(layer) + + layer = SpriteOffsetAnchorScale.initLayer(layer) + Helper.titleLabel:setString("Sprite offset + anchor + scale") + Helper.subtitleLabel:setString("") + + return layer +end + +-------------------------------------------------------------------- +-- +-- SpriteBatchNodeOffsetAnchorScale +-- +-------------------------------------------------------------------- +local SpriteBatchNodeOffsetAnchorScale = {} + +function SpriteBatchNodeOffsetAnchorScale.initLayer(layer) + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + local spritesheet = cc.SpriteBatchNode:create("animations/grossini.png") + layer:addChild(spritesheet) + + for i = 0,2 do + -- Animation using Sprite BatchNode + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width/4*(i+1), s.height/2)) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale(0.25) + point:setPosition(sprite:getPosition()) + layer:addChild(point, 200) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0,0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + else + sprite:setAnchorPoint(cc.p(1, 1)) + end + + point:setPosition(sprite:getPosition()) + + local animFrames = {} + local str + for k = 0, 13 do + str = string.format("grossini_dance_%02d.png", (k+1)) + local frame = cache:getSpriteFrame(str) + animFrames[k + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + local scale = cc.ScaleBy:create(2, 2) + local scale_back = scale:reverse() + local seq_scale = cc.Sequence:create(scale, scale_back) + sprite:runAction(cc.RepeatForever:create(seq_scale)) + + spritesheet:addChild(sprite, i) + end + + return layer +end + +function SpriteBatchNodeOffsetAnchorScale.onExit() + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") +end + +function SpriteBatchNodeOffsetAnchorScale.eventHandler(tag) + if tag == "exit" then + SpriteBatchNodeOffsetAnchorScale.onExit() + end +end + +function SpriteBatchNodeOffsetAnchorScale.create() + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + layer:registerScriptHandler(SpriteBatchNodeOffsetAnchorScale.eventHandler) + + layer = SpriteBatchNodeOffsetAnchorScale.initLayer(layer) + Helper.titleLabel:setString("SpriteBatchNode offset + anchor + scale") + Helper.subtitleLabel:setString("") + + return layer +end + +-------------------------------------------------------------------- +-- +-- SpriteOffsetAnchorSkew +-- +-------------------------------------------------------------------- +local SpriteOffsetAnchorSkew = {} +SpriteOffsetAnchorSkew.__index = SpriteOffsetAnchorSkew + +function SpriteOffsetAnchorSkew.initLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + for i = 0, 2 do + -- + -- Animation using Sprite batch + -- + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width / 4 * (i + 1), s.height / 2)) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale(0.25) + point:setPosition(sprite:getPosition()) + layer:addChild(point, 1) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0, 0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + elseif i == 2 then + sprite:setAnchorPoint(cc.p(1, 1)) + end + + point:setPosition(sprite:getPosition()) + + local animFrames = {} + for j = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png", j + 1)) + animFrames[j + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + local skewX = cc.SkewBy:create(2, 45, 0) + local skewX_back = skewX:reverse() + local skewY = cc.SkewBy:create(2, 0, 45) + local skewY_back = skewY:reverse() + + local seq_skew = cc.Sequence:create(skewX, skewX_back, skewY, skewY_back) + sprite:runAction(cc.RepeatForever:create(seq_skew)) + + layer:addChild(sprite, 0) + end + + return layer +end + +function SpriteOffsetAnchorSkew.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteOffsetAnchorSkew.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteOffsetAnchorSkew.eventHandler) + Helper.initWithLayer(layer) + + layer = SpriteOffsetAnchorSkew.initLayer(layer) + Helper.titleLabel:setString("SpriteBatchNode offset + anchor + scale") + Helper.subtitleLabel:setString("") + + return layer +end +------------------------------------------------------------ +-- SpriteOffsetAnchorRotationalSkew +------------------------------------------------------------ +local SpriteOffsetAnchorRotationalSkew = {} +SpriteOffsetAnchorRotationalSkew.__index = SpriteOffsetAnchorRotationalSkew + +function SpriteOffsetAnchorRotationalSkew.initLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + for i = 0, 2 do + -- + -- Animation using Sprite batch + -- + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width/4*(i+1), s.height/2)) + + local point = cc.Sprite:create("Images/r1.png") + + point:setScale(0.25) + point:setPosition(cc.p(sprite:getPosition())) + layer:addChild(point, 1) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0,0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + elseif i == 2 then + sprite:setAnchorPoint(cc.p(1,1)) + end + + point:setPosition(cc.p(sprite:getPosition())) + + local animFrames = {} + for i = 0,13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png", (i+1))) + animFrames[i + 1] = frame + end + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + local skewX = cc.RotateBy:create(2, 45) + local skewX_back = skewX:reverse() + local skewY = cc.RotateBy:create(2, -45) + local skewY_back = skewY:reverse() + + local seq_skew = cc.Sequence:create(skewX, skewX_back, skewY, skewY_back) + sprite:runAction(cc.RepeatForever:create(seq_skew)) + + layer:addChild(sprite, 0) + end + return layer +end + +function SpriteOffsetAnchorRotationalSkew.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteOffsetAnchorRotationalSkew.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteOffsetAnchorRotationalSkew.eventHandler) + Helper.initWithLayer(layer) + + layer = SpriteOffsetAnchorRotationalSkew.initLayer(layer) + Helper.titleLabel:setString("Sprite offset + anchor + rotational skew") + Helper.subtitleLabel:setString("") + + return layer +end + + +------------------------------------------------------------ +-- SpriteBatchNodeOffsetAnchorSkew +------------------------------------------------------------ +local SpriteBatchNodeOffsetAnchorSkew = {} +SpriteBatchNodeOffsetAnchorSkew.__index = SpriteBatchNodeOffsetAnchorSkew + +function SpriteBatchNodeOffsetAnchorSkew.initLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + local spritebatch = cc.SpriteBatchNode:create("animations/grossini.png") + layer:addChild(spritebatch) + + for i = 0, 2 do + -- + -- Animation using Sprite batch + -- + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width / 4 * (i + 1), s.height / 2)) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale(0.25) + point:setPosition(cc.p(sprite:getPosition())) + layer:addChild(point, 200) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0, 0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + elseif i == 2 then + sprite:setAnchorPoint(cc.p(1, 1)) + end + + point:setPosition(cc.p(sprite:getPosition())) + + local animFrames = {} + for j = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png", j + 1)) + animFrames[j + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + local skewX = cc.SkewBy:create(2, 45, 0) + local skewX_back = skewX:reverse() + local skewY = cc.SkewBy:create(2, 0, 45) + local skewY_back = skewY:reverse() + + local seq_skew = cc.Sequence:create(skewX, skewX_back, skewY, skewY_back) + sprite:runAction(cc.RepeatForever:create(seq_skew)) + + spritebatch:addChild(sprite, i) + end + + return layer +end + +function SpriteBatchNodeOffsetAnchorSkew.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteBatchNodeOffsetAnchorSkew.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteBatchNodeOffsetAnchorSkew.eventHandler) + Helper.initWithLayer(layer) + + layer = SpriteBatchNodeOffsetAnchorSkew.initLayer(layer) + Helper.titleLabel:setString("SpriteBatchNode offset + anchor + skew") + Helper.subtitleLabel:setString("") + + return layer +end + +-- +-- SpriteBatchNodeOffsetAnchorRotationalSkew +-- +local SpriteBatchNodeOffsetAnchorRotationalSkew = {} +SpriteBatchNodeOffsetAnchorRotationalSkew.__index = SpriteBatchNodeOffsetAnchorRotationalSkew + +function SpriteBatchNodeOffsetAnchorRotationalSkew.initLayer(layer) + + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + local spritebatch = cc.SpriteBatchNode:create("animations/grossini.png") + layer:addChild(spritebatch) + + for i = 0, 2 do + -- + -- Animation using Sprite batch + -- + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width/4*(i+1), s.height/2)) + + local point = cc.Sprite:create("Images/r1.png") + + point:setScale(0.25) + point:setPosition(cc.p(sprite:getPosition())) + layer:addChild(point, 200) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0,0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + elseif i == 2 then + sprite:setAnchorPoint(cc.p(1,1)) + end + + point:setPosition(cc.p(sprite:getPosition())) + + local animFrames = {} + for j = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png", (j+1))) + animFrames[j + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + local skewX = cc.RotateBy:create(2, 45) + local skewX_back = skewX:reverse() + local skewY = cc.RotateBy:create(2, -45) + local skewY_back = skewY:reverse() + + local seq_skew = cc.Sequence:create(skewX, skewX_back, skewY, skewY_back) + sprite:runAction(cc.RepeatForever:create(seq_skew)) + + spritebatch:addChild(sprite, i) + end + + return layer +end + +-- remove resources +function SpriteBatchNodeOffsetAnchorRotationalSkew.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteBatchNodeOffsetAnchorRotationalSkew.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteBatchNodeOffsetAnchorRotationalSkew.eventHandler) + Helper.initWithLayer(layer) + + layer = SpriteBatchNodeOffsetAnchorRotationalSkew.initLayer(layer) + Helper.titleLabel:setString("SSpriteBatchNode offset + anchor + rot skew") + Helper.subtitleLabel:setString("") + + return layer +end + +-- +-- SpriteOffsetAnchorSkewScale +-- +local SpriteOffsetAnchorSkewScale = {} + +function SpriteOffsetAnchorSkewScale.initLayer(layer) + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + for i = 0, 2 do + -- Animation using Sprite batch + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width / 4 * (i + 1), s.height / 2)) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale(0.25) + point:setPosition(sprite:getPosition()) + layer:addChild(point, 1) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0,0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + else + sprite:setAnchorPoint(cc.p(1, 1)) + end + + point:setPosition(sprite:getPosition()) + + local animFrames = {} + for j = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png", j+1)) + animFrames[j + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + -- Skew + local skewX = cc.SkewBy:create(2, 45, 0) + local skewX_back = skewX:reverse() + local skewY = cc.SkewBy:create(2, 0, 45) + local skewY_back = skewY:reverse() + + local seq_skew = cc.Sequence:create(skewX, skewX_back, skewY, skewY_back) + sprite:runAction(cc.RepeatForever:create(seq_skew)) + + -- Scale + local scale = cc.ScaleBy:create(2, 2) + local scale_back = scale:reverse() + local seq_scale = cc.Sequence:create(scale, scale_back) + sprite:runAction(cc.RepeatForever:create(seq_scale)) + + layer:addChild(sprite, 0) + end + + return layer +end + +function SpriteOffsetAnchorSkewScale.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteOffsetAnchorSkewScale.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteOffsetAnchorSkewScale.eventHandler) + Helper.initWithLayer(layer) + + layer = SpriteOffsetAnchorSkewScale.initLayer(layer) + Helper.titleLabel:setString("Sprite anchor + skew + scale") + + return layer +end + +-- +-- SpriteOffsetAnchorRotationalSkewScale +-- +local SpriteOffsetAnchorRotationalSkewScale = {} + +function SpriteOffsetAnchorRotationalSkewScale.initLayer(layer) + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + for i = 0, 2 do + -- Animation using Sprite batch + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width/4*(i+1), s.height/2)) + + local point = cc.Sprite:create("Images/r1.png") + + point:setScale(0.25) + point:setPosition(sprite:getPosition()) + layer:addChild(point, 1) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0, 0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + else + sprite:setAnchorPoint(cc.p(1, 1)) + end + + point:setPosition(sprite:getPosition()) + + local animFrames = {} + for j = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png", (j+1))) + animFrames[j + 1] = frame + end + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + -- Skew + local skewX = cc.RotateBy:create(2, 45, 0) + local skewX_back = skewX:reverse() + local skewY = cc.RotateBy:create(2, 0, 45) + local skewY_back = skewY:reverse() + + local seq_skew = cc.Sequence:create(skewX, skewX_back, skewY, skewY_back) + sprite:runAction(cc.RepeatForever:create(seq_skew)) + + -- Scale + local scale = cc.ScaleBy:create(2, 2) + local scale_back = scale:reverse() + local seq_scale = cc.Sequence:create(scale, scale_back) + sprite:runAction(cc.RepeatForever:create(seq_scale)) + + layer:addChild(sprite, i) + end + + return layer +end + +function SpriteOffsetAnchorRotationalSkewScale.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteOffsetAnchorRotationalSkewScale.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteOffsetAnchorRotationalSkewScale.eventHandler) + Helper.initWithLayer(layer) + + layer = SpriteOffsetAnchorRotationalSkewScale.initLayer(layer) + Helper.titleLabel:setString("Sprite anchor + rot skew + scale") + + return layer +end + +-- +-- SpriteBatchNodeOffsetAnchorSkewScale +-- +local SpriteBatchNodeOffsetAnchorSkewScale = {} + +function SpriteBatchNodeOffsetAnchorSkewScale.initLayer(layer) + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + local spritebatch = cc.SpriteBatchNode:create("animations/grossini.png") + layer:addChild(spritebatch) + + for i = 0, 2 do + -- Animation using Sprite batch + + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width / 4 * (i + 1), s.height / 2)) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale(0.25) + point:setPosition(sprite:getPosition()) + layer:addChild(point, 200) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0, 0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + else + sprite:setAnchorPoint(cc.p(1, 1)) + end + + point:setPosition(sprite:getPosition()) + + local animFrames = {} + for j = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png", (j+1))) + animFrames[j + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + -- skew + local skewX = cc.SkewBy:create(2, 45, 0) + local skewX_back = skewX:reverse() + local skewY = cc.SkewBy:create(2, 0, 45) + local skewY_back = skewY:reverse() + + local seq_skew = cc.Sequence:create(skewX, skewX_back, skewY, skewY_back) + sprite:runAction(cc.RepeatForever:create(seq_skew)) + + + -- scale + local scale = cc.ScaleBy:create(2, 2) + local scale_back = scale:reverse() + local seq_scale = cc.Sequence:create(scale, scale_back) + sprite:runAction(cc.RepeatForever:create(seq_scale)) + + spritebatch:addChild(sprite, i) + end +end + +function SpriteBatchNodeOffsetAnchorSkewScale.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteBatchNodeOffsetAnchorSkewScale.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteBatchNodeOffsetAnchorSkewScale.eventHandler) + Helper.initWithLayer(layer) + + SpriteBatchNodeOffsetAnchorSkewScale.initLayer(layer) + Helper.titleLabel:setString("SpriteBatchNode anchor + skew + scale") + + return layer +end + +-- +-- SpriteBatchNodeOffsetAnchorRotationalSkewScale +-- +local SpriteBatchNodeOffsetAnchorRotationalSkewScale = {} +function SpriteBatchNodeOffsetAnchorRotationalSkewScale.initLayer(layer) + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + local spritebatch = cc.SpriteBatchNode:create("animations/grossini.png") + layer:addChild(spritebatch) + + for i = 0, 2 do + -- Animation using Sprite batch + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width/4*(i+1), s.height/2)) + + local point = cc.Sprite:create("Images/r1.png") + + point:setScale(0.25) + point:setPosition(sprite:getPosition()) + + layer:addChild(point, 200) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0, 0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + else + sprite:setAnchorPoint(cc.p(1, 1)) + end + + point:setPosition(sprite:getPosition()) + + local animFrames = {} + for j = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png", j+1)) + animFrames[j + 1] = frame + end + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + -- Skew + local skewX = cc.RotateBy:create(2, 45, 0) + local skewX_back = skewX:reverse() + local skewY = cc.RotateBy:create(2, 0, 45) + local skewY_back = skewY:reverse() + + local seq_skew = cc.Sequence:create(skewX, skewX_back, skewY, skewY_back) + sprite:runAction(cc.RepeatForever:create(seq_skew)) + + -- Scale + local scale = cc.ScaleBy:create(2, 2) + local scale_back = scale:reverse() + local seq_scale = cc.Sequence:create(scale, scale_back) + sprite:runAction(cc.RepeatForever:create(seq_scale)) + + spritebatch:addChild(sprite, i) + end +end + +function SpriteBatchNodeOffsetAnchorRotationalSkewScale.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteBatchNodeOffsetAnchorRotationalSkewScale.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteBatchNodeOffsetAnchorRotationalSkewScale.eventHandler) + Helper.initWithLayer(layer) + SpriteBatchNodeOffsetAnchorRotationalSkewScale.initLayer(layer) + + Helper.titleLabel:setString("SpriteBatchNode anchor + rot skew + scale") + + return layer +end + +-- +-- SpriteOffsetAnchorFlip +-- +local SpriteOffsetAnchorFlip = {} +function SpriteOffsetAnchorFlip.initLayer(layer) + local s = cc.Director:getInstance():getWinSize() + + local cache = cc.SpriteFrameCache:getInstance() + cache:addSpriteFrames("animations/grossini.plist") + cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png") + + for i = 0, 2 do + -- Animation using Sprite batch + local sprite = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png") + sprite:setPosition(cc.p(s.width / 4 * (i + 1), s.height / 2)) + + local point = cc.Sprite:create("Images/r1.png") + point:setScale(0.25) + point:setPosition(sprite:getPosition()) + layer:addChild(point, 1) + + if i == 0 then + sprite:setAnchorPoint(cc.p(0, 0)) + elseif i == 1 then + sprite:setAnchorPoint(cc.p(0.5, 0.5)) + else + sprite:setAnchorPoint(cc.p(1, 1)) + end + + point:setPosition(sprite:getPosition()) + + local animFrames = {} + for j = 0, 13 do + local frame = cache:getSpriteFrame(string.format("grossini_dance_%02d.png", j+1)) + animFrames[j + 1] = frame + end + + local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3) + sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation))) + + local flip = cc.FlipY:create(true) + local flip_back = cc.FlipY:create(false) + local delay = cc.DelayTime:create(1) + local delay2 = cc.DelayTime:create(1) + + local seq = cc.Sequence:create(delay, flip, delay2, flip_back) + sprite:runAction(cc.RepeatForever:create(seq)) + + layer:addChild(sprite, 0) + end +end + +function SpriteOffsetAnchorFlip.eventHandler(tag) + if tag == "exit" then + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("animations/grossini.plist") + cache:removeSpriteFramesFromFile("animations/grossini_gray.plist") + end +end + +function SpriteOffsetAnchorFlip.create() + local layer = cc.Layer:create() + layer:registerScriptHandler(SpriteOffsetAnchorFlip.eventHandler) + Helper.initWithLayer(layer) + + SpriteOffsetAnchorFlip.initLayer(layer) + Helper.titleLabel:setString("Sprite offset + anchor + flip") + Helper.subtitleLabel:setString("issue #1078") + + return layer +end + + +function SpriteTest() + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + Sprite1.create, + SpriteBatchNode1.create, + SpriteFrameTest.create, + SpriteFrameAliasNameTest.create, + SpriteAnchorPoint.create, + SpriteBatchNodeAnchorPoint.create, + SpriteOffsetAnchorRotation.create, + SpriteBatchNodeOffsetAnchorRotation.create, + SpriteOffsetAnchorScale.create, + SpriteBatchNodeOffsetAnchorScale.create, + SpriteOffsetAnchorSkew.create, + SpriteOffsetAnchorRotationalSkew.create, + SpriteBatchNodeOffsetAnchorSkew.create, + SpriteBatchNodeOffsetAnchorRotationalSkew.create, + SpriteOffsetAnchorSkewScale.create, + SpriteOffsetAnchorRotationalSkewScale.create, + SpriteBatchNodeOffsetAnchorSkewScale.create, + SpriteBatchNodeOffsetAnchorRotationalSkewScale.create, + SpriteOffsetAnchorFlip.create + } + + scene:addChild(Sprite1.create()) + scene:addChild(CreateBackMenuItem()) + + return scene +end diff --git a/samples/lua-tests/src/Texture2dTest/Texture2dTest.lua b/samples/lua-tests/src/Texture2dTest/Texture2dTest.lua new file mode 100644 index 0000000000..66a31b7988 --- /dev/null +++ b/samples/lua-tests/src/Texture2dTest/Texture2dTest.lua @@ -0,0 +1,1434 @@ +local scheduler = cc.Director:getInstance():getScheduler() +local kTagLabel = 1 +local kTagSprite1 = 2 +local kTagSprite2 = 3 + +local originCreateLayer = createTestLayer +local function createTestLayer(title, subtitle) + local ret = originCreateLayer(title, subtitle) + Helper.titleLabel:setTag(kTagLabel) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + local col = cc.LayerColor:create(cc.c4b(128,128,128,255)) + ret:addChild(col, -10) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end +-------------------------------------------------------------------- +-- +-- TextureTIFF +-- +-------------------------------------------------------------------- + +local function TextureTIFF() + local ret = createTestLayer("TIFF Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image.tiff") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePNG +-- +-------------------------------------------------------------------- +local function TexturePNG() + local ret = createTestLayer("PNG Test") + + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image.png") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TextureJPEG +-- +-------------------------------------------------------------------- +local function TextureJPEG() + local ret = createTestLayer("JPEG Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image.jpeg") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TextureWEBP +-- +-------------------------------------------------------------------- +local function TextureWEBP() + local ret = createTestLayer("WEBP Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image.webp") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TextureMipMap +-- +-------------------------------------------------------------------- +local function TextureMipMap() + local ret = createTestLayer("Texture Mipmap", + "Left image uses mipmap. Right image doesn't") + local s = cc.Director:getInstance():getWinSize() + + local texture0 = cc.Director:getInstance():getTextureCache():addImage( + "Images/grossini_dance_atlas.png") + texture0:generateMipmap() + texture0:setTexParameters(gl.LINEAR_MIPMAP_LINEAR, gl.LINEAR, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE) + + local texture1 = cc.Director:getInstance():getTextureCache():addImage( + "Images/grossini_dance_atlas_nomipmap.png") + + local img0 = cc.Sprite:createWithTexture(texture0) + img0:setTextureRect(cc.rect(85, 121, 85, 121)) + img0:setPosition(cc.p( s.width/3.0, s.height/2.0)) + ret:addChild(img0) + + local img1 = cc.Sprite:createWithTexture(texture1) + img1:setTextureRect(cc.rect(85, 121, 85, 121)) + img1:setPosition(cc.p( 2*s.width/3.0, s.height/2.0)) + ret:addChild(img1) + + local scale1 = cc.EaseOut:create(cc.ScaleBy:create(4, 0.01), 3) + local sc_back = scale1:reverse() + + local scale2 = tolua.cast(scale1:clone(), "cc.EaseOut") + local sc_back2 = scale2:reverse() + + img0:runAction(cc.RepeatForever:create(cc.Sequence:create(scale1, sc_back))) + img1:runAction(cc.RepeatForever:create(cc.Sequence:create(scale2, sc_back2))) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRMipMap +-- To generate PVR images read this article: +-- http:--developer.apple.com/iphone/library/qa/qa2008/qa1611.html +-- +-------------------------------------------------------------------- +local function TexturePVRMipMap() + local ret = createTestLayer("PVRTC MipMap Test", "Left image uses mipmap. Right image doesn't") + local s = cc.Director:getInstance():getWinSize() + + local imgMipMap = cc.Sprite:create("Images/logo-mipmap.pvr") + if imgMipMap ~= nil then + imgMipMap:setPosition(cc.p( s.width/2.0-100, s.height/2.0)) + ret:addChild(imgMipMap) + + -- support mipmap filtering + imgMipMap:getTexture():setTexParameters(gl.LINEAR_MIPMAP_LINEAR, gl.LINEAR, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE) + end + + local img = cc.Sprite:create("Images/logo-nomipmap.pvr") + if img ~= nil then + + img:setPosition(cc.p( s.width/2.0+100, s.height/2.0)) + ret:addChild(img) + + local scale1 = cc.EaseOut:create(cc.ScaleBy:create(4, 0.01), 3) + local sc_back = scale1:reverse() + + local scale2 = tolua.cast(scale1:clone(), "cc.EaseOut") + local sc_back2 = scale2:reverse() + + imgMipMap:runAction(cc.RepeatForever:create(cc.Sequence:create(scale1, sc_back))) + img:runAction(cc.RepeatForever:create(cc.Sequence:create(scale2, sc_back2))) + end + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + + +-------------------------------------------------------------------- +-- +-- TexturePVRMipMap2 +-- +-------------------------------------------------------------------- +local function TexturePVRMipMap2() + local ret = createTestLayer("PVR MipMap Test #2", "Left image uses mipmap. Right image doesn't") + local s = cc.Director:getInstance():getWinSize() + + local imgMipMap = cc.Sprite:create("Images/test_image_rgba4444_mipmap.pvr") + imgMipMap:setPosition(cc.p( s.width/2.0-100, s.height/2.0)) + ret:addChild(imgMipMap) + + -- support mipmap filtering + imgMipMap:getTexture():setTexParameters(gl.LINEAR_MIPMAP_LINEAR, gl.LINEAR, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE) + + local img = cc.Sprite:create("Images/test_image.png") + img:setPosition(cc.p( s.width/2.0+100, s.height/2.0)) + ret:addChild(img) + + local scale1 = cc.EaseOut:create(cc.ScaleBy:create(4, 0.01), 3) + local sc_back = scale1:reverse() + + local scale2 = tolua.cast(scale1:clone(), "cc.EaseOut") + local sc_back2 = scale2:reverse() + + imgMipMap:runAction(cc.RepeatForever:create(cc.Sequence:create(scale1, sc_back))) + img:runAction(cc.RepeatForever:create(cc.Sequence:create(scale2, sc_back2))) + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVR2BPP +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVR2BPP() + local ret = createTestLayer("PVR TC 2bpp Test") + + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_pvrtc2bpp.pvr") + + if img ~= nil then + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVR +-- To generate PVR images read this article: +-- http:--developer.apple.com/iphone/library/qa/qa2008/qa1611.html +-- +-------------------------------------------------------------------- +local function TexturePVR() + local ret = createTestLayer("PVR TC 4bpp Test #2") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image.pvr") + + if img ~= nil then + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + else + cclog("This test is not supported.") + end + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVR4BPP +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVR4BPP() + local ret = createTestLayer("PVR TC 4bpp Test #3") + + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_pvrtc4bpp.pvr") + + if img ~= nil then + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + else + cclog("This test is not supported in cocos2d-mac") + end + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRRGBA8888 +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRRGBA8888() + local ret = createTestLayer("PVR + RGBA 8888 Test") + + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgba8888.pvr") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRBGRA8888 +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRBGRA8888() + local ret = createTestLayer("PVR + BGRA 8888 Test") + + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_bgra8888.pvr") + if img ~= nil then + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + else + cclog("BGRA8888 images are not supported") + end + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRRGBA5551 +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRRGBA5551() + local ret = createTestLayer("PVR + RGBA 5551 Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgba5551.pvr") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRRGBA4444 +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRRGBA4444() + local ret = createTestLayer("PVR + RGBA 4444 Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgba4444.pvr") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRRGBA4444GZ +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRRGBA4444GZ() + local ret = createTestLayer("PVR + RGBA 4444 + GZ Test", + "This is a gzip PVR image") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgba4444.pvr") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRRGBA4444CCZ +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRRGBA4444CCZ() + local ret = createTestLayer("PVR + RGBA 4444 + cc.Z Test", + "This is a ccz PVR image") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgba4444.pvr.ccz") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRRGB565 +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRRGB565() + local ret = createTestLayer("PVR + RGB 565 Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgb565.pvr") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVR RGB888 +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +local function TexturePVRRGB888() + local ret = createTestLayer("PVR + RGB 888 Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgb888.pvr") + if img ~= nil then + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRA8 +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRA8() + local ret = createTestLayer("PVR + A8 Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_a8.pvr") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRI8 +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRI8() + local ret = createTestLayer("PVR + I8 Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_i8.pvr") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + + +-------------------------------------------------------------------- +-- +-- TexturePVRAI88 +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRAI88() + local ret = createTestLayer("PVR + AI88 Test") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_ai88.pvr") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVR2BPPv3 +local function TexturePVR2BPPv3() + local ret = createTestLayer("PVR TC 2bpp Test", "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_pvrtc2bpp_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRII2BPPv3 +local function TexturePVRII2BPPv3() + local ret = createTestLayer("PVR TC II 2bpp Test", "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_pvrtcii2bpp_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVR4BPPv3 +local function TexturePVR4BPPv3() + local ret = createTestLayer("PVR TC 4bpp Test", "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_pvrtc4bpp_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + else + cclog("This test is not supported") + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRII4BPPv3 + +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp + +local function TexturePVRII4BPPv3() + local ret = createTestLayer("PVR TC II 4bpp Test", + "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_pvrtcii4bpp_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + else + cclog("This test is not supported") + end + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRRGBA8888v3 +local function TexturePVRRGBA8888v3() + local ret = createTestLayer("PVR + RGBA 8888 Test", + "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgba8888_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRBGRA8888v3 +local function TexturePVRBGRA8888v3() + local ret = createTestLayer("PVR + BGRA 8888 Test", + "Testing PVR File Format v3") + + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_bgra8888_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + else + cclog("BGRA images are not supported") + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRRGBA5551v3 +local function TexturePVRRGBA5551v3() + local ret = createTestLayer("PVR + RGBA 5551 Test", + "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + local img = cc.Sprite:create("Images/test_image_rgba5551_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRRGBA4444v3 +local function TexturePVRRGBA4444v3() + local ret = createTestLayer("PVR + RGBA 4444 Test", + "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgba4444_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRRGB565v3 +local function TexturePVRRGB565v3() + local ret = createTestLayer("PVR + RGB 565 Test", + "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgb565_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRRGB888v3 +local function TexturePVRRGB888v3() + local ret = createTestLayer("PVR + RGB 888 Test", + "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_rgb888_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRA8v3 +local function TexturePVRA8v3() + local ret = createTestLayer("PVR + A8 Test", + "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_a8_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRI8v3 +local function TexturePVRI8v3() + local ret = createTestLayer("PVR + I8 Test", + "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_i8_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-- TexturePVRAI88v3 +local function TexturePVRAI88v3() + local ret = createTestLayer("PVR + AI88 Test", + "Testing PVR File Format v3") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image_ai88_v3.pvr") + + if img ~= nil then + img:setPosition(cc.p(s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRBadEncoding +-- Image generated using PVRTexTool: +-- http:--www.imgtec.com/powervr/insider/powervr-pvrtextool.asp +-- +-------------------------------------------------------------------- +local function TexturePVRBadEncoding() + local ret = createTestLayer("PVR Unsupported encoding", + "You should not see any image") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/test_image-bad_encoding.pvr") + if img ~= nil then + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRNonSquare +-- +-------------------------------------------------------------------- +local function TexturePVRNonSquare() + local ret = createTestLayer("PVR + Non square texture", + "Loading a 128x256 texture") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/grossini_128x256_mipmap.pvr") + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRNPOT4444 +-- +-------------------------------------------------------------------- +local function TexturePVRNPOT4444() + local ret = createTestLayer("PVR RGBA4 + NPOT texture", + "Loading a 81x121 RGBA4444 texture.") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/grossini_pvr_rgba4444.pvr") + if img ~= nil then + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePVRNPOT8888 +-- +-------------------------------------------------------------------- +local function TexturePVRNPOT8888() + local ret = createTestLayer("PVR RGBA8 + NPOT texture", + "Loading a 81x121 RGBA8888 texture.") + local s = cc.Director:getInstance():getWinSize() + + local img = cc.Sprite:create("Images/grossini_pvr_rgba8888.pvr") + if img ~= nil then + img:setPosition(cc.p( s.width/2.0, s.height/2.0)) + ret:addChild(img) + end + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TextureAlias +-- +-------------------------------------------------------------------- +local function TextureAlias() + local ret = createTestLayer("AntiAlias / Alias textures", + "Left image is antialiased. Right image is aliases") + local s = cc.Director:getInstance():getWinSize() + + -- + -- Sprite 1: gl.LINEAR + -- + -- Default filter is gl.LINEAR + + local sprite = cc.Sprite:create("Images/grossinis_sister1.png") + sprite:setPosition(cc.p( s.width/3.0, s.height/2.0)) + ret:addChild(sprite) + + -- this is the default filterting + sprite:getTexture():setAntiAliasTexParameters() + + -- + -- Sprite 1: GL_NEAREST + -- + + local sprite2 = cc.Sprite:create("Images/grossinis_sister2.png") + sprite2:setPosition(cc.p( 2*s.width/3.0, s.height/2.0)) + ret:addChild(sprite2) + + -- Use Nearest in this one + sprite2:getTexture():setAliasTexParameters() + + -- scale them to show + local sc = cc.ScaleBy:create(3, 8.0) + local sc_back = tolua.cast(sc:reverse(), "cc.ScaleBy") + local scaleforever = cc.RepeatForever:create(cc.Sequence:create(sc, sc_back)) + local scaleToo = tolua.cast(scaleforever:clone(), "cc.RepeatForever") + + sprite2:runAction(scaleforever) + sprite:runAction(scaleToo) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TexturePixelFormat +-- +-------------------------------------------------------------------- +local function TexturePixelFormat() + local ret = createTestLayer("Texture Pixel Formats", + "Textures: RGBA8888, RGBA4444, RGB5A1, RGB888, RGB565, A8") + -- + -- This example displays 1 png images 4 times. + -- Each time the image is generated using: + -- 1- 32-bit RGBA8 + -- 2- 16-bit RGBA4 + -- 3- 16-bit RGB5A1 + -- 4- 16-bit RGB565 + + local label = tolua.cast(ret:getChildByTag(kTagLabel), "cc.LabelTTF") + label:setColor(cc.c3b(16,16,255)) + + local s = cc.Director:getInstance():getWinSize() + + local background = cc.LayerColor:create(cc.c4b(128,128,128,255), s.width, s.height) + ret:addChild(background, -1) + + -- RGBA 8888 image (32-bit) + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + local sprite1 = cc.Sprite:create("Images/test-rgba1.png") + sprite1:setPosition(cc.p(1*s.width/7, s.height/2+32)) + ret:addChild(sprite1, 0) + + -- remove texture from texture manager + cc.Director:getInstance():getTextureCache():removeTexture(sprite1:getTexture()) + + -- RGBA 4444 image (16-bit) + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A4444) + local sprite2 = cc.Sprite:create("Images/test-rgba1.png") + sprite2:setPosition(cc.p(2*s.width/7, s.height/2-32)) + ret:addChild(sprite2, 0) + + -- remove texture from texture manager + cc.Director:getInstance():getTextureCache():removeTexture(sprite2:getTexture()) + + -- RGB5A1 image (16-bit) + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB5_A1) + local sprite3 = cc.Sprite:create("Images/test-rgba1.png") + sprite3:setPosition(cc.p(3*s.width/7, s.height/2+32)) + ret:addChild(sprite3, 0) + + -- remove texture from texture manager + cc.Director:getInstance():getTextureCache():removeTexture(sprite3:getTexture()) + + -- RGB888 image + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RG_B888 ) + local sprite4 = cc.Sprite:create("Images/test-rgba1.png") + sprite4:setPosition(cc.p(4*s.width/7, s.height/2-32)) + ret:addChild(sprite4, 0) + + -- remove texture from texture manager + cc.Director:getInstance():getTextureCache():removeTexture(sprite4:getTexture()) + + -- RGB565 image (16-bit) + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RG_B565) + local sprite5 = cc.Sprite:create("Images/test-rgba1.png") + sprite5:setPosition(cc.p(5*s.width/7, s.height/2+32)) + ret:addChild(sprite5, 0) + + -- remove texture from texture manager + cc.Director:getInstance():getTextureCache():removeTexture(sprite5:getTexture()) + + -- A8 image (8-bit) + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_A8 ) + local sprite6 = cc.Sprite:create("Images/test-rgba1.png") + sprite6:setPosition(cc.p(6*s.width/7, s.height/2-32)) + ret:addChild(sprite6, 0) + + -- remove texture from texture manager + cc.Director:getInstance():getTextureCache():removeTexture(sprite6:getTexture()) + + local fadeout = cc.FadeOut:create(2) + local fadein = cc.FadeIn:create(2) + local seq = cc.Sequence:create(cc.DelayTime:create(2), fadeout, fadein) + local seq_4ever = cc.RepeatForever:create(seq) + local seq_4ever2 = tolua.cast(seq_4ever:clone(), "cc.RepeatForever") + local seq_4ever3 = tolua.cast(seq_4ever:clone(), "cc.RepeatForever") + local seq_4ever4 = tolua.cast(seq_4ever:clone(), "cc.RepeatForever") + local seq_4ever5 = tolua.cast(seq_4ever:clone(), "cc.RepeatForever") + + sprite1:runAction(seq_4ever) + sprite2:runAction(seq_4ever2) + sprite3:runAction(seq_4ever3) + sprite4:runAction(seq_4ever4) + sprite5:runAction(seq_4ever5) + + -- restore default + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_DEFAULT) + print(cc.Director:getInstance():getTextureCache():getCachedTextureInfo()) + return ret +end + +-------------------------------------------------------------------- +-- +-- TextureBlend +-- +-------------------------------------------------------------------- +local function TextureBlend() + local ret = createTestLayer("Texture Blending", + "Testing 3 different blending modes") + local i = 0 + for i=0, 14 do + -- BOTTOM sprites have alpha pre-multiplied + -- they use by default GL_ONE, GL_ONE_MINUS_SRC_ALPHA + local cloud = cc.Sprite:create("Images/test_blend.png") + ret:addChild(cloud, i+1, 100+i) + cloud:setPosition(cc.p(50+25*i, 80)) + cloud:setBlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA) + + -- CENTER sprites have also alpha pre-multiplied + -- they use by default GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA + cloud = cc.Sprite:create("Images/test_blend.png") + ret:addChild(cloud, i+1, 200+i) + cloud:setPosition(cc.p(50+25*i, 160)) + cloud:setBlendFunc(gl.ONE_MINUS_DST_COLOR , gl.ZERO) + + -- UPPER sprites are using custom blending function + -- You can set any blend function to your sprites + cloud = cc.Sprite:create("Images/test_blend.png") + ret:addChild(cloud, i+1, 200+i) + cloud:setPosition(cc.p(50+25*i, 320-80)) + cloud:setBlendFunc(gl.SRC_ALPHA, gl.ONE) -- additive blending + end + return ret +end + +-------------------------------------------------------------------- +-- +-- TextureAsync +-- +-------------------------------------------------------------------- + +local function TextureAsync() + local ret = createTestLayer("Texture Async Load", + "Textures should load while an animation is being run") + local m_nImageOffset = 0 + + local size =cc.Director:getInstance():getWinSize() + + local label = cc.LabelTTF:create("Loading...", "Marker Felt", 32) + label:setPosition(cc.p( size.width/2, size.height/2)) + ret:addChild(label, 10) + + local scale = cc.ScaleBy:create(0.3, 2) + local scale_back = tolua.cast(scale:reverse(), "cc.ScaleBy") + local seq = cc.Sequence:create(scale, scale_back) + label:runAction(cc.RepeatForever:create(seq)) + + local function imageLoaded(pObj) + local tex = tolua.cast(pObj, "cc.Texture2D") + local director = cc.Director:getInstance() + + --cc.ASSERT( [NSThread currentThread] == [director runningThread], @"FAIL. Callback should be on cocos2d thread") + + -- IMPORTANT: The order on the callback is not guaranteed. Don't depend on the callback + + -- This test just creates a sprite based on the Texture + + local sprite = cc.Sprite:createWithTexture(tex) + sprite:setAnchorPoint(cc.p(0,0)) + ret:addChild(sprite, -1) + + local size = director:getWinSize() + local i = m_nImageOffset * 32 + sprite:setPosition(cc.p( i % size.width, (i / size.width) * 32 )) + + m_nImageOffset = m_nImageOffset + 1 + cclog("Image loaded:...")-- %p", tex) + end + + local function loadImages(dt) + local i = 0 + local j = 0 + for i=0, 7 do + for j=0, 7 do + local szSpriteName = string.format( + "Images/sprites_test/sprite-%d-%d.png", i, j) + cc.Director:getInstance():getTextureCache():addImageAsync( + szSpriteName, imageLoaded) + end + end + + cc.Director:getInstance():getTextureCache():addImageAsync("Images/background1.jpg", imageLoaded) + cc.Director:getInstance():getTextureCache():addImageAsync("Images/background2.jpg", imageLoaded) + cc.Director:getInstance():getTextureCache():addImageAsync("Images/background.png", imageLoaded) + cc.Director:getInstance():getTextureCache():addImageAsync("Images/atlastest.png", imageLoaded) + cc.Director:getInstance():getTextureCache():addImageAsync("Images/grossini_dance_atlas.png",imageLoaded) + end + + local schedulerEntry = nil + local function onNodeEvent(event) + if event == "enter" then + schedulerEntry = scheduler:scheduleScriptFunc(loadImages, 1.0, false) + elseif event == "exit" then + scheduler:unscheduleScriptEntry(schedulerEntry) + cc.Director:getInstance():getTextureCache():removeAllTextures() + end + end + + ret:registerScriptHandler(onNodeEvent) + return ret +end + +-------------------------------------------------------------------- +-- +-- TextureGlClamp +-- +-------------------------------------------------------------------- +local function TextureGlClamp() + local ret = createTestLayer("Texture GL_CLAMP") + + local size = cc.Director:getInstance():getWinSize() + + -- The .png image MUST be power of 2 in order to create a continue effect. + -- eg: 32x64, 512x128, 256x1024, 64x64, etc.. + local sprite = cc.Sprite:create("Images/pattern1.png", cc.rect(0,0,512,256)) + ret:addChild(sprite, -1, kTagSprite1) + sprite:setPosition(cc.p(size.width/2,size.height/2)) + sprite:getTexture():setTexParameters(gl.LINEAR, gl.LINEAR, gl.CLAMP_TO_EDGE, gl.CLAMP_TO_EDGE) + + local rotate = cc.RotateBy:create(4, 360) + sprite:runAction(rotate) + local scale = cc.ScaleBy:create(2, 0.04) + local scaleBack = tolua.cast(scale:reverse(), "cc.ScaleBy") + local seq = cc.Sequence:create(scale, scaleBack) + sprite:runAction(seq) + local function onNodeEvent(event) + if event == "exit" then + cc.Director:getInstance():getTextureCache():removeUnusedTextures() + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +-------------------------------------------------------------------- +-- +-- TextureGlRepeat +-- +-------------------------------------------------------------------- +local function TextureGlRepeat() + local ret = createTestLayer("Texture gl.REPEAT") + + local size = cc.Director:getInstance():getWinSize() + + -- The .png image MUST be power of 2 in order to create a continue effect. + -- eg: 32x64, 512x128, 256x1024, 64x64, etc.. + local sprite = cc.Sprite:create("Images/pattern1.png", cc.rect(0, 0, 4096, 4096)) + ret:addChild(sprite, -1, kTagSprite1) + sprite:setPosition(cc.p(size.width/2,size.height/2)) + sprite:getTexture():setTexParameters(gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT) + + local rotate = cc.RotateBy:create(4, 360) + sprite:runAction(rotate) + local scale = cc.ScaleBy:create(2, 0.04) + local scaleBack = tolua.cast(scale:reverse(), "cc.ScaleBy") + local seq = cc.Sequence:create(scale, scaleBack) + sprite:runAction(seq) + local function onNodeEvent(event) + if event == "exit" then + cc.Director:getInstance():getTextureCache():removeUnusedTextures() + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +-------------------------------------------------------------------- +-- +-- TextureSizeTest +-- +-------------------------------------------------------------------- +local function TextureSizeTest() + local ret = createTestLayer("Different Texture Sizes", + "512x512, 1024x1024. See the console.") + local sprite = nil + + cclog("Loading 512x512 image...") + sprite = cc.Sprite:create("Images/texture512x512.png") + if sprite ~= nil then + cclog("OK\n") + else + cclog("Error\n") + + cclog("Loading 1024x1024 image...") + sprite = cc.Sprite:create("Images/texture1024x1024.png") + if sprite ~= nil then + cclog("OK\n") + else + cclog("Error\n") + -- @todo + -- cclog("Loading 2048x2048 image...") + -- sprite = cc.Sprite:create("Images/texture2048x2048.png") + -- if( sprite ) + -- cclog("OK\n") + -- else + -- cclog("Error\n") + -- + -- cclog("Loading 4096x4096 image...") + -- sprite = cc.Sprite:create("Images/texture4096x4096.png") + -- if( sprite ) + -- cclog("OK\n") + -- else + -- cclog("Error\n") + end + end + return ret +end +-------------------------------------------------------------------- +-- +-- TextureCache1 +-- +-------------------------------------------------------------------- +local function TextureCache1() + local ret = createTestLayer("TextureCache: remove", + "4 images should appear: alias, antialias, alias, antilias") + local s = cc.Director:getInstance():getWinSize() + + local sprite = nil + + sprite = cc.Sprite:create("Images/grossinis_sister1.png") + sprite:setPosition(cc.p(s.width/5*1, s.height/2)) + sprite:getTexture():setAliasTexParameters() + sprite:setScale(2) + ret:addChild(sprite) + + cc.Director:getInstance():getTextureCache():removeTexture(sprite:getTexture()) + + sprite = cc.Sprite:create("Images/grossinis_sister1.png") + sprite:setPosition(cc.p(s.width/5*2, s.height/2)) + sprite:getTexture():setAntiAliasTexParameters() + sprite:setScale(2) + ret:addChild(sprite) + + -- 2nd set of sprites + + sprite = cc.Sprite:create("Images/grossinis_sister2.png") + sprite:setPosition(cc.p(s.width/5*3, s.height/2)) + sprite:getTexture():setAliasTexParameters() + sprite:setScale(2) + ret:addChild(sprite) + + cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister2.png") + + sprite = cc.Sprite:create("Images/grossinis_sister2.png") + sprite:setPosition(cc.p(s.width/5*4, s.height/2)) + sprite:getTexture():setAntiAliasTexParameters() + sprite:setScale(2) + ret:addChild(sprite) + return ret +end + +-- TextureDrawAtPoint +local function TextureDrawAtPoint() + local m_pTex1 = nil + local m_pTex2F = nil + local ret = createTestLayer("Texture2D: drawAtPoint", + "draws 2 textures using drawAtPoint") + + local function draw() + -- TextureDemo:draw() + + local s = cc.Director:getInstance():getWinSize() + + m_pTex1:drawAtPoint(cc.p(s.width/2-50, s.height/2 - 50)) + m_pTex2F:drawAtPoint(cc.p(s.width/2+50, s.height/2 - 50)) + end + + m_pTex1 = cc.Director:getInstance():getTextureCache():addImage("Images/grossinis_sister1.png") + m_pTex2F = cc.Director:getInstance():getTextureCache():addImage("Images/grossinis_sister2.png") + + m_pTex1:retain() + m_pTex2F:retain() + local function onNodeEvent(event) + if event == "exit" then + m_pTex1:release() + m_pTex2F:release() + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +-- TextureDrawInRect + +local function TextureDrawInRect() + local ret = createTestLayer("Texture2D: drawInRect", + "draws 2 textures using drawInRect") + local function draw() + -- TextureDemo:draw() + + local s = cc.Director:getInstance():getWinSize() + + local rect1 = cc.rect( s.width/2 - 80, 20, m_pTex1:getContentSize().width * 0.5, m_pTex1:getContentSize().height *2 ) + local rect2 = cc.rect( s.width/2 + 80, s.height/2, m_pTex1:getContentSize().width * 2, m_pTex1:getContentSize().height * 0.5 ) + + m_pTex1:drawInRect(rect1) + m_pTex2F:drawInRect(rect2) + end + + local m_pTex1 = cc.Director:getInstance():getTextureCache():addImage("Images/grossinis_sister1.png") + local m_pTex2F = cc.Director:getInstance():getTextureCache():addImage("Images/grossinis_sister2.png") + + m_pTex1:retain() + m_pTex2F:retain() + local function onNodeEvent(event) + if event == "exit" then + m_pTex1:release() + m_pTex2F:release() + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +-- -------------------------------------------------------------------- +-- -- +-- TextureMemoryAlloc +-- +-------------------------------------------------------------------- +local function TextureMemoryAlloc() + local ret = createTestLayer("Texture memory", + "Testing Texture Memory allocation. Use Instruments + VM Tracker") + local m_pBackground = nil + + cc.MenuItemFont:setFontSize(24) + + local function updateImage(tag,sender) + if m_pBackground ~= nil then + cclog("updateImage"..tag) + m_pBackground:removeFromParent(true) + end + cc.Director:getInstance():getTextureCache():removeUnusedTextures() + + local file = "" + if tag == 0 then + file = "Images/test_1021x1024.png" + elseif tag == 1 then + file = "Images/test_1021x1024_rgba8888.pvr" + elseif tag == 2 then + file = "Images/test_1021x1024_rgb888.pvr" + elseif tag == 3 then + file = "Images/test_1021x1024_rgba4444.pvr" + elseif tag == 4 then + file = "Images/test_1021x1024_a8.pvr" + end + + m_pBackground = cc.Sprite:create(file) + ret:addChild(m_pBackground, -10) + + m_pBackground:setVisible(false) + + local s = cc.Director:getInstance():getWinSize() + m_pBackground:setPosition(cc.p(s.width/2, s.height/2)) + end + + local item1 = cc.MenuItemFont:create("PNG") + item1:registerScriptTapHandler(updateImage) + item1:setTag(0) + + local item2 = cc.MenuItemFont:create("RGBA8") + item2:registerScriptTapHandler(updateImage) + item2:setTag(1) + + local item3 = cc.MenuItemFont:create("RGB8") + item3:registerScriptTapHandler(updateImage) + item3:setTag(2) + + local item4 = cc.MenuItemFont:create("RGBA4") + item4:registerScriptTapHandler(updateImage) + item4:setTag(3) + + local item5 = cc.MenuItemFont:create("A8") + item5:registerScriptTapHandler(updateImage) + item5:setTag(4) + + local menu = cc.Menu:create(item1, item2, item3, item4, item5) + menu:alignItemsHorizontally() + + ret:addChild(menu) + + local warmup = cc.MenuItemFont:create("warm up texture") + + local function changeBackgroundVisible(tag, sender) + if m_pBackground ~= nil then + cclog("changeBackgroundVisible") + m_pBackground:setVisible(true) + end + end + + warmup:registerScriptTapHandler(changeBackgroundVisible) + local menu2 = cc.Menu:create(warmup) + + menu2:alignItemsHorizontally() + + ret:addChild(menu2) + local s = cc.Director:getInstance():getWinSize() + + menu2:setPosition(cc.p(s.width/2, s.height/4)) + return ret +end + +-- TexturePVRv3Premult +local function TexturePVRv3Premult() + local ret = createTestLayer("PVRv3 Premult Flag", + "All images should look exactly the same") + + local function transformSprite(sprite) + local fade = cc.FadeOut:create(2) + local dl = cc.DelayTime:create(2) + local fadein = tolua.cast(fade:reverse(), "cc.FadeOut") + local seq = cc.Sequence:create(fade, fadein, dl) + local repeatAction = cc.RepeatForever:create(seq) + sprite:runAction(repeatAction) + end + + local size = cc.Director:getInstance():getWinSize() + + local background = cc.LayerColor:create(cc.c4b(128,128,128,255), size.width, size.height) + ret:addChild(background, -1) + + + -- PVR premultiplied + local pvr1 = cc.Sprite:create("Images/grossinis_sister1-testalpha_premult.pvr") + ret:addChild(pvr1, 0) + pvr1:setPosition(cc.p(size.width/4*1, size.height/2)) + transformSprite(pvr1) + + -- PVR non-premultiplied + local pvr2 = cc.Sprite:create("Images/grossinis_sister1-testalpha_nopremult.pvr") + ret:addChild(pvr2, 0) + pvr2:setPosition(cc.p(size.width/4*2, size.height/2)) + transformSprite(pvr2) + + -- PNG + cc.Texture2D:setDefaultAlphaPixelFormat(cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) + cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png") + local png = cc.Sprite:create("Images/grossinis_sister1-testalpha.png") + ret:addChild(png, 0) + png:setPosition(cc.p(size.width/4*3, size.height/2)) + transformSprite(png) + return ret +end + + +function Texture2dTestMain() + cclog("Texture2dTestMain") + Helper.index = 1 + local scene = cc.Scene:create() + Helper.createFunctionTable = { + TextureMemoryAlloc, + TextureAlias, + TexturePVRMipMap, + TexturePVRMipMap2, + TexturePVRNonSquare, + TexturePVRNPOT4444, + TexturePVRNPOT8888, + TexturePVR, + TexturePVR2BPP, + TexturePVR2BPPv3, + TexturePVR4BPP, + TexturePVR4BPPv3, + TexturePVRII4BPPv3, + TexturePVRRGBA8888, + TexturePVRRGBA8888v3, + TexturePVRBGRA8888, + TexturePVRBGRA8888v3, + TexturePVRRGBA4444, + TexturePVRRGBA4444v3, + TexturePVRRGBA4444GZ, + TexturePVRRGBA4444CCZ, + TexturePVRRGBA5551, + TexturePVRRGBA5551v3, + TexturePVRRGB565, + TexturePVRRGB565v3, + TexturePVRRGB888, + TexturePVRRGB888v3, + TexturePVRA8, + TexturePVRA8v3, + TexturePVRI8, + TexturePVRI8v3, + TexturePVRAI88, + TexturePVRAI88v3, + TexturePVRv3Premult, + TexturePVRBadEncoding, + TexturePNG, + TextureJPEG, + TextureTIFF, + TextureWEBP, + TextureMipMap, + TexturePixelFormat, + TextureBlend, + TextureAsync, + TextureGlClamp, + TextureGlRepeat, + TextureSizeTest, + TextureCache1, + TextureDrawAtPoint, + TextureDrawInRect + } + scene:addChild(TextureMemoryAlloc()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/TileMapTest/TileMapTest.lua b/samples/lua-tests/src/TileMapTest/TileMapTest.lua new file mode 100644 index 0000000000..389fe7721f --- /dev/null +++ b/samples/lua-tests/src/TileMapTest/TileMapTest.lua @@ -0,0 +1,1276 @@ +local size = cc.Director:getInstance():getWinSize() +local scheduler = cc.Director:getInstance():getScheduler() + +local kTagTileMap = 1 + + +local function createTileDemoLayer(title, subtitle) + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + local titleStr = title == nil and "No title" or title + local subTitleStr = subtitle == nil and "drag the screen" or subtitle + Helper.titleLabel:setString(titleStr) + Helper.subtitleLabel:setString(subTitleStr) + + local function onTouchesMoved(touches, event ) + local diff = touches[1]:getDelta() + local node = layer:getChildByTag(kTagTileMap) + local currentPosX, currentPosY= node:getPosition() + node:setPosition(cc.p(currentPosX + diff.x, currentPosY + diff.y)) + end + + local listener = cc.EventListenerTouchAllAtOnce:create() + listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED ) + local eventDispatcher = layer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer) + + return layer +end +-------------------------------------------------------------------- +-- +-- TileMapTest +-- +-------------------------------------------------------------------- +local function TileMapTest() + local layer = createTileDemoLayer("TileMapAtlas") + + local map = cc.TileMapAtlas:create(s_TilesPng, s_LevelMapTga, 16, 16) + -- Convert it to "alias" (GL_LINEAR filtering) + map:getTexture():setAntiAliasTexParameters() + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + -- If you are not going to use the Map, you can free it now + -- NEW since v0.7 + map:releaseMap() + + layer:addChild(map, 0, kTagTileMap) + + map:setAnchorPoint( cc.p(0, 0.5) ) + + local scale = cc.ScaleBy:create(4, 0.8) + local scaleBack = scale:reverse() + + local seq = cc.Sequence:create(scale, scaleBack) + + map:runAction(cc.RepeatForever:create(seq)) + + return layer +end + +-------------------------------------------------------------------- +-- +-- TileMapEditTest +-- +-------------------------------------------------------------------- +local function TileMapEditTest() + local layer = createTileDemoLayer("Editable TileMapAtlas") + local map = cc.TileMapAtlas:create(s_TilesPng, s_LevelMapTga, 16, 16) + -- Create an Aliased Atlas + map:getTexture():setAliasTexParameters() + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + -- If you are not going to use the Map, you can free it now + -- [tilemap releaseMap) + -- And if you are going to use, it you can access the data with: + local function updateMap(dt) + + -- IMPORTANT + -- The only limitation is that you cannot change an empty, or assign an empty tile to a tile + -- The value 0 not rendered so don't assign or change a tile with value 0 + + local tilemap = tolua.cast(layer:getChildByTag(kTagTileMap), "cc.TileMapAtlas") + + -- + -- For example you can iterate over all the tiles + -- using this code, but try to avoid the iteration + -- over all your tiles in every frame. It's very expensive + -- for(int x=0 x < tilemap.tgaInfo:width x++) + -- for(int y=0 y < tilemap.tgaInfo:height y++) + -- Color3B c =[tilemap getTileAt:local Make(x,y)) + -- if( c.r != 0 ) + -- --------cclog("%d,%d = %d", x,y,c.r) + -- end + -- end + -- end + + -- NEW since v0.7 + local c = tilemap:getTileAt(cc.p(13,21)) + c.r = c.r + 1 + c.r = c.r % 50 + + if( c.r==0) then + c.r=1 + end + -- NEW since v0.7 + tilemap:setTile(c, cc.p(13,21) ) + end + + local schedulerEntry = nil + local function onNodeEvent(event) + if event == "enter" then + schedulerEntry = scheduler:scheduleScriptFunc(updateMap, 0.2, false) + elseif event == "exit" then + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + + layer:registerScriptHandler(onNodeEvent) + + layer:addChild(map, 0, kTagTileMap) + + map:setAnchorPoint( cc.p(0, 0) ) + map:setPosition( cc.p(-20,-200) ) + + return layer +end + + +-------------------------------------------------------------------- +-- +-- TMXOrthoTest +-- +-------------------------------------------------------------------- +local function TMXOrthoTest() + local layer = createTileDemoLayer("TMX Orthogonal test") + -- + -- Test orthogonal with 3d camera and anti-alias textures + -- + -- it should not flicker. No artifacts should appear + -- + --local color = cc.LayerColor:create( cc.c4b(64,64,64,255) ) + --addChild(color, -1) + + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test2.tmx") + layer:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + local pChildrenArray = map:getChildren() + local child = nil + local pObject = nil + local i = 0 + local len = table.getn(pChildrenArray) + for i = 0, len-1, 1 do + pObject = pChildrenArray[i + 1] + child = tolua.cast(pObject, "cc.SpriteBatchNode") + + if child == nil then + break + end + + child:getTexture():setAntiAliasTexParameters() + end + + -- local x = 0 + -- local y = 0 + -- local z = 0 + -- x, y, z = map:getCamera():getEye() + -- cclog("before eye x="..x..",y="..y..",z="..z) + -- map:getCamera():setEye(x-200, y, z+300) + -- x, y, z = map:getCamera():getEye() + -- cclog("after eye x="..x..",y="..y..",z="..z) + + + local function onNodeEvent(event) + if event == "enter" then + cc.Director:getInstance():setProjection(cc.DIRECTOR_PROJECTION3_D ) + elseif event == "exit" then + cc.Director:getInstance():setProjection(cc.DIRECTOR_PROJECTION2_D ) + end + end + + layer:registerScriptHandler(onNodeEvent) + + + return layer +end + +-------------------------------------------------------------------- +-- +-- TMXOrthoTest2 +-- +-------------------------------------------------------------------- +local function TMXOrthoTest2() + local layer = createTileDemoLayer("TMX Ortho test2") + + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test1.tmx") + layer:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + local pChildrenArray = map:getChildren() + local child = nil + local pObject = nil + local i = 0 + local len = table.getn(pChildrenArray) + + for i = 0, len-1, 1 do + child = tolua.cast(pChildrenArray[i + 1], "cc.SpriteBatchNode") + + if child == nil then + break + end + child:getTexture():setAntiAliasTexParameters() + end + + map:runAction( cc.ScaleBy:create(2, 0.5) ) + return layer +end + +-------------------------------------------------------------------- +-- +-- TMXOrthoTest3 +-- +-------------------------------------------------------------------- +local function TMXOrthoTest3() + local layer = createTileDemoLayer("TMX anchorPoint test") + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test3.tmx") + layer:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + local pChildrenArray = map:getChildren() + local child = nil + local pObject = nil + local i = 0 + local len = table.getn(pChildrenArray) + + for i = 0, len-1, 1 do + child = tolua.cast(pChildrenArray[i + 1], "cc.SpriteBatchNode") + + if child == nil then + break + end + child:getTexture():setAntiAliasTexParameters() + end + + map:setScale(0.2) + map:setAnchorPoint( cc.p(0.5, 0.5) ) + return layer +end + + +-------------------------------------------------------------------- +-- +-- TMXOrthoTest4 +-- +-------------------------------------------------------------------- +local function TMXOrthoTest4() + local ret = createTileDemoLayer("TMX width/height test") + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test4.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s1 = map:getContentSize() + cclog("ContentSize: %f, %f", s1.width,s1.height) + + local pChildrenArray = map:getChildren() + local child = nil + local pObject = nil + local i = 0 + local len = table.getn(pChildrenArray) + + for i = 0, len-1, 1 do + child = tolua.cast(pChildrenArray[i + 1], "cc.SpriteBatchNode") + + if child == nil then + break + end + child:getTexture():setAntiAliasTexParameters() + end + + map:setAnchorPoint(cc.p(0, 0)) + + local layer = map:getLayer("Layer 0") + local s = layer:getLayerSize() + + local sprite = layer:getTileAt(cc.p(0,0)) + sprite:setScale(2) + sprite = layer:getTileAt(cc.p(s.width-1,0)) + sprite:setScale(2) + sprite = layer:getTileAt(cc.p(0,s.height-1)) + sprite:setScale(2) + sprite = layer:getTileAt(cc.p(s.width-1,s.height-1)) + sprite:setScale(2) + + local schedulerEntry = nil + + local function removeSprite(dt) + scheduler:unscheduleScriptEntry(schedulerEntry) + schedulerEntry = nil + local map = tolua.cast(ret:getChildByTag(kTagTileMap), "cc.TMXTiledMap") + local layer0 = map:getLayer("Layer 0") + local s = layer0:getLayerSize() + + local sprite = layer0:getTileAt( cc.p(s.width-1,0) ) + layer0:removeChild(sprite, true) + end + + + local function onNodeEvent(event) + if event == "enter" then + schedulerEntry = scheduler:scheduleScriptFunc(removeSprite, 2, false) + elseif event == "exit" and schedulerEntry ~= nil then + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + + ret:registerScriptHandler(onNodeEvent) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXReadWriteTest +-- +-------------------------------------------------------------------- +local SID_UPDATECOL = 100 +local SID_REPAINTWITHGID = 101 +local SID_REMOVETILES = 102 + +local function TMXReadWriteTest() + local ret = createTileDemoLayer("TMX Read/Write test") + local m_gid = 0 + local m_gid2 = 0 + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test2.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + + local layer = map:getLayer("Layer 0") + layer:getTexture():setAntiAliasTexParameters() + + map:setScale( 1 ) + + local tile0 = layer:getTileAt(cc.p(1,63)) + local tile1 = layer:getTileAt(cc.p(2,63)) + local tile2 = layer:getTileAt(cc.p(3,62))--cc.p(1,62)) + local tile3 = layer:getTileAt(cc.p(2,62)) + tile0:setAnchorPoint( cc.p(0.5, 0.5) ) + tile1:setAnchorPoint( cc.p(0.5, 0.5) ) + tile2:setAnchorPoint( cc.p(0.5, 0.5) ) + tile3:setAnchorPoint( cc.p(0.5, 0.5) ) + + local move = cc.MoveBy:create(0.5, cc.p(0,160)) + local rotate = cc.RotateBy:create(2, 360) + local scale = cc.ScaleBy:create(2, 5) + local opacity = cc.FadeOut:create(2) + local fadein = cc.FadeIn:create(2) + local scaleback = cc.ScaleTo:create(1, 1) + + local function removeSprite(sender) + --------cclog("removing tile: %x", sender) + local node = tolua.cast(sender, "cc.Node") + if nil == node then + print("Errro node is nil") + end + local p = node:getParent() + + if p ~= nil then + p:removeChild(node, true) + end + ----------cclog("atlas quantity: %d", p:textureAtlas():totalQuads()) + end + + local finish = cc.CallFunc:create(removeSprite) + local seq0 = cc.Sequence:create(move, rotate, scale, opacity, fadein, scaleback, finish) + local seq1 = tolua.cast(seq0:clone(), "cc.Action") + local seq2 = tolua.cast(seq0:clone(), "cc.Action") + local seq3 = tolua.cast(seq0:clone(), "cc.Action") + + tile0:runAction(seq0) + tile1:runAction(seq1) + tile2:runAction(seq2) + tile3:runAction(seq3) + + + m_gid = layer:getTileGIDAt(cc.p(0,63)) + --------cclog("Tile GID at:(0,63) is: %d", m_gid) + local updateColScheduler = nil + local repainWithGIDScheduler = nil + local removeTilesScheduler = nil + + local function updateCol(dt) + + local map = tolua.cast(ret:getChildByTag(kTagTileMap), "cc.TMXTiledMap") + local layer = tolua.cast(map:getChildByTag(0), "cc.TMXLayer") + + --------cclog("++++atlas quantity: %d", layer:textureAtlas():getTotalQuads()) + --------cclog("++++children: %d", layer:getChildren():count() ) + + + local s = layer:getLayerSize() + local y = 0 + for y=0, s.height-1, 1 do + layer:setTileGID(m_gid2, cc.p(3, y)) + end + + m_gid2 = (m_gid2 + 1) % 80 + end + + local function repaintWithGID(dt) + -- unschedule:_cmd) + local map = tolua.cast(ret:getChildByTag(kTagTileMap), "cc.TMXTiledMap") + local layer = tolua.cast(map:getChildByTag(0), "cc.TMXLayer") + + local s = layer:getLayerSize() + local x = 0 + for x=0, s.width-1, 1 do + local y = s.height-1 + local tmpgid = layer:getTileGIDAt( cc.p(x, y) ) + layer:setTileGID(tmpgid+1, cc.p(x, y)) + end + end + + local function removeTiles(dt) + scheduler:unscheduleScriptEntry(removeTilesScheduler) + removeTilesScheduler = nil + local map = tolua.cast(ret:getChildByTag(kTagTileMap), "cc.TMXTiledMap") + local layer = tolua.cast(map:getChildByTag(0), "cc.TMXLayer") + local s = layer:getLayerSize() + local y = 0 + for y=0, s.height-1, 1 do + layer:removeTileAt( cc.p(5.0, y) ) + end + end + + local function onNodeEvent(event) + if event == "enter" then + updateColScheduler = scheduler:scheduleScriptFunc(updateCol, 2, false) + repainWithGIDScheduler = scheduler:scheduleScriptFunc(repaintWithGID, 2.05, false) + removeTilesScheduler = scheduler:scheduleScriptFunc(removeTiles, 1.0, false) + elseif event == "exit" then + if updateColScheduler ~= nil then + scheduler:unscheduleScriptEntry(updateColScheduler) + end + + if repainWithGIDScheduler ~= nil then + scheduler:unscheduleScriptEntry(repainWithGIDScheduler) + end + + if removeTilesScheduler ~= nil then + scheduler:unscheduleScriptEntry(removeTilesScheduler) + end + + end + end + + ret:registerScriptHandler(onNodeEvent) + --------cclog("++++atlas quantity: %d", layer:textureAtlas():getTotalQuads()) + --------cclog("++++children: %d", layer:getChildren():count() ) + + m_gid2 = 0 + return ret +end + + +-------------------------------------------------------------------- +-- +-- TMXHexTest +-- +-------------------------------------------------------------------- +local function TMXHexTest() + local ret = createTileDemoLayer("TMX Hex tes") + local color = cc.LayerColor:create( cc.c4b(64,64,64,255) ) + ret:addChild(color, -1) + + local map = cc.TMXTiledMap:create("TileMaps/hexa-test.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXIsoTest +-- +-------------------------------------------------------------------- +local function TMXIsoTest() + local ret = createTileDemoLayer("TMX Isometric test 0") + local color = cc.LayerColor:create( cc.c4b(64,64,64,255) ) + ret:addChild(color, -1) + + local map = cc.TMXTiledMap:create("TileMaps/iso-test.tmx") + ret:addChild(map, 0, kTagTileMap) + + -- move map to the center of the screen + local ms = map:getMapSize() + local ts = map:getTileSize() + map:runAction( cc.MoveTo:create(1.0, cc.p( -ms.width * ts.width/2, -ms.height * ts.height/2 )) ) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXIsoTest1 +-- +-------------------------------------------------------------------- +local function TMXIsoTest1() + local ret = createTileDemoLayer("TMX Isometric test + anchorPoint") + local color = cc.LayerColor:create( cc.c4b(64,64,64,255) ) + ret:addChild(color, -1) + + local map = cc.TMXTiledMap:create("TileMaps/iso-test1.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + map:setAnchorPoint(cc.p(0.5, 0.5)) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXIsoTest2 +-- +-------------------------------------------------------------------- +local function TMXIsoTest2() + local ret = createTileDemoLayer("TMX Isometric test 2") + local color = cc.LayerColor:create( cc.c4b(64,64,64,255) ) + ret:addChild(color, -1) + + local map = cc.TMXTiledMap:create("TileMaps/iso-test2.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + -- move map to the center of the screen + local ms = map:getMapSize() + local ts = map:getTileSize() + map:runAction( cc.MoveTo:create(1.0, cc.p( -ms.width * ts.width/2, -ms.height * ts.height/2 ) )) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXUncompressedTest +-- +-------------------------------------------------------------------- +local function TMXUncompressedTest() + local ret = createTileDemoLayer("TMX Uncompressed test") + local color = cc.LayerColor:create( cc.c4b(64,64,64,255) ) + ret:addChild(color, -1) + + local map = cc.TMXTiledMap:create("TileMaps/iso-test2-uncompressed.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + -- move map to the center of the screen + local ms = map:getMapSize() + local ts = map:getTileSize() + map:runAction(cc.MoveTo:create(1.0, cc.p( -ms.width * ts.width/2, -ms.height * ts.height/2 ) )) + + -- testing release map + local pChildrenArray = map:getChildren() + local layer = nil + local i = 0 + local len = table.getn(pChildrenArray) + for i = 0, len-1, 1 do + layer = tolua.cast(pChildrenArray[i + 1], "cc.TMXLayer") + if layer == nil then + break + end + layer:releaseMap() + end + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXTilesetTest +-- +-------------------------------------------------------------------- +local function TMXTilesetTest() + local ret = createTileDemoLayer("TMX Tileset test") + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test5.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + local layer = map:getLayer("Layer 0") + layer:getTexture():setAntiAliasTexParameters() + + layer = map:getLayer("Layer 1") + layer:getTexture():setAntiAliasTexParameters() + + layer = map:getLayer("Layer 2") + layer:getTexture():setAntiAliasTexParameters() + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXOrthoObjectsTest +-- +-------------------------------------------------------------------- +local function TMXOrthoObjectsTest() + local ret = createTileDemoLayer("TMX Ortho object test", "You should see a white box around the 3 platforms") + local map = cc.TMXTiledMap:create("TileMaps/ortho-objects.tmx") + ret:addChild(map, -1, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + --------cclog("---: Iterating over all the group objets") + local group = map:getObjectGroup("Object Group 1") + local objects = group:getObjects() + + local dict = nil + local i = 0 + local len = table.getn(objects) + + for i = 0, len-1, 1 do + dict = objects[i + 1] + + if dict == nil then + break + end + --------cclog("object: %x", dict) + end + + --------cclog("---: Fetching 1 object by name") + -- local platform = group:objectNamed("platform") + --------cclog("platform: %x", platform) + return ret +end + +local function draw() + + local map = tolua.cast(getChildByTag(kTagTileMap), "cc.TMXTiledMap") + local group = map:getObjectGroup("Object Group 1") + + local objects = group:getObjects() + local dict = nil + local i = 0 + local len = table.getn(objects) + for i = 0, len-1, 1 do + dict = objects[i + 1] + + if dict == nil then + break + end + + local key = "x" + local x = dict["x"] + key = "y" + local y = dict["y"]--dynamic_cast(dict:objectForKey("y")):getNumber() + key = "width" + local width = dict["width"]--dynamic_cast(dict:objectForKey("width")):getNumber() + key = "height" + local height = dict["height"]--dynamic_cast(dict:objectForKey("height")):getNumber() + + glLineWidth(3) + + cc.DrawPrimitives.drawLine( cc.p(x, y), cc.p((x+width), y) ) + cc.DrawPrimitives.drawLine( cc.p((x+width), y), cc.p((x+width), (y+height)) ) + cc.DrawPrimitives.drawLine( cc.p((x+width), (y+height)), cc.p(x, (y+height)) ) + cc.DrawPrimitives.drawLine( cc.p(x, (y+height)), cc.p(x, y) ) + + glLineWidth(1) + end +end + +-------------------------------------------------------------------- +-- +-- TMXIsoObjectsTest +-- +-------------------------------------------------------------------- + +local function TMXIsoObjectsTest() + local ret = createTileDemoLayer("TMX Iso object test", "You need to parse them manually. See bug #810") + local map = cc.TMXTiledMap:create("TileMaps/iso-test-objectgroup.tmx") + ret:addChild(map, -1, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + local group = map:getObjectGroup("Object Group 1") + + --UxMutableArray* objects = group:objects() + local objects = group:getObjects() + --UxMutableDictionary* dict + local dict = nil + local i = 0 + local len = table.getn(objects) + for i = 0, len-1, 1 do + dict = tolua.cast(objects[i + 1], "cc.Dictionary") + + if dict == nil then + break + end + --------cclog("object: %x", dict) + end + return ret +end + +local function draw() + + local map = tolua.cast(getChildByTag(kTagTileMap), "cc.TMXTiledMap") + local group = map:getObjectGroup("Object Group 1") + + local objects = group:getObjects() + local dict = nil + local i = 0 + local len = table.getn(objects) + for i = 0, len-1, 1 do + dict = tolua.cast(objects[i + 1], "cc.Dictionary") + + if dict == nil then + break + end + + local key = "x" + local x = (tolua.cast(dict:objectForKey(key), "cc.String")):intValue()--dynamic_cast(dict:objectForKey("x")):getNumber() + key = "y" + local y = (tolua.cast(dict:objectForKey(key), "cc.String")):intValue()--dynamic_cast(dict:objectForKey("y")):getNumber() + key = "width" + local width = (tolua.cast(dict:objectForKey(key), "cc.String")):intValue()--dynamic_cast(dict:objectForKey("width")):getNumber() + key = "height" + local height = (tolua.cast(dict:objectForKey(key), "cc.String")):intValue()--dynamic_cast(dict:objectForKey("height")):getNumber() + + glLineWidth(3) + + cc.DrawPrimitives.drawLine( cc.p(x,y), cc.p(x+width,y) ) + cc.DrawPrimitives.drawLine( cc.p(x+width,y), cc.p(x+width,y+height) ) + cc.DrawPrimitives.drawLine( cc.p(x+width,y+height), cc.p(x,y+height) ) + cc.DrawPrimitives.drawLine( cc.p(x,y+height), cc.p(x,y) ) + + glLineWidth(1) + end +end + +-------------------------------------------------------------------- +-- +-- TMXResizeTest +-- +-------------------------------------------------------------------- + +local function TMXResizeTest() + local ret = createTileDemoLayer("TMX resize test", "Should not crash. Testing issue #740") + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test5.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + local layer = map:getLayer("Layer 0") + local ls = layer:getLayerSize() + local x = 0 + local y = 0 + for y = 0, ls.height-1, 1 do + for x = 0, ls.width-1, 1 do + layer:setTileGID(1, cc.p( x, y ) ) + end + end + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXIsoZorder +-- +-------------------------------------------------------------------- +local function TMXIsoZorder() + local m_tamara = nil + local ret = createTileDemoLayer("TMX Iso Zorder", "Sprite should hide behind the trees") + local map = cc.TMXTiledMap:create("TileMaps/iso-test-zorder.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + map:setPosition(cc.p(-s.width/2,0)) + + m_tamara = cc.Sprite:create(s_pPathSister1) + map:addChild(m_tamara, table.getn(map:getChildren())) + m_tamara:retain() + local mapWidth = map:getMapSize().width * map:getTileSize().width + m_tamara:setPosition(CC_POINT_PIXELS_TO_POINTS(cc.p( mapWidth/2,0))) + m_tamara:setAnchorPoint(cc.p(0.5,0)) + + local move = cc.MoveBy:create(10, cc.p(300,250)) + local back = move:reverse() + local seq = cc.Sequence:create(move, back) + m_tamara:runAction( cc.RepeatForever:create(seq) ) + + local function repositionSprite(dt) + local p = cc.p(m_tamara:getPosition()) + p = CC_POINT_POINTS_TO_PIXELS(p) + local map = ret:getChildByTag(kTagTileMap) + + -- there are only 4 layers. (grass and 3 trees layers) + -- if tamara < 48, z=4 + -- if tamara < 96, z=3 + -- if tamara < 144,z=2 + + local newZ = 4 - (p.y / 48) + newZ = math.max(newZ,0) + + map:reorderChild(m_tamara, newZ) + end + + local schedulerEntry = nil + local function onNodeEvent(event) + if event == "enter" then + schedulerEntry = scheduler:scheduleScriptFunc(repositionSprite, 0, false) + elseif event == "exit" then + if m_tamara ~= nil then + m_tamara:release() + end + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXOrthoZorder +-- +-------------------------------------------------------------------- +local function TMXOrthoZorder() + local m_tamara = nil + local ret = createTileDemoLayer("TMX Ortho Zorder", "Sprite should hide behind the trees") + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test-zorder.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + m_tamara = cc.Sprite:create(s_pPathSister1) + map:addChild(m_tamara, table.getn(map:getChildren())) + m_tamara:retain() + m_tamara:setAnchorPoint(cc.p(0.5,0)) + + + local move = cc.MoveBy:create(10, cc.p(400,450)) + local back = move:reverse() + local seq = cc.Sequence:create(move, back) + m_tamara:runAction( cc.RepeatForever:create(seq)) + + local function repositionSprite(dt) + local p = cc.p(m_tamara:getPosition()) + p = CC_POINT_POINTS_TO_PIXELS(p) + local map = ret:getChildByTag(kTagTileMap) + + -- there are only 4 layers. (grass and 3 trees layers) + -- if tamara < 81, z=4 + -- if tamara < 162, z=3 + -- if tamara < 243,z=2 + + -- -10: customization for this particular sample + local newZ = 4 - ( (p.y-10) / 81) + newZ = math.max(newZ,0) + map:reorderChild(m_tamara, newZ) + end + + local schedulerEntry = nil + local function onNodeEvent(event) + if event == "enter" then + schedulerEntry = scheduler:scheduleScriptFunc(repositionSprite, 0, false) + elseif event == "exit" then + if m_tamara ~= nil then + m_tamara:release() + end + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXIsoVertexZ +-- +-------------------------------------------------------------------- +local function TMXIsoVertexZ() + local m_tamara = nil + local ret = createTileDemoLayer("TMX Iso VertexZ", "Sprite should hide behind the trees") + local map = cc.TMXTiledMap:create("TileMaps/iso-test-vertexz.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + map:setPosition( cc.p(-s.width/2,0) ) + cclog("ContentSize: %f, %f", s.width,s.height) + + -- because I'm lazy, I'm reusing a tile as an sprite, but since this method uses vertexZ, you + -- can use any cc.Sprite and it will work OK. + local layer = map:getLayer("Trees") + m_tamara = layer:getTileAt( cc.p(29,29) ) + m_tamara:retain() + + local move = cc.MoveBy:create(10, cc.pMul( cc.p(300,250), 1/CC_CONTENT_SCALE_FACTOR() ) ) + local back = move:reverse() + local seq = cc.Sequence:create(move, back) + m_tamara:runAction( cc.RepeatForever:create(seq) ) + local function repositionSprite(dt) + + -- tile height is 64x32 + -- map size: 30x30 + local p = cc.p(m_tamara:getPosition()) + p = CC_POINT_POINTS_TO_PIXELS(p) + local newZ = -(p.y+32) /16 + m_tamara:setPositionZ( newZ ) + end + + local schedulerEntry = nil + local function onNodeEvent(event) + if event == "enter" then + -- TIP: 2d projection should be used + cc.Director:getInstance():setProjection(cc.DIRECTOR_PROJECTION2_D ) + schedulerEntry = scheduler:scheduleScriptFunc(repositionSprite, 0, false) + elseif event == "exit" then + -- At exit use any other projection. + -- cc.Director:getInstance():setProjection:cc.DIRECTOR_PROJECTION3_D ) + + if m_tamara ~= nil then + m_tamara:release() + end + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + + ret:registerScriptHandler(onNodeEvent) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXOrthoVertexZ +-- +-------------------------------------------------------------------- +local function TMXOrthoVertexZ() + local m_tamara = nil + local ret = createTileDemoLayer("TMX Ortho vertexZ", "Sprite should hide behind the trees") + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test-vertexz.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + -- because I'm lazy, I'm reusing a tile as an sprite, but since this method uses vertexZ, you + -- can use any cc.Sprite and it will work OK. + local layer = map:getLayer("trees") + m_tamara = layer:getTileAt(cc.p(0,11)) + cclog("vertexZ:"..m_tamara:getPositionZ()) + m_tamara:retain() + + local move = cc.MoveBy:create(10, cc.pMul( cc.p(400,450), 1/CC_CONTENT_SCALE_FACTOR())) + local back = move:reverse() + local seq = cc.Sequence:create(move, back) + m_tamara:runAction( cc.RepeatForever:create(seq)) + + local function repositionSprite(dt) + -- tile height is 101x81 + -- map size: 12x12 + local p = cc.p(m_tamara:getPosition()) + p = CC_POINT_POINTS_TO_PIXELS(p) + m_tamara:setPositionZ( -( (p.y+81) /81) ) + end + + local schedulerEntry = nil + local function onNodeEvent(event) + if event == "enter" then + -- TIP: 2d projection should be used + cc.Director:getInstance():setProjection(cc.DIRECTOR_PROJECTION2_D ) + schedulerEntry = scheduler:scheduleScriptFunc(repositionSprite, 0, false) + elseif event == "exit" then + -- At exit use any other projection. + -- cc.Director:getInstance():setProjection:cc.DIRECTOR_PROJECTION3_D ) + if m_tamara ~= nil then + m_tamara:release() + end + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXIsoMoveLayer +-- +-------------------------------------------------------------------- +local function TMXIsoMoveLayer() + local ret = createTileDemoLayer("TMX Iso Move Layer", "Trees should be horizontally aligned") + local map = cc.TMXTiledMap:create("TileMaps/iso-test-movelayer.tmx") + ret:addChild(map, 0, kTagTileMap) + + map:setPosition(cc.p(-700,-50)) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + return ret +end + + +-------------------------------------------------------------------- +-- +-- TMXOrthoMoveLayer +-- +-------------------------------------------------------------------- +local function TMXOrthoMoveLayer() + local ret = createTileDemoLayer("TMX Ortho Move Layer", "Trees should be horizontally aligned") + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test-movelayer.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXTilePropertyTest +-- +-------------------------------------------------------------------- + +local function TMXTilePropertyTest() + local ret = createTileDemoLayer("TMX Tile Property Test", "In the console you should see tile properties") + local map = cc.TMXTiledMap:create("TileMaps/ortho-tile-property.tmx") + ret:addChild(map ,0 ,kTagTileMap) + local i = 0 + for i=1, 20, 1 do + cclog("GID:%i, Properties:", i)--, map:getPropertiesForGID(i)) + end + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXOrthoFlipTest +-- +-------------------------------------------------------------------- + +local function TMXOrthoFlipTest() + local ret = createTileDemoLayer("TMX tile flip test") + local map = cc.TMXTiledMap:create("TileMaps/ortho-rotation-test.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + local i = 0 + for i = 0, table.getn(map:getChildren())-1, 1 do + local child = tolua.cast(map:getChildren()[i + 1], "cc.SpriteBatchNode") + child:getTexture():setAntiAliasTexParameters() + end + + local action = cc.ScaleBy:create(2, 0.5) + map:runAction(action) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXOrthoFlipRunTimeTest +-- +-------------------------------------------------------------------- + +local function TMXOrthoFlipRunTimeTest() + local ret = createTileDemoLayer("TMX tile flip run time test", "in 2 sec bottom left tiles will flip") + local map = cc.TMXTiledMap:create("TileMaps/ortho-rotation-test.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + local i = 0 + for i = 0, table.getn(map:getChildren())-1, 1 do + local child = tolua.cast(map:getChildren()[i + 1], "cc.SpriteBatchNode") + child:getTexture():setAntiAliasTexParameters() + end + + local action = cc.ScaleBy:create(2, 0.5) + map:runAction(action) + local function flipIt(dt) + + -- local map = tolua.cast(ret:getChildByTag(kTagTileMap), "TMXTiledMap") + -- local layer = map:getLayer("Layer 0") + + -- --blue diamond + -- local tileCoord = cc.p(1,10) + -- local flags = 0 + -- local GID = layer:getTileGIDAt(tileCoord, (ccTMXTileFlags*)&flags) + -- -- Vertical + -- if( flags & kcc.TMXTileVerticalFlag ) + -- flags &= ~kcc.TMXTileVerticalFlag + -- else + -- flags |= kcc.TMXTileVerticalFlag + -- layer:setTileGID(GID ,tileCoord, (ccTMXTileFlags)flags) + + + -- tileCoord = cc.p(1,8) + -- GID = layer:getTileGIDAt(tileCoord, (ccTMXTileFlags*)&flags) + -- -- Vertical + -- if( flags & kcc.TMXTileVerticalFlag ) + -- flags &= ~kcc.TMXTileVerticalFlag + -- else + -- flags |= kcc.TMXTileVerticalFlag + -- layer:setTileGID(GID ,tileCoord, (ccTMXTileFlags)flags) + + + -- tileCoord = cc.p(2,8) + -- GID = layer:getTileGIDAt(tileCoord, (ccTMXTileFlags*)&flags) + -- -- Horizontal + -- if( flags & kcc.TMXTileHorizontalFlag ) + -- flags &= ~kcc.TMXTileHorizontalFlag + -- else + -- flags |= kcc.TMXTileHorizontalFlag + -- layer:setTileGID(GID, tileCoord, (ccTMXTileFlags)flags) + end + local schedulerEntry = nil + local function onNodeEvent(event) + if event == "enter" then + schedulerEntry = scheduler:scheduleScriptFunc(flipIt, 1.0, false) + elseif event == "exit" then + scheduler:unscheduleScriptEntry(schedulerEntry) + end + end + return ret +end + + +-------------------------------------------------------------------- +-- +-- TMXOrthoFromXMLTest +-- +-------------------------------------------------------------------- + +local function TMXOrthoFromXMLTest() + local ret = createTileDemoLayer("TMX created from XML test") + local resources = "TileMaps" -- partial paths are OK as resource paths. + local file = resources.."/orthogonal-test1.tmx" + + local str = cc.FileUtils:getInstance():getStringFromFile(file) + -- cc.ASSERT(str != NULL, "Unable to open file") + if (str == nil) then + cclog("Unable to open file") + end + + local map = cc.TMXTiledMap:createWithXML(str ,resources) + ret:addChild(map, 0, kTagTileMap) + + local s = map:getContentSize() + cclog("ContentSize: %f, %f", s.width,s.height) + + local i = 0 + local len = table.getn(map:getChildren()) + for i = 0, len-1, 1 do + local child = tolua.cast(map:getChildren()[i + 1], "cc.SpriteBatchNode") + child:getTexture():setAntiAliasTexParameters() + end + + local action = cc.ScaleBy:create(2, 0.5) + map:runAction(action) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXBug987 +-- +-------------------------------------------------------------------- +local function TMXBug987() + local ret = createTileDemoLayer("TMX Bug 987", "You should see an square") + local map = cc.TMXTiledMap:create("TileMaps/orthogonal-test6.tmx") + ret:addChild(map, 0, kTagTileMap) + + local s1 = map:getContentSize() + cclog("ContentSize: %f, %f", s1.width,s1.height) + + local childs = map:getChildren() + + local i = 0 + local len = table.getn(childs) + local pNode = nil + for i = 0, len-1, 1 do + pNode = tolua.cast(childs[i + 1], "cc.TMXLayer") + if pNode == nil then + break + end + pNode:getTexture():setAntiAliasTexParameters() + end + + map:setAnchorPoint(cc.p(0, 0)) + local layer = map:getLayer("Tile Layer 1") + layer:setTileGID(3, cc.p(2,2)) + return ret +end + +-------------------------------------------------------------------- +-- +-- TMXBug787 +-- +-------------------------------------------------------------------- +local function TMXBug787() + local ret = createTileDemoLayer("TMX Bug 787", "You should see a map") + local map = cc.TMXTiledMap:create("TileMaps/iso-test-bug787.tmx") + ret:addChild(map, 0, kTagTileMap) + map:setScale(0.25) + return ret +end + +function TileMapTestMain() + cclog("TileMapTestMain") + Helper.index = 1 + cc.Director:getInstance():setDepthTest(true) + local scene = cc.Scene:create() + + Helper.createFunctionTable = { + TileMapTest, + TileMapEditTest, + TMXOrthoTest, + TMXOrthoTest2, + TMXOrthoTest3, + TMXOrthoTest4, + TMXReadWriteTest, + TMXHexTest, + TMXIsoTest, + TMXIsoTest1, + TMXIsoTest2, + TMXUncompressedTest, + TMXTilesetTest, + TMXOrthoObjectsTest, + TMXIsoObjectsTest, + TMXResizeTest, + TMXIsoZorder, + TMXOrthoZorder, + TMXIsoVertexZ, + TMXOrthoVertexZ, + TMXIsoMoveLayer, + TMXOrthoMoveLayer, + TMXTilePropertyTest, + TMXOrthoFlipTest, + TMXOrthoFlipRunTimeTest, + TMXOrthoFromXMLTest, + TMXBug987, + TMXBug787 + } + scene:addChild(TileMapTest()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/TouchesTest/Ball.lua b/samples/lua-tests/src/TouchesTest/Ball.lua new file mode 100644 index 0000000000..7d5603e41d --- /dev/null +++ b/samples/lua-tests/src/TouchesTest/Ball.lua @@ -0,0 +1,95 @@ + +require "extern" +require "src/VisibleRect" +require "src/TouchesTest/Paddle" + +Ball = class("Ball", function(texture) + return cc.Sprite:createWithTexture(texture) +end) + +Ball.__index = Ball + +Ball.m_velocity = cc.p(0,0) + +local M_PI = 3.1415926 + +function Ball:radius() + local size = self:getTexture():getContentSize() + return size.width/2 +end + +function Ball:move(delta) + local getPosition = cc.p(self:getPosition()) + local position = cc.pMul(self.m_velocity, delta) + self:setPosition( cc.pAdd(getPosition, position) ) + + if (getPosition.x > VisibleRect:right().x - self:radius()) then + self:setPosition( cc.p( VisibleRect:right().x - self:radius(), getPosition.y) ) + self.m_velocity.x = self.m_velocity.x * -1 + elseif (getPosition.x < VisibleRect:left().x + self:radius()) then + self:setPosition( cc.p(VisibleRect:left().x + self:radius(), getPosition.y) ) + self.m_velocity.x = self.m_velocity.x * -1 + end +end + +function Ball:collideWithPaddle(paddle) + local paddleRect = paddle:rect() + local paddleGetPosition = cc.p(paddle:getPosition()) + local selfGetPosition = cc.p(self:getPosition()) + + paddleRect.x = paddleRect.x + paddleGetPosition.x + paddleRect.y = paddleRect.y + paddleGetPosition.y + + local lowY = cc.rectGetMinY(paddleRect) + local midY = cc.rectGetMidY(paddleRect) + local highY = cc.rectGetMaxY(paddleRect) + + local leftX = cc.rectGetMinX(paddleRect) + local rightX = cc.rectGetMaxX(paddleRect) + + if (selfGetPosition.x > leftX and selfGetPosition.x < rightX) then + + local hit = false + local angleOffset = 0.0 + + if (selfGetPosition.y > midY and selfGetPosition.y <= highY + self:radius()) then + self:setPosition( cc.p(selfGetPosition.x, highY + self:radius()) ) + hit = true + angleOffset = M_PI / 2 + elseif (selfGetPosition.y < midY and selfGetPosition.y >= lowY - self:radius()) then + self:setPosition( cc.p(selfGetPosition.x, lowY - self:radius()) ) + hit = true + angleOffset = -M_PI / 2 + end + + if (hit) then + local hitAngle = cc.pToAngleSelf(cc.pSub(paddleGetPosition, paddleGetPosition)) + angleOffset + + local scalarVelocity = cc.pGetLength(self.m_velocity) * 1.05 + local velocityAngle = -cc.pToAngleSelf(self.m_velocity) + 0.5 * hitAngle + + self.m_velocity = cc.pMul(cc.pForAngle(velocityAngle), scalarVelocity) + end + end + +end + +function Ball:setVelocity(velocity) + self.m_velocity = velocity +end + +function Ball:getVelocity() + return self.m_velocity +end + +function Ball.ballWithTexture(aTexture) + + if(aTexture == nil) then + cclog("in ballWithTexture aTexture == nil") + end + + local ball = Ball.new(aTexture) + --ball:autorelease() + return ball +end + diff --git a/samples/lua-tests/src/TouchesTest/Paddle.lua b/samples/lua-tests/src/TouchesTest/Paddle.lua new file mode 100644 index 0000000000..65a319c44b --- /dev/null +++ b/samples/lua-tests/src/TouchesTest/Paddle.lua @@ -0,0 +1,70 @@ +require "extern" +require "src/VisibleRect" + +Paddle = class("Paddle", function(texture) + return cc.Sprite:createWithTexture(texture) +end) + +Paddle.__index = Paddle + +local kPaddleStateGrabbed = 0 +local kPaddleStateUngrabbed = 1 + +Paddle._state = kPaddleStateGrabbed + + +function Paddle:rect() + local s = self:getTexture():getContentSize() + return cc.rect(-s.width / 2, -s.height / 2, s.width, s.height) +end + +function Paddle:containsTouchLocation(x,y) + local position = cc.p(self:getPosition()) + local s = self:getTexture():getContentSize() + local touchRect = cc.rect(-s.width / 2 + position.x, -s.height / 2 + position.y, s.width, s.height) + local b = cc.rectContainsPoint(touchRect, cc.p(x,y)) + return b +end + +function Paddle:paddleWithTexture(aTexture) + local pPaddle = Paddle.new(aTexture) + pPaddle._state = kPaddleStateUngrabbed + pPaddle:registerScriptHandler(function(tag) + if "enter" == tag then + pPaddle:onEnter() + elseif "exit" == tag then + end +end) + return pPaddle +end + +function Paddle:onEnter() + local listenner = cc.EventListenerTouchOneByOne:create() + listenner:setSwallowTouches(true) + listenner:registerScriptHandler(function(touch, event) + print(string.format("Paddle::onTouchBegan id = %d, x = %f, y = %f", touch:getId(), touch:getLocation().x, touch:getLocation().y)) + if (self._state ~= kPaddleStateUngrabbed) then + return false + end + + if not self:containsTouchLocation(touch:getLocation().x,touch:getLocation().y) then + return false + end + + self._state = kPaddleStateGrabbed + return true + end,cc.Handler.EVENT_TOUCH_BEGAN ) + listenner:registerScriptHandler(function(touch, event) + print(string.format("Paddle::onTouchMoved id = %d, x = %f, y = %f", touch:getId(), touch:getLocation().x, touch:getLocation().y)) + assert(self._state == kPaddleStateGrabbed, "Paddle - Unexpected state!") + local touchPoint = touch:getLocation() + local curPosX,curPosY = self:getPosition() + self:setPosition(cc.p(touchPoint.x,curPosY)) + end,cc.Handler.EVENT_TOUCH_MOVED ) + listenner:registerScriptHandler(function(touch, event) + assert(self._state == kPaddleStateGrabbed, "Paddle - Unexpected state!") + self._state = kPaddleStateUngrabbed + end,cc.Handler.EVENT_TOUCH_ENDED ) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listenner, self) +end diff --git a/samples/lua-tests/src/TouchesTest/TouchesTest.lua b/samples/lua-tests/src/TouchesTest/TouchesTest.lua new file mode 100644 index 0000000000..241e5a369f --- /dev/null +++ b/samples/lua-tests/src/TouchesTest/TouchesTest.lua @@ -0,0 +1,111 @@ +require "src/testResource" +require "src/TouchesTest/Ball" +require "src/TouchesTest/Paddle" +require "src/VisibleRect" + +local kHighPlayer = 0 +local kLowPlayer = 1 + +local kStatusBarHeight = 0.0 + +local kSpriteTag = 0 + +local m_ball = nil +local m_ballStartingVelocity = nil +local m_paddles = {} +local layer = nil + +local function backCallback(sender) + local scene = cc.Scene:create() + scene:addChild(CreateBackMenuItem()) + cc.Director:getInstance():replaceScene(scene) +end + + +local function resetAndScoreBallForPlayer(player) + m_ballStartingVelocity = cc.pMul(m_ballStartingVelocity, -1.1); + m_ball:setVelocity( m_ballStartingVelocity ); + m_ball:setPosition( VisibleRect:center() ); +end + +local function doStep(delta) + m_ball:move(delta); + + for _,paddle in ipairs(m_paddles) do + + if(paddle == nil) then + break + end + m_ball:collideWithPaddle( paddle ); + end + + local ballPosition = cc.p(m_ball:getPosition()) + if (ballPosition.y > VisibleRect:top().y - kStatusBarHeight + m_ball:radius()) then + resetAndScoreBallForPlayer( kLowPlayer ); + elseif (ballPosition.y < VisibleRect:bottom().y-m_ball:radius()) then + resetAndScoreBallForPlayer( kHighPlayer ); + end + m_ball:draw(); +end + +local function CreateTouchesLayer() + layer = cc.Layer:create() + + m_ballStartingVelocity = cc.p(20.0, -100.0); + local mgr = cc.Director:getInstance():getTextureCache() + local texture = mgr:addImage(s_Ball) + m_ball = Ball.ballWithTexture(texture); + + m_ball:setPosition( VisibleRect:center() ); + m_ball:setVelocity( m_ballStartingVelocity ); + layer:addChild( m_ball ); + m_ball:retain(); + + local paddleTexture = cc.Director:getInstance():getTextureCache():addImage(s_Paddle); + + local paddlesM = {} + + local paddle = Paddle:paddleWithTexture(paddleTexture); + paddle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:bottom().y + 15) ); + paddlesM[#paddlesM+1] = paddle + + paddle = Paddle:paddleWithTexture( paddleTexture ); + paddle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - kStatusBarHeight - 15) ); + paddlesM[#paddlesM+1] = paddle + + paddle = Paddle:paddleWithTexture( paddleTexture ); + paddle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:bottom().y + 100) ); + paddlesM[#paddlesM+1] = paddle + + paddle = Paddle:paddleWithTexture( paddleTexture ); + paddle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - kStatusBarHeight - 100) ); + paddlesM[#paddlesM+1] = paddle + + m_paddles = paddlesM + + for i = 1,#paddlesM do + paddle = paddlesM[i] + + if(paddle == nil) then + break + end + + layer:addChild(paddle); + end + + -- schedule + layer:scheduleUpdateWithPriorityLua(doStep, 0) + return layer +end + +local function nextAction() + return CreateTouchesLayer() +end + +function TouchesTest() + local scene = cc.Scene:create() + scene:addChild(nextAction()) + scene:addChild(CreateBackMenuItem()) + return scene +end + diff --git a/samples/lua-tests/src/TransitionsTest/TransitionsName.lua b/samples/lua-tests/src/TransitionsTest/TransitionsName.lua new file mode 100644 index 0000000000..c49f4caae2 --- /dev/null +++ b/samples/lua-tests/src/TransitionsTest/TransitionsName.lua @@ -0,0 +1,94 @@ +require "src/helper" + +Transition_Table = +{ + "CCTransitionJumpZoom", + "CCTransitionProgressRadialCCW", + "CCTransitionProgressRadialCW", + "CCTransitionProgressHorizontal", + "CCTransitionProgressVertical", + "CCTransitionProgressInOut", + "CCTransitionProgressOutIn", + "CCTransitionCrossFade", + "TransitionPageForward", + "TransitionPageBackward", + "CCTransitionFadeTR", + "CCTransitionFadeBL", + "CCTransitionFadeUp", + "CCTransitionFadeDown", + "CCTransitionTurnOffTiles", + "CCTransitionSplitRows", + "CCTransitionSplitCols", + "CCTransitionFade", + "FadeWhiteTransition", + "FlipXLeftOver", + "FlipXRightOver", + "FlipYUpOver", + "FlipYDownOver", + "FlipAngularLeftOver", + "FlipAngularRightOver", + "ZoomFlipXLeftOver", + "ZoomFlipXRightOver", + "ZoomFlipYUpOver", + "ZoomFlipYDownOver", + "ZoomFlipAngularLeftOver", + "ZoomFlipAngularRightOver", + "CCTransitionShrinkGrow", + "CCTransitionRotoZoom", + "CCTransitionMoveInL", + "CCTransitionMoveInR", + "CCTransitionMoveInT", + "CCTransitionMoveInB", + "CCTransitionSlideInL", + "CCTransitionSlideInR", + "CCTransitionSlideInT", + "CCTransitionSlideInB", + "MAX_LAYER", +} + +Transition_Table = CreateEnumTable(Transition_Table) + +Transition_Name = +{ + [0] = "CCTransitionJumpZoom", + "CCTransitionProgressRadialCCW", + "CCTransitionProgressRadialCW", + "CCTransitionProgressHorizontal", + "CCTransitionProgressVertical", + "CCTransitionProgressInOut", + "CCTransitionProgressOutIn", + "CCTransitionCrossFade", + "TransitionPageForward", + "TransitionPageBackward", + "CCTransitionFadeTR", + "CCTransitionFadeBL", + "CCTransitionFadeUp", + "CCTransitionFadeDown", + "CCTransitionTurnOffTiles", + "CCTransitionSplitRows", + "CCTransitionSplitCols", + "CCTransitionFade", + "FadeWhiteTransition", + "FlipXLeftOver", + "FlipXRightOver", + "FlipYUpOver", + "FlipYDownOver", + "FlipAngularLeftOver", + "FlipAngularRightOver", + "ZoomFlipXLeftOver", + "ZoomFlipXRightOver", + "ZoomFlipYUpOver", + "ZoomFlipYDownOver", + "ZoomFlipAngularLeftOver", + "ZoomFlipAngularRightOver", + "CCTransitionShrinkGrow", + "CCTransitionRotoZoom", + "CCTransitionMoveInL", + "CCTransitionMoveInR", + "CCTransitionMoveInT", + "CCTransitionMoveInB", + "CCTransitionSlideInL", + "CCTransitionSlideInR", + "CCTransitionSlideInT", + "CCTransitionSlideInB", +} diff --git a/samples/lua-tests/src/TransitionsTest/TransitionsTest.lua b/samples/lua-tests/src/TransitionsTest/TransitionsTest.lua new file mode 100644 index 0000000000..b2811f5780 --- /dev/null +++ b/samples/lua-tests/src/TransitionsTest/TransitionsTest.lua @@ -0,0 +1,265 @@ +require "src/TransitionsTest/TransitionsName" + + +local SceneIdx = -1 +local CurSceneNo = 2 +local TRANSITION_DURATION = 1.2 +local s = cc.Director:getInstance():getWinSize() + +local function switchSceneTypeNo() + if CurSceneNo == 1 then + CurSceneNo = 2 + else + CurSceneNo = 1 + end +end + +local function backAction() + SceneIdx = SceneIdx - 1 + if SceneIdx < 0 then + SceneIdx = SceneIdx + Transition_Table.MAX_LAYER + end + + switchSceneTypeNo() + return generateTranScene() +end + +local function restartAction() + return generateTranScene() +end + +local function nextAction() + SceneIdx = SceneIdx + 1 + SceneIdx = math.mod(SceneIdx, Transition_Table.MAX_LAYER) + + switchSceneTypeNo() + return generateTranScene() +end + +local function backCallback(sender) + local scene = backAction() + cc.Director:getInstance():replaceScene(scene) +end + +local function restartCallback(sender) + local scene = restartAction() + cc.Director:getInstance():replaceScene(scene) +end + +local function nextCallback(sender) + local scene = nextAction() + cc.Director:getInstance():replaceScene(scene) +end + +----------------------------- +-- TestLayer1 +----------------------------- +local function createLayer1() + local layer = cc.Layer:create() + local x, y = s.width, s.height + + local bg1 = cc.Sprite:create(s_back1) + bg1:setPosition(cc.p(s.width / 2, s.height / 2)) + layer:addChild(bg1, -1) + + local titleLabel = cc.LabelTTF:create(Transition_Name[SceneIdx], "Thonburi", 32) + layer:addChild(titleLabel) + titleLabel:setColor(cc.c3b(255,32,32)) + titleLabel:setPosition(x / 2, y - 100) + + local label = cc.LabelTTF:create("SCENE 1", "Marker Felt", 38) + label:setColor(cc.c3b(16,16,255)) + label:setPosition(x / 2, y / 2) + layer:addChild(label) + + -- menu + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + item1:registerScriptTapHandler(backCallback) + item2:registerScriptTapHandler(restartCallback) + item3:registerScriptTapHandler(nextCallback) + + local menu = cc.Menu:create() + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + menu:setPosition(cc.p(0, 0)) + item1:setPosition(cc.p(s.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(s.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(s.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + + layer:addChild(menu, 1) + + return layer +end + +----------------------------- +-- TestLayer2 +----------------------------- +local function createLayer2() + local layer = cc.Layer:create() + local x, y = s.width, s.height + + local bg1 = cc.Sprite:create(s_back2) + bg1:setPosition(cc.p(s.width / 2, s.height / 2)) + layer:addChild(bg1, -1) + + local titleLabel = cc.LabelTTF:create(Transition_Name[SceneIdx], "Thonburi", 32 ) + layer:addChild(titleLabel) + titleLabel:setColor(cc.c3b(255,32,32)) + titleLabel:setPosition(x / 2, y - 100) + + local label = cc.LabelTTF:create("SCENE 2", "Marker Felt", 38) + label:setColor(cc.c3b(16,16,255)) + label:setPosition(x / 2, y / 2) + layer:addChild(label) + + -- menu + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + item1:registerScriptTapHandler(backCallback) + item2:registerScriptTapHandler(restartCallback) + item3:registerScriptTapHandler(nextCallback) + + local menu = cc.Menu:create() + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + menu:setPosition(cc.p(0, 0)) + item1:setPosition(cc.p(s.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(s.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(s.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + + layer:addChild(menu, 1) + + return layer +end + +----------------------------- +-- Create Transition Test +----------------------------- +local function createTransition(index, t, scene) + cc.Director:getInstance():setDepthTest(false) + + if firstEnter == true then + firstEnter = false + return scene + end + + if index == Transition_Table.CCTransitionJumpZoom then + scene = cc.TransitionJumpZoom:create(t, scene) + elseif index == Transition_Table.CCTransitionProgressRadialCCW then + scene = cc.TransitionProgressRadialCCW:create(t, scene) + elseif index == Transition_Table.CCTransitionProgressRadialCW then + scene = cc.TransitionProgressRadialCW:create(t, scene) + elseif index == Transition_Table.CCTransitionProgressHorizontal then + scene = cc.TransitionProgressHorizontal:create(t, scene) + elseif index == Transition_Table.CCTransitionProgressVertical then + scene = cc.TransitionProgressVertical:create(t, scene) + elseif index == Transition_Table.CCTransitionProgressInOut then + scene = cc.TransitionProgressInOut:create(t, scene) + elseif index == Transition_Table.CCTransitionProgressOutIn then + scene = cc.TransitionProgressOutIn:create(t, scene) + elseif index == Transition_Table.CCTransitionCrossFade then + scene = cc.TransitionCrossFade:create(t, scene) + elseif index == Transition_Table.TransitionPageForward then + cc.Director:getInstance():setDepthTest(true) + scene = cc.TransitionPageTurn:create(t, scene, false) + elseif index == Transition_Table.TransitionPageBackward then + cc.Director:getInstance():setDepthTest(true) + scene = cc.TransitionPageTurn:create(t, scene, true) + elseif index == Transition_Table.CCTransitionFadeTR then + scene = cc.TransitionFadeTR:create(t, scene) + elseif index == Transition_Table.CCTransitionFadeBL then + scene = cc.TransitionFadeBL:create(t, scene) + elseif index == Transition_Table.CCTransitionFadeUp then + scene = cc.TransitionFadeUp:create(t, scene) + elseif index == Transition_Table.CCTransitionFadeDown then + scene = cc.TransitionFadeDown:create(t, scene) + elseif index == Transition_Table.CCTransitionTurnOffTiles then + scene = cc.TransitionTurnOffTiles:create(t, scene) + elseif index == Transition_Table.CCTransitionSplitRows then + scene = cc.TransitionSplitRows:create(t, scene) + elseif index == Transition_Table.CCTransitionSplitCols then + scene = cc.TransitionSplitCols:create(t, scene) + elseif index == Transition_Table.CCTransitionFade then + scene = cc.TransitionFade:create(t, scene) + elseif index == Transition_Table.FadeWhiteTransition then + scene = cc.TransitionFade:create(t, scene, cc.c3b(255, 255, 255)) + elseif index == Transition_Table.FlipXLeftOver then + scene = cc.TransitionFlipX:create(t, scene, cc.TRANSITION_ORIENTATION_LEFT_OVER ) + elseif index == Transition_Table.FlipXRightOver then + scene = cc.TransitionFlipX:create(t, scene, cc.TRANSITION_ORIENTATION_RIGHT_OVER ) + elseif index == Transition_Table.FlipYUpOver then + scene = cc.TransitionFlipY:create(t, scene, cc.TRANSITION_ORIENTATION_UP_OVER) + elseif index == Transition_Table.FlipYDownOver then + scene = cc.TransitionFlipY:create(t, scene, cc.TRANSITION_ORIENTATION_DOWN_OVER ) + elseif index == Transition_Table.FlipAngularLeftOver then + scene = cc.TransitionFlipAngular:create(t, scene, cc.TRANSITION_ORIENTATION_LEFT_OVER ) + elseif index == Transition_Table.FlipAngularRightOver then + scene = cc.TransitionFlipAngular:create(t, scene, cc.TRANSITION_ORIENTATION_RIGHT_OVER ) + elseif index == Transition_Table.ZoomFlipXLeftOver then + scene = cc.TransitionZoomFlipX:create(t, scene, cc.TRANSITION_ORIENTATION_LEFT_OVER ) + elseif index == Transition_Table.ZoomFlipXRightOver then + scene = cc.TransitionZoomFlipX:create(t, scene, cc.TRANSITION_ORIENTATION_RIGHT_OVER ) + elseif index == Transition_Table.ZoomFlipYUpOver then + scene = cc.TransitionZoomFlipY:create(t, scene, cc.TRANSITION_ORIENTATION_UP_OVER) + elseif index == Transition_Table.ZoomFlipYDownOver then + scene = cc.TransitionZoomFlipY:create(t, scene, cc.TRANSITION_ORIENTATION_DOWN_OVER ) + elseif index == Transition_Table.ZoomFlipAngularLeftOver then + scene = cc.TransitionZoomFlipAngular:create(t, scene, cc.TRANSITION_ORIENTATION_LEFT_OVER ) + elseif index == Transition_Table.ZoomFlipAngularRightOver then + scene = cc.TransitionZoomFlipAngular:create(t, scene, cc.TRANSITION_ORIENTATION_RIGHT_OVER ) + elseif index == Transition_Table.CCTransitionShrinkGrow then + scene = cc.TransitionShrinkGrow:create(t, scene) + elseif index == Transition_Table.CCTransitionRotoZoom then + scene = cc.TransitionRotoZoom:create(t, scene) + elseif index == Transition_Table.CCTransitionMoveInL then + scene = cc.TransitionMoveInL:create(t, scene) + elseif index == Transition_Table.CCTransitionMoveInR then + scene = cc.TransitionMoveInR:create(t, scene) + elseif index == Transition_Table.CCTransitionMoveInT then + scene = cc.TransitionMoveInT:create(t, scene) + elseif index == Transition_Table.CCTransitionMoveInB then + scene = cc.TransitionMoveInB:create(t, scene) + elseif index == Transition_Table.CCTransitionSlideInL then + scene = cc.TransitionSlideInL:create(t, scene) + elseif index == Transition_Table.CCTransitionSlideInR then + scene = cc.TransitionSlideInR:create(t, scene) + elseif index == Transition_Table.CCTransitionSlideInT then + scene = cc.TransitionSlideInT:create(t, scene) + elseif index == Transition_Table.CCTransitionSlideInB then + scene = cc.TransitionSlideInB:create(t, scene) + end + + return scene +end + +function generateTranScene() + local scene = cc.Scene:create() + local layer = nil + + if CurSceneNo == 1 then + layer = createLayer1() + elseif CurSceneNo == 2 then + layer = createLayer2() + end + + scene:addChild(layer) + scene:addChild(CreateBackMenuItem()) + + return createTransition(SceneIdx, TRANSITION_DURATION, scene) +end + +function TransitionsTest() + cclog("TransitionsTest") + local scene = cc.Scene:create() + + SceneIdx = -1 + CurSceneNo = 2 + firstEnter = true + + return nextAction() +end diff --git a/samples/lua-tests/src/UserDefaultTest/UserDefaultTest.lua b/samples/lua-tests/src/UserDefaultTest/UserDefaultTest.lua new file mode 100644 index 0000000000..b86eb3a1d3 --- /dev/null +++ b/samples/lua-tests/src/UserDefaultTest/UserDefaultTest.lua @@ -0,0 +1,81 @@ +-- enable log + + +local function doTest() + cclog("********************** init value ***********************") + + -- set default value + + cc.UserDefault:getInstance():setStringForKey("string", "value1") + cc.UserDefault:getInstance():setIntegerForKey("integer", 10) + cc.UserDefault:getInstance():setFloatForKey("float", 2.3) + cc.UserDefault:getInstance():setDoubleForKey("double", 2.4) + cc.UserDefault:getInstance():setBoolForKey("bool", true) + + -- print value + + local ret = cc.UserDefault:getInstance():getStringForKey("string") + cclog("string is %s", ret) + + local d = cc.UserDefault:getInstance():getDoubleForKey("double") + cclog("double is %f", d) + + local i = cc.UserDefault:getInstance():getIntegerForKey("integer") + cclog("integer is %d", i) + + local f = cc.UserDefault:getInstance():getFloatForKey("float") + cclog("float is %f", f) + + local b = cc.UserDefault:getInstance():getBoolForKey("bool") + if b == true then + cclog("bool is true") + else + cclog("bool is false") + end + + --cc.UserDefault:getInstance():flush() + + cclog("********************** after change value ***********************") + + -- change the value + + cc.UserDefault:getInstance():setStringForKey("string", "value2") + cc.UserDefault:getInstance():setIntegerForKey("integer", 11) + cc.UserDefault:getInstance():setFloatForKey("float", 2.5) + cc.UserDefault:getInstance():setDoubleForKey("double", 2.6) + cc.UserDefault:getInstance():setBoolForKey("bool", false) + + cc.UserDefault:getInstance():flush() + + -- print value + + ret = cc.UserDefault:getInstance():getStringForKey("string") + cclog("string is %s", ret) + + d = cc.UserDefault:getInstance():getDoubleForKey("double") + cclog("double is %f", d) + + i = cc.UserDefault:getInstance():getIntegerForKey("integer") + cclog("integer is %d", i) + + f = cc.UserDefault:getInstance():getFloatForKey("float") + cclog("float is %f", f) + + b = cc.UserDefault:getInstance():getBoolForKey("bool") + if b == true then + cclog("bool is true") + else + cclog("bool is false") + end +end + +function UserDefaultTestMain() + local ret = cc.Scene:create() + local s = cc.Director:getInstance():getWinSize() + local label = cc.LabelTTF:create("UserDefault test see log", "Arial", 28) + ret:addChild(label, 0) + label:setPosition( cc.p(s.width/2, s.height-50) ) + ret:addChild(CreateBackMenuItem()) + doTest() + return ret +end diff --git a/samples/lua-tests/src/VisibleRect.lua b/samples/lua-tests/src/VisibleRect.lua new file mode 100644 index 0000000000..5d53b1f8e5 --- /dev/null +++ b/samples/lua-tests/src/VisibleRect.lua @@ -0,0 +1,72 @@ +require "extern" +require "Cocos2d" + +VisibleRect = class("VisibleRect") +VisibleRect.__index = VisibleRect + + +VisibleRect.s_visibleRect = cc.rect(0,0,0,0) + +function VisibleRect:lazyInit() + if (self.s_visibleRect.width == 0.0 and self.s_visibleRect.height == 0.0) then + --[[ + local pEGLView = cc.EGLView:getInstance() + local origin = pEGLView:getVisibleOrigin() + ]]-- + self.s_visibleRect.x = 0 + self.s_visibleRect.y = 0 + local size = cc.Director:getInstance():getWinSize() + self.s_visibleRect.width = size.width + self.s_visibleRect.height = size.height + end +end + +function VisibleRect:getVisibleRect() + self:lazyInit() + return cc.rect(self.s_visibleRect.x, self.s_visibleRect.y, self.s_visibleRect.width, self.s_visibleRect.height) +end + +function VisibleRect:left() + self:lazyInit() + return cc.p(self.s_visibleRect.x, self.s_visibleRect.y+self.s_visibleRect.height/2) +end + +function VisibleRect:right() + self:lazyInit() + return cc.p(self.s_visibleRect.x+self.s_visibleRect.width, self.s_visibleRect.y+self.s_visibleRect.height/2) +end + +function VisibleRect:top() + self:lazyInit() + return cc.p(self.s_visibleRect.x+self.s_visibleRect.width/2, self.s_visibleRect.y+self.s_visibleRect.height) +end + +function VisibleRect:bottom() + self:lazyInit() + return cc.p(self.s_visibleRect.x+self.s_visibleRect.width/2, self.s_visibleRect.y) +end + +function VisibleRect:center() + self:lazyInit() + return cc.p(self.s_visibleRect.x+self.s_visibleRect.width/2, self.s_visibleRect.y+self.s_visibleRect.height/2) +end + +function VisibleRect:leftTop() + self:lazyInit() + return cc.p(self.s_visibleRect.x, self.s_visibleRect.y+self.s_visibleRect.height) +end + +function VisibleRect:rightTop() + self:lazyInit() + return cc.p(self.s_visibleRect.x+self.s_visibleRect.width, self.s_visibleRect.y+self.s_visibleRect.height) +end + +function VisibleRect:leftBottom() + self:lazyInit() + return cc.p(self.s_visibleRect.x,self.s_visibleRect.y) +end + +function VisibleRect:rightBottom() + self:lazyInit() + return cc.p(self.s_visibleRect.x+self.s_visibleRect.width, self.s_visibleRect.y) +end diff --git a/samples/lua-tests/src/XMLHttpRequestTest/XMLHttpRequestTest.lua b/samples/lua-tests/src/XMLHttpRequestTest/XMLHttpRequestTest.lua new file mode 100644 index 0000000000..4bec50a734 --- /dev/null +++ b/samples/lua-tests/src/XMLHttpRequestTest/XMLHttpRequestTest.lua @@ -0,0 +1,147 @@ +require("json") + +local function XMLHttpRequestLayer() + local layer = cc.Layer:create() + local winSize = cc.Director:getInstance():getWinSize() + local margin = 40 + local space = 35 + + local function init() + local label = cc.LabelTTF:create("XML Http Request Test", "Arial", 28) + label:setPosition(cc.p(winSize.width / 2, winSize.height - margin)) + layer:addChild(label, 0) + + --Response Code Label + local labelStatusCode = cc.LabelTTF:create("HTTP Status Code", "Marker Felt", 20) + labelStatusCode:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 6 * space)) + layer:addChild(labelStatusCode) + + local menuRequest = cc.Menu:create() + menuRequest:setPosition(cc.p(0,0)) + layer:addChild(menuRequest) + + --Get + local function onMenuGetClicked() + local xhr = cc.XMLHttpRequest:new() + xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING + xhr:open("GET", "http://httpbin.org/get") + + local function onReadyStateChange() + local statusString = "Http Status Code:"..xhr.statusText + labelStatusCode:setString(statusString) + print(xhr.response) + end + + xhr:registerScriptHandler(onReadyStateChange) + xhr:send() + + labelStatusCode:setString("waiting...") + end + + local labelGet = cc.LabelTTF:create("Test Get", "Arial", 22) + local itemGet = cc.MenuItemLabel:create(labelGet) + itemGet:registerScriptTapHandler(onMenuGetClicked) + itemGet:setPosition(cc.p(winSize.width / 2, winSize.height - margin - space)) + menuRequest:addChild(itemGet) + + --Post + local function onMenuPostClicked() + local xhr = cc.XMLHttpRequest:new() + xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_STRING + xhr:open("POST", "http://httpbin.org/post") + local function onReadyStateChange() + labelStatusCode:setString("Http Status Code:"..xhr.statusText) + print(xhr.response) + end + xhr:registerScriptHandler(onReadyStateChange) + xhr:send() + + labelStatusCode:setString("waiting...") + end + + local labelPost = cc.LabelTTF:create("Test Post", "Arial", 22) + local itemPost = cc.MenuItemLabel:create(labelPost) + itemPost:registerScriptTapHandler(onMenuPostClicked) + itemPost:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 2 * space)) + menuRequest:addChild(itemPost) + + --Post Binary + local function onMenuPostBinaryClicked() + local xhr = cc.XMLHttpRequest:new() + xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER + xhr:open("POST", "http://httpbin.org/post") + + local function onReadyStateChange() + local response = xhr.response + local size = table.getn(response) + local strInfo = "" + + for i = 1,size do + if 0 == response[i] then + strInfo = strInfo.."\'\\0\'" + else + strInfo = strInfo..string.char(response[i]) + end + end + labelStatusCode:setString("Http Status Code:"..xhr.statusText) + print(strInfo) + end + + xhr:registerScriptHandler(onReadyStateChange) + xhr:send() + + labelStatusCode:setString("waiting...") + end + + local labelPostBinary = cc.LabelTTF:create("Test Post Binary", "Arial", 22) + local itemPostBinary = cc.MenuItemLabel:create(labelPostBinary) + itemPostBinary:registerScriptTapHandler(onMenuPostBinaryClicked) + itemPostBinary:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 3 * space)) + menuRequest:addChild(itemPostBinary) + + --Post Json + + local function onMenuPostJsonClicked() + local xhr = cc.XMLHttpRequest:new() + xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_JSON + xhr:open("POST", "http://httpbin.org/post") + + local function onReadyStateChange() + labelStatusCode:setString("Http Status Code:"..xhr.statusText) + local response = xhr.response + local output = json.decode(response,1) + table.foreach(output,function(i, v) print (i, v) end) + print("headers are") + table.foreach(output.headers,print) + end + + xhr:registerScriptHandler(onReadyStateChange) + xhr:send() + + labelStatusCode:setString("waiting...") + end + + local labelPostJson = cc.LabelTTF:create("Test Post Json", "Arial", 22) + local itemPostJson = cc.MenuItemLabel:create(labelPostJson) + itemPostJson:registerScriptTapHandler(onMenuPostJsonClicked) + itemPostJson:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 4 * space)) + menuRequest:addChild(itemPostJson) + end + + local function onNodeEvent(eventName) + if "enter" == eventName then + init() + end + end + + layer:registerScriptHandler(onNodeEvent) + + return layer +end + +function XMLHttpRequestTestMain() + local scene = cc.Scene:create() + scene:addChild(XMLHttpRequestLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/ZwoptexTest/ZwoptexTest.lua b/samples/lua-tests/src/ZwoptexTest/ZwoptexTest.lua new file mode 100644 index 0000000000..3a548d1aad --- /dev/null +++ b/samples/lua-tests/src/ZwoptexTest/ZwoptexTest.lua @@ -0,0 +1,128 @@ +local scheduler = cc.Director:getInstance():getScheduler() +-------------------------------------------------------------------- +-- +-- ZwoptexGenericTest +-- +-------------------------------------------------------------------- +local function ZwoptexGenericTest() + local ret = createTestLayer("Zwoptex Tests", + "Coordinate Formats, Rotation, Trimming, flipX/Y") + local spriteFrameIndex = 0 + local counter = 0 + local s = cc.Director:getInstance():getWinSize() + local schedulerEntry = nil + local schedulerFlipSpriteEntry = nil + local sprite1 = nil + local sprite2 = nil + + local function onEnter() + cc.SpriteFrameCache:getInstance():addSpriteFrames("zwoptex/grossini.plist") + cc.SpriteFrameCache:getInstance():addSpriteFrames("zwoptex/grossini-generic.plist") + + local layer1 = cc.LayerColor:create(cc.c4b(255, 0, 0, 255), 85, 121) + layer1:setPosition(cc.p(s.width/2-80 - (85.0 * 0.5), s.height/2 - (121.0 * 0.5))) + ret:addChild(layer1) + + sprite1 = cc.Sprite:createWithSpriteFrame(cc.SpriteFrameCache:getInstance():getSpriteFrame("grossini_dance_01.png")) + sprite1:setPosition(cc.p( s.width/2-80, s.height/2)) + ret:addChild(sprite1) + + sprite1:setFlippedX(false) + sprite1:setFlippedY(false) + + local layer2 = cc.LayerColor:create(cc.c4b(255, 0, 0, 255), 85, 121) + layer2:setPosition(cc.p(s.width/2+80 - (85.0 * 0.5), s.height/2 - (121.0 * 0.5))) + ret:addChild(layer2) + + sprite2 = cc.Sprite:createWithSpriteFrame(cc.SpriteFrameCache:getInstance():getSpriteFrame("grossini_dance_generic_01.png")) + sprite2:setPosition(cc.p( s.width/2 + 80, s.height/2)) + ret:addChild(sprite2) + + sprite2:setFlippedX(false) + sprite2:setFlippedY(false) + + local function flipSprites(dt) + counter = counter + 1 + + local fx = false + local fy = false + local i = counter % 4 + + if i == 0 then + fx = false + fy = false + elseif i == 1 then + fx = true + fy = false + elseif i == 2 then + fx = false + fy = true + elseif i == 3 then + fx = true + fy = true + end + + sprite1:setFlippedX(fx) + sprite2:setFlippedX(fx) + sprite1:setFlippedY(fy) + sprite2:setFlippedY(fy) + + spriteFrameIndex = spriteFrameIndex + 1 + if spriteFrameIndex > 14 then + spriteFrameIndex = 1 + end + + local str1 = string.format("grossini_dance_%02d.png", spriteFrameIndex) + local str2 = string.format("grossini_dance_generic_%02d.png", spriteFrameIndex) + sprite1:setSpriteFrame(cc.SpriteFrameCache:getInstance():getSpriteFrame(str1)) + sprite2:setSpriteFrame(cc.SpriteFrameCache:getInstance():getSpriteFrame(str2)) + end + + sprite1:retain() + sprite2:retain() + counter = 0 + local function startIn05Secs(dt) + scheduler:unscheduleScriptEntry(schedulerEntry) + schedulerFlipSpriteEntry = scheduler:scheduleScriptFunc(flipSprites, 0.5, false) + end + schedulerEntry = scheduler:scheduleScriptFunc(startIn05Secs, 1.0, false) + end + + local function onExit() + if schedulerEntry ~= nil then + scheduler:unscheduleScriptEntry(schedulerEntry) + end + if schedulerFlipSpriteEntry ~= nil then + scheduler:unscheduleScriptEntry(schedulerFlipSpriteEntry) + end + sprite1:release() + sprite2:release() + local cache = cc.SpriteFrameCache:getInstance() + cache:removeSpriteFramesFromFile("zwoptex/grossini.plist") + cache:removeSpriteFramesFromFile("zwoptex/grossini-generic.plist") + end + + local function onNodeEvent(event) + if event == "enter" then + onEnter() + elseif event == "exit" then + onExit() + end + end + + ret:registerScriptHandler(onNodeEvent) + + return ret +end + +function ZwoptexTestMain() + cclog("ZwoptexTestMain") + Helper.index = 1 + local scene = cc.Scene:create() + Helper.createFunctionTable = { + ZwoptexGenericTest + } + scene:addChild(ZwoptexGenericTest()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/samples/lua-tests/src/controller.lua b/samples/lua-tests/src/controller.lua new file mode 100644 index 0000000000..2ccad7250d --- /dev/null +++ b/samples/lua-tests/src/controller.lua @@ -0,0 +1,13 @@ + +-- avoid memory leak +collectgarbage("setpause", 100) +collectgarbage("setstepmul", 5000) + +require "src/mainMenu" +---------------- + + +-- run +local scene = cc.Scene:create() +scene:addChild(CreateTestMenu()) +cc.Director:getInstance():runWithScene(scene) diff --git a/samples/lua-tests/src/helper.lua b/samples/lua-tests/src/helper.lua new file mode 100644 index 0000000000..c2706d02d3 --- /dev/null +++ b/samples/lua-tests/src/helper.lua @@ -0,0 +1,141 @@ +require "Cocos2d" + +CC_CONTENT_SCALE_FACTOR = function() + return cc.Director:getInstance():getContentScaleFactor() +end + + +CC_POINT_PIXELS_TO_POINTS = function(pixels) + return cc.p(pixels.x/CC_CONTENT_SCALE_FACTOR(), pixels.y/CC_CONTENT_SCALE_FACTOR()) +end + +CC_POINT_POINTS_TO_PIXELS = function(points) + return cc.p(points.x*CC_CONTENT_SCALE_FACTOR(), points.y*CC_CONTENT_SCALE_FACTOR()) +end + + +-- cclog +cclog = function(...) + print(string.format(...)) +end + +-- change table to enum type +function CreateEnumTable(tbl, index) + local enumTable = {} + local enumIndex = index or -1 + for i, v in ipairs(tbl) do + enumTable[v] = enumIndex + i + end + return enumTable +end + +-- back menu callback +local function MainMenuCallback() + Helper.usePhysics = false + local scene = cc.Scene:create() + scene:addChild(CreateTestMenu()) + + cc.Director:getInstance():replaceScene(scene) +end + +-- add the menu item for back to main menu +function CreateBackMenuItem() + local label = cc.LabelTTF:create("MainMenu", "Arial", 20) + local MenuItem = cc.MenuItemLabel:create(label) + MenuItem:registerScriptTapHandler(MainMenuCallback) + + local s = cc.Director:getInstance():getWinSize() + local Menu = cc.Menu:create() + Menu:addChild(MenuItem) + Menu:setPosition(0, 0) + MenuItem:setPosition(s.width - 50, 25) + + return Menu +end + +Helper = { + index = 1, + createFunctioinTable = nil, + currentLayer = nil, + titleLabel = nil, + subtitleLabel = nil +} +function Helper.nextAction() + Helper.index = Helper.index + 1 + if Helper.index > table.getn(Helper.createFunctionTable) then + Helper.index = 1 + end + + return Helper.newScene() +end + +function Helper.backAction() + Helper.index = Helper.index - 1 + if Helper.index == 0 then + Helper.index = table.getn(Helper.createFunctionTable) + end + + return Helper.newScene() +end + +function Helper.restartAction() + return Helper.newScene() +end + +function Helper.newScene() + local scene + if Helper.usePhysics then + scene = cc.Scene:createWithPhysics() + else + scene = cc.Scene:create() + end + Helper.currentLayer = Helper.createFunctionTable[Helper.index]() + scene:addChild(Helper.currentLayer) + scene:addChild(CreateBackMenuItem()) + + cc.Director:getInstance():replaceScene(scene) +end + +function Helper.initWithLayer(layer) + Helper.currentLayer = layer + + local size = cc.Director:getInstance():getWinSize() + Helper.titleLabel = cc.LabelTTF:create("", "Arial", 28) + layer:addChild(Helper.titleLabel, 1) + Helper.titleLabel:setPosition(size.width / 2, size.height - 50) + + Helper.subtitleLabel = cc.LabelTTF:create("", "Thonburi", 16) + layer:addChild(Helper.subtitleLabel, 1) + Helper.subtitleLabel:setPosition(size.width / 2, size.height - 80) + + -- menu + local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2) + local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2) + local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2) + item1:registerScriptTapHandler(Helper.backAction) + item2:registerScriptTapHandler(Helper.restartAction) + item3:registerScriptTapHandler(Helper.nextAction) + + local menu = cc.Menu:create() + menu:addChild(item1) + menu:addChild(item2) + menu:addChild(item3) + menu:setPosition(cc.p(0, 0)) + item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2)) + item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2)) + layer:addChild(menu, 1) + + local background = cc.Layer:create() + layer:addChild(background, -10) +end + +function createTestLayer(title, subtitle) + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + local titleStr = title == nil and "No title" or title + local subTitleStr = subtitle == nil and "" or subtitle + Helper.titleLabel:setString(titleStr) + Helper.subtitleLabel:setString(subTitleStr) + return layer +end diff --git a/samples/lua-tests/src/mainMenu.lua b/samples/lua-tests/src/mainMenu.lua new file mode 100644 index 0000000000..4d0b967332 --- /dev/null +++ b/samples/lua-tests/src/mainMenu.lua @@ -0,0 +1,207 @@ +require "Cocos2d" +require "Cocos2dConstants" +require "Opengl" +require "OpenglConstants" +require "StudioConstants" +require "GuiConstants" +require "src/helper" +require "src/testResource" +require "src/VisibleRect" + +require "src/AccelerometerTest/AccelerometerTest" +require "src/ActionManagerTest/ActionManagerTest" +require "src/ActionsEaseTest/ActionsEaseTest" +require "src/ActionsProgressTest/ActionsProgressTest" +require "src/ActionsTest/ActionsTest" +require "src/AssetsManagerTest/AssetsManagerTest" +require "src/BugsTest/BugsTest" +require "src/ClickAndMoveTest/ClickAndMoveTest" +require "src/CocosDenshionTest/CocosDenshionTest" +require "src/CocoStudioTest/CocoStudioTest" +require "src/CurrentLanguageTest/CurrentLanguageTest" +require "src/DrawPrimitivesTest/DrawPrimitivesTest" +require "src/EffectsTest/EffectsTest" +require "src/EffectsAdvancedTest/EffectsAdvancedTest" +require "src/ExtensionTest/ExtensionTest" +require "src/FontTest/FontTest" +require "src/IntervalTest/IntervalTest" +require "src/KeypadTest/KeypadTest" +require "src/LabelTest/LabelTest" +require "src/LabelTestNew/LabelTestNew" +require "src/LayerTest/LayerTest" +require "src/MenuTest/MenuTest" +require "src/MotionStreakTest/MotionStreakTest" +require "src/NewEventDispatcherTest/NewEventDispatcherTest" +require "src/NodeTest/NodeTest" +require "src/OpenGLTest/OpenGLTest" +require "src/ParallaxTest/ParallaxTest" +require "src/ParticleTest/ParticleTest" +require "src/PerformanceTest/PerformanceTest" +require "src/RenderTextureTest/RenderTextureTest" +require "src/RotateWorldTest/RotateWorldTest" +require "src/SpriteTest/SpriteTest" +require "src/SceneTest/SceneTest" +require "src/SpineTest/SpineTest" +require "src/Texture2dTest/Texture2dTest" +require "src/TileMapTest/TileMapTest" +require "src/TouchesTest/TouchesTest" +require "src/TransitionsTest/TransitionsTest" +require "src/UserDefaultTest/UserDefaultTest" +require "src/ZwoptexTest/ZwoptexTest" +require "src/LuaBridgeTest/LuaBridgeTest" +require "src/XMLHttpRequestTest/XMLHttpRequestTest" +require "src/PhysicsTest/PhysicsTest" + + +local LINE_SPACE = 40 + +local CurPos = {x = 0, y = 0} +local BeginPos = {x = 0, y = 0} + + +local _allTests = { + { isSupported = true, name = "Accelerometer" , create_func= AccelerometerMain }, + { isSupported = true, name = "ActionManagerTest" , create_func = ActionManagerTestMain }, + { isSupported = true, name = "ActionsEaseTest" , create_func = EaseActionsTest }, + { isSupported = true, name = "ActionsProgressTest" , create_func = ProgressActionsTest }, + { isSupported = true, name = "ActionsTest" , create_func = ActionsTest }, + { isSupported = true, name = "AssetsManagerTest" , create_func = AssetsManagerTestMain }, + { isSupported = false, name = "Box2dTest" , create_func= Box2dTestMain }, + { isSupported = false, name = "Box2dTestBed" , create_func= Box2dTestBedMain }, + { isSupported = true, name = "BugsTest" , create_func= BugsTestMain }, + { isSupported = false, name = "ChipmunkAccelTouchTest" , create_func= ChipmunkAccelTouchTestMain }, + { isSupported = true, name = "ClickAndMoveTest" , create_func = ClickAndMoveTest }, + { isSupported = true, name = "CocosDenshionTest" , create_func = CocosDenshionTestMain }, + { isSupported = true, name = "CocoStudioTest" , create_func = CocoStudioTestMain }, + { isSupported = false, name = "CurlTest" , create_func= CurlTestMain }, + { isSupported = true, name = "CurrentLanguageTest" , create_func= CurrentLanguageTestMain }, + { isSupported = true, name = "DrawPrimitivesTest" , create_func= DrawPrimitivesTest }, + { isSupported = true, name = "EffectsTest" , create_func = EffectsTest }, + { isSupported = true, name = "EffectAdvancedTest" , create_func = EffectAdvancedTestMain }, + { isSupported = true, name = "ExtensionsTest" , create_func= ExtensionsTestMain }, + { isSupported = true, name = "FontTest" , create_func = FontTestMain }, + { isSupported = true, name = "IntervalTest" , create_func = IntervalTestMain }, + { isSupported = true, name = "KeypadTest" , create_func= KeypadTestMain }, + { isSupported = true, name = "LabelTest" , create_func = LabelTest }, + { isSupported = true, name = "LabelTestNew" , create_func = LabelTestNew }, + { isSupported = true, name = "LayerTest" , create_func = LayerTestMain }, + { isSupported = true, name = "LuaBridgeTest" , create_func = LuaBridgeMainTest }, + { isSupported = true, name = "MenuTest" , create_func = MenuTestMain }, + { isSupported = true, name = "MotionStreakTest" , create_func = MotionStreakTest }, + { isSupported = false, name = "MutiTouchTest" , create_func= MutiTouchTestMain }, + { isSupported = true, name = "NewEventDispatcherTest" , create_func = NewEventDispatcherTest }, + { isSupported = true, name = "NodeTest" , create_func = CocosNodeTest }, + { isSupported = true, name = "OpenGLTest" , create_func= OpenGLTestMain }, + { isSupported = true, name = "ParallaxTest" , create_func = ParallaxTestMain }, + { isSupported = true, name = "ParticleTest" , create_func = ParticleTest }, + { isSupported = true, name = "PerformanceTest" , create_func= PerformanceTestMain }, + { isSupported = true, name = "PhysicsTest" , create_func = PhysicsTest }, + { isSupported = true, name = "RenderTextureTest" , create_func = RenderTextureTestMain }, + { isSupported = true, name = "RotateWorldTest" , create_func = RotateWorldTest }, + { isSupported = true, name = "SceneTest" , create_func = SceneTestMain }, + { isSupported = true, name = "SpineTest" , create_func = SpineTestMain }, + { isSupported = false, name = "SchdulerTest" , create_func= SchdulerTestMain }, + { isSupported = false, name = "ShaderTest" , create_func= ShaderTestMain }, + { isSupported = true, name = "SpriteTest" , create_func = SpriteTest }, + { isSupported = false, name = "TextInputTest" , create_func= TextInputTestMain }, + { isSupported = true, name = "Texture2DTest" , create_func = Texture2dTestMain }, + { isSupported = false, name = "TextureCacheTest" , create_func= TextureCacheTestMain }, + { isSupported = true, name = "TileMapTest" , create_func = TileMapTestMain }, + { isSupported = true, name = "TouchesTest" , create_func = TouchesTest }, + { isSupported = true, name = "TransitionsTest" , create_func = TransitionsTest }, + { isSupported = true, name = "UserDefaultTest" , create_func= UserDefaultTestMain }, + { isSupported = true, name = "XMLHttpRequestTest" , create_func = XMLHttpRequestTestMain }, + { isSupported = true, name = "ZwoptexTest" , create_func = ZwoptexTestMain } +} + +local TESTS_COUNT = table.getn(_allTests) + +-- create scene +local function CreateTestScene(nIdx) + cc.Director:getInstance():purgeCachedData() + local scene = _allTests[nIdx].create_func() + return scene +end +-- create menu +function CreateTestMenu() + local menuLayer = cc.Layer:create() + + local function closeCallback() + cc.Director:getInstance():endToLua() + end + + local function menuCallback(tag) + print(tag) + local Idx = tag - 10000 + local testScene = CreateTestScene(Idx) + if testScene then + cc.Director:getInstance():replaceScene(testScene) + end + end + + -- add close menu + local s = cc.Director:getInstance():getWinSize() + local CloseItem = cc.MenuItemImage:create(s_pPathClose, s_pPathClose) + CloseItem:registerScriptTapHandler(closeCallback) + CloseItem:setPosition(cc.p(s.width - 30, s.height - 30)) + + local CloseMenu = cc.Menu:create() + CloseMenu:setPosition(0, 0) + CloseMenu:addChild(CloseItem) + menuLayer:addChild(CloseMenu) + + -- add menu items for tests + local MainMenu = cc.Menu:create() + local index = 0 + local obj = nil + for index, obj in pairs(_allTests) do + local testLabel = cc.LabelTTF:create(obj.name, "Arial", 24) + local testMenuItem = cc.MenuItemLabel:create(testLabel) + if not obj.isSupported then + testMenuItem:setEnabled(false) + end + testMenuItem:registerScriptTapHandler(menuCallback) + testMenuItem:setPosition(cc.p(s.width / 2, (s.height - (index) * LINE_SPACE))) + MainMenu:addChild(testMenuItem, index + 10000, index + 10000) + end + + MainMenu:setContentSize(cc.size(s.width, (TESTS_COUNT + 1) * (LINE_SPACE))) + MainMenu:setPosition(CurPos.x, CurPos.y) + menuLayer:addChild(MainMenu) + + -- handling touch events + local function onTouchBegan(touch, event) + BeginPos = touch:getLocation() + -- CCTOUCHBEGAN event must return true + return true + end + + local function onTouchMoved(touch, event) + local location = touch:getLocation() + local nMoveY = location.y - BeginPos.y + local curPosx, curPosy = MainMenu:getPosition() + local nextPosy = curPosy + nMoveY + local winSize = cc.Director:getInstance():getWinSize() + if nextPosy < 0 then + MainMenu:setPosition(0, 0) + return + end + + if nextPosy > ((TESTS_COUNT + 1) * LINE_SPACE - winSize.height) then + MainMenu:setPosition(0, ((TESTS_COUNT + 1) * LINE_SPACE - winSize.height)) + return + end + + MainMenu:setPosition(curPosx, nextPosy) + BeginPos = {x = location.x, y = location.y} + CurPos = {x = curPosx, y = nextPosy} + end + + local listener = cc.EventListenerTouchOneByOne:create() + listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) + listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) + local eventDispatcher = menuLayer:getEventDispatcher() + eventDispatcher:addEventListenerWithSceneGraphPriority(listener, menuLayer) + + return menuLayer +end diff --git a/samples/lua-tests/src/testResource.lua b/samples/lua-tests/src/testResource.lua new file mode 100644 index 0000000000..ca53adaa85 --- /dev/null +++ b/samples/lua-tests/src/testResource.lua @@ -0,0 +1,41 @@ +s_pPathGrossini = "Images/grossini.png" +s_pPathSister1 = "Images/grossinis_sister1.png" +s_pPathSister2 = "Images/grossinis_sister2.png" +s_pPathB1 = "Images/b1.png" +s_pPathB2 = "Images/b2.png" +s_pPathR1 = "Images/r1.png" +s_pPathR2 = "Images/r2.png" +s_pPathF1 = "Images/f1.png" +s_pPathF2 = "Images/f2.png" +s_pPathBlock = "Images/blocks.png" +s_back = "Images/background.png" +s_back1 = "Images/background1.png" +s_back2 = "Images/background2.png" +s_back3 = "Images/background3.png" +s_stars1 = "Images/stars.png" +s_stars2 = "Images/stars2.png" +s_fire = "Images/fire.png" +s_snow = "Images/snow.png" +s_streak = "Images/streak.png" +s_PlayNormal = "Images/btn-play-normal.png" +s_PlaySelect = "Images/btn-play-selected.png" +s_AboutNormal = "Images/btn-about-normal.png" +s_AboutSelect = "Images/btn-about-selected.png" +s_HighNormal = "Images/btn-highscores-normal.png" +s_HighSelect = "Images/btn-highscores-selected.png" +s_Ball = "Images/ball.png" +s_Paddle = "Images/paddle.png" +s_pPathClose = "Images/close.png" +s_MenuItem = "Images/menuitemsprite.png" +s_SendScore = "Images/SendScoreButton.png" +s_PressSendScore = "Images/SendScoreButtonPressed.png" +s_Power = "Images/powered.png" +s_AtlasTest = "Images/atlastest.png" + +-- tilemaps resource +s_TilesPng = "TileMaps/tiles.png" +s_LevelMapTga = "TileMaps/levelmap.tga" + +-- spine test resource +s_pPathSpineBoyJson = "spine/spineboy.json" +s_pPathSpineBoyAtlas = "spine/spineboy.atlas" diff --git a/tools/bindings-generator b/tools/bindings-generator new file mode 160000 index 0000000000..cd30a5b87d --- /dev/null +++ b/tools/bindings-generator @@ -0,0 +1 @@ +Subproject commit cd30a5b87d5c9b6fdbd57cdc5a9213bcb975f65b