catvm/CatVm2/browser/Storage.js
Big1moster d843fcb236 dsf
2023-02-06 11:36:48 +08:00

60 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 从浏览器中知道Storage是全局的且原型链只是一层因此比较好伪造window有多层所以要伪造多层
// 浏览器中new会报错因此我们此处也需要报错
var Storage = function Storage() { // 构造函数
throw new TypeError("Illegal constructor");
};
catvm.safefunction(Storage);
// 浏览器
Object.defineProperties(Storage.prototype, {
[Symbol.toStringTag]: {
value: "Storage",
configurable: true
}
});
var localStorage = {};
localStorage.__proto__ = Storage.prototype;
////////// 浏览器代码自动生成部分
function get_length() {
return Object.keys(catvm.memory.storage).length;
}
Storage.prototype.length = get_length();
Storage.prototype.key = function key(index) {
return Object.keys(catvm.memory.storage)[index];
};
catvm.safefunction(Storage.prototype.key);
Storage.prototype.getItem = function getItem(keyName) {
var result = catvm.memory.storage[keyName];
if (result) {
return result;
} else {
return null;
}
};
catvm.safefunction(Storage.prototype.getItem);
Storage.prototype.setItem = function setItem(keyName, keyValue) {
catvm.memory.storage[keyName] = keyValue;
};
catvm.safefunction(Storage.prototype.setItem);
Storage.prototype.removeItem = function removeItem(keyName) {
delete catvm.memory.storage[keyName];
};
catvm.safefunction(Storage.prototype.removeItem);
Storage.prototype.clear = function clear() {
catvm.memory.storage = {};
};
catvm.safefunction(Storage.prototype.clear);
////////
// 代理一般挂在实例上
localStorage = catvm.proxy(localStorage);
Storage = catvm.proxy(Storage);