-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmemory.js
More file actions
97 lines (83 loc) · 2.44 KB
/
memory.js
File metadata and controls
97 lines (83 loc) · 2.44 KB
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
var memoryJS = {
name: 'A memoryJS base global scoped object'
}
class Pointer {
constructor (address = '0' /* address string */, local = false) {
var _value = address
var _local = local
this.isLocal = function () {
return _local
}
this.value = function () {
return _value
}
}
pointedTo (MemoryObj /* in case of Local Memory object, pass it. Otherwise, leave it */) {
if (this.isLocal()) {
return MemoryObj.valueOf(this.value())
} else {
return memoryJS.publicMemoryObj.valueOf(this.value())
}
}
changeValue (object, MemoryObj /* in case of Local Memory object, pass it. Otherwise, leave it */) {
if (this.isLocal()) {
MemoryObj.changeValue(this.value(), object)
} else {
memoryJS.publicMemoryObj.changeValue(this.value(), object)
}
return 0
}
free (MemoryObj /* in case of Local Memory object, pass it. Otherwise, leave it */) {
if (this.isLocal()) {
MemoryObj.free(this.value())
} else {
memoryJS.publicMemoryObj.free()
}
return null
}
set point (object) {
if (this.isLocal()) {
throw new Error('Pointer.point shorthand is only applicable for global pointers. Use changeValue() function instead')
} else {
this.changeValue(object)
}
}
get point () {
if (this.isLocal()) {
throw new Error('Pointer.point shorthand is only applicable for global pointers. Use pointedTo() function instead')
} else {
return this.pointedTo()
}
}
}
class Memory {
constructor (global = false) {
var _endaddr = 0
var _objbook = []
this.newobj = function (object /* object */) {
_objbook.push(object)
_endaddr = _endaddr + 1
if (global) {
return new Pointer((_endaddr - 1).toString(36))
} else {
return new Pointer((_endaddr - 1).toString(36), true)
}
}
this.valueOf = function (address = '0' /* string */) {
return _objbook[parseInt(address, 36)]
}
this.changeValue = function (address, object) {
_objbook[parseInt(address, 36)] = object
return 0
}
this.free = function (address = '0' /* string */) {
_objbook[parseInt(address, 36)] = null
return 0
}
this.nullptr = this.newobj(null)
}
}
memoryJS.publicMemoryObj = new Memory(true)
if (typeof window === 'undefined') {
module.exports = memoryJS
}