1/*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2012 Bernd Buschinski (b.buschinski@googlemail.com)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#include "json_object.h"
23
24#include "jsonlexer.h"
25#include <config-kjs.h>
26#include "lookup.h"
27#include "array_instance.h"
28#include "jsonstringify.h"
29
30#include "json_object.lut.h"
31
32using namespace KJS;
33
34// ------------------------------ JSONObjectImp --------------------------------
35
36const ClassInfo JSONObjectImp::info = { "JSON", 0, &jsonTable, 0 };
37
38/* Source for json_object.lut.h
39@begin jsonTable 2
40 parse JSONObjectImp::Parse DontEnum|Function 2
41 stringify JSONObjectImp::Stringify DontEnum|Function 3
42@end
43*/
44
45
46JSONObjectImp::JSONObjectImp(ExecState*, ObjectPrototype *objProto)
47 : JSObject(objProto)
48{
49}
50
51bool JSONObjectImp::getOwnPropertySlot(ExecState *exec, const Identifier &propertyName, PropertySlot &slot)
52{
53 // As of ECMA 5.1r6 JSON only has 2 functions, so only functionSlot is needed
54 return getStaticFunctionSlot<JSONFuncImp, JSObject>(exec, &jsonTable, this, propertyName, slot);
55}
56
57// ------------------------------ JSONFuncImp --------------------------------
58
59JSONFuncImp::JSONFuncImp(ExecState* exec, int i, int l, const Identifier& name)
60 : InternalFunctionImp(static_cast<FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()), name),
61 id(i)
62{
63 putDirect(exec->propertyNames().length, l, DontDelete|ReadOnly|DontEnum);
64}
65
66static void reviver(ExecState* exec, JSValue* value, JSObject* func)
67{
68 if (exec->hadException())
69 return;
70
71 JSType type = value->type();
72 switch (type) {
73 case ObjectType: {
74 JSObject* obj = value->getObject();
75 bool isArray = obj->inherits(&ArrayInstance::info);
76 bool validArrayIndex = false;
77
78 PropertyNameArray names;
79 obj->getOwnPropertyNames(exec, names, KJS::PropertyMap::ExcludeDontEnumProperties);
80 const int nameSize = names.size();
81 for (int i = 0; i < nameSize; ++i) {
82 // For Array only take properties that are valid Array indexes
83 if (isArray) {
84 names[i].toArrayIndex(&validArrayIndex);
85 if (!validArrayIndex)
86 continue;
87 }
88
89 JSValue* val = obj->get(exec, names[i]);
90
91 List args;
92 args.append(jsString(names[i].ustring()));
93 args.append(val);
94
95 JSValue* ret = func->call(exec, obj, args);
96 if (exec->hadException())
97 return;
98 if (ret->isUndefined())
99 obj->deleteProperty(exec, names[i]);
100 else {
101 obj->put(exec, names[i], ret);
102 reviver(exec, ret, func);
103 }
104 }
105 break;
106 }
107 case NullType:
108 case NumberType:
109 case BooleanType:
110 case StringType:
111 break;
112 case UnspecifiedType:
113 case GetterSetterType:
114 case UndefinedType:
115 // should never be reached, as JSON doesn't know them
116 // and we only have json data here
117 ASSERT_NOT_REACHED();
118 break;
119 }
120}
121
122JSValue *JSONFuncImp::callAsFunction(ExecState* exec, JSObject* /*thisObj*/, const List& args)
123{
124 switch (id) {
125 case JSONObjectImp::Parse: {
126 if (args.size() < 1)
127 return throwError(exec, SyntaxError, "Invalid JSON Syntax");
128
129 JSONParser parser(args[0]->toString(exec));
130 if (exec->hadException())
131 return jsUndefined();
132 JSValue* val = parser.tryParse(exec);
133
134 if (!val)
135 return throwError(exec, SyntaxError, "Invalid JSON Syntax");
136
137 if (args.size() < 2)
138 return val;
139
140 JSValue* func = args[1];
141 if (func->implementsCall()) {
142 JSObject* function = func->getObject();
143
144 List args;
145 args.append(jsString(""));
146 args.append(val);
147
148 JSObject* jsobjectArg = val->toObject(exec);
149 if (exec->hadException())
150 return jsUndefined();
151 JSValue* ret = function->call(exec, jsobjectArg, args);
152 if (ret->isUndefined())
153 return ret;
154 else {
155 reviver(exec, ret, function);
156 if (exec->hadException())
157 return jsUndefined();
158 }
159 }
160
161 return val;
162 }
163 case JSONObjectImp::Stringify: {
164 JSValue* object = args[0];
165 JSONStringify stringifier(exec, args[1], args[2]);
166
167 JSONStringify::StringifyState state;
168 JSValue* ret = stringifier.stringify(exec, object, state);
169 switch (state) {
170 case JSONStringify::Success:
171 return ret;
172 case JSONStringify::FailedCyclic:
173 return throwError(exec, TypeError, "cyclic object value");
174 case JSONStringify::FailedStackLimitExceeded:
175 return throwError(exec, TypeError, "object stack limit exceeded");
176 case JSONStringify::FailedException:
177 //stringify already got an exception
178 return jsUndefined();
179 }
180 }
181 default:
182 ASSERT_NOT_REACHED();
183 }
184
185 return jsUndefined();
186}
187