Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 18 additions & 12 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 @@ -29,41 +30,46 @@ class JSCache {
*
* @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 {boolean} [jsifyResult=true] whether or not the result will be automatically converted to JavaScript objects when possible.
Comment thread
Nadahar marked this conversation as resolved.
Outdated
* @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 {boolean} [jsifyResult=true] whether or not the result will be automatically converted to JavaScript objects when possible.
Comment thread
Nadahar marked this conversation as resolved.
Outdated
* @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
6 changes: 3 additions & 3 deletions src/rules/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const SCRIPT_TYPE = 'application/javascript';
const GENERATED_RULE_ITEM_TAG = 'GENERATED_RULE_ITEM';

const items = require('../items/items');
const { randomUUID, jsArrayToJavaSet, javaMapToJsObj, javaify } = require('../utils');
const { randomUUID, jsArrayToJavaSet, javaMapToJsObj, jsify, javaify } = require('../utils');
const log = require('../log')('rules');
const { getService } = require('../osgi');
const triggers = require('../triggers');
Expand Down Expand Up @@ -496,7 +496,7 @@ function _collapseInputMap (input) {
*/
function _getTriggeredData (rawInput, javaEventBackwardCompat = false) {
const inputAsJsObj = javaMapToJsObj(rawInput);
const input = _collapseInputMap(inputAsJsObj);
const input = jsify(_collapseInputMap(inputAsJsObj));
Comment thread
Nadahar marked this conversation as resolved.
Outdated

const event = input.event;
/**
Expand All @@ -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
9 changes: 6 additions & 3 deletions types/cache.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,27 @@ export class JSCache {
*
* @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 {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 {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.