11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/builtins/builtins-utils-inl.h"
61cb0ef41Sopenharmony_ci#include "src/builtins/builtins.h"
71cb0ef41Sopenharmony_ci#include "src/codegen/code-factory.h"
81cb0ef41Sopenharmony_ci#include "src/codegen/compiler.h"
91cb0ef41Sopenharmony_ci#include "src/logging/counters.h"
101cb0ef41Sopenharmony_ci#include "src/objects/objects-inl.h"
111cb0ef41Sopenharmony_ci#include "src/strings/uri.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// ES6 section 18.2.6.2 decodeURI (encodedURI)
171cb0ef41Sopenharmony_ciBUILTIN(GlobalDecodeURI) {
181cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
191cb0ef41Sopenharmony_ci  Handle<String> encoded_uri;
201cb0ef41Sopenharmony_ci  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
211cb0ef41Sopenharmony_ci      isolate, encoded_uri,
221cb0ef41Sopenharmony_ci      Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(isolate, Uri::DecodeUri(isolate, encoded_uri));
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci// ES6 section 18.2.6.3 decodeURIComponent (encodedURIComponent)
281cb0ef41Sopenharmony_ciBUILTIN(GlobalDecodeURIComponent) {
291cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
301cb0ef41Sopenharmony_ci  Handle<String> encoded_uri_component;
311cb0ef41Sopenharmony_ci  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
321cb0ef41Sopenharmony_ci      isolate, encoded_uri_component,
331cb0ef41Sopenharmony_ci      Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(
361cb0ef41Sopenharmony_ci      isolate, Uri::DecodeUriComponent(isolate, encoded_uri_component));
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci// ES6 section 18.2.6.4 encodeURI (uri)
401cb0ef41Sopenharmony_ciBUILTIN(GlobalEncodeURI) {
411cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
421cb0ef41Sopenharmony_ci  Handle<String> uri;
431cb0ef41Sopenharmony_ci  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
441cb0ef41Sopenharmony_ci      isolate, uri, Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(isolate, Uri::EncodeUri(isolate, uri));
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci// ES6 section 18.2.6.5 encodeURIComponenet (uriComponent)
501cb0ef41Sopenharmony_ciBUILTIN(GlobalEncodeURIComponent) {
511cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
521cb0ef41Sopenharmony_ci  Handle<String> uri_component;
531cb0ef41Sopenharmony_ci  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
541cb0ef41Sopenharmony_ci      isolate, uri_component,
551cb0ef41Sopenharmony_ci      Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(isolate,
581cb0ef41Sopenharmony_ci                           Uri::EncodeUriComponent(isolate, uri_component));
591cb0ef41Sopenharmony_ci}
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci// ES6 section B.2.1.1 escape (string)
621cb0ef41Sopenharmony_ciBUILTIN(GlobalEscape) {
631cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
641cb0ef41Sopenharmony_ci  Handle<String> string;
651cb0ef41Sopenharmony_ci  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
661cb0ef41Sopenharmony_ci      isolate, string,
671cb0ef41Sopenharmony_ci      Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(isolate, Uri::Escape(isolate, string));
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci// ES6 section B.2.1.2 unescape (string)
731cb0ef41Sopenharmony_ciBUILTIN(GlobalUnescape) {
741cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
751cb0ef41Sopenharmony_ci  Handle<String> string;
761cb0ef41Sopenharmony_ci  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
771cb0ef41Sopenharmony_ci      isolate, string,
781cb0ef41Sopenharmony_ci      Object::ToString(isolate, args.atOrUndefined(isolate, 1)));
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(isolate, Uri::Unescape(isolate, string));
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci// ES6 section 18.2.1 eval (x)
841cb0ef41Sopenharmony_ciBUILTIN(GlobalEval) {
851cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
861cb0ef41Sopenharmony_ci  Handle<Object> x = args.atOrUndefined(isolate, 1);
871cb0ef41Sopenharmony_ci  Handle<JSFunction> target = args.target();
881cb0ef41Sopenharmony_ci  Handle<JSObject> target_global_proxy(target->global_proxy(), isolate);
891cb0ef41Sopenharmony_ci  if (!Builtins::AllowDynamicFunction(isolate, target, target_global_proxy)) {
901cb0ef41Sopenharmony_ci    isolate->CountUsage(v8::Isolate::kFunctionConstructorReturnedUndefined);
911cb0ef41Sopenharmony_ci    return ReadOnlyRoots(isolate).undefined_value();
921cb0ef41Sopenharmony_ci  }
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  // Run embedder pre-checks before executing eval. If the argument is a
951cb0ef41Sopenharmony_ci  // non-String (or other object the embedder doesn't know to handle), then
961cb0ef41Sopenharmony_ci  // return it directly.
971cb0ef41Sopenharmony_ci  MaybeHandle<String> source;
981cb0ef41Sopenharmony_ci  bool unhandled_object;
991cb0ef41Sopenharmony_ci  std::tie(source, unhandled_object) =
1001cb0ef41Sopenharmony_ci      Compiler::ValidateDynamicCompilationSource(
1011cb0ef41Sopenharmony_ci          isolate, handle(target->native_context(), isolate), x);
1021cb0ef41Sopenharmony_ci  if (unhandled_object) return *x;
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci  Handle<JSFunction> function;
1051cb0ef41Sopenharmony_ci  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
1061cb0ef41Sopenharmony_ci      isolate, function,
1071cb0ef41Sopenharmony_ci      Compiler::GetFunctionFromValidatedString(
1081cb0ef41Sopenharmony_ci          handle(target->native_context(), isolate), source,
1091cb0ef41Sopenharmony_ci          NO_PARSE_RESTRICTION, kNoSourcePosition));
1101cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(
1111cb0ef41Sopenharmony_ci      isolate,
1121cb0ef41Sopenharmony_ci      Execution::Call(isolate, function, target_global_proxy, 0, nullptr));
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci}  // namespace internal
1161cb0ef41Sopenharmony_ci}  // namespace v8
117