Skip to content

Commit a453fdc

Browse files
committed
Preventing assignment to readonly properties of function.
1 parent ca96156 commit a453fdc

3 files changed

Lines changed: 25 additions & 5 deletions

File tree

lib/nodeJavaBridge.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ java.onJvmCreated = function() {
179179
var MODIFIER_PUBLIC = 1;
180180
var MODIFIER_STATIC = 8;
181181

182+
function isWritable(obj, prop) {
183+
if (!prop || !obj) {
184+
return false;
185+
}
186+
187+
// If the property has no descriptor, or wasn't explicitly marked as not writable,
188+
// assume it is.
189+
return (Object.getOwnPropertyDescriptor(obj, prop) || {}).writable !== false;
190+
}
182191

183192
java.import = function(name) {
184193
var clazz = java.findClassSync(name); // TODO: change to Class.forName when classloader issue is resolved.
@@ -223,12 +232,20 @@ java.import = function(name) {
223232
if (((modifiers & MODIFIER_PUBLIC) === MODIFIER_PUBLIC)
224233
&& ((modifiers & MODIFIER_STATIC) === MODIFIER_STATIC)) {
225234
var methodName = SyncCall(methods[i], 'getName')();
226-
result[methodName + syncSuffix] = java.callStaticMethodSync.bind(java, name, methodName);
227-
if (typeof asyncSuffix === 'string') {
228-
result[methodName + asyncSuffix] = java.callStaticMethod.bind(java, name, methodName);
235+
var syncName = methodName + syncSuffix;
236+
var asyncName = methodName + asyncSuffix;
237+
var promiseName = methodName + promiseSuffix;
238+
239+
if (isWritable(result, syncName)) {
240+
result[syncName] = java.callStaticMethodSync.bind(java, name, methodName);
229241
}
230-
if (promisify) {
231-
result[methodName + promiseSuffix] = promisify(java.callStaticMethod.bind(java, name, methodName));
242+
243+
if (typeof asyncSuffix === 'string' && isWritable(result, asyncName)) {
244+
result[asyncName] = java.callStaticMethod.bind(java, name, methodName);
245+
}
246+
247+
if (promisify && isWritable(result, promiseName)) {
248+
result[promiseName] = promisify(java.callStaticMethod.bind(java, name, methodName));
232249
}
233250
}
234251
}

test/Test.class

46 Bytes
Binary file not shown.

test/Test.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,7 @@ public static enum StaticEnum {
171171
public static String varArgsSignature(Long... args) { return "Long..."; }
172172
public static String varArgsSignature(Number... args) { return "Number..."; }
173173
public static String varArgsSignature(String... args) { return "String..."; }
174+
// Test readonly properties on functions that throw errors in certain versions of
175+
// node.
176+
public static void name() {}
174177
}

0 commit comments

Comments
 (0)