Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Fixed: Ivars with accessors in a Category failed with duplicate ivar error #2072

merged 2 commits into from

6 participants


This fixed issue #2063


Milestone: Someday. Label: #new. What's next? A reviewer should examine this issue.


Do we have a test for what this is fixing?


We now have a test case for this


This seems to be ready to commit?



Milestone: Someday. Label: #ready-to-commit. What's next? The changes for this issue are ready to be committed by a member of the core team.


Hmm so we allow ivars through categories? It's a little spooky.


I kind of agree with you. We can't do it like this in Obj-c (but with another way). But in the same time, that's the "power" of javascript...


It would reduce portability between Objective-C and Objective-J in a kind of quirky way. Imagine a source to source translator. Now it would have to know all categories before outputting an Objective-C class since it'd have to move ivars to there.

On the other hand direct portability has never been a huge goal.


This is how Objective-J has worked from the beginning. In Objective-C you have properties that can be declared on a category. We don't have that here. You can look at this as a workaround. I kind of like it.


You can do it in Objective-C, but you need to implement the method of the accessors as well (with playing with the runtime). Not the case in Obj-J yet, even easier because JS...but different.

I personally think we should stay the closest possible of Objective-C but it doesn't depend on me only

@primalmotion primalmotion merged commit 371b660 into cappuccino:master

1 check passed

Details continuous-integration/travis-ci The Travis CI build passed

+#fixed thanks



@cappbot cappbot added #fixed and removed #ready-to-commit labels

Milestone: Someday. Label: #fixed. What's next? This issue is considered successfully resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 21, 2014
  1. @mrcarlberg
Commits on Apr 17, 2014
  1. @mrcarlberg

    Fixed: Added test case

    mrcarlberg authored
This page is out of date. Refresh to see the latest.
19 Objective-J/ObjJAcornCompiler.js
@@ -1663,6 +1663,8 @@ ClassDeclarationStatement: function(node, st, c) {
compiler.currentSuperMetaClass = "objj_getMetaClass(\"" + className + "\").super_class";
var firstIvarDeclaration = true,
+ ivars = classDef.ivars,
+ classDefIvars = [],
hasAccessors = false;
// Then we add all ivars
@@ -1672,7 +1674,6 @@ ClassDeclarationStatement: function(node, st, c) {
var ivarDecl = node.ivardeclarations[i],
ivarType = ivarDecl.ivartype ? : null,
ivarName =,
- ivars = classDef.ivars,
ivar = {"type": ivarType, "name": ivarName},
accessors = ivarDecl.accessors;
@@ -1694,7 +1695,10 @@ ClassDeclarationStatement: function(node, st, c) {
if (ivarDecl.outlet)
ivar.outlet = true;
- ivars[ivarName] = ivar;
+ // Store the classDef ivars into array and add them later when accessors are created to prevent ivar duplicate error when generating accessors
+ classDefIvars.push(ivar);
if (!classScope.ivars)
classScope.ivars = Object.create(null);
classScope.ivars[ivarName] = {type: "ivar", name: ivarName, node:, ivar: ivar};
@@ -1784,7 +1788,16 @@ ClassDeclarationStatement: function(node, st, c) {
- // We will store the classDef first after accessors are done so we don't get a duplicate class error
+ // We will store the ivars into the classDef first after accessors are done so we don't get a duplicate ivars error when generating accessors
+ for (var ivarSize = classDefIvars.length, i = 0; i < ivarSize; i++) {
+ var ivar = classDefIvars[i],
+ ivarName =;
+ // Store the ivar into the classDef
+ ivars[ivarName] = ivar;
+ }
+ // We will store the classDef first after accessors are done so we don't get a duplicate class error when generating accessors
compiler.classDefs[className] = classDef;
var bodies = node.body,
11 Tests/Objective-J/Preprocessor/OutputTests/Class/accessors.j
@@ -0,0 +1,11 @@
+@implementation Class
+@implementation Class (Accessors)
+ Type ivar @accessors;
13 Tests/Objective-J/Preprocessor/OutputTests/Class/accessors.js
@@ -0,0 +1,13 @@
+var the_class=objj_allocateClassPair(Nil,"Class"),meta_class=the_class.isa;
+var the_class=objj_getClass("Class");
+throw new SyntaxError("*** Could not find definition for class \"Class\"");
+var meta_class=the_class.isa;
+class_addIvars(the_class,[new objj_ivar("ivar")]);
+class_addMethods(the_class,[new objj_method(sel_getUid("ivar"),function $Class__ivar(_1,_2){
+return _1.ivar;
+},["Type"]),new objj_method(sel_getUid("setIvar:"),function $Class__setIvar_(_3,_4,_5){
1  Tests/Objective-J/Preprocessor/OutputTests/OutputTest.j
@@ -6,6 +6,7 @@ var FILENAMES = [
+ "Class/accessors",
Something went wrong with that request. Please try again.