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 // The dart:fletch.ffi library is a low-level 'foreign function interface' | 5 // The dart:dartino.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.fletch.ffi; | 8 library dart.dartino.ffi; |
9 | 9 |
10 import 'dart:fletch._system' as fletch; | 10 import 'dart:dartino._system' as dartino; |
11 import 'dart:fletch'; | 11 import 'dart:dartino'; |
12 import 'dart:typed_data'; | 12 import 'dart:typed_data'; |
13 | 13 |
14 part 'utf.dart'; | 14 part 'utf.dart'; |
15 | 15 |
16 abstract class Foreign { | 16 abstract class Foreign { |
17 int get address; | 17 int get address; |
18 | 18 |
19 const Foreign(); | 19 const Foreign(); |
20 | 20 |
21 static const int UNKNOWN = 0; | 21 static const int UNKNOWN = 0; |
(...skipping 15 matching lines...) Expand all Loading... |
37 static int get errno => _errno(); | 37 static int get errno => _errno(); |
38 | 38 |
39 // Helper for converting the argument to a machine word. | 39 // Helper for converting the argument to a machine word. |
40 int _convert(argument) { | 40 int _convert(argument) { |
41 if (argument is Foreign) return argument.address; | 41 if (argument is Foreign) return argument.address; |
42 if (argument is Port) return _convertPort(argument); | 42 if (argument is Port) return _convertPort(argument); |
43 if (argument is int) return argument; | 43 if (argument is int) return argument; |
44 throw new ArgumentError(); | 44 throw new ArgumentError(); |
45 } | 45 } |
46 | 46 |
47 @fletch.native static int _bitsPerMachineWord() { | 47 @dartino.native static int _bitsPerMachineWord() { |
48 throw new UnsupportedError('_bitsPerMachineWord'); | 48 throw new UnsupportedError('_bitsPerMachineWord'); |
49 } | 49 } |
50 @fletch.native static int _errno() { | 50 @dartino.native static int _errno() { |
51 throw new UnsupportedError('_errno'); | 51 throw new UnsupportedError('_errno'); |
52 } | 52 } |
53 @fletch.native static int _platform() { | 53 @dartino.native static int _platform() { |
54 throw new UnsupportedError('_platform'); | 54 throw new UnsupportedError('_platform'); |
55 } | 55 } |
56 @fletch.native static int _architecture() { | 56 @dartino.native static int _architecture() { |
57 throw new UnsupportedError('_architecture'); | 57 throw new UnsupportedError('_architecture'); |
58 } | 58 } |
59 @fletch.native static int _convertPort(Port port) { | 59 @dartino.native static int _convertPort(Port port) { |
60 throw new ArgumentError(); | 60 throw new ArgumentError(); |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 class ForeignFunction extends Foreign { | 64 class ForeignFunction extends Foreign { |
65 final int address; | 65 final int address; |
66 final ForeignLibrary _library; | 66 final ForeignLibrary _library; |
67 const ForeignFunction.fromAddress(this.address, [this._library = null]); | 67 const ForeignFunction.fromAddress(this.address, [this._library = null]); |
68 | 68 |
69 /// Helper function for retrying functions that follow the POSIX-convention | 69 /// Helper function for retrying functions that follow the POSIX-convention |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 // - Takes: | 195 // - Takes: |
196 // * a word, | 196 // * a word, |
197 // * a 64 bit int | 197 // * a 64 bit int |
198 // * a word | 198 // * a word |
199 int Lcall$wLw(a0, a1, a2) { | 199 int Lcall$wLw(a0, a1, a2) { |
200 return _Lcall$wLw(address, _convert(a0), _convert(a1), _convert(a2)); | 200 return _Lcall$wLw(address, _convert(a0), _convert(a1), _convert(a2)); |
201 } | 201 } |
202 | 202 |
203 int Lcall$wLwRetry(a0, a1, a2) => retry(() => Lcall$wLw(a0, a1, a2)); | 203 int Lcall$wLwRetry(a0, a1, a2) => retry(() => Lcall$wLw(a0, a1, a2)); |
204 | 204 |
205 @fletch.native static int _icall$0(int address) { | 205 @dartino.native static int _icall$0(int address) { |
206 throw new ArgumentError(); | 206 throw new ArgumentError(); |
207 } | 207 } |
208 @fletch.native static int _icall$1(int address, a0) { | 208 @dartino.native static int _icall$1(int address, a0) { |
209 throw new ArgumentError(); | 209 throw new ArgumentError(); |
210 } | 210 } |
211 @fletch.native static int _icall$2(int address, a0, a1) { | 211 @dartino.native static int _icall$2(int address, a0, a1) { |
212 throw new ArgumentError(); | 212 throw new ArgumentError(); |
213 } | 213 } |
214 @fletch.native static int _icall$3(int address, a0, a1, a2) { | 214 @dartino.native static int _icall$3(int address, a0, a1, a2) { |
215 throw new ArgumentError(); | 215 throw new ArgumentError(); |
216 } | 216 } |
217 @fletch.native static int _icall$4(int address, a0, a1, a2, a3) { | 217 @dartino.native static int _icall$4(int address, a0, a1, a2, a3) { |
218 throw new ArgumentError(); | 218 throw new ArgumentError(); |
219 } | 219 } |
220 @fletch.native static int _icall$5(int address, a0, a1, a2, a3, a4) { | 220 @dartino.native static int _icall$5(int address, a0, a1, a2, a3, a4) { |
221 throw new ArgumentError(); | 221 throw new ArgumentError(); |
222 } | 222 } |
223 @fletch.native static int _icall$6( | 223 @dartino.native static int _icall$6( |
224 int address, a0, a1, a2, a3, a4, a5) { | 224 int address, a0, a1, a2, a3, a4, a5) { |
225 throw new ArgumentError(); | 225 throw new ArgumentError(); |
226 } | 226 } |
227 @fletch.native static int _icall$7( | 227 @dartino.native static int _icall$7( |
228 int address, a0, a1, a2, a3, a4, a5, a6) { | 228 int address, a0, a1, a2, a3, a4, a5, a6) { |
229 throw new ArgumentError(); | 229 throw new ArgumentError(); |
230 } | 230 } |
231 | 231 |
232 @fletch.native static int _pcall$0(int address) { | 232 @dartino.native static int _pcall$0(int address) { |
233 throw new ArgumentError(); | 233 throw new ArgumentError(); |
234 } | 234 } |
235 @fletch.native static int _pcall$1(int address, a0) { | 235 @dartino.native static int _pcall$1(int address, a0) { |
236 throw new ArgumentError(); | 236 throw new ArgumentError(); |
237 } | 237 } |
238 @fletch.native static int _pcall$2(int address, a0, a1) { | 238 @dartino.native static int _pcall$2(int address, a0, a1) { |
239 throw new ArgumentError(); | 239 throw new ArgumentError(); |
240 } | 240 } |
241 @fletch.native static int _pcall$3(int address, a0, a1, a2) { | 241 @dartino.native static int _pcall$3(int address, a0, a1, a2) { |
242 throw new ArgumentError(); | 242 throw new ArgumentError(); |
243 } | 243 } |
244 @fletch.native static int _pcall$4(int address, a0, a1, a2, a3) { | 244 @dartino.native static int _pcall$4(int address, a0, a1, a2, a3) { |
245 throw new ArgumentError(); | 245 throw new ArgumentError(); |
246 } | 246 } |
247 @fletch.native static int _pcall$5(int address, a0, a1, a2, a3, a4) { | 247 @dartino.native static int _pcall$5(int address, a0, a1, a2, a3, a4) { |
248 throw new ArgumentError(); | 248 throw new ArgumentError(); |
249 } | 249 } |
250 @fletch.native static int _pcall$6( | 250 @dartino.native static int _pcall$6( |
251 int address, a0, a1, a2, a3, a4, a5) { | 251 int address, a0, a1, a2, a3, a4, a5) { |
252 throw new ArgumentError(); | 252 throw new ArgumentError(); |
253 } | 253 } |
254 | 254 |
255 @fletch.native static int _vcall$0(int address) { | 255 @dartino.native static int _vcall$0(int address) { |
256 throw new ArgumentError(); | 256 throw new ArgumentError(); |
257 } | 257 } |
258 @fletch.native static int _vcall$1(int address, a0) { | 258 @dartino.native static int _vcall$1(int address, a0) { |
259 throw new ArgumentError(); | 259 throw new ArgumentError(); |
260 } | 260 } |
261 @fletch.native static int _vcall$2(int address, a0, a1) { | 261 @dartino.native static int _vcall$2(int address, a0, a1) { |
262 throw new ArgumentError(); | 262 throw new ArgumentError(); |
263 } | 263 } |
264 @fletch.native static int _vcall$3(int address, a0, a1, a2) { | 264 @dartino.native static int _vcall$3(int address, a0, a1, a2) { |
265 throw new ArgumentError(); | 265 throw new ArgumentError(); |
266 } | 266 } |
267 @fletch.native static int _vcall$4(int address, a0, a1, a2, a3) { | 267 @dartino.native static int _vcall$4(int address, a0, a1, a2, a3) { |
268 throw new ArgumentError(); | 268 throw new ArgumentError(); |
269 } | 269 } |
270 @fletch.native static int _vcall$5(int address, a0, a1, a2, a3, a4) { | 270 @dartino.native static int _vcall$5(int address, a0, a1, a2, a3, a4) { |
271 throw new ArgumentError(); | 271 throw new ArgumentError(); |
272 } | 272 } |
273 @fletch.native static int _vcall$6( | 273 @dartino.native static int _vcall$6( |
274 int address, a0, a1, a2, a3, a4, a5) { | 274 int address, a0, a1, a2, a3, a4, a5) { |
275 throw new ArgumentError(); | 275 throw new ArgumentError(); |
276 } | 276 } |
277 | 277 |
278 @fletch.native static int _Lcall$wLw(int address, a0, a1, a2) { | 278 @dartino.native static int _Lcall$wLw(int address, a0, a1, a2) { |
279 throw new ArgumentError(); | 279 throw new ArgumentError(); |
280 } | 280 } |
281 } | 281 } |
282 | 282 |
283 class ForeignPointer extends Foreign { | 283 class ForeignPointer extends Foreign { |
284 final int address; | 284 final int address; |
285 const ForeignPointer(this.address); | 285 const ForeignPointer(this.address); |
286 | 286 |
287 static const ForeignPointer NULL = const ForeignPointer(0); | 287 static const ForeignPointer NULL = const ForeignPointer(0); |
288 } | 288 } |
289 | 289 |
290 class _ForeignValue extends ForeignPointer { | 290 class _ForeignValue extends ForeignPointer { |
291 final ForeignLibrary _library; | 291 final ForeignLibrary _library; |
292 | 292 |
293 const _ForeignValue(int address, [this._library = null]) : super(address); | 293 const _ForeignValue(int address, [this._library = null]) : super(address); |
294 } | 294 } |
295 | 295 |
296 class ForeignLibrary extends ForeignPointer { | 296 class ForeignLibrary extends ForeignPointer { |
297 /// The ForeignLibrary main is used for looking up functions in the libraries | 297 /// The ForeignLibrary main is used for looking up functions in the libraries |
298 /// linked in to the main Fletch binary. | 298 /// linked in to the main Dartino binary. |
299 static ForeignLibrary main = new ForeignLibrary.fromName(null); | 299 static ForeignLibrary main = new ForeignLibrary.fromName(null); |
300 | 300 |
301 const ForeignLibrary.fromAddress(int address) : super(address); | 301 const ForeignLibrary.fromAddress(int address) : super(address); |
302 | 302 |
303 /// Looks up a foreign library by name. If the global flag is set we use | 303 /// Looks up a foreign library by name. If the global flag is set we use |
304 /// RTLD_GLOBAL that will enable later lookups to use code from this library. | 304 /// RTLD_GLOBAL that will enable later lookups to use code from this library. |
305 factory ForeignLibrary.fromName(String name, {bool global: false}) { | 305 factory ForeignLibrary.fromName(String name, {bool global: false}) { |
306 return new ForeignLibrary.fromAddress(_lookupLibrary(name, global)); | 306 return new ForeignLibrary.fromAddress(_lookupLibrary(name, global)); |
307 } | 307 } |
308 | 308 |
309 ForeignFunction lookup(String name) { | 309 ForeignFunction lookup(String name) { |
310 return new ForeignFunction.fromAddress(_lookupFunction(address, name), | 310 return new ForeignFunction.fromAddress(_lookupFunction(address, name), |
311 this); | 311 this); |
312 } | 312 } |
313 | 313 |
314 ForeignPointer lookupVariable(String name) { | 314 ForeignPointer lookupVariable(String name) { |
315 return new _ForeignValue(_lookupFunction(address, name), this); | 315 return new _ForeignValue(_lookupFunction(address, name), this); |
316 } | 316 } |
317 | 317 |
318 /// Provides a platform specific location for a library relative to the | 318 /// Provides a platform specific location for a library relative to the |
319 /// location of the Fletch vm. Takes the name without lib in front and | 319 /// location of the Dartino vm. Takes the name without lib in front and |
320 /// returns a platform specific path. Example, on linux, foobar_hash | 320 /// returns a platform specific path. Example, on linux, foobar_hash |
321 /// become PATH_TO_EXECUTABLE/lib/libfoobar_hash.so. | 321 /// become PATH_TO_EXECUTABLE/lib/libfoobar_hash.so. |
322 @fletch.native static String bundleLibraryName(String libraryName) { | 322 @dartino.native static String bundleLibraryName(String libraryName) { |
323 throw new ArgumentError(); | 323 throw new ArgumentError(); |
324 } | 324 } |
325 | 325 |
326 @fletch.native static int _lookupLibrary(String name, bool global) { | 326 @dartino.native static int _lookupLibrary(String name, bool global) { |
327 var error = fletch.nativeError; | 327 var error = dartino.nativeError; |
328 throw (error != fletch.indexOutOfBounds) ? error : new ArgumentError(); | 328 throw (error != dartino.indexOutOfBounds) ? error : new ArgumentError(); |
329 } | 329 } |
330 | 330 |
331 @fletch.native static int _lookupFunction(int address, String name) { | 331 @dartino.native static int _lookupFunction(int address, String name) { |
332 var error = fletch.nativeError; | 332 var error = dartino.nativeError; |
333 throw (error != fletch.indexOutOfBounds) ? error : new ArgumentError(); | 333 throw (error != dartino.indexOutOfBounds) ? error : new ArgumentError(); |
334 } | 334 } |
335 } | 335 } |
336 | 336 |
337 abstract class UnsafeMemory extends Foreign { | 337 abstract class UnsafeMemory extends Foreign { |
338 int get length; | 338 int get length; |
339 const UnsafeMemory(); | 339 const UnsafeMemory(); |
340 | 340 |
341 int getInt8(int offset) => _getInt8(_computeAddress(offset, 1)); | 341 int getInt8(int offset) => _getInt8(_computeAddress(offset, 1)); |
342 int getInt16(int offset) => _getInt16(_computeAddress(offset, 2)); | 342 int getInt16(int offset) => _getInt16(_computeAddress(offset, 2)); |
343 int getInt32(int offset) => _getInt32(_computeAddress(offset, 4)); | 343 int getInt32(int offset) => _getInt32(_computeAddress(offset, 4)); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 } | 388 } |
389 } | 389 } |
390 | 390 |
391 void copyBytesFromList(List<int> list, int from, int to, int listOffset) { | 391 void copyBytesFromList(List<int> list, int from, int to, int listOffset) { |
392 int length = to - from; | 392 int length = to - from; |
393 for (int i = 0; i < length; i++) { | 393 for (int i = 0; i < length; i++) { |
394 setUint8(from + i, list[listOffset + i]); | 394 setUint8(from + i, list[listOffset + i]); |
395 } | 395 } |
396 } | 396 } |
397 | 397 |
398 @fletch.native static int _allocate(int length) { | 398 @dartino.native static int _allocate(int length) { |
399 throw new ArgumentError(); | 399 throw new ArgumentError(); |
400 } | 400 } |
401 @fletch.native void _markForFinalization(int length) { | 401 @dartino.native void _markForFinalization(int length) { |
402 throw new ArgumentError(); | 402 throw new ArgumentError(); |
403 } | 403 } |
404 | 404 |
405 @fletch.native static int _getInt8(int address) { | 405 @dartino.native static int _getInt8(int address) { |
406 throw new ArgumentError(); | 406 throw new ArgumentError(); |
407 } | 407 } |
408 @fletch.native static int _getInt16(int address) { | 408 @dartino.native static int _getInt16(int address) { |
409 throw new ArgumentError(); | 409 throw new ArgumentError(); |
410 } | 410 } |
411 @fletch.native static int _getInt32(int address) { | 411 @dartino.native static int _getInt32(int address) { |
412 throw new ArgumentError(); | 412 throw new ArgumentError(); |
413 } | 413 } |
414 @fletch.native static int _getInt64(int address) { | 414 @dartino.native static int _getInt64(int address) { |
415 throw new ArgumentError(); | 415 throw new ArgumentError(); |
416 } | 416 } |
417 | 417 |
418 @fletch.native static int _setInt8(int address, int value) { | 418 @dartino.native static int _setInt8(int address, int value) { |
419 throw new ArgumentError(); | 419 throw new ArgumentError(); |
420 } | 420 } |
421 @fletch.native static int _setInt16(int address, int value) { | 421 @dartino.native static int _setInt16(int address, int value) { |
422 throw new ArgumentError(); | 422 throw new ArgumentError(); |
423 } | 423 } |
424 @fletch.native static int _setInt32(int address, int value) { | 424 @dartino.native static int _setInt32(int address, int value) { |
425 throw new ArgumentError(); | 425 throw new ArgumentError(); |
426 } | 426 } |
427 @fletch.native static int _setInt64(int address, int value) { | 427 @dartino.native static int _setInt64(int address, int value) { |
428 throw new ArgumentError(); | 428 throw new ArgumentError(); |
429 } | 429 } |
430 | 430 |
431 @fletch.native static int _getUint8(int address) { | 431 @dartino.native static int _getUint8(int address) { |
432 throw new ArgumentError(); | 432 throw new ArgumentError(); |
433 } | 433 } |
434 @fletch.native static int _getUint16(int address) { | 434 @dartino.native static int _getUint16(int address) { |
435 throw new ArgumentError(); | 435 throw new ArgumentError(); |
436 } | 436 } |
437 @fletch.native static int _getUint32(int address) { | 437 @dartino.native static int _getUint32(int address) { |
438 throw new ArgumentError(); | 438 throw new ArgumentError(); |
439 } | 439 } |
440 @fletch.native static int _getUint64(int address) { | 440 @dartino.native static int _getUint64(int address) { |
441 throw new ArgumentError(); | 441 throw new ArgumentError(); |
442 } | 442 } |
443 | 443 |
444 @fletch.native static int _setUint8(int address, int value) { | 444 @dartino.native static int _setUint8(int address, int value) { |
445 throw new ArgumentError(); | 445 throw new ArgumentError(); |
446 } | 446 } |
447 @fletch.native static int _setUint16(int address, int value) { | 447 @dartino.native static int _setUint16(int address, int value) { |
448 throw new ArgumentError(); | 448 throw new ArgumentError(); |
449 } | 449 } |
450 @fletch.native static int _setUint32(int address, int value) { | 450 @dartino.native static int _setUint32(int address, int value) { |
451 throw new ArgumentError(); | 451 throw new ArgumentError(); |
452 } | 452 } |
453 @fletch.native static int _setUint64(int address, int value) { | 453 @dartino.native static int _setUint64(int address, int value) { |
454 throw new ArgumentError(); | 454 throw new ArgumentError(); |
455 } | 455 } |
456 | 456 |
457 @fletch.native static double _getFloat32(int address) { | 457 @dartino.native static double _getFloat32(int address) { |
458 throw new ArgumentError(); | 458 throw new ArgumentError(); |
459 } | 459 } |
460 @fletch.native static double _getFloat64(int address) { | 460 @dartino.native static double _getFloat64(int address) { |
461 throw new ArgumentError(); | 461 throw new ArgumentError(); |
462 } | 462 } |
463 | 463 |
464 @fletch.native static double _setFloat32(int address, double value) { | 464 @dartino.native static double _setFloat32(int address, double value) { |
465 throw new ArgumentError(); | 465 throw new ArgumentError(); |
466 } | 466 } |
467 @fletch.native static double _setFloat64(int address, double value) { | 467 @dartino.native static double _setFloat64(int address, double value) { |
468 throw new ArgumentError(); | 468 throw new ArgumentError(); |
469 } | 469 } |
470 } | 470 } |
471 | 471 |
472 class ImmutableForeignMemory extends UnsafeMemory { | 472 class ImmutableForeignMemory extends UnsafeMemory { |
473 // Address is split into two Smis instead of using one int that might be a | 473 // Address is split into two Smis instead of using one int that might be a |
474 // heap number. This makes allocation simpler and ensures that objects can be | 474 // heap number. This makes allocation simpler and ensures that objects can be |
475 // finalized individually in the GC without imposing ordering constraints on | 475 // finalized individually in the GC without imposing ordering constraints on |
476 // the GC. | 476 // the GC. |
477 final int _address0; | 477 final int _address0; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 if (length > 0) { | 575 if (length > 0) { |
576 if (_markedForFinalization) { | 576 if (_markedForFinalization) { |
577 _decreaseMemoryUsage(length); | 577 _decreaseMemoryUsage(length); |
578 } | 578 } |
579 _free(); | 579 _free(); |
580 } | 580 } |
581 address = 0; | 581 address = 0; |
582 length = 0; | 582 length = 0; |
583 } | 583 } |
584 | 584 |
585 @fletch.native void _decreaseMemoryUsage(int length) { | 585 @dartino.native void _decreaseMemoryUsage(int length) { |
586 throw new ArgumentError(); | 586 throw new ArgumentError(); |
587 } | 587 } |
588 @fletch.native void _free() { | 588 @dartino.native void _free() { |
589 throw new ArgumentError(); | 589 throw new ArgumentError(); |
590 } | 590 } |
591 } | 591 } |
592 | 592 |
593 // NOTE We could make this a view on a memory object instead. | 593 // NOTE We could make this a view on a memory object instead. |
594 class Struct extends ForeignMemory { | 594 class Struct extends ForeignMemory { |
595 final wordSize; | 595 final wordSize; |
596 int get numFields => length ~/ wordSize; | 596 int get numFields => length ~/ wordSize; |
597 | 597 |
598 Struct(int fields) | 598 Struct(int fields) |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 Struct32.fromAddress(int address, int fields) | 654 Struct32.fromAddress(int address, int fields) |
655 : super.fromAddressWithWordSize(address, fields, 4); | 655 : super.fromAddressWithWordSize(address, fields, 4); |
656 } | 656 } |
657 | 657 |
658 class Struct64 extends Struct { | 658 class Struct64 extends Struct { |
659 Struct64(int fields) : super.withWordSize(fields, 8); | 659 Struct64(int fields) : super.withWordSize(fields, 8); |
660 Struct64.finalized(int fields) : super.withWordSizeFinalized(fields, 8); | 660 Struct64.finalized(int fields) : super.withWordSizeFinalized(fields, 8); |
661 Struct64.fromAddress(int address, int fields) | 661 Struct64.fromAddress(int address, int fields) |
662 : super.fromAddressWithWordSize(address, fields, 8); | 662 : super.fromAddressWithWordSize(address, fields, 8); |
663 } | 663 } |
OLD | NEW |