1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "js/PropertyDescriptor.h"
8
9 #include "mozilla/Maybe.h" // mozilla::Maybe
10
11 #include <stddef.h> // size_t
12 #include <string.h> // strlen
13
14 #include "jstypes.h" // JS_PUBLIC_API
15 #include "js/Context.h" // js::AssertHeapIsIdle
16 #include "js/Id.h" // jsid
17 #include "js/RootingAPI.h" // JS::Rooted, JS::Handle, JS::MutableHandle
18 #include "vm/JSAtom.h" // JSAtom, Atomize, AtomizeChars
19 #include "vm/JSContext.h" // JSContext, CHECK_THREAD
20 #include "vm/JSObject.h" // JSObject
21 #include "vm/ObjectOperations.h" // GetOwnPropertyDescriptor
22
23 #include "vm/JSAtom-inl.h" // AtomToId
24 #include "vm/JSContext-inl.h" // JSContext::check
25
26 using namespace js;
27
JS_GetOwnPropertyDescriptorById(JSContext * cx,JS::Handle<JSObject * > obj,JS::Handle<jsid> id,JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc)28 JS_PUBLIC_API bool JS_GetOwnPropertyDescriptorById(
29 JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
30 JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc) {
31 AssertHeapIsIdle();
32 CHECK_THREAD(cx);
33 cx->check(obj, id);
34 return GetOwnPropertyDescriptor(cx, obj, id, desc);
35 }
36
JS_GetOwnPropertyDescriptor(JSContext * cx,JS::Handle<JSObject * > obj,const char * name,JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc)37 JS_PUBLIC_API bool JS_GetOwnPropertyDescriptor(
38 JSContext* cx, JS::Handle<JSObject*> obj, const char* name,
39 JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc) {
40 JSAtom* atom = Atomize(cx, name, strlen(name));
41 if (!atom) {
42 return false;
43 }
44 JS::Rooted<jsid> id(cx, AtomToId(atom));
45 return JS_GetOwnPropertyDescriptorById(cx, obj, id, desc);
46 }
47
JS_GetOwnUCPropertyDescriptor(JSContext * cx,JS::Handle<JSObject * > obj,const char16_t * name,size_t namelen,JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc)48 JS_PUBLIC_API bool JS_GetOwnUCPropertyDescriptor(
49 JSContext* cx, JS::Handle<JSObject*> obj, const char16_t* name,
50 size_t namelen,
51 JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc) {
52 JSAtom* atom = AtomizeChars(cx, name, namelen);
53 if (!atom) {
54 return false;
55 }
56 JS::Rooted<jsid> id(cx, AtomToId(atom));
57 return JS_GetOwnPropertyDescriptorById(cx, obj, id, desc);
58 }
59
JS_GetPropertyDescriptorById(JSContext * cx,JS::Handle<JSObject * > obj,JS::Handle<jsid> id,JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc,JS::MutableHandle<JSObject * > holder)60 JS_PUBLIC_API bool JS_GetPropertyDescriptorById(
61 JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
62 JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc,
63 JS::MutableHandle<JSObject*> holder) {
64 cx->check(obj, id);
65 return GetPropertyDescriptor(cx, obj, id, desc, holder);
66 }
67
JS_GetPropertyDescriptor(JSContext * cx,JS::Handle<JSObject * > obj,const char * name,JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc,JS::MutableHandle<JSObject * > holder)68 JS_PUBLIC_API bool JS_GetPropertyDescriptor(
69 JSContext* cx, JS::Handle<JSObject*> obj, const char* name,
70 JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc,
71 JS::MutableHandle<JSObject*> holder) {
72 JSAtom* atom = Atomize(cx, name, strlen(name));
73 if (!atom) {
74 return false;
75 }
76 JS::Rooted<jsid> id(cx, AtomToId(atom));
77 return JS_GetPropertyDescriptorById(cx, obj, id, desc, holder);
78 }
79
JS_GetUCPropertyDescriptor(JSContext * cx,JS::Handle<JSObject * > obj,const char16_t * name,size_t namelen,JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc,JS::MutableHandle<JSObject * > holder)80 JS_PUBLIC_API bool JS_GetUCPropertyDescriptor(
81 JSContext* cx, JS::Handle<JSObject*> obj, const char16_t* name,
82 size_t namelen,
83 JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc,
84 JS::MutableHandle<JSObject*> holder) {
85 JSAtom* atom = AtomizeChars(cx, name, namelen);
86 if (!atom) {
87 return false;
88 }
89 JS::Rooted<jsid> id(cx, AtomToId(atom));
90 return JS_GetPropertyDescriptorById(cx, obj, id, desc, holder);
91 }
92