| 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 /// Higher level access to the FFI library. | 5 /// Higher level access to the FFI library. |
| 6 /// | 6 /// |
| 7 /// This package is a set of functions and classes that builds on the lower | 7 /// This package is a set of functions and classes that builds on the lower |
| 8 /// level FFI library (dart:fletch.ffi). | 8 /// level FFI library (dart:dartino.ffi). |
| 9 library ffi; | 9 library ffi; |
| 10 | 10 |
| 11 import 'dart:fletch.ffi'; | 11 import 'dart:dartino.ffi'; |
| 12 import 'dart:typed_data'; | 12 import 'dart:typed_data'; |
| 13 | 13 |
| 14 part 'utf.dart'; | 14 part 'utf.dart'; |
| 15 | 15 |
| 16 final ForeignFunction _strlen = ForeignLibrary.main.lookup('strlen'); | 16 final ForeignFunction _strlen = ForeignLibrary.main.lookup('strlen'); |
| 17 | 17 |
| 18 /// Converts a C string to a String in the Fletch heap. | 18 /// Converts a C string to a String in the Dartino heap. |
| 19 /// This call expects a null terminated string. The string will be decoded | 19 /// This call expects a null terminated string. The string will be decoded |
| 20 /// using a UTF8 decoder. | 20 /// using a UTF8 decoder. |
| 21 String cStringToString(ForeignPointer ptr) { | 21 String cStringToString(ForeignPointer ptr) { |
| 22 int length = _strlen.icall$1(ptr); | 22 int length = _strlen.icall$1(ptr); |
| 23 return memoryToString(ptr, length); | 23 return memoryToString(ptr, length); |
| 24 } | 24 } |
| 25 | 25 |
| 26 /// Converts a C memory region to a String in the Fletch heap. The string | 26 /// Converts a C memory region to a String in the Dartino heap. The string |
| 27 /// will be decoded using a UTF8 decoder. | 27 /// will be decoded using a UTF8 decoder. |
| 28 String memoryToString(ForeignPointer ptr, int length) { | 28 String memoryToString(ForeignPointer ptr, int length) { |
| 29 var memory = new ForeignMemory.fromAddress(ptr.address, length); | 29 var memory = new ForeignMemory.fromAddress(ptr.address, length); |
| 30 var encodedString = new List(length); | 30 var encodedString = new List(length); |
| 31 for (int i = 0; i < length; ++i) { | 31 for (int i = 0; i < length; ++i) { |
| 32 encodedString[i] = memory.getUint8(i); | 32 encodedString[i] = memory.getUint8(i); |
| 33 } | 33 } |
| 34 return _decodeUtf8(encodedString); | 34 return _decodeUtf8(encodedString); |
| 35 } | 35 } |
| 36 | 36 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 int head = _head; | 126 int head = _head; |
| 127 // TODO(ricow): Consider if we should do a memcpy here (two if wrapping) | 127 // TODO(ricow): Consider if we should do a memcpy here (two if wrapping) |
| 128 for (int i = 0; i < bytes; i++) { | 128 for (int i = 0; i < bytes; i++) { |
| 129 _buffer.setUint8(DATA_START + head, bytelist[i]); | 129 _buffer.setUint8(DATA_START + head, bytelist[i]); |
| 130 head = (head + 1) % _size; | 130 head = (head + 1) % _size; |
| 131 } | 131 } |
| 132 _head = head; | 132 _head = head; |
| 133 return bytes; | 133 return bytes; |
| 134 } | 134 } |
| 135 } | 135 } |
| OLD | NEW |