Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/cache.js
Comment thread
Nadahar marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

const { privateCache, sharedCache } = require('@runtime/cache');
const { javaify, jsify } = require('./utils');

/**
* The {@link JSCache} can be used by to share information between subsequent runs of the same script or between scripts (depending on implementation).
Expand All @@ -28,42 +29,47 @@ class JSCache {
* Returns the value to which the specified key is mapped.
*
* @param {string} key the key whose associated value is to be returned
* @param {function} [defaultSupplier] if the specified key is not already associated with a value, this function will return a default value
* @param {function} [defaultSupplier] if the specified key is not already associated with a value, this function will return a default value. The output of the function will be run through javaify() if the cache is shared, since the shared cache can't contain JavaScript objects.
* @param {boolean} [jsifyResult=true] whether or not the result will be automatically converted to JavaScript objects when possible
* @returns {*|null} the current object for the supplied key, the value returned by defaultSupplier (if provided), or `null`
*/
get (key, defaultSupplier) {
if (this.exists(key)) return this.#valueCache.get(key);
get (key, defaultSupplier, jsifyResult = true) {
const isShared = this.#isSharedCache();
if (this.exists(key)) {
const result = this.#valueCache.get(key);
return isShared && jsifyResult ? jsify(result) : result;
}
// key doesn't exist in cache: invoke supplier if provided
if (typeof defaultSupplier !== 'function') return null;
const supplied = defaultSupplier();
if (supplied === null) return null; // do not store null values in cache
this.#valueCache.put(key, supplied);
return supplied;
this.#valueCache.put(key, isShared ? javaify(supplied) : supplied);
return isShared && jsifyResult ? jsify(supplied) : supplied;
}

/**
Comment thread
florian-h05 marked this conversation as resolved.
* Associates the specified value with the specified key.
*
* @param {string} key key with which the specified value is to be associated
* @param {*} value value to be associated with the specified key
* @param {*} value value to be associated with the specified key. The value will be run through javaify() if the cache is shared, since the shared cache can't contain JavaScript objects.
* @param {boolean} [jsifyResult=true] whether or not the result will be automatically converted to JavaScript objects when possible
* @returns {*|null} the previous value associated with the key, or null if there was no mapping for key
*/
put (key, value) {
// see https://www.graalvm.org/latest/reference-manual/js/JavaScriptCompatibility/ for Java. docs
if (typeof value === 'object' && Java.isScriptObject(value) && this.#isSharedCache()) {
console.warn(`It is not recommended to store the JS object with the key '${key}' in the shared cache, as JS objects must not be accessed from multiple threads. Multi-threaded access to JS objects will lead to script execution failure.`);
}
return this.#valueCache.put(key, value);
put (key, value, jsifyResult = true) {
const isShared = this.#isSharedCache();
const result = this.#valueCache.put(key, isShared ? javaify(value) : value);
return isShared && jsifyResult ? jsify(result) : result;
}

/**
* Removes the mapping for a key from this map if it is present.
*
* @param {string} key key whose mapping is to be removed from the cache
* @param {boolean} [jsifyResult=true] whether or not the result will be automatically converted to JavaScript objects when possible
* @returns {*|null} the previous value associated with the key or null if there was no mapping for key
*/
remove (key) {
return this.#valueCache.remove(key);
remove (key, jsifyResult = true) {
return this.#isSharedCache() && jsifyResult ? jsify(this.#valueCache.remove(key)) : this.#valueCache.remove(key);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/rules/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ function _getTriggeredData (rawInput, javaEventBackwardCompat = false) {
const data = {};

// Add input to data to pass through any properties not captured below
data.raw = inputAsJsObj;
data.raw = rawInput;

// Dynamically added properties, depending on their availability

Expand Down
13 changes: 8 additions & 5 deletions types/cache.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,28 @@ export class JSCache {
* Returns the value to which the specified key is mapped.
*
* @param {string} key the key whose associated value is to be returned
* @param {function} [defaultSupplier] if the specified key is not already associated with a value, this function will return a default value
* @param {function} [defaultSupplier] if the specified key is not already associated with a value, this function will return a default value. The output of the function will be run through javaify() if the cache is shared, since the shared cache can't contain JavaScript objects.
* @param {boolean} [jsifyResult=true] whether or not the result will be automatically converted to JavaScript objects when possible
* @returns {*|null} the current object for the supplied key, the value returned by defaultSupplier (if provided), or `null`
*/
get(key: string, defaultSupplier?: Function): any | null;
get(key: string, defaultSupplier?: Function, jsifyResult?: boolean): any | null;
/**
* Associates the specified value with the specified key.
*
* @param {string} key key with which the specified value is to be associated
* @param {*} value value to be associated with the specified key
* @param {*} value value to be associated with the specified key. The value will be run through javaify() if the cache is shared, since the shared cache can't contain JavaScript objects.
* @param {boolean} [jsifyResult=true] whether or not the result will be automatically converted to JavaScript objects when possible
* @returns {*|null} the previous value associated with the key, or null if there was no mapping for key
*/
put(key: string, value: any): any | null;
put(key: string, value: any, jsifyResult?: boolean): any | null;
/**
* Removes the mapping for a key from this map if it is present.
*
* @param {string} key key whose mapping is to be removed from the cache
* @param {boolean} [jsifyResult=true] whether or not the result will be automatically converted to JavaScript objects when possible
* @returns {*|null} the previous value associated with the key or null if there was no mapping for key
*/
remove(key: string): any | null;
remove(key: string, jsifyResult?: boolean): any | null;
/**
* Checks the mapping for a key from this map.
*
Expand Down
2 changes: 1 addition & 1 deletion types/cache.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.