Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setmetatable for an Object #21

Open
lujiajing1126 opened this issue Jun 24, 2017 · 1 comment
Open

How to setmetatable for an Object #21

lujiajing1126 opened this issue Jun 24, 2017 · 1 comment

Comments

@lujiajing1126
Copy link

lujiajing1126 commented Jun 24, 2017

I want override the default toString in both lua and js.

So normally I could do that in JavaScript with the Object.setPrototypeOf function, which finally call Object.defineProperty after babel-transformation, to change the proto chain .

However, I found that castl defines the Object.defineProperty with rawset, and also does not expose the setmetatable function.

So I'd like to ask how can I make use of the custom toString and __tostring in both lua and javascript

@lujiajing1126
Copy link
Author

An example code would be like this:

const util = require("util") // also as a custom runtime-lib for lua

const version = {
	major: 1,
	minor: 2,
	patch: 2
}

Object.setPrototypeOf(version, {
	toString() {
		return util.format("%d.%d.%d%s", this.major, this.minor, this.patch, this.pre_release ? this.pre_release : "")
	}
})

module.exports = {
	_NAME: "sample-project",
	_VERSION: "" + version,
	_VERSION_TABLE: version
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant