SharedArrayBuffer
JavaScriptCore
class_A.h:
class A {
void hello(); // declaration
}
class_A.cpp:
#include <iostream>
#include "class_A.h" // import the declaration
void A::hello() {
std::cout << "hello!" << std::endl;
}
class ProtectedArguments {
public:
//exposed stuff here
}
ProtectedArguments(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[]):
myContext(ctx), myArguments(arguments, arguments + argumentCount)
{
for(auto i: myArguments)
JSValueProtect(myContext, i);
}
util.cpp
file
class ProtectedArguments {
public:
ProtectedArguments(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[]):
myContext(ctx), myArguments(arguments, arguments + argumentCount)
{
for(auto i: myArguments)
JSValueProtect(myContext, i);
}
ProtectedArguments(const ProtectedArguments & other): myContext(other.myContext), myArguments(other.myArguments) {
for(auto i: myArguments)
JSValueProtect(myContext, i);
}
~ProtectedArguments() {
for(auto i: myArguments)
JSValueUnprotect(myContext, i);
}
}
JSValueRef operator[](int index) { return myArguments[index]; }
JSValueRef operator[](int index) { return myArguments[index]; }
operator JSValueRef *() { return &myArguments[0]; }
JSValueRef operator[](int index) const { return myArguments[index]; }
operator const JSValueRef *() const { return myArguments.data(); }
const
next to them
const
access.. when you have a constant pointer to an object
Here we go
Uh oh
int x = 0; // define an integer with name x and value 0
int * y = &x; // define a pointer to an integer with name y and point at x
*y = 10;
*y = 10;
x;
//10
const
pointers
SO...
operator JSValueRef *() { return &myArguments[0]; }
this defines how *y = 10;
would operate
*args[x] = ...;
const
pointers.
gives you the address and lets you dereference it
JSValueRef * x = args[0]; // taking the address
*x = ....; // dereferencing
*args[0] = ...; // also dereferencing
ProtectedArguments args = ....; // normal object
const ProtectedArguments & args2(args); /// const reference to object
args[0] // accesses the regular version of the [] operator
args2[0] // accesses the const version of the [] operator
const Type & alwaysReferences(objectToRefernce);
alwaysReferences
will permanently point to objectToReference
objectToReference
goes out of scope and you access alwaysReferences
, you're basically out of luck. Crash.