Rust Wasm Bindgen returns object but gets a number - rust

today while doing some rust wasm vs js speed benchmarking with wasm-bindgen, I ran into a problem.
I had made a simple struct as you can see here:
I used this struct in a simple function called gimmeDirections
as shown here:
After compiling this into browser javascript, I looked into the .d.ts file that was compiled into it and noticed that the gimmeDirections function returned a number.
even though in the js, it states in the JSDOC that it returned the class of XY which was defined earlier in the compiled code.
here is the class:
export class XY {
static __wrap(ptr) {
const obj = Object.create(XY.prototype);
obj.ptr = ptr;
return obj;
}
free() {
const ptr = this.ptr;
this.ptr = 0;
wasm.__wbg_xy_free(ptr);
}
/**
* #returns {number}
*/
get x() {
var ret = wasm.__wbg_get_xy_x(this.ptr);
return ret;
}
/**
* #param {number} arg0
*/
set x(arg0) {
wasm.__wbg_set_xy_x(this.ptr, arg0);
}
/**
* #returns {number}
*/
get y() {
var ret = wasm.__wbg_get_xy_y(this.ptr);
return ret;
}
/**
* #param {number} arg0
*/
set y(arg0) {
wasm.__wbg_set_xy_y(this.ptr, arg0);
}
}
after being very confused, due to the fact of how the typescript said it would return a number but the js said it would return a class, I decided to run it... and got a number back.
The object below is my javascript function running identical code for the benchmark, as you can see, I got an object, not a number.
Here is my JS code:
import * as funcs from './wasm/wildz.js';
// compiled wasm js file
function directionsJS(x, y) {
let xX = x;
let yY = y;
if (Math.abs(xX) === Math.abs(yY)) {
xX /= Math.SQRT2;
yY /= Math.SQRT2;
}
return {
x: x,
y: yY
};
}
(async() => {
const game = await funcs.default();
console.time('Rust Result'); console.log(game.gimmeDirections(10, 10));
console.timeEnd('Rust Result'); console.time('JS Result');
console.log(directionsJS(10, 10)); console.timeEnd('JS Result');
})();
I'm still very confused on why it's returning a number when clearly I'm returning a object. Help is much needed, and appreciated

Much of this and more is explained in Exporting a struct to JS in the wasm-bindgen guide, but I'll summarize.
Rust structs are "returned" by allocating space for them dynamically and returning a pointer to it. What you're seeing, in regards to the function returning number, is the "raw" ffi function that binds the JS runtime and wasm module. It just returns that pointer value.
The generated XY Javascript class is a wrapper around that pointer value and provides functions for interacting with it. The generated gimmeDirections function is a wrapper around that wasm module call that creates that class.

Related

Javascript. What is the functional equivalent for class constructors?

Class constructor initializes and create objects/functions in a class. If I'm using functions, how would I initialize a function within the function?
This is the simple class
export default class MainProcess{
constructor() {
this.isReady = false
this.init()
}
init() {
this.setupApplicationMenu()
this.initWindowManager()
this.getIcons()
}
}
how can I initiate a MainPRocess Function?
While I'm not entirely sure I understand the question, I think you're asking "How can I create a function in a way that's similar to how I'm used to writing classes, but without using the class keyword?" Here's an example of that:
function Example () {
this.value = 10;
// instance method
this.print = function () {
console.log(this.value);
}
}
// static method
Example.printHello = function () {
console.log('hello world');
}
const example1 = new Example();
example1.print(); // 10
example1.value = 20;
example1.print(); //20
console.log(Object.getPrototypeOf(example1).constructor.name); // "Example"
const example2 = new Example();
example2. print(); //10
Example.printHello(); // "hello world"
Functions are part of the class. Classes are a grouping of functions(methods) and data(as properties). These functions are used to modify properties.
In the above example, you created a class MainProcess with some functions. However, functions defined in the init method is not present. The compiler will throw an error.
constructor is a special method used to create an object with that class.
If I'm using functions, how would I initialize a function within the
function?
It seems you are mixing two concepts function constructors in JS and Classes which are introduced later. Class is nothing, but a syntactic sugar on function constructor. JS is a prototype-based language.
difference b/w function and function constructor?
Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was created. This is different from using Global_Objects/eval with code for a function expression.
var x = 10;
function createFunction1() {
var x = 20;
return new Function('return x;'); // this |x| refers global |x|
}
function createFunction2() {
var x = 20;
function f() {
return x; // this |x| refers local |x| above
}
return f;
}
var f1 = createFunction1();
console.log(f1()); // 10
var f2 = createFunction2();
console.log(f2()); // 20
I highly recommend you first understand the how JS has implemented class in it.

