-
Notifications
You must be signed in to change notification settings - Fork 39
/
conv.go
executable file
·367 lines (293 loc) · 7.33 KB
/
conv.go
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2017~2022 The Bottos Authors
// This file is part of the Bottos Chain library.
// Created by Rocket Core Team of Bottos.
// This program is free software: you can distribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Bottos. If not, see <http://www.gnu.org/licenses/>.
// Copyright 2017 The go-interpreter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
* file description: convert variable
* @Author: Stewart Li
* @Date: 2017-12-07
* @Last Modified by:
* @Last Modified time:
*/
package exec
import (
"bytes"
"encoding/binary"
"math"
"strconv"
"fmt"
)
func (vm *VM) i32Wrapi64() {
vm.pushUint32(uint32(vm.popUint64()))
}
func (vm *VM) i32TruncSF32() {
vm.pushInt32(int32(math.Trunc(float64(vm.popFloat32()))))
}
func (vm *VM) i32TruncUF32() {
vm.pushUint32(uint32(math.Trunc(float64(vm.popFloat32()))))
}
func (vm *VM) i32TruncSF64() {
vm.pushInt32(int32(math.Trunc(vm.popFloat64())))
}
func (vm *VM) i32TruncUF64() {
vm.pushUint32(uint32(math.Trunc(vm.popFloat64())))
}
func (vm *VM) i64ExtendSI32() {
vm.pushInt64(int64(vm.popInt32()))
}
func (vm *VM) i64ExtendUI32() {
vm.pushUint64(uint64(vm.popUint32()))
}
func (vm *VM) i64TruncSF32() {
vm.pushInt64(int64(math.Trunc(float64(vm.popFloat32()))))
}
func (vm *VM) i64TruncUF32() {
vm.pushUint64(uint64(math.Trunc(float64(vm.popFloat32()))))
}
func (vm *VM) i64TruncSF64() {
vm.pushInt64(int64(math.Trunc(vm.popFloat64())))
}
func (vm *VM) i64TruncUF64() {
vm.pushUint64(uint64(math.Trunc(vm.popFloat64())))
}
func (vm *VM) f32ConvertSI32() {
vm.pushFloat32(float32(vm.popInt32()))
}
func (vm *VM) f32ConvertUI32() {
vm.pushFloat32(float32(vm.popUint32()))
}
func (vm *VM) f32ConvertSI64() {
vm.pushFloat32(float32(vm.popInt64()))
}
func (vm *VM) f32ConvertUI64() {
vm.pushFloat32(float32(vm.popUint64()))
}
func (vm *VM) f32DemoteF64() {
vm.pushFloat32(float32(vm.popFloat64()))
}
func (vm *VM) f64ConvertSI32() {
vm.pushFloat64(float64(vm.popInt32()))
}
func (vm *VM) f64ConvertUI32() {
vm.pushFloat64(float64(vm.popUint32()))
}
func (vm *VM) f64ConvertSI64() {
vm.pushFloat64(float64(vm.popInt64()))
}
func (vm *VM) f64ConvertUI64() {
vm.pushFloat64(float64(vm.popUint64()))
}
func (vm *VM) f64PromoteF32() {
vm.pushFloat64(float64(vm.popFloat32()))
}
// BytesToString convert bytes to string
func BytesToString(bytes []byte) string {
for i, b := range bytes {
if b == 0 {
return string(bytes[:i])
}
}
return string(bytes)
}
// F32ToBytes convert float32 to bytes
func F32ToBytes(f32 float32) []byte {
bytes := make([]byte, 4)
bits := math.Float32bits(f32)
binary.LittleEndian.PutUint32(bytes, bits)
return bytes
}
// BytesToF32 convert bytes to float32
func BytesToF32(b []byte) float32 {
f32 := math.Float32frombits(binary.LittleEndian.Uint32(b))
return f32
}
// F64ToBytes convert float64 to bytes
func F64ToBytes(f64 float64) []byte {
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes, math.Float64bits(f64))
return bytes
}
// BytesToF64 convert bytes to float64
func BytesToF64(b []byte) float64 {
f64 := math.Float64frombits(binary.LittleEndian.Uint64(b))
return f64
}
// I32ToBytes convert int32 to bytes
func I32ToBytes(i32 uint32) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.LittleEndian, i32)
return bytesBuffer.Bytes()
}
// I64ToBytes convert int64 to bytes
func I64ToBytes(i64 uint64) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.LittleEndian, i64)
return bytesBuffer.Bytes()
}
// ByteToFloat64 convert byte to float64
func ByteToFloat64(bytes []byte) float64 {
bits := binary.LittleEndian.Uint64(bytes)
return math.Float64frombits(bits)
}
func (vm *VM) StrLen(pos uint64) uint64 {
if pos <= 0 {
return 0
}
if vm.sourceFile == JS {
//if source file is js file , the byte array's first element will be array's length
return uint64(vm.memory[pos])
}
var i uint64 = pos
for {
if vm.memory[i] == 0 {
break
}
i++
}
return (i - pos)
}
//for js
func ConvertStr (param []byte , pos uint64 , length uint64) ([]byte , error) {
var i uint64 = pos + 4
var cnt uint64 = 0
var res []byte
if length == 0 {
return nil , ERR_EMPTY_INVALID_PARAM
}
for {
res = append(res , param[i])
i += 2
cnt += 1
if cnt == length {
break
}
}
return res , nil
}
func UnConvertStr(param []byte , pos uint64 , length uint64) ([]byte , error) {
dLen := len(param)
nLen := uint64(3 + dLen * 2)
var value []byte
var i uint64 = 3
var j uint64 = 0
value = append(value , byte(dLen))
value = append(value , 0)
value = append(value , 0)
for {
if i % 2 == 0 {
value = append(value, param[j])
j++
} else {
value = append(value, 0)
}
if i == nLen - 1 {
break
}
i++
}
return value ,nil
}
//for common
func Convert(vm *VM , pos uint64 , length uint64) ([]byte , error) {
if vm.memory == nil || pos < 0 || length < 0 {
return nil , ERR_EMPTY_INVALID_PARAM
}
vmLen := uint64(len(vm.memory))
if pos >= vmLen || pos + length >= vmLen {
return nil , ERR_EMPTY_INVALID_PARAM
}
var value []byte
var err error
if vm.sourceFile == CPP {
value = vm.memory[pos : pos + length]
} else if vm.sourceFile == JS {
value , err = ConvertStr(vm.memory , pos , length)
if err != nil {
return nil , err
}
} else if vm.sourceFile == PY {
//it will be implemented in the feature
}
return value , nil
}
func RemoveElement(slice []byte, start, end uint64) []byte {
return append(slice[:start], slice[end:]...)
}
func ByteArrToNum(numArray []byte) (byte , error) {
num ,err := strconv.Atoi(string(numArray))
if err != nil{
fmt.Println("*ERROR* Failed to convert number from string !!!")
return 0 , err
}
return byte(num) , nil
}
//for js
func PackStrToByteArray(vm *VM, pos uint64 , length uint64) ([]byte , error) {
if length == 0 {
return nil , ERR_EMPTY_INVALID_PARAM
}
arrByte , err := ConvertStr(vm.memory , pos , length)
if err != nil {
return nil , err
}
var i uint64 = 0
var idx uint64 = 0
var tmp []byte
var res []byte
var num byte = 0
for {
if arrByte[i] == 44 { // 44 = ','
if len(tmp) == 0 {
continue
}
if num ,err = ByteArrToNum(tmp) ; err != nil {
return nil , err
}
res = append(res , num)
tmp = RemoveElement(tmp , 0 , idx)
idx = 0
} else {
tmp = append(tmp , arrByte[i])
idx += 1
}
i += 1
if i == length {
if num ,err = ByteArrToNum(tmp) ; err != nil {
return nil , err
}
res = append(res , num)
break
}
}
return res , nil
}
//for js
func ConvertU8Byte (param []byte , pos uint64 , length uint64) ([]byte , error) {
var i uint64 = pos + 16
var cnt uint64 = 0
var res []byte
if length == 0 {
return nil , ERR_EMPTY_INVALID_PARAM
}
for {
res = append(res , param[i])
i += 4
cnt += 1
if cnt == length {
break
}
}
return res , nil
}