Skip to content

Commit

Permalink
tweak(scripting/v8): better error messages for ref calls
Browse files Browse the repository at this point in the history
  • Loading branch information
AvarianKnight committed Sep 9, 2024
1 parent 98fd272 commit 6b058f2
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions data/shared/citizen/scripting/v8/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const EXT_LOCALFUNCREF = 11;
let boundaryIdx = 1;
let lastBoundaryStart = null;
const isDuplicityVersion = IsDuplicityVersion();
const currentResourceName = GetCurrentResourceName();

// temp
global.FormatStackTrace = function (args, argLength) {
Expand Down Expand Up @@ -76,13 +77,30 @@ const EXT_LOCALFUNCREF = 11;

function refFunctionUnpacker(refSerialized) {
const fnRef = Citizen.makeFunctionReference(refSerialized);
const invoker = GetInvokingResource();

return function (...args) {
return runWithBoundaryEnd(() => {
const retvals = unpack(fnRef(pack(args)));
let retvals = null;
try {
retvals = unpack(fnRef(pack(args)));
} catch (e) {
}

if (retvals === null) {
throw new Error('Error in nested ref call.');
let errorMessage = `Error in nested ref call for ${currentResourceName}. `
// invoker can be null, we don't want to give an even worse
// error by erroring here :P
if (invoker) {
errorMessage += `${currentResourceName} tried to call a function reference in ${invoker} but the reference wasn't valid. `
if (GetResourceState(invoker) !== "started") {
errorMessage += `And ${invoker} isn't started, was the resource restarted mid call?`
} else {
errorMessage += `(did ${invoker} restart recently?)`
}
}

throw new Error(errorMessage);
}

switch (retvals.length) {
Expand Down Expand Up @@ -519,7 +537,7 @@ const EXT_LOCALFUNCREF = 11;
const getExportEventName = (resource, name) => `__cfx_export_${resource}_${name}`;

on(`on${eventType}ResourceStart`, (resource) => {
if (resource === GetCurrentResourceName()) {
if (resource === currentResourceName) {
const numMetaData = GetNumResourceMetadata(resource, exportKey) || 0;

for (let i = 0; i < numMetaData; i++) {
Expand Down Expand Up @@ -582,7 +600,7 @@ const EXT_LOCALFUNCREF = 11;

const [exportName, func] = args;

on(getExportEventName(GetCurrentResourceName(), exportName), (setCB) => {
on(getExportEventName(currentResourceName, exportName), (setCB) => {
setCB(func);
});
},
Expand Down

0 comments on commit 6b058f2

Please sign in to comment.