Skip to content

Function define inside chaiscript and arguments cast #303

Description

@lucas-sarie
  • Compiler Used: MSVC13
  • Operating System: Windows 7
  • Architecture (ARM/x86/32bit/64bit/etc): 64bit

There is a different of treatment with arguments casting between c++ function called with chaiscript, and chaiscript function.
With C++ there is a boxed_cast for each argument in order to make match provided arguments and required arguments. (call_func in proxy_functions_detail.hpp:219)
With Chaiscript function there isn't any boxed_cast so if argument doesn't perfectly match an eval_error is raised (match in proxy_functions.hpp:110)

Here is an example :


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

#include <iostream>


class A
{
public:
	A() {}
	virtual ~A() {}
};

class B : public A
{
public:
	B() : A() {}
	virtual ~B() {}
};

int main(int ac, char** av)
{
	chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());

	//-------------------------------------------------------------------------

	chai.add(chaiscript::user_type<A>(), "A");

	chai.add(chaiscript::user_type<B>(), "B");
	chai.add(chaiscript::base_class<A, B>());

	chai.add(chaiscript::fun([](const B * test)
	{
		std::cout << "ok" << std::endl;
	}), "CppFunctWithBArg");

	chai.add(chaiscript::fun([]() -> A*
	{
		return ((A*) new B());
	}), "Create");

	try
	{
		chai.eval(R"(
		var inst = Create() // A*

		// it prints "A"
		inst.type_name().print() 

		// Ok it is casted using conversion
		CppFunctWithBArg(inst)

		// Define a function with B as argument
		def ChaiFuncWithBArg(B inst)
		{
			print("ok")
		}
	
		// don't work
		ChaiFuncWithBArg(inst)
		)");
	}
	catch (chaiscript::exception::eval_error e)
	{
		std::cout << e.what() << std::endl;
		std::cout << e.pretty_print() << std::endl;
	}
	catch (std::exception e)
	{
		std::cout << e.what() << std::endl;
	}
}

Output is :

A
ok
Error: "Guard evaluation failed with function 'ChaiFuncWithBArg'"
Error: "Guard evaluation failed with function 'ChaiFuncWithBArg'" during evaluation at (__EVAL__ 17, 3)

  __EVAL__ (17, 3) 'ChaiFuncWithBArg(inst)'

Thank you for reading !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions