1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef JSWeakObjectMapRefPrivate_h
27#define JSWeakObjectMapRefPrivate_h
28
29#include <JavaScriptCore/JSContextRef.h>
30#include <JavaScriptCore/JSValueRef.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/*! @typedef JSWeakObjectMapRef A weak map for storing JSObjectRefs */
37typedef struct OpaqueJSWeakObjectMap* JSWeakObjectMapRef;
38
39/*!
40 @typedef JSWeakMapDestroyedCallback
41 @abstract The callback invoked when a JSWeakObjectMapRef is being destroyed.
42 @param map The map that is being destroyed.
43 @param data The private data (if any) that was associated with the map instance.
44 */
45typedef void (*JSWeakMapDestroyedCallback)(JSWeakObjectMapRef map, void* data);
46
47/*!
48 @function
49 @abstract Creates a weak value map that can be used to reference user defined objects without preventing them from being collected.
50 @param ctx The execution context to use.
51 @param data A void* to set as the map's private data. Pass NULL to specify no private data.
52 @param destructor A function to call when the weak map is destroyed.
53 @result A JSWeakObjectMapRef bound to the given context, data and destructor.
54 @discussion The JSWeakObjectMapRef can be used as a storage mechanism to hold custom JS objects without forcing those objects to
55 remain live as JSValueProtect would.
56 */
57JS_EXPORT JSWeakObjectMapRef JSWeakObjectMapCreate(JSContextRef ctx, void* data, JSWeakMapDestroyedCallback destructor);
58
59/*!
60 @function
61 @abstract Associates a JSObjectRef with the given key in a JSWeakObjectMap.
62 @param ctx The execution context to use.
63 @param map The map to operate on.
64 @param key The key to associate a weak reference with.
65 @param object The user defined object to associate with the key.
66 */
67JS_EXPORT void JSWeakObjectMapSet(JSContextRef ctx, JSWeakObjectMapRef map, void* key, JSObjectRef object);
68
69/*!
70 @function
71 @abstract Retrieves the JSObjectRef associated with a key.
72 @param ctx The execution context to use.
73 @param map The map to query.
74 @param key The key to search for.
75 @result Either the live object associated with the provided key, or NULL.
76 */
77JS_EXPORT JSObjectRef JSWeakObjectMapGet(JSContextRef ctx, JSWeakObjectMapRef map, void* key);
78
79/*!
80 @function
81 @abstract Removes the entry for the given key if the key is present, otherwise it has no effect.
82 @param ctx The execution context to use.
83 @param map The map to use.
84 @param key The key to remove.
85 */
86JS_EXPORT void JSWeakObjectMapRemove(JSContextRef ctx, JSWeakObjectMapRef map, void* key);
87
88#ifdef __cplusplus
89}
90#endif
91
92#endif // JSWeakObjectMapPrivate_h
93