From 730bad97281a64012b88f5eab45cb61945fa7d41 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 15 May 2012 14:58:38 -0700 Subject: [PATCH 1/5] Adding support for building with clang/libcxx. --- CMakeLists.txt | 67 +++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d3e07d..17ea7b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,13 +24,11 @@ set(CPACK_PACKAGE_VENDOR "ChaiScript.com") set(CPACK_PACKAGE_CONTACT "contact@chaiscript.com") set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "An embedded scripting language for C++") -set(CPACK_DEBIAN_PACKAGE_DEPENDS "libboost-dev (>=1.36.0)") set(CPACK_DEBIAN_PACKAGE_SECTION "devel") set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional") set(CPACK_RPM_PACKAGE_LICENSE "BSD") set(CPACK_RPM_PACKAGE_GROUP "Programming") -set(CPACK_RPM_PACKAGE_REQUIRES "boost-devel >= 1.36.0, boost-thread >= 1.36.0") set(CHAI_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}) @@ -61,31 +59,29 @@ if(MSVC) add_definitions(/bigobj) endif() else() - add_definitions(-Wall -Wextra -Wshadow -pedantic) + add_definitions(-Wall -Wextra -Wshadow -pedantic -std=c++0x) if (APPLE) - # -Wno-missing-field-initializers is for boost on macos - add_definitions(-Wno-missing-field-initializers -Wno-sign-compare) + add_definitions(-Wno-sign-compare) endif() endif() +if (CMAKE_CXX_COMPILER MATCHES ".*clang") + message(STATUS "Using clang's libcxx") + add_definitions(-stdlib=libc++) + set (EXTRA_LINKER_FLAGS -std=c++0x -stdlib=libc++) +else() + set (EXTRA_LINKER_FLAGS ) +endif() + include_directories(include) -set(Boost_ADDITIONAL_VERSIONS "1.44" "1.44.0" "1.43" "1.43.0" "1.42" "1.42.0" "1.41") -set(Boost_USE_MULTITHREADED ON) set (Chai_INCLUDES include/chaiscript/chaiscript.hpp include/chaiscript/chaiscript_threading.hpp include/chaiscript/dispatchkit/bad_boxed_cast.hpp include/chaiscript/dispatchkit/bind_first.hpp include/chaiscript/dispatchkit/bootstrap.hpp include/chaiscript/dispatchkit/bootstrap_stl.hpp include/chaiscript/dispatchkit/boxed_cast.hpp include/chaiscript/dispatchkit/boxed_cast_helper.hpp include/chaiscript/dispatchkit/boxed_number.hpp include/chaiscript/dispatchkit/boxed_value.hpp include/chaiscript/dispatchkit/dispatchkit.hpp include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp include/chaiscript/dispatchkit/dynamic_object.hpp include/chaiscript/dispatchkit/exception_specification.hpp include/chaiscript/dispatchkit/function_call.hpp include/chaiscript/dispatchkit/function_call_detail.hpp include/chaiscript/dispatchkit/handle_return.hpp include/chaiscript/dispatchkit/operators.hpp include/chaiscript/dispatchkit/proxy_constructors.hpp include/chaiscript/dispatchkit/proxy_functions.hpp include/chaiscript/dispatchkit/proxy_functions_detail.hpp include/chaiscript/dispatchkit/register_function.hpp include/chaiscript/dispatchkit/type_info.hpp include/chaiscript/language/chaiscript_algebraic.hpp include/chaiscript/language/chaiscript_common.hpp include/chaiscript/language/chaiscript_engine.hpp include/chaiscript/language/chaiscript_eval.hpp include/chaiscript/language/chaiscript_parser.hpp include/chaiscript/language/chaiscript_prelude.hpp include/chaiscript/language/chaiscript_prelude_docs.hpp include/chaiscript/utility/utility.hpp) set_source_files_properties(${Chai_INCLUDES} PROPERTIES HEADER_FILE_ONLY TRUE) if (MULTITHREAD_SUPPORT_ENABLED) - find_package(Boost 1.36.0 COMPONENTS thread) - - if (Boost_FOUND) - link_directories( ${Boost_LIBRARY_DIRS} ) - else() - message(FATAL_ERROR "Can not find Boost") - endif(Boost_FOUND) else() add_definitions(-DCHAISCRIPT_NO_THREADS) endif() @@ -94,37 +90,30 @@ if (CMAKE_HOST_UNIX) set(DYNAMIC_LOADER "dl") endif(CMAKE_HOST_UNIX) -if (MSVC) - # Boost on MSVC does automatic linking - set(LIBS ${DYNAMIC_LOADER} ${READLINE_LIB}) -else() - set(LIBS ${DYNAMIC_LOADER} ${Boost_LIBRARIES} ${READLINE_LIB}) -endif() +set(LIBS ${DYNAMIC_LOADER} ${READLINE_LIB}) if (CMAKE_COMPILER_2005) # vs2005 is a bit too loud about possible loss of data warnings # ADD_DEFINITIONS(/wd4244) endif() -include_directories(${Boost_INCLUDE_DIRS}) -include_directories(${Boost_INCLUDE_DIR}) add_executable(chai src/main.cpp ${Chai_INCLUDES}) -target_link_libraries(chai ${LIBS}) +target_link_libraries(chai ${LIBS} ${EXTRA_LINKER_FLAGS}) if (BUILD_SAMPLES) add_executable(example samples/example.cpp) - target_link_libraries(example ${LIBS}) + target_link_libraries(example ${LIBS} ${EXTRA_LINKER_FLAGS}) add_executable(memory_leak_test samples/memory_leak_test.cpp) - target_link_libraries(memory_leak_test ${LIBS}) + target_link_libraries(memory_leak_test ${LIBS} ${EXTRA_LINKER_FLAGS}) endif() if (BUILD_MODULES) add_library(stl_extra MODULE src/stl_extra.cpp) - target_link_libraries(stl_extra ${LIBS}) + target_link_libraries(stl_extra ${LIBS} ${EXTRA_LINKER_FLAGS}) add_library(reflection MODULE src/reflection.cpp) - target_link_libraries(reflection ${LIBS}) + target_link_libraries(reflection ${LIBS} ${EXTRA_LINKER_FLAGS}) set(MODULES stl_extra reflection) endif() @@ -148,53 +137,53 @@ if(BUILD_TESTING) if (NOT UNIT_TEST_LIGHT) add_executable(utility_test unittests/utility_test.cpp) - target_link_libraries(utility_test ${LIBS}) + target_link_libraries(utility_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME Utility_Test COMMAND utility_test) add_executable(dynamic_object_test unittests/dynamic_object_test.cpp) - target_link_libraries(dynamic_object_test ${LIBS}) + target_link_libraries(dynamic_object_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME Dynamic_Object_Test COMMAND dynamic_object_test) add_executable(functor_creation_test unittests/functor_creation_test.cpp) - target_link_libraries(functor_creation_test ${LIBS}) + target_link_libraries(functor_creation_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME Functor_Creation_Test COMMAND functor_creation_test) add_executable(functor_cast_test unittests/functor_cast_test.cpp) - target_link_libraries(functor_cast_test ${LIBS}) + target_link_libraries(functor_cast_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME Functor_Cast_Test COMMAND functor_cast_test) add_executable(boxed_cast_test unittests/boxed_cast_test.cpp) - target_link_libraries(boxed_cast_test ${LIBS}) + target_link_libraries(boxed_cast_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME Boxed_Cast_Test COMMAND boxed_cast_test) add_executable(object_lifetime_test unittests/object_lifetime_test.cpp) - target_link_libraries(object_lifetime_test ${LIBS}) + target_link_libraries(object_lifetime_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME Object_Lifetime_Test COMMAND object_lifetime_test) add_executable(function_ordering_test unittests/function_ordering_test.cpp) - target_link_libraries(function_ordering_test ${LIBS}) + target_link_libraries(function_ordering_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME Function_Ordering_Test COMMAND function_ordering_test) add_executable(type_info_test unittests/type_info_test.cpp) - target_link_libraries(type_info_test ${LIBS}) + target_link_libraries(type_info_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME Type_Info_Test COMMAND type_info_test) add_executable(eval_catch_exception_test unittests/eval_catch_exception_test.cpp) - target_link_libraries(eval_catch_exception_test ${LIBS}) + target_link_libraries(eval_catch_exception_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME Eval_Catch_Exception_Test COMMAND eval_catch_exception_test) add_executable(short_comparison_test unittests/short_comparison_test.cpp) - target_link_libraries(short_comparison_test ${LIBS}) + target_link_libraries(short_comparison_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME short_comparison_test COMMAND short_comparison_test) add_executable(multifile_test unittests/multifile_test_main.cpp unittests/multifile_test_chai.cpp unittests/multifile_test_module.cpp) - target_link_libraries(multifile_test ${LIBS}) + target_link_libraries(multifile_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME MultiFile_Test COMMAND multifile_test) add_library(test_module MODULE src/test_module.cpp) - target_link_libraries(test_module ${LIBS}) + target_link_libraries(test_module ${LIBS} ${EXTRA_LINKER_FLAGS}) install(TARGETS test_module RUNTIME DESTINATION bin LIBRARY DESTINATION lib/chaiscript) endif() From 7deb2311f55479bec8971ea398737aac89d1aa01 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 15 May 2012 19:48:46 -0600 Subject: [PATCH 2/5] Restore CMakeLists.txt on head to working version Reverted to revision: 277b4eec9aee6962f30a283caf9f145285e19951 --- CMakeLists.txt | 67 +++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 17ea7b3..4d3e07d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,11 +24,13 @@ set(CPACK_PACKAGE_VENDOR "ChaiScript.com") set(CPACK_PACKAGE_CONTACT "contact@chaiscript.com") set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "An embedded scripting language for C++") +set(CPACK_DEBIAN_PACKAGE_DEPENDS "libboost-dev (>=1.36.0)") set(CPACK_DEBIAN_PACKAGE_SECTION "devel") set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional") set(CPACK_RPM_PACKAGE_LICENSE "BSD") set(CPACK_RPM_PACKAGE_GROUP "Programming") +set(CPACK_RPM_PACKAGE_REQUIRES "boost-devel >= 1.36.0, boost-thread >= 1.36.0") set(CHAI_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}) @@ -59,29 +61,31 @@ if(MSVC) add_definitions(/bigobj) endif() else() - add_definitions(-Wall -Wextra -Wshadow -pedantic -std=c++0x) + add_definitions(-Wall -Wextra -Wshadow -pedantic) if (APPLE) - add_definitions(-Wno-sign-compare) + # -Wno-missing-field-initializers is for boost on macos + add_definitions(-Wno-missing-field-initializers -Wno-sign-compare) endif() endif() -if (CMAKE_CXX_COMPILER MATCHES ".*clang") - message(STATUS "Using clang's libcxx") - add_definitions(-stdlib=libc++) - set (EXTRA_LINKER_FLAGS -std=c++0x -stdlib=libc++) -else() - set (EXTRA_LINKER_FLAGS ) -endif() - include_directories(include) +set(Boost_ADDITIONAL_VERSIONS "1.44" "1.44.0" "1.43" "1.43.0" "1.42" "1.42.0" "1.41") +set(Boost_USE_MULTITHREADED ON) set (Chai_INCLUDES include/chaiscript/chaiscript.hpp include/chaiscript/chaiscript_threading.hpp include/chaiscript/dispatchkit/bad_boxed_cast.hpp include/chaiscript/dispatchkit/bind_first.hpp include/chaiscript/dispatchkit/bootstrap.hpp include/chaiscript/dispatchkit/bootstrap_stl.hpp include/chaiscript/dispatchkit/boxed_cast.hpp include/chaiscript/dispatchkit/boxed_cast_helper.hpp include/chaiscript/dispatchkit/boxed_number.hpp include/chaiscript/dispatchkit/boxed_value.hpp include/chaiscript/dispatchkit/dispatchkit.hpp include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp include/chaiscript/dispatchkit/dynamic_object.hpp include/chaiscript/dispatchkit/exception_specification.hpp include/chaiscript/dispatchkit/function_call.hpp include/chaiscript/dispatchkit/function_call_detail.hpp include/chaiscript/dispatchkit/handle_return.hpp include/chaiscript/dispatchkit/operators.hpp include/chaiscript/dispatchkit/proxy_constructors.hpp include/chaiscript/dispatchkit/proxy_functions.hpp include/chaiscript/dispatchkit/proxy_functions_detail.hpp include/chaiscript/dispatchkit/register_function.hpp include/chaiscript/dispatchkit/type_info.hpp include/chaiscript/language/chaiscript_algebraic.hpp include/chaiscript/language/chaiscript_common.hpp include/chaiscript/language/chaiscript_engine.hpp include/chaiscript/language/chaiscript_eval.hpp include/chaiscript/language/chaiscript_parser.hpp include/chaiscript/language/chaiscript_prelude.hpp include/chaiscript/language/chaiscript_prelude_docs.hpp include/chaiscript/utility/utility.hpp) set_source_files_properties(${Chai_INCLUDES} PROPERTIES HEADER_FILE_ONLY TRUE) if (MULTITHREAD_SUPPORT_ENABLED) + find_package(Boost 1.36.0 COMPONENTS thread) + + if (Boost_FOUND) + link_directories( ${Boost_LIBRARY_DIRS} ) + else() + message(FATAL_ERROR "Can not find Boost") + endif(Boost_FOUND) else() add_definitions(-DCHAISCRIPT_NO_THREADS) endif() @@ -90,30 +94,37 @@ if (CMAKE_HOST_UNIX) set(DYNAMIC_LOADER "dl") endif(CMAKE_HOST_UNIX) -set(LIBS ${DYNAMIC_LOADER} ${READLINE_LIB}) +if (MSVC) + # Boost on MSVC does automatic linking + set(LIBS ${DYNAMIC_LOADER} ${READLINE_LIB}) +else() + set(LIBS ${DYNAMIC_LOADER} ${Boost_LIBRARIES} ${READLINE_LIB}) +endif() if (CMAKE_COMPILER_2005) # vs2005 is a bit too loud about possible loss of data warnings # ADD_DEFINITIONS(/wd4244) endif() +include_directories(${Boost_INCLUDE_DIRS}) +include_directories(${Boost_INCLUDE_DIR}) add_executable(chai src/main.cpp ${Chai_INCLUDES}) -target_link_libraries(chai ${LIBS} ${EXTRA_LINKER_FLAGS}) +target_link_libraries(chai ${LIBS}) if (BUILD_SAMPLES) add_executable(example samples/example.cpp) - target_link_libraries(example ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(example ${LIBS}) add_executable(memory_leak_test samples/memory_leak_test.cpp) - target_link_libraries(memory_leak_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(memory_leak_test ${LIBS}) endif() if (BUILD_MODULES) add_library(stl_extra MODULE src/stl_extra.cpp) - target_link_libraries(stl_extra ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(stl_extra ${LIBS}) add_library(reflection MODULE src/reflection.cpp) - target_link_libraries(reflection ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(reflection ${LIBS}) set(MODULES stl_extra reflection) endif() @@ -137,53 +148,53 @@ if(BUILD_TESTING) if (NOT UNIT_TEST_LIGHT) add_executable(utility_test unittests/utility_test.cpp) - target_link_libraries(utility_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(utility_test ${LIBS}) add_test(NAME Utility_Test COMMAND utility_test) add_executable(dynamic_object_test unittests/dynamic_object_test.cpp) - target_link_libraries(dynamic_object_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(dynamic_object_test ${LIBS}) add_test(NAME Dynamic_Object_Test COMMAND dynamic_object_test) add_executable(functor_creation_test unittests/functor_creation_test.cpp) - target_link_libraries(functor_creation_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(functor_creation_test ${LIBS}) add_test(NAME Functor_Creation_Test COMMAND functor_creation_test) add_executable(functor_cast_test unittests/functor_cast_test.cpp) - target_link_libraries(functor_cast_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(functor_cast_test ${LIBS}) add_test(NAME Functor_Cast_Test COMMAND functor_cast_test) add_executable(boxed_cast_test unittests/boxed_cast_test.cpp) - target_link_libraries(boxed_cast_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(boxed_cast_test ${LIBS}) add_test(NAME Boxed_Cast_Test COMMAND boxed_cast_test) add_executable(object_lifetime_test unittests/object_lifetime_test.cpp) - target_link_libraries(object_lifetime_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(object_lifetime_test ${LIBS}) add_test(NAME Object_Lifetime_Test COMMAND object_lifetime_test) add_executable(function_ordering_test unittests/function_ordering_test.cpp) - target_link_libraries(function_ordering_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(function_ordering_test ${LIBS}) add_test(NAME Function_Ordering_Test COMMAND function_ordering_test) add_executable(type_info_test unittests/type_info_test.cpp) - target_link_libraries(type_info_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(type_info_test ${LIBS}) add_test(NAME Type_Info_Test COMMAND type_info_test) add_executable(eval_catch_exception_test unittests/eval_catch_exception_test.cpp) - target_link_libraries(eval_catch_exception_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(eval_catch_exception_test ${LIBS}) add_test(NAME Eval_Catch_Exception_Test COMMAND eval_catch_exception_test) add_executable(short_comparison_test unittests/short_comparison_test.cpp) - target_link_libraries(short_comparison_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(short_comparison_test ${LIBS}) add_test(NAME short_comparison_test COMMAND short_comparison_test) add_executable(multifile_test unittests/multifile_test_main.cpp unittests/multifile_test_chai.cpp unittests/multifile_test_module.cpp) - target_link_libraries(multifile_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(multifile_test ${LIBS}) add_test(NAME MultiFile_Test COMMAND multifile_test) add_library(test_module MODULE src/test_module.cpp) - target_link_libraries(test_module ${LIBS} ${EXTRA_LINKER_FLAGS}) + target_link_libraries(test_module ${LIBS}) install(TARGETS test_module RUNTIME DESTINATION bin LIBRARY DESTINATION lib/chaiscript) endif() From 4bcaa75fa41786ba2c9cb6dd403e195720495f9a Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 16 May 2012 11:40:43 -0600 Subject: [PATCH 3/5] Throw eval exception if a variable or function is redefined #28 --- include/chaiscript/dispatchkit/bootstrap.hpp | 3 - .../chaiscript/dispatchkit/dispatchkit.hpp | 70 ++++++++++++++----- .../dispatchkit/proxy_functions.hpp | 7 +- .../chaiscript/language/chaiscript_eval.hpp | 12 +++- unittests/function_ordering_test.cpp | 2 +- unittests/function_redefinition.chai | 2 + unittests/object_lifetime_test.cpp | 10 +-- unittests/utility_test.cpp | 4 +- unittests/variable_redefinition.chai | 2 + 9 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 unittests/function_redefinition.chai create mode 100644 unittests/variable_redefinition.chai diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index d61f338..31c2c23 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -422,9 +422,6 @@ namespace chaiscript m->add(fun(&Type_Info::name), "cpp_name"); m->add(fun(&Type_Info::bare_name), "cpp_bare_name"); m->add(fun(&Type_Info::bare_equal), "bare_equal"); - typedef bool (Type_Info::*typeinfocompare)(const Type_Info &) const; - m->add(fun(typeinfocompare(&Type_Info::operator==)), "=="); - m->add(fun(&Type_Info::bare_equal), "bare_equal"); basic_constructors("bool", m); diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index b96d53a..c7b5147 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -35,10 +35,7 @@ namespace chaiscript namespace exception { /** - * Exception thrown in the case that a multi method dispatch fails - * because no matching function was found - * at runtime due to either an arity_error, a guard_error or a bad_boxed_cast - * exception + * Exception thrown in the case that an object name is invalid because it is a reserved word */ class reserved_word_error : public std::runtime_error { @@ -60,6 +57,30 @@ namespace chaiscript }; + /** + * Exception thrown in the case that an object name is invalid because it already exists in current context + */ + class name_conflict_error : public std::runtime_error + { + public: + name_conflict_error(const std::string &t_name) throw() + : std::runtime_error("Name already exists in current context " + t_name), m_name(t_name) + { + } + + virtual ~name_conflict_error() throw() {} + + std::string name() const + { + return m_name; + } + + private: + std::string m_name; + + }; + + /** * Exception thrown in the case that a non-const object was added as a shared object */ @@ -146,7 +167,12 @@ namespace chaiscript { while (begin != end) { - t.add(begin->first, begin->second); + try { + t.add(begin->first, begin->second); + } catch (const exception::name_conflict_error &) { + /// \todo Should we throw an error if there's a name conflict + /// while applying a module? + } ++begin; } } @@ -378,16 +404,17 @@ namespace chaiscript /** * Add a new named Proxy_Function to the system */ - bool add(const Proxy_Function &f, const std::string &name) + void add(const Proxy_Function &f, const std::string &name) { validate_object_name(name); - return add_function(f, name); + add_function(f, name); } /** * Set the value of an object, by name. If the object * is not available in the current scope it is created */ + /* void add(const Boxed_Value &obj, const std::string &name) { validate_object_name(name); @@ -405,6 +432,7 @@ namespace chaiscript add_object(name, obj); } + */ /** * Adds a named object to the current scope @@ -413,7 +441,15 @@ namespace chaiscript { StackData &stack = get_stack_data(); validate_object_name(name); - stack.back()[name] = obj; + + Scope &scope = stack.back(); + Scope::iterator itr = scope.find(name); + if (itr != stack.back().end()) + { + throw exception::name_conflict_error(name); + } else { + stack.back().insert(std::make_pair(name, obj)); + } } /** @@ -429,7 +465,12 @@ namespace chaiscript chaiscript::detail::threading::unique_lock l(m_global_object_mutex); - m_state.m_global_objects[name] = obj; + if (m_state.m_global_objects.find(name) != m_state.m_global_objects.end()) + { + throw exception::name_conflict_error(name); + } else { + m_state.m_global_objects.insert(std::make_pair(name, obj)); + } } /** @@ -936,11 +977,10 @@ namespace chaiscript } /** - * Implementation detail for adding a function. Returns - * true if the function was added, false if a function with the - * same signature and name already exists. + * Implementation detail for adding a function. + * \throws exception::name_conflict_error if there's a function matching the given one being added */ - bool add_function(const Proxy_Function &t_f, const std::string &t_name) + void add_function(const Proxy_Function &t_f, const std::string &t_name) { chaiscript::detail::threading::unique_lock l(m_mutex); @@ -958,7 +998,7 @@ namespace chaiscript { if ((*t_f) == *(*itr2)) { - return false; + throw exception::name_conflict_error(t_name); } } @@ -969,8 +1009,6 @@ namespace chaiscript vec.push_back(t_f); funcs.insert(std::make_pair(t_name, vec)); } - - return true; } mutable chaiscript::detail::threading::shared_mutex m_mutex; diff --git a/include/chaiscript/dispatchkit/proxy_functions.hpp b/include/chaiscript/dispatchkit/proxy_functions.hpp index b6bb7e0..75e591b 100644 --- a/include/chaiscript/dispatchkit/proxy_functions.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions.hpp @@ -219,7 +219,12 @@ namespace chaiscript virtual bool operator==(const Proxy_Function_Base &rhs) const { - return this == &rhs; + const Dynamic_Proxy_Function *prhs = dynamic_cast(&rhs); + + return this == &rhs + || (prhs + && this->m_arity == prhs->m_arity + && !this->m_guard && !prhs->m_guard); } virtual bool call_match(const std::vector &vals) const diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index aad307e..4bf708f 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -340,6 +340,8 @@ namespace chaiscript } catch (const exception::reserved_word_error &) { throw exception::eval_error("Reserved word used as variable '" + this->children[0]->text + "'"); + } catch (const exception::name_conflict_error &e) { + throw exception::eval_error("Variable redefined '" + e.name() + "'"); } return t_ss.get_object(this->children[0]->text); } @@ -639,6 +641,8 @@ namespace chaiscript } catch (const exception::reserved_word_error &e) { throw exception::eval_error("Reserved word used as function name '" + e.word() + "'"); + } catch (const exception::name_conflict_error &e) { + throw exception::eval_error("Function redefined '" + e.name() + "'"); } return Boxed_Value(); } @@ -950,6 +954,7 @@ namespace chaiscript end_point = this->children.size() - 1; } for (unsigned int i = 1; i < end_point; ++i) { + chaiscript::eval::detail::Scope_Push_Pop catchscope(t_ss); AST_NodePtr catch_block = this->children[i]; if (catch_block->children.size() == 1) { @@ -992,6 +997,7 @@ namespace chaiscript } catch (Boxed_Value &except) { for (size_t i = 1; i < this->children.size(); ++i) { + chaiscript::eval::detail::Scope_Push_Pop catchscope(t_ss); AST_NodePtr catch_block = this->children[i]; if (catch_block->children.size() == 1) { @@ -1006,7 +1012,7 @@ namespace chaiscript break; } else if (catch_block->children.size() == 3) { - //Variable capture, no guards + //Variable capture, guards t_ss.add_object(catch_block->children[0]->text, except); bool guard; @@ -1134,6 +1140,8 @@ namespace chaiscript } catch (const exception::reserved_word_error &e) { throw exception::eval_error("Reserved word used as method name '" + e.word() + "'"); + } catch (const exception::name_conflict_error &e) { + throw exception::eval_error("Method redefined '" + e.name() + "'"); } return Boxed_Value(); } @@ -1161,6 +1169,8 @@ namespace chaiscript } catch (const exception::reserved_word_error &) { throw exception::eval_error("Reserved word used as attribute '" + this->children[1]->text + "'"); + } catch (const exception::name_conflict_error &e) { + throw exception::eval_error("Attribute redefined '" + e.name() + "'"); } return Boxed_Value(); } diff --git a/unittests/function_ordering_test.cpp b/unittests/function_ordering_test.cpp index 0a9652c..b4209a9 100644 --- a/unittests/function_ordering_test.cpp +++ b/unittests/function_ordering_test.cpp @@ -17,7 +17,7 @@ int main() chaiscript::ChaiScript chai; chai.eval("def test_fun(x) { return 3; }"); chai.eval("def test_fun(x) : x == \"hi\" { return 4; }"); - chai.eval("def test_fun(x) { return 5; }"); +// chai.eval("def test_fun(x) { return 5; }"); chai.add(chaiscript::fun(&test_one), "test_fun"); chai.add(chaiscript::fun(&test_two), "test_fun"); diff --git a/unittests/function_redefinition.chai b/unittests/function_redefinition.chai new file mode 100644 index 0000000..caabd3d --- /dev/null +++ b/unittests/function_redefinition.chai @@ -0,0 +1,2 @@ +assert_throws("Function already defined", fun() { def foo(x) { x + 1 }; def foo(x) { x + 1 } } ); + diff --git a/unittests/object_lifetime_test.cpp b/unittests/object_lifetime_test.cpp index d59957b..a58e3d2 100644 --- a/unittests/object_lifetime_test.cpp +++ b/unittests/object_lifetime_test.cpp @@ -38,19 +38,19 @@ int main() chaiscript::ChaiScript chai; chai.add(m); - chai.add(chaiscript::fun(&Test::count), "count"); +// chai.add(chaiscript::fun(&Test::count), "count"); int count = chai.eval("count()"); int count2 = chai.eval("var i = 0; { var t = Test(); } return i;"); - int count3 = chai.eval("var i = 0; { var t = Test(); i = count(); } return i;"); + int count3 = chai.eval("i = 0; { var t = Test(); i = count(); } return i;"); - int count4 = chai.eval("var i = 0; { var t = Test(); { var t2 = Test(); i = count(); } } return i;"); + int count4 = chai.eval("i = 0; { var t = Test(); { var t2 = Test(); i = count(); } } return i;"); - int count5 = chai.eval("var i = 0; { var t = Test(); { var t2 = Test(); } i = count(); } return i;"); + int count5 = chai.eval("i = 0; { var t = Test(); { var t2 = Test(); } i = count(); } return i;"); - int count6 = chai.eval("var i = 0; { var t = Test(); { var t2 = Test(); } } i = count(); return i;"); + int count6 = chai.eval("i = 0; { var t = Test(); { var t2 = Test(); } } i = count(); return i;"); if (count == 0 && count2 == 0 diff --git a/unittests/utility_test.cpp b/unittests/utility_test.cpp index bcd3240..43e9017 100644 --- a/unittests/utility_test.cpp +++ b/unittests/utility_test.cpp @@ -30,8 +30,8 @@ int main() chaiscript::ChaiScript chai; chai.add(m); if (chai.eval("var t = Test(); t.function2(); ") == "Function2" - && chai.eval("var t = Test(); t.functionOverload(1); ") == "int" - && chai.eval("var t = Test(); t.functionOverload(1.1); ") == "double") + && chai.eval("var t2 = Test(); t2.functionOverload(1); ") == "int" + && chai.eval("var t3 = Test(); t3.functionOverload(1.1); ") == "double") { chai.eval("t = Test();"); return EXIT_SUCCESS; diff --git a/unittests/variable_redefinition.chai b/unittests/variable_redefinition.chai new file mode 100644 index 0000000..b1600e6 --- /dev/null +++ b/unittests/variable_redefinition.chai @@ -0,0 +1,2 @@ +assert_throws("Variable already defined", fun() { var y = 10; var y = 20; }) + From bca86c87e128cb8235b7124e63cf6806fc6d6114 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 16 May 2012 11:54:46 -0600 Subject: [PATCH 4/5] Update copyrights to 2012 #23 --- include/chaiscript/chaiscript.hpp | 2 +- include/chaiscript/chaiscript_defines.hpp | 2 +- include/chaiscript/chaiscript_stdlib.hpp | 2 +- include/chaiscript/chaiscript_threading.hpp | 2 +- include/chaiscript/dispatchkit/any.hpp | 2 +- include/chaiscript/dispatchkit/bad_boxed_cast.hpp | 2 +- include/chaiscript/dispatchkit/bind_first.hpp | 2 +- include/chaiscript/dispatchkit/bootstrap.hpp | 2 +- include/chaiscript/dispatchkit/bootstrap_stl.hpp | 2 +- include/chaiscript/dispatchkit/boxed_cast.hpp | 2 +- include/chaiscript/dispatchkit/boxed_cast_helper.hpp | 2 +- include/chaiscript/dispatchkit/boxed_number.hpp | 2 +- include/chaiscript/dispatchkit/boxed_value.hpp | 2 +- include/chaiscript/dispatchkit/dispatchkit.hpp | 2 +- include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp | 2 +- include/chaiscript/dispatchkit/dynamic_object.hpp | 2 +- include/chaiscript/dispatchkit/exception_specification.hpp | 2 +- include/chaiscript/dispatchkit/function_call_detail.hpp | 2 +- include/chaiscript/dispatchkit/handle_return.hpp | 2 +- include/chaiscript/dispatchkit/operators.hpp | 2 +- include/chaiscript/dispatchkit/proxy_constructors.hpp | 2 +- include/chaiscript/dispatchkit/proxy_functions.hpp | 2 +- include/chaiscript/dispatchkit/proxy_functions_detail.hpp | 2 +- include/chaiscript/dispatchkit/register_function.hpp | 2 +- include/chaiscript/dispatchkit/type_info.hpp | 2 +- include/chaiscript/language/chaiscript_algebraic.hpp | 2 +- include/chaiscript/language/chaiscript_common.hpp | 2 +- include/chaiscript/language/chaiscript_engine.hpp | 2 +- include/chaiscript/language/chaiscript_eval.hpp | 2 +- include/chaiscript/language/chaiscript_parser.hpp | 2 +- include/chaiscript/language/chaiscript_prelude.hpp | 2 +- include/chaiscript/utility/utility.hpp | 2 +- license.txt | 2 +- readme.txt | 2 +- 34 files changed, 34 insertions(+), 34 deletions(-) diff --git a/include/chaiscript/chaiscript.hpp b/include/chaiscript/chaiscript.hpp index 13f5339..11a013a 100644 --- a/include/chaiscript/chaiscript.hpp +++ b/include/chaiscript/chaiscript.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/chaiscript_defines.hpp b/include/chaiscript/chaiscript_defines.hpp index 38cd72b..162454c 100644 --- a/include/chaiscript/chaiscript_defines.hpp +++ b/include/chaiscript/chaiscript_defines.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/chaiscript_stdlib.hpp b/include/chaiscript/chaiscript_stdlib.hpp index 18cb569..8f4bafa 100644 --- a/include/chaiscript/chaiscript_stdlib.hpp +++ b/include/chaiscript/chaiscript_stdlib.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/chaiscript_threading.hpp b/include/chaiscript/chaiscript_threading.hpp index bd34ef3..a955e2b 100644 --- a/include/chaiscript/chaiscript_threading.hpp +++ b/include/chaiscript/chaiscript_threading.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/any.hpp b/include/chaiscript/dispatchkit/any.hpp index 23f1803..e8dcfb8 100644 --- a/include/chaiscript/dispatchkit/any.hpp +++ b/include/chaiscript/dispatchkit/any.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/bad_boxed_cast.hpp b/include/chaiscript/dispatchkit/bad_boxed_cast.hpp index 53200c0..c893365 100644 --- a/include/chaiscript/dispatchkit/bad_boxed_cast.hpp +++ b/include/chaiscript/dispatchkit/bad_boxed_cast.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/bind_first.hpp b/include/chaiscript/dispatchkit/bind_first.hpp index a00d7ee..5bd4348 100644 --- a/include/chaiscript/dispatchkit/bind_first.hpp +++ b/include/chaiscript/dispatchkit/bind_first.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 1a8bf93..004076c 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 9bfc66a..85178b1 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/boxed_cast.hpp b/include/chaiscript/dispatchkit/boxed_cast.hpp index c4d820f..b110391 100644 --- a/include/chaiscript/dispatchkit/boxed_cast.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp index aec80df..ac70b3b 100644 --- a/include/chaiscript/dispatchkit/boxed_cast_helper.hpp +++ b/include/chaiscript/dispatchkit/boxed_cast_helper.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 4c082d6..0c92fcb 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index 660db0c..8428265 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 93a640d..111e3c1 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp b/include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp index 9fce9cf..f33b520 100644 --- a/include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp +++ b/include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/dynamic_object.hpp b/include/chaiscript/dispatchkit/dynamic_object.hpp index 6a95127..16e15db 100644 --- a/include/chaiscript/dispatchkit/dynamic_object.hpp +++ b/include/chaiscript/dispatchkit/dynamic_object.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/exception_specification.hpp b/include/chaiscript/dispatchkit/exception_specification.hpp index 0f08cdc..4863a7a 100644 --- a/include/chaiscript/dispatchkit/exception_specification.hpp +++ b/include/chaiscript/dispatchkit/exception_specification.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/function_call_detail.hpp b/include/chaiscript/dispatchkit/function_call_detail.hpp index e41e85c..48b76b7 100644 --- a/include/chaiscript/dispatchkit/function_call_detail.hpp +++ b/include/chaiscript/dispatchkit/function_call_detail.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/handle_return.hpp b/include/chaiscript/dispatchkit/handle_return.hpp index 18f63cc..8f07248 100644 --- a/include/chaiscript/dispatchkit/handle_return.hpp +++ b/include/chaiscript/dispatchkit/handle_return.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/operators.hpp b/include/chaiscript/dispatchkit/operators.hpp index 2686976..7863130 100644 --- a/include/chaiscript/dispatchkit/operators.hpp +++ b/include/chaiscript/dispatchkit/operators.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/proxy_constructors.hpp b/include/chaiscript/dispatchkit/proxy_constructors.hpp index 32bae3e..cff94e1 100644 --- a/include/chaiscript/dispatchkit/proxy_constructors.hpp +++ b/include/chaiscript/dispatchkit/proxy_constructors.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/proxy_functions.hpp b/include/chaiscript/dispatchkit/proxy_functions.hpp index b1cf052..4f65510 100644 --- a/include/chaiscript/dispatchkit/proxy_functions.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/proxy_functions_detail.hpp b/include/chaiscript/dispatchkit/proxy_functions_detail.hpp index 579074b..686db78 100644 --- a/include/chaiscript/dispatchkit/proxy_functions_detail.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions_detail.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/register_function.hpp b/include/chaiscript/dispatchkit/register_function.hpp index 2bb4cfe..109208c 100644 --- a/include/chaiscript/dispatchkit/register_function.hpp +++ b/include/chaiscript/dispatchkit/register_function.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/dispatchkit/type_info.hpp b/include/chaiscript/dispatchkit/type_info.hpp index 3c2fb20..b6d0dea 100644 --- a/include/chaiscript/dispatchkit/type_info.hpp +++ b/include/chaiscript/dispatchkit/type_info.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/language/chaiscript_algebraic.hpp b/include/chaiscript/language/chaiscript_algebraic.hpp index 3d363ce..7729bc8 100644 --- a/include/chaiscript/language/chaiscript_algebraic.hpp +++ b/include/chaiscript/language/chaiscript_algebraic.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index 0878603..7579a61 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 26faa16..b4a5708 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index b6d24d2..45cc002 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index f21c7b9..8e7c062 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/language/chaiscript_prelude.hpp b/include/chaiscript/language/chaiscript_prelude.hpp index 9cdecaf..ba427d8 100644 --- a/include/chaiscript/language/chaiscript_prelude.hpp +++ b/include/chaiscript/language/chaiscript_prelude.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/include/chaiscript/utility/utility.hpp b/include/chaiscript/utility/utility.hpp index 47bd9b3..63c5a23 100644 --- a/include/chaiscript/utility/utility.hpp +++ b/include/chaiscript/utility/utility.hpp @@ -1,6 +1,6 @@ // This file is distributed under the BSD License. // See "license.txt" for details. -// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com) +// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com diff --git a/license.txt b/license.txt index 57cd72b..0601f2f 100644 --- a/license.txt +++ b/license.txt @@ -1,4 +1,4 @@ -Copyright 2009-2011 Jason Turner and Jonathan Turner. All Rights Reserved. +Copyright 2009-2012 Jason Turner and Jonathan Turner. All Rights Reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/readme.txt b/readme.txt index 6c73989..b211c39 100644 --- a/readme.txt +++ b/readme.txt @@ -1,6 +1,6 @@ ChaiScript http://www.chaiscript.com -(c) 2009-2011 Jason Turner and Jonathan Turner +(c) 2009-2012 Jason Turner and Jonathan Turner Release under the BSD license, see "license.txt" for details. [Introduction] From 68df78a2a6defcdb7f6677e8abb31bb558ed7674 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 16 May 2012 15:55:03 -0600 Subject: [PATCH 5/5] Add examples for using C++ lambdas with chaiscript. #32 --- CMakeLists.txt | 4 ++++ unittests/cpp_lambda_test.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 unittests/cpp_lambda_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d448095..8d60975 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,6 +182,10 @@ if(BUILD_TESTING) target_link_libraries(short_comparison_test ${LIBS} ${EXTRA_LINKER_FLAGS}) add_test(NAME short_comparison_test COMMAND short_comparison_test) + add_executable(cpp_lambda_test unittests/cpp_lambda_test.cpp) + target_link_libraries(cpp_lambda_test ${LIBS} ${EXTRA_LINKER_FLAGS}) + add_test(NAME cpp_lambda_test COMMAND cpp_lambda_test) + add_executable(multifile_test unittests/multifile_test_main.cpp unittests/multifile_test_chai.cpp unittests/multifile_test_module.cpp) diff --git a/unittests/cpp_lambda_test.cpp b/unittests/cpp_lambda_test.cpp new file mode 100644 index 0000000..ec5a0ff --- /dev/null +++ b/unittests/cpp_lambda_test.cpp @@ -0,0 +1,30 @@ +#include + +#include + +int main() +{ + + // We cannot deduce the type of a lambda expression, you must either wrap it + // in an std::function or provide the signature + + + chaiscript::ChaiScript chai(chaiscript::Std_Lib::library()); + + // provide the signature + chai.add(chaiscript::fun([] { return "hello"; } ), "f1"); + + // wrap + chai.add(chaiscript::fun(std::function([] { return "world"; } )), "f2"); + + if (chai.eval("f1()") == "hello" + && chai.eval("f2()") == "world") + { + return EXIT_SUCCESS; + } else { + return EXIT_FAILURE; + } + + + +}