Skip to main content
DeMineArchiver
Participant
June 21, 2023
Answered

C - Level JSAP - switching to C++ breaks

  • June 21, 2023
  • 1 reply
  • 407 views

I am using the C-Level Extensibility JSAPI in Adobe Animate 2023. I downloaded the official sample file and learned that the API requires to call JS_DefineFunction. To test the API, I programmed this simple extension (using the C programming language) :

#include <Windows.h>
#include <stdlib.h>
#include <tchar.h>
#include <mm_jsapi.h>

JSBool messageBox(JSContext *cx, JSObject *obj, unsigned int argc, jsval *argv, jsval *rval) {
  if (argc != 2) return JS_FALSE;

  unsigned int textLength;
  unsigned short* text = JS_ValueToString(cx, argv[0], &textLength);

  unsigned int captionLength;
  unsigned short* caption = JS_ValueToString(cx, argv[1], &captionLength);

  int result = MessageBoxW(NULL, (const wchar_t*) text, (const wchar_t*) caption, MB_YESNOCANCEL);
  *rval = JS_IntegerToValue(result);

  return JS_TRUE;
}

MM_STATE

void MM_Init() {
  JS_DefineFunction(L"messageBox", messageBox, 2);
}

 

Please note that the code above functions properly.

I would like to integrate C++ into my workflow and use the C++ standard library (threading and more). Here's the code I've adapted for C++ (there are no compilation errors now):

#include <Windows.h>
#include <cstdlib>
#include <mm_jsapi.h>

JSBool messageBox(JSContext *cx, JSObject *obj, unsigned int argc, jsval *argv, jsval *rval) {
  if (argc != 2) return JS_FALSE;

  unsigned int textLength;
  unsigned short* text = JS_ValueToString(cx, argv[0], &textLength);

  unsigned int captionLength;
  unsigned short* caption = JS_ValueToString(cx, argv[1], &captionLength);

  int result = MessageBoxW(NULL, (const wchar_t*) text, (const wchar_t*) caption, MB_YESNOCANCEL);
  *rval = JS_IntegerToValue(result);

  return JS_TRUE;
}

MM_STATE

void MM_Init() {
  JS_DefineFunction((unsigned short*) L"messageBox", messageBox, 2);
}

 When I compiled the code above and installed the DLL, I tried to run it with the same JSFL script as before, supposing that it would work fine.

But the function did not register properly.

So first I need to solve the issue with converting this C code to C++:

#include <tchar.h>
#include <mm_jsapi.h>

MM_STATE

void MM_Init() {
  JS_DefineFunction(L"messageBox", messageBox, 2);
}

C++ requires the the value of function name to be cast to unsigned short*. Here's the code I use now, but the function is still not registered:

#include <mm_jsapi.h>

MM_STATE

void MM_Init() {
  JS_DefineFunction((unsigned short*) L"messageBox", messageBox, 2);
}

I am using CMake for building. Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.8)

set(WIN32 ON)

set(CXX_STANDARD 20)
set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

project(JsflNative CXX)

add_library(mm_jsapi INTERFACE)

file(GLOB JSFL_TEST_SRC "src/main.cpp")
add_library(JsflTest SHARED ${JSFL_TEST_SRC})
target_include_directories(JsflTest PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)

install(TARGETS JsflTest DESTINATION "$ENV{LOCALAPPDATA}/Adobe/Animate 2023/en_US/Configuration/External Libraries")


I really hope someone would be able to help me, any help is appreaciated.

This topic has been closed for replies.
Correct answer DeMineArchiver

I was able to solve this!
Here's what I did:
"mm_jsapi.h"

#ifndef _MM_JSAPI_H_
#define _MM_JSAPI_H_

extern "C" {
  // Code
}

#endif

 After that everything should work fine!

1 reply

DeMineArchiver
DeMineArchiverAuthorCorrect answer
Participant
June 22, 2023

I was able to solve this!
Here's what I did:
"mm_jsapi.h"

#ifndef _MM_JSAPI_H_
#define _MM_JSAPI_H_

extern "C" {
  // Code
}

#endif

 After that everything should work fine!