Skip to content

Commit

Permalink
Allocate a local JVM frame when binding interface methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn Zurbrigg committed Sep 5, 2017
1 parent a131410 commit 53c6d20
Show file tree
Hide file tree
Showing 4 changed files with 413 additions and 19 deletions.
46 changes: 46 additions & 0 deletions duktape/src/main/jni/LocalFrame.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2017 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DUKTAPE_ANDROID_LOCALFRAME_H
#define DUKTAPE_ANDROID_LOCALFRAME_H

#include <jni.h>

/**
* RAII wrapper that allocates a new local reference frame for the JVM and releases it when leaving
* scope.
*/
struct LocalFrame {
LocalFrame(JNIEnv* env, std::size_t capacity)
: m_env(*env) {
if (m_env.PushLocalFrame(capacity)) {
// Out of memory.
throw std::bad_alloc();
}
}

~LocalFrame() {
m_env.PopLocalFrame(nullptr);
}

// No copying allowed.
LocalFrame(const LocalFrame&) = delete;
LocalFrame& operator=(const LocalFrame&) = delete;

private:
JNIEnv& m_env;
};

#endif //DUKTAPE_ANDROID_LOCALFRAME_H
21 changes: 3 additions & 18 deletions duktape/src/main/jni/java/JavaMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "GlobalRef.h"
#include "JavaExceptions.h"
#include "JavaType.h"
#include "../LocalFrame.h"

JavaMethod::JavaMethod(JavaTypeMap& typeMap, JNIEnv* env, jobject method) {
jclass methodClass = env->GetObjectClass(method);
Expand All @@ -32,6 +33,8 @@ JavaMethod::JavaMethod(JavaTypeMap& typeMap, JNIEnv* env, jobject method) {
jobjectArray parameterTypes =
static_cast<jobjectArray>(env->CallObjectMethod(method, getParameterTypes));
const jsize numArgs = env->GetArrayLength(parameterTypes);
// Release any local objects allocated in this frame when we leave this scope.
const LocalFrame localFrame(env, numArgs);
m_argumentLoaders.resize(numArgs);
for (jsize i = 0; i < numArgs; ++i) {
auto parameterType = env->GetObjectArrayElement(parameterTypes, i);
Expand All @@ -57,24 +60,6 @@ JavaMethod::JavaMethod(JavaTypeMap& typeMap, JNIEnv* env, jobject method) {
};
}

namespace {

struct LocalFrame {
LocalFrame(JNIEnv* env, std::size_t capacity)
: m_env(*env) {
m_env.PushLocalFrame(capacity);
}

~LocalFrame() {
m_env.PopLocalFrame(nullptr);
}

private:
JNIEnv& m_env;
};

} // anonymous namespace

duk_ret_t JavaMethod::invoke(duk_context* ctx, JNIEnv* env, jobject javaThis) const {
const auto argCount = duk_get_top(ctx);
const auto minArgs = m_isVarArgs
Expand Down
4 changes: 3 additions & 1 deletion duktape/src/main/jni/javascript/JavaScriptObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "../java/JavaExceptions.h"
#include "../java/JavaType.h"
#include "../StackChecker.h"
#include "../LocalFrame.h"

namespace {

Expand Down Expand Up @@ -227,7 +228,8 @@ JavaScriptObject::MethodBody buildMethodBody(JavaTypeMap& typeMap, JNIEnv* env,
jobjectArray parameterTypes =
static_cast<jobjectArray>(env->CallObjectMethod(method, getParameterTypes));
const jsize numArgs = env->GetArrayLength(parameterTypes);

// Release any local objects allocated in this frame when we leave this scope.
const LocalFrame localFrame(env, numArgs);
std::vector<const JavaType*> argumentLoaders(numArgs);
for (jsize i = 0; i < numArgs; ++i) {
auto parameterType = env->GetObjectArrayElement(parameterTypes, i);
Expand Down

0 comments on commit 53c6d20

Please sign in to comment.