Cleanups and split up into _basic options
This commit is contained in:
parent
7561aa8828
commit
4e6e63ab5d
@ -249,27 +249,30 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
|
||||
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
|
||||
|
||||
add_library(stdlib STATIC static_libs/chaiscript_stdlib.cpp)
|
||||
add_library(parser STATIC static_libs/chaiscript_parser.cpp)
|
||||
|
||||
add_library(chaiscript_stdlib-${CHAI_VERSION} MODULE src/chaiscript_stdlib.cpp)
|
||||
add_library(chaiscript_stdlib-${CHAI_VERSION} MODULE src/chaiscript_stdlib_module.cpp)
|
||||
target_link_libraries(chaiscript_stdlib-${CHAI_VERSION} ${LIBS} ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
||||
set(CHAISCRIPT_LIBS stdlib parser)
|
||||
|
||||
add_executable(chai src/main.cpp ${Chai_INCLUDES})
|
||||
target_link_libraries(chai ${LIBS})
|
||||
add_dependencies(chai chaiscript_stdlib-${CHAI_VERSION})
|
||||
target_link_libraries(chai ${LIBS} ${CHAISCRIPT_LIBS})
|
||||
|
||||
if(BUILD_SAMPLES)
|
||||
add_executable(example samples/example.cpp)
|
||||
target_link_libraries(example ${LIBS})
|
||||
add_executable(test_num_exceptions samples/test_num_exceptions.cpp)
|
||||
target_link_libraries(test_num_exceptions ${LIBS})
|
||||
target_link_libraries(test_num_exceptions ${LIBS} ${CHAISCRIPT_LIBS})
|
||||
add_executable(memory_leak_test samples/memory_leak_test.cpp)
|
||||
target_link_libraries(memory_leak_test ${LIBS})
|
||||
target_link_libraries(memory_leak_test ${LIBS} ${CHAISCRIPT_LIBS})
|
||||
add_executable(inheritance samples/inheritance.cpp)
|
||||
target_link_libraries(inheritance ${LIBS})
|
||||
target_link_libraries(inheritance ${LIBS} ${CHAISCRIPT_LIBS})
|
||||
add_executable(factory samples/factory.cpp)
|
||||
target_link_libraries(factory ${LIBS})
|
||||
target_link_libraries(factory ${LIBS} ${CHAISCRIPT_LIBS})
|
||||
add_executable(fun_call_performance samples/fun_call_performance.cpp)
|
||||
target_link_libraries(fun_call_performance ${LIBS})
|
||||
target_link_libraries(fun_call_performance ${LIBS} ${CHAISCRIPT_LIBS})
|
||||
endif()
|
||||
|
||||
|
||||
@ -421,7 +424,7 @@ if(BUILD_TESTING)
|
||||
|
||||
if(NOT UNIT_TEST_LIGHT)
|
||||
add_executable(compiled_tests unittests/compiled_tests.cpp)
|
||||
target_link_libraries(compiled_tests ${LIBS})
|
||||
target_link_libraries(compiled_tests ${LIBS} ${CHAISCRIPT_LIBS})
|
||||
ADD_CATCH_TESTS(compiled_tests)
|
||||
|
||||
|
||||
@ -434,11 +437,11 @@ if(BUILD_TESTING)
|
||||
add_test(NAME Type_Info_Test COMMAND type_info_test)
|
||||
|
||||
add_executable(c_linkage_test unittests/c_linkage_test.cpp)
|
||||
target_link_libraries(c_linkage_test ${LIBS})
|
||||
target_link_libraries(c_linkage_test ${LIBS} ${CHAISCRIPT_LIBS})
|
||||
add_test(NAME C_Linkage_Test COMMAND c_linkage_test)
|
||||
|
||||
add_executable(integer_literal_test unittests/integer_literal_test.cpp)
|
||||
target_link_libraries(integer_literal_test ${LIBS})
|
||||
target_link_libraries(integer_literal_test ${LIBS} ${CHAISCRIPT_LIBS})
|
||||
add_test(NAME Integer_Literal_Test COMMAND integer_literal_test)
|
||||
|
||||
if(MULTITHREAD_SUPPORT_ENABLED)
|
||||
|
@ -816,16 +816,24 @@
|
||||
/// @namespace chaiscript::detail
|
||||
/// @brief Classes and functions reserved for internal use. Items in this namespace are not supported.
|
||||
|
||||
#include "chaiscript_defines.hpp"
|
||||
|
||||
#include "dispatchkit/dispatchkit.hpp"
|
||||
#include "dispatchkit/function_call.hpp"
|
||||
#include "dispatchkit/dynamic_object.hpp"
|
||||
#include "dispatchkit/boxed_number.hpp"
|
||||
|
||||
#include "language/chaiscript_eval.hpp"
|
||||
#include "language/chaiscript_engine.hpp"
|
||||
#include "chaiscript_basic.hpp"
|
||||
#include "language/chaiscript_parser.hpp"
|
||||
#include "chaiscript_stdlib.hpp"
|
||||
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
class ChaiScript : public ChaiScript_Basic
|
||||
{
|
||||
ChaiScript(std::vector<std::string> t_modulepaths = {},
|
||||
std::vector<std::string> t_usepaths = {})
|
||||
: ChaiScript_Basic(
|
||||
chaiscript::Std_Lib::library(),
|
||||
std::make_unique<parser::ChaiScript_Parser<eval::Noop_Tracer, optimizer::Optimizer_Default>>(),
|
||||
t_modulepaths, t_usepaths)
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif /* CHAISCRIPT_HPP_ */
|
||||
|
39
include/chaiscript/chaiscript_basic.hpp
Normal file
39
include/chaiscript/chaiscript_basic.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
// This file is distributed under the BSD License.
|
||||
// See "license.txt" for details.
|
||||
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
|
||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#ifndef CHAISCRIPT_BASIC_HPP_
|
||||
#define CHAISCRIPT_BASIC_HPP_
|
||||
|
||||
#include "chaiscript_defines.hpp"
|
||||
|
||||
#include "dispatchkit/dispatchkit.hpp"
|
||||
#include "dispatchkit/function_call.hpp"
|
||||
#include "dispatchkit/dynamic_object.hpp"
|
||||
#include "dispatchkit/boxed_number.hpp"
|
||||
|
||||
#include "language/chaiscript_eval.hpp"
|
||||
#include "language/chaiscript_engine.hpp"
|
||||
|
||||
// This file includes all of the basic requirements for ChaiScript,
|
||||
// to use, you might do something like:
|
||||
//
|
||||
|
||||
/*
|
||||
|
||||
#include "chaiscript_stdlib.hpp"
|
||||
#include "language/chaiscript_parser.hpp"
|
||||
|
||||
ChaiScript_Basic chai(
|
||||
chaiscript::Std_Lib::library(),
|
||||
std::make_unique<parser::ChaiScript_Parser<eval::Noop_Tracer, optimizer::Optimizer_Default>>());
|
||||
|
||||
*/
|
||||
|
||||
// If you want a fully packaged ready to go ChaiScript, use chaiscript.hpp
|
||||
|
||||
|
||||
|
||||
#endif /* CHAISCRIPT_BASIC_HPP_ */
|
@ -44,8 +44,6 @@ namespace chaiscript
|
||||
|
||||
static ModulePtr library()
|
||||
{
|
||||
// using namespace bootstrap;
|
||||
|
||||
auto lib = std::make_shared<Module>();
|
||||
bootstrap::Bootstrap::bootstrap(*lib);
|
||||
|
||||
|
@ -21,10 +21,7 @@ namespace chaiscript {
|
||||
class bad_any_cast : public std::bad_cast
|
||||
{
|
||||
public:
|
||||
bad_any_cast() noexcept
|
||||
: m_what("bad any cast")
|
||||
{
|
||||
}
|
||||
bad_any_cast() = default;
|
||||
|
||||
bad_any_cast(const bad_any_cast &) = default;
|
||||
|
||||
@ -37,7 +34,7 @@ namespace chaiscript {
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_what;
|
||||
std::string m_what = "bad any cast";
|
||||
};
|
||||
}
|
||||
|
||||
@ -151,8 +148,7 @@ namespace chaiscript {
|
||||
|
||||
const std::type_info & type() const
|
||||
{
|
||||
if (m_data)
|
||||
{
|
||||
if (m_data) {
|
||||
return m_data->type();
|
||||
} else {
|
||||
return typeid(void);
|
||||
|
@ -41,7 +41,7 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
explicit bad_boxed_cast(std::string t_what) noexcept
|
||||
: to(nullptr), m_what(std::move(t_what))
|
||||
: m_what(std::move(t_what))
|
||||
{
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
Type_Info from; ///< Type_Info contained in the Boxed_Value
|
||||
const std::type_info *to; ///< std::type_info of the desired (but failed) result type
|
||||
const std::type_info *to = nullptr; ///< std::type_info of the desired (but failed) result type
|
||||
|
||||
private:
|
||||
std::string m_what;
|
||||
|
@ -15,18 +15,6 @@ namespace chaiscript
|
||||
/// \brief Classes and functions useful for bootstrapping of ChaiScript and adding of new types
|
||||
namespace bootstrap
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
/// \brief Constructs a new POD value object from a Boxed_Number
|
||||
/// \param[in] v Boxed_Number to copy into the new object
|
||||
/// \returns The newly created object.
|
||||
template<typename P1>
|
||||
std::shared_ptr<P1> construct_pod(const Boxed_Number &v)
|
||||
{
|
||||
return std::make_shared<P1>(v.get_as<P1>());
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, typename = typename std::enable_if<std::is_array<T>::value>::type >
|
||||
void array(const std::string &type, Module& m)
|
||||
{
|
||||
@ -110,7 +98,7 @@ namespace chaiscript
|
||||
template<typename T>
|
||||
void construct_pod(const std::string &type, Module& m)
|
||||
{
|
||||
m.add(fun(&detail::construct_pod<T>), type);
|
||||
m.add(fun([](const Boxed_Number &bn){ return bn.get_as<T>(); }), type);
|
||||
}
|
||||
|
||||
|
||||
@ -161,15 +149,14 @@ namespace chaiscript
|
||||
/// for handling of Proxy_Function object (that is,
|
||||
/// function variables.
|
||||
template<typename Type>
|
||||
std::shared_ptr<Type> shared_ptr_clone(const std::shared_ptr<Type> &p)
|
||||
auto shared_ptr_clone(const std::shared_ptr<Type> &p)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
/// Specific version of shared_ptr_clone just for Proxy_Functions
|
||||
template<typename Type>
|
||||
std::shared_ptr<typename std::remove_const<Type>::type>
|
||||
shared_ptr_unconst_clone(const std::shared_ptr<typename std::add_const<Type>::type> &p)
|
||||
auto shared_ptr_unconst_clone(const std::shared_ptr<typename std::add_const<Type>::type> &p)
|
||||
{
|
||||
return std::const_pointer_cast<typename std::remove_const<Type>::type>(p);
|
||||
}
|
||||
|
@ -41,7 +41,6 @@ namespace chaiscript
|
||||
struct Bidir_Range
|
||||
{
|
||||
typedef Container container_type;
|
||||
typedef typename std::iterator_traits<typename Container::iterator>::reference reference_type;
|
||||
|
||||
Bidir_Range(Container &c)
|
||||
: m_begin(c.begin()), m_end(c.end())
|
||||
@ -71,16 +70,16 @@ namespace chaiscript
|
||||
--m_end;
|
||||
}
|
||||
|
||||
reference_type front() const
|
||||
decltype(auto) front() const
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
throw std::range_error("Range empty");
|
||||
}
|
||||
return *m_begin;
|
||||
return (*m_begin);
|
||||
}
|
||||
|
||||
reference_type back() const
|
||||
decltype(auto) back() const
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
@ -88,7 +87,7 @@ namespace chaiscript
|
||||
}
|
||||
typename Container::iterator pos = m_end;
|
||||
--pos;
|
||||
return *(pos);
|
||||
return (*(pos));
|
||||
}
|
||||
|
||||
typename Container::iterator m_begin;
|
||||
@ -129,24 +128,24 @@ namespace chaiscript
|
||||
--m_end;
|
||||
}
|
||||
|
||||
const_reference_type front() const
|
||||
decltype(auto) front() const
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
throw std::range_error("Range empty");
|
||||
}
|
||||
return *m_begin;
|
||||
return (*m_begin);
|
||||
}
|
||||
|
||||
const_reference_type back() const
|
||||
decltype(auto) back() const
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
throw std::range_error("Range empty");
|
||||
}
|
||||
typename Container::const_iterator pos = m_end;
|
||||
auto pos = m_end;
|
||||
--pos;
|
||||
return *(pos);
|
||||
return (*(pos));
|
||||
}
|
||||
|
||||
typename Container::const_iterator m_begin;
|
||||
@ -482,12 +481,8 @@ namespace chaiscript
|
||||
{
|
||||
m.add(user_type<PairType>(), type);
|
||||
|
||||
|
||||
typename PairType::first_type PairType::* f = &PairType::first;
|
||||
typename PairType::second_type PairType::* s = &PairType::second;
|
||||
|
||||
m.add(fun(f), "first");
|
||||
m.add(fun(s), "second");
|
||||
m.add(fun(&PairType::first), "first");
|
||||
m.add(fun(&PairType::second), "second");
|
||||
|
||||
basic_constructors<PairType>(type, m);
|
||||
m.add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
|
||||
@ -605,7 +600,6 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
|
||||
/// hopefully working List type
|
||||
/// http://www.sgi.com/tech/stl/List.html
|
||||
template<typename ListType>
|
||||
void list_type(const std::string &type, Module& m)
|
||||
|
@ -77,7 +77,7 @@ namespace chaiscript
|
||||
|
||||
struct Object_Data
|
||||
{
|
||||
static std::shared_ptr<Data> get(Boxed_Value::Void_Type, bool t_return_value)
|
||||
static auto get(Boxed_Value::Void_Type, bool t_return_value)
|
||||
{
|
||||
return std::make_shared<Data>(
|
||||
detail::Get_Type_Info<void>::get(),
|
||||
@ -89,13 +89,13 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static std::shared_ptr<Data> get(const std::shared_ptr<T> *obj, bool t_return_value)
|
||||
static auto get(const std::shared_ptr<T> *obj, bool t_return_value)
|
||||
{
|
||||
return get(*obj, t_return_value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static std::shared_ptr<Data> get(const std::shared_ptr<T> &obj, bool t_return_value)
|
||||
static auto get(const std::shared_ptr<T> &obj, bool t_return_value)
|
||||
{
|
||||
return std::make_shared<Data>(
|
||||
detail::Get_Type_Info<T>::get(),
|
||||
@ -107,7 +107,7 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static std::shared_ptr<Data> get(std::shared_ptr<T> &&obj, bool t_return_value)
|
||||
static auto get(std::shared_ptr<T> &&obj, bool t_return_value)
|
||||
{
|
||||
auto ptr = obj.get();
|
||||
return std::make_shared<Data>(
|
||||
@ -120,20 +120,20 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static std::shared_ptr<Data> get(T *t, bool t_return_value)
|
||||
static auto get(T *t, bool t_return_value)
|
||||
{
|
||||
return get(std::ref(*t), t_return_value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static std::shared_ptr<Data> get(const T *t, bool t_return_value)
|
||||
static auto get(const T *t, bool t_return_value)
|
||||
{
|
||||
return get(std::cref(*t), t_return_value);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
static std::shared_ptr<Data> get(std::reference_wrapper<T> obj, bool t_return_value)
|
||||
static auto get(std::reference_wrapper<T> obj, bool t_return_value)
|
||||
{
|
||||
auto p = &obj.get();
|
||||
return std::make_shared<Data>(
|
||||
@ -146,7 +146,7 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static std::shared_ptr<Data> get(T t, bool t_return_value)
|
||||
static auto get(T t, bool t_return_value)
|
||||
{
|
||||
auto p = std::make_shared<T>(std::move(t));
|
||||
auto ptr = p.get();
|
||||
@ -182,10 +182,7 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
/// Unknown-type constructor
|
||||
Boxed_Value()
|
||||
: m_data(Object_Data::get())
|
||||
{
|
||||
}
|
||||
Boxed_Value() = default;
|
||||
|
||||
Boxed_Value(Boxed_Value&&) = default;
|
||||
Boxed_Value& operator=(Boxed_Value&&) = default;
|
||||
@ -349,7 +346,7 @@ namespace chaiscript
|
||||
: m_data(t_data) {
|
||||
}
|
||||
|
||||
std::shared_ptr<Data> m_data;
|
||||
std::shared_ptr<Data> m_data = Object_Data::get();
|
||||
};
|
||||
|
||||
/// @brief Creates a Boxed_Value. If the object passed in is a value type, it is copied. If it is a pointer, std::shared_ptr, or std::reference_type
|
||||
|
@ -8,7 +8,6 @@
|
||||
#define CHAISCRIPT_DISPATCHKIT_HPP_
|
||||
|
||||
#include <algorithm>
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <map>
|
||||
@ -31,6 +30,7 @@
|
||||
#include "proxy_constructors.hpp"
|
||||
#include "proxy_functions.hpp"
|
||||
#include "type_info.hpp"
|
||||
#include "short_alloc.hpp"
|
||||
|
||||
namespace chaiscript {
|
||||
class Boxed_Number;
|
||||
@ -376,20 +376,55 @@ namespace chaiscript
|
||||
{
|
||||
struct Stack_Holder
|
||||
{
|
||||
typedef std::vector<std::pair<std::string, Boxed_Value>> Scope;
|
||||
typedef std::vector<Scope> StackData;
|
||||
//template <class T, std::size_t BufSize = sizeof(T)*20000>
|
||||
// using SmallVector = std::vector<T, short_alloc<T, BufSize>>;
|
||||
|
||||
template <class T>
|
||||
using SmallVector = std::vector<T>;
|
||||
|
||||
|
||||
typedef SmallVector<std::pair<std::string, Boxed_Value>> Scope;
|
||||
typedef SmallVector<Scope> StackData;
|
||||
typedef SmallVector<StackData> Stacks;
|
||||
typedef SmallVector<Boxed_Value> Call_Param_List;
|
||||
typedef SmallVector<Call_Param_List> Call_Params;
|
||||
|
||||
Stack_Holder()
|
||||
{
|
||||
stacks.reserve(2);
|
||||
stacks.emplace_back(1);
|
||||
call_params.emplace_back();
|
||||
call_params.back().reserve(2);
|
||||
push_stack();
|
||||
push_call_params();
|
||||
}
|
||||
|
||||
std::vector<StackData> stacks;
|
||||
void push_stack_data()
|
||||
{
|
||||
stacks.back().emplace_back();
|
||||
// stacks.back().emplace_back(Scope(scope_allocator));
|
||||
}
|
||||
|
||||
void push_stack()
|
||||
{
|
||||
stacks.emplace_back(1);
|
||||
// stacks.emplace_back(StackData(1, Scope(scope_allocator), stack_data_allocator));
|
||||
}
|
||||
|
||||
void push_call_params()
|
||||
{
|
||||
call_params.emplace_back();
|
||||
// call_params.emplace_back(Call_Param_List(call_param_list_allocator));
|
||||
}
|
||||
|
||||
//Scope::allocator_type::arena_type scope_allocator;
|
||||
//StackData::allocator_type::arena_type stack_data_allocator;
|
||||
//Stacks::allocator_type::arena_type stacks_allocator;
|
||||
//Call_Param_List::allocator_type::arena_type call_param_list_allocator;
|
||||
//Call_Params::allocator_type::arena_type call_params_allocator;
|
||||
|
||||
// Stacks stacks = Stacks(stacks_allocator);
|
||||
// Call_Params call_params = Call_Params(call_params_allocator);
|
||||
|
||||
Stacks stacks;
|
||||
Call_Params call_params;
|
||||
|
||||
std::vector<std::vector<Boxed_Value>> call_params;
|
||||
int call_depth = 0;
|
||||
};
|
||||
|
||||
@ -401,7 +436,7 @@ namespace chaiscript
|
||||
public:
|
||||
typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map;
|
||||
typedef std::vector<std::pair<std::string, Boxed_Value>> Scope;
|
||||
typedef std::vector<Scope> StackData;
|
||||
typedef Stack_Holder::StackData StackData;
|
||||
|
||||
struct State
|
||||
{
|
||||
@ -585,8 +620,8 @@ namespace chaiscript
|
||||
/// Adds a new scope to the stack
|
||||
static void new_scope(Stack_Holder &t_holder)
|
||||
{
|
||||
get_stack_data(t_holder).emplace_back();
|
||||
t_holder.call_params.emplace_back();
|
||||
t_holder.push_stack_data();
|
||||
t_holder.push_call_params();
|
||||
}
|
||||
|
||||
/// Pops the current scope from the stack
|
||||
@ -605,7 +640,7 @@ namespace chaiscript
|
||||
static void new_stack(Stack_Holder &t_holder)
|
||||
{
|
||||
// add a new Stack with 1 element
|
||||
t_holder.stacks.emplace_back(1);
|
||||
t_holder.push_stack();
|
||||
}
|
||||
|
||||
static void pop_stack(Stack_Holder &t_holder)
|
||||
@ -817,7 +852,7 @@ namespace chaiscript
|
||||
{
|
||||
auto &stack = get_stack_data();
|
||||
auto &scope = stack.front();
|
||||
scope = std::vector<std::pair<std::string, Boxed_Value>>(t_locals.begin(), t_locals.end());
|
||||
scope.assign(t_locals.begin(), t_locals.end());
|
||||
}
|
||||
|
||||
|
||||
|
157
include/chaiscript/dispatchkit/short_alloc.hpp
Normal file
157
include/chaiscript/dispatchkit/short_alloc.hpp
Normal file
@ -0,0 +1,157 @@
|
||||
#ifndef SHORT_ALLOC_H
|
||||
#define SHORT_ALLOC_H
|
||||
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Howard Hinnant
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <cstddef>
|
||||
#include <cassert>
|
||||
|
||||
template <std::size_t N, std::size_t alignment = alignof(std::max_align_t)>
|
||||
class arena
|
||||
{
|
||||
alignas(alignment) char buf_[N];
|
||||
char* ptr_;
|
||||
|
||||
public:
|
||||
~arena() {ptr_ = nullptr;}
|
||||
arena() noexcept : ptr_(buf_) {}
|
||||
arena(const arena&) = delete;
|
||||
arena& operator=(const arena&) = delete;
|
||||
|
||||
template <std::size_t ReqAlign> char* allocate(std::size_t n);
|
||||
void deallocate(char* p, std::size_t n) noexcept;
|
||||
|
||||
static constexpr std::size_t size() noexcept {return N;}
|
||||
std::size_t used() const noexcept {return static_cast<std::size_t>(ptr_ - buf_);}
|
||||
void reset() noexcept {ptr_ = buf_;}
|
||||
|
||||
private:
|
||||
static
|
||||
std::size_t
|
||||
align_up(std::size_t n) noexcept
|
||||
{return (n + (alignment-1)) & ~(alignment-1);}
|
||||
|
||||
bool
|
||||
pointer_in_buffer(char* p) noexcept
|
||||
{return buf_ <= p && p <= buf_ + N;}
|
||||
};
|
||||
|
||||
template <std::size_t N, std::size_t alignment>
|
||||
template <std::size_t ReqAlign>
|
||||
char*
|
||||
arena<N, alignment>::allocate(std::size_t n)
|
||||
{
|
||||
static_assert(ReqAlign <= alignment, "alignment is too small for this arena");
|
||||
assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
|
||||
auto const aligned_n = align_up(n);
|
||||
if (static_cast<decltype(aligned_n)>(buf_ + N - ptr_) >= aligned_n)
|
||||
{
|
||||
char* r = ptr_;
|
||||
ptr_ += aligned_n;
|
||||
return r;
|
||||
}
|
||||
|
||||
static_assert(alignment <= alignof(std::max_align_t), "you've chosen an "
|
||||
"alignment that is larger than alignof(std::max_align_t), and "
|
||||
"cannot be guaranteed by normal operator new");
|
||||
return static_cast<char*>(::operator new(n));
|
||||
}
|
||||
|
||||
template <std::size_t N, std::size_t alignment>
|
||||
void
|
||||
arena<N, alignment>::deallocate(char* p, std::size_t n) noexcept
|
||||
{
|
||||
assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena");
|
||||
if (pointer_in_buffer(p))
|
||||
{
|
||||
n = align_up(n);
|
||||
if (p + n == ptr_)
|
||||
ptr_ = p;
|
||||
}
|
||||
else
|
||||
::operator delete(p);
|
||||
}
|
||||
|
||||
template <class T, std::size_t N, std::size_t Align = alignof(std::max_align_t)>
|
||||
class short_alloc
|
||||
{
|
||||
public:
|
||||
using value_type = T;
|
||||
static auto constexpr alignment = Align;
|
||||
static auto constexpr size = N;
|
||||
using arena_type = arena<size, alignment>;
|
||||
|
||||
private:
|
||||
arena_type& a_;
|
||||
|
||||
public:
|
||||
short_alloc(const short_alloc&) = default;
|
||||
short_alloc& operator=(const short_alloc&) = delete;
|
||||
|
||||
short_alloc(arena_type& a) noexcept : a_(a)
|
||||
{
|
||||
static_assert(size % alignment == 0,
|
||||
"size N needs to be a multiple of alignment Align");
|
||||
}
|
||||
template <class U>
|
||||
short_alloc(const short_alloc<U, N, alignment>& a) noexcept
|
||||
: a_(a.a_) {}
|
||||
|
||||
template <class _Up> struct rebind {using other = short_alloc<_Up, N, alignment>;};
|
||||
|
||||
T* allocate(std::size_t n)
|
||||
{
|
||||
return reinterpret_cast<T*>(a_.template allocate<alignof(T)>(n*sizeof(T)));
|
||||
}
|
||||
void deallocate(T* p, std::size_t n) noexcept
|
||||
{
|
||||
a_.deallocate(reinterpret_cast<char*>(p), n*sizeof(T));
|
||||
}
|
||||
|
||||
template <class T1, std::size_t N1, std::size_t A1,
|
||||
class U, std::size_t M, std::size_t A2>
|
||||
friend
|
||||
bool
|
||||
operator==(const short_alloc<T1, N1, A1>& x, const short_alloc<U, M, A2>& y) noexcept;
|
||||
|
||||
template <class U, std::size_t M, std::size_t A> friend class short_alloc;
|
||||
};
|
||||
|
||||
template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2>
|
||||
inline
|
||||
bool
|
||||
operator==(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept
|
||||
{
|
||||
return N == M && A1 == A2 && &x.a_ == &y.a_;
|
||||
}
|
||||
|
||||
template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2>
|
||||
inline
|
||||
bool
|
||||
operator!=(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept
|
||||
{
|
||||
return !(x == y);
|
||||
}
|
||||
|
||||
#endif // SHORT_ALLOC_HPP
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#include "../chaiscript_defines.hpp"
|
||||
#include "../chaiscript_threading.hpp"
|
||||
@ -46,7 +47,6 @@
|
||||
|
||||
|
||||
#include "../dispatchkit/exception_specification.hpp"
|
||||
#include "chaiscript_parser.hpp"
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
@ -58,7 +58,7 @@ namespace chaiscript
|
||||
|
||||
|
||||
/// \brief The main object that the ChaiScript user will use.
|
||||
class ChaiScript {
|
||||
class ChaiScript_Basic {
|
||||
|
||||
mutable chaiscript::detail::threading::shared_mutex m_mutex;
|
||||
mutable chaiscript::detail::threading::recursive_mutex m_use_mutex;
|
||||
@ -215,13 +215,14 @@ namespace chaiscript
|
||||
/// \param[in] t_lib Standard library to apply to this ChaiScript instance
|
||||
/// \param[in] t_modulepaths Vector of paths to search when attempting to load a binary module
|
||||
/// \param[in] t_usepaths Vector of paths to search when attempting to "use" an included ChaiScript file
|
||||
ChaiScript(const ModulePtr &t_lib,
|
||||
std::vector<std::string> t_modulepaths = std::vector<std::string>(),
|
||||
std::vector<std::string> t_usepaths = std::vector<std::string>())
|
||||
: m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths)),
|
||||
m_parser(std::make_unique<parser::ChaiScript_Parser<eval::Noop_Tracer, optimizer::Optimizer<optimizer::Partial_Fold, optimizer::Unused_Return, optimizer::Constant_Fold, optimizer::If, optimizer::Return, optimizer::Dead_Code, optimizer::Block, optimizer::For_Loop>>>()),
|
||||
ChaiScript_Basic(const ModulePtr &t_lib,
|
||||
std::unique_ptr<parser::ChaiScript_Parser_Base> &&parser,
|
||||
std::vector<std::string> t_modulepaths = {},
|
||||
std::vector<std::string> t_usepaths = {})
|
||||
: m_module_paths(std::move(t_modulepaths)),
|
||||
m_use_paths(std::move(t_usepaths)),
|
||||
m_parser(std::move(parser)),
|
||||
m_engine(*m_parser)
|
||||
|
||||
{
|
||||
if (m_module_paths.empty())
|
||||
{
|
||||
@ -243,10 +244,12 @@ namespace chaiscript
|
||||
///
|
||||
/// \param[in] t_modulepaths Vector of paths to search when attempting to load a binary module
|
||||
/// \param[in] t_usepaths Vector of paths to search when attempting to "use" an included ChaiScript file
|
||||
ChaiScript( std::vector<std::string> t_modulepaths = std::vector<std::string>(),
|
||||
std::vector<std::string> t_usepaths = std::vector<std::string>())
|
||||
: m_module_paths(std::move(t_modulepaths)), m_use_paths(std::move(t_usepaths)),
|
||||
m_parser(std::make_unique<parser::ChaiScript_Parser<eval::Noop_Tracer, optimizer::Optimizer<optimizer::Partial_Fold, optimizer::Unused_Return, optimizer::Constant_Fold, optimizer::If, optimizer::Return, optimizer::Dead_Code, optimizer::Block, optimizer::For_Loop>>>()),
|
||||
ChaiScript_Basic(std::unique_ptr<parser::ChaiScript_Parser_Base> &&parser,
|
||||
std::vector<std::string> t_modulepaths = {},
|
||||
std::vector<std::string> t_usepaths = {})
|
||||
: m_module_paths(std::move(t_modulepaths)),
|
||||
m_use_paths(std::move(t_usepaths)),
|
||||
m_parser(std::move(parser)),
|
||||
m_engine(*m_parser)
|
||||
{
|
||||
if (m_module_paths.empty())
|
||||
@ -265,14 +268,14 @@ namespace chaiscript
|
||||
|
||||
union cast_union
|
||||
{
|
||||
Boxed_Value (ChaiScript::*in_ptr)(const std::string&);
|
||||
Boxed_Value (ChaiScript_Basic::*in_ptr)(const std::string&);
|
||||
void *out_ptr;
|
||||
};
|
||||
|
||||
Dl_info rInfo;
|
||||
memset( &rInfo, 0, sizeof(rInfo) );
|
||||
cast_union u;
|
||||
u.in_ptr = &ChaiScript::use;
|
||||
u.in_ptr = &ChaiScript_Basic::use;
|
||||
if ( dladdr(static_cast<void*>(u.out_ptr), &rInfo) && rInfo.dli_fname ) {
|
||||
std::string dllpath(rInfo.dli_fname);
|
||||
const size_t lastslash = dllpath.rfind('/');
|
||||
@ -385,7 +388,7 @@ namespace chaiscript
|
||||
/// \param[in] t_name Name of the value to add
|
||||
/// \throw chaiscript::exception::global_non_const If t_bv is not a constant object
|
||||
/// \sa Boxed_Value::is_const
|
||||
ChaiScript &add_global_const(const Boxed_Value &t_bv, const std::string &t_name)
|
||||
ChaiScript_Basic &add_global_const(const Boxed_Value &t_bv, const std::string &t_name)
|
||||
{
|
||||
Name_Validator::validate_object_name(t_name);
|
||||
m_engine.add_global_const(t_bv, t_name);
|
||||
@ -397,14 +400,14 @@ namespace chaiscript
|
||||
/// \param[in] t_name Name of the value to add
|
||||
/// \warning The user is responsible for making sure the object is thread-safe if necessary
|
||||
/// ChaiScript is thread-safe but provides no threading locking mechanism to the script
|
||||
ChaiScript &add_global(const Boxed_Value &t_bv, const std::string &t_name)
|
||||
ChaiScript_Basic &add_global(const Boxed_Value &t_bv, const std::string &t_name)
|
||||
{
|
||||
Name_Validator::validate_object_name(t_name);
|
||||
m_engine.add_global(t_bv, t_name);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ChaiScript &set_global(const Boxed_Value &t_bv, const std::string &t_name)
|
||||
ChaiScript_Basic &set_global(const Boxed_Value &t_bv, const std::string &t_name)
|
||||
{
|
||||
Name_Validator::validate_object_name(t_name);
|
||||
m_engine.set_global(t_bv, t_name);
|
||||
@ -504,7 +507,7 @@ namespace chaiscript
|
||||
///
|
||||
/// \sa \ref adding_items
|
||||
template<typename T>
|
||||
ChaiScript &add(const T &t_t, const std::string &t_name)
|
||||
ChaiScript_Basic &add(const T &t_t, const std::string &t_name)
|
||||
{
|
||||
Name_Validator::validate_object_name(t_name);
|
||||
m_engine.add(t_t, t_name);
|
||||
@ -520,7 +523,7 @@ namespace chaiscript
|
||||
/// chaiscript::ChaiScript chai;
|
||||
/// chai.add(chaiscript::base_class<std::runtime_error, chaiscript::dispatch_error>());
|
||||
/// \endcode
|
||||
ChaiScript &add(const Type_Conversion &d)
|
||||
ChaiScript_Basic &add(const Type_Conversion &d)
|
||||
{
|
||||
m_engine.add(d);
|
||||
return *this;
|
||||
@ -529,7 +532,7 @@ namespace chaiscript
|
||||
/// \brief Adds all elements of a module to ChaiScript runtime
|
||||
/// \param[in] t_p The module to add.
|
||||
/// \sa chaiscript::Module
|
||||
ChaiScript &add(const ModulePtr &t_p)
|
||||
ChaiScript_Basic &add(const ModulePtr &t_p)
|
||||
{
|
||||
t_p->apply(*this, this->get_eval_engine());
|
||||
return *this;
|
||||
|
@ -417,6 +417,8 @@ namespace chaiscript {
|
||||
}
|
||||
};
|
||||
|
||||
typedef Optimizer<optimizer::Partial_Fold, optimizer::Unused_Return, optimizer::Constant_Fold,
|
||||
optimizer::If, optimizer::Return, optimizer::Dead_Code, optimizer::Block, optimizer::For_Loop> Optimizer_Default;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -16,10 +16,6 @@
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
|
||||
#if defined(CHAISCRIPT_UTF16_UTF32)
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
@ -28,6 +24,11 @@
|
||||
#include "chaiscript_optimizer.hpp"
|
||||
#include "chaiscript_tracer.hpp"
|
||||
|
||||
#if defined(CHAISCRIPT_UTF16_UTF32)
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
#endif
|
||||
|
||||
#if defined(CHAISCRIPT_MSVC) && defined(max) && defined(min)
|
||||
#define CHAISCRIPT_PUSHED_MIN_MAX
|
||||
#pragma push_macro("max") // Why Microsoft? why? This is worse than bad
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
|
||||
class Entity
|
||||
{
|
||||
@ -57,7 +56,7 @@ class Factory
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.add(chaiscript::fun(&Entity::width), "width");
|
||||
chai.add(chaiscript::fun(&Entity::height), "height");
|
||||
|
@ -13,7 +13,6 @@
|
||||
#endif
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
|
||||
#ifdef READLINE_AVAILABLE
|
||||
#include <readline/readline.h>
|
||||
@ -291,7 +290,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
//chaiscript::ChaiScript chai(modulepaths, usepaths);
|
||||
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library(), usepaths);
|
||||
chaiscript::ChaiScript chai(usepaths);
|
||||
|
||||
chai.add(chaiscript::fun(&myexit), "exit");
|
||||
chai.add(chaiscript::fun(&myexit), "quit");
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
|
||||
class BaseClass
|
||||
{
|
||||
@ -70,7 +69,7 @@ class ChaiScriptDerived : public BaseClass
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(chaiscript::fun(&BaseClass::doSomething), "doSomething");
|
||||
chai.add(chaiscript::fun(&BaseClass::setValue), "setValue");
|
||||
chai.add(chaiscript::fun(&BaseClass::getValue), "getValue");
|
||||
|
@ -1,7 +1,6 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
|
||||
#ifdef READLINE_AVAILABLE
|
||||
#include <readline/readline.h>
|
||||
@ -32,16 +31,9 @@ void function(void)
|
||||
class test
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript::State backupState;
|
||||
chaiscript::ChaiScript::State backupState = chai.get_state();
|
||||
|
||||
public:
|
||||
test()
|
||||
: chai(chaiscript::Std_Lib::library())
|
||||
{
|
||||
backupState = chai.get_state();
|
||||
}
|
||||
~test(){}
|
||||
|
||||
void ResetState()
|
||||
{
|
||||
chai.set_state(backupState);
|
||||
|
@ -1,11 +1,10 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
||||
#include <chaiscript/dispatchkit/function_call.hpp>
|
||||
|
||||
int main( int /*argc*/ , char * /*argv*/[] )
|
||||
{
|
||||
chaiscript::ChaiScript ch( chaiscript::Std_Lib::library( ) );
|
||||
chaiscript::ChaiScript ch;
|
||||
|
||||
|
||||
try
|
||||
|
@ -12,7 +12,10 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#endif
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
#include "../static_libs/chaiscript_parser.hpp"
|
||||
#include "../static_libs/chaiscript_stdlib.hpp"
|
||||
|
||||
|
||||
#ifdef READLINE_AVAILABLE
|
||||
#include <readline/readline.h>
|
||||
@ -217,7 +220,7 @@ void myexit(int return_val) {
|
||||
exit(return_val);
|
||||
}
|
||||
|
||||
void interactive(chaiscript::ChaiScript& chai)
|
||||
void interactive(chaiscript::ChaiScript_Basic& chai)
|
||||
{
|
||||
using_history();
|
||||
|
||||
@ -288,7 +291,7 @@ int main(int argc, char *argv[])
|
||||
modulepaths.push_back(modulepath);
|
||||
}
|
||||
|
||||
chaiscript::ChaiScript chai(modulepaths,usepaths);
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser(),modulepaths,usepaths);
|
||||
|
||||
chai.add(chaiscript::fun(&myexit), "exit");
|
||||
chai.add(chaiscript::fun(&myexit), "quit");
|
||||
|
@ -4,7 +4,7 @@
|
||||
// Copyright 2009-2016, Jason Turner (jason@emptycrate.com)
|
||||
// http://www.chaiscript.com
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
@ -1,5 +1,5 @@
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
#include <chaiscript/dispatchkit/bootstrap.hpp>
|
||||
#include <string>
|
||||
|
||||
|
8
static_libs/chaiscript_parser.cpp
Normal file
8
static_libs/chaiscript_parser.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "../include/chaiscript/language/chaiscript_parser.hpp"
|
||||
#include "chaiscript_parser.hpp"
|
||||
|
||||
std::unique_ptr<chaiscript::parser::ChaiScript_Parser_Base> create_chaiscript_parser()
|
||||
{
|
||||
return std::make_unique<chaiscript::parser::ChaiScript_Parser<chaiscript::eval::Noop_Tracer, chaiscript::optimizer::Optimizer_Default>>();
|
||||
}
|
||||
|
13
static_libs/chaiscript_parser.hpp
Normal file
13
static_libs/chaiscript_parser.hpp
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
#ifndef CHAISCRIPT_PARSER_LIB
|
||||
#define CHAISCRIPT_PARSER_LIB
|
||||
|
||||
namespace chaiscript {
|
||||
namespace parser {
|
||||
class ChaiScript_Parser_Base;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<chaiscript::parser::ChaiScript_Parser_Base> create_chaiscript_parser();
|
||||
|
||||
#endif
|
8
static_libs/chaiscript_stdlib.cpp
Normal file
8
static_libs/chaiscript_stdlib.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "../include/chaiscript/chaiscript_stdlib.hpp"
|
||||
#include "chaiscript_stdlib.hpp"
|
||||
|
||||
std::shared_ptr<chaiscript::Module> create_chaiscript_stdlib()
|
||||
{
|
||||
return chaiscript::Std_Lib::library();
|
||||
}
|
||||
|
11
static_libs/chaiscript_stdlib.hpp
Normal file
11
static_libs/chaiscript_stdlib.hpp
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
#ifndef CHAISCRIPT_STDLIB
|
||||
#define CHAISCRIPT_STDLIB
|
||||
|
||||
namespace chaiscript {
|
||||
class Module;
|
||||
}
|
||||
|
||||
std::shared_ptr<chaiscript::Module> create_chaiscript_stdlib();
|
||||
|
||||
#endif
|
@ -1,4 +1,6 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
#include "../static_libs/chaiscript_parser.hpp"
|
||||
#include "../static_libs/chaiscript_stdlib.hpp"
|
||||
|
||||
|
||||
extern "C"
|
||||
@ -11,8 +13,7 @@ extern "C"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(chaiscript::fun(&do_something), "do_something");
|
||||
|
||||
return chai.eval<int>("do_something(101)") == 101 % 2?EXIT_SUCCESS:EXIT_FAILURE;
|
||||
|
@ -15,10 +15,12 @@
|
||||
#endif
|
||||
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
||||
|
||||
#include "../static_libs/chaiscript_parser.hpp"
|
||||
#include "../static_libs/chaiscript_stdlib.hpp"
|
||||
|
||||
|
||||
|
||||
@ -34,7 +36,7 @@ TEST_CASE("C++11 Lambdas Can Be Registered")
|
||||
|
||||
// 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::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
chai.add(chaiscript::fun([]()->std::string { return "hello"; } ), "f1");
|
||||
|
||||
@ -49,7 +51,7 @@ TEST_CASE("C++11 Lambdas Can Be Registered")
|
||||
// dynamic_object tests
|
||||
TEST_CASE("Dynamic_Object attributes can be shared with C++")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
chai("attr bob::z; def bob::bob() { this.z = 10 }; auto x = bob()");
|
||||
|
||||
@ -77,7 +79,7 @@ TEST_CASE("Dynamic_Object attributes can be shared with C++")
|
||||
TEST_CASE("Function objects can be created from chaiscript functions")
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
chai.eval("def func() { print(\"Hello World\"); } ");
|
||||
|
||||
@ -91,7 +93,7 @@ TEST_CASE("Function objects can be created from chaiscript functions")
|
||||
|
||||
TEST_CASE("ChaiScript can be created and destroyed on heap")
|
||||
{
|
||||
chaiscript::ChaiScript *chai = new chaiscript::ChaiScript();
|
||||
auto *chai = new chaiscript::ChaiScript_Basic(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
delete chai;
|
||||
}
|
||||
|
||||
@ -123,7 +125,7 @@ void arithmetic_conversions_f_func_return(const std::function<unsigned int (unsi
|
||||
|
||||
TEST_CASE("Test automatic arithmetic conversions")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
chai.add(chaiscript::fun(&arithmetic_conversions_f1), "f1");
|
||||
chai.add(chaiscript::fun(&arithmetic_conversions_f2), "f2");
|
||||
@ -164,7 +166,7 @@ TEST_CASE("Test automatic arithmetic conversions")
|
||||
|
||||
TEST_CASE("Generic exception handling with C++")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
try {
|
||||
chai.eval("throw(runtime_error(\"error\"));");
|
||||
@ -177,7 +179,7 @@ TEST_CASE("Generic exception handling with C++")
|
||||
|
||||
TEST_CASE("Throw an int")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
try {
|
||||
chai.eval("throw(1)", chaiscript::exception_specification<int>());
|
||||
@ -189,7 +191,7 @@ TEST_CASE("Throw an int")
|
||||
|
||||
TEST_CASE("Throw int or double")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
try {
|
||||
chai.eval("throw(1.0)", chaiscript::exception_specification<int, double>());
|
||||
@ -201,7 +203,7 @@ TEST_CASE("Throw int or double")
|
||||
|
||||
TEST_CASE("Throw a runtime_error")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
try {
|
||||
chai.eval("throw(runtime_error(\"error\"))", chaiscript::exception_specification<int, double, float, const std::string &, const std::exception &>());
|
||||
@ -221,7 +223,7 @@ TEST_CASE("Throw a runtime_error")
|
||||
|
||||
TEST_CASE("Throw unhandled type")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
try {
|
||||
chai.eval("throw(\"error\")", chaiscript::exception_specification<int, double, float, const std::exception &>());
|
||||
@ -250,7 +252,7 @@ int expected_eval_errors_test_one(const int &)
|
||||
|
||||
TEST_CASE("No unexpected exceptions leak")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(chaiscript::fun(&expected_eval_errors_test_one), "test_fun");
|
||||
|
||||
chai.eval("def guard_fun(i) : i.get_type_info().is_type_arithmetic() {} ");
|
||||
@ -313,7 +315,7 @@ int function_ordering_test_two(int &)
|
||||
|
||||
TEST_CASE("Function ordering")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
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; }");
|
||||
@ -337,7 +339,7 @@ int functor_cast_test_call(const std::function<int (int)> &f, int val)
|
||||
TEST_CASE("Functor cast")
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
chai.add(chaiscript::fun(&functor_cast_test_call), "test_call");
|
||||
|
||||
@ -356,10 +358,10 @@ int set_state_test_myfun()
|
||||
|
||||
TEST_CASE("Set and restore chai state")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
// save the initial state of globals and locals
|
||||
chaiscript::ChaiScript::State firststate = chai.get_state();
|
||||
auto firststate = chai.get_state();
|
||||
std::map<std::string, chaiscript::Boxed_Value> locals = chai.get_locals();
|
||||
|
||||
// add some new globals and locals
|
||||
@ -402,7 +404,7 @@ class Short_Comparison_Test {
|
||||
|
||||
TEST_CASE("Short comparison with int")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(chaiscript::user_type<Short_Comparison_Test>(), "Test");
|
||||
chai.add(chaiscript::constructor<Short_Comparison_Test()>(), "Test");
|
||||
|
||||
@ -425,7 +427,7 @@ class Type_Name_MyClass
|
||||
|
||||
TEST_CASE("Test lookup of type names")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
auto type = chaiscript::user_type<Type_Name_MyClass>();
|
||||
chai.add(type, "MyClass");
|
||||
|
||||
@ -451,13 +453,13 @@ int simultaneous_chaiscript_do_something_else(int i)
|
||||
|
||||
TEST_CASE("Simultaneous ChaiScript tests")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(chaiscript::fun(&simultaneous_chaiscript_do_something), "do_something");
|
||||
chai.add(chaiscript::var(1), "i");
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
chaiscript::ChaiScript chai2;
|
||||
chaiscript::ChaiScript_Basic chai2(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai2.add(chaiscript::fun(&simultaneous_chaiscript_do_something_else), "do_something_else");
|
||||
|
||||
CHECK(chai.eval<int>("do_something(" + std::to_string(i) + ")") == i + 2);
|
||||
@ -506,7 +508,7 @@ TEST_CASE("Utility_Test utility class wrapper")
|
||||
);
|
||||
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(m);
|
||||
|
||||
CHECK(chai.eval<std::string>("auto t = Utility_Test(); t.function2(); ") == "Function2");
|
||||
@ -549,7 +551,7 @@ TEST_CASE("Utility_Test utility class wrapper for enum")
|
||||
);
|
||||
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(m);
|
||||
|
||||
CHECK(chai.eval<Utility_Test_Numbers>("ONE ") == 0);
|
||||
@ -644,7 +646,7 @@ TEST_CASE("Object copy counts")
|
||||
m->add(chaiscript::constructor<Object_Copy_Count_Test(const Object_Copy_Count_Test &)>(), "Object_Copy_Count_Test");
|
||||
m->add(chaiscript::fun(&object_copy_count_create), "create");
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(m);
|
||||
|
||||
chai.eval(" { auto i = create(); } ");
|
||||
@ -700,7 +702,7 @@ TEST_CASE("Object lifetime tests")
|
||||
m->add(chaiscript::constructor<Object_Lifetime_Test(const Object_Lifetime_Test &)>(), "Object_Lifetime_Test");
|
||||
m->add(chaiscript::fun(&Object_Lifetime_Test::count), "count");
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(m);
|
||||
|
||||
CHECK(chai.eval<int>("count()") == 0);
|
||||
@ -753,7 +755,7 @@ Object_Lifetime_Vector2<float> Object_Lifetime_Vector2_GetValue()
|
||||
|
||||
TEST_CASE("Object lifetime test 2")
|
||||
{
|
||||
chaiscript::ChaiScript _script;
|
||||
chaiscript::ChaiScript_Basic _script(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
//Registering stuff
|
||||
_script.add(chaiscript::user_type<Object_Lifetime_Vector2<float>>(), "Object_Lifetime_Vector2f");
|
||||
@ -794,7 +796,7 @@ int myfunction(Non_Poly_Base *)
|
||||
|
||||
TEST_CASE("Test Derived->Base with non-polymorphic classes")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(chaiscript::base_class<Non_Poly_Base, Non_Poly_Derived>());
|
||||
Non_Poly_Derived d;
|
||||
chai.add(chaiscript::var(&d), "d");
|
||||
@ -813,7 +815,7 @@ struct TestCppVariableScope
|
||||
|
||||
TEST_CASE("Variable Scope When Calling From C++")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(chaiscript::user_type<TestCppVariableScope>(), "Test");
|
||||
chai.add(chaiscript::constructor<TestCppVariableScope()>(), "Test");
|
||||
chai.add(chaiscript::fun(&TestCppVariableScope::print), "print");
|
||||
@ -836,7 +838,7 @@ TEST_CASE("Variable Scope When Calling From C++")
|
||||
|
||||
TEST_CASE("Variable Scope When Calling From C++ 2")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.eval("var obj = 2;");
|
||||
auto func = chai.eval<std::function<void()>>("fun(){ return obj; }");
|
||||
CHECK_THROWS(func());
|
||||
@ -853,7 +855,7 @@ void longlong(long long i) {
|
||||
|
||||
TEST_CASE("Test long long dispatch")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(chaiscript::fun(&longlong), "longlong");
|
||||
chai.add(chaiscript::fun(&ulonglong), "ulonglong");
|
||||
chai.eval("longlong(15)");
|
||||
@ -873,7 +875,7 @@ struct Returned_Converted_Config
|
||||
|
||||
TEST_CASE("Return of converted type from script")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
|
||||
chai.add(chaiscript::constructor<Returned_Converted_Config ()>(), "Returned_Converted_Config");
|
||||
chai.add(chaiscript::fun(&Returned_Converted_Config::num_iterations), "num_iterations");
|
||||
@ -921,7 +923,7 @@ int get_value_a(const std::map<std::string, int> &t_m)
|
||||
|
||||
TEST_CASE("Map conversions")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
chai.add(chaiscript::map_conversion<std::map<std::string, int>>());
|
||||
chai.add(chaiscript::fun(&get_value_a), "get_value_a");
|
||||
|
||||
@ -941,7 +943,7 @@ TEST_CASE("Parse floats with non-posix locale")
|
||||
#else
|
||||
std::setlocale(LC_ALL, "en_ZA.utf8");
|
||||
#endif
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
const double parsed = chai.eval<double>("print(1.3); 1.3");
|
||||
CHECK(parsed == Approx(1.3));
|
||||
const std::string str = chai.eval<std::string>("to_string(1.3)");
|
||||
|
@ -1,4 +1,7 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
#include "../static_libs/chaiscript_parser.hpp"
|
||||
#include "../static_libs/chaiscript_stdlib.hpp"
|
||||
|
||||
|
||||
#define TEST_LITERAL(v) test_literal(v, #v)
|
||||
|
||||
@ -6,7 +9,7 @@ template<typename T>
|
||||
bool test_literal(T val, const std::string &str)
|
||||
{
|
||||
std::cout << "Comparing : " << val;
|
||||
chaiscript::ChaiScript chai;
|
||||
chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser());
|
||||
T val2 = chai.eval<T>(str);
|
||||
std::cout << " " << val2 << '\n';
|
||||
return val == val2;
|
||||
|
@ -1,14 +1,16 @@
|
||||
#include "multifile_test_chai.hpp"
|
||||
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
#include <chaiscript/language/chaiscript_parser.hpp>
|
||||
|
||||
Multi_Test_Chai::Multi_Test_Chai()
|
||||
: m_chai(new chaiscript::ChaiScript(chaiscript::Std_Lib::library()))
|
||||
: m_chai(new chaiscript::ChaiScript_Basic(chaiscript::Std_Lib::library(),
|
||||
std::make_unique<chaiscript::parser::ChaiScript_Parser<chaiscript::eval::Noop_Tracer, chaiscript::optimizer::Optimizer_Default>>()))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
std::shared_ptr<chaiscript::ChaiScript> Multi_Test_Chai::get_chai()
|
||||
std::shared_ptr<chaiscript::ChaiScript_Basic> Multi_Test_Chai::get_chai()
|
||||
{
|
||||
return m_chai;
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
|
||||
class Multi_Test_Chai
|
||||
{
|
||||
public:
|
||||
Multi_Test_Chai();
|
||||
|
||||
std::shared_ptr<chaiscript::ChaiScript> get_chai();
|
||||
std::shared_ptr<chaiscript::ChaiScript_Basic> get_chai();
|
||||
|
||||
private:
|
||||
std::shared_ptr<chaiscript::ChaiScript> m_chai;
|
||||
std::shared_ptr<chaiscript::ChaiScript_Basic> m_chai;
|
||||
};
|
||||
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
#include "multifile_test_chai.hpp"
|
||||
#include "multifile_test_module.hpp"
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
Multi_Test_Chai chaitest;
|
||||
Multi_Test_Module chaimodule;
|
||||
|
||||
std::shared_ptr<chaiscript::ChaiScript> chai = chaitest.get_chai();
|
||||
std::shared_ptr<chaiscript::ChaiScript_Basic> chai = chaitest.get_chai();
|
||||
chai->add(chaimodule.get_module());
|
||||
return chai->eval<int>("get_module_value()");
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
|
||||
#include "multifile_test_module.hpp"
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
|
||||
class Multi_Test_Module
|
||||
{
|
||||
|
@ -2,7 +2,8 @@
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_basic.hpp>
|
||||
#include <chaiscript/language/chaiscript_parser.hpp>
|
||||
|
||||
int expected_value(int num_iters)
|
||||
{
|
||||
@ -62,7 +63,12 @@ int main()
|
||||
modulepaths.push_back(modulepath);
|
||||
}
|
||||
|
||||
chaiscript::ChaiScript chai(modulepaths,usepaths);
|
||||
|
||||
// For this test we are going to load the dynamic stdlib
|
||||
// to make sure it continues to work
|
||||
chaiscript::ChaiScript_Basic chai(
|
||||
std::make_unique<chaiscript::parser::ChaiScript_Parser<chaiscript::eval::Noop_Tracer, chaiscript::optimizer::Optimizer_Default>>(),
|
||||
modulepaths,usepaths);
|
||||
|
||||
std::vector<std::shared_ptr<std::thread> > threads;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user