Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detach immediately frees wasm allocated memory. #541

Merged
merged 1 commit into from
Jun 18, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Fixed

- Only dimension on size (+ color) wrongly displayed as treemap, not tablechart.
- Allocated memory is immediately freed after calling detach(), CAnimation and Snapshot
also can be released using their new `free()` method.

## [0.11.1] - 2024-05-31

Expand Down
2 changes: 2 additions & 0 deletions src/apps/weblib/ts-api/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export class Chart implements ChartInterface {

detach(): void {
this._module.unregisterChart(this._cChart)
this._cCanvas.free()
this._cChart.free()
}

start(): void {
Expand Down
4 changes: 2 additions & 2 deletions src/apps/weblib/ts-api/module/canimctrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CObject } from './cenv.js'
import { CObject, CManagedObject } from './cenv.js'

/** Stored Animation object. */
export class CAnimation extends CObject {}
export class CAnimation extends CManagedObject {}

export class CAnimControl extends CObject {
setParam(path: string, value = ''): void {
Expand Down
4 changes: 2 additions & 2 deletions src/apps/weblib/ts-api/module/ccanvas.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type CEnv, CObject } from './cenv.js'
import { type CEnv, CManagedObject } from './cenv.js'
import { type CPointerClosure } from './objregistry.js'
import { CColorGradient } from './ccolorgradient.js'
import type { CString, CColorGradientPtr } from '../cvizzu.types'

export class CCanvas extends CObject {
export class CCanvas extends CManagedObject {
constructor(env: CEnv, getId: CPointerClosure) {
super(getId, env)
}
Expand Down
6 changes: 3 additions & 3 deletions src/apps/weblib/ts-api/module/cchart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as Config from '../types/config.js'
import * as Styles from '../types/styles.js'
import * as Data from '../types/data.js'

import { CObject, CEnv } from './cenv.js'
import { CManagedObject, CObject, CEnv } from './cenv.js'
import { CPointerClosure } from './objregistry.js'
import { CProxy } from './cproxy.js'
import { CCanvas } from './ccanvas.js'
Expand All @@ -14,7 +14,7 @@ import { CAnimation } from './canimctrl.js'
import { isIterable } from '../utils.js'

/** Stored Chart object. */
export class Snapshot extends CObject {}
export class Snapshot extends CManagedObject {}

export class CEvent extends CObject {
preventDefault(): void {
Expand All @@ -26,7 +26,7 @@ class CConfig extends CProxy<Config.Chart> {}
class CStyle extends CProxy<Styles.Chart> {}
class CAnimOptions extends CProxy<Anim.Options> {}

export class CChart extends CObject {
export class CChart extends CManagedObject {
config: CConfig
style: CStyle
computedStyle: CStyle
Expand Down
22 changes: 18 additions & 4 deletions src/apps/weblib/ts-api/module/cenv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CPointer, CVizzu, CString } from '../cvizzu.types'

import { CError } from './cerror.js'
import { ObjectRegistry, FnGetter, CPointerClosure } from './objregistry.js'
import { ObjectRegistry, CPointerClosure } from './objregistry.js'

export class CEnv {
protected _wasm: CVizzu
Expand All @@ -23,8 +23,9 @@ export class CEnv {
return this._wasm.UTF8ToString(str)
}

protected _getStatic(getter: FnGetter): CPointerClosure {
return this._objectRegistry.get(this._callStatic(getter))
protected _getStatic(getter: () => CPointer): CPointerClosure {
const cPointer = this._callStatic(getter)()
return (): CPointer => cPointer
}

protected _callStatic<T extends unknown[], R>(f: (...params: T) => R): (...params: T) => R {
Expand Down Expand Up @@ -55,7 +56,8 @@ export class CObject extends CEnv {
}

protected _get(getter: (self: CPointer) => CPointer): CPointerClosure {
return this._objectRegistry.get(this._call(getter))
const cPointer = this._call(getter)()
return (): CPointer => cPointer
}

protected _call<T extends unknown[], R>(
Expand All @@ -64,3 +66,15 @@ export class CObject extends CEnv {
return super._callStatic(f).bind(this, this.getId())
}
}

export class CManagedObject extends CObject {
constructor(getId: CPointerClosure, cenv: CEnv) {
super(getId, cenv)
this._objectRegistry.register(this.getId)
}

free(): void {
this._objectRegistry.unregister(this.getId)
this._wasm._object_free(this.getId())
}
}
12 changes: 6 additions & 6 deletions src/apps/weblib/ts-api/module/objregistry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CPointer } from '../cvizzu.types'

export type FnFree = (rawCPointer: CPointer) => void
export type FnGetter = () => CPointer

export type CPointerClosure = () => CPointer

Expand All @@ -14,10 +13,11 @@ export class ObjectRegistry {
})
}

get(fnGetter: FnGetter): CPointerClosure {
const cPointer = fnGetter()
const object = (): CPointer => cPointer
this._finalizationRegistry.register(object, cPointer)
return object
register(objectId: CPointerClosure): void {
this._finalizationRegistry.register(objectId, objectId())
}

unregister(objectId: CPointerClosure): void {
this._finalizationRegistry.unregister(objectId)
}
}