-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
131 lines (115 loc) · 4 KB
/
index.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
const ajax = require("./ajax");
function toJSON(vm, vValue) {
const target = vm.isArray(vValue) ? [] : {};
const global = vm.getGlobal();
const vObject = vm.getProperty(global, "Object");
const vKeysFunc = vm.getProperty(vObject, "keys");
const vKeys = vm.call(vKeysFunc, vm.createUndefined(), vValue);
const vLength = vm.getProperty(vKeys, "length");
const length = vm.asNumber(vLength);
for (let i = 0; i < length; i++) {
const vKey = vm.getProperty(vKeys, i);
const key = vm.asString(vKey);
const vValueItem = vm.getProperty(vValue, key);
if (vm.isUndefined(vValueItem)) {
target[key] = undefined;
} else if (vm.isNull(vValueItem)) {
target[key] = null;
} else if (vm.isBoolean(vValueItem)) {
target[key] = vm.asBoolean(vValueItem);
} else if (vm.isNumber(vValueItem)) {
target[key] = vm.asNumber(vValueItem);
} else if (vm.isString(vValueItem)) {
target[key] = vm.asString(vValueItem);
} else if (vm.isObject(vValueItem)) {
target[key] = toJSON(vm, vValueItem);
}
}
return target;
}
function fromJSON(vm, json) {
const vObject = json.length ? vm.createArray() : vm.createObject();
for (let key in json) {
if (!json.hasOwnProperty(key)) {
continue;
}
const value = json[key];
if (value === undefined) {
vm.setProperty(vObject, key, vm.createUndefined());
} else if (value === null) {
vm.setProperty(vObject, key, vm.createNull());
} else if (typeof value === "boolean") {
vm.setProperty(vObject, key, vm.createBoolean(value));
} else if (typeof value === "number") {
vm.setProperty(vObject, key, vm.createNumber(value));
} else if (typeof value === "string") {
vm.setProperty(vObject, key, vm.createString(value));
} else if (typeof value === "object") {
vm.setProperty(vObject, key, fromJSON(vm, value));
}
}
return vObject;
}
module.exports = (vm) => {
const global = vm.getGlobal();
const vAjax = vm.createFunction("ajax", function (vOption) {
const option = {};
const vUrl = vm.getProperty(vOption, "url");
const vXdrURL = vm.getProperty(vOption, "xdrURL");
const vType = vm.getProperty(vOption, "type");
const vMethod = vm.getProperty(vOption, "method");
const vWithCredentials = vm.getProperty(vOption, "withCredentials");
const vJSONP = vm.getProperty(vOption, "jsonp");
const vHeaders = vm.getProperty(vOption, "headers");
const vData = vm.getProperty(vOption, "data");
const vSuccess = vm.getProperty(vOption, "success");
const vError = vm.getProperty(vOption, "error");
if (vm.isString(vUrl)) {
option.url = vm.asString(vUrl);
}
if (vm.isString(vXdrURL)) {
option.xdrURL = vm.asString(vXdrURL);
}
if (vm.isString(vType)) {
option.type = vm.asString(vType);
}
if (vm.isString(vMethod)) {
option.method = vm.asString(vMethod);
}
if (vm.isBoolean(vWithCredentials)) {
option.withCredentials = vm.asBoolean(vWithCredentials);
}
if (vm.isString(vJSONP)) {
option.jsonp = vm.asString(vJSONP);
}else if (vm.isBoolean(vJSONP)) {
option.jsonp = vm.asBoolean(vJSONP);
}
if (vm.isObject(vHeaders)) {
option.headers = toJSON(vm, vHeaders);
}
if (vm.isString(vData)) {
option.data = vm.asString(vData);
} else if (vm.isObject(vData)) {
option.data = toJSON(vm, vData);
}
if (vm.isFunction(vSuccess)) {
option.success = function (response, headers) {
vm.call(
vSuccess,
vm.createUndefined(),
typeof response === "string" ? vm.createString(response) : fromJSON(vm, response),
fromJSON(vm, headers)
);
};
}
if (vm.isFunction(vError)) {
option.error = function (readyState, status) {
vm.call(vError, vm.createUndefined(), vm.createNumber(readyState), vm.createNumber(status));
};
}
ajax(option);
return vm.createUndefined();
});
vm.setProperty(global, "ajax", vAjax);
};
module.exports.ajax = ajax;