From ddc4523fd74ec31590bb49838f5967210c9ec865 Mon Sep 17 00:00:00 2001 From: maheshrajus Date: Mon, 29 Apr 2019 14:43:43 +0530 Subject: [PATCH] [MultiLang #672] Review comments fix: Java script stub generation issue fix after migrating to GraalVM RC14 --- .../run/compiler/GraalStubGenerator.java | 39 ++++++++++--------- core/src/test/resources/student.js | 25 ++++++++++++ .../js/amino/run/appdemo/KeyValueStore.js | 17 +++++--- 3 files changed, 56 insertions(+), 25 deletions(-) diff --git a/core/src/main/java/amino/run/compiler/GraalStubGenerator.java b/core/src/main/java/amino/run/compiler/GraalStubGenerator.java index 40d453542..45127ec38 100755 --- a/core/src/main/java/amino/run/compiler/GraalStubGenerator.java +++ b/core/src/main/java/amino/run/compiler/GraalStubGenerator.java @@ -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 = @@ -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 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"); } } diff --git a/core/src/test/resources/student.js b/core/src/test/resources/student.js index c8288bf1a..cf3b42393 100644 --- a/core/src/test/resources/student.js +++ b/core/src/test/resources/student.js @@ -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; + } } diff --git a/examples/kvstorejs/src/main/js/amino/run/appdemo/KeyValueStore.js b/examples/kvstorejs/src/main/js/amino/run/appdemo/KeyValueStore.js index da6f5abff..2214421c9 100644 --- a/examples/kvstorejs/src/main/js/amino/run/appdemo/KeyValueStore.js +++ b/examples/kvstorejs/src/main/js/amino/run/appdemo/KeyValueStore.js @@ -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; } }