Compare commits
No commits in common. "master" and "Windows-x86" have entirely different histories.
master
...
Windows-x8
51
.travis.yml
51
.travis.yml
@ -1,51 +0,0 @@
|
|||||||
#
|
|
||||||
# LuaDist Travis-CI Hook
|
|
||||||
#
|
|
||||||
|
|
||||||
# We assume C build environments
|
|
||||||
language: C
|
|
||||||
|
|
||||||
# Try using multiple Lua Implementations
|
|
||||||
env:
|
|
||||||
- TOOL="gcc" # Use native compiler (GCC usually)
|
|
||||||
- TOOL="clang" # Use clang
|
|
||||||
- TOOL="i686-w64-mingw32" # 32bit MinGW
|
|
||||||
- TOOL="x86_64-w64-mingw32" # 64bit MinGW
|
|
||||||
- TOOL="arm-linux-gnueabihf" # ARM hard-float (hf), linux
|
|
||||||
|
|
||||||
# Crosscompile builds may fail
|
|
||||||
matrix:
|
|
||||||
allow_failures:
|
|
||||||
- env: TOOL="i686-w64-mingw32"
|
|
||||||
- env: TOOL="x86_64-w64-mingw32"
|
|
||||||
- env: TOOL="arm-linux-gnueabihf"
|
|
||||||
|
|
||||||
# Install dependencies
|
|
||||||
install:
|
|
||||||
- git clone git://github.com/LuaDist/Tools.git ~/_tools
|
|
||||||
- ~/_tools/travis/travis install
|
|
||||||
|
|
||||||
# Bootstap
|
|
||||||
before_script:
|
|
||||||
- ~/_tools/travis/travis bootstrap
|
|
||||||
|
|
||||||
# Build the module
|
|
||||||
script:
|
|
||||||
- ~/_tools/travis/travis build
|
|
||||||
|
|
||||||
# Execute additional tests or commands
|
|
||||||
after_script:
|
|
||||||
- ~/_tools/travis/travis test
|
|
||||||
|
|
||||||
# Only watch the master branch
|
|
||||||
branches:
|
|
||||||
only:
|
|
||||||
- master
|
|
||||||
|
|
||||||
# Notify the LuaDist Dev group if needed
|
|
||||||
notifications:
|
|
||||||
recipients:
|
|
||||||
- luadist-dev@googlegroups.com
|
|
||||||
email:
|
|
||||||
on_success: change
|
|
||||||
on_failure: always
|
|
@ -1,14 +0,0 @@
|
|||||||
# Copyright (C) 2007-2013 LuaDist.
|
|
||||||
# Created by Peter Drahoš
|
|
||||||
# Redistribution and use of this file is allowed according to the terms of the MIT license.
|
|
||||||
# For details see the COPYRIGHT file distributed with LuaDist.
|
|
||||||
# Please note that the package source code is licensed under its own license.
|
|
||||||
|
|
||||||
project ( dkjson NONE )
|
|
||||||
cmake_minimum_required ( VERSION 2.8 )
|
|
||||||
include ( cmake/dist.cmake )
|
|
||||||
include ( lua )
|
|
||||||
|
|
||||||
install_lua_module ( dkjson dkjson.lua )
|
|
||||||
install_example ( jsontest.lua speedtest.lua )
|
|
||||||
install_data ( versions.txt )
|
|
@ -1,118 +0,0 @@
|
|||||||
# Locate Lua library
|
|
||||||
# This module defines
|
|
||||||
# LUA_EXECUTABLE, if found
|
|
||||||
# LUA_FOUND, if false, do not try to link to Lua
|
|
||||||
# LUA_LIBRARIES
|
|
||||||
# LUA_INCLUDE_DIR, where to find lua.h
|
|
||||||
# LUA_VERSION_STRING, the version of Lua found (since CMake 2.8.8)
|
|
||||||
#
|
|
||||||
# Note that the expected include convention is
|
|
||||||
# #include "lua.h"
|
|
||||||
# and not
|
|
||||||
# #include <lua/lua.h>
|
|
||||||
# This is because, the lua location is not standardized and may exist
|
|
||||||
# in locations other than lua/
|
|
||||||
|
|
||||||
#=============================================================================
|
|
||||||
# Copyright 2007-2009 Kitware, Inc.
|
|
||||||
# Modified to support Lua 5.2 by LuaDist 2012
|
|
||||||
#
|
|
||||||
# Distributed under the OSI-approved BSD License (the "License");
|
|
||||||
# see accompanying file Copyright.txt for details.
|
|
||||||
#
|
|
||||||
# This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
||||||
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
# See the License for more information.
|
|
||||||
#=============================================================================
|
|
||||||
# (To distribute this file outside of CMake, substitute the full
|
|
||||||
# License text for the above reference.)
|
|
||||||
#
|
|
||||||
# The required version of Lua can be specified using the
|
|
||||||
# standard syntax, e.g. FIND_PACKAGE(Lua 5.1)
|
|
||||||
# Otherwise the module will search for any available Lua implementation
|
|
||||||
|
|
||||||
# Always search for non-versioned lua first (recommended)
|
|
||||||
SET(_POSSIBLE_LUA_INCLUDE include include/lua)
|
|
||||||
SET(_POSSIBLE_LUA_EXECUTABLE lua)
|
|
||||||
SET(_POSSIBLE_LUA_LIBRARY lua)
|
|
||||||
|
|
||||||
# Determine possible naming suffixes (there is no standard for this)
|
|
||||||
IF(Lua_FIND_VERSION_MAJOR AND Lua_FIND_VERSION_MINOR)
|
|
||||||
SET(_POSSIBLE_SUFFIXES "${Lua_FIND_VERSION_MAJOR}${Lua_FIND_VERSION_MINOR}" "${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR}" "-${Lua_FIND_VERSION_MAJOR}.${Lua_FIND_VERSION_MINOR}")
|
|
||||||
ELSE(Lua_FIND_VERSION_MAJOR AND Lua_FIND_VERSION_MINOR)
|
|
||||||
SET(_POSSIBLE_SUFFIXES "52" "5.2" "-5.2" "51" "5.1" "-5.1")
|
|
||||||
ENDIF(Lua_FIND_VERSION_MAJOR AND Lua_FIND_VERSION_MINOR)
|
|
||||||
|
|
||||||
# Set up possible search names and locations
|
|
||||||
FOREACH(_SUFFIX ${_POSSIBLE_SUFFIXES})
|
|
||||||
LIST(APPEND _POSSIBLE_LUA_INCLUDE "include/lua${_SUFFIX}")
|
|
||||||
LIST(APPEND _POSSIBLE_LUA_EXECUTABLE "lua${_SUFFIX}")
|
|
||||||
LIST(APPEND _POSSIBLE_LUA_LIBRARY "lua${_SUFFIX}")
|
|
||||||
ENDFOREACH(_SUFFIX)
|
|
||||||
|
|
||||||
# Find the lua executable
|
|
||||||
FIND_PROGRAM(LUA_EXECUTABLE
|
|
||||||
NAMES ${_POSSIBLE_LUA_EXECUTABLE}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Find the lua header
|
|
||||||
FIND_PATH(LUA_INCLUDE_DIR lua.h
|
|
||||||
HINTS
|
|
||||||
$ENV{LUA_DIR}
|
|
||||||
PATH_SUFFIXES ${_POSSIBLE_LUA_INCLUDE}
|
|
||||||
PATHS
|
|
||||||
~/Library/Frameworks
|
|
||||||
/Library/Frameworks
|
|
||||||
/usr/local
|
|
||||||
/usr
|
|
||||||
/sw # Fink
|
|
||||||
/opt/local # DarwinPorts
|
|
||||||
/opt/csw # Blastwave
|
|
||||||
/opt
|
|
||||||
)
|
|
||||||
|
|
||||||
# Find the lua library
|
|
||||||
FIND_LIBRARY(LUA_LIBRARY
|
|
||||||
NAMES ${_POSSIBLE_LUA_LIBRARY}
|
|
||||||
HINTS
|
|
||||||
$ENV{LUA_DIR}
|
|
||||||
PATH_SUFFIXES lib64 lib
|
|
||||||
PATHS
|
|
||||||
~/Library/Frameworks
|
|
||||||
/Library/Frameworks
|
|
||||||
/usr/local
|
|
||||||
/usr
|
|
||||||
/sw
|
|
||||||
/opt/local
|
|
||||||
/opt/csw
|
|
||||||
/opt
|
|
||||||
)
|
|
||||||
|
|
||||||
IF(LUA_LIBRARY)
|
|
||||||
# include the math library for Unix
|
|
||||||
IF(UNIX AND NOT APPLE)
|
|
||||||
FIND_LIBRARY(LUA_MATH_LIBRARY m)
|
|
||||||
SET( LUA_LIBRARIES "${LUA_LIBRARY};${LUA_MATH_LIBRARY}" CACHE STRING "Lua Libraries")
|
|
||||||
# For Windows and Mac, don't need to explicitly include the math library
|
|
||||||
ELSE(UNIX AND NOT APPLE)
|
|
||||||
SET( LUA_LIBRARIES "${LUA_LIBRARY}" CACHE STRING "Lua Libraries")
|
|
||||||
ENDIF(UNIX AND NOT APPLE)
|
|
||||||
ENDIF(LUA_LIBRARY)
|
|
||||||
|
|
||||||
# Determine Lua version
|
|
||||||
IF(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h")
|
|
||||||
FILE(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_str REGEX "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua .+\"")
|
|
||||||
|
|
||||||
STRING(REGEX REPLACE "^#define[ \t]+LUA_RELEASE[ \t]+\"Lua ([^\"]+)\".*" "\\1" LUA_VERSION_STRING "${lua_version_str}")
|
|
||||||
UNSET(lua_version_str)
|
|
||||||
ENDIF()
|
|
||||||
|
|
||||||
INCLUDE(FindPackageHandleStandardArgs)
|
|
||||||
# handle the QUIETLY and REQUIRED arguments and set LUA_FOUND to TRUE if
|
|
||||||
# all listed variables are TRUE
|
|
||||||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Lua
|
|
||||||
REQUIRED_VARS LUA_LIBRARIES LUA_INCLUDE_DIR
|
|
||||||
VERSION_VAR LUA_VERSION_STRING)
|
|
||||||
|
|
||||||
MARK_AS_ADVANCED(LUA_INCLUDE_DIR LUA_LIBRARIES LUA_LIBRARY LUA_MATH_LIBRARY LUA_EXECUTABLE)
|
|
||||||
|
|
321
cmake/dist.cmake
321
cmake/dist.cmake
@ -1,321 +0,0 @@
|
|||||||
# LuaDist CMake utility library.
|
|
||||||
# Provides sane project defaults and macros common to LuaDist CMake builds.
|
|
||||||
#
|
|
||||||
# Copyright (C) 2007-2012 LuaDist.
|
|
||||||
# by David Manura, Peter Drahoš
|
|
||||||
# Redistribution and use of this file is allowed according to the terms of the MIT license.
|
|
||||||
# For details see the COPYRIGHT file distributed with LuaDist.
|
|
||||||
# Please note that the package source code is licensed under its own license.
|
|
||||||
|
|
||||||
## Extract information from dist.info
|
|
||||||
if ( NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/dist.info )
|
|
||||||
message ( FATAL_ERROR
|
|
||||||
"Missing dist.info file (${CMAKE_CURRENT_SOURCE_DIR}/dist.info)." )
|
|
||||||
endif ()
|
|
||||||
file ( READ ${CMAKE_CURRENT_SOURCE_DIR}/dist.info DIST_INFO )
|
|
||||||
if ( "${DIST_INFO}" STREQUAL "" )
|
|
||||||
message ( FATAL_ERROR "Failed to load dist.info." )
|
|
||||||
endif ()
|
|
||||||
# Reads field `name` from dist.info string `DIST_INFO` into variable `var`.
|
|
||||||
macro ( _parse_dist_field name var )
|
|
||||||
string ( REGEX REPLACE ".*${name}[ \t]?=[ \t]?[\"']([^\"']+)[\"'].*" "\\1"
|
|
||||||
${var} "${DIST_INFO}" )
|
|
||||||
if ( ${var} STREQUAL DIST_INFO )
|
|
||||||
message ( FATAL_ERROR "Failed to extract \"${var}\" from dist.info" )
|
|
||||||
endif ()
|
|
||||||
endmacro ()
|
|
||||||
#
|
|
||||||
_parse_dist_field ( name DIST_NAME )
|
|
||||||
_parse_dist_field ( version DIST_VERSION )
|
|
||||||
_parse_dist_field ( license DIST_LICENSE )
|
|
||||||
_parse_dist_field ( author DIST_AUTHOR )
|
|
||||||
_parse_dist_field ( maintainer DIST_MAINTAINER )
|
|
||||||
_parse_dist_field ( url DIST_URL )
|
|
||||||
_parse_dist_field ( desc DIST_DESC )
|
|
||||||
message ( "DIST_NAME: ${DIST_NAME}")
|
|
||||||
message ( "DIST_VERSION: ${DIST_VERSION}")
|
|
||||||
message ( "DIST_LICENSE: ${DIST_LICENSE}")
|
|
||||||
message ( "DIST_AUTHOR: ${DIST_AUTHOR}")
|
|
||||||
message ( "DIST_MAINTAINER: ${DIST_MAINTAINER}")
|
|
||||||
message ( "DIST_URL: ${DIST_URL}")
|
|
||||||
message ( "DIST_DESC: ${DIST_DESC}")
|
|
||||||
string ( REGEX REPLACE ".*depends[ \t]?=[ \t]?[\"']([^\"']+)[\"'].*" "\\1"
|
|
||||||
DIST_DEPENDS ${DIST_INFO} )
|
|
||||||
if ( DIST_DEPENDS STREQUAL DIST_INFO )
|
|
||||||
set ( DIST_DEPENDS "" )
|
|
||||||
endif ()
|
|
||||||
message ( "DIST_DEPENDS: ${DIST_DEPENDS}")
|
|
||||||
## 2DO: Parse DIST_DEPENDS and try to install Dependencies with automatically using externalproject_add
|
|
||||||
|
|
||||||
|
|
||||||
## INSTALL DEFAULTS (Relative to CMAKE_INSTALL_PREFIX)
|
|
||||||
# Primary paths
|
|
||||||
set ( INSTALL_BIN bin CACHE PATH "Where to install binaries to." )
|
|
||||||
set ( INSTALL_LIB lib CACHE PATH "Where to install libraries to." )
|
|
||||||
set ( INSTALL_INC include CACHE PATH "Where to install headers to." )
|
|
||||||
set ( INSTALL_ETC etc CACHE PATH "Where to store configuration files" )
|
|
||||||
set ( INSTALL_SHARE share CACHE PATH "Directory for shared data." )
|
|
||||||
|
|
||||||
# Secondary paths
|
|
||||||
option ( INSTALL_VERSION
|
|
||||||
"Install runtime libraries and executables with version information." OFF)
|
|
||||||
set ( INSTALL_DATA ${INSTALL_SHARE}/${DIST_NAME} CACHE PATH
|
|
||||||
"Directory the package can store documentation, tests or other data in.")
|
|
||||||
set ( INSTALL_DOC ${INSTALL_DATA}/doc CACHE PATH
|
|
||||||
"Recommended directory to install documentation into.")
|
|
||||||
set ( INSTALL_EXAMPLE ${INSTALL_DATA}/example CACHE PATH
|
|
||||||
"Recommended directory to install examples into.")
|
|
||||||
set ( INSTALL_TEST ${INSTALL_DATA}/test CACHE PATH
|
|
||||||
"Recommended directory to install tests into.")
|
|
||||||
set ( INSTALL_FOO ${INSTALL_DATA}/etc CACHE PATH
|
|
||||||
"Where to install additional files")
|
|
||||||
|
|
||||||
# Tweaks and other defaults
|
|
||||||
# Setting CMAKE to use loose block and search for find modules in source directory
|
|
||||||
set ( CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true )
|
|
||||||
set ( CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH} )
|
|
||||||
option ( BUILD_SHARED_LIBS "Build shared libraries" ON )
|
|
||||||
|
|
||||||
# In MSVC, prevent warnings that can occur when using standard libraries.
|
|
||||||
if ( MSVC )
|
|
||||||
add_definitions ( -D_CRT_SECURE_NO_WARNINGS )
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
# RPath and relative linking
|
|
||||||
option ( USE_RPATH "Use relative linking." ON)
|
|
||||||
if ( USE_RPATH )
|
|
||||||
string ( REGEX REPLACE "[^!/]+" ".." UP_DIR ${INSTALL_BIN} )
|
|
||||||
set ( CMAKE_SKIP_BUILD_RPATH FALSE CACHE STRING "" FORCE )
|
|
||||||
set ( CMAKE_BUILD_WITH_INSTALL_RPATH FALSE CACHE STRING "" FORCE )
|
|
||||||
set ( CMAKE_INSTALL_RPATH $ORIGIN/${UP_DIR}/${INSTALL_LIB}
|
|
||||||
CACHE STRING "" FORCE )
|
|
||||||
set ( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE CACHE STRING "" FORCE )
|
|
||||||
set ( CMAKE_INSTALL_NAME_DIR @executable_path/${UP_DIR}/${INSTALL_LIB}
|
|
||||||
CACHE STRING "" FORCE )
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
## MACROS
|
|
||||||
# Parser macro
|
|
||||||
macro ( parse_arguments prefix arg_names option_names)
|
|
||||||
set ( DEFAULT_ARGS )
|
|
||||||
foreach ( arg_name ${arg_names} )
|
|
||||||
set ( ${prefix}_${arg_name} )
|
|
||||||
endforeach ()
|
|
||||||
foreach ( option ${option_names} )
|
|
||||||
set ( ${prefix}_${option} FALSE )
|
|
||||||
endforeach ()
|
|
||||||
|
|
||||||
set ( current_arg_name DEFAULT_ARGS )
|
|
||||||
set ( current_arg_list )
|
|
||||||
foreach ( arg ${ARGN} )
|
|
||||||
set ( larg_names ${arg_names} )
|
|
||||||
list ( FIND larg_names "${arg}" is_arg_name )
|
|
||||||
if ( is_arg_name GREATER -1 )
|
|
||||||
set ( ${prefix}_${current_arg_name} ${current_arg_list} )
|
|
||||||
set ( current_arg_name ${arg} )
|
|
||||||
set ( current_arg_list )
|
|
||||||
else ()
|
|
||||||
set ( loption_names ${option_names} )
|
|
||||||
list ( FIND loption_names "${arg}" is_option )
|
|
||||||
if ( is_option GREATER -1 )
|
|
||||||
set ( ${prefix}_${arg} TRUE )
|
|
||||||
else ()
|
|
||||||
set ( current_arg_list ${current_arg_list} ${arg} )
|
|
||||||
endif ()
|
|
||||||
endif ()
|
|
||||||
endforeach ()
|
|
||||||
set ( ${prefix}_${current_arg_name} ${current_arg_list} )
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
|
|
||||||
# install_executable ( executable_targets )
|
|
||||||
# Installs any executables generated using "add_executable".
|
|
||||||
# USE: install_executable ( lua )
|
|
||||||
# NOTE: subdirectories are NOT supported
|
|
||||||
set ( CPACK_COMPONENT_RUNTIME_DISPLAY_NAME "${DIST_NAME} Runtime" )
|
|
||||||
set ( CPACK_COMPONENT_RUNTIME_DESCRIPTION
|
|
||||||
"Executables and runtime libraries. Installed into ${INSTALL_BIN}." )
|
|
||||||
macro ( install_executable )
|
|
||||||
foreach ( _file ${ARGN} )
|
|
||||||
if ( INSTALL_VERSION )
|
|
||||||
set_target_properties ( ${_file} PROPERTIES VERSION ${DIST_VERSION}
|
|
||||||
SOVERSION ${DIST_VERSION} )
|
|
||||||
endif ()
|
|
||||||
install ( TARGETS ${_file} RUNTIME DESTINATION ${INSTALL_BIN}
|
|
||||||
COMPONENT Runtime )
|
|
||||||
endforeach()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# install_library ( library_targets )
|
|
||||||
# Installs any libraries generated using "add_library" into apropriate places.
|
|
||||||
# USE: install_library ( libexpat )
|
|
||||||
# NOTE: subdirectories are NOT supported
|
|
||||||
set ( CPACK_COMPONENT_LIBRARY_DISPLAY_NAME "${DIST_NAME} Development Libraries" )
|
|
||||||
set ( CPACK_COMPONENT_LIBRARY_DESCRIPTION
|
|
||||||
"Static and import libraries needed for development. Installed into ${INSTALL_LIB} or ${INSTALL_BIN}." )
|
|
||||||
macro ( install_library )
|
|
||||||
foreach ( _file ${ARGN} )
|
|
||||||
if ( INSTALL_VERSION )
|
|
||||||
set_target_properties ( ${_file} PROPERTIES VERSION ${DIST_VERSION}
|
|
||||||
SOVERSION ${DIST_VERSION} )
|
|
||||||
endif ()
|
|
||||||
install ( TARGETS ${_file}
|
|
||||||
RUNTIME DESTINATION ${INSTALL_BIN} COMPONENT Runtime
|
|
||||||
LIBRARY DESTINATION ${INSTALL_LIB} COMPONENT Runtime
|
|
||||||
ARCHIVE DESTINATION ${INSTALL_LIB} COMPONENT Library )
|
|
||||||
endforeach()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# helper function for various install_* functions, for PATTERN/REGEX args.
|
|
||||||
macro ( _complete_install_args )
|
|
||||||
if ( NOT("${_ARG_PATTERN}" STREQUAL "") )
|
|
||||||
set ( _ARG_PATTERN PATTERN ${_ARG_PATTERN} )
|
|
||||||
endif ()
|
|
||||||
if ( NOT("${_ARG_REGEX}" STREQUAL "") )
|
|
||||||
set ( _ARG_REGEX REGEX ${_ARG_REGEX} )
|
|
||||||
endif ()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# install_header ( files/directories [INTO destination] )
|
|
||||||
# Install a directories or files into header destination.
|
|
||||||
# USE: install_header ( lua.h luaconf.h ) or install_header ( GL )
|
|
||||||
# USE: install_header ( mylib.h INTO mylib )
|
|
||||||
# For directories, supports optional PATTERN/REGEX arguments like install().
|
|
||||||
set ( CPACK_COMPONENT_HEADER_DISPLAY_NAME "${DIST_NAME} Development Headers" )
|
|
||||||
set ( CPACK_COMPONENT_HEADER_DESCRIPTION
|
|
||||||
"Headers needed for development. Installed into ${INSTALL_INC}." )
|
|
||||||
macro ( install_header )
|
|
||||||
parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
|
|
||||||
_complete_install_args()
|
|
||||||
foreach ( _file ${_ARG_DEFAULT_ARGS} )
|
|
||||||
if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
|
|
||||||
install ( DIRECTORY ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO}
|
|
||||||
COMPONENT Header ${_ARG_PATTERN} ${_ARG_REGEX} )
|
|
||||||
else ()
|
|
||||||
install ( FILES ${_file} DESTINATION ${INSTALL_INC}/${_ARG_INTO}
|
|
||||||
COMPONENT Header )
|
|
||||||
endif ()
|
|
||||||
endforeach()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# install_data ( files/directories [INTO destination] )
|
|
||||||
# This installs additional data files or directories.
|
|
||||||
# USE: install_data ( extra data.dat )
|
|
||||||
# USE: install_data ( image1.png image2.png INTO images )
|
|
||||||
# For directories, supports optional PATTERN/REGEX arguments like install().
|
|
||||||
set ( CPACK_COMPONENT_DATA_DISPLAY_NAME "${DIST_NAME} Data" )
|
|
||||||
set ( CPACK_COMPONENT_DATA_DESCRIPTION
|
|
||||||
"Application data. Installed into ${INSTALL_DATA}." )
|
|
||||||
macro ( install_data )
|
|
||||||
parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
|
|
||||||
_complete_install_args()
|
|
||||||
foreach ( _file ${_ARG_DEFAULT_ARGS} )
|
|
||||||
if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
|
|
||||||
install ( DIRECTORY ${_file}
|
|
||||||
DESTINATION ${INSTALL_DATA}/${_ARG_INTO}
|
|
||||||
COMPONENT Data ${_ARG_PATTERN} ${_ARG_REGEX} )
|
|
||||||
else ()
|
|
||||||
install ( FILES ${_file} DESTINATION ${INSTALL_DATA}/${_ARG_INTO}
|
|
||||||
COMPONENT Data )
|
|
||||||
endif ()
|
|
||||||
endforeach()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# INSTALL_DOC ( files/directories [INTO destination] )
|
|
||||||
# This installs documentation content
|
|
||||||
# USE: install_doc ( doc/ doc.pdf )
|
|
||||||
# USE: install_doc ( index.html INTO html )
|
|
||||||
# For directories, supports optional PATTERN/REGEX arguments like install().
|
|
||||||
set ( CPACK_COMPONENT_DOCUMENTATION_DISPLAY_NAME "${DIST_NAME} Documentation" )
|
|
||||||
set ( CPACK_COMPONENT_DOCUMENTATION_DESCRIPTION
|
|
||||||
"Application documentation. Installed into ${INSTALL_DOC}." )
|
|
||||||
macro ( install_doc )
|
|
||||||
parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
|
|
||||||
_complete_install_args()
|
|
||||||
foreach ( _file ${_ARG_DEFAULT_ARGS} )
|
|
||||||
if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
|
|
||||||
install ( DIRECTORY ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO}
|
|
||||||
COMPONENT Documentation ${_ARG_PATTERN} ${_ARG_REGEX} )
|
|
||||||
else ()
|
|
||||||
install ( FILES ${_file} DESTINATION ${INSTALL_DOC}/${_ARG_INTO}
|
|
||||||
COMPONENT Documentation )
|
|
||||||
endif ()
|
|
||||||
endforeach()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# install_example ( files/directories [INTO destination] )
|
|
||||||
# This installs additional examples
|
|
||||||
# USE: install_example ( examples/ exampleA )
|
|
||||||
# USE: install_example ( super_example super_data INTO super)
|
|
||||||
# For directories, supports optional PATTERN/REGEX argument like install().
|
|
||||||
set ( CPACK_COMPONENT_EXAMPLE_DISPLAY_NAME "${DIST_NAME} Examples" )
|
|
||||||
set ( CPACK_COMPONENT_EXAMPLE_DESCRIPTION
|
|
||||||
"Examples and their associated data. Installed into ${INSTALL_EXAMPLE}." )
|
|
||||||
macro ( install_example )
|
|
||||||
parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
|
|
||||||
_complete_install_args()
|
|
||||||
foreach ( _file ${_ARG_DEFAULT_ARGS} )
|
|
||||||
if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
|
|
||||||
install ( DIRECTORY ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO}
|
|
||||||
COMPONENT Example ${_ARG_PATTERN} ${_ARG_REGEX} )
|
|
||||||
else ()
|
|
||||||
install ( FILES ${_file} DESTINATION ${INSTALL_EXAMPLE}/${_ARG_INTO}
|
|
||||||
COMPONENT Example )
|
|
||||||
endif ()
|
|
||||||
endforeach()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# install_test ( files/directories [INTO destination] )
|
|
||||||
# This installs tests and test files, DOES NOT EXECUTE TESTS
|
|
||||||
# USE: install_test ( my_test data.sql )
|
|
||||||
# USE: install_test ( feature_x_test INTO x )
|
|
||||||
# For directories, supports optional PATTERN/REGEX argument like install().
|
|
||||||
set ( CPACK_COMPONENT_TEST_DISPLAY_NAME "${DIST_NAME} Tests" )
|
|
||||||
set ( CPACK_COMPONENT_TEST_DESCRIPTION
|
|
||||||
"Tests and associated data. Installed into ${INSTALL_TEST}." )
|
|
||||||
macro ( install_test )
|
|
||||||
parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
|
|
||||||
_complete_install_args()
|
|
||||||
foreach ( _file ${_ARG_DEFAULT_ARGS} )
|
|
||||||
if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
|
|
||||||
install ( DIRECTORY ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO}
|
|
||||||
COMPONENT Test ${_ARG_PATTERN} ${_ARG_REGEX} )
|
|
||||||
else ()
|
|
||||||
install ( FILES ${_file} DESTINATION ${INSTALL_TEST}/${_ARG_INTO}
|
|
||||||
COMPONENT Test )
|
|
||||||
endif ()
|
|
||||||
endforeach()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# install_foo ( files/directories [INTO destination] )
|
|
||||||
# This installs optional or otherwise unneeded content
|
|
||||||
# USE: install_foo ( etc/ example.doc )
|
|
||||||
# USE: install_foo ( icon.png logo.png INTO icons)
|
|
||||||
# For directories, supports optional PATTERN/REGEX argument like install().
|
|
||||||
set ( CPACK_COMPONENT_OTHER_DISPLAY_NAME "${DIST_NAME} Unspecified Content" )
|
|
||||||
set ( CPACK_COMPONENT_OTHER_DESCRIPTION
|
|
||||||
"Other unspecified content. Installed into ${INSTALL_FOO}." )
|
|
||||||
macro ( install_foo )
|
|
||||||
parse_arguments ( _ARG "INTO;PATTERN;REGEX" "" ${ARGN} )
|
|
||||||
_complete_install_args()
|
|
||||||
foreach ( _file ${_ARG_DEFAULT_ARGS} )
|
|
||||||
if ( IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/${_file}" )
|
|
||||||
install ( DIRECTORY ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO}
|
|
||||||
COMPONENT Other ${_ARG_PATTERN} ${_ARG_REGEX} )
|
|
||||||
else ()
|
|
||||||
install ( FILES ${_file} DESTINATION ${INSTALL_FOO}/${_ARG_INTO}
|
|
||||||
COMPONENT Other )
|
|
||||||
endif ()
|
|
||||||
endforeach()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
## CTest defaults
|
|
||||||
|
|
||||||
## CPack defaults
|
|
||||||
set ( CPACK_GENERATOR "ZIP" )
|
|
||||||
set ( CPACK_STRIP_FILES TRUE )
|
|
||||||
set ( CPACK_PACKAGE_NAME "${DIST_NAME}" )
|
|
||||||
set ( CPACK_PACKAGE_VERSION "${DIST_VERSION}")
|
|
||||||
set ( CPACK_PACKAGE_VENDOR "LuaDist" )
|
|
||||||
set ( CPACK_COMPONENTS_ALL Runtime Library Header Data Documentation Example Other )
|
|
||||||
include ( CPack )
|
|
390
cmake/lua.cmake
390
cmake/lua.cmake
@ -1,390 +0,0 @@
|
|||||||
# LuaDist CMake utility library for Lua.
|
|
||||||
#
|
|
||||||
# Copyright (C) 2007-2012 LuaDist.
|
|
||||||
# by David Manura, Peter Drahos
|
|
||||||
# Redistribution and use of this file is allowed according to the terms of the MIT license.
|
|
||||||
# For details see the COPYRIGHT file distributed with LuaDist.
|
|
||||||
# Please note that the package source code is licensed under its own license.
|
|
||||||
|
|
||||||
set ( INSTALL_LMOD ${INSTALL_LIB}/lua
|
|
||||||
CACHE PATH "Directory to install Lua modules." )
|
|
||||||
set ( INSTALL_CMOD ${INSTALL_LIB}/lua
|
|
||||||
CACHE PATH "Directory to install Lua binary modules." )
|
|
||||||
|
|
||||||
option ( SKIP_LUA_WRAPPER
|
|
||||||
"Do not build and install Lua executable wrappers." OFF)
|
|
||||||
|
|
||||||
# List of (Lua module name, file path) pairs.
|
|
||||||
# Used internally by add_lua_test. Built by add_lua_module.
|
|
||||||
set ( _lua_modules )
|
|
||||||
|
|
||||||
# utility function: appends path `path` to path `basepath`, properly
|
|
||||||
# handling cases when `path` may be relative or absolute.
|
|
||||||
macro ( _append_path basepath path result )
|
|
||||||
if ( IS_ABSOLUTE "${path}" )
|
|
||||||
set ( ${result} "${path}" )
|
|
||||||
else ()
|
|
||||||
set ( ${result} "${basepath}/${path}" )
|
|
||||||
endif ()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# install_lua_executable ( target source )
|
|
||||||
# Automatically generate a binary wrapper for lua application and install it
|
|
||||||
# The wrapper and the source of the application will be placed into /bin
|
|
||||||
# If the application source did not have .lua suffix then it will be added
|
|
||||||
# USE: lua_executable ( sputnik src/sputnik.lua )
|
|
||||||
macro ( install_lua_executable _name _source )
|
|
||||||
get_filename_component ( _source_name ${_source} NAME_WE )
|
|
||||||
if ( NOT SKIP_LUA_WRAPPER )
|
|
||||||
enable_language ( C )
|
|
||||||
|
|
||||||
find_package ( Lua REQUIRED )
|
|
||||||
include_directories ( ${LUA_INCLUDE_DIR} )
|
|
||||||
|
|
||||||
set ( _wrapper ${CMAKE_CURRENT_BINARY_DIR}/${_name}.c )
|
|
||||||
set ( _code
|
|
||||||
"// Not so simple executable wrapper for Lua apps
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <signal.h>
|
|
||||||
#include <lua.h>
|
|
||||||
#include <lauxlib.h>
|
|
||||||
#include <lualib.h>
|
|
||||||
|
|
||||||
lua_State *L\;
|
|
||||||
|
|
||||||
static int getargs (lua_State *L, char **argv, int n) {
|
|
||||||
int narg\;
|
|
||||||
int i\;
|
|
||||||
int argc = 0\;
|
|
||||||
while (argv[argc]) argc++\;
|
|
||||||
narg = argc - (n + 1)\;
|
|
||||||
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 void lstop (lua_State *L, lua_Debug *ar) {
|
|
||||||
(void)ar\;
|
|
||||||
lua_sethook(L, NULL, 0, 0)\;
|
|
||||||
luaL_error(L, \"interrupted!\")\;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void laction (int i) {
|
|
||||||
signal(i, SIG_DFL)\;
|
|
||||||
lua_sethook(L, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1)\;
|
|
||||||
}
|
|
||||||
|
|
||||||
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(\"${_source_name}\", msg)\;
|
|
||||||
lua_pop(L, 1)\;
|
|
||||||
}
|
|
||||||
return status\;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int traceback (lua_State *L) {
|
|
||||||
if (!lua_isstring(L, 1))
|
|
||||||
return 1\;
|
|
||||||
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)\;
|
|
||||||
lua_pushinteger(L, 2)\;
|
|
||||||
lua_call(L, 2, 1)\;
|
|
||||||
return 1\;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int docall (lua_State *L, int narg, int clear) {
|
|
||||||
int status\;
|
|
||||||
int base = lua_gettop(L) - narg\;
|
|
||||||
lua_pushcfunction(L, traceback)\;
|
|
||||||
lua_insert(L, base)\;
|
|
||||||
signal(SIGINT, laction)\;
|
|
||||||
status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base)\;
|
|
||||||
signal(SIGINT, SIG_DFL)\;
|
|
||||||
lua_remove(L, base)\;
|
|
||||||
if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0)\;
|
|
||||||
return status\;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main (int argc, char **argv) {
|
|
||||||
L=lua_open()\;
|
|
||||||
lua_gc(L, LUA_GCSTOP, 0)\;
|
|
||||||
luaL_openlibs(L)\;
|
|
||||||
lua_gc(L, LUA_GCRESTART, 0)\;
|
|
||||||
int narg = getargs(L, argv, 0)\;
|
|
||||||
lua_setglobal(L, \"arg\")\;
|
|
||||||
|
|
||||||
// Script
|
|
||||||
char script[500] = \"./${_source_name}.lua\"\;
|
|
||||||
lua_getglobal(L, \"_PROGDIR\")\;
|
|
||||||
if (lua_isstring(L, -1)) {
|
|
||||||
sprintf( script, \"%s/${_source_name}.lua\", lua_tostring(L, -1))\;
|
|
||||||
}
|
|
||||||
lua_pop(L, 1)\;
|
|
||||||
|
|
||||||
// Run
|
|
||||||
int status = luaL_loadfile(L, script)\;
|
|
||||||
lua_insert(L, -(narg+1))\;
|
|
||||||
if (status == 0)
|
|
||||||
status = docall(L, narg, 0)\;
|
|
||||||
else
|
|
||||||
lua_pop(L, narg)\;
|
|
||||||
|
|
||||||
report(L, status)\;
|
|
||||||
lua_close(L)\;
|
|
||||||
return status\;
|
|
||||||
};
|
|
||||||
")
|
|
||||||
file ( WRITE ${_wrapper} ${_code} )
|
|
||||||
add_executable ( ${_name} ${_wrapper} )
|
|
||||||
target_link_libraries ( ${_name} ${LUA_LIBRARY} )
|
|
||||||
install ( TARGETS ${_name} DESTINATION ${INSTALL_BIN} )
|
|
||||||
endif()
|
|
||||||
install ( PROGRAMS ${_source} DESTINATION ${INSTALL_BIN}
|
|
||||||
RENAME ${_source_name}.lua )
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
macro ( _lua_module_helper is_install _name )
|
|
||||||
parse_arguments ( _MODULE "LINK;ALL_IN_ONE" "" ${ARGN} )
|
|
||||||
# _target is CMake-compatible target name for module (e.g. socket_core).
|
|
||||||
# _module is relative path of target (e.g. socket/core),
|
|
||||||
# without extension (e.g. .lua/.so/.dll).
|
|
||||||
# _MODULE_SRC is list of module source files (e.g. .lua and .c files).
|
|
||||||
# _MODULE_NAMES is list of module names (e.g. socket.core).
|
|
||||||
if ( _MODULE_ALL_IN_ONE )
|
|
||||||
string ( REGEX REPLACE "\\..*" "" _target "${_name}" )
|
|
||||||
string ( REGEX REPLACE "\\..*" "" _module "${_name}" )
|
|
||||||
set ( _target "${_target}_all_in_one")
|
|
||||||
set ( _MODULE_SRC ${_MODULE_ALL_IN_ONE} )
|
|
||||||
set ( _MODULE_NAMES ${_name} ${_MODULE_DEFAULT_ARGS} )
|
|
||||||
else ()
|
|
||||||
string ( REPLACE "." "_" _target "${_name}" )
|
|
||||||
string ( REPLACE "." "/" _module "${_name}" )
|
|
||||||
set ( _MODULE_SRC ${_MODULE_DEFAULT_ARGS} )
|
|
||||||
set ( _MODULE_NAMES ${_name} )
|
|
||||||
endif ()
|
|
||||||
if ( NOT _MODULE_SRC )
|
|
||||||
message ( FATAL_ERROR "no module sources specified" )
|
|
||||||
endif ()
|
|
||||||
list ( GET _MODULE_SRC 0 _first_source )
|
|
||||||
|
|
||||||
get_filename_component ( _ext ${_first_source} EXT )
|
|
||||||
if ( _ext STREQUAL ".lua" ) # Lua source module
|
|
||||||
list ( LENGTH _MODULE_SRC _len )
|
|
||||||
if ( _len GREATER 1 )
|
|
||||||
message ( FATAL_ERROR "more than one source file specified" )
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
set ( _module "${_module}.lua" )
|
|
||||||
|
|
||||||
get_filename_component ( _module_dir ${_module} PATH )
|
|
||||||
get_filename_component ( _module_filename ${_module} NAME )
|
|
||||||
_append_path ( "${CMAKE_CURRENT_SOURCE_DIR}" "${_first_source}" _module_path )
|
|
||||||
list ( APPEND _lua_modules "${_name}" "${_module_path}" )
|
|
||||||
|
|
||||||
if ( ${is_install} )
|
|
||||||
install ( FILES ${_first_source} DESTINATION ${INSTALL_LMOD}/${_module_dir}
|
|
||||||
RENAME ${_module_filename} )
|
|
||||||
endif ()
|
|
||||||
else () # Lua C binary module
|
|
||||||
enable_language ( C )
|
|
||||||
find_package ( Lua REQUIRED )
|
|
||||||
include_directories ( ${LUA_INCLUDE_DIR} )
|
|
||||||
|
|
||||||
set ( _module "${_module}${CMAKE_SHARED_MODULE_SUFFIX}" )
|
|
||||||
|
|
||||||
get_filename_component ( _module_dir ${_module} PATH )
|
|
||||||
get_filename_component ( _module_filenamebase ${_module} NAME_WE )
|
|
||||||
foreach ( _thisname ${_MODULE_NAMES} )
|
|
||||||
list ( APPEND _lua_modules "${_thisname}"
|
|
||||||
"${CMAKE_CURRENT_BINARY_DIR}/\${CMAKE_CFG_INTDIR}/${_module}" )
|
|
||||||
endforeach ()
|
|
||||||
|
|
||||||
add_library( ${_target} MODULE ${_MODULE_SRC})
|
|
||||||
target_link_libraries ( ${_target} ${LUA_LIBRARY} ${_MODULE_LINK} )
|
|
||||||
set_target_properties ( ${_target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY
|
|
||||||
"${_module_dir}" PREFIX "" OUTPUT_NAME "${_module_filenamebase}" )
|
|
||||||
if ( ${is_install} )
|
|
||||||
install ( TARGETS ${_target} DESTINATION ${INSTALL_CMOD}/${_module_dir})
|
|
||||||
endif ()
|
|
||||||
endif ()
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# add_lua_module
|
|
||||||
# Builds a Lua source module into a destination locatable by Lua
|
|
||||||
# require syntax.
|
|
||||||
# Binary modules are also supported where this function takes sources and
|
|
||||||
# libraries to compile separated by LINK keyword.
|
|
||||||
# USE: add_lua_module ( socket.http src/http.lua )
|
|
||||||
# USE2: add_lua_module ( mime.core src/mime.c )
|
|
||||||
# USE3: add_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} )
|
|
||||||
# USE4: add_lua_module ( ssl.context ssl.core ALL_IN_ONE src/context.c src/ssl.c )
|
|
||||||
# This form builds an "all-in-one" module (e.g. ssl.so or ssl.dll containing
|
|
||||||
# both modules ssl.context and ssl.core). The CMake target name will be
|
|
||||||
# ssl_all_in_one.
|
|
||||||
# Also sets variable _module_path (relative path where module typically
|
|
||||||
# would be installed).
|
|
||||||
macro ( add_lua_module )
|
|
||||||
_lua_module_helper ( 0 ${ARGN} )
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
|
|
||||||
# install_lua_module
|
|
||||||
# This is the same as `add_lua_module` but also installs the module.
|
|
||||||
# USE: install_lua_module ( socket.http src/http.lua )
|
|
||||||
# USE2: install_lua_module ( mime.core src/mime.c )
|
|
||||||
# USE3: install_lua_module ( socket.core ${SRC_SOCKET} LINK ${LIB_SOCKET} )
|
|
||||||
macro ( install_lua_module )
|
|
||||||
_lua_module_helper ( 1 ${ARGN} )
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# Builds string representing Lua table mapping Lua modules names to file
|
|
||||||
# paths. Used internally.
|
|
||||||
macro ( _make_module_table _outvar )
|
|
||||||
set ( ${_outvar} )
|
|
||||||
list ( LENGTH _lua_modules _n )
|
|
||||||
if ( ${_n} GREATER 0 ) # avoids cmake complaint
|
|
||||||
foreach ( _i RANGE 1 ${_n} 2 )
|
|
||||||
list ( GET _lua_modules ${_i} _path )
|
|
||||||
math ( EXPR _ii ${_i}-1 )
|
|
||||||
list ( GET _lua_modules ${_ii} _name )
|
|
||||||
set ( ${_outvar} "${_table} ['${_name}'] = '${_path}'\;\n")
|
|
||||||
endforeach ()
|
|
||||||
endif ()
|
|
||||||
set ( ${_outvar}
|
|
||||||
"local modules = {
|
|
||||||
${_table}}" )
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
# add_lua_test ( _testfile [ WORKING_DIRECTORY _working_dir ] )
|
|
||||||
# Runs Lua script `_testfile` under CTest tester.
|
|
||||||
# Optional named argument `WORKING_DIRECTORY` is current working directory to
|
|
||||||
# run test under (defaults to ${CMAKE_CURRENT_BINARY_DIR}).
|
|
||||||
# Both paths, if relative, are relative to ${CMAKE_CURRENT_SOURCE_DIR}.
|
|
||||||
# Any modules previously defined with install_lua_module are automatically
|
|
||||||
# preloaded (via package.preload) prior to running the test script.
|
|
||||||
# Under LuaDist, set test=true in config.lua to enable testing.
|
|
||||||
# USE: add_lua_test ( test/test1.lua [args...] [WORKING_DIRECTORY dir])
|
|
||||||
macro ( add_lua_test _testfile )
|
|
||||||
if ( NOT SKIP_TESTING )
|
|
||||||
parse_arguments ( _ARG "WORKING_DIRECTORY" "" ${ARGN} )
|
|
||||||
include ( CTest )
|
|
||||||
find_program ( LUA NAMES lua lua.bat )
|
|
||||||
get_filename_component ( TESTFILEABS ${_testfile} ABSOLUTE )
|
|
||||||
get_filename_component ( TESTFILENAME ${_testfile} NAME )
|
|
||||||
get_filename_component ( TESTFILEBASE ${_testfile} NAME_WE )
|
|
||||||
|
|
||||||
# Write wrapper script.
|
|
||||||
# Note: One simple way to allow the script to find modules is
|
|
||||||
# to just put them in package.preload.
|
|
||||||
set ( TESTWRAPPER ${CMAKE_CURRENT_BINARY_DIR}/${TESTFILENAME} )
|
|
||||||
_make_module_table ( _table )
|
|
||||||
set ( TESTWRAPPERSOURCE
|
|
||||||
"local CMAKE_CFG_INTDIR = ... or '.'
|
|
||||||
${_table}
|
|
||||||
local function preload_modules(modules)
|
|
||||||
for name, path in pairs(modules) do
|
|
||||||
if path:match'%.lua' then
|
|
||||||
package.preload[name] = assert(loadfile(path))
|
|
||||||
else
|
|
||||||
local name = name:gsub('.*%-', '') -- remove any hyphen prefix
|
|
||||||
local symbol = 'luaopen_' .. name:gsub('%.', '_')
|
|
||||||
--improve: generalize to support all-in-one loader?
|
|
||||||
local path = path:gsub('%$%{CMAKE_CFG_INTDIR%}', CMAKE_CFG_INTDIR)
|
|
||||||
package.preload[name] = assert(package.loadlib(path, symbol))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
preload_modules(modules)
|
|
||||||
arg[0] = '${TESTFILEABS}'
|
|
||||||
table.remove(arg, 1)
|
|
||||||
return assert(loadfile '${TESTFILEABS}')(unpack(arg))
|
|
||||||
" )
|
|
||||||
if ( _ARG_WORKING_DIRECTORY )
|
|
||||||
get_filename_component (
|
|
||||||
TESTCURRENTDIRABS ${_ARG_WORKING_DIRECTORY} ABSOLUTE )
|
|
||||||
# note: CMake 2.6 (unlike 2.8) lacks WORKING_DIRECTORY parameter.
|
|
||||||
set ( _pre ${CMAKE_COMMAND} -E chdir "${TESTCURRENTDIRABS}" )
|
|
||||||
endif ()
|
|
||||||
file ( WRITE ${TESTWRAPPER} ${TESTWRAPPERSOURCE})
|
|
||||||
add_test ( NAME ${TESTFILEBASE} COMMAND ${_pre} ${LUA}
|
|
||||||
${TESTWRAPPER} "${CMAKE_CFG_INTDIR}"
|
|
||||||
${_ARG_DEFAULT_ARGS} )
|
|
||||||
endif ()
|
|
||||||
# see also http://gdcm.svn.sourceforge.net/viewvc/gdcm/Sandbox/CMakeModules/UsePythonTest.cmake
|
|
||||||
# Note: ${CMAKE_CFG_INTDIR} is a command-line argument to allow proper
|
|
||||||
# expansion by the native build tool.
|
|
||||||
endmacro ()
|
|
||||||
|
|
||||||
|
|
||||||
# Converts Lua source file `_source` to binary string embedded in C source
|
|
||||||
# file `_target`. Optionally compiles Lua source to byte code (not available
|
|
||||||
# under LuaJIT2, which doesn't have a bytecode loader). Additionally, Lua
|
|
||||||
# versions of bin2c [1] and luac [2] may be passed respectively as additional
|
|
||||||
# arguments.
|
|
||||||
#
|
|
||||||
# [1] http://lua-users.org/wiki/BinToCee
|
|
||||||
# [2] http://lua-users.org/wiki/LuaCompilerInLua
|
|
||||||
function ( add_lua_bin2c _target _source )
|
|
||||||
find_program ( LUA NAMES lua lua.bat )
|
|
||||||
execute_process ( COMMAND ${LUA} -e "string.dump(function()end)"
|
|
||||||
RESULT_VARIABLE _LUA_DUMP_RESULT ERROR_QUIET )
|
|
||||||
if ( NOT ${_LUA_DUMP_RESULT} )
|
|
||||||
SET ( HAVE_LUA_DUMP true )
|
|
||||||
endif ()
|
|
||||||
message ( "-- string.dump=${HAVE_LUA_DUMP}" )
|
|
||||||
|
|
||||||
if ( ARGV2 )
|
|
||||||
get_filename_component ( BIN2C ${ARGV2} ABSOLUTE )
|
|
||||||
set ( BIN2C ${LUA} ${BIN2C} )
|
|
||||||
else ()
|
|
||||||
find_program ( BIN2C NAMES bin2c bin2c.bat )
|
|
||||||
endif ()
|
|
||||||
if ( HAVE_LUA_DUMP )
|
|
||||||
if ( ARGV3 )
|
|
||||||
get_filename_component ( LUAC ${ARGV3} ABSOLUTE )
|
|
||||||
set ( LUAC ${LUA} ${LUAC} )
|
|
||||||
else ()
|
|
||||||
find_program ( LUAC NAMES luac luac.bat )
|
|
||||||
endif ()
|
|
||||||
endif ( HAVE_LUA_DUMP )
|
|
||||||
message ( "-- bin2c=${BIN2C}" )
|
|
||||||
message ( "-- luac=${LUAC}" )
|
|
||||||
|
|
||||||
get_filename_component ( SOURCEABS ${_source} ABSOLUTE )
|
|
||||||
if ( HAVE_LUA_DUMP )
|
|
||||||
get_filename_component ( SOURCEBASE ${_source} NAME_WE )
|
|
||||||
add_custom_command (
|
|
||||||
OUTPUT ${_target} DEPENDS ${_source}
|
|
||||||
COMMAND ${LUAC} -o ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo
|
|
||||||
${SOURCEABS}
|
|
||||||
COMMAND ${BIN2C} ${CMAKE_CURRENT_BINARY_DIR}/${SOURCEBASE}.lo
|
|
||||||
">${_target}" )
|
|
||||||
else ()
|
|
||||||
add_custom_command (
|
|
||||||
OUTPUT ${_target} DEPENDS ${SOURCEABS}
|
|
||||||
COMMAND ${BIN2C} ${_source} ">${_target}" )
|
|
||||||
endif ()
|
|
||||||
endfunction()
|
|
35
dist.info
35
dist.info
@ -1,14 +1,29 @@
|
|||||||
--- This file is part of LuaDist project
|
type = "x86"
|
||||||
|
arch = "Windows"
|
||||||
name = "dkjson"
|
author = "David Kolf"
|
||||||
version = "2.5"
|
depends = {
|
||||||
|
[[lua >= 5.1]],
|
||||||
|
}
|
||||||
|
|
||||||
desc = "dkjson is a module for encoding and decoding JSON data. It supports UTF-8."
|
desc = "dkjson is a module for encoding and decoding JSON data. It supports UTF-8."
|
||||||
author = "David Kolf"
|
version = "2.2"
|
||||||
license = "MIT/X11"
|
|
||||||
url = "http://dkolf.de/src/dkjson-lua.fsl/"
|
|
||||||
maintainer = "Peter Drahoš"
|
maintainer = "Peter Drahoš"
|
||||||
|
files = {
|
||||||
depends = {
|
Example = {
|
||||||
"lua >= 5.1"
|
[[share\dkjson\example\jsontest.lua]],
|
||||||
|
[[share\dkjson\example\speedtest.lua]],
|
||||||
}
|
}
|
||||||
|
,
|
||||||
|
Data = {
|
||||||
|
[[share\dkjson\versions.txt]],
|
||||||
|
}
|
||||||
|
,
|
||||||
|
Unspecified = {
|
||||||
|
[[lib\lua\dkjson.lua]],
|
||||||
|
}
|
||||||
|
,
|
||||||
|
}
|
||||||
|
|
||||||
|
license = "MIT/X11"
|
||||||
|
name = "dkjson"
|
||||||
|
url = "http://chiselapp.com/user/dhkolf/repository/dkjson/"
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
package = "dkjson"
|
|
||||||
version = "2.5-2"
|
|
||||||
source = {
|
|
||||||
url = "http://dkolf.de/src/dkjson-lua.fsl/tarball/dkjson-2.5.tar.gz?uuid=release_2_5",
|
|
||||||
file = "dkjson-2.5.tar.gz"
|
|
||||||
}
|
|
||||||
description = {
|
|
||||||
summary = "David Kolf's JSON module for Lua",
|
|
||||||
detailed = [[
|
|
||||||
dkjson is a module for encoding and decoding JSON data. It supports UTF-8.
|
|
||||||
|
|
||||||
JSON (JavaScript Object Notation) is a format for serializing data based
|
|
||||||
on the syntax for JavaScript data structures.
|
|
||||||
|
|
||||||
dkjson is written in Lua without any dependencies, but
|
|
||||||
when LPeg is available dkjson uses it to speed up decoding.
|
|
||||||
]],
|
|
||||||
homepage = "http://dkolf.de/src/dkjson-lua.fsl/",
|
|
||||||
license = "MIT/X11"
|
|
||||||
}
|
|
||||||
dependencies = {
|
|
||||||
"lua >= 5.1, < 5.4"
|
|
||||||
}
|
|
||||||
build = {
|
|
||||||
type = "builtin",
|
|
||||||
modules = {
|
|
||||||
dkjson = "dkjson.lua"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,23 +1,170 @@
|
|||||||
-- Module options:
|
-- Module options:
|
||||||
local always_try_using_lpeg = true
|
local always_try_using_lpeg = true
|
||||||
local register_global_module_table = false
|
|
||||||
local global_module_name = 'json'
|
|
||||||
|
|
||||||
--[==[
|
--[==[
|
||||||
|
|
||||||
David Kolf's JSON module for Lua 5.1/5.2
|
David Kolf's JSON module for Lua 5.1/5.2
|
||||||
|
========================================
|
||||||
|
|
||||||
Version 2.5
|
*Version 2.2*
|
||||||
|
|
||||||
|
This module writes no global values, not even the module table.
|
||||||
|
Import it using
|
||||||
|
|
||||||
For the documentation see the corresponding readme.txt or visit
|
json = require ("dkjson")
|
||||||
<http://dkolf.de/src/dkjson-lua.fsl/>.
|
|
||||||
|
|
||||||
You can contact the author by sending an e-mail to 'david' at the
|
Exported functions and values:
|
||||||
domain 'dkolf.de'.
|
|
||||||
|
|
||||||
|
`json.encode (object [, state])`
|
||||||
|
--------------------------------
|
||||||
|
|
||||||
Copyright (C) 2010-2014 David Heiko Kolf
|
Create a string representing the object. `Object` can be a table,
|
||||||
|
a string, a number, a boolean, `nil`, `json.null` or any object with
|
||||||
|
a function `__tojson` in its metatable. A table can only use strings
|
||||||
|
and numbers as keys and its values have to be valid objects as
|
||||||
|
well. It raises an error for any invalid data types or reference
|
||||||
|
cycles.
|
||||||
|
|
||||||
|
`state` is an optional table with the following fields:
|
||||||
|
|
||||||
|
- `indent`
|
||||||
|
When `indent` (a boolean) is set, the created string will contain
|
||||||
|
newlines and indentations. Otherwise it will be one long line.
|
||||||
|
- `keyorder`
|
||||||
|
`keyorder` is an array to specify the ordering of keys in the
|
||||||
|
encoded output. If an object has keys which are not in this array
|
||||||
|
they are written after the sorted keys.
|
||||||
|
- `level`
|
||||||
|
This is the initial level of indentation used when `indent` is
|
||||||
|
set. For each level two spaces are added. When absent it is set
|
||||||
|
to 0.
|
||||||
|
- `buffer`
|
||||||
|
`buffer` is an array to store the strings for the result so they
|
||||||
|
can be concatenated at once. When it isn't given, the encode
|
||||||
|
function will create it temporary and will return the
|
||||||
|
concatenated result.
|
||||||
|
- `bufferlen`
|
||||||
|
When `bufferlen` is set, it has to be the index of the last
|
||||||
|
element of `buffer`.
|
||||||
|
- `tables`
|
||||||
|
`tables` is a set to detect reference cycles. It is created
|
||||||
|
temporary when absent. Every table that is currently processed
|
||||||
|
is used as key, the value is `true`.
|
||||||
|
|
||||||
|
When `state.buffer` was set, the return value will be `true` on
|
||||||
|
success. Without `state.buffer` the return value will be a string.
|
||||||
|
|
||||||
|
`json.decode (string [, position [, null]])`
|
||||||
|
--------------------------------------------
|
||||||
|
|
||||||
|
Decode `string` starting at `position` or at 1 if `position` was
|
||||||
|
omitted.
|
||||||
|
|
||||||
|
`null` is an optional value to be returned for null values. The
|
||||||
|
default is `nil`, but you could set it to `json.null` or any other
|
||||||
|
value.
|
||||||
|
|
||||||
|
The return values are the object or `nil`, the position of the next
|
||||||
|
character that doesn't belong to the object, and in case of errors
|
||||||
|
an error message.
|
||||||
|
|
||||||
|
Two metatables are created. Every array or object that is decoded gets
|
||||||
|
a metatable with the `__jsontype` field set to either `array` or
|
||||||
|
`object`. If you want to provide your own metatables use the syntax
|
||||||
|
|
||||||
|
json.decode (string, position, null, objectmeta, arraymeta)
|
||||||
|
|
||||||
|
To prevent the assigning of metatables pass `nil`:
|
||||||
|
|
||||||
|
json.decode (string, position, null, nil)
|
||||||
|
|
||||||
|
`<metatable>.__jsonorder`
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
`__jsonorder` can overwrite the `keyorder` for a specific table.
|
||||||
|
|
||||||
|
`<metatable>.__jsontype`
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
`__jsontype` can be either `"array"` or `"object"`. This value is only
|
||||||
|
checked for empty tables. (The default for empty tables is `"array"`).
|
||||||
|
|
||||||
|
`<metatable>.__tojson (self, state)`
|
||||||
|
------------------------------------
|
||||||
|
|
||||||
|
You can provide your own `__tojson` function in a metatable. In this
|
||||||
|
function you can either add directly to the buffer and return true,
|
||||||
|
or you can return a string. On errors nil and a message should be
|
||||||
|
returned.
|
||||||
|
|
||||||
|
`json.null`
|
||||||
|
-----------
|
||||||
|
|
||||||
|
You can use this value for setting explicit `null` values.
|
||||||
|
|
||||||
|
`json.version`
|
||||||
|
--------------
|
||||||
|
|
||||||
|
Set to `"dkjson 2.2"`.
|
||||||
|
|
||||||
|
`json.quotestring (string)`
|
||||||
|
---------------------------
|
||||||
|
|
||||||
|
Quote a UTF-8 string and escape critical characters using JSON
|
||||||
|
escape sequences. This function is only necessary when you build
|
||||||
|
your own `__tojson` functions.
|
||||||
|
|
||||||
|
`json.addnewline (state)`
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
When `state.indent` is set, add a newline to `state.buffer` and spaces
|
||||||
|
according to `state.level`.
|
||||||
|
|
||||||
|
LPeg support
|
||||||
|
------------
|
||||||
|
|
||||||
|
When the local configuration variable `always_try_using_lpeg` is set,
|
||||||
|
this module tries to load LPeg to replace the `decode` function. The
|
||||||
|
speed increase is significant. You can get the LPeg module at
|
||||||
|
<http://www.inf.puc-rio.br/~roberto/lpeg/>.
|
||||||
|
When LPeg couldn't be loaded, the pure Lua functions stay active.
|
||||||
|
|
||||||
|
In case you don't want this module to require LPeg on its own,
|
||||||
|
disable the option `always_try_using_lpeg` in the options section at
|
||||||
|
the top of the module.
|
||||||
|
|
||||||
|
In this case you can later load LPeg support using
|
||||||
|
|
||||||
|
### `json.use_lpeg ()`
|
||||||
|
|
||||||
|
Require the LPeg module and replace the functions `quotestring` and
|
||||||
|
and `decode` with functions that use LPeg patterns.
|
||||||
|
This function returns the module table, so you can load the module
|
||||||
|
using:
|
||||||
|
|
||||||
|
json = require "dkjson".use_lpeg()
|
||||||
|
|
||||||
|
Alternatively you can use `pcall` so the JSON module still works when
|
||||||
|
LPeg isn't found.
|
||||||
|
|
||||||
|
json = require "dkjson"
|
||||||
|
pcall (json.use_lpeg)
|
||||||
|
|
||||||
|
### `json.using_lpeg`
|
||||||
|
|
||||||
|
This variable is set to `true` when LPeg was loaded successfully.
|
||||||
|
|
||||||
|
---------------------------------------------------------------------
|
||||||
|
|
||||||
|
Contact
|
||||||
|
-------
|
||||||
|
|
||||||
|
You can contact the author by sending an e-mail to 'kolf' at the
|
||||||
|
e-mail provider 'gmx.de'.
|
||||||
|
|
||||||
|
---------------------------------------------------------------------
|
||||||
|
|
||||||
|
*Copyright (C) 2010, 2011, 2012 David Heiko Kolf*
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
a copy of this software and associated documentation files (the
|
a copy of this software and associated documentation files (the
|
||||||
@ -39,7 +186,13 @@ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|||||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
|
|
||||||
--]==]
|
<!-- This documentation can be parsed using Markdown to generate HTML.
|
||||||
|
The source code is enclosed in a HTML comment so it won't be displayed
|
||||||
|
by browsers, but it should be removed from the final HTML file as
|
||||||
|
it isn't a valid HTML comment (and wastes space).
|
||||||
|
-->
|
||||||
|
|
||||||
|
<!--]==]
|
||||||
|
|
||||||
-- global dependencies:
|
-- global dependencies:
|
||||||
local pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset =
|
local pairs, type, tostring, tonumber, getmetatable, setmetatable, rawset =
|
||||||
@ -49,17 +202,12 @@ local floor, huge = math.floor, math.huge
|
|||||||
local strrep, gsub, strsub, strbyte, strchar, strfind, strlen, strformat =
|
local strrep, gsub, strsub, strbyte, strchar, strfind, strlen, strformat =
|
||||||
string.rep, string.gsub, string.sub, string.byte, string.char,
|
string.rep, string.gsub, string.sub, string.byte, string.char,
|
||||||
string.find, string.len, string.format
|
string.find, string.len, string.format
|
||||||
local strmatch = string.match
|
|
||||||
local concat = table.concat
|
local concat = table.concat
|
||||||
|
|
||||||
local json = { version = "dkjson 2.5" }
|
|
||||||
|
|
||||||
if register_global_module_table then
|
|
||||||
_G[global_module_name] = json
|
|
||||||
end
|
|
||||||
|
|
||||||
local _ENV = nil -- blocking globals in Lua 5.2
|
local _ENV = nil -- blocking globals in Lua 5.2
|
||||||
|
|
||||||
|
local json = { version = "dkjson 2.2" }
|
||||||
|
|
||||||
pcall (function()
|
pcall (function()
|
||||||
-- Enable access to blocked metatables.
|
-- Enable access to blocked metatables.
|
||||||
-- Don't worry, this module doesn't change anything in them.
|
-- Don't worry, this module doesn't change anything in them.
|
||||||
@ -149,48 +297,15 @@ local function quotestring (value)
|
|||||||
value = fsub (value, "\216[\128-\132]", escapeutf8)
|
value = fsub (value, "\216[\128-\132]", escapeutf8)
|
||||||
value = fsub (value, "\220\143", escapeutf8)
|
value = fsub (value, "\220\143", escapeutf8)
|
||||||
value = fsub (value, "\225\158[\180\181]", escapeutf8)
|
value = fsub (value, "\225\158[\180\181]", escapeutf8)
|
||||||
value = fsub (value, "\226\128[\140-\143\168-\175]", escapeutf8)
|
value = fsub (value, "\226\128[\140-\143\168\175]", escapeutf8)
|
||||||
value = fsub (value, "\226\129[\160-\175]", escapeutf8)
|
value = fsub (value, "\226\129[\160-\175]", escapeutf8)
|
||||||
value = fsub (value, "\239\187\191", escapeutf8)
|
value = fsub (value, "\239\187\191", escapeutf8)
|
||||||
value = fsub (value, "\239\191[\176-\191]", escapeutf8)
|
value = fsub (value, "\239\191[\176\191]", escapeutf8)
|
||||||
end
|
end
|
||||||
return "\"" .. value .. "\""
|
return "\"" .. value .. "\""
|
||||||
end
|
end
|
||||||
json.quotestring = quotestring
|
json.quotestring = quotestring
|
||||||
|
|
||||||
local function replace(str, o, n)
|
|
||||||
local i, j = strfind (str, o, 1, true)
|
|
||||||
if i then
|
|
||||||
return strsub(str, 1, i-1) .. n .. strsub(str, j+1, -1)
|
|
||||||
else
|
|
||||||
return str
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- locale independent num2str and str2num functions
|
|
||||||
local decpoint, numfilter
|
|
||||||
|
|
||||||
local function updatedecpoint ()
|
|
||||||
decpoint = strmatch(tostring(0.5), "([^05+])")
|
|
||||||
-- build a filter that can be used to remove group separators
|
|
||||||
numfilter = "[^0-9%-%+eE" .. gsub(decpoint, "[%^%$%(%)%%%.%[%]%*%+%-%?]", "%%%0") .. "]+"
|
|
||||||
end
|
|
||||||
|
|
||||||
updatedecpoint()
|
|
||||||
|
|
||||||
local function num2str (num)
|
|
||||||
return replace(fsub(tostring(num), numfilter, ""), decpoint, ".")
|
|
||||||
end
|
|
||||||
|
|
||||||
local function str2num (str)
|
|
||||||
local num = tonumber(replace(str, ".", decpoint))
|
|
||||||
if not num then
|
|
||||||
updatedecpoint()
|
|
||||||
num = tonumber(replace(str, ".", decpoint))
|
|
||||||
end
|
|
||||||
return num
|
|
||||||
end
|
|
||||||
|
|
||||||
local function addnewline2 (level, buffer, buflen)
|
local function addnewline2 (level, buffer, buflen)
|
||||||
buffer[buflen+1] = "\n"
|
buffer[buflen+1] = "\n"
|
||||||
buffer[buflen+2] = strrep (" ", level)
|
buffer[buflen+2] = strrep (" ", level)
|
||||||
@ -207,7 +322,7 @@ end
|
|||||||
|
|
||||||
local encode2 -- forward declaration
|
local encode2 -- forward declaration
|
||||||
|
|
||||||
local function addpair (key, value, prev, indent, level, buffer, buflen, tables, globalorder, state)
|
local function addpair (key, value, prev, indent, level, buffer, buflen, tables, globalorder)
|
||||||
local kt = type (key)
|
local kt = type (key)
|
||||||
if kt ~= 'string' and kt ~= 'number' then
|
if kt ~= 'string' and kt ~= 'number' then
|
||||||
return nil, "type '" .. kt .. "' is not supported as a key by JSON."
|
return nil, "type '" .. kt .. "' is not supported as a key by JSON."
|
||||||
@ -221,50 +336,31 @@ local function addpair (key, value, prev, indent, level, buffer, buflen, tables,
|
|||||||
end
|
end
|
||||||
buffer[buflen+1] = quotestring (key)
|
buffer[buflen+1] = quotestring (key)
|
||||||
buffer[buflen+2] = ":"
|
buffer[buflen+2] = ":"
|
||||||
return encode2 (value, indent, level, buffer, buflen + 2, tables, globalorder, state)
|
return encode2 (value, indent, level, buffer, buflen + 2, tables, globalorder)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function appendcustom(res, buffer, state)
|
encode2 = function (value, indent, level, buffer, buflen, tables, globalorder)
|
||||||
local buflen = state.bufferlen
|
|
||||||
if type (res) == 'string' then
|
|
||||||
buflen = buflen + 1
|
|
||||||
buffer[buflen] = res
|
|
||||||
end
|
|
||||||
return buflen
|
|
||||||
end
|
|
||||||
|
|
||||||
local function exception(reason, value, state, buffer, buflen, defaultmessage)
|
|
||||||
defaultmessage = defaultmessage or reason
|
|
||||||
local handler = state.exception
|
|
||||||
if not handler then
|
|
||||||
return nil, defaultmessage
|
|
||||||
else
|
|
||||||
state.bufferlen = buflen
|
|
||||||
local ret, msg = handler (reason, value, state, defaultmessage)
|
|
||||||
if not ret then return nil, msg or defaultmessage end
|
|
||||||
return appendcustom(ret, buffer, state)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function json.encodeexception(reason, value, state, defaultmessage)
|
|
||||||
return quotestring("<" .. defaultmessage .. ">")
|
|
||||||
end
|
|
||||||
|
|
||||||
encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, state)
|
|
||||||
local valtype = type (value)
|
local valtype = type (value)
|
||||||
local valmeta = getmetatable (value)
|
local valmeta = getmetatable (value)
|
||||||
valmeta = type (valmeta) == 'table' and valmeta -- only tables
|
valmeta = type (valmeta) == 'table' and valmeta -- only tables
|
||||||
local valtojson = valmeta and valmeta.__tojson
|
local valtojson = valmeta and valmeta.__tojson
|
||||||
if valtojson then
|
if valtojson then
|
||||||
if tables[value] then
|
if tables[value] then
|
||||||
return exception('reference cycle', value, state, buffer, buflen)
|
return nil, "reference cycle"
|
||||||
end
|
end
|
||||||
tables[value] = true
|
tables[value] = true
|
||||||
state.bufferlen = buflen
|
local state = {
|
||||||
|
indent = indent, level = level, buffer = buffer,
|
||||||
|
bufferlen = buflen, tables = tables, keyorder = globalorder
|
||||||
|
}
|
||||||
local ret, msg = valtojson (value, state)
|
local ret, msg = valtojson (value, state)
|
||||||
if not ret then return exception('custom encoder failed', value, state, buffer, buflen, msg) end
|
if not ret then return nil, msg end
|
||||||
tables[value] = nil
|
tables[value] = nil
|
||||||
buflen = appendcustom(ret, buffer, state)
|
buflen = state.bufferlen
|
||||||
|
if type (ret) == 'string' then
|
||||||
|
buflen = buflen + 1
|
||||||
|
buffer[buflen] = ret
|
||||||
|
end
|
||||||
elseif value == nil then
|
elseif value == nil then
|
||||||
buflen = buflen + 1
|
buflen = buflen + 1
|
||||||
buffer[buflen] = "null"
|
buffer[buflen] = "null"
|
||||||
@ -274,7 +370,7 @@ encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, s
|
|||||||
-- This is the behaviour of the original JSON implementation.
|
-- This is the behaviour of the original JSON implementation.
|
||||||
s = "null"
|
s = "null"
|
||||||
else
|
else
|
||||||
s = num2str (value)
|
s = tostring (value)
|
||||||
end
|
end
|
||||||
buflen = buflen + 1
|
buflen = buflen + 1
|
||||||
buffer[buflen] = s
|
buffer[buflen] = s
|
||||||
@ -286,7 +382,7 @@ encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, s
|
|||||||
buffer[buflen] = quotestring (value)
|
buffer[buflen] = quotestring (value)
|
||||||
elseif valtype == 'table' then
|
elseif valtype == 'table' then
|
||||||
if tables[value] then
|
if tables[value] then
|
||||||
return exception('reference cycle', value, state, buffer, buflen)
|
return nil, "reference cycle"
|
||||||
end
|
end
|
||||||
tables[value] = true
|
tables[value] = true
|
||||||
level = level + 1
|
level = level + 1
|
||||||
@ -299,7 +395,7 @@ encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, s
|
|||||||
buflen = buflen + 1
|
buflen = buflen + 1
|
||||||
buffer[buflen] = "["
|
buffer[buflen] = "["
|
||||||
for i = 1, n do
|
for i = 1, n do
|
||||||
buflen, msg = encode2 (value[i], indent, level, buffer, buflen, tables, globalorder, state)
|
buflen, msg = encode2 (value[i], indent, level, buffer, buflen, tables, globalorder)
|
||||||
if not buflen then return nil, msg end
|
if not buflen then return nil, msg end
|
||||||
if i < n then
|
if i < n then
|
||||||
buflen = buflen + 1
|
buflen = buflen + 1
|
||||||
@ -321,20 +417,20 @@ encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, s
|
|||||||
local v = value[k]
|
local v = value[k]
|
||||||
if v then
|
if v then
|
||||||
used[k] = true
|
used[k] = true
|
||||||
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state)
|
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder)
|
||||||
prev = true -- add a seperator before the next element
|
prev = true -- add a seperator before the next element
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for k,v in pairs (value) do
|
for k,v in pairs (value) do
|
||||||
if not used[k] then
|
if not used[k] then
|
||||||
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state)
|
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder)
|
||||||
if not buflen then return nil, msg end
|
if not buflen then return nil, msg end
|
||||||
prev = true -- add a seperator before the next element
|
prev = true -- add a seperator before the next element
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else -- unordered
|
else -- unordered
|
||||||
for k,v in pairs (value) do
|
for k,v in pairs (value) do
|
||||||
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder, state)
|
buflen, msg = addpair (k, v, prev, indent, level, buffer, buflen, tables, globalorder)
|
||||||
if not buflen then return nil, msg end
|
if not buflen then return nil, msg end
|
||||||
prev = true -- add a seperator before the next element
|
prev = true -- add a seperator before the next element
|
||||||
end
|
end
|
||||||
@ -347,8 +443,7 @@ encode2 = function (value, indent, level, buffer, buflen, tables, globalorder, s
|
|||||||
end
|
end
|
||||||
tables[value] = nil
|
tables[value] = nil
|
||||||
else
|
else
|
||||||
return exception ('unsupported type', value, state, buffer, buflen,
|
return nil, "type '" .. valtype .. "' is not supported by JSON."
|
||||||
"type '" .. valtype .. "' is not supported by JSON.")
|
|
||||||
end
|
end
|
||||||
return buflen
|
return buflen
|
||||||
end
|
end
|
||||||
@ -357,18 +452,14 @@ function json.encode (value, state)
|
|||||||
state = state or {}
|
state = state or {}
|
||||||
local oldbuffer = state.buffer
|
local oldbuffer = state.buffer
|
||||||
local buffer = oldbuffer or {}
|
local buffer = oldbuffer or {}
|
||||||
state.buffer = buffer
|
|
||||||
updatedecpoint()
|
|
||||||
local ret, msg = encode2 (value, state.indent, state.level or 0,
|
local ret, msg = encode2 (value, state.indent, state.level or 0,
|
||||||
buffer, state.bufferlen or 0, state.tables or {}, state.keyorder, state)
|
buffer, state.bufferlen or 0, state.tables or {}, state.keyorder)
|
||||||
if not ret then
|
if not ret then
|
||||||
error (msg, 2)
|
error (msg, 2)
|
||||||
elseif oldbuffer == buffer then
|
elseif oldbuffer then
|
||||||
state.bufferlen = ret
|
state.bufferlen = ret
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
state.bufferlen = nil
|
|
||||||
state.buffer = nil
|
|
||||||
return concat (buffer)
|
return concat (buffer)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -396,17 +487,9 @@ local function scanwhite (str, pos)
|
|||||||
while true do
|
while true do
|
||||||
pos = strfind (str, "%S", pos)
|
pos = strfind (str, "%S", pos)
|
||||||
if not pos then return nil end
|
if not pos then return nil end
|
||||||
local sub2 = strsub (str, pos, pos + 1)
|
if strsub (str, pos, pos + 2) == "\239\187\191" then
|
||||||
if sub2 == "\239\187" and strsub (str, pos + 2, pos + 2) == "\191" then
|
|
||||||
-- UTF-8 Byte Order Mark
|
-- UTF-8 Byte Order Mark
|
||||||
pos = pos + 3
|
pos = pos + 3
|
||||||
elseif sub2 == "//" then
|
|
||||||
pos = strfind (str, "[\n\r]", pos + 2)
|
|
||||||
if not pos then return nil end
|
|
||||||
elseif sub2 == "/*" then
|
|
||||||
pos = strfind (str, "*/", pos + 2)
|
|
||||||
if not pos then return nil end
|
|
||||||
pos = pos + 2
|
|
||||||
else
|
else
|
||||||
return pos
|
return pos
|
||||||
end
|
end
|
||||||
@ -564,7 +647,7 @@ scanvalue = function (str, pos, nullval, objectmeta, arraymeta)
|
|||||||
else
|
else
|
||||||
local pstart, pend = strfind (str, "^%-?[%d%.]+[eE]?[%+%-]?%d*", pos)
|
local pstart, pend = strfind (str, "^%-?[%d%.]+[eE]?[%+%-]?%d*", pos)
|
||||||
if pstart then
|
if pstart then
|
||||||
local number = str2num (strsub (str, pstart, pend))
|
local number = tonumber (strsub (str, pstart, pend))
|
||||||
if number then
|
if number then
|
||||||
return number, pend + 1
|
return number, pend + 1
|
||||||
end
|
end
|
||||||
@ -599,13 +682,8 @@ end
|
|||||||
|
|
||||||
function json.use_lpeg ()
|
function json.use_lpeg ()
|
||||||
local g = require ("lpeg")
|
local g = require ("lpeg")
|
||||||
|
|
||||||
if g.version() == "0.11" then
|
|
||||||
error "due to a bug in LPeg 0.11, it cannot be used for JSON matching"
|
|
||||||
end
|
|
||||||
|
|
||||||
local pegmatch = g.match
|
local pegmatch = g.match
|
||||||
local P, S, R = g.P, g.S, g.R
|
local P, S, R, V = g.P, g.S, g.R, g.V
|
||||||
|
|
||||||
local function ErrorCall (str, pos, msg, state)
|
local function ErrorCall (str, pos, msg, state)
|
||||||
if not state.msg then
|
if not state.msg then
|
||||||
@ -619,9 +697,7 @@ function json.use_lpeg ()
|
|||||||
return g.Cmt (g.Cc (msg) * g.Carg (2), ErrorCall)
|
return g.Cmt (g.Cc (msg) * g.Carg (2), ErrorCall)
|
||||||
end
|
end
|
||||||
|
|
||||||
local SingleLineComment = P"//" * (1 - S"\n\r")^0
|
local Space = (S" \n\r\t" + P"\239\187\191")^0
|
||||||
local MultiLineComment = P"/*" * (1 - P"*/")^0 * P"*/"
|
|
||||||
local Space = (S" \n\r\t" + P"\239\187\191" + SingleLineComment + MultiLineComment)^0
|
|
||||||
|
|
||||||
local PlainChar = 1 - S"\"\\\n\r"
|
local PlainChar = 1 - S"\"\\\n\r"
|
||||||
local EscapeSequence = (P"\\" * g.C (S"\"\\/bfnrt" + Err "unsupported escape sequence")) / escapechars
|
local EscapeSequence = (P"\\" * g.C (S"\"\\/bfnrt" + Err "unsupported escape sequence")) / escapechars
|
||||||
@ -644,7 +720,7 @@ function json.use_lpeg ()
|
|||||||
local Integer = P"-"^(-1) * (P"0" + (R"19" * R"09"^0))
|
local Integer = P"-"^(-1) * (P"0" + (R"19" * R"09"^0))
|
||||||
local Fractal = P"." * R"09"^0
|
local Fractal = P"." * R"09"^0
|
||||||
local Exponent = (S"eE") * (S"+-")^(-1) * R"09"^1
|
local Exponent = (S"eE") * (S"+-")^(-1) * R"09"^1
|
||||||
local Number = (Integer * Fractal^(-1) * Exponent^(-1))/str2num
|
local Number = (Integer * Fractal^(-1) * Exponent^(-1))/tonumber
|
||||||
local Constant = P"true" * g.Cc (true) + P"false" * g.Cc (false) + P"null" * g.Carg (1)
|
local Constant = P"true" * g.Cc (true) + P"false" * g.Cc (false) + P"null" * g.Carg (1)
|
||||||
local SimpleValue = Number + String + Constant
|
local SimpleValue = Number + String + Constant
|
||||||
local ArrayContent, ObjectContent
|
local ArrayContent, ObjectContent
|
||||||
@ -712,3 +788,4 @@ end
|
|||||||
|
|
||||||
return json
|
return json
|
||||||
|
|
||||||
|
-->
|
211
readme.txt
211
readme.txt
@ -1,211 +0,0 @@
|
|||||||
David Kolf's JSON module for Lua 5.1/5.2
|
|
||||||
========================================
|
|
||||||
|
|
||||||
*Version 2.5*
|
|
||||||
|
|
||||||
In the default configuration this module writes no global values, not even
|
|
||||||
the module table. Import it using
|
|
||||||
|
|
||||||
json = require ("dkjson")
|
|
||||||
|
|
||||||
In environments where `require` or a similiar function are not available
|
|
||||||
and you cannot receive the return value of the module, you can set the
|
|
||||||
option `register_global_module_table` to `true`. The module table will
|
|
||||||
then be saved in the global variable with the name given by the option
|
|
||||||
`global_module_name`.
|
|
||||||
|
|
||||||
Exported functions and values:
|
|
||||||
|
|
||||||
`json.encode (object [, state])`
|
|
||||||
--------------------------------
|
|
||||||
|
|
||||||
Create a string representing the object. `Object` can be a table,
|
|
||||||
a string, a number, a boolean, `nil`, `json.null` or any object with
|
|
||||||
a function `__tojson` in its metatable. A table can only use strings
|
|
||||||
and numbers as keys and its values have to be valid objects as
|
|
||||||
well. It raises an error for any invalid data types or reference
|
|
||||||
cycles.
|
|
||||||
|
|
||||||
`state` is an optional table with the following fields:
|
|
||||||
|
|
||||||
- `indent`
|
|
||||||
When `indent` (a boolean) is set, the created string will contain
|
|
||||||
newlines and indentations. Otherwise it will be one long line.
|
|
||||||
- `keyorder`
|
|
||||||
`keyorder` is an array to specify the ordering of keys in the
|
|
||||||
encoded output. If an object has keys which are not in this array
|
|
||||||
they are written after the sorted keys.
|
|
||||||
- `level`
|
|
||||||
This is the initial level of indentation used when `indent` is
|
|
||||||
set. For each level two spaces are added. When absent it is set
|
|
||||||
to 0.
|
|
||||||
- `buffer`
|
|
||||||
`buffer` is an array to store the strings for the result so they
|
|
||||||
can be concatenated at once. When it isn't given, the encode
|
|
||||||
function will create it temporary and will return the
|
|
||||||
concatenated result.
|
|
||||||
- `bufferlen`
|
|
||||||
When `bufferlen` is set, it has to be the index of the last
|
|
||||||
element of `buffer`.
|
|
||||||
- `tables`
|
|
||||||
`tables` is a set to detect reference cycles. It is created
|
|
||||||
temporary when absent. Every table that is currently processed
|
|
||||||
is used as key, the value is `true`.
|
|
||||||
- `exception`
|
|
||||||
When `exception` is given, it will be called whenever the encoder
|
|
||||||
cannot encode a given value.
|
|
||||||
The parameters are `reason`, `value`, `state` and `defaultmessage`.
|
|
||||||
`reason` is either `"reference cycle"`, `"custom encoder failed"` or
|
|
||||||
`"unsupported type"`. `value` is the original value that caused the
|
|
||||||
exception, `state` is this state table, `defaultmessage` is the message
|
|
||||||
of the error that would usually be raised.
|
|
||||||
You can either return `true` and add directly to the buffer or you can
|
|
||||||
return the string directly. To keep raising an error return `nil` and
|
|
||||||
the desired error message.
|
|
||||||
An example implementation for an exception function is given in
|
|
||||||
`json.encodeexception`.
|
|
||||||
|
|
||||||
When `state.buffer` was set, the return value will be `true` on
|
|
||||||
success. Without `state.buffer` the return value will be a string.
|
|
||||||
|
|
||||||
`json.decode (string [, position [, null]])`
|
|
||||||
--------------------------------------------
|
|
||||||
|
|
||||||
Decode `string` starting at `position` or at 1 if `position` was
|
|
||||||
omitted.
|
|
||||||
|
|
||||||
`null` is an optional value to be returned for null values. The
|
|
||||||
default is `nil`, but you could set it to `json.null` or any other
|
|
||||||
value.
|
|
||||||
|
|
||||||
The return values are the object or `nil`, the position of the next
|
|
||||||
character that doesn't belong to the object, and in case of errors
|
|
||||||
an error message.
|
|
||||||
|
|
||||||
Two metatables are created. Every array or object that is decoded gets
|
|
||||||
a metatable with the `__jsontype` field set to either `array` or
|
|
||||||
`object`. If you want to provide your own metatables use the syntax
|
|
||||||
|
|
||||||
json.decode (string, position, null, objectmeta, arraymeta)
|
|
||||||
|
|
||||||
To prevent the assigning of metatables pass `nil`:
|
|
||||||
|
|
||||||
json.decode (string, position, null, nil)
|
|
||||||
|
|
||||||
`<metatable>.__jsonorder`
|
|
||||||
-------------------------
|
|
||||||
|
|
||||||
`__jsonorder` can overwrite the `keyorder` for a specific table.
|
|
||||||
|
|
||||||
`<metatable>.__jsontype`
|
|
||||||
------------------------
|
|
||||||
|
|
||||||
`__jsontype` can be either `"array"` or `"object"`. This value is only
|
|
||||||
checked for empty tables. (The default for empty tables is `"array"`).
|
|
||||||
|
|
||||||
`<metatable>.__tojson (self, state)`
|
|
||||||
------------------------------------
|
|
||||||
|
|
||||||
You can provide your own `__tojson` function in a metatable. In this
|
|
||||||
function you can either add directly to the buffer and return true,
|
|
||||||
or you can return a string. On errors nil and a message should be
|
|
||||||
returned.
|
|
||||||
|
|
||||||
`json.null`
|
|
||||||
-----------
|
|
||||||
|
|
||||||
You can use this value for setting explicit `null` values.
|
|
||||||
|
|
||||||
`json.version`
|
|
||||||
--------------
|
|
||||||
|
|
||||||
Set to `"dkjson 2.5"`.
|
|
||||||
|
|
||||||
`json.quotestring (string)`
|
|
||||||
---------------------------
|
|
||||||
|
|
||||||
Quote a UTF-8 string and escape critical characters using JSON
|
|
||||||
escape sequences. This function is only necessary when you build
|
|
||||||
your own `__tojson` functions.
|
|
||||||
|
|
||||||
`json.addnewline (state)`
|
|
||||||
-------------------------
|
|
||||||
|
|
||||||
When `state.indent` is set, add a newline to `state.buffer` and spaces
|
|
||||||
according to `state.level`.
|
|
||||||
|
|
||||||
`json.encodeexception (reason, value, state, defaultmessage)`
|
|
||||||
-------------------------------------------------------------
|
|
||||||
|
|
||||||
This function can be used as value to the `exception` option. Instead of
|
|
||||||
raising an error this function encodes the error message as a string. This
|
|
||||||
can help to debug malformed input data.
|
|
||||||
|
|
||||||
x = json.encode(value, { exception = json.encodeexception })
|
|
||||||
|
|
||||||
LPeg support
|
|
||||||
------------
|
|
||||||
|
|
||||||
When the local configuration variable `always_try_using_lpeg` is set,
|
|
||||||
this module tries to load LPeg to replace the `decode` function. The
|
|
||||||
speed increase is significant. You can get the LPeg module at
|
|
||||||
<http://www.inf.puc-rio.br/~roberto/lpeg/>.
|
|
||||||
When LPeg couldn't be loaded, the pure Lua functions stay active.
|
|
||||||
|
|
||||||
In case you don't want this module to require LPeg on its own,
|
|
||||||
disable the option `always_try_using_lpeg` in the options section at
|
|
||||||
the top of the module.
|
|
||||||
|
|
||||||
In this case you can later load LPeg support using
|
|
||||||
|
|
||||||
### `json.use_lpeg ()`
|
|
||||||
|
|
||||||
Require the LPeg module and replace the functions `quotestring` and
|
|
||||||
and `decode` with functions that use LPeg patterns.
|
|
||||||
This function returns the module table, so you can load the module
|
|
||||||
using:
|
|
||||||
|
|
||||||
json = require "dkjson".use_lpeg()
|
|
||||||
|
|
||||||
Alternatively you can use `pcall` so the JSON module still works when
|
|
||||||
LPeg isn't found.
|
|
||||||
|
|
||||||
json = require "dkjson"
|
|
||||||
pcall (json.use_lpeg)
|
|
||||||
|
|
||||||
### `json.using_lpeg`
|
|
||||||
|
|
||||||
This variable is set to `true` when LPeg was loaded successfully.
|
|
||||||
|
|
||||||
---------------------------------------------------------------------
|
|
||||||
|
|
||||||
Contact
|
|
||||||
-------
|
|
||||||
|
|
||||||
You can contact the author by sending an e-mail to 'david' at the
|
|
||||||
domain 'dkolf.de'.
|
|
||||||
|
|
||||||
---------------------------------------------------------------------
|
|
||||||
|
|
||||||
*Copyright (C) 2010-2014 David Heiko Kolf*
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
@ -12,7 +12,6 @@ local test_module, opt = ... -- command line argument
|
|||||||
--local test_module = 'nm-json'
|
--local test_module = 'nm-json'
|
||||||
--local test_module = 'sb-json'
|
--local test_module = 'sb-json'
|
||||||
--local test_module = 'th-json'
|
--local test_module = 'th-json'
|
||||||
test_module = test_module or 'dkjson'
|
|
||||||
|
|
||||||
--local opt = "esc" -- Test which characters in the BMP get escaped and whether this is correct
|
--local opt = "esc" -- Test which characters in the BMP get escaped and whether this is correct
|
||||||
--local opt = "esc_full" -- Full range from 0 to 0x10ffff
|
--local opt = "esc_full" -- Full range from 0 to 0x10ffff
|
||||||
@ -20,18 +19,6 @@ test_module = test_module or 'dkjson'
|
|||||||
|
|
||||||
--local opt = "refcycle" -- What happens when a reference cycle gets encoded?
|
--local opt = "refcycle" -- What happens when a reference cycle gets encoded?
|
||||||
|
|
||||||
local testlocale = "de_DE.UTF8"
|
|
||||||
|
|
||||||
local function inlocale(fn)
|
|
||||||
local oldloc = os.setlocale(nil, 'numeric')
|
|
||||||
if not os.setlocale(testlocale, 'numeric') then
|
|
||||||
print("test could not switch to locale "..testlocale)
|
|
||||||
else
|
|
||||||
fn()
|
|
||||||
end
|
|
||||||
os.setlocale(oldloc, 'numeric')
|
|
||||||
end
|
|
||||||
|
|
||||||
if test_module == 'dkjson-nopeg' then
|
if test_module == 'dkjson-nopeg' then
|
||||||
test_module = 'dkjson'
|
test_module = 'dkjson'
|
||||||
package.preload["lpeg"] = function () error "lpeg disabled" end
|
package.preload["lpeg"] = function () error "lpeg disabled" end
|
||||||
@ -39,11 +26,6 @@ if test_module == 'dkjson-nopeg' then
|
|||||||
lpeg = nil
|
lpeg = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
if test_module == 'dkjson-lulpeg' then
|
|
||||||
test_module = 'dkjson'
|
|
||||||
package.loaded["lpeg"] = require "lulpeg"
|
|
||||||
end
|
|
||||||
|
|
||||||
do
|
do
|
||||||
-- http://chiselapp.com/user/dhkolf/repository/dkjson/
|
-- http://chiselapp.com/user/dhkolf/repository/dkjson/
|
||||||
local dkjson = require "dkjson"
|
local dkjson = require "dkjson"
|
||||||
@ -52,7 +34,6 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
if test_module == 'cmj-json' then
|
if test_module == 'cmj-json' then
|
||||||
-- https://github.com/craigmj/json4lua/
|
|
||||||
-- http://json.luaforge.net/
|
-- http://json.luaforge.net/
|
||||||
local json = require "cmjjson" -- renamed, the original file was just 'json'
|
local json = require "cmjjson" -- renamed, the original file was just 'json'
|
||||||
encode = json.encode
|
encode = json.encode
|
||||||
@ -91,7 +72,6 @@ elseif test_module == 'sb-json' then
|
|||||||
encode = json.Encode
|
encode = json.Encode
|
||||||
decode = json.Decode
|
decode = json.Decode
|
||||||
elseif test_module == 'th-json' then
|
elseif test_module == 'th-json' then
|
||||||
-- https://github.com/harningt/luajson
|
|
||||||
-- http://luaforge.net/projects/luajson/
|
-- http://luaforge.net/projects/luajson/
|
||||||
local json = require "json"
|
local json = require "json"
|
||||||
encode = json.encode
|
encode = json.encode
|
||||||
@ -106,30 +86,24 @@ if not encode then
|
|||||||
else
|
else
|
||||||
local x, r
|
local x, r
|
||||||
|
|
||||||
local escapecodes = {
|
local function test (x, s)
|
||||||
["\""] = "\\\"", ["\\"] = "\\\\", ["\b"] = "\\b", ["\f"] = "\\f",
|
return string.match(x, "^%s*%[%s*%\"" .. s .. "%\"%s*%]%s*$")
|
||||||
["\n"] = "\\n", ["\r"] = "\\r", ["\t"] = "\\t", ["/"] = "\\/"
|
|
||||||
}
|
|
||||||
local function test (x, n, expect)
|
|
||||||
local enc = encode{ x }:match("^%s*%[%s*%\"(.-)%\"%s*%]%s*$")
|
|
||||||
if not enc or (escapecodes[x] ~= enc
|
|
||||||
and ("\\u%04x"):format(n) ~= enc:gsub("[A-F]", string.lower)
|
|
||||||
and not (expect and enc:match("^"..expect.."$"))) then
|
|
||||||
print(("U+%04X isn't encoded correctly: %q"):format(n, enc))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- necessary escapes for JSON:
|
x = encode{ "'" }
|
||||||
for i = 0,31 do
|
if not test(x, "%'") then
|
||||||
test(string.char(i), i)
|
print("\"'\" isn't encoded correctly:", x)
|
||||||
|
end
|
||||||
|
|
||||||
|
x = encode{ "\011" }
|
||||||
|
if not test(x, "%\\u000[bB]") then
|
||||||
|
print("\\u000b isn't encoded correctly:", x)
|
||||||
|
end
|
||||||
|
|
||||||
|
x = encode{ "\000" }
|
||||||
|
if not test(x, "%\\u0000") then
|
||||||
|
print("\\u0000 isn't encoded correctly")
|
||||||
end
|
end
|
||||||
test("\"", ("\""):byte())
|
|
||||||
test("\\", ("\\"):byte())
|
|
||||||
-- necessary escapes for JavaScript:
|
|
||||||
test("\226\128\168", 0x2028)
|
|
||||||
test("\226\128\169", 0x2029)
|
|
||||||
-- invalid escapes that were seen in the wild:
|
|
||||||
test("'", ("'"):byte(), "%'")
|
|
||||||
|
|
||||||
r,x = pcall (encode, { [1000] = "x" })
|
r,x = pcall (encode, { [1000] = "x" })
|
||||||
if not r then
|
if not r then
|
||||||
@ -201,131 +175,6 @@ else
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
inlocale(function ()
|
|
||||||
local r, x = pcall(encode, { 0.5 })
|
|
||||||
if not r then
|
|
||||||
print("encoding 0.5 in locale raises an error:", x)
|
|
||||||
elseif not x:find(".", 1, true) then
|
|
||||||
print("In locale 0.5 isn't converted into valid JSON:", x)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
-- special tests for dkjson:
|
|
||||||
if test_module == 'dkjson' then
|
|
||||||
do -- encode a function
|
|
||||||
local why, value, exstate
|
|
||||||
local state = {
|
|
||||||
exception = function (w, v, s)
|
|
||||||
why, value, exstate = w, v, s
|
|
||||||
return "\"demo\""
|
|
||||||
end
|
|
||||||
}
|
|
||||||
local encfunction = function () end
|
|
||||||
r, x = pcall(dkencode, { encfunction }, state )
|
|
||||||
if not r then
|
|
||||||
print("encoding a function with exception handler raises an error:", x)
|
|
||||||
else
|
|
||||||
if x ~= "[\"demo\"]" then
|
|
||||||
print("expected to see output of exception handler for type exception, but got", x)
|
|
||||||
end
|
|
||||||
if why ~= "unsupported type" then
|
|
||||||
print("expected exception reason to be 'unsupported type' for type exception")
|
|
||||||
end
|
|
||||||
if value ~= encfunction then
|
|
||||||
print("expected to recieve value for type exception")
|
|
||||||
end
|
|
||||||
if exstate ~= state then
|
|
||||||
print("expected to recieve state for type exception")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
r, x = pcall(dkencode, { function () end }, {
|
|
||||||
exception = function (w, v, s)
|
|
||||||
return nil, "demo"
|
|
||||||
end
|
|
||||||
})
|
|
||||||
if r or x ~= "demo" then
|
|
||||||
print("expected custom error for type exception, but got:", r, x)
|
|
||||||
end
|
|
||||||
|
|
||||||
r, x = pcall(dkencode, { function () end }, {
|
|
||||||
exception = function (w, v, s)
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
})
|
|
||||||
if r or x ~= "type 'function' is not supported by JSON." then
|
|
||||||
print("expected default error for type exception, but got:", r, x)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- encode a reference cycle
|
|
||||||
local why, value, exstate
|
|
||||||
local state = {
|
|
||||||
exception = function (w, v, s)
|
|
||||||
why, value, exstate = w, v, s
|
|
||||||
return "\"demo\""
|
|
||||||
end
|
|
||||||
}
|
|
||||||
local a = {}
|
|
||||||
a[1] = a
|
|
||||||
r, x = pcall(dkencode, a, state )
|
|
||||||
if not r then
|
|
||||||
print("encoding a reference cycle with exception handler raises an error:", x)
|
|
||||||
else
|
|
||||||
if x ~= "[\"demo\"]" then
|
|
||||||
print("expected to see output of exception handler for reference cycle exception, but got", x)
|
|
||||||
end
|
|
||||||
if why ~= "reference cycle" then
|
|
||||||
print("expected exception reason to be 'reference cycle' for reference cycle exception")
|
|
||||||
end
|
|
||||||
if value ~= a then
|
|
||||||
print("expected to recieve value for reference cycle exception")
|
|
||||||
end
|
|
||||||
if exstate ~= state then
|
|
||||||
print("expected to recieve state for reference cycle exception")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- example exception handler
|
|
||||||
r = dkencode(function () end, { exception = require "dkjson".encodeexception })
|
|
||||||
if r ~= [["<type 'function' is not supported by JSON.>"]] then
|
|
||||||
print("expected the exception encoder to encode default error message, but got", r)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- test state buffer for custom __tojson function
|
|
||||||
local origstate = {}
|
|
||||||
local usedstate, usedbuffer, usedbufferlen
|
|
||||||
dkencode({ setmetatable({}, {
|
|
||||||
__tojson = function(self, state)
|
|
||||||
usedstate = state
|
|
||||||
usedbuffer = state.buffer
|
|
||||||
usedbufferlen = state.bufferlen
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
}) }, origstate)
|
|
||||||
if usedstate ~= origstate then print("expected tojson-function to recieve the original state") end
|
|
||||||
if type(usedbuffer) ~= 'table' or #usedbuffer < 1 then print("expected buffer in tojson-function to be an array") end
|
|
||||||
if usedbufferlen ~= 1 then print("expected bufferlen in tojson-function to be 1, but got "..tostring(usedbufferlen)) end
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- do not keep buffer and bufferlen when they were not present initially
|
|
||||||
local origstate = {}
|
|
||||||
dkencode(setmetatable({}, {__tojson = function() return true end}), origstate)
|
|
||||||
if origstate.buffer ~= nil then print("expected buffer to be reset to nil") end
|
|
||||||
if origstate.bufferlen ~= nil then print("expected bufferlen to be reset to nil") end
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- keep buffer and update bufferlen when they were present initially
|
|
||||||
local origbuffer = {}
|
|
||||||
local origstate = { buffer = origbuffer }
|
|
||||||
dkencode(true, origstate)
|
|
||||||
if origstate.buffer ~= origbuffer then print("expected original buffer to remain") end
|
|
||||||
if origstate.bufferlen ~= 1 then print("expected bufferlen to be updated") end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if not decode then
|
if not decode then
|
||||||
@ -404,26 +253,13 @@ else
|
|||||||
print ("1e+2 decoded incorrectly:", r[1])
|
print ("1e+2 decoded incorrectly:", r[1])
|
||||||
end
|
end
|
||||||
|
|
||||||
inlocale(function ()
|
|
||||||
local r, x = pcall(decode, "[0.5]")
|
|
||||||
if not r then
|
|
||||||
print("decoding 0.5 in locale raises an error:", x)
|
|
||||||
elseif not x then
|
|
||||||
print("cannot decode 0.5 in locale")
|
|
||||||
elseif x[1] ~= 0.5 then
|
|
||||||
print("decoded 0.5 incorrectly in locale:", x[1])
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
-- special tests for dkjson:
|
-- special tests for dkjson:
|
||||||
if test_module == 'dkjson' then
|
if test_module == 'dkjson' then
|
||||||
x = dkdecode[=[ [{"x":0}] ]=]
|
x = dkdecode[=[ [{"x":0}] ]=]
|
||||||
local m = getmetatable(x)
|
if getmetatable(x).__jsontype ~= 'array' then
|
||||||
if not m or m.__jsontype ~= 'array' then
|
|
||||||
print ("<metatable>.__jsontype ~= array")
|
print ("<metatable>.__jsontype ~= array")
|
||||||
end
|
end
|
||||||
local m = getmetatable(x[1])
|
if getmetatable(x[1]).__jsontype ~= 'object' then
|
||||||
if not m or m.__jsontype ~= 'object' then
|
|
||||||
print ("<metatable>.__jsontype ~= object")
|
print ("<metatable>.__jsontype ~= object")
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -435,30 +271,6 @@ else
|
|||||||
if p ~= 4 or type(m) ~= 'string' or not m:find("at line 2, column 2$") then
|
if p ~= 4 or type(m) ~= 'string' or not m:find("at line 2, column 2$") then
|
||||||
print (("Invalid location: position=%d, message=%q"):format(p,m))
|
print (("Invalid location: position=%d, message=%q"):format(p,m))
|
||||||
end
|
end
|
||||||
|
|
||||||
do -- single line comments
|
|
||||||
local x, p, m = dkdecode [[
|
|
||||||
{"test://" // comment // --?
|
|
||||||
: [ // continues
|
|
||||||
0] //
|
|
||||||
}
|
|
||||||
]]
|
|
||||||
if type(x) ~= 'table' or type(x["test://"]) ~= 'table' or x["test://"][1] ~= 0 then
|
|
||||||
print("could not decode a string with single line comments: "..tostring(m))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
do -- multi line comments
|
|
||||||
local x, p, m = dkdecode [[
|
|
||||||
{"test:/*"/**//*
|
|
||||||
hi! this is a comment
|
|
||||||
*/ : [/** / **/ 0]
|
|
||||||
}
|
|
||||||
]]
|
|
||||||
if type(x) ~= 'table' or type(x["test:/*"]) ~= 'table' or x["test:/*"][1] ~= 0 then
|
|
||||||
print("could not decode a string with multi line comments: "..tostring(m))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -527,29 +339,6 @@ local function escapeutf8 (uchar)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local isspecial = {}
|
|
||||||
local unifile = io.open("UnicodeData.txt")
|
|
||||||
if unifile then
|
|
||||||
-- <http://www.unicode.org/Public/UNIDATA/UnicodeData.txt>
|
|
||||||
-- each line consists of 15 parts for each defined codepoints
|
|
||||||
local pat = {}
|
|
||||||
for i = 1,14 do
|
|
||||||
pat[i] = "[^;]*;"
|
|
||||||
end
|
|
||||||
pat[1] = "([^;]*);" -- Codepoint
|
|
||||||
pat[3] = "([^;]*);" -- Category
|
|
||||||
pat[15] = "[^;]*"
|
|
||||||
pat = table.concat(pat)
|
|
||||||
|
|
||||||
for line in unifile:lines() do
|
|
||||||
local cp, cat = line:match(pat)
|
|
||||||
if cat:match("^C[^so]") or cat:match("^Z[lp]") then
|
|
||||||
isspecial[tonumber(cp, 16)] = cat
|
|
||||||
end
|
|
||||||
end
|
|
||||||
unifile:close()
|
|
||||||
end
|
|
||||||
|
|
||||||
local x,xe
|
local x,xe
|
||||||
|
|
||||||
local t = {}
|
local t = {}
|
||||||
@ -571,6 +360,7 @@ end
|
|||||||
elseif x == escapecodes[t[1]] then
|
elseif x == escapecodes[t[1]] then
|
||||||
esc[i] = 'c'
|
esc[i] = 'c'
|
||||||
elseif x:sub(1,1) == "\\" then
|
elseif x:sub(1,1) == "\\" then
|
||||||
|
--print ("Invalid escape code for "..i..":", x)
|
||||||
escerr[i] = xe
|
escerr[i] = xe
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -578,34 +368,17 @@ end
|
|||||||
local i = 0
|
local i = 0
|
||||||
while i <= range do
|
while i <= range do
|
||||||
local first
|
local first
|
||||||
while i <= range and not (esc[i] or isspecial[i]) do i = i + 1 end
|
while i <= range and not esc[i] do i = i + 1 end
|
||||||
if i > range then break end
|
if not esc[i] then break end
|
||||||
first = i
|
first = i
|
||||||
local special = isspecial[i]
|
while esc[i] do i = i + 1 end
|
||||||
if esc[i] and special then
|
|
||||||
while esc[i] and isspecial[i] == special do i = i + 1 end
|
|
||||||
if i-1 > first then
|
if i-1 > first then
|
||||||
print (("Escaped %s characters from U+%04X to U+%04X"):format(special,first,i-1))
|
print ("Escaped from "..first.." to "..i-1)
|
||||||
else
|
|
||||||
print (("Escaped %s character U+%04X"):format(special,first))
|
|
||||||
end
|
|
||||||
elseif esc[i] then
|
|
||||||
while esc[i] and not isspecial[i] do i = i + 1 end
|
|
||||||
if i-1 > first then
|
|
||||||
print (("Escaped from U+%04X to U+%04X"):format(first,i-1))
|
|
||||||
else
|
else
|
||||||
if first >= 32 and first <= 127 then
|
if first >= 32 and first <= 127 then
|
||||||
print (("Escaped U+%04X (%c)"):format(first,first))
|
print ("Escaped "..first.." ("..string.char(first)..")")
|
||||||
else
|
else
|
||||||
print (("Escaped U+%04X"):format(first))
|
print ("Escaped "..first)
|
||||||
end
|
|
||||||
end
|
|
||||||
elseif special then
|
|
||||||
while not esc[i] and isspecial[i] == special do i = i + 1 end
|
|
||||||
if i-1 > first then
|
|
||||||
print (("Unescaped %s characters from U+%04X to U+%04X"):format(special,first,i-1))
|
|
||||||
else
|
|
||||||
print (("Unescaped %s character U+%04X"):format(special,first))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -619,9 +392,9 @@ end
|
|||||||
first = i
|
first = i
|
||||||
while escerr[i] do i = i + 1 end
|
while escerr[i] do i = i + 1 end
|
||||||
if i-1 > first then
|
if i-1 > first then
|
||||||
print (("Errors while escaping from U+%04X to U+%04X"):format(first, i-1))
|
print ("Errors while escaping from "..first.." to "..i-1)
|
||||||
else
|
else
|
||||||
print (("Errors while escaping U+%04X"):format(first))
|
print ("Errors while escaping "..first)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -1,49 +1,3 @@
|
|||||||
Version 2.5 (2014-04-28)
|
|
||||||
===========
|
|
||||||
|
|
||||||
Changes since version 2.4:
|
|
||||||
|
|
||||||
* Added customizable exception handling.
|
|
||||||
* Decode input that contains JavaScript comments.
|
|
||||||
|
|
||||||
Version 2.4 (2013-09-28)
|
|
||||||
===========
|
|
||||||
|
|
||||||
Changes since version 2.3:
|
|
||||||
|
|
||||||
* Fixed encoding and decoding of numbers in different numeric locales.
|
|
||||||
* Prevent using version 0.11 of LPeg (causes segmentation faults on
|
|
||||||
some systems).
|
|
||||||
|
|
||||||
Version 1.3 (2013-09-28)
|
|
||||||
===========
|
|
||||||
|
|
||||||
Changes since version 1.2:
|
|
||||||
|
|
||||||
* Fixed encoding and decoding of numbers in different numeric locales.
|
|
||||||
|
|
||||||
Version 2.3 (2013-04-14)
|
|
||||||
===========
|
|
||||||
|
|
||||||
Changes since version 2.2:
|
|
||||||
|
|
||||||
* Corrected the range of escaped characters. Among other characters
|
|
||||||
U+2029 was missing, which would cause trouble when parsed by a
|
|
||||||
JavaScript interpreter.
|
|
||||||
* Added options to register the module table in a global variable.
|
|
||||||
This is useful in environments where functions similar to require are
|
|
||||||
not available.
|
|
||||||
|
|
||||||
Version 1.2 (2013-04-14)
|
|
||||||
===========
|
|
||||||
|
|
||||||
Changes since version 1.1:
|
|
||||||
|
|
||||||
* Corrected the range of escaped characters. Among other characters
|
|
||||||
U+2029 was missing, which would cause trouble when parsed by a
|
|
||||||
JavaScript interpreter.
|
|
||||||
* Locations for error messages were off by one in the first line.
|
|
||||||
|
|
||||||
Version 2.2 (2012-04-28)
|
Version 2.2 (2012-04-28)
|
||||||
===========
|
===========
|
||||||
|
|
Reference in New Issue
Block a user