Skip to content

Commit

Permalink
[MultiLang #672] Review comments fix: Java script stub generation iss…
Browse files Browse the repository at this point in the history
…ue fix after migrating to GraalVM RC14
  • Loading branch information
maheshrajus committed Apr 29, 2019
1 parent 5320d36 commit ddc4523
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
39 changes: 20 additions & 19 deletions core/src/main/java/amino/run/compiler/GraalStubGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private String getClassName() {
return prototype.getMetaObject().toString();
}

public void generateStub() throws FileNotFoundException {
public void generateStub() throws FileNotFoundException, NoSuchMethodException {
String className = getClassName();
String functions = generateFunctions();
String code =
Expand Down Expand Up @@ -310,35 +310,36 @@ private String convertFunctionName(String functionName) {
return functionName;
}

private String generateFunctions() {
private String generateFunctions() throws NoSuchMethodException {
StringBuilder res = new StringBuilder();
String className = getClassName();

// TODO: this is workaround for the issue:https://github.com/oracle/graal/issues/678.
// This will be deleted once the above issue is fixed.
// TODO: For js files, the function "GetMemberKeys" should implement and it should return
// all functions inside the class.
// TODO: For js files, the function "getMemberKeys" should implement and it should return
// all functions and instance variables inside the class.
if (lang.equals("js")) {
Value val = prototype.getMember("GetMemberKeys");
Value val = prototype.getMember("getMemberKeys");
if (val != null) {
Map<String, String> allMethods =
prototype.getMember("GetMemberKeys").execute().as(Map.class);
prototype.getMember("getMemberKeys").execute().as(Map.class);
for (String functionName : allMethods.values()) {
String convertFunctionName = convertFunctionName(functionName);
String function =
String.format(
functionStringFormat,
convertFunctionName,
functionName,
packageName,
className,
convertFunctionName);
res.append(function);
if (prototype.getMember(functionName).canExecute()) {
String convertFunctionName = convertFunctionName(functionName);
String function =
String.format(
functionStringFormat,
convertFunctionName,
functionName,
packageName,
className,
convertFunctionName);
res.append(function);
}
}
} else {
logger.log(
Level.SEVERE,
String.format(" %s.js should have GetMemberKeys function", className));
throw new NoSuchMethodException(
className + ".js" + " must have getMemberKeys() function");
}
}

Expand Down
25 changes: 25 additions & 0 deletions core/src/test/resources/student.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,29 @@ class Student {
getBuddies() {
return this.buddies;
}

//TODO: This is workaround for the issue: https://github.com/oracle/graal/issues/678.
//This will be deleted once the above issue is fixed.
//It will return all functions and instance variables in this class except constructor.
getMemberKeys() {
let obj = new Student();
let property = [];
while (obj = Reflect.getPrototypeOf(obj)) {
//the below condition is used to avoid 'Object' functions like "toString,isPrototypeOf,propertyIsEnumerable,toLocaleString" etc
if (obj != Object.prototype) {
let keys = Reflect.ownKeys(obj)
keys.forEach((k) => {
if (k !== "constructor") {
property.push(k)
}
});
}
}

//Fill the instance variables
Object.getOwnPropertyNames(this).forEach((k) => {
property.push(k)
});
return property;
}
}
17 changes: 11 additions & 6 deletions examples/kvstorejs/src/main/js/amino/run/appdemo/KeyValueStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,27 @@ class KeyValueStore {

//TODO: This is workaround for the issue: https://github.com/oracle/graal/issues/678.
//This will be deleted once the above issue is fixed.
//It will return all functions in this class except constructor.
GetMemberKeys() {
//It will return all functions and instance variables in this class except constructor.
getMemberKeys() {
let obj = new KeyValueStore();
let methods = [];
let property = [];
while (obj = Reflect.getPrototypeOf(obj)) {
//the below condition is used to avoid 'Object' functions like "toString,isPrototypeOf,propertyIsEnumerable,toLocaleString" etc
//the below condition is used to avoid 'Object' functions like "toString,isPrototypeOf,propertyIsEnumerable,toLocaleString" etc
if (obj != Object.prototype) {
let keys = Reflect.ownKeys(obj)
keys.forEach((k) => {
if (k !== "constructor") {
methods.push(k)
property.push(k)
}
});
}
}
return methods;

//Fill the instance variables
Object.getOwnPropertyNames(this).forEach((k) => {
property.push(k)
});
return property;
}
}

Expand Down

0 comments on commit ddc4523

Please sign in to comment.