std::wstring in ChaiScript

438 views Asked by At

I am a beginner for ChaiScript.

I try to use std::wstring type in ChaiScript.

std::string type is works well.

#include <iostream>
#include <chaiscript/chaiscript.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>

int main()
{
    chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());

    std::cout << chai.eval<std::function<int (std::string)> >(
        "fun(s){"       
        "   if(s==\"aaa\"){"
        "       print(\"1\");"      
        "   }"      
        "   if(s[1]=='b'){"
        "       print(\"2\");"
        "   }"
        "   return 3;"
        "}"
    )(std::string("abcd")); 
}

D:\TestWork\test_chaiscript>t1.exe
2
3

std::wstring type raises an exception.

#include <iostream>
#include <chaiscript/chaiscript.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>

int main()
{
    chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());

    std::cout << chai.eval<std::function<int (std::wstring)> >(
        "fun(s){"       
        "   if(s==\"aaa\"){"
        "       print(\"1\");"      
        "   }"      
        "   if(s[1]=='b'){"
        "       print(\"2\");"
        "   }"
        "   return 3;"
        "}"
    )(std::wstring(L"abcd"));
}

D:\TestWork\test_chaiscript>t2.exe
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
what(): Error: "Can not find appropriate '==' operator." With parameters: (NSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEEE, const string)

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

ChaiScript git version and mingw-w64 builds 5.1.0 are used.

g++ t2.cpp -std=c++14 -O2 -Os -Wall -mthreads -o t2.exe -DWIN32 -D_WIN32 -DUNICODE -D_UNICODE -Id:\myprj\chaiscript_git\include -static-libstdc++ -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic

It is same in vs2013.

How can I use std::wstring type in ChaiScript?

1

There are 1 answers

0
lefticus On

std::wstring is just like any other type in ChaiScript - you have to tell ChaiScript how you want to use it.

In this case, you are trying to perform an == comparison, but have not provided ChaiScript with a operator==(const std::wstring &, const std::wstring &) function for it to use.

The easiest way to do this would be to use the existing code for std::string and expose all of the same functionality.

for example:

#include <chaiscript/dispatchkit/bootstrap_stl.hpp>

chai.add(chaiscript::bootstrap::standard_library::string_type<std::wstring>("wstring"));

For reference, see the bootstrap.hpp code that initializes std::string.