mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-27 22:45:57 +00:00
[js/common] use Map instead of object for backends (#17352)
### Description resolved https://github.com/microsoft/onnxruntime/security/code-scanning/1140
This commit is contained in:
parent
75710f0006
commit
d88406a31b
1 changed files with 5 additions and 5 deletions
|
|
@ -12,7 +12,7 @@ interface BackendInfo {
|
|||
aborted?: boolean;
|
||||
}
|
||||
|
||||
const backends: {[name: string]: BackendInfo} = {};
|
||||
const backends: Map<string, BackendInfo> = new Map();
|
||||
const backendsSortedByPriority: string[] = [];
|
||||
|
||||
/**
|
||||
|
|
@ -27,9 +27,9 @@ const backendsSortedByPriority: string[] = [];
|
|||
*/
|
||||
export const registerBackend = (name: string, backend: Backend, priority: number): void => {
|
||||
if (backend && typeof backend.init === 'function' && typeof backend.createSessionHandler === 'function') {
|
||||
const currentBackend = backends[name];
|
||||
const currentBackend = backends.get(name);
|
||||
if (currentBackend === undefined) {
|
||||
backends[name] = {backend, priority};
|
||||
backends.set(name, {backend, priority});
|
||||
} else if (currentBackend.priority > priority) {
|
||||
// same name is already registered with a higher priority. skip registeration.
|
||||
return;
|
||||
|
|
@ -46,7 +46,7 @@ export const registerBackend = (name: string, backend: Backend, priority: number
|
|||
}
|
||||
|
||||
for (let i = 0; i < backendsSortedByPriority.length; i++) {
|
||||
if (backends[backendsSortedByPriority[i]].priority <= priority) {
|
||||
if (backends.get(backendsSortedByPriority[i])!.priority <= priority) {
|
||||
backendsSortedByPriority.splice(i, 0, name);
|
||||
return;
|
||||
}
|
||||
|
|
@ -71,7 +71,7 @@ export const resolveBackend = async(backendHints: readonly string[]): Promise<Ba
|
|||
const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints;
|
||||
const errors = [];
|
||||
for (const backendName of backendNames) {
|
||||
const backendInfo = backends[backendName];
|
||||
const backendInfo = backends.get(backendName);
|
||||
if (backendInfo) {
|
||||
if (backendInfo.initialized) {
|
||||
return backendInfo.backend;
|
||||
|
|
|
|||
Loading…
Reference in a new issue