Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Fletch 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 // The dart:ffi library is a low-level 'foreign function interface' | 5 // The dart:ffi library is a low-level 'foreign function interface' |
| 6 // library that allows Dart code to call arbitrary native platform | 6 // library that allows Dart code to call arbitrary native platform |
| 7 // code defined outside the VM. | 7 // code defined outside the VM. |
| 8 library dart.ffi; | 8 library dart.ffi; |
| 9 | 9 |
| 10 import 'dart:_fletch_system' as fletch; | 10 import 'dart:_fletch_system' as fletch; |
| 11 | 11 |
| 12 class Foreign1 { | |
|
ahe
2015/06/30 13:16:50
Can I assume that you're planning on deleting Fore
ricow1
2015/07/02 16:01:55
Done.
| |
| 13 int _value; | |
| 14 int get value => _value; | |
|
kasperl
2015/06/30 07:16:00
I guess it will be nice when you can stop exposing
ricow1
2015/06/30 07:34:21
yes
ricow1
2015/07/02 16:01:56
Done.
| |
| 15 | |
| 16 Foreign1._(this._value); | |
| 17 | |
| 18 static const int UNKNOWN = 0; | |
| 19 | |
| 20 static const int LINUX = 1; | |
|
Anders Johnsen
2015/06/30 06:30:17
We should use enums going forward.
ricow1
2015/06/30 07:34:22
Acknowledged.
| |
| 21 static const int MACOS = 2; | |
| 22 | |
| 23 static const int IA32 = 1; | |
| 24 static const int X64 = 2; | |
| 25 static const int ARM = 3; | |
| 26 | |
| 27 static final Foreign1 NULL = new Foreign1(); | |
| 28 | |
| 29 // Helper for converting the argument to a machine word. | |
| 30 int _convert(argument) { | |
| 31 if (argument is Foreign) return argument._value; | |
| 32 if (argument is Port) return Foreign._convertPort(argument); | |
| 33 if (argument is int) return argument; | |
| 34 throw new ArgumentError(); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 class ForeignFunction extends Foreign1 { | |
|
kasperl
2015/06/30 07:16:00
So to call a method that returns a C function poin
ricow1
2015/06/30 07:34:21
Good point, will do that
ricow1
2015/07/02 16:01:55
Done.
| |
| 39 ForeignFunction.fromAddress(int value) : super._(value); | |
| 40 | |
| 41 // Support for calling foreign functions that return | |
| 42 // machine words as immediate integer value. | |
| 43 int icall$0() => Foreign._icall$0(_value); | |
| 44 int icall$1(a0) => Foreign._icall$1(_value, _convert(a0)); | |
| 45 int icall$2(a0, a1) => Foreign._icall$2(_value, _convert(a0), _convert(a1)); | |
| 46 int icall$3(a0, a1, a2) { | |
| 47 return Foreign._icall$3(_value, _convert(a0), _convert(a1), _convert(a2)); | |
| 48 } | |
| 49 int icall$4(a0, a1, a2, a3) { | |
| 50 return Foreign._icall$4(_value, _convert(a0), _convert(a1), _convert(a2), | |
| 51 _convert(a3)); | |
| 52 } | |
| 53 int icall$5(a0, a1, a2, a3, a4) { | |
| 54 return Foreign._icall$5(_value, _convert(a0), _convert(a1), _convert(a2), | |
| 55 _convert(a3), _convert(a4)); | |
| 56 } | |
| 57 int icall$6(a0, a1, a2, a3, a4, a5) { | |
| 58 return Foreign._icall$6(_value, _convert(a0), _convert(a1), _convert(a2), | |
| 59 _convert(a3), _convert(a4), _convert(a5)); | |
| 60 } | |
| 61 | |
| 62 // Support for calling foreign functions with no return value. | |
| 63 void vcall$0() { | |
| 64 Foreign._vcall$0(_value); | |
| 65 } | |
| 66 void vcall$1(a0) { | |
| 67 Foreign._vcall$1(_value, _convert(a0)); | |
| 68 } | |
| 69 void vcall$2(a0, a1) { | |
| 70 Foreign._vcall$2(_value, _convert(a0), _convert(a1)); | |
| 71 } | |
| 72 void vcall$3(a0, a1, a2) { | |
| 73 Foreign._vcall$3(_value, _convert(a0), _convert(a1), _convert(a2)); | |
| 74 } | |
| 75 void vcall$4(a0, a1, a2, a3) { | |
| 76 Foreign._vcall$4(_value, _convert(a0), _convert(a1), _convert(a2), | |
| 77 _convert(a3)); | |
| 78 } | |
| 79 void vcall$5(a0, a1, a2, a3, a4) { | |
| 80 Foreign._vcall$5(_value, _convert(a0), _convert(a1), _convert(a2), | |
| 81 _convert(a3), _convert(a4)); | |
| 82 } | |
| 83 void vcall$6(a0, a1, a2, a3, a4, a5) { | |
| 84 Foreign._vcall$6(_value, _convert(a0), _convert(a1), _convert(a2), | |
| 85 _convert(a3), _convert(a4), _convert(a5)); | |
| 86 } | |
| 87 | |
| 88 // TODO(ricow): this is insanely specific and only used for rseek. | |
| 89 // Support for calling foreign functions that | |
| 90 // - Returns a 64 bit integer value. | |
| 91 // - Takes: | |
| 92 // * a word, | |
| 93 // * a 64 bit int | |
| 94 // * a word | |
| 95 int Lcall$wLw(a0, a1, a2) { | |
|
kasperl
2015/06/30 07:16:00
What's the purpose of this? Is it because you don'
ricow1
2015/06/30 07:34:21
This is just an existing function, used for rseek.
| |
| 96 return _Lcall$wLw(_value, | |
| 97 _convert(a0), | |
| 98 _convert(a1), | |
| 99 _convert(a2)); | |
| 100 } | |
| 101 | |
| 102 // Support for calling foreign functions that return | |
| 103 // machine words -- typically pointers -- encapulated in | |
| 104 // the given foreign object arguments. | |
| 105 ForeignPointer pcall$0(ForeignPointer foreign) { | |
| 106 foreign._value = Foreign._icall$0(_value); | |
| 107 return foreign; | |
| 108 } | |
| 109 | |
| 110 ForeignPointer pcall$1(ForeignPointer foreign, a0) { | |
| 111 foreign._value = Foreign._icall$1(_value, _convert(a0)); | |
| 112 return foreign; | |
| 113 } | |
| 114 | |
| 115 ForeignPointer pcall$2(ForeignPointer foreign, a0, a1) { | |
| 116 foreign._value = Foreign._icall$2(_value, _convert(a0), _convert(a1)); | |
| 117 return foreign; | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 class ForeignLibrary extends Foreign1 { | |
| 122 ForeignLibrary.fromAddress(int address) : super._(address); | |
| 123 | |
| 124 factory ForeignLibrary.fromName(String name) { | |
| 125 return new ForeignLibrary.fromAddress(_lookupLibrary(name)); | |
| 126 } | |
| 127 | |
| 128 static ForeignFunction lookupDefaultLibraries(String name) { | |
|
kasperl
2015/06/30 07:16:00
Maybe it would be cleaner to have a ForeignLibrary
ricow1
2015/06/30 07:34:21
sgtm
ricow1
2015/07/02 16:01:55
default is a reserved word - I called it standard
| |
| 129 return new ForeignFunction.fromAddress(Foreign.lookup(name).value); | |
| 130 } | |
| 131 | |
| 132 ForeignFunction lookupFunction(String name) { | |
| 133 return new ForeignFunction.fromAddress(_lookupFunction(_value, name)); | |
| 134 } | |
| 135 | |
| 136 static String bundleLibraryName(String libraryName) => | |
|
kasperl
2015/06/30 07:16:00
Add a comment that explains what bundleLibraryName
kasperl
2015/06/30 07:16:00
Why do you need the separate _bundlePath function?
ricow1
2015/06/30 07:34:21
Acknowledged.
ricow1
2015/06/30 07:34:21
Acknowledged.
ricow1
2015/07/02 16:01:55
Done.
| |
| 137 _bundlePath(libraryName); | |
| 138 | |
| 139 void close() { | |
| 140 _closeLibrary(_value); | |
| 141 } | |
| 142 | |
| 143 @fletch.native static int _lookupLibrary(String name) { | |
| 144 var error = fletch.nativeError; | |
| 145 throw (error != fletch.indexOutOfBounds) ? error : new ArgumentError(); | |
| 146 } | |
| 147 | |
| 148 @fletch.native static int _lookupFunction(int _value, String name) { | |
| 149 var error = fletch.nativeError; | |
| 150 throw (error != fletch.indexOutOfBounds) ? error : new ArgumentError(); | |
| 151 } | |
| 152 | |
| 153 @fletch.native static int _closeLibrary(int _value) { | |
| 154 var error = fletch.nativeError; | |
| 155 throw (error != fletch.indexOutOfBounds) ? error : new ArgumentError(); | |
| 156 } | |
| 157 | |
| 158 @fletch.native external static String _bundlePath(String library); | |
| 159 } | |
| 160 | |
| 161 class ForeignPointer extends Foreign1 { | |
| 162 ForeignPointer.fromAddress(int address) : super._(address); | |
| 163 ForeignPointer() : super._(0); | |
| 164 } | |
| 165 | |
| 166 class ForeignMemory extends ForeignPointer { | |
|
Anders Johnsen
2015/06/30 06:30:17
Should ForeignMemory have a way of auto-deleting (
ricow1
2015/06/30 06:52:07
Yes, as stated in the review description I will mo
kasperl
2015/06/30 07:16:00
Could and should this implement some typed_data in
ricow1
2015/06/30 07:34:21
Yes I probably could, I will look into it
| |
| 167 int _length; | |
| 168 | |
| 169 ForeignMemory.fromForeignPointer(ForeignPointer pointer, int this._length) : | |
|
kasperl
2015/06/30 07:16:00
No need to type the this._length parameter. The la
ricow1
2015/06/30 07:34:21
Acknowledged.
ricow1
2015/07/02 16:01:55
Done.
| |
| 170 super.fromAddress(pointer.value); | |
| 171 | |
| 172 int getInt8(int offset) | |
| 173 => Foreign._getInt8(_computeAddress(offset, 1)); | |
| 174 int getInt16(int offset) | |
| 175 => Foreign._getInt16(_computeAddress(offset, 2)); | |
| 176 int getInt32(int offset) | |
| 177 => Foreign._getInt32(_computeAddress(offset, 4)); | |
| 178 int getInt64(int offset) | |
| 179 => Foreign._getInt64(_computeAddress(offset, 8)); | |
| 180 | |
| 181 int setInt8(int offset, int value) | |
| 182 => Foreign._setInt8(_computeAddress(offset, 1), value); | |
| 183 int setInt16(int offset, int value) | |
| 184 => Foreign._setInt16(_computeAddress(offset, 2), value); | |
| 185 int setInt32(int offset, int value) | |
| 186 => Foreign._setInt32(_computeAddress(offset, 4), value); | |
| 187 int setInt64(int offset, int value) | |
| 188 => Foreign._setInt64(_computeAddress(offset, 8), value); | |
| 189 | |
| 190 int getUint8(int offset) | |
| 191 => Foreign._getUint8(_computeAddress(offset, 1)); | |
| 192 int getUint16(int offset) | |
| 193 => Foreign._getUint16(_computeAddress(offset, 2)); | |
| 194 int getUint32(int offset) | |
| 195 => Foreign._getUint32(_computeAddress(offset, 4)); | |
| 196 int getUint64(int offset) | |
| 197 => Foreign._getUint64(_computeAddress(offset, 8)); | |
| 198 | |
| 199 int setUint8(int offset, int value) | |
| 200 => Foreign._setUint8(_computeAddress(offset, 1), value); | |
| 201 int setUint16(int offset, int value) | |
| 202 => Foreign._setUint16(_computeAddress(offset, 2), value); | |
| 203 int setUint32(int offset, int value) | |
| 204 => Foreign._setUint32(_computeAddress(offset, 4), value); | |
| 205 int setUint64(int offset, int value) | |
| 206 => Foreign._setUint64(_computeAddress(offset, 8), value); | |
| 207 | |
| 208 double getFloat32(int offset) | |
| 209 => Foreign._getFloat32(_computeAddress(offset, 4)); | |
| 210 double getFloat64(int offset) | |
| 211 => Foreign._getFloat64(_computeAddress(offset, 8)); | |
| 212 | |
| 213 double setFloat32(int offset, double value) | |
| 214 => Foreign._setFloat32(_computeAddress(offset, 4), value); | |
| 215 double setFloat64(int offset, double value) | |
| 216 => Foreign._setFloat64(_computeAddress(offset, 8), value); | |
| 217 | |
| 218 // Helper for checking bounds and computing derived | |
| 219 // address for memory address functionality. | |
|
kasperl
2015/06/30 07:16:00
derived address -> derived addresses?
ricow1
2015/06/30 07:34:21
Acknowledged.
ricow1
2015/07/02 16:01:55
Done.
| |
| 220 int _computeAddress(int offset, int n) { | |
| 221 if (offset < 0 || offset + n > _length) throw new IndexError(offset, this); | |
| 222 return _value + offset; | |
| 223 } | |
| 224 | |
| 225 void free() { | |
| 226 if (_length > 0) Foreign._free(_value); | |
|
Anders Johnsen
2015/06/30 06:30:17
Consider using -1 for length, as malloc(0) may ret
ricow1
2015/06/30 07:34:21
Acknowledged.
| |
| 227 _value = 0; | |
| 228 _length = 0; | |
| 229 } | |
| 230 } | |
| 231 | |
| 12 class Foreign { | 232 class Foreign { |
| 13 static const int UNKNOWN = 0; | 233 static const int UNKNOWN = 0; |
| 14 | 234 |
| 15 static const int LINUX = 1; | 235 static const int LINUX = 1; |
| 16 static const int MACOS = 2; | 236 static const int MACOS = 2; |
| 17 | 237 |
| 18 static const int IA32 = 1; | 238 static const int IA32 = 1; |
| 19 static const int X64 = 2; | 239 static const int X64 = 2; |
| 20 static const int ARM = 3; | 240 static const int ARM = 3; |
| 21 | 241 |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 @fletch.native static double _setFloat64(int address, double value) { | 517 @fletch.native static double _setFloat64(int address, double value) { |
| 298 throw new ArgumentError(); | 518 throw new ArgumentError(); |
| 299 } | 519 } |
| 300 | 520 |
| 301 @fletch.native external static int _bitsPerMachineWord(); | 521 @fletch.native external static int _bitsPerMachineWord(); |
| 302 @fletch.native external static int _errno(); | 522 @fletch.native external static int _errno(); |
| 303 @fletch.native external static int _platform(); | 523 @fletch.native external static int _platform(); |
| 304 @fletch.native external static int _architecture(); | 524 @fletch.native external static int _architecture(); |
| 305 @fletch.native external static int _convertPort(Port port); | 525 @fletch.native external static int _convertPort(Port port); |
| 306 } | 526 } |
| OLD | NEW |