1 /* 2 * Copyright (c) 2011, 2012, 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 #import <JavaNativeFoundation/JavaNativeFoundation.h> 30 31 #import "NS_Java_ConversionUtils.h" 32 #import "AppleScriptExecutionContext.h" 33 34 //#define DEBUG 1 35 36 37 /* 38 * Class: apple_applescript_AppleScriptEngineFactory 39 * Method: initNative 40 * Signature: ()V 41 */ 42 JNIEXPORT void JNICALL Java_apple_applescript_AppleScriptEngineFactory_initNative 43 (JNIEnv *env, jclass clazz) 44 { 45 return; 46 } 47 48 49 /* 50 * Class: apple_applescript_AppleScriptEngine 51 * Method: initNative 52 * Signature: ()V 53 */ 54 JNIEXPORT void JNICALL Java_apple_applescript_AppleScriptEngine_initNative 55 (JNIEnv *env, jclass clazz) 56 { 57 return; 58 } 59 60 61 /* 62 * Class: apple_applescript_AppleScriptEngine 63 * Method: createContextFrom 64 * Signature: (Ljava/lang/Object;)J 65 */ 66 JNIEXPORT jlong JNICALL Java_apple_applescript_AppleScriptEngine_createContextFrom 67 (JNIEnv *env, jclass clazz, jobject javaContext) 68 { 69 NSObject *obj = nil; 70 71 JNF_COCOA_ENTER(env); 72 73 obj = [[JavaAppleScriptEngineCoercion coercer] coerceJavaObject:javaContext withEnv:env]; 74 75 #ifdef DEBUG 76 NSLog(@"converted context: %@", obj); 77 #endif 78 79 CFRetain(obj); 80 81 JNF_COCOA_EXIT(env); 82 83 return ptr_to_jlong(obj); 84 } 85 86 87 /* 88 * Class: apple_applescript_AppleScriptEngine 89 * Method: createObjectFrom 90 * Signature: (J)Ljava/lang/Object; 91 */ 92 JNIEXPORT jobject JNICALL Java_apple_applescript_AppleScriptEngine_createObjectFrom 93 (JNIEnv *env, jclass clazz, jlong nativeContext) 94 { 95 jobject obj = NULL; 96 97 JNF_COCOA_ENTER(env); 98 99 obj = [[JavaAppleScriptEngineCoercion coercer] coerceNSObject:(id)jlong_to_ptr(nativeContext) withEnv:env]; 100 101 JNF_COCOA_EXIT(env); 102 103 return obj; 104 } 105 106 107 /* 108 * Class: apple_applescript_AppleScriptEngine 109 * Method: disposeContext 110 * Signature: (J)V 111 */ 112 JNIEXPORT void JNICALL Java_apple_applescript_AppleScriptEngine_disposeContext 113 (JNIEnv *env, jclass clazz, jlong nativeContext) 114 { 115 116 JNF_COCOA_ENTER(env); 117 118 id obj = (id)jlong_to_ptr(nativeContext); 119 if (obj != nil) CFRelease(obj); 120 121 JNF_COCOA_EXIT(env); 122 123 } 124 125 126 /* 127 * Class: apple_applescript_AppleScriptEngine 128 * Method: evalScript 129 * Signature: (Ljava/lang/String;J)J 130 */ 131 JNIEXPORT jlong JNICALL Java_apple_applescript_AppleScriptEngine_evalScript 132 (JNIEnv *env, jclass clazz, jstring ascript, jlong contextptr) 133 { 134 id retval = nil; 135 136 JNF_COCOA_ENTER(env); 137 138 NSDictionary *ncontext = jlong_to_ptr(contextptr); 139 NSString *source = JNFJavaToNSString(env, ascript); 140 141 #ifdef DEBUG 142 NSLog(@"evalScript(source:\"%@\" context: %@)", source, ncontext); 143 #endif 144 145 AppleScriptExecutionContext *scriptInvocationCtx = [[[AppleScriptExecutionContext alloc] initWithSource:source context:ncontext] autorelease]; 146 retval = [scriptInvocationCtx invokeWithEnv:env]; 147 148 #ifdef DEBUG 149 NSLog(@"returning: %@", retval); 150 #endif 151 152 if (retval) CFRetain(retval); 153 154 JNF_COCOA_EXIT(env); 155 156 return ptr_to_jlong(retval); 157 } 158 159 160 /* 161 * Class: apple_applescript_AppleScriptEngine 162 * Method: evalScriptFromURL 163 * Signature: (Ljava/lang/String;J)J 164 */ 165 JNIEXPORT jlong JNICALL Java_apple_applescript_AppleScriptEngine_evalScriptFromURL 166 (JNIEnv *env, jclass clazz, jstring afilename, jlong contextptr) 167 { 168 id retval = nil; 169 170 JNF_COCOA_ENTER(env); 171 172 NSDictionary *ncontext = jlong_to_ptr(contextptr); 173 NSString *filename = JNFJavaToNSString(env, afilename); 174 175 #ifdef DEBUG 176 NSLog(@"evalScript(filename:\"%@\" context: %@)", filename, ncontext); 177 #endif 178 179 AppleScriptExecutionContext *scriptInvocationCtx = [[[AppleScriptExecutionContext alloc] initWithFile:filename context:ncontext] autorelease]; 180 retval = [scriptInvocationCtx invokeWithEnv:env]; 181 182 #ifdef DEBUG 183 NSLog(@"returning: %@", retval); 184 #endif 185 186 if (retval) CFRetain(retval); 187 188 JNF_COCOA_EXIT(env); 189 190 return ptr_to_jlong(retval); 191 }