Skip to content

Binding a free function as a non-static member function #20

Description

@OverShifted

I'm trying to bind a free function, as a non-static member function of a foreign class.
Here is my Wren code:

foreign class MyData {
	foreign value
	foreign value=(rhs)

	foreign twiceValue

	toString { "MyData(%(value))" }
}

// Some where else
class Player {
	// ...
	static puts(obj) {
		System.print("Putting: %(obj)")
		System.print("Putting twice: %(obj.twiceValue)")
	}
}

And here is my C++ code (module names are handled correctly):

class MyData
{
public:
	double value;
};

double twiceValue(MyData& self)
{
	return self.value * 2;
}

// *m_VM is a thin wrapper around `wrenpp::WrenVM` which provides `beginModule` as a wrapper around wrenpp`s version of it.
m_VM->beginModule("src/wren/lib/components")
	.bindClass<MyData>("MyData")
		.bindGetter<decltype(MyData::value), &MyData::value>("value")
		.bindSetter<decltype(MyData::value), &MyData::value>("value=(_)")
		.bindMethod<decltype(twiceValue), twiceValue>(false, "twiceValue")
	.endClass()
.endModule();

// Loading modules...

m_VM->GetWrenpp().method("src/wren/test", "Player", "puts(_)")(MyData{12});

When I run it, I get:

Putting: MyData(12)

And then it segfaults.
Using a static function (binding twiceValue as static and passing instance as its first parameter) or a member function works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions