1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/builtins/builtins-utils-inl.h"
6 #include "src/builtins/builtins.h"
7 #include "src/codegen/code-factory.h"
8 #include "src/codegen/compiler.h"
9 #include "src/logging/counters.h"
10 #include "src/objects/objects-inl.h"
11 #include "src/strings/uri.h"
12
13 namespace v8 {
14 namespace internal {
15
16 // ES6 section 18.2.6.2 decodeURI (encodedURI)
BUILTIN(GlobalDecodeURI)17 BUILTIN(GlobalDecodeURI) {
18 HandleScope scope(isolate);
19 Handle<String> encoded_uri;
20 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
21 isolate, encoded_uri,
22 Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
23
24 RETURN_RESULT_OR_FAILURE(isolate, Uri::DecodeUri(isolate, encoded_uri));
25 }
26
27 // ES6 section 18.2.6.3 decodeURIComponent (encodedURIComponent)
BUILTIN(GlobalDecodeURIComponent)28 BUILTIN(GlobalDecodeURIComponent) {
29 HandleScope scope(isolate);
30 Handle<String> encoded_uri_component;
31 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
32 isolate, encoded_uri_component,
33 Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
34
35 RETURN_RESULT_OR_FAILURE(
36 isolate, Uri::DecodeUriComponent(isolate, encoded_uri_component));
37 }
38
39 // ES6 section 18.2.6.4 encodeURI (uri)
BUILTIN(GlobalEncodeURI)40 BUILTIN(GlobalEncodeURI) {
41 HandleScope scope(isolate);
42 Handle<String> uri;
43 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
44 isolate, uri, Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
45
46 RETURN_RESULT_OR_FAILURE(isolate, Uri::EncodeUri(isolate, uri));
47 }
48
49 // ES6 section 18.2.6.5 encodeURIComponenet (uriComponent)
BUILTIN(GlobalEncodeURIComponent)50 BUILTIN(GlobalEncodeURIComponent) {
51 HandleScope scope(isolate);
52 Handle<String> uri_component;
53 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
54 isolate, uri_component,
55 Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
56
57 RETURN_RESULT_OR_FAILURE(isolate,
58 Uri::EncodeUriComponent(isolate, uri_component));
59 }
60
61 // ES6 section B.2.1.1 escape (string)
BUILTIN(GlobalEscape)62 BUILTIN(GlobalEscape) {
63 HandleScope scope(isolate);
64 Handle<String> string;
65 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
66 isolate, string,
67 Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
68
69 RETURN_RESULT_OR_FAILURE(isolate, Uri::Escape(isolate, string));
70 }
71
72 // ES6 section B.2.1.2 unescape (string)
BUILTIN(GlobalUnescape)73 BUILTIN(GlobalUnescape) {
74 HandleScope scope(isolate);
75 Handle<String> string;
76 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
77 isolate, string,
78 Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
79
80 RETURN_RESULT_OR_FAILURE(isolate, Uri::Unescape(isolate, string));
81 }
82
83 // ES6 section 18.2.1 eval (x)
BUILTIN(GlobalEval)84 BUILTIN(GlobalEval) {
85 HandleScope scope(isolate);
86 Handle<Object> x = args.atOrUndefined(isolate, 1);
87 Handle<JSFunction> target = args.target();
88 Handle<JSObject> target_global_proxy(target->global_proxy(), isolate);
89 if (!Builtins::AllowDynamicFunction(isolate, target, target_global_proxy)) {
90 isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined);
91 return ReadOnlyRoots(isolate).undefined_value();
92 }
93
94 // Run embedder pre-checks before executing eval. If the argument is a
95 // non-String (or other object the embedder doesn't know to handle), then
96 // return it directly.
97 MaybeHandle<String> source;
98 bool unhandled_object;
99 std::tie(source, unhandled_object) =
100 Compiler::ValidateDynamicCompilationSource(
101 isolate, handle(target->native_context(), isolate), x);
102 if (unhandled_object) return *x;
103
104 Handle<JSFunction> function;
105 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
106 isolate, function,
107 Compiler::GetFunctionFromValidatedString(
108 handle(target->native_context(), isolate), source,
109 NO_PARSE_RESTRICTION, kNoSourcePosition));
110 RETURN_RESULT_OR_FAILURE(
111 isolate,
112 Execution::Call(isolate, function, target_global_proxy, 0, nullptr));
113 }
114
115 } // namespace internal
116 } // namespace v8
117