A more concrete proposal:
(1) In a header file we have a macro "list" like this:
_(GetProperty, js::GetProperty) \
_(SetProperty, js::SetProperty, extra VMFunction args) \
...
In the header file we use the macro to define an enum:
enum class VMFunctionIndex : uint32_t { GetProperty, SetProperty, ... }
(2) In the cpp file we have a list of signatures:
using GetPropertyFn = bool (*)(JSContext*, HandleObject, MutableHandleValue);
using SetPropertyFn = ...
...
This file also uses the macro in (1) to automatically generate:
static constexpr VMFunction vmFunctions[] = {
FunctionInfo<GetPropertyFn>(js::GetProperty, ...),
FunctionInfo<SetPropertyFn>(js::SetProperty, ...),
...
};
(3) We have the following function to do the lookup:
const VMFunction& GetVMFunction(VMFunctionIndex index) { return vmFunctions[index]; }
----
To add a new VMFunction, you have to (1) add an entry to the macro in the header file (2) add a using statement in the cpp file. After that you can just use callVM(VMFunctionIndex::Foo).
When someone changes a function signature, they will get a compile error for FooFn in the cpp file and they can then fix all VMFunctionIndex::FooIndex uses.
Bug 1525838 Comment 5 Edit History
Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.
A more concrete proposal:
(1) In a header file we have a macro "list" like this:
```
_(GetProperty, js::GetProperty) \
_(SetProperty, js::SetProperty, extra VMFunction args) \
...
```
In the header file we use the macro to define an enum:
`enum class VMFunctionIndex : uint32_t { GetProperty, SetProperty, ... }`
(2) In the cpp file we have a list of signatures:
```
using GetPropertyFn = bool (*)(JSContext*, HandleObject, MutableHandleValue);
using SetPropertyFn = ...
...
```
This file also uses the macro in (1) to automatically generate:
```
static constexpr VMFunction vmFunctions[] = {
FunctionInfo<GetPropertyFn>(js::GetProperty, ...),
FunctionInfo<SetPropertyFn>(js::SetProperty, ...),
...
};
```
(3) We have the following function to do the lookup:
`const VMFunction& GetVMFunction(VMFunctionIndex index) { return vmFunctions[index]; }`
----
To add a new VMFunction, you have to (1) add an entry to the macro in the header file (2) add a using statement in the cpp file. After that you can just use callVM(VMFunctionIndex::Foo).
When someone changes a function signature, they will get a compile error for FooFn in the cpp file and they can then fix all VMFunctionIndex::FooIndex uses.