eclipsesource / eclipsesource/J2V8

Export Class Fields to JS

Open
#354 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.6k
Forks
387
PR merge metrics
No merged PRs in 30d

Description

Hi! I wonder, if there is a convenient way to export java-class fields in js. It's essential for my team to manipulate with object properties without using getters and setters. I've managed to do this by creating object wrapper like this
```
public class V8Property
{
private String name;
private V8Object context;

private V8JavaFieldProxy(@NonNull V8Object context, @NonNull String name)
{
this.context = context.twin();
this.name = name;
this.context.add(name, (V8Value) null);
}

public void set(TYPE value)
{
if (value instanceof String)
{
context.add(name, (String) value);
}
// .....
}

public TYPE get()
{
return (TYPE) context.get(name);
}
}
```
I wrap the property with it (V8Property) and put some annotation that denotes that this property must be exported to js and than check and export it via reflection. There is an issue with the runtime, though. If I set or get property from an initial thread, all works fine, from the other - not so much. Acquiring locker
```
public void set(TYPE value)
{
if(context.getRuntime().getLocker().tryAcquire())
{
if (value instanceof String)
{
context.add(name, (String) value);
}
// .....
context.getRuntime().getLocker().release();
}
}
```
cause random crushes some time later on acquiring it again. So, is there any other way of doing it?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.