-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataview.ina
204 lines (189 loc) · 6.52 KB
/
dataview.ina
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
isi DataView = {
// Example:
// ```
// muat("api_dataview.js");
// isi calloc = ffi('void *calloc(int, int)');
// isi ptr = calloc(100, 1);
// isi dw = DataView.create(ptr, 0, 100);
//
// dw.setUint8(2, 0xff);
// ```
create: fungsi(buf, off, len) {
let ret = Object.create(this._prot);
if (off !== undefined) {
buf += off;
}
ret._buf = buf;
ret._len = len;
return ret;
},
_prot: {
// ## **`myDW.getInt8(idx)`**
// Get a signed byte value from the dataview's buffer at the given index
// `idx`. Returned value: a number from -128 to 127.
getInt8: function(idx) {
if (!DataView._cl(idx, this._len, 1)) {
return undefined;
}
return DataView._gets(
DataView._pk(this._buf, idx), 1, false
);
},
// ## **`myDW.getUint8(idx)`**
// Get an unsigned byte value from the dataview's buffer at the given index
// `idx`. Returned value: a number from 0 to 255.
getUint8: function(idx) {
if (!DataView._cl(idx, this._len, 1)) {
return undefined;
}
return DataView._getu(
DataView._pk(this._buf, idx), 1, false
);
},
// ## **`myDW.getInt16(idx, le)`**
// Get a signed 2-byte value from the dataview's buffer at the given index
// `idx`. By default the data interpreted as big-endian; if `le` is true,
// then little-endian will be used.
// Returned value: a number from -32768 to 32767.
getInt16: function(idx, le) {
if (!DataView._cl(idx, this._len, 2)) {
return undefined;
}
return DataView._gets(
DataView._pk(this._buf, idx), 2, !le
);
},
// ## **`myDW.getUint16(idx, le)`**
// Get an unsigned 2-byte value from the dataview's buffer at the given
// index `idx`. By default the data interpreted as big-endian; if `le` is
// true, then little-endian will be used.
// Returned value: a number from 0 to 65535.
getUint16: function(idx, le) {
if (!DataView._cl(idx, this._len, 2)) {
return undefined;
}
return DataView._getu(
DataView._pk(this._buf, idx), 2, !le
);
},
// ## **`myDW.getInt32(idx, le)`**
// Get a signed 4-byte value from the dataview's buffer at the given index
// `idx`. By default the data interpreted as big-endian; if `le` is true,
// then little-endian will be used.
// Returned value: a number from -2147483648 to 2147483647.
getInt32: function(idx, le) {
if (!DataView._cl(idx, this._len, 4)) {
return undefined;
}
return DataView._gets(
DataView._pk(this._buf, idx), 4, !le
);
},
// ## **`myDW.getUint32(idx, le)`**
// Get an unsigned 4-byte value from the dataview's buffer at the given
// index `idx`. By default the data interpreted as big-endian; if `le` is
// true, then little-endian will be used.
// Returned value: a number from 0 to 4294967295.
getUint32: function(idx, le) {
if (!DataView._cl(idx, this._len, 4)) {
return undefined;
}
return DataView._getu(
DataView._pk(this._buf, idx), 4, !le
);
},
// ## **`myDW.setInt8(idx, val)`**
// Set a signed byte value into the dataview's buffer at the given index
// `idx`. `val` should be a number from -128 to 127.
// Returned value: none.
setInt8: function(idx, val) {
if (!DataView._cl(idx, this._len, 1)) {
return undefined;
}
DataView._setu(
DataView._pk(this._buf, idx), val, 1, false
);
},
// ## **`myDW.setUint8(idx, val)`**
// Set an unsigned byte value into the dataview's buffer at the given index
// `idx`. `val` should be a number from -128 to 127.
// Returned value: none.
setUint8: function(idx, val) {
if (!DataView._cl(idx, this._len, 1)) {
return undefined;
}
DataView._setu(
DataView._pk(this._buf, idx), val, 1, false
);
},
// ## **`myDW.setInt16(idx, val, le)`**
// Set a signed 2-byte value into the dataview's buffer at the given index
// `idx`. `val` should be a number from -32768 to 32767. By default the
// data is written in big-endian format; if `le` is true, then
// little-endian will be used.
// Returned value: none.
setInt16: function(idx, val, le) {
if (!DataView._cl(idx, this._len, 2)) {
return undefined;
}
DataView._setu(
DataView._pk(this._buf, idx), val, 2, !le
);
},
// ## **`myDW.setUint16(idx, val, le)`**
// Set an unsigned 2-byte value into the dataview's buffer at the given
// index `idx`. `val` should be a number from 0 to 65535. By default the
// data is written in big-endian format; if `le` is true, then
// little-endian will be used.
// Returned value: none.
setUint16: function(idx, val, le) {
if (!DataView._cl(idx, this._len, 2)) {
return undefined;
}
DataView._setu(
DataView._pk(this._buf, idx), val, 2, !le
);
},
// ## **`myDW.setInt32(idx, val, le)`**
// Set a signed 4-byte value into the dataview's buffer at the given index
// `idx`. `val` should be a number from -2147483648 to 2147483647. By
// default the data is written in big-endian format; if `le` is true, then
// little-endian will be used.
// Returned value: none.
setInt32: function(idx, val, le) {
if (!DataView._cl(idx, this._len, 4)) {
return undefined;
}
DataView._setu(
DataView._pk(this._buf, idx), val, 4, !le
);
},
// ## **`myDW.setUint32(idx, val, le)`**
// Set an unsigned 4-byte value into the dataview's buffer at the given
// index `idx`. `val` should be a number from 0 to 4294967295. By default
// the data is written in big-endian format; if `le` is true, then
// little-endian will be used.
// Returned value: none.
setUint32: function(idx, val, le) {
if (!DataView._cl(idx, this._len, 4)) {
return undefined;
}
DataView._setu(
DataView._pk(this._buf, idx), val, 4, !le
);
},
},
_cl: function(idx, len, ilen) {
if (len !== undefined && idx + ilen > len) {
die(DataView._errm);
return false;
}
return true;
},
_p: ffi('void *baik_mem_to_ptr(int)'),
_pk: ffi('void *baik_mem_get_ptr(void *, int)'),
_getu: ffi('double baik_mem_get_uint(void *, int, int)'),
_gets: ffi('double baik_mem_get_int(void *, int, int)'),
_setu: ffi('void baik_mem_set_uint(void *, int, int, int)'),
_errm: "Offset is outside of the bounds of the DataView",
};