| OLD | NEW |
| 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library old_servicec.plugins.java; | 5 library old_servicec.plugins.java; |
| 6 | 6 |
| 7 import 'dart:core' hide Type; | 7 import 'dart:core' hide Type; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart' show | 10 import 'package:path/path.dart' show |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 const HEADER_MK = """ | 31 const HEADER_MK = """ |
| 32 # Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 32 # Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file |
| 33 # for details. All rights reserved. Use of this source code is governed by a | 33 # for details. All rights reserved. Use of this source code is governed by a |
| 34 # BSD-style license that can be found in the LICENSE.md file. | 34 # BSD-style license that can be found in the LICENSE.md file. |
| 35 | 35 |
| 36 # Generated file. Do not edit. | 36 # Generated file. Do not edit. |
| 37 """; | 37 """; |
| 38 | 38 |
| 39 const READER_HEADER = """ | 39 const READER_HEADER = """ |
| 40 package fletch; | 40 package dartino; |
| 41 """; | 41 """; |
| 42 | 42 |
| 43 const FLETCH_API_JAVA = """ | 43 const DARTINO_API_JAVA = """ |
| 44 package fletch; | 44 package dartino; |
| 45 | 45 |
| 46 public class FletchApi { | 46 public class DartinoApi { |
| 47 public static abstract class PrintInterceptor { | 47 public static abstract class PrintInterceptor { |
| 48 public abstract void Out(String message); | 48 public abstract void Out(String message); |
| 49 public abstract void Error(String message); | 49 public abstract void Error(String message); |
| 50 private long nativePtr = 0; | 50 private long nativePtr = 0; |
| 51 } | 51 } |
| 52 public static native void Setup(); | 52 public static native void Setup(); |
| 53 public static native void TearDown(); | 53 public static native void TearDown(); |
| 54 public static native void RunSnapshot(byte[] snapshot); | 54 public static native void RunSnapshot(byte[] snapshot); |
| 55 public static native void WaitForDebuggerConnection(int port); | 55 public static native void WaitForDebuggerConnection(int port); |
| 56 public static native void AddDefaultSharedLibrary(String library); | 56 public static native void AddDefaultSharedLibrary(String library); |
| 57 public static native void RegisterPrintInterceptor(PrintInterceptor intercepto
r); | 57 public static native void RegisterPrintInterceptor(PrintInterceptor intercepto
r); |
| 58 public static native void UnregisterPrintInterceptor(PrintInterceptor intercep
tor); | 58 public static native void UnregisterPrintInterceptor(PrintInterceptor intercep
tor); |
| 59 } | 59 } |
| 60 """; | 60 """; |
| 61 | 61 |
| 62 const FLETCH_SERVICE_API_JAVA = """ | 62 const DARTINO_SERVICE_API_JAVA = """ |
| 63 package fletch; | 63 package dartino; |
| 64 | 64 |
| 65 public class FletchServiceApi { | 65 public class DartinoServiceApi { |
| 66 public static native void Setup(); | 66 public static native void Setup(); |
| 67 public static native void TearDown(); | 67 public static native void TearDown(); |
| 68 } | 68 } |
| 69 """; | 69 """; |
| 70 | 70 |
| 71 const FLETCH_API_JAVA_IMPL = """ | 71 const DARTINO_API_JAVA_IMPL = """ |
| 72 #include <jni.h> | 72 #include <jni.h> |
| 73 #include <stdlib.h> | 73 #include <stdlib.h> |
| 74 | 74 |
| 75 #include "fletch_api.h" | 75 #include "dartino_api.h" |
| 76 | 76 |
| 77 #ifdef ANDROID | 77 #ifdef ANDROID |
| 78 typedef JNIEnv* AttachEnvType; | 78 typedef JNIEnv* AttachEnvType; |
| 79 #else | 79 #else |
| 80 typedef void* AttachEnvType; | 80 typedef void* AttachEnvType; |
| 81 #endif | 81 #endif |
| 82 | 82 |
| 83 static JNIEnv* AttachCurrentThreadAndGetEnv(JavaVM* vm) { | 83 static JNIEnv* AttachCurrentThreadAndGetEnv(JavaVM* vm) { |
| 84 AttachEnvType result = NULL; | 84 AttachEnvType result = NULL; |
| 85 if (vm->AttachCurrentThread(&result, NULL) != JNI_OK) { | 85 if (vm->AttachCurrentThread(&result, NULL) != JNI_OK) { |
| 86 // TODO(zerny): Nicer error recovery? | 86 // TODO(zerny): Nicer error recovery? |
| 87 exit(1); | 87 exit(1); |
| 88 } | 88 } |
| 89 return reinterpret_cast<JNIEnv*>(result); | 89 return reinterpret_cast<JNIEnv*>(result); |
| 90 } | 90 } |
| 91 | 91 |
| 92 class JNIPrintInterceptorInfo { | 92 class JNIPrintInterceptorInfo { |
| 93 public: | 93 public: |
| 94 JNIPrintInterceptorInfo(JavaVM* vm, jobject obj) | 94 JNIPrintInterceptorInfo(JavaVM* vm, jobject obj) |
| 95 : vm(vm), obj(obj), interceptor(NULL) {} | 95 : vm(vm), obj(obj), interceptor(NULL) {} |
| 96 JavaVM* vm; | 96 JavaVM* vm; |
| 97 jobject obj; | 97 jobject obj; |
| 98 FletchPrintInterceptor interceptor; | 98 DartinoPrintInterceptor interceptor; |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 static void JNIPrintInterceptorFunction( | 101 static void JNIPrintInterceptorFunction( |
| 102 const char* message, int out, void* raw) { | 102 const char* message, int out, void* raw) { |
| 103 JNIPrintInterceptorInfo* info = | 103 JNIPrintInterceptorInfo* info = |
| 104 reinterpret_cast<JNIPrintInterceptorInfo*>(raw); | 104 reinterpret_cast<JNIPrintInterceptorInfo*>(raw); |
| 105 JNIEnv* env = AttachCurrentThreadAndGetEnv(info->vm); | 105 JNIEnv* env = AttachCurrentThreadAndGetEnv(info->vm); |
| 106 jobject obj = info->obj; | 106 jobject obj = info->obj; |
| 107 jclass clazz = env->GetObjectClass(obj); | 107 jclass clazz = env->GetObjectClass(obj); |
| 108 jmethodID method = NULL; | 108 jmethodID method = NULL; |
| 109 const char* methodName = (out == 2) ? "Out" : (out == 3) ? "Error" : NULL; | 109 const char* methodName = (out == 2) ? "Out" : (out == 3) ? "Error" : NULL; |
| 110 const char* methodSig = "(Ljava/lang/String;)V"; | 110 const char* methodSig = "(Ljava/lang/String;)V"; |
| 111 if (methodName == NULL) exit(1); | 111 if (methodName == NULL) exit(1); |
| 112 method = env->GetMethodID(clazz, methodName, methodSig); | 112 method = env->GetMethodID(clazz, methodName, methodSig); |
| 113 jstring argument = env->NewStringUTF(message); | 113 jstring argument = env->NewStringUTF(message); |
| 114 env->CallVoidMethod(obj, method, argument); | 114 env->CallVoidMethod(obj, method, argument); |
| 115 info->vm->DetachCurrentThread(); | 115 info->vm->DetachCurrentThread(); |
| 116 } | 116 } |
| 117 | 117 |
| 118 static void RegisterPrintInterceptor(JNIEnv* env, jobject obj) { | 118 static void RegisterPrintInterceptor(JNIEnv* env, jobject obj) { |
| 119 // TODO(zerny): Associate the Java object and native object in a map. | 119 // TODO(zerny): Associate the Java object and native object in a map. |
| 120 jclass clazz = env->GetObjectClass(obj); | 120 jclass clazz = env->GetObjectClass(obj); |
| 121 jfieldID nativePtr = env->GetFieldID(clazz, "nativePtr", "J"); | 121 jfieldID nativePtr = env->GetFieldID(clazz, "nativePtr", "J"); |
| 122 jlong nativePtrValue = env->GetLongField(obj, nativePtr); | 122 jlong nativePtrValue = env->GetLongField(obj, nativePtr); |
| 123 if (nativePtrValue != 0) return; | 123 if (nativePtrValue != 0) return; |
| 124 obj = env->NewGlobalRef(obj); | 124 obj = env->NewGlobalRef(obj); |
| 125 JavaVM* vm = NULL; | 125 JavaVM* vm = NULL; |
| 126 env->GetJavaVM(&vm); | 126 env->GetJavaVM(&vm); |
| 127 JNIPrintInterceptorInfo* info = new JNIPrintInterceptorInfo(vm, obj); | 127 JNIPrintInterceptorInfo* info = new JNIPrintInterceptorInfo(vm, obj); |
| 128 info->interceptor = FletchRegisterPrintInterceptor( | 128 info->interceptor = DartinoRegisterPrintInterceptor( |
| 129 JNIPrintInterceptorFunction, reinterpret_cast<void*>(info)); | 129 JNIPrintInterceptorFunction, reinterpret_cast<void*>(info)); |
| 130 env->SetLongField(obj, nativePtr, reinterpret_cast<jlong>(info)); | 130 env->SetLongField(obj, nativePtr, reinterpret_cast<jlong>(info)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 static void UnregisterPrintInterceptor(JNIEnv* env, jobject obj) { | 133 static void UnregisterPrintInterceptor(JNIEnv* env, jobject obj) { |
| 134 // TODO(zerny): Retrieve the native object from the Java object via a map. | 134 // TODO(zerny): Retrieve the native object from the Java object via a map. |
| 135 jclass clazz = env->GetObjectClass(obj); | 135 jclass clazz = env->GetObjectClass(obj); |
| 136 jfieldID nativePtr = env->GetFieldID(clazz, "nativePtr", "J"); | 136 jfieldID nativePtr = env->GetFieldID(clazz, "nativePtr", "J"); |
| 137 jlong nativePtrValue = env->GetLongField(obj, nativePtr); | 137 jlong nativePtrValue = env->GetLongField(obj, nativePtr); |
| 138 if (nativePtrValue == 0) return; | 138 if (nativePtrValue == 0) return; |
| 139 env->SetLongField(obj, nativePtr, 0); | 139 env->SetLongField(obj, nativePtr, 0); |
| 140 JNIPrintInterceptorInfo* info = | 140 JNIPrintInterceptorInfo* info = |
| 141 reinterpret_cast<JNIPrintInterceptorInfo*>(nativePtrValue); | 141 reinterpret_cast<JNIPrintInterceptorInfo*>(nativePtrValue); |
| 142 FletchUnregisterPrintInterceptor(info->interceptor); | 142 DartinoUnregisterPrintInterceptor(info->interceptor); |
| 143 env->DeleteGlobalRef(info->obj); | 143 env->DeleteGlobalRef(info->obj); |
| 144 delete info; | 144 delete info; |
| 145 } | 145 } |
| 146 | 146 |
| 147 #ifdef __cplusplus | 147 #ifdef __cplusplus |
| 148 extern "C" { | 148 extern "C" { |
| 149 #endif | 149 #endif |
| 150 | 150 |
| 151 JNIEXPORT void JNICALL Java_fletch_FletchApi_Setup(JNIEnv*, jclass) { | 151 JNIEXPORT void JNICALL Java_dartino_DartinoApi_Setup(JNIEnv*, jclass) { |
| 152 FletchSetup(); | 152 DartinoSetup(); |
| 153 } | 153 } |
| 154 | 154 |
| 155 JNIEXPORT void JNICALL Java_fletch_FletchApi_TearDown(JNIEnv*, jclass) { | 155 JNIEXPORT void JNICALL Java_dartino_DartinoApi_TearDown(JNIEnv*, jclass) { |
| 156 FletchTearDown(); | 156 DartinoTearDown(); |
| 157 } | 157 } |
| 158 | 158 |
| 159 JNIEXPORT void JNICALL Java_fletch_FletchApi_RunSnapshot(JNIEnv* env, | 159 JNIEXPORT void JNICALL Java_dartino_DartinoApi_RunSnapshot(JNIEnv* env, |
| 160 jclass, | 160 jclass, |
| 161 jbyteArray snapshot) { | 161 jbyteArray snapshot) { |
| 162 int len = env->GetArrayLength(snapshot); | 162 int len = env->GetArrayLength(snapshot); |
| 163 unsigned char* copy = new unsigned char[len]; | 163 unsigned char* copy = new unsigned char[len]; |
| 164 env->GetByteArrayRegion(snapshot, 0, len, reinterpret_cast<jbyte*>(copy)); | 164 env->GetByteArrayRegion(snapshot, 0, len, reinterpret_cast<jbyte*>(copy)); |
| 165 FletchProgram program = FletchLoadSnapshot(copy, len); | 165 DartinoProgram program = DartinoLoadSnapshot(copy, len); |
| 166 delete[] copy; | 166 delete[] copy; |
| 167 FletchRunMain(program); | 167 DartinoRunMain(program); |
| 168 FletchDeleteProgram(program); | 168 DartinoDeleteProgram(program); |
| 169 } | 169 } |
| 170 | 170 |
| 171 JNIEXPORT void JNICALL Java_fletch_FletchApi_WaitForDebuggerConnection( | 171 JNIEXPORT void JNICALL Java_dartino_DartinoApi_WaitForDebuggerConnection( |
| 172 JNIEnv* env, jclass, int port) { | 172 JNIEnv* env, jclass, int port) { |
| 173 FletchWaitForDebuggerConnection(port); | 173 DartinoWaitForDebuggerConnection(port); |
| 174 } | 174 } |
| 175 | 175 |
| 176 JNIEXPORT void JNICALL Java_fletch_FletchApi_AddDefaultSharedLibrary( | 176 JNIEXPORT void JNICALL Java_dartino_DartinoApi_AddDefaultSharedLibrary( |
| 177 JNIEnv* env, jclass, jstring str) { | 177 JNIEnv* env, jclass, jstring str) { |
| 178 const char* library = env->GetStringUTFChars(str, 0); | 178 const char* library = env->GetStringUTFChars(str, 0); |
| 179 FletchAddDefaultSharedLibrary(library); | 179 DartinoAddDefaultSharedLibrary(library); |
| 180 env->ReleaseStringUTFChars(str, library); | 180 env->ReleaseStringUTFChars(str, library); |
| 181 } | 181 } |
| 182 | 182 |
| 183 JNIEXPORT void JNICALL Java_fletch_FletchApi_RegisterPrintInterceptor( | 183 JNIEXPORT void JNICALL Java_dartino_DartinoApi_RegisterPrintInterceptor( |
| 184 JNIEnv* env, jclass, jobject obj) { | 184 JNIEnv* env, jclass, jobject obj) { |
| 185 RegisterPrintInterceptor(env, obj); | 185 RegisterPrintInterceptor(env, obj); |
| 186 } | 186 } |
| 187 | 187 |
| 188 JNIEXPORT void JNICALL Java_fletch_FletchApi_UnregisterPrintInterceptor( | 188 JNIEXPORT void JNICALL Java_dartino_DartinoApi_UnregisterPrintInterceptor( |
| 189 JNIEnv* env, jclass, jobject obj) { | 189 JNIEnv* env, jclass, jobject obj) { |
| 190 UnregisterPrintInterceptor(env, obj); | 190 UnregisterPrintInterceptor(env, obj); |
| 191 } | 191 } |
| 192 | 192 |
| 193 #ifdef __cplusplus | 193 #ifdef __cplusplus |
| 194 } | 194 } |
| 195 #endif | 195 #endif |
| 196 """; | 196 """; |
| 197 | 197 |
| 198 const FLETCH_SERVICE_API_JAVA_IMPL = """ | 198 const DARTINO_SERVICE_API_JAVA_IMPL = """ |
| 199 #include <jni.h> | 199 #include <jni.h> |
| 200 | 200 |
| 201 #include "service_api.h" | 201 #include "service_api.h" |
| 202 | 202 |
| 203 #ifdef __cplusplus | 203 #ifdef __cplusplus |
| 204 extern "C" { | 204 extern "C" { |
| 205 #endif | 205 #endif |
| 206 | 206 |
| 207 JNIEXPORT void JNICALL Java_fletch_FletchServiceApi_Setup(JNIEnv*, jclass) { | 207 JNIEXPORT void JNICALL Java_dartino_DartinoServiceApi_Setup(JNIEnv*, jclass) { |
| 208 ServiceApiSetup(); | 208 ServiceApiSetup(); |
| 209 } | 209 } |
| 210 | 210 |
| 211 JNIEXPORT void JNICALL Java_fletch_FletchServiceApi_TearDown(JNIEnv*, jclass) { | 211 JNIEXPORT void JNICALL Java_dartino_DartinoServiceApi_TearDown(JNIEnv*, jclass)
{ |
| 212 ServiceApiTearDown(); | 212 ServiceApiTearDown(); |
| 213 } | 213 } |
| 214 | 214 |
| 215 #ifdef __cplusplus | 215 #ifdef __cplusplus |
| 216 } | 216 } |
| 217 #endif | 217 #endif |
| 218 """; | 218 """; |
| 219 | 219 |
| 220 String JNI_UTILS = """ | 220 String JNI_UTILS = """ |
| 221 static JNIEnv* AttachCurrentThreadAndGetEnv(JavaVM* vm) { | 221 static JNIEnv* AttachCurrentThreadAndGetEnv(JavaVM* vm) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 "MessageBuilder.java", | 356 "MessageBuilder.java", |
| 357 "MessageReader.java", | 357 "MessageReader.java", |
| 358 "Reader.java", | 358 "Reader.java", |
| 359 "Segment.java" | 359 "Segment.java" |
| 360 ]; | 360 ]; |
| 361 | 361 |
| 362 void generate(String path, | 362 void generate(String path, |
| 363 Unit unit, | 363 Unit unit, |
| 364 String resourcesDirectory, | 364 String resourcesDirectory, |
| 365 String outputDirectory) { | 365 String outputDirectory) { |
| 366 _generateFletchApis(outputDirectory); | 366 _generateDartinoApis(outputDirectory); |
| 367 _generateServiceJava(path, unit, outputDirectory); | 367 _generateServiceJava(path, unit, outputDirectory); |
| 368 _generateServiceJni(path, unit, outputDirectory); | 368 _generateServiceJni(path, unit, outputDirectory); |
| 369 _generateServiceJniMakeFiles(path, unit, resourcesDirectory, outputDirectory); | 369 _generateServiceJniMakeFiles(path, unit, resourcesDirectory, outputDirectory); |
| 370 | 370 |
| 371 resourcesDirectory = join(resourcesDirectory, 'java', 'fletch'); | 371 resourcesDirectory = join(resourcesDirectory, 'java', 'dartino'); |
| 372 String fletchDirectory = join(outputDirectory, 'java', 'fletch'); | 372 String dartinoDirectory = join(outputDirectory, 'java', 'dartino'); |
| 373 for (String resource in JAVA_RESOURCES) { | 373 for (String resource in JAVA_RESOURCES) { |
| 374 String resourcePath = join(resourcesDirectory, resource); | 374 String resourcePath = join(resourcesDirectory, resource); |
| 375 File file = new File(resourcePath); | 375 File file = new File(resourcePath); |
| 376 String contents = file.readAsStringSync(); | 376 String contents = file.readAsStringSync(); |
| 377 writeToFile(fletchDirectory, resource, contents); | 377 writeToFile(dartinoDirectory, resource, contents); |
| 378 } | 378 } |
| 379 } | 379 } |
| 380 | 380 |
| 381 void _generateFletchApis(String outputDirectory) { | 381 void _generateDartinoApis(String outputDirectory) { |
| 382 String fletchDirectory = join(outputDirectory, 'java', 'fletch'); | 382 String dartinoDirectory = join(outputDirectory, 'java', 'dartino'); |
| 383 String jniDirectory = join(outputDirectory, 'java', 'jni'); | 383 String jniDirectory = join(outputDirectory, 'java', 'jni'); |
| 384 | 384 |
| 385 StringBuffer buffer = new StringBuffer(HEADER); | 385 StringBuffer buffer = new StringBuffer(HEADER); |
| 386 buffer.writeln(); | 386 buffer.writeln(); |
| 387 buffer.write(FLETCH_API_JAVA); | 387 buffer.write(DARTINO_API_JAVA); |
| 388 writeToFile(fletchDirectory, 'FletchApi', buffer.toString(), | 388 writeToFile(dartinoDirectory, 'DartinoApi', buffer.toString(), |
| 389 extension: 'java'); | 389 extension: 'java'); |
| 390 | 390 |
| 391 buffer = new StringBuffer(HEADER); | 391 buffer = new StringBuffer(HEADER); |
| 392 buffer.writeln(); | 392 buffer.writeln(); |
| 393 buffer.write(FLETCH_SERVICE_API_JAVA); | 393 buffer.write(DARTINO_SERVICE_API_JAVA); |
| 394 writeToFile(fletchDirectory, 'FletchServiceApi', buffer.toString(), | 394 writeToFile(dartinoDirectory, 'DartinoServiceApi', buffer.toString(), |
| 395 extension: 'java'); | 395 extension: 'java'); |
| 396 | 396 |
| 397 buffer = new StringBuffer(HEADER); | 397 buffer = new StringBuffer(HEADER); |
| 398 buffer.writeln(); | 398 buffer.writeln(); |
| 399 buffer.write(FLETCH_API_JAVA_IMPL); | 399 buffer.write(DARTINO_API_JAVA_IMPL); |
| 400 writeToFile(jniDirectory, 'fletch_api_wrapper', buffer.toString(), | 400 writeToFile(jniDirectory, 'dartino_api_wrapper', buffer.toString(), |
| 401 extension: 'cc'); | 401 extension: 'cc'); |
| 402 | 402 |
| 403 buffer = new StringBuffer(HEADER); | 403 buffer = new StringBuffer(HEADER); |
| 404 buffer.writeln(); | 404 buffer.writeln(); |
| 405 buffer.write(FLETCH_SERVICE_API_JAVA_IMPL); | 405 buffer.write(DARTINO_SERVICE_API_JAVA_IMPL); |
| 406 writeToFile(jniDirectory, 'fletch_service_api_wrapper', | 406 writeToFile(jniDirectory, 'dartino_service_api_wrapper', |
| 407 buffer.toString(), extension: 'cc'); | 407 buffer.toString(), extension: 'cc'); |
| 408 } | 408 } |
| 409 | 409 |
| 410 void _generateServiceJava(String path, Unit unit, String outputDirectory) { | 410 void _generateServiceJava(String path, Unit unit, String outputDirectory) { |
| 411 _JavaVisitor visitor = new _JavaVisitor(path, outputDirectory); | 411 _JavaVisitor visitor = new _JavaVisitor(path, outputDirectory); |
| 412 visitor.visit(unit); | 412 visitor.visit(unit); |
| 413 String contents = visitor.buffer.toString(); | 413 String contents = visitor.buffer.toString(); |
| 414 String directory = join(outputDirectory, 'java', 'fletch'); | 414 String directory = join(outputDirectory, 'java', 'dartino'); |
| 415 // TODO(ager): We should generate a file per service here. | 415 // TODO(ager): We should generate a file per service here. |
| 416 if (unit.services.length > 1) { | 416 if (unit.services.length > 1) { |
| 417 print('Java plugin: multiple services in one file is not supported.'); | 417 print('Java plugin: multiple services in one file is not supported.'); |
| 418 } | 418 } |
| 419 String serviceName = unit.services.first.name; | 419 String serviceName = unit.services.first.name; |
| 420 writeToFile(directory, serviceName, contents, extension: 'java'); | 420 writeToFile(directory, serviceName, contents, extension: 'java'); |
| 421 } | 421 } |
| 422 | 422 |
| 423 void _generateServiceJni(String path, Unit unit, String outputDirectory) { | 423 void _generateServiceJni(String path, Unit unit, String outputDirectory) { |
| 424 _JniVisitor visitor = new _JniVisitor(path); | 424 _JniVisitor visitor = new _JniVisitor(path); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 void writeReturnTypeToBuffer(Type node, StringBuffer buffer) { | 557 void writeReturnTypeToBuffer(Type node, StringBuffer buffer) { |
| 558 buffer.write(getReturnType(node)); | 558 buffer.write(getReturnType(node)); |
| 559 } | 559 } |
| 560 | 560 |
| 561 void writeListTypeToBuffer(Type node, StringBuffer buffer) { | 561 void writeListTypeToBuffer(Type node, StringBuffer buffer) { |
| 562 buffer.write(getListType(node)); | 562 buffer.write(getListType(node)); |
| 563 } | 563 } |
| 564 | 564 |
| 565 visitUnit(Unit node) { | 565 visitUnit(Unit node) { |
| 566 writeln(HEADER); | 566 writeln(HEADER); |
| 567 writeln('package fletch;'); | 567 writeln('package dartino;'); |
| 568 writeln(); | 568 writeln(); |
| 569 node.structs.forEach(visit); | 569 node.structs.forEach(visit); |
| 570 node.services.forEach(visit); | 570 node.services.forEach(visit); |
| 571 neededListTypes.forEach(writeListReaderImplementation); | 571 neededListTypes.forEach(writeListReaderImplementation); |
| 572 neededListTypes.forEach(writeListBuilderImplementation); | 572 neededListTypes.forEach(writeListBuilderImplementation); |
| 573 } | 573 } |
| 574 | 574 |
| 575 visitService(Service node) { | 575 visitService(Service node) { |
| 576 writeln('public class ${node.name} {'); | 576 writeln('public class ${node.name} {'); |
| 577 writeln(' public static native void Setup();'); | 577 writeln(' public static native void Setup();'); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 writeType(node.type); | 618 writeType(node.type); |
| 619 write(' ${node.name}'); | 619 write(' ${node.name}'); |
| 620 } | 620 } |
| 621 | 621 |
| 622 visitStruct(Struct node) { | 622 visitStruct(Struct node) { |
| 623 writeReader(node); | 623 writeReader(node); |
| 624 writeBuilder(node); | 624 writeBuilder(node); |
| 625 } | 625 } |
| 626 | 626 |
| 627 void writeReader(Struct node) { | 627 void writeReader(Struct node) { |
| 628 String fletchDirectory = join(outputDirectory, 'java', 'fletch'); | 628 String dartinoDirectory = join(outputDirectory, 'java', 'dartino'); |
| 629 String name = node.name; | 629 String name = node.name; |
| 630 StructLayout layout = node.layout; | 630 StructLayout layout = node.layout; |
| 631 | 631 |
| 632 StringBuffer buffer = new StringBuffer(HEADER); | 632 StringBuffer buffer = new StringBuffer(HEADER); |
| 633 buffer.writeln(); | 633 buffer.writeln(); |
| 634 buffer.writeln(READER_HEADER); | 634 buffer.writeln(READER_HEADER); |
| 635 | 635 |
| 636 buffer.writeln('import java.util.List;'); | 636 buffer.writeln('import java.util.List;'); |
| 637 buffer.writeln(); | 637 buffer.writeln(); |
| 638 | 638 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 int offset = slot.offset; | 720 int offset = slot.offset; |
| 721 buffer.writeln(' $returnType reader = new $returnType();'); | 721 buffer.writeln(' $returnType reader = new $returnType();'); |
| 722 buffer.writeln(' return ($returnType)readStruct(reader, $offset);')
; | 722 buffer.writeln(' return ($returnType)readStruct(reader, $offset);')
; |
| 723 buffer.writeln(' }'); | 723 buffer.writeln(' }'); |
| 724 } | 724 } |
| 725 } | 725 } |
| 726 } | 726 } |
| 727 | 727 |
| 728 buffer.writeln('}'); | 728 buffer.writeln('}'); |
| 729 | 729 |
| 730 writeToFile(fletchDirectory, '$name', buffer.toString(), | 730 writeToFile(dartinoDirectory, '$name', buffer.toString(), |
| 731 extension: 'java'); | 731 extension: 'java'); |
| 732 } | 732 } |
| 733 | 733 |
| 734 void writeBuilder(Struct node) { | 734 void writeBuilder(Struct node) { |
| 735 String fletchDirectory = join(outputDirectory, 'java', 'fletch'); | 735 String dartinoDirectory = join(outputDirectory, 'java', 'dartino'); |
| 736 String name = '${node.name}Builder'; | 736 String name = '${node.name}Builder'; |
| 737 StructLayout layout = node.layout; | 737 StructLayout layout = node.layout; |
| 738 | 738 |
| 739 StringBuffer buffer = new StringBuffer(HEADER); | 739 StringBuffer buffer = new StringBuffer(HEADER); |
| 740 buffer.writeln(); | 740 buffer.writeln(); |
| 741 buffer.writeln(READER_HEADER); | 741 buffer.writeln(READER_HEADER); |
| 742 | 742 |
| 743 buffer.write('import java.util.List;'); | 743 buffer.write('import java.util.List;'); |
| 744 buffer.writeln(); | 744 buffer.writeln(); |
| 745 buffer.writeln('public class $name extends Builder {'); | 745 buffer.writeln('public class $name extends Builder {'); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 buffer.writeln(' $builderType result = new $builderType();'); | 833 buffer.writeln(' $builderType result = new $builderType();'); |
| 834 buffer.writeln(' newStruct(result, ${slot.offset}, $size);'); | 834 buffer.writeln(' newStruct(result, ${slot.offset}, $size);'); |
| 835 buffer.writeln(' return result;'); | 835 buffer.writeln(' return result;'); |
| 836 } | 836 } |
| 837 buffer.writeln(' }'); | 837 buffer.writeln(' }'); |
| 838 } | 838 } |
| 839 } | 839 } |
| 840 | 840 |
| 841 buffer.writeln('}'); | 841 buffer.writeln('}'); |
| 842 | 842 |
| 843 writeToFile(fletchDirectory, '$name', buffer.toString(), | 843 writeToFile(dartinoDirectory, '$name', buffer.toString(), |
| 844 extension: 'java'); | 844 extension: 'java'); |
| 845 } | 845 } |
| 846 | 846 |
| 847 void writeListReaderImplementation(Type type) { | 847 void writeListReaderImplementation(Type type) { |
| 848 String fletchDirectory = join(outputDirectory, 'java', 'fletch'); | 848 String dartinoDirectory = join(outputDirectory, 'java', 'dartino'); |
| 849 String name = '${camelize(type.identifier)}List'; | 849 String name = '${camelize(type.identifier)}List'; |
| 850 String listType = getListType(type); | 850 String listType = getListType(type); |
| 851 | 851 |
| 852 StringBuffer buffer = new StringBuffer(HEADER); | 852 StringBuffer buffer = new StringBuffer(HEADER); |
| 853 buffer.writeln(); | 853 buffer.writeln(); |
| 854 buffer.writeln(READER_HEADER); | 854 buffer.writeln(READER_HEADER); |
| 855 | 855 |
| 856 buffer.writeln('public class $name {'); | 856 buffer.writeln('public class $name {'); |
| 857 if (type.isPrimitive) { | 857 if (type.isPrimitive) { |
| 858 int elementSize = primitives.size(type.primitiveType); | 858 int elementSize = primitives.size(type.primitiveType); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 884 'result, index, $elementSize);'); | 884 'result, index, $elementSize);'); |
| 885 buffer.writeln(' return result;'); | 885 buffer.writeln(' return result;'); |
| 886 buffer.writeln(' }'); | 886 buffer.writeln(' }'); |
| 887 } | 887 } |
| 888 | 888 |
| 889 buffer.writeln(); | 889 buffer.writeln(); |
| 890 buffer.writeln(' public int size() { return reader.length; }'); | 890 buffer.writeln(' public int size() { return reader.length; }'); |
| 891 | 891 |
| 892 buffer.writeln('}'); | 892 buffer.writeln('}'); |
| 893 | 893 |
| 894 writeToFile(fletchDirectory, '$name', buffer.toString(), | 894 writeToFile(dartinoDirectory, '$name', buffer.toString(), |
| 895 extension: 'java'); | 895 extension: 'java'); |
| 896 } | 896 } |
| 897 | 897 |
| 898 void writeListBuilderImplementation(Type type) { | 898 void writeListBuilderImplementation(Type type) { |
| 899 String fletchDirectory = join(outputDirectory, 'java', 'fletch'); | 899 String dartinoDirectory = join(outputDirectory, 'java', 'dartino'); |
| 900 String name = '${camelize(type.identifier)}ListBuilder'; | 900 String name = '${camelize(type.identifier)}ListBuilder'; |
| 901 | 901 |
| 902 StringBuffer buffer = new StringBuffer(HEADER); | 902 StringBuffer buffer = new StringBuffer(HEADER); |
| 903 buffer.writeln(); | 903 buffer.writeln(); |
| 904 buffer.writeln(READER_HEADER); | 904 buffer.writeln(READER_HEADER); |
| 905 | 905 |
| 906 buffer.writeln('public class $name {'); | 906 buffer.writeln('public class $name {'); |
| 907 buffer.writeln(' private ListBuilder builder;'); | 907 buffer.writeln(' private ListBuilder builder;'); |
| 908 buffer.writeln(); | 908 buffer.writeln(); |
| 909 buffer.writeln(' public $name(ListBuilder builder) {' | 909 buffer.writeln(' public $name(ListBuilder builder) {' |
| (...skipping 29 matching lines...) Expand all Loading... |
| 939 'result, index, $elementSize);'); | 939 'result, index, $elementSize);'); |
| 940 buffer.writeln(' return result;'); | 940 buffer.writeln(' return result;'); |
| 941 buffer.writeln(' }'); | 941 buffer.writeln(' }'); |
| 942 } | 942 } |
| 943 | 943 |
| 944 buffer.writeln(); | 944 buffer.writeln(); |
| 945 buffer.writeln(' public int size() { return builder.length; }'); | 945 buffer.writeln(' public int size() { return builder.length; }'); |
| 946 | 946 |
| 947 buffer.writeln('}'); | 947 buffer.writeln('}'); |
| 948 | 948 |
| 949 writeToFile(fletchDirectory, '$name', buffer.toString(), | 949 writeToFile(dartinoDirectory, '$name', buffer.toString(), |
| 950 extension: 'java'); | 950 extension: 'java'); |
| 951 } | 951 } |
| 952 } | 952 } |
| 953 | 953 |
| 954 class _JniVisitor extends CcVisitor { | 954 class _JniVisitor extends CcVisitor { |
| 955 static const int REQUEST_HEADER_SIZE = 48 + 8; | 955 static const int REQUEST_HEADER_SIZE = 48 + 8; |
| 956 static const int RESPONSE_HEADER_SIZE = 8; | 956 static const int RESPONSE_HEADER_SIZE = 8; |
| 957 | 957 |
| 958 int methodId = 1; | 958 int methodId = 1; |
| 959 String serviceName; | 959 String serviceName; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 985 writeln('#ifdef ANDROID'); | 985 writeln('#ifdef ANDROID'); |
| 986 writeln(' typedef JNIEnv* AttachEnvType;'); | 986 writeln(' typedef JNIEnv* AttachEnvType;'); |
| 987 writeln('#else'); | 987 writeln('#else'); |
| 988 writeln(' typedef void* AttachEnvType;'); | 988 writeln(' typedef void* AttachEnvType;'); |
| 989 writeln('#endif'); | 989 writeln('#endif'); |
| 990 | 990 |
| 991 writeln(); | 991 writeln(); |
| 992 writeln('static ServiceId service_id_ = kNoServiceId;'); | 992 writeln('static ServiceId service_id_ = kNoServiceId;'); |
| 993 | 993 |
| 994 writeln(); | 994 writeln(); |
| 995 write('JNIEXPORT void JNICALL Java_fletch_'); | 995 write('JNIEXPORT void JNICALL Java_dartino_'); |
| 996 writeln('${serviceName}_Setup(JNIEnv*, jclass) {'); | 996 writeln('${serviceName}_Setup(JNIEnv*, jclass) {'); |
| 997 writeln(' service_id_ = ServiceApiLookup("$serviceName");'); | 997 writeln(' service_id_ = ServiceApiLookup("$serviceName");'); |
| 998 writeln('}'); | 998 writeln('}'); |
| 999 | 999 |
| 1000 writeln(); | 1000 writeln(); |
| 1001 write('JNIEXPORT void JNICALL Java_fletch_'); | 1001 write('JNIEXPORT void JNICALL Java_dartino_'); |
| 1002 writeln('${serviceName}_TearDown(JNIEnv*, jclass) {'); | 1002 writeln('${serviceName}_TearDown(JNIEnv*, jclass) {'); |
| 1003 writeln(' ServiceApiTerminate(service_id_);'); | 1003 writeln(' ServiceApiTerminate(service_id_);'); |
| 1004 writeln('}'); | 1004 writeln('}'); |
| 1005 | 1005 |
| 1006 // TODO(ager): Put this in resources and copy as a file instead. | 1006 // TODO(ager): Put this in resources and copy as a file instead. |
| 1007 writeln(); | 1007 writeln(); |
| 1008 writeln(JNI_UTILS); | 1008 writeln(JNI_UTILS); |
| 1009 | 1009 |
| 1010 node.methods.forEach(visit); | 1010 node.methods.forEach(visit); |
| 1011 | 1011 |
| 1012 writeln(); | 1012 writeln(); |
| 1013 writeln('#ifdef __cplusplus'); | 1013 writeln('#ifdef __cplusplus'); |
| 1014 writeln('}'); | 1014 writeln('}'); |
| 1015 writeln('#endif'); | 1015 writeln('#endif'); |
| 1016 } | 1016 } |
| 1017 | 1017 |
| 1018 visitMethod(Method node) { | 1018 visitMethod(Method node) { |
| 1019 String name = node.name; | 1019 String name = node.name; |
| 1020 String id = '_k${name}Id'; | 1020 String id = '_k${name}Id'; |
| 1021 | 1021 |
| 1022 writeln(); | 1022 writeln(); |
| 1023 write('static const MethodId $id = '); | 1023 write('static const MethodId $id = '); |
| 1024 writeln('reinterpret_cast<MethodId>(${methodId++});'); | 1024 writeln('reinterpret_cast<MethodId>(${methodId++});'); |
| 1025 | 1025 |
| 1026 writeln(); | 1026 writeln(); |
| 1027 write('JNIEXPORT '); | 1027 write('JNIEXPORT '); |
| 1028 writeReturnType(node.returnType); | 1028 writeReturnType(node.returnType); |
| 1029 write(' JNICALL Java_fletch_${serviceName}_${name}('); | 1029 write(' JNICALL Java_dartino_${serviceName}_${name}('); |
| 1030 write('JNIEnv* _env, jclass'); | 1030 write('JNIEnv* _env, jclass'); |
| 1031 if (node.arguments.isNotEmpty) write(', '); | 1031 if (node.arguments.isNotEmpty) write(', '); |
| 1032 if (node.inputKind != InputKind.PRIMITIVES) { | 1032 if (node.inputKind != InputKind.PRIMITIVES) { |
| 1033 write('jobject ${node.arguments.single.name}'); | 1033 write('jobject ${node.arguments.single.name}'); |
| 1034 } else { | 1034 } else { |
| 1035 visitArguments(node.arguments); | 1035 visitArguments(node.arguments); |
| 1036 } | 1036 } |
| 1037 writeln(') {'); | 1037 writeln(') {'); |
| 1038 if (node.inputKind != InputKind.PRIMITIVES) { | 1038 if (node.inputKind != InputKind.PRIMITIVES) { |
| 1039 visitStructArgumentMethodBody(id, node); | 1039 visitStructArgumentMethodBody(id, node); |
| 1040 } else { | 1040 } else { |
| 1041 visitMethodBody(id, node); | 1041 visitMethodBody(id, node); |
| 1042 } | 1042 } |
| 1043 writeln('}'); | 1043 writeln('}'); |
| 1044 | 1044 |
| 1045 String callback; | 1045 String callback; |
| 1046 if (node.inputKind == InputKind.STRUCT) { | 1046 if (node.inputKind == InputKind.STRUCT) { |
| 1047 Struct struct = node.arguments.single.type.resolved; | 1047 Struct struct = node.arguments.single.type.resolved; |
| 1048 StructLayout layout = struct.layout; | 1048 StructLayout layout = struct.layout; |
| 1049 callback = ensureCallback(node.returnType, layout); | 1049 callback = ensureCallback(node.returnType, layout); |
| 1050 } else { | 1050 } else { |
| 1051 callback = | 1051 callback = |
| 1052 ensureCallback(node.returnType, node.inputPrimitiveStructLayout); | 1052 ensureCallback(node.returnType, node.inputPrimitiveStructLayout); |
| 1053 } | 1053 } |
| 1054 | 1054 |
| 1055 writeln(); | 1055 writeln(); |
| 1056 write('JNIEXPORT void JNICALL '); | 1056 write('JNIEXPORT void JNICALL '); |
| 1057 write('Java_fletch_${serviceName}_${name}Async('); | 1057 write('Java_dartino_${serviceName}_${name}Async('); |
| 1058 write('JNIEnv* _env, jclass'); | 1058 write('JNIEnv* _env, jclass'); |
| 1059 if (node.arguments.isNotEmpty) write(', '); | 1059 if (node.arguments.isNotEmpty) write(', '); |
| 1060 if (node.inputKind != InputKind.PRIMITIVES) { | 1060 if (node.inputKind != InputKind.PRIMITIVES) { |
| 1061 write('jobject ${node.arguments.single.name}'); | 1061 write('jobject ${node.arguments.single.name}'); |
| 1062 } else { | 1062 } else { |
| 1063 visitArguments(node.arguments); | 1063 visitArguments(node.arguments); |
| 1064 } | 1064 } |
| 1065 writeln(', jobject _callback) {'); | 1065 writeln(', jobject _callback) {'); |
| 1066 writeln(' jobject callback = NULL;'); | 1066 writeln(' jobject callback = NULL;'); |
| 1067 writeln(' JavaVM* vm = NULL;'); | 1067 writeln(' JavaVM* vm = NULL;'); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1134 write(' ServiceApiInvokeAsync(service_id_, $id, $callback, '); | 1134 write(' ServiceApiInvokeAsync(service_id_, $id, $callback, '); |
| 1135 writeln('_buffer, kSize);'); | 1135 writeln('_buffer, kSize);'); |
| 1136 } else { | 1136 } else { |
| 1137 writeln(' ServiceApiInvoke(service_id_, $id, _buffer, kSize);'); | 1137 writeln(' ServiceApiInvoke(service_id_, $id, _buffer, kSize);'); |
| 1138 if (method.outputKind == OutputKind.STRUCT) { | 1138 if (method.outputKind == OutputKind.STRUCT) { |
| 1139 Type type = method.returnType; | 1139 Type type = method.returnType; |
| 1140 writeln(' int64_t result = *${pointerToArgument(0, 0, 'int64_t')};'); | 1140 writeln(' int64_t result = *${pointerToArgument(0, 0, 'int64_t')};'); |
| 1141 writeln(' char* memory = reinterpret_cast<char*>(result);'); | 1141 writeln(' char* memory = reinterpret_cast<char*>(result);'); |
| 1142 writeln(' jobject rootSegment = GetRootSegment(_env, memory);'); | 1142 writeln(' jobject rootSegment = GetRootSegment(_env, memory);'); |
| 1143 writeln(' jclass resultClass = ' | 1143 writeln(' jclass resultClass = ' |
| 1144 '_env->FindClass("fletch/${type.identifier}");'); | 1144 '_env->FindClass("dartino/${type.identifier}");'); |
| 1145 writeln(' jmethodID create = _env->GetStaticMethodID(' | 1145 writeln(' jmethodID create = _env->GetStaticMethodID(' |
| 1146 'resultClass, "create", ' | 1146 'resultClass, "create", ' |
| 1147 '"(Ljava/lang/Object;)Lfletch/${type.identifier};");'); | 1147 '"(Ljava/lang/Object;)Ldartino/${type.identifier};");'); |
| 1148 writeln(' jobject resultObject = _env->CallStaticObjectMethod(' | 1148 writeln(' jobject resultObject = _env->CallStaticObjectMethod(' |
| 1149 'resultClass, create, rootSegment);'); | 1149 'resultClass, create, rootSegment);'); |
| 1150 writeln(' return resultObject;'); | 1150 writeln(' return resultObject;'); |
| 1151 } else if (!method.returnType.isVoid) { | 1151 } else if (!method.returnType.isVoid) { |
| 1152 writeln(' return *${pointerToArgument(0, 0, 'int64_t')};'); | 1152 writeln(' return *${pointerToArgument(0, 0, 'int64_t')};'); |
| 1153 } | 1153 } |
| 1154 } | 1154 } |
| 1155 } | 1155 } |
| 1156 | 1156 |
| 1157 visitStructArgumentMethodBody(String id, | 1157 visitStructArgumentMethodBody(String id, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1183 writeln('buffer, size);'); | 1183 writeln('buffer, size);'); |
| 1184 } else { | 1184 } else { |
| 1185 writeln(' ServiceApiInvoke(service_id_, $id, buffer, size);'); | 1185 writeln(' ServiceApiInvoke(service_id_, $id, buffer, size);'); |
| 1186 writeln(' int64_t result = *${pointerToArgument(0, 0, 'int64_t')};'); | 1186 writeln(' int64_t result = *${pointerToArgument(0, 0, 'int64_t')};'); |
| 1187 writeln(' DeleteMessage(buffer);'); | 1187 writeln(' DeleteMessage(buffer);'); |
| 1188 if (method.outputKind == OutputKind.STRUCT) { | 1188 if (method.outputKind == OutputKind.STRUCT) { |
| 1189 Type type = method.returnType; | 1189 Type type = method.returnType; |
| 1190 writeln(' char* memory = reinterpret_cast<char*>(result);'); | 1190 writeln(' char* memory = reinterpret_cast<char*>(result);'); |
| 1191 writeln(' jobject rootSegment = GetRootSegment(_env, memory);'); | 1191 writeln(' jobject rootSegment = GetRootSegment(_env, memory);'); |
| 1192 writeln(' jclass resultClass = ' | 1192 writeln(' jclass resultClass = ' |
| 1193 '_env->FindClass("fletch/${type.identifier}");'); | 1193 '_env->FindClass("dartino/${type.identifier}");'); |
| 1194 writeln(' jmethodID create = _env->GetStaticMethodID(' | 1194 writeln(' jmethodID create = _env->GetStaticMethodID(' |
| 1195 'resultClass, "create", ' | 1195 'resultClass, "create", ' |
| 1196 '"(Ljava/lang/Object;)Lfletch/${type.identifier};");'); | 1196 '"(Ljava/lang/Object;)Ldartino/${type.identifier};");'); |
| 1197 writeln(' jobject resultObject = _env->CallStaticObjectMethod(' | 1197 writeln(' jobject resultObject = _env->CallStaticObjectMethod(' |
| 1198 'resultClass, create, rootSegment);'); | 1198 'resultClass, create, rootSegment);'); |
| 1199 writeln(' return resultObject;'); | 1199 writeln(' return resultObject;'); |
| 1200 } else { | 1200 } else { |
| 1201 if (!method.returnType.isVoid) writeln(' return result;'); | 1201 if (!method.returnType.isVoid) writeln(' return result;'); |
| 1202 } | 1202 } |
| 1203 } | 1203 } |
| 1204 } | 1204 } |
| 1205 | 1205 |
| 1206 static const Map<String, String> PRIMITIVE_TYPES = const { | 1206 static const Map<String, String> PRIMITIVE_TYPES = const { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1246 'int32' : 'I', | 1246 'int32' : 'I', |
| 1247 'int64' : 'J', | 1247 'int64' : 'J', |
| 1248 | 1248 |
| 1249 'float32' : 'F', | 1249 'float32' : 'F', |
| 1250 'float64' : 'D', | 1250 'float64' : 'D', |
| 1251 }; | 1251 }; |
| 1252 | 1252 |
| 1253 String getJNISignatureType(Type type) { | 1253 String getJNISignatureType(Type type) { |
| 1254 String name = type.identifier; | 1254 String name = type.identifier; |
| 1255 if (type.isPrimitive) return PRIMITIVE_JNI_SIG[name]; | 1255 if (type.isPrimitive) return PRIMITIVE_JNI_SIG[name]; |
| 1256 return 'Lfletch/$name;'; | 1256 return 'Ldartino/$name;'; |
| 1257 } | 1257 } |
| 1258 | 1258 |
| 1259 final Map<String, String> callbacks = {}; | 1259 final Map<String, String> callbacks = {}; |
| 1260 String ensureCallback(Type type, | 1260 String ensureCallback(Type type, |
| 1261 StructLayout layout, | 1261 StructLayout layout, |
| 1262 {bool cStyle: false}) { | 1262 {bool cStyle: false}) { |
| 1263 String key = '${type.identifier}_${layout.size}'; | 1263 String key = '${type.identifier}_${layout.size}'; |
| 1264 return callbacks.putIfAbsent(key, () { | 1264 return callbacks.putIfAbsent(key, () { |
| 1265 String cast(String type) => CcVisitor.cast(type, cStyle); | 1265 String cast(String type) => CcVisitor.cast(type, cStyle); |
| 1266 String pointerToArgument(int offset, String type) { | 1266 String pointerToArgument(int offset, String type) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1283 writeln(' DeleteMessage(buffer);'); | 1283 writeln(' DeleteMessage(buffer);'); |
| 1284 if (!type.isPrimitive) { | 1284 if (!type.isPrimitive) { |
| 1285 writeln(' char* memory = reinterpret_cast<char*>(result);'); | 1285 writeln(' char* memory = reinterpret_cast<char*>(result);'); |
| 1286 writeln(' jobject rootSegment = GetRootSegment(env, memory);'); | 1286 writeln(' jobject rootSegment = GetRootSegment(env, memory);'); |
| 1287 write(' jfieldID returnTypeField = env->GetFieldID('); | 1287 write(' jfieldID returnTypeField = env->GetFieldID('); |
| 1288 writeln('clazz, "returnType", "Ljava/lang/Class;");'); | 1288 writeln('clazz, "returnType", "Ljava/lang/Class;");'); |
| 1289 write(' jclass resultClass = (jclass)'); | 1289 write(' jclass resultClass = (jclass)'); |
| 1290 writeln('env->GetObjectField(info->callback, returnTypeField);'); | 1290 writeln('env->GetObjectField(info->callback, returnTypeField);'); |
| 1291 writeln(' jmethodID create = env->GetStaticMethodID(' | 1291 writeln(' jmethodID create = env->GetStaticMethodID(' |
| 1292 'resultClass, "create", ' | 1292 'resultClass, "create", ' |
| 1293 '"(Ljava/lang/Object;)Lfletch/${type.identifier};");'); | 1293 '"(Ljava/lang/Object;)Ldartino/${type.identifier};");'); |
| 1294 writeln(' jobject resultObject = env->CallStaticObjectMethod(' | 1294 writeln(' jobject resultObject = env->CallStaticObjectMethod(' |
| 1295 'resultClass, create, rootSegment);'); | 1295 'resultClass, create, rootSegment);'); |
| 1296 } | 1296 } |
| 1297 } else { | 1297 } else { |
| 1298 writeln(' DeleteMessage(buffer);'); | 1298 writeln(' DeleteMessage(buffer);'); |
| 1299 } | 1299 } |
| 1300 write(' jmethodID methodId = env->GetMethodID'); | 1300 write(' jmethodID methodId = env->GetMethodID'); |
| 1301 write('(clazz, "handle", '); | 1301 write('(clazz, "handle", '); |
| 1302 if (type.isVoid) { | 1302 if (type.isVoid) { |
| 1303 writeln('"()V");'); | 1303 writeln('"()V");'); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1319 return name; | 1319 return name; |
| 1320 }); | 1320 }); |
| 1321 } | 1321 } |
| 1322 } | 1322 } |
| 1323 | 1323 |
| 1324 void _generateServiceJniMakeFiles(String path, | 1324 void _generateServiceJniMakeFiles(String path, |
| 1325 Unit unit, | 1325 Unit unit, |
| 1326 String resourcesDirectory, | 1326 String resourcesDirectory, |
| 1327 String outputDirectory) { | 1327 String outputDirectory) { |
| 1328 String out = join(outputDirectory, 'java'); | 1328 String out = join(outputDirectory, 'java'); |
| 1329 // TODO(stanm): pass fletch root directly | 1329 // TODO(stanm): pass dartino root directly |
| 1330 String fletchRoot = join(resourcesDirectory, '..', '..', '..', '..', '..'); | 1330 String dartinoRoot = join(resourcesDirectory, '..', '..', '..', '..', '..'); |
| 1331 String fletchLibraryBuildDir = join(fletchRoot, | 1331 String dartinoLibraryBuildDir = join(dartinoRoot, |
| 1332 'tools', | 1332 'tools', |
| 1333 'android_build', | 1333 'android_build', |
| 1334 'jni'); | 1334 'jni'); |
| 1335 | 1335 |
| 1336 String fletchIncludeDir = join(fletchRoot, 'include'); | 1336 String dartinoIncludeDir = join(dartinoRoot, 'include'); |
| 1337 | 1337 |
| 1338 String modulePath = relative(fletchLibraryBuildDir, from: out); | 1338 String modulePath = relative(dartinoLibraryBuildDir, from: out); |
| 1339 String includePath = relative(fletchIncludeDir, from: out); | 1339 String includePath = relative(dartinoIncludeDir, from: out); |
| 1340 | 1340 |
| 1341 StringBuffer buffer = new StringBuffer(HEADER_MK); | 1341 StringBuffer buffer = new StringBuffer(HEADER_MK); |
| 1342 | 1342 |
| 1343 buffer.writeln(); | 1343 buffer.writeln(); |
| 1344 buffer.writeln('LOCAL_PATH := \$(call my-dir)'); | 1344 buffer.writeln('LOCAL_PATH := \$(call my-dir)'); |
| 1345 | 1345 |
| 1346 buffer.writeln(); | 1346 buffer.writeln(); |
| 1347 buffer.writeln('include \$(CLEAR_VARS)'); | 1347 buffer.writeln('include \$(CLEAR_VARS)'); |
| 1348 buffer.writeln('LOCAL_MODULE := fletch'); | 1348 buffer.writeln('LOCAL_MODULE := dartino'); |
| 1349 buffer.writeln('LOCAL_CFLAGS := -DFLETCH32'); | 1349 buffer.writeln('LOCAL_CFLAGS := -DDARTINO32'); |
| 1350 buffer.writeln('LOCAL_LDLIBS := -llog -ldl -rdynamic'); | 1350 buffer.writeln('LOCAL_LDLIBS := -llog -ldl -rdynamic'); |
| 1351 | 1351 |
| 1352 buffer.writeln(); | 1352 buffer.writeln(); |
| 1353 buffer.writeln('LOCAL_SRC_FILES := \\'); | 1353 buffer.writeln('LOCAL_SRC_FILES := \\'); |
| 1354 buffer.writeln('\tfletch_api_wrapper.cc \\'); | 1354 buffer.writeln('\tdartino_api_wrapper.cc \\'); |
| 1355 buffer.writeln('\tfletch_service_api_wrapper.cc \\'); | 1355 buffer.writeln('\tdartino_service_api_wrapper.cc \\'); |
| 1356 | 1356 |
| 1357 if (unit.services.length > 1) { | 1357 if (unit.services.length > 1) { |
| 1358 print('Java plugin: multiple services in one file is not supported.'); | 1358 print('Java plugin: multiple services in one file is not supported.'); |
| 1359 } | 1359 } |
| 1360 String projectName = basenameWithoutExtension(path); | 1360 String projectName = basenameWithoutExtension(path); |
| 1361 String file = '${projectName}_wrapper'; | 1361 String file = '${projectName}_wrapper'; |
| 1362 | 1362 |
| 1363 buffer.writeln('\t${file}.cc'); | 1363 buffer.writeln('\t${file}.cc'); |
| 1364 | 1364 |
| 1365 buffer.writeln(); | 1365 buffer.writeln(); |
| 1366 buffer.writeln('LOCAL_C_INCLUDES += \$(LOCAL_PATH)'); | 1366 buffer.writeln('LOCAL_C_INCLUDES += \$(LOCAL_PATH)'); |
| 1367 buffer.writeln('LOCAL_C_INCLUDES += ${includePath}'); | 1367 buffer.writeln('LOCAL_C_INCLUDES += ${includePath}'); |
| 1368 buffer.writeln('LOCAL_STATIC_LIBRARIES := fletch-library'); | 1368 buffer.writeln('LOCAL_STATIC_LIBRARIES := dartino-library'); |
| 1369 | 1369 |
| 1370 buffer.writeln(); | 1370 buffer.writeln(); |
| 1371 buffer.writeln('include \$(BUILD_SHARED_LIBRARY)'); | 1371 buffer.writeln('include \$(BUILD_SHARED_LIBRARY)'); |
| 1372 | 1372 |
| 1373 buffer.writeln(); | 1373 buffer.writeln(); |
| 1374 buffer.writeln('\$(call import-module, ${modulePath})'); | 1374 buffer.writeln('\$(call import-module, ${modulePath})'); |
| 1375 | 1375 |
| 1376 writeToFile(join(out, 'jni'), 'Android', buffer.toString(), | 1376 writeToFile(join(out, 'jni'), 'Android', buffer.toString(), |
| 1377 extension: 'mk'); | 1377 extension: 'mk'); |
| 1378 | 1378 |
| 1379 buffer = new StringBuffer(HEADER_MK); | 1379 buffer = new StringBuffer(HEADER_MK); |
| 1380 buffer.writeln('APP_STL := gnustl_static'); | 1380 buffer.writeln('APP_STL := gnustl_static'); |
| 1381 buffer.writeln('APP_ABI := x86 armeabi-v7a'); | 1381 buffer.writeln('APP_ABI := x86 armeabi-v7a'); |
| 1382 // TODO(zerny): Is this the right place and way to ensure ABI >= 8? | 1382 // TODO(zerny): Is this the right place and way to ensure ABI >= 8? |
| 1383 buffer.writeln('APP_PLATFORM := android-8'); | 1383 buffer.writeln('APP_PLATFORM := android-8'); |
| 1384 writeToFile(join(out, 'jni'), 'Application', buffer.toString(), | 1384 writeToFile(join(out, 'jni'), 'Application', buffer.toString(), |
| 1385 extension: 'mk'); | 1385 extension: 'mk'); |
| 1386 } | 1386 } |
| OLD | NEW |