Create new this on method call

This may be a stupid question, but is it possible to create a new this on a method call of a class?
E.g:
const foo = new Foo();
console.log(foo.a(1).b(2));
// for example, outputs 3 (1+2)
// the a method will create a new namespace and attach 1 to it, and b will use that new namespace
console.log(foo.b(2));
// this will result in an error, as there is no new namespace from the a method anymore, so b cannot add to anything?
Maybe this is too hard to understand, sorry.
class Foo {
a(number) {
this.a = number;
return this;
}
b(number) {
return this.a + number;
}
}
This would be the code where it uses the same this variable - this doesn't fit what I wanted but is what I currently have.
// pseudo
class Foo {
a(number) {
const uniqueVariable = number
return uniqueVariable
// it'll somehow pass the number from this method to the next method
}
// where it can be used with the second method's input
b(uniqueVariable, number) {
return uniqueVariable + number
}
}
foo.a(1).b(2) = 3
This example would obviously cause an error because the return value of a() a number, not something to use a method on again.
Please let me know if I need to explain further -- I'm having some struggle explaining it properly.
If the intention is that foo.a(1).b(2) changes foo, or if you don't mind changing foo, the other answers here work.
But if you only want foo.a(1).b(2) to return 3 without modifying foo, then you need to return a new Foo.
Now, if you really hell bent on having console.log() print 3 rather than something like Foo { value: 3 }, you can also customize inspect() (given that the question is tagged with node.js).
All together:
const util = require('util');
class Foo {
constructor(value) {
this.value = value || 0;
}
add(value) {
return new Foo(this.value + value);
}
a(value) {
return this.add(value);
}
b(value) {
return this.add(value);
}
[util.inspect.custom]() {
return this.value;
}
}
const foo = new Foo();
console.log(foo);
console.log(foo.a(2).b(1));
console.log(foo);
Output:
0
3
0
On my solution, I decided to create two variables to hold the values of each method. (https://jsbin.com/wozuyefebu/edit?js,console)
The a() method will return a number if the isSingle parameter is set to true. If not, it will return the this object, allowing you to chain the b() method. This is might be a hack but I believe it solves your problem.
I write about Javascript and web development on my blog :) https://changani.me/blog
class Foo {
constructor() {
this.aValue = 0;
this.bValue = 0;
}
/**
* #param {Number} value
* #param {Boolean} isSingle
* #returns {Object/Number}
*/
a(value = 0, isSingle = false) {
this.aValue = value;
return isSingle ? this.aValue : this;
}
/**
* #param {Number} value
* #returns {Number}
*/
b(value = 0) {
this.bValue = this.aValue + value;
return this.bValue;
}
}
const x = new Foo();
console.log("Should return 3: ", x.a(2).b(1));
console.log("Should return an 2: ", x.a(2, true));
console.log("Should return an instance of the object: ", x.a(2));
console.log("Should return 1: ", x.b(1));
console.log("Should return 0: ", x.a().b());
(https://jsbin.com/wozuyefebu/edit?js,console)
If you want to be able to invoke methods on return value of methods, then, you should return this from those methods. However, you will need an additional method, say value() to actuall get the result of sum.
A possible way is show below.
class Foo {
_a = 0;
_b = 0;
a(number) {
this._a = number;
return this;
}
b(number) {
this._b = number;
return this;
}
value() {
return this._a + this._b;
}
}
const foo = new Foo();
console.log(foo.a(1).b(2).value());
console.log(foo.b(5).value());

duktape how to parse argument of type String Object (similarly Number object) in duktape c function

How to type check the String object/Number object argument types in duktape c function and parse the value from String object/Number object. There is generic api like duk_is_object() but I need the correct object type to parse the value .
ex:
ecmascript code
var str1 = new String("duktape");
var version = new Number(2.2);
dukFunPrintArgs(str1,str2);
duktape c function :
dukFunPrintArgs(ctx)
{
// code to know whether the args is of type String Object / Number Object
}
Where did you find the information how to register a C function in duktape? That place certainly also has details on how to access the parameters passed to it. Already on the homepage of duktape.org you can find a Getting Started example:
3 Add C function bindings
To call a C function from Ecmascript code, first declare your C functions:
/* Being an embeddable engine, Duktape doesn't provide I/O
* bindings by default. Here's a simple one argument print()
* function.
*/
static duk_ret_t native_print(duk_context *ctx) {
printf("%s\n", duk_to_string(ctx, 0));
return 0; /* no return value (= undefined) */
}
/* Adder: add argument values. */
static duk_ret_t native_adder(duk_context *ctx) {
int i;
int n = duk_get_top(ctx); /* #args */
double res = 0.0;
for (i = 0; i < n; i++) {
res += duk_to_number(ctx, i);
}
duk_push_number(ctx, res);
return 1; /* one return value */
}
Register your functions e.g. into the global object:
duk_push_c_function(ctx, native_print, 1 /*nargs*/);
duk_put_global_string(ctx, "print");
duk_push_c_function(ctx, native_adder, DUK_VARARGS);
duk_put_global_string(ctx, "adder");
You can then call your function from Ecmascript code:
duk_eval_string_noresult(ctx, "print('2+3=' + adder(2, 3));");
One of the core concepts in duktape are stacks. The value stack is where parameters are stored. Read more on the Getting Started page.

Unable to convert "cocos2d::CCPoint" to "cocos2d::CCPoint"

I am newbie in C++ and cocos2d-x, so i do not understand why it's wrong.
Code
void
MainLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
// Get any touch and convert the touch position to in-game position.
CCTouch* touch = (CCTouch*)pTouches->anyObject();
CCPoint position = touch->locationInView();
position = CCDirector::sharedDirector()->convertToGL(position);
__pShip->setMove(position);
}
This is code of function.
Ship::setMove(CCPoint *newPosition)
{
__move=*newPosition;
}
As you can see it uses CCPoint type as parameter, but it fails with position
Header:
class Ship : public AnimatedObject
{
public:
Ship();
bool init(const char* frameName, CCSpriteBatchNode* pSpriteSheet);
void setMove(CCPoint* newPosition);
void move();
private:
/**
* A CCMoveTo action set in the move() method.
*/
cocos2d::CCFiniteTimeAction* __pMoveAction;
/**
* This value specifies the ship's speed.
*/
float __speed;
/**
* This value specifies position to which the ship should move.
* It's set in touch events callbacks in MainLayer class.
*/
CCPoint __move;
};
What am I doing wrong? Why this code fails to converting CCPoints?
Use:
__pShip->setMove(&position); // Address-of
Or change the function itself:
Ship::setMove(CCPoint newPosition) // better: const CCPoint& newPosition
{
__move = newPosition;
}
If CCPoint is a small class, use by value (as shown), or if it is larger (copy is expensive), use the commented prototype.

