Skip to content

Instantly share code, notes, and snippets.

@fitzgen
Forked from zpao/gist:1346649
Created November 8, 2011 00:41
Show Gist options
  • Save fitzgen/1346669 to your computer and use it in GitHub Desktop.
Save fitzgen/1346669 to your computer and use it in GitHub Desktop.
diff --git a/js/src/jsobj.cpp b/js/src/jsobj.cpp
index afa2125..2afdbb8 100644
--- a/js/src/jsobj.cpp
+++ b/js/src/jsobj.cpp
@@ -7230,7 +7230,6 @@ dumpValue(const Value &v)
fprintf(stderr, "false");
} else if (v.isMagic()) {
fprintf(stderr, "<invalid");
-#ifdef DEBUG
switch (v.whyMagic()) {
case JS_ARRAY_HOLE: fprintf(stderr, " array hole"); break;
case JS_ARGS_HOLE: fprintf(stderr, " args hole"); break;
@@ -7239,7 +7238,6 @@ dumpValue(const Value &v)
case JS_GENERATOR_CLOSING: fprintf(stderr, " generator closing"); break;
default: fprintf(stderr, " ?!"); break;
}
-#endif
fprintf(stderr, ">");
} else {
fprintf(stderr, "unexpected value");
diff --git a/js/src/v8api/object.cpp b/js/src/v8api/object.cpp
index 149d1c3..9d75277 100644
--- a/js/src/v8api/object.cpp
+++ b/js/src/v8api/object.cpp
@@ -575,14 +575,17 @@ Object::GetIndexedPropertiesPixelDataLength()
UNIMPLEMENTEDAPI(0);
}
+static jsid proxyProperty() {
+ return INTERNED_STRING_TO_JSID(cx(), JS_InternString(cx(), "rawArray"));
+}
+
static JSObject* grabTypedArray(JSObject* obj) {
if (js_IsTypedArray(obj))
return obj;
- if (!obj->isObjectProxy())
+ if (!js::IsProxy(obj))
return NULL;
- jsid name = INTERNED_STRING_TO_JSID(JS_InternString(cx(), "rawArray"));
js::Value v;
- js::JSProxy::get(cx(), obj, obj, name, &v);
+ js::Proxy::get(cx(), obj, obj, proxyProperty(), &v);
if (v.isObjectOrNull())
return v.toObjectOrNull();
return NULL;
@@ -597,15 +600,48 @@ Object::SetIndexedPropertiesToExternalArrayData(void* data,
JS_ASSERT (array_type == GetIndexedPropertiesExternalArrayDataType());
if (number_of_elements < 0)
return;
- js::TypedArray* arr = js::TypedArray::fromJSObject(grabTypedArray(*this));
- // Hardcoded for bytes now
- size_t elemSize = arr->slotWidth();
+ // js::TypedArray* arr = js::TypedArray::fromJSObject(grabTypedArray(*this));
+ // // Hardcoded for bytes now
+ // size_t elemSize = js::TypedArray::slotWidth(array_type);
+ // size_t bufferSize = elemSize * number_of_elements;
+ // js::ArrayBuffer* buffer = arr->buffer;
+ // buffer->freeStorage(cx());
+ // buffer->data = data;
+ // buffer->byteLength = bufferSize;
+ // buffer->isExternal = true;
+
+ // At this point I'm going to cheat. If it's a typed array already, then I'll create a new one from this one
+
+ // We're going to create a new buffer because that's how we do.
+
+
+ // Previously we added the ability to manipulate a buffer directly. That's not
+ // cool, so what we'll do is create a new buffer. If this is already a TypedArray,
+ // then we'll just create a new TypedArray and replace this*. If we're working
+ // with a Proxy, then we need to re-set
+ JSObject* arr = grabTypedArray(*this);
+ size_t elemSize = js::TypedArray::slotWidth(array_type);
size_t bufferSize = elemSize * number_of_elements;
- js::ArrayBuffer* buffer = arr->buffer;
- buffer->freeStorage(cx());
- buffer->data = data;
- buffer->byteLength = bufferSize;
- buffer->isExternal = true;
+
+ // create the typed array
+ JSObject* newBuf = js_CreateArrayBuffer(cx(), bufferSize);
+ // set it's private data to data
+ newBuf->setPrivate(data);
+
+ // create the new typed array
+ JSObject* newArr = js_CreateTypedArrayWithBuffer(cx(), array_type, newBuf, 0, number_of_elements);
+
+ // JSObject* t = this*;
+
+ // similar to
+ if (js_IsTypedArray(*this)) {
+ *this = newArr;
+ }
+ else if (js::IsProxy(*this)) {
+ js::Value* vp;
+ vp->setObject(*newArr);
+ js::Proxy::set(cx(), *this, *this, proxyProperty(), JS_TRUE, vp);
+ }
}
bool
@@ -618,9 +654,11 @@ void*
Object::GetIndexedPropertiesExternalArrayData()
{
JS_ASSERT(HasIndexedPropertiesInExternalArrayData());
- js::TypedArray* arr = js::TypedArray::fromJSObject(grabTypedArray(*this));
- // XXX: take arr->byteOffset into account?
- return arr->data;
+ JSObject* arr = grabTypedArray(*this);
+ return JS_GetTypedArrayData(arr);
+ // js::TypedArray* arr = js::TypedArray::fromJSObject(grabTypedArray(*this));
+ // // XXX: take arr->byteOffset into account?
+ // return arr->data;
}
@@ -635,8 +673,10 @@ int
Object::GetIndexedPropertiesExternalArrayDataLength()
{
JS_ASSERT(HasIndexedPropertiesInExternalArrayData());
- js::TypedArray* arr = js::TypedArray::fromJSObject(grabTypedArray(*this));
- return arr->byteLength;
+ JSObject* arr = grabTypedArray(*this);
+ return JS_GetTypedArrayByteLength(arr);
+ // js::TypedArray* arr = js::TypedArray::fromJSObject(grabTypedArray(*this));
+ // return arr->byteLength;
}
Object::Object(JSObject *obj) :
diff --git a/js/src/v8api/v8.cpp b/js/src/v8api/v8.cpp
index b01f490..978cc4e 100644
--- a/js/src/v8api/v8.cpp
+++ b/js/src/v8api/v8.cpp
@@ -601,17 +601,17 @@ Handle<Integer> ScriptOrigin::ResourceColumnOffset() const {
//// ScriptData class
ScriptData::~ScriptData() {
if (mScript)
- JS_RemoveObjectRoot(cx(), &mScript);
+ JS_RemoveScriptRoot(cx(), &mScript);
if (mXdr)
JS_XDRDestroy(mXdr);
}
-void ScriptData::SerializeScriptObject(JSObject *scriptObj) {
+void ScriptData::SerializeScriptObject(JSScript *script) {
mXdr = JS_XDRNewMem(cx(), JSXDR_ENCODE);
if (!mXdr)
return;
- if (!JS_XDRScriptObject(mXdr, &scriptObj)) {
+ if (!JS_XDRScript(mXdr, &script)) {
JS_XDRDestroy(mXdr);
mXdr = NULL;
return;
@@ -636,13 +636,13 @@ ScriptData* ScriptData::PreCompile(const char* input, int length) {
if (!sd)
return NULL;
- JSObject *scriptObj = JS_CompileScript(cx(), global,
- input, length, NULL, 0);
- if (!scriptObj)
+ JSScript *script = JS_CompileScript(cx(), global,
+ input, length, NULL, 0);
+ if (!script)
return sd;
if (sd)
- sd->SerializeScriptObject(scriptObj);
+ sd->SerializeScriptObject(script);
return sd;
}
@@ -657,13 +657,13 @@ ScriptData* ScriptData::PreCompile(Handle<String> source) {
if (!sd)
return NULL;
- JSObject *scriptObj = JS_CompileUCScript(cx(), global,
- chars, len, NULL, 0);
- if (!scriptObj)
+ JSScript *script = JS_CompileUCScript(cx(), global,
+ chars, len, NULL, 0);
+ if (!script)
return sd;
if (sd)
- sd->SerializeScriptObject(scriptObj);
+ sd->SerializeScriptObject(script);
return sd;
}
@@ -678,7 +678,7 @@ ScriptData* ScriptData::New(const char* aData, int aLength) {
if (!sd->mScript)
return sd;
- JS_AddObjectRoot(cx(), &sd->mScript);
+ JS_AddNamedScriptRoot(cx(), &sd->mScript, "v8::ScriptData::New");
return sd;
}
@@ -698,11 +698,11 @@ bool ScriptData::HasError() {
return mError;
}
-JSObject* ScriptData::ScriptObject() {
+JSScript* ScriptData::ScriptObject() {
return mScript;
}
-JSObject* ScriptData::GenerateScriptObject(void *aData, int aLen) {
+JSScript* ScriptData::GenerateScriptObject(void *aData, int aLen) {
mXdr = JS_XDRNewMem(cx(), JSXDR_DECODE);
if (!mXdr)
return NULL;
@@ -711,15 +711,15 @@ JSObject* ScriptData::GenerateScriptObject(void *aData, int aLen) {
JSErrorReporter older = JS_SetErrorReporter(cx(), NULL);
JS_XDRMemSetData(mXdr, aData, aLen);
- JSObject *scriptObj;
- JS_XDRScriptObject(mXdr, &scriptObj);
+ JSScript *script;
+ JS_XDRScript(mXdr, &script);
JS_XDRMemSetData(mXdr, NULL, 0);
JS_SetErrorReporter(cx(), older);
JS_XDRDestroy(mXdr);
mXdr = NULL;
- return scriptObj;
+ return script;
}
//////////////////////////////////////////////////////////////////////////////
@@ -727,13 +727,14 @@ JSObject* ScriptData::GenerateScriptObject(void *aData, int aLen) {
JS_STATIC_ASSERT(sizeof(Script) == sizeof(GCReference));
-Script::Script(JSObject *s)
+Script::Script(JSScript *s)
{
- mVal = OBJECT_TO_JSVAL(s);
+ // mVal = OBJECT_TO_JSVAL(s);
+ mVal = OBJECT_TO_JSVAL(JS_GetObjectFromScript(s));
}
-Script::operator JSObject *() {
- return JSVAL_TO_OBJECT(mVal);
+Script::operator JSScript *() {
+ return static_cast<JSScript*>(JSVAL_TO_PRIVATE(mVal));
}
Handle<Object> Script::InternalObject() {
@@ -742,7 +743,7 @@ Handle<Object> Script::InternalObject() {
}
Local<Script> Script::Create(Handle<String> source, ScriptOrigin *origin, ScriptData *preData, Handle<String> scriptData, bool bindToCurrentContext) {
- JSObject* s = NULL;
+ JSScript* s = NULL;
if (preData)
s = preData->ScriptObject();
diff --git a/js/src/v8api/v8.h b/js/src/v8api/v8.h
index 258f2e3..9f8fd9c 100644
--- a/js/src/v8api/v8.h
+++ b/js/src/v8api/v8.h
@@ -934,14 +934,15 @@ class ScriptData {
JS_DECLARE_ALLOCATION_FRIENDS_FOR_PRIVATE_CONSTRUCTOR;
ScriptData() : mXdr(NULL), mData(NULL), mLen(0), mError(true) {}
- void SerializeScriptObject(JSObject *scriptObj);
- JSObject* GenerateScriptObject(void *data, int len);
+ //XXXzpao should we drop "Object" from these?
+ void SerializeScriptObject(JSScript *script);
+ JSScript* GenerateScriptObject(void *data, int len);
JSXDRState *mXdr;
const char *mData;
uint32 mLen;
bool mError;
- JSObject *mScript;
+ JSScript *mScript;
public:
~ScriptData();
static ScriptData* PreCompile(const char* input, int length);
@@ -951,15 +952,15 @@ public:
const char* Data();
bool HasError();
protected:
- JSObject* ScriptObject();
+ JSScript* ScriptObject();
friend class Script;
};
class Script : public internal::GCReference {
- Script(JSObject *s);
+ Script(JSScript *s);
- operator JSObject *();
+ operator JSScript *();
Handle<Object> InternalObject();
static Local<Script> Create(Handle<String> source, ScriptOrigin *origin, ScriptData *preData, Handle<String> scriptData, bool bindToCurrentContext);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment