forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
153 lines (131 loc) · 3.97 KB
/
file.js
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
/**
* @constructor
* @param {Sk.builtin.str} name
* @param {Sk.builtin.str} mode
* @param {Object} buffering
*/
Sk.builtin.file = function (name, mode, buffering) {
var i;
var elem;
this.mode = mode;
this.name = name;
this.closed = false;
if (Sk.inBrowser) { // todo: Maybe provide a replaceable function for non-import files
elem = document.getElementById(name.v);
if (elem == null) {
throw new Sk.builtin.IOError("[Errno 2] No such file or directory: '" + name.v + "'");
} else {
if (elem.nodeName.toLowerCase() == "textarea") {
this.data$ = elem.value;
}
else {
this.data$ = elem.textContent;
}
}
} else {
this.data$ = Sk.read(name.v);
}
this.lineList = this.data$.split("\n");
this.lineList = this.lineList.slice(0, -1);
for (i in this.lineList) {
this.lineList[i] = this.lineList[i] + "\n";
}
this.currentLine = 0;
this.pos$ = 0;
this.__class__ = Sk.builtin.file;
return this;
};
Sk.builtin.file.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj("file", Sk.builtin.file);
Sk.builtin.file.prototype.tp$getattr = Sk.builtin.object.prototype.GenericGetAttr;
Sk.builtin.file.prototype["$r"] = function () {
return new Sk.builtin.str("<" +
(this.closed ? "closed" : "open") +
"file '" +
this.name +
"', mode '" +
this.mode +
"'>");
};
Sk.builtin.file.prototype.tp$iter = function () {
var allLines = this.lineList;
var ret =
{
tp$iter : function () {
return ret;
},
$obj : this,
$index : 0,
$lines : allLines,
tp$iternext: function () {
if (ret.$index >= ret.$lines.length) {
return undefined;
}
return new Sk.builtin.str(ret.$lines[ret.$index++]);
}
};
return ret;
};
Sk.builtin.file.prototype["close"] = new Sk.builtin.func(function (self) {
self.closed = true;
});
Sk.builtin.file.prototype["flush"] = new Sk.builtin.func(function (self) {
});
Sk.builtin.file.prototype["fileno"] = new Sk.builtin.func(function (self) {
return 10;
}); // > 0, not 1/2/3
Sk.builtin.file.prototype["isatty"] = new Sk.builtin.func(function (self) {
return false;
});
Sk.builtin.file.prototype["read"] = new Sk.builtin.func(function (self, size) {
var ret;
var len;
if (self.closed) {
throw new Sk.builtin.ValueError("I/O operation on closed file");
}
len = self.data$.length;
if (size === undefined) {
size = len;
}
ret = new Sk.builtin.str(self.data$.substr(self.pos$, size));
self.pos$ += size;
if (self.pos$ >= len) {
self.pos$ = len;
}
return ret;
});
Sk.builtin.file.prototype["readline"] = new Sk.builtin.func(function (self, size) {
var line = "";
if (self.currentLine < self.lineList.length) {
line = self.lineList[self.currentLine];
self.currentLine++;
}
return new Sk.builtin.str(line);
});
Sk.builtin.file.prototype["readlines"] = new Sk.builtin.func(function (self, sizehint) {
var i;
var arr = [];
for (i = self.currentLine; i < self.lineList.length; i++) {
arr.push(new Sk.builtin.str(self.lineList[i]));
}
return new Sk.builtin.list(arr);
});
Sk.builtin.file.prototype["seek"] = new Sk.builtin.func(function (self, offset, whence) {
if (whence === undefined) {
whence = 1;
}
if (whence == 1) {
self.pos$ = offset;
} else {
self.pos$ = self.data$ + offset;
}
});
Sk.builtin.file.prototype["tell"] = new Sk.builtin.func(function (self) {
return self.pos$;
});
Sk.builtin.file.prototype["truncate"] = new Sk.builtin.func(function (self, size) {
goog.asserts.fail();
});
Sk.builtin.file.prototype["write"] = new Sk.builtin.func(function (self, str) {
goog.asserts.fail();
});
goog.exportSymbol("Sk.builtin.file", Sk.builtin.file);