vm-interface.d.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Used as an optional.
  3. * `{ value: S } | { error: E }`.
  4. */
  5. export type SuccessOrFail<S, F> = {
  6. value: S;
  7. error?: undefined;
  8. } | {
  9. error: F;
  10. };
  11. export declare function isSuccess<S, F>(successOrFail: SuccessOrFail<S, F>): successOrFail is {
  12. value: S;
  13. };
  14. export declare function isFail<S, F>(successOrFail: SuccessOrFail<S, F>): successOrFail is {
  15. error: F;
  16. };
  17. /**
  18. * Used as an optional for results of a Vm call.
  19. * `{ value: VmHandle } | { error: VmHandle }`.
  20. */
  21. export type VmCallResult<VmHandle> = SuccessOrFail<VmHandle, VmHandle>;
  22. /**
  23. * A VmFunctionImplementation takes handles as arguments.
  24. * It should return a handle, or be void.
  25. *
  26. * To indicate an exception, a VMs can throw either a handle (transferred
  27. * directly) or any other Javascript value (only the poperties `name` and
  28. * `message` will be transferred). Or, the VmFunctionImplementation may return
  29. * a VmCallResult's `{ error: handle }` error variant.
  30. *
  31. * VmFunctionImplementation should not free its arguments or its return value.
  32. * It should not retain a reference to its return value or thrown error.
  33. */
  34. export type VmFunctionImplementation<VmHandle> = (this: VmHandle, ...args: VmHandle[]) => VmHandle | VmCallResult<VmHandle> | void;
  35. /**
  36. * A minimal interface to a Javascript execution environment.
  37. *
  38. * Higher-level tools should build over the LowLevelJavascriptVm interface to
  39. * share as much as possible between executors.
  40. *
  41. * From https://www.figma.com/blog/how-we-built-the-figma-plugin-system/
  42. */
  43. export interface LowLevelJavascriptVm<VmHandle> {
  44. global: VmHandle;
  45. undefined: VmHandle;
  46. typeof(handle: VmHandle): string;
  47. getNumber(handle: VmHandle): number;
  48. getString(handle: VmHandle): string;
  49. newNumber(value: number): VmHandle;
  50. newString(value: string): VmHandle;
  51. newObject(prototype?: VmHandle): VmHandle;
  52. newFunction(name: string, value: VmFunctionImplementation<VmHandle>): VmHandle;
  53. getProp(handle: VmHandle, key: string | VmHandle): VmHandle;
  54. setProp(handle: VmHandle, key: string | VmHandle, value: VmHandle): void;
  55. defineProp(handle: VmHandle, key: string | VmHandle, descriptor: VmPropertyDescriptor<VmHandle>): void;
  56. callFunction(func: VmHandle, thisVal: VmHandle, ...args: VmHandle[]): VmCallResult<VmHandle>;
  57. evalCode(code: string, filename?: string): VmCallResult<VmHandle>;
  58. }
  59. /**
  60. * From https://www.figma.com/blog/how-we-built-the-figma-plugin-system/
  61. */
  62. export interface VmPropertyDescriptor<VmHandle> {
  63. value?: VmHandle;
  64. configurable?: boolean;
  65. enumerable?: boolean;
  66. get?: (this: VmHandle) => VmHandle;
  67. set?: (this: VmHandle, value: VmHandle) => void;
  68. }