Excluding non-relevant parameters when using PyArg_ParseTupleAndKeywords

In an embedded python scenario we are using PyArg_ParseTupleAndKeywords to receive data from Python (version >=3 .x) and use it in a C++ application.
At the moment we have a similar setup:
PyObject* whatever(PyObject *self, PyObject *args, PyObject *keywds) {
....
static char* kwlist[] = { "foo", "bar", NULL };
...
if(!PyArg_ParseTupleAndKeywords(args, keywds, ..., kwlist, ...))
{
...bail out
however, if we pass more parameters than the two expected (e.g. issuing a python call like whatever(foo="a", bar="b", baz="c")) the whole thing crashes (not really, it returns an error, but that's beyond the scope here).
We would like to avoid such scenarios; it would be great if we could parse only the parameters in the kwlist and ignore everthing else. What's the best way to do it?
One solution we were thinking about was to convert kwlist into a dict, then manipulate it with PyDict_Merge or the like.
In the end we solved it like below:
(I'm answering my own question since nobody answered, and I think it might be valuable to somebody else in the future).
PyObject* whatever(PyObject *self, PyObject *args, PyObject *incoming_keywds)
{
static char* kwlist[] = { "foo", "bar", NULL };
PyObject* keywds = PyDict_New();
/**
* The following routine returns a subset of the incoming dictionary 'incoming_keywds'
* containing only the keys allowed in the list 'kwlist'
*/
for ( int i = 0 ; kwlist[i] != NULL ; i++ )
{
char* key = kwlist[i];
PyObject *single_key = Py_BuildValue("s", key);
if ( PyDict_Contains(incoming_keywds, single_key) )
{
// not checking for NULL as GetItem return value since
// we already checked above if the dict contains key 'single_key'
if ( PyDict_SetItem(keywds, single_key, PyDict_GetItem(incoming_keywds, single_key)) < 0 )
{
/* error */
}
}
Py_DECREF(single_key);
}
/** end */

Resources