Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: lib/ffi/ffi.dart

Issue 1209033003: Work in progres, please take a look and give early feedback if this is the way we want to structure… (Closed) Base URL: git@github.com:dart-lang/fletch.git@master
Patch Set: remove accidentally added stuff Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/shared/natives.h » ('j') | src/vm/ffi_macos.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 {
ricow1 2015/06/29 16:55:23 This will be renamed Foreign once we agree on the
13 int _value;
14 int get value => _value;
15
16 Foreign1._(this._value);
17
18 static const int UNKNOWN = 0;
19
20 static const int LINUX = 1;
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 {
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) {
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) {
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) =>
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 {
167 int _length;
168
169 ForeignMemory.fromForeignPointer(ForeignPointer pointer, int this._length) :
170 super.fromAddress(pointer.value);
171
172 int getInt8(int offset)
173 => Foreign._getInt8(_computeAddress(offset, 1));
ricow1 2015/06/29 16:55:23 all the natives will move into this class
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.
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);
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
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 }
OLDNEW
« no previous file with comments | « no previous file | src/shared/natives.h » ('j') | src/vm/ffi_macos.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698