Skip to content

Is it possible somehow to "recover" object instance from PlatformObject #424

Open
@fluorumlabs

Description

@fluorumlabs

Let's say I have

Integer x = 5;
PlatformObject nativeObj = Platform.getPlatformObject(x);
// pass nativeObj via some 3rd party JS library
Integer y = (Integer)nativeObj;
Integer z = y + 1;

The last line will fail with $intValue is not a function, because Integer is not a final class and, therefore, TeaVM is not sure which method to call (which is logical), so it's considering intValue a virtual method for this instance. Is it possible somehow to "fake" TeaVM into thinking that (Integer)nativeObj is a concrete instance of Integer?

So far the only thing that comes to mind is to add a new "dummy" empty constructor via class transformer to all classes (which does nothing), then do something like

Integer[] tempArray = new Integer[](1);
tempArray[0] = /* inject instance creation and a call to dummy constructor of Integer */;
/* overwrite element 0 of tempArray with other PlatformObject via JS call */
Integer y = tempArray[0];

Other alternative would be to create via ClassTransformer (again) a constructor taking PlatformObject as an argument, where Object.<init>(PlatformObject that) will call Object.assign(this, that).

Or is there some other better approach?

p.s. TeaVM 0.6.0, platform JS

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions