1 /* 2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #import "apple_applescript_AppleScriptEngine.h" 27 #import "apple_applescript_AppleScriptEngineFactory.h" 28 29 // Must include this before JavaNativeFoundation.h to get jni.h from build 30 #include "jni.h" 31 #include "jni_util.h" 32 33 #import <JavaNativeFoundation/JavaNativeFoundation.h> 34 35 #import "NS_Java_ConversionUtils.h" 36 #import "AppleScriptExecutionContext.h" 37 38 //#define DEBUG 1 39 40 /* 41 * Declare library specific JNI_Onload entry if static build 42 */ 43 DEF_STATIC_JNI_OnLoad 44 45 /* 46 * Class: apple_applescript_AppleScriptEngineFactory 47 * Method: initNative 48 * Signature: ()V 49 */ 50 JNIEXPORT void JNICALL Java_apple_applescript_AppleScriptEngineFactory_initNative 51 (JNIEnv *env, jclass clazz) 52 { 53 return; 54 } 55 56 57 /* 58 * Class: apple_applescript_AppleScriptEngine 59 * Method: initNative 60 * Signature: ()V 61 */ 62 JNIEXPORT void JNICALL Java_apple_applescript_AppleScriptEngine_initNative 63 (JNIEnv *env, jclass clazz) 64 { 65 return; 66 } 67 68 69 /* 70 * Class: apple_applescript_AppleScriptEngine 71 * Method: createContextFrom 72 * Signature: (Ljava/lang/Object;)J 73 */ 74 JNIEXPORT jlong JNICALL Java_apple_applescript_AppleScriptEngine_createContextFrom 75 (JNIEnv *env, jclass clazz, jobject javaContext) 76 { 77 NSObject *obj = nil; 78 79 JNF_COCOA_ENTER(env); 80 81 obj = [[JavaAppleScriptEngineCoercion coercer] coerceJavaObject:javaContext withEnv:env]; 82 83 #ifdef DEBUG 84 NSLog(@"converted context: %@", obj); 85 #endif 86 87 CFRetain(obj); 88 89 JNF_COCOA_EXIT(env); 90 91 return ptr_to_jlong(obj); 92 } 93 94 95 /* 96 * Class: apple_applescript_AppleScriptEngine 97 * Method: createObjectFrom 98 * Signature: (J)Ljava/lang/Object; 99 */ 100 JNIEXPORT jobject JNICALL Java_apple_applescript_AppleScriptEngine_createObjectFrom 101 (JNIEnv *env, jclass clazz, jlong nativeContext) 102 { 103 jobject obj = NULL; 104 105 JNF_COCOA_ENTER(env); 106 107 obj = [[JavaAppleScriptEngineCoercion coercer] coerceNSObject:(id)jlong_to_ptr(nativeContext) withEnv:env]; 108 109 JNF_COCOA_EXIT(env); 110 111 return obj; 112 } 113 114 115 /* 116 * Class: apple_applescript_AppleScriptEngine 117 * Method: disposeContext 118 * Signature: (J)V 119 */ 120 JNIEXPORT void JNICALL Java_apple_applescript_AppleScriptEngine_disposeContext 121 (JNIEnv *env, jclass clazz, jlong nativeContext) 122 { 123 124 JNF_COCOA_ENTER(env); 125 126 id obj = (id)jlong_to_ptr(nativeContext); 127 if (obj != nil) CFRelease(obj); 128 129 JNF_COCOA_EXIT(env); 130 131 } 132 133 134 /* 135 * Class: apple_applescript_AppleScriptEngine 136 * Method: evalScript 137 * Signature: (Ljava/lang/String;J)J 138 */ 139 JNIEXPORT jlong JNICALL Java_apple_applescript_AppleScriptEngine_evalScript 140 (JNIEnv *env, jclass clazz, jstring ascript, jlong contextptr) 141 { 142 id retval = nil; 143 144 JNF_COCOA_ENTER(env); 145 146 NSDictionary *ncontext = jlong_to_ptr(contextptr); 147 NSString *source = JNFJavaToNSString(env, ascript); 148 149 #ifdef DEBUG 150 NSLog(@"evalScript(source:\"%@\" context: %@)", source, ncontext); 151 #endif 152 153 AppleScriptExecutionContext *scriptInvocationCtx = [[[AppleScriptExecutionContext alloc] initWithSource:source context:ncontext] autorelease]; 154 retval = [scriptInvocationCtx invokeWithEnv:env]; 155 156 #ifdef DEBUG 157 NSLog(@"returning: %@", retval); 158 #endif 159 160 if (retval) CFRetain(retval); 161 162 JNF_COCOA_EXIT(env); 163 164 return ptr_to_jlong(retval); 165 } 166 167 168 /* 169 * Class: apple_applescript_AppleScriptEngine 170 * Method: evalScriptFromURL 171 * Signature: (Ljava/lang/String;J)J 172 */ 173 JNIEXPORT jlong JNICALL Java_apple_applescript_AppleScriptEngine_evalScriptFromURL 174 (JNIEnv *env, jclass clazz, jstring afilename, jlong contextptr) 175 { 176 id retval = nil; 177 178 JNF_COCOA_ENTER(env); 179 180 NSDictionary *ncontext = jlong_to_ptr(contextptr); 181 NSString *filename = JNFJavaToNSString(env, afilename); 182 183 #ifdef DEBUG 184 NSLog(@"evalScript(filename:\"%@\" context: %@)", filename, ncontext); 185 #endif 186 187 AppleScriptExecutionContext *scriptInvocationCtx = [[[AppleScriptExecutionContext alloc] initWithFile:filename context:ncontext] autorelease]; 188 retval = [scriptInvocationCtx invokeWithEnv:env]; 189 190 #ifdef DEBUG 191 NSLog(@"returning: %@", retval); 192 #endif 193 194 if (retval) CFRetain(retval); 195 196 JNF_COCOA_EXIT(env); 197 198 return ptr_to_jlong(retval); 199 }