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

tweak(scripting/v8): better error messages for ref calls #2781

Merged
Merged
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
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
Loading