Skip to content

Commit 92208a7

Browse files
committed
Merge branch 'esr102' into esr115
2 parents ccdeb2c + 9ad7523 commit 92208a7

1 file changed

Lines changed: 252 additions & 0 deletions

File tree

docs/Migration Guide.md

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,3 +689,255 @@ This is a non-exhaustive list of minor API changes and renames.
689689
- `JS::FormatStackDump()` has removed its input buffer parameter.
690690
- `JS::GCForReason()``JS::NonIncrementalGC()`
691691
- `JS::GCPolicy<T>::initial()``JS::SafelyInitialized<T>`
692+
693+
## ESR 10 to 17 ##
694+
695+
At this time the JSAPI was still being documented in MDN.
696+
Viewing the pages on archive.org may give more details.
697+
[This](https://web.archive.org/web/20200424132803/https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_reference)
698+
is the last available snapshot, but older snapshots may be useful as
699+
well.
700+
701+
### Headers ###
702+
703+
The write barrier API (`JS_RegisterReference()`, `JS_ModifyReference()`,
704+
etc.) has moved from `jsapi.h` to `jsfriendapi.h`.
705+
706+
### C++ ###
707+
708+
At this point in SpiderMonkey's history, its headers could be compiled
709+
as either C or C++.
710+
There are a number of newer APIs behind an `#ifdef __cplusplus`, such as
711+
`JS::Value` and the Handle APIs.
712+
713+
**Recommendation:**
714+
If your codebase is in C, migrate it to C++.
715+
That will have to happen eventually anyway, as the C API gets removed in
716+
a future version.
717+
Migrating now will allow any new code that's being written to opt-in to
718+
future-proof APIs such as `JS::Value`.
719+
720+
### Types ###
721+
722+
Mozilla's custom integer types (`uint8`, `int32`, `JSUint64`, etc.) are
723+
deprecated, although still present in ESR 17.
724+
The standard types from `<stdint.h>` are preferred.
725+
726+
Other types have been removed in favour of plain C types:
727+
- `intN``int`
728+
- `jsdouble``double`
729+
- `jsint``int`
730+
- `jsrefcount``unsigned`
731+
- `jsuint``unsigned`
732+
- `jsuword``uintptr_t`
733+
- `jsword``intptr_t`
734+
- `uintN``unsigned`
735+
736+
**Recommendation:**
737+
Replace all occurrences of these types.
738+
739+
### Access from other threads ###
740+
741+
The `JS_THREADSAFE` build option is permanently turned on, meaning that
742+
JS APIs cannot be accessed from arbitrary threads.
743+
If your embedding relied on this, the code will probably need some
744+
refactoring.
745+
746+
APIs having to do with multi-threaded JS runtimes have all been removed:
747+
- `JS_ClearContextThread()`
748+
- `JS_GetContextThread()`
749+
- `JS_Lock()`
750+
- `JS_SetContextThread()`
751+
- `JS_Unlock()`
752+
753+
### JSClass ###
754+
755+
In this version there were several changes in the `JSClass` struct and
756+
its associated operations.
757+
758+
First of all, some of the members were reordered.
759+
Hopefully any affected code would fail to compile with the new ESR, but
760+
it's probably good to check all your `JSClass` definitions anyway.
761+
762+
The `JSClass.xdrObject` member was removed.
763+
It's likely that embedders were already not using JSXDR and were already
764+
setting this member to null.
765+
If you used this API, you may have to look for a custom solution.
766+
767+
Previously, a `JSClass` could provide either a `JSMarkOp` or `JSTraceOp`
768+
function pointer as the `JSClass.mark` member.
769+
The `JSCLASS_MARK_IS_TRACE` flag was used to indicate which one it was,
770+
and therefore how the function pointer would be called.
771+
The member has been renamed to `JSClass.trace`, and `JSMarkOp` has been
772+
removed, as well as `JSCLASS_MARK_IS_TRACE`.
773+
774+
If you had any `JSClass` with a non-null `mark` member that _didn't_ use
775+
`JSCLASS_MARK_IS_TRACE`, you must rewrite the mark operation as a trace
776+
operation.
777+
In most cases this should consist of a pretty straightforward
778+
replacement of `JS_MarkGCThing()` with `JS_CALL_TRACER()` and updating
779+
the function signature.
780+
An example is
781+
[here](https://bugzilla.mozilla.org/attachment.cgi?id=516449&action=diff#a/js/src/shell/js.cpp_sec2).
782+
783+
Several other `JSClass` operations, such as `JSPropertyOp`, changed
784+
their signatures to take Handles instead of pointers.
785+
Handles are part of the C++ API, although there are some alternate
786+
[typedefs](https://searchfox.org/mozilla-esr17/source/js/src/jsapi.h#1650)
787+
for use in the C API.
788+
789+
You will need to update the signatures of the class operations to match
790+
the new definitions.
791+
The definition of `JSPropertyOp`, for example, changed as follows:
792+
793+
```c++
794+
// old
795+
typedef JSBool (*JSPropertyOp)(JSContext*, JSObject*, jsid, jsval*);
796+
// new
797+
typedef JSBool (*JSPropertyOp)(JSContext*, JSHandleObject, JSHandleId, JSMutableHandleValue);
798+
```
799+
800+
If you are using C++ (recommended), you may need to use the Handle's
801+
`get()` or `address()` methods if you need access to the underlying
802+
GC-rooted thing, and `set()` if you need to set the value in a mutable
803+
Handle.
804+
For embeddings that still use C, use the `._` member of the ersatz
805+
Handle typedef or just cast the Handle to a pointer to the underlying
806+
GC thing, which should share the same memory layout (e.g. `(JSObject**)`
807+
for `JSHandleObject`).
808+
809+
`JSFinalizeOp`'s `JSContext*` parameter was replaced with a `JSFreeOp*`
810+
parameter.
811+
This is normally not used by embeddings.
812+
813+
There are some other minor removals listed below in the "Various API
814+
changes" section.
815+
816+
**Recommendations:**
817+
- Double-check your `JSClass` definitions to make sure the members
818+
match the new order.
819+
- Remove any usage of `JSCLASS_MARK_IS_TRACE`.
820+
- If you have any `JSMarkOp` functions, port them to match the signature
821+
of `JSTraceOp`.
822+
- Update signatures of other class operations where needed, to use the
823+
Handle API.
824+
825+
### E4X ###
826+
827+
E4X support (inline XML in JS) is being phased out at this point.
828+
If your code uses E4X, you'll need to pass `JSOPTION_ALLOW_XML` and
829+
possibly `JSOPTION_MOAR_XML` to `JS_SetOptions()` in order for it to
830+
keep working.
831+
832+
However, E4X is a dead end in terms of standardization, and is removed
833+
altogether in the very next version of SpiderMonkey, so the
834+
recommendation is to migrate your code not to use it.
835+
There isn't a one-size-fits-all replacement; some options are
836+
transpilation, parsing the XML from a string, and writing new APIs that
837+
take plain objects.
838+
839+
### GC callbacks ###
840+
841+
The old GC callback API has been split up.
842+
GC callbacks are now called only when GC begins and ends, and is passed
843+
a `JSGCStatus` value of `JSGC_BEGIN` or `JSGC_END`.
844+
For the finalize phase, there is a separate callback API that is set
845+
with `JS_SetFinalizeCallback()`, and is passed a `JSFinalizeStatus`
846+
value of `JSFINALIZE_START` or `JSFINALIZE_END`.
847+
This replaces the old `JSGCStatus` values of `JSGC_MARK_END` and
848+
`JSGC_FINALIZE_END`.
849+
850+
`JS_SetGCCallback()` and `JS_SetGCCallbackRT()` have been combined into
851+
a single `JS_SetGCCallback()` API that takes `JSRuntime*` instead of
852+
`JSContext*`.
853+
854+
**Recommendation:**
855+
If your GC callback performed any actions on `JSGC_MARK_END` or
856+
`JSGC_FINALIZE_END`, you'll need to split that code out into a separate
857+
finalize callback.
858+
You may need to fix up some API calls that take `JSRuntime*` instead of
859+
`JSContext*` (see also "Various API changes" below).
860+
Use `JS_GetRuntime()` or `JS_GetObjectRuntime()` if you don't have a
861+
pointer to the runtime already.
862+
863+
### Various API changes ###
864+
865+
This is a non-exhaustive list of minor API changes and renames.
866+
867+
- `JS_BufferIsCompilableUnit()` gets a boolean "bytes are UTF-8"
868+
argument in position 2.
869+
Previously, the bytes were not treated as UTF-8.
870+
- `JS_CompileFile()` → `JS_CompileUTF8File()`
871+
- `JS_CompileFileHandle()` → `JS_CompileUTF8FileHandle()`
872+
- `JS_CompileFileHandleForPrinicpals()` →
873+
`JS_CompileUTF8FileHandleForPrincipals()`
874+
- `JS_CompileFileHandleForPrinicpalsVersion()` →
875+
`JS_CompileUTF8FileHandleForPrincipalsVersion()`
876+
- Several APIs now take a `JSRuntime*` instead of a `JSContext*`:
877+
- `JS_CompartmentGC()`
878+
- `JS_DumpHeap()`
879+
- `JS_GC()`
880+
- `JS_IsInRequest()`
881+
- `JS_SetNativeStackQuota()`
882+
- `JS_TracerInit()`
883+
- Several APIs no longer take a `JSContext*` as the first argument:
884+
- `JS_GetClass()`
885+
- `JS_GetCompartmentPrivate()`
886+
- `JS_GetParent()`
887+
- `JS_GetPrivate()`
888+
- `JS_GetPrototype()`
889+
- `JS_GetReservedSlot()`
890+
- `JS_IsAboutToBeFinalized()`
891+
- `JS_SetCompartmentPrivate()`
892+
- `JS_SetPrivate()`
893+
- `JS_SetReservedSlot()`
894+
- `JSIdArray` is now opaque.
895+
Instead of accessing the `.length` and `.vector` members, use
896+
`JS_IdArrayLength()` and `JS_IdArrayGet()`.
897+
- `JS_NewCompartmentAndGlobalObject()` → `JS_NewGlobalObject()`
898+
- `JS_NewObjectForConstructor()` now takes an additional `JSClass*`
899+
argument.
900+
- `JS_Remove___Root()` functions no longer have a return value.
901+
- `JSTraceCallback` is passed a `void**` pointer to the traced pointer,
902+
instead of the `void*` pointer itself.
903+
904+
The following APIs have been removed.
905+
906+
- `JSCLASS_CONCURRENT_FINALIZER`: objects with finalize hooks are never
907+
finalized on a background thread anymore.
908+
- `JSCLASS_CONSTRUCT_PROTOTYPE`: this flag caused a class's constructor
909+
to be called once on its prototype object.
910+
If you need to do any initialization on the prototype object, do it
911+
after initializing the class instead.
912+
- `JS_ConstructObject()` and `JS_ConstructObjectWithArguments()`: use
913+
`JS_New()` instead, and pass a pointer to the constructor object.
914+
The old APIs would malfunction if the constructor was no longer
915+
reachable from the global object.
916+
- `JS_DestroyContextMaybeGC()`: use either `JS_DestroyContext()` or
917+
`JS_DestroyContextNoGC()`.
918+
- `JSFinalizeStub`: use null instead.
919+
(Despite the comment in `jsapi.h` that says `JSClass.finalize` must
920+
not be null!)
921+
- `JS_FlushCaches()`: no replacement.
922+
- `JS_GET_CLASS` macro: use `JS_GetClass()` instead.
923+
- `JS_IsConstructing_PossiblyWithGivenThisObject()`: once no longer
924+
using `JS_ConstructObject()` and `JSCLASS_CONSTRUCT_PROTOTYPE`, this
925+
can be replaced with `JS_IsConstructing()`.
926+
- `JS_IsScriptFrame()`: can be replaced by `JS_GetFrameScript()` which
927+
returns null if the frame is not a script frame.
928+
- `JS::MarkRuntime()`: this was an internal function and was unlikely to
929+
be used by embeddings.
930+
- `JS_NewNumberValue()`: use `JS_NumberValue()` instead, but note that
931+
the semantics are slightly different: it canonicalizes NaN, and it
932+
cannot fail.
933+
- `JSOPTION_JIT`, `JSOPTION_PROFILING`: no replacement.
934+
- `JSOPTION_SOFTEN`: Use `JS_SetJitHardening()` instead.
935+
- The `JSPD_ARGUMENT` flag for property descriptors: in ESR 17 you can
936+
assume that it's always set.
937+
- `JS_SetThreadStackLimit()`: use `JS_SetNativeStackQuota()` instead.
938+
- `JSVAL_IS_OBJECT()`: a drop-in replacement is
939+
`JSVAL_IS_NULL(v) || !JSVAL_IS_PRIMITIVE(v)`, but the long-term
940+
recommendation is to migrate to the C++ `JS::Value` API (see above.)
941+
Note that `JSVAL_IS_OBJECT()` corresponds to
942+
`JS::Value::isObjectOrNull()`, not `JS::Value::isObject()`.
943+
- `JS::Value::setObjectOrUndefined()`: open-code this if you need it.

0 commit comments

Comments
 (0)